From 4ea1211ea8507cd035fc94560127c1200cfad8aa Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 11 May 2017 23:29:56 -0400 Subject: [PATCH 1/4] Move q errno check inside db existence check - Remove if ! function_exists - Fix formatting --- include/dba.php | 125 ++++++++++++++++++++---------------------------- 1 file changed, 53 insertions(+), 72 deletions(-) diff --git a/include/dba.php b/include/dba.php index 877f6cd3..37e1e173 100644 --- a/include/dba.php +++ b/include/dba.php @@ -16,7 +16,8 @@ class dba public function __construct($server, $user, $pass, $db, $install = false) { $this->db = @new mysqli($server, $user, $pass, $db); - if ((mysqli_connect_errno()) && (! install)) { + + if (mysqli_connect_errno() && ! $install) { system_unavailable(); } } @@ -40,7 +41,7 @@ class dba $mesg = ''; if ($this->db->mysqli->errno) { - $debug_text .= $this->db->mysqli->error . EOL; + $debug_text .= $this->db->mysqli->error . EOL; } if ($result === false) { @@ -48,19 +49,19 @@ class dba } elseif ($result === true) { $mesg = 'true'; } else { - $mesg = $result->num_rows.' results' . EOL; + $mesg = $result->num_rows . ' results' . EOL; } - $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL; + $str = 'SQL = ' . printable($sql) . EOL . 'SQL returned ' . $mesg . EOL; switch ($this->debug) { - case 3: - echo $str; - break; - default: - $debug_text .= $str; - break; - } + case 3: + echo $str; + break; + default: + $debug_text .= $str; + break; + } } if (($result === true) || ($result === false)) { @@ -76,9 +77,9 @@ class dba } if ($this->debug == 2) { - $debug_text .= printable(print_r($r, true). EOL); + $debug_text .= printable(print_r($r, true) . EOL); } elseif ($this->debug == 3) { - echo printable(print_r($r, true) . EOL) ; + echo printable(print_r($r, true) . EOL); } return $r; @@ -100,93 +101,73 @@ class dba } } -if (! function_exists('printable')) { - function printable($s) - { - $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~", ".", $s); - $s = str_replace("\x00", '.', $s); - if (x($_SERVER, 'SERVER_NAME')) { - $s = escape_tags($s); - } - return $s; +function printable($s) +{ + $s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~", ".", $s); + $s = str_replace("\x00", '.', $s); + if (x($_SERVER, 'SERVER_NAME')) { + $s = escape_tags($s); } + return $s; } - // Procedural functions -if (! function_exists('dbg')) { - function dbg($state) - { - global $db; - $db->dbg($state); - } +function dbg($state) +{ + global $db; + $db->dbg($state); } -if (! function_exists('dbesc')) { - function dbesc($str) - { - global $db; - if ($db) { - return($db->escape($str)); - } +function dbesc($str) +{ + global $db; + if ($db) { + return($db->escape($str)); } } - // Function: q($sql,$args); // Description: execute SQL query with printf style args. // Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d", // 'user', 1); -if (! function_exists('q')) { - function q($sql) - { - global $db; - $args = func_get_args(); - unset($args[0]); - if ($db) { - $ret = $db->q(vsprintf($sql, $args)); - } +function q($sql) +{ + global $db; + $args = func_get_args(); + unset($args[0]); + + if ($db) { + $ret = $db->q(vsprintf($sql, $args)); + if ($db->db->errno) { logger('dba: ' . $db->db->error); } - - - return $ret; } -} + return $ret; +} // Caller is responsible for ensuring that any integer arguments to // dbesc_array are actually integers and not malformed strings containing // SQL injection vectors. All integer array elements should be specifically // cast to int to avoid trouble. - - -if (! function_exists('dbesc_array_cb')) { - function dbesc_array_cb(&$item, $key) - { - if (is_string($item)) { - $item = dbesc($item); - } +function dbesc_array_cb(&$item, $key) +{ + if (is_string($item)) { + $item = dbesc($item); } } - -if (! function_exists('dbesc_array')) { - function dbesc_array(&$arr) - { - if (is_array($arr) && count($arr)) { - array_walk($arr, 'dbesc_array_cb'); - } +function dbesc_array(&$arr) +{ + if (is_array($arr) && count($arr)) { + array_walk($arr, 'dbesc_array_cb'); } } - -if (! function_exists('closedb')) { - function closedb() - { - global $db; -// $db->close(); - } +function closedb() +{ + global $db; + // $db->close(); } From 5559df82a6af6f088806346c581dad2baea4d394 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 12 May 2017 00:04:04 -0400 Subject: [PATCH 2/4] Formatting --- include/session.php | 117 +++++++++++++----------- src/Rendering/View.php | 197 ++++++++++++++++++++--------------------- 2 files changed, 160 insertions(+), 154 deletions(-) diff --git a/include/session.php b/include/session.php index 6c32e299..e51dbc1b 100644 --- a/include/session.php +++ b/include/session.php @@ -1,68 +1,73 @@ helpers; - } - - public function addHelper($name, Closure $helper) - { - $this->helpers[$name] = $helper; - } - - public function getView(){ - return $this->view; - } - - public function setView($value){ - $this->view = $value; - } - - public function getLayout(){ - return $this->layout; - } - - public function setLayout($value){ - $this->layout = $value; - } - - public function __construct($view=null, $layout="default") - { - - $this->view = $view; - $this->layout = $layout; - $this->helpers = array(); - - } - - public function render(array $data=array()) - { - - //First the outer view. - $view = self::getViewPath($this->view); - $viewContent = $this->encapsulatedRequire($view, $data); - - //Then the layout, including the view as $content. - $data['content'] = $viewContent; - $layout = self::getLayoutPath($this->layout); - return $this->encapsulatedRequire($layout, $data); - - } - - public function output(array $data=array()) - { - - header("Content-type: text/html; charset=utf-8"); - echo $this->render($data); exit; - - } - - public function encapsulatedRequire($filename, array $data=null) - { - - //This will provide our variables on the global scope. - $call = function($__FILE__, $__VARS__){ - extract($__VARS__, EXTR_SKIP); - require $__FILE__; - }; - - //Use our current data as fallback. - if(!is_array($data)){ - $data = $this->currentData; - } - - //This will add the helper class to $this. - $helpers = new ViewHelpers($this, $data); - $call = $call->bindTo($helpers, get_class($helpers)); - - //Run and return the value. - ob_start(); - $call($filename, $data); - return ob_get_clean(); - - } - -} \ No newline at end of file + #TODO: Replace this with better code. + + protected $layout; + protected $view; + protected $helpers; + + public static function getViewPath($name) + { + return dirname(__DIR__) . '/templates/view/' . $name . '.php'; + } + + public static function getLayoutPath($name) + { + return dirname(__DIR__) . '/templates/layout/' . $name . '.php'; + } + + public function getHelpers() + { + return $this->helpers; + } + + public function addHelper($name, Closure $helper) + { + $this->helpers[$name] = $helper; + } + + public function getView() + { + return $this->view; + } + + public function setView($value) + { + $this->view = $value; + } + + public function getLayout() + { + return $this->layout; + } + + public function setLayout($value) + { + $this->layout = $value; + } + + public function __construct($view = null, $layout = "default") + { + $this->view = $view; + $this->layout = $layout; + $this->helpers = array(); + } + + public function render(array $data = array()) + { + //First the outer view. + $view = self::getViewPath($this->view); + $viewContent = $this->encapsulatedRequire($view, $data); + + //Then the layout, including the view as $content. + $data['content'] = $viewContent; + $layout = self::getLayoutPath($this->layout); + return $this->encapsulatedRequire($layout, $data); + } + + public function output(array $data = array()) + { + header("Content-type: text/html; charset=utf-8"); + echo $this->render($data); + } + + public function encapsulatedRequire($filename, array $data = null) + { + //This will provide our variables on the global scope. + $call = function($__FILE__, $__VARS__) { + extract($__VARS__, EXTR_SKIP); + require $__FILE__; + }; + + //Use our current data as fallback. + if (!is_array($data)) { + $data = $this->currentData; + } + + //This will add the helper class to $this. + $helpers = new ViewHelpers($this, $data); + $call = $call->bindTo($helpers, get_class($helpers)); + + //Run and return the value. + ob_start(); + $call($filename, $data); + return ob_get_clean(); + } +} From 381e68edc78637865712dde8eae0e59e2faa5641 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 12 May 2017 00:04:37 -0400 Subject: [PATCH 3/4] Remove closedb() --- boot.php | 1 - include/dba.php | 6 ------ index.php | 1 - 3 files changed, 8 deletions(-) diff --git a/boot.php b/boot.php index 1c869c39..d2f8634e 100644 --- a/boot.php +++ b/boot.php @@ -206,7 +206,6 @@ if (!function_exists('killme')) { function killme() { session_write_close(); - closedb(); exit; } } diff --git a/include/dba.php b/include/dba.php index 37e1e173..705676f0 100644 --- a/include/dba.php +++ b/include/dba.php @@ -165,9 +165,3 @@ function dbesc_array(&$arr) array_walk($arr, 'dbesc_array_cb'); } } - -function closedb() -{ - global $db; - // $db->close(); -} diff --git a/index.php b/index.php index 325d6208..857424b9 100644 --- a/index.php +++ b/index.php @@ -115,6 +115,5 @@ require_once $template; session_write_close(); -closedb(); exit; From 2cabf7f37b0cc28c7e087bbc2076f2718045348c Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Fri, 12 May 2017 00:05:06 -0400 Subject: [PATCH 4/4] Generalize the use of killme() - Add fail condition to q() --- include/dba.php | 4 ++++ index.php | 7 +++---- src/Rendering/View.php | 1 - 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/dba.php b/include/dba.php index 705676f0..89f141d8 100644 --- a/include/dba.php +++ b/include/dba.php @@ -137,12 +137,16 @@ function q($sql) $args = func_get_args(); unset($args[0]); + $ret = null; + if ($db) { $ret = $db->q(vsprintf($sql, $args)); if ($db->db->errno) { logger('dba: ' . $db->db->error); } + } else { + error_log(__FILE__ . ':' . __LINE__ . ' $db has gone'); } return $ret; diff --git a/index.php b/index.php index 857424b9..082f0c0e 100644 --- a/index.php +++ b/index.php @@ -75,6 +75,8 @@ if ($a->module_loaded) { if ((!$a->error) && (function_exists($a->module . '_content'))) { $func = $a->module . '_content'; $a->page['content'] = $func($a); + + killme(); } } @@ -113,7 +115,4 @@ $template = 'view/' require_once $template; -session_write_close(); - - -exit; +killme(); diff --git a/src/Rendering/View.php b/src/Rendering/View.php index eae0f6fa..9d7e9862 100644 --- a/src/Rendering/View.php +++ b/src/Rendering/View.php @@ -9,7 +9,6 @@ use \Closure; */ class View { - exit; #TODO: Replace this with better code. protected $layout;