Changeset 59 for branches/mvc/Base
- Timestamp:
- Mar 1, 2015, 12:43:56 PM (10 years ago)
- Location:
- branches/mvc
- Files:
-
- 4 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/mvc/Base/Config/ConfigSample.php
r47 r59 33 33 'PageRefreshPeriod' => 5000, 34 34 ), 35 'SystemPassword' => sha1('test'), 35 36 ); -
branches/mvc/Base/Controller.php
r47 r59 7 7 function OnAccessDenied($Action, $Id) 8 8 { 9 return($this->System->Output->SystemMessage('Nemáte oprávnění provést akci "'.$Action.'" modulu "'.substr(get_class($this), 0, -10).'" na položce "'.$Id.'"')); 9 return($this->System->Output->SystemMessage('Nemáte oprávnění provést akci "'. 10 $Action.'" modulu "'.substr(get_class($this), 0, -10).'" na položce "'.$Id.'"')); 10 11 } 11 12 -
branches/mvc/Base/Database.php
r47 r59 2 2 3 3 // Extended database class 4 // Date: 2009-02-16 5 6 class Database extends mysqli 4 // Date: 2011-11-25 5 6 class DatabaseResult 7 { 8 var $PDOStatement; 9 var $num_rows = 0; 10 11 function fetch_assoc() 12 { 13 return($this->PDOStatement->fetch(PDO::FETCH_ASSOC)); 14 } 15 16 function fetch_array() 17 { 18 return($this->PDOStatement->fetch(PDO::FETCH_BOTH)); 19 } 20 21 function fetch_row() 22 { 23 return($this->PDOStatement->fetch(PDO::FETCH_NUM)); 24 } 25 } 26 27 class Database 7 28 { 8 29 var $Prefix = ''; 9 var $Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 10 var $ShowSQLQuery = false; 11 var $ShowSQLError = false; 30 var $Functions; 31 var $Type; 32 var $PDO; 33 var $Error = ''; 34 var $insert_id; 35 var $LastQuery = ''; 36 var $ShowSQLError; 37 var $ShowSQLQuery; 38 39 function __construct() 40 { 41 $this->Type = 'mysql'; // mysql, pgsql 42 $this->ShowSQLError = false; 43 $this->ShowSQLQuery = false; 44 $this->Functions = array('NOW()', 'CURDATE()', 'CURTIME()', 'UUID()'); 45 } 46 47 function Connect($Host, $User, $Password, $Database) 48 { 49 if($this->Type == 'mysql') $ConnectionString = 'mysql:host='.$Host.';dbname='.$Database; 50 else if($this->Type == 'pgsql') $ConnectionString = 'pgsql:dbname='.$Database.';host='.$Host; 51 else $ConnectionString = ''; 52 try { 53 $this->PDO = new PDO($ConnectionString, $User, $Password); 54 55 } catch (Exception $E) 56 { 57 unset($this->PDO); 58 throw new Exception($E->getMessage()); 59 } 60 } 61 62 function Disconnect() 63 { 64 unset($this->PDO); 65 } 66 67 function Connected() 68 { 69 return(isset($this->PDO)); 70 } 71 72 function select_db($Database) 73 { 74 $this->query('USE `'.$Database.'`'); 75 } 12 76 13 77 function query($Query) 14 78 { 15 if($this->ShowSQLQuery) 16 { 17 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>'); 18 else echo($Query."\n"); 19 } 20 $Result = parent::query($Query); 21 if(($this->error != '') and ($this->ShowSQLError)) 22 { 23 if(isset($_SERVER['REMOTE_ADDR'])) echo('<div><strong>SQL Error: </strong>'.$this->error.'<br />'.$Query.'</div>'); 24 echo('SQL Error: '.$this->error.' '.$Query."\n"); 25 } 26 79 if(!$this->Connected()) throw new Exception('Not connected to database'); 80 $this->LastQuery = $Query; 81 if($this->ShowSQLQuery == true) 82 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"); 83 $Result = new DatabaseResult(); 84 $Result->PDOStatement = $this->PDO->query($Query); 85 if($Result->PDOStatement) 86 { 87 $Result->num_rows = $Result->PDOStatement->rowCount(); 88 $this->insert_id = $this->PDO->lastInsertId(); 89 } else 90 { 91 $this->Error = $this->PDO->errorInfo(); 92 $this->Error = $this->Error[2]; 93 if(($this->Error != '') and ($this->ShowSQLError == true)) 94 echo('<div><strong>SQL Error: </strong>'.$this->Error.'<br />'.$Query.'</div>'); 95 throw new Exception('SQL Error: '.$this->Error.', Query: '.$Query); 96 } 27 97 return($Result); 28 98 } … … 45 115 { 46 116 $Name .= ',`'.$Key.'`'; 47 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"'; 117 if(!in_array($Value, $this->Functions)) 118 { 119 if(is_null($Value)) $Value = 'NULL'; 120 else $Value = $this->PDO->quote($Value); 121 } 48 122 $Values .= ','.$Value; 49 123 } … … 51 125 $Values = substr($Values, 1); 52 126 $this->query('INSERT INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 127 $this->insert_id = $this->PDO->lastInsertId(); 53 128 } 54 129 … … 58 133 foreach($Data as $Key => $Value) 59 134 { 60 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"'; 135 if(!in_array($Value, $this->Functions)) 136 { 137 if(is_null($Value)) $Value = 'NULL'; 138 else $Value = $this->PDO->quote($Value); 139 } 61 140 $Values .= ', `'.$Key.'`='.$Value; 62 141 } … … 71 150 foreach($Data as $Key => $Value) 72 151 { 73 if(!in_array($Value, $this->Functions)) $Value = '"'.$this->real_escape_string($Value).'"'; 152 if(!in_array($Value, $this->Functions)) 153 { 154 if(is_null($Value)) $Value = 'NULL'; 155 else $Value = $this->PDO->quote($Value); 156 } 74 157 $Name .= ',`'.$Key.'`'; 75 158 $Values .= ','.$Value; … … 79 162 //echo('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES ('.$Values.')<br />'); 80 163 $this->query('REPLACE INTO `'.$this->Prefix.$Table.'` ('.$Name.') VALUES('.$Values.')'); 81 //echo($this->error().'<br />');164 //echo($this->error().'<br>'); 82 165 } 83 166 … … 87 170 } 88 171 89 function TimeToMysqlDateTime($Time) 90 { 91 return(date('Y-m-d H:i:s', $Time)); 92 } 93 94 function MysqlDateTimeToTime($Time) 95 { 96 $Parts = explode(' ', $Time); 97 $DateParts = explode('-', $Parts[0]); 98 $TimeParts = explode(':', $Parts[1]); 99 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 100 return($Result); 101 } 102 103 function MysqlDateToTime($Time) 104 { 105 return(MysqlDateTimeToTime($Time.' 0:0:0')); 106 } 107 } 172 function real_escape_string($Text) 173 { 174 return(addslashes($Text)); 175 } 176 177 public function __sleep() 178 { 179 return array('LastQuery'); 180 } 181 182 public function __wakeup() 183 { 184 } 185 186 function MysqlDateTimeToTime($DateTime) 187 { 188 return(MysqlDateTimeToTime($DateTime)); 189 } 190 191 function TimeToMysqlDateTime($DateTime) 192 { 193 return(TimeToMysqlDateTime($DateTime)); 194 } 195 } 196 197 function TimeToMysqlDateTime($Time) 198 { 199 if($Time == NULL) return(NULL); 200 else return(date('Y-m-d H:i:s', $Time)); 201 } 202 203 function TimeToMysqlDate($Time) 204 { 205 if($Time == NULL) return(NULL); 206 else return(date('Y-m-d', $Time)); 207 } 208 209 function TimeToMysqlTime($Time) 210 { 211 if($Time == NULL) return(NULL); 212 else return(date('H:i:s', $Time)); 213 } 214 215 function MysqlDateTimeToTime($DateTime) 216 { 217 if($DateTime == '') return(NULL); 218 $Parts = explode(' ', $DateTime); 219 $DateParts = explode('-', $Parts[0]); 220 $TimeParts = explode(':', $Parts[1]); 221 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 222 return($Result); 223 } 224 225 function MysqlDateToTime($Date) 226 { 227 if($Date == '') return(NULL); 228 return(MysqlDateTimeToTime($Date.' 0:0:0')); 229 } 230 231 function MysqlTimeToTime($Time) 232 { 233 if($Time == '') return(NULL); 234 return(MysqlDateTimeToTime('0000-00-00 '.$Time)); 235 } -
branches/mvc/Base/System.php
r47 r59 1 1 <?php 2 2 3 include_once(dirname(__FILE__).'/Version.php'); 3 4 include_once(dirname(__FILE__).'/Database.php'); 4 5 include_once(dirname(__FILE__).'/Error.php'); … … 9 10 include_once(dirname(__FILE__).'/Output.php'); 10 11 include_once(dirname(__FILE__).'/Permission.php'); 12 include_once(dirname(__FILE__).'/PrefixMultiplier.php'); 13 include_once(dirname(__FILE__).'/Setup.php'); 11 14 12 15 $MonthNames = array('', 'Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec'); 13 14 $PrefixMultipliers = array15 (16 'Binary' => array17 (18 'BaseIndex' => 0,19 'Definition' => array20 (21 array('', '', pow(2, 0)),22 array('Ki', 'kibi', pow(2, 10)),23 array('Mi', 'mebi', pow(2, 20)),24 array('Gi', 'gibi', pow(2, 30)),25 array('Ti', 'tebi', pow(2, 40)),26 array('Pi', 'pebi', pow(2, 50)),27 array('Ei', 'exbi', pow(2, 60)),28 array('Zi', 'zebi', pow(2, 70)),29 array('Yi', 'yobi', pow(2, 80)),30 ),31 ),32 'Decimal' => array33 (34 'BaseIndex' => 8,35 'Definition' => array36 (37 array('y', 'yocto', pow(10, -24)),38 array('z', 'zepto', pow(10, -21)),39 array('a', 'atto', pow(10, -18)),40 array('f', 'femto', pow(10, -15)),41 array('p', 'piko', pow(10, -12)),42 array('n', 'nano', pow(10, -9)),43 array('u', 'mikro', pow(10, -6)),44 array('m', 'mili', pow(10, -3)),45 array('', '', pow(10, 0)),46 array('k', 'kilo', pow(10, 3)),47 array('M', 'mega', pow(10, 6)),48 array('G', 'giga', pow(10, 9)),49 array('T', 'tera', pow(10, 12)),50 array('P', 'peta', pow(10, 15)),51 array('E', 'exa', pow(10, 18)),52 array('Z', 'zetta', pow(10, 21)),53 array('Y', 'yotta', pow(10, 24)),54 ),55 ),56 'Time' => array57 (58 'BaseIndex' => 8,59 'Definition' => array60 (61 array('ys', 'yoctosekunda', pow(10, -24)),62 array('zs', 'zeptosekunda', pow(10, -21)),63 array('as', 'attosekunda', pow(10, -18)),64 array('fs', 'femtosekunda', pow(10, -15)),65 array('ps', 'pikosekunda', pow(10, -12)),66 array('ns', 'nanosekunda', pow(10, -9)),67 array('us', 'mikrosekunda', pow(10, -6)),68 array('ms', 'milisekunda', pow(10, -3)),69 array('s', 'sekund', 1),70 array('minut', 'minuta', 60),71 array('hodin', 'hodina', 60 * 60) ,72 array('dnů', 'den', 24 * 60 * 60),73 array('týdnů', 'týden', 7 * 24 * 60 * 60),74 array('měsíců', 'měsíc', 30 * 24 * 60 * 60),75 array('roků', 'rok', 364 * 24 * 60 * 60),76 array('desitiletí', 'desetiletí', 10 * 364 * 24 * 60 * 60),77 array('staletí', 'staletí', 100 * 364 * 24 * 60 * 60),78 array('tisíciletí', 'tisiciletí', 10000 * 364 * 24 * 60 * 60),79 ),80 ),81 );82 16 83 17 function ErrorHandler() … … 112 46 $this->Output = new Output($this); 113 47 48 $Config = array(); 49 114 50 $FileName = dirname(__FILE__).'/Config/Config.php'; 115 51 if(file_exists($FileName)) include($FileName); … … 118 54 exit; 119 55 } 120 121 $Config = array();122 56 123 57 $FileName = dirname(__FILE__).'/../Application/Config/Config.php'; … … 138 72 139 73 if(isset($_SERVER['REMOTE_ADDR'])) session_start(); 140 $this->Database = new Database($this->Config['Database']['Host'], 141 $this->Config['Database']['User'], $this->Config['Database']['Password'], 142 $this->Config['Database']['Database']); 74 $this->Database = new Database(); 75 $this->Database->Connect($this->Config['Database']['Host'], 76 $this->Config['Database']['User'], $this->Config['Database']['Password'], 77 $this->Config['Database']['Database']); 143 78 $this->Database->ShowSQLQuery = $this->Config['System']['ShowSQLQuery']; 144 79 $this->Database->ShowSQLError = $this->Config['System']['ShowSQLError']; … … 160 95 foreach($_GET as $Index => $Item) $_GET[$Index] = addslashes($Item); 161 96 97 // Register and start existing modules 98 $this->Setup = new Setup($this); 99 $this->Setup->Start(); 100 162 101 if(array_key_exists('M', $_GET)) $Module = $_GET['M']; 163 102 else $Module = $this->DefaultModule; 164 103 165 104 $ControllerName = $Module.'Controller'; 166 $FileName = dirname(__FILE__).'/../Application/Controller/'.$Module.'.php'; 167 if(!file_exists($FileName)) 105 if(!class_exists($ControllerName)) 168 106 { 169 $this->Output->Show($this->Output->SystemMessage('Soubor '.$FileName.' nenalezen.')); 170 exit; 171 } else include($FileName); 107 // Try to load controller class 108 $FileName = dirname(__FILE__).'/../Application/Controller/'.$Module.'.php'; 109 if(!file_exists($FileName)) 110 { 111 $this->Output->Show($this->Output->SystemMessage('Soubor '.$FileName.' nenalezen.')); 112 exit; 113 } else include($FileName); 114 } 172 115 if(class_exists($ControllerName)) 173 116 { … … 190 133 { 191 134 return(date('j.n.Y', $Time)); 192 }193 194 function TruncateDigits($Value, $Digits = 4)195 {196 for($II = 2; $II > -6; $II--)197 {198 if($Value >= pow(10, $II))199 {200 if($Digits < ($II + 1)) $RealDigits = $II + 1; else $RealDigits = $Digits;201 $Value = round($Value / pow(10, $II - $RealDigits + 1)) * pow(10, $II - $RealDigits + 1);202 break;203 }204 }205 return($Value);206 }207 208 function AddPrefixMultipliers($Value, $Unit, $Digits = 4, $PrefixType = 'Decimal')209 {210 global $PrefixMultipliers;211 212 $Negative = ($Value < 0);213 $Value = abs($Value);214 if(($Unit == '') and ($PrefixType != 'Time'))215 return($this->TruncateDigits($Value, $Digits));216 217 $I = $PrefixMultipliers[$PrefixType]['BaseIndex'];218 if($Value == 0) return($Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit);219 220 if($Value > 1)221 {222 while((($I + 1) <= count($PrefixMultipliers[$PrefixType]['Definition'])) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I + 1][2]) > 1))223 $I = $I + 1;224 } else225 if($Value < 1)226 {227 while((($I - 1) >= 0) and (($Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2]) < 1))228 $I = $I - 1;229 }230 $Value = $Value / $PrefixMultipliers[$PrefixType]['Definition'][$I][2];231 232 // Truncate digits count233 $Value = $this->TruncateDigits($Value, $Digits);234 if($Negative) $Value = -$Value;235 return($Value.' '.$PrefixMultipliers[$PrefixType]['Definition'][$I][0].$Unit);236 135 } 237 136
Note:
See TracChangeset
for help on using the changeset viewer.