| [ Index ] |
XOOPS v2.2 Reference |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: theme.php,v 1.1.2.23 2005/08/23 03:06:19 phppp Exp $ 3 /** 4 * xoops.theme service definition 5 * 6 * @copyright The Xoops project http://www.xoops.org/ 7 * @license http://www.fsf.org/copyleft/gpl.html GNU public license 8 * @package xoops 9 * @subpackage lang 10 * @author Skalpa Keo <skalpa@xoops.org> 11 * @since 2.1.0 12 * @internal Almost a straight copy from the Xoops 2.0.x include/common.php code 13 * --------------------------------------------------------------------------------- 14 * See the enclosed file LICENSE for licensing information. 15 * If you did not receive this file, see http://www.fsf.org/copyleft/gpl.html 16 * --------------------------------------------------------------------------------- 17 */ 18 19 require_once XOOPS_ROOT_PATH.'/class/template.php'; 20 21 class XTheme { 22 23 /*#@+ 24 * xoops.theme properties 25 **/ 26 /** 27 * The name of this theme 28 * @var string 29 **/ 30 var $name = 'default'; 31 var $fullName = ''; 32 33 var $path = ''; 34 var $url = ''; 35 /** 36 * Whether or not the theme engine should include output generated by php 37 * @var string 38 **/ 39 var $bufferOutput = true; 40 /** 41 * Default content-type of pages generated by this theme 42 * @var string 43 **/ 44 var $contentType = 'text/html'; 45 /** 46 * The templates this theme contains in an array of relative paths 47 * @var array 48 **/ 49 var $templateFiles = array(); 50 51 /** 52 * Default file for this theme (in case the specified one can't be found) 53 * @var string 54 **/ 55 var $defaultFile = 'theme.html'; 56 /** 57 * The file to fetch from the theme 58 * @var string 59 **/ 60 var $pageTpl = ''; 61 /** 62 * The type of page to show (main, admin or rss) 63 * @var string 64 **/ 65 var $pageType = ''; 66 /** 67 * Enable banner system ? 68 * @var bool 69 */ 70 var $enableBanner = false; 71 /** 72 * Template engine used to render pages 73 * @var object 74 */ 75 var $tplEngine = false; 76 77 /**#@-*/ 78 79 /*#@+ 80 * xoops.theme.page properties 81 **/ 82 /** 83 * Array of strings defining the page title (we use an array here so modules can add a string to the main title) 84 * @var array 85 **/ 86 var $title = array(); 87 /** 88 * Separator used to rebuild the title 89 * @var string 90 **/ 91 var $titleSeparator = ' - '; 92 /** 93 * Page banner 94 * @var object 95 **/ 96 var $banner = null; 97 98 /** 99 * Page footer 100 * @var string 101 **/ 102 var $footer = ''; 103 104 /** 105 * see addJS() and getJS() methods 106 **/ 107 var $js = array(); 108 109 /** 110 * Constructor 111 * 112 * @param bool $enableBanner whether to enable banners on this page 113 * @param bool $debugging whether debugging is enabled 114 * @param bool $mainService whether this is the main theme service (Not used yet, Mith) 115 * @param string $name name of the theme to use 116 * 117 * @return void 118 */ 119 function XTheme($enableBanner = true, $debugging = false, $mainService = false, $name = "") { 120 $this->path = XOOPS_THEME_PATH; 121 $this->url = XOOPS_THEME_URL; 122 /*switch ($this->contentType) { 123 case 'text/html': 124 $this->document =& $GLOBALS[XOOPS]->create( 'xoops.output.document.xhtml' ); 125 break; 126 default: 127 $this->document =& $GLOBALS[XOOPS]->create( 'xoops.output.document' ); 128 }*/ 129 $this->enableBanner = $enableBanner; 130 if ($name != "") { 131 $this->name = $name; 132 } 133 134 $this->tplEngine = new XoopsTpl(); 135 $this->tplEngine->xoops_setDebugging($debugging); 136 $this->tplEngine->template_dir = XOOPS_THEME_PATH; 137 $this->tplEngine->assign( 'xoops_banner', $this->enableBanner ? xoops_getbanner() : ' ' ); 138 139 $GLOBALS['xoopsTpl'] =& $this->tplEngine; 140 if ( $this->bufferOutput ) { 141 global $xoopsConfig; 142 //if Gzip is enabled and debug is turned off 143 if ( $xoopsConfig['gzip_compression'] == 1 && ($xoopsConfig['debug_mode'] == array(0 => 0) || $xoopsConfig['debug_mode'] == array())) 144 { 145 $ob_started = false; 146 $phpver = phpversion(); 147 $useragent = ( isset( $_SERVER["HTTP_USER_AGENT"] ) ) ? $_SERVER["HTTP_USER_AGENT"] : ""; 148 149 if ( $phpver >= '4.0.4pl1' && ( strstr( $useragent, 'compatible' ) || strstr( $useragent, 'Gecko' ) ) ) 150 { 151 if ( extension_loaded( 'zlib' ) ) { 152 ob_start( 'ob_gzhandler' ); 153 $ob_started = true; 154 } 155 } 156 else if ( $phpver > '4.0' ) 157 { 158 if ( strstr( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) ) 159 { 160 if ( extension_loaded( 'zlib' ) ) 161 { 162 ob_start(); 163 ob_implicit_flush( 0 ); 164 header( 'Content-Encoding: gzip' ); 165 $ob_started = true; 166 } 167 } 168 } 169 if (!$ob_started) { 170 ob_start(); 171 } 172 } 173 else 174 { 175 ob_start(); 176 } 177 } 178 179 $this->loadGlobalVars(); 180 $this->loadTheme(); 181 return true; 182 } 183 184 /** 185 * Builds title string for page title 186 * 187 * @return string 188 */ 189 function titleString() { 190 return implode( $this->titleSeparator, $this->title ); 191 } 192 193 194 /** 195 * Determines path for a given theme template 196 * 197 * checks first theme path for $tplpath 198 * then default theme path for $tplpath 199 * then returns the default theme's default file 200 * 201 * @param string $tplpath name of template 202 */ 203 function templatePath( $tplpath ) { 204 // if ( isset($this->templateFiles[$tplpath]) ) { 205 // return $this->templateFiles[$tplpath]; 206 // } 207 $defpath = $this->path.'/default/' . $this->defaultFile; 208 $deffilepath = $this->path.'/default/' . $tplpath; 209 $thmpath = $this->path.'/'.$this->name.'/'.$tplpath; 210 if ( is_readable( $thmpath ) ) { 211 return $thmpath; 212 } 213 if ( is_readable( $deffilepath ) ) { 214 return $defpath; 215 } 216 if ( is_readable( $defpath ) ) { 217 return $defpath; 218 } 219 trigger_error( "Cannot determine path for template $tplpath", E_USER_WARNING ); 220 return false; 221 } 222 223 /** 224 * Display a template 225 * 226 * @param string $contentTemplate Name of template to display 227 * @param string $pageTpl Name of template to use for whole page 228 * @param array $customVars additional variables to assign before display 229 * 230 * @return bool 231 */ 232 function display( $contentTemplate = null, $pageTpl = null, $customVars = null ) { 233 static $called = false; 234 235 if ( $this->bufferOutput ) { 236 if ( $called ) { 237 return true; 238 } 239 $content = ob_get_contents(); 240 ob_end_clean(); 241 } else { 242 $content = ''; 243 } 244 $called = true; 245 246 //$this->tplEngine->caching = 0; // ??? @todo: Kept from Xoops 2.0.x, but to be kicked out 247 248 global $xoopsConfig, $xoopsOption; 249 $this->name = $xoopsConfig['theme_set']; 250 251 //load CSS and theme language prior to assigning content 252 $this->pageType = isset($xoopsOption['pagetype']) && in_array($xoopsOption['pagetype'], array("admin", "rss")) ? $xoopsOption['pagetype'] : "main"; 253 254 $this->tplEngine->assign(array('xoops_theme' => $this->name, 255 'xoops_imageurl' => $this->url.'/'.$this->name.'/', 256 'xoops_themecss'=> xoops_getcss($this->name, $this->pageType))); 257 //load theme language 258 $filename = $this->pageType == "admin" ? "admin.php" : "main.php"; 259 if (file_exists(XOOPS_THEME_PATH."/$this->name/language/".$GLOBALS['xoopsConfig']['language']."/".$filename)) { 260 include_once(XOOPS_THEME_PATH."/$this->name/language/".$GLOBALS['xoopsConfig']['language']."/".$filename); 261 } 262 elseif (file_exists(XOOPS_THEME_PATH."/$this->name/language/english/".$filename)) { 263 include_once(XOOPS_THEME_PATH."/$this->name/language/english/".$filename); 264 } 265 266 if ( isset( $customVars ) ) { 267 $this->tplEngine->assign( $customVars ); 268 } 269 270 //Add module CSS code 271 $moduleCSS = xoops_getcss($this->name, "module"); 272 if (count($moduleCSS)>0) { 273 foreach($moduleCSS as $modulescss){ 274 $this->addCSS($modulescss); 275 } 276 } 277 278 //assign JavaScript 279 $this->tplEngine->assign('xoops_js', '//--></script>'.implode('', $this->getJS()).'<script type="text/javascript"><!--'); 280 281 // Load module-specified variables for cached module pages 282 // Keep the two variables before they are deprecated in modules 283 global $xoopsOption; 284 if(isset($xoopsOption["xoops_pagetitle"])){ 285 $this->tplEngine->assign('xoops_pagetitle', $xoopsOption["xoops_pagetitle"]); 286 } 287 if(isset($xoopsOption["xoops_module_header"])){ 288 $this->tplEngine->assign('xoops_module_header', $xoopsOption["xoops_module_header"]); 289 } 290 291 //Assign main content 292 if ( !empty( $contentTemplate ) ) { 293 if ( strpos( $contentTemplate, ':' ) === false ) { 294 $contentTemplate = $this->templatePath($contentTemplate); 295 } 296 $content .= $this->tplEngine->fetch( $contentTemplate, $this->getCachedTemplateId() ); 297 } 298 299 $this->tplEngine->assign( 'xoops_contents', $content ); 300 301 if ((isset($xoopsOption['output_type']) && $xoopsOption['output_type'] == "plain")) { 302 //display "plain" template that has theme CSS, but no specific content 303 $this->pageTpl = 'system_plain.html'; 304 $tpl = "db:".$this->pageTpl; 305 } 306 else { 307 if ((isset($xoopsOption['pagetype']) && $xoopsOption['pagetype'] == "admin")) { 308 //display theme admin template 309 $this->pageTpl = 'themeadmin.html'; 310 } 311 312 //Find page template to show 313 if (is_null($pageTpl)) { 314 if ( empty($this->pageTpl) || (false === ( $tpl = $this->templatePath($this->pageTpl) ) ) ) { 315 $tpl = $this->templatePath( $this->defaultFile ); 316 } 317 } 318 elseif ((false === ( $tpl = $this->templatePath($pageTpl) ) )) { 319 $tpl = $this->templatePath( $this->defaultFile ); 320 } 321 } 322 $this->tplEngine->caching = 0; // ??? @todo: Kept from Xoops 2.0.x, but to be kicked out 323 $this->tplEngine->display( $tpl ); 324 return true; 325 } 326 327 /** 328 * Add Javascript file or JS code to the document head 329 * 330 * @param string $src path to .js file 331 * @param array $attributes name => value paired array of attributes such as title 332 * @param string $content JavaScript code 333 * 334 * @return void 335 **/ 336 function addJS($src, $attributes = array(), $content = "") { 337 $js = "<script type=\"text/javascript\""; 338 if (!is_null($src)) { 339 $attributes['src'] = $src; 340 } 341 if (is_array($attributes) && count($attributes) > 0) { 342 foreach ($attributes as $name => $value) { 343 $js .= " ".$name ."=\"".$value."\""; 344 } 345 } 346 $js .= ">"; 347 if (is_null($src)) { 348 $js .= $content; 349 } 350 $js .= "</script>\n"; 351 $this->js[] = $js; 352 } 353 354 /** 355 * Add StyleSheet or CSS code to the document head 356 * 357 * @param string $src path to .css file 358 * @param array $attributes name => value paired array of attributes such as title 359 * @param string $content CSS code 360 * 361 * @return void 362 **/ 363 function addCSS($src, $attributes = array(), $content = "") { 364 if (!is_null($src)) { 365 $css = "<link rel=\"stylesheet\" type=\"text/css\""; 366 $attributes['href'] = $src; 367 } 368 else { 369 $css = "<script type=\"text/css\""; 370 } 371 if (is_array($attributes) && count($attributes) > 0) { 372 foreach ($attributes as $name => $value) { 373 $css .= " ".$name."=\"".$value."\""; 374 } 375 } 376 if (is_null($src)) { 377 $css .= ">"; 378 $css .= $content; 379 $css .= "</script>\n"; 380 } 381 else { 382 $css .= " />\n"; 383 } 384 $this->js[] = $css; 385 } 386 387 /** 388 * Get Javascript code for document head 389 * 390 * @return array 391 **/ 392 function getJS() { 393 return $this->js; 394 } 395 396 /** 397 * Load variables used globally in themes 398 * 399 * @param bool $loadConfig whether to load configuration data from db (called with false from xoops_cp_header() ) 400 * 401 * @return void 402 */ 403 function loadGlobalVars($loadConfig = true) { 404 global $xoopsConfig, $xoopsModule; 405 $this->tplEngine->assign(array('xoops_url' => XOOPS_URL, 406 'xoops_abs_url' => XOOPS_ABS_URL, 407 'xoops_rootpath' => XOOPS_ROOT_PATH, 408 'xoops_langcode' => _LANGCODE, 409 'xoops_charset' => _CHARSET, 410 'xoops_version' => XOOPS_VERSION, 411 'xoops_upload_url' => XOOPS_UPLOAD_URL)); 412 $this->tplEngine->assign( 413 array('xoops_requesturi' => htmlspecialchars($GLOBALS['xoopsRequestUri'], ENT_QUOTES), 414 'xoops_sitename' => htmlspecialchars($xoopsConfig['sitename'], ENT_QUOTES), 415 'xoops_slogan' => htmlspecialchars($xoopsConfig['slogan'], ENT_QUOTES))); 416 if ($loadConfig == true) { 417 // Meta tags 418 $config_handler =& xoops_gethandler('config'); 419 $config =& $config_handler->getConfigsByCat(XOOPS_CONF_METAFOOTER); 420 foreach ($config as $name => $value) { 421 // prefix each tag with 'xoops_' 422 $this->tplEngine->assign('xoops_'.$name, $value); 423 } 424 $this->addJS(XOOPS_URL.'/include/xoops.js'); 425 426 // Load module information 427 if (isset($xoopsModule) && is_object($xoopsModule) && $xoopsModule->getVar('dirname') != "system") { 428 // set page title 429 $this->tplEngine->assign('xoops_pagetitle', $xoopsModule->getVar('name')); 430 $this->tplEngine->assign('xoops_dirname', $xoopsModule->getVar('dirname')); 431 } 432 else { 433 $this->tplEngine->assign('xoops_pagetitle', htmlspecialchars($xoopsConfig['slogan'], ENT_QUOTES)); 434 $this->tplEngine->assign('xoops_dirname', "system"); 435 } 436 } 437 global $xoopsUser, $xoopsModule; 438 // Load user variables 439 if ($xoopsUser != '') { 440 $this->tplEngine->assign(array( 'xoops_isuser' => true, 441 'xoops_userid' => $xoopsUser->getVar('uid'), 442 'xoops_uname' => $xoopsUser->getVar('uname'))); 443 if (is_object($xoopsModule)) { 444 $this->tplEngine->assign('xoops_isadmin', $xoopsUser->isAdmin($xoopsModule->getVar('mid'))); 445 } 446 } else { 447 $this->tplEngine->assign(array('xoops_isuser' => false, 'xoops_isadmin' => false)); 448 } 449 } 450 451 /** 452 * Load the theme if valid 453 * 454 * @return void 455 **/ 456 function loadTheme() { 457 global $xoopsConfig, $xoopsUser; 458 if (!empty($_REQUEST['xoops_theme_select']) && in_array($_REQUEST['xoops_theme_select'], $xoopsConfig['theme_set_allowed'])) { 459 $xoopsConfig['theme_set'] = $_REQUEST['xoops_theme_select']; 460 $_SESSION['xoopsUserTheme'] = $_REQUEST['xoops_theme_select']; 461 } elseif (!empty($_SESSION['xoopsUserTheme']) && in_array($_SESSION['xoopsUserTheme'], $xoopsConfig['theme_set_allowed'])) { 462 $xoopsConfig['theme_set'] = $_SESSION['xoopsUserTheme']; 463 } 464 elseif ($xoopsUser) { 465 if ($xoopsUser->getVar('theme') != "" && in_array($xoopsUser->getVar('theme'), $xoopsConfig['theme_set_allowed'])) { 466 $xoopsConfig['theme_set'] = $xoopsUser->getVar('theme'); 467 } 468 } 469 if (isset($GLOBALS['xoopsOption']['pagetype']) && $GLOBALS['xoopsOption']['pagetype'] == "admin") { 470 //if admin theme is set to use frontside theme 471 if (is_numeric($xoopsConfig['theme_set_admin']) && $xoopsConfig['theme_set_admin'] == 0) { 472 //set current admin theme to frontside theme 473 $xoopsConfig['theme_set_admin'] = $xoopsConfig['theme_set']; 474 } 475 //check theme for admin template 476 if ($this->tplEngine->template_exists($xoopsConfig['theme_set_admin'].'/themeadmin.html')) { 477 //set the current theme to the one selected for admin area 478 $xoopsConfig['theme_set'] = $xoopsConfig['theme_set_admin']; 479 } 480 else { 481 //revert to default theme for admin area 482 $xoopsConfig['theme_set'] = $this->name; 483 } 484 } 485 $GLOBALS['xoopsOption']['theme_use_smarty'] = 1; 486 } 487 488 function getCachedTemplateId() { 489 // generate safe cache Id 490 $cachegroup = isset($GLOBALS['xoopsOption']['cache_group']) ? "_".$GLOBALS['xoopsOption']['cache_group'] : ""; 491 492 $protocol = strtolower(substr($_SERVER['SERVER_PROTOCOL'], 0, strpos($_SERVER['SERVER_PROTOCOL'], "/", 0))); 493 return 'mod_'.$GLOBALS['xoopsModule']->getVar('dirname').'|'.md5(str_replace(XOOPS_URL, '', $protocol."://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$cachegroup)); 494 } 495 496 /** 497 * Checks if a template is cached 498 * 499 * @param string $template name of template 500 * 501 * @return bool 502 **/ 503 function is_cached($template) { 504 return $this->tplEngine->is_cached($template, $this->