Changeset 712 for trunk/inc


Ignore:
Timestamp:
Jul 30, 2013, 11:34:19 PM (11 years ago)
Author:
chronos
Message:
  • Upraveno: Aktualizace inicializačního skriptu pro sql databázi na revizi 710.
  • Upraveno: Aktualizována třída Database na novější využívající třídu PDO a generující výjímky při chybě, které je pak nutno očetřit.
Location:
trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        55minimanager
        66mmfpm
         7.settings
         8.buildpath
         9.project
  • trunk/inc/config.sample.php

    r690 r712  
    1919    'UDBRevision' => '204',
    2020    'ScriptDev2Revision' => '137',
    21     'ClientVersion' => '3.1.3',
     21    'ClientVersion' => '3.3.5a',
    2222    'DatabaseHost' => 'localhost',
    2323    'DatabaseUser' => 'server1',
     
    3838  (
    3939    'Charset' => 'utf-8',
    40     'Host' => 'george-virt.zdechov.net',
     40    'Host' => 'localhost',
    4141    'Description' => 'Neoficiální herní server hry World of Warcraft',
    4242    'Keywords' => 'wowserver, world of warcraft, free, wow, server, hof, heroes of fantasy, zdechov, mangos',
     
    5151    'ShowPHPError' => false,
    5252    'ServerFounded' => '1.1.2000',
    53     'BankAccount' => '670100-2202937132/6210',
     53    'BankAccount' => '',
    5454    'TableRowPerPage' => 20,
    5555    'DefaultRealmIndex' => 1,
  • trunk/inc/database.php

    r597 r712  
    22
    33// Extended database class
    4 // Date: 2009-02-16
    5 
    6 class Database extends mysqli
    7 {
    8   var $Prefix = '';
     4// Date: 2011-11-25
     5
     6
     7class DatabaseResult
     8{
     9  var $PDOStatement;
     10  var $num_rows = 0;
     11 
     12  function fetch_assoc()
     13  {
     14    return($this->PDOStatement->fetch(PDO::FETCH_ASSOC));
     15  }
     16 
     17  function fetch_array()
     18  {
     19    return($this->PDOStatement->fetch(PDO::FETCH_BOTH));
     20  }
     21
     22  function fetch_row()
     23  {
     24    return($this->PDOStatement->fetch(PDO::FETCH_NUM));
     25  }
     26}
     27
     28class Database
     29{
     30  var $Prefix;
     31  var $Functions;
     32  var $Type; 
     33  var $PDO;
     34  var $Error;
     35  var $insert_id;
     36  var $LastQuery;
     37  var $ShowSQLError;
     38  var $ShowSQLQuery;
     39  var $LogFile;
     40 
     41  function __construct()
     42  {   
     43        $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
     44        $this->Type = 'mysql'; // mysql, pgsql
     45        $this->ShowSQLError = false;
     46        $this->ShowSQLQuery = false;
     47        $this->LogSQLQuery = false;
     48        $this->LogFile = dirname(__FILE__).'/../Query.log';
     49  }
     50 
     51  function Connect($Host, $User, $Password, $Database)
     52  {   
     53    if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
     54      else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
     55      else $ConnectionString = '';
     56    $this->PDO = new PDO($ConnectionString, $User, $Password);
     57  }
     58 
     59  function select_db($Database)
     60  {
     61    $this->query('USE `'.$Database.'`');
     62  }
    963 
    1064  function query($Query)
    1165  {
    12           global $Config;
    13        
    14           if($Config['Web']['ShowSQLQuery'] == true)
    15             echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.'</div>');
    16           $Result = parent::query($Query);
    17     if(($this->error != '') and ($Config['Web']['ShowSQLError'] == true))
    18             echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>');
    19 
     66    $this->LastQuery = $Query;
     67    if($this->ShowSQLQuery == true)
     68      echo('<div style="border-bottom-width: 1px; border-bottom-style: solid; padding-bottom: 3px; padding-top: 3px; font-size: 12px; font-family: Arial;">'.$Query.'</div>'."\n");
     69    if($this->LogSQLQuery == true)
     70      file_put_contents($this->LogFile, $Query."\n", FILE_APPEND);
     71    $Result = new DatabaseResult();
     72    $Result->PDOStatement = $this->PDO->query($Query);
     73    if($Result->PDOStatement)
     74    {
     75      $Result->num_rows = $Result->PDOStatement->rowCount();
     76      $this->insert_id = $this->PDO->lastInsertId();
     77    } else
     78    {
     79      $this->Error = $this->PDO->errorInfo();
     80      $this->Error = $this->Error[2];
     81      if(($this->Error != '') and ($this->ShowSQLError == true))
     82        echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
     83        throw new Exception('SQL Error: '.$this->Error);
     84    }
    2085    return($Result); 
    2186  }
    2287
    2388  function select($Table, $What = '*', $Condition = 1)
    24   {
     89  {   
    2590    return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 
    2691  }
     
    37102    foreach($Data as $Key => $Value)
    38103    {
    39       $Value = strtr($Value, '"', '\"');
    40104      $Name .= ',`'.$Key.'`';
    41             if($Value == 'NOW()') $Values .= ','.$Value;
    42 else if($Value == 'UUID()') $Values .= ','.$Value;
    43               else $Values .= ",'".$Value."'";
     105      if(!in_array($Value, $this->Functions))
     106      {
     107        if(is_null($Value)) $Value = 'NULL';
     108        else $Value = $this->PDO->quote($Value);
     109      }
     110      $Values .= ','.$Value;
    44111    }
    45112    $Name = substr($Name, 1);
    46113    $Values = substr($Values, 1);
    47114    $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
     115    $this->insert_id = $this->PDO->lastInsertId();
    48116  }
    49117 
     
    53121    foreach($Data as $Key => $Value)
    54122    {
    55             $Value = strtr($Value, '"', '\"');
    56       if($Value != 'NOW()') $Value = "'".$Value."'";
    57       $Values .= ', '.$Key.'='.$Value;
     123      if(!in_array($Value, $this->Functions))
     124      {
     125        if(is_null($Value)) $Value = 'NULL';
     126        else $Value = $this->PDO->quote($Value);
     127      }
     128      $Values .= ', `'.$Key.'`='.$Value;
    58129    }
    59130    $Values = substr($Values, 2); 
     
    67138    foreach($Data as $Key => $Value)
    68139    {
    69       $Value = strtr($Value, '"', '\"');
     140      if(!in_array($Value, $this->Functions))
     141      {
     142        if(is_null($Value)) $Value = 'NULL';
     143        else $Value = $this->PDO->quote($Value);
     144      }
    70145      $Name .= ',`'.$Key.'`';
    71       if($Value == 'NOW()') $Values .= ','.$Value;
    72         else $Values .= ',"'.$Value.'"';
     146      $Values .= ','.$Value;
    73147    }
    74148    $Name = substr($Name, 1);
     
    83157    $this->query('SET NAMES "'.$Charset.'"');
    84158  }
    85 }
    86 
    87 ?>
     159 
     160  function real_escape_string($Text)
     161  {
     162    return(addslashes($Text));
     163  } 
     164}
     165
     166function TimeToMysqlDateTime($Time)
     167{
     168  if($Time == NULL) return(NULL);
     169    else return(date('Y-m-d H:i:s', $Time)); 
     170}
     171
     172function TimeToMysqlDate($Time)
     173{
     174  if($Time == NULL) return(NULL);
     175    else return(date('Y-m-d', $Time)); 
     176}
     177
     178function TimeToMysqlTime($Time)
     179{
     180  if($Time == NULL) return(NULL);
     181    else return(date('H:i:s', $Time)); 
     182}
     183
     184function MysqlDateTimeToTime($DateTime)
     185{
     186  if($DateTime == '') return(0);     
     187  $Parts = explode(' ', $DateTime);
     188  $DateParts = explode('-', $Parts[0]);
     189  $TimeParts = explode(':', $Parts[1]);
     190  $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
     191  return($Result); 
     192}
     193
     194function MysqlDateToTime($Date)
     195{
     196  if($Date == '') return(0);
     197  return(MysqlDateTimeToTime($Date.' 0:0:0')); 
     198}
     199
     200function MysqlTimeToTime($Time)
     201{
     202  if($Time == '') return(0);
     203  return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 
     204}
  • trunk/inc/realm.php

    r695 r712  
    1313    $DbResult = $this->Database->query('SELECT * FROM Realm WHERE Id='.$Id.' AND Enabled=1');
    1414    $this->Data = $DbResult->fetch_assoc();
    15     $this->CharactersDatabase = new Database($this->Data['DatabaseHost'], $this->Data['DatabaseUser'], $this->Data['DatabasePassword'], $this->Data['DatabaseCharacters']);
     15    $this->CharactersDatabase = new Database();
     16    $this->CharactersDatabase->Connect($this->Data['DatabaseHost'], $this->Data['DatabaseUser'], $this->Data['DatabasePassword'], $this->Data['DatabaseCharacters']);
    1617    $this->CharactersDatabase->select_db($this->Data['DatabaseCharacters']);
    17     if($this->CharactersDatabase->connect_error)
    18     {
    19       die('Přihlášení k databázi realmu '.$this->Id.' selhalo: '.$this->Database->connect_error);
    20     }
     18    //if($this->CharactersDatabase->connect_error)
     19    //{
     20    //  die('Přihlášení k databázi realmu '.$this->Id.' selhalo: '.$this->Database->connect_error);
     21    //}
    2122    $this->CharactersDatabase->charset($this->Config['Database']['Charset']);
    2223   
    23     $this->MangosDatabase = new Database($this->Data['DatabaseHost'], $this->Data['DatabaseUser'], $this->Data['DatabasePassword'], $this->Data['DatabaseMangos']);
     24    $this->MangosDatabase = new Database();
     25    $this->MangosDatabase->Connect($this->Data['DatabaseHost'], $this->Data['DatabaseUser'], $this->Data['DatabasePassword'], $this->Data['DatabaseMangos']);
    2426    $this->MangosDatabase->select_db($this->Data['DatabaseMangos']);
    25     if($this->MangosDatabase->connect_error)
    26     {
    27       die('Přihlášení k databázi realmu '.$this->Id.' selhalo: '.$this->MangosDatabase->connect_error);
    28     }
     27    //if($this->MangosDatabase->connect_error)
     28    //{
     29    //  die('Přihlášení k databázi realmu '.$this->Id.' selhalo: '.$this->MangosDatabase->connect_error);
     30    //}
    2931    $this->MangosDatabase->charset($this->Config['Database']['Charset']);
    3032  }
  • trunk/inc/server.php

    r697 r712  
    1414    $this->Id = $Id;
    1515    $DbResult = $this->Database->query('SELECT * FROM Logon WHERE Id='.$Id.' AND Enabled=1' );
    16     $this->Data = $DbResult->fetch_assoc();
    17     $this->ServerDatabase = new Database($this->Data['DatabaseHost'], $this->Data['DatabaseUser'], $this->Data['DatabasePassword'], $this->Data['DatabaseRealmd']);
    18     $this->ServerDatabase->select_db($this->Data['DatabaseRealmd']);
    19     if($this->ServerDatabase->connect_error)
     16    if($DbResult->num_rows == 1)
    2017    {
    21       die('Přihlášení k databázi serveru '.$this->Id.' selhalo: '.$this->ServerDatabase->connect_error);
    22     }
    23     $this->ServerDatabase->charset($this->Config['Database']['Charset']);
     18      $this->Data = $DbResult->fetch_assoc();
     19      $this->ServerDatabase = new Database();
     20      $this->ServerDatabase->Connect($this->Data['DatabaseHost'], $this->Data['DatabaseUser'], $this->Data['DatabasePassword'], $this->Data['DatabaseRealmd']);
     21      $this->ServerDatabase->select_db($this->Data['DatabaseRealmd']);
     22      //if($this->ServerDatabase->connect_error)
     23      //{
     24      //  die('Přihlášení k databázi serveru '.$this->Id.' selhalo: '.$this->ServerDatabase->connect_error);
     25      //}
     26      $this->ServerDatabase->charset($this->Config['Database']['Charset']);
     27    } else throw new Exception('Záznam pro přihlašovací server id '.$Id.' nenalezen!');
    2428  }
    2529
  • trunk/inc/system.php

    r695 r712  
    4747  function OpenLogonServerDatabase()
    4848  {
    49     $Database = new Database($this->Config['Mangos']['DatabaseHost'], $this->Config['Mangos']['DatabaseUser'], $this->Config['Mangos']['DatabasePassword'], $this->Config['Mangos']['DatabaseRealmd']);
     49    $Database = new Database();
     50    $Database->Connect($this->Config['Mangos']['DatabaseHost'], $this->Config['Mangos']['DatabaseUser'], $this->Config['Mangos']['DatabasePassword'], $this->Config['Mangos']['DatabaseRealmd']);
    5051    if(mysqli_connect_error())
    5152    {
     
    5859  function OpenWebDatabase()
    5960  {
    60     $Database = new Database($this->Config['Database']['Host'], $this->Config['Database']['User'], $this->Config['Database']['Password'], $this->Config['Database']['Database']);
     61    $Database = new Database();
     62    $Database->Connect($this->Config['Database']['Host'], $this->Config['Database']['User'], $this->Config['Database']['Password'], $this->Config['Database']['Database']);
    6163    if(mysqli_connect_error())
    6264    {
Note: See TracChangeset for help on using the changeset viewer.