Merge pull request #21 from Hypolite/bug/fix-dba-missing-db
Fix missing $db when session write on exit
This commit is contained in:
commit
9a4ac58f67
5 changed files with 211 additions and 230 deletions
1
boot.php
1
boot.php
|
@ -206,7 +206,6 @@ if (!function_exists('killme')) {
|
||||||
function killme()
|
function killme()
|
||||||
{
|
{
|
||||||
session_write_close();
|
session_write_close();
|
||||||
closedb();
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
127
include/dba.php
127
include/dba.php
|
@ -16,7 +16,8 @@ class dba
|
||||||
public function __construct($server, $user, $pass, $db, $install = false)
|
public function __construct($server, $user, $pass, $db, $install = false)
|
||||||
{
|
{
|
||||||
$this->db = @new mysqli($server, $user, $pass, $db);
|
$this->db = @new mysqli($server, $user, $pass, $db);
|
||||||
if ((mysqli_connect_errno()) && (! install)) {
|
|
||||||
|
if (mysqli_connect_errno() && ! $install) {
|
||||||
system_unavailable();
|
system_unavailable();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,7 +41,7 @@ class dba
|
||||||
$mesg = '';
|
$mesg = '';
|
||||||
|
|
||||||
if ($this->db->mysqli->errno) {
|
if ($this->db->mysqli->errno) {
|
||||||
$debug_text .= $this->db->mysqli->error . EOL;
|
$debug_text .= $this->db->mysqli->error . EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($result === false) {
|
if ($result === false) {
|
||||||
|
@ -48,19 +49,19 @@ class dba
|
||||||
} elseif ($result === true) {
|
} elseif ($result === true) {
|
||||||
$mesg = 'true';
|
$mesg = 'true';
|
||||||
} else {
|
} 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) {
|
switch ($this->debug) {
|
||||||
case 3:
|
case 3:
|
||||||
echo $str;
|
echo $str;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$debug_text .= $str;
|
$debug_text .= $str;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (($result === true) || ($result === false)) {
|
if (($result === true) || ($result === false)) {
|
||||||
|
@ -76,9 +77,9 @@ class dba
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->debug == 2) {
|
if ($this->debug == 2) {
|
||||||
$debug_text .= printable(print_r($r, true). EOL);
|
$debug_text .= printable(print_r($r, true) . EOL);
|
||||||
} elseif ($this->debug == 3) {
|
} elseif ($this->debug == 3) {
|
||||||
echo printable(print_r($r, true) . EOL) ;
|
echo printable(print_r($r, true) . EOL);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $r;
|
return $r;
|
||||||
|
@ -100,93 +101,71 @@ class dba
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! function_exists('printable')) {
|
function printable($s)
|
||||||
function printable($s)
|
{
|
||||||
{
|
$s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~", ".", $s);
|
||||||
$s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~", ".", $s);
|
$s = str_replace("\x00", '.', $s);
|
||||||
$s = str_replace("\x00", '.', $s);
|
if (x($_SERVER, 'SERVER_NAME')) {
|
||||||
if (x($_SERVER, 'SERVER_NAME')) {
|
$s = escape_tags($s);
|
||||||
$s = escape_tags($s);
|
|
||||||
}
|
|
||||||
return $s;
|
|
||||||
}
|
}
|
||||||
|
return $s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Procedural functions
|
// Procedural functions
|
||||||
if (! function_exists('dbg')) {
|
function dbg($state)
|
||||||
function dbg($state)
|
{
|
||||||
{
|
global $db;
|
||||||
global $db;
|
$db->dbg($state);
|
||||||
$db->dbg($state);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! function_exists('dbesc')) {
|
function dbesc($str)
|
||||||
function dbesc($str)
|
{
|
||||||
{
|
global $db;
|
||||||
global $db;
|
if ($db) {
|
||||||
if ($db) {
|
return($db->escape($str));
|
||||||
return($db->escape($str));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Function: q($sql,$args);
|
// Function: q($sql,$args);
|
||||||
// Description: execute SQL query with printf style args.
|
// Description: execute SQL query with printf style args.
|
||||||
// Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
|
// Example: $r = q("SELECT * FROM `%s` WHERE `uid` = %d",
|
||||||
// 'user', 1);
|
// 'user', 1);
|
||||||
|
|
||||||
if (! function_exists('q')) {
|
function q($sql)
|
||||||
function q($sql)
|
{
|
||||||
{
|
global $db;
|
||||||
global $db;
|
$args = func_get_args();
|
||||||
$args = func_get_args();
|
unset($args[0]);
|
||||||
unset($args[0]);
|
|
||||||
if ($db) {
|
$ret = null;
|
||||||
$ret = $db->q(vsprintf($sql, $args));
|
|
||||||
}
|
if ($db) {
|
||||||
|
$ret = $db->q(vsprintf($sql, $args));
|
||||||
|
|
||||||
if ($db->db->errno) {
|
if ($db->db->errno) {
|
||||||
logger('dba: ' . $db->db->error);
|
logger('dba: ' . $db->db->error);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
error_log(__FILE__ . ':' . __LINE__ . ' $db has gone');
|
||||||
return $ret;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
|
|
||||||
// Caller is responsible for ensuring that any integer arguments to
|
// Caller is responsible for ensuring that any integer arguments to
|
||||||
// dbesc_array are actually integers and not malformed strings containing
|
// dbesc_array are actually integers and not malformed strings containing
|
||||||
// SQL injection vectors. All integer array elements should be specifically
|
// SQL injection vectors. All integer array elements should be specifically
|
||||||
// cast to int to avoid trouble.
|
// cast to int to avoid trouble.
|
||||||
|
function dbesc_array_cb(&$item, $key)
|
||||||
|
{
|
||||||
if (! function_exists('dbesc_array_cb')) {
|
if (is_string($item)) {
|
||||||
function dbesc_array_cb(&$item, $key)
|
$item = dbesc($item);
|
||||||
{
|
|
||||||
if (is_string($item)) {
|
|
||||||
$item = dbesc($item);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function dbesc_array(&$arr)
|
||||||
if (! function_exists('dbesc_array')) {
|
{
|
||||||
function dbesc_array(&$arr)
|
if (is_array($arr) && count($arr)) {
|
||||||
{
|
array_walk($arr, 'dbesc_array_cb');
|
||||||
if (is_array($arr) && count($arr)) {
|
|
||||||
array_walk($arr, 'dbesc_array_cb');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (! function_exists('closedb')) {
|
|
||||||
function closedb()
|
|
||||||
{
|
|
||||||
global $db;
|
|
||||||
// $db->close();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,68 +1,73 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// Session management functions. These provide database storage of PHP
|
// Session management functions. These provide database storage of PHP
|
||||||
// session info.
|
// session info.
|
||||||
|
|
||||||
$session_exists = 0;
|
$session_exists = 0;
|
||||||
$session_expire = 180000;
|
$session_expire = 180000;
|
||||||
|
|
||||||
if(! function_exists('ref_session_open')) {
|
function ref_session_open($s, $n)
|
||||||
function ref_session_open ($s,$n) {
|
{
|
||||||
return true;
|
return true;
|
||||||
}}
|
}
|
||||||
|
|
||||||
if(! function_exists('ref_session_read')) {
|
function ref_session_read($id)
|
||||||
function ref_session_read ($id) {
|
{
|
||||||
global $session_exists;
|
global $session_exists;
|
||||||
if(x($id))
|
|
||||||
$r = q("SELECT `data` FROM `session` WHERE `sid`= '%s'", dbesc($id));
|
|
||||||
if(count($r)) {
|
|
||||||
$session_exists = true;
|
|
||||||
return $r[0]['data'];
|
|
||||||
}
|
|
||||||
return '';
|
|
||||||
}}
|
|
||||||
|
|
||||||
if(! function_exists('ref_session_write')) {
|
if (x($id)) {
|
||||||
function ref_session_write ($id,$data) {
|
$r = q("SELECT `data` FROM `session` WHERE `sid`= '%s'", dbesc($id));
|
||||||
global $session_exists, $session_expire;
|
}
|
||||||
if(! $id || ! $data) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$expire = time() + $session_expire;
|
if (count($r)) {
|
||||||
$default_expire = time() + 300;
|
$session_exists = true;
|
||||||
|
return $r[0]['data'];
|
||||||
|
}
|
||||||
|
|
||||||
if($session_exists)
|
return '';
|
||||||
$r = q("UPDATE `session`
|
}
|
||||||
SET `data` = '%s', `expire` = '%s'
|
|
||||||
WHERE `sid` = '%s' LIMIT 1",
|
|
||||||
dbesc($data), dbesc($expire), dbesc($id));
|
|
||||||
else
|
|
||||||
$r = q("INSERT INTO `session`
|
|
||||||
SET `sid` = '%s', `expire` = '%s', `data` = '%s'",
|
|
||||||
dbesc($id), dbesc($default_expire), dbesc($data));
|
|
||||||
|
|
||||||
return true;
|
function ref_session_write($id, $data)
|
||||||
}}
|
{
|
||||||
|
global $session_exists, $session_expire;
|
||||||
|
|
||||||
if(! function_exists('ref_session_close')) {
|
if (!$id || !$data) {
|
||||||
function ref_session_close() {
|
return false;
|
||||||
return true;
|
}
|
||||||
}}
|
|
||||||
|
|
||||||
if(! function_exists('ref_session_destroy')) {
|
$expire = time() + $session_expire;
|
||||||
function ref_session_destroy ($id) {
|
$default_expire = time() + 300;
|
||||||
q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id));
|
|
||||||
return true;
|
|
||||||
}}
|
|
||||||
|
|
||||||
if(! function_exists('ref_session_gc')) {
|
if ($session_exists) {
|
||||||
function ref_session_gc($expire) {
|
$r = q("UPDATE `session`
|
||||||
q("DELETE FROM `session` WHERE `expire` < %d", dbesc(time()));
|
SET `data` = '%s', `expire` = '%s'
|
||||||
q("OPTIMIZE TABLE `sess_data`");
|
WHERE `sid` = '%s' LIMIT 1", dbesc($data), dbesc($expire), dbesc($id));
|
||||||
return true;
|
} else {
|
||||||
}}
|
$r = q("INSERT INTO `session`
|
||||||
|
SET `sid` = '%s', `expire` = '%s', `data` = '%s'", dbesc($id), dbesc($default_expire), dbesc($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ref_session_close()
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ref_session_destroy($id)
|
||||||
|
{
|
||||||
|
q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id));
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function ref_session_gc($expire)
|
||||||
|
{
|
||||||
|
q("DELETE FROM `session` WHERE `expire` < %d", dbesc(time()));
|
||||||
|
q("OPTIMIZE TABLE `sess_data`");
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
$gc_probability = 50;
|
$gc_probability = 50;
|
||||||
|
|
||||||
|
@ -70,7 +75,11 @@ ini_set('session.gc_probability', $gc_probability);
|
||||||
ini_set('session.use_only_cookies', 1);
|
ini_set('session.use_only_cookies', 1);
|
||||||
ini_set('session.cookie_httponly', 1);
|
ini_set('session.cookie_httponly', 1);
|
||||||
|
|
||||||
|
session_set_save_handler(
|
||||||
session_set_save_handler ('ref_session_open', 'ref_session_close',
|
'ref_session_open',
|
||||||
'ref_session_read', 'ref_session_write',
|
'ref_session_close',
|
||||||
'ref_session_destroy', 'ref_session_gc');
|
'ref_session_read',
|
||||||
|
'ref_session_write',
|
||||||
|
'ref_session_destroy',
|
||||||
|
'ref_session_gc'
|
||||||
|
);
|
||||||
|
|
|
@ -75,6 +75,8 @@ if ($a->module_loaded) {
|
||||||
if ((!$a->error) && (function_exists($a->module . '_content'))) {
|
if ((!$a->error) && (function_exists($a->module . '_content'))) {
|
||||||
$func = $a->module . '_content';
|
$func = $a->module . '_content';
|
||||||
$a->page['content'] = $func($a);
|
$a->page['content'] = $func($a);
|
||||||
|
|
||||||
|
killme();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,8 +115,4 @@ $template = 'view/'
|
||||||
|
|
||||||
require_once $template;
|
require_once $template;
|
||||||
|
|
||||||
session_write_close();
|
killme();
|
||||||
|
|
||||||
closedb();
|
|
||||||
|
|
||||||
exit;
|
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
<?php namespace Friendica\Directory\Rendering;
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Directory\Rendering;
|
||||||
|
|
||||||
use \Closure;
|
use \Closure;
|
||||||
|
|
||||||
|
@ -7,103 +9,97 @@ use \Closure;
|
||||||
*/
|
*/
|
||||||
class View
|
class View
|
||||||
{
|
{
|
||||||
|
#TODO: Replace this with better code.
|
||||||
|
|
||||||
#TODO: Replace this with better code.
|
protected $layout;
|
||||||
|
protected $view;
|
||||||
|
protected $helpers;
|
||||||
|
|
||||||
public static function getViewPath($name)
|
public static function getViewPath($name)
|
||||||
{
|
{
|
||||||
return dirname(__DIR__).'/templates/view/'.$name.'.php';
|
return dirname(__DIR__) . '/templates/view/' . $name . '.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getLayoutPath($name)
|
public static function getLayoutPath($name)
|
||||||
{
|
{
|
||||||
return dirname(__DIR__).'/templates/layout/'.$name.'.php';
|
return dirname(__DIR__) . '/templates/layout/' . $name . '.php';
|
||||||
}
|
}
|
||||||
|
|
||||||
protected $layout;
|
public function getHelpers()
|
||||||
protected $view;
|
{
|
||||||
protected $helpers;
|
return $this->helpers;
|
||||||
|
}
|
||||||
|
|
||||||
public function getHelpers(){
|
public function addHelper($name, Closure $helper)
|
||||||
return $this->helpers;
|
{
|
||||||
}
|
$this->helpers[$name] = $helper;
|
||||||
|
}
|
||||||
|
|
||||||
public function addHelper($name, Closure $helper)
|
public function getView()
|
||||||
{
|
{
|
||||||
$this->helpers[$name] = $helper;
|
return $this->view;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getView(){
|
public function setView($value)
|
||||||
return $this->view;
|
{
|
||||||
}
|
$this->view = $value;
|
||||||
|
}
|
||||||
|
|
||||||
public function setView($value){
|
public function getLayout()
|
||||||
$this->view = $value;
|
{
|
||||||
}
|
return $this->layout;
|
||||||
|
}
|
||||||
|
|
||||||
public function getLayout(){
|
public function setLayout($value)
|
||||||
return $this->layout;
|
{
|
||||||
}
|
$this->layout = $value;
|
||||||
|
}
|
||||||
|
|
||||||
public function setLayout($value){
|
public function __construct($view = null, $layout = "default")
|
||||||
$this->layout = $value;
|
{
|
||||||
}
|
$this->view = $view;
|
||||||
|
$this->layout = $layout;
|
||||||
|
$this->helpers = array();
|
||||||
|
}
|
||||||
|
|
||||||
public function __construct($view=null, $layout="default")
|
public function render(array $data = array())
|
||||||
{
|
{
|
||||||
|
//First the outer view.
|
||||||
|
$view = self::getViewPath($this->view);
|
||||||
|
$viewContent = $this->encapsulatedRequire($view, $data);
|
||||||
|
|
||||||
$this->view = $view;
|
//Then the layout, including the view as $content.
|
||||||
$this->layout = $layout;
|
$data['content'] = $viewContent;
|
||||||
$this->helpers = array();
|
$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 render(array $data=array())
|
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__;
|
||||||
|
};
|
||||||
|
|
||||||
//First the outer view.
|
//Use our current data as fallback.
|
||||||
$view = self::getViewPath($this->view);
|
if (!is_array($data)) {
|
||||||
$viewContent = $this->encapsulatedRequire($view, $data);
|
$data = $this->currentData;
|
||||||
|
}
|
||||||
|
|
||||||
//Then the layout, including the view as $content.
|
//This will add the helper class to $this.
|
||||||
$data['content'] = $viewContent;
|
$helpers = new ViewHelpers($this, $data);
|
||||||
$layout = self::getLayoutPath($this->layout);
|
$call = $call->bindTo($helpers, get_class($helpers));
|
||||||
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();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
//Run and return the value.
|
||||||
|
ob_start();
|
||||||
|
$call($filename, $data);
|
||||||
|
return ob_get_clean();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue