📄 savant2.php
字号:
*/ function setEscape() { $this->_escape = func_get_args(); } /** * * Adds to the callbacks used when calling $this->escape(). * * Each parameter passed to this function is treated as a separate * callback. For example: * * <code> * $savant->addEscape( * 'stripslashes', * 'htmlspecialchars', * array('StaticClass', 'method'), * array($object, $method) * ); * </code> * * @access public * * @return void * */ function addEscape() { $args = func_get_args(); $this->_escape = array_merge($this->_escape, $args); } /** * * Gets the array of output-escaping callbacks. * * @access public * * @return array The array of output-escaping callbacks. * */ function getEscape() { return $this->_escape; } /** * * Applies escaping to a value. * * You can override the predefined escaping callbacks by passing * added parameters as replacement callbacks. * * <code> * // use predefined callbacks * $result = $savant->escape($value); * * // use replacement callbacks * $result = $savant->escape( * $value, * 'stripslashes', * 'htmlspecialchars', * array('StaticClass', 'method'), * array($object, $method) * ); * </code> * * @access public * * @param mixed $value The value to be escaped. * * @return mixed * */ function escape($value) { // were custom callbacks passed? if (func_num_args() == 1) { // no, only a value was passed. // loop through the predefined callbacks. foreach ($this->_escape as $func) { $value = call_user_func($func, $value); } } else { // yes, use the custom callbacks instead. $callbacks = func_get_args(); // drop $value array_shift($callbacks); // loop through custom callbacks. foreach ($callbacks as $func) { $value = call_user_func($func, $value); } } return $value; } /** * * Prints a value after escaping it for output. * * You can override the predefined escaping callbacks by passing * added parameters as replacement callbacks. * * <code> * // use predefined callbacks * $this->_($value); * * // use replacement callbacks * $this->_( * $value, * 'stripslashes', * 'htmlspecialchars', * array('StaticClass', 'method'), * array($object, $method) * ); * </code> * * @access public * * @param mixed $value The value to be escaped and printed. * * @return void * */ function eprint($value) { $args = func_get_args(); echo call_user_func_array( array($this, 'escape'), $args ); } /** * * Alias to eprint() and identical in every way. * * @access public * * @param mixed $value The value to be escaped and printed. * * @return void * */ function _($value) { $args = func_get_args(); return call_user_func_array( array($this, 'eprint'), $args ); } // ----------------------------------------------------------------- // // Path management and file finding // // ----------------------------------------------------------------- /** * * Sets an entire array of search paths. * * @access public * * @param string $type The type of path to set, typcially 'template' * or 'resource'. * * @param string|array $new The new set of search paths. If null or * false, resets to the current directory only. * * @return void * */ function setPath($type, $new) { // clear out the prior search dirs $this->_path[$type] = array(); // convert from string to path if (is_string($new) && ! strpos($new, '://')) { // the search config is a string, and it's not a stream // identifier (the "://" piece), add it as a path // string. $new = explode(PATH_SEPARATOR, $new); } else { // force to array settype($new, 'array'); } // always add the fallback directories as last resort switch (strtolower($type)) { case 'template': $this->addPath($type, '.'); break; case 'resource': $this->addPath($type, dirname(__FILE__) . '/Savant2/'); break; } // actually add the user-specified directories foreach ($new as $dir) { $this->addPath($type, $dir); } } /** * * Adds a search directory for templates. * * @access public * * @param string $dir The directory or stream to search. * * @return void * */ function addPath($type, $dir) { // no surrounding spaces allowed! $dir = trim($dir); // add trailing separators as needed if (strpos($dir, '://') && substr($dir, -1) != '/') { // stream $dir .= '/'; } elseif (substr($dir, -1) != DIRECTORY_SEPARATOR) { // directory $dir .= DIRECTORY_SEPARATOR; } // add to the top of the search dirs array_unshift($this->_path[$type], $dir); } /** * * Gets the array of search directories for template sources. * * @access public * * @return array The array of search directories for template sources. * */ function getPath($type = null) { if (! $type) { return $this->_path; } else { return $this->_path[$type]; } } /** * * Searches a series of paths for a given file. * * @param array $type The type of paths to search (template, plugin, * or filter). * * @param string $file The file name to look for. * * @return string|bool The full path and file name for the target file, * or boolean false if the file is not found in any of the paths. * */ function findFile($type, $file) { // get the set of paths $set = $this->getPath($type); // start looping through them foreach ($set as $path) { // get the path to the file $fullname = $path . $file; // are we doing path checks? if (! $this->_restrict) { // no. this is faster but less secure. if (file_exists($fullname) && is_readable($fullname)) { return $fullname; } } else { // yes. this is slower, but attempts to restrict // access only to defined paths. // is the path based on a stream? if (strpos($path, '://') === false) { // not a stream, so do a realpath() to avoid // directory traversal attempts on the local file // system. Suggested by Ian Eure, initially // rejected, but then adopted when the secure // compiler was added. $path = realpath($path); // needed for substr() later $fullname = realpath($fullname); } // the substr() check added by Ian Eure to make sure // that the realpath() results in a directory registered // with Savant so that non-registered directores are not // accessible via directory traversal attempts. if (file_exists($fullname) && is_readable($fullname) && substr($fullname, 0, strlen($path)) == $path) { return $fullname; } } } // could not find the file in the set of paths return false; } // ----------------------------------------------------------------- // // Variable and reference assignment // // ----------------------------------------------------------------- /** * * Sets variables for the template. * * This method is overloaded; you can assign all the properties of * an object, an associative array, or a single value by name. * * You are not allowed to set variables that begin with an underscore; * these are either private properties for Savant2 or private variables * within the template script itself. * * <code> * * $Savant2 =& new Savant2(); * * // assign directly * $Savant2->var1 = 'something'; * $Savant2->var2 = 'else'; * * // assign by name and value * $Savant2->assign('var1', 'something'); * $Savant2->assign('var2', 'else'); * * // assign by assoc-array * $ary = array('var1' => 'something', 'var2' => 'else'); * $Savant2->assign($obj); * * // assign by object * $obj = new stdClass; * $obj->var1 = 'something'; * $obj->var2 = 'else'; * $Savant2->assign($obj); * * </code> * * Greg Beaver came up with the idea of assigning to public class * properties. * * @access public * * @return void * * @throws object An error object with a SAVANT2_ERROR_ASSIGN code. * */ function assign() { // this method is overloaded. $arg = func_get_args(); // must have at least one argument. no error, just do nothing. if (! isset($arg[0])) { return; } // assign by object if (is_object($arg[0])) { // assign public properties foreach (get_object_vars($arg[0]) as $key => $val) { if (substr($key, 0, 1) != '_') { $this->$key = $val; } } return; } // assign by associative array if (is_array($arg[0])) { foreach ($arg[0] as $key => $val) { if (substr($key, 0, 1) != '_') { $this->$key = $val; } } return; } // assign by string name and mixed value. // // we use array_key_exists() instead of isset() becuase isset() // fails if the value is set to null. if (is_string($arg[0]) && substr($arg[0], 0, 1) != '_' && array_key_exists(1, $arg)) { $this->$arg[0] = $arg[1]; } else { return $this->error(SAVANT2_ERROR_ASSIGN, $arg); } } /** * * Sets references for the template. * * // assign by name and value * $Savant2->assignRef('ref', $reference); * * // assign directly * $Savant2->ref =& $reference; * * Greg Beaver came up with the idea of assigning to public class * properties. * * @access public * * @param string $key The name for the reference in the template. * * @param mixed &$val The referenced variable. * * @return void * * @throws object An error object with a SAVANT2_ERROR_ASSIGNREF code. * */ function assignRef($key, &$val) { if (is_string($key) && substr($key, 0, 1) != '_') { $this->$key =& $val; } else { return $this->error( SAVANT2_ERROR_ASSIGNREF, array('key' => $key, 'val' => $val) ); } } /** * * Unsets assigned variables and references. * * @access public * * @param mixed $var If null, clears all variables; if a string, clears * the one variable named by the string; if a sequential array, clears * the variables names in that array. * * @return void * */ function clear($var = null) { if (is_null($var)) { // clear all variables $var = array_keys(get_object_vars($this)); } else { // clear specific variables settype($var, 'array'); } // clear out the selected variables foreach ($var as $name) { if (substr($name, 0, 1) != '_' && isset($this->$name)) { unset($this->$name); } } } /** * * Gets the current value of one, many, or all assigned variables. * * Never returns variables starting with an underscore; these are * reserved for internal Savant2 use. * * @access public * * @param mixed $key If null, returns a copy of all variables and * their values; if an array, returns an only those variables named * in the array; if a string, returns only that variable. * * @return mixed If multiple variables were reqested, returns an * associative array where the key is the variable name and the * value is the variable value; if one variable was requested, * returns the variable value only. * */ function getVars($key = null) { if (is_null($key)) { $key = array_keys(get_object_vars($this)); } if (is_array($key)) { // return a series of vars $tmp = array(); foreach ($key as $var) { if (substr($var, 0, 1) != '_' && isset($this->$var)) { $tmp[$var] = $this->$var; } } return $tmp; } else { // return a single var if (substr($key, 0, 1) != '_' && isset($this->$key)) { return $this->$key; } } } // ----------------------------------------------------------------- // // Template processing // // ----------------------------------------------------------------- /** * * Loads a template script for execution (does not execute the script). * * This will optionally compile the template source into a PHP script * if a compiler object has been passed into Savant2. * * Also good for including templates from the template paths within * another template, like so: * * include $this->loadTemplate('template.tpl.php'); * * @access public * * @param string $tpl The template source name to look for. * * @param bool $setScript Default false; if true, sets the $this->_script * property to the resulting script path (or null on error). Normally, * only $this->fetch() will need to set this to true. * * @return string The full path to the compiled template script. * * @throws object An error object with a SAVANT2_ERROR_NOTEMPLATE code. * */ function loadTemplate($tpl = null, $setScript = false) { // set to default template if none specified. if (is_null($tpl)) { $tpl = $this->_template; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -