Changeset View
Changeset View
Standalone View
Standalone View
core/kernel/console_application.php
- This file was added.
Property | Old Value | New Value |
---|---|---|
svn:eol-style | null | LF |
svn:keywords | null | Id |
<?php | |||||
class ConsoleApplication extends kApplication | |||||
{ | |||||
/** | |||||
* Store for query data emulation. | |||||
* | |||||
* @var Params | |||||
*/ | |||||
private $_queryStorage; | |||||
/** | |||||
* Allowed query vars. | |||||
* | |||||
* @var array | |||||
*/ | |||||
private $_allowedQueryVars = array( | |||||
'pass_through', | |||||
'passed', | |||||
'all_passed', | |||||
'm_lang', | |||||
'm_theme', | |||||
'm_cat_id', | |||||
'm_cat_page', | |||||
'm_opener', | |||||
'm_wid', | |||||
'lang.current_id', | |||||
'theme.current_id', | |||||
'admin', | |||||
); | |||||
/** | |||||
* Storage for session emulation. | |||||
* | |||||
* @var Params | |||||
*/ | |||||
private $_sessionStorage; | |||||
/** | |||||
* Allowed session vars. | |||||
* | |||||
* @var array | |||||
*/ | |||||
private $_allowedSessionVars = array( | |||||
'curr_iso', | |||||
'visit_id', | |||||
'user_id', | |||||
); | |||||
/** | |||||
* Handles initialization part before "OnAfterConfigRead" events are called. | |||||
* | |||||
* @return void | |||||
*/ | |||||
protected function initBeforeOnAfterConfigRead() | |||||
{ | |||||
$this->_queryStorage = $this->makeClass('Params'); | |||||
$this->_sessionStorage = $this->makeClass('Params'); | |||||
$this->SetVar('passed', 'm'); | |||||
$this->SetVar('all_passed', 'm'); | |||||
$this->SetVar('m_cat_page', 1); | |||||
$this->SetVar('m_opener', 's'); | |||||
$this->StoreVar('user_id', USER_CLI); | |||||
$this->VerifyThemeId(); | |||||
$this->VerifyLanguageId(); | |||||
} | |||||
/** | |||||
* Returns site domain field. When none of site domains are found false is returned. | |||||
* | |||||
* @param string $field Field name. | |||||
* @param boolean $formatted Value should be formatted. | |||||
* @param string $format Format to be used. | |||||
* | |||||
* @return mixed | |||||
* @todo Move into separate module. | |||||
*/ | |||||
public function siteDomainField($field, $formatted = false, $format = null) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
return false; | |||||
} | |||||
return parent::siteDomainField($field, $formatted, $format); | |||||
} | |||||
/** | |||||
* Returns current session id (SID) | |||||
* | |||||
* @return integer | |||||
*/ | |||||
public function GetSID() | |||||
{ | |||||
// Used mostly for creating temp tables, that needs to be different in different process runs. | |||||
if ( !$this->isWebRequest() ) { | |||||
return 'cli' . getmypid(); | |||||
} | |||||
return parent::GetSID(); | |||||
} | |||||
/** | |||||
* Destroys current session | |||||
* | |||||
* @return void | |||||
* @see UserHelper::logoutUser() | |||||
*/ | |||||
public function DestroySession() | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
parent::DestroySession(); | |||||
} | |||||
/** | |||||
* Returns variable passed to the script as GET/POST/COOKIE | |||||
* | |||||
* @param string $name Name of variable to retrieve. | |||||
* @param mixed $default Default value returned in case if variable not present. | |||||
* | |||||
* @return mixed | |||||
*/ | |||||
public function GetVar($name, $default = false) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
return $this->_queryStorage->Get($name, $default); | |||||
} | |||||
return parent::GetVar($name, $default); | |||||
} | |||||
/** | |||||
* Removes forceful escaping done to the variable upon Front-End submission. | |||||
* | |||||
* @param string|array $value Value. | |||||
* | |||||
* @return string|array | |||||
* @see kHttpQuery::StripSlashes | |||||
* @todo Temporary method for marking problematic places to take care of, when forceful escaping will be removed. | |||||
*/ | |||||
public function unescapeRequestVariable($value) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
return parent::unescapeRequestVariable($value); | |||||
} | |||||
/** | |||||
* Returns variable passed to the script as $type | |||||
* | |||||
* @param string $name Name of variable to retrieve. | |||||
* @param string $type Get/Post/Cookie. | |||||
* @param mixed $default Default value returned in case if variable not present. | |||||
* | |||||
* @return mixed | |||||
*/ | |||||
public function GetVarDirect($name, $type, $default = false) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
return parent::GetVarDirect($name, $type, $default); | |||||
} | |||||
/** | |||||
* Returns ALL variables passed to the script as GET/POST/COOKIE | |||||
* | |||||
* @return array | |||||
* | |||||
* @deprecated | |||||
*/ | |||||
public function GetVars() | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
return parent::GetVars(); | |||||
} | |||||
/** | |||||
* Set the variable 'as it was passed to the script through GET/POST/COOKIE' | |||||
* | |||||
* This could be useful to set the variable when you know that | |||||
* other objects would relay on variable passed from GET/POST/COOKIE | |||||
* or you could use SetVar() / GetVar() pairs to pass the values between different objects.<br> | |||||
* | |||||
* @param string $var Variable name to set. | |||||
* @param mixed $val Variable value. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function SetVar($var, $val) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
$this->_queryStorage->Set($var, $val); | |||||
return; | |||||
} | |||||
parent::SetVar($var, $val); | |||||
} | |||||
/** | |||||
* Deletes kHTTPQuery variable | |||||
* | |||||
* @param string $var Variable name. | |||||
* | |||||
* @return void | |||||
* @todo Think about method name. | |||||
*/ | |||||
public function DeleteVar($var) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
$this->_queryStorage->Remove($var); | |||||
return; | |||||
} | |||||
parent::DeleteVar($var); | |||||
} | |||||
/** | |||||
* Deletes Session variable | |||||
* | |||||
* @param string $var Variable name. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function RemoveVar($var) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
$this->_sessionStorage->Remove($var); | |||||
return; | |||||
} | |||||
parent::RemoveVar($var); | |||||
} | |||||
/** | |||||
* Removes variable from persistent session | |||||
* | |||||
* @param string $var Variable name. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function RemovePersistentVar($var) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
parent::RemovePersistentVar($var); | |||||
} | |||||
/** | |||||
* Restores Session variable to it's db version | |||||
* | |||||
* @param string $var Variable name. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function RestoreVar($var) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
parent::RestoreVar($var); | |||||
} | |||||
/** | |||||
* Returns session variable value | |||||
* | |||||
* Return value of $var variable stored in Session. An optional default value could be passed as second parameter. | |||||
* | |||||
* @param string $var Variable name. | |||||
* @param mixed $default Default value to return if no $var variable found in session. | |||||
* | |||||
* @return mixed | |||||
* @see Session::RecallVar() | |||||
*/ | |||||
public function RecallVar($var, $default = false) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
return $this->_sessionStorage->Get($var, $default); | |||||
} | |||||
return parent::RecallVar($var, $default); | |||||
} | |||||
/** | |||||
* Returns variable value from persistent session | |||||
* | |||||
* @param string $var Variable name. | |||||
* @param mixed $default Default value to return if no $var variable found in persistent session. | |||||
* | |||||
* @return mixed | |||||
* @see Session::RecallPersistentVar() | |||||
*/ | |||||
public function RecallPersistentVar($var, $default = false) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
return parent::RecallPersistentVar($var, $default); | |||||
} | |||||
/** | |||||
* Stores variable $val in session under name $var | |||||
* Use this method to store variable in session. Later this variable could be recalled. | |||||
* | |||||
* @param string $var Variable name. | |||||
* @param mixed $val Variable value. | |||||
* @param boolean $optional Is value optional. | |||||
* | |||||
* @return void | |||||
* @see kApplication::RecallVar() | |||||
*/ | |||||
public function StoreVar($var, $val, $optional = false) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
$this->_sessionStorage->Set($var, $val); | |||||
return; | |||||
} | |||||
parent::StoreVar($var, $val, $optional); | |||||
} | |||||
/** | |||||
* Stores variable to persistent session | |||||
* | |||||
* @param string $var Variable name. | |||||
* @param mixed $val Variable value. | |||||
* @param boolean $optional Is value optional. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function StorePersistentVar($var, $val, $optional = false) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
parent::StorePersistentVar($var, $val, $optional); | |||||
} | |||||
/** | |||||
* Stores default value for session variable | |||||
* | |||||
* @param string $var Variable name. | |||||
* @param string $val Variable value. | |||||
* @param boolean $optional Is value optional. | |||||
* | |||||
* @return void | |||||
* @see Session::RecallVar() | |||||
* @see Session::StoreVar() | |||||
*/ | |||||
public function StoreVarDefault($var, $val, $optional = false) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
parent::StoreVarDefault($var, $val, $optional); | |||||
} | |||||
/** | |||||
* Links HTTP Query variable with session variable | |||||
* | |||||
* If variable $var is passed in HTTP Query it is stored in session for later use. If it's not passed it's | |||||
* recalled from session. This method could be used for making sure that GetVar will return query or session | |||||
* value for given variable, when query variable should overwrite session (and be stored there for later use).<br> | |||||
* This could be used for passing item's ID into popup with multiple tab - | |||||
* in popup script you just need to call LinkVar('id', 'current_id') before first use of GetVar('id'). | |||||
* After that you can be sure that GetVar('id') will return passed id or id passed earlier and stored in session | |||||
* | |||||
* @param string $var HTTP Query (GPC) variable name. | |||||
* @param mixed $ses_var Session variable name. | |||||
* @param mixed $default Default variable value. | |||||
* @param boolean $optional Is value optional. | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function LinkVar($var, $ses_var = null, $default = '', $optional = false) | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
parent::LinkVar($var, $ses_var, $default, $optional); | |||||
} | |||||
/** | |||||
* Returns variable from HTTP Query, or from session if not passed in HTTP Query | |||||
* The same as LinkVar, but also returns the variable value taken from HTTP Query if passed, or | |||||
* from session if not passed. Returns the default value if variable does not exist in session | |||||
* and was not passed in HTTP Query | |||||
* | |||||
* @param string $var HTTP Query (GPC) variable name. | |||||
* @param mixed $ses_var Session variable name. | |||||
* @param mixed $default Default variable value. | |||||
* | |||||
* @return mixed | |||||
* @see LinkVar | |||||
*/ | |||||
public function GetLinkedVar($var, $ses_var = null, $default = '') | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
return parent::GetLinkedVar($var, $ses_var, $default); | |||||
} | |||||
/** | |||||
* Checks if user is logged in, and creates | |||||
* user object if so. User object can be recalled | |||||
* later using "u.current" prefix_special. Also you may | |||||
* get user id by getting "u.current_id" variable. | |||||
* | |||||
* @return void | |||||
*/ | |||||
protected function ValidateLogin() | |||||
{ | |||||
if ( $this->isWebRequest() ) { | |||||
parent::ValidateLogin(); | |||||
} | |||||
// Do nothing for CLI requests. | |||||
} | |||||
/** | |||||
* Loads current user persistent session data | |||||
* | |||||
* @return void | |||||
*/ | |||||
public function LoadPersistentVars() | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); | |||||
} | |||||
parent::LoadPersistentVars(); | |||||
} | |||||
/** | |||||
* Allows to check if user in this session is logged in or not | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public function LoggedIn() | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
return false; | |||||
} | |||||
return parent::LoggedIn(); | |||||
} | |||||
/** | |||||
* Determines if access permissions should not be checked. | |||||
* | |||||
* @param integer|null $user_id User ID. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public function permissionCheckingDisabled($user_id = null) | |||||
{ | |||||
// Any user in CLI mode is allowed to do anything. | |||||
if ( !$this->isWebRequest() ) { | |||||
return true; | |||||
} | |||||
return parent::permissionCheckingDisabled($user_id); | |||||
} | |||||
/** | |||||
* Returns Window ID of passed prefix main prefix (in edit mode) | |||||
* | |||||
* @param string $prefix Prefix. | |||||
* | |||||
* @return integer | |||||
*/ | |||||
public function GetTopmostWid($prefix) | |||||
{ | |||||
// No editing popups in CLI. | |||||
if ( !$this->isWebRequest() ) { | |||||
return ''; | |||||
} | |||||
return parent::GetTopmostWid($prefix); | |||||
} | |||||
/** | |||||
* Checks, that given prefix is in temp mode | |||||
* | |||||
* @param string $prefix Prefix. | |||||
* @param string $special Special. | |||||
* | |||||
* @return boolean | |||||
*/ | |||||
public function IsTempMode($prefix, $special = '') | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
return false; | |||||
} | |||||
return parent::IsTempMode($prefix, $special); | |||||
} | |||||
/** | |||||
* Returns the client IP address. | |||||
* | |||||
* @return string The client IP address | |||||
*/ | |||||
public function getClientIp() | |||||
{ | |||||
if ( !$this->isWebRequest() ) { | |||||
return ''; | |||||
} | |||||
return parent::getClientIp(); | |||||
} | |||||
} |