PHPOpenChat PHP Cross Reference Customer Relationship Management

Source: /include/class.Template.inc - 531 lines - 16282 bytes - Summary - Text - Print

   1  <?php //-*-php-*-
   2  /*   ********************************************************************   **
   3  **   Copyright notice                                                       **
   4  **                                                                          **
   5  **   (c) 1995-2004 PHPOpenChat Development Team                             **
   6  **   http://phpopenchat.sourceforge.net/                                    **
   7  **                                                                          **
   8  **   All rights reserved                                                    **
   9  **                                                                          **
  10  **   This script is part of the PHPOpenChat project. The PHPOpenChat        **
  11  **   project is free software; you can redistribute it and/or modify        **
  12  **   it under the terms of the GNU General Public License as published by   **
  13  **   the Free Software Foundation; either version 2 of the License, or      **
  14  **   (at your option) any later version.                                    **
  15  **                                                                          **
  16  **   The GNU General Public License can be found at                         **
  17  **   http://www.gnu.org/copyleft/gpl.html.                                  **
  18  **   A copy is found in the textfile GPL and important notices to the       **
  19  **   license from the team is found in the textfile LICENSE distributed     **
  20  **   with these scripts.                                                    **
  21  **                                                                          **
  22  **   This script is distributed in the hope that it will be useful,         **
  23  **   but WITHOUT ANY WARRANTY; without even the implied warranty of         **
  24  **   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          **
  25  **   GNU General Public License for more details.                           **
  26  **                                                                          **
  27  **   This copyright notice MUST APPEAR in all copies of the script!         **
  28  **   ********************************************************************   */
  29  
  30  //Get default values
  31  require_once (POC_BASE.'/config.inc.php');
  32  require_once (POC_INCLUDE_PATH.'/class.Logger.inc');
  33  require_once (POC_INCLUDE_PATH.'/class.HttpNegotiation.inc');
  34  
  35  //Get theme depending properties if exists
  36  /*
  37  if(isset($_SESSION['chatter'])
  38     && file_exists(POC_INCLUDE_PATH.'/templates/'.$_SESSION['chatter']->get_theme().'/theme_config.inc.php')){
  39    include_once(POC_INCLUDE_PATH.'/templates/'.$_SESSION['chatter']->get_theme().'/theme_config.inc.php');
  40  }elseif(isset($_COOKIE['poc_theme'])
  41     && file_exists(POC_INCLUDE_PATH.'/templates/'.$_COOKIE['poc_theme'].'/theme_config.inc.php')){
  42    include_once(POC_INCLUDE_PATH.'/templates/'.$_COOKIE['poc_theme'].'/theme_config.inc.php');
  43  }else{
  44    include_once(POC_INCLUDE_PATH.'/templates/'.DEFAULT_THEME.'/theme_config.inc.php');
  45  }
  46  */
  47  
  48  /**
  49  * Handles the complete Templatesystem
  50  *
  51  * @author  Sascha Ragtschaa <sascha@ragtschaa.com>
  52  * @author  Michael Oertel <michael@ortelius.de>
  53  * @access  public
  54  * @version $Id: class Template.inc,v 1.0 2002/09/25
  55  */
  56  class POC_Template
  57  {
  58  
  59    /**
  60    * Contains the name of current theme
  61    * 
  62    * @var string
  63    * @access private
  64    */
  65    var $theme         = '';
  66    var $old_theme     = null;
  67    
  68    var $include_path  = '';
  69    var $web_root      = null;
  70    var $poc_web_root  = null;
  71    var $tmpl_web_path = null;
  72    var $tmpl_sys_path = null;
  73    var $template      = '';
  74    var $cache_it      = false;
  75    var $max_age       = 0;
  76    var $content_type  = 'text/html';
  77    var $valid_xhtml   = false;
  78    var $requires_relogin = false;
  79    var $extra_smileys = array();
  80    
  81    /**
  82    * Constructor
  83    *
  84    * @access public
  85    * @return void
  86    */
  87    function POC_Template( $theme = null )
  88    {
  89      if ( !defined('DEFAULT_THEME') )
  90        die('No DEFAULT_THEME specified in config.inc!');
  91      else
  92      {
  93        $this->theme = ( !is_null($theme) )? $this->theme = $theme : $this->theme = DEFAULT_THEME;
  94        $this->include_path = POC_INCLUDE_PATH;
  95        $this->_build_pathes();
  96        if( isset($_COOKIE['poc_theme']) ){
  97          $this->set_theme($_COOKIE['poc_theme']);
  98        }
  99        $this->_set_theme_properties();
 100        $this->set_content_type();
 101      }
 102    }
 103    
 104    function _set_theme_properties()
 105    {
 106      if( file_exists($this->get_tmpl_sys_path().'/theme_config.inc.php') ){
 107        include($this->get_tmpl_sys_path().'/theme_config.inc.php');
 108        $this->valid_xhtml = (defined('VALID_XHTML'))? VALID_XHTML:false;
 109        $this->requires_relogin = (defined('REQUIRES_RELOGIN'))? REQUIRES_RELOGIN:false;
 110        $this->extra_smileys = (isset($extra_smileys) && is_array($extra_smileys) && count($extra_smileys)>0)?
 111                               $extra_smileys:array();
 112      }
 113    }
 114    
 115    function requires_relogin()
 116    {
 117      return $this->requires_relogin;
 118    }
 119    
 120    function has_extra_smileys()
 121    {
 122      return ( count($this->extra_smileys) > 0 );
 123    }
 124    
 125    /**
 126    * Sets the correct mime type for the current theme,
 127    * this depends on content-type negotioation
 128    *
 129    * @access public
 130    * @return void
 131    */
 132    function set_content_type()
 133    {
 134      $serverVariants = 'application/xhtml+xml,text/html;q=0.9';
 135      $defaultVariant = 'text/html';
 136      if($this->valid_xhtml && isset($_SESSION['httpneg'])){
 137        $preferredContentType = $_SESSION['httpneg']->getPreferredContentType($serverVariants, $defaultVariant);
 138        $this->content_type   = $preferredContentType;
 139        unset($serverVariants);
 140        unset($preferredContentType);
 141      }else{
 142        $this->content_type   = $defaultVariant;
 143      }
 144      unset($defaultVariant);
 145    }
 146  
 147    function is_shockwave()
 148    {
 149      $serverVariants = 'application/x-shockwave-flash,text/html;q=0.9';
 150      $defaultVariant = 'text/html';
 151      $preferredContentType = $_SESSION['httpneg']->getPreferredContentType($serverVariants, $defaultVariant);
 152  
 153      return(preg_match('/shockwave\-flash/',$preferredContentType));
 154    }
 155    
 156    /**
 157    * Provides the content-type
 158    *
 159    * @access public
 160    * @return string
 161    */
 162    function get_content_type()
 163    {
 164      return $this->content_type;
 165    }
 166  
 167    /**
 168    * Provides the extra smileys of the current theme
 169    *
 170    * @access public
 171    * @return array
 172    */
 173    function get_extra_smileys()
 174    {
 175      return $this->extra_smileys;
 176    }
 177    
 178    function _check_pathes($file)
 179    {
 180      return file_exists($file);
 181    }
 182    
 183    function _build_pathes( $old_theme = null )
 184    {
 185      if( is_null($old_theme) ) {
 186        
 187        //set absolut path to templates
 188        //$this->web_root = dirname($_SERVER['SCRIPT_NAME']);
 189        $this->web_root   = preg_replace('#/[^/]*$#', '', $_SERVER['REQUEST_URI']);
 190        //echo'debug: '.$this->web_root;
 191        //                   /var/www/html/chat3 . /include/templates . / . openchat
 192        $this->tmpl_sys_path = realpath('.') . BASETEMPLATE_PATH .'/'. $this->theme;
 193        $this->tmpl_web_path = BASETEMPLATE_PATH .'/'. $this->theme;
 194        if( $this->web_root != '/' && $this->web_root != '\\' ) {
 195          $this->tmpl_web_path = $this->web_root . $this->tmpl_web_path;
 196        }
 197        //unset($pathinfo);
 198        //check path
 199        $value      = '';
 200        $tmpl_found = false;
 201        if( !$this->_check_pathes($this->tmpl_sys_path.'/index.tpl') ) {
 202          //okay, this isn't a standallone installation
 203          $tried = array();
 204          $ties  = array(
 205                    'postnuke'=>'/modules/' . PN_MODULE_NAME,
 206                    '' => ''
 207                   );
 208          foreach ($ties as $value) {
 209            $this->tmpl_web_path = $this->web_root .$value. BASETEMPLATE_PATH  .'/'. $this->theme;
 210            $this->tmpl_sys_path = realpath('.')   .$value. BASETEMPLATE_PATH  .'/'. $this->theme;
 211            $tried[] = $this->tmpl_sys_path;
 212            if($this->_check_pathes($this->tmpl_sys_path.'/index.tpl')){
 213              $tmpl_found = true;
 214              break;
 215            }
 216          }
 217          if( !$tmpl_found ){
 218            print_r($tried);
 219            die('Couln\'t find any template, giving up.');
 220          }
 221  
 222          //BASETEMPLATE_PATH','/include/templates $this->theme
 223          //include/templates/openchat
 224          $this->poc_web_root = str_replace(BASETEMPLATE_PATH.'/'.$this->theme,'',$this->tmpl_web_path);
 225          //$this->poc_web_root = $this->tmpl_web_path;
 226        }
 227      } else {
 228        $this->tmpl_web_path = preg_replace('/'.$old_theme.'$/', $this->theme, $this->tmpl_web_path);
 229        $this->tmpl_sys_path = preg_replace('/'.$old_theme.'$/', $this->theme, $this->tmpl_sys_path);
 230      }
 231    }
 232  
 233    /**
 234    * Calculates a unique key to store data in the db
 235    *
 236    * @access private
 237    * @return string
 238    */
 239    function _calculate_cacheId()
 240    {
 241      $_request = array();
 242      $_request = $_REQUEST;//copy request array
 243      unset($_request[ 'chatter_image' ]);//remove posted image data from request (used in profile)
 244      $sessionName = session_name();
 245      unset($_request[ $sessionName ]);//remove session id from request
 246      unset($sessionName);
 247      $cacheId = serialize($_request);
 248      $cacheId .= '_'.$_SERVER['PHP_SELF'].'_'.$_SESSION['chat']->get_language();//append template name and language
 249      
 250      return $cacheId;
 251    }
 252    
 253    function get_include_path()
 254    {
 255      return $this->include_path;
 256    }
 257    
 258    /**
 259    * Finds the given template in the POC-install-directory tree
 260    *
 261    * @access private
 262    * @param string
 263    * @return mixed
 264    */
 265    function _find_template($template=null)
 266    {
 267      if( is_null($template) ) {
 268        if(false){
 269          //In serveral cases pathinfo produces unexpected results %-) 
 270          $pathinfo  = array();
 271          $pathinfo  = pathinfo( $_SERVER['REQUEST_URI'] );
 272          $template  = '/'. str_replace( '.'.$pathinfo['extension'], '', $pathinfo['basename'] );
 273          unset($pathinfo);
 274        } else {
 275          preg_match('#^.*/(.*)\.php[\?|\#]?.*$#', $_SERVER['REQUEST_URI'], $matches);
 276          $template = $matches[1];
 277          unset($matches);
 278        }
 279        $template .= '.'.TEMPLATE_EXTENSION;
 280      } else {
 281        $template  = '/'.$template.'.'.TEMPLATE_EXTENSION;
 282      }
 283      
 284      //$tmpl_subdirectory = str_replace( $this->web_root, '', dirname($_SERVER['SCRIPT_NAME']));
 285      $tmpl_subdirectory = str_replace( $this->web_root, '', dirname($_SERVER['REQUEST_URI']));
 286      //BugFix: http://sourceforge.net/tracker/index.php?func=detail&aid=733343&group_id=14195&atid=114195
 287      $tmpl_directory    = $this->tmpl_sys_path . '/' . $tmpl_subdirectory;
 288      $tmpl              = str_replace('//','/',$tmpl_directory.$template);
 289      return (POC_OS == 'win')? str_replace( '/', DELI, $tmpl):$tmpl;
 290    }
 291  
 292    /**
 293    * Puts content into the cache
 294    *
 295    * @access private
 296    * @param string
 297    * @return boolean
 298    */
 299    function _put_into_cache( $content )
 300    {
 301      $cacheId = $this->_calculate_cacheId();
 302      $_SESSION['chat']->connect();
 303       $success = $_SESSION['chat']->put_template_into_cache( $cacheId, $content, $this->cache_max_age );
 304      $_SESSION['chat']->disconnect();
 305      
 306      return $success;
 307    }
 308    
 309    /**
 310    * Provides the template with path and outputs html
 311    *
 312    * @access public
 313    */
 314    function get_template( $template=null, $path_only = false )
 315    {
 316  
 317      if( ! $this->template = $this->_find_template($template) )
 318        die('No such template found, giving up.');
 319  
 320      if($path_only) return $this->template;
 321  
 322      global $TEMPLATE_OUT;
 323      ob_start();
 324      if( !file_exists($this->template) ) {
 325        /* in case user changed directory structure and
 326         * has created the session object already */
 327        $this->_build_pathes();
 328        $this->template = $this->_find_template();
 329      }
 330  
 331      include_once($this->template);
 332      $content = (TRIM_OUTPUT)? preg_replace('/([\r\n])[\s]+/', "\r\n", ob_get_contents()):ob_get_contents();
 333      ob_end_clean();
 334      $content = '<?xml version="1.0" encoding="'.$_SESSION['translator']->out('CHARACTER_ENCODING').'"?>'.NL.$content;
 335      header('Content-type: '.$this->content_type.'; charset='.$_SESSION['translator']->out('CHARACTER_ENCODING'));      
 336      print $content;
 337      flush();
 338  
 339      if( $this->cache_it )
 340      {
 341        if( !$this->_put_into_cache( $content ) )
 342          $_SESSION['logger']->debug('Could not cache template',__FILE__,__LINE__);
 343  
 344        $this->cache_it = false;
 345        $this->cache_max_age = 0;
 346      }
 347      exit;
 348    }
 349  
 350    /**
 351    * Searches for a template in right language, if such template not found 
 352    * the template in default language will be returned
 353    *
 354    * @access public
 355    */
 356    function get_language_template( $template = null )
 357    {
 358      if( ! $this->template = $this->_find_template($template) )
 359        die('No such template found, giving up.');
 360  
 361      global $TEMPLATE_OUT;
 362      ob_start();
 363      if( file_exists($this->template.'.'.$_SESSION['chat']->get_language()) )
 364        include_once($this->template.'.'.$_SESSION['chat']->get_language());
 365      else 
 366        include_once($this->template.'.'.$_SESSION['chat']->get_default_language());
 367  
 368      $content = (TRIM_OUTPUT)? preg_replace('/([\r\n])[\s]+/', "\r\n", ob_get_contents()):ob_get_contents();
 369      ob_end_clean();
 370      $content = '<?xml version="1.0" encoding="'.$_SESSION['translator']->out('CHARACTER_ENCODING').'"?>'.NL.$content;
 371      
 372      header('Content-type: '.$this->content_type.'; charset='.$_SESSION['translator']->out('CHARACTER_ENCODING'));
 373      print $content;
 374      exit;
 375    }
 376    
 377    /**
 378    * Provides the current choosen theme
 379    *
 380    * @return string
 381    */
 382    function get_theme()
 383    {
 384      return $this->theme;
 385    }
 386  
 387    /**
 388    * Provides content from cache
 389    * @param string maximum age for the cached content
 390    * @return string
 391    */
 392    function get_cached_content( $max_age )
 393    {
 394      if( !is_object($_SESSION['chat']) || !CACHE_ENGINE ) return false;
 395      
 396      $cacheId = $this->_calculate_cacheId();
 397      $_SESSION['chat']->connect();
 398       $cached_content = $_SESSION['chat']->get_template_from_cache($cacheId);
 399      $_SESSION['chat']->disconnect();
 400      
 401      if($cached_content == '')
 402      {
 403        $this->cache_it = true;
 404        $this->cache_max_age = intval($max_age);
 405        return false;
 406      }
 407  
 408      print $cached_content;
 409      exit;
 410    }
 411  
 412    function get_web_root()
 413    {
 414      return $this->web_root;
 415    }
 416    
 417    /**
 418    * Provides the path to the current theme
 419    *
 420    * @return string
 421    */
 422    function get_tmpl_web_path()
 423    {
 424      //path clean up
 425    //$this->tmpl_web_path = preg_replace('#//#','',$this->tmpl_web_path);
 426      return $this->tmpl_web_path;
 427    }
 428    function get_theme_path() {return $this->get_tmpl_web_path();}
 429  
 430    /**
 431    * Provides the path to the current theme
 432    *
 433    * @return string
 434    */
 435    function get_path( $path )
 436    {
 437      if($path=='THEME_PATH')
 438        return $this->get_theme_path();
 439        
 440    }
 441    
 442    /**
 443    * Provides the web path to POC
 444    *
 445    * @return string
 446    */
 447    function get_poc_web_root()
 448    {
 449      return 'http://'.$_SERVER['SERVER_NAME'].$this->poc_web_root;
 450    }
 451    
 452    /**
 453    * Provides the path to the current theme
 454    *
 455    * @return string
 456    */
 457    function get_tmpl_sys_path()
 458    {
 459      return $this->tmpl_sys_path;
 460    }
 461  
 462    /**
 463    * Sets the current theme
 464    *
 465    * @param string
 466    */
 467    function set_theme( $theme )
 468    {
 469      $old_theme = $this->theme;
 470      $this->theme = $theme;
 471      $this->_build_pathes($old_theme);
 472      $this->set_content_type();
 473      unset($old_theme);
 474      $this->_set_theme_properties();
 475    }
 476  
 477    /**
 478    * Provides all installed themes
 479    *
 480    * @return array All available themes
 481    */
 482    function get_theme_list()
 483    {
 484      $handle = opendir($this->include_path . '/templates'); 
 485      $themes = array();  
 486      while ( $dir = readdir($handle) )
 487      {
 488        if ( !is_file($dir)
 489        && $dir != '.'
 490        && $dir != '..'
 491        && $dir != 'CVS'
 492        ) {
 493           $themes[] = $dir;
 494        }
 495      }
 496      return $themes;
 497    }
 498    
 499    
 500    /**
 501    * Provides all installed themes as an option list
 502    *
 503    * @return string; html options
 504    */
 505    function get_theme_option_list()
 506    {
 507      $theme_option_list = '';
 508      $themes = array();
 509      $themes = $this->get_theme_list();
 510      
 511      foreach ($themes as $dir) {
 512        $selected =( $dir == $this->get_theme() )? 'selected="selected"':'';
 513        $theme_option_list .= NL.'<option value="'.$dir.'" '.$selected.'>'.$dir.'</option>';
 514        //$theme_option_list .= NL.'<option>'.$dir.'</option>';
 515      }
 516      return $theme_option_list;
 517    }
 518    
 519    /**
 520    * Filters the given string and replaces some placeholders
 521    *
 522    * @see get_path()
 523    * @param string
 524    * @return string
 525    */
 526    function filter_placeholder( $string )
 527    {
 528      return preg_replace( "/\@{3}([^@]*)\@{3}/e", "\$this->get_path('\\1')", $string );
 529    }
 530  }
 531  ?>

title

Description

title

Description

title

Description

title

title

Body