Changeset 507


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
Files:
4 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/action.php

    r505 r507  
    269269
    270270  $Output = '<h3>Novinky</h3>'.$PageList['Output'];
    271   if($User->Licence(LICENCE_USER)) $Output .= ' <a href="admin.php?action=addnew">Vložit</a>';
     271  if($User->Licence(LICENCE_ADMIN)) $Output .= ' <a href="admin/?action=addnew">Vložit</a>';
    272272  $Output .= '<div class="shoutbox">';
    273273  $DbResult = $System->Database->query('SELECT `News`.`Time`, `News`.`Text`, `User`.`Name` AS `User` FROM `News` JOIN `User` ON `User`.`Id`=`News`.`User` ORDER BY `News`.`Time` DESC '.$PageList['SQLLimit']);
     
    278278}
    279279
    280 $GroupId = LoadGroupIdParameter();
    281 $Table = $TranslationTree[$GroupId]['TablePrefix'];
     280if(array_key_exists('group', $_GET)) $GroupId = LoadGroupIdParameter();
     281  else $GroupId = 1;
     282//$Table = $TranslationTree[$GroupId]['TablePrefix'];
    282283$Action = '';
    283284if(array_key_exists('action', $_GET)) $Action = $_GET['action'];
  • trunk/dictionary.php

    r506 r507  
    5353  {
    5454    $_SESSION['language'] = $User->Language;
    55   } else $_SESSION['language'] = 1;
     55  } else $_SESSION['language'] = 2;
    5656}
    5757if(array_key_exists('language', $_GET)) $_SESSION['language'] = LoadLanguageIdParameter();
     
    8181      foreach($TranslationTree[$GroupId]['Items'] as $Index => $TextItem)
    8282        $Text .= ' '.$Line[$TextItem['Column']];
    83           $Output .= WriteTranslatNames($Text,$mode);
     83          $Output .= WriteTranslatNames($Text, $mode);
    8484    }
    8585  }
  • trunk/img_level.php

    r504 r507  
    111111    // Build TranslatedCount query
    112112    $TranslatedCount = '(';
    113     foreach($TranslationTree as $Group)
     113    if(count($TranslationTree) > 0)
     114    {
     115          foreach($TranslationTree as $Group)
    114116      if($Group['TablePrefix'] != '')
    115117      {
     
    117119        $TranslatedCount .= 'COALESCE('.$Count.', 0) + ';
    118120      }
    119     $TranslatedCount = substr($TranslatedCount, 0, -3).')';
     121      $TranslatedCount = substr($TranslatedCount, 0, -3).')';
     122    } else $TranslatedCount = 0;
    120123   
    121124    $xp = GetXPFromTranslation($LineUser['ID']);
  • 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 
  • trunk/index.php

    r505 r507  
    2828  $UnionItems = array();
    2929  $DbResult = $System->Database->query($GroupListQuery);
     30  if($DbResult->num_rows > 0)
     31  {
    3032  while($DbRow = $DbResult->fetch_assoc())
    3133  {
     
    5153  }
    5254  $Output .= '</table>';
     55  }
    5356  return($Output);
    5457}
  • trunk/log.php

    r504 r507  
    2727  $Output .= GenerateRSS(array
    2828  (
    29     'Title' => 'WoWpřeklad',
     29    'Title' => $Config['Web']['Title'],
    3030    'Link' => 'http://'.$Config['Web']['Host'].$System->Link('/'),
    3131    'Description' => $Config['Web']['Title'],
     
    4444    else $WhereType = '1=1';
    4545   
    46   $RSSChannels = array(array('Title' => 'Záznamy změn', 'URL' => 'log.php?rss&type='.$_SESSION['type']));
     46  $RSSChannels = array(
     47        array('Title' => 'Záznamy změn', 'Channel' => 'log&amp;type='.$_SESSION['type'])
     48  );
    4749 
    4850  if($User->Licence(LICENCE_MODERATOR))
     
    8890  while($Line = $DbResult->fetch_assoc())
    8991  {
    90     $Output .= '<tr><td>'.$Line['Date'].'</td><td><span style="color: '.$Line['LogColor'].'">'.$Line['Text'].'</span></td><td>'.$Line['UserName'].'</td><td>'.$Line['IP'].'</td></tr>';
     92    //$Output .= '<tr><td>'.$Line['Date'].'</td><td><span style="color: '.$Line['LogColor'].'">'.$Line['Text'].'</span></td><td>'.$Line['UserName'].'</td><td>'.$Line['IP'].'</td></tr>';
    9193  }
    9294  $Output .= '</table>'.
  • trunk/serverlist.php

    r504 r507  
    2121
    2222
    23 $Query = 'SELECT * FROM `ServerList` '.$Order['SQL'].$PageList['SQLLimit'];
     23$Query = 'SELECT `URL`, `Name` FROM `ServerList` '.$Order['SQL'].$PageList['SQLLimit'];
    2424
    2525$DbResult = $System->Database->query($Query);
  • trunk/statistic.php

    r506 r507  
    1212  global $System;
    1313 
    14   $LanguageId = LoadLanguageIdParameter();
     14  $Output = '';
     15  if(!isset($_SESSION['language']))
     16  {
     17    if($User->Licence(LICENCE_USER))
     18    {
     19      $_SESSION['language'] = $User->Language;
     20    } else $_SESSION['language'] = 2;
     21  }
     22  if(array_key_exists('language', $_GET)) $_SESSION['language'] = LoadLanguageIdParameter();
     23 
    1524  $BuildNumber = GetBuildNumber($_SESSION['StatVersion']);
    1625 
     
    1827  $Query = '';
    1928  $DbResult = $System->Database->query($GroupListQuery);
     29  if($DbResult->num_rows > 0)
     30  { 
    2031  while($DbRow = $DbResult->fetch_assoc())
    2132  {
    2233    $Query .= 'SELECT (SELECT COUNT(DISTINCT(`Entry`)) FROM ('.
    2334    ' SELECT `T`.`Id`, `T`.`Entry` FROM `'.$DbRow['TablePrefix'].'` AS `T`'.
    24     ' WHERE (`Complete` = 1) AND (`Language`='.$LanguageId.') AND (`VersionStart` <= '.$BuildNumber.') AND (`VersionEnd` >= '.$BuildNumber.')'.
     35    ' WHERE (`Complete` = 1) AND (`Language`='.$_SESSION['language'].') AND (`VersionStart` <= '.$BuildNumber.') AND (`VersionEnd` >= '.$BuildNumber.')'.
    2536    ') AS `C1`) AS `Translated`, '.
    2637    '(SELECT COUNT(DISTINCT(`Entry`)) FROM ('.
     
    3445  $DbRow = $DbResult->fetch_row();
    3546  $PageList = GetPageList($DbRow[0]); 
    36   $Output = $PageList['Output'];
     47  $Output .= $PageList['Output'];
    3748 
    3849  $Output .= '<table class="BaseTable">';
     
    6071  $Output .= '<tr><td><strong>Celkem</strong></td><td><strong>'.$Translated.'</strong></td><td><strong>'.$Total.'</strong></td><td><strong>'.ProgressBar(150, $TotalCount).'</strong></td></tr>'.
    6172    '</table>';
     73  }
    6274  return($Output);
    6375}
  • trunk/team.php

    r504 r507  
    9797      if(($Count == 0) and ($_POST['Name'] != ''))
    9898      {
    99         $System->Database->query('INSERT INTO `Team` (`Name` ,`Description`, `URL`, `TimeCreate`,`Leader`) VALUES ("'.trim($_POST['Name']).'", "'.trim($_POST['Description']).'", "'.$_POST['URL'].'", NOW(), '.$User->Id.')'); 
     99        $System->Database->query('INSERT INTO `Team` (`Name` ,`Description`, `URL`, `TimeCreate`, `Leader`)'.
     100        ' VALUES ("'.trim($_POST['Name']).'", "'.trim($_POST['Description']).'", "'.
     101        $_POST['URL'].'", NOW(), '.$User->Id.')'); 
    100102        $System->Database->query('UPDATE `User` SET `Team` = '.$System->Database->insert_id.' WHERE `ID` = '.$User->Id);
    101103        $Output .= ShowMessage('Překladatelský tým vytvořen.');
     
    220222  $Query = '';
    221223  $DbResult = $System->Database->query($GroupListQuery);
     224  if($DbResult->num_rows > 0)
     225  {
    222226  while($DbRow = $DbResult->fetch_assoc())
    223227  {
     
    247251
    248252  $Order = GetOrderTableHeader($TableColumns, 'Name', 0);
    249   $Output .=$Order['Output'];
     253  $Output .= $Order['Output'];
    250254
    251255  $Translated = 0;
     
    260264  $Output .='<tr><td><strong>Celkem</strong></td><td><strong>'.$Translated.'</strong></td><td><strong>'.$Total.'</strong></td><td><strong>'.ProgressBar(150, round($Translated / $Total * 100, 2)).'</strong></td></tr>';
    261265  $Output .='</table>';
    262  
     266  }
    263267      $Output .='</fieldset>';   
    264268    } else $Output .=ShowMessage('Tým nenalezen', MESSAGE_CRITICAL);
Note: See TracChangeset for help on using the changeset viewer.