Documentation is available at Crafty.class.php
- <?PHP
- /**
- * Implementation file of the Crafty main class.
- *
- * <pre>
- * PROJECT : Crafty
- * Template Engine.
- * AUTHOR : Crafty Team <crafty@zulan.net>
- * COPYRIGHT : (c) Thomas Ilsche, 2004
- *
- * FILE : [ROOT]\Crafty.class.php
- * DESCRIPTION: Implementation file of the Crafty main class.
- * </pre>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- * @package Crafty
- * @author Crafty Team <crafty@zulan.net>
- * @copyright Copyright (c) Thomas Ilsche 2004
- * @version 1.0.4.2005.02.16
- * @link http://crafty.zulan.net
- * @license http://www.gnu.org/licenses/lgpl.html
- * GNU Lesser General Public License Version 2.1
- ***/
- /**
- * Crafty version number
- **/
- define('CRAFTY_VERSION', '1.0.4.2005.02.18');
- /**#@+
- * Constant for use in {@link Crafty::display()}
- **/
- /**
- * Compile if template file date is more recent than compiled template file
- * date. Counts only for the initial template file.
- * If you only changed a .block tpl then simply `touch` the initial template.
- **/
- define('CRAFTY_COMPILE_AUTO', 1);
- /**
- * Compile unconditionally whenever {@link Crafty::display()} is called.
- **/
- define('CRAFTY_COMPILE_ALWAYS', 2);
- /**
- * Never compile, except the include file does not exists.
- **/
- define('CRAFTY_COMPILE_NEVER', 3);
- /**#@-*/ * Crafty main class implementation
- *
- * This is the main class of the Crafty template engine. You should always
- * instantiate this and make use of it's member functions. See also the Crafty
- * Usage and Syntax manual for further information.
- *
- * @package Crafty
- * @author Crafty Team <crafty@zulan.net>
- * @copyright Copyright (c) Thomas Ilsche
- * @version 1.0.4.2005.02.16
- * @link http://crafty.zulan.net
- * @license http://www.gnu.org/licenses/lgpl.html
- * GNU Lesser General Public License Version 2.1
- **/
- class Crafty
- {
- /**
- * Stores assigned values
- *
- * This array is used to store the values assigned to this template by
- * {@link assign()}, {@link assignRef()}, {@link append()} or
- * {@link appendRef()}.
- *
- * @var array
- * @access private
- ***/
- private $_output_data = array();
- /**
- * Path to Crafty directory
- *
- * Stores the absolute path to the Crafty directory. It is set by the {@link }
- * __construct() constructor}, so you will have to pass it at least once.
- *
- * @var string
- ***/
- public $path_crafty;
- /**
- * Root path to the compiled templates
- *
- * Stores the absolute path to the compiled template files. This is
- * automatically set by the constructor, you will not have to take care of it.
- *
- * @var string
- ***/
- public $path_templates_c;
- /**
- * Object handle of a Compiler object
- *
- * If compiling is neccessary (display modes CRAFTY_COMPILE_AUTO and
- * CRAFTY_COMPILE_ALWAYS), this will contain an object handle of a
- * Crafty_Compiler object. It is only for internal use.
- *
- * @var Crafty_Compiler
- ***/
- public $compiler;
- /**
- * Initializes a Crafty instance
- *
- * The constructor intializes a Crafty instance and sets up the neccessary
- * internal path variables. It also includes the {@link Crafty_Exception}
- * Exception classes}. You have to pass the absolute or relative path to
- * the constructor so Crafty can find it's files.<br />
- * <br />
- * From version 1.0.1 the parameter is optional and will be acquired using
- * the __FILE__ magic constant by default.
- *
- * @param string $path_crafty [optional] Path to the crafty directory
- ***/
- public function __construct($path_crafty = '')
- {
- if (empty($path_crafty)) {
- $path_crafty = dirname(__FILE__);
- } else {
- $path_crafty = realpath($path_crafty);
- }
- /* Backwards compabilitiy */
- $path_crafty = $path_crafty . DIRECTORY_SEPARATOR;
- require_once($path_crafty . 'Crafty_Exceptions.class.php');
- $this->path_crafty = $path_crafty;
- $this->path_templates_c = $path_crafty . 'templates_c' . DIRECTORY_SEPARATOR;
- }
- /**
- * Assign a variable content
- *
- * This assigns a variable content to a template before its beeing displayed
- * by {@link display()}. If $extract is true and data is an associative array,
- * every key will be taken as sub-variable and the corresponding value as
- * sub-variable content. The array itself will still be assigned.
- * $extract also works recursive
- *
- * @param string $name Variable path and name
- * @param mixed $data Variable content
- * @param boolean $extract [optional, default=false] Whether or not to
- * extract an array as multiple variables.
- ***/
- public function assign($name, $data, $extract = FALSE)
- {
- $this->_output_data[$name] = $data;
- if ($extract && is_array($data)) {
- foreach($data as $key => $value) {
- $this->assign("$name.$key", $value, TRUE);
- }
- }
- }
- /**
- * Assign variable content as reference
- *
- * This assigns a variable content as reference. So you can first assign
- * the variable and then manipulate it's content. There's no extraction for
- * references like in {@link assign()}.
- *
- * @param string $name Variable path and name
- * @param reference $data Variable reference
- ***/
- public function assignRef($name, &$data)
- {
- $this->_output_data[$name] = &$data;
- }
- /**
- * Append a content to an data array
- *
- * Appends content $data to the assigned data array $name.
- * $data will allways be appended at the end.<br/>
- * If the array is not yet assigned, a new one will be created,
- * if only a value is set this value will be used as first element of the
- * array and $data as the second one.<br/>
- * If $index is set it will not just append to the main array but append
- * with a $index as key for the main array. However this will not influence
- * the order of the displayed blocks.<br/>
- * If $index_sub is set it will create and append the following array:<br/>
- * array($index_sub => $data) then the value of $index can be used as
- * variablename inside a blocklist, instead of $value. This is useful for
- * cascaded blocklists.
- *
- * @param string $name Variable path and name
- * @param mixed $data Variable content
- * @param string $index [optional, default=''] Index
- * for main array
- * @param string $index_sub [optional, default=''] Index
- * for subarray
- ***/
- public function append($name, $data, $index = '', $index_sub = '')
- {
- if (!empty($index_sub)) {
- $data = array($index_sub => $data);
- }
- if (empty($index)) {
- if (!isset($this->_output_data[$name])) {
- $this->_output_data[$name] = array($data);
- } elseif (is_array($this->_output_data[$name])) {
- $this->_output_data[$name][] = $data;
- } else {
- $this->_output_data[$name] = array($this->_output_data[$name],
- $data);
- }
- } else {
- if (!isset($this->_output_data[$name])) {
- $this->_output_data[$name] = array($index => $data);
- } elseif (is_array($this->_output_data[$name])) {
- $this->_output_data[$name][$index] = $data;
- } else {
- $this->_output_data[$name] = array($this->_output_data[$name],
- $index => $data);
- }
- }
- }
- /**
- * Append a reference to an data array
- *
- * Same as {@link append()}, but uses an reference instead of "real" values.
- *
- * @param string $name Variable path and name
- * @param mixed $data Variable content
- * @param string $index [optional, default=''] Index
- * for main array
- * @param string $index_sub [optional, default=''] Index
- * for subarray
- ***/
- public function appendRef($name, &$data, $index = '', $index_sub = '')
- {
- if (!empty($index_sub)) {
- $data = array($index_sub => &$data);
- }
- if (empty($index)) {
- if (!isset($this->_output_data[$name])) {
- $this->_output_data[$name] = array(&$data);
- } elseif (is_array($this->_output_data[$name])) {
- $this->_output_data[$name][] = &$data;
- } else {
- $this->_output_data[$name] = array($this->_output_data[$name],
- &$data);
- }
- } else {
- if (!isset($this->_output_data[$name])) {
- $this->_output_data[$name] = array($index => &$data);
- } elseif (is_array($this->_output_data[$name])) {
- $this->_output_data[$name][$index] = &$data;
- } else {
- $this->_output_data[$name] = array($this->_output_data[$name],
- $index => &$data);
- }
- }
- }
- /**
- * Display template
- *
- * Displays template $template_name in compilation mode $compile_mode. Call
- * this from your file after all values are assigned.
- *
- * @param string $template_name Filename of template to display
- * @param integer $compile_mode [optional, default=
- * {@link CRAFTY_COMPILE_AUTO}]<br/>
- * Compilation method.<br/>
- * Other possible values:<br/>
- * {@link CRAFTY_COMPILE_ALWAYS},
- * {@link CRAFTY_COMPILE_NEVER}
- * @return boolean State of success
- ***/
- public function display($template_name, $compile_mode = CRAFTY_COMPILE_AUTO)
- {
- $d =& $this->_output_data;
- $template_hash = md5(realpath($template_name) . CRAFTY_VERSION);
- $template_c = $this->path_templates_c . $template_hash . '.tpl.php';
- ob_start();
- set_error_handler(array($this,'error_handler'));
- if (($compile_mode == CRAFTY_COMPILE_ALWAYS)
- || (!file_exists($template_c))
- || (($compile_mode == CRAFTY_COMPILE_AUTO) &&
- (filemtime($template_name) > filemtime($template_c)))
- || (FALSE === @include($template_c))) {
- try {
- require_once($this->path_crafty . 'Crafty_Compiler.class.php');
- $this->compiler = new crafty_compiler($this);
- $this->compiler->load($template_name);
- $this->compiler->compile($template_hash);
- if (!function_exists('_' . $template_hash . '_main')) {
- include($template_c);
- } else {
- call_user_func('_' . $template_hash . '_main','',$d);
- }
- } catch (Crafty_Exception $e) {
- ob_clean();
- exit($e->getFormattedMessage('HTML_FULL'));
- }
- }
- restore_error_handler();
- ob_flush();
- return TRUE;
- }
- /**
- * Error handler method for compiled templates
- *
- * See {@link set_error_handler} for the parameters.
- **/
- public function error_handler($errno, $errstr, $errfile, $errline, $errcontext)
- {
- ob_clean();
- echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"';
- echo '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
- echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
- echo '<head><title>Error</title>';
- echo '<body><h1>The following error occured in the compiled template:</h1>';
- echo "'<b>$errstr</b>' - in <b>$errfile</b> on line <b>$errline</b><br />";
- echo 'Undefined indexes are usually caused by missing assignes.<br />';
- echo 'Please check your code for missing assignes and your template for';
- echo ' references to unassigned variables.<br />';
- echo 'Include errors are caused by variable block(list)s that reference to';
- echo ' non compiled blocks. Do not forget to [REQUIRE file_1,file_2,file3]';
- echo ' if the files are external.';
- echo '</body></html>';
- exit;
- }
- /**
- * Checks security integrity of a string
- *
- * This checks whether or not it is secure to print the string $param in a
- * template. If not, an exception will be thrown, and Crafty will refuse
- * further work.
- *
- * @param string $param String to check
- ***/
- public function securityCheck($param)
- {
- if (strstr($param, '../')) {
- throw new Crafty_Exception("Invalid parameter. Parameters must not
- contain '../'");
- }
- }
- }
- ?>
Documentation generated on Sat, 19 Feb 2005 01:43:38 +0100 by phpDocumentor 1.3.0RC3