Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 40 additions & 39 deletions h2o/context.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ class H2o_Context implements ArrayAccess {
public $scopes;
public $options;
public $autoescape = true;

private $arrayMethods = array('first'=> 0, 'last'=> 1, 'length'=> 2, 'size'=> 3);
static $lookupTable = array();

function __construct($context = array(), $options = array()){
if (is_object($context))
$context = get_object_vars($context);
$this->scopes = array($context);
if (isset($options['safeClass']))

if (isset($options['safeClass']))
$this->safeClass = array_merge($this->safeClass, $options['safeClass']);
if (isset($options['autoescape']))

if (isset($options['autoescape']))
$this->autoescape = $options['autoescape'];

$this->options = $options;
}

Expand Down Expand Up @@ -54,13 +54,13 @@ function offsetGet($key) {
}
return;
}

function offsetSet($key, $value) {
if (strpos($key, '.') > -1)
throw new Exception('cannot set non local variable');
return $this->scopes[0][$key] = $value;
}

function offsetUnset($key) {
foreach ($this->scopes as $layer) {
if (isset($layer[$key])) unset($layer[$key]);
Expand All @@ -83,11 +83,11 @@ function isDefined($key) {
return $this->offsetExists($key);
}
/**
*
*
*
*
*
*
* Variable name
*
*
* @param $var variable name or array(0 => variable name, 'filters' => filters array)
* @return unknown_type
*/
Expand All @@ -96,15 +96,15 @@ function resolve($var) {
# if $var is array - it contains filters to apply
$filters = array();
if ( is_array($var) ) {

$name = array_shift($var);
$filters = isset($var['filters'])? $var['filters'] : array();
}

}
else $name = $var;

$result = null;

# Lookup basic types, null, boolean, numeric and string
# Variable starts with : (:users.name) to short-circuit lookup
if ($name[0] === ':') {
Expand All @@ -116,12 +116,12 @@ function resolve($var) {
}
elseif ($name === 'false') {
$result = false;
}
}
elseif (preg_match('/^-?\d+(\.\d+)?$/', $name, $matches)) {
$result = isset($matches[1])? floatval($name) : intval($name);
}
elseif (preg_match('/^"([^"\\\\]*(?:\\.[^"\\\\]*)*)"|' .
'\'([^\'\\\\]*(?:\\.[^\'\\\\]*)*)\'$/', $name)) {
'\'([^\'\\\\]*(?:\\.[^\'\\\\]*)*)\'$/', $name)) {
$result = stripcslashes(substr($name, 1, -1));
}
}
Expand All @@ -131,7 +131,7 @@ function resolve($var) {
$result = $this->applyFilters($result,$filters);
return $result;
}

function getVariable($name) {
# Local variables. this gives as a bit of performance improvement
if (!strpos($name, '.'))
Expand All @@ -147,10 +147,11 @@ function getVariable($name) {
if (isset($object[$part])) {
$object = $object[$part];
} elseif ($part === 'first') {
$object = isset($object[0])?$object[0]:null;
$o = array_slice($object, 0, 1);
$object = count($o) ? $o[0] : null;
} elseif ($part === 'last') {
$last = count($object)-1;
$object = isset($object[$last])?$object[$last]:null;
$o = array_slice($object, -1, 1);
$object = count($o) ? $o[0] : null;
} elseif ($part === 'size' or $part === 'length') {
return count($object);
} else {
Expand All @@ -161,7 +162,7 @@ function getVariable($name) {
if (isset($object->$part))
$object = $object->$part;
elseif (is_callable(array($object, $part))) {
$methodAllowed = in_array(get_class($object), $this->safeClass) ||
$methodAllowed = in_array(get_class($object), $this->safeClass) ||
(isset($object->h2o_safe) && (
$object->h2o_safe === true || in_array($part, $object->h2o_safe)
)
Expand All @@ -176,12 +177,12 @@ function getVariable($name) {
}

function applyFilters($object, $filters) {

foreach ($filters as $filter) {
$name = substr(array_shift($filter), 1);
$args = $filter;
if (isset(h2o::$filters[$name])) {

if (isset(h2o::$filters[$name])) {
foreach ($args as $i => $argument) {
# name args
if (is_array($argument)) {
Expand All @@ -201,26 +202,26 @@ function applyFilters($object, $filters) {
}

function escape($value, $var) {

$safe = false;
$filters = (is_array($var) && isset($var['filters']))? $var['filters'] : array();

foreach ( $filters as $filter ) {

$name = substr(array_shift($filter), 1);
$safe = !$safe && ($name === 'safe');

$escaped = $name === 'escape';
}

$should_escape = $this->autoescape || isset($escaped) && $escaped;

if ( ($should_escape && !$safe)) {
$value = htmlspecialchars($value);
}
}

return $value;
}
}

function externalLookup($name) {
if (!empty(self::$lookupTable)) {
Expand All @@ -238,7 +239,7 @@ class BlockContext {
var $h2o_safe = array('name', 'depth', 'super');
var $block, $index;
private $context;

function __construct($block, $context, $index) {
$this->block =& $block;
$this->context = $context;
Expand All @@ -256,9 +257,9 @@ function depth() {
function super() {
$stream = new StreamWriter;
$this->block->parent->render($this->context, $stream, $this->index+1);
return $stream->close();
return $stream->close();
}

function __toString() {
return "[BlockContext : {$this->block->name}, {$this->block->filename}]";
}
Expand Down
Loading