linea21-core
[ class tree: linea21-core ] [ index: linea21-core ] [ all elements ]

Source for file class.mysql.php

Documentation is available at class.mysql.php

  1. <?php
  2. /**
  3.  * @package linea21.core
  4.  * @subpackage system
  5.  * @author linea21 <info@linea21.com>
  6.  * @version $id SVN
  7.  * @access public
  8.  * @license http://opensource.org/licenses/gpl-3.0.html
  9.  *  MySQL data manipulation
  10.  */
  11.  
  12. class mysql {
  13.   /*
  14.    * @parameters
  15.    */
  16.   var $DB_HOST = DB_HOST;
  17.   var $DB_USER = DB_USER;
  18.   var $DB_PASS = DB_PASS;
  19.   var $DB_NAME = DB_NAME;
  20.   var $DB_LINK = "";
  21.   var $DB_ADMINMAIL = DB_ADMINMAIL;
  22.   var $RESULT;
  23.   var $DEBUG;
  24.   protected $dispatcher = null;
  25.   
  26.   public function __construct()
  27.   {
  28.       $this->dispatcher = $GLOBALS['dispatcher'];
  29.   }
  30.  
  31.  
  32.   /**
  33.    * mysql::DBInitialise()
  34.    * Initialisation de la connexion
  35.    *
  36.    * @access public
  37.    * @param string $user : utilisateur base de données
  38.    * @param string $pass : password utilisateur base de données
  39.    * @param string $serveur : serveur de base de données
  40.    * @param string $bdd : nom de la base de données
  41.    * @return boolean 
  42.    */
  43.  
  44.   function DBInitialise($user ''$pass ''$serveur ''$bdd '')
  45.   {
  46.       // Notify the end of the current method
  47.       $this->dispatcher->notify(new sfEvent($this'dbconnector.init'array('user' => $user'password' => $pass'host' => $serveur'db' => $bdd)));
  48.       
  49.     if ($user != ''{
  50.       $this->DB_USER = $user;
  51.     }
  52.     if ($pass != ''{
  53.       $this->DB_PASS = $pass;
  54.     }
  55.     if ($serveur != ''{
  56.       $this->DB_HOST = $serveur;
  57.     }
  58.     if ($bdd != ''{
  59.       $this->DB_NAME = $bdd;
  60.     }
  61.     return true;
  62.   }
  63.  
  64.   /**
  65.    * mysql::DBConnexion()
  66.    * connexion Base de données
  67.    *
  68.    * @access public
  69.    * @param none 
  70.    * @return boolean 
  71.    */
  72.  
  73.   function DBConnexion()
  74.   {
  75.  
  76.       // Notify the end of the current method
  77.       $this->dispatcher->notify(new sfEvent($this'dbconnector.connect'));
  78.        
  79.     $this->DB_LINK = @mysqli_connect($this->DB_HOST$this->DB_USER$this->DB_PASS$this->DB_NAMEor die($this->DBError(_t('divers','sql_connect_failed')));
  80. //     $this->DBSelectDB();
  81.  
  82.     if(defined('MOD_DEBUG'&& MOD_DEBUG == true && isset($GLOBALS['Dbg'])) {
  83.       global $Dbg;
  84.  
  85.       // $Dbg->queryRel('Connecting to <i>'.SQL.'</i> DATABASE [<b>'.$this->DB_NAME.'</b>] dns: '.$this->DB_USER.':'.$this->DB_PASS.'@'.DB_HOST);
  86.       $Dbg->stopTimer();
  87.     }
  88.  
  89.     if(strtolower(CHARSET== 'utf-8'$this->DBQuery("SET NAMES UTF8");
  90.     return true;
  91.   }
  92.  
  93.   /**
  94.    * mysql::DBSelectDB()
  95.    * Sélection de la Base de données
  96.    *
  97.    * @access public
  98.    * @param none 
  99.    * @return boolean 
  100.    */
  101.  
  102.   function DBSelectDB()
  103.   {
  104.       // Notify the end of the current method
  105.       $this->dispatcher->notify(new sfEvent($this'dbconnector.selectDB'));
  106.     mysqli_select_db($this->DB_NAME$this->DB_LINKor die($this->DBError(_t('divers','db_connect_failed')));
  107.     return true;
  108.   }
  109.  
  110.   /**
  111.    * mysql::DBError()
  112.    * Gestion des erreurs MySQL
  113.    *
  114.    * @access private
  115.    * @param string $message_err : message retourné par la requête
  116.    * @param string $query : requête provoquant l'erreur
  117.    * @return boolean 
  118.    */
  119.   function DBError($message_err$query = -1)
  120.   {
  121.       // Notify the end of the current method
  122.       $this->dispatcher->notify(new sfEvent($this'dbconnector.error'));
  123.  
  124.     $erreur $message_err "<br />[<em> error n° " @mysqli_errno$this->DB_LINK " : " @mysqli_error$this->DB_LINK "</em> ]<br />\n\n";
  125.     if ($query != -1{
  126.       $this->_logDbError($query);
  127.       $erreur .= "SQL query : $query<br />\n";
  128.     }
  129.     $date date("[D d/M/y H:i:s]<br />\n");
  130.     $erreur .= $date PHP_EOL;
  131.     $erreur .= "running script :" $_SERVER["SCRIPT_NAME"getHttpParameters()"<br />" PHP_EOL;;
  132.     $erreur .= "website : " .SITE_ROOT_URL PHP_EOL;
  133.     if(defined('MOD_DEBUG'&& MOD_DEBUG == true{
  134.       echo $erreur;
  135.     }
  136.     if ($this->DB_ADMINMAIL != -1@error_log ($erreur1$this->DB_ADMINMAIL);
  137.   }
  138.  
  139.   /**
  140.    * mysql::DBInsert()
  141.    * Insertion de données dans la BDD
  142.    *
  143.    * @access public
  144.    * @param string $query : requête SQL
  145.    * @param string $returnid : Si $returnid==1 ==> renvoie last_id()
  146.    * @return integer || boolean : $result last_id()
  147.    */
  148.  
  149.   function DBInsert ($query$returnid = -1)
  150.   {
  151.       // Notify the end of the current method
  152.       $this->dispatcher->notify(new sfEvent($this'dbconnector.insert'array('query' => $query'lastid' => $returnid)));
  153.       
  154.     if (!($this->RESULT = $this->DBQuery($query))) return false;
  155.     // Si returnid=1 on renvoie l'id de l'enregistrement
  156.     // A utiliser uniquement si attribut auto_increment
  157.     if ($returnid == 1{
  158.       $res mysqli_insert_id($this->DB_LINK);
  159.     else {
  160.       $res true;
  161.     }
  162.     return $res;
  163.   }
  164.  
  165.   /**
  166.    * mysql::DBQuery()
  167.    * effectue une requête en tous genres -UPDATE - DELETE - SELECT
  168.    *
  169.    * @access public
  170.    * @param string $query : requête SQL
  171.    * @return boolean : $result
  172.    */
  173.   function DBQuery ($query)
  174.   {
  175.  
  176.       // Notify the end of the current method
  177.       $this->dispatcher->notify(new sfEvent($this'dbconnector.query'array('query' => $query)));
  178.       
  179.     if(defined('MOD_DEBUG'&& MOD_DEBUG == true && isset($GLOBALS['Dbg'])) {
  180.       global $Dbg;
  181.  
  182.       $this->_logDbQuery($query);
  183.       $Dbg->stopTimer();
  184.       $Dbg->query($query);
  185.       $Dbg->stopTimer();
  186.     }
  187.     $this->RESULT = mysqli_query($this->DB_LINK$queryor die($this->DbError(_t('divers','sql_query_failed')$query));
  188.  
  189.     return $this->RESULT;
  190.   }
  191.  
  192.   /**
  193.    * mysql::DBSelect()
  194.    * Requêtes - SELECT
  195.    *
  196.    * @access public
  197.    * @param string $query : requête SQL
  198.    * @param string $fetch : renvoie des données, valeur ASSOC (default), ARRAY, OBJECT
  199.    * @return array : $result
  200.    */
  201.  
  202.   function DBSelect ($query$fetch 'ASSOC')
  203.   {
  204.  
  205.       // Notify the end of the current method
  206.       $this->dispatcher->notify(new sfEvent($this'dbconnector.select'array('query' => $query'type' => $fetch)));
  207.       
  208.     if (!($this->RESULT = $this->DBQuery($query))) {
  209.       @mysqli_free_result($this->RESULT);
  210.       return false;
  211.     }
  212.     if ($fetch == 'ARRAY'{
  213.       $i 0;
  214.       while ($data @mysqli_fetch_array($this->RESULT)) {
  215.         $table[$i$data;
  216.         $i++;
  217.       }
  218.     }
  219.     if ($fetch == 'OBJECT'{
  220.       $i 0;
  221.       while ($data @mysqli_fetch_object($this->RESULT)) {
  222.         $table[$i$data;
  223.         $i++;
  224.       }
  225.     }
  226.     if ($fetch == 'ASSOC'{
  227.       $i 0;
  228.       while ($data @mysqli_fetch_assoc($this->RESULT)) {
  229.         $table[$i$data;
  230.         $i++;
  231.       }
  232.     }
  233.  
  234.     if (!isset($table)) $table 0;
  235.     return $table;
  236.   }
  237.  
  238.   /**
  239.    * mysql::DBescape()
  240.    * Echappement  des variables
  241.    *
  242.    * @access public
  243.    * @param string/array $input 
  244.    * @return string/array : $output
  245.    */
  246.  
  247.   function DBescape ($input)
  248.   {
  249.  
  250.       // Filter data event + return value
  251.       $r $this->dispatcher->filter(new sfEvent($this'dbconnector.escape'array('data' => $input))$input);
  252.       $input $r->getReturnValue();
  253.       
  254.     if(is_array($input)) {
  255.  
  256.       $output array();
  257.       
  258.       foreach ($input as $key => $value{
  259.         if(is_string($value)) {
  260.           if (get_magic_quotes_gpc()) $value=stripslashes($value);
  261.           $output[$keymysqli_real_escape_string($this->DB_LINK$value);
  262.         else {
  263.           $output[$key=  $value;
  264.         }
  265.       }
  266.     else {
  267.       
  268.       // it is a simple string
  269.       if(is_string($input)) {
  270.         if (get_magic_quotes_gpc()) $input=stripslashes($input);
  271.         $output mysqli_real_escape_string($this->DB_LINK$input);
  272.       else {
  273.         $output =  $input;
  274.       }
  275.     }
  276.     return $output;
  277.      
  278.   }
  279.  
  280.   /**
  281.    * mysql::DBaffectedRow()
  282.    * Retourne le nombre d'enregistrements affecté par la dernière requête
  283.    *
  284.    * @access public
  285.    * @return integer : $res
  286.    */
  287.   function DBaffectedRow()
  288.   {
  289.       // Notify the end of the current method
  290.       $this->dispatcher->notify(new sfEvent($this'dbconnector.affected_rows'));
  291.       
  292.     $res mysqli_affected_rows($this->DB_LINK);
  293.     return $res;
  294.   }
  295.  
  296.   /**
  297.    * _logDbQuery()
  298.    * @access private
  299.    * @param string $q 
  300.    * @return void 
  301.    */
  302.   function _logDbQuery($q)
  303.   {
  304.       // Filter data event + return value
  305.       $r $this->dispatcher->filter(new sfEvent($this'dbconnector.log_query'array('query' => $q))$q);
  306.       $q $r->getReturnValue();
  307.       
  308.     if(defined('SQL_LOG_DEBUG'&& SQL_LOG_DEBUG == 1{
  309.  
  310.       $q str_replace(array("\r""\n""\t"),"",$q);
  311.       logfile(LOG_SQL_QUERIESarray($qget_class()));
  312.  
  313.     }
  314.   }
  315.  
  316.   /**
  317.    * _logDbError()
  318.    * @access private
  319.    * @param string $q 
  320.    * @return void 
  321.    */
  322.   function _logDbError($q)
  323.   {
  324.       // Filter data event + return value
  325.       $r $this->dispatcher->filter(new sfEvent($this'dbconnector.log_error'array('query' => $q))$q);
  326.       $q $r->getReturnValue();
  327.       
  328.     $q str_replace(array("\r""\n""\t"),"",$q);
  329.     logfile(LOG_SQL_ERRORSarray($q$_SERVER["SCRIPT_NAME"]get_class()));
  330.   }
  331.   /**
  332.    * mysql::DBclose()
  333.    * Retourne le nombre d'enregistrements affecté par la dernière requête
  334.    *
  335.    * @access public
  336.    * @param string $query : requête SQL
  337.    * @return boolean : $result
  338.    */
  339.   function DBClose()
  340.   {
  341.       // Notify the end of the current method
  342.       $this->dispatcher->notify(new sfEvent($this'dbconnector.close'));
  343.       
  344.     @mysqli_close($this->DB_LINK);
  345.   }
  346. }
  347.  
  348. ?>

Documentation generated on Thu, 20 Mar 2014 16:46:14 +0100 by phpDocumentor 1.4.1