| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* 	Sunrise CMS - Open source CMS for widespread use. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |     Copyright (c) 2019 Mykola Burakov ([email protected]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |     See SOURCE.txt for other and additional information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |     This file is part of Sunrise CMS. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     it under the terms of the GNU General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     GNU General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     You should have received a copy of the GNU General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     along with this program. If not, see <http://www.gnu.org/licenses/>. */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | namespace Sunrise\Engine\Library; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class Db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $statement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     // tracy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct($hostname, $username, $password, $database, $port = '3306') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $this->connection = @new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database, $username, $password, array(\PDO::ATTR_PERSISTENT => true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             throw new \Exception('Error: Could not make a database link using ' . $username . '@' . $hostname . '!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->connection->exec("SET NAMES 'utf8mb4'"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->connection->exec("SET CHARACTER SET utf8mb4"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->connection->exec("SET CHARACTER_SET_CONNECTION=utf8mb4"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->connection->exec("SET SQL_MODE = ''"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         // Tracy Debugger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if (isset($_SESSION['_tracy']['sql_log'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             unset($_SESSION['_tracy']['sql_log']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->log = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->log['page_url'] = (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : 'index.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->log['query_total_time'] = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::__construct'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     public function query($sql, $params = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         // Tracy Debugger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->log['query_total_time'] = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $trace = debug_backtrace(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $filename = (isset($trace[0]['file'])) ? $trace[0]['file'] : '---'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $cmsPath = str_replace('upload/engine/', '', $_SERVER['DOCUMENT_ROOT'] . '/engine/'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $cmsPath = str_replace('public/engine/', '', $cmsPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $pureFile = str_replace($cmsPath, '', $filename); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $bench = new \Ubench; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $bench->start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->statement = $this->connection->prepare($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $result = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             if ($this->statement && $this->statement->execute($params)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $data = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     $data[] = $row; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $result = new \stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 $result->row = (isset($data[0]) ? $data[0] : array()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 $result->rows = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 $result->num_rows = $this->statement->rowCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode() . ' <br />' . $sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         // Tracy Debugger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $bench->end(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $exec_time = $bench->getTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         if (!isset($this->log['query_total_time'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $this->log['query_total_time'] = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->log['query_total_time'] = (float) $this->log['query_total_time'] + (float) $exec_time; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $this->log['file']  = $pureFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->log['time']  = $exec_time; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->log['query'] = \SqlFormatter::format($sql); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $_SESSION['_tracy']['sql_log'][] = $this->log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         // | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         if ($result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $result = new \stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $result->row = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $result->rows = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $result->num_rows = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::query'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     public function execute() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             if ($this->statement && $this->statement->execute()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 $data = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 while ($row = $this->statement->fetch(\PDO::FETCH_ASSOC)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |                     $data[] = $row; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 $result = new \stdClass(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 $result->row = (isset($data[0])) ? $data[0] : array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |                 $result->rows = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |                 $result->num_rows = $this->statement->rowCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } catch (\PDOException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |             throw new \Exception('Error: ' . $e->getMessage() . ' Error Code : ' . $e->getCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::execute'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     public function escape($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return str_replace(array("\\", "\0", "\n", "\r", "\x1a", "'", '"'), array("\\\\", "\\0", "\\n", "\\r", "\Z", "\'", '\"'), $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::escape'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |     public function prepare($sql) | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |         $this->statement = $this->connection->prepare($sql); | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::prepare'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function bindParam($parameter, $variable, $data_type = \PDO::PARAM_STR, $length = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         if ($length) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             $this->statement->bindParam($parameter, $variable, $data_type, $length); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             $this->statement->bindParam($parameter, $variable, $data_type); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::bindParam'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     public function countAffected() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         if ($this->statement) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             return $this->statement->rowCount(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::countAffected'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     public function getLastId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         return $this->connection->lastInsertId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::getLastId'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     public function isConnected() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         if ($this->connection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::isConnected'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |     public function __destruct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         $this->connection = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |         \Zarganwar\PerformancePanel\Register::add('Sunrise\Engine\Library\Db::__destruct'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 204 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 205 |  |  |  |