| 2 |
|
-<?php define('DWOO_DIRECTORY', dirname(__FILE__) . DIRECTORY_SEPARATOR); class Dwoo { const VERSION = '1.1.1'; const RELEASE_TAG = 17; const CLASS_PLUGIN = 1; const FUNC_PLUGIN = 2; const NATIVE_PLUGIN = 4; const BLOCK_PLUGIN = 8; const COMPILABLE_PLUGIN = 16; const CUSTOM_PLUGIN = 32; const SMARTY_MODIFIER = 64; const SMARTY_BLOCK = 128; const SMARTY_FUNCTION = 256; const PROXY_PLUGIN = 512; const TEMPLATE_PLUGIN = 1024; protected $charset = 'utf-8'; public $globals; protected $compileDir; protected $cacheDir; protected $cacheTime = 0; protected $securityPolicy = null; protected $plugins = array(); protected $filters = array(); protected $resources = array ( 'file' => array ( 'class' => 'Dwoo_Template_File', 'compiler' => null ), 'string' => array ( 'class' => 'Dwoo_Template_String', 'compiler' => null ) ); protected $loader = null; protected $template = null; protected $runtimePlugins; public $data; public $scope; protected $scopeTree; protected $stack; protected $curBlock; protected $buffer; protected $pluginProxy; public function __construct($compileDir = null, $cacheDir = null) { if ($compileDir !== null) { $this->setCompileDir($compileDir); } if ($cacheDir !== null) { $this->setCacheDir($cacheDir); } $this->initGlobals(); } public function __clone() { $this->template = null; unset($this->data); } public function output($tpl, $data = array(), Dwoo_ICompiler $compiler = null) { return $this->get($tpl, $data, $compiler, true); } public function get($_tpl, $data = array(), $_compiler = null, $_output = false) { if ($this->template instanceof Dwoo_ITemplate) { $proxy = clone $this; return $proxy->get($_tpl, $data, $_compiler, $_output); } if ($_tpl instanceof Dwoo_ITemplate) { } elseif (is_string($_tpl) && file_exists($_tpl)) { $_tpl = new Dwoo_Template_File($_tpl); } else { throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE); } $this->template = $_tpl; if ($data instanceof Dwoo_IDataProvider) { $this->data = $data->getData(); } elseif (is_array($data)) { $this->data = $data; } else { throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE); } $this->globals['template'] = $_tpl->getName(); $this->initRuntimeVars($_tpl); $file = $_tpl->getCachedTemplate($this); $doCache = $file === true; $cacheLoaded = is_string($file); if ($cacheLoaded === true) { if ($_output === true) { include $file; $this->template = null; } else { ob_start(); include $file; $this->template = null; return ob_get_clean(); } } else { if ($doCache === true) { $dynamicId = uniqid(); } $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); $out = include $compiledTemplate; if ($out === false) { $_tpl->forceCompilation(); $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); $out = include $compiledTemplate; } if ($doCache === true) { $out = preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php /*'.$dynamicId.'*/ echo \'$1\'; ?>', $out); if (!class_exists('Dwoo_plugin_dynamic', false)) { $this->getLoader()->loadPlugin('dynamic'); } $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate); } foreach ($this->filters as $filter) { if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) { $out = call_user_func($filter, $out); } else { $out = call_user_func($filter, $this, $out); } } if ($doCache === true) { $file = $_tpl->cache($this, $out); if ($_output === true) { include $file; $this->template = null; } else { ob_start(); include $file; $this->template = null; return ob_get_clean(); } } else { $this->template = null; if ($_output === true) { echo $out; } return $out; } } } protected function initGlobals() { $this->globals = array ( 'version' => self::VERSION, 'ad' => '<a href="http://dwoo.org/">Powered by Dwoo</a>', 'now' => $_SERVER['REQUEST_TIME'], 'charset' => $this->charset, ); } protected function initRuntimeVars(Dwoo_ITemplate $tpl) { $this->runtimePlugins = array(); $this->scope =& $this->data; $this->scopeTree = array(); $this->stack = array(); $this->curBlock = null; $this->buffer = ''; } public function addPlugin($name, $callback, $compilable = false) { $compilable = $compilable ? self::COMPILABLE_PLUGIN : 0; if (is_array($callback)) { if (is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'Dwoo_Block_Plugin')) { $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0])); } else { $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]); } } elseif (class_exists($callback, false)) { if (is_subclass_of($callback, 'Dwoo_Block_Plugin')) { $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback); } else { $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process'); } } elseif (function_exists($callback)) { $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN | $compilable, 'callback'=>$callback); } else { throw new Dwoo_Exception('Callback could not be processed correctly, please check that the function/class you used exists'); } } public function removePlugin($name) { if (isset($this->plugins[$name])) { unset($this->plugins[$name]); } } public function addFilter($callback, $autoload = false) { if ($autoload) { $class = 'Dwoo_Filter_'.$callback; if (!class_exists($class, false) && !function_exists($class)) { try { $this->getLoader()->loadPlugin($callback); } catch (Dwoo_Exception $e) { if (strstr($callback, 'Dwoo_Filter_')) { throw new Dwoo_Exception('Wrong filter name : '.$callback.', the "Dwoo_Filter_" prefix should not be used, please only use "'.str_replace('Dwoo_Filter_', '', $callback).'"'); } else { throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); } } } if (class_exists($class, false)) { $callback = array(new $class($this), 'process'); } elseif (function_exists($class)) { $callback = $class; } else { throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); } $this->filters[] = $callback; } else { $this->filters[] = $callback; } } public function removeFilter($callback) { if (($index = array_search('Dwoo_Filter_'.$callback, $this->filters, true)) !== false) { unset($this->filters[$index]); } elseif (($index = array_search($callback, $this->filters, true)) !== false) { unset($this->filters[$index]); } else { $class = 'Dwoo_Filter_' . $callback; foreach ($this->filters as $index=>$filter) { if (is_array($filter) && $filter[0] instanceof $class) { unset($this->filters[$index]); break; } } } } public function addResource($name, $class, $compilerFactory = null) { if (strlen($name) < 2) { throw new Dwoo_Exception('Resource names must be at least two-character long to avoid conflicts with Windows paths'); } if (!class_exists($class)) { throw new Dwoo_Exception('Resource class does not exist'); } $interfaces = class_implements($class); if (in_array('Dwoo_ITemplate', $interfaces) === false) { throw new Dwoo_Exception('Resource class must implement Dwoo_ITemplate'); } $this->resources[$name] = array('class'=>$class, 'compiler'=>$compilerFactory); } public function removeResource($name) { unset($this->resources[$name]); if ($name==='file') { $this->resources['file'] = array('class'=>'Dwoo_Template_File', 'compiler'=>null); } } public function setLoader(Dwoo_ILoader $loader) { $this->loader = $loader; } public function getLoader() { if ($this->loader === null) { $this->loader = new Dwoo_Loader($this->getCompileDir()); } return $this->loader; } public function getCustomPlugins() { return $this->plugins; } public function getCacheDir() { if ($this->cacheDir === null) { $this->setCacheDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR); } return $this->cacheDir; } public function setCacheDir($dir) { $this->cacheDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; if (is_writable($this->cacheDir) === false) { throw new Dwoo_Exception('The cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable'); } } public function getCompileDir() { if ($this->compileDir === null) { $this->setCompileDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'compiled'.DIRECTORY_SEPARATOR); } return $this->compileDir; } public function setCompileDir($dir) { $this->compileDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; if (is_writable($this->compileDir) === false) { throw new Dwoo_Exception('The compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable'); } } public function getCacheTime() { return $this->cacheTime; } public function setCacheTime($seconds) { $this->cacheTime = (int) $seconds; } public function getCharset() { return $this->charset; } public function setCharset($charset) { $this->charset = strtolower((string) $charset); } public function getTemplate() { return $this->template; } public function setTemplate(Dwoo_ITemplate $tpl) { $this->template = $tpl; } public function setDefaultCompilerFactory($resourceName, $compilerFactory) { $this->resources[$resourceName]['compiler'] = $compilerFactory; } public function getDefaultCompilerFactory($resourceName) { return $this->resources[$resourceName]['compiler']; } public function setSecurityPolicy(Dwoo_Security_Policy $policy = null) { $this->securityPolicy = $policy; } public function getSecurityPolicy() { return $this->securityPolicy; } public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) { $this->pluginProxy = $pluginProxy; } public function getPluginProxy() { return $this->pluginProxy; } public function isCached(Dwoo_ITemplate $tpl) { return is_string($tpl->getCachedTemplate($this)); } public function clearCache($olderThan=-1) { $cacheDirs = new RecursiveDirectoryIterator($this->getCacheDir()); $cache = new RecursiveIteratorIterator($cacheDirs); $expired = time() - $olderThan; $count = 0; foreach ($cache as $file) { if ($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html') { continue; } if ($cache->getCTime() < $expired) { $count += unlink((string) $file) ? 1 : 0; } } return $count; } public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { if (isset($this->resources[$resourceName])) { return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate); } else { throw new Dwoo_Exception('Unknown resource type : '.$resourceName); } } public function isArray($value, $checkIsEmpty=false) { if (is_array($value) === true || $value instanceof ArrayAccess) { if ($checkIsEmpty === false) { return true; } else { return $this->count($value); } } } public function isTraversable($value, $checkIsEmpty=false) { if (is_array($value) === true) { if ($checkIsEmpty === false) { return true; } else { return count($value) > 0; } } elseif ($value instanceof Traversable) { if ($checkIsEmpty === false) { return true; } else { return $this->count($value); } } return false; } public function count($value) { if (is_array($value) === true || $value instanceof Countable) { return count($value); } elseif ($value instanceof ArrayAccess) { if ($value->offsetExists(0)) { return true; } } elseif ($value instanceof Iterator) { $value->rewind(); if ($value->valid()) { return true; } } elseif ($value instanceof Traversable) { foreach ($value as $dummy) { return true; } } return 0; } public function triggerError($message, $level=E_USER_NOTICE) { if (!($tplIdentifier = $this->template->getResourceIdentifier())) { $tplIdentifier = $this->template->getResourceName(); } trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level); } public function addStack($blockName, array $args=array()) { if (isset($this->plugins[$blockName])) { $class = $this->plugins[$blockName]['class']; } else { $class = 'Dwoo_Plugin_'.$blockName; } if ($this->curBlock !== null) { $this->curBlock->buffer(ob_get_contents()); ob_clean(); } else { $this->buffer .= ob_get_contents(); ob_clean(); } $block = new $class($this); $cnt = count($args); if ($cnt===0) { $block->init(); } elseif ($cnt===1) { $block->init($args[0]); } elseif ($cnt===2) { $block->init($args[0], $args[1]); } elseif ($cnt===3) { $block->init($args[0], $args[1], $args[2]); } elseif ($cnt===4) { $block->init($args[0], $args[1], $args[2], $args[3]); } else { call_user_func_array(array($block,'init'), $args); } $this->stack[] = $this->curBlock = $block; return $block; } public function delStack() { $args = func_get_args(); $this->curBlock->buffer(ob_get_contents()); ob_clean(); $cnt = count($args); if ($cnt===0) { $this->curBlock->end(); } elseif ($cnt===1) { $this->curBlock->end($args[0]); } elseif ($cnt===2) { $this->curBlock->end($args[0], $args[1]); } elseif ($cnt===3) { $this->curBlock->end($args[0], $args[1], $args[2]); } elseif ($cnt===4) { $this->curBlock->end($args[0], $args[1], $args[2], $args[3]); } else { call_user_func_array(array($this->curBlock, 'end'), $args); } $tmp = array_pop($this->stack); if (count($this->stack) > 0) { $this->curBlock = end($this->stack); $this->curBlock->buffer($tmp->process()); } else { $this->curBlock = null; echo $tmp->process(); } unset($tmp); } public function getParentBlock(Dwoo_Block_Plugin $block) { $index = array_search($block, $this->stack, true); if ($index !== false && $index > 0) { return $this->stack[$index-1]; } return false; } public function findBlock($type) { if (isset($this->plugins[$type])) { $type = $this->plugins[$type]['class']; } else { $type = 'Dwoo_Plugin_'.str_replace('Dwoo_Plugin_', '', $type); } $keys = array_keys($this->stack); while (($key = array_pop($keys)) !== false) { if ($this->stack[$key] instanceof $type) { return $this->stack[$key]; } } return false; } public function getObjectPlugin($class) { if (isset($this->runtimePlugins[$class])) { return $this->runtimePlugins[$class]; } return $this->runtimePlugins[$class] = new $class($this); } public function classCall($plugName, array $params = array()) { $class = 'Dwoo_Plugin_'.$plugName; $plugin = $this->getObjectPlugin($class); $cnt = count($params); if ($cnt===0) { return $plugin->process(); } elseif ($cnt===1) { return $plugin->process($params[0]); } elseif ($cnt===2) { return $plugin->process($params[0], $params[1]); } elseif ($cnt===3) { return $plugin->process($params[0], $params[1], $params[2]); } elseif ($cnt===4) { return $plugin->process($params[0], $params[1], $params[2], $params[3]); } else { return call_user_func_array(array($plugin, 'process'), $params); } } public function arrayMap($callback, array $params) { if ($params[0] === $this) { $addThis = true; array_shift($params); } if ((is_array($params[0]) || ($params[0] instanceof Iterator && $params[0] instanceof ArrayAccess))) { if (empty($params[0])) { return $params[0]; } $out = array(); $cnt = count($params); if (isset($addThis)) { array_unshift($params, $this); $items = $params[1]; $keys = array_keys($items); if (is_string($callback) === false) { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); } } elseif ($cnt===1) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i]); } } elseif ($cnt===2) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i], $params[2]); } } elseif ($cnt===3) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i], $params[2], $params[3]); } } else { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); } } } else { $items = $params[0]; $keys = array_keys($items); if (is_string($callback) === false) { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array($items[$i]) + $params); } } elseif ($cnt===1) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i]); } } elseif ($cnt===2) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1]); } } elseif ($cnt===3) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1], $params[2]); } } elseif ($cnt===4) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1], $params[2], $params[3]); } } else { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array($items[$i]) + $params); } } } return $out; } else { return $params[0]; } } public function readVarInto($varstr, $data, $safeRead = false) { if ($data === null) { return null; } if (is_array($varstr) === false) { preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); } else { $m = $varstr; } unset($varstr); while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if ((is_array($data) || $data instanceof ArrayAccess) && ($safeRead === false || isset($data[$m[2][$k]]))) { $data = $data[$m[2][$k]]; } else { return null; } } else { if (is_object($data) && ($safeRead === false || isset($data->$m[2][$k]) || is_callable(array($data, '__get')))) { $data = $data->$m[2][$k]; } else { return null; } } } return $data; } public function readParentVar($parentLevels, $varstr = null) { $tree = $this->scopeTree; $cur = $this->data; while ($parentLevels--!==0) { array_pop($tree); } while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } if ($varstr!==null) { return $this->readVarInto($varstr, $cur); } else { return $cur; } } public function readVar($varstr) { if (is_array($varstr)===true) { $m = $varstr; unset($varstr); } else { if (strstr($varstr, '.') === false && strstr($varstr, '[') === false && strstr($varstr, '->') === false) { if ($varstr === 'dwoo') { return $this->globals; } elseif ($varstr === '__' || $varstr === '_root' ) { return $this->data; $varstr = substr($varstr, 6); } elseif ($varstr === '_' || $varstr === '_parent') { $varstr = '.'.$varstr; $tree = $this->scopeTree; $cur = $this->data; array_pop($tree); while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } return $cur; } $cur = $this->scope; if (isset($cur[$varstr])) { return $cur[$varstr]; } else { return null; } } if (substr($varstr, 0, 1) === '.') { $varstr = 'dwoo'.$varstr; } preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); } $i = $m[2][0]; if ($i === 'dwoo') { $cur = $this->globals; array_shift($m[2]); array_shift($m[1]); switch ($m[2][0]) { case 'get': $cur = $_GET; break; case 'post': $cur = $_POST; break; case 'session': $cur = $_SESSION; break; case 'cookies': case 'cookie': $cur = $_COOKIE; break; case 'server': $cur = $_SERVER; break; case 'env': $cur = $_ENV; break; case 'request': $cur = $_REQUEST; break; case 'const': array_shift($m[2]); if (defined($m[2][0])) { return constant($m[2][0]); } else { return null; } } if ($cur !== $this->globals) { array_shift($m[2]); array_shift($m[1]); } } elseif ($i === '__' || $i === '_root') { $cur = $this->data; array_shift($m[2]); array_shift($m[1]); } elseif ($i === '_' || $i === '_parent') { $tree = $this->scopeTree; $cur = $this->data; while (true) { array_pop($tree); array_shift($m[2]); array_shift($m[1]); if (current($m[2]) === '_' || current($m[2]) === '_parent') { continue; } while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } break; } } else { $cur = $this->scope; } while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) { $cur = $cur[$m[2][$k]]; } else { return null; } } elseif ($sep === '->') { if (is_object($cur)) { $cur = $cur->$m[2][$k]; } else { return null; } } else { return null; } } return $cur; } public function assignInScope($value, $scope) { $tree =& $this->scopeTree; $data =& $this->data; if (!is_string($scope)) { return $this->triggerError('Assignments must be done into strings, ('.gettype($scope).') '.var_export($scope, true).' given', E_USER_ERROR); } if (strstr($scope, '.') === false && strstr($scope, '->') === false) { $this->scope[$scope] = $value; } else { preg_match_all('#(\[|->|\.)?([^.[\]-]+)\]?#i', $scope, $m); $cur =& $this->scope; $last = array(array_pop($m[1]), array_pop($m[2])); while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if (is_array($cur) === false) { $cur = array(); } $cur =& $cur[$m[2][$k]]; } elseif ($sep === '->') { if (is_object($cur) === false) { $cur = new stdClass; } $cur =& $cur->$m[2][$k]; } else { return false; } } if ($last[0] === '.' || $last[0] === '[' || $last[0] === '') { if (is_array($cur) === false) { $cur = array(); } $cur[$last[1]] = $value; } elseif ($last[0] === '->') { if (is_object($cur) === false) { $cur = new stdClass; } $cur->$last[1] = $value; } else { return false; } } } public function setScope($scope, $absolute = false) { $old = $this->scopeTree; if (is_string($scope)===true) { $scope = explode('.', $scope); } if ($absolute===true) { $this->scope =& $this->data; $this->scopeTree = array(); } while (($bit = array_shift($scope)) !== null) { if ($bit === '_' || $bit === '_parent') { array_pop($this->scopeTree); $this->scope =& $this->data; $cnt = count($this->scopeTree); for ($i=0;$i<$cnt;$i++) $this->scope =& $this->scope[$this->scopeTree[$i]]; } elseif ($bit === '__' || $bit === '_root') { $this->scope =& $this->data; $this->scopeTree = array(); } elseif (isset($this->scope[$bit])) { $this->scope =& $this->scope[$bit]; $this->scopeTree[] = $bit; } else { unset($this->scope); $this->scope = null; } } return $old; } public function getData() { return $this->data; } public function &getScope() { return $this->scope; } public function __call($method, $args) { $proxy = $this->getPluginProxy(); if (!$proxy) { throw new Dwoo_Exception('Call to undefined method '.__CLASS__.'::'.$method.'()'); } return call_user_func_array($proxy->getCallback($method), $args); } } interface Dwoo_IPluginProxy { public function handles($name); public function getCode($name, $params); public function getCallback($name); public function getLoader($name); } interface Dwoo_IElseable { } interface Dwoo_ILoader { public function loadPlugin($class, $forceRehash = true); } class Dwoo_Loader implements Dwoo_ILoader { protected $paths = array(); protected $classPath = array(); protected $cacheDir; protected $corePluginDir; public function __construct($cacheDir) { $this->corePluginDir = DWOO_DIRECTORY . 'plugins'; $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php'; if (file_exists($cacheFile)) { $classpath = file_get_contents($cacheFile); $this->classPath = unserialize($classpath) + $this->classPath; } else { $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); } } protected function rebuildClassPathCache($path, $cacheFile) { if ($cacheFile!==false) { $tmp = $this->classPath; $this->classPath = array(); } $list = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*'); if (is_array($list)) { foreach ($list as $f) { if (is_dir($f)) { $this->rebuildClassPathCache($f, false); } else { $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f, '.php'))] = $f; } } } if ($cacheFile!==false) { if (!file_put_contents($cacheFile, serialize($this->classPath))) { throw new Dwoo_Exception('Could not write into '.$cacheFile.', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); } $this->classPath += $tmp; } } public function loadPlugin($class, $forceRehash = true) { if (!isset($this->classPath[$class]) || !(include $this->classPath[$class])) { if ($forceRehash) { $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d'.Dwoo::RELEASE_TAG.'.php'); foreach ($this->paths as $path=>$file) { $this->rebuildClassPathCache($path, $file); } if (isset($this->classPath[$class])) { include $this->classPath[$class]; } else { throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); } } else { throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); } } } public function addDirectory($pluginDirectory) { $pluginDir = realpath($pluginDirectory); if (!$pluginDir) { throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory); } $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo::RELEASE_TAG.'.php'; $this->paths[$pluginDir] = $cacheFile; if (file_exists($cacheFile)) { $classpath = file_get_contents($cacheFile); $this->classPath = unserialize($classpath) + $this->classPath; } else { $this->rebuildClassPathCache($pluginDir, $cacheFile); } } } class Dwoo_Exception extends Exception { } class Dwoo_Security_Policy { const PHP_ENCODE = 1; const PHP_REMOVE = 2; const PHP_ALLOW = 3; const CONST_DISALLOW = false; const CONST_ALLOW = true; protected $allowedPhpFunctions = array ( 'str_repeat', 'number_format', 'htmlentities', 'htmlspecialchars', 'long2ip', 'strlen', 'list', 'empty', 'count', 'sizeof', 'in_array', 'is_array', ); protected $allowedDirectories = array(); protected $phpHandling = self::PHP_REMOVE; protected $constHandling = self::CONST_DISALLOW; public function allowPhpFunction($func) { if (is_array($func)) foreach ($func as $fname) $this->allowedPhpFunctions[strtolower($fname)] = true; else $this->allowedPhpFunctions[strtolower($func)] = true; } public function disallowPhpFunction($func) { if (is_array($func)) foreach ($func as $fname) unset($this->allowedPhpFunctions[strtolower($fname)]); else unset($this->allowedPhpFunctions[strtolower($func)]); } public function getAllowedPhpFunctions() { return $this->allowedPhpFunctions; } public function allowDirectory($path) { if (is_array($path)) foreach ($path as $dir) $this->allowedDirectories[realpath($dir)] = true; else $this->allowedDirectories[realpath($path)] = true; } public function disallowDirectory($path) { if (is_array($path)) foreach ($path as $dir) unset($this->allowedDirectories[realpath($dir)]); else unset($this->allowedDirectories[realpath($path)]); } public function getAllowedDirectories() { return $this->allowedDirectories; } public function setPhpHandling($level = self::PHP_REMOVE) { $this->phpHandling = $level; } public function getPhpHandling() { return $this->phpHandling; } public function setConstantHandling($level = self::CONST_DISALLOW) { $this->constHandling = $level; } public function getConstantHandling() { return $this->constHandling; } } class Dwoo_Security_Exception extends Dwoo_Exception { } interface Dwoo_ICompilable { } interface Dwoo_ICompiler { public function compile(Dwoo $dwoo, Dwoo_ITemplate $template); public function setCustomPlugins(array $customPlugins); public function setSecurityPolicy(Dwoo_Security_Policy $policy = null); } interface Dwoo_IDataProvider { public function getData(); } interface Dwoo_ITemplate { public function getCacheTime(); public function setCacheTime($seconds = null); public function getCachedTemplate(Dwoo $dwoo); public function cache(Dwoo $dwoo, $output); public function clearCache(Dwoo $dwoo, $olderThan = -1); public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null); public function getName(); public function getResourceName(); public function getResourceIdentifier(); public function getSource(); public function getUid(); public function getCompiler(); public function getIsModifiedCode(); public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null); } interface Dwoo_ICompilable_Block { } abstract class Dwoo_Plugin { protected $dwoo; public function __construct(Dwoo $dwoo) { $this->dwoo = $dwoo; } public static function paramsToAttributes(array $params, $delim = '\'') { if (isset($params['*'])) { $params = array_merge($params, $params['*']); unset($params['*']); } $out = ''; foreach ($params as $attr=>$val) { $out .= ' '.$attr.'='; if (trim($val, '"\'')=='' || $val=='null') { $out .= str_replace($delim, '\\'.$delim, '""'); } elseif (substr($val, 0, 1) === $delim && substr($val, -1) === $delim) { $out .= str_replace($delim, '\\'.$delim, '"'.substr($val, 1, -1).'"'); } else { $out .= str_replace($delim, '\\'.$delim, '"') . $delim . '.'.$val.'.' . $delim . str_replace($delim, '\\'.$delim, '"'); } } return ltrim($out); } } abstract class Dwoo_Block_Plugin extends Dwoo_Plugin { protected $buffer = ''; public function buffer($input) { $this->buffer .= $input; } public function end() { } public function process() { return $this->buffer; } public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type) { return Dwoo_Compiler::PHP_OPEN.$prepend.'$this->addStack("'.$type.'", array('.Dwoo_Compiler::implode_r($compiler->getCompiledParams($params)).'));'.$append.Dwoo_Compiler::PHP_CLOSE; } public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content) { return $content . Dwoo_Compiler::PHP_OPEN.$prepend.'$this->delStack();'.$append.Dwoo_Compiler::PHP_CLOSE; } } abstract class Dwoo_Filter { protected $dwoo; public function __construct(Dwoo $dwoo) { $this->dwoo = $dwoo; } abstract public function process($input); } abstract class Dwoo_Processor { protected $compiler; public function __construct(Dwoo_Compiler $compiler) { $this->compiler = $compiler; } abstract public function process($input); } class Dwoo_Template_String implements Dwoo_ITemplate { protected $name; protected $compileId; protected $cacheId; protected $cacheTime; protected $compilationEnforced; protected static $cache = array('cached'=>array(), 'compiled'=>array()); protected $compiler; protected $chmod = 0777; public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) { $this->template = $templateString; if (function_exists('hash')) { $this->name = hash('md4', $templateString); } else { $this->name = md5($templateString); } $this->cacheTime = $cacheTime; if ($compileId !== null) { $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if ($cacheId !== null) { $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } } public function getCacheTime() { return $this->cacheTime; } public function setCacheTime($seconds = null) { $this->cacheTime = $seconds; } public function getChmod() { return $this->chmod; } public function setChmod($mask = null) { $this->chmod = $mask; } public function getName() { return $this->name; } public function getResourceName() { return 'string'; } public function getResourceIdentifier() { return false; } public function getSource() { return $this->template; } public function getUid() { return $this->name; } public function getCompiler() { return $this->compiler; } public function forceCompilation() { $this->compilationEnforced = true; } public function getCachedTemplate(Dwoo $dwoo) { if ($this->cacheTime !== null) { $cacheLength = $this->cacheTime; } else { $cacheLength = $dwoo->getCacheTime(); } if ($cacheLength === 0) { return false; } $cachedFile = $this->getCacheFilename($dwoo); if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { return $cachedFile; } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === -1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($dwoo))) { self::$cache['cached'][$this->cacheId] = true; return $cachedFile; } else { return true; } } public function cache(Dwoo $dwoo, $output) { $cacheDir = $dwoo->getCacheDir(); $cachedFile = $this->getCacheFilename($dwoo); $temp = tempnam($cacheDir, 'temp'); if (!($file = @fopen($temp, 'wb'))) { $temp = $cacheDir . uniqid('temp'); if (!($file = @fopen($temp, 'wb'))) { trigger_error('Error writing temporary file \''.$temp.'\'', E_USER_WARNING); return false; } } fwrite($file, $output); fclose($file); $this->makeDirectory(dirname($cachedFile), $cacheDir); if (!@rename($temp, $cachedFile)) { @unlink($cachedFile); @rename($temp, $cachedFile); } if ($this->chmod !== null) { chmod($cachedFile, $this->chmod); } self::$cache['cached'][$this->cacheId] = true; return $cachedFile; } public function clearCache(Dwoo $dwoo, $olderThan = -1) { $cachedFile = $this->getCacheFilename($dwoo); return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); } public function getCompiledTemplate(Dwoo $dwoo, Dwoo_ICompiler $compiler = null) { $compiledFile = $this->getCompiledFilename($dwoo); if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { self::$cache['compiled'][$this->compileId] = true; } else { $this->compilationEnforced = false; if ($compiler === null) { $compiler = $dwoo->getDefaultCompilerFactory($this->getResourceName()); if ($compiler === null || $compiler === array('Dwoo_Compiler', 'compilerFactory')) { if (class_exists('Dwoo_Compiler', false) === false) { include DWOO_DIRECTORY . 'Dwoo/Compiler.php'; } $compiler = Dwoo_Compiler::compilerFactory(); } else { $compiler = call_user_func($compiler); } } $this->compiler = $compiler; $compiler->setCustomPlugins($dwoo->getCustomPlugins()); $compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); $this->makeDirectory(dirname($compiledFile), $dwoo->getCompileDir()); file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); if ($this->chmod !== null) { chmod($compiledFile, $this->chmod); } self::$cache['compiled'][$this->compileId] = true; } return $compiledFile; } protected function isValidCompiledFile($file) { return file_exists($file); } public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { return new self($resourceId, $cacheTime, $cacheId, $compileId); } protected function getCompiledFilename(Dwoo $dwoo) { if ($this->compileId===null) { $this->compileId = $this->name; } return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php'; } protected function getCacheFilename(Dwoo $dwoo) { if ($this->cacheId === null) { if (isset($_SERVER['REQUEST_URI']) === true) { $cacheId = $_SERVER['REQUEST_URI']; } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { $cacheId = $_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']); } else { $cacheId = ''; } $this->getCompiledFilename($dwoo); $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } return $dwoo->getCacheDir() . $this->cacheId.'.html'; } public function getIsModifiedCode() { return null; } protected function makeDirectory($path, $baseDir = null) { if (is_dir($path) === true) { return; } if ($this->chmod === null) { $chmod = 0777; } else { $chmod = $this->chmod; } mkdir($path, $chmod, true); if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); $folders = explode('/', trim($path, '/')); foreach ($folders as $folder) { $baseDir .= $folder . DIRECTORY_SEPARATOR; chmod($baseDir, $chmod); } } } } class Dwoo_Template_File extends Dwoo_Template_String { protected $file; protected $includePath = null; protected $resolvedPath = null; public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = null) { $this->file = $file; $this->name = basename($file); $this->cacheTime = $cacheTime; if ($compileId !== null) { $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if ($cacheId !== null) { $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if (is_string($includePath)) { $this->includePath = array($includePath); } elseif (is_array($includePath)) { $this->includePath = $includePath; } } public function setIncludePath($paths) { if (is_array($paths) === false) { $paths = array($paths); } $this->includePath = $paths; $this->resolvedPath = null; } public function getIncludePath() { return $this->includePath; } protected function isValidCompiledFile($file) { return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); } public function getSource() { return file_get_contents($this->getResourceIdentifier()); } public function getResourceName() { return 'file'; } public function getResourceIdentifier() { if ($this->resolvedPath !== null) { return $this->resolvedPath; } elseif ($this->includePath === null) { return $this->file; } else { foreach ($this->includePath as $path) { $path = rtrim($path, DIRECTORY_SEPARATOR); if (file_exists($path.DIRECTORY_SEPARATOR.$this->file) === true) { $this->resolvedPath = $path . DIRECTORY_SEPARATOR . $this->file; return $this->resolvedPath; } } throw new Dwoo_Exception('Template "'.$this->file.'" could not be found in any of your include path(s)'); } } public function getUid() { return (string) filemtime($this->getResourceIdentifier()); } public static function templateFactory(Dwoo $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { if (DIRECTORY_SEPARATOR === '\\') { $resourceId = str_replace(array("\t", "\n", "\r", "\f", "\v"), array('\\t', '\\n', '\\r', '\\f', '\\v'), $resourceId); } $resourceId = strtr($resourceId, '\\', '/'); $includePath = null; if (file_exists($resourceId) === false) { if ($parentTemplate === null) { $parentTemplate = $dwoo->getTemplate(); } if ($parentTemplate instanceof Dwoo_Template_File) { if ($includePath = $parentTemplate->getIncludePath()) { if (strstr($resourceId, '../')) { throw new Dwoo_Exception('When using an include path you can not reference a template into a parent directory (using ../)'); } } else { $resourceId = dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId; if (file_exists($resourceId) === false) { return null; } } } else { return null; } } if ($policy = $dwoo->getSecurityPolicy()) { while (true) { if (preg_match('{^([a-z]+?)://}i', $resourceId)) { throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.'); } if ($includePath) { break; } $resourceId = realpath($resourceId); $dirs = $policy->getAllowedDirectories(); foreach ($dirs as $dir=>$dummy) { if (strpos($resourceId, $dir) === 0) { break 2; } } throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$resourceId.'</em>'); } } $class = 'Dwoo_Template_File'; if ($parentTemplate) { $class = get_class($parentTemplate); } return new $class($resourceId, $cacheTime, $cacheId, $compileId, $includePath); } protected function getCompiledFilename(Dwoo $dwoo) { if ($this->compileId===null) { $this->compileId = str_replace('../', '__', strtr($this->getResourceIdentifier(), '\\:', '/-')); } return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo::RELEASE_TAG.'.php'; } public function getIsModifiedCode() { return '"'.$this->getUid().'" == filemtime('.var_export($this->getResourceIdentifier(), true).')'; } } class Dwoo_Data implements Dwoo_IDataProvider { protected $data = array(); public function getData() { return $this->data; } public function clear($name = null) { if ($name === null) { $this->data = array(); } elseif (is_array($name)) { foreach ($name as $index) unset($this->data[$index]); } else { unset($this->data[$name]); } } public function setData(array $data) { $this->data = $data; } public function mergeData(array $data) { $args = func_get_args(); while (list(,$v) = each($args)) { if (is_array($v)) { $this->data = array_merge($this->data, $v); } } } public function assign($name, $val = null) { if (is_array($name)) { reset($name); while (list($k,$v) = each($name)) $this->data[$k] = $v; } else { $this->data[$name] = $val; } } public function __set($name, $value) { $this->assign($name, $value); } public function assignByRef($name, &$val) { $this->data[$name] =& $val; } public function append($name, $val = null, $merge = false) { if (is_array($name)) { foreach ($name as $key=>$val) { if (isset($this->data[$key]) && !is_array($this->data[$key])) { settype($this->data[$key], 'array'); } if ($merge === true && is_array($val)) { $this->data[$key] = $val + $this->data[$key]; } else { $this->data[$key][] = $val; } } } elseif ($val !== null) { if (isset($this->data[$name]) && !is_array($this->data[$name])) { settype($this->data[$name], 'array'); } elseif (!isset($this->data[$name])) { $this->data[$name] = array(); } if ($merge === true && is_array($val)) { $this->data[$name] = $val + $this->data[$name]; } else { $this->data[$name][] = $val; } } } public function appendByRef($name, &$val, $merge = false) { if (isset($this->data[$name]) && !is_array($this->data[$name])) { settype($this->data[$name], 'array'); } if ($merge === true && is_array($val)) { foreach ($val as $key => &$val) { $this->data[$name][$key] =& $val; } } else { $this->data[$name][] =& $val; } } public function isAssigned($name) { return isset($this->data[$name]); } public function __isset($name) { return isset($this->data[$name]); } public function unassign($name) { unset($this->data[$name]); } public function __unset($name) { unset($this->data[$name]); } public function get($name) { return $this->__get($name); } public function __get($name) { if (isset($this->data[$name])) { return $this->data[$name]; } else { throw new Dwoo_Exception('Tried to read a value that was not assigned yet : "'.$name.'"'); } } } |
|
2 |
+<?php define('DWOO_DIRECTORY', dirname(__FILE__) . DIRECTORY_SEPARATOR); class Dwoo_Core { const VERSION = '1.1.1'; const RELEASE_TAG = 17; const CLASS_PLUGIN = 1; const FUNC_PLUGIN = 2; const NATIVE_PLUGIN = 4; const BLOCK_PLUGIN = 8; const COMPILABLE_PLUGIN = 16; const CUSTOM_PLUGIN = 32; const SMARTY_MODIFIER = 64; const SMARTY_BLOCK = 128; const SMARTY_FUNCTION = 256; const PROXY_PLUGIN = 512; const TEMPLATE_PLUGIN = 1024; protected $charset = 'utf-8'; public $globals; protected $compileDir; protected $cacheDir; protected $cacheTime = 0; protected $securityPolicy = null; protected $plugins = array(); protected $filters = array(); protected $resources = array ( 'file' => array ( 'class' => 'Dwoo_Template_File', 'compiler' => null ), 'string' => array ( 'class' => 'Dwoo_Template_String', 'compiler' => null ) ); protected $loader = null; protected $template = null; protected $runtimePlugins; public $data; public $scope; protected $scopeTree; protected $stack; protected $curBlock; protected $buffer; protected $pluginProxy; public function __construct($compileDir = null, $cacheDir = null) { if ($compileDir !== null) { $this->setCompileDir($compileDir); } if ($cacheDir !== null) { $this->setCacheDir($cacheDir); } $this->initGlobals(); } public function __clone() { $this->template = null; unset($this->data); } public function output($tpl, $data = array(), Dwoo_ICompiler $compiler = null) { return $this->get($tpl, $data, $compiler, true); } public function get($_tpl, $data = array(), $_compiler = null, $_output = false) { if ($this->template instanceof Dwoo_ITemplate) { $proxy = clone $this; return $proxy->get($_tpl, $data, $_compiler, $_output); } if ($_tpl instanceof Dwoo_ITemplate) { } elseif (is_string($_tpl) && file_exists($_tpl)) { $_tpl = new Dwoo_Template_File($_tpl); } else { throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE); } $this->template = $_tpl; if ($data instanceof Dwoo_IDataProvider) { $this->data = $data->getData(); } elseif (is_array($data)) { $this->data = $data; } else { throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE); } $this->globals['template'] = $_tpl->getName(); $this->initRuntimeVars($_tpl); $file = $_tpl->getCachedTemplate($this); $doCache = $file === true; $cacheLoaded = is_string($file); if ($cacheLoaded === true) { if ($_output === true) { include $file; $this->template = null; } else { ob_start(); include $file; $this->template = null; return ob_get_clean(); } } else { if ($doCache === true) { $dynamicId = uniqid(); } $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); $out = include $compiledTemplate; if ($out === false) { $_tpl->forceCompilation(); $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); $out = include $compiledTemplate; } if ($doCache === true) { $out = preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php /*'.$dynamicId.'*/ echo \'$1\'; ?>', $out); if (!class_exists('Dwoo_plugin_dynamic', false)) { $this->getLoader()->loadPlugin('dynamic'); } $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate); } foreach ($this->filters as $filter) { if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) { $out = call_user_func($filter, $out); } else { $out = call_user_func($filter, $this, $out); } } if ($doCache === true) { $file = $_tpl->cache($this, $out); if ($_output === true) { include $file; $this->template = null; } else { ob_start(); include $file; $this->template = null; return ob_get_clean(); } } else { $this->template = null; if ($_output === true) { echo $out; } return $out; } } } protected function initGlobals() { $this->globals = array ( 'version' => self::VERSION, 'ad' => '<a href="http://dwoo.org/">Powered by Dwoo</a>', 'now' => $_SERVER['REQUEST_TIME'], 'charset' => $this->charset, ); } protected function initRuntimeVars(Dwoo_ITemplate $tpl) { $this->runtimePlugins = array(); $this->scope =& $this->data; $this->scopeTree = array(); $this->stack = array(); $this->curBlock = null; $this->buffer = ''; } public function addPlugin($name, $callback, $compilable = false) { $compilable = $compilable ? self::COMPILABLE_PLUGIN : 0; if (is_array($callback)) { if (is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'Dwoo_Block_Plugin')) { $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0])); } else { $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]); } } elseif (class_exists($callback, false)) { if (is_subclass_of($callback, 'Dwoo_Block_Plugin')) { $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback); } else { $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process'); } } elseif (function_exists($callback)) { $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN | $compilable, 'callback'=>$callback); } else { throw new Dwoo_Exception('Callback could not be processed correctly, please check that the function/class you used exists'); } } public function removePlugin($name) { if (isset($this->plugins[$name])) { unset($this->plugins[$name]); } } public function addFilter($callback, $autoload = false) { if ($autoload) { $class = 'Dwoo_Filter_'.$callback; if (!class_exists($class, false) && !function_exists($class)) { try { $this->getLoader()->loadPlugin($callback); } catch (Dwoo_Exception $e) { if (strstr($callback, 'Dwoo_Filter_')) { throw new Dwoo_Exception('Wrong filter name : '.$callback.', the "Dwoo_Filter_" prefix should not be used, please only use "'.str_replace('Dwoo_Filter_', '', $callback).'"'); } else { throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); } } } if (class_exists($class, false)) { $callback = array(new $class($this), 'process'); } elseif (function_exists($class)) { $callback = $class; } else { throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); } $this->filters[] = $callback; } else { $this->filters[] = $callback; } } public function removeFilter($callback) { if (($index = array_search('Dwoo_Filter_'.$callback, $this->filters, true)) !== false) { unset($this->filters[$index]); } elseif (($index = array_search($callback, $this->filters, true)) !== false) { unset($this->filters[$index]); } else { $class = 'Dwoo_Filter_' . $callback; foreach ($this->filters as $index=>$filter) { if (is_array($filter) && $filter[0] instanceof $class) { unset($this->filters[$index]); break; } } } } public function addResource($name, $class, $compilerFactory = null) { if (strlen($name) < 2) { throw new Dwoo_Exception('Resource names must be at least two-character long to avoid conflicts with Windows paths'); } if (!class_exists($class)) { throw new Dwoo_Exception('Resource class does not exist'); } $interfaces = class_implements($class); if (in_array('Dwoo_ITemplate', $interfaces) === false) { throw new Dwoo_Exception('Resource class must implement Dwoo_ITemplate'); } $this->resources[$name] = array('class'=>$class, 'compiler'=>$compilerFactory); } public function removeResource($name) { unset($this->resources[$name]); if ($name==='file') { $this->resources['file'] = array('class'=>'Dwoo_Template_File', 'compiler'=>null); } } public function setLoader(Dwoo_ILoader $loader) { $this->loader = $loader; } public function getLoader() { if ($this->loader === null) { $this->loader = new Dwoo_Loader($this->getCompileDir()); } return $this->loader; } public function getCustomPlugins() { return $this->plugins; } public function getCacheDir() { if ($this->cacheDir === null) { $this->setCacheDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR); } return $this->cacheDir; } public function setCacheDir($dir) { $this->cacheDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; if (is_writable($this->cacheDir) === false) { throw new Dwoo_Exception('The cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable'); } } public function getCompileDir() { if ($this->compileDir === null) { $this->setCompileDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'compiled'.DIRECTORY_SEPARATOR); } return $this->compileDir; } public function setCompileDir($dir) { $this->compileDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; if (is_writable($this->compileDir) === false) { throw new Dwoo_Exception('The compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable'); } } public function getCacheTime() { return $this->cacheTime; } public function setCacheTime($seconds) { $this->cacheTime = (int) $seconds; } public function getCharset() { return $this->charset; } public function setCharset($charset) { $this->charset = strtolower((string) $charset); } public function getTemplate() { return $this->template; } public function setTemplate(Dwoo_ITemplate $tpl) { $this->template = $tpl; } public function setDefaultCompilerFactory($resourceName, $compilerFactory) { $this->resources[$resourceName]['compiler'] = $compilerFactory; } public function getDefaultCompilerFactory($resourceName) { return $this->resources[$resourceName]['compiler']; } public function setSecurityPolicy(Dwoo_Security_Policy $policy = null) { $this->securityPolicy = $policy; } public function getSecurityPolicy() { return $this->securityPolicy; } public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) { $this->pluginProxy = $pluginProxy; } public function getPluginProxy() { return $this->pluginProxy; } public function isCached(Dwoo_ITemplate $tpl) { return is_string($tpl->getCachedTemplate($this)); } public function clearCache($olderThan=-1) { $cacheDirs = new RecursiveDirectoryIterator($this->getCacheDir()); $cache = new RecursiveIteratorIterator($cacheDirs); $expired = time() - $olderThan; $count = 0; foreach ($cache as $file) { if ($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html') { continue; } if ($cache->getCTime() < $expired) { $count += unlink((string) $file) ? 1 : 0; } } return $count; } public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { if (isset($this->resources[$resourceName])) { return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate); } else { throw new Dwoo_Exception('Unknown resource type : '.$resourceName); } } public function isArray($value, $checkIsEmpty=false) { if (is_array($value) === true || $value instanceof ArrayAccess) { if ($checkIsEmpty === false) { return true; } else { return $this->count($value); } } } public function isTraversable($value, $checkIsEmpty=false) { if (is_array($value) === true) { if ($checkIsEmpty === false) { return true; } else { return count($value) > 0; } } elseif ($value instanceof Traversable) { if ($checkIsEmpty === false) { return true; } else { return $this->count($value); } } return false; } public function count($value) { if (is_array($value) === true || $value instanceof Countable) { return count($value); } elseif ($value instanceof ArrayAccess) { if ($value->offsetExists(0)) { return true; } } elseif ($value instanceof Iterator) { $value->rewind(); if ($value->valid()) { return true; } } elseif ($value instanceof Traversable) { foreach ($value as $dummy) { return true; } } return 0; } public function triggerError($message, $level=E_USER_NOTICE) { if (!($tplIdentifier = $this->template->getResourceIdentifier())) { $tplIdentifier = $this->template->getResourceName(); } trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level); } public function addStack($blockName, array $args=array()) { if (isset($this->plugins[$blockName])) { $class = $this->plugins[$blockName]['class']; } else { $class = 'Dwoo_Plugin_'.$blockName; } if ($this->curBlock !== null) { $this->curBlock->buffer(ob_get_contents()); ob_clean(); } else { $this->buffer .= ob_get_contents(); ob_clean(); } $block = new $class($this); $cnt = count($args); if ($cnt===0) { $block->init(); } elseif ($cnt===1) { $block->init($args[0]); } elseif ($cnt===2) { $block->init($args[0], $args[1]); } elseif ($cnt===3) { $block->init($args[0], $args[1], $args[2]); } elseif ($cnt===4) { $block->init($args[0], $args[1], $args[2], $args[3]); } else { call_user_func_array(array($block,'init'), $args); } $this->stack[] = $this->curBlock = $block; return $block; } public function delStack() { $args = func_get_args(); $this->curBlock->buffer(ob_get_contents()); ob_clean(); $cnt = count($args); if ($cnt===0) { $this->curBlock->end(); } elseif ($cnt===1) { $this->curBlock->end($args[0]); } elseif ($cnt===2) { $this->curBlock->end($args[0], $args[1]); } elseif ($cnt===3) { $this->curBlock->end($args[0], $args[1], $args[2]); } elseif ($cnt===4) { $this->curBlock->end($args[0], $args[1], $args[2], $args[3]); } else { call_user_func_array(array($this->curBlock, 'end'), $args); } $tmp = array_pop($this->stack); if (count($this->stack) > 0) { $this->curBlock = end($this->stack); $this->curBlock->buffer($tmp->process()); } else { $this->curBlock = null; echo $tmp->process(); } unset($tmp); } public function getParentBlock(Dwoo_Block_Plugin $block) { $index = array_search($block, $this->stack, true); if ($index !== false && $index > 0) { return $this->stack[$index-1]; } return false; } public function findBlock($type) { if (isset($this->plugins[$type])) { $type = $this->plugins[$type]['class']; } else { $type = 'Dwoo_Plugin_'.str_replace('Dwoo_Plugin_', '', $type); } $keys = array_keys($this->stack); while (($key = array_pop($keys)) !== false) { if ($this->stack[$key] instanceof $type) { return $this->stack[$key]; } } return false; } public function getObjectPlugin($class) { if (isset($this->runtimePlugins[$class])) { return $this->runtimePlugins[$class]; } return $this->runtimePlugins[$class] = new $class($this); } public function classCall($plugName, array $params = array()) { $class = 'Dwoo_Plugin_'.$plugName; $plugin = $this->getObjectPlugin($class); $cnt = count($params); if ($cnt===0) { return $plugin->process(); } elseif ($cnt===1) { return $plugin->process($params[0]); } elseif ($cnt===2) { return $plugin->process($params[0], $params[1]); } elseif ($cnt===3) { return $plugin->process($params[0], $params[1], $params[2]); } elseif ($cnt===4) { return $plugin->process($params[0], $params[1], $params[2], $params[3]); } else { return call_user_func_array(array($plugin, 'process'), $params); } } public function arrayMap($callback, array $params) { if ($params[0] === $this) { $addThis = true; array_shift($params); } if ((is_array($params[0]) || ($params[0] instanceof Iterator && $params[0] instanceof ArrayAccess))) { if (empty($params[0])) { return $params[0]; } $out = array(); $cnt = count($params); if (isset($addThis)) { array_unshift($params, $this); $items = $params[1]; $keys = array_keys($items); if (is_string($callback) === false) { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); } } elseif ($cnt===1) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i]); } } elseif ($cnt===2) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i], $params[2]); } } elseif ($cnt===3) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($this, $items[$i], $params[2], $params[3]); } } else { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); } } } else { $items = $params[0]; $keys = array_keys($items); if (is_string($callback) === false) { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array($items[$i]) + $params); } } elseif ($cnt===1) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i]); } } elseif ($cnt===2) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1]); } } elseif ($cnt===3) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1], $params[2]); } } elseif ($cnt===4) { while (($i = array_shift($keys)) !== null) { $out[] = $callback($items[$i], $params[1], $params[2], $params[3]); } } else { while (($i = array_shift($keys)) !== null) { $out[] = call_user_func_array($callback, array($items[$i]) + $params); } } } return $out; } else { return $params[0]; } } public function readVarInto($varstr, $data, $safeRead = false) { if ($data === null) { return null; } if (is_array($varstr) === false) { preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); } else { $m = $varstr; } unset($varstr); while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if ((is_array($data) || $data instanceof ArrayAccess) && ($safeRead === false || isset($data[$m[2][$k]]))) { $data = $data[$m[2][$k]]; } else { return null; } } else { if (is_object($data) && ($safeRead === false || isset($data->$m[2][$k]) || is_callable(array($data, '__get')))) { $data = $data->$m[2][$k]; } else { return null; } } } return $data; } public function readParentVar($parentLevels, $varstr = null) { $tree = $this->scopeTree; $cur = $this->data; while ($parentLevels--!==0) { array_pop($tree); } while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } if ($varstr!==null) { return $this->readVarInto($varstr, $cur); } else { return $cur; } } public function readVar($varstr) { if (is_array($varstr)===true) { $m = $varstr; unset($varstr); } else { if (strstr($varstr, '.') === false && strstr($varstr, '[') === false && strstr($varstr, '->') === false) { if ($varstr === 'dwoo') { return $this->globals; } elseif ($varstr === '__' || $varstr === '_root' ) { return $this->data; $varstr = substr($varstr, 6); } elseif ($varstr === '_' || $varstr === '_parent') { $varstr = '.'.$varstr; $tree = $this->scopeTree; $cur = $this->data; array_pop($tree); while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } return $cur; } $cur = $this->scope; if (isset($cur[$varstr])) { return $cur[$varstr]; } else { return null; } } if (substr($varstr, 0, 1) === '.') { $varstr = 'dwoo'.$varstr; } preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); } $i = $m[2][0]; if ($i === 'dwoo') { $cur = $this->globals; array_shift($m[2]); array_shift($m[1]); switch ($m[2][0]) { case 'get': $cur = $_GET; break; case 'post': $cur = $_POST; break; case 'session': $cur = $_SESSION; break; case 'cookies': case 'cookie': $cur = $_COOKIE; break; case 'server': $cur = $_SERVER; break; case 'env': $cur = $_ENV; break; case 'request': $cur = $_REQUEST; break; case 'const': array_shift($m[2]); if (defined($m[2][0])) { return constant($m[2][0]); } else { return null; } } if ($cur !== $this->globals) { array_shift($m[2]); array_shift($m[1]); } } elseif ($i === '__' || $i === '_root') { $cur = $this->data; array_shift($m[2]); array_shift($m[1]); } elseif ($i === '_' || $i === '_parent') { $tree = $this->scopeTree; $cur = $this->data; while (true) { array_pop($tree); array_shift($m[2]); array_shift($m[1]); if (current($m[2]) === '_' || current($m[2]) === '_parent') { continue; } while (($i = array_shift($tree)) !== null) { if (is_object($cur)) { $cur = $cur->$i; } else { $cur = $cur[$i]; } } break; } } else { $cur = $this->scope; } while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) { $cur = $cur[$m[2][$k]]; } else { return null; } } elseif ($sep === '->') { if (is_object($cur)) { $cur = $cur->$m[2][$k]; } else { return null; } } else { return null; } } return $cur; } public function assignInScope($value, $scope) { $tree =& $this->scopeTree; $data =& $this->data; if (!is_string($scope)) { return $this->triggerError('Assignments must be done into strings, ('.gettype($scope).') '.var_export($scope, true).' given', E_USER_ERROR); } if (strstr($scope, '.') === false && strstr($scope, '->') === false) { $this->scope[$scope] = $value; } else { preg_match_all('#(\[|->|\.)?([^.[\]-]+)\]?#i', $scope, $m); $cur =& $this->scope; $last = array(array_pop($m[1]), array_pop($m[2])); while (list($k, $sep) = each($m[1])) { if ($sep === '.' || $sep === '[' || $sep === '') { if (is_array($cur) === false) { $cur = array(); } $cur =& $cur[$m[2][$k]]; } elseif ($sep === '->') { if (is_object($cur) === false) { $cur = new stdClass; } $cur =& $cur->$m[2][$k]; } else { return false; } } if ($last[0] === '.' || $last[0] === '[' || $last[0] === '') { if (is_array($cur) === false) { $cur = array(); } $cur[$last[1]] = $value; } elseif ($last[0] === '->') { if (is_object($cur) === false) { $cur = new stdClass; } $cur->$last[1] = $value; } else { return false; } } } public function setScope($scope, $absolute = false) { $old = $this->scopeTree; if (is_string($scope)===true) { $scope = explode('.', $scope); } if ($absolute===true) { $this->scope =& $this->data; $this->scopeTree = array(); } while (($bit = array_shift($scope)) !== null) { if ($bit === '_' || $bit === '_parent') { array_pop($this->scopeTree); $this->scope =& $this->data; $cnt = count($this->scopeTree); for ($i=0;$i<$cnt;$i++) $this->scope =& $this->scope[$this->scopeTree[$i]]; } elseif ($bit === '__' || $bit === '_root') { $this->scope =& $this->data; $this->scopeTree = array(); } elseif (isset($this->scope[$bit])) { $this->scope =& $this->scope[$bit]; $this->scopeTree[] = $bit; } else { unset($this->scope); $this->scope = null; } } return $old; } public function getData() { return $this->data; } public function &getScope() { return $this->scope; } public function __call($method, $args) { $proxy = $this->getPluginProxy(); if (!$proxy) { throw new Dwoo_Exception('Call to undefined method '.__CLASS__.'::'.$method.'()'); } return call_user_func_array($proxy->getCallback($method), $args); } } class Dwoo extends Dwoo_Core { } interface Dwoo_IPluginProxy { public function handles($name); public function getCode($name, $params); public function getCallback($name); public function getLoader($name); } interface Dwoo_IElseable { } interface Dwoo_ILoader { public function loadPlugin($class, $forceRehash = true); } class Dwoo_Loader implements Dwoo_ILoader { protected $paths = array(); protected $classPath = array(); protected $cacheDir; protected $corePluginDir; public function __construct($cacheDir) { $this->corePluginDir = DWOO_DIRECTORY . 'plugins'; $this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; $cacheFile = $this->cacheDir.'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php'; if (file_exists($cacheFile)) { $classpath = file_get_contents($cacheFile); $this->classPath = unserialize($classpath) + $this->classPath; } else { $this->rebuildClassPathCache($this->corePluginDir, $cacheFile); } } protected function rebuildClassPathCache($path, $cacheFile) { if ($cacheFile!==false) { $tmp = $this->classPath; $this->classPath = array(); } $list = glob(rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*'); if (is_array($list)) { foreach ($list as $f) { if (is_dir($f)) { $this->rebuildClassPathCache($f, false); } else { $this->classPath[str_replace(array('function.','block.','modifier.','outputfilter.','filter.','prefilter.','postfilter.','pre.','post.','output.','shared.','helper.'), '', basename($f, '.php'))] = $f; } } } if ($cacheFile!==false) { if (!file_put_contents($cacheFile, serialize($this->classPath))) { throw new Dwoo_Exception('Could not write into '.$cacheFile.', either because the folder is not there (create it) or because of the chmod configuration (please ensure this directory is writable by php), alternatively you can change the directory used with $dwoo->setCompileDir() or provide a custom loader object with $dwoo->setLoader()'); } $this->classPath += $tmp; } } public function loadPlugin($class, $forceRehash = true) { if (!isset($this->classPath[$class]) || !(include $this->classPath[$class])) { if ($forceRehash) { $this->rebuildClassPathCache($this->corePluginDir, $this->cacheDir . 'classpath.cache.d'.Dwoo_Core::RELEASE_TAG.'.php'); foreach ($this->paths as $path=>$file) { $this->rebuildClassPathCache($path, $file); } if (isset($this->classPath[$class])) { include $this->classPath[$class]; } else { throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); } } else { throw new Dwoo_Exception('Plugin <em>'.$class.'</em> can not be found, maybe you forgot to bind it if it\'s a custom plugin ?', E_USER_NOTICE); } } } public function addDirectory($pluginDirectory) { $pluginDir = realpath($pluginDirectory); if (!$pluginDir) { throw new Dwoo_Exception('Plugin directory does not exist or can not be read : '.$pluginDirectory); } $cacheFile = $this->cacheDir . 'classpath-'.substr(strtr($pluginDir, '/\\:'.PATH_SEPARATOR, '----'), strlen($pluginDir) > 80 ? -80 : 0).'.d'.Dwoo_Core::RELEASE_TAG.'.php'; $this->paths[$pluginDir] = $cacheFile; if (file_exists($cacheFile)) { $classpath = file_get_contents($cacheFile); $this->classPath = unserialize($classpath) + $this->classPath; } else { $this->rebuildClassPathCache($pluginDir, $cacheFile); } } } class Dwoo_Exception extends Exception { } class Dwoo_Security_Policy { const PHP_ENCODE = 1; const PHP_REMOVE = 2; const PHP_ALLOW = 3; const CONST_DISALLOW = false; const CONST_ALLOW = true; protected $allowedPhpFunctions = array ( 'str_repeat', 'number_format', 'htmlentities', 'htmlspecialchars', 'long2ip', 'strlen', 'list', 'empty', 'count', 'sizeof', 'in_array', 'is_array', ); protected $allowedDirectories = array(); protected $phpHandling = self::PHP_REMOVE; protected $constHandling = self::CONST_DISALLOW; public function allowPhpFunction($func) { if (is_array($func)) foreach ($func as $fname) $this->allowedPhpFunctions[strtolower($fname)] = true; else $this->allowedPhpFunctions[strtolower($func)] = true; } public function disallowPhpFunction($func) { if (is_array($func)) foreach ($func as $fname) unset($this->allowedPhpFunctions[strtolower($fname)]); else unset($this->allowedPhpFunctions[strtolower($func)]); } public function getAllowedPhpFunctions() { return $this->allowedPhpFunctions; } public function allowDirectory($path) { if (is_array($path)) foreach ($path as $dir) $this->allowedDirectories[realpath($dir)] = true; else $this->allowedDirectories[realpath($path)] = true; } public function disallowDirectory($path) { if (is_array($path)) foreach ($path as $dir) unset($this->allowedDirectories[realpath($dir)]); else unset($this->allowedDirectories[realpath($path)]); } public function getAllowedDirectories() { return $this->allowedDirectories; } public function setPhpHandling($level = self::PHP_REMOVE) { $this->phpHandling = $level; } public function getPhpHandling() { return $this->phpHandling; } public function setConstantHandling($level = self::CONST_DISALLOW) { $this->constHandling = $level; } public function getConstantHandling() { return $this->constHandling; } } class Dwoo_Security_Exception extends Dwoo_Exception { } interface Dwoo_ICompilable { } interface Dwoo_ICompiler { public function compile(Dwoo_Core $dwoo, Dwoo_ITemplate $template); public function setCustomPlugins(array $customPlugins); public function setSecurityPolicy(Dwoo_Security_Policy $policy = null); } interface Dwoo_IDataProvider { public function getData(); } interface Dwoo_ITemplate { public function getCacheTime(); public function setCacheTime($seconds = null); public function getCachedTemplate(Dwoo_Core $dwoo); public function cache(Dwoo_Core $dwoo, $output); public function clearCache(Dwoo_Core $dwoo, $olderThan = -1); public function getCompiledTemplate(Dwoo_Core $dwoo, Dwoo_ICompiler $compiler = null); public function getName(); public function getResourceName(); public function getResourceIdentifier(); public function getSource(); public function getUid(); public function getCompiler(); public function getIsModifiedCode(); public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null); } interface Dwoo_ICompilable_Block { } abstract class Dwoo_Plugin { protected $dwoo; public function __construct(Dwoo_Core $dwoo) { $this->dwoo = $dwoo; } public static function paramsToAttributes(array $params, $delim = '\'') { if (isset($params['*'])) { $params = array_merge($params, $params['*']); unset($params['*']); } $out = ''; foreach ($params as $attr=>$val) { $out .= ' '.$attr.'='; if (trim($val, '"\'')=='' || $val=='null') { $out .= str_replace($delim, '\\'.$delim, '""'); } elseif (substr($val, 0, 1) === $delim && substr($val, -1) === $delim) { $out .= str_replace($delim, '\\'.$delim, '"'.substr($val, 1, -1).'"'); } else { $out .= str_replace($delim, '\\'.$delim, '"') . $delim . '.'.$val.'.' . $delim . str_replace($delim, '\\'.$delim, '"'); } } return ltrim($out); } } abstract class Dwoo_Block_Plugin extends Dwoo_Plugin { protected $buffer = ''; public function buffer($input) { $this->buffer .= $input; } public function end() { } public function process() { return $this->buffer; } public static function preProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $type) { return Dwoo_Compiler::PHP_OPEN.$prepend.'$this->addStack("'.$type.'", array('.Dwoo_Compiler::implode_r($compiler->getCompiledParams($params)).'));'.$append.Dwoo_Compiler::PHP_CLOSE; } public static function postProcessing(Dwoo_Compiler $compiler, array $params, $prepend, $append, $content) { return $content . Dwoo_Compiler::PHP_OPEN.$prepend.'$this->delStack();'.$append.Dwoo_Compiler::PHP_CLOSE; } } abstract class Dwoo_Filter { protected $dwoo; public function __construct(Dwoo_Core $dwoo) { $this->dwoo = $dwoo; } abstract public function process($input); } abstract class Dwoo_Processor { protected $compiler; public function __construct(Dwoo_Compiler $compiler) { $this->compiler = $compiler; } abstract public function process($input); } class Dwoo_Template_String implements Dwoo_ITemplate { protected $name; protected $compileId; protected $cacheId; protected $cacheTime; protected $compilationEnforced; protected static $cache = array('cached'=>array(), 'compiled'=>array()); protected $compiler; protected $chmod = 0777; public function __construct($templateString, $cacheTime = null, $cacheId = null, $compileId = null) { $this->template = $templateString; if (function_exists('hash')) { $this->name = hash('md4', $templateString); } else { $this->name = md5($templateString); } $this->cacheTime = $cacheTime; if ($compileId !== null) { $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if ($cacheId !== null) { $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } } public function getCacheTime() { return $this->cacheTime; } public function setCacheTime($seconds = null) { $this->cacheTime = $seconds; } public function getChmod() { return $this->chmod; } public function setChmod($mask = null) { $this->chmod = $mask; } public function getName() { return $this->name; } public function getResourceName() { return 'string'; } public function getResourceIdentifier() { return false; } public function getSource() { return $this->template; } public function getUid() { return $this->name; } public function getCompiler() { return $this->compiler; } public function forceCompilation() { $this->compilationEnforced = true; } public function getCachedTemplate(Dwoo_Core $dwoo) { if ($this->cacheTime !== null) { $cacheLength = $this->cacheTime; } else { $cacheLength = $dwoo->getCacheTime(); } if ($cacheLength === 0) { return false; } $cachedFile = $this->getCacheFilename($dwoo); if (isset(self::$cache['cached'][$this->cacheId]) === true && file_exists($cachedFile)) { return $cachedFile; } elseif ($this->compilationEnforced !== true && file_exists($cachedFile) && ($cacheLength === -1 || filemtime($cachedFile) > ($_SERVER['REQUEST_TIME'] - $cacheLength)) && $this->isValidCompiledFile($this->getCompiledFilename($dwoo))) { self::$cache['cached'][$this->cacheId] = true; return $cachedFile; } else { return true; } } public function cache(Dwoo_Core $dwoo, $output) { $cacheDir = $dwoo->getCacheDir(); $cachedFile = $this->getCacheFilename($dwoo); $temp = tempnam($cacheDir, 'temp'); if (!($file = @fopen($temp, 'wb'))) { $temp = $cacheDir . uniqid('temp'); if (!($file = @fopen($temp, 'wb'))) { trigger_error('Error writing temporary file \''.$temp.'\'', E_USER_WARNING); return false; } } fwrite($file, $output); fclose($file); $this->makeDirectory(dirname($cachedFile), $cacheDir); if (!@rename($temp, $cachedFile)) { @unlink($cachedFile); @rename($temp, $cachedFile); } if ($this->chmod !== null) { chmod($cachedFile, $this->chmod); } self::$cache['cached'][$this->cacheId] = true; return $cachedFile; } public function clearCache(Dwoo_Core $dwoo, $olderThan = -1) { $cachedFile = $this->getCacheFilename($dwoo); return !file_exists($cachedFile) || (filectime($cachedFile) < (time() - $olderThan) && unlink($cachedFile)); } public function getCompiledTemplate(Dwoo_Core $dwoo, Dwoo_ICompiler $compiler = null) { $compiledFile = $this->getCompiledFilename($dwoo); if ($this->compilationEnforced !== true && isset(self::$cache['compiled'][$this->compileId]) === true) { } elseif ($this->compilationEnforced !== true && $this->isValidCompiledFile($compiledFile)) { self::$cache['compiled'][$this->compileId] = true; } else { $this->compilationEnforced = false; if ($compiler === null) { $compiler = $dwoo->getDefaultCompilerFactory($this->getResourceName()); if ($compiler === null || $compiler === array('Dwoo_Compiler', 'compilerFactory')) { if (class_exists('Dwoo_Compiler', false) === false) { include DWOO_DIRECTORY . 'Dwoo/Compiler.php'; } $compiler = Dwoo_Compiler::compilerFactory(); } else { $compiler = call_user_func($compiler); } } $this->compiler = $compiler; $compiler->setCustomPlugins($dwoo->getCustomPlugins()); $compiler->setSecurityPolicy($dwoo->getSecurityPolicy()); $this->makeDirectory(dirname($compiledFile), $dwoo->getCompileDir()); file_put_contents($compiledFile, $compiler->compile($dwoo, $this)); if ($this->chmod !== null) { chmod($compiledFile, $this->chmod); } self::$cache['compiled'][$this->compileId] = true; } return $compiledFile; } protected function isValidCompiledFile($file) { return file_exists($file); } public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { return new self($resourceId, $cacheTime, $cacheId, $compileId); } protected function getCompiledFilename(Dwoo_Core $dwoo) { if ($this->compileId===null) { $this->compileId = $this->name; } return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo_Core::RELEASE_TAG.'.php'; } protected function getCacheFilename(Dwoo_Core $dwoo) { if ($this->cacheId === null) { if (isset($_SERVER['REQUEST_URI']) === true) { $cacheId = $_SERVER['REQUEST_URI']; } elseif (isset($_SERVER['SCRIPT_FILENAME']) && isset($_SERVER['argv'])) { $cacheId = $_SERVER['SCRIPT_FILENAME'].'-'.implode('-', $_SERVER['argv']); } else { $cacheId = ''; } $this->getCompiledFilename($dwoo); $this->cacheId = str_replace('../', '__', $this->compileId . strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } return $dwoo->getCacheDir() . $this->cacheId.'.html'; } public function getIsModifiedCode() { return null; } protected function makeDirectory($path, $baseDir = null) { if (is_dir($path) === true) { return; } if ($this->chmod === null) { $chmod = 0777; } else { $chmod = $this->chmod; } mkdir($path, $chmod, true); if (strpos(PHP_OS, 'WIN') !== 0 && $baseDir !== null) { $path = strtr(str_replace($baseDir, '', $path), '\\', '/'); $folders = explode('/', trim($path, '/')); foreach ($folders as $folder) { $baseDir .= $folder . DIRECTORY_SEPARATOR; chmod($baseDir, $chmod); } } } } class Dwoo_Template_File extends Dwoo_Template_String { protected $file; protected $includePath = null; protected $resolvedPath = null; public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = null) { $this->file = $file; $this->name = basename($file); $this->cacheTime = $cacheTime; if ($compileId !== null) { $this->compileId = str_replace('../', '__', strtr($compileId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if ($cacheId !== null) { $this->cacheId = str_replace('../', '__', strtr($cacheId, '\\%?=!:;'.PATH_SEPARATOR, '/-------')); } if (is_string($includePath)) { $this->includePath = array($includePath); } elseif (is_array($includePath)) { $this->includePath = $includePath; } } public function setIncludePath($paths) { if (is_array($paths) === false) { $paths = array($paths); } $this->includePath = $paths; $this->resolvedPath = null; } public function getIncludePath() { return $this->includePath; } protected function isValidCompiledFile($file) { return parent::isValidCompiledFile($file) && (int)$this->getUid() <= filemtime($file); } public function getSource() { return file_get_contents($this->getResourceIdentifier()); } public function getResourceName() { return 'file'; } public function getResourceIdentifier() { if ($this->resolvedPath !== null) { return $this->resolvedPath; } elseif ($this->includePath === null) { return $this->file; } else { foreach ($this->includePath as $path) { $path = rtrim($path, DIRECTORY_SEPARATOR); if (file_exists($path.DIRECTORY_SEPARATOR.$this->file) === true) { $this->resolvedPath = $path . DIRECTORY_SEPARATOR . $this->file; return $this->resolvedPath; } } throw new Dwoo_Exception('Template "'.$this->file.'" could not be found in any of your include path(s)'); } } public function getUid() { return (string) filemtime($this->getResourceIdentifier()); } public static function templateFactory(Dwoo_Core $dwoo, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) { if (DIRECTORY_SEPARATOR === '\\') { $resourceId = str_replace(array("\t", "\n", "\r", "\f", "\v"), array('\\t', '\\n', '\\r', '\\f', '\\v'), $resourceId); } $resourceId = strtr($resourceId, '\\', '/'); $includePath = null; if (file_exists($resourceId) === false) { if ($parentTemplate === null) { $parentTemplate = $dwoo->getTemplate(); } if ($parentTemplate instanceof Dwoo_Template_File) { if ($includePath = $parentTemplate->getIncludePath()) { if (strstr($resourceId, '../')) { throw new Dwoo_Exception('When using an include path you can not reference a template into a parent directory (using ../)'); } } else { $resourceId = dirname($parentTemplate->getResourceIdentifier()).DIRECTORY_SEPARATOR.$resourceId; if (file_exists($resourceId) === false) { return null; } } } else { return null; } } if ($policy = $dwoo->getSecurityPolicy()) { while (true) { if (preg_match('{^([a-z]+?)://}i', $resourceId)) { throw new Dwoo_Security_Exception('The security policy prevents you to read files from external sources : <em>'.$resourceId.'</em>.'); } if ($includePath) { break; } $resourceId = realpath($resourceId); $dirs = $policy->getAllowedDirectories(); foreach ($dirs as $dir=>$dummy) { if (strpos($resourceId, $dir) === 0) { break 2; } } throw new Dwoo_Security_Exception('The security policy prevents you to read <em>'.$resourceId.'</em>'); } } $class = 'Dwoo_Template_File'; if ($parentTemplate) { $class = get_class($parentTemplate); } return new $class($resourceId, $cacheTime, $cacheId, $compileId, $includePath); } protected function getCompiledFilename(Dwoo_Core $dwoo) { if ($this->compileId===null) { $this->compileId = str_replace('../', '__', strtr($this->getResourceIdentifier(), '\\:', '/-')); } return $dwoo->getCompileDir() . $this->compileId.'.d'.Dwoo_Core::RELEASE_TAG.'.php'; } public function getIsModifiedCode() { return '"'.$this->getUid().'" == filemtime('.var_export($this->getResourceIdentifier(), true).')'; } } class Dwoo_Data implements Dwoo_IDataProvider { protected $data = array(); public function getData() { return $this->data; } public function clear($name = null) { if ($name === null) { $this->data = array(); } elseif (is_array($name)) { foreach ($name as $index) unset($this->data[$index]); } else { unset($this->data[$name]); } } public function setData(array $data) { $this->data = $data; } public function mergeData(array $data) { $args = func_get_args(); while (list(,$v) = each($args)) { if (is_array($v)) { $this->data = array_merge($this->data, $v); } } } public function assign($name, $val = null) { if (is_array($name)) { reset($name); while (list($k,$v) = each($name)) $this->data[$k] = $v; } else { $this->data[$name] = $val; } } public function __set($name, $value) { $this->assign($name, $value); } public function assignByRef($name, &$val) { $this->data[$name] =& $val; } public function append($name, $val = null, $merge = false) { if (is_array($name)) { foreach ($name as $key=>$val) { if (isset($this->data[$key]) && !is_array($this->data[$key])) { settype($this->data[$key], 'array'); } if ($merge === true && is_array($val)) { $this->data[$key] = $val + $this->data[$key]; } else { $this->data[$key][] = $val; } } } elseif ($val !== null) { if (isset($this->data[$name]) && !is_array($this->data[$name])) { settype($this->data[$name], 'array'); } elseif (!isset($this->data[$name])) { $this->data[$name] = array(); } if ($merge === true && is_array($val)) { $this->data[$name] = $val + $this->data[$name]; } else { $this->data[$name][] = $val; } } } public function appendByRef($name, &$val, $merge = false) { if (isset($this->data[$name]) && !is_array($this->data[$name])) { settype($this->data[$name], 'array'); } if ($merge === true && is_array($val)) { foreach ($val as $key => &$val) { $this->data[$name][$key] =& $val; } } else { $this->data[$name][] =& $val; } } public function isAssigned($name) { return isset($this->data[$name]); } public function __isset($name) { return isset($this->data[$name]); } public function unassign($name) { unset($this->data[$name]); } public function __unset($name) { unset($this->data[$name]); } public function get($name) { return $this->__get($name); } public function __get($name) { if (isset($this->data[$name])) { return $this->data[$name]; } else { throw new Dwoo_Exception('Tried to read a value that was not assigned yet : "'.$name.'"'); } } } |
| 26 |
12 |
* @license http://dwoo.org/LICENSE Modified BSD License |
| 27 |
13 |
* @link http://dwoo.org/ |
| 28 |
14 |
* @version 1.1.0 |
| 29 |
|
- * @date 2009-07-18 |
|
15 |
+ * @date 2010-02-28 |
| 30 |
16 |
* @package Dwoo |
|
17 |
+ * @see Dwoo_Core |
| 31 |
18 |
*/ |
| 32 |
|
-class Dwoo |
|
19 |
+class Dwoo extends Dwoo_Core |
| 33 |
20 |
{ |
| 34 |
|
- /** |
| 35 |
|
- * current version number |
| 36 |
|
- * |
| 37 |
|
- * @var string |
| 38 |
|
- */ |
| 39 |
|
- const VERSION = '1.1.1'; |
| 40 |
|
- |
| 41 |
|
- /** |
| 42 |
|
- * unique number of this dwoo release |
| 43 |
|
- * |
| 44 |
|
- * this can be used by templates classes to check whether the compiled template |
| 45 |
|
- * has been compiled before this release or not, so that old templates are |
| 46 |
|
- * recompiled automatically when Dwoo is updated |
| 47 |
|
- */ |
| 48 |
|
- const RELEASE_TAG = 17; |
| 49 |
|
- |
| 50 |
|
- /**#@+ |
| 51 |
|
- * constants that represents all plugin types |
| 52 |
|
- * |
| 53 |
|
- * these are bitwise-operation-safe values to allow multiple types |
| 54 |
|
- * on a single plugin |
| 55 |
|
- * |
| 56 |
|
- * @var int |
| 57 |
|
- */ |
| 58 |
|
- const CLASS_PLUGIN = 1; |
| 59 |
|
- const FUNC_PLUGIN = 2; |
| 60 |
|
- const NATIVE_PLUGIN = 4; |
| 61 |
|
- const BLOCK_PLUGIN = 8; |
| 62 |
|
- const COMPILABLE_PLUGIN = 16; |
| 63 |
|
- const CUSTOM_PLUGIN = 32; |
| 64 |
|
- const SMARTY_MODIFIER = 64; |
| 65 |
|
- const SMARTY_BLOCK = 128; |
| 66 |
|
- const SMARTY_FUNCTION = 256; |
| 67 |
|
- const PROXY_PLUGIN = 512; |
| 68 |
|
- const TEMPLATE_PLUGIN = 1024; |
| 69 |
|
- /**#@-*/ |
| 70 |
|
- |
| 71 |
|
- /** |
| 72 |
|
- * character set of the template, used by string manipulation plugins |
| 73 |
|
- * |
| 74 |
|
- * it must be lowercase, but setCharset() will take care of that |
| 75 |
|
- * |
| 76 |
|
- * @see setCharset |
| 77 |
|
- * @see getCharset |
| 78 |
|
- * @var string |
| 79 |
|
- */ |
| 80 |
|
- protected $charset = 'utf-8'; |
| 81 |
|
- |
| 82 |
|
- /** |
| 83 |
|
- * global variables that are accessible through $dwoo.* in the templates |
| 84 |
|
- * |
| 85 |
|
- * default values include: |
| 86 |
|
- * |
| 87 |
|
- * $dwoo.version - current version number |
| 88 |
|
- * $dwoo.ad - a Powered by Dwoo link pointing to dwoo.org |
| 89 |
|
- * $dwoo.now - the current time |
| 90 |
|
- * $dwoo.template - the current template filename |
| 91 |
|
- * $dwoo.charset - the character set used by the template |
| 92 |
|
- * |
| 93 |
|
- * on top of that, foreach and other plugins can store special values in there, |
| 94 |
|
- * see their documentation for more details. |
| 95 |
|
- * |
| 96 |
|
- * @private |
| 97 |
|
- * @var array |
| 98 |
|
- */ |
| 99 |
|
- public $globals; |
| 100 |
|
- |
| 101 |
|
- /** |
| 102 |
|
- * directory where the compiled templates are stored |
| 103 |
|
- * |
| 104 |
|
- * defaults to DWOO_COMPILEDIR (= dwoo_dir/compiled by default) |
| 105 |
|
- * |
| 106 |
|
- * @var string |
| 107 |
|
- */ |
| 108 |
|
- protected $compileDir; |
| 109 |
|
- |
| 110 |
|
- /** |
| 111 |
|
- * directory where the cached templates are stored |
| 112 |
|
- * |
| 113 |
|
- * defaults to DWOO_CACHEDIR (= dwoo_dir/cache by default) |
| 114 |
|
- * |
| 115 |
|
- * @var string |
| 116 |
|
- */ |
| 117 |
|
- protected $cacheDir; |
| 118 |
|
- |
| 119 |
|
- /** |
| 120 |
|
- * defines how long (in seconds) the cached files must remain valid |
| 121 |
|
- * |
| 122 |
|
- * can be overriden on a per-template basis |
| 123 |
|
- * |
| 124 |
|
- * -1 = never delete |
| 125 |
|
- * 0 = disabled |
| 126 |
|
- * >0 = duration in seconds |
| 127 |
|
- * |
| 128 |
|
- * @var int |
| 129 |
|
- */ |
| 130 |
|
- protected $cacheTime = 0; |
| 131 |
|
- |
| 132 |
|
- /** |
| 133 |
|
- * security policy object |
| 134 |
|
- * |
| 135 |
|
- * @var Dwoo_Security_Policy |
| 136 |
|
- */ |
| 137 |
|
- protected $securityPolicy = null; |
| 138 |
|
- |
| 139 |
|
- /** |
| 140 |
|
- * stores the custom plugins callbacks |
| 141 |
|
- * |
| 142 |
|
- * @see addPlugin |
| 143 |
|
- * @see removePlugin |
| 144 |
|
- * @var array |
| 145 |
|
- */ |
| 146 |
|
- protected $plugins = array(); |
| 147 |
|
- |
| 148 |
|
- /** |
| 149 |
|
- * stores the filter callbacks |
| 150 |
|
- * |
| 151 |
|
- * @see addFilter |
| 152 |
|
- * @see removeFilter |
| 153 |
|
- * @var array |
| 154 |
|
- */ |
| 155 |
|
- protected $filters = array(); |
| 156 |
|
- |
| 157 |
|
- /** |
| 158 |
|
- * stores the resource types and associated |
| 159 |
|
- * classes / compiler classes |
| 160 |
|
- * |
| 161 |
|
- * @var array |
| 162 |
|
- */ |
| 163 |
|
- protected $resources = array |
| 164 |
|
- ( |
| 165 |
|
- 'file' => array |
| 166 |
|
- ( |
| 167 |
|
- 'class' => 'Dwoo_Template_File', |
| 168 |
|
- 'compiler' => null |
| 169 |
|
- ), |
| 170 |
|
- 'string' => array |
| 171 |
|
- ( |
| 172 |
|
- 'class' => 'Dwoo_Template_String', |
| 173 |
|
- 'compiler' => null |
| 174 |
|
- ) |
| 175 |
|
- ); |
| 176 |
|
- |
| 177 |
|
- /** |
| 178 |
|
- * the dwoo loader object used to load plugins by this dwoo instance |
| 179 |
|
- * |
| 180 |
|
- * @var Dwoo_ILoader |
| 181 |
|
- */ |
| 182 |
|
- protected $loader = null; |
| 183 |
|
- |
| 184 |
|
- /** |
| 185 |
|
- * currently rendered template, set to null when not-rendering |
| 186 |
|
- * |
| 187 |
|
- * @var Dwoo_ITemplate |
| 188 |
|
- */ |
| 189 |
|
- protected $template = null; |
| 190 |
|
- |
| 191 |
|
- /** |
| 192 |
|
- * stores the instances of the class plugins during template runtime |
| 193 |
|
- * |
| 194 |
|
- * @var array |
| 195 |
|
- */ |
| 196 |
|
- protected $runtimePlugins; |
| 197 |
|
- |
| 198 |
|
- /** |
| 199 |
|
- * stores the data during template runtime |
| 200 |
|
- * |
| 201 |
|
- * @var array |
| 202 |
|
- * @private |
| 203 |
|
- */ |
| 204 |
|
- public $data; |
| 205 |
|
- |
| 206 |
|
- /** |
| 207 |
|
- * stores the current scope during template runtime |
| 208 |
|
- * |
| 209 |
|
- * this should ideally not be accessed directly from outside template code |
| 210 |
|
- * |
| 211 |
|
- * @var mixed |
| 212 |
|
- * @private |
| 213 |
|
- */ |
| 214 |
|
- public $scope; |
| 215 |
|
- |
| 216 |
|
- /** |
| 217 |
|
- * stores the scope tree during template runtime |
| 218 |
|
- * |
| 219 |
|
- * @var array |
| 220 |
|
- */ |
| 221 |
|
- protected $scopeTree; |
| 222 |
|
- |
| 223 |
|
- /** |
| 224 |
|
- * stores the block plugins stack during template runtime |
| 225 |
|
- * |
| 226 |
|
- * @var array |
| 227 |
|
- */ |
| 228 |
|
- protected $stack; |
| 229 |
|
- |
| 230 |
|
- /** |
| 231 |
|
- * stores the current block plugin at the top of the stack during template runtime |
| 232 |
|
- * |
| 233 |
|
- * @var Dwoo_Block_Plugin |
| 234 |
|
- */ |
| 235 |
|
- protected $curBlock; |
| 236 |
|
- |
| 237 |
|
- /** |
| 238 |
|
- * stores the output buffer during template runtime |
| 239 |
|
- * |
| 240 |
|
- * @var string |
| 241 |
|
- */ |
| 242 |
|
- protected $buffer; |
| 243 |
|
- |
| 244 |
|
- /** |
| 245 |
|
- * stores plugin proxy |
| 246 |
|
- * |
| 247 |
|
- * @var Dwoo_IPluginProxy |
| 248 |
|
- */ |
| 249 |
|
- protected $pluginProxy; |
| 250 |
|
- |
| 251 |
|
- /** |
| 252 |
|
- * constructor, sets the cache and compile dir to the default values if not provided |
| 253 |
|
- * |
| 254 |
|
- * @param string $compileDir path to the compiled directory, defaults to lib/compiled |
| 255 |
|
- * @param string $cacheDir path to the cache directory, defaults to lib/cache |
| 256 |
|
- */ |
| 257 |
|
- public function __construct($compileDir = null, $cacheDir = null) |
| 258 |
|
- { |
| 259 |
|
- if ($compileDir !== null) { |
| 260 |
|
- $this->setCompileDir($compileDir); |
| 261 |
|
- } |
| 262 |
|
- if ($cacheDir !== null) { |
| 263 |
|
- $this->setCacheDir($cacheDir); |
| 264 |
|
- } |
| 265 |
|
- $this->initGlobals(); |
| 266 |
|
- } |
| 267 |
|
- |
| 268 |
|
- /** |
| 269 |
|
- * resets some runtime variables to allow a cloned object to be used to render sub-templates |
| 270 |
|
- */ |
| 271 |
|
- public function __clone() |
| 272 |
|
- { |
| 273 |
|
- $this->template = null; |
| 274 |
|
- unset($this->data); |
| 275 |
|
- } |
| 276 |
|
- |
| 277 |
|
- /** |
| 278 |
|
- * outputs the template instead of returning it, this is basically a shortcut for get(*, *, *, true) |
| 279 |
|
- * |
| 280 |
|
- * @see get |
| 281 |
|
- * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or |
| 282 |
|
- * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster, |
| 283 |
|
- * especially if you render a template multiple times |
| 284 |
|
- * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're |
| 285 |
|
- * rendering the template from cache, it can be left null |
| 286 |
|
- * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default |
| 287 |
|
- * Dwoo_Compiler will be used. |
| 288 |
|
- * @return string nothing or the template output if $output is true |
| 289 |
|
- */ |
| 290 |
|
- public function output($tpl, $data = array(), Dwoo_ICompiler $compiler = null) |
| 291 |
|
- { |
| 292 |
|
- return $this->get($tpl, $data, $compiler, true); |
| 293 |
|
- } |
| 294 |
|
- |
| 295 |
|
- /** |
| 296 |
|
- * returns the given template rendered using the provided data and optional compiler |
| 297 |
|
- * |
| 298 |
|
- * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or |
| 299 |
|
- * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster, |
| 300 |
|
- * especially if you render a template multiple times |
| 301 |
|
- * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're |
| 302 |
|
- * rendering the template from cache, it can be left null |
| 303 |
|
- * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default |
| 304 |
|
- * Dwoo_Compiler will be used. |
| 305 |
|
- * @param bool $output flag that defines whether the function returns the output of the template (false, default) or echoes it directly (true) |
| 306 |
|
- * @return string nothing or the template output if $output is true |
| 307 |
|
- */ |
| 308 |
|
- public function get($_tpl, $data = array(), $_compiler = null, $_output = false) |
| 309 |
|
- { |
| 310 |
|
- // a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one |
| 311 |
|
- if ($this->template instanceof Dwoo_ITemplate) { |
| 312 |
|
- $proxy = clone $this; |
| 313 |
|
- return $proxy->get($_tpl, $data, $_compiler, $_output); |
| 314 |
|
- } |
| 315 |
|
- |
| 316 |
|
- // auto-create template if required |
| 317 |
|
- if ($_tpl instanceof Dwoo_ITemplate) { |
| 318 |
|
- // valid, skip |
| 319 |
|
- } elseif (is_string($_tpl) && file_exists($_tpl)) { |
| 320 |
|
- $_tpl = new Dwoo_Template_File($_tpl); |
| 321 |
|
- } else { |
| 322 |
|
- throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE); |
| 323 |
|
- } |
| 324 |
|
- |
| 325 |
|
- // save the current template, enters render mode at the same time |
| 326 |
|
- // if another rendering is requested it will be proxied to a new Dwoo instance |
| 327 |
|
- $this->template = $_tpl; |
| 328 |
|
- |
| 329 |
|
- // load data |
| 330 |
|
- if ($data instanceof Dwoo_IDataProvider) { |
| 331 |
|
- $this->data = $data->getData(); |
| 332 |
|
- } elseif (is_array($data)) { |
| 333 |
|
- $this->data = $data; |
| 334 |
|
- } else { |
| 335 |
|
- throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE); |
| 336 |
|
- } |
| 337 |
|
- |
| 338 |
|
- $this->globals['template'] = $_tpl->getName(); |
| 339 |
|
- $this->initRuntimeVars($_tpl); |
| 340 |
|
- |
| 341 |
|
- // try to get cached template |
| 342 |
|
- $file = $_tpl->getCachedTemplate($this); |
| 343 |
|
- $doCache = $file === true; |
| 344 |
|
- $cacheLoaded = is_string($file); |
| 345 |
|
- |
| 346 |
|
- if ($cacheLoaded === true) { |
| 347 |
|
- // cache is present, run it |
| 348 |
|
- if ($_output === true) { |
| 349 |
|
- include $file; |
| 350 |
|
- $this->template = null; |
| 351 |
|
- } else { |
| 352 |
|
- ob_start(); |
| 353 |
|
- include $file; |
| 354 |
|
- $this->template = null; |
| 355 |
|
- return ob_get_clean(); |
| 356 |
|
- } |
| 357 |
|
- } else { |
| 358 |
|
- // no cache present |
| 359 |
|
- if ($doCache === true) { |
| 360 |
|
- $dynamicId = uniqid(); |
| 361 |
|
- } |
| 362 |
|
- |
| 363 |
|
- // render template |
| 364 |
|
- $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); |
| 365 |
|
- $out = include $compiledTemplate; |
| 366 |
|
- |
| 367 |
|
- // template returned false so it needs to be recompiled |
| 368 |
|
- if ($out === false) { |
| 369 |
|
- $_tpl->forceCompilation(); |
| 370 |
|
- $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); |
| 371 |
|
- $out = include $compiledTemplate; |
| 372 |
|
- } |
| 373 |
|
- |
| 374 |
|
- if ($doCache === true) { |
| 375 |
|
- $out = preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php /*'.$dynamicId.'*/ echo \'$1\'; ?>', $out); |
| 376 |
|
- if (!class_exists('Dwoo_plugin_dynamic', false)) { |
| 377 |
|
- $this->getLoader()->loadPlugin('dynamic'); |
| 378 |
|
- } |
| 379 |
|
- $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate); |
| 380 |
|
- } |
| 381 |
|
- |
| 382 |
|
- // process filters |
| 383 |
|
- foreach ($this->filters as $filter) { |
| 384 |
|
- if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) { |
| 385 |
|
- $out = call_user_func($filter, $out); |
| 386 |
|
- } else { |
| 387 |
|
- $out = call_user_func($filter, $this, $out); |
| 388 |
|
- } |
| 389 |
|
- } |
| 390 |
|
- |
| 391 |
|
- if ($doCache === true) { |
| 392 |
|
- // building cache |
| 393 |
|
- $file = $_tpl->cache($this, $out); |
| 394 |
|
- |
| 395 |
|
- // run it from the cache to be sure dynamics are rendered |
| 396 |
|
- if ($_output === true) { |
| 397 |
|
- include $file; |
| 398 |
|
- // exit render mode |
| 399 |
|
- $this->template = null; |
| 400 |
|
- } else { |
| 401 |
|
- ob_start(); |
| 402 |
|
- include $file; |
| 403 |
|
- // exit render mode |
| 404 |
|
- $this->template = null; |
| 405 |
|
- return ob_get_clean(); |
| 406 |
|
- } |
| 407 |
|
- } else { |
| 408 |
|
- // no need to build cache |
| 409 |
|
- // exit render mode |
| 410 |
|
- $this->template = null; |
| 411 |
|
- // output |
| 412 |
|
- if ($_output === true) { |
| 413 |
|
- echo $out; |
| 414 |
|
- } |
| 415 |
|
- return $out; |
| 416 |
|
- } |
| 417 |
|
- } |
| 418 |
|
- } |
| 419 |
|
- |
| 420 |
|
- /** |
| 421 |
|
- * re-initializes the globals array before each template run |
| 422 |
|
- * |
| 423 |
|
- * this method is only callede once when the Dwoo object is created |
| 424 |
|
- */ |
| 425 |
|
- protected function initGlobals() |
| 426 |
|
- { |
| 427 |
|
- $this->globals = array |
| 428 |
|
- ( |
| 429 |
|
- 'version' => self::VERSION, |
| 430 |
|
- 'ad' => '<a href="http://dwoo.org/">Powered by Dwoo</a>', |
| 431 |
|
- 'now' => $_SERVER['REQUEST_TIME'], |
| 432 |
|
- 'charset' => $this->charset, |
| 433 |
|
- ); |
| 434 |
|
- } |
| 435 |
|
- |
| 436 |
|
- /** |
| 437 |
|
- * re-initializes the runtime variables before each template run |
| 438 |
|
- * |
| 439 |
|
- * override this method to inject data in the globals array if needed, this |
| 440 |
|
- * method is called before each template execution |
| 441 |
|
- * |
| 442 |
|
- * @param Dwoo_ITemplate $tpl the template that is going to be rendered |
| 443 |
|
- */ |
| 444 |
|
- protected function initRuntimeVars(Dwoo_ITemplate $tpl) |
| 445 |
|
- { |
| 446 |
|
- $this->runtimePlugins = array(); |
| 447 |
|
- $this->scope =& $this->data; |
| 448 |
|
- $this->scopeTree = array(); |
| 449 |
|
- $this->stack = array(); |
| 450 |
|
- $this->curBlock = null; |
| 451 |
|
- $this->buffer = ''; |
| 452 |
|
- } |
| 453 |
|
- |
| 454 |
|
- /* |
| 455 |
|
- * --------- settings functions --------- |
| 456 |
|
- */ |
| 457 |
|
- |
| 458 |
|
- /** |
| 459 |
|
- * adds a custom plugin that is not in one of the plugin directories |
| 460 |
|
- * |
| 461 |
|
- * @param string $name the plugin name to be used in the templates |
| 462 |
|
- * @param callback $callback the plugin callback, either a function name, |
| 463 |
|
- * a class name or an array containing an object |
| 464 |
|
- * or class name and a method name |
| 465 |
|
- * @param bool $compilable if set to true, the plugin is assumed to be compilable |
| 466 |
|
- */ |
| 467 |
|
- public function addPlugin($name, $callback, $compilable = false) |
| 468 |
|
- { |
| 469 |
|
- $compilable = $compilable ? self::COMPILABLE_PLUGIN : 0; |
| 470 |
|
- if (is_array($callback)) { |
| 471 |
|
- if (is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'Dwoo_Block_Plugin')) { |
| 472 |
|
- $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0])); |
| 473 |
|
- } else { |
| 474 |
|
- $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]); |
| 475 |
|
- } |
| 476 |
|
- } elseif (class_exists($callback, false)) { |
| 477 |
|
- if (is_subclass_of($callback, 'Dwoo_Block_Plugin')) { |
| 478 |
|
- $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback); |
| 479 |
|
- } else { |
| 480 |
|
- $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process'); |
| 481 |
|
- } |
| 482 |
|
- } elseif (function_exists($callback)) { |
| 483 |
|
- $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN | $compilable, 'callback'=>$callback); |
| 484 |
|
- } else { |
| 485 |
|
- throw new Dwoo_Exception('Callback could not be processed correctly, please check that the function/class you used exists'); |
| 486 |
|
- } |
| 487 |
|
- } |
| 488 |
|
- |
| 489 |
|
- /** |
| 490 |
|
- * removes a custom plugin |
| 491 |
|
- * |
| 492 |
|
- * @param string $name the plugin name |
| 493 |
|
- */ |
| 494 |
|
- public function removePlugin($name) |
| 495 |
|
- { |
| 496 |
|
- if (isset($this->plugins[$name])) { |
| 497 |
|
- unset($this->plugins[$name]); |
| 498 |
|
- } |
| 499 |
|
- } |
| 500 |
|
- |
| 501 |
|
- /** |
| 502 |
|
- * adds a filter to this Dwoo instance, it will be used to filter the output of all the templates rendered by this instance |
| 503 |
|
- * |
| 504 |
|
- * @param mixed $callback a callback or a filter name if it is autoloaded from a plugin directory |
| 505 |
|
- * @param bool $autoload if true, the first parameter must be a filter name from one of the plugin directories |
| 506 |
|
- */ |
| 507 |
|
- public function addFilter($callback, $autoload = false) |
| 508 |
|
- { |
| 509 |
|
- if ($autoload) { |
| 510 |
|
- $class = 'Dwoo_Filter_'.$callback; |
| 511 |
|
- |
| 512 |
|
- if (!class_exists($class, false) && !function_exists($class)) { |
| 513 |
|
- try { |
| 514 |
|
- $this->getLoader()->loadPlugin($callback); |
| 515 |
|
- } catch (Dwoo_Exception $e) { |
| 516 |
|
- if (strstr($callback, 'Dwoo_Filter_')) { |
| 517 |
|
- throw new Dwoo_Exception('Wrong filter name : '.$callback.', the "Dwoo_Filter_" prefix should not be used, please only use "'.str_replace('Dwoo_Filter_', '', $callback).'"'); |
| 518 |
|
- } else { |
| 519 |
|
- throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); |
| 520 |
|
- } |
| 521 |
|
- } |
| 522 |
|
- } |
| 523 |
|
- |
| 524 |
|
- if (class_exists($class, false)) { |
| 525 |
|
- $callback = array(new $class($this), 'process'); |
| 526 |
|
- } elseif (function_exists($class)) { |
| 527 |
|
- $callback = $class; |
| 528 |
|
- } else { |
| 529 |
|
- throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); |
| 530 |
|
- } |
| 531 |
|
- |
| 532 |
|
- $this->filters[] = $callback; |
| 533 |
|
- } else { |
| 534 |
|
- $this->filters[] = $callback; |
| 535 |
|
- } |
| 536 |
|
- } |
| 537 |
|
- |
| 538 |
|
- /** |
| 539 |
|
- * removes a filter |
| 540 |
|
- * |
| 541 |
|
- * @param mixed $callback callback or filter name if it was autoloaded |
| 542 |
|
- */ |
| 543 |
|
- public function removeFilter($callback) |
| 544 |
|
- { |
| 545 |
|
- if (($index = array_search('Dwoo_Filter_'.$callback, $this->filters, true)) !== false) { |
| 546 |
|
- unset($this->filters[$index]); |
| 547 |
|
- } elseif (($index = array_search($callback, $this->filters, true)) !== false) { |
| 548 |
|
- unset($this->filters[$index]); |
| 549 |
|
- } else { |
| 550 |
|
- $class = 'Dwoo_Filter_' . $callback; |
| 551 |
|
- foreach ($this->filters as $index=>$filter) { |
| 552 |
|
- if (is_array($filter) && $filter[0] instanceof $class) { |
| 553 |
|
- unset($this->filters[$index]); |
| 554 |
|
- break; |
| 555 |
|
- } |
| 556 |
|
- } |
| 557 |
|
- } |
| 558 |
|
- } |
| 559 |
|
- |
| 560 |
|
- /** |
| 561 |
|
- * adds a resource or overrides a default one |
| 562 |
|
- * |
| 563 |
|
- * @param string $name the resource name |
| 564 |
|
- * @param string $class the resource class (which must implement Dwoo_ITemplate) |
| 565 |
|
- * @param callback $compilerFactory the compiler factory callback, a function that must return a compiler instance used to compile this resource, if none is provided. by default it will produce a Dwoo_Compiler object |
| 566 |
|
- */ |
| 567 |
|
- public function addResource($name, $class, $compilerFactory = null) |
| 568 |
|
- { |
| 569 |
|
- if (strlen($name) < 2) { |
| 570 |
|
- throw new Dwoo_Exception('Resource names must be at least two-character long to avoid conflicts with Windows paths'); |
| 571 |
|
- } |
| 572 |
|
- |
| 573 |
|
- if (!class_exists($class)) { |
| 574 |
|
- throw new Dwoo_Exception('Resource class does not exist'); |
| 575 |
|
- } |
| 576 |
|
- |
| 577 |
|
- $interfaces = class_implements($class); |
| 578 |
|
- if (in_array('Dwoo_ITemplate', $interfaces) === false) { |
| 579 |
|
- throw new Dwoo_Exception('Resource class must implement Dwoo_ITemplate'); |
| 580 |
|
- } |
| 581 |
|
- |
| 582 |
|
- $this->resources[$name] = array('class'=>$class, 'compiler'=>$compilerFactory); |
| 583 |
|
- } |
| 584 |
|
- |
| 585 |
|
- /** |
| 586 |
|
- * removes a custom resource |
| 587 |
|
- * |
| 588 |
|
- * @param string $name the resource name |
| 589 |
|
- */ |
| 590 |
|
- public function removeResource($name) |
| 591 |
|
- { |
| 592 |
|
- unset($this->resources[$name]); |
| 593 |
|
- if ($name==='file') { |
| 594 |
|
- $this->resources['file'] = array('class'=>'Dwoo_Template_File', 'compiler'=>null); |
| 595 |
|
- } |
| 596 |
|
- } |
| 597 |
|
- |
| 598 |
|
- /* |
| 599 |
|
- * --------- getters and setters --------- |
| 600 |
|
- */ |
| 601 |
|
- |
| 602 |
|
- /** |
| 603 |
|
- * sets the loader object to use to load plugins |
| 604 |
|
- * |
| 605 |
|
- * @param Dwoo_ILoader $loader loader object |
| 606 |
|
- */ |
| 607 |
|
- public function setLoader(Dwoo_ILoader $loader) |
| 608 |
|
- { |
| 609 |
|
- $this->loader = $loader; |
| 610 |
|
- } |
| 611 |
|
- |
| 612 |
|
- /** |
| 613 |
|
- * returns the current loader object or a default one if none is currently found |
| 614 |
|
- * |
| 615 |
|
- * @param Dwoo_ILoader |
| 616 |
|
- */ |
| 617 |
|
- public function getLoader() |
| 618 |
|
- { |
| 619 |
|
- if ($this->loader === null) { |
| 620 |
|
- $this->loader = new Dwoo_Loader($this->getCompileDir()); |
| 621 |
|
- } |
| 622 |
|
- |
| 623 |
|
- return $this->loader; |
| 624 |
|
- } |
| 625 |
|
- |
| 626 |
|
- /** |
| 627 |
|
- * returns the custom plugins loaded |
| 628 |
|
- * |
| 629 |
|
- * used by the Dwoo_ITemplate classes to pass the custom plugins to their Dwoo_ICompiler instance |
| 630 |
|
- * |
| 631 |
|
- * @return array |
| 632 |
|
- */ |
| 633 |
|
- public function getCustomPlugins() |
| 634 |
|
- { |
| 635 |
|
- return $this->plugins; |
| 636 |
|
- } |
| 637 |
|
- |
| 638 |
|
- /** |
| 639 |
|
- * returns the cache directory with a trailing DIRECTORY_SEPARATOR |
| 640 |
|
- * |
| 641 |
|
- * @return string |
| 642 |
|
- */ |
| 643 |
|
- public function getCacheDir() |
| 644 |
|
- { |
| 645 |
|
- if ($this->cacheDir === null) { |
| 646 |
|
- $this->setCacheDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR); |
| 647 |
|
- } |
| 648 |
|
- |
| 649 |
|
- return $this->cacheDir; |
| 650 |
|
- } |
| 651 |
|
- |
| 652 |
|
- /** |
| 653 |
|
- * sets the cache directory and automatically appends a DIRECTORY_SEPARATOR |
| 654 |
|
- * |
| 655 |
|
- * @param string $dir the cache directory |
| 656 |
|
- */ |
| 657 |
|
- public function setCacheDir($dir) |
| 658 |
|
- { |
| 659 |
|
- $this->cacheDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; |
| 660 |
|
- if (is_writable($this->cacheDir) === false) { |
| 661 |
|
- throw new Dwoo_Exception('The cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable'); |
| 662 |
|
- } |
| 663 |
|
- } |
| 664 |
|
- |
| 665 |
|
- /** |
| 666 |
|
- * returns the compile directory with a trailing DIRECTORY_SEPARATOR |
| 667 |
|
- * |
| 668 |
|
- * @return string |
| 669 |
|
- */ |
| 670 |
|
- public function getCompileDir() |
| 671 |
|
- { |
| 672 |
|
- if ($this->compileDir === null) { |
| 673 |
|
- $this->setCompileDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'compiled'.DIRECTORY_SEPARATOR); |
| 674 |
|
- } |
| 675 |
|
- |
| 676 |
|
- return $this->compileDir; |
| 677 |
|
- } |
| 678 |
|
- |
| 679 |
|
- /** |
| 680 |
|
- * sets the compile directory and automatically appends a DIRECTORY_SEPARATOR |
| 681 |
|
- * |
| 682 |
|
- * @param string $dir the compile directory |
| 683 |
|
- */ |
| 684 |
|
- public function setCompileDir($dir) |
| 685 |
|
- { |
| 686 |
|
- $this->compileDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; |
| 687 |
|
- if (is_writable($this->compileDir) === false) { |
| 688 |
|
- throw new Dwoo_Exception('The compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable'); |
| 689 |
|
- } |
| 690 |
|
- } |
| 691 |
|
- |
| 692 |
|
- /** |
| 693 |
|
- * returns the default cache time that is used with templates that do not have a cache time set |
| 694 |
|
- * |
| 695 |
|
- * @return int the duration in seconds |
| 696 |
|
- */ |
| 697 |
|
- public function getCacheTime() |
| 698 |
|
- { |
| 699 |
|
- return $this->cacheTime; |
| 700 |
|
- } |
| 701 |
|
- |
| 702 |
|
- /** |
| 703 |
|
- * sets the default cache time to use with templates that do not have a cache time set |
| 704 |
|
- * |
| 705 |
|
- * @param int $seconds the duration in seconds |
| 706 |
|
- */ |
| 707 |
|
- public function setCacheTime($seconds) |
| 708 |
|
- { |
| 709 |
|
- $this->cacheTime = (int) $seconds; |
| 710 |
|
- } |
| 711 |
|
- |
| 712 |
|
- /** |
| 713 |
|
- * returns the character set used by the string manipulation plugins |
| 714 |
|
- * |
| 715 |
|
- * the charset is automatically lowercased |
| 716 |
|
- * |
| 717 |
|
- * @return string |
| 718 |
|
- */ |
| 719 |
|
- public function getCharset() |
| 720 |
|
- { |
| 721 |
|
- return $this->charset; |
| 722 |
|
- } |
| 723 |
|
- |
| 724 |
|
- /** |
| 725 |
|
- * sets the character set used by the string manipulation plugins |
| 726 |
|
- * |
| 727 |
|
- * the charset will be automatically lowercased |
| 728 |
|
- * |
| 729 |
|
- * @param string $charset the character set |
| 730 |
|
- */ |
| 731 |
|
- public function setCharset($charset) |
| 732 |
|
- { |
| 733 |
|
- $this->charset = strtolower((string) $charset); |
| 734 |
|
- } |
| 735 |
|
- |
| 736 |
|
- /** |
| 737 |
|
- * returns the current template being rendered, when applicable, or null |
| 738 |
|
- * |
| 739 |
|
- * @return Dwoo_ITemplate|null |
| 740 |
|
- */ |
| 741 |
|
- public function getTemplate() |
| 742 |
|
- { |
| 743 |
|
- return $this->template; |
| 744 |
|
- } |
| 745 |
|
- |
| 746 |
|
- /** |
| 747 |
|
- * sets the current template being rendered |
| 748 |
|
- * |
| 749 |
|
- * @param Dwoo_ITemplate $tpl template object |
| 750 |
|
- */ |
| 751 |
|
- public function setTemplate(Dwoo_ITemplate $tpl) |
| 752 |
|
- { |
| 753 |
|
- $this->template = $tpl; |
| 754 |
|
- } |
| 755 |
|
- |
| 756 |
|
- /** |
| 757 |
|
- * sets the default compiler factory function for the given resource name |
| 758 |
|
- * |
| 759 |
|
- * a compiler factory must return a Dwoo_ICompiler object pre-configured to fit your needs |
| 760 |
|
- * |
| 761 |
|
- * @param string $resourceName the resource name (i.e. file, string) |
| 762 |
|
- * @param callback $compilerFactory the compiler factory callback |
| 763 |
|
- */ |
| 764 |
|
- public function setDefaultCompilerFactory($resourceName, $compilerFactory) |
| 765 |
|
- { |
| 766 |
|
- $this->resources[$resourceName]['compiler'] = $compilerFactory; |
| 767 |
|
- } |
| 768 |
|
- |
| 769 |
|
- /** |
| 770 |
|
- * returns the default compiler factory function for the given resource name |
| 771 |
|
- * |
| 772 |
|
- * @param string $resourceName the resource name |
| 773 |
|
- * @return callback the compiler factory callback |
| 774 |
|
- */ |
| 775 |
|
- public function getDefaultCompilerFactory($resourceName) |
| 776 |
|
- { |
| 777 |
|
- return $this->resources[$resourceName]['compiler']; |
| 778 |
|
- } |
| 779 |
|
- |
| 780 |
|
- /** |
| 781 |
|
- * sets the security policy object to enforce some php security settings |
| 782 |
|
- * |
| 783 |
|
- * use this if untrusted persons can modify templates |
| 784 |
|
- * |
| 785 |
|
- * @param Dwoo_Security_Policy $policy the security policy object |
| 786 |
|
- */ |
| 787 |
|
- public function setSecurityPolicy(Dwoo_Security_Policy $policy = null) |
| 788 |
|
- { |
| 789 |
|
- $this->securityPolicy = $policy; |
| 790 |
|
- } |
| 791 |
|
- |
| 792 |
|
- /** |
| 793 |
|
- * returns the current security policy object or null by default |
| 794 |
|
- * |
| 795 |
|
- * @return Dwoo_Security_Policy|null the security policy object if any |
| 796 |
|
- */ |
| 797 |
|
- public function getSecurityPolicy() |
| 798 |
|
- { |
| 799 |
|
- return $this->securityPolicy; |
| 800 |
|
- } |
| 801 |
|
- |
| 802 |
|
- /** |
| 803 |
|
- * sets the object that must be used as a plugin proxy when plugin can't be found |
| 804 |
|
- * by dwoo's loader |
| 805 |
|
- * |
| 806 |
|
- * @param Dwoo_IPluginProxy $pluginProxy the proxy object |
| 807 |
|
- */ |
| 808 |
|
- public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) { |
| 809 |
|
- $this->pluginProxy = $pluginProxy; |
| 810 |
|
- } |
| 811 |
|
- |
| 812 |
|
- /** |
| 813 |
|
- * returns the current plugin proxy object or null by default |
| 814 |
|
- * |
| 815 |
|
- * @param Dwoo_IPluginProxy|null the proxy object if any |
| 816 |
|
- */ |
| 817 |
|
- public function getPluginProxy() { |
| 818 |
|
- return $this->pluginProxy; |
| 819 |
|
- } |
| 820 |
|
- |
| 821 |
|
- /* |
| 822 |
|
- * --------- util functions --------- |
| 823 |
|
- */ |
| 824 |
|
- |
| 825 |
|
- /** |
| 826 |
|
- * [util function] checks whether the given template is cached or not |
| 827 |
|
- * |
| 828 |
|
- * @param Dwoo_ITemplate $tpl the template object |
| 829 |
|
- * @return bool |
| 830 |
|
- */ |
| 831 |
|
- public function isCached(Dwoo_ITemplate $tpl) |
| 832 |
|
- { |
| 833 |
|
- return is_string($tpl->getCachedTemplate($this)); |
| 834 |
|
- } |
| 835 |
|
- |
| 836 |
|
- /** |
| 837 |
|
- * [util function] clears the cached templates if they are older than the given time |
| 838 |
|
- * |
| 839 |
|
- * @param int $olderThan minimum time (in seconds) required for a cached template to be cleared |
| 840 |
|
- * @return int the amount of templates cleared |
| 841 |
|
- */ |
| 842 |
|
- public function clearCache($olderThan=-1) |
| 843 |
|
- { |
| 844 |
|
- $cacheDirs = new RecursiveDirectoryIterator($this->getCacheDir()); |
| 845 |
|
- $cache = new RecursiveIteratorIterator($cacheDirs); |
| 846 |
|
- $expired = time() - $olderThan; |
| 847 |
|
- $count = 0; |
| 848 |
|
- foreach ($cache as $file) { |
| 849 |
|
- if ($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html') { |
| 850 |
|
- continue; |
| 851 |
|
- } |
| 852 |
|
- if ($cache->getCTime() < $expired) { |
| 853 |
|
- $count += unlink((string) $file) ? 1 : 0; |
| 854 |
|
- } |
| 855 |
|
- } |
| 856 |
|
- return $count; |
| 857 |
|
- } |
| 858 |
|
- |
| 859 |
|
- /** |
| 860 |
|
- * [util function] fetches a template object of the given resource |
| 861 |
|
- * |
| 862 |
|
- * @param string $resourceName the resource name (i.e. file, string) |
| 863 |
|
- * @param string $resourceId the resource identifier (i.e. file path) |
| 864 |
|
- * @param int $cacheTime the cache time setting for this resource |
| 865 |
|
- * @param string $cacheId the unique cache identifier |
| 866 |
|
- * @param string $compileId the unique compiler identifier |
| 867 |
|
- * @return Dwoo_ITemplate |
| 868 |
|
- */ |
| 869 |
|
- public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) |
| 870 |
|
- { |
| 871 |
|
- if (isset($this->resources[$resourceName])) { |
| 872 |
|
- // TODO could be changed to $this->resources[$resourceName]['class']::templateFactory(..) in 5.3 maybe |
| 873 |
|
- return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate); |
| 874 |
|
- } else { |
| 875 |
|
- throw new Dwoo_Exception('Unknown resource type : '.$resourceName); |
| 876 |
|
- } |
| 877 |
|
- } |
| 878 |
|
- |
| 879 |
|
- /** |
| 880 |
|
- * [util function] checks if the input is an array or arrayaccess object, optionally it can also check if it's empty |
| 881 |
|
- * |
| 882 |
|
- * @param mixed $value the variable to check |
| 883 |
|
- * @param bool $checkIsEmpty if true, the function will also check if the array|arrayaccess is empty, |
| 884 |
|
- * and return true only if it's not empty |
| 885 |
|
- * @return int|bool true if it's an array|arrayaccess (or the item count if $checkIsEmpty is true) or false if it's not an array|arrayaccess (or 0 if $checkIsEmpty is true) |
| 886 |
|
- */ |
| 887 |
|
- public function isArray($value, $checkIsEmpty=false) |
| 888 |
|
- { |
| 889 |
|
- if (is_array($value) === true || $value instanceof ArrayAccess) { |
| 890 |
|
- if ($checkIsEmpty === false) { |
| 891 |
|
- return true; |
| 892 |
|
- } else { |
| 893 |
|
- return $this->count($value); |
| 894 |
|
- } |
| 895 |
|
- } |
| 896 |
|
- } |
| 897 |
|
- |
| 898 |
|
- /** |
| 899 |
|
- * [util function] checks if the input is an array or a traversable object, optionally it can also check if it's empty |
| 900 |
|
- * |
| 901 |
|
- * @param mixed $value the variable to check |
| 902 |
|
- * @param bool $checkIsEmpty if true, the function will also check if the array|traversable is empty, |
| 903 |
|
- * and return true only if it's not empty |
| 904 |
|
- * @return int|bool true if it's an array|traversable (or the item count if $checkIsEmpty is true) or false if it's not an array|traversable (or 0 if $checkIsEmpty is true) |
| 905 |
|
- */ |
| 906 |
|
- public function isTraversable($value, $checkIsEmpty=false) |
| 907 |
|
- { |
| 908 |
|
- if (is_array($value) === true) { |
| 909 |
|
- if ($checkIsEmpty === false) { |
| 910 |
|
- return true; |
| 911 |
|
- } else { |
| 912 |
|
- return count($value) > 0; |
| 913 |
|
- } |
| 914 |
|
- } elseif ($value instanceof Traversable) { |
| 915 |
|
- if ($checkIsEmpty === false) { |
| 916 |
|
- return true; |
| 917 |
|
- } else { |
| 918 |
|
- return $this->count($value); |
| 919 |
|
- } |
| 920 |
|
- } |
| 921 |
|
- return false; |
| 922 |
|
- } |
| 923 |
|
- |
| 924 |
|
- /** |
| 925 |
|
- * [util function] counts an array or arrayaccess/traversable object |
| 926 |
|
- * @param mixed $value |
| 927 |
|
- * @return int|bool the count for arrays and objects that implement countable, true for other objects that don't, and 0 for empty elements |
| 928 |
|
- */ |
| 929 |
|
- public function count($value) |
| 930 |
|
- { |
| 931 |
|
- if (is_array($value) === true || $value instanceof Countable) { |
| 932 |
|
- return count($value); |
| 933 |
|
- } elseif ($value instanceof ArrayAccess) { |
| 934 |
|
- if ($value->offsetExists(0)) { |
| 935 |
|
- return true; |
| 936 |
|
- } |
| 937 |
|
- } elseif ($value instanceof Iterator) { |
| 938 |
|
- $value->rewind(); |
| 939 |
|
- if ($value->valid()) { |
| 940 |
|
- return true; |
| 941 |
|
- } |
| 942 |
|
- } elseif ($value instanceof Traversable) { |
| 943 |
|
- foreach ($value as $dummy) { |
| 944 |
|
- return true; |
| 945 |
|
- } |
| 946 |
|
- } |
| 947 |
|
- return 0; |
| 948 |
|
- } |
| 949 |
|
- |
| 950 |
|
- /** |
| 951 |
|
- * [util function] triggers a dwoo error |
| 952 |
|
- * |
| 953 |
|
- * @param string $message the error message |
| 954 |
|
- * @param int $level the error level, one of the PHP's E_* constants |
| 955 |
|
- */ |
| 956 |
|
- public function triggerError($message, $level=E_USER_NOTICE) |
| 957 |
|
- { |
| 958 |
|
- if (!($tplIdentifier = $this->template->getResourceIdentifier())) { |
| 959 |
|
- $tplIdentifier = $this->template->getResourceName(); |
| 960 |
|
- } |
| 961 |
|
- trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level); |
| 962 |
|
- } |
| 963 |
|
- |
| 964 |
|
- /* |
| 965 |
|
- * --------- runtime functions --------- |
| 966 |
|
- */ |
| 967 |
|
- |
| 968 |
|
- /** |
| 969 |
|
- * [runtime function] adds a block to the block stack |
| 970 |
|
- * |
| 971 |
|
- * @param string $blockName the block name (without Dwoo_Plugin_ prefix) |
| 972 |
|
- * @param array $args the arguments to be passed to the block's init() function |
| 973 |
|
- * @return Dwoo_Block_Plugin the newly created block |
| 974 |
|
- */ |
| 975 |
|
- public function addStack($blockName, array $args=array()) |
| 976 |
|
- { |
| 977 |
|
- if (isset($this->plugins[$blockName])) { |
| 978 |
|
- $class = $this->plugins[$blockName]['class']; |
| 979 |
|
- } else { |
| 980 |
|
- $class = 'Dwoo_Plugin_'.$blockName; |
| 981 |
|
- } |
| 982 |
|
- |
| 983 |
|
- if ($this->curBlock !== null) { |
| 984 |
|
- $this->curBlock->buffer(ob_get_contents()); |
| 985 |
|
- ob_clean(); |
| 986 |
|
- } else { |
| 987 |
|
- $this->buffer .= ob_get_contents(); |
| 988 |
|
- ob_clean(); |
| 989 |
|
- } |
| 990 |
|
- |
| 991 |
|
- $block = new $class($this); |
| 992 |
|
- |
| 993 |
|
- $cnt = count($args); |
| 994 |
|
- if ($cnt===0) { |
| 995 |
|
- $block->init(); |
| 996 |
|
- } elseif ($cnt===1) { |
| 997 |
|
- $block->init($args[0]); |
| 998 |
|
- } elseif ($cnt===2) { |
| 999 |
|
- $block->init($args[0], $args[1]); |
| 1000 |
|
- } elseif ($cnt===3) { |
| 1001 |
|
- $block->init($args[0], $args[1], $args[2]); |
| 1002 |
|
- } elseif ($cnt===4) { |
| 1003 |
|
- $block->init($args[0], $args[1], $args[2], $args[3]); |
| 1004 |
|
- } else { |
| 1005 |
|
- call_user_func_array(array($block,'init'), $args); |
| 1006 |
|
- } |
| 1007 |
|
- |
| 1008 |
|
- $this->stack[] = $this->curBlock = $block; |
| 1009 |
|
- return $block; |
| 1010 |
|
- } |
| 1011 |
|
- |
| 1012 |
|
- /** |
| 1013 |
|
- * [runtime function] removes the plugin at the top of the block stack |
| 1014 |
|
- * |
| 1015 |
|
- * calls the block buffer() function, followed by a call to end() |
| 1016 |
|
- * and finally a call to process() |
| 1017 |
|
- */ |
| 1018 |
|
- public function delStack() |
| 1019 |
|
- { |
| 1020 |
|
- $args = func_get_args(); |
| 1021 |
|
- |
| 1022 |
|
- $this->curBlock->buffer(ob_get_contents()); |
| 1023 |
|
- ob_clean(); |
| 1024 |
|
- |
| 1025 |
|
- $cnt = count($args); |
| 1026 |
|
- if ($cnt===0) { |
| 1027 |
|
- $this->curBlock->end(); |
| 1028 |
|
- } elseif ($cnt===1) { |
| 1029 |
|
- $this->curBlock->end($args[0]); |
| 1030 |
|
- } elseif ($cnt===2) { |
| 1031 |
|
- $this->curBlock->end($args[0], $args[1]); |
| 1032 |
|
- } elseif ($cnt===3) { |
| 1033 |
|
- $this->curBlock->end($args[0], $args[1], $args[2]); |
| 1034 |
|
- } elseif ($cnt===4) { |
| 1035 |
|
- $this->curBlock->end($args[0], $args[1], $args[2], $args[3]); |
| 1036 |
|
- } else { |
| 1037 |
|
- call_user_func_array(array($this->curBlock, 'end'), $args); |
| 1038 |
|
- } |
| 1039 |
|
- |
| 1040 |
|
- $tmp = array_pop($this->stack); |
| 1041 |
|
- |
| 1042 |
|
- if (count($this->stack) > 0) { |
| 1043 |
|
- $this->curBlock = end($this->stack); |
| 1044 |
|
- $this->curBlock->buffer($tmp->process()); |
| 1045 |
|
- } else { |
| 1046 |
|
- $this->curBlock = null; |
| 1047 |
|
- echo $tmp->process(); |
| 1048 |
|
- } |
| 1049 |
|
- |
| 1050 |
|
- unset($tmp); |
| 1051 |
|
- } |
| 1052 |
|
- |
| 1053 |
|
- /** |
| 1054 |
|
- * [runtime function] returns the parent block of the given block |
| 1055 |
|
- * |
| 1056 |
|
- * @param Dwoo_Block_Plugin $block |
| 1057 |
|
- * @return Dwoo_Block_Plugin or false if the given block isn't in the stack |
| 1058 |
|
- */ |
| 1059 |
|
- public function getParentBlock(Dwoo_Block_Plugin $block) |
| 1060 |
|
- { |
| 1061 |
|
- $index = array_search($block, $this->stack, true); |
| 1062 |
|
- if ($index !== false && $index > 0) { |
| 1063 |
|
- return $this->stack[$index-1]; |
| 1064 |
|
- } |
| 1065 |
|
- return false; |
| 1066 |
|
- } |
| 1067 |
|
- |
| 1068 |
|
- /** |
| 1069 |
|
- * [runtime function] finds the closest block of the given type, starting at the top of the stack |
| 1070 |
|
- * |
| 1071 |
|
- * @param string $type the type of plugin you want to find |
| 1072 |
|
- * @return Dwoo_Block_Plugin or false if no plugin of such type is in the stack |
| 1073 |
|
- */ |
| 1074 |
|
- public function findBlock($type) |
| 1075 |
|
- { |
| 1076 |
|
- if (isset($this->plugins[$type])) { |
| 1077 |
|
- $type = $this->plugins[$type]['class']; |
| 1078 |
|
- } else { |
| 1079 |
|
- $type = 'Dwoo_Plugin_'.str_replace('Dwoo_Plugin_', '', $type); |
| 1080 |
|
- } |
| 1081 |
|
- |
| 1082 |
|
- $keys = array_keys($this->stack); |
| 1083 |
|
- while (($key = array_pop($keys)) !== false) { |
| 1084 |
|
- if ($this->stack[$key] instanceof $type) { |
| 1085 |
|
- return $this->stack[$key]; |
| 1086 |
|
- } |
| 1087 |
|
- } |
| 1088 |
|
- return false; |
| 1089 |
|
- } |
| 1090 |
|
- |
| 1091 |
|
- /** |
| 1092 |
|
- * [runtime function] returns a Dwoo_Plugin of the given class |
| 1093 |
|
- * |
| 1094 |
|
- * this is so a single instance of every class plugin is created at each template run, |
| 1095 |
|
- * allowing class plugins to have "per-template-run" static variables |
| 1096 |
|
- * |
| 1097 |
|
- * @private |
| 1098 |
|
- * @param string $class the class name |
| 1099 |
|
- * @return mixed an object of the given class |
| 1100 |
|
- */ |
| 1101 |
|
- public function getObjectPlugin($class) |
| 1102 |
|
- { |
| 1103 |
|
- if (isset($this->runtimePlugins[$class])) { |
| 1104 |
|
- return $this->runtimePlugins[$class]; |
| 1105 |
|
- } |
| 1106 |
|
- return $this->runtimePlugins[$class] = new $class($this); |
| 1107 |
|
- } |
| 1108 |
|
- |
| 1109 |
|
- /** |
| 1110 |
|
- * [runtime function] calls the process() method of the given class-plugin name |
| 1111 |
|
- * |
| 1112 |
|
- * @param string $plugName the class plugin name (without Dwoo_Plugin_ prefix) |
| 1113 |
|
- * @param array $params an array of parameters to send to the process() method |
| 1114 |
|
- * @return string the process() return value |
| 1115 |
|
- */ |
| 1116 |
|
- public function classCall($plugName, array $params = array()) |
| 1117 |
|
- { |
| 1118 |
|
- $class = 'Dwoo_Plugin_'.$plugName; |
| 1119 |
|
- |
| 1120 |
|
- $plugin = $this->getObjectPlugin($class); |
| 1121 |
|
- |
| 1122 |
|
- $cnt = count($params); |
| 1123 |
|
- if ($cnt===0) { |
| 1124 |
|
- return $plugin->process(); |
| 1125 |
|
- } elseif ($cnt===1) { |
| 1126 |
|
- return $plugin->process($params[0]); |
| 1127 |
|
- } elseif ($cnt===2) { |
| 1128 |
|
- return $plugin->process($params[0], $params[1]); |
| 1129 |
|
- } elseif ($cnt===3) { |
| 1130 |
|
- return $plugin->process($params[0], $params[1], $params[2]); |
| 1131 |
|
- } elseif ($cnt===4) { |
| 1132 |
|
- return $plugin->process($params[0], $params[1], $params[2], $params[3]); |
| 1133 |
|
- } else { |
| 1134 |
|
- return call_user_func_array(array($plugin, 'process'), $params); |
| 1135 |
|
- } |
| 1136 |
|
- } |
| 1137 |
|
- |
| 1138 |
|
- /** |
| 1139 |
|
- * [runtime function] calls a php function |
| 1140 |
|
- * |
| 1141 |
|
- * @param string $callback the function to call |
| 1142 |
|
- * @param array $params an array of parameters to send to the function |
| 1143 |
|
- * @return mixed the return value of the called function |
| 1144 |
|
- */ |
| 1145 |
|
- public function arrayMap($callback, array $params) |
| 1146 |
|
- { |
| 1147 |
|
- if ($params[0] === $this) { |
| 1148 |
|
- $addThis = true; |
| 1149 |
|
- array_shift($params); |
| 1150 |
|
- } |
| 1151 |
|
- if ((is_array($params[0]) || ($params[0] instanceof Iterator && $params[0] instanceof ArrayAccess))) { |
| 1152 |
|
- if (empty($params[0])) { |
| 1153 |
|
- return $params[0]; |
| 1154 |
|
- } |
| 1155 |
|
- |
| 1156 |
|
- // array map |
| 1157 |
|
- $out = array(); |
| 1158 |
|
- $cnt = count($params); |
| 1159 |
|
- |
| 1160 |
|
- if (isset($addThis)) { |
| 1161 |
|
- array_unshift($params, $this); |
| 1162 |
|
- $items = $params[1]; |
| 1163 |
|
- $keys = array_keys($items); |
| 1164 |
|
- |
| 1165 |
|
- if (is_string($callback) === false) { |
| 1166 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1167 |
|
- $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); |
| 1168 |
|
- } |
| 1169 |
|
- } elseif ($cnt===1) { |
| 1170 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1171 |
|
- $out[] = $callback($this, $items[$i]); |
| 1172 |
|
- } |
| 1173 |
|
- } elseif ($cnt===2) { |
| 1174 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1175 |
|
- $out[] = $callback($this, $items[$i], $params[2]); |
| 1176 |
|
- } |
| 1177 |
|
- } elseif ($cnt===3) { |
| 1178 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1179 |
|
- $out[] = $callback($this, $items[$i], $params[2], $params[3]); |
| 1180 |
|
- } |
| 1181 |
|
- } else { |
| 1182 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1183 |
|
- $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); |
| 1184 |
|
- } |
| 1185 |
|
- } |
| 1186 |
|
- } else { |
| 1187 |
|
- $items = $params[0]; |
| 1188 |
|
- $keys = array_keys($items); |
| 1189 |
|
- |
| 1190 |
|
- if (is_string($callback) === false) { |
| 1191 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1192 |
|
- $out[] = call_user_func_array($callback, array($items[$i]) + $params); |
| 1193 |
|
- } |
| 1194 |
|
- } elseif ($cnt===1) { |
| 1195 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1196 |
|
- $out[] = $callback($items[$i]); |
| 1197 |
|
- } |
| 1198 |
|
- } elseif ($cnt===2) { |
| 1199 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1200 |
|
- $out[] = $callback($items[$i], $params[1]); |
| 1201 |
|
- } |
| 1202 |
|
- } elseif ($cnt===3) { |
| 1203 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1204 |
|
- $out[] = $callback($items[$i], $params[1], $params[2]); |
| 1205 |
|
- } |
| 1206 |
|
- } elseif ($cnt===4) { |
| 1207 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1208 |
|
- $out[] = $callback($items[$i], $params[1], $params[2], $params[3]); |
| 1209 |
|
- } |
| 1210 |
|
- } else { |
| 1211 |
|
- while (($i = array_shift($keys)) !== null) { |
| 1212 |
|
- $out[] = call_user_func_array($callback, array($items[$i]) + $params); |
| 1213 |
|
- } |
| 1214 |
|
- } |
| 1215 |
|
- } |
| 1216 |
|
- return $out; |
| 1217 |
|
- } else { |
| 1218 |
|
- return $params[0]; |
| 1219 |
|
- } |
| 1220 |
|
- } |
| 1221 |
|
- |
| 1222 |
|
- /** |
| 1223 |
|
- * [runtime function] reads a variable into the given data array |
| 1224 |
|
- * |
| 1225 |
|
- * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property") |
| 1226 |
|
- * @param mixed $data the data array or object to read from |
| 1227 |
|
- * @param bool $safeRead if true, the function will check whether the index exists to prevent any notices from being output |
| 1228 |
|
- * @return mixed |
| 1229 |
|
- */ |
| 1230 |
|
- public function readVarInto($varstr, $data, $safeRead = false) |
| 1231 |
|
- { |
| 1232 |
|
- if ($data === null) { |
| 1233 |
|
- return null; |
| 1234 |
|
- } |
| 1235 |
|
- |
| 1236 |
|
- if (is_array($varstr) === false) { |
| 1237 |
|
- preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); |
| 1238 |
|
- } else { |
| 1239 |
|
- $m = $varstr; |
| 1240 |
|
- } |
| 1241 |
|
- unset($varstr); |
| 1242 |
|
- |
| 1243 |
|
- while (list($k, $sep) = each($m[1])) { |
| 1244 |
|
- if ($sep === '.' || $sep === '[' || $sep === '') { |
| 1245 |
|
- if ((is_array($data) || $data instanceof ArrayAccess) && ($safeRead === false || isset($data[$m[2][$k]]))) { |
| 1246 |
|
- $data = $data[$m[2][$k]]; |
| 1247 |
|
- } else { |
| 1248 |
|
- return null; |
| 1249 |
|
- } |
| 1250 |
|
- } else { |
| 1251 |
|
- if (is_object($data) && ($safeRead === false || isset($data->$m[2][$k]) || is_callable(array($data, '__get')))) { |
| 1252 |
|
- $data = $data->$m[2][$k]; |
| 1253 |
|
- } else { |
| 1254 |
|
- return null; |
| 1255 |
|
- } |
| 1256 |
|
- } |
| 1257 |
|
- } |
| 1258 |
|
- |
| 1259 |
|
- return $data; |
| 1260 |
|
- } |
| 1261 |
|
- |
| 1262 |
|
- /** |
| 1263 |
|
- * [runtime function] reads a variable into the parent scope |
| 1264 |
|
- * |
| 1265 |
|
- * @param int $parentLevels the amount of parent levels to go from the current scope |
| 1266 |
|
- * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property") |
| 1267 |
|
- * @return mixed |
| 1268 |
|
- */ |
| 1269 |
|
- public function readParentVar($parentLevels, $varstr = null) |
| 1270 |
|
- { |
| 1271 |
|
- $tree = $this->scopeTree; |
| 1272 |
|
- $cur = $this->data; |
| 1273 |
|
- |
| 1274 |
|
- while ($parentLevels--!==0) { |
| 1275 |
|
- array_pop($tree); |
| 1276 |
|
- } |
| 1277 |
|
- |
| 1278 |
|
- while (($i = array_shift($tree)) !== null) { |
| 1279 |
|
- if (is_object($cur)) { |
| 1280 |
|
- $cur = $cur->$i; |
| 1281 |
|
- } else { |
| 1282 |
|
- $cur = $cur[$i]; |
| 1283 |
|
- } |
| 1284 |
|
- } |
| 1285 |
|
- |
| 1286 |
|
- if ($varstr!==null) { |
| 1287 |
|
- return $this->readVarInto($varstr, $cur); |
| 1288 |
|
- } else { |
| 1289 |
|
- return $cur; |
| 1290 |
|
- } |
| 1291 |
|
- } |
| 1292 |
|
- |
| 1293 |
|
- /** |
| 1294 |
|
- * [runtime function] reads a variable into the current scope |
| 1295 |
|
- * |
| 1296 |
|
- * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property") |
| 1297 |
|
- * @return mixed |
| 1298 |
|
- */ |
| 1299 |
|
- public function readVar($varstr) |
| 1300 |
|
- { |
| 1301 |
|
- if (is_array($varstr)===true) { |
| 1302 |
|
- $m = $varstr; |
| 1303 |
|
- unset($varstr); |
| 1304 |
|
- } else { |
| 1305 |
|
- if (strstr($varstr, '.') === false && strstr($varstr, '[') === false && strstr($varstr, '->') === false) { |
| 1306 |
|
- if ($varstr === 'dwoo') { |
| 1307 |
|
- return $this->globals; |
| 1308 |
|
- } elseif ($varstr === '__' || $varstr === '_root' ) { |
| 1309 |
|
- return $this->data; |
| 1310 |
|
- $varstr = substr($varstr, 6); |
| 1311 |
|
- } elseif ($varstr === '_' || $varstr === '_parent') { |
| 1312 |
|
- $varstr = '.'.$varstr; |
| 1313 |
|
- $tree = $this->scopeTree; |
| 1314 |
|
- $cur = $this->data; |
| 1315 |
|
- array_pop($tree); |
| 1316 |
|
- |
| 1317 |
|
- while (($i = array_shift($tree)) !== null) { |
| 1318 |
|
- if (is_object($cur)) { |
| 1319 |
|
- $cur = $cur->$i; |
| 1320 |
|
- } else { |
| 1321 |
|
- $cur = $cur[$i]; |
| 1322 |
|
- } |
| 1323 |
|
- } |
| 1324 |
|
- |
| 1325 |
|
- return $cur; |
| 1326 |
|
- } |
| 1327 |
|
- |
| 1328 |
|
- $cur = $this->scope; |
| 1329 |
|
- |
| 1330 |
|
- if (isset($cur[$varstr])) { |
| 1331 |
|
- return $cur[$varstr]; |
| 1332 |
|
- } else { |
| 1333 |
|
- return null; |
| 1334 |
|
- } |
| 1335 |
|
- } |
| 1336 |
|
- |
| 1337 |
|
- if (substr($varstr, 0, 1) === '.') { |
| 1338 |
|
- $varstr = 'dwoo'.$varstr; |
| 1339 |
|
- } |
| 1340 |
|
- |
| 1341 |
|
- preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); |
| 1342 |
|
- } |
| 1343 |
|
- |
| 1344 |
|
- $i = $m[2][0]; |
| 1345 |
|
- if ($i === 'dwoo') { |
| 1346 |
|
- $cur = $this->globals; |
| 1347 |
|
- array_shift($m[2]); |
| 1348 |
|
- array_shift($m[1]); |
| 1349 |
|
- switch ($m[2][0]) { |
| 1350 |
|
- |
| 1351 |
|
- case 'get': |
| 1352 |
|
- $cur = $_GET; |
| 1353 |
|
- break; |
| 1354 |
|
- case 'post': |
| 1355 |
|
- $cur = $_POST; |
| 1356 |
|
- break; |
| 1357 |
|
- case 'session': |
| 1358 |
|
- $cur = $_SESSION; |
| 1359 |
|
- break; |
| 1360 |
|
- case 'cookies': |
| 1361 |
|
- case 'cookie': |
| 1362 |
|
- $cur = $_COOKIE; |
| 1363 |
|
- break; |
| 1364 |
|
- case 'server': |
| 1365 |
|
- $cur = $_SERVER; |
| 1366 |
|
- break; |
| 1367 |
|
- case 'env': |
| 1368 |
|
- $cur = $_ENV; |
| 1369 |
|
- break; |
| 1370 |
|
- case 'request': |
| 1371 |
|
- $cur = $_REQUEST; |
| 1372 |
|
- break; |
| 1373 |
|
- case 'const': |
| 1374 |
|
- array_shift($m[2]); |
| 1375 |
|
- if (defined($m[2][0])) { |
| 1376 |
|
- return constant($m[2][0]); |
| 1377 |
|
- } else { |
| 1378 |
|
- return null; |
| 1379 |
|
- } |
| 1380 |
|
- |
| 1381 |
|
- } |
| 1382 |
|
- if ($cur !== $this->globals) { |
| 1383 |
|
- array_shift($m[2]); |
| 1384 |
|
- array_shift($m[1]); |
| 1385 |
|
- } |
| 1386 |
|
- } elseif ($i === '__' || $i === '_root') { |
| 1387 |
|
- $cur = $this->data; |
| 1388 |
|
- array_shift($m[2]); |
| 1389 |
|
- array_shift($m[1]); |
| 1390 |
|
- } elseif ($i === '_' || $i === '_parent') { |
| 1391 |
|
- $tree = $this->scopeTree; |
| 1392 |
|
- $cur = $this->data; |
| 1393 |
|
- |
| 1394 |
|
- while (true) { |
| 1395 |
|
- array_pop($tree); |
| 1396 |
|
- array_shift($m[2]); |
| 1397 |
|
- array_shift($m[1]); |
| 1398 |
|
- if (current($m[2]) === '_' || current($m[2]) === '_parent') { |
| 1399 |
|
- continue; |
| 1400 |
|
- } |
| 1401 |
|
- |
| 1402 |
|
- while (($i = array_shift($tree)) !== null) { |
| 1403 |
|
- if (is_object($cur)) { |
| 1404 |
|
- $cur = $cur->$i; |
| 1405 |
|
- } else { |
| 1406 |
|
- $cur = $cur[$i]; |
| 1407 |
|
- } |
| 1408 |
|
- } |
| 1409 |
|
- break; |
| 1410 |
|
- } |
| 1411 |
|
- } else { |
| 1412 |
|
- $cur = $this->scope; |
| 1413 |
|
- } |
| 1414 |
|
- |
| 1415 |
|
- while (list($k, $sep) = each($m[1])) { |
| 1416 |
|
- if ($sep === '.' || $sep === '[' || $sep === '') { |
| 1417 |
|
- if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) { |
| 1418 |
|
- $cur = $cur[$m[2][$k]]; |
| 1419 |
|
- } else { |
| 1420 |
|
- return null; |
| 1421 |
|
- } |
| 1422 |
|
- } elseif ($sep === '->') { |
| 1423 |
|
- if (is_object($cur)) { |
| 1424 |
|
- $cur = $cur->$m[2][$k]; |
| 1425 |
|
- } else { |
| 1426 |
|
- return null; |
| 1427 |
|
- } |
| 1428 |
|
- } else { |
| 1429 |
|
- return null; |
| 1430 |
|
- } |
| 1431 |
|
- } |
| 1432 |
|
- |
| 1433 |
|
- return $cur; |
| 1434 |
|
- } |
| 1435 |
|
- |
| 1436 |
|
- /** |
| 1437 |
|
- * [runtime function] assign the value to the given variable |
| 1438 |
|
- * |
| 1439 |
|
- * @param mixed $value the value to assign |
| 1440 |
|
- * @param string $scope the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property") |
| 1441 |
|
- * @return bool true if assigned correctly or false if a problem occured while parsing the var string |
| 1442 |
|
- */ |
| 1443 |
|
- public function assignInScope($value, $scope) |
| 1444 |
|
- { |
| 1445 |
|
- $tree =& $this->scopeTree; |
| 1446 |
|
- $data =& $this->data; |
| 1447 |
|
- |
| 1448 |
|
- if (!is_string($scope)) { |
| 1449 |
|
- return $this->triggerError('Assignments must be done into strings, ('.gettype($scope).') '.var_export($scope, true).' given', E_USER_ERROR); |
| 1450 |
|
- } |
| 1451 |
|
- if (strstr($scope, '.') === false && strstr($scope, '->') === false) { |
| 1452 |
|
- $this->scope[$scope] = $value; |
| 1453 |
|
- } else { |
| 1454 |
|
- // TODO handle _root/_parent scopes ? |
| 1455 |
|
- preg_match_all('#(\[|->|\.)?([^.[\]-]+)\]?#i', $scope, $m); |
| 1456 |
|
- |
| 1457 |
|
- $cur =& $this->scope; |
| 1458 |
|
- $last = array(array_pop($m[1]), array_pop($m[2])); |
| 1459 |
|
- |
| 1460 |
|
- while (list($k, $sep) = each($m[1])) { |
| 1461 |
|
- if ($sep === '.' || $sep === '[' || $sep === '') { |
| 1462 |
|
- if (is_array($cur) === false) { |
| 1463 |
|
- $cur = array(); |
| 1464 |
|
- } |
| 1465 |
|
- $cur =& $cur[$m[2][$k]]; |
| 1466 |
|
- } elseif ($sep === '->') { |
| 1467 |
|
- if (is_object($cur) === false) { |
| 1468 |
|
- $cur = new stdClass; |
| 1469 |
|
- } |
| 1470 |
|
- $cur =& $cur->$m[2][$k]; |
| 1471 |
|
- } else { |
| 1472 |
|
- return false; |
| 1473 |
|
- } |
| 1474 |
|
- } |
| 1475 |
|
- |
| 1476 |
|
- if ($last[0] === '.' || $last[0] === '[' || $last[0] === '') { |
| 1477 |
|
- if (is_array($cur) === false) { |
| 1478 |
|
- $cur = array(); |
| 1479 |
|
- } |
| 1480 |
|
- $cur[$last[1]] = $value; |
| 1481 |
|
- } elseif ($last[0] === '->') { |
| 1482 |
|
- if (is_object($cur) === false) { |
| 1483 |
|
- $cur = new stdClass; |
| 1484 |
|
- } |
| 1485 |
|
- $cur->$last[1] = $value; |
| 1486 |
|
- } else { |
| 1487 |
|
- return false; |
| 1488 |
|
- } |
| 1489 |
|
- } |
| 1490 |
|
- } |
| 1491 |
|
- |
| 1492 |
|
- /** |
| 1493 |
|
- * [runtime function] sets the scope to the given scope string or array |
| 1494 |
|
- * |
| 1495 |
|
- * @param mixed $scope a string i.e. "level1.level2" or an array i.e. array("level1", "level2") |
| 1496 |
|
- * @param bool $absolute if true, the scope is set from the top level scope and not from the current scope |
| 1497 |
|
- * @return array the current scope tree |
| 1498 |
|
- */ |
| 1499 |
|
- public function setScope($scope, $absolute = false) |
| 1500 |
|
- { |
| 1501 |
|
- $old = $this->scopeTree; |
| 1502 |
|
- |
| 1503 |
|
- if (is_string($scope)===true) { |
| 1504 |
|
- $scope = explode('.', $scope); |
| 1505 |
|
- } |
| 1506 |
|
- |
| 1507 |
|
- if ($absolute===true) { |
| 1508 |
|
- $this->scope =& $this->data; |
| 1509 |
|
- $this->scopeTree = array(); |
| 1510 |
|
- } |
| 1511 |
|
- |
| 1512 |
|
- while (($bit = array_shift($scope)) !== null) { |
| 1513 |
|
- if ($bit === '_' || $bit === '_parent') { |
| 1514 |
|
- array_pop($this->scopeTree); |
| 1515 |
|
- $this->scope =& $this->data; |
| 1516 |
|
- $cnt = count($this->scopeTree); |
| 1517 |
|
- for ($i=0;$i<$cnt;$i++) |
| 1518 |
|
- $this->scope =& $this->scope[$this->scopeTree[$i]]; |
| 1519 |
|
- } elseif ($bit === '__' || $bit === '_root') { |
| 1520 |
|
- $this->scope =& $this->data; |
| 1521 |
|
- $this->scopeTree = array(); |
| 1522 |
|
- } elseif (isset($this->scope[$bit])) { |
| 1523 |
|
- $this->scope =& $this->scope[$bit]; |
| 1524 |
|
- $this->scopeTree[] = $bit; |
| 1525 |
|
- } else { |
| 1526 |
|
- unset($this->scope); |
| 1527 |
|
- $this->scope = null; |
| 1528 |
|
- } |
| 1529 |
|
- } |
| 1530 |
|
- |
| 1531 |
|
- return $old; |
| 1532 |
|
- } |
| 1533 |
|
- |
| 1534 |
|
- /** |
| 1535 |
|
- * [runtime function] returns the entire data array |
| 1536 |
|
- * |
| 1537 |
|
- * @return array |
| 1538 |
|
- */ |
| 1539 |
|
- public function getData() |
| 1540 |
|
- { |
| 1541 |
|
- return $this->data; |
| 1542 |
|
- } |
| 1543 |
|
- |
| 1544 |
|
- /** |
| 1545 |
|
- * [runtime function] returns a reference to the current scope |
| 1546 |
|
- * |
| 1547 |
|
- * @return &mixed |
| 1548 |
|
- */ |
| 1549 |
|
- public function &getScope() |
| 1550 |
|
- { |
| 1551 |
|
- return $this->scope; |
| 1552 |
|
- } |
| 1553 |
|
- |
| 1554 |
|
- /** |
| 1555 |
|
- * Redirects all calls to unexisting to plugin proxy. |
| 1556 |
|
- * |
| 1557 |
|
- * @param string Method name |
| 1558 |
|
- * @param array List of arguments |
| 1559 |
|
- * @return mixed |
| 1560 |
|
- */ |
| 1561 |
|
- public function __call($method, $args) { |
| 1562 |
|
- $proxy = $this->getPluginProxy(); |
| 1563 |
|
- if (!$proxy) { |
| 1564 |
|
- throw new Dwoo_Exception('Call to undefined method '.__CLASS__.'::'.$method.'()'); |
| 1565 |
|
- } |
| 1566 |
|
- return call_user_func_array($proxy->getCallback($method), $args); |
| 1567 |
|
- } |
| 1568 |
|
-} |
|
21 |
+} |
|
2 |
+<?php |
|
3 |
+ |
|
4 |
+define('DWOO_DIRECTORY', dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR); |
|
5 |
+ |
|
6 |
+/** |
|
7 |
+ * main dwoo class, allows communication between the compiler, template and data classes |
|
8 |
+ * |
|
9 |
+ * <pre> |
|
10 |
+ * requirements : |
|
11 |
+ * php 5.2.0 or above (might work below, it's a rough estimate) |
|
12 |
+ * SPL and PCRE extensions (for php versions prior to 5.3.0) |
|
13 |
+ * mbstring extension for some string manipulation plugins (especially if you intend to use UTF-8) |
|
14 |
+ * recommended : |
|
15 |
+ * hash extension (for Dwoo_Template_String - minor performance boost) |
|
16 |
+ * |
|
17 |
+ * project created : |
|
18 |
+ * 2008-01-05 |
|
19 |
+ * </pre> |
|
20 |
+ * |
|
21 |
+ * This software is provided 'as-is', without any express or implied warranty. |
|
22 |
+ * In no event will the authors be held liable for any damages arising from the use of this software. |
|
23 |
+ * |
|
24 |
+ * @author Jordi Boggiano <j.boggiano@seld.be> |
|
25 |
+ * @copyright Copyright (c) 2008, Jordi Boggiano |
|
26 |
+ * @license http://dwoo.org/LICENSE Modified BSD License |
|
27 |
+ * @link http://dwoo.org/ |
|
28 |
+ * @version 1.1.0 |
|
29 |
+ * @date 2009-07-18 |
|
30 |
+ * @package Dwoo |
|
31 |
+ */ |
|
32 |
+class Dwoo_Core |
|
33 |
+{ |
|
34 |
+ /** |
|
35 |
+ * current version number |
|
36 |
+ * |
|
37 |
+ * @var string |
|
38 |
+ */ |
|
39 |
+ const VERSION = '1.1.1'; |
|
40 |
+ |
|
41 |
+ /** |
|
42 |
+ * unique number of this dwoo release |
|
43 |
+ * |
|
44 |
+ * this can be used by templates classes to check whether the compiled template |
|
45 |
+ * has been compiled before this release or not, so that old templates are |
|
46 |
+ * recompiled automatically when Dwoo is updated |
|
47 |
+ */ |
|
48 |
+ const RELEASE_TAG = 17; |
|
49 |
+ |
|
50 |
+ /**#@+ |
|
51 |
+ * constants that represents all plugin types |
|
52 |
+ * |
|
53 |
+ * these are bitwise-operation-safe values to allow multiple types |
|
54 |
+ * on a single plugin |
|
55 |
+ * |
|
56 |
+ * @var int |
|
57 |
+ */ |
|
58 |
+ const CLASS_PLUGIN = 1; |
|
59 |
+ const FUNC_PLUGIN = 2; |
|
60 |
+ const NATIVE_PLUGIN = 4; |
|
61 |
+ const BLOCK_PLUGIN = 8; |
|
62 |
+ const COMPILABLE_PLUGIN = 16; |
|
63 |
+ const CUSTOM_PLUGIN = 32; |
|
64 |
+ const SMARTY_MODIFIER = 64; |
|
65 |
+ const SMARTY_BLOCK = 128; |
|
66 |
+ const SMARTY_FUNCTION = 256; |
|
67 |
+ const PROXY_PLUGIN = 512; |
|
68 |
+ const TEMPLATE_PLUGIN = 1024; |
|
69 |
+ /**#@-*/ |
|
70 |
+ |
|
71 |
+ /** |
|
72 |
+ * character set of the template, used by string manipulation plugins |
|
73 |
+ * |
|
74 |
+ * it must be lowercase, but setCharset() will take care of that |
|
75 |
+ * |
|
76 |
+ * @see setCharset |
|
77 |
+ * @see getCharset |
|
78 |
+ * @var string |
|
79 |
+ */ |
|
80 |
+ protected $charset = 'utf-8'; |
|
81 |
+ |
|
82 |
+ /** |
|
83 |
+ * global variables that are accessible through $dwoo.* in the templates |
|
84 |
+ * |
|
85 |
+ * default values include: |
|
86 |
+ * |
|
87 |
+ * $dwoo.version - current version number |
|
88 |
+ * $dwoo.ad - a Powered by Dwoo link pointing to dwoo.org |
|
89 |
+ * $dwoo.now - the current time |
|
90 |
+ * $dwoo.template - the current template filename |
|
91 |
+ * $dwoo.charset - the character set used by the template |
|
92 |
+ * |
|
93 |
+ * on top of that, foreach and other plugins can store special values in there, |
|
94 |
+ * see their documentation for more details. |
|
95 |
+ * |
|
96 |
+ * @private |
|
97 |
+ * @var array |
|
98 |
+ */ |
|
99 |
+ public $globals; |
|
100 |
+ |
|
101 |
+ /** |
|
102 |
+ * directory where the compiled templates are stored |
|
103 |
+ * |
|
104 |
+ * defaults to DWOO_COMPILEDIR (= dwoo_dir/compiled by default) |
|
105 |
+ * |
|
106 |
+ * @var string |
|
107 |
+ */ |
|
108 |
+ protected $compileDir; |
|
109 |
+ |
|
110 |
+ /** |
|
111 |
+ * directory where the cached templates are stored |
|
112 |
+ * |
|
113 |
+ * defaults to DWOO_CACHEDIR (= dwoo_dir/cache by default) |
|
114 |
+ * |
|
115 |
+ * @var string |
|
116 |
+ */ |
|
117 |
+ protected $cacheDir; |
|
118 |
+ |
|
119 |
+ /** |
|
120 |
+ * defines how long (in seconds) the cached files must remain valid |
|
121 |
+ * |
|
122 |
+ * can be overriden on a per-template basis |
|
123 |
+ * |
|
124 |
+ * -1 = never delete |
|
125 |
+ * 0 = disabled |
|
126 |
+ * >0 = duration in seconds |
|
127 |
+ * |
|
128 |
+ * @var int |
|
129 |
+ */ |
|
130 |
+ protected $cacheTime = 0; |
|
131 |
+ |
|
132 |
+ /** |
|
133 |
+ * security policy object |
|
134 |
+ * |
|
135 |
+ * @var Dwoo_Security_Policy |
|
136 |
+ */ |
|
137 |
+ protected $securityPolicy = null; |
|
138 |
+ |
|
139 |
+ /** |
|
140 |
+ * stores the custom plugins callbacks |
|
141 |
+ * |
|
142 |
+ * @see addPlugin |
|
143 |
+ * @see removePlugin |
|
144 |
+ * @var array |
|
145 |
+ */ |
|
146 |
+ protected $plugins = array(); |
|
147 |
+ |
|
148 |
+ /** |
|
149 |
+ * stores the filter callbacks |
|
150 |
+ * |
|
151 |
+ * @see addFilter |
|
152 |
+ * @see removeFilter |
|
153 |
+ * @var array |
|
154 |
+ */ |
|
155 |
+ protected $filters = array(); |
|
156 |
+ |
|
157 |
+ /** |
|
158 |
+ * stores the resource types and associated |
|
159 |
+ * classes / compiler classes |
|
160 |
+ * |
|
161 |
+ * @var array |
|
162 |
+ */ |
|
163 |
+ protected $resources = array |
|
164 |
+ ( |
|
165 |
+ 'file' => array |
|
166 |
+ ( |
|
167 |
+ 'class' => 'Dwoo_Template_File', |
|
168 |
+ 'compiler' => null |
|
169 |
+ ), |
|
170 |
+ 'string' => array |
|
171 |
+ ( |
|
172 |
+ 'class' => 'Dwoo_Template_String', |
|
173 |
+ 'compiler' => null |
|
174 |
+ ) |
|
175 |
+ ); |
|
176 |
+ |
|
177 |
+ /** |
|
178 |
+ * the dwoo loader object used to load plugins by this dwoo instance |
|
179 |
+ * |
|
180 |
+ * @var Dwoo_ILoader |
|
181 |
+ */ |
|
182 |
+ protected $loader = null; |
|
183 |
+ |
|
184 |
+ /** |
|
185 |
+ * currently rendered template, set to null when not-rendering |
|
186 |
+ * |
|
187 |
+ * @var Dwoo_ITemplate |
|
188 |
+ */ |
|
189 |
+ protected $template = null; |
|
190 |
+ |
|
191 |
+ /** |
|
192 |
+ * stores the instances of the class plugins during template runtime |
|
193 |
+ * |
|
194 |
+ * @var array |
|
195 |
+ */ |
|
196 |
+ protected $runtimePlugins; |
|
197 |
+ |
|
198 |
+ /** |
|
199 |
+ * stores the data during template runtime |
|
200 |
+ * |
|
201 |
+ * @var array |
|
202 |
+ * @private |
|
203 |
+ */ |
|
204 |
+ public $data; |
|
205 |
+ |
|
206 |
+ /** |
|
207 |
+ * stores the current scope during template runtime |
|
208 |
+ * |
|
209 |
+ * this should ideally not be accessed directly from outside template code |
|
210 |
+ * |
|
211 |
+ * @var mixed |
|
212 |
+ * @private |
|
213 |
+ */ |
|
214 |
+ public $scope; |
|
215 |
+ |
|
216 |
+ /** |
|
217 |
+ * stores the scope tree during template runtime |
|
218 |
+ * |
|
219 |
+ * @var array |
|
220 |
+ */ |
|
221 |
+ protected $scopeTree; |
|
222 |
+ |
|
223 |
+ /** |
|
224 |
+ * stores the block plugins stack during template runtime |
|
225 |
+ * |
|
226 |
+ * @var array |
|
227 |
+ */ |
|
228 |
+ protected $stack; |
|
229 |
+ |
|
230 |
+ /** |
|
231 |
+ * stores the current block plugin at the top of the stack during template runtime |
|
232 |
+ * |
|
233 |
+ * @var Dwoo_Block_Plugin |
|
234 |
+ */ |
|
235 |
+ protected $curBlock; |
|
236 |
+ |
|
237 |
+ /** |
|
238 |
+ * stores the output buffer during template runtime |
|
239 |
+ * |
|
240 |
+ * @var string |
|
241 |
+ */ |
|
242 |
+ protected $buffer; |
|
243 |
+ |
|
244 |
+ /** |
|
245 |
+ * stores plugin proxy |
|
246 |
+ * |
|
247 |
+ * @var Dwoo_IPluginProxy |
|
248 |
+ */ |
|
249 |
+ protected $pluginProxy; |
|
250 |
+ |
|
251 |
+ /** |
|
252 |
+ * constructor, sets the cache and compile dir to the default values if not provided |
|
253 |
+ * |
|
254 |
+ * @param string $compileDir path to the compiled directory, defaults to lib/compiled |
|
255 |
+ * @param string $cacheDir path to the cache directory, defaults to lib/cache |
|
256 |
+ */ |
|
257 |
+ public function __construct($compileDir = null, $cacheDir = null) |
|
258 |
+ { |
|
259 |
+ if ($compileDir !== null) { |
|
260 |
+ $this->setCompileDir($compileDir); |
|
261 |
+ } |
|
262 |
+ if ($cacheDir !== null) { |
|
263 |
+ $this->setCacheDir($cacheDir); |
|
264 |
+ } |
|
265 |
+ $this->initGlobals(); |
|
266 |
+ } |
|
267 |
+ |
|
268 |
+ /** |
|
269 |
+ * resets some runtime variables to allow a cloned object to be used to render sub-templates |
|
270 |
+ */ |
|
271 |
+ public function __clone() |
|
272 |
+ { |
|
273 |
+ $this->template = null; |
|
274 |
+ unset($this->data); |
|
275 |
+ } |
|
276 |
+ |
|
277 |
+ /** |
|
278 |
+ * outputs the template instead of returning it, this is basically a shortcut for get(*, *, *, true) |
|
279 |
+ * |
|
280 |
+ * @see get |
|
281 |
+ * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or |
|
282 |
+ * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster, |
|
283 |
+ * especially if you render a template multiple times |
|
284 |
+ * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're |
|
285 |
+ * rendering the template from cache, it can be left null |
|
286 |
+ * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default |
|
287 |
+ * Dwoo_Compiler will be used. |
|
288 |
+ * @return string nothing or the template output if $output is true |
|
289 |
+ */ |
|
290 |
+ public function output($tpl, $data = array(), Dwoo_ICompiler $compiler = null) |
|
291 |
+ { |
|
292 |
+ return $this->get($tpl, $data, $compiler, true); |
|
293 |
+ } |
|
294 |
+ |
|
295 |
+ /** |
|
296 |
+ * returns the given template rendered using the provided data and optional compiler |
|
297 |
+ * |
|
298 |
+ * @param mixed $tpl template, can either be a Dwoo_ITemplate object (i.e. Dwoo_Template_File), a valid path to a template, or |
|
299 |
+ * a template as a string it is recommended to provide a Dwoo_ITemplate as it will probably make things faster, |
|
300 |
+ * especially if you render a template multiple times |
|
301 |
+ * @param mixed $data the data to use, can either be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array. if you're |
|
302 |
+ * rendering the template from cache, it can be left null |
|
303 |
+ * @param Dwoo_ICompiler $compiler the compiler that must be used to compile the template, if left empty a default |
|
304 |
+ * Dwoo_Compiler will be used. |
|
305 |
+ * @param bool $output flag that defines whether the function returns the output of the template (false, default) or echoes it directly (true) |
|
306 |
+ * @return string nothing or the template output if $output is true |
|
307 |
+ */ |
|
308 |
+ public function get($_tpl, $data = array(), $_compiler = null, $_output = false) |
|
309 |
+ { |
|
310 |
+ // a render call came from within a template, so we need a new dwoo instance in order to avoid breaking this one |
|
311 |
+ if ($this->template instanceof Dwoo_ITemplate) { |
|
312 |
+ $proxy = clone $this; |
|
313 |
+ return $proxy->get($_tpl, $data, $_compiler, $_output); |
|
314 |
+ } |
|
315 |
+ |
|
316 |
+ // auto-create template if required |
|
317 |
+ if ($_tpl instanceof Dwoo_ITemplate) { |
|
318 |
+ // valid, skip |
|
319 |
+ } elseif (is_string($_tpl) && file_exists($_tpl)) { |
|
320 |
+ $_tpl = new Dwoo_Template_File($_tpl); |
|
321 |
+ } else { |
|
322 |
+ throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s first argument must be a Dwoo_ITemplate (i.e. Dwoo_Template_File) or a valid path to a template file', E_USER_NOTICE); |
|
323 |
+ } |
|
324 |
+ |
|
325 |
+ // save the current template, enters render mode at the same time |
|
326 |
+ // if another rendering is requested it will be proxied to a new Dwoo_Core(instance |
|
327 |
+ $this->template = $_tpl; |
|
328 |
+ |
|
329 |
+ // load data |
|
330 |
+ if ($data instanceof Dwoo_IDataProvider) { |
|
331 |
+ $this->data = $data->getData(); |
|
332 |
+ } elseif (is_array($data)) { |
|
333 |
+ $this->data = $data; |
|
334 |
+ } else { |
|
335 |
+ throw new Dwoo_Exception('Dwoo->get/Dwoo->output\'s data argument must be a Dwoo_IDataProvider object (i.e. Dwoo_Data) or an associative array', E_USER_NOTICE); |
|
336 |
+ } |
|
337 |
+ |
|
338 |
+ $this->globals['template'] = $_tpl->getName(); |
|
339 |
+ $this->initRuntimeVars($_tpl); |
|
340 |
+ |
|
341 |
+ // try to get cached template |
|
342 |
+ $file = $_tpl->getCachedTemplate($this); |
|
343 |
+ $doCache = $file === true; |
|
344 |
+ $cacheLoaded = is_string($file); |
|
345 |
+ |
|
346 |
+ if ($cacheLoaded === true) { |
|
347 |
+ // cache is present, run it |
|
348 |
+ if ($_output === true) { |
|
349 |
+ include $file; |
|
350 |
+ $this->template = null; |
|
351 |
+ } else { |
|
352 |
+ ob_start(); |
|
353 |
+ include $file; |
|
354 |
+ $this->template = null; |
|
355 |
+ return ob_get_clean(); |
|
356 |
+ } |
|
357 |
+ } else { |
|
358 |
+ // no cache present |
|
359 |
+ if ($doCache === true) { |
|
360 |
+ $dynamicId = uniqid(); |
|
361 |
+ } |
|
362 |
+ |
|
363 |
+ // render template |
|
364 |
+ $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); |
|
365 |
+ $out = include $compiledTemplate; |
|
366 |
+ |
|
367 |
+ // template returned false so it needs to be recompiled |
|
368 |
+ if ($out === false) { |
|
369 |
+ $_tpl->forceCompilation(); |
|
370 |
+ $compiledTemplate = $_tpl->getCompiledTemplate($this, $_compiler); |
|
371 |
+ $out = include $compiledTemplate; |
|
372 |
+ } |
|
373 |
+ |
|
374 |
+ if ($doCache === true) { |
|
375 |
+ $out = preg_replace('/(<%|%>|<\?php|<\?|\?>)/', '<?php /*'.$dynamicId.'*/ echo \'$1\'; ?>', $out); |
|
376 |
+ if (!class_exists('Dwoo_plugin_dynamic', false)) { |
|
377 |
+ $this->getLoader()->loadPlugin('dynamic'); |
|
378 |
+ } |
|
379 |
+ $out = Dwoo_Plugin_dynamic::unescape($out, $dynamicId, $compiledTemplate); |
|
380 |
+ } |
|
381 |
+ |
|
382 |
+ // process filters |
|
383 |
+ foreach ($this->filters as $filter) { |
|
384 |
+ if (is_array($filter) && $filter[0] instanceof Dwoo_Filter) { |
|
385 |
+ $out = call_user_func($filter, $out); |
|
386 |
+ } else { |
|
387 |
+ $out = call_user_func($filter, $this, $out); |
|
388 |
+ } |
|
389 |
+ } |
|
390 |
+ |
|
391 |
+ if ($doCache === true) { |
|
392 |
+ // building cache |
|
393 |
+ $file = $_tpl->cache($this, $out); |
|
394 |
+ |
|
395 |
+ // run it from the cache to be sure dynamics are rendered |
|
396 |
+ if ($_output === true) { |
|
397 |
+ include $file; |
|
398 |
+ // exit render mode |
|
399 |
+ $this->template = null; |
|
400 |
+ } else { |
|
401 |
+ ob_start(); |
|
402 |
+ include $file; |
|
403 |
+ // exit render mode |
|
404 |
+ $this->template = null; |
|
405 |
+ return ob_get_clean(); |
|
406 |
+ } |
|
407 |
+ } else { |
|
408 |
+ // no need to build cache |
|
409 |
+ // exit render mode |
|
410 |
+ $this->template = null; |
|
411 |
+ // output |
|
412 |
+ if ($_output === true) { |
|
413 |
+ echo $out; |
|
414 |
+ } |
|
415 |
+ return $out; |
|
416 |
+ } |
|
417 |
+ } |
|
418 |
+ } |
|
419 |
+ |
|
420 |
+ /** |
|
421 |
+ * re-initializes the globals array before each template run |
|
422 |
+ * |
|
423 |
+ * this method is only callede once when the Dwoo object is created |
|
424 |
+ */ |
|
425 |
+ protected function initGlobals() |
|
426 |
+ { |
|
427 |
+ $this->globals = array |
|
428 |
+ ( |
|
429 |
+ 'version' => self::VERSION, |
|
430 |
+ 'ad' => '<a href="http://dwoo.org/">Powered by Dwoo</a>', |
|
431 |
+ 'now' => $_SERVER['REQUEST_TIME'], |
|
432 |
+ 'charset' => $this->charset, |
|
433 |
+ ); |
|
434 |
+ } |
|
435 |
+ |
|
436 |
+ /** |
|
437 |
+ * re-initializes the runtime variables before each template run |
|
438 |
+ * |
|
439 |
+ * override this method to inject data in the globals array if needed, this |
|
440 |
+ * method is called before each template execution |
|
441 |
+ * |
|
442 |
+ * @param Dwoo_ITemplate $tpl the template that is going to be rendered |
|
443 |
+ */ |
|
444 |
+ protected function initRuntimeVars(Dwoo_ITemplate $tpl) |
|
445 |
+ { |
|
446 |
+ $this->runtimePlugins = array(); |
|
447 |
+ $this->scope =& $this->data; |
|
448 |
+ $this->scopeTree = array(); |
|
449 |
+ $this->stack = array(); |
|
450 |
+ $this->curBlock = null; |
|
451 |
+ $this->buffer = ''; |
|
452 |
+ } |
|
453 |
+ |
|
454 |
+ /* |
|
455 |
+ * --------- settings functions --------- |
|
456 |
+ */ |
|
457 |
+ |
|
458 |
+ /** |
|
459 |
+ * adds a custom plugin that is not in one of the plugin directories |
|
460 |
+ * |
|
461 |
+ * @param string $name the plugin name to be used in the templates |
|
462 |
+ * @param callback $callback the plugin callback, either a function name, |
|
463 |
+ * a class name or an array containing an object |
|
464 |
+ * or class name and a method name |
|
465 |
+ * @param bool $compilable if set to true, the plugin is assumed to be compilable |
|
466 |
+ */ |
|
467 |
+ public function addPlugin($name, $callback, $compilable = false) |
|
468 |
+ { |
|
469 |
+ $compilable = $compilable ? self::COMPILABLE_PLUGIN : 0; |
|
470 |
+ if (is_array($callback)) { |
|
471 |
+ if (is_subclass_of(is_object($callback[0]) ? get_class($callback[0]) : $callback[0], 'Dwoo_Block_Plugin')) { |
|
472 |
+ $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0])); |
|
473 |
+ } else { |
|
474 |
+ $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>(is_object($callback[0]) ? get_class($callback[0]) : $callback[0]), 'function'=>$callback[1]); |
|
475 |
+ } |
|
476 |
+ } elseif (class_exists($callback, false)) { |
|
477 |
+ if (is_subclass_of($callback, 'Dwoo_Block_Plugin')) { |
|
478 |
+ $this->plugins[$name] = array('type'=>self::BLOCK_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback); |
|
479 |
+ } else { |
|
480 |
+ $this->plugins[$name] = array('type'=>self::CLASS_PLUGIN | $compilable, 'callback'=>$callback, 'class'=>$callback, 'function'=>'process'); |
|
481 |
+ } |
|
482 |
+ } elseif (function_exists($callback)) { |
|
483 |
+ $this->plugins[$name] = array('type'=>self::FUNC_PLUGIN | $compilable, 'callback'=>$callback); |
|
484 |
+ } else { |
|
485 |
+ throw new Dwoo_Exception('Callback could not be processed correctly, please check that the function/class you used exists'); |
|
486 |
+ } |
|
487 |
+ } |
|
488 |
+ |
|
489 |
+ /** |
|
490 |
+ * removes a custom plugin |
|
491 |
+ * |
|
492 |
+ * @param string $name the plugin name |
|
493 |
+ */ |
|
494 |
+ public function removePlugin($name) |
|
495 |
+ { |
|
496 |
+ if (isset($this->plugins[$name])) { |
|
497 |
+ unset($this->plugins[$name]); |
|
498 |
+ } |
|
499 |
+ } |
|
500 |
+ |
|
501 |
+ /** |
|
502 |
+ * adds a filter to this Dwoo instance, it will be used to filter the output of all the templates rendered by this instance |
|
503 |
+ * |
|
504 |
+ * @param mixed $callback a callback or a filter name if it is autoloaded from a plugin directory |
|
505 |
+ * @param bool $autoload if true, the first parameter must be a filter name from one of the plugin directories |
|
506 |
+ */ |
|
507 |
+ public function addFilter($callback, $autoload = false) |
|
508 |
+ { |
|
509 |
+ if ($autoload) { |
|
510 |
+ $class = 'Dwoo_Filter_'.$callback; |
|
511 |
+ |
|
512 |
+ if (!class_exists($class, false) && !function_exists($class)) { |
|
513 |
+ try { |
|
514 |
+ $this->getLoader()->loadPlugin($callback); |
|
515 |
+ } catch (Dwoo_Exception $e) { |
|
516 |
+ if (strstr($callback, 'Dwoo_Filter_')) { |
|
517 |
+ throw new Dwoo_Exception('Wrong filter name : '.$callback.', the "Dwoo_Filter_" prefix should not be used, please only use "'.str_replace('Dwoo_Filter_', '', $callback).'"'); |
|
518 |
+ } else { |
|
519 |
+ throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); |
|
520 |
+ } |
|
521 |
+ } |
|
522 |
+ } |
|
523 |
+ |
|
524 |
+ if (class_exists($class, false)) { |
|
525 |
+ $callback = array(new $class($this), 'process'); |
|
526 |
+ } elseif (function_exists($class)) { |
|
527 |
+ $callback = $class; |
|
528 |
+ } else { |
|
529 |
+ throw new Dwoo_Exception('Wrong filter name : '.$callback.', when using autoload the filter must be in one of your plugin dir as "name.php" containg a class or function named "Dwoo_Filter_name"'); |
|
530 |
+ } |
|
531 |
+ |
|
532 |
+ $this->filters[] = $callback; |
|
533 |
+ } else { |
|
534 |
+ $this->filters[] = $callback; |
|
535 |
+ } |
|
536 |
+ } |
|
537 |
+ |
|
538 |
+ /** |
|
539 |
+ * removes a filter |
|
540 |
+ * |
|
541 |
+ * @param mixed $callback callback or filter name if it was autoloaded |
|
542 |
+ */ |
|
543 |
+ public function removeFilter($callback) |
|
544 |
+ { |
|
545 |
+ if (($index = array_search('Dwoo_Filter_'.$callback, $this->filters, true)) !== false) { |
|
546 |
+ unset($this->filters[$index]); |
|
547 |
+ } elseif (($index = array_search($callback, $this->filters, true)) !== false) { |
|
548 |
+ unset($this->filters[$index]); |
|
549 |
+ } else { |
|
550 |
+ $class = 'Dwoo_Filter_' . $callback; |
|
551 |
+ foreach ($this->filters as $index=>$filter) { |
|
552 |
+ if (is_array($filter) && $filter[0] instanceof $class) { |
|
553 |
+ unset($this->filters[$index]); |
|
554 |
+ break; |
|
555 |
+ } |
|
556 |
+ } |
|
557 |
+ } |
|
558 |
+ } |
|
559 |
+ |
|
560 |
+ /** |
|
561 |
+ * adds a resource or overrides a default one |
|
562 |
+ * |
|
563 |
+ * @param string $name the resource name |
|
564 |
+ * @param string $class the resource class (which must implement Dwoo_ITemplate) |
|
565 |
+ * @param callback $compilerFactory the compiler factory callback, a function that must return a compiler instance used to compile this resource, if none is provided. by default it will produce a Dwoo_Compiler object |
|
566 |
+ */ |
|
567 |
+ public function addResource($name, $class, $compilerFactory = null) |
|
568 |
+ { |
|
569 |
+ if (strlen($name) < 2) { |
|
570 |
+ throw new Dwoo_Exception('Resource names must be at least two-character long to avoid conflicts with Windows paths'); |
|
571 |
+ } |
|
572 |
+ |
|
573 |
+ if (!class_exists($class)) { |
|
574 |
+ throw new Dwoo_Exception('Resource class does not exist'); |
|
575 |
+ } |
|
576 |
+ |
|
577 |
+ $interfaces = class_implements($class); |
|
578 |
+ if (in_array('Dwoo_ITemplate', $interfaces) === false) { |
|
579 |
+ throw new Dwoo_Exception('Resource class must implement Dwoo_ITemplate'); |
|
580 |
+ } |
|
581 |
+ |
|
582 |
+ $this->resources[$name] = array('class'=>$class, 'compiler'=>$compilerFactory); |
|
583 |
+ } |
|
584 |
+ |
|
585 |
+ /** |
|
586 |
+ * removes a custom resource |
|
587 |
+ * |
|
588 |
+ * @param string $name the resource name |
|
589 |
+ */ |
|
590 |
+ public function removeResource($name) |
|
591 |
+ { |
|
592 |
+ unset($this->resources[$name]); |
|
593 |
+ if ($name==='file') { |
|
594 |
+ $this->resources['file'] = array('class'=>'Dwoo_Template_File', 'compiler'=>null); |
|
595 |
+ } |
|
596 |
+ } |
|
597 |
+ |
|
598 |
+ /* |
|
599 |
+ * --------- getters and setters --------- |
|
600 |
+ */ |
|
601 |
+ |
|
602 |
+ /** |
|
603 |
+ * sets the loader object to use to load plugins |
|
604 |
+ * |
|
605 |
+ * @param Dwoo_ILoader $loader loader object |
|
606 |
+ */ |
|
607 |
+ public function setLoader(Dwoo_ILoader $loader) |
|
608 |
+ { |
|
609 |
+ $this->loader = $loader; |
|
610 |
+ } |
|
611 |
+ |
|
612 |
+ /** |
|
613 |
+ * returns the current loader object or a default one if none is currently found |
|
614 |
+ * |
|
615 |
+ * @param Dwoo_ILoader |
|
616 |
+ */ |
|
617 |
+ public function getLoader() |
|
618 |
+ { |
|
619 |
+ if ($this->loader === null) { |
|
620 |
+ $this->loader = new Dwoo_Loader($this->getCompileDir()); |
|
621 |
+ } |
|
622 |
+ |
|
623 |
+ return $this->loader; |
|
624 |
+ } |
|
625 |
+ |
|
626 |
+ /** |
|
627 |
+ * returns the custom plugins loaded |
|
628 |
+ * |
|
629 |
+ * used by the Dwoo_ITemplate classes to pass the custom plugins to their Dwoo_ICompiler instance |
|
630 |
+ * |
|
631 |
+ * @return array |
|
632 |
+ */ |
|
633 |
+ public function getCustomPlugins() |
|
634 |
+ { |
|
635 |
+ return $this->plugins; |
|
636 |
+ } |
|
637 |
+ |
|
638 |
+ /** |
|
639 |
+ * returns the cache directory with a trailing DIRECTORY_SEPARATOR |
|
640 |
+ * |
|
641 |
+ * @return string |
|
642 |
+ */ |
|
643 |
+ public function getCacheDir() |
|
644 |
+ { |
|
645 |
+ if ($this->cacheDir === null) { |
|
646 |
+ $this->setCacheDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR); |
|
647 |
+ } |
|
648 |
+ |
|
649 |
+ return $this->cacheDir; |
|
650 |
+ } |
|
651 |
+ |
|
652 |
+ /** |
|
653 |
+ * sets the cache directory and automatically appends a DIRECTORY_SEPARATOR |
|
654 |
+ * |
|
655 |
+ * @param string $dir the cache directory |
|
656 |
+ */ |
|
657 |
+ public function setCacheDir($dir) |
|
658 |
+ { |
|
659 |
+ $this->cacheDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; |
|
660 |
+ if (is_writable($this->cacheDir) === false) { |
|
661 |
+ throw new Dwoo_Exception('The cache directory must be writable, chmod "'.$this->cacheDir.'" to make it writable'); |
|
662 |
+ } |
|
663 |
+ } |
|
664 |
+ |
|
665 |
+ /** |
|
666 |
+ * returns the compile directory with a trailing DIRECTORY_SEPARATOR |
|
667 |
+ * |
|
668 |
+ * @return string |
|
669 |
+ */ |
|
670 |
+ public function getCompileDir() |
|
671 |
+ { |
|
672 |
+ if ($this->compileDir === null) { |
|
673 |
+ $this->setCompileDir(dirname(__FILE__).DIRECTORY_SEPARATOR.'compiled'.DIRECTORY_SEPARATOR); |
|
674 |
+ } |
|
675 |
+ |
|
676 |
+ return $this->compileDir; |
|
677 |
+ } |
|
678 |
+ |
|
679 |
+ /** |
|
680 |
+ * sets the compile directory and automatically appends a DIRECTORY_SEPARATOR |
|
681 |
+ * |
|
682 |
+ * @param string $dir the compile directory |
|
683 |
+ */ |
|
684 |
+ public function setCompileDir($dir) |
|
685 |
+ { |
|
686 |
+ $this->compileDir = rtrim($dir, '/\\').DIRECTORY_SEPARATOR; |
|
687 |
+ if (is_writable($this->compileDir) === false) { |
|
688 |
+ throw new Dwoo_Exception('The compile directory must be writable, chmod "'.$this->compileDir.'" to make it writable'); |
|
689 |
+ } |
|
690 |
+ } |
|
691 |
+ |
|
692 |
+ /** |
|
693 |
+ * returns the default cache time that is used with templates that do not have a cache time set |
|
694 |
+ * |
|
695 |
+ * @return int the duration in seconds |
|
696 |
+ */ |
|
697 |
+ public function getCacheTime() |
|
698 |
+ { |
|
699 |
+ return $this->cacheTime; |
|
700 |
+ } |
|
701 |
+ |
|
702 |
+ /** |
|
703 |
+ * sets the default cache time to use with templates that do not have a cache time set |
|
704 |
+ * |
|
705 |
+ * @param int $seconds the duration in seconds |
|
706 |
+ */ |
|
707 |
+ public function setCacheTime($seconds) |
|
708 |
+ { |
|
709 |
+ $this->cacheTime = (int) $seconds; |
|
710 |
+ } |
|
711 |
+ |
|
712 |
+ /** |
|
713 |
+ * returns the character set used by the string manipulation plugins |
|
714 |
+ * |
|
715 |
+ * the charset is automatically lowercased |
|
716 |
+ * |
|
717 |
+ * @return string |
|
718 |
+ */ |
|
719 |
+ public function getCharset() |
|
720 |
+ { |
|
721 |
+ return $this->charset; |
|
722 |
+ } |
|
723 |
+ |
|
724 |
+ /** |
|
725 |
+ * sets the character set used by the string manipulation plugins |
|
726 |
+ * |
|
727 |
+ * the charset will be automatically lowercased |
|
728 |
+ * |
|
729 |
+ * @param string $charset the character set |
|
730 |
+ */ |
|
731 |
+ public function setCharset($charset) |
|
732 |
+ { |
|
733 |
+ $this->charset = strtolower((string) $charset); |
|
734 |
+ } |
|
735 |
+ |
|
736 |
+ /** |
|
737 |
+ * returns the current template being rendered, when applicable, or null |
|
738 |
+ * |
|
739 |
+ * @return Dwoo_ITemplate|null |
|
740 |
+ */ |
|
741 |
+ public function getTemplate() |
|
742 |
+ { |
|
743 |
+ return $this->template; |
|
744 |
+ } |
|
745 |
+ |
|
746 |
+ /** |
|
747 |
+ * sets the current template being rendered |
|
748 |
+ * |
|
749 |
+ * @param Dwoo_ITemplate $tpl template object |
|
750 |
+ */ |
|
751 |
+ public function setTemplate(Dwoo_ITemplate $tpl) |
|
752 |
+ { |
|
753 |
+ $this->template = $tpl; |
|
754 |
+ } |
|
755 |
+ |
|
756 |
+ /** |
|
757 |
+ * sets the default compiler factory function for the given resource name |
|
758 |
+ * |
|
759 |
+ * a compiler factory must return a Dwoo_ICompiler object pre-configured to fit your needs |
|
760 |
+ * |
|
761 |
+ * @param string $resourceName the resource name (i.e. file, string) |
|
762 |
+ * @param callback $compilerFactory the compiler factory callback |
|
763 |
+ */ |
|
764 |
+ public function setDefaultCompilerFactory($resourceName, $compilerFactory) |
|
765 |
+ { |
|
766 |
+ $this->resources[$resourceName]['compiler'] = $compilerFactory; |
|
767 |
+ } |
|
768 |
+ |
|
769 |
+ /** |
|
770 |
+ * returns the default compiler factory function for the given resource name |
|
771 |
+ * |
|
772 |
+ * @param string $resourceName the resource name |
|
773 |
+ * @return callback the compiler factory callback |
|
774 |
+ */ |
|
775 |
+ public function getDefaultCompilerFactory($resourceName) |
|
776 |
+ { |
|
777 |
+ return $this->resources[$resourceName]['compiler']; |
|
778 |
+ } |
|
779 |
+ |
|
780 |
+ /** |
|
781 |
+ * sets the security policy object to enforce some php security settings |
|
782 |
+ * |
|
783 |
+ * use this if untrusted persons can modify templates |
|
784 |
+ * |
|
785 |
+ * @param Dwoo_Security_Policy $policy the security policy object |
|
786 |
+ */ |
|
787 |
+ public function setSecurityPolicy(Dwoo_Security_Policy $policy = null) |
|
788 |
+ { |
|
789 |
+ $this->securityPolicy = $policy; |
|
790 |
+ } |
|
791 |
+ |
|
792 |
+ /** |
|
793 |
+ * returns the current security policy object or null by default |
|
794 |
+ * |
|
795 |
+ * @return Dwoo_Security_Policy|null the security policy object if any |
|
796 |
+ */ |
|
797 |
+ public function getSecurityPolicy() |
|
798 |
+ { |
|
799 |
+ return $this->securityPolicy; |
|
800 |
+ } |
|
801 |
+ |
|
802 |
+ /** |
|
803 |
+ * sets the object that must be used as a plugin proxy when plugin can't be found |
|
804 |
+ * by dwoo's loader |
|
805 |
+ * |
|
806 |
+ * @param Dwoo_IPluginProxy $pluginProxy the proxy object |
|
807 |
+ */ |
|
808 |
+ public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy) { |
|
809 |
+ $this->pluginProxy = $pluginProxy; |
|
810 |
+ } |
|
811 |
+ |
|
812 |
+ /** |
|
813 |
+ * returns the current plugin proxy object or null by default |
|
814 |
+ * |
|
815 |
+ * @param Dwoo_IPluginProxy|null the proxy object if any |
|
816 |
+ */ |
|
817 |
+ public function getPluginProxy() { |
|
818 |
+ return $this->pluginProxy; |
|
819 |
+ } |
|
820 |
+ |
|
821 |
+ /* |
|
822 |
+ * --------- util functions --------- |
|
823 |
+ */ |
|
824 |
+ |
|
825 |
+ /** |
|
826 |
+ * [util function] checks whether the given template is cached or not |
|
827 |
+ * |
|
828 |
+ * @param Dwoo_ITemplate $tpl the template object |
|
829 |
+ * @return bool |
|
830 |
+ */ |
|
831 |
+ public function isCached(Dwoo_ITemplate $tpl) |
|
832 |
+ { |
|
833 |
+ return is_string($tpl->getCachedTemplate($this)); |
|
834 |
+ } |
|
835 |
+ |
|
836 |
+ /** |
|
837 |
+ * [util function] clears the cached templates if they are older than the given time |
|
838 |
+ * |
|
839 |
+ * @param int $olderThan minimum time (in seconds) required for a cached template to be cleared |
|
840 |
+ * @return int the amount of templates cleared |
|
841 |
+ */ |
|
842 |
+ public function clearCache($olderThan=-1) |
|
843 |
+ { |
|
844 |
+ $cacheDirs = new RecursiveDirectoryIterator($this->getCacheDir()); |
|
845 |
+ $cache = new RecursiveIteratorIterator($cacheDirs); |
|
846 |
+ $expired = time() - $olderThan; |
|
847 |
+ $count = 0; |
|
848 |
+ foreach ($cache as $file) { |
|
849 |
+ if ($cache->isDot() || $cache->isDir() || substr($file, -5) !== '.html') { |
|
850 |
+ continue; |
|
851 |
+ } |
|
852 |
+ if ($cache->getCTime() < $expired) { |
|
853 |
+ $count += unlink((string) $file) ? 1 : 0; |
|
854 |
+ } |
|
855 |
+ } |
|
856 |
+ return $count; |
|
857 |
+ } |
|
858 |
+ |
|
859 |
+ /** |
|
860 |
+ * [util function] fetches a template object of the given resource |
|
861 |
+ * |
|
862 |
+ * @param string $resourceName the resource name (i.e. file, string) |
|
863 |
+ * @param string $resourceId the resource identifier (i.e. file path) |
|
864 |
+ * @param int $cacheTime the cache time setting for this resource |
|
865 |
+ * @param string $cacheId the unique cache identifier |
|
866 |
+ * @param string $compileId the unique compiler identifier |
|
867 |
+ * @return Dwoo_ITemplate |
|
868 |
+ */ |
|
869 |
+ public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) |
|
870 |
+ { |
|
871 |
+ if (isset($this->resources[$resourceName])) { |
|
872 |
+ // TODO could be changed to $this->resources[$resourceName]['class']::templateFactory(..) in 5.3 maybe |
|
873 |
+ return call_user_func(array($this->resources[$resourceName]['class'], 'templateFactory'), $this, $resourceId, $cacheTime, $cacheId, $compileId, $parentTemplate); |
|
874 |
+ } else { |
|
875 |
+ throw new Dwoo_Exception('Unknown resource type : '.$resourceName); |
|
876 |
+ } |
|
877 |
+ } |
|
878 |
+ |
|
879 |
+ /** |
|
880 |
+ * [util function] checks if the input is an array or arrayaccess object, optionally it can also check if it's empty |
|
881 |
+ * |
|
882 |
+ * @param mixed $value the variable to check |
|
883 |
+ * @param bool $checkIsEmpty if true, the function will also check if the array|arrayaccess is empty, |
|
884 |
+ * and return true only if it's not empty |
|
885 |
+ * @return int|bool true if it's an array|arrayaccess (or the item count if $checkIsEmpty is true) or false if it's not an array|arrayaccess (or 0 if $checkIsEmpty is true) |
|
886 |
+ */ |
|
887 |
+ public function isArray($value, $checkIsEmpty=false) |
|
888 |
+ { |
|
889 |
+ if (is_array($value) === true || $value instanceof ArrayAccess) { |
|
890 |
+ if ($checkIsEmpty === false) { |
|
891 |
+ return true; |
|
892 |
+ } else { |
|
893 |
+ return $this->count($value); |
|
894 |
+ } |
|
895 |
+ } |
|
896 |
+ } |
|
897 |
+ |
|
898 |
+ /** |
|
899 |
+ * [util function] checks if the input is an array or a traversable object, optionally it can also check if it's empty |
|
900 |
+ * |
|
901 |
+ * @param mixed $value the variable to check |
|
902 |
+ * @param bool $checkIsEmpty if true, the function will also check if the array|traversable is empty, |
|
903 |
+ * and return true only if it's not empty |
|
904 |
+ * @return int|bool true if it's an array|traversable (or the item count if $checkIsEmpty is true) or false if it's not an array|traversable (or 0 if $checkIsEmpty is true) |
|
905 |
+ */ |
|
906 |
+ public function isTraversable($value, $checkIsEmpty=false) |
|
907 |
+ { |
|
908 |
+ if (is_array($value) === true) { |
|
909 |
+ if ($checkIsEmpty === false) { |
|
910 |
+ return true; |
|
911 |
+ } else { |
|
912 |
+ return count($value) > 0; |
|
913 |
+ } |
|
914 |
+ } elseif ($value instanceof Traversable) { |
|
915 |
+ if ($checkIsEmpty === false) { |
|
916 |
+ return true; |
|
917 |
+ } else { |
|
918 |
+ return $this->count($value); |
|
919 |
+ } |
|
920 |
+ } |
|
921 |
+ return false; |
|
922 |
+ } |
|
923 |
+ |
|
924 |
+ /** |
|
925 |
+ * [util function] counts an array or arrayaccess/traversable object |
|
926 |
+ * @param mixed $value |
|
927 |
+ * @return int|bool the count for arrays and objects that implement countable, true for other objects that don't, and 0 for empty elements |
|
928 |
+ */ |
|
929 |
+ public function count($value) |
|
930 |
+ { |
|
931 |
+ if (is_array($value) === true || $value instanceof Countable) { |
|
932 |
+ return count($value); |
|
933 |
+ } elseif ($value instanceof ArrayAccess) { |
|
934 |
+ if ($value->offsetExists(0)) { |
|
935 |
+ return true; |
|
936 |
+ } |
|
937 |
+ } elseif ($value instanceof Iterator) { |
|
938 |
+ $value->rewind(); |
|
939 |
+ if ($value->valid()) { |
|
940 |
+ return true; |
|
941 |
+ } |
|
942 |
+ } elseif ($value instanceof Traversable) { |
|
943 |
+ foreach ($value as $dummy) { |
|
944 |
+ return true; |
|
945 |
+ } |
|
946 |
+ } |
|
947 |
+ return 0; |
|
948 |
+ } |
|
949 |
+ |
|
950 |
+ /** |
|
951 |
+ * [util function] triggers a dwoo error |
|
952 |
+ * |
|
953 |
+ * @param string $message the error message |
|
954 |
+ * @param int $level the error level, one of the PHP's E_* constants |
|
955 |
+ */ |
|
956 |
+ public function triggerError($message, $level=E_USER_NOTICE) |
|
957 |
+ { |
|
958 |
+ if (!($tplIdentifier = $this->template->getResourceIdentifier())) { |
|
959 |
+ $tplIdentifier = $this->template->getResourceName(); |
|
960 |
+ } |
|
961 |
+ trigger_error('Dwoo error (in '.$tplIdentifier.') : '.$message, $level); |
|
962 |
+ } |
|
963 |
+ |
|
964 |
+ /* |
|
965 |
+ * --------- runtime functions --------- |
|
966 |
+ */ |
|
967 |
+ |
|
968 |
+ /** |
|
969 |
+ * [runtime function] adds a block to the block stack |
|
970 |
+ * |
|
971 |
+ * @param string $blockName the block name (without Dwoo_Plugin_ prefix) |
|
972 |
+ * @param array $args the arguments to be passed to the block's init() function |
|
973 |
+ * @return Dwoo_Block_Plugin the newly created block |
|
974 |
+ */ |
|
975 |
+ public function addStack($blockName, array $args=array()) |
|
976 |
+ { |
|
977 |
+ if (isset($this->plugins[$blockName])) { |
|
978 |
+ $class = $this->plugins[$blockName]['class']; |
|
979 |
+ } else { |
|
980 |
+ $class = 'Dwoo_Plugin_'.$blockName; |
|
981 |
+ } |
|
982 |
+ |
|
983 |
+ if ($this->curBlock !== null) { |
|
984 |
+ $this->curBlock->buffer(ob_get_contents()); |
|
985 |
+ ob_clean(); |
|
986 |
+ } else { |
|
987 |
+ $this->buffer .= ob_get_contents(); |
|
988 |
+ ob_clean(); |
|
989 |
+ } |
|
990 |
+ |
|
991 |
+ $block = new $class($this); |
|
992 |
+ |
|
993 |
+ $cnt = count($args); |
|
994 |
+ if ($cnt===0) { |
|
995 |
+ $block->init(); |
|
996 |
+ } elseif ($cnt===1) { |
|
997 |
+ $block->init($args[0]); |
|
998 |
+ } elseif ($cnt===2) { |
|
999 |
+ $block->init($args[0], $args[1]); |
|
1000 |
+ } elseif ($cnt===3) { |
|
1001 |
+ $block->init($args[0], $args[1], $args[2]); |
|
1002 |
+ } elseif ($cnt===4) { |
|
1003 |
+ $block->init($args[0], $args[1], $args[2], $args[3]); |
|
1004 |
+ } else { |
|
1005 |
+ call_user_func_array(array($block,'init'), $args); |
|
1006 |
+ } |
|
1007 |
+ |
|
1008 |
+ $this->stack[] = $this->curBlock = $block; |
|
1009 |
+ return $block; |
|
1010 |
+ } |
|
1011 |
+ |
|
1012 |
+ /** |
|
1013 |
+ * [runtime function] removes the plugin at the top of the block stack |
|
1014 |
+ * |
|
1015 |
+ * calls the block buffer() function, followed by a call to end() |
|
1016 |
+ * and finally a call to process() |
|
1017 |
+ */ |
|
1018 |
+ public function delStack() |
|
1019 |
+ { |
|
1020 |
+ $args = func_get_args(); |
|
1021 |
+ |
|
1022 |
+ $this->curBlock->buffer(ob_get_contents()); |
|
1023 |
+ ob_clean(); |
|
1024 |
+ |
|
1025 |
+ $cnt = count($args); |
|
1026 |
+ if ($cnt===0) { |
|
1027 |
+ $this->curBlock->end(); |
|
1028 |
+ } elseif ($cnt===1) { |
|
1029 |
+ $this->curBlock->end($args[0]); |
|
1030 |
+ } elseif ($cnt===2) { |
|
1031 |
+ $this->curBlock->end($args[0], $args[1]); |
|
1032 |
+ } elseif ($cnt===3) { |
|
1033 |
+ $this->curBlock->end($args[0], $args[1], $args[2]); |
|
1034 |
+ } elseif ($cnt===4) { |
|
1035 |
+ $this->curBlock->end($args[0], $args[1], $args[2], $args[3]); |
|
1036 |
+ } else { |
|
1037 |
+ call_user_func_array(array($this->curBlock, 'end'), $args); |
|
1038 |
+ } |
|
1039 |
+ |
|
1040 |
+ $tmp = array_pop($this->stack); |
|
1041 |
+ |
|
1042 |
+ if (count($this->stack) > 0) { |
|
1043 |
+ $this->curBlock = end($this->stack); |
|
1044 |
+ $this->curBlock->buffer($tmp->process()); |
|
1045 |
+ } else { |
|
1046 |
+ $this->curBlock = null; |
|
1047 |
+ echo $tmp->process(); |
|
1048 |
+ } |
|
1049 |
+ |
|
1050 |
+ unset($tmp); |
|
1051 |
+ } |
|
1052 |
+ |
|
1053 |
+ /** |
|
1054 |
+ * [runtime function] returns the parent block of the given block |
|
1055 |
+ * |
|
1056 |
+ * @param Dwoo_Block_Plugin $block |
|
1057 |
+ * @return Dwoo_Block_Plugin or false if the given block isn't in the stack |
|
1058 |
+ */ |
|
1059 |
+ public function getParentBlock(Dwoo_Block_Plugin $block) |
|
1060 |
+ { |
|
1061 |
+ $index = array_search($block, $this->stack, true); |
|
1062 |
+ if ($index !== false && $index > 0) { |
|
1063 |
+ return $this->stack[$index-1]; |
|
1064 |
+ } |
|
1065 |
+ return false; |
|
1066 |
+ } |
|
1067 |
+ |
|
1068 |
+ /** |
|
1069 |
+ * [runtime function] finds the closest block of the given type, starting at the top of the stack |
|
1070 |
+ * |
|
1071 |
+ * @param string $type the type of plugin you want to find |
|
1072 |
+ * @return Dwoo_Block_Plugin or false if no plugin of such type is in the stack |
|
1073 |
+ */ |
|
1074 |
+ public function findBlock($type) |
|
1075 |
+ { |
|
1076 |
+ if (isset($this->plugins[$type])) { |
|
1077 |
+ $type = $this->plugins[$type]['class']; |
|
1078 |
+ } else { |
|
1079 |
+ $type = 'Dwoo_Plugin_'.str_replace('Dwoo_Plugin_', '', $type); |
|
1080 |
+ } |
|
1081 |
+ |
|
1082 |
+ $keys = array_keys($this->stack); |
|
1083 |
+ while (($key = array_pop($keys)) !== false) { |
|
1084 |
+ if ($this->stack[$key] instanceof $type) { |
|
1085 |
+ return $this->stack[$key]; |
|
1086 |
+ } |
|
1087 |
+ } |
|
1088 |
+ return false; |
|
1089 |
+ } |
|
1090 |
+ |
|
1091 |
+ /** |
|
1092 |
+ * [runtime function] returns a Dwoo_Plugin of the given class |
|
1093 |
+ * |
|
1094 |
+ * this is so a single instance of every class plugin is created at each template run, |
|
1095 |
+ * allowing class plugins to have "per-template-run" static variables |
|
1096 |
+ * |
|
1097 |
+ * @private |
|
1098 |
+ * @param string $class the class name |
|
1099 |
+ * @return mixed an object of the given class |
|
1100 |
+ */ |
|
1101 |
+ public function getObjectPlugin($class) |
|
1102 |
+ { |
|
1103 |
+ if (isset($this->runtimePlugins[$class])) { |
|
1104 |
+ return $this->runtimePlugins[$class]; |
|
1105 |
+ } |
|
1106 |
+ return $this->runtimePlugins[$class] = new $class($this); |
|
1107 |
+ } |
|
1108 |
+ |
|
1109 |
+ /** |
|
1110 |
+ * [runtime function] calls the process() method of the given class-plugin name |
|
1111 |
+ * |
|
1112 |
+ * @param string $plugName the class plugin name (without Dwoo_Plugin_ prefix) |
|
1113 |
+ * @param array $params an array of parameters to send to the process() method |
|
1114 |
+ * @return string the process() return value |
|
1115 |
+ */ |
|
1116 |
+ public function classCall($plugName, array $params = array()) |
|
1117 |
+ { |
|
1118 |
+ $class = 'Dwoo_Plugin_'.$plugName; |
|
1119 |
+ |
|
1120 |
+ $plugin = $this->getObjectPlugin($class); |
|
1121 |
+ |
|
1122 |
+ $cnt = count($params); |
|
1123 |
+ if ($cnt===0) { |
|
1124 |
+ return $plugin->process(); |
|
1125 |
+ } elseif ($cnt===1) { |
|
1126 |
+ return $plugin->process($params[0]); |
|
1127 |
+ } elseif ($cnt===2) { |
|
1128 |
+ return $plugin->process($params[0], $params[1]); |
|
1129 |
+ } elseif ($cnt===3) { |
|
1130 |
+ return $plugin->process($params[0], $params[1], $params[2]); |
|
1131 |
+ } elseif ($cnt===4) { |
|
1132 |
+ return $plugin->process($params[0], $params[1], $params[2], $params[3]); |
|
1133 |
+ } else { |
|
1134 |
+ return call_user_func_array(array($plugin, 'process'), $params); |
|
1135 |
+ } |
|
1136 |
+ } |
|
1137 |
+ |
|
1138 |
+ /** |
|
1139 |
+ * [runtime function] calls a php function |
|
1140 |
+ * |
|
1141 |
+ * @param string $callback the function to call |
|
1142 |
+ * @param array $params an array of parameters to send to the function |
|
1143 |
+ * @return mixed the return value of the called function |
|
1144 |
+ */ |
|
1145 |
+ public function arrayMap($callback, array $params) |
|
1146 |
+ { |
|
1147 |
+ if ($params[0] === $this) { |
|
1148 |
+ $addThis = true; |
|
1149 |
+ array_shift($params); |
|
1150 |
+ } |
|
1151 |
+ if ((is_array($params[0]) || ($params[0] instanceof Iterator && $params[0] instanceof ArrayAccess))) { |
|
1152 |
+ if (empty($params[0])) { |
|
1153 |
+ return $params[0]; |
|
1154 |
+ } |
|
1155 |
+ |
|
1156 |
+ // array map |
|
1157 |
+ $out = array(); |
|
1158 |
+ $cnt = count($params); |
|
1159 |
+ |
|
1160 |
+ if (isset($addThis)) { |
|
1161 |
+ array_unshift($params, $this); |
|
1162 |
+ $items = $params[1]; |
|
1163 |
+ $keys = array_keys($items); |
|
1164 |
+ |
|
1165 |
+ if (is_string($callback) === false) { |
|
1166 |
+ while (($i = array_shift($keys)) !== null) { |
|
1167 |
+ $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); |
|
1168 |
+ } |
|
1169 |
+ } elseif ($cnt===1) { |
|
1170 |
+ while (($i = array_shift($keys)) !== null) { |
|
1171 |
+ $out[] = $callback($this, $items[$i]); |
|
1172 |
+ } |
|
1173 |
+ } elseif ($cnt===2) { |
|
1174 |
+ while (($i = array_shift($keys)) !== null) { |
|
1175 |
+ $out[] = $callback($this, $items[$i], $params[2]); |
|
1176 |
+ } |
|
1177 |
+ } elseif ($cnt===3) { |
|
1178 |
+ while (($i = array_shift($keys)) !== null) { |
|
1179 |
+ $out[] = $callback($this, $items[$i], $params[2], $params[3]); |
|
1180 |
+ } |
|
1181 |
+ } else { |
|
1182 |
+ while (($i = array_shift($keys)) !== null) { |
|
1183 |
+ $out[] = call_user_func_array($callback, array(1=>$items[$i]) + $params); |
|
1184 |
+ } |
|
1185 |
+ } |
|
1186 |
+ } else { |
|
1187 |
+ $items = $params[0]; |
|
1188 |
+ $keys = array_keys($items); |
|
1189 |
+ |
|
1190 |
+ if (is_string($callback) === false) { |
|
1191 |
+ while (($i = array_shift($keys)) !== null) { |
|
1192 |
+ $out[] = call_user_func_array($callback, array($items[$i]) + $params); |
|
1193 |
+ } |
|
1194 |
+ } elseif ($cnt===1) { |
|
1195 |
+ while (($i = array_shift($keys)) !== null) { |
|
1196 |
+ $out[] = $callback($items[$i]); |
|
1197 |
+ } |
|
1198 |
+ } elseif ($cnt===2) { |
|
1199 |
+ while (($i = array_shift($keys)) !== null) { |
|
1200 |
+ $out[] = $callback($items[$i], $params[1]); |
|
1201 |
+ } |
|
1202 |
+ } elseif ($cnt===3) { |
|
1203 |
+ while (($i = array_shift($keys)) !== null) { |
|
1204 |
+ $out[] = $callback($items[$i], $params[1], $params[2]); |
|
1205 |
+ } |
|
1206 |
+ } elseif ($cnt===4) { |
|
1207 |
+ while (($i = array_shift($keys)) !== null) { |
|
1208 |
+ $out[] = $callback($items[$i], $params[1], $params[2], $params[3]); |
|
1209 |
+ } |
|
1210 |
+ } else { |
|
1211 |
+ while (($i = array_shift($keys)) !== null) { |
|
1212 |
+ $out[] = call_user_func_array($callback, array($items[$i]) + $params); |
|
1213 |
+ } |
|
1214 |
+ } |
|
1215 |
+ } |
|
1216 |
+ return $out; |
|
1217 |
+ } else { |
|
1218 |
+ return $params[0]; |
|
1219 |
+ } |
|
1220 |
+ } |
|
1221 |
+ |
|
1222 |
+ /** |
|
1223 |
+ * [runtime function] reads a variable into the given data array |
|
1224 |
+ * |
|
1225 |
+ * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property") |
|
1226 |
+ * @param mixed $data the data array or object to read from |
|
1227 |
+ * @param bool $safeRead if true, the function will check whether the index exists to prevent any notices from being output |
|
1228 |
+ * @return mixed |
|
1229 |
+ */ |
|
1230 |
+ public function readVarInto($varstr, $data, $safeRead = false) |
|
1231 |
+ { |
|
1232 |
+ if ($data === null) { |
|
1233 |
+ return null; |
|
1234 |
+ } |
|
1235 |
+ |
|
1236 |
+ if (is_array($varstr) === false) { |
|
1237 |
+ preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); |
|
1238 |
+ } else { |
|
1239 |
+ $m = $varstr; |
|
1240 |
+ } |
|
1241 |
+ unset($varstr); |
|
1242 |
+ |
|
1243 |
+ while (list($k, $sep) = each($m[1])) { |
|
1244 |
+ if ($sep === '.' || $sep === '[' || $sep === '') { |
|
1245 |
+ if ((is_array($data) || $data instanceof ArrayAccess) && ($safeRead === false || isset($data[$m[2][$k]]))) { |
|
1246 |
+ $data = $data[$m[2][$k]]; |
|
1247 |
+ } else { |
|
1248 |
+ return null; |
|
1249 |
+ } |
|
1250 |
+ } else { |
|
1251 |
+ if (is_object($data) && ($safeRead === false || isset($data->$m[2][$k]) || is_callable(array($data, '__get')))) { |
|
1252 |
+ $data = $data->$m[2][$k]; |
|
1253 |
+ } else { |
|
1254 |
+ return null; |
|
1255 |
+ } |
|
1256 |
+ } |
|
1257 |
+ } |
|
1258 |
+ |
|
1259 |
+ return $data; |
|
1260 |
+ } |
|
1261 |
+ |
|
1262 |
+ /** |
|
1263 |
+ * [runtime function] reads a variable into the parent scope |
|
1264 |
+ * |
|
1265 |
+ * @param int $parentLevels the amount of parent levels to go from the current scope |
|
1266 |
+ * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property") |
|
1267 |
+ * @return mixed |
|
1268 |
+ */ |
|
1269 |
+ public function readParentVar($parentLevels, $varstr = null) |
|
1270 |
+ { |
|
1271 |
+ $tree = $this->scopeTree; |
|
1272 |
+ $cur = $this->data; |
|
1273 |
+ |
|
1274 |
+ while ($parentLevels--!==0) { |
|
1275 |
+ array_pop($tree); |
|
1276 |
+ } |
|
1277 |
+ |
|
1278 |
+ while (($i = array_shift($tree)) !== null) { |
|
1279 |
+ if (is_object($cur)) { |
|
1280 |
+ $cur = $cur->$i; |
|
1281 |
+ } else { |
|
1282 |
+ $cur = $cur[$i]; |
|
1283 |
+ } |
|
1284 |
+ } |
|
1285 |
+ |
|
1286 |
+ if ($varstr!==null) { |
|
1287 |
+ return $this->readVarInto($varstr, $cur); |
|
1288 |
+ } else { |
|
1289 |
+ return $cur; |
|
1290 |
+ } |
|
1291 |
+ } |
|
1292 |
+ |
|
1293 |
+ /** |
|
1294 |
+ * [runtime function] reads a variable into the current scope |
|
1295 |
+ * |
|
1296 |
+ * @param string $varstr the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property") |
|
1297 |
+ * @return mixed |
|
1298 |
+ */ |
|
1299 |
+ public function readVar($varstr) |
|
1300 |
+ { |
|
1301 |
+ if (is_array($varstr)===true) { |
|
1302 |
+ $m = $varstr; |
|
1303 |
+ unset($varstr); |
|
1304 |
+ } else { |
|
1305 |
+ if (strstr($varstr, '.') === false && strstr($varstr, '[') === false && strstr($varstr, '->') === false) { |
|
1306 |
+ if ($varstr === 'dwoo') { |
|
1307 |
+ return $this->globals; |
|
1308 |
+ } elseif ($varstr === '__' || $varstr === '_root' ) { |
|
1309 |
+ return $this->data; |
|
1310 |
+ $varstr = substr($varstr, 6); |
|
1311 |
+ } elseif ($varstr === '_' || $varstr === '_parent') { |
|
1312 |
+ $varstr = '.'.$varstr; |
|
1313 |
+ $tree = $this->scopeTree; |
|
1314 |
+ $cur = $this->data; |
|
1315 |
+ array_pop($tree); |
|
1316 |
+ |
|
1317 |
+ while (($i = array_shift($tree)) !== null) { |
|
1318 |
+ if (is_object($cur)) { |
|
1319 |
+ $cur = $cur->$i; |
|
1320 |
+ } else { |
|
1321 |
+ $cur = $cur[$i]; |
|
1322 |
+ } |
|
1323 |
+ } |
|
1324 |
+ |
|
1325 |
+ return $cur; |
|
1326 |
+ } |
|
1327 |
+ |
|
1328 |
+ $cur = $this->scope; |
|
1329 |
+ |
|
1330 |
+ if (isset($cur[$varstr])) { |
|
1331 |
+ return $cur[$varstr]; |
|
1332 |
+ } else { |
|
1333 |
+ return null; |
|
1334 |
+ } |
|
1335 |
+ } |
|
1336 |
+ |
|
1337 |
+ if (substr($varstr, 0, 1) === '.') { |
|
1338 |
+ $varstr = 'dwoo'.$varstr; |
|
1339 |
+ } |
|
1340 |
+ |
|
1341 |
+ preg_match_all('#(\[|->|\.)?((?:[^.[\]-]|-(?!>))+)\]?#i', $varstr, $m); |
|
1342 |
+ } |
|
1343 |
+ |
|
1344 |
+ $i = $m[2][0]; |
|
1345 |
+ if ($i === 'dwoo') { |
|
1346 |
+ $cur = $this->globals; |
|
1347 |
+ array_shift($m[2]); |
|
1348 |
+ array_shift($m[1]); |
|
1349 |
+ switch ($m[2][0]) { |
|
1350 |
+ |
|
1351 |
+ case 'get': |
|
1352 |
+ $cur = $_GET; |
|
1353 |
+ break; |
|
1354 |
+ case 'post': |
|
1355 |
+ $cur = $_POST; |
|
1356 |
+ break; |
|
1357 |
+ case 'session': |
|
1358 |
+ $cur = $_SESSION; |
|
1359 |
+ break; |
|
1360 |
+ case 'cookies': |
|
1361 |
+ case 'cookie': |
|
1362 |
+ $cur = $_COOKIE; |
|
1363 |
+ break; |
|
1364 |
+ case 'server': |
|
1365 |
+ $cur = $_SERVER; |
|
1366 |
+ break; |
|
1367 |
+ case 'env': |
|
1368 |
+ $cur = $_ENV; |
|
1369 |
+ break; |
|
1370 |
+ case 'request': |
|
1371 |
+ $cur = $_REQUEST; |
|
1372 |
+ break; |
|
1373 |
+ case 'const': |
|
1374 |
+ array_shift($m[2]); |
|
1375 |
+ if (defined($m[2][0])) { |
|
1376 |
+ return constant($m[2][0]); |
|
1377 |
+ } else { |
|
1378 |
+ return null; |
|
1379 |
+ } |
|
1380 |
+ |
|
1381 |
+ } |
|
1382 |
+ if ($cur !== $this->globals) { |
|
1383 |
+ array_shift($m[2]); |
|
1384 |
+ array_shift($m[1]); |
|
1385 |
+ } |
|
1386 |
+ } elseif ($i === '__' || $i === '_root') { |
|
1387 |
+ $cur = $this->data; |
|
1388 |
+ array_shift($m[2]); |
|
1389 |
+ array_shift($m[1]); |
|
1390 |
+ } elseif ($i === '_' || $i === '_parent') { |
|
1391 |
+ $tree = $this->scopeTree; |
|
1392 |
+ $cur = $this->data; |
|
1393 |
+ |
|
1394 |
+ while (true) { |
|
1395 |
+ array_pop($tree); |
|
1396 |
+ array_shift($m[2]); |
|
1397 |
+ array_shift($m[1]); |
|
1398 |
+ if (current($m[2]) === '_' || current($m[2]) === '_parent') { |
|
1399 |
+ continue; |
|
1400 |
+ } |
|
1401 |
+ |
|
1402 |
+ while (($i = array_shift($tree)) !== null) { |
|
1403 |
+ if (is_object($cur)) { |
|
1404 |
+ $cur = $cur->$i; |
|
1405 |
+ } else { |
|
1406 |
+ $cur = $cur[$i]; |
|
1407 |
+ } |
|
1408 |
+ } |
|
1409 |
+ break; |
|
1410 |
+ } |
|
1411 |
+ } else { |
|
1412 |
+ $cur = $this->scope; |
|
1413 |
+ } |
|
1414 |
+ |
|
1415 |
+ while (list($k, $sep) = each($m[1])) { |
|
1416 |
+ if ($sep === '.' || $sep === '[' || $sep === '') { |
|
1417 |
+ if ((is_array($cur) || $cur instanceof ArrayAccess) && isset($cur[$m[2][$k]])) { |
|
1418 |
+ $cur = $cur[$m[2][$k]]; |
|
1419 |
+ } else { |
|
1420 |
+ return null; |
|
1421 |
+ } |
|
1422 |
+ } elseif ($sep === '->') { |
|
1423 |
+ if (is_object($cur)) { |
|
1424 |
+ $cur = $cur->$m[2][$k]; |
|
1425 |
+ } else { |
|
1426 |
+ return null; |
|
1427 |
+ } |
|
1428 |
+ } else { |
|
1429 |
+ return null; |
|
1430 |
+ } |
|
1431 |
+ } |
|
1432 |
+ |
|
1433 |
+ return $cur; |
|
1434 |
+ } |
|
1435 |
+ |
|
1436 |
+ /** |
|
1437 |
+ * [runtime function] assign the value to the given variable |
|
1438 |
+ * |
|
1439 |
+ * @param mixed $value the value to assign |
|
1440 |
+ * @param string $scope the variable string, using dwoo variable syntax (i.e. "var.subvar[subsubvar]->property") |
|
1441 |
+ * @return bool true if assigned correctly or false if a problem occured while parsing the var string |
|
1442 |
+ */ |
|
1443 |
+ public function assignInScope($value, $scope) |
|
1444 |
+ { |
|
1445 |
+ $tree =& $this->scopeTree; |
|
1446 |
+ $data =& $this->data; |
|
1447 |
+ |
|
1448 |
+ if (!is_string($scope)) { |
|
1449 |
+ return $this->triggerError('Assignments must be done into strings, ('.gettype($scope).') '.var_export($scope, true).' given', E_USER_ERROR); |
|
1450 |
+ } |
|
1451 |
+ if (strstr($scope, '.') === false && strstr($scope, '->') === false) { |
|
1452 |
+ $this->scope[$scope] = $value; |
|
1453 |
+ } else { |
|
1454 |
+ // TODO handle _root/_parent scopes ? |
|
1455 |
+ preg_match_all('#(\[|->|\.)?([^.[\]-]+)\]?#i', $scope, $m); |
|
1456 |
+ |
|
1457 |
+ $cur =& $this->scope; |
|
1458 |
+ $last = array(array_pop($m[1]), array_pop($m[2])); |
|
1459 |
+ |
|
1460 |
+ while (list($k, $sep) = each($m[1])) { |
|
1461 |
+ if ($sep === '.' || $sep === '[' || $sep === '') { |
|
1462 |
+ if (is_array($cur) === false) { |
|
1463 |
+ $cur = array(); |
|
1464 |
+ } |
|
1465 |
+ $cur =& $cur[$m[2][$k]]; |
|
1466 |
+ } elseif ($sep === '->') { |
|
1467 |
+ if (is_object($cur) === false) { |
|
1468 |
+ $cur = new stdClass; |
|
1469 |
+ } |
|
1470 |
+ $cur =& $cur->$m[2][$k]; |
|
1471 |
+ } else { |
|
1472 |
+ return false; |
|
1473 |
+ } |
|
1474 |
+ } |
|
1475 |
+ |
|
1476 |
+ if ($last[0] === '.' || $last[0] === '[' || $last[0] === '') { |
|
1477 |
+ if (is_array($cur) === false) { |
|
1478 |
+ $cur = array(); |
|
1479 |
+ } |
|
1480 |
+ $cur[$last[1]] = $value; |
|
1481 |
+ } elseif ($last[0] === '->') { |
|
1482 |
+ if (is_object($cur) === false) { |
|
1483 |
+ $cur = new stdClass; |
|
1484 |
+ } |
|
1485 |
+ $cur->$last[1] = $value; |
|
1486 |
+ } else { |
|
1487 |
+ return false; |
|
1488 |
+ } |
|
1489 |
+ } |
|
1490 |
+ } |
|
1491 |
+ |
|
1492 |
+ /** |
|
1493 |
+ * [runtime function] sets the scope to the given scope string or array |
|
1494 |
+ * |
|
1495 |
+ * @param mixed $scope a string i.e. "level1.level2" or an array i.e. array("level1", "level2") |
|
1496 |
+ * @param bool $absolute if true, the scope is set from the top level scope and not from the current scope |
|
1497 |
+ * @return array the current scope tree |
|
1498 |
+ */ |
|
1499 |
+ public function setScope($scope, $absolute = false) |
|
1500 |
+ { |
|
1501 |
+ $old = $this->scopeTree; |
|
1502 |
+ |
|
1503 |
+ if (is_string($scope)===true) { |
|
1504 |
+ $scope = explode('.', $scope); |
|
1505 |
+ } |
|
1506 |
+ |
|
1507 |
+ if ($absolute===true) { |
|
1508 |
+ $this->scope =& $this->data; |
|
1509 |
+ $this->scopeTree = array(); |
|
1510 |
+ } |
|
1511 |
+ |
|
1512 |
+ while (($bit = array_shift($scope)) !== null) { |
|
1513 |
+ if ($bit === '_' || $bit === '_parent') { |
|
1514 |
+ array_pop($this->scopeTree); |
|
1515 |
+ $this->scope =& $this->data; |
|
1516 |
+ $cnt = count($this->scopeTree); |
|
1517 |
+ for ($i=0;$i<$cnt;$i++) |
|
1518 |
+ $this->scope =& $this->scope[$this->scopeTree[$i]]; |
|
1519 |
+ } elseif ($bit === '__' || $bit === '_root') { |
|
1520 |
+ $this->scope =& $this->data; |
|
1521 |
+ $this->scopeTree = array(); |
|
1522 |
+ } elseif (isset($this->scope[$bit])) { |
|
1523 |
+ $this->scope =& $this->scope[$bit]; |
|
1524 |
+ $this->scopeTree[] = $bit; |
|
1525 |
+ } else { |
|
1526 |
+ unset($this->scope); |
|
1527 |
+ $this->scope = null; |
|
1528 |
+ } |
|
1529 |
+ } |
|
1530 |
+ |
|
1531 |
+ return $old; |
|
1532 |
+ } |
|
1533 |
+ |
|
1534 |
+ /** |
|
1535 |
+ * [runtime function] returns the entire data array |
|
1536 |
+ * |
|
1537 |
+ * @return array |
|
1538 |
+ */ |
|
1539 |
+ public function getData() |
|
1540 |
+ { |
|
1541 |
+ return $this->data; |
|
1542 |
+ } |
|
1543 |
+ |
|
1544 |
+ /** |
|
1545 |
+ * [runtime function] returns a reference to the current scope |
|
1546 |
+ * |
|
1547 |
+ * @return &mixed |
|
1548 |
+ */ |
|
1549 |
+ public function &getScope() |
|
1550 |
+ { |
|
1551 |
+ return $this->scope; |
|
1552 |
+ } |
|
1553 |
+ |
|
1554 |
+ /** |
|
1555 |
+ * Redirects all calls to unexisting to plugin proxy. |
|
1556 |
+ * |
|
1557 |
+ * @param string Method name |
|
1558 |
+ * @param array List of arguments |
|
1559 |
+ * @return mixed |
|
1560 |
+ */ |
|
1561 |
+ public function __call($method, $args) { |
|
1562 |
+ $proxy = $this->getPluginProxy(); |
|
1563 |
+ if (!$proxy) { |
|
1564 |
+ throw new Dwoo_Exception('Call to undefined method '.__CLASS__.'::'.$method.'()'); |
|
1565 |
+ } |
|
1566 |
+ return call_user_func_array($proxy->getCallback($method), $args); |
|
1567 |
+ } |
|
1568 |
+} |