Formatting
This commit is contained in:
parent
4ea1211ea8
commit
5559df82a6
2 changed files with 160 additions and 154 deletions
|
@ -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'
|
||||||
|
);
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
<?php namespace Friendica\Directory\Rendering;
|
<?php
|
||||||
|
|
||||||
|
namespace Friendica\Directory\Rendering;
|
||||||
|
|
||||||
use \Closure;
|
use \Closure;
|
||||||
|
|
||||||
|
@ -7,103 +9,98 @@ use \Closure;
|
||||||
*/
|
*/
|
||||||
class View
|
class View
|
||||||
{
|
{
|
||||||
|
|
||||||
#TODO: Replace this with better code.
|
|
||||||
|
|
||||||
public static function getViewPath($name)
|
|
||||||
{
|
|
||||||
return dirname(__DIR__).'/templates/view/'.$name.'.php';
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function getLayoutPath($name)
|
|
||||||
{
|
|
||||||
return dirname(__DIR__).'/templates/layout/'.$name.'.php';
|
|
||||||
}
|
|
||||||
|
|
||||||
protected $layout;
|
|
||||||
protected $view;
|
|
||||||
protected $helpers;
|
|
||||||
|
|
||||||
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);
|
|
||||||
exit;
|
exit;
|
||||||
|
#TODO: Replace this with better code.
|
||||||
}
|
|
||||||
|
protected $layout;
|
||||||
public function encapsulatedRequire($filename, array $data=null)
|
protected $view;
|
||||||
{
|
protected $helpers;
|
||||||
|
|
||||||
//This will provide our variables on the global scope.
|
public static function getViewPath($name)
|
||||||
$call = function($__FILE__, $__VARS__){
|
{
|
||||||
extract($__VARS__, EXTR_SKIP);
|
return dirname(__DIR__) . '/templates/view/' . $name . '.php';
|
||||||
require $__FILE__;
|
}
|
||||||
};
|
|
||||||
|
public static function getLayoutPath($name)
|
||||||
//Use our current data as fallback.
|
{
|
||||||
if(!is_array($data)){
|
return dirname(__DIR__) . '/templates/layout/' . $name . '.php';
|
||||||
$data = $this->currentData;
|
}
|
||||||
}
|
|
||||||
|
public function getHelpers()
|
||||||
//This will add the helper class to $this.
|
{
|
||||||
$helpers = new ViewHelpers($this, $data);
|
return $this->helpers;
|
||||||
$call = $call->bindTo($helpers, get_class($helpers));
|
}
|
||||||
|
|
||||||
//Run and return the value.
|
public function addHelper($name, Closure $helper)
|
||||||
ob_start();
|
{
|
||||||
$call($filename, $data);
|
$this->helpers[$name] = $helper;
|
||||||
return ob_get_clean();
|
}
|
||||||
|
|
||||||
}
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue