rcmail_output_html.php 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/include/rcmail_output_html.php |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2006-2014, The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. | |
  13. | PURPOSE: |
  14. | Class to handle HTML page output using a skin template. |
  15. | |
  16. +-----------------------------------------------------------------------+
  17. | Author: Thomas Bruederli <roundcube@gmail.com> |
  18. +-----------------------------------------------------------------------+
  19. */
  20. /**
  21. * Class to create HTML page output using a skin template
  22. *
  23. * @package Webmail
  24. * @subpackage View
  25. */
  26. class rcmail_output_html extends rcmail_output
  27. {
  28. public $type = 'html';
  29. protected $message;
  30. protected $template_name;
  31. protected $objects = array();
  32. protected $js_env = array();
  33. protected $js_labels = array();
  34. protected $js_commands = array();
  35. protected $skin_paths = array();
  36. protected $scripts_path = '';
  37. protected $script_files = array();
  38. protected $css_files = array();
  39. protected $scripts = array();
  40. protected $default_template = "<html>\n<head><title></title></head>\n<body></body>\n</html>";
  41. protected $header = '';
  42. protected $footer = '';
  43. protected $body = '';
  44. protected $base_path = '';
  45. protected $assets_path;
  46. protected $assets_dir = RCUBE_INSTALL_PATH;
  47. protected $devel_mode = false;
  48. // deprecated names of templates used before 0.5
  49. protected $deprecated_templates = array(
  50. 'contact' => 'showcontact',
  51. 'contactadd' => 'addcontact',
  52. 'contactedit' => 'editcontact',
  53. 'identityedit' => 'editidentity',
  54. 'messageprint' => 'printmessage',
  55. );
  56. /**
  57. * Constructor
  58. */
  59. public function __construct($task = null, $framed = false)
  60. {
  61. parent::__construct();
  62. $this->devel_mode = $this->config->get('devel_mode');
  63. $this->set_env('task', $task);
  64. $this->set_env('x_frame_options', $this->config->get('x_frame_options', 'sameorigin'));
  65. $this->set_env('standard_windows', (bool) $this->config->get('standard_windows'));
  66. $this->set_env('locale', $_SESSION['language']);
  67. $this->set_env('devel_mode', $this->devel_mode);
  68. // add cookie info
  69. $this->set_env('cookie_domain', ini_get('session.cookie_domain'));
  70. $this->set_env('cookie_path', ini_get('session.cookie_path'));
  71. $this->set_env('cookie_secure', filter_var(ini_get('session.cookie_secure'), FILTER_VALIDATE_BOOLEAN));
  72. // load the correct skin (in case user-defined)
  73. $skin = $this->config->get('skin');
  74. $this->set_skin($skin);
  75. $this->set_env('skin', $skin);
  76. $this->set_assets_path($this->config->get('assets_path'), $this->config->get('assets_dir'));
  77. if (!empty($_REQUEST['_extwin']))
  78. $this->set_env('extwin', 1);
  79. if ($this->framed || $framed)
  80. $this->set_env('framed', 1);
  81. $lic = <<<EOF
  82. /*
  83. @licstart The following is the entire license notice for the
  84. JavaScript code in this page.
  85. Copyright (C) 2005-2014 The Roundcube Dev Team
  86. The JavaScript code in this page is free software: you can redistribute
  87. it and/or modify it under the terms of the GNU General Public License
  88. as published by the Free Software Foundation, either version 3 of
  89. the License, or (at your option) any later version.
  90. The code is distributed WITHOUT ANY WARRANTY; without even the implied
  91. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  92. See the GNU GPL for more details.
  93. @licend The above is the entire license notice
  94. for the JavaScript code in this page.
  95. */
  96. EOF;
  97. // add common javascripts
  98. $this->add_script($lic, 'head_top');
  99. $this->add_script('var '.self::JS_OBJECT_NAME.' = new rcube_webmail();', 'head_top');
  100. // don't wait for page onload. Call init at the bottom of the page (delayed)
  101. $this->add_script(self::JS_OBJECT_NAME.'.init();', 'docready');
  102. $this->scripts_path = 'program/js/';
  103. $this->include_script('jquery.min.js');
  104. $this->include_script('common.js');
  105. $this->include_script('app.js');
  106. // register common UI objects
  107. $this->add_handlers(array(
  108. 'loginform' => array($this, 'login_form'),
  109. 'preloader' => array($this, 'preloader'),
  110. 'username' => array($this, 'current_username'),
  111. 'message' => array($this, 'message_container'),
  112. 'charsetselector' => array($this, 'charset_selector'),
  113. 'aboutcontent' => array($this, 'about_content'),
  114. ));
  115. }
  116. /**
  117. * Set environment variable
  118. *
  119. * @param string $name Property name
  120. * @param mixed $value Property value
  121. * @param boolean $addtojs True if this property should be added
  122. * to client environment
  123. */
  124. public function set_env($name, $value, $addtojs = true)
  125. {
  126. $this->env[$name] = $value;
  127. if ($addtojs || isset($this->js_env[$name])) {
  128. $this->js_env[$name] = $value;
  129. }
  130. }
  131. /**
  132. * Parse and set assets path
  133. *
  134. * @param string $path Assets path URL (relative or absolute)
  135. * @param string $fs_dif Assets path in filesystem
  136. */
  137. public function set_assets_path($path, $fs_dir = null)
  138. {
  139. if (empty($path)) {
  140. return;
  141. }
  142. $path = rtrim($path, '/') . '/';
  143. // handle relative assets path
  144. if (!preg_match('|^https?://|', $path) && $path[0] != '/') {
  145. // save the path to search for asset files later
  146. $this->assets_dir = $path;
  147. $base = preg_replace('/[?#&].*$/', '', $_SERVER['REQUEST_URI']);
  148. $base = rtrim($base, '/');
  149. // remove url token if exists
  150. if ($len = intval($this->config->get('use_secure_urls'))) {
  151. $_base = explode('/', $base);
  152. $last = count($_base) - 1;
  153. $length = $len > 1 ? $len : 16; // as in rcube::get_secure_url_token()
  154. // we can't use real token here because it
  155. // does not exists in unauthenticated state,
  156. // hope this will not produce false-positive matches
  157. if ($last > -1 && preg_match('/^[a-f0-9]{' . $length . '}$/', $_base[$last])) {
  158. $path = '../' . $path;
  159. }
  160. }
  161. }
  162. // set filesystem path for assets
  163. if ($fs_dir) {
  164. if ($fs_dir[0] != '/') {
  165. $fs_dir = realpath(RCUBE_INSTALL_PATH . $fs_dir);
  166. }
  167. // ensure the path ends with a slash
  168. $this->assets_dir = rtrim($fs_dir, '/') . '/';
  169. }
  170. $this->assets_path = $path;
  171. $this->set_env('assets_path', $path);
  172. }
  173. /**
  174. * Getter for the current page title
  175. *
  176. * @return string The page title
  177. */
  178. protected function get_pagetitle()
  179. {
  180. if (!empty($this->pagetitle)) {
  181. $title = $this->pagetitle;
  182. }
  183. else if ($this->env['task'] == 'login') {
  184. $title = $this->app->gettext(array(
  185. 'name' => 'welcome',
  186. 'vars' => array('product' => $this->config->get('product_name')
  187. )));
  188. }
  189. else {
  190. $title = ucfirst($this->env['task']);
  191. }
  192. return $title;
  193. }
  194. /**
  195. * Set skin
  196. *
  197. * @param string $skin Skin name
  198. *
  199. * @return bool True if the skin exist and is readable, False otherwise
  200. */
  201. public function set_skin($skin)
  202. {
  203. // Sanity check to prevent from path traversal vulnerability (#1490620)
  204. if (strpos($skin, '/') !== false || strpos($skin, "\\") !== false) {
  205. rcube::raise_error(array(
  206. 'file' => __FILE__,
  207. 'line' => __LINE__,
  208. 'message' => 'Invalid skin name'
  209. ), true, false);
  210. return false;
  211. }
  212. $valid = false;
  213. $path = RCUBE_INSTALL_PATH . 'skins/';
  214. if (!empty($skin) && is_dir($path . $skin) && is_readable($path . $skin)) {
  215. $skin_path = 'skins/' . $skin;
  216. $valid = true;
  217. }
  218. else {
  219. $skin_path = $this->config->get('skin_path');
  220. if (!$skin_path) {
  221. $skin_path = 'skins/' . rcube_config::DEFAULT_SKIN;
  222. }
  223. $valid = !$skin;
  224. }
  225. $skin_path = rtrim($skin_path, '/');
  226. $this->config->set('skin_path', $skin_path);
  227. $this->base_path = $skin_path;
  228. // register skin path(s)
  229. $this->skin_paths = array();
  230. $this->load_skin($skin_path);
  231. return $valid;
  232. }
  233. /**
  234. * Helper method to recursively read skin meta files and register search paths
  235. */
  236. private function load_skin($skin_path)
  237. {
  238. $this->skin_paths[] = $skin_path;
  239. // read meta file and check for dependecies
  240. $meta = @file_get_contents(RCUBE_INSTALL_PATH . $skin_path . '/meta.json');
  241. $meta = @json_decode($meta, true);
  242. $meta['path'] = $skin_path;
  243. $path_elements = explode('/', $skin_path);
  244. $skin_id = end($path_elements);
  245. if (!$meta['name']) {
  246. $meta['name'] = $skin_id;
  247. }
  248. $this->skins[$skin_id] = $meta;
  249. if ($meta['extends']) {
  250. $path = RCUBE_INSTALL_PATH . 'skins/';
  251. if (is_dir($path . $meta['extends']) && is_readable($path . $meta['extends'])) {
  252. $this->load_skin('skins/' . $meta['extends']);
  253. }
  254. }
  255. }
  256. /**
  257. * Check if a specific template exists
  258. *
  259. * @param string $name Template name
  260. *
  261. * @return boolean True if template exists
  262. */
  263. public function template_exists($name)
  264. {
  265. foreach ($this->skin_paths as $skin_path) {
  266. $filename = RCUBE_INSTALL_PATH . $skin_path . '/templates/' . $name . '.html';
  267. if ((is_file($filename) && is_readable($filename))
  268. || ($this->deprecated_templates[$name] && $this->template_exists($this->deprecated_templates[$name]))
  269. ) {
  270. return true;
  271. }
  272. }
  273. return false;
  274. }
  275. /**
  276. * Find the given file in the current skin path stack
  277. *
  278. * @param string $file File name/path to resolve (starting with /)
  279. * @param string &$skin_path Reference to the base path of the matching skin
  280. * @param string $add_path Additional path to search in
  281. *
  282. * @return mixed Relative path to the requested file or False if not found
  283. */
  284. public function get_skin_file($file, &$skin_path = null, $add_path = null)
  285. {
  286. $skin_paths = $this->skin_paths;
  287. if ($add_path) {
  288. array_unshift($skin_paths, $add_path);
  289. }
  290. foreach ($skin_paths as $skin_path) {
  291. $path = realpath(RCUBE_INSTALL_PATH . $skin_path . $file);
  292. if ($path && is_file($path)) {
  293. return $skin_path . $file;
  294. }
  295. if ($this->assets_dir != RCUBE_INSTALL_PATH) {
  296. $path = realpath($this->assets_dir . $skin_path . $file);
  297. if ($path && is_file($path)) {
  298. return $skin_path . $file;
  299. }
  300. }
  301. }
  302. return false;
  303. }
  304. /**
  305. * Register a GUI object to the client script
  306. *
  307. * @param string $obj Object name
  308. * @param string $id Object ID
  309. */
  310. public function add_gui_object($obj, $id)
  311. {
  312. $this->add_script(self::JS_OBJECT_NAME.".gui_object('$obj', '$id');");
  313. }
  314. /**
  315. * Call a client method
  316. *
  317. * @param string Method to call
  318. * @param ... Additional arguments
  319. */
  320. public function command()
  321. {
  322. $cmd = func_get_args();
  323. if (strpos($cmd[0], 'plugin.') !== false)
  324. $this->js_commands[] = array('triggerEvent', $cmd[0], $cmd[1]);
  325. else
  326. $this->js_commands[] = $cmd;
  327. }
  328. /**
  329. * Add a localized label to the client environment
  330. */
  331. public function add_label()
  332. {
  333. $args = func_get_args();
  334. if (count($args) == 1 && is_array($args[0])) {
  335. $args = $args[0];
  336. }
  337. foreach ($args as $name) {
  338. $this->js_labels[$name] = $this->app->gettext($name);
  339. }
  340. }
  341. /**
  342. * Invoke display_message command
  343. *
  344. * @param string $message Message to display
  345. * @param string $type Message type [notice|confirm|error]
  346. * @param array $vars Key-value pairs to be replaced in localized text
  347. * @param boolean $override Override last set message
  348. * @param int $timeout Message display time in seconds
  349. *
  350. * @uses self::command()
  351. */
  352. public function show_message($message, $type='notice', $vars=null, $override=true, $timeout=0)
  353. {
  354. if ($override || !$this->message) {
  355. if ($this->app->text_exists($message)) {
  356. if (!empty($vars))
  357. $vars = array_map(array('rcube','Q'), $vars);
  358. $msgtext = $this->app->gettext(array('name' => $message, 'vars' => $vars));
  359. }
  360. else
  361. $msgtext = $message;
  362. $this->message = $message;
  363. $this->command('display_message', $msgtext, $type, $timeout * 1000);
  364. }
  365. }
  366. /**
  367. * Delete all stored env variables and commands
  368. *
  369. * @param bool $all Reset all env variables (including internal)
  370. */
  371. public function reset($all = false)
  372. {
  373. $framed = $this->framed;
  374. $env = $all ? null : array_intersect_key($this->env, array('extwin'=>1, 'framed'=>1));
  375. parent::reset();
  376. // let some env variables survive
  377. $this->env = $this->js_env = $env;
  378. $this->framed = $framed || $this->env['framed'];
  379. $this->js_labels = array();
  380. $this->js_commands = array();
  381. $this->script_files = array();
  382. $this->scripts = array();
  383. $this->header = '';
  384. $this->footer = '';
  385. $this->body = '';
  386. // load defaults
  387. if (!$all) {
  388. $this->__construct();
  389. }
  390. }
  391. /**
  392. * Redirect to a certain url
  393. *
  394. * @param mixed $p Either a string with the action or url parameters as key-value pairs
  395. * @param int $delay Delay in seconds
  396. * @param bool $secure Redirect to secure location (see rcmail::url())
  397. */
  398. public function redirect($p = array(), $delay = 1, $secure = false)
  399. {
  400. if ($this->env['extwin'])
  401. $p['extwin'] = 1;
  402. $location = $this->app->url($p, false, false, $secure);
  403. header('Location: ' . $location);
  404. exit;
  405. }
  406. /**
  407. * Send the request output to the client.
  408. * This will either parse a skin tempalte or send an AJAX response
  409. *
  410. * @param string $templ Template name
  411. * @param boolean $exit True if script should terminate (default)
  412. */
  413. public function send($templ = null, $exit = true)
  414. {
  415. if ($templ != 'iframe') {
  416. // prevent from endless loops
  417. if ($exit != 'recur' && $this->app->plugins->is_processing('render_page')) {
  418. rcube::raise_error(array('code' => 505, 'type' => 'php',
  419. 'file' => __FILE__, 'line' => __LINE__,
  420. 'message' => 'Recursion alert: ignoring output->send()'), true, false);
  421. return;
  422. }
  423. $this->parse($templ, false);
  424. }
  425. else {
  426. $this->framed = true;
  427. $this->write();
  428. }
  429. // set output asap
  430. ob_flush();
  431. flush();
  432. if ($exit) {
  433. exit;
  434. }
  435. }
  436. /**
  437. * Process template and write to stdOut
  438. *
  439. * @param string $template HTML template content
  440. */
  441. public function write($template = '')
  442. {
  443. if (!empty($this->script_files)) {
  444. $this->set_env('request_token', $this->app->get_request_token());
  445. }
  446. $commands = $this->get_js_commands($framed);
  447. // if all js commands go to parent window we can ignore all
  448. // script files and skip rcube_webmail initialization (#1489792)
  449. if ($framed) {
  450. $this->scripts = array();
  451. $this->script_files = array();
  452. $this->header = '';
  453. $this->footer = '';
  454. }
  455. // write all javascript commands
  456. $this->add_script($commands, 'head_top');
  457. // allow (legal) iframe content to be loaded
  458. $iframe = $this->framed || $this->env['framed'];
  459. if (!headers_sent() && $iframe && ($xopt = $this->app->config->get('x_frame_options', 'sameorigin'))) {
  460. if (strtolower($xopt) != 'sameorigin') {
  461. header('X-Frame-Options: sameorigin', true);
  462. }
  463. }
  464. // call super method
  465. $this->_write($template, $this->config->get('skin_path'));
  466. }
  467. /**
  468. * Parse a specific skin template and deliver to stdout (or return)
  469. *
  470. * @param string $name Template name
  471. * @param boolean $exit Exit script
  472. * @param boolean $write Don't write to stdout, return parsed content instead
  473. *
  474. * @link http://php.net/manual/en/function.exit.php
  475. */
  476. function parse($name = 'main', $exit = true, $write = true)
  477. {
  478. $plugin = false;
  479. $realname = $name;
  480. $plugin_skin_paths = array();
  481. $this->template_name = $realname;
  482. $temp = explode('.', $name, 2);
  483. if (count($temp) > 1) {
  484. $plugin = $temp[0];
  485. $name = $temp[1];
  486. $skin_dir = $plugin . '/skins/' . $this->config->get('skin');
  487. // apply skin search escalation list to plugin directory
  488. foreach ($this->skin_paths as $skin_path) {
  489. $plugin_skin_paths[] = $this->app->plugins->url . $plugin . '/' . $skin_path;
  490. }
  491. // add fallback to default skin
  492. if (is_dir($this->app->plugins->dir . $plugin . '/skins/default')) {
  493. $skin_dir = $plugin . '/skins/default';
  494. $plugin_skin_paths[] = $this->app->plugins->url . $skin_dir;
  495. }
  496. // prepend plugin skin paths to search list
  497. $this->skin_paths = array_merge($plugin_skin_paths, $this->skin_paths);
  498. }
  499. // find skin template
  500. $path = false;
  501. foreach ($this->skin_paths as $skin_path) {
  502. $path = RCUBE_INSTALL_PATH . "$skin_path/templates/$name.html";
  503. // fallback to deprecated template names
  504. if (!is_readable($path) && ($dname = $this->deprecated_templates[$realname])) {
  505. $path = RCUBE_INSTALL_PATH . "$skin_path/templates/$dname.html";
  506. if (is_readable($path)) {
  507. rcube::raise_error(array(
  508. 'code' => 502, 'file' => __FILE__, 'line' => __LINE__,
  509. 'message' => "Using deprecated template '$dname' in $skin_path/templates. Please rename to '$realname'"
  510. ), true, false);
  511. }
  512. }
  513. if (is_readable($path)) {
  514. $this->config->set('skin_path', $skin_path);
  515. // set base_path to core skin directory (not plugin's skin)
  516. $this->base_path = preg_replace('!plugins/\w+/!', '', $skin_path);
  517. $skin_dir = preg_replace('!^plugins/!', '', $skin_path);
  518. break;
  519. }
  520. else {
  521. $path = false;
  522. }
  523. }
  524. // read template file
  525. if (!$path || ($templ = @file_get_contents($path)) === false) {
  526. rcube::raise_error(array(
  527. 'code' => 404,
  528. 'type' => 'php',
  529. 'line' => __LINE__,
  530. 'file' => __FILE__,
  531. 'message' => 'Error loading template for '.$realname
  532. ), true, $write);
  533. $this->skin_paths = array_slice($this->skin_paths, count($plugin_skin_paths));
  534. return false;
  535. }
  536. // replace all path references to plugins/... with the configured plugins dir
  537. // and /this/ to the current plugin skin directory
  538. if ($plugin) {
  539. $templ = preg_replace(
  540. array('/\bplugins\//', '/(["\']?)\/this\//'),
  541. array($this->app->plugins->url, '\\1'.$this->app->plugins->url.$skin_dir.'/'),
  542. $templ
  543. );
  544. }
  545. // parse for specialtags
  546. $output = $this->parse_conditions($templ);
  547. $output = $this->parse_xml($output);
  548. // trigger generic hook where plugins can put additional content to the page
  549. $hook = $this->app->plugins->exec_hook("render_page", array('template' => $realname, 'content' => $output));
  550. // save some memory
  551. $output = $hook['content'];
  552. unset($hook['content']);
  553. // remove plugin skin paths from current context
  554. $this->skin_paths = array_slice($this->skin_paths, count($plugin_skin_paths));
  555. if (!$write) {
  556. return $this->postrender($output);
  557. }
  558. $this->write(trim($output));
  559. if ($exit) {
  560. exit;
  561. }
  562. }
  563. /**
  564. * Return executable javascript code for all registered commands
  565. */
  566. protected function get_js_commands(&$framed = null)
  567. {
  568. $out = '';
  569. $parent_commands = 0;
  570. $top_commands = array();
  571. // these should be always on top,
  572. // e.g. hide_message() below depends on env.framed
  573. if (!$this->framed && !empty($this->js_env)) {
  574. $top_commands[] = array('set_env', $this->js_env);
  575. }
  576. if (!empty($this->js_labels)) {
  577. $top_commands[] = array('add_label', $this->js_labels);
  578. }
  579. // unlock interface after iframe load
  580. $unlock = preg_replace('/[^a-z0-9]/i', '', $_REQUEST['_unlock']);
  581. if ($this->framed) {
  582. $top_commands[] = array('iframe_loaded', $unlock);
  583. }
  584. else if ($unlock) {
  585. $top_commands[] = array('hide_message', $unlock);
  586. }
  587. $commands = array_merge($top_commands, $this->js_commands);
  588. foreach ($commands as $i => $args) {
  589. $method = array_shift($args);
  590. $parent = $this->framed || preg_match('/^parent\./', $method);
  591. foreach ($args as $i => $arg) {
  592. $args[$i] = self::json_serialize($arg, $this->devel_mode);
  593. }
  594. if ($parent) {
  595. $parent_commands++;
  596. $method = preg_replace('/^parent\./', '', $method);
  597. $parent_prefix = 'if (window.parent && parent.' . self::JS_OBJECT_NAME . ') parent.';
  598. $method = $parent_prefix . self::JS_OBJECT_NAME . '.' . $method;
  599. }
  600. else {
  601. $method = self::JS_OBJECT_NAME . '.' . $method;
  602. }
  603. $out .= sprintf("%s(%s);\n", $method, implode(',', $args));
  604. }
  605. $framed = $parent_prefix && $parent_commands == count($commands);
  606. // make the output more compact if all commands go to parent window
  607. if ($framed) {
  608. $out = "if (window.parent && parent." . self::JS_OBJECT_NAME . ") {\n"
  609. . str_replace($parent_prefix, "\tparent.", $out)
  610. . "}\n";
  611. }
  612. return $out;
  613. }
  614. /**
  615. * Make URLs starting with a slash point to skin directory
  616. *
  617. * @param string $str Input string
  618. * @param bool $search_path True if URL should be resolved using the current skin path stack
  619. *
  620. * @return string URL
  621. */
  622. public function abs_url($str, $search_path = false)
  623. {
  624. if ($str[0] == '/') {
  625. if ($search_path && ($file_url = $this->get_skin_file($str, $skin_path))) {
  626. return $file_url;
  627. }
  628. return $this->base_path . $str;
  629. }
  630. return $str;
  631. }
  632. /**
  633. * Show error page and terminate script execution
  634. *
  635. * @param int $code Error code
  636. * @param string $message Error message
  637. */
  638. public function raise_error($code, $message)
  639. {
  640. global $__page_content, $ERROR_CODE, $ERROR_MESSAGE;
  641. $ERROR_CODE = $code;
  642. $ERROR_MESSAGE = $message;
  643. include RCUBE_INSTALL_PATH . 'program/steps/utils/error.inc';
  644. exit;
  645. }
  646. /**
  647. * Modify path by adding URL prefix if configured
  648. */
  649. public function asset_url($path)
  650. {
  651. // iframe content can't be in a different domain
  652. // @TODO: check if assests are on a different domain
  653. if (!$this->assets_path || in_array($path[0], array('?', '/', '.')) || strpos($path, '://')) {
  654. return $path;
  655. }
  656. return $this->assets_path . $path;
  657. }
  658. /***** Template parsing methods *****/
  659. /**
  660. * Replace all strings ($varname)
  661. * with the content of the according global variable.
  662. */
  663. protected function parse_with_globals($input)
  664. {
  665. $GLOBALS['__version'] = html::quote(RCMAIL_VERSION);
  666. $GLOBALS['__comm_path'] = html::quote($this->app->comm_path);
  667. $GLOBALS['__skin_path'] = html::quote($this->base_path);
  668. return preg_replace_callback('/\$(__[a-z0-9_\-]+)/',
  669. array($this, 'globals_callback'), $input);
  670. }
  671. /**
  672. * Callback funtion for preg_replace_callback() in parse_with_globals()
  673. */
  674. protected function globals_callback($matches)
  675. {
  676. return $GLOBALS[$matches[1]];
  677. }
  678. /**
  679. * Correct absolute paths in images and other tags
  680. * add timestamp to .js and .css filename
  681. */
  682. protected function fix_paths($output)
  683. {
  684. return preg_replace_callback(
  685. '!(src|href|background)=(["\']?)([a-z0-9/_.-]+)(["\'\s>])!i',
  686. array($this, 'file_callback'), $output);
  687. }
  688. /**
  689. * Callback function for preg_replace_callback in fix_paths()
  690. *
  691. * @return string Parsed string
  692. */
  693. protected function file_callback($matches)
  694. {
  695. $file = $matches[3];
  696. $file = preg_replace('!^/this/!', '/', $file);
  697. // correct absolute paths
  698. if ($file[0] == '/') {
  699. $file = $this->base_path . $file;
  700. }
  701. // add file modification timestamp
  702. if (preg_match('/\.(js|css)$/', $file, $m)) {
  703. $file = $this->file_mod($file);
  704. }
  705. return $matches[1] . '=' . $matches[2] . $file . $matches[4];
  706. }
  707. /**
  708. * Correct paths of asset files according to assets_path
  709. */
  710. protected function fix_assets_paths($output)
  711. {
  712. return preg_replace_callback(
  713. '!(src|href|background)=(["\']?)([a-z0-9/_.?=-]+)(["\'\s>])!i',
  714. array($this, 'assets_callback'), $output);
  715. }
  716. /**
  717. * Callback function for preg_replace_callback in fix_assets_paths()
  718. *
  719. * @return string Parsed string
  720. */
  721. protected function assets_callback($matches)
  722. {
  723. $file = $this->asset_url($matches[3]);
  724. return $matches[1] . '=' . $matches[2] . $file . $matches[4];
  725. }
  726. /**
  727. * Modify file by adding mtime indicator
  728. */
  729. protected function file_mod($file)
  730. {
  731. $fs = false;
  732. $ext = substr($file, strrpos($file, '.') + 1);
  733. // use minified file if exists (not in development mode)
  734. if (!$this->devel_mode && !preg_match('/\.min\.' . $ext . '$/', $file)) {
  735. $minified_file = substr($file, 0, strlen($ext) * -1) . 'min.' . $ext;
  736. if ($fs = @filemtime($this->assets_dir . $minified_file)) {
  737. return $minified_file . '?s=' . $fs;
  738. }
  739. }
  740. if ($fs = @filemtime($this->assets_dir . $file)) {
  741. $file .= '?s=' . $fs;
  742. }
  743. return $file;
  744. }
  745. /**
  746. * Public wrapper to dipp into template parsing.
  747. *
  748. * @param string $input Template content
  749. *
  750. * @return string
  751. * @uses rcmail_output_html::parse_xml()
  752. * @since 0.1-rc1
  753. */
  754. public function just_parse($input)
  755. {
  756. $input = $this->parse_conditions($input);
  757. $input = $this->parse_xml($input);
  758. $input = $this->postrender($input);
  759. return $input;
  760. }
  761. /**
  762. * Parse for conditional tags
  763. */
  764. protected function parse_conditions($input)
  765. {
  766. $matches = preg_split('/<roundcube:(if|elseif|else|endif)\s+([^>]+)>\n?/is', $input, 2, PREG_SPLIT_DELIM_CAPTURE);
  767. if ($matches && count($matches) == 4) {
  768. if (preg_match('/^(else|endif)$/i', $matches[1])) {
  769. return $matches[0] . $this->parse_conditions($matches[3]);
  770. }
  771. $attrib = html::parse_attrib_string($matches[2]);
  772. if (isset($attrib['condition'])) {
  773. $condmet = $this->check_condition($attrib['condition']);
  774. $submatches = preg_split('/<roundcube:(elseif|else|endif)\s+([^>]+)>\n?/is', $matches[3], 2, PREG_SPLIT_DELIM_CAPTURE);
  775. if ($condmet) {
  776. $result = $submatches[0];
  777. if ($submatches[1] != 'endif') {
  778. $result .= preg_replace('/.*<roundcube:endif\s+[^>]+>\n?/Uis', '', $submatches[3], 1);
  779. }
  780. else {
  781. $result .= $submatches[3];
  782. }
  783. }
  784. else {
  785. $result = "<roundcube:$submatches[1] $submatches[2]>" . $submatches[3];
  786. }
  787. return $matches[0] . $this->parse_conditions($result);
  788. }
  789. rcube::raise_error(array(
  790. 'code' => 500, 'line' => __LINE__, 'file' => __FILE__,
  791. 'message' => "Unable to parse conditional tag " . $matches[2]
  792. ), true, false);
  793. }
  794. return $input;
  795. }
  796. /**
  797. * Determines if a given condition is met
  798. *
  799. * @param string $condition Condition statement
  800. *
  801. * @return boolean True if condition is met, False if not
  802. * @todo Extend this to allow real conditions, not just "set"
  803. */
  804. protected function check_condition($condition)
  805. {
  806. return $this->eval_expression($condition);
  807. }
  808. /**
  809. * Inserts hidden field with CSRF-prevention-token into POST forms
  810. */
  811. protected function alter_form_tag($matches)
  812. {
  813. $out = $matches[0];
  814. $attrib = html::parse_attrib_string($matches[1]);
  815. if (strtolower($attrib['method']) == 'post') {
  816. $hidden = new html_hiddenfield(array('name' => '_token', 'value' => $this->app->get_request_token()));
  817. $out .= "\n" . $hidden->show();
  818. }
  819. return $out;
  820. }
  821. /**
  822. * Parse & evaluate a given expression and return its result.
  823. *
  824. * @param string $expression Expression statement
  825. *
  826. * @return mixed Expression result
  827. */
  828. protected function eval_expression($expression)
  829. {
  830. $expression = preg_replace(
  831. array(
  832. '/session:([a-z0-9_]+)/i',
  833. '/config:([a-z0-9_]+)(:([a-z0-9_]+))?/i',
  834. '/env:([a-z0-9_]+)/i',
  835. '/request:([a-z0-9_]+)/i',
  836. '/cookie:([a-z0-9_]+)/i',
  837. '/browser:([a-z0-9_]+)/i',
  838. '/template:name/i',
  839. ),
  840. array(
  841. "\$_SESSION['\\1']",
  842. "\$app->config->get('\\1',rcube_utils::get_boolean('\\3'))",
  843. "\$env['\\1']",
  844. "rcube_utils::get_input_value('\\1', rcube_utils::INPUT_GPC)",
  845. "\$_COOKIE['\\1']",
  846. "\$browser->{'\\1'}",
  847. "'" . $this->template_name . "'",
  848. ),
  849. $expression
  850. );
  851. $fn = create_function('$app,$browser,$env', "return ($expression);");
  852. if (!$fn) {
  853. rcube::raise_error(array(
  854. 'code' => 505, 'file' => __FILE__, 'line' => __LINE__,
  855. 'message' => "Expression parse error on: ($expression)"
  856. ), true, false);
  857. return;
  858. }
  859. return $fn($this->app, $this->browser, $this->env);
  860. }
  861. /**
  862. * Search for special tags in input and replace them
  863. * with the appropriate content
  864. *
  865. * @param string $input Input string to parse
  866. *
  867. * @return string Altered input string
  868. * @todo Use DOM-parser to traverse template HTML
  869. * @todo Maybe a cache.
  870. */
  871. protected function parse_xml($input)
  872. {
  873. return preg_replace_callback('/<roundcube:([-_a-z]+)\s+((?:[^>]|\\\\>)+)(?<!\\\\)>/Ui', array($this, 'xml_command'), $input);
  874. }
  875. /**
  876. * Callback function for parsing an xml command tag
  877. * and turn it into real html content
  878. *
  879. * @param array $matches Matches array of preg_replace_callback
  880. *
  881. * @return string Tag/Object content
  882. */
  883. protected function xml_command($matches)
  884. {
  885. $command = strtolower($matches[1]);
  886. $attrib = html::parse_attrib_string($matches[2]);
  887. // empty output if required condition is not met
  888. if (!empty($attrib['condition']) && !$this->check_condition($attrib['condition'])) {
  889. return '';
  890. }
  891. // localize title and summary attributes
  892. if ($command != 'button' && !empty($attrib['title']) && $this->app->text_exists($attrib['title'])) {
  893. $attrib['title'] = $this->app->gettext($attrib['title']);
  894. }
  895. if ($command != 'button' && !empty($attrib['summary']) && $this->app->text_exists($attrib['summary'])) {
  896. $attrib['summary'] = $this->app->gettext($attrib['summary']);
  897. }
  898. // execute command
  899. switch ($command) {
  900. // return a button
  901. case 'button':
  902. if ($attrib['name'] || $attrib['command']) {
  903. return $this->button($attrib);
  904. }
  905. break;
  906. // frame
  907. case 'frame':
  908. return $this->frame($attrib);
  909. break;
  910. // show a label
  911. case 'label':
  912. if ($attrib['expression'])
  913. $attrib['name'] = $this->eval_expression($attrib['expression']);
  914. if ($attrib['name'] || $attrib['command']) {
  915. $vars = $attrib + array('product' => $this->config->get('product_name'));
  916. unset($vars['name'], $vars['command']);
  917. $label = $this->app->gettext($attrib + array('vars' => $vars));
  918. $quoting = !empty($attrib['quoting']) ? strtolower($attrib['quoting']) : (rcube_utils::get_boolean((string)$attrib['html']) ? 'no' : '');
  919. // 'noshow' can be used in skins to define new labels
  920. if ($attrib['noshow']) {
  921. return '';
  922. }
  923. switch ($quoting) {
  924. case 'no':
  925. case 'raw':
  926. break;
  927. case 'javascript':
  928. case 'js':
  929. $label = rcube::JQ($label);
  930. break;
  931. default:
  932. $label = html::quote($label);
  933. break;
  934. }
  935. return $label;
  936. }
  937. break;
  938. case 'add_label':
  939. $this->add_label($attrib['name']);
  940. break;
  941. // include a file
  942. case 'include':
  943. $old_base_path = $this->base_path;
  944. if (!empty($attrib['skin_path'])) $attrib['skinpath'] = $attrib['skin_path'];
  945. if ($path = $this->get_skin_file($attrib['file'], $skin_path, $attrib['skinpath'])) {
  946. // set base_path to core skin directory (not plugin's skin)
  947. $this->base_path = preg_replace('!plugins/\w+/!', '', $skin_path);
  948. $path = realpath(RCUBE_INSTALL_PATH . $path);
  949. }
  950. if (is_readable($path)) {
  951. if ($this->config->get('skin_include_php')) {
  952. $incl = $this->include_php($path);
  953. }
  954. else {
  955. $incl = file_get_contents($path);
  956. }
  957. $incl = $this->parse_conditions($incl);
  958. $incl = $this->parse_xml($incl);
  959. $incl = $this->fix_paths($incl);
  960. $this->base_path = $old_base_path;
  961. return $incl;
  962. }
  963. break;
  964. case 'plugin.include':
  965. $hook = $this->app->plugins->exec_hook("template_plugin_include", $attrib);
  966. return $hook['content'];
  967. // define a container block
  968. case 'container':
  969. if ($attrib['name'] && $attrib['id']) {
  970. $this->command('gui_container', $attrib['name'], $attrib['id']);
  971. // let plugins insert some content here
  972. $hook = $this->app->plugins->exec_hook("template_container", $attrib);
  973. return $hook['content'];
  974. }
  975. break;
  976. // return code for a specific application object
  977. case 'object':
  978. $object = strtolower($attrib['name']);
  979. $content = '';
  980. // we are calling a class/method
  981. if (($handler = $this->object_handlers[$object]) && is_array($handler)) {
  982. if ((is_object($handler[0]) && method_exists($handler[0], $handler[1])) ||
  983. (is_string($handler[0]) && class_exists($handler[0])))
  984. $content = call_user_func($handler, $attrib);
  985. $external = true;
  986. }
  987. // execute object handler function
  988. else if (function_exists($handler)) {
  989. $content = call_user_func($handler, $attrib);
  990. }
  991. else if ($object == 'doctype') {
  992. $content = html::doctype($attrib['value']);
  993. }
  994. else if ($object == 'logo') {
  995. $attrib += array('alt' => $this->xml_command(array('', 'object', 'name="productname"')));
  996. if ($logo = $this->config->get('skin_logo')) {
  997. if (is_array($logo)) {
  998. if ($template_logo = $logo[$this->template_name]) {
  999. $attrib['src'] = $template_logo;
  1000. }
  1001. elseif ($template_logo = $logo['*']) {
  1002. $attrib['src'] = $template_logo;
  1003. }
  1004. }
  1005. else {
  1006. $attrib['src'] = $logo;
  1007. }
  1008. }
  1009. $content = html::img($attrib);
  1010. }
  1011. else if ($object == 'productname') {
  1012. $name = $this->config->get('product_name', 'Roundcube Webmail');
  1013. $content = html::quote($name);
  1014. }
  1015. else if ($object == 'version') {
  1016. $ver = (string)RCMAIL_VERSION;
  1017. if (is_file(RCUBE_INSTALL_PATH . '.svn/entries')) {
  1018. if (preg_match('/Revision:\s(\d+)/', @shell_exec('svn info'), $regs))
  1019. $ver .= ' [SVN r'.$regs[1].']';
  1020. }
  1021. else if (is_file(RCUBE_INSTALL_PATH . '.git/index')) {
  1022. if (preg_match('/Date:\s+([^\n]+)/', @shell_exec('git log -1'), $regs)) {
  1023. if ($date = date('Ymd.Hi', strtotime($regs[1]))) {
  1024. $ver .= ' [GIT '.$date.']';
  1025. }
  1026. }
  1027. }
  1028. $content = html::quote($ver);
  1029. }
  1030. else if ($object == 'steptitle') {
  1031. $content = html::quote($this->get_pagetitle());
  1032. }
  1033. else if ($object == 'pagetitle') {
  1034. if ($this->devel_mode && !empty($_SESSION['username']))
  1035. $title = $_SESSION['username'].' :: ';
  1036. else if ($prod_name = $this->config->get('product_name'))
  1037. $title = $prod_name . ' :: ';
  1038. else
  1039. $title = '';
  1040. $title .= $this->get_pagetitle();
  1041. $content = html::quote($title);
  1042. }
  1043. // exec plugin hooks for this template object
  1044. $hook = $this->app->plugins->exec_hook("template_object_$object", $attrib + array('content' => $content));
  1045. if (strlen($hook['content']) && !empty($external)) {
  1046. $object_id = uniqid('TEMPLOBJECT:', true);
  1047. $this->objects[$object_id] = $hook['content'];
  1048. $hook['content'] = $object_id;
  1049. }
  1050. return $hook['content'];
  1051. // return code for a specified eval expression
  1052. case 'exp':
  1053. return html::quote($this->eval_expression($attrib['expression']));
  1054. // return variable
  1055. case 'var':
  1056. $var = explode(':', $attrib['name']);
  1057. $name = $var[1];
  1058. $value = '';
  1059. switch ($var[0]) {
  1060. case 'env':
  1061. $value = $this->env[$name];
  1062. break;
  1063. case 'config':
  1064. $value = $this->config->get($name);
  1065. if (is_array($value) && $value[$_SESSION['storage_host']]) {
  1066. $value = $value[$_SESSION['storage_host']];
  1067. }
  1068. break;
  1069. case 'request':
  1070. $value = rcube_utils::get_input_value($name, rcube_utils::INPUT_GPC);
  1071. break;
  1072. case 'session':
  1073. $value = $_SESSION[$name];
  1074. break;
  1075. case 'cookie':
  1076. $value = htmlspecialchars($_COOKIE[$name]);
  1077. break;
  1078. case 'browser':
  1079. $value = $this->browser->{$name};
  1080. break;
  1081. }
  1082. if (is_array($value)) {
  1083. $value = implode(', ', $value);
  1084. }
  1085. return html::quote($value);
  1086. case 'form':
  1087. return $this->form_tag($attrib);
  1088. }
  1089. return '';
  1090. }
  1091. /**
  1092. * Include a specific file and return it's contents
  1093. *
  1094. * @param string $file File path
  1095. *
  1096. * @return string Contents of the processed file
  1097. */
  1098. protected function include_php($file)
  1099. {
  1100. ob_start();
  1101. include $file;
  1102. $out = ob_get_contents();
  1103. ob_end_clean();
  1104. return $out;
  1105. }
  1106. /**
  1107. * Put objects' content back into template output
  1108. */
  1109. protected function postrender($output)
  1110. {
  1111. // insert objects' contents
  1112. foreach ($this->objects as $key => $val) {
  1113. $output = str_replace($key, $val, $output, $count);
  1114. if ($count) {
  1115. $this->objects[$key] = null;
  1116. }
  1117. }
  1118. // make sure all <form> tags have a valid request token
  1119. $output = preg_replace_callback('/<form\s+([^>]+)>/Ui', array($this, 'alter_form_tag'), $output);
  1120. return $output;
  1121. }
  1122. /**
  1123. * Create and register a button
  1124. *
  1125. * @param array $attrib Named button attributes
  1126. *
  1127. * @return string HTML button
  1128. * @todo Remove all inline JS calls and use jQuery instead.
  1129. * @todo Remove all sprintf()'s - they are pretty, but also slow.
  1130. */
  1131. public function button($attrib)
  1132. {
  1133. static $s_button_count = 100;
  1134. static $disabled_actions = null;
  1135. // these commands can be called directly via url
  1136. $a_static_commands = array('compose', 'list', 'preferences', 'folders', 'identities');
  1137. if (!($attrib['command'] || $attrib['name'] || $attrib['href'])) {
  1138. return '';
  1139. }
  1140. // try to find out the button type
  1141. if ($attrib['type']) {
  1142. $attrib['type'] = strtolower($attrib['type']);
  1143. if ($pos = strpos($attrib['type'], '-menuitem')) {
  1144. $attrib['type'] = substr($attrib['type'], 0, -9);
  1145. $menuitem = true;
  1146. }
  1147. }
  1148. else {
  1149. $attrib['type'] = ($attrib['image'] || $attrib['imagepas'] || $attrib['imageact']) ? 'image' : 'link';
  1150. }
  1151. $command = $attrib['command'];
  1152. if ($attrib['task']) {
  1153. $element = $command = $attrib['task'] . '.' . $command;
  1154. }
  1155. else {
  1156. $element = ($this->env['task'] ? $this->env['task'] . '.' : '') . $command;
  1157. }
  1158. if ($disabled_actions === null) {
  1159. $disabled_actions = (array) $this->config->get('disabled_actions');
  1160. }
  1161. // remove buttons for disabled actions
  1162. if (in_array($element, $disabled_actions)) {
  1163. return '';
  1164. }
  1165. if (!$attrib['image']) {
  1166. $attrib['image'] = $attrib['imagepas'] ? $attrib['imagepas'] : $attrib['imageact'];
  1167. }
  1168. if (!$attrib['id']) {
  1169. $attrib['id'] = sprintf('rcmbtn%d', $s_button_count++);
  1170. }
  1171. // get localized text for labels and titles
  1172. if ($attrib['title']) {
  1173. $attrib['title'] = html::quote($this->app->gettext($attrib['title'], $attrib['domain']));
  1174. }
  1175. if ($attrib['label']) {
  1176. $attrib['label'] = html::quote($this->app->gettext($attrib['label'], $attrib['domain']));
  1177. }
  1178. if ($attrib['alt']) {
  1179. $attrib['alt'] = html::quote($this->app->gettext($attrib['alt'], $attrib['domain']));
  1180. }
  1181. // set accessibility attributes
  1182. if (!$attrib['role']) {
  1183. $attrib['role'] = 'button';
  1184. }
  1185. if (!empty($attrib['class']) && !empty($attrib['classact']) || !empty($attrib['imagepas']) && !empty($attrib['imageact'])) {
  1186. if (array_key_exists('tabindex', $attrib))
  1187. $attrib['data-tabindex'] = $attrib['tabindex'];
  1188. $attrib['tabindex'] = '-1'; // disable button by default
  1189. $attrib['aria-disabled'] = 'true';
  1190. }
  1191. // set title to alt attribute for IE browsers
  1192. if ($this->browser->ie && !$attrib['title'] && $attrib['alt']) {
  1193. $attrib['title'] = $attrib['alt'];
  1194. }
  1195. // add empty alt attribute for XHTML compatibility
  1196. if (!isset($attrib['alt'])) {
  1197. $attrib['alt'] = '';
  1198. }
  1199. // register button in the system
  1200. if ($attrib['command']) {
  1201. $this->add_script(sprintf(
  1202. "%s.register_button('%s', '%s', '%s', '%s', '%s', '%s');",
  1203. self::JS_OBJECT_NAME,
  1204. $command,
  1205. $attrib['id'],
  1206. $attrib['type'],
  1207. $attrib['imageact'] ? $this->abs_url($attrib['imageact']) : $attrib['classact'],
  1208. $attrib['imagesel'] ? $this->abs_url($attrib['imagesel']) : $attrib['classsel'],
  1209. $attrib['imageover'] ? $this->abs_url($attrib['imageover']) : ''
  1210. ));
  1211. // make valid href to specific buttons
  1212. if (in_array($attrib['command'], rcmail::$main_tasks)) {
  1213. $attrib['href'] = $this->app->url(array('task' => $attrib['command']));
  1214. $attrib['onclick'] = sprintf("return %s.command('switch-task','%s',this,event)", self::JS_OBJECT_NAME, $attrib['command']);
  1215. }
  1216. else if ($attrib['task'] && in_array($attrib['task'], rcmail::$main_tasks)) {
  1217. $attrib['href'] = $this->app->url(array('action' => $attrib['command'], 'task' => $attrib['task']));
  1218. }
  1219. else if (in_array($attrib['command'], $a_static_commands)) {
  1220. $attrib['href'] = $this->app->url(array('action' => $attrib['command']));
  1221. }
  1222. else if (($attrib['command'] == 'permaurl' || $attrib['command'] == 'extwin') && !empty($this->env['permaurl'])) {
  1223. $attrib['href'] = $this->env['permaurl'];
  1224. }
  1225. }
  1226. // overwrite attributes
  1227. if (!$attrib['href']) {
  1228. $attrib['href'] = '#';
  1229. }
  1230. if ($attrib['task']) {
  1231. if ($attrib['classact'])
  1232. $attrib['class'] = $attrib['classact'];
  1233. }
  1234. else if ($command && !$attrib['onclick']) {
  1235. $attrib['onclick'] = sprintf(
  1236. "return %s.command('%s','%s',this,event)",
  1237. self::JS_OBJECT_NAME,
  1238. $command,
  1239. $attrib['prop']
  1240. );
  1241. }
  1242. $out = '';
  1243. // generate image tag
  1244. if ($attrib['type'] == 'image') {
  1245. $attrib_str = html::attrib_string(
  1246. $attrib,
  1247. array(
  1248. 'style', 'class', 'id', 'width', 'height', 'border', 'hspace',
  1249. 'vspace', 'align', 'alt', 'tabindex', 'title'
  1250. )
  1251. );
  1252. $btn_content = sprintf('<img src="%s"%s />', $this->abs_url($attrib['image']), $attrib_str);
  1253. if ($attrib['label']) {
  1254. $btn_content .= ' '.$attrib['label'];
  1255. }
  1256. $link_attrib = array('href', 'onclick', 'onmouseover', 'onmouseout', 'onmousedown', 'onmouseup', 'target');
  1257. }
  1258. else if ($attrib['type'] == 'link') {
  1259. $btn_content = isset($attrib['content']) ? $attrib['content'] : ($attrib['label'] ? $attrib['label'] : $attrib['command']);
  1260. $link_attrib = array_merge(html::$common_attrib, array('href', 'onclick', 'tabindex', 'target'));
  1261. if ($attrib['innerclass'])
  1262. $btn_content = html::span($attrib['innerclass'], $btn_content);
  1263. }
  1264. else if ($attrib['type'] == 'input') {
  1265. $attrib['type'] = 'button';
  1266. if ($attrib['label']) {
  1267. $attrib['value'] = $attrib['label'];
  1268. }
  1269. if ($attrib['command']) {
  1270. $attrib['disabled'] = 'disabled';
  1271. }
  1272. $out = html::tag('input', $attrib, null, array('type', 'value', 'onclick', 'id', 'class', 'style', 'tabindex', 'disabled'));
  1273. }
  1274. // generate html code for button
  1275. if ($btn_content) {
  1276. $attrib_str = html::attrib_string($attrib, $link_attrib);
  1277. $out = sprintf('<a%s>%s</a>', $attrib_str, $btn_content);
  1278. }
  1279. if ($attrib['wrapper']) {
  1280. $out = html::tag($attrib['wrapper'], null, $out);
  1281. }
  1282. if ($menuitem) {
  1283. $class = $attrib['menuitem-class'] ? ' class="' . $attrib['menuitem-class'] . '"' : '';
  1284. $out = '<li role="menuitem"' . $class . '>' . $out . '</li>';
  1285. }
  1286. return $out;
  1287. }
  1288. /**
  1289. * Link an external script file
  1290. *
  1291. * @param string $file File URL
  1292. * @param string $position Target position [head|foot]
  1293. */
  1294. public function include_script($file, $position='head')
  1295. {
  1296. if (!preg_match('|^https?://|i', $file) && $file[0] != '/') {
  1297. $file = $this->file_mod($this->scripts_path . $file);
  1298. }
  1299. if (!is_array($this->script_files[$position])) {
  1300. $this->script_files[$position] = array();
  1301. }
  1302. if (!in_array($file, $this->script_files[$position])) {
  1303. $this->script_files[$position][] = $file;
  1304. }
  1305. }
  1306. /**
  1307. * Add inline javascript code
  1308. *
  1309. * @param string $script JS code snippet
  1310. * @param string $position Target position [head|head_top|foot]
  1311. */
  1312. public function add_script($script, $position='head')
  1313. {
  1314. if (!isset($this->scripts[$position])) {
  1315. $this->scripts[$position] = "\n" . rtrim($script);
  1316. }
  1317. else {
  1318. $this->scripts[$position] .= "\n" . rtrim($script);
  1319. }
  1320. }
  1321. /**
  1322. * Link an external css file
  1323. *
  1324. * @param string $file File URL
  1325. */
  1326. public function include_css($file)
  1327. {
  1328. $this->css_files[] = $file;
  1329. }
  1330. /**
  1331. * Add HTML code to the page header
  1332. *
  1333. * @param string $str HTML code
  1334. */
  1335. public function add_header($str)
  1336. {
  1337. $this->header .= "\n" . $str;
  1338. }
  1339. /**
  1340. * Add HTML code to the page footer
  1341. * To be added right befor </body>
  1342. *
  1343. * @param string $str HTML code
  1344. */
  1345. public function add_footer($str)
  1346. {
  1347. $this->footer .= "\n" . $str;
  1348. }
  1349. /**
  1350. * Process template and write to stdOut
  1351. *
  1352. * @param string $templ HTML template
  1353. * @param string $base_path Base for absolute paths
  1354. */
  1355. protected function _write($templ = '', $base_path = '')
  1356. {
  1357. $output = trim($templ);
  1358. if (empty($output)) {
  1359. $output = html::doctype('html5') . "\n" . $this->default_template;
  1360. $is_empty = true;
  1361. }
  1362. // set default page title
  1363. if (empty($this->pagetitle)) {
  1364. $this->pagetitle = 'Roundcube Mail';
  1365. }
  1366. // declare page language
  1367. if (!empty($_SESSION['language'])) {
  1368. $lang = substr($_SESSION['language'], 0, 2);
  1369. $output = preg_replace('/<html/', '<html lang="' . html::quote($lang) . '"', $output, 1);
  1370. if (!headers_sent()) {
  1371. header('Content-Language: ' . $lang);
  1372. }
  1373. }
  1374. // replace specialchars in content
  1375. $page_title = html::quote($this->pagetitle);
  1376. $page_header = '';
  1377. $page_footer = '';
  1378. // include meta tag with charset
  1379. if (!empty($this->charset)) {
  1380. if (!headers_sent()) {
  1381. header('Content-Type: text/html; charset=' . $this->charset);
  1382. }
  1383. $page_header = '<meta http-equiv="content-type"';
  1384. $page_header.= ' content="text/html; charset=';
  1385. $page_header.= $this->charset . '" />'."\n";
  1386. }
  1387. // definition of the code to be placed in the document header and footer
  1388. if (is_array($this->script_files['head'])) {
  1389. foreach ($this->script_files['head'] as $file) {
  1390. $page_header .= html::script($file);
  1391. }
  1392. }
  1393. $head_script = $this->scripts['head_top'] . $this->scripts['head'];
  1394. if (!empty($head_script)) {
  1395. $page_header .= html::script(array(), $head_script);
  1396. }
  1397. if (!empty($this->header)) {
  1398. $page_header .= $this->header;
  1399. }
  1400. // put docready commands into page footer
  1401. if (!empty($this->scripts['docready'])) {
  1402. $this->add_script('$(document).ready(function(){ ' . $this->scripts['docready'] . "\n});", 'foot');
  1403. }
  1404. if (is_array($this->script_files['foot'])) {
  1405. foreach ($this->script_files['foot'] as $file) {
  1406. $page_footer .= html::script($file);
  1407. }
  1408. }
  1409. if (!empty($this->footer)) {
  1410. $page_footer .= $this->footer . "\n";
  1411. }
  1412. if (!empty($this->scripts['foot'])) {
  1413. $page_footer .= html::script(array(), $this->scripts['foot']);
  1414. }
  1415. // find page header
  1416. if ($hpos = stripos($output, '</head>')) {
  1417. $page_header .= "\n";
  1418. }
  1419. else {
  1420. if (!is_numeric($hpos)) {
  1421. $hpos = stripos($output, '<body');
  1422. }
  1423. if (!is_numeric($hpos) && ($hpos = stripos($output, '<html'))) {
  1424. while ($output[$hpos] != '>') {
  1425. $hpos++;
  1426. }
  1427. $hpos++;
  1428. }
  1429. $page_header = "<head>\n<title>$page_title</title>\n$page_header\n</head>\n";
  1430. }
  1431. // add page hader
  1432. if ($hpos) {
  1433. $output = substr_replace($output, $page_header, $hpos, 0);
  1434. }
  1435. else {
  1436. $output = $page_header . $output;
  1437. }
  1438. // add page footer
  1439. if (($fpos = strripos($output, '</body>')) || ($fpos = strripos($output, '</html>'))) {
  1440. $output = substr_replace($output, $page_footer."\n", $fpos, 0);
  1441. }
  1442. else {
  1443. $output .= "\n".$page_footer;
  1444. }
  1445. // add css files in head, before scripts, for speed up with parallel downloads
  1446. if (!empty($this->css_files) && !$is_empty
  1447. && (($pos = stripos($output, '<script ')) || ($pos = stripos($output, '</head>')))
  1448. ) {
  1449. $css = '';
  1450. foreach ($this->css_files as $file) {
  1451. $css .= html::tag('link', array('rel' => 'stylesheet',
  1452. 'type' => 'text/css', 'href' => $file, 'nl' => true));
  1453. }
  1454. $output = substr_replace($output, $css, $pos, 0);
  1455. }
  1456. $output = $this->parse_with_globals($this->fix_paths($output));
  1457. if ($this->assets_path) {
  1458. $output = $this->fix_assets_paths($output);
  1459. }
  1460. $output = $this->postrender($output);
  1461. // trigger hook with final HTML content to be sent
  1462. $hook = $this->app->plugins->exec_hook("send_page", array('content' => $output));
  1463. if (!$hook['abort']) {
  1464. if ($this->charset != RCUBE_CHARSET) {
  1465. echo rcube_charset::convert($hook['content'], RCUBE_CHARSET, $this->charset);
  1466. }
  1467. else {
  1468. echo $hook['content'];
  1469. }
  1470. }
  1471. }
  1472. /**
  1473. * Returns iframe object, registers some related env variables
  1474. *
  1475. * @param array $attrib HTML attributes
  1476. * @param boolean $is_contentframe Register this iframe as the 'contentframe' gui object
  1477. *
  1478. * @return string IFRAME element
  1479. */
  1480. public function frame($attrib, $is_contentframe = false)
  1481. {
  1482. static $idcount = 0;
  1483. if (!$attrib['id']) {
  1484. $attrib['id'] = 'rcmframe' . ++$idcount;
  1485. }
  1486. $attrib['name'] = $attrib['id'];
  1487. $attrib['src'] = $attrib['src'] ? $this->abs_url($attrib['src'], true) : 'program/resources/blank.gif';
  1488. // register as 'contentframe' object
  1489. if ($is_contentframe || $attrib['contentframe']) {
  1490. $this->set_env('contentframe', $attrib['contentframe'] ? $attrib['contentframe'] : $attrib['name']);
  1491. $this->set_env('blankpage', $this->asset_url($attrib['src']));
  1492. }
  1493. return html::iframe($attrib);
  1494. }
  1495. /* ************* common functions delivering gui objects ************** */
  1496. /**
  1497. * Create a form tag with the necessary hidden fields
  1498. *
  1499. * @param array $attrib Named tag parameters
  1500. * @param string $content HTML content of the form
  1501. *
  1502. * @return string HTML code for the form
  1503. */
  1504. public function form_tag($attrib, $content = null)
  1505. {
  1506. if ($this->env['extwin']) {
  1507. $hiddenfield = new html_hiddenfield(array('name' => '_extwin', 'value' => '1'));
  1508. $hidden = $hiddenfield->show();
  1509. }
  1510. else if ($this->framed || $this->env['framed']) {
  1511. $hiddenfield = new html_hiddenfield(array('name' => '_framed', 'value' => '1'));
  1512. $hidden = $hiddenfield->show();
  1513. }
  1514. if (!$content) {
  1515. $attrib['noclose'] = true;
  1516. }
  1517. return html::tag('form',
  1518. $attrib + array('action' => $this->app->comm_path, 'method' => "get"),
  1519. $hidden . $content,
  1520. array('id','class','style','name','method','action','enctype','onsubmit')
  1521. );
  1522. }
  1523. /**
  1524. * Build a form tag with a unique request token
  1525. *
  1526. * @param array $attrib Named tag parameters including 'action' and 'task' values
  1527. * which will be put into hidden fields
  1528. * @param string $content Form content
  1529. *
  1530. * @return string HTML code for the form
  1531. */
  1532. public function request_form($attrib, $content = '')
  1533. {
  1534. $hidden = new html_hiddenfield();
  1535. if ($attrib['task']) {
  1536. $hidden->add(array('name' => '_task', 'value' => $attrib['task']));
  1537. }
  1538. if ($attrib['action']) {
  1539. $hidden->add(array('name' => '_action', 'value' => $attrib['action']));
  1540. }
  1541. // we already have a <form> tag
  1542. if ($attrib['form']) {
  1543. if ($this->framed || $this->env['framed']) {
  1544. $hidden->add(array('name' => '_framed', 'value' => '1'));
  1545. }
  1546. return $hidden->show() . $content;
  1547. }
  1548. unset($attrib['task'], $attrib['request']);
  1549. $attrib['action'] = './';
  1550. return $this->form_tag($attrib, $hidden->show() . $content);
  1551. }
  1552. /**
  1553. * GUI object 'username'
  1554. * Showing IMAP username of the current session
  1555. *
  1556. * @param array $attrib Named tag parameters (currently not used)
  1557. *
  1558. * @return string HTML code for the gui object
  1559. */
  1560. public function current_username($attrib)
  1561. {
  1562. static $username;
  1563. // alread fetched
  1564. if (!empty($username)) {
  1565. return $username;
  1566. }
  1567. // Current username is an e-mail address
  1568. if (strpos($_SESSION['username'], '@')) {
  1569. $username = $_SESSION['username'];
  1570. }
  1571. // get e-mail address from default identity
  1572. else if ($sql_arr = $this->app->user->get_identity()) {
  1573. $username = $sql_arr['email'];
  1574. }
  1575. else {
  1576. $username = $this->app->user->get_username();
  1577. }
  1578. return rcube_utils::idn_to_utf8($username);
  1579. }
  1580. /**
  1581. * GUI object 'loginform'
  1582. * Returns code for the webmail login form
  1583. *
  1584. * @param array $attrib Named parameters
  1585. *
  1586. * @return string HTML code for the gui object
  1587. */
  1588. protected function login_form($attrib)
  1589. {
  1590. $default_host = $this->config->get('default_host');
  1591. $autocomplete = (int) $this->config->get('login_autocomplete');
  1592. $_SESSION['temp'] = true;
  1593. // save original url
  1594. $url = rcube_utils::get_input_value('_url', rcube_utils::INPUT_POST);
  1595. if (empty($url) && !preg_match('/_(task|action)=logout/', $_SERVER['QUERY_STRING']))
  1596. $url = $_SERVER['QUERY_STRING'];
  1597. // Disable autocapitalization on iPad/iPhone (#1488609)
  1598. $attrib['autocapitalize'] = 'off';
  1599. $form_name = !empty($attrib['form']) ? $attrib['form'] : 'form';
  1600. // set atocomplete attribute
  1601. $user_attrib = $autocomplete > 0 ? array() : array('autocomplete' => 'off');
  1602. $host_attrib = $autocomplete > 0 ? array() : array('autocomplete' => 'off');
  1603. $pass_attrib = $autocomplete > 1 ? array() : array('autocomplete' => 'off');
  1604. $input_task = new html_hiddenfield(array('name' => '_task', 'value' => 'login'));
  1605. $input_action = new html_hiddenfield(array('name' => '_action', 'value' => 'login'));
  1606. $input_tzone = new html_hiddenfield(array('name' => '_timezone', 'id' => 'rcmlogintz', 'value' => '_default_'));
  1607. $input_url = new html_hiddenfield(array('name' => '_url', 'id' => 'rcmloginurl', 'value' => $url));
  1608. $input_user = new html_inputfield(array('name' => '_user', 'id' => 'rcmloginuser', 'required' => 'required')
  1609. + $attrib + $user_attrib);
  1610. $input_pass = new html_passwordfield(array('name' => '_pass', 'id' => 'rcmloginpwd', 'required' => 'required')
  1611. + $attrib + $pass_attrib);
  1612. $input_host = null;
  1613. if (is_array($default_host) && count($default_host) > 1) {
  1614. $input_host = new html_select(array('name' => '_host', 'id' => 'rcmloginhost'));
  1615. foreach ($default_host as $key => $value) {
  1616. if (!is_array($value)) {
  1617. $input_host->add($value, (is_numeric($key) ? $value : $key));
  1618. }
  1619. else {
  1620. $input_host = null;
  1621. break;
  1622. }
  1623. }
  1624. }
  1625. else if (is_array($default_host) && ($host = key($default_host)) !== null) {
  1626. $hide_host = true;
  1627. $input_host = new html_hiddenfield(array(
  1628. 'name' => '_host', 'id' => 'rcmloginhost', 'value' => is_numeric($host) ? $default_host[$host] : $host) + $attrib);
  1629. }
  1630. else if (empty($default_host)) {
  1631. $input_host = new html_inputfield(array('name' => '_host', 'id' => 'rcmloginhost')
  1632. + $attrib + $host_attrib);
  1633. }
  1634. $this->add_gui_object('loginform', $form_name);
  1635. // create HTML table with two cols
  1636. $table = new html_table(array('cols' => 2));
  1637. $table->add('title', html::label('rcmloginuser', html::quote($this->app->gettext('username'))));
  1638. $table->add('input', $input_user->show(rcube_utils::get_input_value('_user', rcube_utils::INPUT_GPC)));
  1639. $table->add('title', html::label('rcmloginpwd', html::quote($this->app->gettext('password'))));
  1640. $table->add('input', $input_pass->show());
  1641. // add host selection row
  1642. if (is_object($input_host) && !$hide_host) {
  1643. $table->add('title', html::label('rcmloginhost', html::quote($this->app->gettext('server'))));
  1644. $table->add('input', $input_host->show(rcube_utils::get_input_value('_host', rcube_utils::INPUT_GPC)));
  1645. }
  1646. $out = $input_task->show();
  1647. $out .= $input_action->show();
  1648. $out .= $input_tzone->show();
  1649. $out .= $input_url->show();
  1650. $out .= $table->show();
  1651. if ($hide_host) {
  1652. $out .= $input_host->show();
  1653. }
  1654. if (rcube_utils::get_boolean($attrib['submit'])) {
  1655. $submit = new html_inputfield(array('type' => 'submit', 'id' => 'rcmloginsubmit',
  1656. 'class' => 'button mainaction', 'value' => $this->app->gettext('login')));
  1657. $out .= html::p('formbuttons', $submit->show());
  1658. }
  1659. // surround html output with a form tag
  1660. if (empty($attrib['form'])) {
  1661. $out = $this->form_tag(array('name' => $form_name, 'method' => 'post'), $out);
  1662. }
  1663. // include script for timezone detection
  1664. $this->include_script('jstz.min.js');
  1665. return $out;
  1666. }
  1667. /**
  1668. * GUI object 'preloader'
  1669. * Loads javascript code for images preloading
  1670. *
  1671. * @param array $attrib Named parameters
  1672. * @return void
  1673. */
  1674. protected function preloader($attrib)
  1675. {
  1676. $images = preg_split('/[\s\t\n,]+/', $attrib['images'], -1, PREG_SPLIT_NO_EMPTY);
  1677. $images = array_map(array($this, 'abs_url'), $images);
  1678. $images = array_map(array($this, 'asset_url'), $images);
  1679. if (empty($images) || $_REQUEST['_task'] == 'logout') {
  1680. return;
  1681. }
  1682. $this->add_script('var images = ' . self::json_serialize($images, $this->devel_mode) .';
  1683. for (var i=0; i<images.length; i++) {
  1684. img = new Image();
  1685. img.src = images[i];
  1686. }', 'docready');
  1687. }
  1688. /**
  1689. * GUI object 'searchform'
  1690. * Returns code for search function
  1691. *
  1692. * @param array $attrib Named parameters
  1693. *
  1694. * @return string HTML code for the gui object
  1695. */
  1696. protected function search_form($attrib)
  1697. {
  1698. // add some labels to client
  1699. $this->add_label('searching');
  1700. $attrib['name'] = '_q';
  1701. if (empty($attrib['id'])) {
  1702. $attrib['id'] = 'rcmqsearchbox';
  1703. }
  1704. if ($attrib['type'] == 'search' && !$this->browser->khtml) {
  1705. unset($attrib['type'], $attrib['results']);
  1706. }
  1707. $input_q = new html_inputfield($attrib);
  1708. $out = $input_q->show();
  1709. $this->add_gui_object('qsearchbox', $attrib['id']);
  1710. // add form tag around text field
  1711. if (empty($attrib['form'])) {
  1712. $out = $this->form_tag(array(
  1713. 'name' => "rcmqsearchform",
  1714. 'onsubmit' => self::JS_OBJECT_NAME . ".command('search'); return false",
  1715. 'style' => "display:inline"
  1716. ), $out);
  1717. }
  1718. return $out;
  1719. }
  1720. /**
  1721. * Builder for GUI object 'message'
  1722. *
  1723. * @param array Named tag parameters
  1724. * @return string HTML code for the gui object
  1725. */
  1726. protected function message_container($attrib)
  1727. {
  1728. if (isset($attrib['id']) === false) {
  1729. $attrib['id'] = 'rcmMessageContainer';
  1730. }
  1731. $this->add_gui_object('message', $attrib['id']);
  1732. return html::div($attrib, '');
  1733. }
  1734. /**
  1735. * GUI object 'charsetselector'
  1736. *
  1737. * @param array $attrib Named parameters for the select tag
  1738. *
  1739. * @return string HTML code for the gui object
  1740. */
  1741. public function charset_selector($attrib)
  1742. {
  1743. // pass the following attributes to the form class
  1744. $field_attrib = array('name' => '_charset');
  1745. foreach ($attrib as $attr => $value) {
  1746. if (in_array($attr, array('id', 'name', 'class', 'style', 'size', 'tabindex'))) {
  1747. $field_attrib[$attr] = $value;
  1748. }
  1749. }
  1750. $charsets = array(
  1751. 'UTF-8' => 'UTF-8 ('.$this->app->gettext('unicode').')',
  1752. 'US-ASCII' => 'ASCII ('.$this->app->gettext('english').')',
  1753. 'ISO-8859-1' => 'ISO-8859-1 ('.$this->app->gettext('westerneuropean').')',
  1754. 'ISO-8859-2' => 'ISO-8859-2 ('.$this->app->gettext('easterneuropean').')',
  1755. 'ISO-8859-4' => 'ISO-8859-4 ('.$this->app->gettext('baltic').')',
  1756. 'ISO-8859-5' => 'ISO-8859-5 ('.$this->app->gettext('cyrillic').')',
  1757. 'ISO-8859-6' => 'ISO-8859-6 ('.$this->app->gettext('arabic').')',
  1758. 'ISO-8859-7' => 'ISO-8859-7 ('.$this->app->gettext('greek').')',
  1759. 'ISO-8859-8' => 'ISO-8859-8 ('.$this->app->gettext('hebrew').')',
  1760. 'ISO-8859-9' => 'ISO-8859-9 ('.$this->app->gettext('turkish').')',
  1761. 'ISO-8859-10' => 'ISO-8859-10 ('.$this->app->gettext('nordic').')',
  1762. 'ISO-8859-11' => 'ISO-8859-11 ('.$this->app->gettext('thai').')',
  1763. 'ISO-8859-13' => 'ISO-8859-13 ('.$this->app->gettext('baltic').')',
  1764. 'ISO-8859-14' => 'ISO-8859-14 ('.$this->app->gettext('celtic').')',
  1765. 'ISO-8859-15' => 'ISO-8859-15 ('.$this->app->gettext('westerneuropean').')',
  1766. 'ISO-8859-16' => 'ISO-8859-16 ('.$this->app->gettext('southeasterneuropean').')',
  1767. 'WINDOWS-1250' => 'Windows-1250 ('.$this->app->gettext('easterneuropean').')',
  1768. 'WINDOWS-1251' => 'Windows-1251 ('.$this->app->gettext('cyrillic').')',
  1769. 'WINDOWS-1252' => 'Windows-1252 ('.$this->app->gettext('westerneuropean').')',
  1770. 'WINDOWS-1253' => 'Windows-1253 ('.$this->app->gettext('greek').')',
  1771. 'WINDOWS-1254' => 'Windows-1254 ('.$this->app->gettext('turkish').')',
  1772. 'WINDOWS-1255' => 'Windows-1255 ('.$this->app->gettext('hebrew').')',
  1773. 'WINDOWS-1256' => 'Windows-1256 ('.$this->app->gettext('arabic').')',
  1774. 'WINDOWS-1257' => 'Windows-1257 ('.$this->app->gettext('baltic').')',
  1775. 'WINDOWS-1258' => 'Windows-1258 ('.$this->app->gettext('vietnamese').')',
  1776. 'ISO-2022-JP' => 'ISO-2022-JP ('.$this->app->gettext('japanese').')',
  1777. 'ISO-2022-KR' => 'ISO-2022-KR ('.$this->app->gettext('korean').')',
  1778. 'ISO-2022-CN' => 'ISO-2022-CN ('.$this->app->gettext('chinese').')',
  1779. 'EUC-JP' => 'EUC-JP ('.$this->app->gettext('japanese').')',
  1780. 'EUC-KR' => 'EUC-KR ('.$this->app->gettext('korean').')',
  1781. 'EUC-CN' => 'EUC-CN ('.$this->app->gettext('chinese').')',
  1782. 'BIG5' => 'BIG5 ('.$this->app->gettext('chinese').')',
  1783. 'GB2312' => 'GB2312 ('.$this->app->gettext('chinese').')',
  1784. );
  1785. if (!empty($_POST['_charset'])) {
  1786. $set = $_POST['_charset'];
  1787. }
  1788. else if (!empty($attrib['selected'])) {
  1789. $set = $attrib['selected'];
  1790. }
  1791. else {
  1792. $set = $this->get_charset();
  1793. }
  1794. $set = strtoupper($set);
  1795. if (!isset($charsets[$set])) {
  1796. $charsets[$set] = $set;
  1797. }
  1798. $select = new html_select($field_attrib);
  1799. $select->add(array_values($charsets), array_keys($charsets));
  1800. return $select->show($set);
  1801. }
  1802. /**
  1803. * Include content from config/about.<LANG>.html if available
  1804. */
  1805. protected function about_content($attrib)
  1806. {
  1807. $content = '';
  1808. $filenames = array(
  1809. 'about.' . $_SESSION['language'] . '.html',
  1810. 'about.' . substr($_SESSION['language'], 0, 2) . '.html',
  1811. 'about.html',
  1812. );
  1813. foreach ($filenames as $file) {
  1814. $fn = RCUBE_CONFIG_DIR . $file;
  1815. if (is_readable($fn)) {
  1816. $content = file_get_contents($fn);
  1817. $content = $this->parse_conditions($content);
  1818. $content = $this->parse_xml($content);
  1819. break;
  1820. }
  1821. }
  1822. return $content;
  1823. }
  1824. }