Example #1
0
 /**
  * Actually perform the query.
  * $type can be 'master' or 'slave'.
  * If $query is FALSE, escape string in $string.
  */
 private function query($type, $query, $string = '')
 {
     global $url;
     global $db_name;
     global $db_user;
     global $db_pass;
     global $t_query;
     $t_query_start = microtime(TRUE);
     // Check if we have 'queries credit'.
     if ($this->no_more !== FALSE) {
         return FALSE;
     }
     // Debugging.
     if ($query !== FALSE) {
         $this->count += 1;
         $query_log = preg_replace('/\\n/', ' ', $query);
         $query_log = preg_replace('/ +/', ' ', $query_log);
         $this->log .= $query_log . "\n";
     }
     // If needed, connect to database.
     if (empty($this->{$type . '_handler'})) {
         if ($type == 'master') {
             global $db_host_master;
             $host = $db_host_master;
         } else {
             if ($type == 'slave') {
                 global $db_host_slave;
                 $host = $db_host_slave;
             }
         }
         $this->{$type . '_handler'} = mysqli_connect($host, $db_user, $db_pass, $db_name);
         if ($this->{$type . '_handler'}->connect_error) {
             // Error in connection: Report to sysadmins.
             global $base_url;
             logger("{$base_url}/{$url} - Error trying to connect to {$type}", 'db_query_error.log');
             // Error in connection: Report to user.
             header('HTTP/1.1 503 Service Unavailable');
             $tpl = new Template();
             print $tpl->error('temporarily');
             die;
         } else {
             // Connection OK.
             mysqli_query($this->{$type . '_handler'}, 'SET NAMES "utf8"');
         }
     }
     // Make the query.
     if ($query !== FALSE) {
         $result = mysqli_query($this->{$type . '_handler'}, $query);
         if ($result === FALSE) {
             // Log query errors in a file.
             $string = "{$url} - " . mysqli_error($this->{$type . '_handler'}) . " - {$query}";
             logger($string, 'db_query_error.log');
         }
         $t_query += microtime(TRUE) - $t_query_start;
         return $result;
     } else {
         // Not an actual query.
         return mysqli_real_escape_string($this->{$type . '_handler'}, $string);
     }
 }
Example #2
0
 /**
  * @param Exception $exception
  */
 public static function exception_handler($exception, $template = null)
 {
     global $import;
     if (error_reporting() == 0) {
         return;
     }
     if ($template === null) {
         if (!empty($import)) {
             $template = $import->template;
         } else {
             $template = new Template(null);
         }
     }
     $message = $exception->getMessage();
     $trace = $exception->getTrace();
     $line = $exception->getLine();
     $file = $exception->getFile();
     $template->error($message, isset($trace[0]['args'][1]) ? $trace[0]['args'][1] : null, $line, $file);
 }