Index: core/install/cache/class_structure.php =================================================================== --- core/install/cache/class_structure.php +++ core/install/cache/class_structure.php @@ -36,6 +36,7 @@ 'ConfigurationItem' => '/core/units/configuration/configuration.php', 'ConfigurationTagProcessor' => '/core/units/configuration/configuration_tag_processor.php', 'ConfigurationValidator' => '/core/units/configuration/configuration_validator.php', + 'ConsoleApplication' => '/core/kernel/console_application.php', 'ContentEventHandler' => '/core/units/content/content_eh.php', 'ContentTagProcessor' => '/core/units/content/content_tp.php', 'CoreUpgrades' => '/core/install/upgrades.php', @@ -548,6 +549,13 @@ 0 => 'kValidator', ), ), + 'ConsoleApplication' => array( + 'type' => 1, + 'modifiers' => 0, + 'extends' => array( + 0 => 'kApplication', + ), + ), 'ContentEventHandler' => array( 'type' => 1, 'modifiers' => 0, Index: core/kernel/Console/ConsoleApplication.php =================================================================== --- core/kernel/Console/ConsoleApplication.php +++ core/kernel/Console/ConsoleApplication.php @@ -52,6 +52,8 @@ 'In-Portal CLI', $this->Application->ModuleInfo['Core']['Version'] . ' (PHP v' . phpversion() . ')' ); + + $this->setAutoExit(false); } /** Index: core/kernel/application.php =================================================================== --- core/kernel/application.php +++ core/kernel/application.php @@ -216,6 +216,15 @@ public $siteDomain = null; /** + * Web Request, that is being processed currently. + * + * Will be null, when no request is being processed (e.g. application is used from CLI). + * + * @var \Symfony\Component\HttpFoundation\Request + */ + public $request; + + /** * Prevent kApplication class to be created directly, only via Instance method * * @access private @@ -256,7 +265,14 @@ static $instance = false; if ( !$instance ) { - $class = defined('APPLICATION_CLASS') ? APPLICATION_CLASS : 'kApplication'; + $class = APPLICATION_CLASS; + + // Replacement application class won't work for CLI. + if ( PHP_SAPI === 'cli' ) { + kUtil::includeOnce(__DIR__ . '/console_application.php'); + $class = 'ConsoleApplication'; + } + $instance = new $class(); } @@ -279,6 +295,11 @@ return false; } + // FIXME: Assign Symfony Request object provided from outside. + if ( PHP_SAPI !== 'cli' ) { + $this->request = true; + } + if ( preg_match('/utf-8/i', CHARSET) ) { setlocale(LC_ALL, 'en_US.UTF-8'); mb_internal_encoding('UTF-8'); @@ -323,7 +344,46 @@ define('MOD_REWRITE', $this->ConfigValue('UseModRewrite') && !$this->isAdmin ? 1 : 0); - // start processing request + $this->initBeforeOnAfterConfigRead(); + + $site_timezone = $this->ConfigValue('Config_Site_Time'); + + if ( $site_timezone ) { + date_default_timezone_set($site_timezone); + } + + // Must be called before AfterConfigRead, because current user should be available there. + $this->ValidateLogin(); + + $this->UnitConfigReader->AfterConfigRead(); + + if ( defined('DEBUG_MODE') && $this->isDebugMode() ) { + $this->Debugger->appendTimestamp('Processed AfterConfigRead'); + } + + $this->initAfterOnAfterConfigRead(); + + if ( defined('DEBUG_MODE') && $this->isDebugMode() ) { + $this->Debugger->profileFinish('kernel4_startup'); + } + + $this->InitDone = true; + + if ( PHP_SAPI !== 'cli' && !$this->isAdmin ) { + $this->HandleEvent(new kEvent('adm:OnStartup')); + } + + return true; + } + + /** + * Handles initialization part before "OnAfterConfigRead" events are called. + * + * @return void + */ + protected function initBeforeOnAfterConfigRead() + { + // Start processing request. $this->HttpQuery = $this->recallObject('kHTTPQuery'); $this->HttpQuery->process(); @@ -345,24 +405,18 @@ $this->cacheManager->LoadApplicationCache(); - $site_timezone = $this->ConfigValue('Config_Site_Time'); - - if ( $site_timezone ) { - date_default_timezone_set($site_timezone); - } - if ( defined('DEBUG_MODE') && $this->isDebugMode() ) { $this->Debugger->appendTimestamp('Loaded cache and phrases'); } + } - $this->ValidateLogin(); // must be called before AfterConfigRead, because current user should be available there - - $this->UnitConfigReader->AfterConfigRead(); - - if ( defined('DEBUG_MODE') && $this->isDebugMode() ) { - $this->Debugger->appendTimestamp('Processed AfterConfigRead'); - } - + /** + * Handles initialization part after "OnAfterConfigRead" events are called. + * + * @return void + */ + protected function initAfterOnAfterConfigRead() + { if ( $this->GetVar('m_cat_id') === false ) { $this->SetVar('m_cat_id', 0); } @@ -376,18 +430,16 @@ if ( $visit_id !== false ) { $this->SetVar('visits_id', $visit_id); } + } - if ( defined('DEBUG_MODE') && $this->isDebugMode() ) { - $this->Debugger->profileFinish('kernel4_startup'); - } - - $this->InitDone = true; - - if ( PHP_SAPI !== 'cli' && !$this->isAdmin ) { - $this->HandleEvent(new kEvent('adm:OnStartup')); - } - - return true; + /** + * Detects if this is a web request. + * + * @return boolean + */ + public function isWebRequest() + { + return isset($this->request); } /** @@ -2472,7 +2524,7 @@ array_unshift($prefixes, $current_prefix); } - if ( $real_top ) { + if ( $real_top || !$this->isWebRequest() ) { return $current_prefix; } @@ -2553,6 +2605,22 @@ } /** + * Determines if access permissions should not be checked. + * + * @param integer|null $user_id User ID. + * + * @return boolean + */ + public function permissionCheckingDisabled($user_id = null) + { + if ( !isset($user_id) ) { + $user_id = $this->RecallVar('user_id'); + } + + return $user_id == USER_ROOT; + } + + /** * Check current user permissions based on it's group permissions in specified category * * @param string $name permission name Index: core/kernel/console_application.php =================================================================== --- /dev/null +++ core/kernel/console_application.php @@ -0,0 +1,596 @@ +_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); + } + + /** + * Actually runs the parser against current template and stores parsing result + * + * This method gets 't' variable passed to the script, loads the template given in 't' variable and + * parses it. The result is store in {@link $this->HTML} property. + * + * @return void + */ + public function Run() + { + if ( !$this->isWebRequest() ) { + $this->_exitCode = $this->getConsoleApplication()->run(); + + return; + } + + parent::Run(); + } + + /** + * Send the parser results to browser + * + * Actually send everything stored in {@link $this->HTML}, to the browser by echoing it. + * + * @return void + */ + public function Done() + { + if ( !$this->isWebRequest() ) { + if ( defined('DBG_CAPTURE_STATISTICS') && DBG_CAPTURE_STATISTICS && !$this->isAdmin ) { + $this->_storeStatistics(); + } + + exit($this->_exitCode); + } + + parent::Done(); + } + + /** + * 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 + * @throws LogicException When used during CLI request. + * @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 + * @throws LogicException When used during CLI request. + * @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 + * @throws LogicException When used during CLI request. + */ + 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 + * @throws LogicException When used during CLI request. + * + * @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.
+ * + * @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 + * @throws LogicException When used during CLI request. + */ + 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 + * @throws LogicException When used during CLI request. + */ + 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 + * @throws LogicException When used during CLI request. + * @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 + * @throws LogicException When used during CLI request. + */ + 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 + * @throws LogicException When used during CLI request. + * @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).
+ * 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 + * @throws LogicException When used during CLI request. + */ + 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 + * @throws LogicException When used during CLI request. + * @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 + * @throws LogicException When used during CLI request. + */ + 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(); + } + +} Index: core/kernel/db/cat_event_handler.php =================================================================== --- core/kernel/db/cat_event_handler.php +++ core/kernel/db/cat_event_handler.php @@ -1875,7 +1875,13 @@ // get PerPage (forced -> session -> config -> 10) $object->SetPerPage($this->getPerPage($event)); - // main lists on Front-End have special get parameter for page + if ( !$this->Application->isWebRequest() ) { + $object->SetPage(1); + + return; + } + + // Main lists on Front-End have special get parameter for page. $page = $object->isMainList() ? $this->Application->GetVar('page') : false; if ( !$page ) { @@ -2761,8 +2767,13 @@ } } else { - $sorting_settings = $this->getListSetting($event, 'Sortings'); - $sort_by = trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ','); + if ( $this->Application->isWebRequest() ) { + $sorting_settings = $this->getListSetting($event, 'Sortings'); + $sort_by = trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ','); + } + else { + $sort_by = ''; + } if ( !$sort_by ) { $event->setEventParam('sort_by', 'Relevance,desc|' . $default_sorting); @@ -2870,14 +2881,16 @@ $config->addGrids($grid_data, $process_grid . 'ShowAll'); } - // add options for CategoryId field (quick way to select item's primary category) - $category_helper = $this->Application->recallObject('CategoryHelper'); - /* @var $category_helper CategoryHelper */ - - $virtual_fields = $config->getVirtualFields(); - $virtual_fields['CategoryId']['default'] = (int)$this->Application->GetVar('m_cat_id'); - $virtual_fields['CategoryId']['options'] = $category_helper->getStructureTreeAsOptions(); - $config->setVirtualFields($virtual_fields); + if ( $this->Application->isWebRequest() ) { + // Add options for CategoryId field (quick way to select item's primary category). + $category_helper = $this->Application->recallObject('CategoryHelper'); + /* @var $category_helper CategoryHelper */ + + $virtual_fields = $config->getVirtualFields(); + $virtual_fields['CategoryId']['default'] = (int)$this->Application->GetVar('m_cat_id'); + $virtual_fields['CategoryId']['options'] = $category_helper->getStructureTreeAsOptions(); + $config->setVirtualFields($virtual_fields); + } } /** Index: core/kernel/db/db_event_handler.php =================================================================== --- core/kernel/db/db_event_handler.php +++ core/kernel/db/db_event_handler.php @@ -236,6 +236,12 @@ return $main_object->GetDBField($event->getUnitConfig()->getIDField()); } + if ( !$this->Application->isWebRequest() ) { + throw new LogicException( + 'The "' . $event->getPrefixSpecial() . '" object can\'t be auto-loaded from CLI request.' + ); + } + // 1. get id from post (used in admin) $ret = $this->Application->GetVar($event->getPrefixSpecial(true) . '_id'); if ( ($ret !== false) && ($ret != '') ) { @@ -290,6 +296,10 @@ */ protected function StoreSelectedIDs(kEvent $event, $direct_ids = NULL) { + if ( !$this->Application->isWebRequest() ) { + throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); + } + $wid = $this->Application->GetTopmostWid($event->Prefix); $session_name = rtrim($event->getPrefixSpecial() . '_selected_ids_' . $wid, '_'); @@ -362,6 +372,10 @@ */ protected function getSelectedIDs(kEvent $event, $from_session = false) { + if ( !$this->Application->isWebRequest() ) { + throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); + } + if ( $from_session ) { $wid = $this->Application->GetTopmostWid($event->Prefix); $var_name = rtrim($event->getPrefixSpecial() . '_selected_ids_' . $wid, '_'); @@ -420,6 +434,10 @@ */ protected function clearSelectedIDs(kEvent $event) { + if ( !$this->Application->isWebRequest() ) { + throw new LogicException('Web Request related method "' . __METHOD__ . '" was called from CLI mode.'); + } + $prefix_special = $event->getPrefixSpecial(); $ids = implode(',', $this->getSelectedIDs($event, true)); @@ -450,10 +468,13 @@ if ( $event->getEventParam('form_name') !== false ) { $form_name = $event->getEventParam('form_name'); } - else { - $request_forms = $this->Application->GetVar('forms', Array ()); + elseif ( $this->Application->isWebRequest() ) { + $request_forms = $this->Application->GetVar('forms', array()); $form_name = (string)getArrayValue($request_forms, $object->getPrefixSpecial()); } + else { + $form_name = null; + } $object->Configure($event->getEventParam('populate_ml_fields') || $event->getUnitConfig()->getPopulateMlFields(), $form_name); $this->PrepareObject($object, $event); @@ -473,8 +494,11 @@ $this->Application->setEvent($event->getPrefixSpecial(), ''); - $save_event = $this->UseTempTables($event) && $this->Application->GetTopmostPrefix($event->Prefix) == $event->Prefix ? 'OnSave' : 'OnUpdate'; - $this->Application->SetVar($event->getPrefixSpecial() . '_SaveEvent', $save_event); + if ( $this->Application->isWebRequest() ) { + $is_topmost_prefix = $this->Application->GetTopmostPrefix($event->Prefix) == $event->Prefix; + $save_event = $this->UseTempTables($event) && $is_topmost_prefix ? 'OnSave' : 'OnUpdate'; + $this->Application->SetVar($event->getPrefixSpecial() . '_SaveEvent', $save_event); + } } /** @@ -558,19 +582,19 @@ $event->setEventParam('top_prefix', $this->Application->GetTopmostPrefix($event->Prefix, true)); $status_checked = false; - if ( $user_id == USER_ROOT || $this->CheckPermission($event) ) { - // don't autoload item, when user doesn't have view permission + if ( $this->Application->permissionCheckingDisabled($user_id) || $this->CheckPermission($event) ) { + // Don't autoload item, when user doesn't have view permission. $this->LoadItem($event); $status_checked = true; $editing_mode = defined('EDITING_MODE') ? EDITING_MODE : false; $id_from_request = $event->getEventParam(kEvent::FLAG_ID_FROM_REQUEST); - if ( $user_id != USER_ROOT + if ( !$this->Application->permissionCheckingDisabled($user_id) && !$this->Application->isAdmin && !($editing_mode || ($id_from_request ? $this->checkItemStatus($event) : true)) ) { - // non-root user AND on front-end AND (not editing mode || incorrect status) + // Permissions are being checked AND on Front-End AND (not editing mode || incorrect status). $perm_status = false; } } @@ -655,7 +679,10 @@ $object->setParentEvent($parent_event); } - $object->BuildTables($event->Prefix, $this->getSelectedIDs($event)); + $object->BuildTables( + $event->Prefix, + $this->Application->isWebRequest() ? $this->getSelectedIDs($event) : array() + ); } /** @@ -743,8 +770,11 @@ $object->linkToParent($this->getMainSpecial($event)); } - $this->AddFilters($event); - $this->SetCustomQuery($event); // new!, use this for dynamic queries based on specials for ex. + if ( $this->Application->isWebRequest() ) { + $this->AddFilters($event); + } + + $this->SetCustomQuery($event); // New!, use this for dynamic queries based on specials for ex. $this->SetPagination($event); $this->SetSorting($event); @@ -895,7 +925,13 @@ // get PerPage (forced -> session -> config -> 10) $object->SetPerPage($this->getPerPage($event)); - // main lists on Front-End have special get parameter for page + if ( !$this->Application->isWebRequest() ) { + $object->SetPage(1); + + return; + } + + // Main lists on Front-End have special get parameter for page. $page = $object->isMainList() ? $this->Application->GetVar('page') : false; if ( !$page ) { @@ -972,33 +1008,35 @@ return $per_page; } - if ( !$per_page && $object->isMainList() ) { - // main lists on Front-End have special get parameter for per-page - $per_page = $this->Application->GetVar('per_page'); - } + if ( $this->Application->isWebRequest() ) { + if ( !$per_page && $object->isMainList() ) { + // Main lists on Front-End have special get parameter for per-page. + $per_page = $this->Application->GetVar('per_page'); + } - if ( !$per_page ) { - // per-page is given in "env" variable for given prefix - $per_page = $this->Application->GetVar($event->getPrefixSpecial() . '_PerPage'); - } + if ( !$per_page ) { + // Per-page is given in "env" variable for given prefix. + $per_page = $this->Application->GetVar($event->getPrefixSpecial() . '_PerPage'); + } - if ( !$per_page && $event->Special ) { - // when not part of env, then variables like "prefix.special_PerPage" are - // replaced (by PHP) with "prefix_special_PerPage", so check for that too - $per_page = $this->Application->GetVar($event->getPrefixSpecial(true) . '_PerPage'); - } + if ( !$per_page && $event->Special ) { + // When not part of env, then variables like "prefix.special_PerPage" are + // replaced (by PHP) with "prefix_special_PerPage", so check for that too. + $per_page = $this->Application->GetVar($event->getPrefixSpecial(true) . '_PerPage'); + } - if ( !$object->isMainList() ) { - // per-page given in env and not in main list - $view_name = $this->Application->RecallVar($event->getPrefixSpecial() . '_current_view'); + if ( !$object->isMainList() ) { + // Per-page given in env and not in main list. + $view_name = $this->Application->RecallVar($event->getPrefixSpecial() . '_current_view'); - if ( $per_page ) { - // per-page found in request -> store in session and persistent session - $this->setListSetting($event, 'PerPage', $per_page); - } - else { - // per-page not found in request -> get from pesistent session (or session) - $per_page = $this->getListSetting($event, 'PerPage'); + if ( $per_page ) { + // Per-page found in request -> store in session and persistent session. + $this->setListSetting($event, 'PerPage', $per_page); + } + else { + // Per-page not found in request -> get from pesistent session (or session). + $per_page = $this->getListSetting($event, 'PerPage'); + } } } @@ -1035,41 +1073,44 @@ $object = $event->getObject(); /* @var $object kDBList */ - if ( $object->isMainList() ) { - $sort_by = $this->Application->GetVar('sort_by'); - $cur_sort1 = $cur_sort1_dir = $cur_sort2 = $cur_sort2_dir = false; + $cur_sort1 = $cur_sort1_dir = $cur_sort2 = $cur_sort2_dir = false; - if ( $sort_by ) { - $sortings = explode('|', $sort_by); - list ($cur_sort1, $cur_sort1_dir) = explode(',', $sortings[0]); + if ( $this->Application->isWebRequest() ) { + if ( $object->isMainList() ) { + $sort_by = $this->Application->GetVar('sort_by'); + + if ( $sort_by ) { + $sortings = explode('|', $sort_by); + list ($cur_sort1, $cur_sort1_dir) = explode(',', $sortings[0]); - if ( isset($sortings[1]) ) { - list ($cur_sort2, $cur_sort2_dir) = explode(',', $sortings[1]); + if ( isset($sortings[1]) ) { + list ($cur_sort2, $cur_sort2_dir) = explode(',', $sortings[1]); + } } } - } - else { - $sorting_settings = $this->getListSetting($event, 'Sortings'); + else { + $sorting_settings = $this->getListSetting($event, 'Sortings'); - $cur_sort1 = getArrayValue($sorting_settings, 'Sort1'); - $cur_sort1_dir = getArrayValue($sorting_settings, 'Sort1_Dir'); - $cur_sort2 = getArrayValue($sorting_settings, 'Sort2'); - $cur_sort2_dir = getArrayValue($sorting_settings, 'Sort2_Dir'); - } + $cur_sort1 = getArrayValue($sorting_settings, 'Sort1'); + $cur_sort1_dir = getArrayValue($sorting_settings, 'Sort1_Dir'); + $cur_sort2 = getArrayValue($sorting_settings, 'Sort2'); + $cur_sort2_dir = getArrayValue($sorting_settings, 'Sort2_Dir'); + } - $tag_sort_by = $event->getEventParam('sort_by'); + $tag_sort_by = $event->getEventParam('sort_by'); - if ( $tag_sort_by ) { - if ( $tag_sort_by == 'random' ) { - $object->AddOrderField('RAND()', ''); - } - else { - // multiple sortings could be specified at once - $tag_sort_by = explode('|', $tag_sort_by); + if ( $tag_sort_by ) { + if ( $tag_sort_by == 'random' ) { + $object->AddOrderField('RAND()', ''); + } + else { + // Multiple sortings could be specified at once. + $tag_sort_by = explode('|', $tag_sort_by); - foreach ($tag_sort_by as $sorting_element) { - list ($by, $dir) = explode(',', $sorting_element); - $object->AddOrderField($by, $dir); + foreach ( $tag_sort_by as $sorting_element ) { + list ($by, $dir) = explode(',', $sorting_element); + $object->AddOrderField($by, $dir); + } } } } @@ -1900,6 +1941,11 @@ */ public function SaveLoggedChanges($changes_var_name, $save = true) { + // Nothing needs to be saved > exit immediately. + if ( !$save ) { + return; + } + // 1. get changes, that were made $changes = $this->Application->RecallVar($changes_var_name); $changes = $changes ? unserialize($changes) : Array (); Index: core/kernel/db/db_tag_processor.php =================================================================== --- core/kernel/db/db_tag_processor.php +++ core/kernel/db/db_tag_processor.php @@ -484,8 +484,12 @@ $block_empty_cell_params['name'] = $this->SelectParam($params, 'empty_cell_render_as,block_empty_cell,empty_cell_block'); $i = 0; + $is_web_request = $this->Application->isWebRequest(); + + if ( $is_web_request ) { + $backup_id = $this->Application->GetVar($this->Prefix . '_id'); + } - $backup_id = $this->Application->GetVar($this->Prefix . '_id'); $displayed = Array (); $column_number = 1; @@ -493,9 +497,13 @@ $limit = isset($params['limit']) ? $params['limit'] : false; - while (!$list->EOL() && (!$limit || $i<$limit)) { - $this->Application->SetVar($this->getPrefixSpecial() . '_id', $list->GetDBField($id_field)); // for edit/delete links using GET - $this->Application->SetVar($this->Prefix . '_id', $list->GetDBField($id_field)); + while ( !$list->EOL() && (!$limit || $i < $limit) ) { + if ( $is_web_request ) { + // For edit/delete links using GET. + $this->Application->SetVar($this->getPrefixSpecial() . '_id', $list->GetDBField($id_field)); + $this->Application->SetVar($this->Prefix . '_id', $list->GetDBField($id_field)); + } + $block_params['is_last'] = ($i == $list->GetSelectedCount() - 1); $block_params['last_row'] = ($i + (($i + 1) % $columns) >= $list->GetSelectedCount() - 1); $block_params['not_last'] = !$block_params['is_last']; // for front-end @@ -573,19 +581,22 @@ $i++; } - $cur_displayed = $this->Application->GetVar($this->Prefix . '_displayed_ids'); - if ( !$cur_displayed ) { - $cur_displayed = Array (); - } - else { - $cur_displayed = explode(',', $cur_displayed); - } + if ( $is_web_request ) { + $cur_displayed = $this->Application->GetVar($this->Prefix . '_displayed_ids'); + + if ( !$cur_displayed ) { + $cur_displayed = array(); + } + else { + $cur_displayed = explode(',', $cur_displayed); + } - $displayed = array_unique(array_merge($displayed, $cur_displayed)); - $this->Application->SetVar($this->Prefix . '_displayed_ids', implode(',', $displayed)); + $displayed = array_unique(array_merge($displayed, $cur_displayed)); + $this->Application->SetVar($this->Prefix . '_displayed_ids', implode(',', $displayed)); - $this->Application->SetVar($this->Prefix . '_id', $backup_id); - $this->Application->SetVar($this->getPrefixSpecial() . '_id', ''); + $this->Application->SetVar($this->Prefix . '_id', $backup_id); + $this->Application->SetVar($this->getPrefixSpecial() . '_id', ''); + } if ( isset($params['more_link_render_as']) ) { $block_params = $params; Index: core/kernel/db/dbitem.php =================================================================== --- core/kernel/db/dbitem.php +++ core/kernel/db/dbitem.php @@ -570,6 +570,10 @@ */ public function getPendingActions($id = null) { + if ( !$this->Application->isWebRequest() ) { + return array(); + } + if ( !isset($id) ) { $id = $this->GetID(); } @@ -603,6 +607,10 @@ */ public function setPendingActions($new_pending_actions = null, $id = null) { + if ( !$this->Application->isWebRequest() ) { + return; + } + if ( !isset($new_pending_actions) ) { $new_pending_actions = Array (); } @@ -1190,6 +1198,10 @@ */ public function setModifiedFlag($mode = null) { + if ( !$this->Application->isWebRequest() ) { + return; + } + $main_prefix = $this->Application->GetTopmostPrefix($this->Prefix); $this->Application->StoreVar($main_prefix . '_modified', '1', true); // true for optional @@ -1214,6 +1226,10 @@ */ public function ShouldLogChanges($log_changes = null) { + if ( !$this->Application->isWebRequest() ) { + return false; + } + $config = $this->getUnitConfig(); if ( !isset($log_changes) ) { Index: core/kernel/kbase.php =================================================================== --- core/kernel/kbase.php +++ core/kernel/kbase.php @@ -694,7 +694,7 @@ $allowed_modifiers[] = 'upload_dir'; } - if ( !isset($field_modifiers) ) { + if ( !isset($field_modifiers) && $this->Application->isWebRequest() ) { $field_modifiers = $this->Application->GetVar('field_modifiers'); if ( !$field_modifiers ) { Index: core/kernel/managers/cache_manager.php =================================================================== --- core/kernel/managers/cache_manager.php +++ core/kernel/managers/cache_manager.php @@ -341,7 +341,12 @@ $this->getToCache() ); - $cache_rebuild_by = SERVER_NAME . ' (' . $this->Application->getClientIp() . ') - ' . date('d/m/Y H:i:s'); + if ( $this->Application->isWebRequest() ) { + $cache_rebuild_by = SERVER_NAME . ' (' . $this->Application->getClientIp() . ') - ' . date('d/m/Y H:i:s'); + } + else { + $cache_rebuild_by = 'CLI - ' . date('d/m/Y H:i:s'); + } if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) { $this->Application->setCache('master:configs_parsed', serialize($cache)); Index: core/kernel/managers/hook_manager.php =================================================================== --- core/kernel/managers/hook_manager.php +++ core/kernel/managers/hook_manager.php @@ -139,6 +139,8 @@ return ; } + $is_web_request = $this->Application->isWebRequest(); + foreach ($hooks as $hook) { if ($hook['DoSpecial'] == '*') { // use same special as master event @@ -147,7 +149,7 @@ $prefix_special = rtrim($hook['DoPrefix'].'_'.$hook['DoSpecial'], '_'); - if ( $hook['Conditional'] && !$this->Application->GetVar($prefix_special) ) { + if ( $hook['Conditional'] && $is_web_request && !$this->Application->GetVar($prefix_special) ) { continue; } @@ -182,4 +184,4 @@ return $hooks[ strtolower($event_key.'.'.$event->Name) ]; } -} \ No newline at end of file +} Index: core/kernel/managers/request_manager.php =================================================================== --- core/kernel/managers/request_manager.php +++ core/kernel/managers/request_manager.php @@ -150,7 +150,7 @@ $event_handler = $this->Application->recallObject($event->Prefix . '_EventHandler'); /* @var $event_handler kEventHandler */ - if ( ($this->Application->RecallVar('user_id') == USER_ROOT) || $event_handler->CheckPermission($event) ) { + if ( $this->Application->permissionCheckingDisabled() || $event_handler->CheckPermission($event) ) { $this->Application->HandleEvent($event); $this->Application->notifyEventSubscribers($event); } @@ -475,4 +475,4 @@ $opener_stack->push($template, $params, $index_file); $opener_stack->save(); } -} \ No newline at end of file +} Index: core/kernel/managers/scheduled_task_manager.php =================================================================== --- core/kernel/managers/scheduled_task_manager.php +++ core/kernel/managers/scheduled_task_manager.php @@ -116,8 +116,12 @@ $events_source = $this->getAll(); - $user_id = $this->Application->RecallVar('user_id'); - $this->Application->StoreVar('user_id', USER_ROOT, true); // to prevent permission checking inside events, true for optional storage + if ( $this->Application->isWebRequest() ) { + $user_id = $this->Application->RecallVar('user_id'); + + // To prevent permission checking inside events, true for optional storage. + $this->Application->StoreVar('user_id', USER_ROOT, true); + } $site_helper = $this->Application->recallObject('SiteHelper'); /* @var $site_helper SiteHelper */ @@ -148,7 +152,9 @@ $this->run($event_data); } - $this->Application->StoreVar('user_id', $user_id, $user_id == USER_GUEST); + if ( $this->Application->isWebRequest() ) { + $this->Application->StoreVar('user_id', $user_id, $user_id == USER_GUEST); + } } /** Index: core/kernel/managers/url_manager.php =================================================================== --- core/kernel/managers/url_manager.php +++ core/kernel/managers/url_manager.php @@ -130,8 +130,9 @@ unset($params['_auto_prefix_']); // this is parser-related param, do not need to pass it here } - $ssl = isset($params['__SSL__']) ? $params['__SSL__'] : NULL; - if ( $ssl !== NULL ) { + $ssl = isset($params['__SSL__']) ? $params['__SSL__'] : null; + + if ( $ssl !== null && $this->Application->isWebRequest() ) { $session = $this->Application->recallObject('Session'); /* @var $session Session */ @@ -199,10 +200,12 @@ // append pass through variables to each link to be build $params = array_merge($this->getPassThroughVariables($params), $params); - $session = $this->Application->recallObject('Session'); + if ( $this->Application->isWebRequest() ) { + $session = $this->Application->recallObject('Session'); - if ( $session->NeedQueryString() && !$force_no_sid ) { - $params['sid'] = $this->Application->GetSID(); + if ( $session->NeedQueryString() && !$force_no_sid ) { + $params['sid'] = $this->Application->GetSID(); + } } if ( $force_rewrite || ($this->Application->RewriteURLs($ssl) && $rewrite) ) { Index: core/kernel/processors/main_processor.php =================================================================== --- core/kernel/processors/main_processor.php +++ core/kernel/processors/main_processor.php @@ -20,6 +20,10 @@ { parent::__construct(); + if ( !$this->Application->isWebRequest() ) { + return; + } + $actions = $this->Application->recallObject('kActions'); /* @var $actions Params */ @@ -1304,4 +1308,16 @@ return false; } + /** + * Determines if this is the Web Request. + * + * @param array $params Tag params. + * + * @return string + */ + protected function IsWebRequest(array $params) + { + return $this->Application->isWebRequest(); + } + } Index: core/kernel/session/session.php =================================================================== --- core/kernel/session/session.php +++ core/kernel/session/session.php @@ -248,10 +248,6 @@ { parent::Init($prefix, $special); - if ( php_sapi_name() == 'cli' ) { - $this->SetMode(self::smGET_ONLY); - } - $this->CheckIfCookiesAreOn(); $this->Checkers = Array(); Index: core/kernel/startup.php =================================================================== --- core/kernel/startup.php +++ core/kernel/startup.php @@ -203,3 +203,4 @@ // system users define('USER_ROOT', -1); define('USER_GUEST', -2); + define('USER_CLI', -3); Index: core/kernel/utility/email.php =================================================================== --- core/kernel/utility/email.php +++ core/kernel/utility/email.php @@ -790,9 +790,8 @@ $language_id = $restore ? $prev_language_id : $this->params['language_id']; $this->Application->SetVar('m_lang', $language_id); - $language = $this->Application->recallObject('lang.current'); - /* @var $language LanguagesItem */ - + /** @var LanguagesItem $language */ + $language = $this->Application->recallObject('lang.current', null, array('skip_autoload' => true)); $language->Load($language_id); $this->Application->Phrases->LanguageId = $language_id; Index: core/kernel/utility/formatters/multilang_formatter.php =================================================================== --- core/kernel/utility/formatters/multilang_formatter.php +++ core/kernel/utility/formatters/multilang_formatter.php @@ -251,7 +251,14 @@ $lang = $this->Application->GetVar('m_lang'); $def_lang = $this->Application->GetDefaultLanguageId(); - if ( !$this->Application->GetVar('allow_translation') && ($lang != $def_lang) && $object->isRequired($field) ) { + if ( $this->Application->isWebRequest() ) { + $allow_translation = $this->Application->GetVar('allow_translation'); + } + else { + $allow_translation = false; + } + + if ( !$allow_translation && ($lang != $def_lang) && $object->isRequired($field) ) { $def_lang_field = 'l' . $def_lang . '_' . $master_field; if ( !$object->ValidateRequired($def_lang_field, $options) ) { @@ -313,4 +320,4 @@ return $value; } -} \ No newline at end of file +} Index: core/kernel/utility/logger.php =================================================================== --- core/kernel/utility/logger.php +++ core/kernel/utility/logger.php @@ -120,14 +120,9 @@ const LI_ADMIN = 2; /** - * Log interface: Cron (Front) + * Log interface: CLI */ - const LI_CRON_FRONT = 3; - - /** - * Log interface: Cron (Admin) - */ - const LI_CRON_ADMIN = 4; + const LI_CLI = 3; /** * Log interface: API @@ -316,16 +311,20 @@ 'LogNotificationStatus' => self::LNS_DISABLED, ); - if ( $this->Application->isAdmin ) { - $this->_logRecord['LogInterface'] = defined('CRON') && CRON ? self::LI_CRON_ADMIN : self::LI_ADMIN; + if ( $this->Application->isWebRequest() ) { + $this->_logRecord['LogInterface'] = $this->Application->isAdmin ? self::LI_ADMIN : self::LI_FRONT; } else { - $this->_logRecord['LogInterface'] = defined('CRON') && CRON ? self::LI_CRON_FRONT : self::LI_FRONT; + $this->_logRecord['LogInterface'] = self::LI_CLI; } if ( $this->Application->InitDone ) { $this->_logRecord['LogUserId'] = $this->Application->RecallVar('user_id'); - $this->_logRecord['LogSessionKey'] = $this->Application->GetSID(); + + if ( $this->Application->isWebRequest() ) { + $this->_logRecord['LogSessionKey'] = $this->Application->GetSID(); + } + $this->_logRecord['IpAddress'] = $this->Application->getClientIp(); } Index: core/units/admin/admin_events_handler.php =================================================================== --- core/units/admin/admin_events_handler.php +++ core/units/admin/admin_events_handler.php @@ -196,7 +196,7 @@ $this->Application->DeleteUnitCache(); - if ( $this->Application->GetVar('validate_configs') ) { + if ( $this->Application->isWebRequest() && $this->Application->GetVar('validate_configs') ) { $event->SetRedirectParam('validate_configs', 1); } Index: core/units/categories/categories_event_handler.php =================================================================== --- core/units/categories/categories_event_handler.php +++ core/units/categories/categories_event_handler.php @@ -1555,7 +1555,7 @@ { parent::SetPagination($event); - if ( !$this->Application->isAdmin ) { + if ( !$this->Application->isAdmin && $this->Application->isWebRequest() ) { $page_var = $event->getEventParam('page_var'); if ( $page_var !== false ) { @@ -1681,8 +1681,13 @@ } } else { - $sorting_settings = $this->getListSetting($event, 'Sortings'); - $sort_by = trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ','); + if ( $this->Application->isWebRequest() ) { + $sorting_settings = $this->getListSetting($event, 'Sortings'); + $sort_by = trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ','); + } + else { + $sort_by = ''; + } if ( !$sort_by ) { $event->setEventParam('sort_by', 'Relevance,desc|' . $default_sorting); @@ -2207,25 +2212,33 @@ $config->addSectionAdjustments($section_adjustments); - // prepare structure dropdown - $category_helper = $this->Application->recallObject('CategoryHelper'); - /* @var $category_helper CategoryHelper */ + if ( $this->Application->isWebRequest() ) { + $category_helper = $this->Application->recallObject('CategoryHelper'); + /* @var $category_helper CategoryHelper */ - $fields = $config->getFields(); + $fields = $config->getFields(); - $fields['ParentId']['default'] = (int)$this->Application->GetVar('m_cat_id'); - $fields['ParentId']['options'] = $category_helper->getStructureTreeAsOptions(); + // Prepare structure dropdown. + $fields['ParentId']['default'] = (int)$this->Application->GetVar('m_cat_id'); + $fields['ParentId']['options'] = $category_helper->getStructureTreeAsOptions(); - // limit design list by theme - $theme_id = $this->_getCurrentThemeId(); - $design_sql = $fields['Template']['options_sql']; - $design_sql = str_replace('(tf.FilePath = "/designs")', '(' . implode(' OR ', $this->getDesignFolders()) . ')' . ' AND (t.ThemeId = ' . $theme_id . ')', $design_sql); - $fields['Template']['options_sql'] = $design_sql; + // Limit design list by theme. + $theme_id = $this->_getCurrentThemeId(); + $design_sql = $fields['Template']['options_sql']; + $design_sql = str_replace( + '(tf.FilePath = "/designs")', + '(' . implode(' OR ', $this->getDesignFolders()) . ') AND (t.ThemeId = ' . $theme_id . ')', + $design_sql + ); + $fields['Template']['options_sql'] = $design_sql; - // adds "Inherit From Parent" option to "Template" field - $fields['Template']['options'] = Array (CATEGORY_TEMPLATE_INHERIT => $this->Application->Phrase('la_opt_InheritFromParent')); + // Adds "Inherit From Parent" option to "Template" field. + $fields['Template']['options'] = array( + CATEGORY_TEMPLATE_INHERIT => $this->Application->Phrase('la_opt_InheritFromParent'), + ); - $config->setFields($fields); + $config->setFields($fields); + } if ($this->Application->isAdmin) { // don't sort by Front-End sorting fields Index: core/units/email_templates/email_template_eh.php =================================================================== --- core/units/email_templates/email_template_eh.php +++ core/units/email_templates/email_template_eh.php @@ -243,7 +243,7 @@ $ml_helper->replaceMLCalculatedFields($event); - if ( $this->Application->GetVar('regional') ) { + if ( $this->Application->isWebRequest() && $this->Application->GetVar('regional') ) { $config->setPopulateMlFields(true); } } Index: core/units/forms/form_submissions/form_submissions_eh.php =================================================================== --- core/units/forms/form_submissions/form_submissions_eh.php +++ core/units/forms/form_submissions/form_submissions_eh.php @@ -83,6 +83,10 @@ protected function OnBuildFormFields(kEvent $event) { + if ( !$this->Application->isWebRequest() ) { + return; + } + $form_id = $this->Application->GetVar('form_id'); if ( !$form_id ) { Index: core/units/helpers/file_helper.php =================================================================== --- core/units/helpers/file_helper.php +++ core/units/helpers/file_helper.php @@ -139,8 +139,14 @@ */ public function createItemFiles($prefix, $is_image = false) { - $items_info = $this->Application->GetVar($prefix); - if ($items_info) { + if ( $this->Application->isWebRequest() ) { + $items_info = $this->Application->GetVar($prefix); + } + else { + $items_info = false; + } + + if ( $items_info ) { list (, $fields_values) = each($items_info); $this->createUploadFields($prefix, $fields_values, $is_image); } Index: core/units/helpers/permissions_helper.php =================================================================== --- core/units/helpers/permissions_helper.php +++ core/units/helpers/permissions_helper.php @@ -584,8 +584,7 @@ { $user_id = (int)$user_id; - if ( $user_id == USER_ROOT ) { - // "root" is allowed anywhere + if ( $this->Application->permissionCheckingDisabled($user_id) ) { return substr($name, -5) == '.deny' || $name == 'SYSTEM_ACCESS.READONLY' ? 0 : 1; } @@ -844,4 +843,4 @@ return 0; } - } \ No newline at end of file + } Index: core/units/helpers/upload_helper.php =================================================================== --- core/units/helpers/upload_helper.php +++ core/units/helpers/upload_helper.php @@ -140,7 +140,7 @@ $admin_session = $this->Application->recallObject('Session.admin'); /* @var $admin_session Session */ - if ( $admin_session->RecallVar('user_id') == USER_ROOT ) { + if ( $this->Application->permissionCheckingDisabled($admin_session->RecallVar('user_id')) ) { return true; } Index: core/units/logs/system_logs/system_logs_config.php =================================================================== --- core/units/logs/system_logs/system_logs_config.php +++ core/units/logs/system_logs/system_logs_config.php @@ -144,8 +144,7 @@ 'formatter' => 'kOptionsFormatter', 'options' => Array ( kLogger::LI_FRONT => 'Front', kLogger::LI_ADMIN => 'Admin', - kLogger::LI_CRON_FRONT => 'Cron (Front)', - kLogger::LI_CRON_ADMIN => 'Cron (Admin)', + kLogger::LI_CLI => 'CLI', kLogger::LI_API => 'API', ), 'not_null' => 1, 'default' => 0 @@ -202,4 +201,4 @@ ), ), ), -); \ No newline at end of file +); Index: core/units/phrases/phrases_event_handler.php =================================================================== --- core/units/phrases/phrases_event_handler.php +++ core/units/phrases/phrases_event_handler.php @@ -487,7 +487,7 @@ $ml_helper->replaceMLCalculatedFields($event); - if ( $this->Application->GetVar('regional') ) { + if ( $this->Application->isWebRequest() && $this->Application->GetVar('regional') ) { $event->getUnitConfig()->setPopulateMlFields(true); } } Index: core/units/scheduled_tasks/scheduled_task_eh.php =================================================================== --- core/units/scheduled_tasks/scheduled_task_eh.php +++ core/units/scheduled_tasks/scheduled_task_eh.php @@ -270,10 +270,11 @@ $config->setFields($fields); } - $cron_helper = $this->Application->recallObject('kCronHelper'); - /* @var $cron_helper kCronHelper */ - - $cron_helper->initUnit($event->Prefix, 'RunSchedule'); - + if ( $this->Application->isWebRequest() ) { + /** @var kCronHelper $cron_helper */ + $cron_helper = $this->Application->recallObject('kCronHelper'); + $cron_helper->initUnit($event->Prefix, 'RunSchedule'); + } } + } Index: in-portal =================================================================== --- in-portal +++ in-portal @@ -20,7 +20,7 @@ $start = microtime(true); -define('CRON', 1); +define('CRON', 1); // Means 'used in CLI' (name kept for BC reasons). define('INDEX_FILE', 'index.php'); define('DBG_SKIP_REPORTING', 1); $_SERVER['REQUEST_URI'] = 'CRON'; @@ -31,11 +31,7 @@ $application =& kApplication::Instance(); $application->Init(); - -// Assume user with all privileges, because nobody is doing authentication from CLI. -$application->StoreVar('user_id', USER_ROOT, true); - -$application->getConsoleApplication()->run(); +$application->Run(); $application->Done(); $end = microtime(true); Index: modules/in-commerce/units/addresses/addresses_event_handler.php =================================================================== --- modules/in-commerce/units/addresses/addresses_event_handler.php +++ modules/in-commerce/units/addresses/addresses_event_handler.php @@ -102,8 +102,10 @@ $object = $event->getObject(); /* @var $object kDBItem */ - if ( !$object->isLoaded() || !$this->checkItemStatus($event) ) { - // not trivially loaded object OR not current user address + if ( !$object->isLoaded() + || (!$this->Application->permissionCheckingDisabled() && !$this->checkItemStatus($event)) + ) { + // Not trivially loaded object OR not current user address. $event->status = kEvent::erPERM_FAIL; return ; } @@ -398,8 +400,10 @@ $object = $event->getObject(); /* @var $object kDBItem */ - if ( !$object->isLoaded() || !$this->checkItemStatus($event) ) { - // not trivially loaded object OR not current user address + if ( !$object->isLoaded() + || (!$this->Application->permissionCheckingDisabled() && !$this->checkItemStatus($event)) + ) { + // Not trivially loaded object OR not current user address. $event->status = kEvent::erPERM_FAIL; return; } @@ -455,4 +459,4 @@ ) )); } - } \ No newline at end of file + } Index: modules/in-commerce/units/orders/orders_event_handler.php =================================================================== --- modules/in-commerce/units/orders/orders_event_handler.php +++ modules/in-commerce/units/orders/orders_event_handler.php @@ -3793,7 +3793,7 @@ $fields['BillingCountry']['default'] = $site_helper->getDefaultCountry('Billing'); $fields['ShippingCountry']['default'] = $site_helper->getDefaultCountry('Shipping'); - if (!$this->Application->isAdminUser) { + if ( !$this->Application->isAdminUser && $this->Application->isWebRequest() ) { $user_groups = explode(',', $this->Application->RecallVar('UserGroups')); $default_group = $this->Application->ConfigValue('User_LoggedInGroup'); if (!in_array($default_group, $user_groups)){ Index: modules/in-commerce/units/reports/reports_event_handler.php =================================================================== --- modules/in-commerce/units/reports/reports_event_handler.php +++ modules/in-commerce/units/reports/reports_event_handler.php @@ -334,6 +334,10 @@ function OnUpdateConfig(kEvent $event) { + if ( !$this->Application->isWebRequest() ) { + return; + } + $report = $this->Application->RecallVar('report_options'); if ( !$report ) { Index: modules/in-link/units/link_validation/link_validation_eh.php =================================================================== --- modules/in-link/units/link_validation/link_validation_eh.php +++ modules/in-link/units/link_validation/link_validation_eh.php @@ -494,8 +494,8 @@ */ function OnPrepareLinkEditing($event) { - // hook to OnAfterConfigRead instead of OnEdit, because fake ids should be available in CheckPermission - if ( $this->Application->GetVar('l_event') != 'OnEdit' ) { + // Hook to OnAfterConfigRead instead of OnEdit, because fake ids should be available in CheckPermission. + if ( !$this->Application->isWebRequest() || $this->Application->GetVar('l_event') != 'OnEdit' ) { return; }