Action.class.php

gehe zur Dokumentation dieser Datei
00001 <?php
00002 // ---------------------------------------------------------------------------
00003 // $Id$
00004 // ---------------------------------------------------------------------------
00005 // OpenRat Content Management System
00006 // Copyright (C) 2002-2004 Jan Dankert, cms@jandankert.de
00007 //
00008 // This program is free software; you can redistribute it and/or
00009 // modify it under the terms of the GNU General Public License
00010 // as published by the Free Software Foundation; either version 2
00011 // of the License, or (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the Free Software
00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021 
00022 
00023 define('OR_NOTICE_OK'   ,'ok'     );
00024 define('OR_NOTICE_WARN' ,'warning');
00025 define('OR_NOTICE_ERROR','error'  );
00026 
00040 class Action
00041 {
00042      var $db;
00043      var $templateVars = Array();
00044      var $actionName;
00045      var $subActionName;
00046      var $actionClassName;
00047 
00048      var $writable;
00049      var $publishing;
00050      var $actionConfig;
00051      
00058      var $currentUser;
00059 
00060 
00066      function init()
00067      {
00068           global $conf;
00069           $this->writable    = !$conf['security']['readonly' ];
00070           $this->publishing  = !$conf['security']['nopublish'];
00071           $this->currentUser = Session::getUser();
00072           
00073           $this->templateVars['errors' ] = array();
00074           $this->templateVars['notices'] = array();
00075           $this->templateVars['mode'   ] = $this->getRequestVar('mode');
00076           
00077           header('Content-Language: '.$conf['language']['language_code']);
00078      }
00079 
00080 
00087      function getSessionVar( $varName )
00088      {
00089           global $SESS;
00090 
00091           if   ( !isset($SESS[ $varName ]) )
00092                return '';
00093           else return $SESS[ $varName ];
00094      }
00095 
00096 
00104      function setSessionVar( $varName,$value )
00105      {
00106           global $SESS;
00107 
00108           $SESS[ $varName ] = $value;
00109      }
00110 
00111 
00119      function getRequestVar( $varName,$transcode='' )
00120      {
00121           global $REQ;
00122 
00123           if   ( !isset($REQ[ $varName ]) )
00124                return '';
00125                
00126           
00127           switch( $transcode )
00128           {
00129                case 'abc':
00130                     $value  = strip_tags( strtolower($REQ[ $varName ] ) );
00131                     $my_set = 'abcdefghijklmnopqrstuvwxyz._-';
00132                     $first  = strtr( $value, $my_set, str_repeat('#', strlen($my_set)) );
00133                     $second = strtr( $value, $first , str_repeat('_', strlen($first )) );
00134                     return $second;
00135 
00136                case 'all':
00137                     return strip_tags( $REQ[ $varName ] );
00138                     
00139                default:
00140                     return $REQ[ $varName ];
00141           }
00142      }
00143 
00144 
00152      function hasRequestVar( $varName )
00153      {
00154           global $REQ;
00155 
00156           return( !empty($REQ[$varName]) );
00157      }
00158 
00159 
00166      function getRequestId()
00167      {
00168           if   ( $this->hasRequestVar('idvar') )
00169                return intval( $this->getRequestVar( $this->getRequestVar('idvar') ) );
00170           else
00171                return intval( $this->getRequestVar( REQ_PARAM_ID ) );
00172      }
00173 
00174 
00175 
00182      function setTemplateVar( $varName,$value )
00183      {
00184           $this->templateVars[ $varName ] = $value;
00185      }
00186 
00187 
00193      function setTemplateVars( $varList )
00194      {
00195           foreach( $varList as $name=>$value )
00196           {
00197                $this->setTemplateVar( $name,$value );
00198           }
00199      }
00200 
00201 
00208      function addValidationError( $name,$message="COMMON_VALIDATION_ERROR",$vars=array(),$log=array() )
00209      {
00210           if   ( !empty($message) )
00211                $this->addNotice('','',$message,OR_NOTICE_ERROR,$vars,$log);
00212 
00213           $this->templateVars['errors'][] = $name;
00214      }
00215 
00216      
00227      function addNotice( $type,$name,$text,$status=OR_NOTICE_OK,$vars=array(),$log=array() )
00228      {
00229           if   ( !is_array($log))
00230                $log = array($log);
00231 
00232           if   ( !is_array($vars))
00233                $vars = array($vars);
00234                
00235           if   ( $status === true )
00236                $status = OR_NOTICE_OK;
00237           elseif    ( $status === false )
00238                $status = OR_NOTICE_ERROR;
00239 
00240           $this->templateVars['notices'][] = array('type'=>$type,
00241                                                  'name'=>$name,
00242                                                    'key'=>'NOTICE_'.$text,
00243                                                    'vars'=>$vars,
00244                                                    'text'=>lang('NOTICE_'.$text,$vars),
00245                                                    'log'=>$log,
00246                                                    'status'=>$status);
00247      }
00248 
00249 
00250      
00256      function message( $title='ERROR',$add_info='' )
00257      {
00258           Logger::warn( 'creating error message, info='.$add_info );
00259 
00260           $this->setTemplateVar( 'title',lang( $title         ) );
00261           $this->setTemplateVar( 'text' ,lang( $title.'_DESC' ) );
00262           $this->setTemplateVar( 'info' ,$add_info );
00263           
00264           $this->forward('message');
00265      }
00266 
00267 
00277      function forward( $unbenutzterParameter = "" )
00278      {
00279           if   ( isset($this->actionConfig[$this->subActionName]['direct']) )
00280                exit; // Die Ausgabe ist bereits erfolgt (z.B. Bin�rdateien o. WebDAV)
00281 
00282           // Pruefen, ob HTTP-Header gesendet wurden. Dies deutet stark darauf hin, dass eine
00283           // PHP-Fehlermeldung ausgegeben wurde. In diesem Fall wird hier abgebrochen.
00284           // Weitere Ausgabe wuerde keinen Sinn machen, da wir nicht wissen, was
00285           // passiert ist.
00286           if   ( headers_sent() )
00287                Http::serverError("Some server error messages occured - see above - CMS canceled.");
00288                
00289           $httpAccept = getenv('HTTP_ACCEPT');
00290           $types = explode(',',$httpAccept);
00291           
00292           // Weitere Variablen anreichern.
00293           $this->templateVars['session'] = array('name'=>session_name(),'id'=>session_id());
00294           $this->templateVars['version'] = OR_VERSION;
00295           
00296           if   ( sizeof($types)==1 && in_array('application/json',$types) || $this->getRequestVar('output')=='json' )
00297           {
00298                require_once( OR_SERVICECLASSES_DIR."JSON.class.".PHP_EXT );
00299                $json = new JSON();
00300                header('Content-Type: application/json');
00301                echo $json->encode( $this->templateVars );
00302                exit;
00303           }
00304 
00305           if   ( sizeof($types)==1 && in_array('application/xml',$types) || $this->getRequestVar('output')=='xml' )
00306           {
00307                require_once( OR_SERVICECLASSES_DIR."XML.class.".PHP_EXT );
00308                $xml = new XML();
00309                $xml->root = 'server'; // Name des XML-root-Elementes
00310                header('Content-Type: application/xml');
00311                echo $xml->encode( $this->templateVars );
00312                exit;
00313           }
00314 
00315           $this->setMenu();
00316           
00317           $tplName = $this->actionName.'/'.$this->subActionName;
00318 
00319                
00320           if   ( isset($this->actionConfig[$this->subActionName]['action']) )
00321                $tplName = $this->actionConfig[$this->subActionName]['action'].'/'.$this->subActionName;
00322 
00323           if   (isset($this->actionConfig[$this->subActionName]['alias']))
00324                $tplName = (method_exists(new ObjectAction(),$this->subActionName)?'object':$this->actionName).'/'.$this->actionConfig[$this->subActionName]['alias'];
00325                
00326           if   (isset($this->actionConfig[$this->subActionName]['target']))
00327                $targetSubActionName = $this->actionConfig[$this->subActionName]['target'];
00328 
00329           if   ( isset($this->actionConfig[$this->subActionName]['menu']))
00330                $windowTitle = 'menu_title_'.$this->actionName.'_'.$this->actionConfig[$this->subActionName]['menu'];
00331 
00332           global $conf;
00333           global $REQ;
00334           global $PHP_SELF;
00335           global $HTTP_SERVER_VARS;
00336           global $image_dir;
00337                  
00338           $tplName = str_replace( '_','/',$tplName );
00339           
00340           $tplFileName = $tplName.'.tpl.'.PHP_EXT;
00341           $conf_php = PHP_EXT;
00342      
00343           // ?bertragen der Array-Variablen in den aktuellen Kontext
00344           //
00345           extract( $this->templateVars );
00346 
00347           // ?bertragen der Array-Variablen in den aktuellen Kontext
00348           //
00349           
00350           if   ( count($errors)>0 )
00351                extract( $REQ );
00352      
00353           // Setzen einiger Standard-Variablen
00354           //
00355           $tpl_dir    = OR_THEMES_DIR.$conf['interface']['theme'].'/pages/html/';
00356           $image_dir  = OR_THEMES_EXT_DIR.$conf['interface']['theme'].'/images/';
00357      
00358           $user = Session::getUser();
00359           
00360           if   ( strpos($conf['interface']['style']['extend'],'/')===false )
00361                $root_stylesheet = OR_THEMES_EXT_DIR.$conf['interface']['theme'].'/css/'.$conf['interface']['style']['extend'].'.css';
00362           else
00363                $root_stylesheet = $style['extend'];
00364                
00365           if   ( !is_object($user) )
00366                $user_stylesheet = OR_THEMES_EXT_DIR.$conf['interface']['theme'].'/css/'.$conf['interface']['style']['default'].'.css';
00367           else
00368                $user_stylesheet = OR_THEMES_EXT_DIR.$conf['interface']['theme'].'/css/'.$user->style.'.css';
00369           
00370           $self = $HTTP_SERVER_VARS['PHP_SELF'];
00371      
00372           $tplFileName = str_replace( '_','/',$tplFileName );
00373 
00374           if   ( !empty($conf['interface']['override_title']) )
00375                $cms_title = $conf['interface']['override_title'];
00376           else
00377                $cms_title = OR_TITLE.' '.OR_VERSION;
00378 
00379           $charset = $this->getCharset();
00380                
00381           $showDuration = $conf['interface']['show_duration'];
00382 
00383           $subActionName = $this->subActionName;
00384           $actionName    = $this->actionName;
00385           $requestId     = $this->getRequestId();
00386           
00387           if   ( $conf['theme']['compiler']['enable'] )
00388           {
00389                $te = new TemplateEngine();
00390                $te->compile( $tplName );
00391                unset($te);
00392           }
00393 
00394           // Einbinden des Templates
00395           require( $tpl_dir.$tplFileName );
00396           
00397           exit;
00398      }
00399      
00400      
00406      function callSubAction( $subActionName )
00407      {
00408           if   ( in_array($this->actionName,array('page','file','link','folder')) )
00409                Session::setSubaction( $subActionName );
00410 
00411           $this->subActionName = $subActionName;       
00412 
00413           Logger::trace("next subaction is '$subActionName'");
00414           
00415           $this->$subActionName();
00416      }
00417 
00418 
00423      function userIsAdmin()
00424      {
00425           $user = Session::getUser();
00426           return $user->isAdmin;
00427      }
00428 
00429 
00430 
00435      function getUserFromSession()
00436      {
00437           return Session::getUser();
00438      }
00439 
00440 
00441      
00463      function lastModified( $time )
00464      {
00465           $user = Session::getUser();
00466           if   ( $user->loginDate > $time )
00467                // Falls Benutzer-Login nach letzter �nderung.
00468                // Zweck: Nach einem Login sollte mind. 1x jede Seite neu geladen werden, dies
00469                // Ist z.B. nach einer Style-�nderung durch den Benutzer notwendig.
00470                // Falls aus Versehen doch einmal zuviel gecacht wurde, kann man das durch ein
00471                // Neu-Login beheben. 
00472                $time = $user->loginDate;
00473 
00474           // Conditional-Get eingeschaltet?
00475           global $conf;
00476           if   ( ! $conf['cache']['conditional_get'] )
00477                return;
00478 
00479           $lastModified = substr(date('r',$time-date('Z')),0,-5).'GMT';
00480           $etag         = '"'.md5($lastModified).'"';
00481 
00482           // Header senden
00483           header('Last-Modified: '.$lastModified );
00484           header('ETag: '         .$etag          );
00485           
00486           // Die vom Interpreter sonst automatisch gesetzten
00487           // Header uebersteuern
00488           header('Cache-Control: must-revalidate');
00489           header('Pragma:');
00490 
00491           // See if the client has provided the required headers
00492           $if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ? stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) : false;
00493           $if_none_match     = isset($_SERVER['HTTP_IF_NONE_MATCH']    ) ? stripslashes($_SERVER['HTTP_IF_NONE_MATCH']    ) :     false;
00494 
00495           if   ( !$if_modified_since && !$if_none_match )
00496                return;
00497 
00498           // At least one of the headers is there - check them
00499           if   ( $if_none_match && $if_none_match != $etag )
00500                return; // etag is there but doesn't match
00501 
00502 
00503           if   ( $if_modified_since && $if_modified_since != $lastModified )
00504                return; // if-modified-since is there but doesn't match
00505 
00506           // Der entfernte Browser bzw. Proxy holt die Seite nun aus seinem Cache 
00507           header('HTTP/1.0 304 Not Modified');
00508           exit;  // Sofortiges Skript-Ende
00509      }
00510 
00511 
00512 
00516      function maxAge( $max=3600 ) 
00517      {
00518           // Die Header "Last-Modified" und "ETag" wurden bereits in der
00519           // Methode "lastModified()" gesetzt.
00520           
00521           header('Expires: '.substr(date('r',time()-date('Z')+$max),0,-5).'GMT' );
00522           header('Pragma: '); // 'Pragma' ist Bullshit und
00523                               // wird von den meisten Browsern ignoriert.
00524           header('Cache-Control: public, max-age='.$max.", s-maxage=".$max);
00525      }    
00526      
00527      
00528      
00529      function setMenu()
00530      {
00531           if   (!isset($this->actionConfig[$this->subActionName]['menu']))
00532                return;
00533           $windowMenu = array();
00534           $name       = $this->actionConfig[$this->subActionName]['menu'];
00535           $menuList   = explode(',',$this->actionConfig['menu'][$name]);
00536 
00537           if   ( isset($this->actionConfig[$this->subActionName]['menuaction']))
00538                $actionName = $this->actionConfig[$this->subActionName]['menuaction'];
00539           else
00540                $actionName = $this->subActionName;
00541           
00542           foreach( $menuList as $menuName )
00543           {
00544                if   ( isset($this->actionConfig[$menuName]['alias']) )
00545                     $menuText = 'menu_'.$this->actionName.'_'.$this->actionConfig[$menuName]['alias'];
00546                else
00547                     $menuText = 'menu_'.$this->actionName.'_'.$menuName;
00548                
00549                
00550                $menuKey = 'accesskey_window_'.$menuName;
00551                
00552 //             Logger::trace("testing menu $menuName");
00553                $menuEntry = array('subaction'=>$menuName,
00554                                'text'     =>$menuText,
00555                                'title'    =>$menuText.'_DESC',
00556                                'key'      =>$menuKey );
00557                                
00558                if   ( $this->checkMenu($menuName) )
00559                     $menuEntry['url'] = Html::url($actionName,$menuName,$this->getRequestId());
00560                     
00561                $windowMenu[] = $menuEntry;
00562           }
00563           $this->setTemplateVar('windowMenu',$windowMenu);
00564      }
00565      
00566      
00567      
00578      function checkMenu( $name )
00579      {
00580           // Standard: Alle Men�punkt sind aktiv.
00581           return true;
00582      }
00583      
00584      
00585      
00591      function getCharset()
00592      {
00593           $db = db_connection();
00594           
00595           if   ( isset($db->conf['charset']) )
00596                return $db->conf['charset'];
00597           else
00598                return lang('CHARSET');
00599      }
00600      
00601      
00607      function isEditable()
00608      {
00609           return isset($this->actionConfig[$this->subActionName]['editable']) && $this->actionConfig[$this->subActionName]['editable']; 
00610      }
00611 
00612      
00618      function isEditMode()
00619      {
00620           return !$this->isEditable() || $this->getRequestVar('mode')=='edit' || (isset($this->templateVars) && $this->templateVars['mode']=='edit'); 
00621      }
00622 }
00623 
00624 ?>

Erzeugt am Thu May 14 00:55:48 2009 für OpenRat von  doxygen 1.5.8