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()
|
||||
{
|
||||
session_write_close();
|
||||
closedb();
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -100,7 +101,6 @@ class dba
|
|||
}
|
||||
}
|
||||
|
||||
if (! function_exists('printable')) {
|
||||
function printable($s)
|
||||
{
|
||||
$s = preg_replace("~([\x01-\x08\x0E-\x0F\x10-\x1F\x7F-\xFF])~", ".", $s);
|
||||
|
@ -110,19 +110,14 @@ if (! function_exists('printable')) {
|
|||
}
|
||||
return $s;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Procedural functions
|
||||
if (! function_exists('dbg')) {
|
||||
function dbg($state)
|
||||
{
|
||||
global $db;
|
||||
$db->dbg($state);
|
||||
}
|
||||
}
|
||||
|
||||
if (! function_exists('dbesc')) {
|
||||
function dbesc($str)
|
||||
{
|
||||
global $db;
|
||||
|
@ -130,63 +125,47 @@ if (! function_exists('dbesc')) {
|
|||
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]);
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (! function_exists('dbesc_array')) {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,31 +1,35 @@
|
|||
<?php
|
||||
|
||||
// Session management functions. These provide database storage of PHP
|
||||
// session info.
|
||||
|
||||
$session_exists = 0;
|
||||
$session_expire = 180000;
|
||||
|
||||
if(! function_exists('ref_session_open')) {
|
||||
function ref_session_open ($s,$n) {
|
||||
function ref_session_open($s, $n)
|
||||
{
|
||||
return true;
|
||||
}}
|
||||
}
|
||||
|
||||
if(! function_exists('ref_session_read')) {
|
||||
function ref_session_read ($id) {
|
||||
function ref_session_read($id)
|
||||
{
|
||||
global $session_exists;
|
||||
if(x($id))
|
||||
|
||||
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')) {
|
||||
function ref_session_write ($id,$data) {
|
||||
return '';
|
||||
}
|
||||
|
||||
function ref_session_write($id, $data)
|
||||
{
|
||||
global $session_exists, $session_expire;
|
||||
|
||||
if (!$id || !$data) {
|
||||
return false;
|
||||
}
|
||||
|
@ -33,36 +37,37 @@ function ref_session_write ($id,$data) {
|
|||
$expire = time() + $session_expire;
|
||||
$default_expire = time() + 300;
|
||||
|
||||
if($session_exists)
|
||||
if ($session_exists) {
|
||||
$r = q("UPDATE `session`
|
||||
SET `data` = '%s', `expire` = '%s'
|
||||
WHERE `sid` = '%s' LIMIT 1",
|
||||
dbesc($data), dbesc($expire), dbesc($id));
|
||||
else
|
||||
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));
|
||||
SET `sid` = '%s', `expire` = '%s', `data` = '%s'", dbesc($id), dbesc($default_expire), dbesc($data));
|
||||
}
|
||||
|
||||
return true;
|
||||
}}
|
||||
}
|
||||
|
||||
if(! function_exists('ref_session_close')) {
|
||||
function ref_session_close() {
|
||||
function ref_session_close()
|
||||
{
|
||||
return true;
|
||||
}}
|
||||
}
|
||||
|
||||
if(! function_exists('ref_session_destroy')) {
|
||||
function ref_session_destroy ($id) {
|
||||
function ref_session_destroy($id)
|
||||
{
|
||||
q("DELETE FROM `session` WHERE `sid` = '%s'", dbesc($id));
|
||||
return true;
|
||||
}}
|
||||
|
||||
if(! function_exists('ref_session_gc')) {
|
||||
function ref_session_gc($expire) {
|
||||
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;
|
||||
|
||||
|
@ -70,7 +75,11 @@ ini_set('session.gc_probability', $gc_probability);
|
|||
ini_set('session.use_only_cookies', 1);
|
||||
ini_set('session.cookie_httponly', 1);
|
||||
|
||||
|
||||
session_set_save_handler ('ref_session_open', 'ref_session_close',
|
||||
'ref_session_read', 'ref_session_write',
|
||||
'ref_session_destroy', 'ref_session_gc');
|
||||
session_set_save_handler(
|
||||
'ref_session_open',
|
||||
'ref_session_close',
|
||||
'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'))) {
|
||||
$func = $a->module . '_content';
|
||||
$a->page['content'] = $func($a);
|
||||
|
||||
killme();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -113,8 +115,4 @@ $template = 'view/'
|
|||
|
||||
require_once $template;
|
||||
|
||||
session_write_close();
|
||||
|
||||
closedb();
|
||||
|
||||
exit;
|
||||
killme();
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
<?php namespace Friendica\Directory\Rendering;
|
||||
<?php
|
||||
|
||||
namespace Friendica\Directory\Rendering;
|
||||
|
||||
use \Closure;
|
||||
|
||||
|
@ -7,9 +9,12 @@ use \Closure;
|
|||
*/
|
||||
class View
|
||||
{
|
||||
|
||||
#TODO: Replace this with better code.
|
||||
|
||||
protected $layout;
|
||||
protected $view;
|
||||
protected $helpers;
|
||||
|
||||
public static function getViewPath($name)
|
||||
{
|
||||
return dirname(__DIR__) . '/templates/view/' . $name . '.php';
|
||||
|
@ -20,11 +25,8 @@ class View
|
|||
return dirname(__DIR__) . '/templates/layout/' . $name . '.php';
|
||||
}
|
||||
|
||||
protected $layout;
|
||||
protected $view;
|
||||
protected $helpers;
|
||||
|
||||
public function getHelpers(){
|
||||
public function getHelpers()
|
||||
{
|
||||
return $this->helpers;
|
||||
}
|
||||
|
||||
|
@ -33,34 +35,35 @@ class View
|
|||
$this->helpers[$name] = $helper;
|
||||
}
|
||||
|
||||
public function getView(){
|
||||
public function getView()
|
||||
{
|
||||
return $this->view;
|
||||
}
|
||||
|
||||
public function setView($value){
|
||||
public function setView($value)
|
||||
{
|
||||
$this->view = $value;
|
||||
}
|
||||
|
||||
public function getLayout(){
|
||||
public function getLayout()
|
||||
{
|
||||
return $this->layout;
|
||||
}
|
||||
|
||||
public function setLayout($value){
|
||||
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);
|
||||
|
@ -69,21 +72,16 @@ class View
|
|||
$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);
|
||||
|
@ -103,7 +101,5 @@ class View
|
|||
ob_start();
|
||||
$call($filename, $data);
|
||||
return ob_get_clean();
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue