[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/core/include/ -> commonDB.php (source)

   1  <?php
   2  /* 

   3   * phpMyVisites : website statistics and audience measurements

   4   * Copyright (C) 2002 - 2006

   5   * http://www.phpmyvisites.net/ 

   6   * phpMyVisites is free software (license GNU/GPL)

   7   * Authors : phpMyVisites team

   8  */
   9  
  10  // $Id: commonDB.php 236 2007-11-04 15:04:08Z matthieu_ $

  11  
  12  
  13  // connnexion BDD

  14  
  15  /*define('DB_HOST', 'localhost'); 

  16  define('DB_LOGIN', 'root');

  17  define('DB_PASSWORD', 'nintendo');

  18  define('DB_NAME', 'v2');

  19  */
  20  
  21  define ("KEY_PHPMV", "PHPMV");
  22  
  23  class Db
  24  {
  25      var $host;
  26      var $login;
  27      var $password;
  28      var $name;
  29      var $state;
  30      var $allInstalled;
  31      var $pluginKey;
  32          
  33      function Db($p_PluginKey)
  34      {
  35          $this->pluginKey = $p_PluginKey;
  36      }
  37      
  38      /**

  39       * Singleton

  40       */
  41      function &getInstance($p_PluginKey = KEY_PHPMV)
  42      {
  43           static $PluginTabInstance;
  44          if (!isset($PluginTabInstance)){
  45              $PluginTabInstance = array();
  46          }
  47          if (!isset($PluginTabInstance[$p_PluginKey])) {
  48              $c = __CLASS__;
  49              $instance = new $c($p_PluginKey);
  50              $PluginTabInstance[$p_PluginKey] =& $instance;
  51          }
  52          else {
  53              $instance =& $PluginTabInstance[$p_PluginKey];
  54          }
  55          return $instance;
  56      }
  57      
  58  	function connect()
  59      {
  60          $this->host = DB_HOST;
  61          $this->login = DB_LOGIN;
  62          $this->password = DB_PASSWORD;
  63          $this->name = DB_NAME;
  64          $this->init();
  65      }
  66      
  67  	function init()
  68      {
  69          $this->connection = @mysql_connect(
  70                              $this->host,
  71                              $this->login,
  72                              $this->password
  73                          );
  74      
  75          
  76          if(!$this->connection) 
  77          { 
  78              $this->state = false;
  79              $GLOBALS['header_error_message_tpl'] = "'".$this->host ."' 
  80                              database connection error! <br/>".mysql_error() ;
  81              
  82          } 
  83          else
  84          {
  85              
  86              // sélection BDD

  87              $selection = @mysql_select_db( $this->name, $this->connection);
  88              if(!$selection) 
  89              { 
  90                  $this->state = false;
  91                  $GLOBALS['header_error_message_tpl'] = "'". $this->name ."' database selection error! <br/>".mysql_error() . "<br>" ; 
  92              }
  93              else
  94              {
  95                  $this->state = true;
  96              }
  97          }
  98          return true;
  99      }
 100      
 101  	function close()
 102      {
 103          mysql_close($this->connection);
 104      }
 105      
 106  	function isReady()
 107      {
 108          return $this->state;
 109      }
 110  
 111  	function getInstallSqlFile() {
 112          if ($this->pluginKey == KEY_PHPMV) {
 113              return INCLUDE_PATH . "/core/include/installSql.php";
 114          }
 115          else {
 116              return PLUGINS_PATH.$this->pluginKey."/updates/installSql.php";
 117          }
 118      }
 119  
 120  	function getUpdatesDir() {
 121          if ($this->pluginKey == KEY_PHPMV) {
 122              return INCLUDE_PATH . "/core/include/updates/";
 123          }
 124          else {
 125              return PLUGINS_PATH.$this->pluginKey."/updates/";
 126          }
 127      }
 128      
 129  	function getPluginPrefix() {
 130          if ($this->pluginKey == KEY_PHPMV) {
 131              return DB_TABLES_PREFIX;
 132          }
 133          else {
 134              return DB_TABLES_PREFIX.$this->pluginKey."_";
 135          }
 136      }
 137      
 138  	function getConstantTableName ($name) {
 139          if ($this->pluginKey == KEY_PHPMV) {
 140              return 'T_' . strtoupper($name);
 141          }
 142          else {
 143              return 'T_' . strtoupper($this->pluginKey."_".$name);
 144          }
 145      }
 146      
 147  	function getAllTablesList()
 148      {
 149          $create = array();
 150          $tmpFile = $this->getInstallSqlFile();
 151          if (is_file($tmpFile)) {
 152              if ($this->pluginKey != KEY_PHPMV) {
 153                  $prefixPlugin = $this->getPluginPrefix();
 154              }
 155              require $tmpFile;
 156          }
 157          return array_keys($create);
 158      }
 159      
 160  	function areAllTablesInstalled()
 161      {
 162          return sizeof($this->getAllInstalledTables()) === sizeof($this->getAllTablesList());
 163      }
 164      
 165  	function getAllInstalledTables()
 166      {
 167          if( !defined('DB_TABLES_PREFIX') ) 
 168              return array();
 169              
 170          if(!isset($this->allInstalled) )
 171          {
 172              $all = $this->getAllTablesList();
 173              $prefixedTables = array();
 174              foreach($all as $name)
 175              {
 176                  $prefixedTables[] = $this->getPluginPrefix() . $name;
 177              }
 178  
 179              $r = query('SHOW TABLES');
 180              
 181              while($l = mysql_fetch_row($r))
 182              {
 183                  $name = $l[0];
 184                  
 185                  if(in_array($name, $prefixedTables))
 186                  { 
 187                      $this->allInstalled[] = $l[0];
 188                  }
 189              }
 190          }
 191          return $this->allInstalled;
 192      }
 193      
 194  	function eraseExistingTables()
 195      {
 196          foreach($this->allInstalled as $name)
 197          {
 198              $r = query("DROP TABLE `$name`");
 199          }
 200      }
 201      
 202  	function createAllTables($p_version = PHPMV_VERSION)
 203      {
 204          $create = array();
 205          $createRecords = array();
 206          //print("create tables!");

 207          $tmpFile = $this->getInstallSqlFile();
 208          if (is_file($tmpFile)) {
 209              if ($this->pluginKey != KEY_PHPMV) {
 210                  $prefixPlugin = $this->getPluginPrefix();
 211              }
 212              require $tmpFile;
 213          }
 214          
 215          foreach($create as $sqlCode)
 216          {            
 217              $r = query($sqlCode);
 218          }
 219          foreach($createRecords as $sqlCode)
 220          {            
 221              $r = query($sqlCode);
 222          }
 223          
 224          $this->updateVersion($p_version);
 225      }
 226      
 227  	function updateTables ($p_version = PHPMV_VERSION) {
 228          $dirUpdates = $this->getUpdatesDir();
 229          $oldVersion = $this->getVersion();
 230          
 231          if($dh = opendir( $dirUpdates ))
 232          {
 233              while(($file = readdir($dh)) !== false)
 234              {
 235                  if($file != "update-currentversion.php" 
 236                  && preg_match("/^update-(.*)\.php$/", $file, $matches))
 237                  {
 238                      if(version_compare( $oldVersion, $matches[1] ) == -1)
 239                      {
 240                          require $dirUpdates . $matches[0];
 241                      }
 242                  }
 243              }
 244              closedir($dh);
 245          }
 246          
 247          $this->setVersion($p_version);
 248      }
 249      
 250  	function defineTables()
 251      {
 252          if(defined('DB_TABLES_PREFIX'))
 253          {
 254              $tables = $this->getAllTablesList();
 255              foreach($tables as $name)
 256              {
 257                  if (! defined($this->getConstantTableName ($name))) {
 258                      define($this->getConstantTableName ($name), $this->getPluginPrefix() . $name);
 259                  }
 260              }
 261          }
 262      }
 263  
 264  	function updateVersion($p_version = PHPMV_VERSION)
 265      {
 266          if ($this->pluginKey == KEY_PHPMV) {
 267              $r = query("UPDATE ".T_VERSION."
 268                          SET `version` = '".PHPMV_VERSION."' 
 269                          LIMIT 1");
 270          }
 271          else {
 272          $r = query("UPDATE ".T_PLUGIN_VERSION."
 273                      SET `version` = '".$p_version."' 
 274                      WHERE code = '".$this->pluginKey."'");
 275          }
 276      }
 277      
 278  	function setVersion( $p_version )
 279      {
 280          if ($this->pluginKey == KEY_PHPMV) {
 281              $r = query("DELETE FROM ".T_VERSION);
 282              $r = query("INSERT INTO ".T_VERSION." (version)
 283                          VALUES ('".$p_version."')");
 284          }
 285          else {
 286              $r = query("DELETE FROM ".T_PLUGIN_VERSION." WHERE code = '".$this->pluginKey."'");
 287              $r = query("INSERT INTO ".T_PLUGIN_VERSION." (code, version)
 288                          VALUES ('".$this->pluginKey."', '".$p_version."')");
 289          }
 290      }
 291      
 292  	function getVersion()
 293      {
 294          if(!isset($this->version))
 295          {
 296              $this->loadVersion();
 297          }
 298          return $this->version;
 299      }
 300      
 301  	function loadVersion()
 302      {
 303          if ($this->pluginKey == KEY_PHPMV) {
 304              $r = query("SELECT version
 305                          FROM ".T_VERSION);
 306          }
 307          else {
 308              $r = query("SELECT version
 309                          FROM ".T_PLUGIN_VERSION." WHERE code = '".$this->pluginKey."'");
 310          }
 311                      
 312          $rr = mysql_fetch_assoc($r);
 313          
 314          $return = $rr['version'];
 315          
 316          if ($this->pluginKey == KEY_PHPMV) {
 317              $this->version = empty($return) ? '2.0beta1' : $return;
 318          }
 319          else {
 320              $this->version = empty($return) ? '0.1beta1' : $return;
 321          }
 322      }
 323  }
 324  
 325  
 326  /**

 327   * performs a query to the database and manage errors

 328   * 

 329   * @param string $query SQL Query

 330   * @param int $line line of the file where the query is called

 331   * 

 332   * @return resource mysql_query

 333   */
 334  function query($query)
 335  {
 336      $db =& Db::getInstance();
 337      if($db->isReady())
 338      {
 339          if(!isset($GLOBALS['query_log'][$query]))
 340          {
 341              $GLOBALS['query_log'][$query]=0;
 342          }
 343          $GLOBALS['query_log'][$query]++;
 344          if(!isset($GLOBALS['query_count']))
 345          {
 346              $GLOBALS['query_count'] = 0;
 347          }
 348          
 349          if(!isset($GLOBALS['total_time_query']))
 350          {
 351                  $GLOBALS['total_time_query'] = 0;
 352          }
 353          $GLOBALS['query_count']++;
 354          
 355          $beg = getMicrotime();
 356          if(PRINT_QUERY)
 357          {
 358              $a_d = debug_backtrace();
 359              printDebug("<hr><i>line  : ".$a_d[0]['line']." in file ".$a_d[0]['file']."</i><br>".$query."<br>");
 360          }    
 361          
 362          $r = mysql_query($query);
 363          
 364          $end = getMicrotime()-$beg;
 365          $res = getMicrotime()-$GLOBALS['time_start'];
 366          $GLOBALS['total_time_query'] += $end;
 367          
 368          if(PRINT_QUERY)
 369          {
 370              if($end>TIME_SLOW_QUERY)
 371              {
 372                  $GLOBALS['slow_queries'][] = $query."<br><b>$end s</b>";
 373              }
 374              printDebug("Total time : <b>$res</b> | Query Time <b>".substr($end, 0, 6)."</b> sec<br><br>");
 375          }
 376          
 377          if($r)
 378          {
 379              return $r;
 380          }
 381          else
 382          {        
 383              print(mysql_error()."</b>");
 384              print("<br><br>Query : ".$query);
 385              exit();
 386              return;
 387          }
 388      }
 389      return false;
 390  }
 391  
 392  // $arr =

 393  // field name => value

 394  
 395  // $idField = idfieldname

 396  function updateLine($table, $a_fieldNamesValues, $idFieldName)
 397  {
 398      $values = array();
 399      foreach($a_fieldNamesValues as $key => $val)
 400      {                
 401          if ((isset($key) && isset($val)) && (!empty($key) && ($val == 0 || !empty($val)) ))
 402          {
 403              $values[] = "$key = '$val'";
 404          }
 405      }
 406      if ( !count($values) ) 
 407      {
 408          return false;
 409      }
 410      $query = "UPDATE $table 
 411                  SET ".implode(", ", $values)." 
 412                  WHERE $idFieldName = '".$a_fieldNamesValues[$idFieldName]."' 
 413                  LIMIT 1";
 414      $r = query( $query );
 415      if(mysql_affected_rows() === 0)
 416      {
 417          //print(mysql_error()." <= Requete UPDATE n'affecte aucune ligne ! <br><b>$query</b><br><br>");

 418          //exit;

 419      }
 420      return $r;
 421  }
 422  
 423  function insertLine( $table, $a_fieldNames, $a_values )
 424  {
 425      $r = query("INSERT INTO $table (".implode(", ", $a_fieldNames).")
 426                  VALUES ('".implode("','", $a_values)."')
 427                  ");
 428      return mysql_insert_id();
 429  }
 430  
 431  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics