Changeset 507 for trunk/includes


Ignore:
Timestamp:
Feb 15, 2013, 10:12:08 PM (12 years ago)
Author:
chronos
Message:
  • Upraveno: Použita novější databázová třída Database, která běží přes PDO.
  • Přidáno: Pokud databáze nesouhlasí k verzi systému, je zobrazeno chybové hlášení.
  • Přidáno: Instalační skript admin/install.php, přes který je možno nainstalovat čistou databázi a provádět automatickou aktualizaci databáze.
  • Upraveno: Konkrétní rozdílové SQL aktualizace jsou nyní zapsány přímo v kódu v souboru UpdateTrace namísto textových SQL skriptů. To umožňuje provádět také dynamickou aktualizaci dle obsahu dat v tabulkách. Zde pro získání seznamu překladových tabulek.
  • Opraveno: Některé části kódu nepodporovaly nulový počet překladových skupin nebo jazyků.
Location:
trunk/includes
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/Database.php

    r457 r507  
    22
    33// Extended database class
    4 // Date: 2010-01-29
     4// Date: 2011-11-25
    55
    6 class Database extends mysqli
     6
     7class DatabaseResult
    78{
    8   var $HostName = 'localhost';
    9   var $UserName;
    10   var $Password;
    11   var $Schema;
    12   var $Charset = 'utf8';
    13   var $Prefix = '';
    14   var $ShowSQLQuery = false;
    15   var $ShowSQLError = false;
    16 
    17   function open()
     9  var $PDOStatement;
     10  var $num_rows = 0;
     11 
     12  function fetch_assoc()
    1813  {
    19     parent::connect($this->HostName, $this->UserName, $this->Password, $this->Schema);
    20     $this->charset($this->Charset);
     14    return($this->PDOStatement->fetch(PDO::FETCH_ASSOC));
     15  }
     16 
     17  function fetch_array()
     18  {
     19    return($this->PDOStatement->fetch(PDO::FETCH_BOTH));
    2120  }
    2221
     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 = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()');
     32  var $Type = 'mysql';  // mysql, pgsql
     33  var $PDO;
     34  var $Error = '';
     35  var $insert_id;
     36  var $LastQuery = '';
     37  var $ShowSQLError = false;
     38  var $ShowSQLQuery = false;
     39 
     40  function __construct($Host, $User, $Password, $Database)
     41  {   
     42    if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database;
     43      else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host;
     44      else $ConnectionString = '';
     45    $this->PDO = new PDO($ConnectionString, $User, $Password);
     46  }
     47 
     48  function select_db($Database)
     49  {
     50    $this->query('USE `'.$Database.'`');
     51  }
     52 
    2353  function query($Query)
    2454  {
    25  
    26     if($this->ShowSQLQuery)
     55    $this->LastQuery = $Query;
     56    if($this->ShowSQLQuery == true)
     57      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");
     58    $Result = new DatabaseResult();
     59    $Result->PDOStatement = $this->PDO->query($Query);
     60    if($Result->PDOStatement)
    2761    {
    28       if(isset($_SERVER['REMOTE_ADDR'])) 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>');
    29       else echo($Query."\n");
     62      $Result->num_rows = $Result->PDOStatement->rowCount();
     63    } else
     64    {
     65      $this->Error = $this->PDO->errorInfo();
     66      $this->Error = $this->Error[2];
     67      if(($this->Error != '') and ($this->ShowSQLError == true))
     68        echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>');
    3069    }
    31     $Result = parent::query($Query);
    32     if(($this->error != '') and ($this->ShowSQLError))
    33     {
    34       if(isset($_SERVER['REMOTE_ADDR'])) echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>');
    35       echo('SQL Error: '.$this->error.' '.$Query."\n");
    36     }
    37 
     70    $this->insert_id = $this->PDO->lastInsertId();
    3871    return($Result); 
    3972  }
    4073
    4174  function select($Table, $What = '*', $Condition = 1)
    42   {
     75  {   
    4376    return($this->query('SELECT '.$What.' FROM `'.$this->Prefix.$Table.'` WHERE '.$Condition)); 
    4477  }
     
    5689    {
    5790      $Name .= ',`'.$Key.'`';
     91      if(!in_array($Value, $this->Functions))
     92      {
     93        if(is_null($Value)) $Value = 'NULL';
     94        else $Value = $this->PDO->quote($Value);
     95      }
    5896      $Values .= ','.$Value;
    5997    }
     
    6199    $Values = substr($Values, 1);
    62100    $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')');
     101    $this->insert_id = $this->PDO->lastInsertId();
    63102  }
    64103 
     
    68107    foreach($Data as $Key => $Value)
    69108    {
     109      if(!in_array($Value, $this->Functions))
     110      {
     111        if(is_null($Value)) $Value = 'NULL';
     112        else $Value = $this->PDO->quote($Value);
     113      }
    70114      $Values .= ', `'.$Key.'`='.$Value;
    71115    }
     
    80124    foreach($Data as $Key => $Value)
    81125    {
     126      if(!in_array($Value, $this->Functions))
     127      {
     128        if(is_null($Value)) $Value = 'NULL';
     129        else $Value = $this->PDO->quote($Value);
     130      }
    82131      $Name .= ',`'.$Key.'`';
    83132      $Values .= ','.$Value;
     
    94143    $this->query('SET NAMES "'.$Charset.'"');
    95144  }
     145 
     146  function real_escape_string($Text)
     147  {
     148    return(addslashes($Text));
     149  }
     150 
     151}
    96152
    97   function TimeToMysqlDateTime($Time)
    98   {
    99     return(date('Y-m-d H:i:s', $Time));
    100   }
     153function TimeToMysqlDateTime($Time)
     154{
     155  if($Time == NULL) return(NULL);
     156    else return(date('Y-m-d H:i:s', $Time)); 
     157}
    101158
    102   function MysqlDateTimeToTime($Time)
    103   {
    104     $Parts = explode(' ', $Time);
    105     $DateParts = explode('-', $Parts[0]);
    106     $TimeParts = explode(':', $Parts[1]);
    107     $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
    108     return($Result);
    109   }
     159function TimeToMysqlDate($Time)
     160{
     161  if($Time == NULL) return(NULL);
     162    else return(date('Y-m-d', $Time)); 
     163}
    110164
    111   function MysqlDateToTime($Time)
    112   {
    113     return($this->MysqlDateTimeToTime($Time.' 0:0:0'));
    114   }
     165function TimeToMysqlTime($Time)
     166{
     167  if($Time == NULL) return(NULL);
     168    else return(date('H:i:s', $Time)); 
     169}
     170
     171function MysqlDateTimeToTime($DateTime)
     172{
     173  if($DateTime == '') return(0);     
     174  $Parts = explode(' ', $DateTime);
     175  $DateParts = explode('-', $Parts[0]);
     176  $TimeParts = explode(':', $Parts[1]);
     177  $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
     178  return($Result); 
     179}
     180
     181function MysqlDateToTime($Date)
     182{
     183  if($Date == '') return(0);
     184  return(MysqlDateTimeToTime($Date.' 0:0:0')); 
     185}
     186
     187function MysqlTimeToTime($Time)
     188{
     189  if($Time == '') return(0);
     190  return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 
    115191}
    116192
  • trunk/includes/Page.php

    r506 r507  
    189189  if(isset($RSSChannels))
    190190  foreach($RSSChannels as $Channel)
     191  {
    191192    $Output .= ' <link rel="alternate" title="'.$Channel['Title'].'" href="'.
    192193      $System->Link('/rss.php?channel='.$Channel['Channel']).'" type="application/rss+xml" />';
     194  }
    193195  $Output .= '<title>'.$System->Config['Web']['Title'].'</title>
    194196</head>
  • trunk/includes/error.php

    r443 r507  
    11<?php
    2 
    3 include_once('global_function.php');
    42
    53function EmptyErrorHandler($Number, $Message, $Filename, $LineNumber, $Variables)
     
    8482}
    8583
    86 set_error_handler('CustomErrorHandler');
     84//set_error_handler('CustomErrorHandler');
    8785
    8886?>
  • trunk/includes/global.php

    r506 r507  
    11<?php
    22
    3 $ScriptStartTime = GetMicrotime();
    4 
    5 if(isset($_SERVER['REMOTE_ADDR'])) session_start();
    6 
    7 // SQL injection hack protection
    8 foreach($_POST as $Index => $Item)
    9 {
    10   if(is_array($_POST[$Index]))
    11     foreach($_POST[$Index] as $Index2 => $Item2) $_POST[$Index][$Index2] = addslashes($Item2);
    12   else $_POST[$Index] = addslashes($_POST[$Index]);
    13 }
    14 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($_GET[$Index]);
    15 
    16 if(file_exists(dirname(__FILE__).'/config.php')) include_once(dirname(__FILE__).'/config.php');
    17   else die('Nenalezen konfigurační soubor config.php ve složce includes. '.
    18         'Vytvořte jej zkopírováním vzoru config.sample.php.');
    19 date_default_timezone_set($Config['Web']['Timezone']);
    203include_once(dirname(__FILE__).'/Database.php');
     4include_once(dirname(__FILE__).'/system.php');
     5include_once(dirname(__FILE__).'/Update.php');
    216include_once(dirname(__FILE__).'/rss.php');
    22 include_once(dirname(__FILE__).'/system.php');
    237include_once(dirname(__FILE__).'/user.php');
    248include_once(dirname(__FILE__).'/Page.php');
    25 
    26 $System = new System();
    27 $System->Init();
    28 $User = new User($System);
    29 
    309include_once(dirname(__FILE__).'/error.php');
    3110
    32 $TranslationTree = GetTranslationTree();
    33 
    34 LogReferrer();
     11GlobalInit();
     12
     13function GlobalInit()
     14{
     15        global $System, $ScriptStartTime, $TranslationTree, $User, $StopAfterUpdateManager,
     16          $UpdateManager, $Config;
     17
     18  $ScriptStartTime = GetMicrotime();
     19
     20  if(isset($_SERVER['REMOTE_ADDR'])) session_start();
     21
     22  if(file_exists(dirname(__FILE__).'/config.php')) include_once(dirname(__FILE__).'/config.php');
     23    else die('Nenalezen konfigurační soubor config.php ve složce includes. '.
     24          'Vytvořte jej zkopírováním vzoru config.sample.php.');
     25  date_default_timezone_set($Config['Web']['Timezone']);
     26 
     27  $Revision = 506; // Subversion revision
     28  $ReleaseTime = '2013-02-15'; 
     29 
     30  $System = new System();
     31  $System->Init();
     32
     33  // Check database persistence structure
     34  $UpdateManager = new UpdateManager();
     35  $UpdateManager->Database = $System->Database;
     36  $UpdateManager->Revision = $Revision;
     37  if(isset($StopAfterUpdateManager)) return;
     38  if(!$UpdateManager->IsInstalled()) die('Systém vyžaduje instalaci databáze.');
     39  if(!$UpdateManager->IsUpToDate()) die('Systém vyžaduje aktualizaci databáze.');
     40
     41  // SQL injection hack protection
     42  foreach($_POST as $Index => $Item)
     43  {
     44        if(is_array($_POST[$Index]))
     45                foreach($_POST[$Index] as $Index2 => $Item2) $_POST[$Index][$Index2] = addslashes($Item2);
     46        else $_POST[$Index] = addslashes($_POST[$Index]);
     47  }
     48  foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($_GET[$Index]);
     49 
     50  $User = new User($System);
     51 
     52  set_error_handler('CustomErrorHandler');
     53
     54  // TODO: Global initialized variable should be removed
     55  $TranslationTree = GetTranslationTree();
     56
     57  LogReferrer();
     58}
    3559
    3660$RSSChannels = array(
     
    298322}
    299323
    300 function MysqlDateTimeToTime($Time)
    301 {
    302   $Parts = explode(' ', $Time);
    303   $DateParts = explode('-', $Parts[0]);
    304   $TimeParts = explode(':', $Parts[1]);
    305   $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
    306   return($Result); 
    307 }
    308 
    309324function GetLanguageList()
    310325{
     
    429444 
    430445  if(array_key_exists('language', $_GET)) $LanguageId = $_GET['language'] * 1;
    431     else $LanguageId = 1;
     446    else $LanguageId = 2;
    432447 
    433448  if(isset($LanguageList[$LanguageId]) == false)
  • trunk/includes/system.php

    r506 r507  
    2424  function __construct()
    2525  {
    26     $this->Database = new Database();
    2726    $this->Config = array();
    2827  }
     
    3029  function Init()
    3130  {
    32         global $Config;   
     31        global $Config;
     32           
    3333    $this->Config = $Config;
    34     $this->Database->HostName = $this->Config['Database']['Host'];
    35     $this->Database->UserName = $this->Config['Database']['User'];
    36     $this->Database->Password = $this->Config['Database']['Password'];
    37     $this->Database->Schema = $this->Config['Database']['Database'];
    38     $this->Database->Charset = $this->Config['Database']['Charset'];
     34    $this->Database = new Database($this->Config['Database']['Host'],
     35      $this->Config['Database']['User'], $this->Config['Database']['Password'],
     36      $this->Config['Database']['Database']);
     37    $this->Database->charset($this->Config['Database']['Charset']);
    3938    $this->Database->ShowSQLQuery = $this->Config['Web']['ShowSQLQuery'];
    4039    $this->Database->ShowSQLError = $this->Config['Web']['ShowSQLError'];
    41     $this->Database->open();
    4240  }
    4341 
Note: See TracChangeset for help on using the changeset viewer.