| +-----------------------------------------------------------------------+ $Id$ */ /** * Class to create HTML page output using a skin template * * @package View * @todo Documentation * @uses rcube_html_page */ class rcube_template extends rcube_html_page { private $app; private $config; private $pagetitle = ''; private $message = null; private $js_env = array(); private $js_labels = array(); private $js_commands = array(); private $object_handlers = array(); private $plugin_skin_path; private $template_name; public $browser; public $framed = false; public $env = array(); public $type = 'html'; public $ajax_call = false; // deprecated names of templates used before 0.5 private $deprecated_templates = array( 'contact' => 'showcontact', 'contactadd' => 'addcontact', 'contactedit' => 'editcontact', 'identityedit' => 'editidentity', 'messageprint' => 'printmessage', ); /** * Constructor * * @todo Replace $this->config with the real rcube_config object */ public function __construct($task, $framed = false) { parent::__construct(); $this->app = rcmail::get_instance(); $this->config = $this->app->config->all(); $this->browser = new rcube_browser(); //$this->framed = $framed; $this->set_env('task', $task); $this->set_env('x_frame_options', $this->app->config->get('x_frame_options', 'sameorigin')); // load the correct skin (in case user-defined) $skin = $this->app->config->get('skin'); $this->set_skin($skin); $this->set_env('skin', $skin); // add common javascripts $this->add_script('var '.JS_OBJECT_NAME.' = new rcube_webmail();', 'head_top'); // don't wait for page onload. Call init at the bottom of the page (delayed) $this->add_script(JS_OBJECT_NAME.'.init();', 'docready'); $this->scripts_path = 'program/js/'; $this->include_script('jquery.min.js'); $this->include_script('common.js'); $this->include_script('app.js'); // register common UI objects $this->add_handlers(array( 'loginform' => array($this, 'login_form'), 'preloader' => array($this, 'preloader'), 'username' => array($this, 'current_username'), 'message' => array($this, 'message_container'), 'charsetselector' => array($this, 'charset_selector'), 'aboutcontent' => array($this, 'about_content'), )); } /** * Set environment variable * * @param string Property name * @param mixed Property value * @param boolean True if this property should be added to client environment */ public function set_env($name, $value, $addtojs = true) { $this->env[$name] = $value; if ($addtojs || isset($this->js_env[$name])) { $this->js_env[$name] = $value; } } /** * Set page title variable */ public function set_pagetitle($title) { $this->pagetitle = $title; } /** * Getter for the current page title * * @return string The page title */ public function get_pagetitle() { if (!empty($this->pagetitle)) { $title = $this->pagetitle; } else if ($this->env['task'] == 'login') { $title = rcube_label(array('name' => 'welcome', 'vars' => array('product' => $this->config['product_name']))); } else { $title = ucfirst($this->env['task']); } return $title; } /** * Set skin */ public function set_skin($skin) { $valid = false; if (!empty($skin) && is_dir('skins/'.$skin) && is_readable('skins/'.$skin)) { $skin_path = 'skins/'.$skin; $valid = true; } else { $skin_path = $this->config['skin_path'] ? $this->config['skin_path'] : 'skins/'.rcube_config::DEFAULT_SKIN; $valid = !$skin; } $this->app->config->set('skin_path', $skin_path); $this->config['skin_path'] = $skin_path; return $valid; } /** * Getter for the current skin path property */ public function get_skin_path() { return $this->config['skin_path']; } /** * Check if a specific template exists * * @param string Template name * @return boolean True if template exists */ public function template_exists($name) { $filename = $this->config['skin_path'] . '/templates/' . $name . '.html'; return (is_file($filename) && is_readable($filename)) || ($this->deprecated_templates[$name] && $this->template_exists($this->deprecated_templates[$name])); } /** * Register a template object handler * * @param string Object name * @param string Function name to call * @return void */ public function add_handler($obj, $func) { $this->object_handlers[$obj] = $func; } /** * Register a list of template object handlers * * @param array Hash array with object=>handler pairs * @return void */ public function add_handlers($arr) { $this->object_handlers = array_merge($this->object_handlers, $arr); } /** * Register a GUI object to the client script * * @param string Object name * @param string Object ID * @return void */ public function add_gui_object($obj, $id) { $this->add_script(JS_OBJECT_NAME.".gui_object('$obj', '$id');"); } /** * Call a client method * * @param string Method to call * @param ... Additional arguments */ public function command() { $cmd = func_get_args(); if (strpos($cmd[0], 'plugin.') !== false) $this->js_commands[] = array('triggerEvent', $cmd[0], $cmd[1]); else $this->js_commands[] = $cmd; } /** * Add a localized label to the client environment */ public function add_label() { $args = func_get_args(); if (count($args) == 1 && is_array($args[0])) $args = $args[0]; foreach ($args as $name) { $this->js_labels[$name] = rcube_label($name); } } /** * Invoke display_message command * * @param string $message Message to display * @param string $type Message type [notice|confirm|error] * @param array $vars Key-value pairs to be replaced in localized text * @param boolean $override Override last set message * @param int $timeout Message display time in seconds * @uses self::command() */ public function show_message($message, $type='notice', $vars=null, $override=true, $timeout=0) { if ($override || !$this->message) { if (rcube_label_exists($message)) { if (!empty($vars)) $vars = array_map('Q', $vars); $msgtext = rcube_label(array('name' => $message, 'vars' => $vars)); } else $msgtext = $message; $this->message = $message; $this->command('display_message', $msgtext, $type, $timeout * 1000); } } /** * Delete all stored env variables and commands * * @return void * @uses rcube_html::reset() * @uses self::$env * @uses self::$js_env * @uses self::$js_commands * @uses self::$object_handlers */ public function reset() { $this->env = array(); $this->js_env = array(); $this->js_labels = array(); $this->js_commands = array(); $this->object_handlers = array(); parent::reset(); } /** * Redirect to a certain url * * @param mixed Either a string with the action or url parameters as key-value pairs * @see rcmail::url() */ public function redirect($p = array()) { $location = $this->app->url($p); header('Location: ' . $location); exit; } /** * Send the request output to the client. * This will either parse a skin tempalte or send an AJAX response * * @param string Template name * @param boolean True if script should terminate (default) */ public function send($templ = null, $exit = true) { if ($templ != 'iframe') { // prevent from endless loops if ($exit != 'recur' && $this->app->plugins->is_processing('render_page')) { raise_error(array('code' => 505, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => 'Recursion alert: ignoring output->send()'), true, false); return; } $this->parse($templ, false); } else { $this->framed = $templ == 'iframe' ? true : $this->framed; $this->write(); } // set output asap ob_flush(); flush(); if ($exit) { exit; } } /** * Process template and write to stdOut * * @param string HTML template * @see rcube_html_page::write() * @override */ public function write($template = '') { // unlock interface after iframe load $unlock = preg_replace('/[^a-z0-9]/i', '', $_REQUEST['_unlock']); if ($this->framed) { array_unshift($this->js_commands, array('set_busy', false, null, $unlock)); } else if ($unlock) { array_unshift($this->js_commands, array('hide_message', $unlock)); } if (!empty($this->script_files)) $this->set_env('request_token', $this->app->get_request_token()); // write all env variables to client $js = $this->framed ? "if(window.parent) {\n" : ''; $js .= $this->get_js_commands() . ($this->framed ? ' }' : ''); $this->add_script($js, 'head_top'); // send clickjacking protection headers $iframe = $this->framed || !empty($_REQUEST['_framed']); if (!headers_sent() && ($xframe = $this->app->config->get('x_frame_options', 'sameorigin'))) header('X-Frame-Options: ' . ($iframe && $xframe == 'deny' ? 'sameorigin' : $xframe)); // call super method parent::write($template, $this->config['skin_path']); } /** * Parse a specific skin template and deliver to stdout (or return) * * @param string Template name * @param boolean Exit script * @param boolean Don't write to stdout, return parsed content instead * * @link http://php.net/manual/en/function.exit.php */ function parse($name = 'main', $exit = true, $write = true) { $skin_path = $this->config['skin_path']; $plugin = false; $realname = $name; $temp = explode('.', $name, 2); $this->plugin_skin_path = null; $this->template_name = $realname; if (count($temp) > 1) { $plugin = $temp[0]; $name = $temp[1]; $skin_dir = $plugin . '/skins/' . $this->config['skin']; $skin_path = $this->plugin_skin_path = $this->app->plugins->dir . $skin_dir; // fallback to default skin if (!is_dir($skin_path)) { $skin_dir = $plugin . '/skins/default'; $skin_path = $this->plugin_skin_path = $this->app->plugins->dir . $skin_dir; } } $path = "$skin_path/templates/$name.html"; if (!is_readable($path) && $this->deprecated_templates[$realname]) { $path = "$skin_path/templates/".$this->deprecated_templates[$realname].".html"; if (is_readable($path)) raise_error(array('code' => 502, 'type' => 'php', 'file' => __FILE__, 'line' => __LINE__, 'message' => "Using deprecated template '".$this->deprecated_templates[$realname] ."' in ".$this->config['skin_path']."/templates. Please rename to '".$realname."'"), true, false); } // read template file if (($templ = @file_get_contents($path)) === false) { raise_error(array( 'code' => 501, 'type' => 'php', 'line' => __LINE__, 'file' => __FILE__, 'message' => 'Error loading template for '.$realname ), true, true); return false; } // replace all path references to plugins/... with the configured plugins dir // and /this/ to the current plugin skin directory if ($plugin) { $templ = preg_replace(array('/\bplugins\//', '/(["\']?)\/this\//'), array($this->app->plugins->url, '\\1'.$this->app->plugins->url.$skin_dir.'/'), $templ); } // parse for specialtags $output = $this->parse_conditions($templ); $output = $this->parse_xml($output); // trigger generic hook where plugins can put additional content to the page $hook = $this->app->plugins->exec_hook("render_page", array('template' => $realname, 'content' => $output)); // save some memory $output = $hook['content']; unset($hook['content']); $output = $this->parse_with_globals($output); // make sure all