aliroAbstractRequest.php

Go to the documentation of this file.
00001 <?php
00002 
00003 abstract class aliroAbstractRequest {
00004     // Singleton object holder - will contain the single instance of aliroUserRequest or aliroAdminRequest
00005     protected static $instance = null;
00006 
00007     // Request attributes
00008     protected $option = '';
00009     protected $isHome = false;
00010     protected $formcheck = 0;
00011     protected $component_name = '';
00012     protected $bestmatch = null;
00013     protected $aliroVersion = '';
00014     protected $urlerror = false;
00015     protected $title = '';
00016     protected $metatags = array();
00017     protected $customtags = array();
00018     protected $templateName = '';
00019     protected $templateObject = null;
00020     protected $do_gzip = false;
00021     protected $error_message = array();
00022     protected $overlib = false;
00023 
00024     // Core singleton objects providing key information resources
00025     protected $user = null;
00026     protected $critical = null;
00027     protected $configuration = null;
00028     protected $pathway = null;
00029     protected $version = null;
00030 
00031     // Singleton "handler" objects
00032     protected $mhandler = null;
00033     protected $chandler = null;
00034     protected $xhandler = null;
00035     protected $purifier = null;
00036 
00037 
00038     protected function __construct () {
00039         // This is not necessarily right - but should avoid getting a notice
00040         if (function_exists('date_default_timezone_set')) date_default_timezone_set('UTC');
00041         @set_magic_quotes_runtime( 0 );
00042         //require_once(criticalInfo::getInstance()->absolute_path.'/includes/phpgettext/phpgettext.class.php');
00043         // Note that none of the things called here can use aliroAbstractRequest!
00044         // Otherwise, a loop will be created and Aliro will fail!
00045         // Ensure session started straight away
00046 
00047         aliroSessionFactory::getSession();
00048         // Check for problems with globals - do after session has started to be able to handle session variables
00049         $this->handleGlobals();
00050         $this->setUsefulObjects();
00051         if (extension_loaded('zlib') AND $this->configuration->getCfg('gzip')) $this->do_gzip = true;
00052         $this->setHandlers();
00053         if (count($_POST)) $this->fixPostItems();
00054         $this->option = $this->component_name = strtolower($this->getParam($_REQUEST, 'option'));
00055         if ($this->option != 'login' AND $this->option != 'logout') $this->user = aliroUser::getInstance();
00056         if ($message = $this->getParam($_REQUEST, 'mosmsg')) {
00057             $severity = $this->getParam($_REQUEST, 'severity', _ALIRO_ERROR_INFORM);
00058             $this->setErrorMessage ($message, intval($severity));
00059         }
00060     }
00061 
00062     private function setHandlers () {
00063         $this->mhandler = aliroMenuHandler::getInstance();
00064         $this->chandler = aliroComponentHandler::getInstance();
00065         $this->xhandler = aliroExtensionHandler::getInstance();
00066     }
00067 
00068     private function setUsefulObjects () {
00069         $this->critical = criticalInfo::getInstance();
00070         // Initiate HTML Purifier autoloading
00071         if (function_exists('spl_autoload_register') AND function_exists('spl_autoload_unregister')) {
00072             // HTML Purifier needs unregister for our pre-registering functionality
00073             HTMLPurifier_Bootstrap::registerAutoload();
00074             // Be polite and ensure that userland autoload gets retained
00075             spl_autoload_register('__autoload');
00076         }
00077         // End of HTML Purifier related code       
00078         $this->version = version::getInstance();
00079         $this->aliroVersion = $this->version->RELEASE.'/'.$this->version->DEV_STATUS.'/'.$this->version->DEV_LEVEL;
00080         $this->configuration = aliroCore::getInstance();
00081         $this->configuration->fixLanguage();
00082     }
00083 
00084     protected function fixPostItems () {
00085         $this->formcheck = $this->checkFormStamp();
00086         if (_ALIRO_FORM_CHECK_EXPIRED == $this->formcheck OR _ALIRO_FORM_CHECK_FAIL == $this->formcheck) {
00087             $this->setErrorMessage(T_('Sorry, your request used an invalid or expired form, please try again'));
00088             $_POST = array();
00089         }
00090         if (_ALIRO_FORM_CHECK_REPEAT == $this->formcheck) {
00091             $this->setErrorMessage(T_('This form submission has already been processed'));
00092             $_POST = array();
00093         }
00094         if ($params = $this->getParam($_POST, 'params', null, _MOS_ALLOWHTML)) {
00095             $pobject = new aliroParameters();
00096             $pobject->processInput($params);
00097             $_POST['params'] = $pobject->asString();
00098         }
00099         if (isset($_POST['alironstask']) AND (!isset($_REQUEST['task']) OR !$_REQUEST['task'])) $_POST['task'] = $_REQUEST['task'] = $_POST['alironstask'];
00100     }
00101 
00102     protected function __clone () {
00103         // Declared to enforce singleton
00104     }
00105 
00106     public function __call ($method, $args) {
00107         // May want to add language
00108         foreach (array($this->configuration, $this->pathway) as $object) {
00109             if (method_exists($object, $method)) return call_user_func_array(array($object, $method), $args);
00110         }
00111         trigger_error (sprintf(T_('Invalid method call on aliroRequest - %s'), $method));
00112         echo aliroRequest::trace();
00113         return null;
00114     }
00115 
00116     public function __get ($property) {
00117         if (isset($this->critical->$property)) return $this->critical->$property;
00118         trigger_error (sprintf(T_('Invalid property request on aliroAbstractRequest - %s'), $property));
00119         return null;
00120     }
00121 
00122     private function handleGlobals () {
00123         $superglobals = array($_SERVER, $_ENV, $_FILES, $_COOKIE, $_POST, $_GET, $_SESSION);
00124 
00125         // Emulate register_globals on
00126         if (!ini_get('register_globals') AND aliroCore::getInstance()->getCfg('register_globals')) {
00127             foreach ($_GET as $key=>$value) {
00128                 if (!isset($GLOBALS[$key])) $GLOBALS[$key]=$value;
00129             }
00130             foreach ($_POST as $key=>$value) {
00131                 if (!isset($GLOBALS[$key])) $GLOBALS[$key]=$value;
00132             }
00133         }
00134         // Emulate register_globals off
00135         elseif (ini_get('register_globals') AND !$this->getCfg('register_globals')) {
00136             foreach ($superglobals as $superglobal) {
00137                 foreach ($superglobal as $key=>$value) {
00138                     unset( $GLOBALS[$key]);
00139                 }
00140             }
00141         }
00142     }
00143 
00144     public function getComponentName () {
00145         return $this->component_name;
00146     }
00147 
00148     public function showHead () {
00149         $html = aliroSEF::getInstance()->getHead($this->title, $this->metatags, $this->customtags);
00150         if ($this->getCfg('sef')) $html .= "<base href=\"{$this->getCfg('live_site')}/\" />\r\n";
00151         if ( $this->user->id ) $html .= "<script src='{$this->getCfg('live_site')}/includes/js/alirojavascript.js' type='text/javascript'></script>";
00152         return $html;
00153     }
00154 
00155     public function getFavIcon () {
00156         // Default favourites icon
00157         return $this->getCfg('live_site').'/images/favicon.ico';
00158     }
00159 
00160     public function getItemid () {
00161         return isset($this->bestmatch) ? $this->bestmatch->id : 0;
00162     }
00163 
00164     public function getOption () {
00165         return $this->option;
00166     }
00167 
00168     public function redirect ($url='', $message='', $severity=_ALIRO_ERROR_INFORM) {
00169         if (is_null($url) OR !$url) $url = '';
00170         else {
00171             $url = $this->stripFromURL($url, 'mosmsg');
00172             $url = $this->stripFromURL($url, 'severity');
00173         }
00174         if ($message AND !$url) $url = 'index.php';
00175         if (strpos($url, 'http') !== 0) {
00176             if ($url AND $url[0] != '/') $url = '/'.$url;
00177             $url = $this->siteBaseURL.$url;
00178         }
00179         if ($message) {
00180             $url .= (strpos($url, '?') ? '&' : '?').'mosmsg='.urlencode($message);
00181             if ($severity) $url .= '&severity='.intval($severity);
00182         }
00183         @session_write_close();
00184         if (headers_sent()) printf (T_('Please click on %s this link %s to continue'), "<a href='$url'>", '</a>');
00185         else {
00186             @ob_end_clean(); // clear output buffer
00187             header( "Location: $url" );
00188         }
00189         exit();
00190     }
00191 
00192     public function redirectSame ($message='', $severity=_ALIRO_ERROR_INFORM) {
00193         $url = 'index.php?'.$_SERVER['QUERY_STRING'];
00194         $this->redirect ($url, $message, $severity);
00195     }
00196 
00197     public function stripFromURL ($url, $property) {
00198         if ($position = strpos($url, $property)) {
00199             if ($endpos = strpos($url, '&', $position)) $url = substr($url, 0, $position).substr($url, $endpos+1);
00200             else $url = substr($url, 0, $position-1);
00201         }
00202         return $url;
00203     }
00204 
00205     public function setErrorMessage ($message, $severity=_ALIRO_ERROR_FATAL) {
00206         $this->error_message[$severity][] = $message;
00207     }
00208 
00209     public function isErrorLevelSet ($severity) {
00210         return isset($this->error_message[$severity]);
00211     }
00212 
00213     public function pullErrorMessages () {
00214         $messages = $this->error_message;
00215         $this->error_message = array();
00216         return $messages;
00217     }
00218 
00219     public function getUserState( $var_name ) {
00220         return is_array($_SESSION["aliro_{$this->prefix}state"]) ? $this->getParam($_SESSION["aliro_{$this->prefix}state"], $var_name) : null;
00221     }
00222 
00223     public function setUserState( $var_name, $var_value ) {
00224         $_SESSION["aliro_{$this->prefix}state"][$var_name] = $var_value;
00225     }
00226 
00227     protected function isUserStateSet ($var_name) {
00228         return isset($_SESSION["aliro_{$this->prefix}state"][$var_name]);
00229     }
00230 
00231     public function getUserStateFromRequest($var_name, $req_name, $var_default=null) {
00232         if (isset($_REQUEST[$req_name])) {
00233             if ((string) $var_default == (string) (int) $var_default) $_REQUEST[$req_name] = intval($_REQUEST[$req_name]);
00234             $this->setUserState($var_name, $_REQUEST[$req_name]);
00235         }
00236         elseif (isset($var_default) AND !$this->isUserStateSet($var_name)) $this->setUserState($var_name, $var_default);
00237         return $this->getUserState($var_name);
00238     }
00239 
00240     public function makeFormStamp () {
00241         $formid = md5(uniqid(mt_rand(), true));
00242         $checker = md5(uniqid(mt_rand(), true));
00243         $_SESSION['aliro_formid_'.$formid] = $checker;
00244         $_SESSION['aliro_formdone_'.$formid] = 0;
00245         $html = <<<FORM_STAMP
00246         <input type="hidden" name="aliroformid" value="$formid" />
00247         <input type="hidden" name="alirochecker" value="$checker" />
00248 FORM_STAMP;
00249         return $html;
00250     }
00251 
00252     public function getFormCheckError () {
00253         $messages = array (
00254         _ALIRO_FORM_CHECK_EXPIRED => T_('Sorry, the form you used has expired, please try again'),
00255         _ALIRO_FORM_CHECK_FAIL => T_('Sorry, the form you used is invalid'),
00256         _ALIRO_FORM_CHECK_NULL => T_('Sorry, the form you used did not have a required authentication'),
00257         _ALIRO_FORM_CHECK_REPEAT => T_('The form you used has already been processed')
00258         );
00259         if ($this->formcheck) {
00260             if (isset($messages[$this->formcheck])) return $messages[$this->formcheck];
00261             else return T_('Internal error - invalid form check value');
00262         }
00263         else return '';
00264     }
00265 
00266     private function checkFormStamp () {
00267         $formid = $this->getParam($_POST, 'aliroformid');
00268         $checker = $this->getParam($_POST, 'alirochecker');
00269         if ($formid) {
00270             if (!isset($_SESSION['aliro_formid_'.$formid])) return _ALIRO_FORM_CHECK_EXPIRED;
00271             if ($_SESSION['aliro_formid_'.$formid] == $checker) {
00272                 if ($_SESSION['aliro_formdone_'.$formid]) return _ALIRO_FORM_CHECK_REPEAT;
00273                 else {
00274                     $_SESSION['aliro_formdone_'.$formid] = 1;
00275                     return _ALIRO_FORM_CHECK_OK;
00276                 }
00277             }
00278             else {
00279                 $this->setErrorMessage(T_('Form failed consistency check'), _ALIRO_ERROR_FATAL);
00280                 return _ALIRO_FORM_CHECK_FAIL;
00281             }
00282         }
00283         else return _ALIRO_FORM_CHECK_NULL;
00284     }
00285 
00286     public function getParam( &$arr, $name, $def=null, $mask=0 ) {
00287         if (isset( $arr[$name] )) {
00288             if (is_array($arr[$name])) foreach ($arr[$name] as $key=>$element) {
00289                 $result[$key] = $this->getParam ($arr[$name], $key, $def, $mask);
00290             }
00291             else {
00292                 $result = $arr[$name];
00293                 if (!($mask&_MOS_NOTRIM)) $result = trim($result);
00294                 if (!is_numeric($result)) {
00295                     if (get_magic_quotes_gpc() AND !($mask & _MOS_NOSTRIP)) $result = stripslashes($result);
00296                     if (!($mask&_MOS_ALLOWRAW) AND is_numeric($def)) $result = $def;
00297                     elseif ($result) {
00298                         if ($mask & _MOS_ALLOWHTML) $result = $this->doPurify($result);
00299                         else {
00300                             $result = strip_tags($result);
00301                             // $result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
00302                         }
00303                     }
00304                 }
00305             }
00306             return $result;
00307         }
00308         return $def;
00309     }
00310 
00311     public function doPurify ($string) {
00312         if (null == $this->purifier) {
00313             $config = HTMLPurifier_Config::createDefault();
00314             if (criticalInfo::getInstance()->isAdmin) $config->set('HTML', 'Trusted', true);
00315             $this->purifier = new HTMLPurifier($config);
00316         }
00317         return $this->purifier->purify($string);
00318     }
00319 
00320     // Cannot be applied to items that return an array, only to a scalar
00321     public function getStickyParam (&$arr, $name, $def=null, $mask=0) {
00322         $var = 'aliro_sticky_'.$this->getComponentName().'_'.$name;
00323         return $this->getSticky ($var, $arr, $name, $def=null, $mask=0);
00324     }
00325 
00326     public function getStickyAliroParam (&$arr, $name, $def=null, $mask=0) {
00327         $var = 'aliro_sticky_aliro_'.$name;
00328         return $this->getSticky ($var, $arr, $name, $def=null, $mask=0);
00329     }
00330 
00331     private function getSticky ($var, &$arr, $name, $def, $mask) {
00332         if ((!isset($arr[$name]) OR !$arr[$name]) AND isset($_SESSION[$var])) return $_SESSION[$var];
00333         $provided = $this->getParam($arr, $name, $def, $mask);
00334         if ($provided) $_SESSION[$var] = $provided;
00335         return $provided;
00336     }
00337 
00338     public function unstick ($name) {
00339         $var = 'aliro_sticky_'.$this->getComponentName().'_'.$name;
00340         if (isset($_SESSION[$var])) unset ($_SESSION[$var]);
00341     }
00342 
00343     public function getTemplate() {
00344         if (!$this->templateName) $this->templateName = aliroTemplateHandler::getInstance()->getDefaultTemplateName();
00345         return $this->templateName;
00346     }
00347 
00348     public function setPageTitle ($title=null) {
00349         if ($this->getCfg('pagetitles')) {
00350             $title = trim($title);
00351             $base = $this->getCfg('sitename');
00352             $this->title = $title ?  $title.' - '.$base : $base;
00353         }
00354     }
00355 
00356     public function getPageTitle () {
00357         return $this->title;
00358     }
00359 
00360     protected function fix_metatag ($operation, $name, $content, $prepend='', $append='') {
00361         $content = trim(htmlspecialchars($content));
00362         if (!$content) return;
00363         $name = trim(htmlspecialchars($name));
00364         $prepend = trim($prepend);
00365         $append = trim($append);
00366         if ('new' == $operation) $this->metatags[$name] = array($content, $prepend, $append);
00367         else {
00368             $tag = isset($this->metatags[$name]) ?  $this->metatags[$name] : array('', '', '');
00369             if ('pre' == $operation) $tag[0] = $content.$tag[0];
00370             else $tag[0] = $content.(($tag[0] AND $content) ? ',' : '').$tag[0];
00371             $this->metatags[$name] = $tag;
00372         }
00373     }
00374 
00375     public function addMetaTag($name, $content, $prepend='', $append='') {
00376         $this->fix_metatag ('new', $name, $content, $prepend, $append);
00377     }
00378 
00379     public function appendMetaTag ($name, $content) {
00380         $this->fix_metatag ('post', $name, $content);
00381     }
00382 
00383     public function prependMetaTag ($name, $content) {
00384         $this->fix_metatag ('pre', $name, $content);
00385     }
00386 
00387     public function addCustomHeadTag ($html) {
00388         $this->customtags[] = trim ($html);
00389     }
00390 
00391     public function addScript ($relativeFile) {
00392         $link = <<<SCRIPT_LINK
00393 
00394     <script type="text/javascript" src="{$this->getCfg('live_site')}$relativeFile"></script>
00395 
00396 SCRIPT_LINK;
00397 
00398         $this->addCustomHeadTag($link);
00399     }
00400 
00401     public function addCSS ($relativeFile, $media='screen') {
00402         $link = <<<CSS_LINK
00403 
00404     <link href="{$this->getCfg('live_site')}$relativeFile" rel="stylesheet" type="text/css" media="$media" />
00405 
00406 CSS_LINK;
00407 
00408         $this->addCustomHeadTag($link);
00409     }
00410 
00411     public function setMetadataInCache (&$cache_object) {
00412         $cache_object->title = $this->title;
00413         $cache_object->metatags = $this->metatags;
00414         $cache_object->customtags = $this->customtags;
00415     }
00416 
00417     public function setMetadataFromCache ($cache_object) {
00418         $this->title = $cache_object->title;
00419         $this->metatags = $cache_object->metatags;
00420         $this->customtags = $cache_object->customtags;
00421     }
00422 
00423     public function requestOverlib () {
00424         if ($this->overlib) return;
00425         $html = <<<OVERLIB
00426         <script type="text/javascript" src="{$this->getCfg('live_site')}/includes/js/overlib_mini.js"></script>
00427 OVERLIB;
00428         $this->addCustomHeadTag ($html);
00429         $this->overlib = true;
00430     }
00431 
00432     public function divOverlib () {
00433         if ($this->overlib) return '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:10000;"></div>';
00434         return '';
00435     }
00436 
00437     public function getDebug () {
00438         if ($this->getCfg('debug')) {
00439             $database = aliroDatabase::getInstance();
00440             $log = $database->getLogged();
00441             $database = aliroCoreDatabase::getInstance();
00442             $log .= $database->getLogged();
00443             $loader = aliroDebug::getInstance();
00444             $log .= $loader->getLogged();
00445             return $log;
00446         }
00447         else return '';
00448     }
00449 
00450     public function getCustomTags () {
00451         if (count($this->customtags)) return implode("\n", $this->customtags);
00452         return '';
00453     }
00454 
00455     public function getComponentObject () {
00456         if ($this->core_item) {
00457             $component = new aliroComponent();
00458             $component->option = $component->extformalname = $this->core_item;
00459             $component->name = $this->core_item;
00460             $component->adminclass = 'aliroComponentAdminManager';
00461         }
00462         else $component = $this->chandler->getComponentByFormalName($this->option);
00463         return $component;
00464     }
00465 
00466     protected function invokeComponent ($menu=null) {
00467         try {
00468             $this->chandler->startBuffer();
00469             if (!$this->option AND $menu AND $menu->component) $this->option = $menu->component;
00470             $component = $this->getComponentObject();
00471             $message = T_('At entry of aliroRequest::invokeComponent');
00472             if (!$this->urlerror AND ($this->option OR $this->core_item)) {
00473                 $componentname = $this->option? $this->option : $this->core_item;
00474                 define ('_ALIRO_COMPONENT_NAME', $componentname);
00475                 if ($component) {
00476                     if ($this->pathway) {
00477                         $cname = aliroSEF::getInstance()->sefComponentName($component->option);
00478                         $this->pathway->addItem($cname, 'index.php?option='.$component->option);
00479                     }
00480                     $class = $this->getComponentClass($component);
00481                     if ($class) $this->standardCall ($component, $class, $menu);
00482                     else $this->urlerror = $this->retroCall ($menu);
00483                     if ($this->urlerror) trigger_error(T_('Retro call was unable to find component: ').$this->option);
00484                 }
00485                 else {
00486                     $this->urlerror = true;
00487                     $message = T_('Unable to find component object for ').$this->option;
00488                 }
00489             }
00490             else {
00491                 $this->urlerror = true;
00492                 if ($this->chandler->componentCount() AND $this->mhandler->getMenuCount('mainmenu')) {
00493                     $message = sprintf(T_('Failed on urlerror from SEF or no option (%s)'), $this->option);
00494                 }
00495             }
00496             if ($this->urlerror) new aliroPage404($message);
00497             $this->chandler->endBuffer();
00498         } catch (databaseException $exception) {
00499             $target = $this->core_item ? $this->core_item : $this->option;
00500             $message = sprintf(T_('A database error occurred on %s at %s while processing %s'), date('Y-M-d'), date('H:i:s'), $target);
00501             $errorkey = "SQL/{$exception->getCode()}/$target/$exception->dbname/{$exception->getMessage()}/$exception->sql";
00502             aliroErrorRecorder::getInstance()->recordError($message, $errorkey, $message, $exception);
00503             $this->redirect('', $message, _ALIRO_ERROR_FATAL);
00504         }
00505     }
00506 
00507     protected function standardCall ($component, $class, $menu) {
00508         $worker = new $class ($component, 'Aliro', $this->aliroVersion, $menu);
00509         $worker->activate();
00510     }
00511 
00512     protected function retroCall ($menu) {
00513         $mainframe = mosMainFrame::getInstance();
00514         $path = $mainframe->getPath($this->path_side);
00515         if (!$path) return true;
00516         $this->invokeRetroCode($path, null, $menu);
00517         return false;
00518     }
00519 
00520     public function invokeRetroCode ($path, $function=null, $menu=null) {
00521         $GLOBALS['task'] = $task = $this->getParam($_REQUEST, 'task');
00522         $GLOBALS['act'] = $act = $this->getParam($_REQUEST, 'act');
00523         $GLOBALS['id'] = $id = $this->getParam($_REQUEST, 'id', 0);
00524         $GLOBALS['section'] = $section = $this->getParam($_REQUEST, 'section');
00525         require_once ($this->critical->absolute_path.'/includes/mambofunc.php');
00526         $GLOBALS['acl'] = $acl = aliroAuthoriser::getInstance();
00527         $GLOBALS['my'] = $my = aliroUser::getInstance();
00528         $GLOBALS['gid'] = $gid = $my->gid;
00529         $GLOBALS['mainframe'] = $mainframe = mosMainFrame::getInstance();
00530         $GLOBALS['database'] = $database = aliroDatabase::getInstance();
00531         $GLOBALS['Itemid'] = $Itemid = $this->getItemid();
00532         $GLOBALS['option'] = $option = $this->option;
00533         $GLOBALS['_VERSION'] = $this->version;
00534 
00535         // This will not do - what should happen??
00536         $GLOBALS['mosConfig_lang'] = 'english';
00537 
00538         error_reporting(E_ALL);
00539         $this->globalizeConfig();
00540         foreach ($GLOBALS as $key=>$value) if ('mosConfig_' == substr($key,0,10)) $$key = $value;
00541         require($path);
00542         if ($function) $function();
00543         error_reporting(E_ALL|E_STRICT);
00544     }
00545 
00546 }

Generated on Wed May 14 13:01:55 2008 for ALIRO by  doxygen 1.5.5