Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Sat, Feb 22, 12:00 AM

in-portal

This file is larger than 256 KB, so syntax highlighting was skipped.
Index: branches/5.3.x/core/kernel/session/inp_session.php
===================================================================
--- branches/5.3.x/core/kernel/session/inp_session.php (revision 16155)
+++ branches/5.3.x/core/kernel/session/inp_session.php (revision 16156)
@@ -1,92 +1,92 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
-class InpSession extends Session
+class Session extends BaseSession
{
/**
* Enter description here...
*
- * @var InpSessionStorage
+ * @var SessionStorage
* @access protected
*/
protected $Storage;
public function Init($prefix, $special)
{
$this->SessionTimeout = $this->Application->ConfigValue('SessionTimeout');
$path = (BASE_PATH == '') ? '/' : BASE_PATH;
$this->SetCookiePath($path);
$cookie_name = $this->Application->ConfigValue('SessionCookieName');
if ( !$cookie_name ) {
$cookie_name = 'sid';
}
$admin_session = ($this->Application->isAdmin && $special !== 'front') || ($special == 'admin');
if ( $admin_session ) {
$cookie_name = 'adm_' . $cookie_name;
}
$this->SetCookieName($cookie_name);
$this->SetCookieDomain(SERVER_NAME);
if ( $admin_session ) {
$mode = self::smAUTO;
}
elseif ( defined('IS_INSTALL') && IS_INSTALL ) {
$mode = self::smCOOKIES_ONLY;
}
else {
$ses_mode = $this->Application->ConfigValue('CookieSessions');
if ( $ses_mode == 2 ) {
$mode = self::smAUTO;
}
elseif ( $ses_mode == 1 ) {
$mode = self::smCOOKIES_ONLY;
}
elseif ( $ses_mode == 0 ) {
$mode = self::smGET_ONLY;
}
else {
$mode = self::smAUTO;
}
}
$this->SetMode($mode);
parent::Init($prefix, $special);
}
function Destroy()
{
$this->Storage->DeleteSession();
$this->Storage->DeleteEditTables();
$this->Data = new Params();
$this->SID = $this->CachedSID = '';
if ($this->CookiesEnabled) {
$this->SetSessionCookie(); //will remove the cookie due to value (sid) is empty
}
$this->SetSession(); //will create a new session
}
}
Index: branches/5.3.x/core/kernel/session/inp_session_storage.php
===================================================================
--- branches/5.3.x/core/kernel/session/inp_session_storage.php (revision 16155)
+++ branches/5.3.x/core/kernel/session/inp_session_storage.php (revision 16156)
@@ -1,97 +1,98 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
-class InpSessionStorage extends SessionStorage {
+class SessionStorage extends BaseSessionStorage
+{
public function Init($prefix, $special)
{
parent::Init($prefix, $special);
$this->TableName = TABLE_PREFIX.'UserSessions';
$this->SessionDataTable = TABLE_PREFIX.'UserSessionData';
$this->IDField = 'SessionKey';
$this->TimestampField = 'LastAccessed';
$this->DataValueField = 'VariableValue';
$this->DataVarField = 'VariableName';
}
function LocateSession($sid)
{
$res = parent::LocateSession($sid);
if ($res) {
$this->Expiration += $this->SessionTimeout;
}
return $res;
}
function UpdateSession($timeout = 0)
{
$time = time();
// Update LastAccessed only if it's newer than 1/10 of session timeout - perfomance optimization to eliminate needless updates on every click
// if ($time - $this->DirectVars['LastAccessed'] > $this->SessionTimeout/10) {
$this->SetField($this->TimestampField, $time + $this->SessionTimeout);
// }
}
function GetSessionDefaults()
{
$fields_hash = Array (
'PortalUserId' => $this->Application->isAdmin ? 0 : USER_GUEST,
'Language' => $this->Application->GetDefaultLanguageId(true),
'Theme' => $this->Application->GetDefaultThemeId(),
'GroupId' => $this->Application->ConfigValue('User_GuestGroup'),
'GroupList' => $this->Application->ConfigValue('User_GuestGroup'),
'IpAddress' => $this->Application->getClientIp(),
);
if ( !$this->Application->isAdmin ) {
// Guest users on Front-End belongs to Everyone group too
$fields_hash['GroupList'] .= ',' . $this->Application->ConfigValue('User_LoggedInGroup');
}
return array_merge($fields_hash, parent::GetSessionDefaults());
}
function GetExpiredSIDs()
{
$query = ' SELECT '.$this->IDField.' FROM '.$this->TableName.' WHERE '.$this->TimestampField.' < '.(time());
$ret = $this->Conn->GetCol($query);
if($ret) {
$this->DeleteEditTables();
}
return $ret;
}
function DeleteEditTables()
{
$tables = $this->Conn->GetCol('SHOW TABLES');
$mask_edit_table = '/'.TABLE_PREFIX.'ses_(.*)_edit_(.*)/';
$mask_search_table = '/'.TABLE_PREFIX.'ses_(.*?)_(.*)/';
$sql='SELECT COUNT(*) FROM '.$this->TableName.' WHERE '.$this->IDField.' = \'%s\'';
foreach($tables as $table)
{
if( preg_match($mask_edit_table,$table,$rets) || preg_match($mask_search_table,$table,$rets) )
{
$sid = preg_replace('/(.*)_(.*)/', '\\1', $rets[1]); // remove popup's wid from sid
$is_alive = $this->Conn->GetOne( sprintf($sql,$sid) );
if(!$is_alive) $this->Conn->Query('DROP TABLE IF EXISTS '.$table);
}
}
}
-}
\ No newline at end of file
+}
Index: branches/5.3.x/core/kernel/session/session.php
===================================================================
--- branches/5.3.x/core/kernel/session/session.php (revision 16155)
+++ branches/5.3.x/core/kernel/session/session.php (revision 16156)
@@ -1,1131 +1,1132 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
/*
The session works the following way:
1. When a visitor loads a page from the site the script checks if cookies_on varibale has been passed to it as a cookie.
2. If it has been passed, the script tries to get Session ID (SID) from the request:
3. Depending on session mode the script is getting SID differently.
The following modes are available:
- Session::smAUTO
Automatic mode: if cookies are on at the client side, the script relays only on cookies and
ignore all other methods of passing SID. If cookies are off at the client side, the script relays on SID
passed through query string and referal passed by the client. THIS METHOD IS NOT 100% SECURE, as long as
attacker may get SID and substitude referal to gain access to user' session. One of the faults of this method
is that the session is only created when the visitor clicks the first link on the site, so there
is NO session at the first load of the page. (Actually there is a session, but it gets lost after
the first click because we do not use SID in query string while we are not sure if we need it)
- Session::smCOOKIES_ONLY
Cookies only: in this mode the script relays solely on cookies passed from the browser and ignores
all other methods. In this mode there is no way to use sessions for clients without cookies support
or cookies support disabled. The cookies are stored with the full domain name and path to base-directory
of script installation.
- Session::smGET_ONLY
GET only: the script will not set any cookies and will use only SID passed in query string using GET,
it will also check referal. The script will set SID at the first load of the page
- Session::smCOOKIES_AND_GET
Combined mode: the script will use both cookies and GET right from the start. If client has cookies enabled,
the script will check SID stored in cookie and passed in query string, and will use this SID only if both
cookie and query string matches. However if cookies are disabled on the client side, the script will work
the same way as in GET_ONLY mode.
4. After the script has the SID it tries to load it from the Storage (default is database)
5. If such SID is found in the database, the script checks its expiration time. If session is not expired,
it updates its expiration, and resend the cookie (if applicable to session mode)
6. Then the script loads all the data (session variables) pertaining to the SID.
Usage:
$session = new Session(Session::smAUTO); //Session::smAUTO is default, you could just leave the brackets empty, or provide another mode
$session->SetCookieDomain('my.domain.com');
$session->SetCookiePath('/myscript');
$session->SetCookieName('my_sid_cookie');
$session->SetGETName('sid');
$session->InitSession();
...
//link output:
echo "<a href='index.php?'". ( $session->NeedQueryString() ? 'sid='.$session->SID : '' ) .">My Link</a>";
*/
-class Session extends kBase {
+class BaseSession extends kBase
+{
const smAUTO = 1;
const smCOOKIES_ONLY = 2;
const smGET_ONLY = 3;
const smCOOKIES_AND_GET = 4;
var $Checkers;
var $Mode;
var $OriginalMode = null;
var $GETName = 'sid';
var $CookiesEnabled = true;
var $CookieName = 'sid';
var $CookieDomain;
var $CookiePath;
var $CookieSecure = 0;
var $SessionTimeout = 3600;
var $Expiration;
var $SID;
var $CachedSID;
var $SessionSet = false;
/**
* Session ID is used from GET
*
* @var bool
*/
var $_fromGet = false;
/**
* Enter description here...
*
- * @var SessionStorage
+ * @var BaseSessionStorage
* @access protected
*/
protected $Storage;
var $CachedNeedQueryString = null;
/**
* Session Data array
*
* @var Params
*/
var $Data;
/**
* Names of optional session keys with their optional values (which does not need to be always stored)
*
* @var Array
*/
var $OptionalData = Array ();
/**
* Session expiration mark
*
* @var bool
*/
var $expired = false;
/**
* Creates session
*
* @param int $mode
* @access public
*/
public function __construct($mode = self::smAUTO)
{
parent::__construct();
$this->SetMode($mode);
}
function SetMode($mode)
{
$this->Mode = $mode;
$this->CachedNeedQueryString = null;
$this->CachedSID = null;
}
function SetCookiePath($path)
{
$this->CookiePath = str_replace(' ', '%20', $path);
}
/**
* Setting cookie domain. Set false for local domains, because they don't contain dots in their names.
*
* @param string $domain
*/
function SetCookieDomain($domain)
{
// 1. localhost or other like it without "." in domain name
if (!substr_count($domain, '.')) {
// don't use cookie domain at all
$this->CookieDomain = false;
return ;
}
// 2. match using predefined cookie domains from configuration
$cookie_domains = $this->Application->ConfigValue('SessionCookieDomains');
if ($cookie_domains) {
$cookie_domains = array_map('trim', explode("\n", $cookie_domains));
foreach ($cookie_domains as $cookie_domain) {
if (ltrim($cookie_domain, '.') == $domain) {
$this->CookieDomain = $cookie_domain; // as defined in configuration
return ;
}
}
}
// 3. only will execute, when none of domains were matched at previous step
$this->CookieDomain = $this->_autoGuessDomain($domain);
}
/**
* Auto-guess cookie domain based on $_SERVER['HTTP_HOST']
*
* @param $domain
* @return string
*/
function _autoGuessDomain($domain)
{
static $cache = Array ();
if (!array_key_exists($domain, $cache)) {
switch ( substr_count($domain, '.') ) {
case 2:
// 3rd level domain (3 parts)
$cache[$domain] = substr($domain, strpos($domain, '.')); // with leading "."
break;
case 1:
// 2rd level domain (2 parts)
$cache[$domain] = '.' . $domain; // with leading "."
break;
default:
// more then 3rd level
$cache[$domain] = ltrim($domain, '.'); // without leading "."
break;
}
}
return $cache[$domain];
}
function SetGETName($get_name)
{
$this->GETName = $get_name;
}
function SetCookieName($cookie_name)
{
$this->CookieName = $cookie_name;
}
function InitStorage($special)
{
$this->Storage = $this->Application->recallObject('SessionStorage.'.$special);
$this->Storage->setSession($this);
}
public function Init($prefix, $special)
{
parent::Init($prefix, $special);
if ( php_sapi_name() == 'cli' ) {
$this->SetMode(self::smGET_ONLY);
}
$this->CheckIfCookiesAreOn();
$this->Checkers = Array();
$this->InitStorage($special);
$this->Data = new Params();
$tmp_sid = $this->GetPassedSIDValue();
$check = $this->Check();
if ($this->Application->isAdmin) {
// 1. Front-End session may not be created (SID is present, but no data in database).
// Check expiration LATER from kApplication::Init, because template, used in session
// expiration redirect should be retrieved from mod-rewrite url first.
// 2. Admin sessions are always created, so case when SID is present,
// but session in database isn't is 100% session expired. Check expiration
// HERE because Session::SetSession will create missing session in database
// and when Session::ValidateExpired will be called later from kApplication::Init
// it won't consider such session as expired !!!
$this->ValidateExpired();
}
if ($check) {
$this->SID = $this->GetPassedSIDValue();
$this->Refresh();
$this->LoadData();
}
else {
$this->SetSession();
}
if (!is_null($this->OriginalMode)) $this->SetMode($this->OriginalMode);
}
function ValidateExpired()
{
if (defined('IS_INSTALL') && IS_INSTALL) {
return ;
}
// $this->DeleteExpired(); // called from u:OnDeleteExpiredSessions scheduled task now
if ($this->expired || ($this->CachedSID && !$this->_fromGet && !$this->SessionSet)) {
$this->RemoveSessionCookie();
// true was here to force new session creation, but I (kostja) used
// RemoveCookie a line above, to avoid redirect loop with expired sid
// not being removed setSession with true was used before, to set NEW
// session cookie
$this->SetSession();
// case #1: I've OR other site visitor expired my session
// case #2: I have no session in database, but SID is present
$this->expired = false;
$this->Application->HandleEvent(new kEvent('u:OnSessionExpire'));
}
}
/**
* Helper method for detecting cookie availability
*
* @return bool
*/
function _checkCookieReferer()
{
// removing /admin for compatability with in-portal (in-link/admin/add_link.php)
$path = preg_replace('/admin[\/]{0,1}$/', '', $this->CookiePath);
$reg = '#^'.preg_quote(PROTOCOL.ltrim($this->CookieDomain, '.').$path).'#';
return preg_match($reg, getArrayValue($_SERVER, 'HTTP_REFERER') );
}
function CheckIfCookiesAreOn()
{
if ( $this->Mode == self::smGET_ONLY ) {
//we don't need to bother checking if we would not use it
$this->CookiesEnabled = false;
return false;
}
- $http_query = $this->Application->recallObject('HTTPQuery');
+ $http_query = $this->Application->recallObject('kHTTPQuery');
/* @var $http_query kHTTPQuery */
$cookies_on = array_key_exists('cookies_on', $http_query->Cookie); // not good here
$get_sid = getArrayValue($http_query->Get, $this->GETName);
if ( ($this->Application->HttpQuery->IsHTTPSRedirect() && $get_sid) || $this->getFlashSID() ) { // Redirect from http to https on different domain OR flash uploader
$this->OriginalMode = $this->Mode;
$this->SetMode(self::smGET_ONLY);
}
if ( !$cookies_on || $this->Application->HttpQuery->IsHTTPSRedirect() || $this->getFlashSID() ) {
//If referer is our server, but we don't have our cookies_on, it's definetly off
$is_install = defined('IS_INSTALL') && IS_INSTALL;
if ( !$is_install && $this->_checkCookieReferer() && !$this->Application->GetVar('admin') && !$this->Application->HttpQuery->IsHTTPSRedirect() ) {
$this->CookiesEnabled = false;
}
else {
//Otherwise we still suppose cookies are on, because may be it's the first time user visits the site
//So we send cookies on to get it next time (when referal will tell us if they are realy off
$this->SetCookie('cookies_on', 1, time() + 31104000); //one year should be enough
}
}
else {
$this->CookiesEnabled = true;
}
return $this->CookiesEnabled;
}
/**
* Sets cookie for current site using path and domain
*
* @param string $name
* @param mixed $value
* @param int $expires
*/
function SetCookie($name, $value, $expires = null)
{
if (isset($expires) && $expires < time()) {
unset($this->Application->HttpQuery->Cookie[$name]);
}
else {
$this->Application->HttpQuery->Cookie[$name] = $value;
}
$old_style_domains = Array (
// domain like in pre 5.1.0 versions
'.' . SERVER_NAME,
// auto-guessed domain (when user specified other domain in configuration variable)
$this->_autoGuessDomain(SERVER_NAME)
);
$cookie_hasher = $this->Application->makeClass('kCookieHasher');
/* @var $cookie_hasher kCookieHasher */
$encrypted_value = $cookie_hasher->encrypt($name, $value);
foreach ($old_style_domains as $old_style_domain) {
if ($this->CookieDomain != $old_style_domain) {
// new style cookie domain -> delete old style cookie to prevent infinite redirect
setcookie($name, $encrypted_value, time() - 3600, $this->CookiePath, $old_style_domain, $this->CookieSecure, true);
}
}
setcookie($name, $encrypted_value, $expires, $this->CookiePath, $this->CookieDomain, $this->CookieSecure, true);
}
function Check()
{
// don't check referer here, because it doesn't provide any security option and can be easily falsified
$sid = $this->GetPassedSIDValue();
if (empty($sid)) {
return false;
}
//try to load session by sid, if everything is fine
$result = $this->LoadSession($sid);
$this->SessionSet = $result; // fake front-end session will given "false" here
return $result;
}
function LoadSession($sid)
{
if( $this->Storage->LocateSession($sid) ) {
// if we have session with such SID - get its expiration
$this->Expiration = $this->Storage->GetExpiration();
// If session has expired
if ($this->Expiration < time()) {
// when expired session is loaded, then SID is
// not assigned, but used in Destroy method
$this->SID = $sid;
$this->Destroy();
$this->expired = true;
// when Destory methods calls SetSession inside and new session get created
return $this->SessionSet;
}
// Otherwise it's ok
return true;
}
else {
// fake or deleted due to expiration SID
if (!$this->_fromGet) {
$this->expired = true;
}
return false;
}
}
function getFlashSID()
{
- $http_query = $this->Application->recallObject('HTTPQuery');
+ $http_query = $this->Application->recallObject('kHTTPQuery');
/* @var $http_query kHTTPQuery */
return getArrayValue($http_query->Post, 'flashsid');
}
function GetPassedSIDValue($use_cache = 1)
{
if (!empty($this->CachedSID) && $use_cache) {
return $this->CachedSID;
}
// flash sid overrides regular sid
$get_sid = $this->getFlashSID();
if (!$get_sid) {
- $http_query = $this->Application->recallObject('HTTPQuery');
+ $http_query = $this->Application->recallObject('kHTTPQuery');
/* @var $http_query kHTTPQuery */
$get_sid = getArrayValue($http_query->Get, $this->GETName);
}
$sid_from_get = $get_sid ? true : false;
if ($this->Application->GetVar('admin') == 1 && $get_sid) {
$sid = $get_sid;
}
else {
switch ($this->Mode) {
case self::smAUTO:
//Cookies has the priority - we ignore everything else
$sid = $this->CookiesEnabled ? $this->GetSessionCookie() : $get_sid;
if ($this->CookiesEnabled) {
$sid_from_get = false;
}
break;
case self::smCOOKIES_ONLY:
$sid = $this->GetSessionCookie();
break;
case self::smGET_ONLY:
$sid = $get_sid;
break;
case self::smCOOKIES_AND_GET:
$cookie_sid = $this->GetSessionCookie();
//both sids should match if cookies are enabled
if (!$this->CookiesEnabled || ($cookie_sid == $get_sid)) {
$sid = $get_sid; //we use get here just in case cookies are disabled
}
else {
$sid = '';
$sid_from_get = false;
}
break;
}
}
$this->CachedSID = $sid;
$this->_fromGet = $sid_from_get;
return $this->CachedSID;
}
/**
* Returns session id
*
* @return int
* @access public
*/
function GetID()
{
return $this->SID;
}
/**
* Generates new session id
*
* @return int
* @access private
*/
function GenerateSID()
{
$this->setSID(kUtil::generateId());
return $this->SID;
}
/**
* Set's new session id
*
* @param int $new_sid
* @access private
*/
function setSID($new_sid)
{
$this->SID /*= $this->CachedSID*/ = $new_sid; // don't set cached sid here
$this->Application->SetVar($this->GETName,$new_sid);
}
function NeedSession()
{
$data = $this->Data->GetParams();
$data_keys = array_keys($data);
$optional_keys = array_keys($this->OptionalData);
$real_keys = array_diff($data_keys, $optional_keys);
return $real_keys ? true : false;
}
function SetSession($force = false)
{
if ( $this->SessionSet && !$force ) {
return true;
}
$this->Expiration = time() + $this->SessionTimeout;
if ( !$force && /*!$this->Application->isAdmin &&*/ !$this->Application->GetVar('admin') && !$this->NeedSession() ) {
// don't create session (in db) on Front-End, when sid is present (GPC), but data in db isn't
if ( $this->_fromGet ) {
// set sid, that was given in GET
$this->setSID($this->GetPassedSIDValue());
}
else {
// re-generate sid only, when cookies are used
$this->GenerateSID();
}
$this->Storage->StoreSession(false);
return false;
}
if ( !$this->SID || $force ) {
$this->GenerateSID();
}
switch ( $this->Mode ) {
case self::smAUTO:
if ( $this->CookiesEnabled ) {
$this->SetSessionCookie();
}
break;
case self::smGET_ONLY:
break;
case self::smCOOKIES_ONLY:
case self::smCOOKIES_AND_GET:
$this->SetSessionCookie();
break;
}
$this->Storage->StoreSession();
if ( $this->Application->isAdmin || $this->Special == 'admin' ) {
$this->StoreVar('admin', 1);
}
$this->SessionSet = true; // should be called before SaveData, because SaveData will try to SetSession again
if ( $this->Special != '' ) {
// front-session called from admin or otherwise, then save it's data
$this->SaveData();
}
$this->Application->resetCounters('UserSessions');
return true;
}
/**
* Returns SID from cookie.
*
* Use 2 cookies to have 2 expiration:
* - 1. for normal expiration when browser is not closed (30 minutes by default), configurable
* - 2. for advanced expiration when browser is closed
*
* @return int
*/
function GetSessionCookie()
{
$keep_session_on_browser_close = $this->Application->ConfigValue('KeepSessionOnBrowserClose');
if (isset($this->Application->HttpQuery->Cookie[$this->CookieName]) &&
( $keep_session_on_browser_close ||
(
!$keep_session_on_browser_close &&
isset($this->Application->HttpQuery->Cookie[$this->CookieName.'_live'])
&&
$this->Application->HttpQuery->Cookie[$this->CookieName] == $this->Application->HttpQuery->Cookie[$this->CookieName.'_live']
)
)
) {
return $this->Application->HttpQuery->Cookie[$this->CookieName];
}
return false;
}
/**
* Updates SID in cookie with new value
*
*/
function SetSessionCookie()
{
$this->SetCookie($this->CookieName, $this->SID, $this->Expiration);
$this->SetCookie($this->CookieName.'_live', $this->SID);
}
function RemoveSessionCookie()
{
$this->SetCookie($this->CookieName, '');
$this->SetCookie($this->CookieName.'_live', '');
}
/**
* Refreshes session expiration time
*
* @access private
*/
function Refresh()
{
if ($this->Application->GetVar('skip_session_refresh')) {
return ;
}
if ($this->CookiesEnabled) {
// we need to refresh the cookie
$this->SetSessionCookie();
}
$this->Storage->UpdateSession();
}
function Destroy()
{
$this->Storage->DeleteSession();
$this->Data = new Params();
$this->SID = $this->CachedSID = '';
$this->SessionSet = false;
if ($this->CookiesEnabled) {
$this->SetSessionCookie(); //will remove the cookie due to value (sid) is empty
}
$this->SetSession(true); //will create a new session, true to force
}
function NeedQueryString($use_cache = 1)
{
if ($this->CachedNeedQueryString != null && $use_cache) {
return $this->CachedNeedQueryString;
}
$result = false;
switch ($this->Mode) {
case self::smAUTO:
if (!$this->CookiesEnabled) {
$result = true;
}
break;
/*case self::smCOOKIES_ONLY:
break;*/
case self::smGET_ONLY:
case self::smCOOKIES_AND_GET:
$result = true;
break;
}
$this->CachedNeedQueryString = $result;
return $result;
}
function LoadData()
{
$this->Data->AddParams( $this->Storage->LoadData() );
}
/**
* Returns information about session contents
*
* @param bool $include_optional
* @return array
* @access public
*/
public function getSessionData($include_optional = true)
{
$session_data = $this->Data->GetParams();
ksort($session_data);
foreach ($session_data as $session_key => $session_value) {
if ( kUtil::IsSerialized($session_value) ) {
$session_data[$session_key] = unserialize($session_value);
}
}
if ( !$include_optional ) {
$optional_keys = array_keys($this->OptionalData);
foreach ($session_data as $session_key => $session_value) {
if ( in_array($session_key, $optional_keys) ) {
unset($session_data[$session_key]);
}
}
}
return $session_data;
}
/**
* Returns real session data, that was saved
*
* @param Array $session_data
* @return Array
* @access protected
*/
protected function _getRealSessionData($session_data)
{
$data_keys = array_keys($session_data);
$optional_keys = array_keys($this->OptionalData);
$real_keys = array_diff($data_keys, $optional_keys);
if ( !$real_keys ) {
return Array ();
}
$ret = Array ();
foreach ($real_keys as $real_key) {
$ret[$real_key] = $session_data[$real_key];
}
return $ret;
}
function PrintSession($comment = '')
{
if ( defined('DEBUG_MODE') && $this->Application->isDebugMode() && kUtil::constOn('DBG_SHOW_SESSIONDATA') ) {
// dump session data
$this->Application->Debugger->appendHTML('SessionStorage [' . ($this->RecallVar('admin') == 1 ? 'Admin' : 'Front-End') . '] (' . $comment . '):');
$session_data = $this->getSessionData();
$this->Application->Debugger->dumpVars($session_data);
if ( !$this->RecallVar('admin') ) {
// dump real keys (only for front-end)
$real_session_data = $this->_getRealSessionData($session_data);
if ( $real_session_data ) {
$this->Application->Debugger->appendHTML('Real Keys:');
$this->Application->Debugger->dumpVars($real_session_data);
}
}
}
if ( defined('DEBUG_MODE') && $this->Application->isDebugMode() && kUtil::constOn('DBG_SHOW_PERSISTENTDATA') ) {
// dump persistent session data
if ( $this->Storage->PersistentVars ) {
$this->Application->Debugger->appendHTML('Persistant Session:');
$session_data = $this->Storage->PersistentVars;
ksort($session_data);
foreach ($session_data as $session_key => $session_value) {
if ( kUtil::IsSerialized($session_value) ) {
$session_data[$session_key] = unserialize($session_value);
}
}
$this->Application->Debugger->dumpVars($session_data);
}
}
}
function SaveData($params = Array ())
{
if (!$this->SetSession()) { // call it here - it may be not set before, because there was no need; if there is a need, it will be set here
return;
}
if (!$this->Application->GetVar('skip_last_template') && $this->Application->GetVar('ajax') != 'yes') {
$this->SaveLastTemplate( $this->Application->GetVar('t'), $params );
}
$this->PrintSession('after save');
$this->Storage->SaveData();
}
/**
* Save last template
*
* @param string $t
* @param Array $params
*/
function SaveLastTemplate($t, $params = Array ())
{
$wid = $this->Application->GetVar('m_wid');
$last_env = $this->getLastTemplateENV($t, array('m_opener' => 'u'));
$last_template = basename($_SERVER['PHP_SELF']) . '|' . $last_env;
$this->StoreVar(rtrim('last_template_' . $wid, '_'), $last_template);
// prepare last_template for opener stack, module & session could be added later
$last_env = $this->getLastTemplateENV($t);
$last_template = basename($_SERVER['PHP_SELF']) . '|' . $last_env;
// save last_template in persistent session
if (!$wid) {
if ($this->Application->isAdmin) {
// only for main window, not popups, not login template, not temp mode (used in adm:MainFrameLink tag)
$temp_mode = false;
$passed = explode(',', $this->Application->GetVar('passed'));
foreach ($passed as $passed_prefix) {
if ($this->Application->GetVar($passed_prefix.'_mode')) {
$temp_mode = true;
break;
}
}
if (!$temp_mode) {
if ( $this->Application->GetVarDirect('section', 'Get') !== false ) {
// check directly in GET, because LinkVar (session -> request) used on these vars
$last_template .= '&section='.$this->Application->GetVar('section').'&module='.$this->Application->GetVar('module');
}
$this->StorePersistentVar('last_template_popup', $last_template);
}
}
elseif ($this->Application->GetVar('admin')) {
// admin checking by session data to prevent recursive session save
static $admin_saved = null;
if (!$this->RecallVar('admin') && !isset($admin_saved)) {
// bug: we get recursion in this place, when cookies are disabled in browser and we are browsing
// front-end in admin's frame (front-end session is initialized using admin's sid and they are
// mixed together)
$admin_saved = true;
$admin_session = $this->Application->recallObject('Session.admin');
/* @var $admin_session Session */
// save to admin last_template too, because when F5 is pressed in frameset Front-End frame should reload as well
$admin_session->StoreVar('last_template_popup', '../' . $last_template);
$admin_session->StorePersistentVar('last_template_popup', '../' . $last_template);
$admin_session->SaveData( Array ('save_last_template' => false) );
}
else {
// don't allow admin=1 & editing_mode=* to get in admin last_template
$last_template = preg_replace('/&(admin|editing_mode)=[\d]/', '', $last_template);
}
}
}
// save other last... variables for mystical purposes (customizations may be)
$this->StoreVar('last_url', $_SERVER['REQUEST_URI']); // needed by ord:StoreContinueShoppingLink
$this->StoreVar('last_env', $last_env);
$save_last_template = array_key_exists('save_last_template', $params) ? $params['save_last_template'] : true;
if ($save_last_template) {
// save last template here, because section & module could be added before
$this->StoreVar(rtrim('last_template_popup_'.$wid, '_'), $last_template);
}
}
protected function getLastTemplateENV($t, $params = null)
{
if (!isset($params)) {
$params = Array ();
}
if ($this->Application->GetVar('admin') && !array_key_exists('admin', $params) && !defined('EDITING_MODE')) {
$params['editing_mode'] = ''; // used in kApplication::Run
}
$params = array_merge($this->Application->getPassThroughVariables($params), $params);
return $this->Application->BuildEnv($t, $params, 'all', false, false);
}
/**
* Stores variable $val in session under name $var
*
* Use this method to store variable in session. Later this variable could be recalled.
*
* @param string $name Variable name
* @param mixed $value Variable value
* @param bool $optional
* @return void
* @access public
* @see Session::RecallVar()
*/
public function StoreVar($name, $value, $optional = false)
{
$this->Data->Set($name, $value);
if ( $optional ) {
// make variable optional, also remember optional value
$this->OptionalData[$name] = $value;
}
elseif ( !$optional && array_key_exists($name, $this->OptionalData) ) {
if ( $this->OptionalData[$name] == $value ) {
// same value as optional -> don't remove optional mark
return;
}
// make variable non-optional
unset($this->OptionalData[$name]);
}
}
/**
* Stores variable to persistent session
*
* @param string $name
* @param mixed $value
* @param bool $optional
* @return void
* @access public
*/
public function StorePersistentVar($name, $value, $optional = false)
{
$this->Storage->StorePersistentVar($name, $value, $optional);
}
function LoadPersistentVars()
{
$this->Storage->LoadPersistentVars();
}
/**
* Stores default value for session variable
*
* @param string $name
* @param string $value
* @param bool $optional
* @return void
* @access public
* @see Session::RecallVar()
* @see Session::StoreVar()
*/
public function StoreVarDefault($name, $value, $optional = false)
{
$tmp = $this->RecallVar($name);
if ( $tmp === false || $tmp == '' ) {
$this->StoreVar($name, $value, $optional);
}
}
/**
* Returns session variable value
*
* Return value of $var variable stored in Session. An optional default value could be passed as second parameter.
*
* @param string $name Variable name
* @param mixed $default Default value to return if no $var variable found in session
* @return mixed
* @access public
*/
public function RecallVar($name, $default = false)
{
$ret = $this->Data->Get($name);
return ($ret === false) ? $default : $ret;
}
/**
* Returns variable value from persistent session
*
* @param string $name
* @param mixed $default
* @return mixed
* @access public
*/
public function RecallPersistentVar($name, $default = false)
{
return $this->Storage->RecallPersistentVar($name, $default);
}
/**
* Deletes Session variable
*
* @param string $var
* @return void
* @access public
*/
public function RemoveVar($name)
{
$this->Storage->RemoveFromData($name);
$this->Data->Remove($name);
}
/**
* Removes variable from persistent session
*
* @param string $name
* @return void
* @access public
*/
public function RemovePersistentVar($name)
{
$this->Storage->RemovePersistentVar($name);
}
/**
* Ignores session variable value set before
*
* @param string $name
* @return void
* @access public
*/
public function RestoreVar($name)
{
$value = $this->Storage->GetFromData($name, '__missing__');
if ( $value === '__missing__' ) {
// there is nothing to restore (maybe session was not saved), look in optional variable values
$value = array_key_exists($name, $this->OptionalData) ? $this->OptionalData[$name] : false;
}
$this->StoreVar($name, $value);
}
function GetField($var_name, $default = false)
{
return $this->Storage->GetField($var_name, $default);
}
function SetField($var_name, $value)
{
$this->Storage->SetField($var_name, $value);
}
/**
* Deletes expired sessions
*
* @return Array expired sids if any
* @access private
*/
function DeleteExpired()
{
return $this->Storage->DeleteExpired();
}
/**
* Deletes given sessions
*
* @param $session_ids
* @param int $delete_reason
* @return void
*/
function DeleteSessions($session_ids, $delete_reason = SESSION_LOG_EXPIRED)
{
$this->Storage->DeleteSessions($session_ids, $delete_reason);
}
/**
* Allows to check if user in this session is logged in or not
*
* @return bool
*/
function LoggedIn()
{
$user_id = $this->RecallVar('user_id');
$ret = $user_id > 0;
if (($this->RecallVar('admin') == 1 || defined('ADMIN')) && ($user_id == USER_ROOT)) {
$ret = true;
}
return $ret;
}
}
Index: branches/5.3.x/core/kernel/session/session_storage.php
===================================================================
--- branches/5.3.x/core/kernel/session/session_storage.php (revision 16155)
+++ branches/5.3.x/core/kernel/session/session_storage.php (revision 16156)
@@ -1,528 +1,528 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
/**
- * Implements Session Store in the Database
- *
+ * Implements Session Store in the Database.
*/
-class SessionStorage extends kDBBase {
+class BaseSessionStorage extends kDBBase
+{
/**
* Reference to session
*
* @var Session
* @access protected
*/
protected $Session = null;
var $Expiration;
var $SessionTimeout = 0;
var $DirectVars = Array ();
var $ChangedDirectVars = Array ();
var $PersistentVars = Array ();
var $OriginalData = Array ();
var $TimestampField;
var $SessionDataTable;
var $DataValueField;
var $DataVarField;
public function Init($prefix, $special)
{
parent::Init($prefix, $special);
$this->TableName = 'sessions';
$this->IDField = 'sid';
$this->TimestampField = 'expire';
$this->SessionDataTable = 'SessionData';
$this->DataValueField = 'value';
$this->DataVarField = 'var';
}
/**
* Sets reference to session
*
* @param Session $session
*/
public function setSession(&$session)
{
$this->Session =& $session;
$this->SessionTimeout = $session->SessionTimeout;
}
/**
* Calculates browser signature
*
* @return string
*/
function _getBrowserSignature()
{
$signature_parts = Array(
'HTTP_USER_AGENT', 'SERVER_PROTOCOL',
'HTTP_ACCEPT_CHARSET', 'HTTP_ACCEPT_ENCODING', 'HTTP_ACCEPT_LANGUAGE'
);
$ret = '';
foreach ($signature_parts as $signature_part) {
if (array_key_exists($signature_part, $_SERVER)) {
$ret .= '&|&' . $_SERVER[$signature_part];
}
}
return md5( substr($ret, 3) );
}
function GetSessionDefaults()
{
$fields_hash = Array (
$this->IDField => $this->Session->SID,
$this->TimestampField => $this->Session->Expiration,
);
if (!defined('IS_INSTALL') || !IS_INSTALL) {
// this column was added only in 5.0.1 version,
// so accessing it while database is not upgraded
// will result in admin's inability to login inside
// installator
$fields_hash['BrowserSignature'] = $this->_getBrowserSignature();
}
// default values + values set during this script run
return array_merge($fields_hash, $this->DirectVars);
}
/**
* Stores session to database
*
* @param bool $to_database
*
* @return void
* @access public
*/
public function StoreSession($to_database = true)
{
if ( defined('IS_INSTALL') && IS_INSTALL && $to_database && !$this->Application->TableFound($this->TableName, true) ) {
return;
}
$fields_hash = $this->GetSessionDefaults();
if ( $to_database ) {
$this->Conn->doInsert($fields_hash, $this->TableName);
}
foreach ($fields_hash as $field_name => $field_value) {
$this->SetField($field_name, $field_value);
}
// ensure user groups are stored in a way, that kPermissionsHelper::CheckUserPermission can understand
$this->Session->StoreVar('UserGroups', $this->GetField('GroupList'), !$to_database);
}
function DeleteSession()
{
$this->DeleteSessions( Array ($this->Session->SID), SESSION_LOG_LOGGED_OUT );
$this->DirectVars = $this->ChangedDirectVars = $this->OriginalData = Array();
}
function UpdateSession($timeout = 0)
{
$this->SetField($this->TimestampField, $this->Session->Expiration);
$query = ' UPDATE '.$this->TableName.' SET '.$this->TimestampField.' = '.$this->Session->Expiration.' WHERE '.$this->IDField.' = '.$this->Conn->qstr($this->Session->SID);
$this->Conn->Query($query);
}
function LocateSession($sid)
{
$sql = 'SELECT *
FROM ' . $this->TableName . '
WHERE ' . $this->IDField . ' = ' . $this->Conn->qstr($sid);
$result = $this->Conn->GetRow($sql);
if ($result === false) {
return false;
}
// perform security checks to ensure, that session is used by it's creator
if ($this->Application->ConfigValue('SessionBrowserSignatureCheck') && ($result['BrowserSignature'] != $this->_getBrowserSignature()) && $this->Application->GetVar('flashsid') === false) {
return false;
}
if ($this->Application->ConfigValue('SessionIPAddressCheck') && ($result['IpAddress'] != $this->Application->getClientIp())) {
// most secure, except for cases where NAT (Network Address Translation)
// is used and two or more computers can have same IP address
return false;
}
$this->DirectVars = $result;
$this->Expiration = $result[$this->TimestampField];
return true;
}
function GetExpiration()
{
return $this->Expiration;
}
function LoadData()
{
$query = 'SELECT '.$this->DataValueField.','.$this->DataVarField.' FROM '.$this->SessionDataTable.' WHERE '.$this->IDField.' = '.$this->Conn->qstr($this->Session->SID);
$this->OriginalData = $this->Conn->GetCol($query, $this->DataVarField);
return $this->OriginalData;
}
/**
* Enter description here...
*
* @param string $var_name
* @param mixed $default
* @return mixed
*/
function GetField($var_name, $default = false)
{
return isset($this->DirectVars[$var_name]) ? $this->DirectVars[$var_name] : $default;
//return $this->Conn->GetOne('SELECT '.$var_name.' FROM '.$this->TableName.' WHERE `'.$this->IDField.'` = '.$this->Conn->qstr($this->Session->GetID()) );
}
function SetField($var_name, $value)
{
$value_changed = !isset($this->DirectVars[$var_name]) || ($this->DirectVars[$var_name] != $value);
if ($value_changed) {
$this->DirectVars[$var_name] = $value;
$this->ChangedDirectVars[] = $var_name;
$this->ChangedDirectVars = array_unique($this->ChangedDirectVars);
}
//return $this->Conn->Query('UPDATE '.$this->TableName.' SET '.$var_name.' = '.$this->Conn->qstr($value).' WHERE '.$this->IDField.' = '.$this->Conn->qstr($this->Session->GetID()) );
}
/**
* Saves changes in session to database using single REPLACE query
*
* @return void
* @access public
*/
public function SaveData()
{
if ( !$this->Session->SID ) {
// can't save without sid
return ;
}
$replace = '';
$ses_data = $this->Session->Data->GetParams();
foreach ($ses_data as $key => $value) {
if ( isset($this->OriginalData[$key]) && $this->OriginalData[$key] == $value ) {
continue; //skip unchanged session data
}
else {
$replace .= sprintf("(%s, %s, %s),", $this->Conn->qstr($this->Session->SID), $this->Conn->qstr($key), $this->Conn->qstr($value));
}
}
$replace = rtrim($replace, ',');
if ( $replace != '' ) {
$query = ' REPLACE INTO ' . $this->SessionDataTable . ' (' . $this->IDField . ', ' . $this->DataVarField . ', ' . $this->DataValueField . ') VALUES ' . $replace;
$this->Conn->Query($query);
}
if ( $this->ChangedDirectVars ) {
$changes = Array ();
foreach ($this->ChangedDirectVars as $var) {
$changes[] = $var . ' = ' . $this->Conn->qstr($this->DirectVars[$var]);
}
$query = ' UPDATE ' . $this->TableName . '
SET ' . implode(',', $changes) . '
WHERE ' . $this->IDField . ' = ' . $this->Conn->qstr($this->Session->GetID());
$this->Conn->Query($query);
}
}
function RemoveFromData($var)
{
if ($this->Session->SessionSet) {
// only, when session is stored in database
$sql = 'DELETE FROM ' . $this->SessionDataTable . '
WHERE ' . $this->IDField . ' = ' . $this->Conn->qstr($this->Session->SID) . ' AND ' . $this->DataVarField . ' = ' . $this->Conn->qstr($var);
$this->Conn->Query($sql);
}
unset($this->OriginalData[$var]);
}
function GetFromData($var, $default = false)
{
return array_key_exists($var, $this->OriginalData) ? $this->OriginalData[$var] : $default;
}
function GetExpiredSIDs()
{
$sql = 'SELECT ' . $this->IDField . '
FROM ' . $this->TableName . '
WHERE ' . $this->TimestampField . ' > ' . time();
return $this->Conn->GetCol($sql);
}
function DeleteExpired()
{
$expired_sids = $this->GetExpiredSIDs();
$this->DeleteSessions($expired_sids);
return $expired_sids;
}
function DeleteSessions($session_ids, $delete_reason = SESSION_LOG_EXPIRED)
{
if (!$session_ids) {
return ;
}
$log_table = $this->Application->getUnitConfig('session-log')->getTableName();
if ($log_table) {
// mark session with proper status
$sub_sql = 'SELECT ' . $this->TimestampField . ' - ' . $this->SessionTimeout . '
FROM ' . $this->TableName . '
WHERE ' . $this->IDField . ' = ' . $log_table . '.SessionId';
$sql = 'UPDATE ' . $log_table . '
SET Status = ' . $delete_reason . ', SessionEnd = (' . $sub_sql . ')
WHERE Status = ' . SESSION_LOG_ACTIVE . ' AND SessionId IN (' . implode(',', $session_ids) . ')';
$this->Conn->Query($sql);
}
$where_clause = ' WHERE ' . $this->IDField . ' IN (' . implode(',', $session_ids) . ')';
$sql = 'DELETE FROM ' . $this->SessionDataTable . $where_clause;
$this->Conn->Query($sql);
$sql = 'DELETE FROM ' . $this->TableName . $where_clause;
$this->Conn->Query($sql);
// delete debugger ouputs left of deleted sessions
foreach ($session_ids as $session_id) {
$debug_file = (defined('RESTRICTED') ? RESTRICTED : WRITEABLE . '/cache') . '/debug_@' . $session_id . '@.txt';
if (file_exists($debug_file)) {
@unlink($debug_file);
}
}
}
function LoadPersistentVars()
{
$user_id = $this->Session->RecallVar('user_id');
if ($user_id != USER_GUEST) {
// root & normal users
$sql = 'SELECT VariableValue, VariableName
FROM '.TABLE_PREFIX.'UserPersistentSessionData
WHERE PortalUserId = '.$user_id;
$this->PersistentVars = (array)$this->Conn->GetCol($sql, 'VariableName');
}
else {
$this->PersistentVars = Array ();
}
}
/**
* Stores variable to persistent session
*
* @param string $var_name
* @param mixed $var_value
* @param bool $optional
* @return void
* @access public
*/
public function StorePersistentVar($var_name, $var_value, $optional = false)
{
$user_id = $this->Session->RecallVar('user_id');
if ( $user_id == USER_GUEST || $user_id === false ) {
// -2 (when not logged in), false (when after u:OnLogout event)
$this->Session->StoreVar($var_name, $var_value, $optional);
return;
}
$this->PersistentVars[$var_name] = $var_value;
$key_clause = 'PortalUserId = ' . $user_id . ' AND VariableName = ' . $this->Conn->qstr($var_name);
$sql = 'SELECT VariableName
FROM ' . TABLE_PREFIX . 'UserPersistentSessionData
WHERE ' . $key_clause;
$record_found = $this->Conn->GetOne($sql);
$fields_hash = Array (
'PortalUserId' => $user_id,
'VariableName' => $var_name,
'VariableValue' => $var_value,
);
if ( $record_found ) {
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'UserPersistentSessionData', $key_clause);
}
else {
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'UserPersistentSessionData');
}
}
/**
* Gets persistent variable
*
* @param string $var_name
* @param mixed $default
* @return mixed
* @access public
*/
public function RecallPersistentVar($var_name, $default = false)
{
if ( $this->Session->RecallVar('user_id') == USER_GUEST ) {
if ( $default == ALLOW_DEFAULT_SETTINGS ) {
$default = null;
}
return $this->Session->RecallVar($var_name, $default);
}
if ( array_key_exists($var_name, $this->PersistentVars) ) {
return $this->PersistentVars[$var_name];
}
elseif ( $default == ALLOW_DEFAULT_SETTINGS ) {
$default_user_id = $this->Application->ConfigValue('DefaultSettingsUserId');
if ( !$default_user_id ) {
$default_user_id = USER_ROOT;
}
$sql = 'SELECT VariableValue, VariableName
FROM ' . TABLE_PREFIX . 'UserPersistentSessionData
WHERE VariableName = ' . $this->Conn->qstr($var_name) . ' AND PortalUserId = ' . $default_user_id;
$value = $this->Conn->GetOne($sql);
$this->PersistentVars[$var_name] = $value;
if ( $value !== false ) {
$this->StorePersistentVar($var_name, $value); //storing it, so next time we don't load default user setting
}
return $value;
}
return $default;
}
/**
* Removes variable from persistent session
*
* @param string $var_name
* @return void
* @access public
*/
function RemovePersistentVar($var_name)
{
unset($this->PersistentVars[$var_name]);
$user_id = $this->Session->RecallVar('user_id');
if ( $user_id == USER_GUEST || $user_id === false ) {
// -2 (when not logged in), false (when after u:OnLogout event)
$this->Session->RemoveVar($var_name);
}
else {
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'UserPersistentSessionData
WHERE PortalUserId = ' . $user_id . ' AND VariableName = ' . $this->Conn->qstr($var_name);
$this->Conn->Query($sql);
}
}
/**
* Checks of object has given field
*
* @param string $name
*
* @return bool
* @access public
* @throws BadMethodCallException Always.
*/
public function HasField($name)
{
throw new BadMethodCallException('Unsupported');
}
/**
* Returns field values
*
* @return Array
* @access public
* @throws BadMethodCallException Always.
*/
public function GetFieldValues()
{
throw new BadMethodCallException('Unsupported');
}
/**
* Returns unformatted field value
*
* @param string $field
*
* @return string
* @access public
* @throws BadMethodCallException Always.
*/
public function GetDBField($field)
{
throw new BadMethodCallException('Unsupported');
}
/**
* Returns true, when list/item was queried/loaded
*
* @return bool
* @access public
* @throws BadMethodCallException Always.
*/
public function isLoaded()
{
throw new BadMethodCallException('Unsupported');
}
/**
* Returns specified field value from all selected rows.
* Don't affect current record index
*
* @param string $field
*
* @return Array
* @access public
* @throws BadMethodCallException Always.
*/
public function GetCol($field)
{
throw new BadMethodCallException('Unsupported');
}
-}
\ No newline at end of file
+}
Index: branches/5.3.x/core/kernel/db/cat_event_handler.php
===================================================================
--- branches/5.3.x/core/kernel/db/cat_event_handler.php (revision 16155)
+++ branches/5.3.x/core/kernel/db/cat_event_handler.php (revision 16156)
@@ -1,3101 +1,3101 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class kCatDBEventHandler extends kDBEventHandler {
/**
* Allows to override standard permission mapping
*
* @return void
* @access protected
* @see kEventHandler::$permMapping
*/
protected function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnSaveSettings' => Array ('self' => 'add|edit|advanced:import'),
'OnResetSettings' => Array ('self' => 'add|edit|advanced:import'),
'OnBeforeDeleteOriginal' => Array ('self' => 'edit|advanced:approve'),
'OnAfterDeleteOriginal' => Array ('self' => 'edit|advanced:approve'),
'OnCopy' => Array ('self' => true),
'OnDownloadFile' => Array ('self' => 'view'),
'OnCancelAction' => Array ('self' => true),
'OnItemBuild' => Array ('self' => true),
'OnMakeVote' => Array ('self' => true),
'OnReviewHelpful' => Array ('self' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Load item if id is available
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function LoadItem(kEvent $event)
{
$object = $event->getObject();
/* @var $object kDBItem */
$id = $this->getPassedID($event);
if ( $object->Load($id) ) {
$actions = $this->Application->recallObject('kActions');
/* @var $actions Params */
$actions->Set($event->getPrefixSpecial() . '_id', $object->GetID());
$use_pending_editing = $event->getUnitConfig()->getUsePendingEditing();
if ( $use_pending_editing && $event->Special != 'original' ) {
$this->Application->SetVar($event->Prefix . '.original_id', $object->GetDBField('OrgId'));
}
}
else {
$object->setID($id);
}
}
/**
* Checks user permission to execute given $event
*
* @param kEvent $event
* @return bool
* @access public
*/
public function CheckPermission(kEvent $event)
{
if ( !$this->Application->isAdmin ) {
if ( $event->Name == 'OnSetSortingDirect' ) {
// allow sorting on front event without view permission
return true;
}
}
if ( $event->Name == 'OnExport' ) {
// save category_id before doing export
$this->Application->LinkVar('m_cat_id');
}
if ( in_array($event->Name, $this->_getMassPermissionEvents()) ) {
$items = $this->_getPermissionCheckInfo($event);
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
if ( ($event->Name == 'OnSave') && array_key_exists(0, $items) ) {
// adding new item (ID = 0)
$perm_value = $perm_helper->AddCheckPermission($items[0]['CategoryId'], $event->Prefix) > 0;
}
else {
// leave only items, that can be edited
$ids = Array ();
$check_method = in_array($event->Name, Array ('OnMassDelete', 'OnCut')) ? 'DeleteCheckPermission' : 'ModifyCheckPermission';
foreach ($items as $item_id => $item_data) {
if ( $perm_helper->$check_method($item_data['CreatedById'], $item_data['CategoryId'], $event->Prefix) > 0 ) {
$ids[] = $item_id;
}
}
if ( !$ids ) {
// no items left for editing -> no permission
return $perm_helper->finalizePermissionCheck($event, false);
}
$perm_value = true;
$event->setEventParam('ids', $ids); // will be used later by "kDBEventHandler::StoreSelectedIDs" method
}
return $perm_helper->finalizePermissionCheck($event, $perm_value);
}
$export_events = Array ('OnSaveSettings', 'OnResetSettings', 'OnExportBegin');
if ( in_array($event->Name, $export_events) ) {
// when import settings before selecting target import category
return $this->Application->CheckPermission('in-portal:main_import.view');
}
if ( $event->Name == 'OnProcessSelected' ) {
if ( $this->Application->RecallVar('dst_field') == 'ImportCategory' ) {
// when selecting target import category
return $this->Application->CheckPermission('in-portal:main_import.view');
}
}
return parent::CheckPermission($event);
}
/**
* Returns events, that require item-based (not just event-name based) permission check
*
* @return Array
*/
function _getMassPermissionEvents()
{
return Array (
'OnEdit', 'OnSave', 'OnMassDelete', 'OnMassApprove',
'OnMassDecline', 'OnMassMoveUp', 'OnMassMoveDown',
'OnCut',
);
}
/**
* Returns category item IDs, that require permission checking
*
* @param kEvent $event
* @return string
*/
function _getPermissionCheckIDs($event)
{
if ($event->Name == 'OnSave') {
$selected_ids = implode(',', $this->getSelectedIDs($event, true));
if (!$selected_ids) {
$selected_ids = 0; // when saving newly created item (OnPreCreate -> OnPreSave -> OnSave)
}
}
else {
// OnEdit, OnMassDelete events, when items are checked in grid
$selected_ids = implode(',', $this->StoreSelectedIDs($event));
}
return $selected_ids;
}
/**
* Returns information used in permission checking
*
* @param kEvent $event
* @return Array
*/
function _getPermissionCheckInfo($event)
{
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
// when saving data from temp table to live table check by data from temp table
$item_ids = $this->_getPermissionCheckIDs($event);
$items = $perm_helper->GetCategoryItemData($event->Prefix, $item_ids, $event->Name == 'OnSave');
if (!$items) {
// when item not present in temp table, then permission is not checked, because there are no data in db to check
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
list ($id, $fields_hash) = each($items_info);
if (array_key_exists('CategoryId', $fields_hash)) {
$item_category = $fields_hash['CategoryId'];
}
else {
$item_category = $this->Application->GetVar('m_cat_id');
}
$items[$id] = Array (
'CreatedById' => $this->Application->RecallVar('use_id'),
'CategoryId' => $item_category,
);
}
return $items;
}
/**
* Add selected items to clipboard with mode = COPY (CLONE)
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnCopy($event)
{
$this->Application->RemoveVar('clipboard');
$clipboard_helper = $this->Application->recallObject('ClipboardHelper');
/* @var $clipboard_helper kClipboardHelper */
$clipboard_helper->setClipboard($event, 'copy', $this->StoreSelectedIDs($event));
$this->clearSelectedIDs($event);
}
/**
* Add selected items to clipboard with mode = CUT
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnCut($event)
{
$this->Application->RemoveVar('clipboard');
$clipboard_helper = $this->Application->recallObject('ClipboardHelper');
/* @var $clipboard_helper kClipboardHelper */
$clipboard_helper->setClipboard($event, 'cut', $this->StoreSelectedIDs($event));
$this->clearSelectedIDs($event);
}
/**
* Checks permission for OnPaste event
*
* @param kEvent $event
* @return bool
*/
function _checkPastePermission($event)
{
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$category_id = $this->Application->GetVar('m_cat_id');
if ($perm_helper->AddCheckPermission($category_id, $event->Prefix) == 0) {
// no items left for editing -> no permission
return $perm_helper->finalizePermissionCheck($event, false);
}
return true;
}
/**
* Performs category item paste
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnPaste($event)
{
if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) || !$this->_checkPastePermission($event) ) {
$event->status = kEvent::erFAIL;
return;
}
$clipboard_data = $event->getEventParam('clipboard_data');
if ( !$clipboard_data['cut'] && !$clipboard_data['copy'] ) {
return;
}
if ( $clipboard_data['copy'] ) {
$temp = $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler', Array ('parent_event' => $event));
/* @var $temp kTempTablesHandler */
$this->Application->SetVar('ResetCatBeforeClone', 1); // used in "kCatDBEventHandler::OnBeforeClone"
$temp->CloneItems($event->Prefix, $event->Special, $clipboard_data['copy']);
}
if ( $clipboard_data['cut'] ) {
$object = $this->Application->recallObject($event->getPrefixSpecial() . '.item', $event->Prefix, Array ('skip_autoload' => true));
/* @var $object kCatDBItem */
foreach ($clipboard_data['cut'] as $id) {
$object->Load($id);
$object->MoveToCat();
}
}
}
/**
* Deletes all selected items.
* Automatically recurse into sub-items using temp handler, and deletes sub-items
* by calling its Delete method if sub-item has AutoDelete set to true in its config file
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnMassDelete(kEvent $event)
{
if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) {
$event->status = kEvent::erFAIL;
return;
}
$ids = $this->StoreSelectedIDs($event);
$to_delete = Array ();
$recycle_bin = $this->Application->ConfigValue('RecycleBinFolder');
if ( $recycle_bin ) {
$rb = $this->Application->recallObject('c.recycle', NULL, array ('skip_autoload' => true));
/* @var $rb CategoriesItem */
$rb->Load($recycle_bin);
$object = $this->Application->recallObject($event->Prefix . '.recycleitem', NULL, Array ('skip_autoload' => true));
/* @var $object kCatDBItem */
foreach ($ids as $id) {
$object->Load($id);
if ( preg_match('/^' . preg_quote($rb->GetDBField('ParentPath'), '/') . '/', $object->GetDBField('ParentPath')) ) {
$to_delete[] = $id;
continue;
}
$object->MoveToCat($recycle_bin);
}
$ids = $to_delete;
}
$temp_handler = $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler', Array ('parent_event' => $event));
/* @var $temp_handler kTempTablesHandler */
$event->setEventParam('ids', $ids);
$this->customProcessing($event, 'before');
$ids = $event->getEventParam('ids');
if ( $ids ) {
$temp_handler->DeleteItems($event->Prefix, $event->Special, $ids);
}
$this->clearSelectedIDs($event);
}
/**
* Return type clauses for list bulding on front
*
* @param kEvent $event
* @return Array
*/
function getTypeClauses($event)
{
$types = $event->getEventParam('types');
$types = $types ? explode(',', $types) : Array ();
$except_types = $event->getEventParam('except');
$except_types = $except_types ? explode(',', $except_types) : Array ();
$type_clauses = Array();
$user_id = $this->Application->RecallVar('user_id');
$owner_field = $this->getOwnerField($event->Prefix);
$type_clauses['my_items']['include'] = '%1$s.'.$owner_field.' = '.$user_id;
$type_clauses['my_items']['except'] = '%1$s.'.$owner_field.' <> '.$user_id;
$type_clauses['my_items']['having_filter'] = false;
$type_clauses['pick']['include'] = '%1$s.EditorsPick = 1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1';
$type_clauses['pick']['except'] = '%1$s.EditorsPick! = 1 AND '.TABLE_PREFIX.'CategoryItems.PrimaryCat = 1';
$type_clauses['pick']['having_filter'] = false;
$type_clauses['hot']['include'] = '`IsHot` = 1 AND PrimaryCat = 1';
$type_clauses['hot']['except'] = '`IsHot`! = 1 AND PrimaryCat = 1';
$type_clauses['hot']['having_filter'] = true;
$type_clauses['pop']['include'] = '`IsPop` = 1 AND PrimaryCat = 1';
$type_clauses['pop']['except'] = '`IsPop`! = 1 AND PrimaryCat = 1';
$type_clauses['pop']['having_filter'] = true;
$type_clauses['new']['include'] = '`IsNew` = 1 AND PrimaryCat = 1';
$type_clauses['new']['except'] = '`IsNew`! = 1 AND PrimaryCat = 1';
$type_clauses['new']['having_filter'] = true;
$type_clauses['displayed']['include'] = '';
$displayed = $this->Application->GetVar($event->Prefix.'_displayed_ids');
if ($displayed) {
$id_field = $event->getUnitConfig()->getIDField();
$type_clauses['displayed']['except'] = '%1$s.'.$id_field.' NOT IN ('.$displayed.')';
}
else {
$type_clauses['displayed']['except'] = '';
}
$type_clauses['displayed']['having_filter'] = false;
if (in_array('search', $types) || in_array('search', $except_types)) {
$event_mapping = Array (
'simple' => 'OnSimpleSearch',
'subsearch' => 'OnSubSearch',
'advanced' => 'OnAdvancedSearch'
);
$keywords = $event->getEventParam('keyword_string');
$type = $this->Application->GetVar('search_type', 'simple');
if ( $keywords ) {
// processing keyword_string param of ListProducts tag
$this->Application->SetVar('keywords', $keywords);
$type = 'simple';
}
$search_event = $event_mapping[$type];
$this->$search_event($event);
$object = $event->getObject();
/* @var $object kDBList */
$search_sql = ' FROM ' . TABLE_PREFIX . 'ses_' . $this->Application->GetSID() . '_' . TABLE_PREFIX . 'Search search_result
JOIN %1$s ON %1$s.ResourceId = search_result.ResourceId';
$sql = str_replace('FROM %1$s', $search_sql, $object->GetPlainSelectSQL());
$object->SetSelectSQL($sql);
$object->addCalculatedField('Relevance', 'search_result.Relevance');
$type_clauses['search']['include'] = 'PrimaryCat = 1 AND ('.TABLE_PREFIX.'Categories.Status = '.STATUS_ACTIVE.')';
$type_clauses['search']['except'] = 'PrimaryCat = 1 AND ('.TABLE_PREFIX.'Categories.Status = '.STATUS_ACTIVE.')';
$type_clauses['search']['having_filter'] = false;
}
if (in_array('related', $types) || in_array('related', $except_types)) {
$related_to = $event->getEventParam('related_to');
if (!$related_to) {
$related_prefix = $event->Prefix;
}
else {
$sql = 'SELECT Prefix
FROM '.TABLE_PREFIX.'ItemTypes
WHERE ItemName = '.$this->Conn->qstr($related_to);
$related_prefix = $this->Conn->GetOne($sql);
}
$rel_table = $this->Application->getUnitConfig('rel')->getTableName();
$item_type = (int)$event->getUnitConfig()->getItemType();
if ($item_type == 0) {
trigger_error('<strong>ItemType</strong> not defined for prefix <strong>' . $event->Prefix . '</strong>', E_USER_WARNING);
}
// process case, then this list is called inside another list
$prefix_special = $event->getEventParam('PrefixSpecial');
if (!$prefix_special) {
$prefix_special = $this->Application->Parser->GetParam('PrefixSpecial');
}
$id = false;
if ($prefix_special !== false) {
$processed_prefix = $this->Application->processPrefix($prefix_special);
if ($processed_prefix['prefix'] == $related_prefix) {
// printing related categories within list of items (not on details page)
$list = $this->Application->recallObject($prefix_special);
/* @var $list kDBList */
$id = $list->GetID();
}
}
if ($id === false) {
// printing related categories for single item (possibly on details page)
if ($related_prefix == 'c') {
$id = $this->Application->GetVar('m_cat_id');
}
else {
$id = $this->Application->GetVar($related_prefix . '_id');
}
}
$p_item = $this->Application->recallObject($related_prefix.'.current', NULL, Array('skip_autoload' => true));
/* @var $p_item kCatDBItem */
$p_item->Load( (int)$id );
$p_resource_id = $p_item->GetDBField('ResourceId');
$sql = 'SELECT SourceId, TargetId FROM '.$rel_table.'
WHERE
(Enabled = 1)
AND (
(Type = 0 AND SourceId = '.$p_resource_id.' AND TargetType = '.$item_type.')
OR
(Type = 1
AND (
(SourceId = '.$p_resource_id.' AND TargetType = '.$item_type.')
OR
(TargetId = '.$p_resource_id.' AND SourceType = '.$item_type.')
)
)
)';
$related_ids_array = $this->Conn->Query($sql);
$related_ids = Array();
foreach ($related_ids_array as $record) {
$related_ids[] = $record[ $record['SourceId'] == $p_resource_id ? 'TargetId' : 'SourceId' ];
}
if (count($related_ids) > 0) {
$type_clauses['related']['include'] = '%1$s.ResourceId IN ('.implode(',', $related_ids).') AND PrimaryCat = 1';
$type_clauses['related']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $related_ids).') AND PrimaryCat = 1';
}
else {
$type_clauses['related']['include'] = '0';
$type_clauses['related']['except'] = '1';
}
$type_clauses['related']['having_filter'] = false;
}
if (in_array('favorites', $types) || in_array('favorites', $except_types)) {
$sql = 'SELECT ResourceId
FROM ' . $this->Application->getUnitConfig('fav')->getTableName() . '
WHERE PortalUserId = '.$this->Application->RecallVar('user_id');
$favorite_ids = $this->Conn->GetCol($sql);
if ($favorite_ids) {
$type_clauses['favorites']['include'] = '%1$s.ResourceId IN ('.implode(',', $favorite_ids).') AND PrimaryCat = 1';
$type_clauses['favorites']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $favorite_ids).') AND PrimaryCat = 1';
}
else {
$type_clauses['favorites']['include'] = 0;
$type_clauses['favorites']['except'] = 1;
}
$type_clauses['favorites']['having_filter'] = false;
}
return $type_clauses;
}
/**
* Returns SQL clause, that will help to select only data from specified category & it's children
*
* @param int $category_id
* @return string
*/
function getCategoryLimitClause($category_id)
{
if (!$category_id) {
return false;
}
$tree_indexes = $this->Application->getTreeIndex($category_id);
if (!$tree_indexes) {
// id of non-existing category was given
return 'FALSE';
}
return TABLE_PREFIX.'Categories.TreeLeft BETWEEN '.$tree_indexes['TreeLeft'].' AND '.$tree_indexes['TreeRight'];
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
* @return void
* @access protected
* @see kDBEventHandler::OnListBuild()
*/
protected function SetCustomQuery(kEvent $event)
{
parent::SetCustomQuery($event);
$object = $event->getObject();
/* @var $object kCatDBList */
// add category filter if needed
if ($event->Special != 'showall' && $event->Special != 'user') {
if ( (string)$event->getEventParam('parent_cat_id') !== '' ) {
$parent_cat_id = $event->getEventParam('parent_cat_id');
}
else {
$parent_cat_id = $this->Application->GetVar('c_id');
if (!$parent_cat_id) {
$parent_cat_id = $this->Application->GetVar('m_cat_id');
}
if (!$parent_cat_id) {
$parent_cat_id = 0;
}
}
if ("$parent_cat_id" == '0') {
// replace "0" category with "Content" category id (this way template
$parent_cat_id = $this->Application->getBaseCategory();
}
if ((string)$parent_cat_id != 'any') {
if ($event->getEventParam('recursive')) {
$filter_clause = $this->getCategoryLimitClause($parent_cat_id);
if ($filter_clause !== false) {
$object->addFilter('category_filter', $filter_clause);
}
$object->addFilter('primary_filter', 'PrimaryCat = 1');
}
else {
$object->addFilter('category_filter', TABLE_PREFIX.'CategoryItems.CategoryId = '.$parent_cat_id );
}
}
else {
$object->addFilter('primary_filter', 'PrimaryCat = 1');
}
}
else {
$object->addFilter('primary_filter', 'PrimaryCat = 1');
// if using recycle bin don't show items from there
$recycle_bin = $this->Application->ConfigValue('RecycleBinFolder');
if ($recycle_bin) {
$object->addFilter('recyclebin_filter', TABLE_PREFIX.'CategoryItems.CategoryId <> '.$recycle_bin);
}
}
if ($event->Special == 'user') {
$editable_user = $this->Application->GetVar('u_id');
$object->addFilter('owner_filter', '%1$s.'.$this->getOwnerField($event->Prefix).' = '.$editable_user);
}
$this->applyViewPermissionFilter($object);
$types = $event->getEventParam('types');
$this->applyItemStatusFilter($object, $types);
$except_types = $event->getEventParam('except');
$type_clauses = $this->getTypeClauses($event);
$search_helper = $this->Application->recallObject('SearchHelper');
/* @var $search_helper kSearchHelper */
$search_helper->SetComplexFilter($event, $type_clauses, $types, $except_types);
}
/**
* Adds filter, that uses *.VIEW permissions to determine if an item should be shown to a user.
*
* @param kCatDBList $object Object.
*
* @return void
* @access protected
*/
protected function applyViewPermissionFilter(kCatDBList $object)
{
if ( !$this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) {
return;
}
if ( $this->Application->RecallVar('user_id') == USER_ROOT ) {
// for "root" CATEGORY.VIEW permission is checked for items lists too
$view_perm = 1;
}
else {
// for any real user item list view permission is checked instead of CATEGORY.VIEW
$count_helper = $this->Application->recallObject('CountHelper');
/* @var $count_helper kCountHelper */
list ($view_perm, $view_filter) = $count_helper->GetPermissionClause($object->Prefix, 'perm');
$object->addFilter('perm_filter2', $view_filter);
}
$object->addFilter('perm_filter', 'perm.PermId = ' . $view_perm);
}
/**
* Adds filter that filters out items with non-required statuses
*
* @param kDBList $object
* @param string $types
*/
function applyItemStatusFilter(&$object, $types)
{
// Link1 (before modifications) [Status = 1, OrgId = NULL], Link2 (after modifications) [Status = -2, OrgId = Link1_ID]
$pending_editing = $object->getUnitConfig()->getUsePendingEditing();
if (!$this->Application->isAdminUser) {
$types = explode(',', $types);
if (in_array('my_items', $types)) {
$allow_statuses = Array (STATUS_ACTIVE, STATUS_PENDING, STATUS_PENDING_EDITING);
$object->addFilter('status_filter', '%1$s.Status IN ('.implode(',', $allow_statuses).')');
if ($pending_editing) {
$user_id = $this->Application->RecallVar('user_id');
$this->applyPendingEditingFilter($object, $user_id);
}
}
else {
$object->addFilter('status_filter', '(%1$s.Status = ' . STATUS_ACTIVE . ') AND (' . TABLE_PREFIX . 'Categories.Status = ' . STATUS_ACTIVE . ')');
if ($pending_editing) {
// if category item uses pending editing abilities, then in no cases show pending copies on front
$object->addFilter('original_filter', '%1$s.OrgId = 0 OR %1$s.OrgId IS NULL');
}
}
}
else {
if ($pending_editing) {
$this->applyPendingEditingFilter($object);
}
}
}
/**
* Adds filter, that removes live items if they have pending editing copies
*
* @param kDBList $object
* @param int $user_id
*/
function applyPendingEditingFilter(&$object, $user_id = NULL)
{
$sql = 'SELECT OrgId
FROM '.$object->TableName.'
WHERE Status = '.STATUS_PENDING_EDITING.' AND OrgId IS NOT NULL';
if (isset($user_id)) {
$owner_field = $this->getOwnerField($object->Prefix);
$sql .= ' AND '.$owner_field.' = '.$user_id;
}
$pending_ids = $this->Conn->GetCol($sql);
if ($pending_ids) {
$object->addFilter('no_original_filter', '%1$s.'.$object->IDField.' NOT IN ('.implode(',', $pending_ids).')');
}
}
/**
* Adds calculates fields for item statuses
*
* @param kDBItem|kDBList $object
* @param kEvent $event
* @return void
* @access protected
*/
protected function prepareObject(&$object, kEvent $event)
{
$this->prepareItemStatuses($event);
$object->addCalculatedField('CachedNavbar', 'l' . $this->Application->GetVar('m_lang') . '_CachedNavbar');
if ( $event->Special == 'export' || $event->Special == 'import' ) {
$export_helper = $this->Application->recallObject('CatItemExportHelper');
/* @var $export_helper kCatDBItemExportHelper */
$export_helper->prepareExportColumns($event);
}
}
/**
* Creates calculated fields for all item statuses based on config settings
*
* @param kEvent $event
*/
function prepareItemStatuses($event)
{
$object = $event->getObject( Array('skip_autoload' => true) );
$property_map = $event->getUnitConfig()->getItemPropertyMappings();
if (!$property_map) {
return ;
}
// new items
$object->addCalculatedField('IsNew', ' IF(%1$s.NewItem = 2,
IF(%1$s.CreatedOn >= (UNIX_TIMESTAMP() - ' . $this->Application->ConfigValue($property_map['NewDays']) . '*3600*24), 1, 0),
%1$s.NewItem
)');
// hot items (cache updated every hour)
if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) {
$serial_name = $this->Application->incrementCacheSerial($event->Prefix, NULL, false);
$hot_limit = $this->Application->getCache($property_map['HotLimit'] . '[%' . $serial_name . '%]');
}
else {
$hot_limit = $this->Application->getDBCache($property_map['HotLimit']);
}
if ($hot_limit === false) {
$hot_limit = $this->CalculateHotLimit($event);
}
$object->addCalculatedField('IsHot', ' IF(%1$s.HotItem = 2,
IF(%1$s.'.$property_map['ClickField'].' >= '.$hot_limit.', 1, 0),
%1$s.HotItem
)');
// popular items
$object->addCalculatedField('IsPop', ' IF(%1$s.PopItem = 2,
IF(%1$s.CachedVotesQty >= ' . $this->Application->ConfigValue($property_map['MinPopVotes']) . ' AND %1$s.CachedRating >= ' . $this->Application->ConfigValue($property_map['MinPopRating']) . ', 1, 0),
%1$s.PopItem)');
}
/**
* Calculates hot limit for current item's table
*
* @param kEvent $event
* @return float
* @access protected
*/
protected function CalculateHotLimit($event)
{
$config = $event->getUnitConfig();
$property_map = $config->getItemPropertyMappings();
if ( !$property_map ) {
return 0.00;
}
$click_field = $property_map['ClickField'];
$last_hot = $this->Application->ConfigValue($property_map['MaxHotNumber']) - 1;
$sql = 'SELECT ' . $click_field . '
FROM ' . $config->getTableName() . '
ORDER BY ' . $click_field . ' DESC
LIMIT ' . $last_hot . ', 1';
$res = $this->Conn->GetCol($sql);
$hot_limit = (double)array_shift($res);
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$serial_name = $this->Application->incrementCacheSerial($event->Prefix, NULL, false);
$this->Application->setCache($property_map['HotLimit'] . '[%' . $serial_name . '%]', $hot_limit);
}
else {
$this->Application->setDBCache($property_map['HotLimit'], $hot_limit, 3600);
}
return $hot_limit;
}
/**
* Moves item to preferred category, updates item hits
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemUpdate(kEvent $event)
{
parent::OnBeforeItemUpdate($event);
$object = $event->getObject();
/* @var $object kCatDBItem */
// update hits field
$config = $event->getUnitConfig();
$property_map = $config->getUserProfileMapping();
if ( $property_map ) {
$click_field = $property_map['ClickField'];
if ( $this->Application->isAdminUser && ($this->Application->GetVar($click_field . '_original') !== false) && floor($this->Application->GetVar($click_field . '_original')) != $object->GetDBField($click_field) ) {
$sql = 'SELECT MAX(' . $click_field . ')
FROM ' . $config->getTableName() . '
WHERE FLOOR(' . $click_field . ') = ' . $object->GetDBField($click_field);
$hits = ($res = $this->Conn->GetOne($sql)) ? $res + 0.000001 : $object->GetDBField($click_field);
$object->SetDBField($click_field, $hits);
}
}
// change category
$target_category = $object->GetDBField('CategoryId');
if ( $object->GetOriginalField('CategoryId') != $target_category ) {
$object->MoveToCat($target_category);
}
}
/**
* Occurs after loading item, 'id' parameter
* allows to get id of item that was loaded
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterItemLoad(kEvent $event)
{
parent::OnAfterItemLoad($event);
$special = substr($event->Special, -6);
$object = $event->getObject();
/* @var $object kCatDBItem */
if ( $special == 'import' || $special == 'export' ) {
$image_data = $object->getPrimaryImageData();
if ( $image_data ) {
$thumbnail_image = $image_data[$image_data['LocalThumb'] ? 'ThumbPath' : 'ThumbUrl'];
if ( $image_data['SameImages'] ) {
$full_image = '';
}
else {
$full_image = $image_data[$image_data['LocalImage'] ? 'LocalPath' : 'Url'];
}
$object->SetDBField('ThumbnailImage', $thumbnail_image);
$object->SetDBField('FullImage', $full_image);
$object->SetDBField('ImageAlt', $image_data['AltName']);
}
}
// substituting pending status value for pending editing
if ( $object->HasField('OrgId') && $object->GetDBField('OrgId') > 0 && $object->GetDBField('Status') == -2 ) {
$new_options = Array ();
$options = $object->GetFieldOption('Status', 'options', false, Array ());
foreach ($options as $key => $val) {
if ( $key == 2 ) {
$key = -2;
}
$new_options[$key] = $val;
}
$object->SetFieldOption('Status', 'options', $new_options);
}
if ( !$this->Application->isAdmin ) {
// linking existing images for item with virtual fields
$image_helper = $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
$image_helper->LoadItemImages($object);
// linking existing files for item with virtual fields
$file_helper = $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
$file_helper->LoadItemFiles($object);
}
if ( $object->isVirtualField('MoreCategories') ) {
// set item's additional categories to virtual field (used in editing)
$item_categories = $this->getItemCategories($object->GetDBField('ResourceId'));
$object->SetDBField('MoreCategories', $item_categories ? '|' . implode('|', $item_categories) . '|' : '');
}
}
/**
* Occurs after updating item
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterItemUpdate(kEvent $event)
{
parent::OnAfterItemUpdate($event);
$this->CalculateHotLimit($event);
if ( substr($event->Special, -6) == 'import' ) {
$this->setCustomExportColumns($event);
}
$object = $event->getObject();
/* @var $object kCatDBItem */
if ( !$this->Application->isAdmin ) {
$image_helper = $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
// process image upload in virtual fields
$image_helper->SaveItemImages($object);
$file_helper = $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
// process file upload in virtual fields
$file_helper->SaveItemFiles($object);
if ( $event->Special != '-item' ) {
// don't touch categories during cloning
$this->processAdditionalCategories($object, 'update');
}
}
$recycle_bin = $this->Application->ConfigValue('RecycleBinFolder');
if ( $this->Application->isAdminUser && $recycle_bin ) {
$sql = 'SELECT CategoryId
FROM ' . $this->Application->getUnitConfig('ci')->getTableName() . '
WHERE ItemResourceId = ' . $object->GetDBField('ResourceId') . ' AND PrimaryCat = 1';
$primary_category = $this->Conn->GetOne($sql);
if ( $primary_category == $recycle_bin ) {
$event->CallSubEvent('OnAfterItemDelete');
}
}
if ( $object->GetChangedFields() ) {
$now = time();
$object->SetDBField('Modified_date', $now);
$object->SetDBField('Modified_time', $now);
$object->SetDBField('ModifiedById', $this->Application->RecallVar('user_id'));
}
}
/**
* Sets values for import process
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterItemCreate(kEvent $event)
{
parent::OnAfterItemCreate($event);
$object = $event->getObject();
/* @var $object kCatDBItem */
if ( substr($event->Special, -6) == 'import' ) {
$this->setCustomExportColumns($event);
}
$object->assignPrimaryCategory();
if ( !$this->Application->isAdmin ) {
$image_helper = $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
// process image upload in virtual fields
$image_helper->SaveItemImages($object);
$file_helper = $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
// process file upload in virtual fields
$file_helper->SaveItemFiles($object);
if ( $event->Special != '-item' ) {
// don't touch categories during cloning
$this->processAdditionalCategories($object, 'create');
}
}
}
/**
* Make record to search log
*
* @param string $keywords
* @param int $search_type 0 - simple search, 1 - advanced search
*/
function saveToSearchLog($keywords, $search_type = 0)
{
// don't save keywords for each module separately, just one time
// static variable can't help here, because each module uses it's own class instance !
if (!$this->Application->GetVar('search_logged')) {
$sql = 'UPDATE '.TABLE_PREFIX.'SearchLogs
SET Indices = Indices + 1
WHERE Keyword = '.$this->Conn->qstr($keywords).' AND SearchType = '.$search_type; // 0 - simple search, 1 - advanced search
$this->Conn->Query($sql);
if ($this->Conn->getAffectedRows() == 0) {
$fields_hash = Array('Keyword' => $keywords, 'Indices' => 1, 'SearchType' => $search_type);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX.'SearchLogs');
}
$this->Application->SetVar('search_logged', 1);
}
}
/**
* Makes simple search for category items
* based on keywords string
*
* @param kEvent $event
*/
function OnSimpleSearch($event)
{
$event->redirect = false;
$search_table = TABLE_PREFIX.'ses_'.$this->Application->GetSID().'_'.TABLE_PREFIX.'Search';
$keywords = $this->Application->unescapeRequestVariable(trim($this->Application->GetVar('keywords')));
- $query_object = $this->Application->recallObject('HTTPQuery');
+ $query_object = $this->Application->recallObject('kHTTPQuery');
/* @var $query_object kHTTPQuery */
$sql = 'SHOW TABLES LIKE "'.$search_table.'"';
if(!isset($query_object->Get['keywords']) &&
!isset($query_object->Post['keywords']) &&
$this->Conn->Query($sql))
{
return; // used when navigating by pages or changing sorting in search results
}
if(!$keywords || strlen($keywords) < $this->Application->ConfigValue('Search_MinKeyword_Length'))
{
$this->Conn->Query('DROP TABLE IF EXISTS '.$search_table);
$this->Application->SetVar('keywords_too_short', 1);
return; // if no or too short keyword entered, doing nothing
}
$this->Application->StoreVar('keywords', $keywords);
$this->saveToSearchLog($keywords, 0); // 0 - simple search, 1 - advanced search
$event->setPseudoClass('_List');
$object = $event->getObject();
/* @var $object kDBList */
$config = $event->getUnitConfig();
$this->Application->SetVar($event->getPrefixSpecial().'_Page', 1);
$lang = $this->Application->GetVar('m_lang');
$items_table = $config->getTableName();
$module_name = $this->Application->findModule('Var', $event->Prefix, 'Name');
$sql = 'SELECT *
FROM ' . $this->Application->getUnitConfig('confs')->getTableName() . '
WHERE ModuleName = ' . $this->Conn->qstr($module_name) . ' AND SimpleSearch = 1';
$search_config = $this->Conn->Query($sql, 'FieldName');
$field_list = array_keys($search_config);
$join_clauses = Array();
// field processing
$weight_sum = 0;
$alias_counter = 0;
$custom_fields = $config->getCustomFields();
if ($custom_fields) {
$custom_table = $this->Application->getUnitConfig($event->Prefix . '-cdata')->getTableName();
$join_clauses[] = ' LEFT JOIN '.$custom_table.' custom_data ON '.$items_table.'.ResourceId = custom_data.ResourceId';
}
// what field in search config becomes what field in sql (key - new field, value - old field (from searchconfig table))
$search_config_map = Array();
foreach ($field_list as $key => $field) {
$local_table = TABLE_PREFIX.$search_config[$field]['TableName'];
$weight_sum += $search_config[$field]['Priority']; // counting weight sum; used when making relevance clause
// processing multilingual fields
if ( !$search_config[$field]['CustomFieldId'] && $object->GetFieldOption($field, 'formatter') == 'kMultiLanguage' ) {
$field_list[$key.'_primary'] = 'l'.$this->Application->GetDefaultLanguageId().'_'.$field;
$field_list[$key] = 'l'.$lang.'_'.$field;
if (!isset($search_config[$field]['ForeignField'])) {
$field_list[$key.'_primary'] = $local_table.'.'.$field_list[$key.'_primary'];
$search_config_map[ $field_list[$key.'_primary'] ] = $field;
}
}
// processing fields from other tables
$foreign_field = $search_config[$field]['ForeignField'];
if ( $foreign_field ) {
$exploded = explode(':', $foreign_field, 2);
if ($exploded[0] == 'CALC') {
// ignoring having type clauses in simple search
unset($field_list[$key]);
continue;
}
else {
$multi_lingual = false;
if ($exploded[0] == 'MULTI') {
$multi_lingual = true;
$foreign_field = $exploded[1];
}
$exploded = explode('.', $foreign_field); // format: table.field_name
$foreign_table = TABLE_PREFIX.$exploded[0];
$alias_counter++;
$alias = 't'.$alias_counter;
if ($multi_lingual) {
$field_list[$key] = $alias.'.'.'l'.$lang.'_'.$exploded[1];
$field_list[$key.'_primary'] = 'l'.$this->Application->GetDefaultLanguageId().'_'.$field;
$search_config_map[ $field_list[$key] ] = $field;
$search_config_map[ $field_list[$key.'_primary'] ] = $field;
}
else {
$field_list[$key] = $alias.'.'.$exploded[1];
$search_config_map[ $field_list[$key] ] = $field;
}
$join_clause = str_replace('{ForeignTable}', $alias, $search_config[$field]['JoinClause']);
$join_clause = str_replace('{LocalTable}', $items_table, $join_clause);
$join_clauses[] = ' LEFT JOIN '.$foreign_table.' '.$alias.'
ON '.$join_clause;
}
}
else {
// processing fields from local table
if ($search_config[$field]['CustomFieldId']) {
$local_table = 'custom_data';
// search by custom field value on current language
$custom_field_id = array_search($field_list[$key], $custom_fields);
$field_list[$key] = 'l'.$lang.'_cust_'.$custom_field_id;
// search by custom field value on primary language
$field_list[$key.'_primary'] = $local_table.'.l'.$this->Application->GetDefaultLanguageId().'_cust_'.$custom_field_id;
$search_config_map[ $field_list[$key.'_primary'] ] = $field;
}
$field_list[$key] = $local_table.'.'.$field_list[$key];
$search_config_map[ $field_list[$key] ] = $field;
}
}
// keyword string processing
$search_helper = $this->Application->recallObject('SearchHelper');
/* @var $search_helper kSearchHelper */
$where_clause = Array ();
foreach ($field_list as $field) {
if (preg_match('/^' . preg_quote($items_table, '/') . '\.(.*)/', $field, $regs)) {
// local real field
$filter_data = $search_helper->getSearchClause($object, $regs[1], $keywords, false);
if ($filter_data) {
$where_clause[] = $filter_data['value'];
}
}
elseif (preg_match('/^custom_data\.(.*)/', $field, $regs)) {
$custom_field_name = 'cust_' . $search_config_map[$field];
$filter_data = $search_helper->getSearchClause($object, $custom_field_name, $keywords, false);
if ($filter_data) {
$where_clause[] = str_replace('`' . $custom_field_name . '`', $field, $filter_data['value']);
}
}
else {
$where_clause[] = $search_helper->buildWhereClause($keywords, Array ($field));
}
}
$where_clause = '((' . implode(') OR (', $where_clause) . '))'; // 2 braces for next clauses, see below!
$search_scope = $this->Application->GetVar('search_scope');
if ($search_scope == 'category') {
$category_id = $this->Application->GetVar('m_cat_id');
$category_filter = $this->getCategoryLimitClause($category_id);
if ($category_filter !== false) {
$join_clauses[] = ' LEFT JOIN '.TABLE_PREFIX.'CategoryItems ON '.TABLE_PREFIX.'CategoryItems.ItemResourceId = '.$items_table.'.ResourceId';
$join_clauses[] = ' LEFT JOIN '.TABLE_PREFIX.'Categories ON '.TABLE_PREFIX.'Categories.CategoryId = '.TABLE_PREFIX.'CategoryItems.CategoryId';
$where_clause = '('.$this->getCategoryLimitClause($category_id).') AND '.$where_clause;
}
}
$where_clause = $where_clause . ' AND (' . $items_table . '.Status = ' . STATUS_ACTIVE . ')';
if ($event->MasterEvent && $event->MasterEvent->Name == 'OnListBuild') {
$sub_search_ids = $event->MasterEvent->getEventParam('ResultIds');
if ( $sub_search_ids !== false ) {
if ( $sub_search_ids ) {
$where_clause .= 'AND (' . $items_table . '.ResourceId IN (' . implode(',', $sub_search_ids) . '))';
}
else {
$where_clause .= 'AND FALSE';
}
}
}
// making relevance clause
$positive_words = $search_helper->getPositiveKeywords($keywords);
$this->Application->StoreVar('highlight_keywords', serialize($positive_words));
$relevance_parts = Array();
reset($search_config);
foreach ($positive_words as $keyword_index => $positive_word) {
$positive_word = $search_helper->transformWildcards($positive_word);
$positive_words[$keyword_index] = $this->Conn->escape($positive_word);
}
foreach ($field_list as $field) {
if (!array_key_exists($field, $search_config_map)) {
$map_key = $search_config_map[$items_table . '.' . $field];
}
else {
$map_key = $search_config_map[$field];
}
$config_elem = $search_config[ $map_key ];
$weight = $config_elem['Priority'];
// search by whole words only ([[:<:]] - word boundary)
/*$relevance_parts[] = 'IF('.$field.' REGEXP "[[:<:]]('.implode(' ', $positive_words).')[[:>:]]", '.$weight.', 0)';
foreach ($positive_words as $keyword) {
$relevance_parts[] = 'IF('.$field.' REGEXP "[[:<:]]('.$keyword.')[[:>:]]", '.$weight.', 0)';
}*/
// search by partial word matches too
$relevance_parts[] = 'IF('.$field.' LIKE "%'.implode(' ', $positive_words).'%", '.$weight_sum.', 0)';
foreach ($positive_words as $keyword) {
$relevance_parts[] = 'IF('.$field.' LIKE "%'.$keyword.'%", '.$weight.', 0)';
}
}
$relevance_parts = array_unique($relevance_parts);
$conf_postfix = $config->getSearchConfigPostfix();
$rel_keywords = $this->Application->ConfigValue('SearchRel_Keyword_'.$conf_postfix) / 100;
$rel_pop = $this->Application->ConfigValue('SearchRel_Pop_'.$conf_postfix) / 100;
$rel_rating = $this->Application->ConfigValue('SearchRel_Rating_'.$conf_postfix) / 100;
$relevance_clause = '('.implode(' + ', $relevance_parts).') / '.$weight_sum.' * '.$rel_keywords;
if ($rel_pop && $object->isField('Hits')) {
$relevance_clause .= ' + (Hits + 1) / (MAX(Hits) + 1) * '.$rel_pop;
}
if ($rel_rating && $object->isField('CachedRating')) {
$relevance_clause .= ' + (CachedRating + 1) / (MAX(CachedRating) + 1) * '.$rel_rating;
}
// building final search query
if (!$this->Application->GetVar('do_not_drop_search_table')) {
$this->Conn->Query('DROP TABLE IF EXISTS '.$search_table); // erase old search table if clean k4 event
$this->Application->SetVar('do_not_drop_search_table', true);
}
$search_table_exists = $this->Conn->Query('SHOW TABLES LIKE "'.$search_table.'"');
if ($search_table_exists) {
$select_intro = 'INSERT INTO '.$search_table.' (Relevance, ItemId, ResourceId, ItemType, EdPick) ';
}
else {
$select_intro = 'CREATE TABLE '.$search_table.' AS ';
}
$edpick_clause = $config->getFieldByName('EditorsPick') ? $items_table.'.EditorsPick' : '0';
$sql = $select_intro.' SELECT '.$relevance_clause.' AS Relevance,
'.$items_table.'.'.$config->getIDField().' AS ItemId,
'.$items_table.'.ResourceId,
'.$config->getItemType().' AS ItemType,
'.$edpick_clause.' AS EdPick
FROM '.$object->TableName.'
'.implode(' ', $join_clauses).'
WHERE '.$where_clause.'
GROUP BY '.$items_table.'.'.$config->getIDField().' ORDER BY Relevance DESC';
$this->Conn->Query($sql);
if ( !$search_table_exists ) {
$sql = 'ALTER TABLE ' . $search_table . '
ADD INDEX (ResourceId),
ADD INDEX (Relevance)';
$this->Conn->Query($sql);
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnSubSearch($event)
{
// keep search results from other items after doing a sub-search on current item type
$this->Application->SetVar('do_not_drop_search_table', true);
$ids = Array ();
$search_table = TABLE_PREFIX . 'ses_' . $this->Application->GetSID() . '_' . TABLE_PREFIX . 'Search';
$sql = 'SHOW TABLES LIKE "' . $search_table . '"';
if ( $this->Conn->Query($sql) ) {
$item_type = $event->getUnitConfig()->getItemType();
// 1. get ids to be used as search bounds
$sql = 'SELECT DISTINCT ResourceId
FROM ' . $search_table . '
WHERE ItemType = ' . $item_type;
$ids = $this->Conn->GetCol($sql);
// 2. delete previously found ids
$sql = 'DELETE FROM ' . $search_table . '
WHERE ItemType = ' . $item_type;
$this->Conn->Query($sql);
}
$event->setEventParam('ResultIds', $ids);
$event->CallSubEvent('OnSimpleSearch');
}
/**
* Enter description here...
*
* @param kEvent $event
* @todo Change all hardcoded Products table & In-Commerce module usage to dynamic usage from item config !!!
*/
function OnAdvancedSearch($event)
{
- $query_object = $this->Application->recallObject('HTTPQuery');
+ $query_object = $this->Application->recallObject('kHTTPQuery');
/* @var $query_object kHTTPQuery */
if ( !isset($query_object->Post['andor']) ) {
// used when navigating by pages or changing sorting in search results
return;
}
$this->Application->RemoveVar('keywords');
$this->Application->RemoveVar('Search_Keywords');
$module_name = $this->Application->findModule('Var', $event->Prefix, 'Name');
$sql = 'SELECT *
FROM '.$this->Application->getUnitConfig('confs')->getTableName().'
WHERE (ModuleName = '.$this->Conn->qstr($module_name).') AND (AdvancedSearch = 1)';
$search_config = $this->Conn->Query($sql);
$lang = $this->Application->GetVar('m_lang');
$object = $event->getObject();
/* @var $object kDBList */
$object->SetPage(1);
$config = $event->getUnitConfig();
$items_table = $config->getTableName();
$search_keywords = $this->Application->GetVar('value'); // will not be changed
$keywords = $this->Application->GetVar('value'); // will be changed down there
$verbs = $this->Application->GetVar('verb');
$glues = $this->Application->GetVar('andor');
$and_conditions = Array();
$or_conditions = Array();
$and_having_conditions = Array();
$or_having_conditions = Array();
$join_clauses = Array();
$highlight_keywords = Array();
$relevance_parts = Array();
$alias_counter = 0;
$custom_fields = $config->getCustomFields();
if ($custom_fields) {
$custom_table = $this->Application->getUnitConfig($event->Prefix . '-cdata')->getTableName();
$join_clauses[] = ' LEFT JOIN '.$custom_table.' custom_data ON '.$items_table.'.ResourceId = custom_data.ResourceId';
}
$search_log = '';
$weight_sum = 0;
// processing fields and preparing conditions
foreach ($search_config as $record) {
$field = $record['FieldName'];
$join_clause = '';
$condition_mode = 'WHERE';
// field processing
$local_table = TABLE_PREFIX.$record['TableName'];
$weight_sum += $record['Priority']; // counting weight sum; used when making relevance clause
// processing multilingual fields
if ( $object->GetFieldOption($field, 'formatter') == 'kMultiLanguage' ) {
$field_name = 'l'.$lang.'_'.$field;
}
else {
$field_name = $field;
}
// processing fields from other tables
$foreign_field = $record['ForeignField'];
if ( $foreign_field ) {
$exploded = explode(':', $foreign_field, 2);
if($exploded[0] == 'CALC')
{
$user_groups = $this->Application->RecallVar('UserGroups');
$field_name = str_replace('{PREFIX}', TABLE_PREFIX, $exploded[1]);
$join_clause = str_replace('{PREFIX}', TABLE_PREFIX, $record['JoinClause']);
$join_clause = str_replace('{USER_GROUPS}', $user_groups, $join_clause);
$join_clause = ' LEFT JOIN '.$join_clause;
$condition_mode = 'HAVING';
}
else {
$exploded = explode('.', $foreign_field);
$foreign_table = TABLE_PREFIX.$exploded[0];
if($record['CustomFieldId']) {
$exploded[1] = 'l'.$lang.'_'.$exploded[1];
}
$alias_counter++;
$alias = 't'.$alias_counter;
$field_name = $alias.'.'.$exploded[1];
$join_clause = str_replace('{ForeignTable}', $alias, $record['JoinClause']);
$join_clause = str_replace('{LocalTable}', $items_table, $join_clause);
if($record['CustomFieldId'])
{
$join_clause .= ' AND '.$alias.'.CustomFieldId='.$record['CustomFieldId'];
}
$join_clause = ' LEFT JOIN '.$foreign_table.' '.$alias.'
ON '.$join_clause;
}
}
else
{
// processing fields from local table
if ($record['CustomFieldId']) {
$local_table = 'custom_data';
$field_name = 'l'.$lang.'_cust_'.array_search($field_name, $custom_fields);
}
$field_name = $local_table.'.'.$field_name;
}
$condition = $this->getAdvancedSearchCondition($field_name, $record, $keywords, $verbs, $highlight_keywords);
if ($record['CustomFieldId'] && strlen($condition)) {
// search in primary value of custom field + value in current language
$field_name = $local_table.'.'.'l'.$this->Application->GetDefaultLanguageId().'_cust_'.array_search($field, $custom_fields);
$primary_condition = $this->getAdvancedSearchCondition($field_name, $record, $keywords, $verbs, $highlight_keywords);
$condition = '('.$condition.' OR '.$primary_condition.')';
}
if ($condition) {
if ($join_clause) {
$join_clauses[] = $join_clause;
}
$relevance_parts[] = 'IF('.$condition.', '.$record['Priority'].', 0)';
if ($glues[$field] == 1) { // and
if ($condition_mode == 'WHERE') {
$and_conditions[] = $condition;
}
else {
$and_having_conditions[] = $condition;
}
}
else { // or
if ($condition_mode == 'WHERE') {
$or_conditions[] = $condition;
}
else {
$or_having_conditions[] = $condition;
}
}
// create search log record
$search_log_data = Array('search_config' => $record, 'verb' => getArrayValue($verbs, $field), 'value' => ($record['FieldType'] == 'range') ? $search_keywords[$field.'_from'].'|'.$search_keywords[$field.'_to'] : $search_keywords[$field]);
$search_log[] = $this->Application->Phrase('la_Field').' "'.$this->getHuman('Field', $search_log_data).'" '.$this->getHuman('Verb', $search_log_data).' '.$this->Application->Phrase('la_Value').' '.$this->getHuman('Value', $search_log_data).' '.$this->Application->Phrase($glues[$field] == 1 ? 'lu_And' : 'lu_Or');
}
}
if ($search_log) {
$search_log = implode('<br />', $search_log);
$search_log = preg_replace('/(.*) '.preg_quote($this->Application->Phrase('lu_and'), '/').'|'.preg_quote($this->Application->Phrase('lu_or'), '/').'$/is', '\\1', $search_log);
$this->saveToSearchLog($search_log, 1); // advanced search
}
$this->Application->StoreVar('highlight_keywords', serialize($highlight_keywords));
// making relevance clause
if($relevance_parts)
{
$conf_postfix = $config->getSearchConfigPostfix();
$rel_keywords = $this->Application->ConfigValue('SearchRel_Keyword_'.$conf_postfix) / 100;
$rel_pop = $this->Application->ConfigValue('SearchRel_Pop_'.$conf_postfix) / 100;
$rel_rating = $this->Application->ConfigValue('SearchRel_Rating_'.$conf_postfix) / 100;
$relevance_clause = '('.implode(' + ', $relevance_parts).') / '.$weight_sum.' * '.$rel_keywords;
$relevance_clause .= ' + (Hits + 1) / (MAX(Hits) + 1) * '.$rel_pop;
$relevance_clause .= ' + (CachedRating + 1) / (MAX(CachedRating) + 1) * '.$rel_rating;
}
else
{
$relevance_clause = '0';
}
// building having clause
if($or_having_conditions)
{
$and_having_conditions[] = '('.implode(' OR ', $or_having_conditions).')';
}
$having_clause = implode(' AND ', $and_having_conditions);
$having_clause = $having_clause ? ' HAVING '.$having_clause : '';
// building where clause
if($or_conditions)
{
$and_conditions[] = '('.implode(' OR ', $or_conditions).')';
}
// $and_conditions[] = $items_table.'.Status = 1';
$where_clause = implode(' AND ', $and_conditions);
if(!$where_clause)
{
if($having_clause)
{
$where_clause = '1';
}
else
{
$where_clause = '0';
$this->Application->SetVar('adv_search_error', 1);
}
}
$where_clause .= ' AND '.$items_table.'.Status = 1';
// building final search query
$search_table = TABLE_PREFIX.'ses_'.$this->Application->GetSID().'_'.TABLE_PREFIX.'Search';
$this->Conn->Query('DROP TABLE IF EXISTS '.$search_table);
$id_field = $config->getIDField();
$pick_field = $config->getFieldByName('EditorsPick') ? $items_table.'.EditorsPick' : '0';
$sql = ' CREATE TABLE '.$search_table.'
SELECT '.$relevance_clause.' AS Relevance,
'.$items_table.'.'.$id_field.' AS ItemId,
'.$items_table.'.ResourceId AS ResourceId,
11 AS ItemType,
'.$pick_field.' AS EdPick
FROM '.$items_table.'
'.implode(' ', $join_clauses).'
WHERE '.$where_clause.'
GROUP BY '.$items_table.'.'.$id_field.
$having_clause;
$res = $this->Conn->Query($sql);
}
function getAdvancedSearchCondition($field_name, $record, $keywords, $verbs, &$highlight_keywords)
{
$field = $record['FieldName'];
$condition_patterns = Array (
'any' => '%s LIKE %s',
'contains' => '%s LIKE %s',
'notcontains' => '(NOT (%1$s LIKE %2$s) OR %1$s IS NULL)',
'is' => '%s = %s',
'isnot' => '(%1$s != %2$s OR %1$s IS NULL)'
);
$condition = '';
switch ($record['FieldType']) {
case 'select':
$keywords[$field] = $this->Application->unescapeRequestVariable($keywords[$field]);
if ($keywords[$field]) {
$condition = sprintf($condition_patterns['is'], $field_name, $this->Conn->qstr( $keywords[$field] ));
}
break;
case 'multiselect':
$keywords[$field] = $this->Application->unescapeRequestVariable($keywords[$field]);
if ($keywords[$field]) {
$condition = Array ();
$values = explode('|', substr($keywords[$field], 1, -1));
foreach ($values as $selected_value) {
$condition[] = sprintf($condition_patterns['contains'], $field_name, $this->Conn->qstr('%|'.$selected_value.'|%'));
}
$condition = '('.implode(' OR ', $condition).')';
}
break;
case 'text':
$keywords[$field] = $this->Application->unescapeRequestVariable($keywords[$field]);
if (mb_strlen($keywords[$field]) >= $this->Application->ConfigValue('Search_MinKeyword_Length')) {
$highlight_keywords[] = $keywords[$field];
if (in_array($verbs[$field], Array('any', 'contains', 'notcontains'))) {
$keywords[$field] = '%'.strtr($keywords[$field], Array('%' => '\\%', '_' => '\\_')).'%';
}
$condition = sprintf($condition_patterns[$verbs[$field]], $field_name, $this->Conn->qstr( $keywords[$field] ));
}
break;
case 'boolean':
if ($keywords[$field] != -1) {
$config = $this->getUnitConfig();
$items_table = $config->getTableName();
$property_mappings = $config->getItemPropertyMappings();
switch ($field) {
case 'HotItem':
$hot_limit_var = getArrayValue($property_mappings, 'HotLimit');
if ($hot_limit_var) {
$hot_limit = (int)$this->Application->getDBCache($hot_limit_var);
$condition = 'IF('.$items_table.'.HotItem = 2,
IF('.$items_table.'.Hits >= '.
$hot_limit.
', 1, 0), '.$items_table.'.HotItem) = '.$keywords[$field];
}
break;
case 'PopItem':
$votes2pop_var = getArrayValue($property_mappings, 'VotesToPop');
$rating2pop_var = getArrayValue($property_mappings, 'RatingToPop');
if ($votes2pop_var && $rating2pop_var) {
$condition = 'IF('.$items_table.'.PopItem = 2, IF('.$items_table.'.CachedVotesQty >= '.
$this->Application->ConfigValue($votes2pop_var).
' AND '.$items_table.'.CachedRating >= '.
$this->Application->ConfigValue($rating2pop_var).
', 1, 0), '.$items_table.'.PopItem) = '.$keywords[$field];
}
break;
case 'NewItem':
$new_days_var = getArrayValue($property_mappings, 'NewDays');
if ($new_days_var) {
$condition = 'IF('.$items_table.'.NewItem = 2,
IF('.$items_table.'.CreatedOn >= (UNIX_TIMESTAMP() - '.
$this->Application->ConfigValue($new_days_var).
'*3600*24), 1, 0), '.$items_table.'.NewItem) = '.$keywords[$field];
}
break;
case 'EditorsPick':
$condition = $items_table.'.EditorsPick = '.$keywords[$field];
break;
}
}
break;
case 'range':
$range_conditions = Array();
if ($keywords[$field.'_from'] && !preg_match("/[^0-9]/i", $keywords[$field.'_from'])) {
$range_conditions[] = $field_name.' >= '.$keywords[$field.'_from'];
}
if ($keywords[$field.'_to'] && !preg_match("/[^0-9]/i", $keywords[$field.'_to'])) {
$range_conditions[] = $field_name.' <= '.$keywords[$field.'_to'];
}
if ($range_conditions) {
$condition = implode(' AND ', $range_conditions);
}
break;
case 'date':
if ($keywords[$field]) {
if (in_array($keywords[$field], Array('today', 'yesterday'))) {
$current_time = getdate();
$day_begin = mktime(0, 0, 0, $current_time['mon'], $current_time['mday'], $current_time['year']);
$time_mapping = Array('today' => $day_begin, 'yesterday' => ($day_begin - 86400));
$min_time = $time_mapping[$keywords[$field]];
}
else {
$time_mapping = Array (
'last_week' => 604800, 'last_month' => 2628000, 'last_3_months' => 7884000,
'last_6_months' => 15768000, 'last_year' => 31536000,
);
$min_time = time() - $time_mapping[$keywords[$field]];
}
$condition = $field_name.' > '.$min_time;
}
break;
}
return $condition;
}
/**
* Returns human readable representation of searched data to be placed in search log
* @param string $type
* @param Array $search_data
* @return string
* @access protected
*/
protected function getHuman($type, $search_data)
{
// all 3 variables are retrieved from $search_data array
/* @var $search_config Array */
/* @var $verb string */
/* @var $value string */
$type = ucfirst(strtolower($type));
extract($search_data);
switch ($type) {
case 'Field':
return $this->Application->Phrase($search_config['DisplayName']);
break;
case 'Verb':
return $verb ? $this->Application->Phrase('lu_advsearch_'.$verb) : '';
break;
case 'Value':
switch ($search_config['FieldType']) {
case 'date':
$values = Array(0 => 'lu_comm_Any', 'today' => 'lu_comm_Today',
'yesterday' => 'lu_comm_Yesterday', 'last_week' => 'lu_comm_LastWeek',
'last_month' => 'lu_comm_LastMonth', 'last_3_months' => 'lu_comm_Last3Months',
'last_6_months' => 'lu_comm_Last6Months', 'last_year' => 'lu_comm_LastYear');
$ret = $this->Application->Phrase($values[$value]);
break;
case 'range':
$value = explode('|', $value);
return $this->Application->Phrase('lu_comm_From').' "'.$value[0].'" '.$this->Application->Phrase('lu_comm_To').' "'.$value[1].'"';
break;
case 'boolean':
$values = Array(1 => 'lu_comm_Yes', 0 => 'lu_comm_No', -1 => 'lu_comm_Both');
$ret = $this->Application->Phrase($values[$value]);
break;
default:
$ret = $value;
break;
}
return '"'.$ret.'"';
break;
}
return '';
}
/**
* Set's correct page for list based on data provided with event
*
* @param kEvent $event
* @return void
* @access protected
* @see kDBEventHandler::OnListBuild()
*/
protected function SetPagination(kEvent $event)
{
$object = $event->getObject();
/* @var $object kDBList */
// get PerPage (forced -> session -> config -> 10)
$object->SetPerPage($this->getPerPage($event));
// main lists on Front-End have special get parameter for page
$page = $object->isMainList() ? $this->Application->GetVar('page') : false;
if ( !$page ) {
// page is given in "env" variable for given prefix
$page = $this->Application->GetVar($event->getPrefixSpecial() . '_Page');
}
if ( !$page && $event->Special ) {
// when not part of env, then variables like "prefix.special_Page" are
// replaced (by PHP) with "prefix_special_Page", so check for that too
$page = $this->Application->GetVar($event->getPrefixSpecial(true) . '_Page');
}
if ( !$object->isMainList() ) {
// main lists doesn't use session for page storing
$this->Application->StoreVarDefault($event->getPrefixSpecial() . '_Page', 1, true); // true for optional
if ( !$page ) {
if ( $this->Application->RewriteURLs() ) {
// when page not found by prefix+special, then try to search it without special at all
$page = $this->Application->GetVar($event->Prefix . '_Page');
if ( !$page ) {
// page not found in request -> get from session
$page = $this->Application->RecallVar($event->Prefix . '_Page');
}
if ( $page ) {
// page found in request -> store in session
$this->Application->StoreVar($event->getPrefixSpecial() . '_Page', $page, true); //true for optional
}
}
else {
// page not found in request -> get from session
$page = $this->Application->RecallVar($event->getPrefixSpecial() . '_Page');
}
}
else {
// page found in request -> store in session
$this->Application->StoreVar($event->getPrefixSpecial() . '_Page', $page, true); //true for optional
}
if ( !$event->getEventParam('skip_counting') ) {
// when stored page is larger, then maximal list page number
// (such case is also processed in kDBList::Query method)
$pages = $object->GetTotalPages();
if ( $page > $pages ) {
$page = 1;
$this->Application->StoreVar($event->getPrefixSpecial() . '_Page', 1, true);
}
}
}
$object->SetPage($page);
}
/* === RELATED TO IMPORT/EXPORT: BEGIN === */
/**
* Shows export dialog
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnExport(kEvent $event)
{
$selected_ids = $this->StoreSelectedIDs($event);
if ( implode(',', $selected_ids) == '' ) {
// K4 fix when no ids found bad selected ids array is formed
$selected_ids = false;
}
$selected_cats_ids = $this->Application->GetVar('export_categories');
$this->Application->StoreVar($event->Prefix . '_export_ids', $selected_ids ? implode(',', $selected_ids) : '');
$this->Application->StoreVar($event->Prefix . '_export_cats_ids', $selected_cats_ids);
$export_helper = $this->Application->recallObject('CatItemExportHelper');
/* @var $export_helper kCatDBItemExportHelper */
$redirect_params = Array (
$this->Prefix . '.export_event' => 'OnNew',
'pass' => 'all,' . $this->Prefix . '.export'
);
$event->setRedirectParams($redirect_params);
}
/**
* Performs each export step & displays progress percent
*
* @param kEvent $event
*/
function OnExportProgress($event)
{
$export_object = $this->Application->recallObject('CatItemExportHelper');
/* @var $export_object kCatDBItemExportHelper */
$action_method = 'perform'.ucfirst($event->Special);
$field_values = $export_object->$action_method($event);
// finish code is done from JS now
if ($field_values['start_from'] == $field_values['total_records']) {
if ($event->Special == 'import') {
$this->Application->StoreVar('PermCache_UpdateRequired', 1);
$event->SetRedirectParam('m_cat_id', $this->Application->RecallVar('ImportCategory'));
$event->SetRedirectParam('anchor', 'tab-' . $event->Prefix);
$event->redirect = 'catalog/catalog';
}
elseif ($event->Special == 'export') {
$event->redirect = $export_object->getModuleName($event) . '/' . $event->Special . '_finish';
$event->SetRedirectParam('pass', 'all');
}
return ;
}
$export_options = $export_object->loadOptions($event);
echo $export_options['start_from'] * 100 / $export_options['total_records'];
$event->status = kEvent::erSTOP;
}
/**
* Returns specific to each item type columns only
*
* @param kEvent $event
* @return Array
* @access protected
*/
public function getCustomExportColumns(kEvent $event)
{
return Array (
'__VIRTUAL__ThumbnailImage' => 'ThumbnailImage',
'__VIRTUAL__FullImage' => 'FullImage',
'__VIRTUAL__ImageAlt' => 'ImageAlt'
);
}
/**
* Sets non standart virtual fields (e.g. to other tables)
*
* @param kEvent $event
*/
function setCustomExportColumns($event)
{
$this->restorePrimaryImage($event);
}
/**
* Create/Update primary image record in info found in imported data
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function restorePrimaryImage($event)
{
$object = $event->getObject();
/* @var $object kCatDBItem */
if ( !$object->GetDBField('ThumbnailImage') && !$object->GetDBField('FullImage') ) {
return ;
}
$image_data = $object->getPrimaryImageData();
$image = $this->Application->recallObject('img', NULL, Array ('skip_autoload' => true));
/* @var $image kDBItem */
if ( $image_data ) {
$image->Load($image_data['ImageId']);
}
else {
$image->Clear();
$image->SetDBField('Name', 'main');
$image->SetDBField('DefaultImg', 1);
$image->SetDBField('ResourceId', $object->GetDBField('ResourceId'));
}
if ( $object->GetDBField('ImageAlt') ) {
$image->SetDBField('AltName', $object->GetDBField('ImageAlt'));
}
if ( $object->GetDBField('ThumbnailImage') ) {
$thumbnail_field = $this->isURL($object->GetDBField('ThumbnailImage')) ? 'ThumbUrl' : 'ThumbPath';
$image->SetDBField($thumbnail_field, $object->GetDBField('ThumbnailImage'));
$image->SetDBField('LocalThumb', $thumbnail_field == 'ThumbPath' ? 1 : 0);
}
if ( !$object->GetDBField('FullImage') ) {
$image->SetDBField('SameImages', 1);
}
else {
$image->SetDBField('SameImages', 0);
$full_field = $this->isURL($object->GetDBField('FullImage')) ? 'Url' : 'LocalPath';
$image->SetDBField($full_field, $object->GetDBField('FullImage'));
$image->SetDBField('LocalImage', $full_field == 'LocalPath' ? 1 : 0);
}
if ( $image->isLoaded() ) {
$image->Update();
}
else {
$image->Create();
}
}
/**
* Detects if image url is specified in a given path (instead of path on disk)
*
* @param string $path
* @return bool
* @access protected
*/
protected function isURL($path)
{
return preg_match('#(http|https)://(.*)#', $path);
}
/**
* Prepares item for import/export operations
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnNew(kEvent $event)
{
parent::OnNew($event);
if ( $event->Special == 'import' || $event->Special == 'export' ) {
$export_helper = $this->Application->recallObject('CatItemExportHelper');
/* @var $export_helper kCatDBItemExportHelper */
$export_helper->setRequiredFields($event);
}
}
/**
* Process items selected in item_selector
*
* @param kEvent $event
*/
function OnProcessSelected($event)
{
$dst_field = $this->Application->RecallVar('dst_field');
$selected_ids = $this->Application->GetVar('selected_ids');
if ( $dst_field == 'ItemCategory' ) {
// Item Edit -> Categories Tab -> New Categories
$object = $event->getObject();
/* @var $object kCatDBItem */
$category_ids = explode(',', $selected_ids['c']);
foreach ($category_ids as $category_id) {
$object->assignToCategory($category_id);
}
}
if ($dst_field == 'ImportCategory') {
// Tools -> Import -> Item Import -> Select Import Category
$this->Application->StoreVar('ImportCategory', $selected_ids['c']);
$event->SetRedirectParam($event->getPrefixSpecial() . '_id', 0);
$event->SetRedirectParam($event->getPrefixSpecial() . '_event', 'OnExportBegin');
}
$event->SetRedirectParam('opener', 'u');
}
/**
* Saves Import/Export settings to session
*
* @param kEvent $event
*/
function OnSaveSettings($event)
{
$event->redirect = false;
$items_info = $this->Application->GetVar($event->getPrefixSpecial(true));
if ( $items_info ) {
list($id, $field_values) = each($items_info);
$object = $event->getObject(Array ('skip_autoload' => true));
/* @var $object kDBItem */
$object->setID($id);
$object->SetFieldsFromHash($field_values);
$event->setEventParam('form_data', $field_values);
$field_values['ImportFilename'] = $object->GetDBField('ImportFilename'); //if upload formatter has renamed the file during moving !!!
$field_values['ImportSource'] = 2;
$field_values['ImportLocalFilename'] = $object->GetDBField('ImportFilename');
$items_info[$id] = $field_values;
$this->Application->StoreVar($event->getPrefixSpecial() . '_ItemsInfo', serialize($items_info));
}
}
/**
* Saves Import/Export settings to session
*
* @param kEvent $event
*/
function OnResetSettings($event)
{
$this->Application->StoreVar('ImportCategory', $this->Application->getBaseCategory());
}
/**
* Cancels item editing
* @param kEvent $event
* @return void
* @todo Used?
*/
function OnCancelAction($event)
{
$event->redirect = $this->Application->GetVar('cancel_template');
$event->SetRedirectParam('pass', 'all,' . $event->getPrefixSpecial());
}
/* === RELATED TO IMPORT/EXPORT: END === */
/**
* Stores item's owner login into separate field together with id
*
* @param kEvent $event
* @param string $id_field
* @param string $cached_field
*/
function cacheItemOwner($event, $id_field, $cached_field)
{
$object = $event->getObject();
/* @var $object kDBItem */
$object->SetDBField($cached_field, $object->GetField($id_field));
$options = $object->GetFieldOptions($id_field);
if ( isset($options['options'][$user_id]) ) {
$object->SetDBField($cached_field, $options['options'][$user_id]);
}
else {
$user_config = $this->Application->getUnitConfig('u');
$id_field = $user_config->getIDField();
$table_name = $user_config->getTableName();
$sql = 'SELECT Username
FROM ' . $table_name . '
WHERE ' . $id_field . ' = ' . $user_id;
$object->SetDBField($cached_field, $this->Conn->GetOne($sql));
}
}
/**
* Saves edited item into temp table
* If there is no id, new item is created in temp table
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnPreSave(kEvent $event)
{
parent::OnPreSave($event);
$use_pending_editing = $event->getUnitConfig()->getUsePendingEditing();
if ( $event->status == kEvent::erSUCCESS && $use_pending_editing ) {
// decision: clone or not clone
$object = $event->getObject();
/* @var $object kCatDBItem */
if ( $object->GetID() == 0 || $object->GetDBField('OrgId') > 0 ) {
// new items or cloned items shouldn't be cloned again
return ;
}
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$owner_field = $this->getOwnerField($event->Prefix);
if ( $perm_helper->ModifyCheckPermission($object->GetDBField($owner_field), $object->GetDBField('CategoryId'), $event->Prefix) == 2 ) {
// 1. clone original item
$temp_handler = $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler', Array ('parent_event' => $event));
/* @var $temp_handler kTempTablesHandler */
$cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array ($object->GetID()), NULL, NULL, NULL, true);
$ci_table = $this->Application->GetTempName(TABLE_PREFIX . 'CategoryItems');
// 2. delete record from CategoryItems (about cloned item) that was automatically created during call of Create method of kCatDBItem
$sql = 'SELECT ResourceId
FROM ' . $object->TableName . '
WHERE ' . $object->IDField . ' = ' . $cloned_ids[0];
$clone_resource_id = $this->Conn->GetOne($sql);
$sql = 'DELETE FROM ' . $ci_table . '
WHERE ItemResourceId = ' . $clone_resource_id . ' AND PrimaryCat = 1';
$this->Conn->Query($sql);
// 3. copy main item categoryitems to cloned item
$sql = ' INSERT INTO ' . $ci_table . ' (CategoryId, ItemResourceId, PrimaryCat, ItemPrefix, Filename)
SELECT CategoryId, ' . $clone_resource_id . ' AS ItemResourceId, PrimaryCat, ItemPrefix, Filename
FROM ' . $ci_table . '
WHERE ItemResourceId = ' . $object->GetDBField('ResourceId');
$this->Conn->Query($sql);
// 4. put cloned id to OrgId field of item being cloned
$sql = 'UPDATE ' . $object->TableName . '
SET OrgId = ' . $object->GetID() . '
WHERE ' . $object->IDField . ' = ' . $cloned_ids[0];
$this->Conn->Query($sql);
// 5. substitute id of item being cloned with clone id
$this->Application->SetVar($event->getPrefixSpecial() . '_id', $cloned_ids[0]);
$selected_ids = $this->getSelectedIDs($event, true);
$selected_ids[ array_search($object->GetID(), $selected_ids) ] = $cloned_ids[0];
$this->StoreSelectedIDs($event, $selected_ids);
// 6. delete original item from temp table
$temp_handler->DeleteItems($event->Prefix, $event->Special, Array ($object->GetID()));
}
}
}
/**
* Sets item's owner field
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnPreCreate(kEvent $event)
{
parent::OnPreCreate($event);
if ( $event->status != kEvent::erSUCCESS ) {
return ;
}
$object = $event->getObject();
/* @var $object kDBItem */
$owner_field = $this->getOwnerField($event->Prefix);
$object->SetDBField($owner_field, $this->Application->RecallVar('user_id'));
}
/**
* Occurs before original item of item in pending editing got deleted (for hooking only)
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeDeleteOriginal(kEvent $event)
{
}
/**
* Occurs after original item of item in pending editing got deleted (for hooking only)
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterDeleteOriginal(kEvent $event)
{
}
/**
* Occurs before an item has been cloned
* Id of newly created item is passed as event' 'id' param
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeClone(kEvent $event)
{
parent::OnBeforeClone($event);
$object = $event->getObject();
/* @var $object kDBItem */
$object->SetDBField('ResourceId', 0); // this will reset it
if ( $this->Application->GetVar('ResetCatBeforeClone') ) {
$object->SetDBField('CategoryId', NULL);
}
}
/**
* Set status for new category item based on user permission in category
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemCreate(kEvent $event)
{
parent::OnBeforeItemCreate($event);
$object = $event->getObject();
/* @var $object kCatDBItem */
$is_admin = $this->Application->isAdminUser;
$owner_field = $this->getOwnerField($event->Prefix);
if ( (!$object->IsTempTable() && !$is_admin) || ($is_admin && !$object->GetDBField($owner_field)) ) {
// Front-end OR owner not specified -> set to currently logged-in user
$object->SetDBField($owner_field, $this->Application->RecallVar('user_id'));
}
if ( !$this->Application->isAdmin ) {
$this->setItemStatusByPermission($event);
}
}
/**
* Sets category item status based on user permissions (only on Front-end)
*
* @param kEvent $event
*/
function setItemStatusByPermission($event)
{
$use_pending_editing = $event->getUnitConfig()->getUsePendingEditing();
if (!$use_pending_editing) {
return ;
}
$object = $event->getObject();
/* @var $object kCatDBItem */
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$primary_category = $object->GetDBField('CategoryId') > 0 ? $object->GetDBField('CategoryId') : $this->Application->GetVar('m_cat_id');
$item_status = $perm_helper->AddCheckPermission($primary_category, $event->Prefix);
if ($item_status == STATUS_DISABLED) {
$event->status = kEvent::erFAIL;
}
else {
$object->SetDBField('Status', $item_status);
}
}
/**
* Creates category item & redirects to confirmation template (front-end only)
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnCreate(kEvent $event)
{
parent::OnCreate($event);
$this->SetFrontRedirectTemplate($event, 'suggest');
}
/**
* Returns item's categories (allows to exclude primary category)
*
* @param int $resource_id
* @param bool $with_primary
* @return Array
*/
function getItemCategories($resource_id, $with_primary = false)
{
$sql = 'SELECT CategoryId
FROM '.TABLE_PREFIX.'CategoryItems
WHERE (ItemResourceId = '.$resource_id.')';
if (!$with_primary) {
$sql .= ' AND (PrimaryCat = 0)';
}
return $this->Conn->GetCol($sql);
}
/**
* Adds new and removes old additional categories from category item
*
* @param kCatDBItem $object
* @param int $mode
*/
function processAdditionalCategories(&$object, $mode)
{
if ( !$object->isVirtualField('MoreCategories') ) {
// given category item doesn't require such type of processing
return ;
}
$process_categories = $object->GetDBField('MoreCategories');
if ($process_categories === '') {
// field was not in submit & have default value (when no categories submitted, then value is null)
return ;
}
if ($mode == 'create') {
// prevents first additional category to become primary
$object->assignPrimaryCategory();
}
$process_categories = $process_categories ? explode('|', substr($process_categories, 1, -1)) : Array ();
$existing_categories = $this->getItemCategories($object->GetDBField('ResourceId'));
$add_categories = array_diff($process_categories, $existing_categories);
foreach ($add_categories as $category_id) {
$object->assignToCategory($category_id);
}
$remove_categories = array_diff($existing_categories, $process_categories);
foreach ($remove_categories as $category_id) {
$object->removeFromCategory($category_id);
}
}
/**
* Creates category item & redirects to confirmation template (front-end only)
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnUpdate(kEvent $event)
{
$use_pending = $event->getUnitConfig()->getUsePendingEditing();
if ($this->Application->isAdminUser || !$use_pending) {
parent::OnUpdate($event);
$this->SetFrontRedirectTemplate($event, 'modify');
return ;
}
$object = $event->getObject(Array('skip_autoload' => true));
/* @var $object kCatDBItem */
$items_info = $this->Application->GetVar($event->getPrefixSpecial(true));
if ($items_info) {
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$temp_handler = $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler', Array ('parent_event' => $event));
/* @var $temp_handler kTempTablesHandler */
$owner_field = $this->getOwnerField($event->Prefix);
$file_helper = $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
foreach ($items_info as $id => $field_values) {
$object->Load($id);
$edit_perm = $perm_helper->ModifyCheckPermission($object->GetDBField($owner_field), $object->GetDBField('CategoryId'), $event->Prefix);
if ($use_pending && !$object->GetDBField('OrgId') && ($edit_perm == STATUS_PENDING)) {
// pending editing enabled + not pending copy -> get/create pending copy & save changes to it
$original_id = $object->GetID();
$original_resource_id = $object->GetDBField('ResourceId');
$file_helper->PreserveItemFiles($field_values);
$object->Load($original_id, 'OrgId');
if (!$object->isLoaded()) {
// 1. user has no pending copy of live item -> clone live item
$cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array($original_id), NULL, NULL, NULL, true);
$object->Load($cloned_ids[0]);
$object->SetFieldsFromHash($field_values);
$event->setEventParam('form_data', $field_values);
// 1a. delete record from CategoryItems (about cloned item) that was automatically created during call of Create method of kCatDBItem
$ci_table = $this->Application->getUnitConfig('ci')->getTableName();
$sql = 'DELETE FROM '.$ci_table.'
WHERE ItemResourceId = '.$object->GetDBField('ResourceId').' AND PrimaryCat = 1';
$this->Conn->Query($sql);
// 1b. copy main item categoryitems to cloned item
$sql = 'INSERT INTO '.$ci_table.' (CategoryId, ItemResourceId, PrimaryCat, ItemPrefix, Filename)
SELECT CategoryId, '.$object->GetDBField('ResourceId').' AS ItemResourceId, PrimaryCat, ItemPrefix, Filename
FROM '.$ci_table.'
WHERE ItemResourceId = '.$original_resource_id;
$this->Conn->Query($sql);
// 1c. put cloned id to OrgId field of item being cloned
$object->SetDBField('Status', STATUS_PENDING_EDITING);
$object->SetDBField('OrgId', $original_id);
}
else {
// 2. user has pending copy of live item -> just update field values
$object->SetFieldsFromHash($field_values);
$event->setEventParam('form_data', $field_values);
}
// update id in request (used for redirect in mod-rewrite mode)
$this->Application->SetVar($event->getPrefixSpecial().'_id', $object->GetID());
}
else {
// 3. already editing pending copy -> just update field values
$object->SetFieldsFromHash($field_values);
$event->setEventParam('form_data', $field_values);
}
if ($object->Update()) {
$event->status = kEvent::erSUCCESS;
}
else {
$event->status = kEvent::erFAIL;
$event->redirect = false;
break;
}
}
}
$this->SetFrontRedirectTemplate($event, 'modify');
}
/**
* Sets next template to one required for front-end after adding/modifying item
*
* @param kEvent $event
* @param string $template_key - {suggest,modify}
*/
function SetFrontRedirectTemplate($event, $template_key)
{
if ( $this->Application->isAdmin || $event->status != kEvent::erSUCCESS ) {
return;
}
// prepare redirect template
$object = $event->getObject();
/* @var $object kDBItem */
$is_active = ($object->GetDBField('Status') == STATUS_ACTIVE);
$next_template = $is_active ? 'confirm_template' : 'pending_confirm_template';
$event->redirect = $this->Application->GetVar($template_key . '_' . $next_template);
$event->SetRedirectParam('opener', 's');
// send email events
$perm_prefix = $event->getUnitConfig()->getPermItemPrefix();
$owner_field = $this->getOwnerField($event->Prefix);
$owner_id = $object->GetDBField($owner_field);
$send_params = $object->getEmailParams();
switch ( $event->Name ) {
case 'OnCreate':
$event_suffix = $is_active ? 'ADD' : 'ADD.PENDING';
$this->Application->emailUser($perm_prefix . '.' . $event_suffix, $owner_id, $send_params);
$this->Application->emailAdmin($perm_prefix . '.' . $event_suffix, null, $send_params); // there are no ADD.PENDING event for admin :(
break;
case 'OnUpdate':
$event_suffix = $is_active ? 'MODIFY' : 'MODIFY.PENDING';
$user_id = is_numeric($object->GetDBField('ModifiedById')) ? $object->GetDBField('ModifiedById') : $owner_id;
$this->Application->emailUser($perm_prefix . '.' . $event_suffix, $user_id, $send_params);
$this->Application->emailAdmin($perm_prefix . '.' . $event_suffix, null, $send_params); // there are no ADD.PENDING event for admin :(
break;
}
}
/**
* Apply same processing to each item being selected in grid
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function iterateItems(kEvent $event)
{
if ( $event->Name != 'OnMassApprove' && $event->Name != 'OnMassDecline' ) {
parent::iterateItems($event);
}
if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) {
$event->status = kEvent::erFAIL;
return ;
}
$object = $event->getObject(Array ('skip_autoload' => true));
/* @var $object kCatDBItem */
$ids = $this->StoreSelectedIDs($event);
if ( $ids ) {
foreach ($ids as $id) {
$ret = true;
$object->Load($id);
switch ( $event->Name ) {
case 'OnMassApprove':
$ret = $object->ApproveChanges();
break;
case 'OnMassDecline':
$ret = $object->DeclineChanges();
break;
}
if ( !$ret ) {
$event->status = kEvent::erFAIL;
$event->redirect = false;
break;
}
}
}
$this->clearSelectedIDs($event);
}
/**
* Deletes items & preserves clean env
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnDelete(kEvent $event)
{
parent::OnDelete($event);
if ( $event->status == kEvent::erSUCCESS && !$this->Application->isAdmin ) {
$event->SetRedirectParam('pass', 'm');
$event->SetRedirectParam('m_cat_id', 0);
}
}
/**
* Checks, that currently loaded item is allowed for viewing (non permission-based)
*
* @param kEvent $event
* @return bool
* @access protected
*/
protected function checkItemStatus(kEvent $event)
{
$object = $event->getObject();
/* @var $object kDBItem */
if ( !$object->isLoaded() ) {
if ( $event->Special != 'previous' && $event->Special != 'next' ) {
$this->_errorNotFound($event);
}
return true;
}
$status = $object->GetDBField('Status');
$user_id = $this->Application->RecallVar('user_id');
$owner_field = $this->getOwnerField($event->Prefix);
if ( ($status == STATUS_PENDING_EDITING || $status == STATUS_PENDING) && ($object->GetDBField($owner_field) == $user_id) ) {
return true;
}
return $status == STATUS_ACTIVE;
}
/**
* Set's correct sorting for list based on data provided with event
*
* @param kEvent $event
* @return void
* @access protected
* @see kDBEventHandler::OnListBuild()
*/
protected function SetSorting(kEvent $event)
{
if ( !$this->Application->isAdmin ) {
$event->setEventParam('same_special', true);
}
$types = $event->getEventParam('types');
$types = $types ? explode(',', $types) : Array ();
if ( in_array('search', $types) ) {
$event->setPseudoClass('_List');
$object = $event->getObject();
/* @var $object kDBList */
// 1. no user sorting - sort by relevance
$default_sortings = parent::_getDefaultSorting($event);
$default_sorting = key($default_sortings['Sorting']) . ',' . current($default_sortings['Sorting']);
if ( $object->isMainList() ) {
$sort_by = $this->Application->GetVar('sort_by', '');
if ( !$sort_by ) {
$this->Application->SetVar('sort_by', 'Relevance,desc|' . $default_sorting);
}
}
else {
$sorting_settings = $this->getListSetting($event, 'Sortings');
$sort_by = trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ',');
if ( !$sort_by ) {
$event->setEventParam('sort_by', 'Relevance,desc|' . $default_sorting);
}
}
$this->_removeForcedSortings($event);
}
parent::SetSorting($event);
}
/**
* Removes forced sortings
*
* @param kEvent $event
*/
protected function _removeForcedSortings(kEvent $event)
{
$config = $event->getUnitConfig();
foreach ($config->getListSortingSpecials() as $special) {
$list_sortings = $config->getListSortingsBySpecial($special);
unset($list_sortings['ForcedSorting']);
$config->setListSortingsBySpecial('', $list_sortings);
}
}
/**
* Default sorting in search results only comes from relevance field
*
* @param kEvent $event
* @return Array
* @access protected
*/
protected function _getDefaultSorting(kEvent $event)
{
$types = $event->getEventParam('types');
$types = $types ? explode(',', $types) : Array ();
return in_array('search', $types) ? Array () : parent::_getDefaultSorting($event);
}
/**
* Returns current per-page setting for list
*
* @param kEvent $event
* @return int
* @access protected
*/
protected function getPerPage(kEvent $event)
{
if ( !$this->Application->isAdmin ) {
$event->setEventParam('same_special', true);
}
return parent::getPerPage($event);
}
/**
* Returns owner field for given prefix
*
* @param $prefix
* @return string
* @access protected
*/
protected function getOwnerField($prefix)
{
return $this->Application->getUnitConfig($prefix)->getOwnerField('CreatedById');
}
/**
* Creates virtual image fields for item
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterConfigRead(kEvent $event)
{
parent::OnAfterConfigRead($event);
if (defined('IS_INSTALL') && IS_INSTALL) {
$this->addViewPermissionJoin($event);
return ;
}
if ( !$this->Application->isAdmin ) {
$file_helper = $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
$file_helper->createItemFiles($event->Prefix, true); // create image fields
$file_helper->createItemFiles($event->Prefix, false); // create file fields
}
$this->changeSortings($event)->addViewPermissionJoin($event);
// add grids for advanced view (with primary category column)
$config = $event->getUnitConfig();
foreach (Array ('Default', 'Radio') as $process_grid) {
$grid_data = $config->getGridByName($process_grid);
$grid_data['Fields']['CachedNavbar'] = Array ('title' => 'la_col_Path', 'data_block' => 'grid_primary_category_td', 'filter_block' => 'grid_like_filter');
$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);
}
/**
* Changes default sorting according to system settings.
*
* @param kEvent $event Event.
*
* @return self
* @access protected
*/
protected function changeSortings(kEvent $event)
{
$remove_sortings = Array ();
$config = $event->getUnitConfig();
if ( !$this->Application->isAdmin ) {
// remove Pick sorting on Front-end, when not required
$config_mapping = $config->getConfigMapping(Array ());
if ( !isset($config_mapping['ForceEditorPick']) || !$this->Application->ConfigValue($config_mapping['ForceEditorPick']) ) {
$remove_sortings[] = 'EditorsPick';
}
}
else {
// remove all forced sortings in Admin Console
$remove_sortings = array_merge($remove_sortings, Array ('Priority', 'EditorsPick'));
}
if ( !$remove_sortings ) {
return $this;
}
foreach ($config->getListSortingSpecials() as $special) {
$list_sortings = $config->getListSortingsBySpecial($special);
foreach ($remove_sortings as $sorting_field) {
unset($list_sortings['ForcedSorting'][$sorting_field]);
}
$config->setListSortingsBySpecial('', $list_sortings);
}
return $this;
}
/**
* Adds permission table table JOIN clause only, when advanced catalog view permissions enabled.
*
* @param kEvent $event Event.
*
* @return self
* @access protected
*/
protected function addViewPermissionJoin(kEvent $event)
{
if ( $this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) {
$join_clause = 'LEFT JOIN ' . TABLE_PREFIX . 'CategoryPermissionsCache perm ON perm.CategoryId = ' . TABLE_PREFIX . '%3$sCategoryItems.CategoryId';
}
else {
$join_clause = '';
}
$config = $event->getUnitConfig();
foreach ( $config->getListSQLSpecials() as $special ) {
$list_sql = str_replace('{PERM_JOIN}', $join_clause, $config->getListSQLsBySpecial($special));
$config->setListSQLsBySpecial($special, $list_sql);
}
return $this;
}
/**
* Returns file contents associated with item
*
* @param kEvent $event
*/
function OnDownloadFile($event)
{
$object = $event->getObject();
/* @var $object kCatDBItem */
$event->status = kEvent::erSTOP;
$field = $this->Application->GetVar('field');
if (!preg_match('/^File([\d]+)/', $field)) {
return ;
}
$file_helper = $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
$filename = $object->GetField($field, 'full_path');
$file_helper->DownloadFile($filename);
}
/**
* Saves user's vote
*
* @param kEvent $event
*/
function OnMakeVote($event)
{
$event->status = kEvent::erSTOP;
if ($this->Application->GetVar('ajax') != 'yes') {
// this is supposed to call from AJAX only
return ;
}
$rating_helper = $this->Application->recallObject('RatingHelper');
/* @var $rating_helper RatingHelper */
$object = $event->getObject( Array ('skip_autoload' => true) );
/* @var $object kCatDBItem */
$object->Load( $this->Application->GetVar('id') );
echo $rating_helper->makeVote($object);
}
/**
* Marks review as useful
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnReviewHelpful($event)
{
if ( $this->Application->GetVar('ajax') == 'yes' ) {
$event->status = kEvent::erSTOP;
}
$review_id = (int)$this->Application->GetVar('review_id');
if ( !$review_id ) {
return;
}
$spam_helper = $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$spam_helper->InitHelper($review_id, 'ReviewHelpful', strtotime('+1 month') - strtotime('now'));
$field = (int)$this->Application->GetVar('helpful') ? 'HelpfulCount' : 'NotHelpfulCount';
$review_config = $this->Application->getUnitConfig('rev');
$sql = 'SELECT ' . $field . '
FROM ' . $review_config->getTableName() . '
WHERE ' . $review_config->getIDField() . ' = ' . $review_id;
$count = $this->Conn->GetOne($sql);
if ( $spam_helper->InSpamControl() ) {
if ( $this->Application->GetVar('ajax') == 'yes' ) {
echo $count;
}
return;
}
$sql = 'UPDATE ' . $review_config->getTableName() . '
SET ' . $field . ' = ' . $field . ' + 1
WHERE ' . $review_config->getIDField() . ' = ' . $review_id;
$this->Conn->Query($sql);
if ( $this->Conn->getAffectedRows() ) {
// db was changed -> review with such ID exists
$spam_helper->AddToSpamControl();
}
if ( $this->Application->GetVar('ajax') == 'yes' ) {
echo $count + 1;
}
}
/**
* [HOOK] Allows to add cloned subitem to given prefix
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnCloneSubItem(kEvent $event)
{
parent::OnCloneSubItem($event);
if ( $event->MasterEvent->Prefix == 'fav' ) {
$master_config = $event->MasterEvent->getUnitConfig();
$clones = $master_config->getClones();
$sub_item_prefix = $event->Prefix . '-' . $event->MasterEvent->Prefix;
$clones[$sub_item_prefix]['ParentTableKey'] = 'ResourceId';
$clones[$sub_item_prefix]['ForeignKey'] = 'ResourceId';
$master_config->setClones($clones);
}
}
/**
* Set's new unique resource id to user
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterItemValidate(kEvent $event)
{
$object = $event->getObject();
/* @var $object kDBItem */
$resource_id = $object->GetDBField('ResourceId');
if ( !$resource_id ) {
$object->SetDBField('ResourceId', $this->Application->NextResourceId());
}
}
}
Index: branches/5.3.x/core/kernel/application.php
===================================================================
--- branches/5.3.x/core/kernel/application.php (revision 16155)
+++ branches/5.3.x/core/kernel/application.php (revision 16156)
@@ -1,3082 +1,3052 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
/**
* Basic class for Kernel4-based Application
*
* This class is a Facade for any other class which needs to deal with Kernel4 framework.<br>
* The class encapsulates the main run-cycle of the script, provide access to all other objects in the framework.<br>
* <br>
* The class is a singleton, which means that there could be only one instance of kApplication in the script.<br>
* This could be guaranteed by NOT calling the class constructor directly, but rather calling kApplication::Instance() method,
* which returns an instance of the application. The method guarantees that it will return exactly the same instance for any call.<br>
* See singleton pattern by GOF.
*/
class kApplication implements kiCacheable {
/**
* Location of module helper class (used in installator too)
*/
const MODULE_HELPER_PATH = '/../units/helpers/modules_helper.php';
/**
* Is true, when Init method was called already, prevents double initialization
*
* @var bool
*/
public $InitDone = false;
/**
* Holds internal NParser object
*
* @var NParser
* @access public
*/
public $Parser;
/**
* Holds parser output buffer
*
* @var string
* @access protected
*/
protected $HTML = '';
/**
* The main Factory used to create
* almost any class of kernel and
* modules
*
* @var kFactory
* @access protected
*/
protected $Factory;
/**
* Template names, that will be used instead of regular templates
*
* @var Array
* @access public
*/
public $ReplacementTemplates = Array ();
/**
* Mod-Rewrite listeners used during url building and parsing
*
* @var Array
* @access public
*/
public $RewriteListeners = Array ();
/**
* Reference to debugger
*
* @var Debugger
* @access public
*/
public $Debugger = null;
/**
* Holds all phrases used
* in code and template
*
- * @var PhrasesCache
+ * @var kPhraseCache
* @access public
*/
public $Phrases;
/**
* Modules table content, key - module name
*
* @var Array
* @access public
*/
public $ModuleInfo = Array ();
/**
* Holds DBConnection
*
* @var IDBConnection
* @access public
*/
public $Conn = null;
/**
* Reference to event log
*
* @var Array|kLogger
* @access public
*/
protected $_logger = Array ();
// performance needs:
/**
* Holds a reference to httpquery
*
* @var kHttpQuery
* @access public
*/
public $HttpQuery = null;
/**
* Holds a reference to UnitConfigReader
*
* @var kUnitConfigReader
* @access public
*/
public $UnitConfigReader = null;
/**
* Holds a reference to Session
*
* @var Session
* @access public
*/
public $Session = null;
/**
* Holds a ref to kEventManager
*
* @var kEventManager
* @access public
*/
public $EventManager = null;
/**
* Holds a ref to kUrlManager
*
* @var kUrlManager
* @access public
*/
public $UrlManager = null;
/**
* Ref for TemplatesCache
*
* @var TemplatesCache
* @access public
*/
public $TemplatesCache = null;
/**
* Holds current NParser tag while parsing, can be used in error messages to display template file and line
*
* @var _BlockTag
* @access public
*/
public $CurrentNTag = null;
/**
* Object of unit caching class
*
* @var kCacheManager
* @access public
*/
public $cacheManager = null;
/**
* Tells, that administrator has authenticated in administrative console
* Should be used to manipulate data change OR data restrictions!
*
* @var bool
* @access public
*/
public $isAdminUser = false;
/**
* Tells, that admin version of "index.php" was used, nothing more!
* Should be used to manipulate data display!
*
* @var bool
* @access public
*/
public $isAdmin = false;
/**
* Instance of site domain object
*
* @var kDBItem
* @access public
* @todo move away into separate module
*/
public $siteDomain = null;
/**
* Prevent kApplication class to be created directly, only via Instance method
*
* @access private
*/
private function __construct()
{
}
final private function __clone() {}
/**
* Returns kApplication instance anywhere in the script.
*
* This method should be used to get single kApplication object instance anywhere in the
* Kernel-based application. The method is guaranteed to return the SAME instance of kApplication.
* Anywhere in the script you could write:
* <code>
* $application =& kApplication::Instance();
* </code>
* or in an object:
* <code>
* $this->Application =& kApplication::Instance();
* </code>
* to get the instance of kApplication. Note that we call the Instance method as STATIC - directly from the class.
* To use descendant of standard kApplication class in your project you would need to define APPLICATION_CLASS constant
* BEFORE calling kApplication::Instance() for the first time. If APPLICATION_CLASS is not defined the method would
* create and return default KernelApplication instance.
*
* Pattern: Singleton
*
* @static
* @return kApplication
* @access public
*/
public static function &Instance()
{
static $instance = false;
if ( !$instance ) {
$class = defined('APPLICATION_CLASS') ? APPLICATION_CLASS : 'kApplication';
$instance = new $class();
}
return $instance;
}
/**
* Initializes the Application
*
* @param string $factory_class
* @return bool Was Init actually made now or before
* @access public
* @see kHTTPQuery
* @see Session
* @see TemplatesCache
*/
public function Init($factory_class = 'kFactory')
{
if ( $this->InitDone ) {
return false;
}
if ( preg_match('/utf-8/i', CHARSET) ) {
setlocale(LC_ALL, 'en_US.UTF-8');
mb_internal_encoding('UTF-8');
}
$this->isAdmin = kUtil::constOn('ADMIN');
if ( !kUtil::constOn('SKIP_OUT_COMPRESSION') ) {
ob_start(); // collect any output from method (other then tags) into buffer
}
if ( defined('DEBUG_MODE') && $this->isDebugMode() && kUtil::constOn('DBG_PROFILE_MEMORY') ) {
$this->Debugger->appendMemoryUsage('Application before Init:');
}
$this->_logger = new kLogger($this->_logger);
$this->Factory = new $factory_class();
$this->registerDefaultClasses();
$system_config = new kSystemConfig(true);
$vars = $system_config->getData();
$db_class = isset($vars['Databases']) ? 'kDBLoadBalancer' : ($this->isDebugMode() ? 'kDBConnectionDebug' : 'kDBConnection');
$this->Conn = $this->Factory->makeClass($db_class, Array (SQL_TYPE, Array ($this->_logger, 'handleSQLError')));
$this->Conn->setup($vars);
$this->cacheManager = $this->makeClass('kCacheManager');
$this->cacheManager->InitCache();
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('Before UnitConfigReader');
}
// init config reader and all managers
$this->UnitConfigReader = $this->makeClass('kUnitConfigReader');
$this->UnitConfigReader->scanModules(MODULES_PATH); // will also set RewriteListeners when existing cache is read
$this->registerModuleConstants();
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('After UnitConfigReader');
}
define('MOD_REWRITE', $this->ConfigValue('UseModRewrite') && !$this->isAdmin ? 1 : 0);
// start processing request
- $this->HttpQuery = $this->recallObject('HTTPQuery');
+ $this->HttpQuery = $this->recallObject('kHTTPQuery');
$this->HttpQuery->process();
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('Processed HTTPQuery initial');
}
$this->Session = $this->recallObject('Session');
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('Processed Session');
}
$this->Session->ValidateExpired(); // needs mod_rewrite url already parsed to keep user at proper template after session expiration
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('Processed HTTPQuery AfterInit');
}
$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(); // will set RewriteListeners when missing cache is built first time
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('Processed AfterConfigRead');
}
if ( $this->GetVar('m_cat_id') === false ) {
$this->SetVar('m_cat_id', 0);
}
if ( !$this->RecallVar('curr_iso') ) {
$this->StoreVar('curr_iso', $this->GetPrimaryCurrency(), true); // true for optional
}
$visit_id = $this->RecallVar('visit_id');
if ( $visit_id !== false ) {
$this->SetVar('visits_id', $visit_id);
}
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->profileFinish('kernel4_startup');
}
$this->InitDone = true;
$this->HandleEvent(new kEvent('adm:OnStartup'));
return true;
}
/**
* Performs initialization of manager classes, that can be overridden from unit configs
*
* @return void
* @access public
* @throws Exception
*/
public function InitManagers()
{
if ( $this->InitDone ) {
throw new Exception('Duplicate call of ' . __METHOD__, E_USER_ERROR);
}
$this->UrlManager = $this->makeClass('kUrlManager');
- $this->EventManager = $this->makeClass('EventManager');
+ $this->EventManager = $this->makeClass('kEventManager');
$this->Phrases = $this->makeClass('kPhraseCache');
$this->RegisterDefaultBuildEvents();
}
/**
* Returns module information. Searches module by requested field
*
* @param string $field
* @param mixed $value
* @param string $return_field field value to returns, if not specified, then return all fields
* @return Array
*/
public function findModule($field, $value, $return_field = null)
{
$found = $module_info = false;
foreach ($this->ModuleInfo as $module_info) {
if ( strtolower($module_info[$field]) == strtolower($value) ) {
$found = true;
break;
}
}
if ( $found ) {
return isset($return_field) ? $module_info[$return_field] : $module_info;
}
return false;
}
/**
* Refreshes information about loaded modules
*
* @return void
* @access public
*/
public function refreshModuleInfo()
{
if ( defined('IS_INSTALL') && IS_INSTALL && !$this->TableFound('Modules', true) ) {
$this->registerModuleConstants();
+ $this->Factory->configureAutoloader();
+
return;
}
// use makeClass over recallObject, since used before kApplication initialization during installation
- $modules_helper = $this->makeClass('ModulesHelper');
+ $modules_helper = $this->makeClass('kModulesHelper');
/* @var $modules_helper kModulesHelper */
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'Modules
WHERE ' . $modules_helper->getWhereClause() . '
ORDER BY LoadOrder';
$this->ModuleInfo = $this->Conn->Query($sql, 'Name');
$this->registerModuleConstants();
+ $this->Factory->configureAutoloader();
}
/**
* Checks if passed language id if valid and sets it to primary otherwise
*
* @return void
* @access public
*/
public function VerifyLanguageId()
{
/** @var LanguagesItem $lang */
$lang = $this->recallObject('lang.current');
if ( !$lang->isLoaded() || (!$this->isAdmin && !$lang->GetDBField('Enabled')) ) {
if ( !defined('IS_INSTALL') ) {
$this->ApplicationDie('Unknown or disabled language');
}
}
}
/**
* Checks if passed theme id if valid and sets it to primary otherwise
*
* @return void
* @access public
*/
public function VerifyThemeId()
{
if ( $this->isAdmin ) {
kUtil::safeDefine('THEMES_PATH', '/core/admin_templates');
return;
}
$path = $this->GetFrontThemePath();
if ( $path === false ) {
$this->ApplicationDie('No Primary Theme Selected or Current Theme is Unknown or Disabled');
}
kUtil::safeDefine('THEMES_PATH', $path);
}
/**
* Returns relative path to current front-end theme
*
* @param bool $force
* @return string
* @access public
*/
public function GetFrontThemePath($force = false)
{
static $path = null;
if ( !$force && isset($path) ) {
return $path;
}
/** @var ThemeItem $theme */
$theme = $this->recallObject('theme.current');
if ( !$theme->isLoaded() || !$theme->GetDBField('Enabled') ) {
return false;
}
// assign & then return, since it's static variable
$path = '/themes/' . $theme->GetDBField('Name');
return $path;
}
/**
* Returns primary front/admin language id
*
* @param bool $init
* @return int
* @access public
*/
public function GetDefaultLanguageId($init = false)
{
$cache_key = 'primary_language_info[%LangSerial%]';
$language_info = $this->getCache($cache_key);
if ( $language_info === false ) {
// cache primary language info first
$language_config = $this->getUnitConfig('lang');
$table = $language_config->getTableName();
$id_field = $language_config->getIDField();
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT ' . $id_field . ', IF(AdminInterfaceLang, "Admin", "Front") AS LanguageKey
FROM ' . $table . '
WHERE (AdminInterfaceLang = 1 OR PrimaryLang = 1) AND (Enabled = 1)';
$language_info = $this->Conn->GetCol($sql, 'LanguageKey');
if ( $language_info !== false ) {
$this->setCache($cache_key, $language_info);
}
}
$language_key = ($this->isAdmin && $init) || count($language_info) == 1 ? 'Admin' : 'Front';
if ( array_key_exists($language_key, $language_info) && $language_info[$language_key] > 0 ) {
// get from cache
return $language_info[$language_key];
}
$language_id = $language_info && array_key_exists($language_key, $language_info) ? $language_info[$language_key] : false;
if ( !$language_id && defined('IS_INSTALL') && IS_INSTALL ) {
$language_id = 1;
}
return $language_id;
}
/**
* Returns front-end primary theme id (even, when called from admin console)
*
* @param bool $force_front
* @return int
* @access public
*/
public function GetDefaultThemeId($force_front = false)
{
static $theme_id = 0;
if ( $theme_id > 0 ) {
return $theme_id;
}
if ( kUtil::constOn('DBG_FORCE_THEME') ) {
$theme_id = DBG_FORCE_THEME;
}
elseif ( !$force_front && $this->isAdmin ) {
$theme_id = 999;
}
else {
$cache_key = 'primary_theme[%ThemeSerial%]';
$theme_id = $this->getCache($cache_key);
if ( $theme_id === false ) {
$this->Conn->nextQueryCachable = true;
$theme_config = $this->getUnitConfig('theme');
$sql = 'SELECT ' . $theme_config->getIDField() . '
FROM ' . $theme_config->getTableName() . '
WHERE (PrimaryTheme = 1) AND (Enabled = 1)';
$theme_id = $this->Conn->GetOne($sql);
if ( $theme_id !== false ) {
$this->setCache($cache_key, $theme_id);
}
}
}
return $theme_id;
}
/**
* Returns site primary currency ISO code
*
* @return string
* @access public
* @todo Move into In-Commerce
*/
public function GetPrimaryCurrency()
{
$cache_key = 'primary_currency[%CurrSerial%][%SiteDomainSerial%]:' . $this->siteDomainField('DomainId');
$currency_iso = $this->getCache($cache_key);
if ( $currency_iso === false ) {
if ( $this->prefixRegistred('curr') ) {
$this->Conn->nextQueryCachable = true;
$currency_id = $this->siteDomainField('PrimaryCurrencyId');
$sql = 'SELECT ISO
FROM ' . $this->getUnitConfig('curr')->getTableName() . '
WHERE ' . ($currency_id > 0 ? 'CurrencyId = ' . $currency_id : 'IsPrimary = 1');
$currency_iso = $this->Conn->GetOne($sql);
}
else {
$currency_iso = 'USD';
}
$this->setCache($cache_key, $currency_iso);
}
return $currency_iso;
}
/**
* Returns site domain field. When none of site domains are found false is returned.
*
* @param string $field
* @param bool $formatted
* @param string $format
* @return mixed
* @todo Move into separate module
*/
public function siteDomainField($field, $formatted = false, $format = null)
{
if ( $this->isAdmin ) {
// don't apply any filtering in administrative console
return false;
}
if ( !$this->siteDomain ) {
$this->siteDomain = $this->recallObject('site-domain.current', null, Array ('live_table' => true));
/* @var $site_domain kDBItem */
}
if ( $this->siteDomain->isLoaded() ) {
return $formatted ? $this->siteDomain->GetField($field, $format) : $this->siteDomain->GetDBField($field);
}
return false;
}
/**
- * Registers default classes such as kDBEventHandler, kUrlManager
+ * Registers classes, that are used before unit configs (where class registration usually is done) are read.
*
- * Called automatically while initializing kApplication
+ * Called automatically while initializing kApplication.
*
* @return void
* @access public
*/
public function RegisterDefaultClasses()
{
- $this->registerClass('kHelper', KERNEL_PATH . '/kbase.php');
- $this->registerClass('kMultipleFilter', KERNEL_PATH . '/utility/filters.php');
- $this->registerClass('kiCacheable', KERNEL_PATH . '/interfaces/cacheable.php');
+ // Database.
+ $this->registerClass('IDBConnection', KERNEL_PATH . '/db/i_db_connection.php');
+ $this->registerClass('kDBConnection', KERNEL_PATH . '/db/db_connection.php');
+ $this->registerClass('kDBConnectionDebug', KERNEL_PATH . '/db/db_connection.php');
+ $this->registerClass('kDBLoadBalancer', KERNEL_PATH . '/db/db_load_balancer.php');
- $this->registerClass('kEventManager', KERNEL_PATH . '/event_manager.php', 'EventManager');
- $this->registerClass('kHookManager', KERNEL_PATH . '/managers/hook_manager.php');
- $this->registerClass('kScheduledTaskManager', KERNEL_PATH . '/managers/scheduled_task_manager.php');
- $this->registerClass('kRequestManager', KERNEL_PATH . '/managers/request_manager.php');
- $this->registerClass('kSubscriptionManager', KERNEL_PATH . '/managers/subscription_manager.php');
+ // Cache.
+ $this->registerClass('kCacheManager', KERNEL_PATH . '/managers/cache_manager.php');
+ $this->registerClass('kCache', KERNEL_PATH . '/utility/cache.php');
+
+ // Unit configs.
+ $this->registerClass('kUnitConfigReader', KERNEL_PATH . '/utility/unit_config_reader.php');
+ $this->registerClass('kUnitConfigCloner', KERNEL_PATH . '/utility/unit_config_cloner.php');
+ // Urls.
$this->registerClass('kUrlManager', KERNEL_PATH . '/managers/url_manager.php');
$this->registerClass('kUrlProcessor', KERNEL_PATH . '/managers/url_processor.php');
$this->registerClass('kPlainUrlProcessor', KERNEL_PATH . '/managers/plain_url_processor.php');
- $this->registerClass('kRewriteUrlProcessor', KERNEL_PATH . '/managers/rewrite_url_processor.php');
+ // $this->registerClass('kRewriteUrlProcessor', KERNEL_PATH . '/managers/rewrite_url_processor.php');
- $this->registerClass('kCacheManager', KERNEL_PATH . '/managers/cache_manager.php');
- $this->registerClass('PhrasesCache', KERNEL_PATH . '/languages/phrases_cache.php', 'kPhraseCache');
- $this->registerClass('kTempTablesHandler', KERNEL_PATH . '/utility/temp_handler.php');
- $this->registerClass('kValidator', KERNEL_PATH . '/utility/validator.php');
- $this->registerClass('kOpenerStack', KERNEL_PATH . '/utility/opener_stack.php');
- $this->registerClass('kLogger', KERNEL_PATH . '/utility/logger.php');
+ // Events.
+ $this->registerClass('kEventManager', KERNEL_PATH . '/event_manager.php');
+ $this->registerClass('kHookManager', KERNEL_PATH . '/managers/hook_manager.php');
+ $this->registerClass('kScheduledTaskManager', KERNEL_PATH . '/managers/scheduled_task_manager.php');
+ $this->registerClass('kRequestManager', KERNEL_PATH . '/managers/request_manager.php');
- $this->registerClass('kUnitConfig', KERNEL_PATH . '/utility/unit_config.php');
- $this->registerClass('kUnitConfigReader', KERNEL_PATH . '/utility/unit_config_reader.php');
- $this->registerClass('kUnitConfigCloner', KERNEL_PATH . '/utility/unit_config_cloner.php');
- $this->registerClass('PasswordHash', KERNEL_PATH . '/utility/php_pass.php');
+ // Misc.
+ $this->registerClass('kPhraseCache', KERNEL_PATH . '/languages/phrases_cache.php');
+ $this->registerClass('kModulesHelper', KERNEL_PATH . self::MODULE_HELPER_PATH);
- // Params class descendants
- $this->registerClass('kArray', KERNEL_PATH . '/utility/params.php');
- $this->registerClass('Params', KERNEL_PATH . '/utility/params.php');
+ // Aliased.
$this->registerClass('Params', KERNEL_PATH . '/utility/params.php', 'kActions');
- $this->registerClass('kCache', KERNEL_PATH . '/utility/cache.php', 'kCache', 'Params');
- $this->registerClass('kHTTPQuery', KERNEL_PATH . '/utility/http_query.php', 'HTTPQuery');
-
- // session
- $this->registerClass('kCookieHasher', KERNEL_PATH . '/utility/cookie_hasher.php');
- $this->registerClass('Session', KERNEL_PATH . '/session/session.php');
- $this->registerClass('SessionStorage', KERNEL_PATH . '/session/session_storage.php');
- $this->registerClass('InpSession', KERNEL_PATH . '/session/inp_session.php', 'Session');
- $this->registerClass('InpSessionStorage', KERNEL_PATH . '/session/inp_session_storage.php', 'SessionStorage');
-
- // template parser
- $this->registerClass('kTagProcessor', KERNEL_PATH . '/processors/tag_processor.php');
$this->registerClass('kMainTagProcessor', KERNEL_PATH . '/processors/main_processor.php', 'm_TagProcessor');
- $this->registerClass('kDBTagProcessor', KERNEL_PATH . '/db/db_tag_processor.php');
- $this->registerClass('kCatDBTagProcessor', KERNEL_PATH . '/db/cat_tag_processor.php');
- $this->registerClass('NParser', KERNEL_PATH . '/nparser/nparser.php');
- $this->registerClass('TemplatesCache', KERNEL_PATH . '/nparser/template_cache.php');
-
- // database
- $this->registerClass('kDBConnection', KERNEL_PATH . '/db/db_connection.php');
- $this->registerClass('kDBConnectionDebug', KERNEL_PATH . '/db/db_connection.php');
- $this->registerClass('kDBLoadBalancer', KERNEL_PATH . '/db/db_load_balancer.php');
- $this->registerClass('kDBItem', KERNEL_PATH . '/db/dbitem.php');
- $this->registerClass('kCatDBItem', KERNEL_PATH . '/db/cat_dbitem.php');
- $this->registerClass('kDBList', KERNEL_PATH . '/db/dblist.php');
- $this->registerClass('kCatDBList', KERNEL_PATH . '/db/cat_dblist.php');
- $this->registerClass('kDBEventHandler', KERNEL_PATH . '/db/db_event_handler.php');
- $this->registerClass('kCatDBEventHandler', KERNEL_PATH . '/db/cat_event_handler.php');
-
- // email sending
- $this->registerClass('kEmail', KERNEL_PATH . '/utility/email.php');
$this->registerClass('kEmailSendingHelper', KERNEL_PATH . '/utility/email_send.php', 'EmailSender');
- $this->registerClass('kSocket', KERNEL_PATH . '/utility/socket.php', 'Socket');
-
- // do not move to config - this helper is used before configs are read
- $this->registerClass('kModulesHelper', KERNEL_PATH . self::MODULE_HELPER_PATH, 'ModulesHelper');
}
/**
* Registers default build events
*
* @return void
* @access protected
*/
protected function RegisterDefaultBuildEvents()
{
$this->EventManager->registerBuildEvent('kTempTablesHandler', 'OnTempHandlerBuild');
}
/**
* Returns cached category information by given cache name. All given category
* information is recached, when at least one of 4 caches is missing.
*
* @param int $category_id
* @param string $name cache name = {filenames, category_designs, category_tree}
* @return string
* @access public
*/
public function getCategoryCache($category_id, $name)
{
return $this->cacheManager->getCategoryCache($category_id, $name);
}
/**
* Returns caching type (none, memory, temporary)
*
* @param int $caching_type
* @return bool
* @access public
*/
public function isCachingType($caching_type)
{
return $this->cacheManager->isCachingType($caching_type);
}
/**
* Increments serial based on prefix and it's ID (optional)
*
* @param string $prefix
* @param int $id ID (value of IDField) or ForeignKeyField:ID
* @param bool $increment
* @return string
* @access public
*/
public function incrementCacheSerial($prefix, $id = null, $increment = true)
{
return $this->cacheManager->incrementCacheSerial($prefix, $id, $increment);
}
/**
* Returns cached $key value from cache named $cache_name
*
* @param int $key key name from cache
* @param bool $store_locally store data locally after retrieved
* @param int $max_rebuild_seconds
* @return mixed
* @access public
*/
public function getCache($key, $store_locally = true, $max_rebuild_seconds = 0)
{
return $this->cacheManager->getCache($key, $store_locally, $max_rebuild_seconds);
}
/**
* Stores new $value in cache with $key name
*
* @param int $key key name to add to cache
* @param mixed $value value of cached record
* @param int $expiration when value expires (0 - doesn't expire)
* @return bool
* @access public
*/
public function setCache($key, $value, $expiration = 0)
{
return $this->cacheManager->setCache($key, $value, $expiration);
}
/**
* Stores new $value in cache with $key name (only if it's not there)
*
* @param int $key key name to add to cache
* @param mixed $value value of cached record
* @param int $expiration when value expires (0 - doesn't expire)
* @return bool
* @access public
*/
public function addCache($key, $value, $expiration = 0)
{
return $this->cacheManager->addCache($key, $value, $expiration);
}
/**
* Sets rebuilding mode for given cache
*
* @param string $name
* @param int $mode
* @param int $max_rebuilding_time
* @return bool
* @access public
*/
public function rebuildCache($name, $mode = null, $max_rebuilding_time = 0)
{
return $this->cacheManager->rebuildCache($name, $mode, $max_rebuilding_time);
}
/**
* Deletes key from cache
*
* @param string $key
* @return void
* @access public
*/
public function deleteCache($key)
{
$this->cacheManager->deleteCache($key);
}
/**
* Reset's all memory cache at once
*
* @return void
* @access public
*/
public function resetCache()
{
$this->cacheManager->resetCache();
}
/**
* Returns value from database cache
*
* @param string $name key name
* @param int $max_rebuild_seconds
* @return mixed
* @access public
*/
public function getDBCache($name, $max_rebuild_seconds = 0)
{
return $this->cacheManager->getDBCache($name, $max_rebuild_seconds);
}
/**
* Sets value to database cache
*
* @param string $name
* @param mixed $value
* @param int|bool $expiration
* @return void
* @access public
*/
public function setDBCache($name, $value, $expiration = false)
{
$this->cacheManager->setDBCache($name, $value, $expiration);
}
/**
* Sets rebuilding mode for given cache
*
* @param string $name
* @param int $mode
* @param int $max_rebuilding_time
* @return bool
* @access public
*/
public function rebuildDBCache($name, $mode = null, $max_rebuilding_time = 0)
{
return $this->cacheManager->rebuildDBCache($name, $mode, $max_rebuilding_time);
}
/**
* Deletes key from database cache
*
* @param string $name
* @return void
* @access public
*/
public function deleteDBCache($name)
{
$this->cacheManager->deleteDBCache($name);
}
/**
* Registers each module specific constants if any found
*
* @return bool
* @access protected
*/
protected function registerModuleConstants()
{
if ( file_exists(KERNEL_PATH . '/constants.php') ) {
kUtil::includeOnce(KERNEL_PATH . '/constants.php');
}
if ( !$this->ModuleInfo ) {
return false;
}
foreach ($this->ModuleInfo as $module_info) {
$constants_file = FULL_PATH . '/' . $module_info['Path'] . 'constants.php';
if ( file_exists($constants_file) ) {
kUtil::includeOnce($constants_file);
}
}
return true;
}
/**
* Performs redirect to hard maintenance template
*
* @return void
* @access public
*/
public function redirectToMaintenance()
{
$maintenance_page = WRITEBALE_BASE . '/maintenance.html';
$query_string = ''; // $this->isAdmin ? '' : '?next_template=' . kUtil::escape($_SERVER['REQUEST_URI'], kUtil::ESCAPE_URL);
if ( file_exists(FULL_PATH . $maintenance_page) ) {
header('Location: ' . BASE_PATH . $maintenance_page . $query_string);
exit;
}
}
/**
* 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
* @access public
*/
public function Run()
{
// process maintenance mode redirect: begin
$maintenance_mode = $this->getMaintenanceMode();
if ( $maintenance_mode == MaintenanceMode::HARD ) {
$this->redirectToMaintenance();
}
elseif ( $maintenance_mode == MaintenanceMode::SOFT ) {
$maintenance_template = $this->isAdmin ? 'login' : $this->ConfigValue('SoftMaintenanceTemplate');
if ( $this->GetVar('t') != $maintenance_template ) {
$redirect_params = Array ();
if ( !$this->isAdmin ) {
$redirect_params['next_template'] = $_SERVER['REQUEST_URI'];
}
$this->Redirect($maintenance_template, $redirect_params);
}
}
// process maintenance mode redirect: end
if ( defined('DEBUG_MODE') && $this->isDebugMode() && kUtil::constOn('DBG_PROFILE_MEMORY') ) {
$this->Debugger->appendMemoryUsage('Application before Run:');
}
if ( $this->isAdminUser ) {
// for permission checking in events & templates
$this->LinkVar('module'); // for common configuration templates
$this->LinkVar('module_key'); // for common search templates
$this->LinkVar('section'); // for common configuration templates
if ( $this->GetVar('m_opener') == 'p' ) {
$this->LinkVar('main_prefix'); // window prefix, that opened selector
$this->LinkVar('dst_field'); // field to set value choosed in selector
}
if ( $this->GetVar('ajax') == 'yes' && !$this->GetVar('debug_ajax') ) {
// hide debug output from ajax requests automatically
kUtil::safeDefine('DBG_SKIP_REPORTING', 1); // safeDefine, because debugger also defines it
}
}
elseif ( $this->GetVar('admin') ) {
$admin_session = $this->recallObject('Session.admin');
/* @var $admin_session Session */
// store Admin Console User's ID to Front-End's session for cross-session permission checks
$this->StoreVar('admin_user_id', (int)$admin_session->RecallVar('user_id'));
if ( $this->CheckAdminPermission('CATEGORY.MODIFY', 0, $this->getBaseCategory()) ) {
// user can edit cms blocks (when viewing front-end through admin's frame)
$editing_mode = $this->GetVar('editing_mode');
define('EDITING_MODE', $editing_mode ? $editing_mode : EDITING_MODE_BROWSE);
}
}
kUtil::safeDefine('EDITING_MODE', ''); // user can't edit anything
$this->Phrases->setPhraseEditing();
$this->EventManager->ProcessRequest();
$this->InitParser();
$t = $this->GetVar('render_template', $this->GetVar('t'));
if ( !$this->TemplatesCache->TemplateExists($t) && !$this->isAdmin ) {
$cms_handler = $this->recallObject('st_EventHandler');
/* @var $cms_handler CategoriesEventHandler */
$t = ltrim($cms_handler->GetDesignTemplate(), '/');
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendHTML('<strong>Design Template</strong>: ' . $t . '; <strong>CategoryID</strong>: ' . $this->GetVar('m_cat_id'));
}
}
/*else {
$cms_handler->SetCatByTemplate();
}*/
if ( defined('DEBUG_MODE') && $this->isDebugMode() && kUtil::constOn('DBG_PROFILE_MEMORY') ) {
$this->Debugger->appendMemoryUsage('Application before Parsing:');
}
$this->HTML = $this->Parser->Run($t);
if ( defined('DEBUG_MODE') && $this->isDebugMode() && kUtil::constOn('DBG_PROFILE_MEMORY') ) {
$this->Debugger->appendMemoryUsage('Application after Parsing:');
}
}
/**
* Only renders template
*
* @see kDBEventHandler::_errorNotFound()
*/
public function QuickRun()
{
// discard any half-parsed content
ob_clean();
// replace current page content with 404
$this->InitParser();
$this->HTML = $this->Parser->Run($this->GetVar('t'));
}
/**
* Performs template parser/cache initialization
*
* @param bool|string $theme_name
* @return void
* @access public
*/
public function InitParser($theme_name = false)
{
if ( !is_object($this->Parser) ) {
$this->Parser = $this->recallObject('NParser');
$this->TemplatesCache = $this->recallObject('TemplatesCache');
}
$this->TemplatesCache->forceThemeName = $theme_name;
}
/**
* Send the parser results to browser
*
* Actually send everything stored in {@link $this->HTML}, to the browser by echoing it.
*
* @return void
* @access public
*/
public function Done()
{
$this->HandleEvent(new kEvent('adm:OnBeforeShutdown'));
$debug_mode = defined('DEBUG_MODE') && $this->isDebugMode();
if ( $debug_mode ) {
if ( kUtil::constOn('DBG_PROFILE_MEMORY') ) {
$this->Debugger->appendMemoryUsage('Application before Done:');
}
$this->Session->SaveData(); // adds session data to debugger report
$this->HTML = ob_get_clean() . $this->HTML . $this->Debugger->printReport(true);
}
else {
// send "Set-Cookie" header before any output is made
$this->Session->SetSession();
$this->HTML = ob_get_clean() . $this->HTML;
}
$this->_outputPage();
$this->cacheManager->UpdateApplicationCache();
if ( !$debug_mode ) {
$this->Session->SaveData();
}
$this->EventManager->runScheduledTasks();
if ( defined('DBG_CAPTURE_STATISTICS') && DBG_CAPTURE_STATISTICS && !$this->isAdmin ) {
$this->_storeStatistics();
}
}
/**
* Outputs generated page content to end-user
*
* @return void
* @access protected
*/
protected function _outputPage()
{
$this->setContentType();
ob_start();
if ( $this->UseOutputCompression() ) {
$compression_level = $this->ConfigValue('OutputCompressionLevel');
if ( !$compression_level || $compression_level < 0 || $compression_level > 9 ) {
$compression_level = 7;
}
header('Content-Encoding: gzip');
echo gzencode($this->HTML, $compression_level);
}
else {
// when gzip compression not used connection won't be closed early!
echo $this->HTML;
}
// send headers to tell the browser to close the connection
header('Content-Length: ' . ob_get_length());
header('Connection: close');
// flush all output
ob_end_flush();
if ( ob_get_level() ) {
ob_flush();
}
flush();
// close current session
if ( session_id() ) {
session_write_close();
}
}
/**
* Stores script execution statistics to database
*
* @return void
* @access protected
*/
protected function _storeStatistics()
{
global $start;
$script_time = microtime(true) - $start;
$query_statistics = $this->Conn->getQueryStatistics(); // time & count
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'StatisticsCapture
WHERE TemplateName = ' . $this->Conn->qstr($this->GetVar('t'));
$data = $this->Conn->GetRow($sql);
if ( $data ) {
$this->_updateAverageStatistics($data, 'ScriptTime', $script_time);
$this->_updateAverageStatistics($data, 'SqlTime', $query_statistics['time']);
$this->_updateAverageStatistics($data, 'SqlCount', $query_statistics['count']);
$data['Hits']++;
$data['LastHit'] = time();
$this->Conn->doUpdate($data, TABLE_PREFIX . 'StatisticsCapture', 'StatisticsId = ' . $data['StatisticsId']);
}
else {
$data['ScriptTimeMin'] = $data['ScriptTimeAvg'] = $data['ScriptTimeMax'] = $script_time;
$data['SqlTimeMin'] = $data['SqlTimeAvg'] = $data['SqlTimeMax'] = $query_statistics['time'];
$data['SqlCountMin'] = $data['SqlCountAvg'] = $data['SqlCountMax'] = $query_statistics['count'];
$data['TemplateName'] = $this->GetVar('t');
$data['Hits'] = 1;
$data['LastHit'] = time();
$this->Conn->doInsert($data, TABLE_PREFIX . 'StatisticsCapture');
}
}
/**
* Calculates average time for statistics
*
* @param Array $data
* @param string $field_prefix
* @param float $current_value
* @return void
* @access protected
*/
protected function _updateAverageStatistics(&$data, $field_prefix, $current_value)
{
$data[$field_prefix . 'Avg'] = (($data['Hits'] * $data[$field_prefix . 'Avg']) + $current_value) / ($data['Hits'] + 1);
if ( $current_value < $data[$field_prefix . 'Min'] ) {
$data[$field_prefix . 'Min'] = $current_value;
}
if ( $current_value > $data[$field_prefix . 'Max'] ) {
$data[$field_prefix . 'Max'] = $current_value;
}
}
/**
* Remembers slow query SQL and execution time into log
*
* @param string $slow_sql
* @param int $time
* @return void
* @access public
*/
public function logSlowQuery($slow_sql, $time)
{
$query_crc = kUtil::crc32($slow_sql);
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'SlowSqlCapture
WHERE QueryCrc = ' . $query_crc;
$data = $this->Conn->Query($sql, null, true);
if ( $data ) {
$this->_updateAverageStatistics($data, 'Time', $time);
$template_names = explode(',', $data['TemplateNames']);
array_push($template_names, $this->GetVar('t'));
$data['TemplateNames'] = implode(',', array_unique($template_names));
$data['Hits']++;
$data['LastHit'] = time();
$this->Conn->doUpdate($data, TABLE_PREFIX . 'SlowSqlCapture', 'CaptureId = ' . $data['CaptureId']);
}
else {
$data['TimeMin'] = $data['TimeAvg'] = $data['TimeMax'] = $time;
$data['SqlQuery'] = $slow_sql;
$data['QueryCrc'] = $query_crc;
$data['TemplateNames'] = $this->GetVar('t');
$data['Hits'] = 1;
$data['LastHit'] = time();
$this->Conn->doInsert($data, TABLE_PREFIX . 'SlowSqlCapture');
}
}
/**
* Checks if output compression options is available
*
* @return bool
* @access protected
*/
protected function UseOutputCompression()
{
if ( kUtil::constOn('IS_INSTALL') || kUtil::constOn('DBG_ZEND_PRESENT') || kUtil::constOn('SKIP_OUT_COMPRESSION') ) {
return false;
}
$accept_encoding = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
return $this->ConfigValue('UseOutputCompression') && function_exists('gzencode') && strstr($accept_encoding, 'gzip');
}
// Facade
/**
* Returns current session id (SID)
*
* @return int
* @access public
*/
public function GetSID()
{
$session = $this->recallObject('Session');
/* @var $session Session */
return $session->GetID();
}
/**
* Destroys current session
*
* @return void
* @access public
* @see UserHelper::logoutUser()
*/
public function DestroySession()
{
$session = $this->recallObject('Session');
/* @var $session Session */
$session->Destroy();
}
/**
* 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
* @access public
*/
public function GetVar($name, $default = false)
{
return isset($this->HttpQuery->_Params[$name]) ? $this->HttpQuery->_Params[$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)
{
return $this->HttpQuery->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
* @access public
*/
public function GetVarDirect($name, $type, $default = false)
{
// $type = ucfirst($type);
$array = $this->HttpQuery->$type;
return isset($array[$name]) ? $array[$name] : $default;
}
/**
* Returns ALL variables passed to the script as GET/POST/COOKIE
*
* @return Array
* @access public
* @deprecated
*/
public function GetVars()
{
return $this->HttpQuery->GetParams();
}
/**
* 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
* @access public
*/
public function SetVar($var,$val)
{
$this->HttpQuery->Set($var, $val);
}
/**
* Deletes kHTTPQuery variable
*
* @param string $var
* @return void
* @todo Think about method name
*/
public function DeleteVar($var)
{
$this->HttpQuery->Remove($var);
}
/**
* Deletes Session variable
*
* @param string $var
* @return void
* @access public
*/
public function RemoveVar($var)
{
$this->Session->RemoveVar($var);
}
/**
* Removes variable from persistent session
*
* @param string $var
* @return void
* @access public
*/
public function RemovePersistentVar($var)
{
$this->Session->RemovePersistentVar($var);
}
/**
* Restores Session variable to it's db version
*
* @param string $var
* @return void
* @access public
*/
public function RestoreVar($var)
{
$this->Session->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
* @access public
* @see Session::RecallVar()
*/
public function RecallVar($var,$default=false)
{
return $this->Session->RecallVar($var,$default);
}
/**
* Returns variable value from persistent session
*
* @param string $var
* @param mixed $default
* @return mixed
* @access public
* @see Session::RecallPersistentVar()
*/
public function RecallPersistentVar($var, $default = false)
{
return $this->Session->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 bool $optional
* @return void
* @access public
* @see kApplication::RecallVar()
*/
public function StoreVar($var, $val, $optional = false)
{
$session = $this->recallObject('Session');
/* @var $session Session */
$this->Session->StoreVar($var, $val, $optional);
}
/**
* Stores variable to persistent session
*
* @param string $var
* @param mixed $val
* @param bool $optional
* @return void
* @access public
*/
public function StorePersistentVar($var, $val, $optional = false)
{
$this->Session->StorePersistentVar($var, $val, $optional);
}
/**
* Stores default value for session variable
*
* @param string $var
* @param string $val
* @param bool $optional
* @return void
* @access public
* @see Session::RecallVar()
* @see Session::StoreVar()
*/
public function StoreVarDefault($var, $val, $optional = false)
{
$session = $this->recallObject('Session');
/* @var $session Session */
$this->Session->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 bool $optional
* @return void
* @access public
*/
public function LinkVar($var, $ses_var = null, $default = '', $optional = false)
{
if ( !isset($ses_var) ) {
$ses_var = $var;
}
if ( $this->GetVar($var) !== false ) {
$this->StoreVar($ses_var, $this->GetVar($var), $optional);
}
else {
$this->SetVar($var, $this->RecallVar($ses_var, $default));
}
}
/**
* 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
* @access public
* @see LinkVar
*/
public function GetLinkedVar($var, $ses_var = null, $default = '')
{
$this->LinkVar($var, $ses_var, $default);
return $this->GetVar($var);
}
/**
* Renders given tag and returns it's output
*
* @param string $prefix
* @param string $tag
* @param Array $params
* @return mixed
* @access public
* @see kApplication::InitParser()
*/
public function ProcessParsedTag($prefix, $tag, $params)
{
$processor = $this->Parser->GetProcessor($prefix);
/* @var $processor kDBTagProcessor */
return $processor->ProcessParsedTag($tag, $params, $prefix);
}
/**
* Return object of IDBConnection interface
*
* Return object of IDBConnection interface already connected to the project database, configurable in config.php
*
* @return IDBConnection
* @access public
*/
public function &GetADODBConnection()
{
return $this->Conn;
}
/**
* Allows to parse given block name or include template
*
* @param Array $params Parameters to pass to block. Reserved parameter "name" used to specify block name.
* @param bool $pass_params Forces to pass current parser params to this block/template. Use with caution, because you can accidentally pass "block_no_data" parameter.
* @param bool $as_template
* @return string
* @access public
*/
public function ParseBlock($params, $pass_params = false, $as_template = false)
{
if ( substr($params['name'], 0, 5) == 'html:' ) {
return substr($params['name'], 5);
}
return $this->Parser->ParseBlock($params, $pass_params, $as_template);
}
/**
* Checks, that we have given block defined
*
* @param string $name
* @return bool
* @access public
*/
public function ParserBlockFound($name)
{
return $this->Parser->blockFound($name);
}
/**
* Allows to include template with a given name and given parameters
*
* @param Array $params Parameters to pass to template. Reserved parameter "name" used to specify template name.
* @return string
* @access public
*/
public function IncludeTemplate($params)
{
return $this->Parser->IncludeTemplate($params, isset($params['is_silent']) ? 1 : 0);
}
/**
* Return href for template
*
* @param string $t Template path
* @param string $prefix index.php prefix - could be blank, 'admin'
* @param Array $params
* @param string $index_file
* @return string
*/
public function HREF($t, $prefix = '', $params = Array (), $index_file = null)
{
return $this->UrlManager->HREF($t, $prefix, $params, $index_file);
}
/**
* Returns theme template filename and it's corresponding page_id based on given seo template
*
* @param string $seo_template
* @return string
* @access public
*/
public function getPhysicalTemplate($seo_template)
{
return $this->UrlManager->getPhysicalTemplate($seo_template);
}
/**
* Returns seo template by physical template
*
* @param string $physical_template
* @return string
* @access public
*/
public function getSeoTemplate($physical_template)
{
return $this->UrlManager->getSeoTemplate($physical_template);
}
/**
* Returns template name, that corresponds with given virtual (not physical) page id
*
* @param int $page_id
* @return string|bool
* @access public
*/
public function getVirtualPageTemplate($page_id)
{
return $this->UrlManager->getVirtualPageTemplate($page_id);
}
/**
* Returns section template for given physical/virtual template
*
* @param string $template
* @param int $theme_id
* @return string
* @access public
*/
public function getSectionTemplate($template, $theme_id = null)
{
return $this->UrlManager->getSectionTemplate($template, $theme_id);
}
/**
* Returns variables with values that should be passed through with this link + variable list
*
* @param Array $params
* @return Array
* @access public
*/
public function getPassThroughVariables(&$params)
{
return $this->UrlManager->getPassThroughVariables($params);
}
/**
* Builds url
*
* @param string $t
* @param Array $params
* @param string $pass
* @param bool $pass_events
* @param bool $env_var
* @return string
* @access public
*/
public function BuildEnv($t, $params, $pass = 'all', $pass_events = false, $env_var = true)
{
return $this->UrlManager->plain->build($t, $params, $pass, $pass_events, $env_var);
}
/**
* Process QueryString only, create
* events, ids, based on config
* set template name and sid in
* desired application variables.
*
* @param string $env_var environment string value
* @param string $pass_name
* @return Array
* @access public
*/
public function processQueryString($env_var, $pass_name = 'passed')
{
return $this->UrlManager->plain->parse($env_var, $pass_name);
}
/**
* Parses rewrite url and returns parsed variables
*
* @param string $url
* @param string $pass_name
* @return Array
* @access public
*/
public function parseRewriteUrl($url, $pass_name = 'passed')
{
return $this->UrlManager->rewrite->parse($url, $pass_name);
}
/**
* Returns base part of all urls, build on website
*
* @param string $domain Domain override.
* @param boolean $ssl_redirect Redirect to/from SSL.
*
* @return string
*/
public function BaseURL($domain = '', $ssl_redirect = null)
{
if ( $ssl_redirect === null ) {
// stay on same encryption level
return PROTOCOL . ($domain ? $domain : SERVER_NAME) . (defined('PORT') ? ':' . PORT : '') . BASE_PATH . '/';
}
if ( $ssl_redirect ) {
// going from http:// to https://
$protocol = 'https://';
$domain = $this->getSecureDomain();
}
else {
// going from https:// to http://
$protocol = 'http://';
$domain = $this->siteDomainField('DomainName');
if ( $domain === false ) {
$domain = DOMAIN; // not on site domain
}
}
return $protocol . $domain . (defined('PORT') ? ':' . PORT : '') . BASE_PATH . '/';
}
/**
* Returns secure domain.
*
* @return string
*/
public function getSecureDomain()
{
$ret = $this->isAdmin ? $this->ConfigValue('AdminSSLDomain') : false;
if ( !$ret ) {
$ssl_domain = $this->siteDomainField('SSLDomainName');
return strlen($ssl_domain) ? $ssl_domain : $this->ConfigValue('SSLDomain');
}
return $ret;
}
/**
* Redirects user to url, that's build based on given parameters
*
* @param string $t
* @param Array $params
* @param string $prefix
* @param string $index_file
* @return void
* @access public
*/
public function Redirect($t = '', $params = Array(), $prefix = '', $index_file = null)
{
$js_redirect = getArrayValue($params, 'js_redirect');
if ( $t == '' || $t === true ) {
$t = $this->GetVar('t');
}
// pass prefixes and special from previous url
if ( array_key_exists('js_redirect', $params) ) {
unset($params['js_redirect']);
}
// allows to send custom responce code along with redirect header
if ( array_key_exists('response_code', $params) ) {
$response_code = (int)$params['response_code'];
unset($params['response_code']);
}
else {
$response_code = 302; // Found
}
if ( !array_key_exists('pass', $params) ) {
$params['pass'] = 'all';
}
if ( $this->GetVar('ajax') == 'yes' && $t == $this->GetVar('t') ) {
// redirects to the same template as current
$params['ajax'] = 'yes';
}
$location = $this->HREF($t, $prefix, $params, $index_file);
if ( $this->isDebugMode() && (kUtil::constOn('DBG_REDIRECT') || (kUtil::constOn('DBG_RAISE_ON_WARNINGS') && $this->Debugger->WarningCount)) ) {
$this->Debugger->appendTrace();
echo '<strong>Debug output above !!!</strong><br/>' . "\n";
if ( array_key_exists('HTTP_REFERER', $_SERVER) ) {
echo 'Referer: <strong>' . $_SERVER['HTTP_REFERER'] . '</strong><br/>' . "\n";
}
echo "Proceed to redirect: <a href=\"{$location}\">{$location}</a><br/>\n";
}
else {
if ( $js_redirect ) {
// show "redirect" template instead of redirecting,
// because "Set-Cookie" header won't work, when "Location"
// header is used later
$this->SetVar('t', 'redirect');
$this->SetVar('redirect_to', $location);
// make all additional parameters available on "redirect" template too
foreach ($params as $name => $value) {
$this->SetVar($name, $value);
}
return;
}
else {
if ( $this->GetVar('ajax') == 'yes' && ($t != $this->GetVar('t') || !$this->isSOPSafe($location, $t)) ) {
// redirection to other then current template during ajax request OR SOP violation
kUtil::safeDefine('DBG_SKIP_REPORTING', 1);
echo '#redirect#' . $location;
}
elseif ( headers_sent() != '' ) {
// some output occurred -> redirect using javascript
echo '<script type="text/javascript">window.location.href = \'' . $location . '\';</script>';
}
else {
// no output before -> redirect using HTTP header
// header('HTTP/1.1 302 Found');
header('Location: ' . $location, true, $response_code);
}
}
}
// session expiration is called from session initialization,
// that's why $this->Session may be not defined here
$session = $this->recallObject('Session');
/* @var $session Session */
if ( $this->InitDone ) {
// if redirect happened in the middle of application initialization don't call event,
// that presumes that application was successfully initialized
$this->HandleEvent(new kEvent('adm:OnBeforeShutdown'));
}
$session->SaveData();
ob_end_flush();
exit;
}
/**
* Determines if real redirect should be made within AJAX request.
*
* @param string $url Location.
* @param string $template Template.
*
* @return boolean
* @link http://en.wikipedia.org/wiki/Same-origin_policy
*/
protected function isSOPSafe($url, $template)
{
$parsed_url = parse_url($url);
if ( $parsed_url['scheme'] . '://' != PROTOCOL ) {
return false;
}
if ( $parsed_url['host'] != SERVER_NAME ) {
return false;
}
if ( defined('PORT') && isset($parsed_url['port']) && $parsed_url['port'] != PORT ) {
return false;
}
return true;
}
/**
* Returns translation of given label
*
* @param string $label
* @param bool $allow_editing return translation link, when translation is missing on current language
* @param bool $use_admin use current Admin Console language to translate phrase
* @return string
* @access public
*/
public function Phrase($label, $allow_editing = true, $use_admin = false)
{
return $this->Phrases->GetPhrase($label, $allow_editing, $use_admin);
}
/**
* Replace language tags in exclamation marks found in text
*
* @param string $text
* @param bool $force_escape force escaping, not escaping of resulting string
* @return string
* @access public
*/
public function ReplaceLanguageTags($text, $force_escape = null)
{
return $this->Phrases->ReplaceLanguageTags($text, $force_escape);
}
/**
* 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
* @access protected
*/
protected function ValidateLogin()
{
$session = $this->recallObject('Session');
/* @var $session Session */
$user_id = $session->GetField('PortalUserId');
if ( !$user_id && $user_id != USER_ROOT ) {
$user_id = USER_GUEST;
}
$this->SetVar('u.current_id', $user_id);
if ( !$this->isAdmin ) {
// needed for "profile edit", "registration" forms ON FRONT ONLY
$this->SetVar('u_id', $user_id);
}
$this->StoreVar('user_id', $user_id, $user_id == USER_GUEST); // storing Guest user_id (-2) is optional
$this->isAdminUser = $this->isAdmin && $this->LoggedIn();
if ( $this->GetVar('expired') == 1 ) {
// this parameter is set only from admin
$user = $this->recallObject('u.login-admin', null, Array ('form_name' => 'login'));
/* @var $user UsersItem */
$user->SetError('UserLogin', 'session_expired', 'la_text_sess_expired');
}
$this->HandleEvent(new kEvent('adm:OnLogHttpRequest'));
if ( $user_id != USER_GUEST ) {
// normal users + root
$this->LoadPersistentVars();
}
$user_timezone = $this->Session->GetField('TimeZone');
if ( $user_timezone ) {
date_default_timezone_set($user_timezone);
}
}
/**
* Loads current user persistent session data
*
* @return void
* @access public
*/
public function LoadPersistentVars()
{
$this->Session->LoadPersistentVars();
}
/**
* Returns configuration option value by name
*
* @param string $name
* @return string
* @access public
*/
public function ConfigValue($name)
{
return $this->cacheManager->ConfigValue($name);
}
/**
* Changes value of individual configuration variable (+resets cache, when needed)
*
* @param string $name
* @param string $value
* @param bool $local_cache_only
* @return string
* @access public
*/
public function SetConfigValue($name, $value, $local_cache_only = false)
{
return $this->cacheManager->SetConfigValue($name, $value, $local_cache_only);
}
/**
* Allows to process any type of event
*
* @param kEvent $event
* @param Array $params
* @param Array $specific_params
* @return void
* @access public
*/
public function HandleEvent($event, $params = null, $specific_params = null)
{
if ( isset($params) ) {
$event = new kEvent($params, $specific_params);
}
$this->EventManager->HandleEvent($event);
}
/**
* Notifies event subscribers, that event has occured
*
* @param kEvent $event
* @return void
*/
public function notifyEventSubscribers(kEvent $event)
{
$this->EventManager->notifySubscribers($event);
}
/**
* Allows to process any type of event
*
* @param kEvent $event
* @return bool
* @access public
*/
public function eventImplemented(kEvent $event)
{
return $this->EventManager->eventImplemented($event);
}
/**
* Registers new class in the factory
*
* @param string $real_class Real name of class as in class declaration
* @param string $file Filename in what $real_class is declared
* @param string $pseudo_class Name under this class object will be accessed using getObject method
* @return void
* @access public
*/
public function registerClass($real_class, $file, $pseudo_class = null)
{
$this->Factory->registerClass($real_class, $file, $pseudo_class);
}
/**
* Unregisters existing class from factory
*
* @param string $real_class Real name of class as in class declaration
* @param string $pseudo_class Name under this class object is accessed using getObject method
* @return void
* @access public
*/
public function unregisterClass($real_class, $pseudo_class = null)
{
$this->Factory->unregisterClass($real_class, $pseudo_class);
}
/**
* Add new scheduled task
*
* @param string $short_name name to be used to store last maintenance run info
* @param string $event_string
* @param int $run_schedule run schedule like for Cron
* @param string $module
* @param int $status
* @access public
*/
public function registerScheduledTask($short_name, $event_string, $run_schedule, $module, $status = STATUS_ACTIVE)
{
$this->EventManager->registerScheduledTask($short_name, $event_string, $run_schedule, $module, $status);
}
/**
* Registers Hook from subprefix event to master prefix event
*
* Pattern: Observer
*
* @param string $hook_event
* @param string $do_event
* @param int $mode
* @param bool $conditional
* @access public
*/
public function registerHook($hook_event, $do_event, $mode = hAFTER, $conditional = false)
{
$this->EventManager->registerHook($hook_event, $do_event, $mode, $conditional);
}
/**
* Registers build event for given pseudo class
*
* @param string $pseudo_class
* @param string $event_name
* @access public
*/
public function registerBuildEvent($pseudo_class, $event_name)
{
$this->EventManager->registerBuildEvent($pseudo_class, $event_name);
}
/**
* Allows one TagProcessor tag act as other TagProcessor tag
*
* @param Array $tag_info
* @return void
* @access public
*/
public function registerAggregateTag($tag_info)
{
$aggregator = $this->recallObject('TagsAggregator', 'kArray');
/* @var $aggregator kArray */
$tag_data = Array (
$tag_info['LocalPrefix'],
$tag_info['LocalTagName'],
getArrayValue($tag_info, 'LocalSpecial')
);
$aggregator->SetArrayValue($tag_info['AggregateTo'], $tag_info['AggregatedTagName'], $tag_data);
}
/**
* Returns object using params specified, creates it if is required
*
* @param string $name
* @param string $pseudo_class
* @param Array $event_params
* @param Array $arguments
* @return kBase
*/
public function recallObject($name, $pseudo_class = null, $event_params = Array(), $arguments = Array ())
{
/*if ( !$this->hasObject($name) && $this->isDebugMode() && ($name == '_prefix_here_') ) {
// first time, when object with "_prefix_here_" prefix is accessed
$this->Debugger->appendTrace();
}*/
return $this->Factory->getObject($name, $pseudo_class, $event_params, $arguments);
}
/**
* Returns tag processor for prefix specified
*
* @param string $prefix
* @return kDBTagProcessor
* @access public
*/
public function recallTagProcessor($prefix)
{
$this->InitParser(); // because kDBTagProcesor is in NParser dependencies
return $this->recallObject($prefix . '_TagProcessor');
}
/**
* Checks if object with prefix passes was already created in factory
*
* @param string $name object pseudo_class, prefix
* @return bool
* @access public
*/
public function hasObject($name)
{
return $this->Factory->hasObject($name);
}
/**
* Removes object from storage by given name
*
* @param string $name Object's name in the Storage
* @return void
* @access public
*/
public function removeObject($name)
{
$this->Factory->DestroyObject($name);
}
/**
* Get's real class name for pseudo class, includes class file and creates class instance
*
* Pattern: Factory Method
*
* @param string $pseudo_class
* @param Array $arguments
* @return kBase
* @access public
*/
public function makeClass($pseudo_class, $arguments = Array ())
{
return $this->Factory->makeClass($pseudo_class, $arguments);
}
/**
* Checks if application is in debug mode
*
* @param bool $check_debugger check if kApplication debugger is initialized too, not only for defined DEBUG_MODE constant
* @return bool
* @author Alex
* @access public
*/
public function isDebugMode($check_debugger = true)
{
$debug_mode = defined('DEBUG_MODE') && DEBUG_MODE;
if ($check_debugger) {
$debug_mode = $debug_mode && is_object($this->Debugger);
}
return $debug_mode;
}
/**
* Apply url rewriting used by mod_rewrite or not
*
* @param bool|null $ssl Force ssl link to be build
* @return bool
* @access public
*/
public function RewriteURLs($ssl = false)
{
// case #1,#4:
// we want to create https link from http mode
// we want to create https link from https mode
// conditions: ($ssl || PROTOCOL == 'https://') && $this->ConfigValue('UseModRewriteWithSSL')
// case #2,#3:
// we want to create http link from https mode
// we want to create http link from http mode
// conditions: !$ssl && (PROTOCOL == 'https://' || PROTOCOL == 'http://')
$allow_rewriting =
(!$ssl && (PROTOCOL == 'https://' || PROTOCOL == 'http://')) // always allow mod_rewrite for http
|| // or allow rewriting for redirect TO httpS or when already in httpS
(($ssl || PROTOCOL == 'https://') && $this->ConfigValue('UseModRewriteWithSSL')); // but only if it's allowed in config!
return kUtil::constOn('MOD_REWRITE') && $allow_rewriting;
}
/**
* Returns unit config for given prefix
*
* @param string $prefix
* @return kUnitConfig
* @access public
*/
public function getUnitConfig($prefix)
{
return $this->UnitConfigReader->getUnitConfig($prefix);
}
/**
* Returns true if config exists and is allowed for reading
*
* @param string $prefix
* @return bool
*/
public function prefixRegistred($prefix)
{
return $this->UnitConfigReader->prefixRegistered($prefix);
}
/**
* Splits any mixing of prefix and
* special into correct ones
*
* @param string $prefix_special
* @return Array
* @access public
*/
public function processPrefix($prefix_special)
{
return $this->Factory->processPrefix($prefix_special);
}
/**
* Set's new event for $prefix_special
* passed
*
* @param string $prefix_special
* @param string $event_name
* @return void
* @access public
*/
public function setEvent($prefix_special, $event_name)
{
$this->EventManager->setEvent($prefix_special, $event_name);
}
/**
* SQL Error Handler
*
* @param int $code
* @param string $msg
* @param string $sql
* @return bool
* @access public
* @throws Exception
* @deprecated
*/
public function handleSQLError($code, $msg, $sql)
{
return $this->_logger->handleSQLError($code, $msg, $sql);
}
/**
* Returns & blocks next ResourceId available in system
*
* @return int
* @access public
*/
public function NextResourceId()
{
$table_name = TABLE_PREFIX . 'IdGenerator';
$this->Conn->Query('LOCK TABLES ' . $table_name . ' WRITE');
$this->Conn->Query('UPDATE ' . $table_name . ' SET lastid = lastid + 1');
$id = $this->Conn->GetOne('SELECT lastid FROM ' . $table_name);
if ( $id === false ) {
$this->Conn->Query('INSERT INTO ' . $table_name . ' (lastid) VALUES (2)');
$id = 2;
}
$this->Conn->Query('UNLOCK TABLES');
return $id - 1;
}
/**
* Returns genealogical main prefix for sub-table prefix passes
* OR prefix, that has been found in REQUEST and some how is parent of passed sub-table prefix
*
* @param string $current_prefix
* @param bool $real_top if set to true will return real topmost prefix, regardless of its id is passed or not
* @return string
* @access public
*/
public function GetTopmostPrefix($current_prefix, $real_top = false)
{
// 1. get genealogical tree of $current_prefix
$prefixes = Array ($current_prefix);
while ($parent_prefix = $this->getUnitConfig($current_prefix)->getParentPrefix()) {
if ( !$this->prefixRegistred($parent_prefix) ) {
// stop searching, when parent prefix is not registered
break;
}
$current_prefix = $parent_prefix;
array_unshift($prefixes, $current_prefix);
}
if ( $real_top ) {
return $current_prefix;
}
// 2. find what if parent is passed
$passed = explode(',', $this->GetVar('all_passed'));
foreach ($prefixes as $a_prefix) {
if ( in_array($a_prefix, $passed) ) {
return $a_prefix;
}
}
return $current_prefix;
}
/**
* Triggers email event of type Admin
*
* @param string $email_template_name
* @param int $to_user_id
* @param array $send_params associative array of direct send params, possible keys: to_email, to_name, from_email, from_name, message, message_text
* @return kEvent
* @access public
*/
public function emailAdmin($email_template_name, $to_user_id = null, $send_params = Array ())
{
return $this->_email($email_template_name, EmailTemplate::TEMPLATE_TYPE_ADMIN, $to_user_id, $send_params);
}
/**
* Triggers email event of type User
*
* @param string $email_template_name
* @param int $to_user_id
* @param array $send_params associative array of direct send params, possible keys: to_email, to_name, from_email, from_name, message, message_text
* @return kEvent
* @access public
*/
public function emailUser($email_template_name, $to_user_id = null, $send_params = Array ())
{
return $this->_email($email_template_name, EmailTemplate::TEMPLATE_TYPE_FRONTEND, $to_user_id, $send_params);
}
/**
* Triggers general email event
*
* @param string $email_template_name
* @param int $email_template_type (0 for User, 1 for Admin)
* @param int $to_user_id
* @param array $send_params associative array of direct send params,
* possible keys: to_email, to_name, from_email, from_name, message, message_text
* @return kEvent
* @access protected
*/
protected function _email($email_template_name, $email_template_type, $to_user_id = null, $send_params = Array ())
{
$email = $this->makeClass('kEmail');
/* @var $email kEmail */
if ( !$email->findTemplate($email_template_name, $email_template_type) ) {
return false;
}
$email->setParams($send_params);
return $email->send($to_user_id);
}
/**
* Allows to check if user in this session is logged in or not
*
* @return bool
* @access public
*/
public function LoggedIn()
{
// no session during expiration process
return is_null($this->Session) ? false : $this->Session->LoggedIn();
}
/**
* Check current user permissions based on it's group permissions in specified category
*
* @param string $name permission name
* @param int $cat_id category id, current used if not specified
* @param int $type permission type {1 - system, 0 - per category}
* @return int
* @access public
*/
public function CheckPermission($name, $type = 1, $cat_id = null)
{
$perm_helper = $this->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
return $perm_helper->CheckPermission($name, $type, $cat_id);
}
/**
* Check current admin permissions based on it's group permissions in specified category
*
* @param string $name permission name
* @param int $cat_id category id, current used if not specified
* @param int $type permission type {1 - system, 0 - per category}
* @return int
* @access public
*/
public function CheckAdminPermission($name, $type = 1, $cat_id = null)
{
$perm_helper = $this->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
return $perm_helper->CheckAdminPermission($name, $type, $cat_id);
}
/**
* Set's any field of current visit
*
* @param string $field
* @param mixed $value
* @return void
* @access public
* @todo move to separate module
*/
public function setVisitField($field, $value)
{
if ( $this->isAdmin || !$this->ConfigValue('UseVisitorTracking') ) {
// admin logins are not registered in visits list
return;
}
$visit = $this->recallObject('visits', null, Array ('raise_warnings' => 0));
/* @var $visit kDBItem */
if ( $visit->isLoaded() ) {
$visit->SetDBField($field, $value);
$visit->Update();
}
}
/**
* Allows to check if in-portal is installed
*
* @return bool
* @access public
*/
public function isInstalled()
{
return $this->InitDone && (count($this->ModuleInfo) > 0);
}
/**
* Allows to determine if module is installed & enabled
*
* @param string $module_name
* @return bool
* @access public
*/
public function isModuleEnabled($module_name)
{
return $this->findModule('Name', $module_name) !== false;
}
/**
* Returns Window ID of passed prefix main prefix (in edit mode)
*
* @param string $prefix
* @return int
* @access public
*/
public function GetTopmostWid($prefix)
{
$top_prefix = $this->GetTopmostPrefix($prefix);
$mode = $this->GetVar($top_prefix . '_mode');
return $mode != '' ? substr($mode, 1) : '';
}
/**
* Get temp table name
*
* @param string $table
* @param mixed $wid
* @return string
* @access public
*/
public function GetTempName($table, $wid = '')
{
return $this->GetTempTablePrefix($wid) . $table;
}
/**
* Builds temporary table prefix based on given window id
*
* @param string $wid
* @return string
* @access public
*/
public function GetTempTablePrefix($wid = '')
{
if ( preg_match('/prefix:(.*)/', $wid, $regs) ) {
$wid = $this->GetTopmostWid($regs[1]);
}
return TABLE_PREFIX . 'ses_' . $this->GetSID() . ($wid ? '_' . $wid : '') . '_edit_';
}
/**
* Checks if given table is a temporary table
*
* @param string $table
* @return bool
* @access public
*/
public function IsTempTable($table)
{
static $cache = Array ();
if ( !array_key_exists($table, $cache) ) {
$cache[$table] = preg_match('/' . TABLE_PREFIX . 'ses_' . $this->GetSID() . '(_[\d]+){0,1}_edit_(.*)/', $table);
}
return (bool)$cache[$table];
}
/**
* Checks, that given prefix is in temp mode
*
* @param string $prefix
* @param string $special
* @return bool
* @access public
*/
public function IsTempMode($prefix, $special = '')
{
$top_prefix = $this->GetTopmostPrefix($prefix);
$var_names = Array (
$top_prefix,
rtrim($top_prefix . '_' . $special, '_'), // from post
rtrim($top_prefix . '.' . $special, '.'), // assembled locally
);
$var_names = array_unique($var_names);
$temp_mode = false;
foreach ($var_names as $var_name) {
$value = $this->GetVar($var_name . '_mode');
if ( $value && (substr($value, 0, 1) == 't') ) {
$temp_mode = true;
break;
}
}
return $temp_mode;
}
/**
* Return live table name based on temp table name
*
* @param string $temp_table
* @return string
*/
public function GetLiveName($temp_table)
{
if ( preg_match('/' . TABLE_PREFIX . 'ses_' . $this->GetSID() . '(_[\d]+){0,1}_edit_(.*)/', $temp_table, $rets) ) {
// cut wid from table end if any
return $rets[2];
}
else {
return $temp_table;
}
}
/**
* Stops processing of user request and displays given message
*
* @param string $message
* @access public
*/
public function ApplicationDie($message = '')
{
while ( ob_get_level() ) {
ob_end_clean();
}
if ( $this->isDebugMode() ) {
$message .= $this->Debugger->printReport(true);
}
$this->HTML = $message;
$this->_outputPage();
}
/**
* Returns comma-separated list of groups from given user
*
* @param int $user_id
* @return string
*/
public function getUserGroups($user_id)
{
switch ($user_id) {
case USER_ROOT:
$user_groups = $this->ConfigValue('User_LoggedInGroup');
break;
case USER_GUEST:
$user_groups = $this->ConfigValue('User_LoggedInGroup') . ',' . $this->ConfigValue('User_GuestGroup');
break;
default:
$sql = 'SELECT GroupId
FROM ' . TABLE_PREFIX . 'UserGroupRelations
WHERE PortalUserId = ' . (int)$user_id;
$res = $this->Conn->GetCol($sql);
$user_groups = Array ($this->ConfigValue('User_LoggedInGroup'));
if ( $res ) {
$user_groups = array_merge($user_groups, $res);
}
$user_groups = implode(',', $user_groups);
}
return $user_groups;
}
/**
* Allows to detect if page is browsed by spider (293 scheduled_tasks supported)
*
* @return bool
* @access public
*/
/*public function IsSpider()
{
static $is_spider = null;
if ( !isset($is_spider) ) {
$user_agent = trim($_SERVER['HTTP_USER_AGENT']);
$robots = file(FULL_PATH . '/core/robots_list.txt');
foreach ($robots as $robot_info) {
$robot_info = explode("\t", $robot_info, 3);
if ( $user_agent == trim($robot_info[2]) ) {
$is_spider = true;
break;
}
}
}
return $is_spider;
}*/
/**
* Allows to detect table's presence in database
*
* @param string $table_name
* @param bool $force
* @return bool
* @access public
*/
public function TableFound($table_name, $force = false)
{
return $this->Conn->TableFound($table_name, $force);
}
/**
* Returns counter value
*
* @param string $name counter name
* @param Array $params counter parameters
* @param string $query_name specify query name directly (don't generate from parameters)
* @param bool $multiple_results
* @return mixed
* @access public
*/
public function getCounter($name, $params = Array (), $query_name = null, $multiple_results = false)
{
$count_helper = $this->recallObject('CountHelper');
/* @var $count_helper kCountHelper */
return $count_helper->getCounter($name, $params, $query_name, $multiple_results);
}
/**
* Resets counter, which are affected by one of specified tables
*
* @param string $tables comma separated tables list used in counting sqls
* @return void
* @access public
*/
public function resetCounters($tables)
{
if ( kUtil::constOn('IS_INSTALL') ) {
return;
}
$count_helper = $this->recallObject('CountHelper');
/* @var $count_helper kCountHelper */
$count_helper->resetCounters($tables);
}
/**
* Sends XML header + optionally displays xml heading
*
* @param string|bool $xml_version
* @return string
* @access public
* @author Alex
*/
public function XMLHeader($xml_version = false)
{
$this->setContentType('text/xml');
return $xml_version ? '<?xml version="' . $xml_version . '" encoding="' . CHARSET . '"?>' : '';
}
/**
* Returns category tree
*
* @param int $category_id
* @return Array
* @access public
*/
public function getTreeIndex($category_id)
{
$tree_index = $this->getCategoryCache($category_id, 'category_tree');
if ( $tree_index ) {
$ret = Array ();
list ($ret['TreeLeft'], $ret['TreeRight']) = explode(';', $tree_index);
return $ret;
}
return false;
}
/**
* Base category of all categories
* Usually replaced category, with ID = 0 in category-related operations.
*
* @return int
* @access public
*/
public function getBaseCategory()
{
// same, what $this->findModule('Name', 'Core', 'RootCat') does
// don't cache while IS_INSTALL, because of kInstallToolkit::createModuleCategory and upgrade
return $this->ModuleInfo['Core']['RootCat'];
}
/**
* Deletes all data, that was cached during unit config parsing (excluding unit config locations)
*
* @param Array $config_variables
* @access public
*/
public function DeleteUnitCache($config_variables = null)
{
$this->cacheManager->DeleteUnitCache($config_variables);
}
/**
* Deletes cached section tree, used during permission checking and admin console tree display
*
* @return void
* @access public
*/
public function DeleteSectionCache()
{
$this->cacheManager->DeleteSectionCache();
}
/**
* Sets data from cache to object
*
* @param Array $data
* @access public
*/
public function setFromCache(&$data)
{
$this->Factory->setFromCache($data);
$this->UnitConfigReader->setFromCache($data);
$this->EventManager->setFromCache($data);
$this->ReplacementTemplates = $data['Application.ReplacementTemplates'];
$this->RewriteListeners = $data['Application.RewriteListeners'];
$this->ModuleInfo = $data['Application.ModuleInfo'];
}
/**
* Gets object data for caching
* The following caches should be reset based on admin interaction (adjusting config, enabling modules etc)
*
* @access public
* @return Array
*/
public function getToCache()
{
return array_merge(
$this->Factory->getToCache(),
$this->UnitConfigReader->getToCache(),
$this->EventManager->getToCache(),
Array (
'Application.ReplacementTemplates' => $this->ReplacementTemplates,
'Application.RewriteListeners' => $this->RewriteListeners,
'Application.ModuleInfo' => $this->ModuleInfo,
)
);
}
public function delayUnitProcessing($method, $params)
{
$this->cacheManager->delayUnitProcessing($method, $params);
}
/**
* Returns current maintenance mode state
*
* @param bool $check_ips
* @return int
* @access public
*/
public function getMaintenanceMode($check_ips = true)
{
$exception_ips = defined('MAINTENANCE_MODE_IPS') ? MAINTENANCE_MODE_IPS : '';
$setting_name = $this->isAdmin ? 'MAINTENANCE_MODE_ADMIN' : 'MAINTENANCE_MODE_FRONT';
if ( defined($setting_name) && constant($setting_name) > MaintenanceMode::NONE ) {
$exception_ip = $check_ips ? kUtil::ipMatch($exception_ips) : false;
if ( !$exception_ip ) {
return constant($setting_name);
}
}
return MaintenanceMode::NONE;
}
/**
* Sets content type of the page
*
* @param string $content_type
* @param bool $include_charset
* @return void
* @access public
*/
public function setContentType($content_type = 'text/html', $include_charset = null)
{
static $already_set = false;
if ( $already_set ) {
return;
}
$header = 'Content-type: ' . $content_type;
if ( !isset($include_charset) ) {
$include_charset = $content_type = 'text/html' || $content_type == 'text/plain' || $content_type = 'text/xml';
}
if ( $include_charset ) {
$header .= '; charset=' . CHARSET;
}
$already_set = true;
header($header);
}
/**
* Posts message to event log
*
* @param string $message
* @param int $code
* @param bool $write_now Allows further customization of log record by returning kLog object
* @return bool|int|kLogger
* @access public
*/
public function log($message, $code = null, $write_now = false)
{
$log = $this->_logger->prepare($message, $code)->addSource($this->_logger->createTrace(null, 1));
if ( $write_now ) {
return $log->write();
}
return $log;
}
/**
* Deletes log with given id from database or disk, when database isn't available
*
* @param int $unique_id
* @param int $storage_medium
* @return void
* @access public
* @throws InvalidArgumentException
*/
public function deleteLog($unique_id, $storage_medium = kLogger::LS_AUTOMATIC)
{
$this->_logger->delete($unique_id, $storage_medium);
}
/**
* Returns the client IP address.
*
* @return string The client IP address
* @access public
*/
public function getClientIp()
{
return $this->HttpQuery->getClientIp();
}
}
Index: branches/5.3.x/core/kernel/managers/cache_manager.php
===================================================================
--- branches/5.3.x/core/kernel/managers/cache_manager.php (revision 16155)
+++ branches/5.3.x/core/kernel/managers/cache_manager.php (revision 16156)
@@ -1,852 +1,852 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2011 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class kCacheManager extends kBase implements kiCacheable {
/**
* Used variables from SystemSettings table
*
* @var Array
* @access protected
*/
protected $configVariables = Array();
/**
* Used variables from SystemSettings table retrieved from unit cache
*
* @var Array
* @access protected
*/
protected $originalConfigVariables = Array ();
/**
* IDs of config variables used in current run (for caching)
*
* @var Array
* @access protected
*/
protected $configIDs = Array ();
/**
* IDs of config variables retrieved from unit cache
*
* @var Array
* @access protected
*/
protected $originalConfigIDs = Array ();
/**
* Object of memory caching class
*
* @var kCache
* @access protected
*/
protected $cacheHandler = null;
protected $temporaryCache = Array (
'registerAggregateTag' => Array (),
'registerScheduledTask' => Array (),
'registerHook' => Array (),
'registerBuildEvent' => Array (),
'registerAggregateTag' => Array (),
);
/**
* Name of database table, where configuration settings are stored
*
* @var string
* @access protected
*/
protected $settingTableName = '';
/**
* Set's references to kApplication and DBConnection interface class instances
*
* @access public
*/
public function __construct()
{
parent::__construct();
$this->settingTableName = TABLE_PREFIX . 'SystemSettings';
if ( defined('IS_INSTALL') && IS_INSTALL ) {
// table substitution required, so "root" can perform login to upgrade to 5.2.0, where setting table was renamed
if ( !$this->Application->TableFound(TABLE_PREFIX . 'SystemSettings') ) {
$this->settingTableName = TABLE_PREFIX . 'ConfigurationValues';
}
}
}
/**
* Creates caching manager instance
*
* @access public
*/
public function InitCache()
{
$this->cacheHandler = $this->Application->makeClass('kCache');
}
/**
* Returns cache key, used to cache phrase and configuration variable IDs used on current page
*
* @return string
* @access protected
*/
protected function getCacheKey()
{
// TODO: maybe language part isn't required, since same phrase from different languages have one ID now
return $this->Application->GetVar('t') . $this->Application->GetVar('m_theme') . $this->Application->GetVar('m_lang') . $this->Application->isAdmin;
}
/**
* Loads phrases and configuration variables, that were used on this template last time
*
* @access public
*/
public function LoadApplicationCache()
{
$phrase_ids = $config_ids = Array ();
$sql = 'SELECT PhraseList, ConfigVariables
FROM ' . TABLE_PREFIX . 'PhraseCache
WHERE Template = ' . $this->Conn->qstr( md5($this->getCacheKey()) );
$res = $this->Conn->GetRow($sql);
if ($res) {
if ( $res['PhraseList'] ) {
$phrase_ids = explode(',', $res['PhraseList']);
}
if ( $res['ConfigVariables'] ) {
$config_ids = array_diff( explode(',', $res['ConfigVariables']), $this->originalConfigIDs);
}
}
$this->Application->Phrases->Init('phrases', '', null, $phrase_ids);
$this->configIDs = $this->originalConfigIDs = $config_ids;
$this->InitConfig();
}
/**
* Updates phrases and configuration variables, that were used on this template
*
* @access public
*/
public function UpdateApplicationCache()
{
$update = false;
//something changed
$update = $update || $this->Application->Phrases->NeedsCacheUpdate();
$update = $update || (count($this->configIDs) && $this->configIDs != $this->originalConfigIDs);
if ($update) {
$fields_hash = Array (
'PhraseList' => implode(',', $this->Application->Phrases->Ids),
'CacheDate' => time(),
'Template' => md5( $this->getCacheKey() ),
'ConfigVariables' => implode(',', array_unique($this->configIDs)),
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'PhraseCache', 'REPLACE');
}
}
/**
* Loads configuration variables, that were used on this template last time
*
* @access protected
*/
protected function InitConfig()
{
if (!$this->originalConfigIDs) {
return ;
}
$sql = 'SELECT VariableValue, VariableName
FROM ' . $this->settingTableName . '
WHERE VariableId IN (' . implode(',', $this->originalConfigIDs) . ')';
$config_variables = $this->Conn->GetCol($sql, 'VariableName');
$this->configVariables = array_merge($this->configVariables, $config_variables);
}
/**
* Returns configuration option value by name
*
* @param string $name
* @return string
* @access public
*/
public function ConfigValue($name)
{
$site_domain_override = Array (
'DefaultEmailSender' => 'AdminEmail',
'DefaultEmailRecipients' => 'DefaultEmailRecipients',
);
if ( isset($site_domain_override[$name]) ) {
$res = $this->Application->siteDomainField($site_domain_override[$name]);
if ( $res ) {
return $res;
}
}
if ( array_key_exists($name, $this->configVariables) ) {
return $this->configVariables[$name];
}
if ( defined('IS_INSTALL') && IS_INSTALL && !$this->Application->TableFound($this->settingTableName, true) ) {
return false;
}
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT VariableId, VariableValue
FROM ' . $this->settingTableName . '
WHERE VariableName = ' . $this->Conn->qstr($name);
$res = $this->Conn->GetRow($sql);
if ( $res !== false ) {
$this->configIDs[] = $res['VariableId'];
$this->configVariables[$name] = $res['VariableValue'];
return $res['VariableValue'];
}
trigger_error('Usage of undefined configuration variable "<strong>' . $name . '</strong>"', E_USER_NOTICE);
return false;
}
/**
* Changes value of individual configuration variable (+resets cache, when needed)
*
* @param string $name
* @param string $value
* @param bool $local_cache_only
* @return string
* @access public
*/
public function SetConfigValue($name, $value, $local_cache_only = false)
{
$this->configVariables[$name] = $value;
if ( $local_cache_only ) {
return;
}
$fields_hash = Array ('VariableValue' => $value);
$this->Conn->doUpdate($fields_hash, $this->settingTableName, 'VariableName = ' . $this->Conn->qstr($name));
if ( array_key_exists($name, $this->originalConfigVariables) && $value != $this->originalConfigVariables[$name] ) {
$this->DeleteUnitCache();
}
}
/**
* Loads data, that was cached during unit config parsing
*
* @return bool
* @access public
*/
public function LoadUnitCache()
{
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$data = $this->Application->getCache('master:configs_parsed', false, CacheSettings::$unitCacheRebuildTime);
}
else {
$data = $this->Application->getDBCache('configs_parsed', CacheSettings::$unitCacheRebuildTime);
}
if ( $data ) {
$cache = unserialize($data); // 126 KB all modules
unset($data);
$this->Application->InitManagers();
$this->Application->setFromCache($cache);
$aggregator = $this->Application->recallObject('TagsAggregator', 'kArray');
/* @var $aggregator kArray */
$aggregator->setFromCache($cache);
$this->setFromCache($cache);
unset($cache);
return true;
}
return false;
}
/**
* Empties factory and event manager cache (without storing changes)
*/
public function EmptyUnitCache()
{
// maybe discover keys automatically from corresponding classes
$cache_keys = Array (
- 'Factory.Files', 'Factory.realClasses',
+ 'Factory.Files', 'Factory.Namespaces', 'Factory.realClasses',
'ConfigReader.prefixFiles', 'ConfigCloner.clones',
'EventManager.beforeHooks', 'EventManager.afterHooks', 'EventManager.scheduledTasks', 'EventManager.buildEvents',
'Application.ReplacementTemplates', 'Application.RewriteListeners', 'Application.ModuleInfo',
'Application.ConfigHash', 'Application.ConfigCacheIds',
);
$empty_cache = Array ();
foreach ($cache_keys as $cache_key) {
$empty_cache[$cache_key] = Array ();
}
$this->Application->setFromCache($empty_cache);
$this->setFromCache($empty_cache);
- // otherwise ModulesHelper indirectly used from includeConfigFiles won't work
+ // Otherwise kModulesHelper indirectly used from includeConfigFiles won't work.
$this->Application->RegisterDefaultClasses();
}
/**
* Updates data, that was parsed from unit configs this time
*
* @access public
*/
public function UpdateUnitCache()
{
$aggregator = $this->Application->recallObject('TagsAggregator', 'kArray');
/* @var $aggregator kArray */
$this->preloadConfigVars(); // preloading will put to cache
$cache = array_merge(
$this->Application->getToCache(),
$aggregator->getToCache(),
$this->getToCache()
);
$cache_rebuild_by = SERVER_NAME . ' (' . $this->Application->getClientIp() . ') - ' . date('d/m/Y H:i:s');
if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) {
$this->Application->setCache('master:configs_parsed', serialize($cache));
$this->Application->setCache('master:last_cache_rebuild', $cache_rebuild_by);
}
else {
$this->Application->setDBCache('configs_parsed', serialize($cache));
$this->Application->setDBCache('last_cache_rebuild', $cache_rebuild_by);
}
}
public function delayUnitProcessing($method, $params)
{
if ($this->Application->InitDone) {
// init already done -> call immediately (happens during installation)
$function = Array (&$this->Application, $method);
call_user_func_array($function, $params);
return ;
}
$this->temporaryCache[$method][] = $params;
}
public function applyDelayedUnitProcessing()
{
foreach ($this->temporaryCache as $method => $method_calls) {
$function = Array (&$this->Application, $method);
foreach ($method_calls as $method_call) {
call_user_func_array($function, $method_call);
}
$this->temporaryCache[$method] = Array ();
}
}
/**
* Deletes all data, that was cached during unit config parsing (excluding unit config locations)
*
* @param Array $config_variables
* @access public
*/
public function DeleteUnitCache($config_variables = null)
{
if ( isset($config_variables) && !array_intersect(array_keys($this->originalConfigVariables), $config_variables) ) {
// prevent cache reset, when given config variables are not in unit cache
return;
}
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->rebuildCache('master:configs_parsed', kCache::REBUILD_LATER, CacheSettings::$unitCacheRebuildTime);
}
else {
$this->rebuildDBCache('configs_parsed', kCache::REBUILD_LATER, CacheSettings::$unitCacheRebuildTime);
}
}
/**
* Deletes cached section tree, used during permission checking and admin console tree display
*
* @return void
* @access public
*/
public function DeleteSectionCache()
{
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->rebuildCache('master:sections_parsed', kCache::REBUILD_LATER, CacheSettings::$sectionsParsedRebuildTime);
}
else {
$this->rebuildDBCache('sections_parsed', kCache::REBUILD_LATER, CacheSettings::$sectionsParsedRebuildTime);
}
}
/**
* Preloads 21 widely used configuration variables, so they will get to cache for sure
*
* @access protected
*/
protected function preloadConfigVars()
{
$config_vars = Array (
// session related
'SessionTimeout', 'SessionCookieName', 'SessionCookieDomains', 'SessionBrowserSignatureCheck',
'SessionIPAddressCheck', 'CookieSessions', 'KeepSessionOnBrowserClose', 'User_GuestGroup',
'User_LoggedInGroup', 'RegistrationUsernameRequired',
// output related
'UseModRewrite', 'UseContentLanguageNegotiation', 'UseOutputCompression', 'OutputCompressionLevel',
'Config_Site_Time', 'SystemTagCache', 'DefaultGridPerPage',
// tracking related
'UseChangeLog', 'UseVisitorTracking', 'ModRewriteUrlEnding', 'ForceModRewriteUrlEnding',
'RunScheduledTasksFromCron',
);
$escaped_config_vars = $this->Conn->qstrArray($config_vars);
$sql = 'SELECT VariableId, VariableName, VariableValue
FROM ' . $this->settingTableName . '
WHERE VariableName IN (' . implode(',', $escaped_config_vars) . ')';
$data = $this->Conn->Query($sql, 'VariableId');
foreach ($data as $variable_id => $variable_info) {
$this->configIDs[] = $variable_id;
$this->configVariables[ $variable_info['VariableName'] ] = $variable_info['VariableValue'];
}
}
/**
* Sets data from cache to object
*
* Used for cases, when ConfigValue is called before LoadApplicationCache method (e.g. session init, url engine init)
*
* @param Array $data
* @access public
*/
public function setFromCache(&$data)
{
$this->configVariables = $this->originalConfigVariables = $data['Application.ConfigHash'];
$this->configIDs = $this->originalConfigIDs = $data['Application.ConfigCacheIds'];
}
/**
* Gets object data for caching
* The following caches should be reset based on admin interaction (adjusting config, enabling modules etc)
*
* @access public
* @return Array
*/
public function getToCache()
{
return Array (
'Application.ConfigHash' => $this->configVariables,
'Application.ConfigCacheIds' => $this->configIDs,
// not in use, since it only represents template specific values, not global ones
// 'Application.Caches.ConfigVariables' => $this->originalConfigIDs,
);
}
/**
* Returns caching type (none, memory, temporary)
*
* @param int $caching_type
* @return bool
* @access public
*/
public function isCachingType($caching_type)
{
return $this->cacheHandler->getCachingType() == $caching_type;
}
/**
* Returns cached $key value from cache named $cache_name
*
* @param int $key key name from cache
* @param bool $store_locally store data locally after retrieved
* @param int $max_rebuild_seconds
* @return mixed
* @access public
*/
public function getCache($key, $store_locally = true, $max_rebuild_seconds = 0)
{
return $this->cacheHandler->getCache($key, $store_locally, $max_rebuild_seconds);
}
/**
* Stores new $value in cache with $key name
*
* @param int $key key name to add to cache
* @param mixed $value value of cached record
* @param int $expiration when value expires (0 - doesn't expire)
* @return bool
* @access public
*/
public function setCache($key, $value, $expiration = 0)
{
return $this->cacheHandler->setCache($key, $value, $expiration);
}
/**
* Stores new $value in cache with $key name (only if not there already)
*
* @param int $key key name to add to cache
* @param mixed $value value of cached record
* @param int $expiration when value expires (0 - doesn't expire)
* @return bool
* @access public
*/
public function addCache($key, $value, $expiration = 0)
{
return $this->cacheHandler->addCache($key, $value, $expiration);
}
/**
* Sets rebuilding mode for given cache
*
* @param string $name
* @param int $mode
* @param int $max_rebuilding_time
* @return bool
* @access public
*/
public function rebuildCache($name, $mode = null, $max_rebuilding_time = 0)
{
return $this->cacheHandler->rebuildCache($name, $mode, $max_rebuilding_time);
}
/**
* Deletes key from cache
*
* @param string $key
* @return void
* @access public
*/
public function deleteCache($key)
{
$this->cacheHandler->delete($key);
}
/**
* Reset's all memory cache at once
*
* @return void
* @access public
*/
public function resetCache()
{
$this->cacheHandler->reset();
}
/**
* Returns value from database cache
*
* @param string $name key name
* @param int $max_rebuild_seconds
* @return mixed
* @access public
*/
public function getDBCache($name, $max_rebuild_seconds = 0)
{
// no serials in cache key OR cache is outdated
$rebuilding = false;
$wait_seconds = $max_rebuild_seconds;
while (true) {
$cached_data = $this->_getDBCache(Array ($name, $name . '_rebuilding', $name . '_rebuild'));
if ( $cached_data[$name . '_rebuild'] ) {
// cache rebuild requested -> rebuild now
$this->deleteDBCache($name . '_rebuild');
if ( $this->rebuildDBCache($name, kCache::REBUILD_NOW, $max_rebuild_seconds, '[M1]') ) {
return false;
}
}
$cache = $cached_data[$name];
$rebuilding = $cached_data[$name . '_rebuilding'];
if ( ($cache === false) && (!$rebuilding || $wait_seconds == 0) ) {
// cache missing and nobody rebuilding it -> rebuild; enough waiting for cache to be ready
$this->rebuildDBCache($name, kCache::REBUILD_NOW, $max_rebuild_seconds, '[M2' . ($rebuilding ? 'R' : '!R') . ',WS=' . $wait_seconds . ']');
return false;
}
elseif ( $cache !== false ) {
// cache present -> return it
$this->cacheHandler->storeStatistics($name, $rebuilding ? 'h' : 'H');
return $cache;
}
$wait_seconds -= kCache::WAIT_STEP;
sleep(kCache::WAIT_STEP);
}
$this->rebuildDBCache($name, kCache::REBUILD_NOW, $max_rebuild_seconds, '[M3' . ($rebuilding ? 'R' : '!R') . ',WS=' . $wait_seconds . ']');
return false;
}
/**
* Returns value from database cache
*
* @param string|Array $names key name
* @return mixed
* @access protected
*/
protected function _getDBCache($names)
{
$res = Array ();
$names = (array)$names;
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT Data, Cached, LifeTime, VarName
FROM ' . TABLE_PREFIX . 'SystemCache
WHERE VarName IN (' . implode(',', $this->Conn->qstrArray($names)) . ')';
$cached_data = $this->Conn->Query($sql, 'VarName');
foreach ($names as $name) {
if ( !isset($cached_data[$name]) ) {
$res[$name] = false;
continue;
}
$lifetime = (int)$cached_data[$name]['LifeTime']; // in seconds
if ( ($lifetime > 0) && ($cached_data[$name]['Cached'] + $lifetime < time()) ) {
// delete expired
$this->Conn->nextQueryCachable = true;
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'SystemCache
WHERE VarName = ' . $this->Conn->qstr($name);
$this->Conn->Query($sql);
$res[$name] = false;
continue;
}
$res[$name] = $cached_data[$name]['Data'];
}
return count($res) == 1 ? array_pop($res) : $res;
}
/**
* Sets value to database cache
*
* @param string $name
* @param mixed $value
* @param int|bool $expiration
* @return void
* @access public
*/
public function setDBCache($name, $value, $expiration = false)
{
$this->cacheHandler->storeStatistics($name, 'WU');
$this->deleteDBCache($name . '_rebuilding');
$this->_setDBCache($name, $value, $expiration);
}
/**
* Sets value to database cache
*
* @param string $name
* @param mixed $value
* @param int|bool $expiration
* @param string $insert_type
* @return bool
* @access protected
*/
protected function _setDBCache($name, $value, $expiration = false, $insert_type = 'REPLACE')
{
if ( (int)$expiration <= 0 ) {
$expiration = -1;
}
$fields_hash = Array (
'VarName' => $name,
'Data' => &$value,
'Cached' => time(),
'LifeTime' => (int)$expiration,
);
$this->Conn->nextQueryCachable = true;
return $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'SystemCache', $insert_type);
}
/**
* Sets value to database cache
*
* @param string $name
* @param mixed $value
* @param int|bool $expiration
* @return bool
* @access protected
*/
protected function _addDBCache($name, $value, $expiration = false)
{
return $this->_setDBCache($name, $value, $expiration, 'INSERT');
}
/**
* Sets rebuilding mode for given cache
*
* @param string $name
* @param int $mode
* @param int $max_rebuilding_time
* @param string $miss_type
* @return bool
* @access public
*/
public function rebuildDBCache($name, $mode = null, $max_rebuilding_time = 0, $miss_type = 'M')
{
if ( !isset($mode) || $mode == kCache::REBUILD_NOW ) {
$this->cacheHandler->storeStatistics($name, $miss_type);
if ( !$max_rebuilding_time ) {
return true;
}
if ( !$this->_addDBCache($name . '_rebuilding', 1, $max_rebuilding_time) ) {
$this->cacheHandler->storeStatistics($name, 'l');
return false;
}
$this->deleteDBCache($name . '_rebuild');
$this->cacheHandler->storeStatistics($name, 'L');
}
elseif ( $mode == kCache::REBUILD_LATER ) {
$this->_setDBCache($name . '_rebuild', 1, 0);
$this->deleteDBCache($name . '_rebuilding');
}
return true;
}
/**
* Deletes key from database cache
*
* @param string $name
* @return void
* @access public
*/
public function deleteDBCache($name)
{
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'SystemCache
WHERE VarName = ' . $this->Conn->qstr($name);
$this->Conn->Query($sql);
}
/**
* Increments serial based on prefix and it's ID (optional)
*
* @param string $prefix
* @param int $id ID (value of IDField) or ForeignKeyField:ID
* @param bool $increment
* @return string
* @access public
*/
public function incrementCacheSerial($prefix, $id = null, $increment = true)
{
$pascal_case_prefix = implode('', array_map('ucfirst', explode('-', $prefix)));
$serial_name = $pascal_case_prefix . (isset($id) ? 'IDSerial:' . $id : 'Serial');
if ($increment) {
if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Incrementing serial: <strong>' . $serial_name . '</strong>.');
}
$this->setCache($serial_name, (int)$this->getCache($serial_name) + 1);
if (!defined('IS_INSTALL') || !IS_INSTALL) {
// delete cached mod-rewrite urls related to given prefix and id
$delete_clause = isset($id) ? $prefix . ':' . $id : $prefix;
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'CachedUrls
WHERE Prefixes LIKE ' . $this->Conn->qstr('%|' . $delete_clause . '|%');
$this->Conn->Query($sql);
}
}
return $serial_name;
}
/**
* Returns cached category informaton by given cache name. All given category
* information is recached, when at least one of 4 caches is missing.
*
* @param int $category_id
* @param string $name cache name = {filenames, category_designs, category_tree}
* @return string
* @access public
*/
public function getCategoryCache($category_id, $name)
{
$serial_name = '[%CIDSerial:' . $category_id . '%]';
$cache_key = $name . $serial_name;
$ret = $this->getCache($cache_key);
if ($ret === false) {
if (!$category_id) {
// don't query database for "Home" category (ID = 0), because it doesn't exist in database
return false;
}
// this allows to save 2 sql queries for each category
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT NamedParentPath, CachedTemplate, TreeLeft, TreeRight
FROM ' . TABLE_PREFIX . 'Categories
WHERE CategoryId = ' . (int)$category_id;
$category_data = $this->Conn->GetRow($sql);
if ($category_data !== false) {
// only direct links to category pages work (symlinks, container pages and so on won't work)
$this->setCache('filenames' . $serial_name, $category_data['NamedParentPath']);
$this->setCache('category_designs' . $serial_name, ltrim($category_data['CachedTemplate'], '/'));
$this->setCache('category_tree' . $serial_name, $category_data['TreeLeft'] . ';' . $category_data['TreeRight']);
}
}
return $this->getCache($cache_key);
}
-}
\ No newline at end of file
+}
Index: branches/5.3.x/core/kernel/managers/rewrite_url_processor.php
===================================================================
--- branches/5.3.x/core/kernel/managers/rewrite_url_processor.php (revision 16155)
+++ branches/5.3.x/core/kernel/managers/rewrite_url_processor.php (revision 16156)
@@ -1,1080 +1,1080 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2011 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class kRewriteUrlProcessor extends kUrlProcessor {
/**
* Holds a reference to httpquery
*
* @var kHttpQuery
* @access protected
*/
protected $HTTPQuery = null;
/**
* Urls parts, that needs to be matched by rewrite listeners
*
* @var Array
* @access protected
*/
protected $_partsToParse = Array ();
/**
* Category item prefix, that was found
*
* @var string|boolean
* @access protected
*/
protected $modulePrefix = false;
/**
* Template aliases for current theme
*
* @var Array
* @access protected
*/
protected $_templateAliases = null;
/**
* Domain-based primary language id
*
* @var int
* @access public
*/
public $primaryLanguageId = false;
/**
* Domain-based primary theme id
*
* @var int
* @access public
*/
public $primaryThemeId = false;
/**
* Possible url endings from ModRewriteUrlEnding configuration variable
*
* @var Array
* @access protected
*/
protected $_urlEndings = Array ('.html', '/', '');
/**
* Factory storage sub-set, containing mod-rewrite listeners, used during url building and parsing
*
* @var Array
* @access protected
*/
protected $rewriteListeners = Array ();
/**
* Constructor of kRewriteUrlProcessor class
*
* @param $manager
* @return kRewriteUrlProcessor
*/
public function __construct(&$manager)
{
parent::__construct($manager);
- $this->HTTPQuery = $this->Application->recallObject('HTTPQuery');
+ $this->HTTPQuery = $this->Application->recallObject('kHTTPQuery');
// domain based primary language
$this->primaryLanguageId = $this->Application->siteDomainField('PrimaryLanguageId');
if (!$this->primaryLanguageId) {
// when domain-based language not found -> use site-wide language
$this->primaryLanguageId = $this->Application->GetDefaultLanguageId();
}
// domain based primary theme
$this->primaryThemeId = $this->Application->siteDomainField('PrimaryThemeId');
if (!$this->primaryThemeId) {
// when domain-based theme not found -> use site-wide theme
$this->primaryThemeId = $this->Application->GetDefaultThemeId(true);
}
$this->_initRewriteListeners();
}
/**
* Sets module prefix.
*
* @param string $prefix Unit config prefix.
*
* @return void
*/
public function setModulePrefix($prefix)
{
$this->modulePrefix = $prefix;
}
/**
* Parses url
*
* @return void
*/
public function parseRewriteURL()
{
$url = $this->Application->GetVar('_mod_rw_url_');
if ( $url ) {
$this->_redirectToDefaultUrlEnding($url);
$url = $this->_removeUrlEnding($url);
}
$cached = $this->_getCachedUrl($url);
if ( $cached !== false ) {
$vars = $cached['vars'];
$passed = $cached['passed'];
}
else {
$vars = $this->parse($url);
$passed = $vars['pass']; // also used in bottom of this method
unset($vars['pass']);
if ( !$this->_partsToParse ) {
// don't cache 404 Not Found
$this->_setCachedUrl($url, Array ('vars' => $vars, 'passed' => $passed));
}
if ( $this->Application->GetVarDirect('t', 'Post') ) {
// template from POST overrides template from URL.
$vars['t'] = $this->Application->GetVarDirect('t', 'Post');
if ( isset($vars['is_virtual']) && $vars['is_virtual'] ) {
$vars['m_cat_id'] = 0; // this is virtual template category (for Proj-CMS)
}
}
unset($vars['is_virtual']);
}
foreach ($vars as $name => $value) {
$this->HTTPQuery->Set($name, $value);
}
$this->_initAll(); // also will use parsed language to load phrases from it
$this->HTTPQuery->finalizeParsing($passed);
}
/**
* Detects url ending of given url
*
* @param string $url
* @return string
* @access protected
*/
protected function _findUrlEnding($url)
{
if ( !$url ) {
return '';
}
foreach ($this->_urlEndings as $url_ending) {
if ( mb_substr($url, mb_strlen($url) - mb_strlen($url_ending)) == $url_ending ) {
return $url_ending;
}
}
return '';
}
/**
* Removes url ending from url
*
* @param string $url
* @return string
* @access protected
*/
protected function _removeUrlEnding($url)
{
$url_ending = $this->_findUrlEnding($url);
if ( !$url_ending ) {
return $url;
}
return mb_substr($url, 0, mb_strlen($url) - mb_strlen($url_ending));
}
/**
* Redirects user to page with default url ending, where needed
*
* @param string $url
* @return void
* @access protected
*/
protected function _redirectToDefaultUrlEnding($url)
{
$default_ending = $this->Application->ConfigValue('ModRewriteUrlEnding');
if ( $this->_findUrlEnding($url) == $default_ending || !$this->Application->ConfigValue('ForceModRewriteUrlEnding') ) {
return;
}
// user manually typed url with different url ending -> redirect to same url with default url ending
$target_url = $this->Application->BaseURL() . $this->_removeUrlEnding($url) . $default_ending;
trigger_error('Mod-rewrite url "<strong>' . $_SERVER['REQUEST_URI'] . '</strong>" without "<strong>' . $default_ending . '</strong>" line ending used', E_USER_NOTICE);
$this->Application->Redirect('external:' . $target_url, Array ('response_code' => 301));
}
/**
* Returns url parsing result from cache or false, when not yet parsed
*
* @param $url
* @return Array|bool
* @access protected
*/
protected function _getCachedUrl($url)
{
if ( !$url || (defined('DBG_CACHE_URLS') && !DBG_CACHE_URLS) ) {
return false;
}
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'CachedUrls
WHERE Hash = ' . kUtil::crc32($url) . ' AND DomainId = ' . (int)$this->Application->siteDomainField('DomainId');
$data = $this->Conn->GetRow($sql);
if ( $data ) {
$lifetime = (int)$data['LifeTime']; // in seconds
if ( ($lifetime > 0) && ($data['Cached'] + $lifetime < TIMENOW) ) {
// delete expired
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'CachedUrls
WHERE UrlId = ' . $data['UrlId'];
$this->Conn->Query($sql);
return false;
}
return unserialize($data['ParsedVars']);
}
return false;
}
/**
* Caches url
*
* @param string $url
* @param Array $data
* @return void
* @access protected
*/
protected function _setCachedUrl($url, $data)
{
if ( !$url || (defined('DBG_CACHE_URLS') && !DBG_CACHE_URLS) ) {
return;
}
$vars = $data['vars'];
$passed = $data['passed'];
sort($passed);
// get expiration
if ( $vars['m_cat_id'] > 0 ) {
$sql = 'SELECT PageExpiration
FROM ' . TABLE_PREFIX . 'Categories
WHERE CategoryId = ' . $vars['m_cat_id'];
$expiration = $this->Conn->GetOne($sql);
}
// get prefixes
$prefixes = Array ();
$m_index = array_search('m', $passed);
if ( $m_index !== false ) {
unset($passed[$m_index]);
if ( $vars['m_cat_id'] > 0 ) {
$prefixes[] = 'c:' . $vars['m_cat_id'];
}
$prefixes[] = 'lang:' . $vars['m_lang'];
$prefixes[] = 'theme:' . $vars['m_theme'];
}
foreach ($passed as $prefix) {
if ( array_key_exists($prefix . '_id', $vars) && is_numeric($vars[$prefix . '_id']) ) {
$prefixes[] = $prefix . ':' . $vars[$prefix . '_id'];
}
else {
$prefixes[] = $prefix;
}
}
$fields_hash = Array (
'Url' => $url,
'Hash' => kUtil::crc32($url),
'DomainId' => (int)$this->Application->siteDomainField('DomainId'),
'Prefixes' => $prefixes ? '|' . implode('|', $prefixes) . '|' : '',
'ParsedVars' => serialize($data),
'Cached' => time(),
'LifeTime' => isset($expiration) && is_numeric($expiration) ? $expiration : -1
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'CachedUrls');
}
/**
* Loads all registered rewrite listeners, so they could be quickly accessed later
*
* @access protected
*/
protected function _initRewriteListeners()
{
static $init_done = false;
if ($init_done || count($this->Application->RewriteListeners) == 0) {
// not initialized OR mod-rewrite url with missing config cache
return ;
}
foreach ($this->Application->RewriteListeners as $prefix => $listener_data) {
foreach ($listener_data['listener'] as $index => $rewrite_listener) {
list ($listener_prefix, $listener_method) = explode(':', $rewrite_listener);
// don't use temp variable, since it will swap objects in Factory in PHP5
$this->rewriteListeners[$prefix][$index] = Array ();
$this->rewriteListeners[$prefix][$index][0] = $this->Application->recallObject($listener_prefix);
$this->rewriteListeners[$prefix][$index][1] = $listener_method;
}
}
define('MOD_REWRITE_URL_ENDING', $this->Application->ConfigValue('ModRewriteUrlEnding'));
$init_done = true;
}
/**
* Parses given string into a set of variables (url in this case)
*
* @param string $string
* @param string $pass_name
* @return Array
* @access public
*/
public function parse($string, $pass_name = 'pass')
{
// external url (could be back this website as well)
if ( preg_match('/external:(.*)/', $string, $regs) ) {
$string = $regs[1];
}
$vars = Array ();
$url_components = parse_url($string);
if ( isset($url_components['query']) ) {
parse_str(html_entity_decode($url_components['query']), $url_params);
if ( isset($url_params[ENV_VAR_NAME]) ) {
$url_params = array_merge($url_params, $this->manager->plain->parse($url_params[ENV_VAR_NAME], $pass_name));
unset($url_params[ENV_VAR_NAME]);
}
$vars = array_merge($vars, $url_params);
}
$this->_fixPass($vars, $pass_name);
if ( isset($url_components['path']) ) {
if ( BASE_PATH ) {
$string = preg_replace('/^' . preg_quote(BASE_PATH, '/') . '/', '', $url_components['path'], 1);
}
else {
$string = $url_components['path'];
}
$string = $this->_removeUrlEnding(trim($string, '/'));
}
else {
$string = '';
}
$url_parts = $string ? explode('/', mb_strtolower($string)) : Array ();
$this->setModulePrefix(false);
$this->_partsToParse = $url_parts;
if ( ($this->HTTPQuery->Get('rewrite') == 'on') || !$url_parts ) {
$this->_setDefaultValues($vars);
}
if ( !$url_parts ) {
$this->_initAll();
$vars['t'] = $this->Application->UrlManager->getTemplateName();
return $vars;
}
$this->_parseLanguage($url_parts, $vars);
$this->_parseTheme($url_parts, $vars);
// http://site-url/<language>/<theme>/<category>[_<category_page>]/<template>/<module_page>
// http://site-url/<language>/<theme>/<category>[_<category_page>]/<module_page> (category-based section template)
// http://site-url/<language>/<theme>/<category>[_<category_page>]/<template>/<module_item>
// http://site-url/<language>/<theme>/<category>[_<category_page>]/<module_item> (category-based detail template)
// http://site-url/<language>/<theme>/<rl_injections>/<category>[_<category_page>]/<rl_part> (customized url)
if ( !$this->_processRewriteListeners($url_parts, $vars) ) {
// rewrite listener wasn't able to determine template
$this->_parsePhysicalTemplate($url_parts, $vars);
if ( ($this->modulePrefix === false) && $vars['m_cat_id'] && !$this->_partsToParse ) {
// no category item found, but category found and all url matched -> module index page
return $vars;
}
}
if ( $this->_partsToParse ) {
$vars = array_merge($vars, $this->manager->prepare404($vars['m_theme']));
}
return $vars;
}
/**
* Ensures, that "m" is always in "pass" variable
*
* @param Array $vars
* @param string $pass_name
* @return void
* @access protected
*/
protected function _fixPass(&$vars, $pass_name)
{
if ( isset($vars[$pass_name]) ) {
$vars[$pass_name] = array_unique(explode(',', 'm,' . $vars[$pass_name]));
}
else {
$vars[$pass_name] = Array ('m');
}
}
/**
* Initializes theme & language based on parse results
*
* @return void
* @access protected
*/
protected function _initAll()
{
$this->Application->VerifyThemeId();
$this->Application->VerifyLanguageId();
- // no need, since we don't have any cached phrase IDs + nobody will use PhrasesCache::LanguageId soon
+ // No need, since we don't have any cached phrase IDs + nobody will use kPhraseCache::LanguageId soon.
// $this->Application->Phrases->Init('phrases');
}
/**
* Sets default parsed values before actual url parsing (only, for empty url)
*
* @param Array $vars
* @access protected
*/
protected function _setDefaultValues(&$vars)
{
$defaults = Array (
'm_cat_id' => 0, // no category
'm_cat_page' => 1, // first category page
'm_opener' => 's', // stay on same page
't' => 'index' // main site page
);
if ($this->primaryLanguageId) {
// domain-based primary language
$defaults['m_lang'] = $this->primaryLanguageId;
}
if ($this->primaryThemeId) {
// domain-based primary theme
$defaults['m_theme'] = $this->primaryThemeId;
}
foreach ($defaults as $default_key => $default_value) {
if ($this->HTTPQuery->Get($default_key) === false) {
$vars[$default_key] = $default_value;
}
}
}
/**
* Processes url using rewrite listeners
*
* Pattern: Chain of Command
*
* @param Array $url_parts
* @param Array $vars
* @return bool
* @access protected
*/
protected function _processRewriteListeners(&$url_parts, &$vars)
{
$this->_initRewriteListeners();
$page_number = $this->_parsePage($url_parts, $vars);
foreach ($this->rewriteListeners as $prefix => $listeners) {
// set default page
// $vars[$prefix . '_Page'] = 1; // will override page in session in case, when none is given in url
if ($page_number) {
// page given in url - use it
$vars[$prefix . '_id'] = 0;
$vars[$prefix . '_Page'] = $page_number;
}
// $listeners[1] - listener, used for parsing
$listener_result = $listeners[1][0]->$listeners[1][1](REWRITE_MODE_PARSE, $prefix, $vars, $url_parts);
if ($listener_result === false) {
// will not proceed to other methods
return true;
}
}
// will proceed to other methods
return false;
}
/**
* Set's page (when found) to all modules
*
* @param Array $url_parts
* @param Array $vars
* @return string
* @access protected
*
* @todo Should find a way, how to determine what rewrite listener page is it
*/
protected function _parsePage(&$url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
$page_number = end($url_parts);
if (!is_numeric($page_number)) {
return false;
}
array_pop($url_parts);
$this->partParsed($page_number, 'rtl');
return $page_number;
}
/**
* Gets language part from url
*
* @param Array $url_parts
* @param Array $vars
* @return bool
* @access protected
*/
protected function _parseLanguage(&$url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
$url_part = reset($url_parts);
$sql = 'SELECT LanguageId, IF(LOWER(PackName) = ' . $this->Conn->qstr($url_part) . ', 2, PrimaryLang) AS SortKey
FROM ' . TABLE_PREFIX . 'Languages
WHERE Enabled = 1
ORDER BY SortKey DESC';
$language_info = $this->Conn->GetRow($sql);
if ($language_info && $language_info['LanguageId'] && $language_info['SortKey']) {
// primary language will be selected in case, when $url_part doesn't match to other's language pack name
// don't use next enabled language, when primary language is disabled
$vars['m_lang'] = $language_info['LanguageId'];
if ($language_info['SortKey'] == 2) {
// language was found by pack name
array_shift($url_parts);
$this->partParsed($url_part);
}
elseif ($this->primaryLanguageId) {
// use domain-based primary language instead of site-wide primary language
$vars['m_lang'] = $this->primaryLanguageId;
}
return true;
}
return false;
}
/**
* Gets theme part from url
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
protected function _parseTheme(&$url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
$url_part = reset($url_parts);
$sql = 'SELECT ThemeId, IF(LOWER(Name) = ' . $this->Conn->qstr($url_part) . ', 2, PrimaryTheme) AS SortKey, TemplateAliases
FROM ' . TABLE_PREFIX . 'Themes
WHERE Enabled = 1
ORDER BY SortKey DESC';
$theme_info = $this->Conn->GetRow($sql);
if ($theme_info && $theme_info['ThemeId'] && $theme_info['SortKey']) {
// primary theme will be selected in case, when $url_part doesn't match to other's theme name
// don't use next enabled theme, when primary theme is disabled
$vars['m_theme'] = $theme_info['ThemeId'];
if ($theme_info['TemplateAliases']) {
$this->_templateAliases = unserialize($theme_info['TemplateAliases']);
}
else {
$this->_templateAliases = Array ();
}
if ($theme_info['SortKey'] == 2) {
// theme was found by name
array_shift($url_parts);
$this->partParsed($url_part);
}
elseif ($this->primaryThemeId) {
// use domain-based primary theme instead of site-wide primary theme
$vars['m_theme'] = $this->primaryThemeId;
}
return true;
}
$vars['m_theme'] = 0; // required, because used later for category/template detection
return false;
}
/**
* Parses real template name from url
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
protected function _parsePhysicalTemplate($url_parts, &$vars)
{
if ( !$url_parts ) {
return false;
}
$themes_helper = $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
do {
$index_added = false;
$template_path = implode('/', $url_parts);
$template_found = $themes_helper->getTemplateId($template_path, $vars['m_theme']);
if ( !$template_found ) {
$index_added = true;
$template_found = $themes_helper->getTemplateId($template_path . '/index', $vars['m_theme']);
}
if ( !$template_found ) {
array_shift($url_parts);
}
} while ( !$template_found && $url_parts );
if ( $template_found ) {
$template_parts = explode('/', $template_path);
$vars['t'] = $template_path . ($index_added ? '/index' : '');
while ( $template_parts ) {
$this->partParsed(array_pop($template_parts), 'rtl');
}
// 1. will damage actual category during category item review add process
// 2. will use "use_section" parameter of "m_Link" tag to gain same effect
// $vars['m_cat_id'] = $themes_helper->getPageByTemplate($template_path, $vars['m_theme']);
return true;
}
return false;
}
/**
* Returns environment variable values for given prefix (uses directly given params, when available)
*
* @param string $prefix_special
* @param Array $params
* @param bool $keep_events
* @return Array
* @access public
*/
public function getProcessedParams($prefix_special, &$params, $keep_events)
{
list ($prefix) = explode('.', $prefix_special);
$query_vars = $this->Application->getUnitConfig($prefix)->getQueryString(Array ());
if ( !$query_vars ) {
// given prefix doesn't use "env" variable to pass it's data
return false;
}
$event_key = array_search('event', $query_vars);
if ( $event_key ) {
// pass through event of this prefix
unset($query_vars[$event_key]);
}
if ( array_key_exists($prefix_special . '_event', $params) && !$params[$prefix_special . '_event'] ) {
// if empty event, then remove it from url
unset($params[$prefix_special . '_event']);
}
// if pass events is off and event is not implicity passed
if ( !$keep_events && !array_key_exists($prefix_special . '_event', $params) ) {
unset($params[$prefix_special . '_event']); // remove event from url if requested
//otherwise it will use value from get_var
}
$processed_params = Array ();
foreach ($query_vars as $var_name) {
// if value passed in params use it, otherwise use current from application
$var_name = $prefix_special . '_' . $var_name;
$processed_params[$var_name] = array_key_exists($var_name, $params) ? $params[$var_name] : $this->Application->GetVar($var_name);
if ( array_key_exists($var_name, $params) ) {
unset($params[$var_name]);
}
}
return $processed_params;
}
/**
* Returns module item details template specified in given category custom field for given module prefix
*
* @param int|Array $category
* @param string $module_prefix
* @param int $theme_id
* @return string
* @access public
* @todo Move to kPlainUrlProcessor
*/
public function GetItemTemplate($category, $module_prefix, $theme_id = null)
{
if ( !isset($theme_id) ) {
$theme_id = $this->Application->GetVar('m_theme');
}
$category_id = is_array($category) ? $category['CategoryId'] : $category;
$cache_key = __CLASS__ . '::' . __FUNCTION__ . '[%CIDSerial:' . $category_id . '%][%ThemeIDSerial:' . $theme_id . '%]' . $module_prefix;
$cached_value = $this->Application->getCache($cache_key);
if ( $cached_value !== false ) {
return $cached_value;
}
if ( !is_array($category) ) {
if ( $category == 0 ) {
$category = $this->Application->findModule('Var', $module_prefix, 'RootCat');
}
$sql = 'SELECT c.ParentPath, c.CategoryId
FROM ' . TABLE_PREFIX . 'Categories AS c
WHERE c.CategoryId = ' . $category;
$category = $this->Conn->GetRow($sql);
}
$parent_path = implode(',', explode('|', substr($category['ParentPath'], 1, -1)));
// item template is stored in module' system custom field - need to get that field Id
$primary_lang = $this->Application->GetDefaultLanguageId();
$item_template_field_id = $this->getItemTemplateCustomField($module_prefix);
// looking for item template through cats hierarchy sorted by parent path
$query = ' SELECT ccd.l' . $primary_lang . '_cust_' . $item_template_field_id . ',
FIND_IN_SET(c.CategoryId, ' . $this->Conn->qstr($parent_path) . ') AS Ord1,
c.CategoryId, c.Name, ccd.l' . $primary_lang . '_cust_' . $item_template_field_id . '
FROM ' . TABLE_PREFIX . 'Categories AS c
LEFT JOIN ' . TABLE_PREFIX . 'CategoryCustomData AS ccd
ON ccd.ResourceId = c.ResourceId
WHERE c.CategoryId IN (' . $parent_path . ') AND ccd.l' . $primary_lang . '_cust_' . $item_template_field_id . ' != \'\'
ORDER BY FIND_IN_SET(c.CategoryId, ' . $this->Conn->qstr($parent_path) . ') DESC';
$item_template = $this->Conn->GetOne($query);
if ( !isset($this->_templateAliases) ) {
// when empty url OR mod-rewrite disabled
$themes_helper = $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$sql = 'SELECT TemplateAliases
FROM ' . TABLE_PREFIX . 'Themes
WHERE ThemeId = ' . (int)$themes_helper->getCurrentThemeId();
$template_aliases = $this->Conn->GetOne($sql);
$this->_templateAliases = $template_aliases ? unserialize($template_aliases) : Array ();
}
if ( substr($item_template, 0, 1) == '#' ) {
// it's template alias + "#" isn't allowed in filenames
$item_template = (string)getArrayValue($this->_templateAliases, $item_template);
}
$this->Application->setCache($cache_key, $item_template);
return $item_template;
}
/**
* Returns category custom field id, where given module prefix item template name is stored
*
* @param string $module_prefix
* @return int
* @access public
* @todo Move to kPlainUrlProcessor; decrease visibility, since used only during upgrade
*/
public function getItemTemplateCustomField($module_prefix)
{
$cache_key = __CLASS__ . '::' . __FUNCTION__ . '[%CfSerial%]:' . $module_prefix;
$cached_value = $this->Application->getCache($cache_key);
if ($cached_value !== false) {
return $cached_value;
}
$sql = 'SELECT CustomFieldId
FROM ' . TABLE_PREFIX . 'CustomFields
WHERE FieldName = ' . $this->Conn->qstr($module_prefix . '_ItemTemplate');
$item_template_field_id = $this->Conn->GetOne($sql);
$this->Application->setCache($cache_key, $item_template_field_id);
return $item_template_field_id;
}
/**
* Marks url part as parsed
*
* @param string $url_part
* @param string $parse_direction
* @access public
*/
public function partParsed($url_part, $parse_direction = 'ltr')
{
if ( !$this->_partsToParse ) {
return ;
}
if ( $parse_direction == 'ltr' ) {
$expected_url_part = reset($this->_partsToParse);
if ( $url_part == $expected_url_part ) {
array_shift($this->_partsToParse);
}
}
else {
$expected_url_part = end($this->_partsToParse);
if ( $url_part == $expected_url_part ) {
array_pop($this->_partsToParse);
}
}
if ( $url_part != $expected_url_part ) {
trigger_error('partParsed: expected URL part "<strong>' . $expected_url_part . '</strong>", received URL part "<strong>' . $url_part . '</strong>"', E_USER_NOTICE);
}
}
/**
* Determines if there is more to parse in url
*
* @return bool
* @access public
*/
public function moreToParse()
{
return count($this->_partsToParse) > 0;
}
/**
* Builds url
*
* @param string $t
* @param Array $params
* @param string $pass
* @param bool $pass_events
* @param bool $env_var
* @return string
* @access public
*/
public function build($t, $params, $pass = 'all', $pass_events = false, $env_var = false)
{
if ( $this->Application->GetVar('admin') || (array_key_exists('admin', $params) && $params['admin']) ) {
$params['admin'] = 1;
if ( !array_key_exists('editing_mode', $params) ) {
$params['editing_mode'] = EDITING_MODE;
}
}
$ret = '';
$env = '';
if ( isset($params['__SSL__']) ) {
unset($params['__SSL__']);
}
$catalog_item_found = false;
$pass_info = $this->getPassInfo($pass);
if ( $pass_info ) {
if ( $pass_info[0] == 'm' ) {
array_shift($pass_info);
}
$inject_parts = Array (); // url parts for beginning of url
$params['t'] = $t; // make template available for rewrite listeners
$params['pass_template'] = true; // by default we keep given template in resulting url
if ( !array_key_exists('pass_category', $params) ) {
$params['pass_category'] = false; // by default we don't keep categories in url
}
foreach ($pass_info as $pass_index => $pass_element) {
list ($prefix) = explode('.', $pass_element);
$catalog_item = $this->Application->findModule('Var', $prefix) && $this->Application->getUnitConfig($prefix)->getCatalogItem();
if ( array_key_exists($prefix, $this->rewriteListeners) ) {
// if next prefix is same as current, but with special => exclude current prefix from url
$next_prefix = array_key_exists($pass_index + 1, $pass_info) ? $pass_info[$pass_index + 1] : false;
if ( $next_prefix ) {
$next_prefix = substr($next_prefix, 0, strlen($prefix) + 1);
if ( $prefix . '.' == $next_prefix ) {
continue;
}
}
// rewritten url part
$url_part = $this->BuildModuleEnv($pass_element, $params, $pass_events);
if ( is_string($url_part) && $url_part ) {
$ret .= $url_part . '/';
if ( $catalog_item ) {
// pass category later only for catalog items
$catalog_item_found = true;
}
}
elseif ( is_array($url_part) ) {
// rewrite listener want to insert something at the beginning of url too
if ( $url_part[0] ) {
$inject_parts[] = $url_part[0];
}
if ( $url_part[1] ) {
$ret .= $url_part[1] . '/';
}
if ( $catalog_item ) {
// pass category later only for catalog items
$catalog_item_found = true;
}
}
elseif ( $url_part === false ) {
// rewrite listener decided not to rewrite given $pass_element
$env .= ':' . $this->manager->plain->BuildModuleEnv($pass_element, $params, $pass_events);
}
}
else {
$env .= ':' . $this->manager->plain->BuildModuleEnv($pass_element, $params, $pass_events);
}
}
if ( $catalog_item_found || preg_match('/c\.[-\d]*/', implode(',', $pass_info)) ) {
// "c" prefix is present -> keep category
$params['pass_category'] = true;
}
$params['inject_parts'] = $inject_parts;
$ret = $this->BuildModuleEnv('m', $params, $pass_events) . '/' . $ret;
$cat_processed = array_key_exists('category_processed', $params) && $params['category_processed'];
// remove temporary parameters used by listeners
unset($params['t'], $params['inject_parts'], $params['pass_template'], $params['pass_category'], $params['category_processed']);
$ret = trim($ret, '/');
if ( isset($params['url_ending']) ) {
if ( $ret ) {
$ret .= $params['url_ending'];
}
unset($params['url_ending']);
}
elseif ( $ret ) {
$ret .= MOD_REWRITE_URL_ENDING;
}
if ( $env ) {
$params[ENV_VAR_NAME] = ltrim($env, ':');
}
}
unset($params['pass'], $params['opener'], $params['m_event']);
// TODO: why?
// $ret = str_replace('%2F', '/', urlencode($ret));
if ( $params ) {
$params_str = http_build_query($params);
$ret .= '?' . str_replace('%23', '#', $params_str);
}
return $ret;
}
/**
* Builds env part that corresponds prefix passed
*
* @param string $prefix_special item's prefix & [special]
* @param Array $params url params
* @param bool $pass_events
* @return string
* @access protected
*/
protected function BuildModuleEnv($prefix_special, &$params, $pass_events = false)
{
list ($prefix) = explode('.', $prefix_special);
$url_parts = Array ();
$listener = $this->rewriteListeners[$prefix][0];
$ret = $listener[0]->$listener[1](REWRITE_MODE_BUILD, $prefix_special, $params, $url_parts, $pass_events);
return $ret;
}
-}
\ No newline at end of file
+}
Index: branches/5.3.x/core/kernel/managers/plain_url_processor.php
===================================================================
--- branches/5.3.x/core/kernel/managers/plain_url_processor.php (revision 16155)
+++ branches/5.3.x/core/kernel/managers/plain_url_processor.php (revision 16156)
@@ -1,263 +1,263 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2011 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class kPlainUrlProcessor extends kUrlProcessor {
/**
* Process QueryString only, create events, ids, based on config
* set template name and sid in desired application variables.
*
* @param string $env_var environment string value
* @param string $pass_name
* @return Array
*/
public function parse($env_var, $pass_name = 'passed')
{
// env=SID-TEMPLATE:m-1-1-1-1:l0-0-0:n-0-0-0:bb-0-0-1-1-1-0
if ( !$env_var ) {
return Array ('t' => $this->manager->getTemplateName());
}
parse_str(ENV_VAR_NAME . '=' . $env_var, $vars);
$env_var = $vars[ENV_VAR_NAME];
unset($vars[ENV_VAR_NAME]);
// replace escaped ":" symbol not to explode by it
$env_var = str_replace('\:', '_&+$$+&_', $env_var); // replace escaped "=" with spec-chars :)
$parts = explode(':', $env_var);
if ( !$this->Application->RewriteURLs() || ($this->Application->RewriteURLs() && $this->Application->GetVar('rewrite') != 'on') ) {
$vars = array_merge($vars, $this->extractSIDAndTemplate($parts));
}
if ( $parts ) {
$passed = Array ();
foreach ($parts as $mixed_part) {
list ($passed[], $processed_vars) = $this->_parseEnvPart($mixed_part);
$vars = array_merge($vars, $processed_vars);
}
$vars[$pass_name] = implode(',', array_unique($passed));
}
return $vars;
}
/**
* Retrieves SessionID and current template from given ENV parts
*
* @param Array $parts
* @return array
* @access protected
*/
protected function extractSIDAndTemplate(&$parts)
{
$template = '';
$vars = Array ();
if ( preg_match('/^([\d]+|)-(.*)$/', $parts[0], $regs) ) {
// first "env" component matches "sid-template" format
// (will be false, when mod-rewrite url to home page is built)
$sid = $regs[1];
$template = $regs[2];
array_shift($parts);
if ( $sid ) {
// Save Session ID
$this->Application->SetVar('sid', $sid);
$vars['sid'] = $sid;
}
}
// Save Template Name
$vars['t'] = $this->manager->getTemplateName($template);
return $vars;
}
/**
* Converts environment part into variable array (based on query map for given prefix)
*
* @param string $mixed_part
* @return Array
* @access protected
*/
protected function _parseEnvPart($mixed_part)
{
// In-portal old style env conversion - adds '-' between prefix and first var
$mixed_part = str_replace('_&+$$+&_', ':', $mixed_part);
$mixed_part = preg_replace("/^([a-zA-Z]+)([0-9]+)-(.*)/", "$1-$2-$3", $mixed_part);
// replace escaped "-" symbol not to explode by it
$escaped_part = str_replace('\-', '_&+$$+&_', $mixed_part);
$escaped_part = explode('-', $escaped_part);
$mixed_part = Array();
foreach ($escaped_part as $escaped_val) {
$mixed_part[] = str_replace('_&+$$+&_', '-', $escaped_val);
}
$vars = Array ();
$prefix_special = array_shift($mixed_part); // l.pick, l
- $http_query = $this->Application->recallObject('HTTPQuery');
+ $http_query = $this->Application->recallObject('kHTTPQuery');
/* @var $http_query kHTTPQuery */
$query_map = $http_query->discoverUnit($prefix_special); // from $_GET['env']
// if config is not defined for prefix in QueryString, then don't process it
if ($query_map) {
foreach ($query_map as $index => $var_name) {
// l_id, l_page, l_bla-bla-bla
$val = $mixed_part[$index - 1];
if ($val == '') $val = false;
$vars[$prefix_special.'_'.$var_name] = $val;
}
}
return Array ($prefix_special, $vars);
}
/**
* Builds url
*
* @param string $t
* @param Array $params
* @param string $pass
* @param bool $pass_events
* @param bool $env_var
* @return string
* @access public
*/
public function build($t, $params, $pass='all', $pass_events = false, $env_var = true)
{
if ( $this->Application->GetVar('admin') || (array_key_exists('admin', $params) && $params['admin']) ) {
$params['admin'] = 1;
if ( !array_key_exists('editing_mode', $params) ) {
$params['editing_mode'] = EDITING_MODE;
}
}
$ssl = isset($params['__SSL__']) ? $params['__SSL__'] : 0;
$sid = isset($params['sid']) && !$this->Application->RewriteURLs($ssl) ? $params['sid'] : '';
if ( isset($params['__SSL__']) ) {
unset($params['__SSL__']);
}
$env_string = '';
$category_id = isset($params['m_cat_id']) ? $params['m_cat_id'] : $this->Application->GetVar('m_cat_id');
$item_id = false;
$pass_info = $this->getPassInfo($pass);
if ( $pass_info ) {
if ( $pass_info[0] == 'm' ) {
array_shift($pass_info);
}
foreach ($pass_info as $pass_element) {
list($prefix) = explode('.', $pass_element);
$require_rewrite = $this->Application->findModule('Var', $prefix);
if ( $require_rewrite ) {
$item_id = isset($params[$pass_element . '_id']) ? $params[$pass_element . '_id'] : $this->Application->GetVar($pass_element . '_id');
}
$env_string .= ':' . $this->BuildModuleEnv($pass_element, $params, $pass_events);
}
}
if ( strtolower($t) == '__default__' ) {
if ( is_numeric($item_id) ) {
$this->manager->initRewrite();
$t = $this->manager->rewrite->GetItemTemplate($category_id, $pass_element); // $pass_element should be the last processed element
// $t = $this->Application->getCategoryCache($category_id, 'item_templates');
}
elseif ( $category_id ) {
$t = strtolower(preg_replace('/^Content\//i', '', $this->Application->getCategoryCache($category_id, 'filenames')));
}
else {
$t = 'index';
}
}
$env_string = $sid . '-' . $t . ':' . $this->BuildModuleEnv('m', $params, $pass_events) . $env_string;
unset($params['pass'], $params['opener'], $params['m_event']);
$params = array(ENV_VAR_NAME => $env_string) + $params;
$params_str = http_build_query($params);
$ret = str_replace('%23', '#', $params_str);
if ( !$env_var ) {
$ret = substr($ret, strlen(ENV_VAR_NAME) + 1);
}
return $ret;
}
/**
* Builds env part that corresponds prefix passed
*
* @param string $prefix_special item's prefix & [special]
* @param Array $params url params
* @param bool $pass_events
* @return string
* @access public
*/
public function BuildModuleEnv($prefix_special, &$params, $pass_events = false)
{
list($prefix) = explode('.', $prefix_special);
$config = $this->Application->getUnitConfig($prefix);
$query_vars = $config->getQueryString(Array ());
//if pass events is off and event is not implicitly passed
if ( !$pass_events && !isset($params[$prefix_special . '_event']) ) {
$params[$prefix_special . '_event'] = ''; // remove event from url if requested
//otherwise it will use value from get_var
}
if ( !$query_vars ) {
return '';
}
$tmp_string = Array (0 => $prefix_special);
foreach ($query_vars as $index => $var_name) {
//if value passed in params use it, otherwise use current from application
$var_name = $prefix_special . '_' . $var_name;
$tmp_string[$index] = isset($params[$var_name]) ? $params[$var_name] : $this->Application->GetVar($var_name);
if ( isset($params[$var_name]) ) {
unset($params[$var_name]);
}
}
$escaped = array ();
foreach ($tmp_string as $tmp_val) {
$escaped[] = str_replace(Array ('-', ':'), Array ('\-', '\:'), $tmp_val);
}
$ret = implode('-', $escaped);
if ( $config->getPortalStyleEnv() == true ) {
$ret = preg_replace('/^([a-zA-Z]+)-([0-9]+)-(.*)/', '\\1\\2-\\3', $ret);
}
return $ret;
}
-}
\ No newline at end of file
+}
Index: branches/5.3.x/core/kernel/processors/main_processor.php
===================================================================
--- branches/5.3.x/core/kernel/processors/main_processor.php (revision 16155)
+++ branches/5.3.x/core/kernel/processors/main_processor.php (revision 16156)
@@ -1,1292 +1,1292 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class kMainTagProcessor extends kTagProcessor {
public function __construct()
{
parent::__construct();
$actions = $this->Application->recallObject('kActions');
/* @var $actions Params */
$actions->Set('t', $this->Application->GetVar('t'));
$actions->Set('sid', $this->Application->GetSID());
$actions->Set('m_opener', $this->Application->GetVar('m_opener') );
}
/**
* Base folder for all template includes
*
* @param Array $params
* @return string
*/
function TemplatesBase($params)
{
static $cached = Array ();
$cache_key = crc32( serialize($params) );
if (!array_key_exists($cache_key, $cached)) {
$module = array_key_exists('module', $params) ? $params['module'] : 'core';
if ($this->Application->isAdmin) {
if ($module == 'in-portal') {
$module = 'kernel';
}
// remove leading slash + substitute module
$module_path = $this->Application->findModule('Name', $module, 'Path');
if ($module_path !== false) {
$path = $module_path . 'admin_templates';
}
else {
// remove leading slash + substitute module
$path = preg_replace('/\/(.*?)\/(.*)/', $module . '/\\2', THEMES_PATH);
}
}
else {
$path = mb_substr(THEMES_PATH, 1);
if (mb_strtolower($module) == 'in-portal') {
$module_folder = 'platform';
}
else {
$module_folder = $this->Application->findModule('Name', $module, 'TemplatePath');
}
$path .= rtrim('/' . trim($module_folder, '/'), '/') . '/';
}
$cached[$cache_key] = $this->Application->BaseURL() . $path;
}
return $cached[$cache_key];
}
/**
* Creates <base href ..> HTML tag for all templates
* affects future css, js files and href params of links
*
* @param Array $params
* @return string
* @access protected
* @see kMainTagProcessor::TemplatesBase
*/
protected function Base_Ref($params)
{
// tag TemplatesBase adds trailing "/" but only on Front-End
$base_href = rtrim($this->TemplatesBase($params), '/');
return '<base href="' . $base_href . '/" />';
}
/**
* Returns base url for web-site
*
* @return string
* @access public
*/
function BaseURL()
{
return $this->Application->BaseURL();
}
//for compatability with K3 tags
function Base($params)
{
return $this->TemplatesBase($params).'/';
}
function ProjectBase($params)
{
return $this->Application->BaseURL();
}
/*function Base($params)
{
return $this->Application->BaseURL().$params['add'];
}*/
/**
* Used to create link to any template.
* use "pass" paramter if "t" tag to specify
* prefix & special of object to be represented
* in resulting url
*
* @param Array $params
* @return string
* @access public
*/
function T($params)
{
// by default link to current template
$template = $this->SelectParam($params, 't,template');
$prefix = array_key_exists('prefix', $params) ? $params['prefix'] : '';
unset($params['t'], $params['template'], $params['prefix']);
$no_html_escape = false;
if ( isset($params['no_amp']) ) {
$no_html_escape = $params['no_amp'];
unset($params['no_amp']);
}
$ret = $this->Application->HREF($template, $prefix, $params);
if ( !$no_html_escape ) {
// most of the time links are placed into HTML document
// TODO: in future always do escaping according to current "escape context"
$ret = kUtil::escape($ret, kUtil::ESCAPE_HTML);
}
return $ret;
}
function Link($params)
{
// pass "m" prefix, instead of "all", that is by default on Front-End
if (!array_key_exists('pass', $params)) {
$params['pass'] = 'm';
}
return $this->T($params);
}
/**
* Performs redirect to provided template/url
*
* @param Array $params
* @return string
*/
function Redirect($params)
{
$this->Application->Redirect('external:' . $this->Link($params));
return '';
}
/*function Env($params)
{
$t = $params['template'];
unset($params['template']);
return $this->Application->BuildEnv($t, $params, 'm', false, false);
}*/
function FormAction($params)
{
if (!array_key_exists('pass', $params)) {
$params['pass'] = 'all,m';
}
$params['pass_category'] = 1;
return $this->Application->HREF('', '', $params);
}
/*// NEEDS TEST
function Config($params)
{
return $this->Application->ConfigOption($params['var']);
}
function Object($params)
{
$name = $params['name'];
$method = $params['method'];
$tmp = $this->Application->recallObject($name);
if ($tmp != null) {
if (method_exists($tmp, $method))
return $tmp->$method($params);
else
echo "Method $method does not exist in object ".get_class($tmp)." named $name<br>";
}
else
echo "Object $name does not exist in the appliaction<br>";
}*/
/**
* Tag, that always returns true.
* For parser testing purposes
*
* @param Array $params
* @return bool
* @access public
*/
function True($params)
{
return true;
}
/**
* Tag, that always returns false.
* For parser testing purposes
*
* @param Array $params
* @return bool
* @access public
*/
function False($params)
{
return false;
}
/**
* Returns block parameter by name (used only as "check" parameter value for "m_if" tag!)
*
* @param Array $params
* @return stirng
* @access public
*/
function Param($params)
{
$name = $params['name'];
if (array_key_exists($name, $this->Application->Parser->Captures)) {
$capture_params = $params;
$capture_params['name'] = '__capture_' . $name;
$this->Application->Parser->SetParam($name, $this->Application->ParseBlock($capture_params));
}
$res = $this->Application->Parser->GetParam($name);
if ($res === false) {
$res = '';
}
if (array_key_exists('plus', $params)) {
$res += $params['plus'];
}
return $res;
}
/**
* Compares block parameter with value specified
*
* @param Array $params
* @return bool
* @access public
*/
function ParamEquals($params)
{
$name = $this->SelectParam($params, 'name,var,param');
$value = $params['value'];
return ($this->Application->Parser->GetParam($name) == $value);
}
/*function PHP_Self($params)
{
return $HTTP_SERVER_VARS['PHP_SELF'];
}
*/
/**
* Returns session variable value by name
*
* @param Array $params
* @return string
* @access public
*/
function Recall($params)
{
$var_name = $this->SelectParam($params,'name,var,param');
if (isset($params['persistent']) && $params['persistent']) {
$ret = $this->Application->RecallPersistentVar($var_name);
}
else {
$ret = $this->Application->RecallVar($var_name);
}
$ret = ($ret === false && isset($params['no_null'])) ? '' : $ret;
if (getArrayValue($params, 'special') || getArrayValue($params, 'htmlchars')) {
$ret = kUtil::escape($ret, kUtil::ESCAPE_HTML);
}
if (getArrayValue($params, 'urlencode')) {
$ret = kUtil::escape($ret, kUtil::ESCAPE_URL);
}
return $ret;
}
function RemoveVar($params)
{
$this->Application->RemoveVar( $this->SelectParam($params,'name,var,param') );
}
// bad style to store something from template to session !!! (by Alex)
// Used here only to test how session works, nothing more
function Store($params)
{
//echo"Store $params[name]<br>";
$name = $params['name'];
$value = $params['value'];
$this->Application->StoreVar($name,$value);
}
/**
* Links variable from request with variable from session
*
* @param Array $params
* @return string
* @access protected
*/
protected function LinkVar($params)
{
$var_name = $params['name'];
$session_var_name = isset($params['session_name']) ? $params['session_name'] : $var_name;
$default_value = isset($params['default']) ? $params['default'] : '';
$this->Application->LinkVar($var_name, $session_var_name, $default_value);
return '';
}
/**
* Links variable from request with variable from session and returns it's value
*
* @param Array $params
* @return string
* @access protected
*/
protected function GetLinkedVar($params)
{
$this->LinkVar($params);
return $this->Application->GetVar( $params['name'] );
}
/**
* Sets application variable value(-s)
*
* @param Array $params
* @access public
*/
function Set($params)
{
foreach ($params as $param => $value) {
$this->Application->SetVar($param, $value);
}
}
/**
* Increment application variable
* specified by number specified
*
* @param Array $params
* @access public
*/
function Inc($params)
{
$this->Application->SetVar($params['param'], $this->Application->GetVar($params['param']) + $params['by']);
}
/**
* Retrieves application variable
* value by name
*
* @param Array $params
* @return string
* @access public
*/
function Get($params)
{
$name = $this->SelectParam($params, 'name,var,param');
if ( strpos($name, '[') !== false ) {
preg_match('/([^\[\]]+)\[(.*)\]/', $name, $regs);
$function_params = explode('][', $regs[2]);
$ret = $this->Application->GetVar($regs[1], array());
kUtil::array_unshift_ref($function_params, $ret);
$ret = call_user_func_array('getArrayValue', $function_params);
}
else {
$ret = $this->Application->GetVar($name, '');
}
if ( array_key_exists('no_html_escape', $params) && $params['no_html_escape'] ) {
return $this->Application->isAdmin ? $ret : kUtil::unescape($ret, kUtil::ESCAPE_HTML);
}
return kUtil::escape($ret, kUtil::ESCAPE_HTML);
}
/**
* Retrieves application constant
* value by name
*
* @param Array $params
* @return string
* @access public
*/
function GetConst($params)
{
$constant_name = $this->SelectParam($params, 'name,const');
return defined($constant_name) ? constant($constant_name) : '';
}
/**
* Retrieves configuration variable value by name
*
* @param Array $params
* @return string
* @access public
*/
function GetConfig($params)
{
$config_name = $this->SelectParam($params, 'name,var');
$ret = $this->Application->ConfigValue($config_name);
if ( isset($params['formatted']) && $params['formatted'] ) {
$sql = 'SELECT ValueList
FROM ' . TABLE_PREFIX . 'SystemSettings
WHERE VariableName = ' . $this->Conn->qstr($config_name) . ' AND ElementType IN ("select", "radio")';
$value_list = $this->Conn->GetOne($sql);
if ( $value_list ) {
$helper = $this->Application->recallObject('InpCustomFieldsHelper');
/* @var $helper InpCustomFieldsHelper */
$options = $helper->GetValuesHash($value_list);
$ret = isset($options[$ret]) ? $options[$ret] : $ret;
}
}
if ( isset($params['as_label']) && $params['as_label'] ) {
$ret = $this->Application->Phrase($ret);
}
return $ret;
}
/**
* Compares configuration variable to a given value
*
* @param Array $params
* @return bool
* @deprecated
* @access protected
*/
protected function ConfigEquals($params)
{
$option = $this->SelectParam($params, 'name,option,var');
return $this->Application->ConfigValue($option) == $params['value'];
}
/**
* Creates all hidden fields
* needed for kernel_form
*
* @param Array $params
* @return string
* @access protected
*/
protected function DumpSystemInfo($params)
{
$actions = $this->Application->recallObject('kActions');
/* @var $actions Params */
$actions->Set('t', $this->Application->GetVar('t'));
$o = '';
$params = $actions->GetParams();
foreach ($params AS $name => $val) {
$o .= "<input type='hidden' name='$name' id='$name' value='$val'>\n";
}
return $o;
}
/**
* Used for search sidebox on front-end only
*
* @param Array $params
* @return string
* @access protected
*/
protected function GetFormHiddens($params)
{
$t = $this->SelectParam($params, 'template,t');
unset($params['template']);
$form_fields = Array ();
if ( $this->Application->RewriteURLs() ) {
$session = $this->Application->recallObject('Session');
/* @var $session Session */
if ( $session->NeedQueryString() ) {
$form_fields['sid'] = $this->Application->GetSID();
}
}
else {
$form_fields['env'] = $this->Application->BuildEnv($t, $params, 'm', false, false);
}
if ( $this->Application->GetVar('admin') == 1 ) {
$form_fields['admin'] = 1;
}
$ret = '';
$field_tpl = '<input type="hidden" name="%1$s" id="%1$s" value="%2$s"/>' . "\n";
foreach ($form_fields as $form_field => $field_value) {
$ret .= sprintf($field_tpl, $form_field, $field_value);
}
return $ret;
}
function Odd_Even($params)
{
$odd = $params['odd'];
$even = $params['even'];
if (!isset($params['var'])) {
$var = 'odd_even';
}
else {
$var = $params['var'];
}
if ($this->Application->GetVar($var) == 'even') {
if (!isset($params['readonly']) || !$params['readonly']) {
$this->Application->SetVar($var, 'odd');
}
return $even;
}
else {
if (!isset($params['readonly']) || !$params['readonly']) {
$this->Application->SetVar($var, 'even');
}
return $odd;
}
}
/**
* Returns phrase translation by name
*
* @param Array $params
* @return string
* @access public
*/
function Phrase($params)
{
$phrase_name = $this->SelectParam($params, 'label,name,title');
$default_translation = $this->SelectParam($params, 'default');
$no_editing = isset($params['no_editing']) && $params['no_editing'];
$translation = $this->Application->Phrase($phrase_name, !$no_editing);
$phrase_key = mb_strtoupper($phrase_name);
if ( $default_translation && strpos($translation, '!' . $phrase_key . '!') !== false ) {
$phrase = $this->Application->recallObject('phrases.autocreate', null, Array ('skip_autoload' => true));
/* @var $phrase kDBItem */
if ( !$phrase->Load($phrase_key, 'PhraseKey') ) {
$phrase->SetDBField('Phrase', $phrase_name);
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$languages = $ml_helper->getLanguages();
foreach ($languages AS $language_id) {
$phrase->SetDBField('l' . $language_id . '_Translation', $default_translation);
}
if ( $phrase->Create() ) {
$translation = $default_translation;
}
}
}
if ( isset($params['escape']) && $params['escape'] ) {
// html escaping here is redundant
$translation = kUtil::escape($translation, kUtil::ESCAPE_JS);
}
return $translation;
}
// for tabs
function is_active($params)
{
$test_templ = $this->SelectParam($params, 'templ,template,t');
if ( !getArrayValue($params, 'allow_empty') ) {
$if_true = getArrayValue($params, 'true') ? $params['true'] : 1;
$if_false = getArrayValue($params, 'false') ? $params['false'] : 0;
}
else {
$if_true = $params['true'];
$if_false = $params['false'];
}
$physical_template = $this->Application->getPhysicalTemplate($this->Application->GetVar('t'));
return preg_match('/^' . str_replace('/', '\/', $test_templ) . '/i', $physical_template) ? $if_true : $if_false;
}
function IsNotActive($params)
{
return !$this->is_active($params);
}
function IsActive($params)
{
return $this->is_active($params);
}
function is_t_active($params)
{
return $this->is_active($params);
}
function CurrentTemplate($params)
{
return $this->is_active($params);
}
/**
* Checks if session variable
* specified by name value match
* value passed as parameter
*
* @param Array $params
* @return string
* @access public
*/
function RecallEquals($params)
{
$name = $this->SelectParam($params, 'name,var');
$value = $params['value'];
if (isset($params['persistent']) && $params['persistent']) {
return $this->Application->RecallPersistentVar($name) == $value;
}
return ($this->Application->RecallVar($name) == $value);
}
/**
* Checks if application variable specified by name value match value passed as parameter
*
* @param Array $params
* @return bool
* @access protected
* @deprecated
*/
protected function GetEquals($params)
{
$name = $this->SelectParam($params, 'var,name,param');
return $this->Application->GetVar($name) == $params['value'];
}
function ModuleInclude($params)
{
$ret = '';
$included = Array ();
$block_params = array_merge($params, Array ('is_silent' => 2)); // don't make fatal errors in case if template is missing
$current_template = $this->Application->GetVar('t');
$replace_main = isset($params['replace_m']) && $params['replace_m'];
$skip_prefixes = isset($params['skip_prefixes']) ? explode(',', $params['skip_prefixes']) : Array ();
$cms_mode = $this->Application->GetVar('admin');
foreach ($this->Application->ModuleInfo as $module_name => $module_data) {
$module_key = mb_strtolower($module_name);
if ( $module_name == 'In-Portal' ) {
if ( !$cms_mode && $this->Application->isAdmin ) {
// don't process In-Portal templates in admin
continue;
}
// Front-End still relies on In-Portal module
$module_prefix = $module_data['TemplatePath'];
}
elseif ( $this->Application->isAdmin && $module_data['Path'] != 'core/' ) {
$module_prefix = $module_key . '/'; // was $module_data['Path'];
}
else {
$module_prefix = $module_data['TemplatePath']; // always have trailing "/"
}
if ( in_array($module_prefix, $included) ) {
// template by this path was already included by other module (e.g. in-portal used core's template)
continue;
}
$block_params['t'] = $module_prefix . $this->SelectParam($params, $module_key . '_template,' . $module_key . '_t,template,t');
$check_prefix = $module_data['Var'];
if ( $check_prefix == 'adm' && $replace_main ) {
$check_prefix = 'c';
}
if ( $block_params['t'] == $current_template || in_array($check_prefix, $skip_prefixes) ) {
continue;
}
$no_data = $this->SelectParam($params, $module_key . '_block_no_data,block_no_data');
if ( $no_data ) {
$block_params['block_no_data'] = $module_prefix . '/' . $no_data;
}
$ret .= $this->Application->IncludeTemplate($block_params);
$included[] = $module_prefix;
}
return $ret;
}
function ModuleEnabled($params)
{
return $this->Application->isModuleEnabled( $params['module'] );
}
/**
* Checks if debug mode is on
*
* @param Array $params
* @return bool
* @access public
*/
function IsDebugMode($params)
{
return defined('DEBUG_MODE') && $this->Application->isDebugMode();
}
/*function MassParse($params)
{
$qty = $params['qty'];
$block = $params['block'];
$mode = $params['mode'];
$o = '';
if ($mode == 'func') {
$func = create_function('$params', '
$o = \'<tr>\';
$o.= \'<td>a\'.$params[\'param1\'].\'</td>\';
$o.= \'<td>a\'.$params[\'param2\'].\'</td>\';
$o.= \'<td>a\'.$params[\'param3\'].\'</td>\';
$o.= \'<td>a\'.$params[\'param4\'].\'</td>\';
$o.= \'</tr>\';
return $o;
');
for ($i=1; $i<$qty; $i++) {
$block_params['param1'] = rand(1, 10000);
$block_params['param2'] = rand(1, 10000);
$block_params['param3'] = rand(1, 10000);
$block_params['param4'] = rand(1, 10000);
$o .= $func($block_params);
}
return $o;
}
$block_params['name'] = $block;
for ($i=0; $i<$qty; $i++) {
$block_params['param1'] = rand(1, 10000);
$block_params['param2'] = rand(1, 10000);
$block_params['param3'] = rand(1, 10000);
$block_params['param4'] = rand(1, 10000);
$block_params['passed'] = $params['passed'];
$block_params['prefix'] = 'm';
$o.= $this->Application->ParseBlock($block_params);
}
return $o;
}*/
function LoggedIn($params)
{
return $this->Application->LoggedIn();
}
/**
* Allows to check if permission exists directly in template and perform additional actions if required
*
* @param Array $params
* @return bool
*/
function CheckPermission($params)
{
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
return $perm_helper->TagPermissionCheck($params);
}
/**
* Checks if user is logged in and if not redirects it to template passed
*
* @param Array $params
*/
function RequireLogin($params)
{
$t = $this->Application->GetVar('t');
$next_t = getArrayValue($params, 'next_template');
if ( $next_t ) {
$t = $next_t;
}
// check by permissions: begin
if ((isset($params['perm_event']) && $params['perm_event']) ||
(isset($params['perm_prefix']) && $params['perm_prefix']) ||
(isset($params['permissions']) && $params['permissions'])) {
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$perm_status = $perm_helper->TagPermissionCheck($params);
if (!$perm_status) {
list($redirect_template, $redirect_params) = $perm_helper->getPermissionTemplate($params);
$this->Application->Redirect($redirect_template, $redirect_params);
}
else {
return ;
}
}
// check by permissions: end
// check by configuration value: begin
$condition = getArrayValue($params, 'condition');
if (!$condition) {
$condition = true;
}
else {
if (substr($condition, 0, 1) == '!') {
$condition = !$this->Application->ConfigValue(substr($condition, 1));
}
else {
$condition = $this->Application->ConfigValue($condition);
}
}
// check by configuration value: end
// check by belonging to group: begin
$group = $this->SelectParam($params, 'group');
$group_access = true;
if ($group) {
$sql = 'SELECT GroupId
FROM '.TABLE_PREFIX.'UserGroups
WHERE Name = '.$this->Conn->qstr($group);
$group_id = $this->Conn->GetOne($sql);
if ($group_id) {
$groups = explode(',', $this->Application->RecallVar('UserGroups'));
$group_access = in_array($group_id, $groups);
}
}
// check by belonging to group: end
if ((!$this->Application->LoggedIn() || !$group_access) && $condition) {
$redirect_params = $this->Application->HttpQuery->getRedirectParams(true);
if (MOD_REWRITE) {
// TODO: $next_t variable is ignored !!! (is anyone using m_RequireLogin tag with "next_template" parameter?)
$redirect_params = Array (
'm_cat_id' => 0,
'next_template' => 'external:' . $_SERVER['REQUEST_URI'],
);
}
else {
$redirect_params['next_template'] = $t;
}
if (array_key_exists('pass_category', $params)) {
$redirect_params['pass_category'] = $params['pass_category'];
}
if (array_key_exists('use_section', $params)) {
$redirect_params['use_section'] = $params['use_section'];
}
if ( $this->Application->LoggedIn() && !$group_access) {
$this->Application->Redirect($params['no_group_perm_template'], $redirect_params);
}
$this->Application->Redirect($params['login_template'], $redirect_params);
}
}
/**
* Checks, that user belongs to a group with a given name
*
* @param Array $params
* @return bool
*/
protected function IsMember($params)
{
$sql = 'SELECT GroupId
FROM ' . TABLE_PREFIX . 'UserGroups
WHERE Name = ' . $this->Conn->qstr($params['group']);
$group_id = $this->Conn->GetOne($sql);
if ( $group_id ) {
$groups = explode(',', $this->Application->RecallVar('UserGroups'));
return in_array($group_id, $groups);
}
return false;
}
/**
* Checks if SSL is enabled and redirects to SSL Domain if needed
* If SSLDomain is not defined in config - the tag does not do anything
* If for_logged_in_only="1" exits if user is not logged in.
* If called without params forces https right away. If called with by_config="1" checks the
* Require SSL setting from General Config and if it is ON forces https
*
* @param Array $params
*/
protected function CheckSSL($params)
{
$ssl_domain = $this->Application->getSecureDomain();
if ( !$ssl_domain || ($this->Application->TemplatesCache->forceThemeName !== false) ) {
// SSL URL is not set - no way to require SSL
// internal parsing (e.g. "TemplateParser::_parseTemplate") -> don't redirect
return;
}
$require = false;
if ( isset($params['mode']) && $params['mode'] == 'required' ) {
$require = true;
if ( isset($params['for_logged_in_only']) && $params['for_logged_in_only'] && !$this->Application->LoggedIn() ) {
$require = false;
}
if ( isset($params['condition']) ) {
if ( !$this->Application->ConfigValue($params['condition']) ) {
$require = false;
}
}
}
if ( EDITING_MODE ) {
// match SSL mode on front-end to one in administrative console, when browse modes are used
$require = $this->Application->ConfigValue('Require_AdminSSL');
}
- $http_query = $this->Application->recallObject('HTTPQuery');
+ $http_query = $this->Application->recallObject('kHTTPQuery');
/* @var $http_query kHTTPQuery */
$pass = $http_query->getRedirectParams();
$pass['pass_events'] = 1; // to make sure all events are passed when redirect happens
if ( $require ) {
if ( PROTOCOL == 'https://' ) {
$this->Application->SetVar('__KEEP_SSL__', 1);
return;
}
$pass['__SSL__'] = 1;
$this->Application->Redirect('', $pass);
}
else {
if ( PROTOCOL == 'https://' && $this->Application->ConfigValue('Force_HTTP_When_SSL_Not_Required') ) {
if ( $this->Application->GetVar('__KEEP_SSL__') ) {
return;
}
// $pass_more = Array ('pass' => 'm', 'm_cat_id' => 0, '__SSL__' => 0);
$pass['__SSL__'] = 0;
$this->Application->Redirect('', $pass); // $pass_more
}
}
}
function ConstOn($params)
{
$name = $this->SelectParam($params,'name,const');
return kUtil::constOn($name);
}
function SetDefaultCategory($params)
{
$category_id = $this->Application->findModule('Name', $params['module'], 'RootCat');
$this->Application->SetVar('m_cat_id', $category_id);
}
function XMLTemplate($params)
{
$this->NoDebug($params);
if ( isset($params['cache']) && $params['cache'] ) {
$nextyear = intval(date('Y') + 1);
$format = "D, d M Y H:i:s";
$expiration = gmdate($format, time() + $params['cache']) . ' GMT';
$last_modified = time();
header('Cache-Control: public, cache, max-age=' . $params['cache']);
header("Expires: $expiration");
header('Pragma: public');
// Getting headers sent by the client.
$headers = $this->Application->HttpQuery->getHeaders();
// Checking if the client is validating his cache and if it is current.
if ( isset($headers['If-Modified-Since']) && (strtotime($headers['If-Modified-Since']) > $last_modified - $params['cache']) ) {
// Client's cache IS current, so we just respond '304 Not Modified'.
header('Last-Modified: ' . date($format, strtotime($headers['If-Modified-Since'])) . ' GMT', true, 304);
exit;
}
else {
// Image not cached or cache outdated, we respond '200 OK' and output the image.
header('Last-Modified: ' . gmdate($format, $last_modified) . ' GMT', true, 200);
}
}
// xml documents are usually long
kUtil::setResourceLimit();
if ( !$this->Application->GetVar('debug') ) {
return $this->Application->XMLHeader(getArrayValue($params, 'xml_version'));
}
return '';
}
function Header($params)
{
header($params['data']);
}
function NoDebug($params)
{
if ( !$this->Application->GetVar('debug') ) {
kUtil::safeDefine('DBG_SKIP_REPORTING', 1);
}
}
/**
* Returns Home category name
*
* @param Array $params
* @return string
* @deprecated
*/
function RootCategoryName($params)
{
$no_editing = array_key_exists('no_editing', $params) && $params['no_editing'];
return $this->Application->Phrase('la_rootcategory_name', !$no_editing);
}
/**
* Allows to attach file directly from email event template
*
* @param Array $params
*/
function AttachFile($params)
{
$path = FULL_PATH . '/' . $params['path'];
$pseudo = isset($params['special']) ? 'EmailSender.' . $params['special'] : 'EmailSender';
$esender = $this->Application->recallObject($pseudo);
/* @var $esender kEmailSendingHelper */
if ( file_exists($path) ) {
$esender->AddAttachment($path);
}
}
function CaptchaImage($params)
{
$this->NoDebug($params);
$this->Application->SetVar('skip_last_template', 1);
$captcha_helper = $this->Application->recallObject('CaptchaHelper');
/* @var $captcha_helper kCaptchaHelper */
// generate captcha code
$code = $captcha_helper->prepareCode( $this->Application->GetVar('var') );
$captcha_helper->GenerateCaptchaImage($code, $this->Application->GetVar('w'), $this->Application->GetVar('h'), true);
}
function SID($params)
{
return $this->Application->GetSID();
}
function ModuleInfo($params)
{
return $this->Application->findModule($params['key'], $params['value'], $params['return']);
}
function Random($params)
{
return rand(1, 100000000);
}
/**
* Prints parser params, available at current deep level
*
* @param Array $params
* @return string
*/
function PrintCurrentParams($params)
{
$current_params = $this->Application->Parser->Params;
foreach ($current_params as $param_name => $param_value) {
$current_params[$param_name] = $param_name . ' = "' . $param_value . '"';
}
return '<pre>' . implode("\n", $current_params) . '</pre>';
}
/**
* Gets previously defined counter result
*
* @param Array $params
* @return int
*/
function GetCounter($params)
{
return $this->Application->getCounter($params['name'], $params);
}
/**
* Increments PageHit counter
*
* @param Array $params
* @return int
*/
function RegisterPageHit($params)
{
if ($this->Application->ConfigValue('UsePageHitCounter')) {
// get current counte
$sql = 'SELECT VariableValue
FROM '.TABLE_PREFIX.'SystemSettings
WHERE VariableName = "PageHitCounter"';
$page_counter = (int)$this->Conn->GetOne($sql);
$sql = 'UPDATE LOW_PRIORITY '.TABLE_PREFIX.'SystemSettings
SET VariableValue = '.($page_counter + 1).'
WHERE VariableName = "PageHitCounter"';
$this->Conn->Query($sql);
}
}
function Timestamp($params)
{
$format = isset($params['format']) ? $params['format'] : 'd.m.Y H:i:s';
return date($format);
}
function GetUrlHiddenFileds($params)
{
$vars = Array ('page', 'per_page', 'sort_by');
$ret = '<input type="hidden" name="main_list" value="1"/>';
if (array_key_exists('skip', $params)) {
$vars = array_diff($vars, $params['skip']);
}
foreach ($vars as $var_name) {
$var_value = $this->Application->GetVar($var_name);
if ($var_value) {
$ret .= '<input type="hidden" name="' . $var_name . '" value="' . $var_value . '"/>';
}
}
return $ret;
}
/**
* Returns current Page URL (without re-assembling it).
* "skip_query" param is optional and will remove the ?QUERY part from the result.
*
* @param Array $params
* @return string
* @access protected
*/
protected function CurrentPageLink($params)
{
if ( isset($params['skip_query']) && $params['skip_query'] ) {
return preg_replace('/\?' . preg_quote($_SERVER['QUERY_STRING'], '/') . '$/', '', $_SERVER['REQUEST_URI']);
}
return $_SERVER['REQUEST_URI'];
}
/**
* Returns current maintenance mode state
*
* @param Array $params
* @return int
* @access protected
*/
protected function MaintenanceMode($params)
{
$check_ips = isset($params['check_ips']) ? $params['check_ips'] : true;
return $this->Application->getMaintenanceMode($check_ips);
}
/**
* Checks if element with given name is defined
*
* @param Array $params
* @return int
* @access protected
*/
protected function ElementDefined($params)
{
return $this->Application->Parser->blockFound($params['name']);
}
/**
* Detects mobile browser.
*
* @param array $params
*
* @return string
* @link http://detectmobilebrowsers.com/
*/
protected function IsMobileBrowser($params)
{
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if ( preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i', $user_agent) || preg_match('/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i', substr($user_agent, 0, 4)) ) {
return true;
}
return false;
}
}
Index: branches/5.3.x/core/kernel/utility/email_send.php
===================================================================
--- branches/5.3.x/core/kernel/utility/email_send.php (revision 16155)
+++ branches/5.3.x/core/kernel/utility/email_send.php (revision 16156)
@@ -1,2094 +1,2094 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
/**
* Class used to compose email message (using MIME standarts) and send it via mail function or via SMTP server
*
*/
class kEmailSendingHelper extends kHelper {
/**
* headers of main header part
*
* @var Array
*/
var $headers = Array ();
/**
* Tells if all message parts were combined together
*
* @var int
*/
var $bodyPartNumber = false;
/**
* Composed message parts
*
* @var Array
*/
var $parts = Array();
/**
* Lines separator by MIME standart
*
* @var string
*/
var $line_break = "\n";
/**
* Charset used for message composing
*
* @var string
*/
var $charset = 'utf-8';
/**
* Name of mailer program (X-Mailer header)
*
* @var string
*/
var $mailerName = '';
/**
* Options used for message content-type & structure guessing
*
* @var Array
*/
var $guessOptions = Array ();
/**
* Send messages using selected method
*
* @var string
*/
var $sendMethod = 'Mail';
/**
* Parameters used to initiate SMTP server connection
*
* @var Array
*/
var $smtpParams = Array ();
/**
* List of supported authentication methods, in preferential order.
* @var array
* @access public
*/
var $smtpAuthMethods = Array('CRAM-MD5', 'LOGIN', 'PLAIN');
/**
* The socket resource being used to connect to the SMTP server.
* @var kSocket
* @access private
*/
var $smtpSocket = null;
/**
* The most recent server response code.
* @var int
* @access private
*/
var $smtpResponceCode = -1;
/**
* The most recent server response arguments.
* @var array
* @access private
*/
var $smtpRespoceArguments = Array();
/**
* Stores detected features of the SMTP server.
* @var array
* @access private
*/
var $smtpFeatures = Array();
/**
* Stores log data
*
* @var Array
* @access protected
*/
protected $_logData = Array ();
public function __construct()
{
parent::__construct();
// set default guess options
$this->guessOptions = Array (
'attachments' => Array (),
'inline_attachments' => Array (),
'text_part' => false,
'html_part' => false,
);
// read SMTP server connection params from config
$smtp_mapping = Array ('server' => 'Smtp_Server', 'port' => 'Smtp_Port');
if ($this->Application->ConfigValue('Smtp_Authenticate')) {
$smtp_mapping['username'] = 'Smtp_User';
$smtp_mapping['password'] = 'Smtp_Pass';
}
foreach ($smtp_mapping as $smtp_name => $config_name) {
$this->smtpParams[$smtp_name] = $this->Application->ConfigValue($config_name);
}
$this->smtpParams['use_auth'] = isset($this->smtpParams['username']) ? true : false;
$this->smtpParams['localhost'] = 'localhost'; // The value to give when sending EHLO or HELO.
$this->sendMethod = $this->smtpParams['server'] && $this->smtpParams['port'] ? 'SMTP' : 'Mail';
if ($this->sendMethod == 'SMTP') {
// create connection object if we will use SMTP
- $this->smtpSocket = $this->Application->makeClass('Socket');
+ $this->smtpSocket = $this->Application->makeClass('kSocket');
}
$this->SetCharset(null, true);
}
/**
* Returns new message id header by sender's email address
*
* @param string $email_address email address
* @return string
*/
function GenerateMessageID($email_address)
{
list ($micros, $seconds) = explode(' ', microtime());
list ($user, $domain) = explode('@', $email_address, 2);
$message_id = strftime('%Y%m%d%H%M%S', $seconds).substr($micros, 1, 5).'.'.preg_replace('/[^A-Za-z]+/', '-', $user).'@'.$domain;
$this->SetHeader('Message-ID', '<'.$message_id.'>');
}
/**
* Returns extension of given filename
*
* @param string $filename
* @return string
*/
function GetFilenameExtension($filename)
{
$last_dot = mb_strrpos($filename, '.');
return $last_dot !== false ? mb_substr($filename, $last_dot + 1) : '';
}
/**
* Creates boundary for part by number (only if it's missing)
*
* @param int $part_number
*
*/
function CreatePartBoundary($part_number)
{
$part =& $this->parts[$part_number];
if (!isset($part['BOUNDARY'])) {
$part['BOUNDARY'] = md5(uniqid($part_number.time()));
}
}
/**
* Returns ready to use headers associative array of any message part by it's number
*
* @param int $part_number
* @return Array
*/
function GetPartHeaders($part_number)
{
$part =& $this->parts[$part_number];
if (!isset($part['Content-Type'])) {
return $this->SetError('MISSING_CONTENT_TYPE');
}
$full_type = strtolower($part['Content-Type']);
list ($type, $sub_type) = explode('/', $full_type);
$headers['Content-Type'] = $full_type;
switch ($type) {
case 'text':
case 'image':
case 'audio':
case 'video':
case 'application':
case 'message':
// 1. update content-type header
if (isset($part['CHARSET'])) {
$headers['Content-Type'] .= '; charset='.$part['CHARSET'];
}
if (isset($part['NAME'])) {
$headers['Content-Type'] .= '; name="'.$part['NAME'].'"';
}
// 2. set content-transfer-encoding header
if (isset($part['Content-Transfer-Encoding'])) {
$headers['Content-Transfer-Encoding'] = $part['Content-Transfer-Encoding'];
}
// 3. set content-disposition header
if (isset($part['DISPOSITION']) && $part['DISPOSITION']) {
$headers['Content-Disposition'] = $part['DISPOSITION'];
if (isset($part['NAME'])) {
$headers['Content-Disposition'] .= '; filename="'.$part['NAME'].'"';
}
}
break;
case 'multipart':
switch ($sub_type) {
case 'alternative':
case 'related':
case 'mixed':
case 'parallel':
$this->CreatePartBoundary($part_number);
$headers['Content-Type'] .= '; boundary="'.$part['BOUNDARY'].'"';
break;
default:
return $this->SetError('INVALID_MULTIPART_SUBTYPE', Array($sub_type));
}
break;
default:
return $this->SetError('INVALID_CONTENT_TYPE', Array($full_type));
}
// set content-id if any
if (isset($part['Content-ID'])) {
$headers['Content-ID'] = '<'.$part['Content-ID'].'>';
}
return $headers;
}
function GetPartBody($part_number)
{
$part =& $this->parts[$part_number];
if (!isset($part['Content-Type'])) {
return $this->SetError('MISSING_CONTENT_TYPE');
}
$full_type = strtolower($part['Content-Type']);
list ($type, $sub_type) = explode('/', $full_type);
$body = '';
switch ($type) {
// compose text/binary content
case 'text':
case 'image':
case 'audio':
case 'video':
case 'application':
case 'message':
// 1. get content of part
if (isset($part['FILENAME'])) {
// content provided via absolute path to content containing file
$filename = $part['FILENAME'];
$file_size = filesize($filename);
$body = file_get_contents($filename);
if ($body === false) {
return $this->SetError('FILE_PART_OPEN_ERROR', Array($filename));
}
$actual_size = strlen($body);
if (($file_size === false || $actual_size > $file_size) && get_magic_quotes_runtime()) {
$body = stripslashes($body);
}
if ($file_size !== false && $actual_size != $file_size) {
return $this->SetError('FILE_PART_DATA_ERROR', Array($filename));
}
}
else {
// content provided directly as one of part keys
if (!isset($part['DATA'])) {
return $this->SetError('FILE_PART_DATA_MISSING');
}
$body =& $part['DATA'];
}
// 2. get part transfer encoding
$encoding = isset($part['Content-Transfer-Encoding']) ? strtolower($part['Content-Transfer-Encoding']) : '';
if (!in_array($encoding, Array ('', 'base64', 'quoted-printable', '7bit'))) {
return $this->SetError('INVALID_ENCODING', Array($encoding));
}
if ($encoding == 'base64') {
// split base64 encoded text by 76 symbols at line (MIME requirement)
$body = chunk_split( base64_encode($body) );
}
break;
case 'multipart':
// compose multipart message
switch ($sub_type) {
case 'alternative':
case 'related':
case 'mixed':
case 'parallel':
$this->CreatePartBoundary($part_number);
$boundary = $this->line_break.'--'.$part['BOUNDARY'];
foreach ($part['PARTS'] as $multipart_number) {
$body .= $boundary.$this->line_break;
$part_headers = $this->GetPartHeaders($multipart_number);
if ($part_headers === false) {
// some of sub-part headers were invalid
return false;
}
foreach ($part_headers as $header_name => $header_value) {
$body .= $header_name.': '.$header_value.$this->line_break;
}
$part_body = $this->GetPartBody($multipart_number);
if ($part_body === false) {
// part body was invalid
return false;
}
$body .= $this->line_break.$part_body;
}
$body .= $boundary.'--'.$this->line_break;
break;
default:
return $this->SetError('INVALID_MULTIPART_SUBTYPE', Array($sub_type));
}
break;
default:
return $this->SetError('INVALID_CONTENT_TYPE', Array($full_type));
}
return $body;
}
/**
* Applies quoted-printable encoding to specified text
*
* @param string $text
* @param string $header_charset
* @param int $break_lines
* @return unknown
*/
function QuotedPrintableEncode($text, $header_charset = '', $break_lines = 1)
{
$ln = strlen($text);
$h = strlen($header_charset) > 0;
if ($h) {
$s = Array (
'=' => 1,
'?' => 1,
'_' => 1,
'(' => 1,
')' => 1,
'<' => 1,
'>' => 1,
'@' => 1,
',' => 1,
';' => 1,
'"' => 1,
'\\' => 1,
/*
'/' => 1,
'[' => 1,
']' => 1,
':' => 1,
'.' => 1,
*/
);
$b = $space = $break_lines = 0;
for ($i = 0; $i < $ln; $i++) {
if (isset($s[$text[$i]])) {
$b = 1;
break;
}
switch ($o = ord($text[$i])) {
case 9:
case 32:
$space = $i + 1;
$b = 1;
break 2;
case 10:
case 13:
break 2;
default:
if ($o < 32 || $o > 127) {
$b = 1;
break 2;
}
}
}
if($i == $ln) {
return $text;
}
if ($space > 0) {
return substr($text, 0, $space).($space < $ln ? $this->QuotedPrintableEncode(substr($text, $space), $header_charset, 0) : '');
}
}
for ($w = $e = '', $n = 0, $l = 0, $i = 0; $i < $ln; $i++) {
$c = $text[$i];
$o = ord($c);
$en = 0;
switch ($o) {
case 9:
case 32:
if (!$h) {
$w = $c;
$c = '';
}
else {
if ($b) {
if ($o == 32) {
$c = '_';
}
else {
$en = 1;
}
}
}
break;
case 10:
case 13:
if (strlen($w)) {
if ($break_lines && $l + 3 > 75) {
$e .= '='.$this->line_break;
$l = 0;
}
$e .= sprintf('=%02X', ord($w));
$l += 3;
$w = '';
}
$e .= $c;
if ($h) {
$e .= "\t";
}
$l = 0;
continue 2;
case 46:
case 70:
case 102:
$en = (!$h && ($l == 0 || $l + 1 > 75));
break;
default:
if ($o > 127 || $o < 32 || !strcmp($c, '=')) {
$en = 1;
}
elseif ($h && isset($s[$c])) {
$en = 1;
}
break;
}
if (strlen($w)) {
if ($break_lines && $l + 1 > 75) {
$e .= '='.$this->line_break;
$l = 0;
}
$e .= $w;
$l++;
$w = '';
}
if (strlen($c)) {
if ($en) {
$c = sprintf('=%02X', $o);
$el = 3;
$n = 1;
$b = 1;
}
else {
$el = 1;
}
if ($break_lines && $l + $el > 75) {
$e .= '='.$this->line_break;
$l = 0;
}
$e .= $c;
$l += $el;
}
}
if (strlen($w)) {
if ($break_lines && $l + 3 > 75) {
$e .= '='.$this->line_break;
}
$e .= sprintf('=%02X', ord($w));
}
return $h && $n ? '=?'.$header_charset.'?q?'.$e.'?=' : $e;
}
/**
* Sets message header + encodes is by quoted-printable using charset specified
*
* @param string $name
* @param string $value
* @param string $encoding_charset
*/
function SetHeader($name, $value, $encoding_charset = '')
{
if ($encoding_charset) {
// actually for headers base64 method may give shorter result
$value = $this->QuotedPrintableEncode($value, $encoding_charset);
}
$this->headers[$name] = $value;
}
/**
* Sets header + automatically encodes it using default charset
*
* @param string $name
* @param string $value
*/
function SetEncodedHeader($name, $value)
{
$this->SetHeader($name, $value, $this->charset);
}
/**
* Sets header which value is email and username +autoencode
*
* @param string $header
* @param string $address
* @param string $name
*/
function SetEncodedEmailHeader($header, $address, $name)
{
$this->SetHeader($header, $this->QuotedPrintableEncode($name, $this->charset) . ' <' . $address . '>');
}
function SetMultipleEncodedEmailHeader($header, $addresses)
{
$value = '';
foreach ($addresses as $name => $address) {
$value .= $this->QuotedPrintableEncode($name, $this->charset) . ' <' . $address . '>, ';
}
$this->SetHeader($header, substr($value, 0, -2));
}
/**
* Adds new part to message and returns it's number
*
* @param Array $part_definition
* @param int|bool $part_number number of new part
* @return int
*/
function AddPart(&$part_definition, $part_number = false)
{
$part_number = $part_number !== false ? $part_number : count($this->parts);
$this->parts[$part_number] =& $part_definition;
return $part_number;
}
/**
* Returns text version of HTML document
*
* @param string $html
* @param bool $keep_inp_tags
* @return string
*/
function ConvertToText($html, $keep_inp_tags = false)
{
if ( $keep_inp_tags && preg_match_all('/(<[\\/]?)inp2:([^>]*?)([\\/]?>)/s', $html, $regs) ) {
$found_tags = Array ();
foreach ($regs[0] as $index => $tag) {
$tag_placeholder = '%' . md5($index . ':' . $tag) . '%';
$found_tags[$tag_placeholder] = $tag;
// we can have duplicate tags -> replace only 1st occurrence (str_replace can't do that)
$html = preg_replace('/' . preg_quote($tag, '/') . '/', $tag_placeholder, $html, 1);
}
$html = $this->_convertToText($html);
foreach ($found_tags as $tag_placeholder => $tag) {
$html = str_replace($tag_placeholder, $tag, $html);
}
return $html;
}
return $this->_convertToText($html);
}
/**
* Returns text version of HTML document
*
* @param string $html
* @return string
*/
protected function _convertToText($html)
{
$search = Array (
"'(<\/td>.*)[\r\n]+(.*<td)'i",//formating text in tables
"'(<br[ ]?[\/]?>[\r\n]{0,2})|(<\/p>)|(<\/div>)|(<\/tr>)'i",
"'<head>(.*?)</head>'si",
"'<style(.*?)</style>'si",
"'<title>(.*?)</title>'si",
"'<script(.*?)</script>'si",
// "'^[\s\n\r\t]+'", //strip all spacers & newlines in the begin of document
// "'[\s\n\r\t]+$'", //strip all spacers & newlines in the end of document
"'&(quot|#34);'i",
"'&(amp|#38);'i",
"'&(lt|#60);'i",
"'&(gt|#62);'i",
"'&(nbsp|#160);'i",
"'&(iexcl|#161);'i",
"'&(cent|#162);'i",
"'&(pound|#163);'i",
"'&(copy|#169);'i",
"'&#(\d+);'e"
);
$replace = Array (
"\\1\t\\2",
"\n",
"",
"",
"",
"",
// "",
// "",
"\"",
"&",
"<",
">",
" ",
$this->_safeCharEncode(161),
$this->_safeCharEncode(162),
$this->_safeCharEncode(163),
$this->_safeCharEncode(169),
"\$this->_safeCharEncode(\\1)"
);
return strip_tags( preg_replace ($search, $replace, $html) );
}
/**
* Returns symbols, that corresponds given ASCII code and also encodes it into proper charset
*
* @param int $ascii
* @return string
* @access protected
*/
protected function _safeCharEncode($ascii)
{
return mb_convert_encoding(chr($ascii), $this->charset, 'ISO-8859-1');
}
/**
* Add text OR html part to message (optionally encoded)
*
* @param string $text part's text
* @param bool $is_html this html part or not
* @param bool $encode encode message using quoted-printable encoding
*
* @return int number of created part
*/
function CreateTextHtmlPart($text, $is_html = false, $encode = true)
{
if ($is_html) {
// if adding HTML part, then create plain-text part too
$this->CreateTextHtmlPart($this->ConvertToText($text));
}
// in case if text is from $_REQUEST, then line endings are "\r\n", but we need "\n" here
$text = str_replace("\r\n", "\n", $text); // possible case
$text = str_replace("\r", "\n", $text); // impossible case, but just in case replace this too
$definition = Array (
'Content-Type' => $is_html ? 'text/html' : 'text/plain',
'CHARSET' => $this->charset,
'DATA' => $encode ? $this->QuotedPrintableEncode($text) : $text,
);
if ($encode) {
$definition['Content-Transfer-Encoding'] = 'quoted-printable';
}
$guess_name = $is_html ? 'html_part' : 'text_part';
$part_number = $this->guessOptions[$guess_name] !== false ? $this->guessOptions[$guess_name] : false;
$part_number = $this->AddPart($definition, $part_number);
$this->guessOptions[$guess_name] = $part_number;
return $part_number;
}
/**
* Adds attachment part to message
*
* @param string $file name of the file with attachment body
* @param string $attach_name name for attachment (name of file is used, when not specified)
* @param string $content_type content type for attachment
* @param string $content body of file to be attached
* @param bool $inline is attachment inline or not
*
* @return int number of created part
*/
function AddAttachment($file = '', $attach_name = '', $content_type = '', $content = '', $inline = false)
{
$definition = Array (
'Disposition' => $inline ? 'inline' : 'attachment',
'Content-Type' => $content_type ? $content_type : 'automatic/name',
);
if ($file) {
// filename of attachment given
$definition['FileName'] = $file;
}
if ($attach_name) {
// name of attachment given
$definition['Name'] = $attach_name;
}
if ($content) {
// attachment data is given
$definition['Data'] = $content;
}
$definition =& $this->GetFileDefinition($definition);
$part_number = $this->AddPart($definition);
if ($inline) {
// it's inline attachment and needs content-id to be addressed by in message
$this->parts[$part_number]['Content-ID'] = md5(uniqid($part_number.time())).'.'.$this->GetFilenameExtension($attach_name ? $attach_name : $file);
}
$this->guessOptions[$inline ? 'inline_attachments' : 'attachments'][] = $part_number;
return $part_number;
}
/**
* Adds another MIME message as attachment to message being composed
*
* @param string $file name of the file with attachment body
* @param string $attach_name name for attachment (name of file is used, when not specified)
* @param string $content body of file to be attached
*
* @return int number of created part
*/
function AddMessageAttachment($file = '', $attach_name = '', $content = '')
{
$part_number = $this->AddAttachment($file, $attach_name, 'message/rfc822', $content, true);
unset($this->parts[$part_number]['Content-ID']); // messages don't have content-id, but have inline disposition
return $part_number;
}
/**
* Creates multipart of specified type and returns it's number
*
* @param Array $part_numbers
* @param string $multipart_type = {alternative,related,mixed,paralell}
* @return int
*/
function CreateMultipart($part_numbers, $multipart_type)
{
$types = Array ('alternative', 'related' , 'mixed', 'paralell');
if (!in_array($multipart_type, $types)) {
return $this->SetError('INVALID_MULTIPART_SUBTYPE', Array($multipart_type));
}
$definition = Array (
'Content-Type' => 'multipart/'.$multipart_type,
'PARTS' => $part_numbers,
);
return $this->AddPart($definition);
}
/**
* Creates missing content-id header for inline attachments
*
* @param int $part_number
*/
function CreateContentID($part_number)
{
$part =& $this->parts[$part_number];
if (!isset($part['Content-ID']) && $part['DISPOSITION'] == 'inline') {
$part['Content-ID'] = md5(uniqid($part_number.time())).'.'.$this->GetFilenameExtension($part['NAME']);
}
}
/**
* Returns attachment part based on file used in attachment
*
* @param Array $file
* @return Array
*/
function &GetFileDefinition ($file)
{
$name = '';
if (isset($file['Name'])) {
// if name is given directly, then use it
$name = $file['Name'];
}
else {
// auto-guess attachment name based on source filename
$name = isset($file['FileName']) ? basename($file['FileName']) : '';
}
if (!$name || (!isset($file['FileName']) && !isset($file['Data']))) {
// filename not specified || no filename + no direct file content
return $this->SetError('MISSING_FILE_DATA');
}
$encoding = 'base64';
if (isset($file['Content-Type'])) {
$content_type = $file['Content-Type'];
list ($type, $sub_type) = explode('/', $content_type);
switch ($type) {
case 'text':
case 'image':
case 'audio':
case 'video':
case 'application':
break;
case 'message':
$encoding = '7bit';
break;
case 'automatic':
if (!$name) {
return $this->SetError('MISSING_FILE_NAME');
}
$this->guessContentType($name, $content_type, $encoding);
break;
default:
return $this->SetError('INVALID_CONTENT_TYPE', Array($content_type));
}
}
else {
// encoding not passed in file part, then assume, that it's binary
$content_type = 'application/octet-stream';
}
$definition = Array (
'Content-Type' => $content_type,
'Content-Transfer-Encoding' => $encoding,
'NAME' => $name, // attachment name
);
if (isset($file['Disposition'])) {
$disposition = strtolower($file['Disposition']);
if ($disposition == 'inline' || $disposition == 'attachment') {
// valid disposition header value
$definition['DISPOSITION'] = $file['Disposition'];
}
else {
return $this->SetError('INVALID_DISPOSITION', Array($file['Disposition']));
}
}
if (isset($file['FileName'])) {
$definition['FILENAME'] = $file['FileName'];
}
elseif (isset($file['Data'])) {
$definition['DATA'] =& $file['Data'];
}
return $definition;
}
/**
* Returns content-type based on filename extension
*
* @param string $filename
* @param string $content_type
* @param string $encoding
*
* @todo Regular expression used is not completely finished, that's why if extension used for
* comparing in some other extension (from list) part, that partial match extension will be returned.
* Because of two extension that begins with same 2 letters always belong to same content type
* this unfinished regular expression still gives correct result in any case.
*/
function guessContentType($filename, &$content_type, &$encoding)
{
$content_type = kUtil::mimeContentTypeByExtension($filename);
if ( mb_strtolower($this->GetFilenameExtension($filename)) == 'eml' ) {
$encoding = '7bit';
}
}
/**
* Using guess options combines all added parts together and returns combined part number
*
* @return int
*/
function PrepareMessageBody()
{
if ($this->bodyPartNumber === false) {
$part_number = false; // number of generated body part
// 1. set text content of message
if ($this->guessOptions['text_part'] !== false && $this->guessOptions['html_part'] !== false) {
// text & html parts present -> compose into alternative part
$parts = Array (
$this->guessOptions['text_part'],
$this->guessOptions['html_part'],
);
$part_number = $this->CreateMultipart($parts, 'alternative');
}
elseif ($this->guessOptions['text_part'] !== false) {
// only text part is defined, then leave as is
$part_number = $this->guessOptions['text_part'];
}
if ($part_number === false) {
return $this->SetError('MESSAGE_TEXT_MISSING');
}
// 2. if inline attachments found, then create related multipart from text & inline attachments
if ($this->guessOptions['inline_attachments']) {
$parts = array_merge(Array($part_number), $this->guessOptions['inline_attachments']);
$part_number = $this->CreateMultipart($parts, 'related');
}
// 3. if normal attachments found, then create mixed multipart from text & attachments
if ($this->guessOptions['attachments']) {
$parts = array_merge(Array($part_number), $this->guessOptions['attachments']);
$part_number = $this->CreateMultipart($parts, 'mixed');
}
$this->bodyPartNumber = $part_number;
}
return $this->bodyPartNumber;
}
/**
* Returns message headers and body part (by reference in parameters)
*
* @param Array $message_headers
* @param string $message_body
* @return bool
*/
function GetHeadersAndBody(&$message_headers, &$message_body)
{
$part_number = $this->PrepareMessageBody();
if ($part_number === false) {
return $this->SetError('MESSAGE_COMPOSE_ERROR');
}
$message_headers = $this->GetPartHeaders($part_number);
// join message headers and body headers
$message_headers = array_merge($this->headers, $message_headers);
$message_headers['MIME-Version'] = '1.0';
if ($this->mailerName) {
$message_headers['X-Mailer'] = $this->mailerName;
}
$this->GenerateMessageID($message_headers['From']);
$valid_headers = $this->ValidateHeaders($message_headers);
if ($valid_headers) {
// set missing headers from existing
$from_headers = Array ('Reply-To', 'Errors-To');
foreach ($from_headers as $header_name) {
if (!isset($message_headers[$header_name])) {
$message_headers[$header_name] = $message_headers['From'];
}
}
$message_body = $this->GetPartBody($part_number);
return true;
}
return false;
}
/**
* Checks that all required headers are set and not empty
*
* @param Array $message_headers
* @return bool
*/
function ValidateHeaders($message_headers)
{
$from = isset($message_headers['From']) ? $message_headers['From'] : '';
if (!$from) {
return $this->SetError('HEADER_MISSING', Array('From'));
}
if (!isset($message_headers['To'])) {
return $this->SetError('HEADER_MISSING', Array('To'));
}
if (!isset($message_headers['Subject'])) {
return $this->SetError('HEADER_MISSING', Array('Subject'));
}
return true;
}
/**
* Returns full message source (headers + body) for sending to SMTP server
*
* @return string
*/
function GetMessage()
{
$composed = $this->GetHeadersAndBody($message_headers, $message_body);
if ($composed) {
// add headers to resulting message
$message = '';
foreach ($message_headers as $header_name => $header_value) {
$message .= $header_name.': '.$header_value.$this->line_break;
}
// add message body
$message .= $this->line_break.$message_body;
return $message;
}
return false;
}
/**
* Sets just happened error code
*
* @param string $code
* @param Array $params additional error params
* @param bool $fatal
* @return bool
*/
function SetError($code, $params = null, $fatal = true)
{
$error_msgs = Array (
'MAIL_NOT_FOUND' => 'the mail() function is not available in this PHP installation',
'MISSING_CONTENT_TYPE' => 'it was added a part without Content-Type: defined',
'INVALID_CONTENT_TYPE' => 'Content-Type: %s not yet supported',
'INVALID_MULTIPART_SUBTYPE' => 'multipart Content-Type sub_type %s not yet supported',
'FILE_PART_OPEN_ERROR' => 'could not open part file %s',
'FILE_PART_DATA_ERROR' => 'the length of the file that was read does not match the size of the part file %s due to possible data corruption',
'FILE_PART_DATA_MISSING' => 'it was added a part without a body PART',
'INVALID_ENCODING' => '%s is not yet a supported encoding type',
'MISSING_FILE_DATA' => 'file part data is missing',
'MISSING_FILE_NAME' => 'it is not possible to determine content type from the name',
'INVALID_DISPOSITION' => '%s is not a supported message part content disposition',
'MESSAGE_TEXT_MISSING' => 'text part of message was not defined',
'MESSAGE_COMPOSE_ERROR' => 'unknown message composing error',
'HEADER_MISSING' => 'header %s is required',
// SMTP errors
'INVALID_COMMAND' => 'Commands cannot contain newlines',
'CONNECTION_TERMINATED' => 'Connection was unexpectedly closed',
'HELO_ERROR' => 'HELO was not accepted: %s',
'AUTH_METHOD_NOT_SUPPORTED' => '%s is not a supported authentication method',
'AUTH_METHOD_NOT_IMPLEMENTED' => '%s is not a implemented authentication method',
);
if (!is_array($params)) {
$params = Array ();
}
$error_msg = 'mail error: ' . vsprintf($error_msgs[$code], $params);
if ( $this->_logData ) {
$this->_logData['Status'] = EmailLogStatus::ERROR;
$this->_logData['ErrorMessage'] = $error_msg;
$this->Conn->doInsert($this->_logData, TABLE_PREFIX . 'EmailLog');
}
else {
if ($fatal) {
throw new Exception($error_msg);
}
else {
if ( $this->Application->isDebugMode() ) {
$this->Application->Debugger->appendTrace();
}
trigger_error($error_msg, E_USER_WARNING);
}
}
return false;
}
/**
* Simple method of message sending
*
* @param string $from_email
* @param string $to_email
* @param string $subject
* @param string $from_name
* @param string $to_name
*/
function Send($from_email, $to_email, $subject, $from_name = '', $to_name = '')
{
$this->SetSubject($subject);
$this->SetFrom($from_email, trim($from_name) ? trim($from_name) : $from_email);
if (!isset($this->headers['Return-Path'])) {
$this->SetReturnPath($from_email);
}
$this->SetTo($to_email, $to_name ? $to_name : $to_email);
return $this->Deliver();
}
/**
* Prepares class for sending another message
*
*/
function Clear()
{
$this->headers = Array ();
$this->bodyPartNumber = false;
$this->parts = Array ();
$this->guessOptions = Array (
'attachments' => Array (),
'inline_attachments' => Array (),
'text_part' => false,
'html_part' => false,
);
$this->SetCharset(null, true);
$this->_logData = Array ();
}
/**
* Sends message via php mail function
*
* @param Array $message_headers
* @param string $body
*
* @return bool
*/
function SendMail($message_headers, &$body)
{
if (!function_exists('mail')) {
return $this->SetError('MAIL_NOT_FOUND');
}
$to = $message_headers['To'];
$subject = $message_headers['Subject'];
$return_path = $message_headers['Return-Path'];
unset($message_headers['To'], $message_headers['Subject']);
$headers = '';
$header_separator = $this->Application->ConfigValue('MailFunctionHeaderSeparator') == 1 ? "\n" : "\r\n";
foreach ($message_headers as $header_name => $header_value) {
$headers .= $header_name.': '.$header_value.$header_separator;
}
if ($return_path) {
if (kUtil::constOn('SAFE_MODE') || (defined('PHP_OS') && substr(PHP_OS, 0, 3) == 'WIN')) {
// safe mode restriction OR is windows
$return_path = '';
}
}
return mail($to, $subject, $body, $headers, $return_path ? '-f'.$return_path : null);
}
/**
* Sends message via SMTP server
*
* @param Array $message_headers
* @param string $message_body
* @return bool
*/
function SendSMTP($message_headers, &$message_body)
{
if (!$this->SmtpConnect()) {
return false;
}
$from = $this->ExtractRecipientEmail($message_headers['From']);
if (!$this->SmtpSetFrom($from)) {
return false;
}
$recipients = '';
$recipient_headers = Array ('To', 'Cc', 'Bcc');
foreach ($recipient_headers as $recipient_header) {
if (isset($message_headers[$recipient_header])) {
$recipients .= ' '.$message_headers[$recipient_header];
}
}
$recipients_accepted = 0;
$recipients = $this->ExtractRecipientEmail($recipients, true);
foreach ($recipients as $recipient) {
if ($this->SmtpAddTo($recipient)) {
$recipients_accepted++;
}
}
if ($recipients_accepted == 0) {
// none of recipients were accepted
return false;
}
$headers = '';
foreach ($message_headers as $header_name => $header_value) {
$headers .= $header_name.': '.$header_value.$this->line_break;
}
if (!$this->SmtpSendMessage($headers . "\r\n" . $message_body)) {
return false;
}
$this->SmtpDisconnect();
return true;
}
/**
* Send a command to the server with an optional string of
* arguments. A carriage return / linefeed (CRLF) sequence will
* be appended to each command string before it is sent to the
* SMTP server.
*
* @param string $command The SMTP command to send to the server.
* @param string $args A string of optional arguments to append to the command.
*
* @return bool
*
*/
function SmtpSendCommand($command, $args = '')
{
if (!empty($args)) {
$command .= ' ' . $args;
}
if (strcspn($command, "\r\n") !== strlen($command)) {
return $this->SetError('INVALID_COMMAND');
}
return $this->smtpSocket->write($command . "\r\n") === false ? false : true;
}
/**
* Read a reply from the SMTP server. The reply consists of a response code and a response message.
*
* @param mixed $valid The set of valid response codes. These may be specified as an array of integer values or as a single integer value.
*
* @return bool
*
*/
function SmtpParseResponse($valid)
{
$this->smtpResponceCode = -1;
$this->smtpRespoceArguments = array();
while ($line = $this->smtpSocket->readLine()) {
// If we receive an empty line, the connection has been closed.
if (empty($line)) {
$this->SmtpDisconnect();
return $this->SetError('CONNECTION_TERMINATED', null, false);
}
// Read the code and store the rest in the arguments array.
$code = substr($line, 0, 3);
$this->smtpRespoceArguments[] = trim(substr($line, 4));
// Check the syntax of the response code.
if (is_numeric($code)) {
$this->smtpResponceCode = (int)$code;
} else {
$this->smtpResponceCode = -1;
break;
}
// If this is not a multiline response, we're done.
if (substr($line, 3, 1) != '-') {
break;
}
}
// Compare the server's response code with the valid code.
if (is_int($valid) && ($this->smtpResponceCode === $valid)) {
return true;
}
// If we were given an array of valid response codes, check each one.
if (is_array($valid)) {
foreach ($valid as $valid_code) {
if ($this->smtpResponceCode === $valid_code) {
return true;
}
}
}
return false;
}
/**
* Attempt to connect to the SMTP server.
*
* @param int $timeout The timeout value (in seconds) for the socket connection.
* @param bool $persistent Should a persistent socket connection be used ?
*
* @return bool
*
*/
function SmtpConnect($timeout = null, $persistent = false)
{
$result = $this->smtpSocket->connect($this->smtpParams['server'], $this->smtpParams['port'], $persistent, $timeout);
if (!$result) {
return false;
}
if ($this->SmtpParseResponse(220) === false) {
return false;
}
elseif ($this->SmtpNegotiate() === false) {
return false;
}
if ($this->smtpParams['use_auth']) {
$result = $this->SmtpAuthentificate($this->smtpParams['username'], $this->smtpParams['password']);
if (!$result) {
// authentification failed
return false;
}
}
return true;
}
/**
* Attempt to disconnect from the SMTP server.
*
* @return bool
*/
function SmtpDisconnect()
{
if ($this->SmtpSendCommand('QUIT') === false) {
return false;
}
elseif ($this->SmtpParseResponse(221) === false) {
return false;
}
return $this->smtpSocket->disconnect();
}
/**
* Attempt to send the EHLO command and obtain a list of ESMTP
* extensions available, and failing that just send HELO.
*
* @return bool
*/
function SmtpNegotiate()
{
if (!$this->SmtpSendCommand('EHLO', $this->smtpParams['localhost'])) {
return false;
}
if (!$this->SmtpParseResponse(250)) {
// If we receive a 503 response, we're already authenticated.
if ($this->smtpResponceCode === 503) {
return true;
}
// If the EHLO failed, try the simpler HELO command.
if (!$this->SmtpSendCommand('HELO', $this->smtpParams['localhost'])) {
return false;
}
if (!$this->SmtpParseResponse(250)) {
return $this->SetError('HELO_ERROR', Array($this->smtpResponceCode), false);
}
return true;
}
foreach ($this->smtpRespoceArguments as $argument) {
$verb = strtok($argument, ' ');
$arguments = substr($argument, strlen($verb) + 1, strlen($argument) - strlen($verb) - 1);
$this->smtpFeatures[$verb] = $arguments;
}
return true;
}
/**
* Attempt to do SMTP authentication.
*
* @param string $uid The userid to authenticate as.
* @param string $pwd The password to authenticate with.
* @param string $method The requested authentication method. If none is specified, the best supported method will be used.
*
* @return bool
*/
function SmtpAuthentificate($uid, $pwd , $method = '')
{
if (empty($this->smtpFeatures['AUTH'])) {
// server doesn't understand AUTH command, then don't authentificate
return true;
}
$available_methods = explode(' ', $this->smtpFeatures['AUTH']); // methods supported by SMTP server
if (empty($method)) {
foreach ($this->smtpAuthMethods as $supported_method) {
// check if server supports methods, that we have implemented
if (in_array($supported_method, $available_methods)) {
$method = $supported_method;
break;
}
}
} else {
$method = strtoupper($method);
}
if (!in_array($method, $available_methods)) {
// coosen method is not supported by server
return $this->SetError('AUTH_METHOD_NOT_SUPPORTED', Array($method));
}
switch ($method) {
case 'CRAM-MD5':
$result = $this->_authCRAM_MD5($uid, $pwd);
break;
case 'LOGIN':
$result = $this->_authLogin($uid, $pwd);
break;
case 'PLAIN':
$result = $this->_authPlain($uid, $pwd);
break;
default:
return $this->SetError('AUTH_METHOD_NOT_IMPLEMENTED', Array($method));
break;
}
return $result;
}
/**
* Function which implements HMAC MD5 digest
*
* @param string $key The secret key
* @param string $data The data to protect
* @return string The HMAC MD5 digest
*/
function _HMAC_MD5($key, $data)
{
if (strlen($key) > 64) {
$key = pack('H32', md5($key));
}
if (strlen($key) < 64) {
$key = str_pad($key, 64, chr(0));
}
$k_ipad = substr($key, 0, 64) ^ str_repeat(chr(0x36), 64);
$k_opad = substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64);
$inner = pack('H32', md5($k_ipad . $data));
$digest = md5($k_opad . $inner);
return $digest;
}
/**
* Authenticates the user using the CRAM-MD5 method.
*
* @param string $uid The userid to authenticate as.
* @param string $pwd The password to authenticate with.
*
* @return bool
*/
function _authCRAM_MD5($uid, $pwd)
{
if (!$this->SmtpSendCommand('AUTH', 'CRAM-MD5')) {
return false;
}
// 334: Continue authentication request
if (!$this->SmtpParseResponse(334)) {
// 503: Error: already authenticated
return $this->smtpResponceCode === 503 ? true : false;
}
$challenge = base64_decode($this->smtpRespoceArguments[0]);
$auth_str = base64_encode($uid . ' ' . $this->_HMAC_MD5($pwd, $challenge));
if (!$this->SmtpSendCommand($auth_str)) {
return false;
}
// 235: Authentication successful
if (!$this->SmtpParseResponse(235)) {
return false;
}
return true;
}
/**
* Authenticates the user using the LOGIN method.
*
* @param string $uid The userid to authenticate as.
* @param string $pwd The password to authenticate with.
*
* @return bool
*/
function _authLogin($uid, $pwd)
{
if (!$this->SmtpSendCommand('AUTH', 'LOGIN')) {
return false;
}
// 334: Continue authentication request
if (!$this->SmtpParseResponse(334)) {
// 503: Error: already authenticated
return $this->smtpResponceCode === 503 ? true : false;
}
if (!$this->SmtpSendCommand(base64_encode($uid))) {
return false;
}
// 334: Continue authentication request
if (!$this->SmtpParseResponse(334)) {
return false;
}
if (!$this->SmtpSendCommand(base64_encode($pwd))) {
return false;
}
// 235: Authentication successful
if (!$this->SmtpParseResponse(235)) {
return false;
}
return true;
}
/**
* Authenticates the user using the PLAIN method.
*
* @param string $uid The userid to authenticate as.
* @param string $pwd The password to authenticate with.
*
* @return bool
*/
function _authPlain($uid, $pwd)
{
if (!$this->SmtpSendCommand('AUTH', 'PLAIN')) {
return false;
}
// 334: Continue authentication request
if (!$this->SmtpParseResponse(334)) {
// 503: Error: already authenticated
return $this->smtpResponceCode === 503 ? true : false;
}
$auth_str = base64_encode(chr(0) . $uid . chr(0) . $pwd);
if (!$this->SmtpSendCommand($auth_str)) {
return false;
}
// 235: Authentication successful
if (!$this->SmtpParseResponse(235)) {
return false;
}
return true;
}
/**
* Send the MAIL FROM: command.
*
* @param string $sender The sender (reverse path) to set.
* @param string $params String containing additional MAIL parameters, such as the NOTIFY flags defined by RFC 1891 or the VERP protocol.
*
* @return bool
*/
function SmtpSetFrom($sender, $params = null)
{
$args = "FROM:<$sender>";
if (is_string($params)) {
$args .= ' ' . $params;
}
if (!$this->SmtpSendCommand('MAIL', $args)) {
return false;
}
if (!$this->SmtpParseResponse(250)) {
return false;
}
return true;
}
/**
* Send the RCPT TO: command.
*
* @param string $recipient The recipient (forward path) to add.
* @param string $params String containing additional RCPT parameters, such as the NOTIFY flags defined by RFC 1891.
*
* @return bool
*/
function SmtpAddTo($recipient, $params = null)
{
$args = "TO:<$recipient>";
if (is_string($params)) {
$args .= ' ' . $params;
}
if (!$this->SmtpSendCommand('RCPT', $args)) {
return false;
}
if (!$this->SmtpParseResponse(array(250, 251))) {
return false;
}
return true;
}
/**
* Send the DATA command.
*
* @param string $data The message body to send.
*
* @return bool
*/
function SmtpSendMessage($data)
{
/* RFC 1870, section 3, subsection 3 states "a value of zero
* indicates that no fixed maximum message size is in force".
* Furthermore, it says that if "the parameter is omitted no
* information is conveyed about the server's fixed maximum
* message size". */
if (isset($this->smtpFeatures['SIZE']) && ($this->smtpFeatures['SIZE'] > 0)) {
if (strlen($data) >= $this->smtpFeatures['SIZE']) {
$this->SmtpDisconnect();
return $this->SetError('Message size excedes the server limit', null, false);
}
}
// Quote the data based on the SMTP standards
// Change Unix (\n) and Mac (\r) linefeeds into Internet-standard CRLF (\r\n) linefeeds.
$data = preg_replace(Array('/(?<!\r)\n/','/\r(?!\n)/'), "\r\n", $data);
// Because a single leading period (.) signifies an end to the data,
// legitimate leading periods need to be "doubled" (e.g. '..')
$data = str_replace("\n.", "\n..", $data);
if (!$this->SmtpSendCommand('DATA')) {
return false;
}
if (!$this->SmtpParseResponse(354)) {
return false;
}
if ($this->smtpSocket->write($data . "\r\n.\r\n") === false) {
return false;
}
if (!$this->SmtpParseResponse(250)) {
return false;
}
return true;
}
/**
* Sets global charset for every message part
*
* @param string $charset
* @param bool $is_system set charset to default for current language
*/
function SetCharset($charset, $is_system = false)
{
$this->charset = $is_system ? CHARSET : $charset;
}
/**
* Allows to extract recipient's name from text by specifying it's email
*
* @param string $text
* @param string $email
* @return string
*/
function ExtractRecipientName($text, $email = '')
{
$lastspace = mb_strrpos($text, ' ');
$name = trim(mb_substr($text, 0, $lastspace - mb_strlen($text)), " \r\n\t\0\x0b\"'");
if (empty($name)) {
$name = $email;
}
return $name;
}
/**
* Takes $text and returns an email address from it
* Set $multiple to true to retrieve all found addresses
* Returns false if no addresses were found
*
* @param string $text
* @param bool $multiple
* @param bool $allow_only_domain
* @return Array|bool
* @access public
*/
public function ExtractRecipientEmail($text, $multiple = false, $allow_only_domain = false)
{
if ( $allow_only_domain ) {
$pattern = '/((' . REGEX_EMAIL_USER . '@)?' . REGEX_EMAIL_DOMAIN . ')/i';
}
else {
$pattern = '/(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')/i';
}
if ( $multiple ) {
if ( preg_match_all($pattern, $text, $found_emails) >= 1 ) {
return $found_emails[1];
}
else {
return false;
}
}
else {
if ( preg_match($pattern, $text, $found_emails) == 1 ) {
return $found_emails[1];
}
else {
return false;
}
}
}
/**
* Returns array of recipient names and emails
*
* @param string $list
* @param string $separator
* @return Array
*/
function GetRecipients($list, $separator = ';')
{
// by MIME specs recipients should be separated using "," symbol,
// but users can write ";" too (like in OutLook)
if (!trim($list)) {
return false;
}
$list = explode(',', str_replace($separator, ',', $list));
$ret = Array ();
foreach ($list as $recipient) {
$email = $this->ExtractRecipientEmail($recipient);
if (!$email) {
// invalid email format -> error
return false;
}
$name = $this->ExtractRecipientName($recipient, $email);
$ret[] = Array('Name' => $name, 'Email' => $email);
}
return $ret;
}
/* methods for nice header setting */
/**
* Sets "From" header.
*
* @param string $email
* @param string $first_last_name FirstName and LastName or just FirstName
* @param string $last_name LastName (if not specified in previous parameter)
*/
function SetFrom($email, $first_last_name, $last_name = '')
{
$name = rtrim($first_last_name.' '.$last_name, ' ');
$this->SetEncodedEmailHeader('From', $email, $name ? $name : $email);
if (!isset($this->headers['Return-Path'])) {
$this->SetReturnPath($email);
}
}
/**
* Sets "To" header.
*
* @param string $email
* @param string $first_last_name FirstName and LastName or just FirstName
* @param string $last_name LastName (if not specified in previous parameter)
*/
function SetTo($email, $first_last_name, $last_name = '')
{
$name = rtrim($first_last_name.' '.$last_name, ' ');
$email = $this->_replaceRecipientEmail($email);
$this->SetEncodedEmailHeader('To', $email, $name ? $name : $email);
}
/**
* Sets "Return-Path" header (useful for spammers)
*
* @param string $email
*/
function SetReturnPath($email)
{
$this->SetHeader('Return-Path', $email);
}
/**
* Adds one more recipient into "To" header
*
* @param string $email
* @param string $first_last_name FirstName and LastName or just FirstName
* @param string $last_name LastName (if not specified in previous parameter)
*/
function AddTo($email, $first_last_name = '', $last_name = '')
{
$name = rtrim($first_last_name.' '.$last_name, ' ');
$this->AddRecipient('To', $email, $name);
}
/**
* Allows to replace recipient in all sent emails (used for debugging)
*
* @param string $email
* @return string
*/
function _replaceRecipientEmail($email)
{
if ( defined('OVERRIDE_EMAIL_RECIPIENTS') && OVERRIDE_EMAIL_RECIPIENTS ) {
if ( substr(OVERRIDE_EMAIL_RECIPIENTS, 0, 1) == '@' ) {
// domain
$email = str_replace('@', '_at_', $email) . OVERRIDE_EMAIL_RECIPIENTS;
}
else {
$email = OVERRIDE_EMAIL_RECIPIENTS;
}
}
return $email;
}
/**
* Adds one more recipient into "Cc" header
*
* @param string $email
* @param string $first_last_name FirstName and LastName or just FirstName
* @param string $last_name LastName (if not specified in previous parameter)
*/
function AddCc($email, $first_last_name = '', $last_name = '')
{
$name = rtrim($first_last_name.' '.$last_name, ' ');
$this->AddRecipient('Cc', $email, $name);
}
/**
* Adds one more recipient into "Bcc" header
*
* @param string $email
* @param string $first_last_name FirstName and LastName or just FirstName
* @param string $last_name LastName (if not specified in previous parameter)
*/
function AddBcc($email, $first_last_name = '', $last_name = '')
{
$name = rtrim($first_last_name.' '.$last_name, ' ');
$this->AddRecipient('Bcc', $email, $name);
}
/**
* Adds one more recipient to specified header
*
* @param string $header_name
* @param string $email
* @param string $name
*/
function AddRecipient($header_name, $email, $name = '')
{
$email = $this->_replaceRecipientEmail($email);
if (!$name) {
$name = $email;
}
$value = isset($this->headers[$header_name]) ? $this->headers[$header_name] : '';
if ( $value ) {
// not first recipient added - separate with comma
$value .= ', ';
}
$value .= $this->QuotedPrintableEncode($name, $this->charset) . ' <' . $email . '>';
$this->SetHeader($header_name, $value);
}
/**
* Sets "Subject" header.
*
* @param string $subject message subject
*/
function SetSubject($subject)
{
$this->setEncodedHeader('Subject', $subject);
}
/**
* Sets HTML part of message
*
* @param string $html
*/
function SetHTML($html)
{
$this->CreateTextHtmlPart($html, true);
}
/**
* Sets Plain-Text part of message
*
* @param string $plain_text
*/
function SetPlain($plain_text)
{
$this->CreateTextHtmlPart($plain_text);
}
/**
* Sets HTML and optionally plain part of the message
*
* @param string $html
* @param string $plain_text
*/
function SetBody($html, $plain_text = '')
{
$this->SetHTML($html);
if ($plain_text) {
$this->SetPlain($plain_text);
}
}
/**
* Performs mail delivery (supports delayed delivery)
*
* @param string $message message, if not given, then use composed one
* @param bool $immediate_send send message now or MailingId
* @param bool $immediate_clear clear message parts after message is sent
* @return bool
*/
function Deliver($message = null, $immediate_send = true, $immediate_clear = true)
{
if (isset($message)) {
// if message is given directly, then use it
if (is_array($message)) {
$message_headers =& $message[0];
$message_body =& $message[1];
}
else {
$message_headers = Array ();
list ($headers, $message_body) = explode("\n\n", $message, 2);
$headers = explode("\n", $headers);
foreach ($headers as $header) {
$header = explode(':', $header, 2);
$message_headers[ trim($header[0]) ] = trim($header[1]);
}
}
$composed = true;
} else {
// direct message not given, then assemble message from available parts
$composed = $this->GetHeadersAndBody($message_headers, $message_body);
}
if ( $composed ) {
if ( $immediate_send === true ) {
$send_method = 'Send' . $this->sendMethod;
$result = $this->$send_method($message_headers, $message_body);
if ( $result && $this->_logData ) {
// add e-mail log record
$this->Conn->doInsert($this->_logData, TABLE_PREFIX . 'EmailLog');
}
if ( $immediate_clear ) {
$this->Clear();
}
return $result;
}
else {
$fields_hash = Array (
'ToEmail' => $message_headers['To'],
'Subject' => $message_headers['Subject'],
'Queued' => time(),
'SendRetries' => 0,
'LastSendRetry' => 0,
'MailingId' => (int)$immediate_send,
'LogData' => serialize($this->_logData), // remember e-mail log record
);
$fields_hash['MessageHeaders'] = serialize($message_headers);
$fields_hash['MessageBody'] =& $message_body;
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'EmailQueue');
if ( $immediate_clear ) {
$this->Clear();
}
}
}
// if not immediate send, then send result is positive :)
return $immediate_send !== true ? true : false;
}
/**
* Sets log data
*
* @param string $log_data
* @return void
* @access public
*/
public function setLogData($log_data)
{
$this->_logData = $log_data;
}
- }
\ No newline at end of file
+ }
Index: branches/5.3.x/core/kernel/utility/unit_config.php
===================================================================
--- branches/5.3.x/core/kernel/utility/unit_config.php (revision 16155)
+++ branches/5.3.x/core/kernel/utility/unit_config.php (revision 16156)
@@ -1,1402 +1,1399 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2012 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
/**
* TODO: Rename following settings
*
* ConfigMapping -> ConfigMappings
* StatusField -> StatusFields
* PermSection -> PermSections
*/
/**
* @method Array getFilterMenu(mixed $default = false)
* @method kUnitConfig setFilterMenu(Array $value)
*
* @method Array getConfigMapping(mixed $default = false)
* @method kUnitConfig setConfigMapping(Array $value)
*
*
* @method string getModuleFolder(mixed $default = false)
* @method kUnitConfig setModuleFolder(string $value)
*
* @method string getBasePath(mixed $default = false)
* @method kUnitConfig setBasePath(string $value)
*
* @method Array getEventHandlerClass(mixed $default = false)
* @method kUnitConfig setEventHandlerClass(Array $value)
*
* @method Array getTagProcessorClass(mixed $default = false)
* @method kUnitConfig setTagProcessorClass(Array $value)
*
* @method Array getItemClass(mixed $default = false)
* @method kUnitConfig setItemClass(Array $value)
*
* @method Array getListClass(mixed $default = false)
* @method kUnitConfig setListClass(Array $value)
*
* @method Array getValidatorClass(mixed $default = false)
* @method kUnitConfig setValidatorClass(Array $value)
*
* @method Array getQueryString(mixed $default = false)
* @method kUnitConfig setQueryString(Array $value)
*
* @method string getPermItemPrefix(mixed $default = false)
* @method kUnitConfig setPermItemPrefix(string $value)
*
* @method string getPermTabText(mixed $default = false)
* @method kUnitConfig setPermTabText(string $value)
*
* @method Array getPermSection(mixed $default = false)
* @method kUnitConfig setPermSection(Array $value)
*
* @method bool getAutoLoad(mixed $default = false)
* @method kUnitConfig setAutoLoad(bool $value)
*
* @method string getIDField(mixed $default = false)
* @method kUnitConfig setIDField(string $value)
*
* @method string getTableName(mixed $default = false)
* @method kUnitConfig setTableName(string $value)
*
* @method string getCustomDataTableName(mixed $default = false)
* @method kUnitConfig setCustomDataTableName(string $value)
*
* @method kUnitConfig setStatusField(Array $value)
*
* @method string getTitleField(mixed $default = false)
* @method kUnitConfig setTitleField(string $value)
*
* @method string getOrderField(mixed $default = false)
* @method kUnitConfig setOrderField(string $value)
*
* @method string getOwnerField(mixed $default = false)
* @method kUnitConfig setOwnerField(string $value)
*
* @method int getConfigPriority(mixed $default = false)
* @method kUnitConfig setConfigPriority(int $value)
*
* @method bool getCatalogItem(mixed $default = false)
* @method kUnitConfig setCatalogItem(bool $value)
*
* @method string getCatalogSelectorName(mixed $default = false)
* @method kUnitConfig setCatalogSelectorName(string $value)
*
* @method string getAdminTemplatePath(mixed $default = false)
* @method kUnitConfig setAdminTemplatePath(string $value)
*
* @method string getAdminTemplatePrefix(mixed $default = false)
* @method kUnitConfig setAdminTemplatePrefix(string $value)
*
* @method string getSearchConfigPostfix(mixed $default = false)
* @method kUnitConfig setSearchConfigPostfix(string $value)
*
* @method string getTitlePhrase(mixed $default = false)
* @method kUnitConfig setTitlePhrase(string $value)
*
* @method int getItemType(mixed $default = false)
* @method kUnitConfig setItemType(int $value)
*
* @method Array getStatisticsInfo(mixed $default = false)
* @method kUnitConfig setStatisticsInfo(Array $value)
*
* @method string getViewMenuPhrase(mixed $default = false)
* @method kUnitConfig setViewMenuPhrase(string $value)
*
* @method string getCatalogTabIcon(mixed $default = false)
* @method kUnitConfig setCatalogTabIcon(string $value)
*
* @method bool getCacheModRewrite(mixed $default = false)
* @method kUnitConfig setCacheModRewrite(bool $value)
*
* @method kUnitConfig setParentTableKey(mixed $value)
*
* @method kUnitConfig setForeignKey(mixed $value)
*
* @method string getConstrain(mixed $default = false)
* @method kUnitConfig setConstrain(string $value)
*
* @method bool getAutoDelete(mixed $default = false)
* @method kUnitConfig setAutoDelete(bool $value)
*
* @method bool getAutoClone(mixed $default = false)
* @method kUnitConfig setAutoClone(bool $value)
*
* @method string getParentPrefix(mixed $default = false)
* @method kUnitConfig setParentPrefix(string $value)
*
* @method bool getCheckSimulatniousEdit(mixed $default = false)
* @method kUnitConfig setCheckSimulatniousEdit(bool $value)
*
* @method bool getPortalStyleEnv(mixed $default = false)
* @method kUnitConfig setPortalStyleEnv(bool $value)
*
* @method int getRewritePriority(mixed $default = false)
* @method kUnitConfig setRewritePriority(int $value)
*
* @method Array getRewriteListener(mixed $default = false)
* @method kUnitConfig setRewriteListener(Array $value)
*
* @method bool getForceDontLogChanges(mixed $default = false)
* @method kUnitConfig setForceDontLogChanges(bool $value)
*
* @method Array getUserProfileMapping(mixed $default = false)
* @method kUnitConfig setUserProfileMapping(Array $value)
*
* @method bool getUsePendingEditing(mixed $default = false)
* @method kUnitConfig setUsePendingEditing(bool $value)
*
* @method string getNavigationSelectClause(mixed $default = false)
* @method kUnitConfig setNavigationSelectClause(string $value)
*
* @method bool getPopulateMlFields(bool $default = false)
* @method kUnitConfig setPopulateMlFields(bool $value)
*
* @method bool getLogChanges(bool $default = false)
* @method kUnitConfig setLogChanges(bool $value)
*
* @method int getFileCount(bool $default = false)
* @method kUnitConfig setFileCount(int $value)
*
* @method int getImageCount(bool $default = false)
* @method kUnitConfig setImageCount(int $value)
*
* @method string getDownloadHelperClass(bool $default = false)
* @method kUnitConfig setDownloadHelperClass(string $value)
*
* @method string getSectionPrefix(bool $default = false)
* @method kUnitConfig setSectionPrefix(string $value)
*
* @method bool getSiteConfigProcessed(bool $default = false)
* @method kUnitConfig setSiteConfigProcessed(bool $value)
*
*
* @method Array getRegisterClasses(mixed $default = false)
* @method kUnitConfig setRegisterClasses(Array $value)
* @method kUnitConfig addRegisterClasses(Array $value, string $name = null)
* @method kUnitConfig removeRegisterClasses(string $name = null)
*
* @method Array getScheduledTasks(mixed $default = false)
* @method Array getScheduledTaskByName(string $name, mixed $default = false)
* @method kUnitConfig setScheduledTasks(Array $value)
* @method kUnitConfig addScheduledTasks(Array $value, string $name = null)
* @method kUnitConfig removeScheduledTasks(string $name = null)
*
* @method Array getTitlePresets(mixed $default = false)
* @method Array getTitlePresetByName(string $name, mixed $default = false)
* @method kUnitConfig setTitlePresets(Array $value)
* @method kUnitConfig addTitlePresets(Array $value, string $name = null)
* @method kUnitConfig removeTitlePresets(string $name = null)
*
* @method Array getSections(mixed $default = false)
* @method Array getSectionByName(string $name, mixed $default = false)
* @method kUnitConfig setSections(Array $value)
* @method kUnitConfig addSections(Array $value, string $name = null)
* @method kUnitConfig removeSections(string $name = null)
*
* @method Array getFields(mixed $default = false)
* @method Array getFieldByName(string $name, mixed $default = false)
* @method kUnitConfig setFields(Array $value)
* @method kUnitConfig addFields(Array $value, string $name = null)
* @method kUnitConfig removeFields(string $name = null)
*
* @method Array getVirtualFields(mixed $default = false)
* @method Array getVirtualFieldByName(string $name, mixed $default = false)
* @method kUnitConfig setVirtualFields(Array $value)
* @method kUnitConfig addVirtualFields(Array $value, string $name = null)
* @method kUnitConfig removeVirtualFields(string $name = null)
*
* @method Array getGrids(mixed $default = false)
* @method Array getGridByName(string $name, mixed $default = false)
* @method kUnitConfig setGrids(Array $value)
* @method kUnitConfig addGrids(Array $value, string $name = null)
* @method kUnitConfig removeGrids(string $name = null)
*
* @method Array getHooks(mixed $default = false)
* @method kUnitConfig setHooks(Array $value)
* @method kUnitConfig addHooks(Array $value, string $name = null)
* @method kUnitConfig removeHooks(string $name = null)
*
* @method Array getAggregateTags(mixed $default = false)
* @method kUnitConfig setAggregateTags(Array $value)
* @method kUnitConfig addAggregateTags(Array $value, string $name = null)
* @method kUnitConfig removeAggregateTags(string $name = null)
*
* @method Array getEditTabPresets(mixed $default = false)
* @method Array getEditTabPresetByName(string $name, mixed $default = false)
* @method kUnitConfig setEditTabPresets(Array $value)
* @method kUnitConfig addEditTabPresets(Array $value, string $name = null)
* @method kUnitConfig removeEditTabPresets(string $name = null)
*
* @method Array getSubItems(mixed $default = false)
* @method kUnitConfig setSubItems(Array $value)
* @method kUnitConfig addSubItems(string $value, string $name = null)
* @method kUnitConfig removeSubItems(string $name = null)
*
* @method Array getCustomFields(mixed $default = false)
* @method string getCustomFieldByName(string $name, mixed $default = false)
* @method kUnitConfig setCustomFields(Array $value)
* @method kUnitConfig addCustomFields(Array $value, string $name = null)
* @method kUnitConfig removeCustomFields(string $name = null)
*
* @method Array getClones(mixed $default = false)
* @method Array getCloneByName(string $name, mixed $default = false)
* @method kUnitConfig setClones(Array $value)
* @method kUnitConfig addClones(Array $value, string $name = null)
* @method kUnitConfig removeClones(string $name = null)
*
* @method Array getProcessPrefixes(mixed $default = false)
* @method kUnitConfig setProcessPrefixes(Array $value)
* @method kUnitConfig addProcessPrefixes(string $value, string $name = null)
* @method kUnitConfig removeProcessPrefixes(string $name = null)
*
* @method Array getForms(mixed $default = false)
* @method Array getFormByName(string $name, mixed $default = false)
* @method kUnitConfig setForms(Array $value)
* @method kUnitConfig addForms(Array $value, string $name = null)
* @method kUnitConfig removeForms(string $name = null)
*
* @method Array getReplacementTemplates(mixed $default = false)
* @method string getReplacementTemplateByName(string $name, mixed $default = false)
* @method kUnitConfig setReplacementTemplates(Array $value)
* @method kUnitConfig addReplacementTemplates(string $value, string $name = null)
* @method kUnitConfig removeReplacementTemplates(string $name = null)
*
* @method Array getItemPropertyMappings(mixed $default = false)
* @method string getItemPropertyMappingByName(string $name, mixed $default = false)
* @method kUnitConfig setItemPropertyMappings(Array $value)
* @method kUnitConfig addItemPropertyMappings(string $value, string $name = null)
* @method kUnitConfig removeItemPropertyMappings(string $name = null)
*
* @method Array getSectionAdjustments(mixed $default = false)
* @method mixed getSectionAdjustmentByName(string $name, mixed $default = false)
* @method kUnitConfig setSectionAdjustments(Array $value)
* @method kUnitConfig addSectionAdjustments(mixed $value, string $name = null)
* @method kUnitConfig removeSectionAdjustments(string $name = null)
*
* @method Array getImportKeys(mixed $default = false)
* @method kUnitConfig setImportKeys(Array $value)
* @method kUnitConfig addImportKeys(mixed $value, string $name = null)
* @method kUnitConfig removeImportKeys(string $name = null)
*
*
* @method Array getCalculatedFieldSpecials(mixed $default = Array ())
* @method Array getCalculatedFieldsBySpecial(mixed $special, mixed $default = Array ())
* @method kUnitConfig setCalculatedFieldsBySpecial(mixed $special, Array $value)
* @method kUnitConfig addCalculatedFieldsBySpecial(mixed $special, mixed $value, string $name = null)
* @method kUnitConfig removeCalculatedFieldsBySpecial(mixed $special, string $name = null)
*
* @method Array getAggregatedCalculatedFieldSpecials(mixed $default = Array ())
* @method Array getAggregatedCalculatedFieldsBySpecial(mixed $special, mixed $default = Array ())
* @method kUnitConfig setAggregatedCalculatedFieldsBySpecial(mixed $special, Array $value)
* @method kUnitConfig addAggregatedCalculatedFieldsBySpecial(mixed $special, mixed $value, string $name = null)
* @method kUnitConfig removeAggregatedCalculatedFieldsBySpecial(mixed $special, string $name = null)
*
* @method Array getListSQLSpecials(mixed $default = Array ())
* @method string getListSQLsBySpecial(mixed $special, mixed $default = Array ())
* @method kUnitConfig setListSQLsBySpecial(mixed $special, string $value)
* @method kUnitConfig removeListSQLsBySpecial(mixed $special, string $name = null)
*
* @method Array getListSortingSpecials(mixed $default = Array ())
* @method Array getListSortingsBySpecial(mixed $special, mixed $default = Array ())
* @method kUnitConfig setListSortingsBySpecial(mixed $special, Array $value)
* @method kUnitConfig addListSortingsBySpecial(mixed $special, mixed $value, string $name = null)
* @method kUnitConfig removeListSortingsBySpecial(mixed $special, string $name = null)
*
* @method Array getItemSQLSpecials(mixed $default = Array ())
* @method string getItemSQLsBySpecial(mixed $special, mixed $default = Array ())
* @method kUnitConfig setItemSQLsBySpecial(mixed $special, string $value)
* @method kUnitConfig removeItemSQLsBySpecial(mixed $special, string $name = null)
*/
class kUnitConfig {
/**
* Reference to global kApplication instance
*
* @var kApplication
* @access protected
*/
protected $Application = null;
/**
* Connection to database
*
* @var IDBConnection
* @access protected
*/
protected $Conn = null;
/**
* Unit config prefix
*
* @var string
* @access protected
*/
protected $_prefix = '';
/**
* Filename, where unit config is stored
*
* @var string
* @access protected
*/
protected $_filename = '';
/**
* Default unit config data
*
* @var Array
* @access protected
*/
protected static $_defaults = Array (
'ItemClass' => Array ('class' => 'kDBItem', 'file' => '', 'build_event' => 'OnItemBuild'),
'ListClass' => Array ('class' => 'kDBList', 'file' => '', 'build_event' => 'OnListBuild'),
'EventHandlerClass' => Array ('class' => 'kDBEventHandler', 'file' => '', 'build_event' => 'OnBuild'),
'TagProcessorClass' => Array ('class' => 'kDBTagProcessor', 'file' => '', 'build_event' => 'OnBuild'),
'AutoLoad' => true,
'QueryString' => Array (
1 => 'id',
2 => 'Page',
3 => 'PerPage',
4 => 'event',
5 => 'mode',
),
'ListSQLs' => Array (
'' => ' SELECT %1$s.* %2$s
FROM %1$s',
),
'Fields' => Array (),
);
/**
* Word endings, that are suffixed with "es" instead of just "s" during pluralisation
*
* @var string
* @access protected
*/
protected static $_singularEndingsRegExp = '/(x|s|z|sh|ch)$/';
/**
* Words, that ends with es/s, but are always singulars
*
* @var string
* @access protected
*/
protected static $_alwaysSingularRegExp = '/(class|LogChanges|ForceDontLogChanges|PopulateMlFields)$/i';
/**
* Unit config data
*
* @var Array
* @access protected
*/
protected $_data = Array ();
/**
* Unit config settings that can have different values based on special
*
* @var Array
* @access protected
*/
protected $_specialBased = Array (
'CalculatedFields', 'AggregatedCalculatedFields', 'ListSQLs', 'ListSortings', 'ItemSQLs',
);
/**
* Unit config settings, that allow data to be added without a key
*
* @var Array
* @access protected
*/
protected $_withoutKeys = Array (
'RegisterClasses', 'Hooks', 'AggregateTags'
);
/**
* Dynamic method name, that was called
*
* @var string
* @access protected
*/
protected $_currentMethodName = '';
/**
* Arguments given to dynamic method name, that was called
*
* @var Array
* @access protected
*/
protected $_currentArguments = Array ();
/**
* Creates new instance of kUnitConfig class
*
* @param string $prefix
* @param Array $defaults
* @param bool $use_global_defaults
* @access public
*/
public function __construct($prefix, $defaults = null, $use_global_defaults = true)
{
$this->_prefix = $prefix;
$merge_with = $use_global_defaults ? self::$_defaults : Array ();
$this->_data = array_merge($merge_with, isset($defaults) ? $defaults : Array ());
$this->Application =& kApplication::Instance();
$this->Conn =& $this->Application->GetADODBConnection();
}
/**
* Sets filename, where unit config is stored
*
* @param string $filename
* @return void
* @access public
*/
public function setFilename($filename)
{
$this->_filename = $filename;
}
/**
* Returns unit config prefix
*
* @return string
* @access public
*/
public function getPrefix()
{
return $this->_prefix;
}
/**
* Ensures, that only unit config data is saved when object is serialized
*
* @return Array
* @access public
*/
public function __sleep()
{
return Array ('_prefix', '_data', 'Application', 'Conn');
}
/**
* Dumps unit config into a file
*
* @return void
* @access public
*/
public function dump()
{
kUtil::print_r($this->_data, 'Unit Config', true);
}
/**
* Returns unit config data in raw form
*
* @return Array
* @access public
*/
public function getRaw()
{
return $this->_data;
}
/**
* Processed dynamically created methods for changing unit config settings
*
* @param string $method_name
* @param Array $arguments
* @return mixed
* @throws InvalidArgumentException
* @throws RuntimeException
* @access public
*/
public function __call($method_name, $arguments)
{
// === regular ===
// get{SettingName}()
// set{SettingName}($value)
// add{SettingName}s(string $value, string $name = null)
// remove{SettingName}(string $name = null)
// === by special ===
// get{SettingName}Specials()
// get{SettingName}sBySpecial($special)
// set{SettingName}BySpecial($special, $value)
// add{SettingName}sBySpecial(string $special, Array $value, string $name = null)
// remove{SettingName}sBySpecial(string $special, $name = null)
if ( !preg_match('/^(get|set|add|remove)(.*?)(BySpecial|Specials|ByName)*$/', $method_name, $regs) ) {
throw new RuntimeException('Unknown method <strong>' . __CLASS__ . '::' . $this->_currentMethodName . '</strong>');
}
$this->_currentMethodName = $method_name;
$this->_currentArguments = $arguments;
$to_call = '_process' . ucfirst($regs[1]);
return $this->$to_call($regs[2], isset($regs[3]) ? $regs[3] : '');
}
/**
* Processes calls to "get*" methods
*
* @param string $setting_name
* @param string $suffix
* @return mixed
* @access protected
*/
protected function _processGet($setting_name, $suffix = '')
{
$is_plural = $this->_isPluralSetting($setting_name);
if ( $suffix == 'BySpecial' && $is_plural ) {
// get{SettingName}BySpecial($special, $default = false)
$this->_verifyArguments(Array ('special'));
return $this->getSetting($setting_name, $this->_getDefaultValue(1, Array ()), $this->_currentArguments[0]);
}
elseif ( $suffix == 'Specials' && !$is_plural ) {
// get{SettingName}Specials($default = Array ())
$result = $this->getSetting($this->_getPlural($setting_name), $this->_getDefaultValue(0, Array ()));
return array_keys($result);
}
elseif ( $suffix == 'ByName' && !$is_plural ) {
$sub_key = $this->_currentArguments[0];
$result = $this->getSetting($this->_getPlural($setting_name), Array ());
return isset($result[$sub_key]) ? $result[$sub_key] : $this->_getDefaultValue(1, false);
}
// get{SettingName}($default = false)
return $this->getSetting($setting_name, $this->_getDefaultValue(0, false));
}
/**
* Returns default value from given argument or false, when not passed
*
* @param int $arg_index
* @param mixed $default
* @return bool
* @access protected
*/
protected function _getDefaultValue($arg_index, $default = false)
{
return isset($this->_currentArguments[$arg_index]) ? $this->_currentArguments[$arg_index] : $default;
}
/**
* Processes calls to "set*" methods
*
* @param string $setting_name
* @param string $suffix
* @return kUnitConfig
* @access protected
*/
protected function _processSet($setting_name, $suffix = '')
{
$is_plural = $this->_isPluralSetting($setting_name);
if ( $suffix == 'BySpecial' && $is_plural ) {
// set{SettingName}BySpecial($special, $value)
$this->_verifyArguments(Array ('special', 'value'));
return $this->setSetting($setting_name, $this->_currentArguments[1], $this->_currentArguments[0]);
}
// set{SettingName}($value)
$this->_verifyArguments(Array ('value'));
return $this->setSetting($setting_name, $this->_currentArguments[0]);
}
/**
* Processes calls to "add*" method
*
* @param string $setting_name
* @param string $suffix
* @return kUnitConfig
* @access protected
* @throws InvalidArgumentException
*/
protected function _processAdd($setting_name, $suffix = '')
{
$arguments = $this->_currentArguments;
if ( !$this->_isPluralSetting($setting_name) ) {
throw new InvalidArgumentException('Setting "' . $setting_name . '" isn\'t plural');
}
if ( $suffix == 'BySpecial' ) {
// add{SettingName}BySpecial(string $special, string $value, string $name = null)
$this->_verifyArguments(Array ('special', 'value'));
if ( isset($arguments[2]) ) {
// add a single value
$this->_addToSetting($this->_getSingular($setting_name), $arguments[1], $arguments[2], $arguments[0]);
}
else {
// add multiple values
$this->_addToSetting($setting_name, $arguments[1], null, $arguments[0]);
}
}
else {
// add{SettingName}(string $value, string $name = null)
$this->_verifyArguments(Array ('value'));
if ( isset($arguments[1]) ) {
// add a single value
$this->_addToSetting($this->_getSingular($setting_name), $arguments[0], $arguments[1]);
}
else {
// add multiple value
$this->_addToSetting($setting_name, $arguments[0], null);
}
}
return $this;
}
/**
* Adds a value to a setting
*
* @param string $name
* @param mixed $value
* @param string $array_key
* @param string $special
* @return kUnitConfig
* @throws InvalidArgumentException
*/
protected function _addToSetting($name, $value, $array_key, $special = null)
{
if ( $this->_isPluralSetting($name) ) {
// multiple values given - merge with current values
if ( !is_array($value) ) {
throw new InvalidArgumentException('Argument $value must be an array');
}
$result = $this->getSetting($name, Array (), $special);
$this->setSetting($name, array_merge($result, $value), $special);
}
else {
// single value given
$result = $this->getSetting($this->_getPlural($name), Array (), $special);
if ( $this->_isWithoutKeySetting($name) ) {
$result[] = $value;
}
else {
$result[$array_key] = $value;
}
$this->setSetting($this->_getPlural($name), $result, $special);
}
return $this;
}
/**
* Processes calls to "remove*" method
*
* @param string $setting_name
* @param string $suffix
* @return kUnitConfig
* @access protected
* @throws InvalidArgumentException
*/
protected function _processRemove($setting_name, $suffix = '')
{
$arguments = $this->_currentArguments;
if ( !$this->_isPluralSetting($setting_name) ) {
throw new InvalidArgumentException('Setting "' . $setting_name . '" isn\'t plural');
}
if ( $suffix == 'BySpecial' ) {
// remove{SettingName}BySpecial(string $special, string $name = null)
$this->_verifyArguments(Array ('special'));
if ( isset($arguments[1]) ) {
// remove single value
$this->_removeFromSetting($this->_getSingular($setting_name), $arguments[1], $arguments[0]);
}
else {
// remove multiple value
$this->_removeFromSetting($setting_name, null, $arguments[0]);
}
}
else {
// remove{SettingName}(string $name = null)
if ( isset($arguments[0]) ) {
// remove single value
$this->_removeFromSetting($this->_getSingular($setting_name), $arguments[0]);
}
else {
// remove multiple values
$this->_removeFromSetting($setting_name, null);
}
}
return $this;
}
/**
* Removes a value from a setting
*
* @param string $name
* @param string $array_key
* @param string $special
* @return kUnitConfig
* @access protected
* @throws RuntimeException
*/
protected function _removeFromSetting($name, $array_key = null, $special = null)
{
if ( $this->_isPluralSetting($name) ) {
// remove multiple values
if ( $this->getSetting($name, false, $special) !== false ) {
$this->setSetting($name, null, $special);
}
}
else {
// remove single value
if ( $this->_isWithoutKeySetting($name) ) {
throw new RuntimeException('Unable to change setting without key');
}
$result = $this->getSetting($this->_getPlural($name), false, $special);
if ( $result !== false ) {
unset($result[$array_key]);
$this->setSetting($this->_getPlural($name), $result, $special);
}
}
return $this;
}
/**
* Verifies argument count given
*
* @param Array $argument_names
* @return void
* @access protected
* @throws InvalidArgumentException
*/
protected function _verifyArguments($argument_names)
{
if ( count($this->_currentArguments) < count($argument_names) ) {
throw new InvalidArgumentException('Method <strong>' . __CLASS__ . '::' . $this->_currentMethodName . '</strong> expects following arguments: <strong>$' . implode('</strong>, <strong>$', $argument_names) . '</strong>');
}
}
/**
* Returns setting value by name and filter by special (if passed)
*
* @param string $name
* @param mixed $default
* @param string|kBase $special
* @return mixed
* @access public
*/
public function getSetting($name, $default = false, $special = null)
{
if ( in_array($name, $this->_specialBased) && isset($special) ) {
$use_special = $this->_guessSpecial($name, $special);
return isset($this->_data[$name][$use_special]) ? $this->_data[$name][$use_special] : $default;
}
return isset($this->_data[$name]) ? $this->_data[$name] : $default;
}
/**
* Changes value of a setting
*
* @param string $name
* @param mixed $value
* @param string|kBase $special
* @return kUnitConfig
* @access public
*/
public function setSetting($name, $value, $special = null)
{
if ( in_array($name, $this->_specialBased) && isset($special) ) {
if ( !isset($this->_data[$name]) ) {
$this->_data[$name] = Array ();
}
$use_special = $this->_guessSpecial($name, $special);
if ( !isset($value) ) {
unset($this->_data[$name][$use_special]);
}
else {
$this->_data[$name][$use_special] = $value;
}
}
else {
if ( !isset($value) ) {
unset($this->_data[$name]);
}
else {
$this->_data[$name] = $value;
}
}
return $this;
}
/**
* Detects settings, that accept arrays
*
* @param string $name
* @return bool
* @access protected
*/
protected function _isPluralSetting($name)
{
if ( preg_match(self::$_alwaysSingularRegExp, $name) ) {
// simplified exceptions
return false;
}
return preg_match('/(es|s)$/', $name);
}
/**
* Detects anonymous settings
*
* @param string $name
* @return bool
* @access protected
*/
protected function _isWithoutKeySetting($name)
{
return in_array($this->_getPlural($name), $this->_withoutKeys);
}
/**
* Returns plural form given word
*
* @param string $name
* @return string
* @access protected
*/
protected function _getPlural($name)
{
return preg_match(self::$_singularEndingsRegExp, $name) ? $name . 'es' : $name . 's';
}
/**
* Returns singular form of given word
*
* @param $name
* @return mixed
* @throws InvalidArgumentException
*/
protected function _getSingular($name)
{
if ( !$this->_isPluralSetting($name) ) {
throw new InvalidArgumentException('Setting "' . $name . '" isn\'t plural');
}
return preg_replace('/(es|s)$/', '', $name);
}
/**
* Guesses special to be used by event
*
* @param string $setting_name
* @param string|kBase $special
* @return string
* @access protected
*/
protected function _guessSpecial($setting_name, $special)
{
$use_special = $special instanceof kBase ? $special->Special : $special;
$value = $this->getSetting($setting_name, Array ());
return isset($value[$use_special]) ? $use_special : '';
}
/**
* Adds new tab to existing edit tab preset
*
* @param string $preset_name
* @param Array $value
* @param string $name
* @return kUnitConfig
* @throws InvalidArgumentException
* @access public
*/
public function addEditTabPresetTabs($preset_name, $value, $name = null)
{
$preset = $this->getEditTabPresetByName($preset_name, false);
if ( $preset === false ) {
throw new InvalidArgumentException('Edit tab preset "' . $preset_name . '" not defined in "' . $this->_prefix . '" unit config');
}
if ( isset($name) ) {
$preset[$name] = $value;
}
else {
$preset = array_merge($preset, $value);
}
$this->addEditTabPresets($preset, $preset_name);
return $this;
}
public function addGridFields($grid_name, $value, $name = null)
{
$grid = $this->getGridByName($grid_name, false);
if ( $grid === false ) {
throw new InvalidArgumentException('Grid "' . $grid_name . '" not defined in "' . $this->_prefix . '" unit config');
}
if ( isset($name) ) {
$grid['Fields'][$name] = $value;
}
else {
$grid['Fields'] = array_merge($grid['Fields'], $value);
}
$this->addGrids($grid, $grid_name);
return $this;
}
/**
* Returns individual permission section
*
* @param string $name
* @param Array $default
* @return Array
* @access public
* @todo Rename setting to plural form and them remove this method
*/
public function getPermSectionByName($name, $default = Array ())
{
$perm_sections = $this->getPermSection(Array ());
return isset($perm_sections[$name]) ? $perm_sections[$name] : $default;
}
/**
* Returns foreign key by given prefix
*
* @param string $parent_prefix
* @param mixed $default
* @return string|bool
* @access public
*/
public function getForeignKey($parent_prefix = null, $default = false)
{
return $this->_getSettingByPrefix('ForeignKey', $parent_prefix, $default);
}
/**
* Returns parent table key by given prefix
*
* @param string $parent_prefix
* @param mixed $default
* @return string|bool
* @access public
*/
public function getParentTableKey($parent_prefix = null, $default = false)
{
return $this->_getSettingByPrefix('ParentTableKey', $parent_prefix, $default);
}
/**
* Returns value of a setting by prefix (special workaround for non-special settings)
*
* @param string $name
* @param string $prefix
* @param mixed $default
* @return mixed
* @access protected
*/
protected function _getSettingByPrefix($name, $prefix = null, $default = false)
{
$value = $this->getSetting($name, $default);
if ( !is_array($value) || !isset($prefix) ) {
return $value;
}
return isset($value[$prefix]) ? $value[$prefix] : $default;
}
/**
* Returns status field with option to get first field only
*
* @param bool $first_only
* @param mixed $default
* @return mixed
* @access public
*/
public function getStatusField($first_only = false, $default = false)
{
$value = $this->getSetting('StatusField', $default);
return $first_only ? $value[0] : $value;
}
/**
* Register necessary classes
* This method should only process the data which is cached!
*
* @return void
* @access public
*/
public function parse()
{
$this->_parseClasses();
$this->_parseScheduledTasks();
$this->_parseHooks();
$this->_parseAggregatedTags();
}
protected function _parseClasses()
{
$register_classes = $this->_getClasses();
foreach ($register_classes as $class_info) {
- $this->Application->registerClass(
- $class_info['class'],
- $this->getBasePath() . DIRECTORY_SEPARATOR . $class_info['file'],
- $class_info['pseudo']
- );
+ // Thanks to static class map there is no need to specify file during class registration.
+ $this->Application->registerClass($class_info['class'], '', $class_info['pseudo']);
if ( isset($class_info['build_event']) && $class_info['build_event'] && $class_info['build_event'] != 'OnBuild' ) {
$this->Application->delayUnitProcessing('registerBuildEvent', Array ($class_info['pseudo'], $class_info['build_event']));
}
}
}
protected function _getClasses()
{
$register_classes = $this->getRegisterClasses();
$class_params = Array ('ItemClass', 'ListClass', 'EventHandlerClass', 'TagProcessorClass');
foreach ($class_params as $param_name) {
$value = $this->getSetting($param_name);
if ( !$value ) {
continue;
}
$value['pseudo'] = $this->_getPseudoByOptionName($param_name);
$this->setSetting($param_name, $value);
$register_classes[] = $value;
}
return $register_classes;
}
protected function _getPseudoByOptionName($option_name)
{
$pseudo_class_map = Array (
'ItemClass' => '%s',
'ListClass' => '%s_List',
'EventHandlerClass' => '%s_EventHandler',
'TagProcessorClass' => '%s_TagProcessor'
);
return sprintf($pseudo_class_map[$option_name], $this->_prefix);
}
protected function _parseScheduledTasks()
{
if ( !$this->getScheduledTasks() ) {
return ;
}
$scheduled_tasks = $this->getScheduledTasks();
foreach ($scheduled_tasks as $short_name => $scheduled_task_info) {
$event_status = array_key_exists('Status', $scheduled_task_info) ? $scheduled_task_info['Status'] : STATUS_ACTIVE;
$this->Application->delayUnitProcessing('registerScheduledTask', Array ($short_name, $this->_prefix . ':' . $scheduled_task_info['EventName'], $scheduled_task_info['RunSchedule'], $this->getModule(), $event_status));
}
}
/**
* Detects module by unit location.
*
* @return string
*/
public function getModule()
{
$module_path = $this->getModuleFolder() . '/';
foreach ( $this->Application->ModuleInfo as $module_name => $module_data ) {
if ( $module_name == 'In-Portal' ) {
continue;
}
if ( $module_data['Path'] == $module_path ) {
return $module_name;
}
}
return '';
}
protected function _parseHooks()
{
$hooks = $this->getHooks();
if ( !$hooks ) {
return;
}
foreach ($hooks as $hook) {
if ( $this->getParentPrefix() && ($hook['HookToPrefix'] == $this->getParentPrefix()) ) {
trigger_error('Deprecated Hook Usage [prefix: <strong>' . $this->_prefix . '</strong>; do_prefix: <strong>' . $hook['DoPrefix'] . '</strong>] use <strong>#PARENT#</strong> as <strong>HookToPrefix</strong> value, where HookToPrefix is same as ParentPrefix', defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_NOTICE);
}
if ( $hook['HookToPrefix'] == '' ) {
// new: set hooktoprefix to current prefix if not set
$hook['HookToPrefix'] = $this->_prefix;
}
if ( $this->getParentPrefix() ) {
// new: allow to set hook to parent prefix what ever it is
if ( $hook['HookToPrefix'] == '#PARENT#' ) {
$hook['HookToPrefix'] = $this->getParentPrefix();
}
if ( $hook['DoPrefix'] == '#PARENT#' ) {
$hook['DoPrefix'] = $this->getParentPrefix();
}
}
elseif ( $hook['HookToPrefix'] == '#PARENT#' || $hook['DoPrefix'] == '#PARENT#' ) {
// we need parent prefix but it's not set !
continue;
}
$hook_events = (array)$hook['HookToEvent'];
$do_prefix = $hook['DoPrefix'] == '' ? $this->_prefix : $hook['DoPrefix'];
foreach ($hook_events as $hook_event) {
$hook_event = $hook['HookToPrefix'] . '.' . $hook['HookToSpecial'] . ':' . $hook_event;
$do_event = $do_prefix . '.' . $hook['DoSpecial'] . ':' . $hook['DoEvent'];
$this->Application->delayUnitProcessing('registerHook', Array ($hook_event, $do_event, $hook['Mode'], $hook['Conditional']));
}
}
}
protected function _parseAggregatedTags()
{
$aggregated_tags = $this->getAggregateTags();
if ( !$aggregated_tags ) {
return;
}
foreach ($aggregated_tags as $aggregate_tag) {
if ( $this->getParentPrefix() ) {
if ( $aggregate_tag['AggregateTo'] == $this->getParentPrefix() ) {
trigger_error('Deprecated Aggregate Tag Usage [prefix: <b>' . $this->_prefix . '</b>; AggregateTo: <b>' . $aggregate_tag['AggregateTo'] . '</b>] use <b>#PARENT#</b> as <b>AggregateTo</b> value, where AggregateTo is same as ParentPrefix', defined('E_USER_DEPRECATED') ? E_USER_DEPRECATED : E_USER_NOTICE);
}
if ( $aggregate_tag['AggregateTo'] == '#PARENT#' ) {
$aggregate_tag['AggregateTo'] = $this->getParentPrefix();
}
}
$aggregate_tag['LocalPrefix'] = $this->_prefix;
$this->Application->delayUnitProcessing('registerAggregateTag', Array ($aggregate_tag));
}
}
public function validate()
{
global $debugger;
$table_name = $this->getTableName();
$float_types = Array ('float', 'double', 'numeric');
$table_found = $this->Conn->Query('SHOW TABLES LIKE "' . $table_name . '"');
if ( !$table_found ) {
// config present, but table missing, strange
kUtil::safeDefine('DBG_RAISE_ON_WARNINGS', 1);
$debugger->appendHTML("<b class='debug_error'>Config Warning: </b>Table <strong>$table_name</strong> missing, but prefix <b>" . $this->_prefix . "</b> requires it!");
$debugger->WarningCount++;
return;
}
$res = $this->Conn->Query('DESCRIBE ' . $table_name);
$config_link = $debugger->getFileLink(FULL_PATH . $this->_filename, 1, $this->_prefix);
$error_messages = Array (
'field_not_found' => 'Field <strong>%s</strong> exists in the database, but <strong>is not defined</strong> in config',
'default_missing' => 'Default value for field <strong>%s</strong> not set in config',
'not_null_error1' => 'Field <strong>%s</strong> is NOT NULL in the database, but is not configured as not_null', // or required',
'not_null_error2' => 'Field <strong>%s</strong> is described as NOT NULL in config, but <strong>does not have DEFAULT value</strong>',
'not_null_error3' => 'Field <strong>%s</strong> is described as <strong>NOT NULL in config</strong>, but is <strong>NULL in db</strong>',
'invalid_default' => '<strong>Default value</strong> for field %s<strong>%s</strong> not sync. to db (in config = %s, in db = %s)',
'date_column_not_null_error' => 'Field <strong>%s</strong> must be NULL in config and database, since it contains date',
'user_column_default_error' => 'Field <strong>%s</strong> must be have NULL as default value, since it holds user id',
'type_missing' => '<strong>Type definition</strong> for field <strong>%s</strong> missing in config',
'virtual_type_missing' => '<strong>Type definition</strong> for virtual field <strong>%s</strong> missing in config',
'virtual_default_missing' => 'Default value for virtual field <strong>%s</strong> not set in config',
'virtual_not_null_error' => 'Virtual field <strong>%s</strong> cannot be not null, since it doesn\'t exist in database',
'invalid_calculated_field' => 'Calculated field <strong>%s</strong> is missing corresponding virtual field',
);
$config_errors = Array ();
$fields = $this->getFields();
$table_name = preg_replace('/^' . preg_quote(TABLE_PREFIX, '/') . '(.*)/', '\\1', $table_name); // remove table prefix
if ( $fields ) {
// validate unit config field declaration in relation to database table structure
foreach ($res as $field) {
$f_name = $field['Field'];
if ( preg_match('/l[\d]+_[\w]/', $f_name) ) {
// skip multilingual fields
continue;
}
if ( !array_key_exists($f_name, $fields) ) {
$config_errors[] = sprintf($error_messages['field_not_found'], $f_name);
}
else {
$db_default = $field['Default'];
if ( is_numeric($db_default) ) {
$db_default = preg_match('/[\.,]/', $db_default) ? (float)$db_default : (int)$db_default;
}
$default_missing = false;
$options = $fields[$f_name];
$not_null = isset($options['not_null']) && $options['not_null'];
$formatter = array_key_exists('formatter', $options) ? $options['formatter'] : false;
if ( !array_key_exists('default', $options) ) {
$config_errors[] = sprintf($error_messages['default_missing'], $f_name);
$default_missing = true;
}
if ( $field['Null'] != 'YES' ) {
// field is NOT NULL in database (MySQL5 for null returns "NO", but MySQL4 returns "")
if ( $f_name != $this->getIDField() && !isset($options['not_null']) /*&& !isset($options['required'])*/ ) {
$config_errors[] = sprintf($error_messages['not_null_error1'], $f_name);
}
if ( $not_null && !isset($options['default']) ) {
$config_errors[] = sprintf($error_messages['not_null_error2'], $f_name);
}
}
elseif ( $not_null ) {
$config_errors[] = sprintf($error_messages['not_null_error3'], $f_name);
}
if ( ($formatter == 'kDateFormatter') && $not_null ) {
$config_errors[] = sprintf($error_messages['date_column_not_null_error'], $f_name);
}
// columns, holding userid should have NULL as default value
if ( array_key_exists('type', $options) && !$default_missing ) {
// both type and default value set
if ( preg_match('/ById$/', $f_name) && $options['default'] !== null ) {
$config_errors[] = sprintf($error_messages['user_column_default_error'], $f_name);
}
}
if ( !array_key_exists('type', $options) ) {
$config_errors[] = sprintf($error_messages['type_missing'], $f_name);
}
if ( !$default_missing && ($field['Type'] != 'text') ) {
if ( is_null($db_default) && $not_null ) {
$db_default = $options['type'] == 'string' ? '' : 0;
}
if ( $f_name == $this->getIDField() && $options['type'] != 'string' && $options['default'] !== 0 ) {
$config_errors[] = sprintf($error_messages['invalid_default'], '<span class="debug_error">IDField</span> ', $f_name, $this->_varDump($options['default']), $this->_varDump($field['Default']));
}
else if ( ((string)$options['default'] != '#NOW#') && ($db_default !== $options['default']) && !in_array($options['type'], $float_types) ) {
$config_errors[] = sprintf($error_messages['invalid_default'], '', $f_name, $this->_varDump($options['default']), $this->_varDump($db_default));
}
}
}
}
}
// validate virtual fields
$virtual_fields = $this->getVirtualFields();
if ( $virtual_fields ) {
foreach ($virtual_fields as $f_name => $options) {
if ( !array_key_exists('type', $options) ) {
$config_errors[] = sprintf($error_messages['virtual_type_missing'], $f_name);
}
if ( array_key_exists('not_null', $options) ) {
$config_errors[] = sprintf($error_messages['virtual_not_null_error'], $f_name);
}
if ( !array_key_exists('default', $options) ) {
$config_errors[] = sprintf($error_messages['virtual_default_missing'], $f_name);
}
}
}
// validate calculated fields
if ( $this->getCalculatedFieldSpecials() ) {
foreach ($this->getCalculatedFieldSpecials() as $special) {
foreach ($this->getCalculatedFieldsBySpecial($special) as $calculated_field => $calculated_field_expr) {
if ( !isset($virtual_fields[$calculated_field]) ) {
$config_errors[] = sprintf($error_messages['invalid_calculated_field'], $calculated_field);
}
}
}
$config_errors = array_unique($config_errors);
}
if ( $config_errors ) {
$error_prefix = '<strong class="debug_error">Config Error' . (count($config_errors) > 1 ? 's' : '') . ': </strong> for prefix <strong>' . $config_link . '</strong> (' . $table_name . ') in unit config:<br />';
$config_errors = $error_prefix . '&nbsp;&nbsp;&nbsp;' . implode('<br />&nbsp;&nbsp;&nbsp;', $config_errors);
kUtil::safeDefine('DBG_RAISE_ON_WARNINGS', 1);
$debugger->appendHTML($config_errors);
$debugger->WarningCount++;
}
}
protected function _varDump($value)
{
return '<strong>' . var_export($value, true) . '</strong> of ' . gettype($value);
}
}
Index: branches/5.3.x/core/kernel/utility/ClassDiscovery/ClassMapBuilder.php
===================================================================
--- branches/5.3.x/core/kernel/utility/ClassDiscovery/ClassMapBuilder.php (nonexistent)
+++ branches/5.3.x/core/kernel/utility/ClassDiscovery/ClassMapBuilder.php (revision 16156)
@@ -0,0 +1,435 @@
+<?php
+/**
+* @version $Id$
+* @package In-Portal
+* @copyright Copyright (C) 1997 - 2015 Intechnic. All rights reserved.
+* @license GNU/GPL
+* In-Portal is Open Source software.
+* This means that this software may have been modified pursuant
+* the GNU General Public License, and as distributed it includes
+* or is derivative of works licensed under the GNU General Public License
+* or other free or open source software licenses.
+* See http://www.in-portal.org/license for copyright notices and details.
+*/
+
+namespace Intechnic\InPortal\Core\kernel\utility\ClassDiscovery;
+
+
+use PhpParser\Lexer;
+use PhpParser\NodeTraverser;
+use PhpParser\NodeVisitor\NameResolver;
+use PhpParser\Parser;
+
+defined('FULL_PATH') or die('restricted access!');
+
+class ClassMapBuilder
+{
+
+ const CACHE_FORMAT = 1;
+
+ const CACHE_FILE_STRUCTURE = 'class_structure.php';
+
+ const CACHE_FILE_HASHES = 'file_hashes.php';
+
+ /**
+ * Path to scan.
+ *
+ * @var string
+ */
+ protected $scanPath;
+
+ /**
+ * Path to store cache into.
+ *
+ * @var string
+ */
+ protected $cachePath;
+
+ /**
+ * List of classes (key - class, value - file).
+ *
+ * @var array
+ */
+ protected $classToFileMap = array();
+
+ /**
+ * List of classes, declared in each file (key - file, value - class list).
+ *
+ * @var array
+ */
+ protected $fileToClassMap = array();
+
+ /**
+ * Stores hash of each file on given path.
+ *
+ * @var array
+ */
+ protected $fileHashes = array();
+
+ /**
+ * Parser.
+ *
+ * @var Parser
+ */
+ protected $parser;
+
+ /**
+ * Node traverser.
+ *
+ * @var NodeTraverser
+ */
+ protected $traverser;
+
+ /**
+ * Name of file, that is currently processed.
+ *
+ * @var string
+ */
+ protected $currentFile;
+
+ /**
+ * Returns builder array for all eligible folders.
+ *
+ * @param array $module_info Module info.
+ *
+ * @return static[]
+ */
+ public static function createBuilders(array $module_info = null)
+ {
+ $ret = array();
+
+ if ( !isset($module_info) ) {
+ // No module information given > scan everything.
+ $ret[] = new static(FULL_PATH . DIRECTORY_SEPARATOR . 'core');
+
+ foreach ( glob(MODULES_PATH . '/*', GLOB_ONLYDIR) as $module_folder ) {
+ if ( \kModulesHelper::isInPortalModule($module_folder) ) {
+ $ret[] = new static($module_folder);
+ }
+ }
+ }
+ else {
+ // Module information given > scan only these modules.
+ foreach ( $module_info as $module_name => $module_data ) {
+ if ( $module_name == 'In-Portal' ) {
+ continue;
+ }
+
+ $ret[] = new static(FULL_PATH . DIRECTORY_SEPARATOR . rtrim($module_data['Path'], '/'));
+ }
+ }
+
+ return $ret;
+ }
+
+ /**
+ * Creates ClassMapBuilder instance.
+ *
+ * @param string $scan_path Path to scan.
+ */
+ public function __construct($scan_path)
+ {
+ $this->scanPath = $scan_path;
+ $this->assertPath($this->scanPath);
+
+ $this->cachePath = $this->scanPath . '/install/cache';
+ $this->assertPath($this->cachePath);
+ }
+
+ /**
+ * Validates that path exists and is directory.
+ *
+ * @param string $path Path.
+ *
+ * @return void
+ * @throws \InvalidArgumentException When invalid path is given.
+ */
+ protected function assertPath($path)
+ {
+ if ( !file_exists($path) || !is_dir($path) ) {
+ throw new \InvalidArgumentException('Path "' . $path . '" is not a folder or doesn\'t exist');
+ }
+ }
+
+ /**
+ * Returns class map, that was build previously.
+ *
+ * @return array
+ */
+ public function get()
+ {
+ $this->load(self::CACHE_FILE_STRUCTURE, false);
+
+ return $this->classToFileMap;
+ }
+
+ /**
+ * Builds class map.
+ *
+ * @return void
+ * @throws \RuntimeException When PHP parser not found.
+ */
+ public function build()
+ {
+ if ( !class_exists('PhpParser\Parser') ) {
+ $error_msg = 'PHP Parser not found. ';
+ $error_msg .= 'Make sure, that Composer dependencies were installed using "php composer.phar install --dev" command.';
+ throw new \RuntimeException($error_msg);
+ }
+
+ $scan_path = preg_replace('/^' . preg_quote(FULL_PATH, '/') . '/', '...', $this->scanPath, 1);
+
+ echo $this->strPad('path "' . $scan_path . '"', 40);
+
+ $this->load(self::CACHE_FILE_STRUCTURE, true);
+ $this->load(self::CACHE_FILE_HASHES, true);
+
+ $start = microtime(true);
+ $files = $this->scan();
+ echo $this->strPad('scanned in ' . sprintf('%.4f', microtime(true) - $start) . 's', 25);
+
+ $start = microtime(true);
+
+ $this->createParser();
+
+ foreach ( $files as $file ) {
+ $this->parseFile($file);
+ }
+
+ echo $this->strPad('parsed in ' . sprintf('%.4f', microtime(true) - $start) . 's', 25);
+ echo PHP_EOL;
+
+ ksort($this->classToFileMap);
+ ksort($this->fileHashes);
+
+ $this->store(self::CACHE_FILE_STRUCTURE);
+ $this->store(self::CACHE_FILE_HASHES);
+ }
+
+ /**
+ * Pads text with spaces from right side.
+ *
+ * @param string $text Text.
+ * @param integer $length Pad length.
+ *
+ * @return string
+ */
+ protected function strPad($text, $length)
+ {
+ return str_pad($text, $length, ' ', STR_PAD_RIGHT);
+ }
+
+ /**
+ * Loads cache from disk.
+ *
+ * @param string $filename Filename.
+ * @param boolean $for_writing Load cache for writing or reading.
+ *
+ * @return void
+ */
+ protected function load($filename, $for_writing)
+ {
+ $file_path = $this->getCacheFilename($filename);
+
+ if ( !file_exists($file_path) ) {
+ return;
+ }
+
+ $cache = include $file_path;
+
+ if ( $cache['cache_format'] < self::CACHE_FORMAT ) {
+ return;
+ }
+
+ if ( $filename === self::CACHE_FILE_STRUCTURE ) {
+ if ( $for_writing ) {
+ foreach ( $cache['classes'] as $class => $file ) {
+ if ( !isset($this->fileToClassMap[$file]) ) {
+ $this->fileToClassMap[$file] = array();
+ }
+
+ $this->fileToClassMap[$file][] = $class;
+ }
+ }
+ else {
+ $this->classToFileMap = $cache['classes'];
+ }
+ }
+ elseif ( $filename === self::CACHE_FILE_HASHES ) {
+ $this->fileHashes = $cache['file_hashes'];
+ }
+ }
+
+ /**
+ * Scans path for files.
+ *
+ * @return array
+ */
+ protected function scan()
+ {
+ $files = array();
+ $directory_iterator = new \RecursiveDirectoryIterator($this->scanPath);
+ $filter_iterator = new CodeFolderFilterIterator($directory_iterator);
+
+ foreach ( new \RecursiveIteratorIterator($filter_iterator, \RecursiveIteratorIterator::SELF_FIRST) as $file ) {
+ /* @var \SplFileInfo $file */
+ if ( $file->isFile() && $file->getExtension() === 'php' ) {
+ $relative_path = preg_replace('/^' . preg_quote(FULL_PATH, '/') . '/', '', $file->getPathname(), 1);
+ $files[$relative_path] = true;
+ }
+ }
+
+ // Don't include cache file itself in cache.
+ $exclude_file = preg_replace(
+ '/^' . preg_quote(FULL_PATH, '/') . '/',
+ '',
+ $this->getCacheFilename(self::CACHE_FILE_STRUCTURE),
+ 1
+ );
+ unset($files[$exclude_file]);
+
+ $exclude_file = preg_replace(
+ '/^' . preg_quote(FULL_PATH, '/') . '/',
+ '',
+ $this->getCacheFilename(self::CACHE_FILE_HASHES),
+ 1
+ );
+ unset($files[$exclude_file]);
+
+ return array_keys($files);
+ }
+
+ /**
+ * Create parser.
+ *
+ * @return void
+ */
+ protected function createParser()
+ {
+ ini_set('xdebug.max_nesting_level', 3000);
+ $this->parser = new Parser(new Lexer());
+ $this->traverser = new NodeTraverser();
+ $this->traverser->addVisitor(new NameResolver());
+ $this->traverser->addVisitor(new ClassDetector($this));
+ }
+
+ /**
+ * Parses a file.
+ *
+ * @param string $file Path to file.
+ *
+ * @return void
+ */
+ protected function parseFile($file)
+ {
+ $this->currentFile = $file;
+
+ $code = file_get_contents(FULL_PATH . $file);
+ $current_hash = filesize(FULL_PATH . $file);
+ $previous_hash = isset($this->fileHashes[$file]) ? $this->fileHashes[$file] : 0;
+
+ if ( $current_hash === $previous_hash ) {
+ // File wasn't change since time, when cache was built.
+ if ( isset($this->fileToClassMap[$file]) ) {
+ foreach ( $this->fileToClassMap[$file] as $class ) {
+ $this->addClass($class);
+ }
+ }
+ }
+ else {
+ // Parse file, because it's content doesn't match the cache.
+ $this->fileToClassMap[$file] = array();
+ $this->fileHashes[$file] = $current_hash;
+
+ $statements = $this->parser->parse($code);
+ $this->traverser->traverse($statements);
+ }
+ }
+
+ /**
+ * Stores cache to disk.
+ *
+ * @param string $filename Cache filename.
+ *
+ * @return void
+ * @throws \RuntimeException When cache could not be written.
+ */
+ protected function store($filename)
+ {
+ $cache = array('cache_format' => self::CACHE_FORMAT);
+
+ if ( $filename === self::CACHE_FILE_STRUCTURE ) {
+ $cache['classes'] = $this->classToFileMap;
+ }
+ elseif ( $filename === self::CACHE_FILE_HASHES ) {
+ $cache['file_hashes'] = $this->fileHashes;
+ }
+
+ $cache = $this->prettyVarExport($cache);
+
+ $at = '@';
+ $file_content = <<<EOPHP
+<?php
+// {$at}codingStandardsIgnoreFile
+
+/**
+ * This file is automatically {$at}generated. Use 'php tools/build_class_map.php' to rebuild it.
+ */
+return {$cache};
+
+EOPHP;
+
+ $file_path = $this->getCacheFilename($filename);
+
+ // Don't bother saving, because file wasn't even changed.
+ if ( file_exists($file_path) && file_get_contents($file_path) === $file_content ) {
+ return;
+ }
+
+ if ( file_put_contents($file_path, $file_content) === false ) {
+ throw new \RuntimeException('Unable to save cache to "' . $file_path . '" file');
+ }
+ }
+
+ /**
+ * Prettified var_export.
+ *
+ * @param mixed $data Data.
+ *
+ * @return string
+ */
+ protected function prettyVarExport($data)
+ {
+ $result = var_export($data, true);
+ $result = preg_replace("/=> \n[ ]+array \\(/s", '=> array (', $result);
+ $result = str_replace(array('array (', ' '), array('array(', "\t"), $result);
+
+ return $result;
+ }
+
+ /**
+ * Returns cache filename.
+ *
+ * @param string $filename Filename.
+ *
+ * @return string
+ */
+ protected function getCacheFilename($filename)
+ {
+ return $this->cachePath . '/' . $filename;
+ }
+
+ /**
+ * Adds class to the map.
+ *
+ * @param string $class Class.
+ *
+ * @return void
+ */
+ public function addClass($class)
+ {
+ $this->classToFileMap[$class] = $this->currentFile;
+ $this->fileToClassMap[$this->currentFile][] = $class;
+ }
+
+}
Property changes on: branches/5.3.x/core/kernel/utility/ClassDiscovery/ClassMapBuilder.php
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Index: branches/5.3.x/core/kernel/utility/ClassDiscovery/ClassDetector.php
===================================================================
--- branches/5.3.x/core/kernel/utility/ClassDiscovery/ClassDetector.php (nonexistent)
+++ branches/5.3.x/core/kernel/utility/ClassDiscovery/ClassDetector.php (revision 16156)
@@ -0,0 +1,65 @@
+<?php
+/**
+* @version $Id$
+* @package In-Portal
+* @copyright Copyright (C) 1997 - 2015 Intechnic. All rights reserved.
+* @license GNU/GPL
+* In-Portal is Open Source software.
+* This means that this software may have been modified pursuant
+* the GNU General Public License, and as distributed it includes
+* or is derivative of works licensed under the GNU General Public License
+* or other free or open source software licenses.
+* See http://www.in-portal.org/license for copyright notices and details.
+*/
+
+namespace Intechnic\InPortal\Core\kernel\utility\ClassDiscovery;
+
+
+use PhpParser\Node;
+use PhpParser\Node\Stmt\ClassLike;
+use PhpParser\NodeVisitorAbstract;
+
+defined('FULL_PATH') or die('restricted access!');
+
+class ClassDetector extends NodeVisitorAbstract
+{
+
+ /**
+ * List of classes, discovered by the visitor.
+ *
+ * @var array
+ */
+ protected $classes = array();
+
+ /**
+ * Class map builder.
+ *
+ * @var ClassMapBuilder
+ */
+ protected $classMapBuilder;
+
+ /**
+ * Creates class detector instance.
+ *
+ * @param ClassMapBuilder $class_map_builder Class map builder.
+ */
+ public function __construct(ClassMapBuilder $class_map_builder)
+ {
+ $this->classMapBuilder = $class_map_builder;
+ }
+
+ /**
+ * Remembers all encountered classes.
+ *
+ * @param Node $node Node.
+ *
+ * @return void
+ */
+ public function enterNode(Node $node)
+ {
+ if ( $node instanceof ClassLike ) {
+ $this->classMapBuilder->addClass((string)$node->namespacedName);
+ }
+ }
+
+}
Property changes on: branches/5.3.x/core/kernel/utility/ClassDiscovery/ClassDetector.php
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Index: branches/5.3.x/core/kernel/utility/ClassDiscovery/CodeFolderFilterIterator.php
===================================================================
--- branches/5.3.x/core/kernel/utility/ClassDiscovery/CodeFolderFilterIterator.php (nonexistent)
+++ branches/5.3.x/core/kernel/utility/ClassDiscovery/CodeFolderFilterIterator.php (revision 16156)
@@ -0,0 +1,58 @@
+<?php
+/**
+* @version $Id$
+* @package In-Portal
+* @copyright Copyright (C) 1997 - 2015 Intechnic. All rights reserved.
+* @license GNU/GPL
+* In-Portal is Open Source software.
+* This means that this software may have been modified pursuant
+* the GNU General Public License, and as distributed it includes
+* or is derivative of works licensed under the GNU General Public License
+* or other free or open source software licenses.
+* See http://www.in-portal.org/license for copyright notices and details.
+*/
+
+namespace Intechnic\InPortal\Core\kernel\utility\ClassDiscovery;
+
+
+defined('FULL_PATH') or die('restricted access!');
+
+class CodeFolderFilterIterator extends \RecursiveFilterIterator
+{
+
+ /**
+ * Folders, that should be ignored.
+ *
+ * @var array
+ */
+ public $skipFolders = array(
+ 'CVS', '.svn', '.git', 'admin_templates', 'libchart',
+ );
+
+ /**
+ * Creates iterator instance.
+ *
+ * @param \RecursiveDirectoryIterator $directory_iterator Directory iterator.
+ */
+ public function __construct(\RecursiveDirectoryIterator $directory_iterator)
+ {
+ parent::__construct($directory_iterator);
+
+ $this->skipFolders[] = basename(EDITOR_PATH);
+ }
+
+ /**
+ * Accepts only folders with valid names.
+ *
+ * @return boolean
+ */
+ public function accept()
+ {
+ return !in_array(
+ $this->current()->getFilename(),
+ $this->skipFolders,
+ true
+ );
+ }
+
+}
Property changes on: branches/5.3.x/core/kernel/utility/ClassDiscovery/CodeFolderFilterIterator.php
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Index: branches/5.3.x/core/kernel/utility/factory.php
===================================================================
--- branches/5.3.x/core/kernel/utility/factory.php (revision 16155)
+++ branches/5.3.x/core/kernel/utility/factory.php (revision 16156)
@@ -1,347 +1,393 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
+use Intechnic\InPortal\Core\kernel\utility\ClassDiscovery\ClassMapBuilder;
+
defined('FULL_PATH') or die('restricted access!');
class kFactory extends kBase implements kiCacheable {
/**
* Mapping between class name and file name
* where class definition can be found.
* File path absolute!
*
* @var Array
* @access protected
*/
protected $classMap = Array ();
/**
+ * The PSR-4 compliant namespace-to-folder mapping.
+ *
+ * @var array
+ */
+ protected $namespaceMap = array();
+
+ /**
* Map class names to their pseudo
* class names, e.g. key=pseudo_class,
* value=real_class_name
*
* @var Array
* @access protected
*/
protected $realClasses = Array ();
/**
* Where all created objects are stored
*
* @var Array|kBase[]
* @access protected
*/
protected $Storage = Array ();
public function __construct()
{
parent::__construct();
spl_autoload_register(Array (&$this, 'autoload'));
}
/**
+ * Configures module-based autoloader.
+ *
+ * @return void
+ */
+ public function configureAutoloader()
+ {
+ if ( !$this->Application->ModuleInfo ) {
+ $error_msg = 'Autoloader configuration can be only performed after module information is available';
+ throw new LogicException($error_msg);
+ }
+
+ $this->namespaceMap = array();
+
+ foreach ( $this->Application->ModuleInfo as $module_name => $module_info ) {
+ if ( $module_name == 'In-Portal' ) {
+ continue;
+ }
+
+ $this->namespaceMap[$module_info['ClassNamespace']] = rtrim($module_info['Path'], '/');
+ }
+
+ if ( defined('IS_INSTALL') && IS_INSTALL ) {
+ // During installation process all modules, because unit configs from all modules are scanned too.
+ $class_map_builders = ClassMapBuilder::createBuilders();
+ }
+ else {
+ $class_map_builders = ClassMapBuilder::createBuilders($this->Application->ModuleInfo);
+ }
+
+ foreach ( $class_map_builders as $class_map_builder ) {
+ $class_map = $class_map_builder->get();
+ $class_names = array_keys($class_map);
+
+ $this->classMap = array_merge($this->classMap, $class_map);
+ $this->realClasses = array_merge($this->realClasses, array_combine($class_names, $class_names));
+ }
+ }
+
+ /**
* Sets data from cache to object
*
* @param Array $data
* @return void
* @access public
*/
public function setFromCache(&$data)
{
$this->classMap = $data['Factory.Files'];
+ $this->namespaceMap = $data['Factory.Namespaces'];
$this->realClasses = $data['Factory.realClasses'];
}
/**
* Performs automatic loading of classes registered with the factory
*
* @param string $class
* @return bool|null
* @access public
*/
public function autoload($class)
{
$file = $this->findFile($class);
if ( $file ) {
kUtil::includeOnce(FULL_PATH . $file);
return true;
}
return null;
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
* @return string|bool The path if found, false otherwise
* @access protected
*/
protected function findFile($class)
{
if ( $class[0] == '\\' ) {
$class = substr($class, 1);
}
if ( isset($this->classMap[$class]) ) {
return $this->classMap[$class];
}
$pos = strrpos($class, '\\');
if ( $pos !== false ) {
// namespaced class name
$class_path = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
$class_name = substr($class, $pos + 1);
}
else {
// PEAR-like class name
$class_path = null;
$class_name = $class;
}
$class_path .= str_replace('_', DIRECTORY_SEPARATOR, $class_name) . '.php';
- foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
- if ( $module_name == 'In-Portal' ) {
- continue;
- }
-
- if ( strpos($class, $module_info['ClassNamespace']) === 0 ) {
+ foreach ( $this->namespaceMap as $namespace_prefix => $namespace_path ) {
+ if ( strpos($class, $namespace_prefix) === 0 ) {
$test_class_path = str_replace(
- str_replace('\\', DIRECTORY_SEPARATOR, $module_info['ClassNamespace']),
- rtrim($module_info['Path'], '/'),
+ str_replace('\\', DIRECTORY_SEPARATOR, $namespace_prefix),
+ $namespace_path,
$class_path
);
if ( file_exists(FULL_PATH . DIRECTORY_SEPARATOR . $test_class_path) ) {
return DIRECTORY_SEPARATOR . $test_class_path;
}
}
}
return $this->classMap[$class] = false;
}
/**
* Gets object data for caching
*
* @return Array
* @access public
*/
public function getToCache()
{
+ ksort($this->classMap);
+ ksort($this->namespaceMap);
+ ksort($this->realClasses);
+
return Array (
'Factory.Files' => $this->classMap,
+ 'Factory.Namespaces' => $this->namespaceMap,
'Factory.realClasses' => $this->realClasses,
);
}
/**
* Splits any mixing of prefix and
* special into correct ones
*
* @param string $prefix_special
* @return Array
* @access public
*/
public function processPrefix($prefix_special)
{
// l.pick, l, m.test_TagProcessor
//preg_match("/(.*)\.*(.*)(_*)(.*)/", $prefix_special, $regs);
//return Array('prefix'=>$regs[1].$regs[3].$regs[4], 'special'=>$regs[2]);
$tmp = explode('_', $prefix_special, 2);
$tmp[0] = explode('.', $tmp[0]);
$prefix = $tmp[0][0];
$prefix_special = $prefix; // new1
if ( isset($tmp[1]) ) {
$prefix .= '_' . $tmp[1];
}
$special = isset($tmp[0][1]) ? $tmp[0][1] : '';
$prefix_special .= '.' . $special; // new2
return Array ('prefix' => $prefix, 'special' => $special, 'prefix_special' => $prefix_special);
}
-
/**
- * Returns object using params specified, creates it if is required
+ * Returns object using params specified, creates it if is required.
+ *
+ * @param string $name Object name in factory.
+ * @param string $pseudo_class Pseudo class.
+ * @param Array $event_params Event params.
+ * @param Array $arguments Constructor arguments.
*
- * @param string $name
- * @param string $pseudo_class
- * @param Array $event_params
- * @param Array $arguments
* @return kBase
- * @access public
- * @throws kFactoryException
*/
public function getObject($name, $pseudo_class = '', $event_params = Array (), $arguments = Array ())
{
$name = rtrim($name, '.');
if ( isset($this->Storage[$name]) ) {
return $this->Storage[$name];
}
$ret = $this->processPrefix($name);
if ( !$pseudo_class ) {
$pseudo_class = $ret['prefix'];
}
- if ( !isset($this->realClasses[$pseudo_class]) ) {
- $error_msg = 'RealClass not defined for pseudo_class <strong>' . $pseudo_class . '</strong>';
-
- if ( $this->Application->isInstalled() ) {
- throw new kFactoryException($error_msg);
- }
- else {
- if ( $this->Application->isDebugMode() ) {
- $this->Application->Debugger->appendTrace();
- }
-
- trigger_error($error_msg, E_USER_WARNING);
- }
-
- return false;
- }
-
if ( defined('DEBUG_MODE') && defined('DBG_FACTORY') && DBG_FACTORY && $this->Application->isDebugMode() ) {
$this->Application->Debugger->appendHTML('<b>Creating object:</b> Pseudo class: ' . $pseudo_class . ' Prefix: ' . $name);
$this->Application->Debugger->appendTrace();
}
$this->Storage[$name] = $this->makeClass($pseudo_class, $arguments);
$this->Storage[$name]->Init($ret['prefix'], $ret['special']);
$this->Application->EventManager->runBuildEvent($ret['prefix_special'], $pseudo_class, $event_params);
return $this->Storage[$name];
}
/**
* Removes object from storage, so next time it could be created from scratch
*
* @param string $name Object's name in the Storage
* @return void
* @access public
*/
public function DestroyObject($name)
{
unset($this->Storage[$name]);
}
/**
* Checks if object with prefix passes was already created in factory
*
* @param string $name object pseudo_class, prefix
* @return bool
* @access public
*/
public function hasObject($name)
{
return isset($this->Storage[$name]);
}
/**
- * Get's real class name for pseudo class,
- * includes class file and creates class
- * instance.
- * All parameters except first one are passed to object constuctor
- * through mediator method makeClass that creates instance of class
+ * Get's real class name for pseudo class, includes class file and creates class instance.
*
* Pattern: Factory Method
*
- * @param string $pseudo_class
- * @param Array $arguments
+ * @param string $pseudo_class Pseudo class.
+ * @param array $arguments Constructor arguments.
+ *
* @return kBase
- * @access public
+ * @throws kFactoryException When class not found.
*/
public function makeClass($pseudo_class, $arguments = Array ())
{
+ if ( !isset($this->realClasses[$pseudo_class]) ) {
+ $error_msg = 'RealClass not defined for "<strong>' . $pseudo_class . '</strong>" pseudo_class.';
+ $error_msg .= ' Please use "<strong>php tools/build_class_map.php</strong>" to discover new classes.';
+
+ if ( $this->Application->isInstalled() ) {
+ throw new kFactoryException($error_msg);
+ }
+ else {
+ if ( $this->Application->isDebugMode() ) {
+ $this->Application->Debugger->appendTrace();
+ }
+
+ trigger_error($error_msg, E_USER_WARNING);
+ }
+
+ return false;
+ }
+
$real_class = $this->realClasses[$pseudo_class];
$mem_before = memory_get_usage();
$time_before = microtime(true);
$arguments = (array)$arguments;
if ( !$arguments ) {
$class = new $real_class();
}
else {
$reflection = new ReflectionClass($real_class);
$class = $reflection->newInstanceArgs($arguments);
}
if ( defined('DEBUG_MODE') && DEBUG_MODE && defined('DBG_PROFILE_MEMORY') && DBG_PROFILE_MEMORY && $this->Application->isDebugMode() ) {
$mem_after = memory_get_usage();
$time_after = microtime(true);
$mem_used = $mem_after - $mem_before;
$time_used = $time_after - $time_before;
$this->Application->Debugger->appendHTML('Factroy created <b>' . $real_class . '</b> - used ' . round($mem_used / 1024, 3) . 'Kb time: ' . round($time_used, 5));
$this->Application->Debugger->profilerAddTotal('objects', null, $mem_used);
}
return $class;
}
/**
* Registers new class in the factory
*
* @param string $real_class Real name of class as in class declaration
* @param string $file Filename in what $real_class is declared
* @param string $pseudo_class Name under this class object will be accessed using getObject method
* @return void
* @access public
*/
public function registerClass($real_class, $file, $pseudo_class = null)
{
if ( !isset($pseudo_class) ) {
$pseudo_class = $real_class;
}
if ( !isset($this->classMap[$real_class]) ) {
$this->classMap[$real_class] = preg_replace('/^' . preg_quote(FULL_PATH, '/') . '/', '', $file, 1);
}
$this->realClasses[$pseudo_class] = $real_class;
}
/**
* Unregisters existing class from factory
*
* @param string $real_class Real name of class as in class declaration
* @param string $pseudo_class Name under this class object is accessed using getObject method
* @return void
* @access public
*/
public function unregisterClass($real_class, $pseudo_class = null)
{
unset($this->classMap[$real_class]);
}
}
class kFactoryException extends Exception {
-}
\ No newline at end of file
+}
Index: branches/5.3.x/core/kernel/languages/phrases_cache.php
===================================================================
--- branches/5.3.x/core/kernel/languages/phrases_cache.php (revision 16155)
+++ branches/5.3.x/core/kernel/languages/phrases_cache.php (revision 16156)
@@ -1,414 +1,415 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
-class PhrasesCache extends kBase {
+class kPhraseCache extends kBase
+{
var $Phrases = Array();
var $Ids = Array();
var $OriginalIds = Array(); //for comparing cache
var $LanguageId = null;
/**
* Administrator's language, when visiting site (from frame)
*
* @var int
*/
var $AdminLanguageId = null;
var $fromTag = false;
/**
* Allows to edit existing phrases
*
* @var bool
*/
var $_editExisting = false;
/**
* Allows to edit missing phrases
*
* @var bool
*/
var $_editMissing = false;
/**
* Template, used for phrase adding/editing
*
* @var string
*/
var $_phraseEditTemplate = 'languages/phrase_edit';
/**
* Use simplified form for phrase editing
*
* @var bool
*/
var $_simpleEditingMode = false;
/**
* HTML tag used to translate phrases
*
* @var string
*/
var $_translateHtmlTag = 'a';
/**
* Phrases, that are in cache, but are not in database
*
* @var Array
*/
var $_missingPhrases = Array ();
/**
* Mask for editing link
*
* @var string
*/
var $_editLinkMask = '';
/**
* Escape phrase name, before placing it in javascript translation link
*
* @var string
*/
var $_phraseEscapeStrategy = kUtil::ESCAPE_JS;
/**
* Sets phrase editing mode, that corresponds current editing mode
*
*/
function setPhraseEditing()
{
if (!$this->Application->isAdmin && (EDITING_MODE == EDITING_MODE_CONTENT)) {
// front-end viewed in content mode
$this->_editExisting = $this->_editMissing = true;
$this->_simpleEditingMode = !$this->Application->isDebugMode();
$this->_translateHtmlTag = 'span';
}
$this->_editLinkMask = $this->getRawEditLink('#LABEL#');
if (defined('DEBUG_MODE') && DEBUG_MODE && !$this->Application->GetVar('admin')) {
// admin and front-end while not viewed using content mode (via admin)
$this->_editMissing = defined('DBG_PHRASES') && DBG_PHRASES;
if (!$this->Application->isAdmin) {
$this->_phraseEditTemplate = 'phrases_edit';
$url_params = Array (
'm_opener' => 'd',
'phrases_label' => '#LABEL#',
'phrases_event' => 'OnPreparePhrase',
'next_template' => 'external:' . $_SERVER['REQUEST_URI'],
'pass' => 'm,phrases'
);
$this->_phraseEscapeStrategy = kUtil::ESCAPE_URL;
$this->_editLinkMask = $this->Application->HREF($this->_phraseEditTemplate, '', $url_params);
}
}
}
/**
* Returns raw link for given phrase editing.
*
* @param string $label Phrase label.
*
* @return string
*/
protected function getRawEditLink($label)
{
$function_params = array(
$label,
$this->_phraseEditTemplate,
array('event' => 'OnPreparePhrase', 'simple_mode' => $this->_simpleEditingMode),
);
return 'javascript:translate_phrase(' . implode(',', array_map('json_encode', $function_params)) . ');';
}
/**
* Returns final link (using mask) for given phrase editing.
*
* @param string $label Phrase label.
*
* @return string
*/
protected function getEditLink($label)
{
$escaped_label = kUtil::escape($label, $this->_phraseEscapeStrategy);
return str_replace('#LABEL#', $escaped_label, $this->_editLinkMask);
}
/**
* Returns HTML code for label editing.
*
* @param string $url Phrase editing url.
* @param string $text Link text to show (usually label in upper case).
* @param string $alt Text to display when hovered over the link.
*
* @return string
*/
protected function getEditHtmlCode($url, $text, $alt)
{
$url = kUtil::escape($url, kUtil::ESCAPE_HTML);
$ret = '<' . $this->_translateHtmlTag . ' href="' . $url . '" name="cms-translate-phrase" title="' . $alt . '">' . $text . '</' . $this->_translateHtmlTag . '>';
return $this->fromTag ? $this->escapeTagReserved($ret) : $ret;
}
/**
* Loads phrases from current language
* Method is called manually (not from kFactory class) too
*
* @param string $prefix
* @param string $special
* @param int $language_id
* @param Array $phrase_ids
*/
public function Init($prefix, $special = '', $language_id = null, $phrase_ids = null)
{
parent::Init($prefix, $special);
if (kUtil::constOn('IS_INSTALL')) {
$this->LanguageId = 1;
}
else {
if ( !isset($language_id) ) {
if ($this->Application->isAdmin) {
$language_id = $this->Application->Session->GetField('Language');
$this->AdminLanguageId = $language_id; // same languages, when used from Admin Console
}
else {
$language_id = $this->Application->GetVar('m_lang');
}
}
$this->LanguageId = $language_id;
if (!$this->Application->isAdmin && $this->Application->GetVar('admin')) {
$admin_session = $this->Application->recallObject('Session.admin');
/* @var $admin_session Session */
$this->AdminLanguageId = $admin_session->GetField('Language');
}
}
$this->LoadPhrases($phrase_ids);
}
function LoadPhrases($ids)
{
if ( !is_array($ids) || !implode('', $ids) ) {
return ;
}
$sql = 'SELECT l' . $this->LanguageId . '_Translation AS Translation, l' . $this->LanguageId . '_HintTranslation AS HintTranslation, l' . $this->LanguageId . '_ColumnTranslation AS ColumnTranslation, PhraseKey
FROM ' . TABLE_PREFIX . 'LanguageLabels
WHERE PhraseId IN (' . implode(',', $ids) . ') AND l' . $this->LanguageId . '_Translation IS NOT NULL';
$this->Phrases = $this->Conn->Query($sql, 'PhraseKey');
$this->Ids = $this->OriginalIds = $ids;
}
function AddCachedPhrase($label, $value, $allow_editing = true)
{
// uppercase phrase name for cases, when this method is called outside this class
$cache_key = ($allow_editing ? '' : 'NE:') . mb_strtoupper($label);
$this->Phrases[$cache_key] = Array ('Translation' => $value, 'HintTranslation' => $value, 'ColumnTranslation' => $value);
}
function NeedsCacheUpdate()
{
return is_array($this->Ids) && count($this->Ids) > 0 && $this->Ids != $this->OriginalIds;
}
/**
* Returns translation of given label
*
* @param string $label
* @param bool $allow_editing return translation link, when translation is missing on current language
* @param bool $use_admin use current Admin Console language to translate phrase
* @return string
* @access public
*/
public function GetPhrase($label, $allow_editing = true, $use_admin = false)
{
if ( !isset($this->LanguageId) ) {
//actually possible when custom field contains references to language labels and its being rebuilt in OnAfterConfigRead
//which is triggered by Sections rebuild, which in turn read all the configs and all of that happens BEFORE seeting the language...
return 'impossible case';
}
// cut exclamation marks - deprecated form of passing phrase name from templates
$label = preg_replace('/^!(.*)!$/', '\\1', $label);
if ( strlen($label) == 0 ) {
return '';
}
$original_label = $label;
list ($field_prefix, $label) = $this->parseLabel($label);
$translation_field = mb_convert_case($field_prefix, MB_CASE_TITLE) . 'Translation';
$uppercase_label = mb_strtoupper($label);
$cache_key = ($allow_editing ? '' : 'NE:') . $uppercase_label;
if ( isset($this->Phrases[$cache_key]) ) {
$translated_label = $this->Phrases[$cache_key][$translation_field];
if ($this->_editExisting && $allow_editing && !array_key_exists($uppercase_label, $this->_missingPhrases)) {
// option to change translation for Labels
$edit_link = $this->getRawEditLink($label);
$translated_label = $this->getEditHtmlCode($edit_link, $translated_label, 'Edit translation');
}
return $translated_label;
}
$this->LoadPhraseByLabel($uppercase_label, $original_label, $allow_editing, $use_admin);
return $this->GetPhrase($original_label, $allow_editing);
}
function LoadPhraseByLabel($uppercase_label, $original_label, $allow_editing = true, $use_admin = false)
{
if ( !$allow_editing && !$use_admin && !isset($this->_missingPhrases[$uppercase_label]) && isset($this->Phrases[$uppercase_label]) ) {
// label is already translated, but it's version without on the fly translation code is requested
$this->Phrases['NE:' . $uppercase_label] = $this->Phrases[$uppercase_label];
return true;
}
$language_id = $use_admin ? $this->AdminLanguageId : $this->LanguageId;
$sql = 'SELECT PhraseId, l' . $language_id . '_Translation AS Translation, l' . $language_id . '_HintTranslation AS HintTranslation, l' . $language_id . '_ColumnTranslation AS ColumnTranslation
FROM ' . TABLE_PREFIX . 'LanguageLabels
WHERE (PhraseKey = ' . $this->Conn->qstr($uppercase_label) . ') AND (l' . $language_id . '_Translation IS NOT NULL)';
$res = $this->Conn->GetRow($sql);
if ($res === false || count($res) == 0) {
$translation = '!' . $uppercase_label . '!';
if ($this->_editMissing && $allow_editing) {
list (, $original_label) = $this->parseLabel($original_label);
$edit_url = $this->getEditLink($original_label);
$translation = $this->getEditHtmlCode($edit_url, $translation, 'Translate');
$this->_missingPhrases[$uppercase_label] = true; // add as key for faster accessing
}
// add it as already cached, as long as we don't need to cache not found phrase
$this->AddCachedPhrase($uppercase_label, $translation, $allow_editing);
return false;
}
$cache_key = ($allow_editing ? '' : 'NE:') . $uppercase_label;
$this->Phrases[$cache_key] = $res;
array_push($this->Ids, $res['PhraseId']);
$this->Ids = array_unique($this->Ids); // just to make sure
return true;
}
/**
* Parse label into translation field prefix and actual label.
*
* @param string $label Phrase label.
*
* @return array
*/
protected function parseLabel($label)
{
if ( strpos($label, ':') === false || preg_match('/^(HINT|COLUMN):(.*)$/i', $label, $regs) == 0 ) {
return array('', $label);
}
return array($regs[1], $regs[2]);
}
/**
* Sort params by name and then by length
*
* @param string $a
* @param string $b
* @return int
* @access private
*/
function CmpParams($a, $b)
{
$a_len = mb_strlen($a);
$b_len = mb_strlen($b);
if ($a_len == $b_len) return 0;
return $a_len > $b_len ? -1 : 1;
}
/**
* Replace language tags in exclamation marks found in text
*
* @param string $text
* @param bool|null $force_escaping force escaping, not escaping of resulting string
* @return mixed
* @access public
*/
public function ReplaceLanguageTags($text, $force_escaping = null)
{
$this->fromTag = true;
if( isset($force_escaping) ) {
$this->fromTag = $force_escaping;
}
preg_match_all("(!(la|lu|lc)[^!]+!)", $text, $res, PREG_PATTERN_ORDER);
$language_tags = $res[0];
uasort($language_tags, Array(&$this, 'CmpParams'));
$i = 0;
$values = Array();
foreach ($language_tags as $label) {
array_push($values, $this->GetPhrase($label) );
//array_push($values, $this->Application->Phrase($label) );
$language_tags[$i] = '/' . $language_tags[$i] . '/';
$i++;
}
$this->fromTag = false;
return preg_replace($language_tags, $values, $text);
}
/**
* Escape chars in phrase translation, that could harm parser to process tag
*
* @param string $text
* @return string
* @access private
*/
function escapeTagReserved($text)
{
$reserved = Array('"', "'"); // =
$replacement = Array('\"', "\'"); // \=
return str_replace($reserved, $replacement, $text);
}
-}
\ No newline at end of file
+}
Index: branches/5.3.x/core/units/categories/categories_event_handler.php
===================================================================
--- branches/5.3.x/core/units/categories/categories_event_handler.php (revision 16155)
+++ branches/5.3.x/core/units/categories/categories_event_handler.php (revision 16156)
@@ -1,3156 +1,3156 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class CategoriesEventHandler extends kDBEventHandler {
/**
* Allows to override standard permission mapping
*
* @return void
* @access protected
* @see kEventHandler::$permMapping
*/
protected function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnRebuildCache' => Array ('self' => 'add|edit'),
'OnCopy' => Array ('self' => true),
'OnCut' => Array ('self' => 'edit'),
'OnPasteClipboard' => Array ('self' => true),
'OnPaste' => Array ('self' => 'add|edit', 'subitem' => 'edit'),
'OnRecalculatePriorities' => Array ('self' => 'add|edit'), // category ordering
'OnItemBuild' => Array ('self' => true), // always allow to view individual categories (regardless of CATEGORY.VIEW right)
'OnUpdatePreviewBlock' => Array ('self' => true), // for FCKEditor integration
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Categories are sorted using special sorting event
*
*/
function mapEvents()
{
parent::mapEvents();
$events_map = Array (
'OnMassMoveUp' => 'OnChangePriority',
'OnMassMoveDown' => 'OnChangePriority',
);
$this->eventMethods = array_merge($this->eventMethods, $events_map);
}
/**
* Checks user permission to execute given $event
*
* @param kEvent $event
* @return bool
* @access public
*/
public function CheckPermission(kEvent $event)
{
if ( $event->Name == 'OnResetCMSMenuCache' ) {
// events from "Tools -> System Tools" section are controlled via that section "edit" permission
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$perm_value = $this->Application->CheckPermission('in-portal:service.edit');
return $perm_helper->finalizePermissionCheck($event, $perm_value);
}
if ( !$this->Application->isAdmin ) {
if ( $event->Name == 'OnSetSortingDirect' ) {
// allow sorting on front event without view permission
return true;
}
if ( $event->Name == 'OnItemBuild' ) {
$category_id = $this->getPassedID($event);
if ( $category_id == 0 ) {
return true;
}
}
}
if ( in_array($event->Name, $this->_getMassPermissionEvents()) ) {
$items = $this->_getPermissionCheckInfo($event);
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
if ( ($event->Name == 'OnSave') && array_key_exists(0, $items) ) {
// adding new item (ID = 0)
$perm_value = $perm_helper->AddCheckPermission($items[0]['ParentId'], $event->Prefix) > 0;
}
else {
// leave only items, that can be edited
$ids = Array ();
$check_method = in_array($event->Name, Array ('OnMassDelete', 'OnCut')) ? 'DeleteCheckPermission' : 'ModifyCheckPermission';
foreach ($items as $item_id => $item_data) {
if ( $perm_helper->$check_method($item_data['CreatedById'], $item_data['ParentId'], $event->Prefix) > 0 ) {
$ids[] = $item_id;
}
}
if ( !$ids ) {
// no items left for editing -> no permission
return $perm_helper->finalizePermissionCheck($event, false);
}
$perm_value = true;
$event->setEventParam('ids', $ids); // will be used later by "kDBEventHandler::StoreSelectedIDs" method
}
return $perm_helper->finalizePermissionCheck($event, $perm_value);
}
if ( $event->Name == 'OnRecalculatePriorities' ) {
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$category_id = $this->Application->GetVar('m_cat_id');
return $perm_helper->AddCheckPermission($category_id, $event->Prefix) || $perm_helper->ModifyCheckPermission(0, $category_id, $event->Prefix);
}
if ( $event->Name == 'OnPasteClipboard' ) {
// forces permission check to work by current category for "Paste In Category" operation
$category_id = $this->Application->GetVar('m_cat_id');
$this->Application->SetVar('c_id', $category_id);
}
return parent::CheckPermission($event);
}
/**
* Returns events, that require item-based (not just event-name based) permission check
*
* @return Array
*/
function _getMassPermissionEvents()
{
return Array (
'OnEdit', 'OnSave', 'OnMassDelete', 'OnMassApprove',
'OnMassDecline', 'OnMassMoveUp', 'OnMassMoveDown',
'OnCut',
);
}
/**
* Returns category item IDs, that require permission checking
*
* @param kEvent $event
* @return string
*/
function _getPermissionCheckIDs($event)
{
if ($event->Name == 'OnSave') {
$selected_ids = implode(',', $this->getSelectedIDs($event, true));
if (!$selected_ids) {
$selected_ids = 0; // when saving newly created item (OnPreCreate -> OnPreSave -> OnSave)
}
}
else {
// OnEdit, OnMassDelete events, when items are checked in grid
$selected_ids = implode(',', $this->StoreSelectedIDs($event));
}
return $selected_ids;
}
/**
* Returns information used in permission checking
*
* @param kEvent $event
* @return Array
*/
function _getPermissionCheckInfo($event)
{
// when saving data from temp table to live table check by data from temp table
$config = $event->getUnitConfig();
$id_field = $config->getIDField();
$table_name = $config->getTableName();
if ($event->Name == 'OnSave') {
$table_name = $this->Application->GetTempName($table_name, 'prefix:' . $event->Prefix);
}
$sql = 'SELECT ' . $id_field . ', CreatedById, ParentId
FROM ' . $table_name . '
WHERE ' . $id_field . ' IN (' . $this->_getPermissionCheckIDs($event) . ')';
$items = $this->Conn->Query($sql, $id_field);
if (!$items) {
// when creating new category, then no IDs are stored in session
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
list ($id, $fields_hash) = each($items_info);
if (array_key_exists('ParentId', $fields_hash)) {
$item_category = $fields_hash['ParentId'];
}
else {
$item_category = $this->Application->RecallVar('m_cat_id'); // saved in c:OnPreCreate event permission checking
}
$items[$id] = Array (
'CreatedById' => $this->Application->RecallVar('user_id'),
'ParentId' => $item_category,
);
}
return $items;
}
/**
* Set's mark, that root category is edited
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnEdit(kEvent $event)
{
$category_id = $this->Application->GetVar($event->getPrefixSpecial() . '_id');
$home_category = $this->Application->getBaseCategory();
$this->Application->StoreVar('IsRootCategory_' . $this->Application->GetVar('m_wid'), ($category_id === '0') || ($category_id == $home_category));
parent::OnEdit($event);
if ( $event->status == kEvent::erSUCCESS ) {
// keep "Section Properties" link (in browse modes) clean
$this->Application->DeleteVar('admin');
}
}
/**
* Adds selected link to listing
*
* @param kEvent $event
*/
function OnProcessSelected($event)
{
$object = $event->getObject();
/* @var $object kDBItem */
$selected_ids = $this->Application->GetVar('selected_ids');
$this->RemoveRequiredFields($object);
$object->SetDBField($this->Application->RecallVar('dst_field'), $selected_ids['c']);
$object->Update();
$event->SetRedirectParam('opener', 'u');
}
/**
* Apply system filter to categories list
*
* @param kEvent $event
* @return void
* @access protected
* @see kDBEventHandler::OnListBuild()
*/
protected function SetCustomQuery(kEvent $event)
{
parent::SetCustomQuery($event);
$object = $event->getObject();
/* @var $object kDBList */
// don't show "Content" category in advanced view
$object->addFilter('system_categories', '%1$s.Status <> 4');
// show system templates from current theme only + all virtual templates
$object->addFilter('theme_filter', '%1$s.ThemeId = ' . $this->_getCurrentThemeId() . ' OR %1$s.ThemeId = 0');
if ($event->Special == 'showall') {
// if using recycle bin don't show categories from there
$recycle_bin = $this->Application->ConfigValue('RecycleBinFolder');
if ($recycle_bin) {
$sql = 'SELECT TreeLeft, TreeRight
FROM '.TABLE_PREFIX.'Categories
WHERE CategoryId = '.$recycle_bin;
$tree_indexes = $this->Conn->GetRow($sql);
$object->addFilter('recyclebin_filter', '%1$s.TreeLeft < '.$tree_indexes['TreeLeft'].' OR %1$s.TreeLeft > '.$tree_indexes['TreeRight']);
}
}
if ( (string)$event->getEventParam('parent_cat_id') !== '' ) {
$parent_cat_id = $event->getEventParam('parent_cat_id');
if ("$parent_cat_id" == 'Root') {
$module_name = $event->getEventParam('module') ? $event->getEventParam('module') : 'In-Commerce';
$parent_cat_id = $this->Application->findModule('Name', $module_name, 'RootCat');
}
}
else {
$parent_cat_id = $this->Application->GetVar('c_id');
if (!$parent_cat_id) {
$parent_cat_id = $this->Application->GetVar('m_cat_id');
}
if (!$parent_cat_id) {
$parent_cat_id = 0;
}
}
if ("$parent_cat_id" == '0') {
// replace "0" category with "Content" category id (this way template
$parent_cat_id = $this->Application->getBaseCategory();
}
if ("$parent_cat_id" != 'any') {
if ($event->getEventParam('recursive')) {
if ($parent_cat_id > 0) {
// not "Home" category
$tree_indexes = $this->Application->getTreeIndex($parent_cat_id);
$object->addFilter('parent_filter', '%1$s.TreeLeft BETWEEN '.$tree_indexes['TreeLeft'].' AND '.$tree_indexes['TreeRight']);
}
}
else {
$object->addFilter('parent_filter', '%1$s.ParentId = '.$parent_cat_id);
}
}
$this->applyViewPermissionFilter($object);
if (!$this->Application->isAdminUser) {
// apply status filter only on front
$object->addFilter('status_filter', $object->TableName.'.Status = 1');
}
// process "types" and "except" parameters
$type_clauses = Array();
$types = $event->getEventParam('types');
$types = $types ? explode(',', $types) : Array ();
$except_types = $event->getEventParam('except');
$except_types = $except_types ? explode(',', $except_types) : Array ();
$config = $event->getUnitConfig();
if (in_array('related', $types) || in_array('related', $except_types)) {
$related_to = $event->getEventParam('related_to');
if (!$related_to) {
$related_prefix = $event->Prefix;
}
else {
$sql = 'SELECT Prefix
FROM '.TABLE_PREFIX.'ItemTypes
WHERE ItemName = '.$this->Conn->qstr($related_to);
$related_prefix = $this->Conn->GetOne($sql);
}
$rel_table = $this->Application->getUnitConfig('rel')->getTableName();
$item_type = (int)$config->getItemType();
if ($item_type == 0) {
trigger_error('<strong>ItemType</strong> not defined for prefix <strong>' . $event->Prefix . '</strong>', E_USER_WARNING);
}
// process case, then this list is called inside another list
$prefix_special = $event->getEventParam('PrefixSpecial');
if (!$prefix_special) {
$prefix_special = $this->Application->Parser->GetParam('PrefixSpecial');
}
$id = false;
if ($prefix_special !== false) {
$processed_prefix = $this->Application->processPrefix($prefix_special);
if ($processed_prefix['prefix'] == $related_prefix) {
// printing related categories within list of items (not on details page)
$list = $this->Application->recallObject($prefix_special);
/* @var $list kDBList */
$id = $list->GetID();
}
}
if ($id === false) {
// printing related categories for single item (possibly on details page)
if ($related_prefix == 'c') {
$id = $this->Application->GetVar('m_cat_id');
}
else {
$id = $this->Application->GetVar($related_prefix . '_id');
}
}
$p_item = $this->Application->recallObject($related_prefix . '.current', null, Array('skip_autoload' => true));
/* @var $p_item kCatDBItem */
$p_item->Load( (int)$id );
$p_resource_id = $p_item->GetDBField('ResourceId');
$sql = 'SELECT SourceId, TargetId FROM '.$rel_table.'
WHERE
(Enabled = 1)
AND (
(Type = 0 AND SourceId = '.$p_resource_id.' AND TargetType = '.$item_type.')
OR
(Type = 1
AND (
(SourceId = '.$p_resource_id.' AND TargetType = '.$item_type.')
OR
(TargetId = '.$p_resource_id.' AND SourceType = '.$item_type.')
)
)
)';
$related_ids_array = $this->Conn->Query($sql);
$related_ids = Array();
foreach ($related_ids_array as $key => $record) {
$related_ids[] = $record[ $record['SourceId'] == $p_resource_id ? 'TargetId' : 'SourceId' ];
}
if (count($related_ids) > 0) {
$type_clauses['related']['include'] = '%1$s.ResourceId IN ('.implode(',', $related_ids).')';
$type_clauses['related']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $related_ids).')';
}
else {
$type_clauses['related']['include'] = '0';
$type_clauses['related']['except'] = '1';
}
$type_clauses['related']['having_filter'] = false;
}
if (in_array('category_related', $type_clauses)) {
$object->removeFilter('parent_filter');
$resource_id = $this->Conn->GetOne('
SELECT ResourceId FROM '.$config->getTableName().'
WHERE CategoryId = '.$parent_cat_id
);
$sql = 'SELECT DISTINCT(TargetId) FROM '.TABLE_PREFIX.'CatalogRelationships
WHERE SourceId = '.$resource_id.' AND SourceType = 1';
$related_cats = $this->Conn->GetCol($sql);
$related_cats = is_array($related_cats) ? $related_cats : Array();
$sql = 'SELECT DISTINCT(SourceId) FROM '.TABLE_PREFIX.'CatalogRelationships
WHERE TargetId = '.$resource_id.' AND TargetType = 1 AND Type = 1';
$related_cats2 = $this->Conn->GetCol($sql);
$related_cats2 = is_array($related_cats2) ? $related_cats2 : Array();
$related_cats = array_unique( array_merge( $related_cats2, $related_cats ) );
if ($related_cats) {
$type_clauses['category_related']['include'] = '%1$s.ResourceId IN ('.implode(',', $related_cats).')';
$type_clauses['category_related']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $related_cats).')';
}
else
{
$type_clauses['category_related']['include'] = '0';
$type_clauses['category_related']['except'] = '1';
}
$type_clauses['category_related']['having_filter'] = false;
}
if (in_array('product_related', $types)) {
$object->removeFilter('parent_filter');
$product_id = $event->getEventParam('product_id') ? $event->getEventParam('product_id') : $this->Application->GetVar('p_id');
$sql = 'SELECT ResourceId
FROM ' . $this->Application->getUnitConfig('p')->getTableName() . '
WHERE ProductId = ' . $product_id;
$resource_id = $this->Conn->GetOne($sql);
$sql = 'SELECT DISTINCT(TargetId)
FROM ' . TABLE_PREFIX . 'CatalogRelationships
WHERE SourceId = '.$resource_id.' AND TargetType = 1';
$related_cats = $this->Conn->GetCol($sql);
$related_cats = is_array($related_cats) ? $related_cats : Array();
$sql = 'SELECT DISTINCT(SourceId)
FROM ' . TABLE_PREFIX . 'CatalogRelationships
WHERE TargetId = '.$resource_id.' AND SourceType = 1 AND Type = 1';
$related_cats2 = $this->Conn->GetCol($sql);
$related_cats2 = is_array($related_cats2) ? $related_cats2 : Array();
$related_cats = array_unique( array_merge( $related_cats2, $related_cats ) );
if ($related_cats) {
$type_clauses['product_related']['include'] = '%1$s.ResourceId IN ('.implode(',', $related_cats).')';
$type_clauses['product_related']['except'] = '%1$s.ResourceId NOT IN ('.implode(',', $related_cats).')';
}
else {
$type_clauses['product_related']['include'] = '0';
$type_clauses['product_related']['except'] = '1';
}
$type_clauses['product_related']['having_filter'] = false;
}
$type_clauses['menu']['include'] = '%1$s.IsMenu = 1';
$type_clauses['menu']['except'] = '%1$s.IsMenu = 0';
$type_clauses['menu']['having_filter'] = false;
if (in_array('search', $types) || in_array('search', $except_types)) {
$event_mapping = Array (
'simple' => 'OnSimpleSearch',
'subsearch' => 'OnSubSearch',
'advanced' => 'OnAdvancedSearch'
);
$keywords = $event->getEventParam('keyword_string');
$type = $this->Application->GetVar('search_type', 'simple');
if ( $keywords ) {
// processing keyword_string param of ListProducts tag
$this->Application->SetVar('keywords', $keywords);
$type = 'simple';
}
$search_event = $event_mapping[$type];
$this->$search_event($event);
$object = $event->getObject();
/* @var $object kDBList */
$search_sql = ' FROM ' . TABLE_PREFIX . 'ses_' . $this->Application->GetSID() . '_' . TABLE_PREFIX . 'Search
search_result JOIN %1$s ON %1$s.ResourceId = search_result.ResourceId';
$sql = str_replace('FROM %1$s', $search_sql, $object->GetPlainSelectSQL());
$object->SetSelectSQL($sql);
$object->addCalculatedField('Relevance', 'search_result.Relevance');
$type_clauses['search']['include'] = '1';
$type_clauses['search']['except'] = '0';
$type_clauses['search']['having_filter'] = false;
}
$search_helper = $this->Application->recallObject('SearchHelper');
/* @var $search_helper kSearchHelper */
$search_helper->SetComplexFilter($event, $type_clauses, implode(',', $types), implode(',', $except_types));
}
/**
* Adds filter, that uses *.VIEW permissions to determine if an item should be shown to a user.
*
* @param kDBList $object Object.
*
* @return void
* @access protected
*/
protected function applyViewPermissionFilter(kDBList $object)
{
if ( !$this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) {
return;
}
if ( $this->Application->RecallVar('user_id') == USER_ROOT ) {
// for "root" CATEGORY.VIEW permission is checked for items lists too
$view_perm = 1;
}
else {
$count_helper = $this->Application->recallObject('CountHelper');
/* @var $count_helper kCountHelper */
list ($view_perm, $view_filter) = $count_helper->GetPermissionClause($object->Prefix, 'perm');
$object->addFilter('perm_filter2', $view_filter);
}
$object->addFilter('perm_filter', 'perm.PermId = ' . $view_perm); // check for CATEGORY.VIEW permission
}
/**
* Returns current theme id
*
* @return int
*/
function _getCurrentThemeId()
{
$themes_helper = $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
return (int)$themes_helper->getCurrentThemeId();
}
/**
* Returns ID of current item to be edited
* by checking ID passed in get/post as prefix_id
* or by looking at first from selected ids, stored.
* Returned id is also stored in Session in case
* it was explicitly passed as get/post
*
* @param kEvent $event
* @return int
* @access public
*/
public function getPassedID(kEvent $event)
{
if ( ($event->Special == 'page') || $this->_isVirtual($event) || ($event->Prefix == 'st') ) {
return $this->_getPassedStructureID($event);
}
if ( $this->Application->isAdmin ) {
return parent::getPassedID($event);
}
return $this->Application->GetVar('m_cat_id');
}
/**
* Enter description here...
*
* @param kEvent $event
* @return int
*/
function _getPassedStructureID($event)
{
static $page_by_template = Array ();
if ( $event->Special == 'current' ) {
return $this->Application->GetVar('m_cat_id');
}
$event->setEventParam('raise_warnings', 0);
$page_id = parent::getPassedID($event);
if ( $page_id === false ) {
$template = $event->getEventParam('page');
if ( !$template ) {
$template = $this->Application->GetVar('t');
}
// bug: when template contains "-" symbols (or others, that stripDisallowed will replace) it's not found
if ( !array_key_exists($template, $page_by_template) ) {
$config = $event->getUnitConfig();
$template_crc = kUtil::crc32(mb_strtolower($template));
$sql = 'SELECT ' . $config->getIDField() . '
FROM ' . $config->getTableName() . '
WHERE
(
(NamedParentPathHash = ' . $template_crc . ') OR
(`Type` = ' . PAGE_TYPE_TEMPLATE . ' AND CachedTemplateHash = ' . $template_crc . ')
) AND (ThemeId = ' . $this->_getCurrentThemeId() . ' OR ThemeId = 0)';
$page_id = $this->Conn->GetOne($sql);
}
else {
$page_id = $page_by_template[$template];
}
if ( $page_id ) {
$page_by_template[$template] = $page_id;
}
}
if ( !$page_id && !$this->Application->isAdmin ) {
$page_id = $this->Application->GetVar('m_cat_id');
}
return $page_id;
}
function ParentGetPassedID($event)
{
return parent::getPassedID($event);
}
/**
* Adds calculates fields for item statuses
*
* @param kCatDBItem $object
* @param kEvent $event
* @return void
* @access protected
*/
protected function prepareObject(&$object, kEvent $event)
{
if ( $this->_isVirtual($event) ) {
return;
}
$object = $event->getObject(Array ('skip_autoload' => true));
/* @var $object kDBItem */
$object->addCalculatedField(
'IsNew',
' IF(%1$s.NewItem = 2,
IF(%1$s.CreatedOn >= (UNIX_TIMESTAMP() - '.
$this->Application->ConfigValue('Category_DaysNew').
'*3600*24), 1, 0),
%1$s.NewItem
)');
}
/**
* Checks, that this is virtual page
*
* @param kEvent $event
* @return int
* @access protected
*/
protected function _isVirtual(kEvent $event)
{
return strpos($event->Special, '-virtual') !== false;
}
/**
* Gets right special for configuring virtual page
*
* @param kEvent $event
* @return string
* @access protected
*/
protected function _getCategorySpecial(kEvent $event)
{
return $this->_isVirtual($event) ? '-virtual' : $event->Special;
}
/**
* Set correct parent path for newly created categories
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterCopyToLive(kEvent $event)
{
parent::OnAfterCopyToLive($event);
$object = $this->Application->recallObject($event->Prefix . '.-item', null, Array ('skip_autoload' => true, 'live_table' => true));
/* @var $object CategoriesItem */
$parent_path = false;
$object->Load($event->getEventParam('id'));
if ( $event->getEventParam('temp_id') == 0 ) {
if ( $object->isLoaded() ) {
// update path only for real categories (not including "Home" root category)
$fields_hash = $object->buildParentBasedFields();
$this->Conn->doUpdate($fields_hash, $object->TableName, 'CategoryId = ' . $object->GetID());
$parent_path = $fields_hash['ParentPath'];
}
}
else {
$parent_path = $object->GetDBField('ParentPath');
}
if ( $parent_path ) {
$cache_updater = $this->Application->makeClass('kPermCacheUpdater', Array (null, $parent_path));
/* @var $cache_updater kPermCacheUpdater */
$cache_updater->OneStepRun();
}
}
/**
* Set cache modification mark if needed
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeDeleteFromLive(kEvent $event)
{
parent::OnBeforeDeleteFromLive($event);
$id = $event->getEventParam('id');
// loading anyway, because this object is needed by "c-perm:OnBeforeDeleteFromLive" event
$temp_object = $event->getObject(Array ('skip_autoload' => true));
/* @var $temp_object CategoriesItem */
$temp_object->Load($id);
if ( $id == 0 ) {
if ( $temp_object->isLoaded() ) {
// new category -> update cache (not loaded when "Home" category)
$this->Application->StoreVar('PermCache_UpdateRequired', 1);
}
return ;
}
// existing category was edited, check if in-cache fields are modified
$live_object = $this->Application->recallObject($event->Prefix . '.-item', null, Array ('live_table' => true, 'skip_autoload' => true));
/* @var $live_object CategoriesItem */
$live_object->Load($id);
$cached_fields = Array ('l' . $this->Application->GetDefaultLanguageId() . '_Name', 'Filename', 'Template', 'ParentId', 'Priority');
foreach ($cached_fields as $cached_field) {
if ( $live_object->GetDBField($cached_field) != $temp_object->GetDBField($cached_field) ) {
// use session instead of REQUEST because of permission editing in category can contain
// multiple submits, that changes data before OnSave event occurs
$this->Application->StoreVar('PermCache_UpdateRequired', 1);
break;
}
}
// remember category filename change between temp and live records
if ( $temp_object->GetDBField('Filename') != $live_object->GetDBField('Filename') ) {
$filename_changes = $this->Application->GetVar($event->Prefix . '_filename_changes', Array ());
$filename_changes[ $live_object->GetID() ] = Array (
'from' => $live_object->GetDBField('Filename'),
'to' => $temp_object->GetDBField('Filename')
);
$this->Application->SetVar($event->Prefix . '_filename_changes', $filename_changes);
}
}
/**
* Calls kDBEventHandler::OnSave original event
* Used in proj-cms:StructureEventHandler->OnSave
*
* @param kEvent $event
*/
function parentOnSave($event)
{
parent::OnSave($event);
}
/**
* Reset root-category flag when new category is created
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnPreCreate(kEvent $event)
{
// 1. for permission editing of Home category
$this->Application->RemoveVar('IsRootCategory_' . $this->Application->GetVar('m_wid'));
parent::OnPreCreate($event);
$object = $event->getObject();
/* @var $object kDBItem */
// 2. preset template
$category_id = $this->Application->GetVar('m_cat_id');
$root_category = $this->Application->getBaseCategory();
if ( $category_id == $root_category ) {
$object->SetDBField('Template', $this->_getDefaultDesign());
}
// 3. set default owner
$object->SetDBField('CreatedById', $this->Application->RecallVar('user_id'));
}
/**
* Checks cache update mark and redirect to cache if needed
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnSave(kEvent $event)
{
// get data from live table before it is overwritten by parent OnSave method call
$ids = $this->getSelectedIDs($event, true);
$is_editing = implode('', $ids);
$old_statuses = $is_editing ? $this->_getCategoryStatus($ids) : Array ();
$object = $event->getObject();
/* @var $object CategoriesItem */
parent::OnSave($event);
if ( $event->status != kEvent::erSUCCESS ) {
return;
}
if ( $this->Application->RecallVar('PermCache_UpdateRequired') ) {
$this->Application->RemoveVar('IsRootCategory_' . $this->Application->GetVar('m_wid'));
}
$this->Application->StoreVar('RefreshStructureTree', 1);
$this->_resetMenuCache();
if ( $is_editing ) {
// send email event to category owner, when it's status is changed (from admin)
$object->SwitchToLive();
$new_statuses = $this->_getCategoryStatus($ids);
$process_statuses = Array (STATUS_ACTIVE, STATUS_DISABLED);
foreach ($new_statuses as $category_id => $new_status) {
if ( $new_status != $old_statuses[$category_id] && in_array($new_status, $process_statuses) ) {
$object->Load($category_id);
$email_event = $new_status == STATUS_ACTIVE ? 'CATEGORY.APPROVE' : 'CATEGORY.DENY';
$this->Application->emailUser($email_event, $object->GetDBField('CreatedById'), $object->getEmailParams());
}
}
}
// change opener stack in case if edited category filename was changed
$filename_changes = $this->Application->GetVar($event->Prefix . '_filename_changes', Array ());
if ( $filename_changes ) {
$opener_stack = $this->Application->makeClass('kOpenerStack');
/* @var $opener_stack kOpenerStack */
list ($template, $params, $index_file) = $opener_stack->pop();
foreach ($filename_changes as $change_info) {
$template = str_ireplace($change_info['from'], $change_info['to'], $template);
}
$opener_stack->push($template, $params, $index_file);
$opener_stack->save();
}
}
/**
* Returns statuses of given categories
*
* @param Array $category_ids
* @return Array
*/
function _getCategoryStatus($category_ids)
{
$config = $this->getUnitConfig();
$id_field = $config->getIDField();
$table_name = $config->getTableName();
$sql = 'SELECT Status, ' . $id_field . '
FROM ' . $table_name . '
WHERE ' . $id_field . ' IN (' . implode(',', $category_ids) . ')';
return $this->Conn->GetCol($sql, $id_field);
}
/**
* Creates a new item in temp table and
* stores item id in App vars and Session on success
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnPreSaveCreated(kEvent $event)
{
$object = $event->getObject( Array ('skip_autoload' => true) );
/* @var $object CategoriesItem */
if ( $object->IsRoot() ) {
// don't create root category while saving permissions
return;
}
parent::OnPreSaveCreated($event);
}
/**
* Deletes sym link to other category
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterItemDelete(kEvent $event)
{
parent::OnAfterItemDelete($event);
$object = $event->getObject();
/* @var $object kDBItem */
$sql = 'UPDATE ' . $object->TableName . '
SET SymLinkCategoryId = NULL
WHERE SymLinkCategoryId = ' . $object->GetID();
$this->Conn->Query($sql);
// delete direct subscriptions to category, that was deleted
$sql = 'SELECT SubscriptionId
FROM ' . TABLE_PREFIX . 'SystemEventSubscriptions
WHERE CategoryId = ' . $object->GetID();
$ids = $this->Conn->GetCol($sql);
if ( $ids ) {
$temp_handler = $this->Application->recallObject('system-event-subscription_TempHandler', 'kTempTablesHandler', Array ('parent_event' => $event->MasterEvent));
/* @var $temp_handler kTempTablesHandler */
$temp_handler->DeleteItems('system-event-subscription', '', $ids);
}
}
/**
* Exclude root categories from deleting
*
* @param kEvent $event
* @param string $type
* @return void
* @access protected
*/
protected function customProcessing(kEvent $event, $type)
{
if ( $event->Name == 'OnMassDelete' && $type == 'before' ) {
$ids = $event->getEventParam('ids');
if ( !$ids || $this->Application->ConfigValue('AllowDeleteRootCats') ) {
return;
}
$root_categories = Array ();
// get module root categories and exclude them
foreach ($this->Application->ModuleInfo as $module_info) {
$root_categories[] = $module_info['RootCat'];
}
$root_categories = array_unique($root_categories);
if ( $root_categories && array_intersect($ids, $root_categories) ) {
$event->setEventParam('ids', array_diff($ids, $root_categories));
$this->Application->StoreVar('root_delete_error', 1);
}
}
}
/**
* Checks, that given template exists (physically) in given theme
*
* @param string $template
* @param int $theme_id
* @return bool
*/
function _templateFound($template, $theme_id = null)
{
static $init_made = false;
if (!$init_made) {
$this->Application->InitParser(true);
$init_made = true;
}
if (!isset($theme_id)) {
$theme_id = $this->_getCurrentThemeId();
}
$theme_name = $this->_getThemeName($theme_id);
return $this->Application->TemplatesCache->TemplateExists('theme:' . $theme_name . '/' . $template);
}
/**
* Removes ".tpl" in template path
*
* @param string $template
* @return string
*/
function _stripTemplateExtension($template)
{
// return preg_replace('/\.[^.\\\\\\/]*$/', '', $template);
return preg_replace('/^[\\/]{0,1}(.*)\.tpl$/', "$1", $template);
}
/**
* Deletes all selected items.
* Automatically recourse into sub-items using temp handler, and deletes sub-items
* by calling its Delete method if sub-item has AutoDelete set to true in its config file
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnMassDelete(kEvent $event)
{
if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) {
$event->status = kEvent::erFAIL;
return;
}
$to_delete = Array ();
$ids = $this->StoreSelectedIDs($event);
$recycle_bin = $this->Application->ConfigValue('RecycleBinFolder');
if ( $recycle_bin ) {
$rb = $this->Application->recallObject('c.recycle', null, Array ('skip_autoload' => true));
/* @var $rb CategoriesItem */
$rb->Load($recycle_bin);
$cat = $event->getObject(Array ('skip_autoload' => true));
/* @var $cat CategoriesItem */
foreach ($ids as $id) {
$cat->Load($id);
if ( preg_match('/^' . preg_quote($rb->GetDBField('ParentPath'), '/') . '/', $cat->GetDBField('ParentPath')) ) {
// already in "Recycle Bin" -> delete for real
$to_delete[] = $id;
continue;
}
// just move into "Recycle Bin" category
$cat->SetDBField('ParentId', $recycle_bin);
$cat->Update();
}
$ids = $to_delete;
}
$event->setEventParam('ids', $ids);
$this->customProcessing($event, 'before');
$ids = $event->getEventParam('ids');
if ( $ids ) {
$recursive_helper = $this->Application->recallObject('RecursiveHelper');
/* @var $recursive_helper kRecursiveHelper */
foreach ($ids as $id) {
$recursive_helper->DeleteCategory($id, $event->Prefix);
}
}
$this->clearSelectedIDs($event);
$this->_ensurePermCacheRebuild($event);
}
/**
* Add selected items to clipboard with mode = COPY (CLONE)
*
* @param kEvent $event
*/
function OnCopy($event)
{
$this->Application->RemoveVar('clipboard');
$clipboard_helper = $this->Application->recallObject('ClipboardHelper');
/* @var $clipboard_helper kClipboardHelper */
$clipboard_helper->setClipboard($event, 'copy', $this->StoreSelectedIDs($event));
$this->clearSelectedIDs($event);
}
/**
* Add selected items to clipboard with mode = CUT
*
* @param kEvent $event
*/
function OnCut($event)
{
$this->Application->RemoveVar('clipboard');
$clipboard_helper = $this->Application->recallObject('ClipboardHelper');
/* @var $clipboard_helper kClipboardHelper */
$clipboard_helper->setClipboard($event, 'cut', $this->StoreSelectedIDs($event));
$this->clearSelectedIDs($event);
}
/**
* Controls all item paste operations. Can occur only with filled clipboard.
*
* @param kEvent $event
*/
function OnPasteClipboard($event)
{
$clipboard = unserialize( $this->Application->RecallVar('clipboard') );
foreach ($clipboard as $prefix => $clipboard_data) {
$paste_event = new kEvent($prefix.':OnPaste', Array('clipboard_data' => $clipboard_data));
$this->Application->HandleEvent($paste_event);
$event->copyFrom($paste_event);
}
}
/**
* Checks permission for OnPaste event
*
* @param kEvent $event
* @return bool
*/
function _checkPastePermission($event)
{
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$category_id = $this->Application->GetVar('m_cat_id');
if ($perm_helper->AddCheckPermission($category_id, $event->Prefix) == 0) {
// no items left for editing -> no permission
return $perm_helper->finalizePermissionCheck($event, false);
}
return true;
}
/**
* Paste categories with sub-items from clipboard
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnPaste($event)
{
if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) || !$this->_checkPastePermission($event) ) {
$event->status = kEvent::erFAIL;
return;
}
$clipboard_data = $event->getEventParam('clipboard_data');
if ( !$clipboard_data['cut'] && !$clipboard_data['copy'] ) {
return;
}
// 1. get ParentId of moved category(-es) before it gets updated!!!)
$source_category_id = 0;
$config = $event->getUnitConfig();
$id_field = $config->getIDField();
$table_name = $config->getTableName();
if ( $clipboard_data['cut'] ) {
$sql = 'SELECT ParentId
FROM ' . $table_name . '
WHERE ' . $id_field . ' = ' . $clipboard_data['cut'][0];
$source_category_id = $this->Conn->GetOne($sql);
}
$recursive_helper = $this->Application->recallObject('RecursiveHelper');
/* @var $recursive_helper kRecursiveHelper */
if ( $clipboard_data['cut'] ) {
$recursive_helper->MoveCategories($clipboard_data['cut'], $this->Application->GetVar('m_cat_id'));
}
if ( $clipboard_data['copy'] ) {
// don't allow to copy/paste system OR theme-linked virtual pages
$sql = 'SELECT ' . $id_field . '
FROM ' . $table_name . '
WHERE ' . $id_field . ' IN (' . implode(',', $clipboard_data['copy']) . ') AND (`Type` = ' . PAGE_TYPE_VIRTUAL . ') AND (ThemeId = 0)';
$allowed_ids = $this->Conn->GetCol($sql);
if ( !$allowed_ids ) {
return;
}
foreach ($allowed_ids as $id) {
$recursive_helper->PasteCategory($id, $event->Prefix);
}
}
$priority_helper = $this->Application->recallObject('PriorityHelper');
/* @var $priority_helper kPriorityHelper */
if ( $clipboard_data['cut'] ) {
$ids = $priority_helper->recalculatePriorities($event, 'ParentId = ' . $source_category_id);
if ( $ids ) {
$priority_helper->massUpdateChanged($event->Prefix, $ids);
}
}
// recalculate priorities of newly pasted categories in destination category
$parent_id = $this->Application->GetVar('m_cat_id');
$ids = $priority_helper->recalculatePriorities($event, 'ParentId = ' . $parent_id);
if ( $ids ) {
$priority_helper->massUpdateChanged($event->Prefix, $ids);
}
if ( $clipboard_data['cut'] || $clipboard_data['copy'] ) {
$this->_ensurePermCacheRebuild($event);
}
}
/**
* Ensures, that category permission cache is rebuild when category is added/edited/deleted
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function _ensurePermCacheRebuild(kEvent $event)
{
$this->Application->StoreVar('PermCache_UpdateRequired', 1);
$this->Application->StoreVar('RefreshStructureTree', 1);
}
/**
* Occurs when pasting category
*
* @param kEvent $event
*/
/*function OnCatPaste($event)
{
$inp_clipboard = $this->Application->RecallVar('ClipBoard');
$inp_clipboard = explode('-', $inp_clipboard, 2);
if($inp_clipboard[0] == 'COPY')
{
$config = $event->getUnitConfig();
$cat_ids = $event->getEventParam('cat_ids');
$saved_cat_id = $this->Application->GetVar('m_cat_id');
$ids_sql = 'SELECT ' . $config->getIDField() . '
FROM ' . $config->getTableName() . '
WHERE ResourceId IN (%s)';
$resource_ids_sql = 'SELECT ItemResourceId FROM '.TABLE_PREFIX.'CategoryItems WHERE CategoryId = %s AND PrimaryCat = 1';
$object = $this->Application->recallObject($event->Prefix.'.item', $event->Prefix, Array('skip_autoload' => true));
foreach($cat_ids as $source_cat => $dest_cat)
{
$item_resource_ids = $this->Conn->GetCol( sprintf($resource_ids_sql, $source_cat) );
if(!$item_resource_ids) continue;
$this->Application->SetVar('m_cat_id', $dest_cat);
$item_ids = $this->Conn->GetCol( sprintf($ids_sql, implode(',', $item_resource_ids) ) );
$temp = $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler', Array ('parent_event' => $event));
if($item_ids) $temp->CloneItems($event->Prefix, $event->Special, $item_ids);
}
$this->Application->SetVar('m_cat_id', $saved_cat_id);
}
}*/
/**
* Clears clipboard content
*
* @param kEvent $event
*/
function OnClearClipboard($event)
{
$this->Application->RemoveVar('clipboard');
}
/**
* Sets correct status for new categories created on front-end
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemCreate(kEvent $event)
{
parent::OnBeforeItemCreate($event);
$object = $event->getObject();
/* @var $object CategoriesItem */
if ( $object->GetDBField('ParentId') <= 0 ) {
// no parent category - use current (happens during import)
$object->SetDBField('ParentId', $this->Application->GetVar('m_cat_id'));
}
$this->_beforeItemChange($event);
if ( $this->Application->isAdmin || $event->Prefix == 'st' ) {
// don't check category permissions when auto-creating structure pages
return ;
}
$perm_helper = $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$new_status = false;
$category_id = $this->Application->GetVar('m_cat_id');
if ( $perm_helper->CheckPermission('CATEGORY.ADD', 0, $category_id) ) {
$new_status = STATUS_ACTIVE;
}
else {
if ( $perm_helper->CheckPermission('CATEGORY.ADD.PENDING', 0, $category_id) ) {
$new_status = STATUS_PENDING;
}
}
if ( $new_status ) {
$object->SetDBField('Status', $new_status);
// don't forget to set Priority for suggested from Front-End categories
$min_priority = $this->_getNextPriority($object->GetDBField('ParentId'), $object->TableName);
$object->SetDBField('Priority', $min_priority);
}
else {
$event->status = kEvent::erPERM_FAIL;
return ;
}
}
/**
* Returns next available priority for given category from given table
*
* @param int $category_id
* @param string $table_name
* @return int
*/
function _getNextPriority($category_id, $table_name)
{
$sql = 'SELECT MIN(Priority)
FROM ' . $table_name . '
WHERE ParentId = ' . $category_id;
return (int)$this->Conn->GetOne($sql) - 1;
}
/**
* Sets correct status for new categories created on front-end
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemUpdate(kEvent $event)
{
parent::OnBeforeItemUpdate($event);
$this->_beforeItemChange($event);
$object = $event->getObject();
/* @var $object kDBItem */
if ( $object->GetChangedFields() ) {
$object->SetDBField('ModifiedById', $this->Application->RecallVar('user_id'));
}
}
/**
* Creates needed sql query to load item,
* if no query is defined in config for
* special requested, then use list query
*
* @param kEvent $event
* @return string
* @access protected
*/
protected function ItemPrepareQuery(kEvent $event)
{
$object = $event->getObject();
/* @var $object kDBItem */
$sqls = $object->getFormOption('ItemSQLs', Array ());
$category_special = $this->_getCategorySpecial($event);
$special = isset($sqls[$category_special]) ? $category_special : '';
// preferred special not found in ItemSQLs -> use analog from ListSQLs
return isset($sqls[$special]) ? $sqls[$special] : $this->ListPrepareQuery($event);
}
/**
* Creates needed sql query to load list,
* if no query is defined in config for
* special requested, then use default
* query
*
* @param kEvent $event
* @return string
* @access protected
*/
protected function ListPrepareQuery(kEvent $event)
{
$object = $event->getObject();
/* @var $object kDBItem */
$special = $this->_getCategorySpecial($event);
$sqls = $object->getFormOption('ListSQLs', Array ());
return $sqls[array_key_exists($special, $sqls) ? $special : ''];
}
/**
* Performs redirect to correct suggest confirmation template
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnCreate(kEvent $event)
{
parent::OnCreate($event);
if ( $this->Application->isAdmin || $event->status != kEvent::erSUCCESS ) {
// don't sent email or rebuild cache directly after category is created by admin
return;
}
$object = $event->getObject();
/* @var $object kDBItem */
$cache_updater = $this->Application->makeClass('kPermCacheUpdater', Array (null, $object->GetDBField('ParentPath')));
/* @var $cache_updater kPermCacheUpdater */
$cache_updater->OneStepRun();
$is_active = ($object->GetDBField('Status') == STATUS_ACTIVE);
$next_template = $is_active ? 'suggest_confirm_template' : 'suggest_pending_confirm_template';
$event->redirect = $this->Application->GetVar($next_template);
$event->SetRedirectParam('opener', 's');
// send email events
$send_params = $object->getEmailParams();
$event_suffix = $is_active ? 'ADD' : 'ADD.PENDING';
$perm_prefix = $event->getUnitConfig()->getPermItemPrefix();
$this->Application->emailUser($perm_prefix . '.' . $event_suffix, $object->GetDBField('CreatedById'), $send_params);
$this->Application->emailAdmin($perm_prefix . '.' . $event_suffix, null, $send_params);
}
/**
* Returns current per-page setting for list
*
* @param kEvent $event
* @return int
* @access protected
*/
protected function getPerPage(kEvent $event)
{
if ( !$this->Application->isAdmin ) {
$same_special = $event->getEventParam('same_special');
$event->setEventParam('same_special', true);
$per_page = parent::getPerPage($event);
$event->setEventParam('same_special', $same_special);
}
return parent::getPerPage($event);
}
/**
* Set's correct page for list based on data provided with event
*
* @param kEvent $event
* @return void
* @access protected
* @see kDBEventHandler::OnListBuild()
*/
protected function SetPagination(kEvent $event)
{
parent::SetPagination($event);
if ( !$this->Application->isAdmin ) {
$page_var = $event->getEventParam('page_var');
if ( $page_var !== false ) {
$page = $this->Application->GetVar($page_var);
if ( is_numeric($page) ) {
$object = $event->getObject();
/* @var $object kDBList */
$object->SetPage($page);
}
}
}
}
/**
* Apply same processing to each item being selected in grid
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function iterateItems(kEvent $event)
{
if ( $event->Name != 'OnMassApprove' && $event->Name != 'OnMassDecline' ) {
parent::iterateItems($event);
}
if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) {
$event->status = kEvent::erFAIL;
return;
}
$object = $event->getObject(Array ('skip_autoload' => true));
/* @var $object CategoriesItem */
$ids = $this->StoreSelectedIDs($event);
if ( $ids ) {
$propagate_category_status = $this->Application->GetVar('propagate_category_status');
$status_field = $event->getUnitConfig()->getStatusField(true);
foreach ($ids as $id) {
$object->Load($id);
$object->SetDBField($status_field, $event->Name == 'OnMassApprove' ? 1 : 0);
if ( $object->Update() ) {
if ( $propagate_category_status ) {
$sql = 'UPDATE ' . $object->TableName . '
SET ' . $status_field . ' = ' . $object->GetDBField($status_field) . '
WHERE TreeLeft BETWEEN ' . $object->GetDBField('TreeLeft') . ' AND ' . $object->GetDBField('TreeRight');
$this->Conn->Query($sql);
}
$event->status = kEvent::erSUCCESS;
$email_event = $event->Name == 'OnMassApprove' ? 'CATEGORY.APPROVE' : 'CATEGORY.DENY';
$this->Application->emailUser($email_event, $object->GetDBField('CreatedById'), $object->getEmailParams());
}
else {
$event->status = kEvent::erFAIL;
$event->redirect = false;
break;
}
}
}
$this->clearSelectedIDs($event);
$this->Application->StoreVar('RefreshStructureTree', 1);
}
/**
* Checks, that currently loaded item is allowed for viewing (non permission-based)
*
* @param kEvent $event
* @return bool
* @access protected
*/
protected function checkItemStatus(kEvent $event)
{
$object = $event->getObject();
/* @var $object kDBItem */
if ( !$object->isLoaded() ) {
return true;
}
if ( $object->GetDBField('Status') != STATUS_ACTIVE && $object->GetDBField('Status') != 4 ) {
if ( !$object->GetDBField('DirectLinkEnabled') || !$object->GetDBField('DirectLinkAuthKey') ) {
return false;
}
return $this->Application->GetVar('authkey') == $object->GetDBField('DirectLinkAuthKey');
}
return true;
}
/**
* Set's correct sorting for list based on data provided with event
*
* @param kEvent $event
* @return void
* @access protected
* @see kDBEventHandler::OnListBuild()
*/
protected function SetSorting(kEvent $event)
{
$types = $event->getEventParam('types');
$types = $types ? explode(',', $types) : Array ();
if ( in_array('search', $types) ) {
$event->setPseudoClass('_List');
$object = $event->getObject();
/* @var $object kDBList */
// 1. no user sorting - sort by relevance
$default_sortings = parent::_getDefaultSorting($event);
$default_sorting = key($default_sortings['Sorting']) . ',' . current($default_sortings['Sorting']);
if ( $object->isMainList() ) {
$sort_by = $this->Application->GetVar('sort_by', '');
if ( !$sort_by ) {
$this->Application->SetVar('sort_by', 'Relevance,desc|' . $default_sorting);
}
elseif ( strpos($sort_by, 'Relevance,') !== false ) {
$this->Application->SetVar('sort_by', $sort_by . '|' . $default_sorting);
}
}
else {
$sorting_settings = $this->getListSetting($event, 'Sortings');
$sort_by = trim(getArrayValue($sorting_settings, 'Sort1') . ',' . getArrayValue($sorting_settings, 'Sort1_Dir'), ',');
if ( !$sort_by ) {
$event->setEventParam('sort_by', 'Relevance,desc|' . $default_sorting);
}
elseif ( strpos($sort_by, 'Relevance,') !== false ) {
$event->setEventParam('sort_by', $sort_by . '|' . $default_sorting);
}
}
$this->_removeForcedSortings($event);
}
parent::SetSorting($event);
}
/**
* Removes forced sortings
*
* @param kEvent $event
*/
protected function _removeForcedSortings(kEvent $event)
{
$config = $event->getUnitConfig();
foreach ($config->getListSortingSpecials() as $special) {
$list_sortings = $config->getListSortingsBySpecial($special);
unset($list_sortings['ForcedSorting']);
$config->setListSortingsBySpecial('', $list_sortings);
}
}
/**
* Default sorting in search results only comes from relevance field
*
* @param kEvent $event
* @return Array
* @access protected
*/
protected function _getDefaultSorting(kEvent $event)
{
$types = $event->getEventParam('types');
$types = $types ? explode(',', $types) : Array ();
return in_array('search', $types) ? Array () : parent::_getDefaultSorting($event);
}
// ============= for cms page processing =======================
/**
* Returns default design template
*
* @return string
*/
function _getDefaultDesign()
{
$default_design = trim($this->Application->ConfigValue('cms_DefaultDesign'), '/');
if (!$default_design) {
// theme-based alias for default design
return '#default_design#';
}
if (strpos($default_design, '#') === false) {
// real template, not alias, so prefix with "/"
return '/' . $default_design;
}
// alias
return $default_design;
}
/**
* Returns default design based on given virtual template (used from kApplication::Run)
*
* @param string $t
* @return string
* @access public
*/
public function GetDesignTemplate($t = null)
{
if ( !isset($t) ) {
$t = $this->Application->GetVar('t');
}
$page = $this->Application->recallObject($this->Prefix . '.-virtual', null, Array ('page' => $t));
/* @var $page CategoriesItem */
if ( $page->isLoaded() ) {
$real_t = $page->GetDBField('CachedTemplate');
$this->Application->SetVar('m_cat_id', $page->GetDBField('CategoryId'));
if ( $page->GetDBField('FormId') ) {
$this->Application->SetVar('form_id', $page->GetDBField('FormId'));
}
}
else {
$not_found = $this->Application->ConfigValue('ErrorTemplate');
$real_t = $not_found ? $not_found : 'error_notfound';
$themes_helper = $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$theme_id = $this->Application->GetVar('m_theme');
$category_id = $themes_helper->getPageByTemplate($real_t, $theme_id);
$this->Application->SetVar('m_cat_id', $category_id);
header('HTTP/1.0 404 Not Found');
}
// replace alias in form #alias_name# to actual template used in this theme
if ( $this->Application->isAdmin ) {
$themes_helper = $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
// only, used when in "Design Mode"
$this->Application->SetVar('theme.current_id', $themes_helper->getCurrentThemeId());
}
$theme = $this->Application->recallObject('theme.current');
/* @var $theme kDBItem */
$template = $theme->GetField('TemplateAliases', $real_t);
if ( $template ) {
return $template;
}
return $real_t;
}
/**
* Sets category id based on found template (used from kApplication::Run)
*
* @deprecated
*/
/*function SetCatByTemplate()
{
$t = $this->Application->GetVar('t');
$page = $this->Application->recallObject($this->Prefix . '.-virtual');
if ( $page->isLoaded() ) {
$this->Application->SetVar('m_cat_id', $page->GetDBField('CategoryId'));
}
}*/
/**
* Prepares template paths
*
* @param kEvent $event
*/
function _beforeItemChange($event)
{
$object = $event->getObject();
/* @var $object CategoriesItem */
$object->checkFilename();
$object->generateFilename();
$now = time();
if ( !$this->Application->isDebugMode() && strpos($event->Special, 'rebuild') === false ) {
$object->SetDBField('Type', $object->GetOriginalField('Type'));
$object->SetDBField('Protected', $object->GetOriginalField('Protected'));
if ( $object->GetDBField('Protected') ) {
// some fields are read-only for protected pages, when debug mode is off
$object->SetDBField('AutomaticFilename', $object->GetOriginalField('AutomaticFilename'));
$object->SetDBField('Filename', $object->GetOriginalField('Filename'));
$object->SetDBField('Status', $object->GetOriginalField('Status'));
}
}
$is_admin = $this->Application->isAdminUser;
if ( (!$object->IsTempTable() && !$is_admin) || ($is_admin && !$object->GetDBField('CreatedById')) ) {
$object->SetDBField('CreatedById', $this->Application->RecallVar('user_id'));
}
if ($object->GetChangedFields()) {
$object->SetDBField('Modified_date', $now);
$object->SetDBField('Modified_time', $now);
}
$object->setRequired('PageCacheKey', $object->GetDBField('OverridePageCacheKey'));
$object->SetDBField('Template', $this->_stripTemplateExtension( $object->GetDBField('Template') ));
if ($object->GetDBField('Type') == PAGE_TYPE_TEMPLATE) {
if (!$this->_templateFound($object->GetDBField('Template'), $object->GetDBField('ThemeId'))) {
$object->SetError('Template', 'template_file_missing', 'la_error_TemplateFileMissing');
}
}
$this->_saveTitleField($object, 'Title');
$this->_saveTitleField($object, 'MenuTitle');
$root_category = $this->Application->getBaseCategory();
if ( file_exists(FULL_PATH . '/themes') && ($object->GetDBField('ParentId') == $root_category) && ($object->GetDBField('Template') == CATEGORY_TEMPLATE_INHERIT) ) {
// there are themes + creating top level category
$object->SetError('Template', 'no_inherit');
}
if ( !$this->Application->isAdminUser && $object->isVirtualField('cust_RssSource') ) {
// only administrator can set/change "cust_RssSource" field
if ($object->GetDBField('cust_RssSource') != $object->GetOriginalField('cust_RssSource')) {
$object->SetError('cust_RssSource', 'not_allowed', 'la_error_OperationNotAllowed');
}
}
if ( !$object->GetDBField('DirectLinkAuthKey') ) {
$key_parts = Array (
$object->GetID(),
$object->GetDBField('ParentId'),
$object->GetField('Name'),
'b38'
);
$object->SetDBField('DirectLinkAuthKey', substr( md5( implode(':', $key_parts) ), 0, 20 ));
}
}
/**
* Sets page name to requested field in case when:
* 1. page was auto created (through theme file rebuild)
* 2. requested field is empty
*
* @param kDBItem $object
* @param string $field
* @author Alex
*/
function _saveTitleField(&$object, $field)
{
$value = $object->GetField($field, 'no_default'); // current value of target field
$ml_formatter = $this->Application->recallObject('kMultiLanguage');
/* @var $ml_formatter kMultiLanguage */
$src_field = $ml_formatter->LangFieldName('Name');
$dst_field = $ml_formatter->LangFieldName($field);
$dst_field_not_changed = $object->GetOriginalField($dst_field) == $value;
if ($value == '' || preg_match('/^_Auto: (.*)/', $value) || (($object->GetOriginalField($src_field) == $value) && $dst_field_not_changed)) {
// target field is empty OR target field value starts with "_Auto: " OR (source field value
// before change was equals to current target field value AND target field value wasn't changed)
$object->SetField($dst_field, $object->GetField($src_field));
}
}
/**
* Don't allow to delete system pages, when not in debug mode
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemDelete(kEvent $event)
{
parent::OnBeforeItemDelete($event);
$object = $event->getObject();
/* @var $object kDBItem */
if ( $object->GetDBField('Protected') && !$this->Application->isDebugMode(false) ) {
$event->status = kEvent::erFAIL;
}
}
/**
* Creates category based on given TPL file
*
* @param CategoriesItem $object
* @param string $template
* @param int $theme_id
* @param int $system_mode
* @param array $template_info
* @return bool
*/
function _prepareAutoPage(&$object, $template, $theme_id = null, $system_mode = SMS_MODE_AUTO, $template_info = Array ())
{
$template = $this->_stripTemplateExtension($template);
if ($system_mode == SMS_MODE_AUTO) {
$page_type = $this->_templateFound($template, $theme_id) ? PAGE_TYPE_TEMPLATE : PAGE_TYPE_VIRTUAL;
}
else {
$page_type = $system_mode == SMS_MODE_FORCE ? PAGE_TYPE_TEMPLATE : PAGE_TYPE_VIRTUAL;
}
if (($page_type == PAGE_TYPE_TEMPLATE) && ($template_info === false)) {
// do not auto-create system pages, when browsing through site
return false;
}
if (!isset($theme_id)) {
$theme_id = $this->_getCurrentThemeId();
}
$root_category = $this->Application->getBaseCategory();
$page_category = $this->Application->GetVar('m_cat_id');
if (!$page_category) {
$page_category = $root_category;
$this->Application->SetVar('m_cat_id', $page_category);
}
if (($page_type == PAGE_TYPE_VIRTUAL) && (strpos($template, '/') !== false)) {
// virtual page, but have "/" in template path -> create it's path
$category_path = explode('/', $template);
$template = array_pop($category_path);
$page_category = $this->_getParentCategoryFromPath($category_path, $root_category, $theme_id);
}
$page_name = ($page_type == PAGE_TYPE_TEMPLATE) ? '_Auto: ' . $template : $template;
$page_description = '';
if ($page_type == PAGE_TYPE_TEMPLATE) {
$design_template = strtolower($template); // leading "/" not added !
if ($template_info) {
if (array_key_exists('name', $template_info) && $template_info['name']) {
$page_name = $template_info['name'];
}
if (array_key_exists('desc', $template_info) && $template_info['desc']) {
$page_description = $template_info['desc'];
}
if (array_key_exists('section', $template_info) && $template_info['section']) {
// this will override any global "m_cat_id"
$page_category = $this->_getParentCategoryFromPath(explode('||', $template_info['section']), $root_category, $theme_id);
}
}
}
else {
$design_template = $this->_getDefaultDesign(); // leading "/" added !
}
$object->Clear();
$object->SetDBField('ParentId', $page_category);
$object->SetDBField('Type', $page_type);
$object->SetDBField('Protected', 1); // $page_type == PAGE_TYPE_TEMPLATE
$object->SetDBField('IsMenu', 0);
$object->SetDBField('ThemeId', $theme_id);
// put all templates to then end of list (in their category)
$min_priority = $this->_getNextPriority($page_category, $object->TableName);
$object->SetDBField('Priority', $min_priority);
$object->SetDBField('Template', $design_template);
$object->SetDBField('CachedTemplate', $design_template);
$primary_language = $this->Application->GetDefaultLanguageId();
$current_language = $this->Application->GetVar('m_lang');
$object->SetDBField('l' . $primary_language . '_Name', $page_name);
$object->SetDBField('l' . $current_language . '_Name', $page_name);
$object->SetDBField('l' . $primary_language . '_Description', $page_description);
$object->SetDBField('l' . $current_language . '_Description', $page_description);
return $object->Create();
}
function _getParentCategoryFromPath($category_path, $base_category, $theme_id = null)
{
static $category_ids = Array ();
if (!$category_path) {
return $base_category;
}
if (array_key_exists(implode('||', $category_path), $category_ids)) {
return $category_ids[ implode('||', $category_path) ];
}
$backup_category_id = $this->Application->GetVar('m_cat_id');
$object = $this->Application->recallObject($this->Prefix . '.rebuild-path', null, Array ('skip_autoload' => true));
/* @var $object CategoriesItem */
$parent_id = $base_category;
$filenames_helper = $this->Application->recallObject('FilenamesHelper');
/* @var $filenames_helper kFilenamesHelper */
$safe_category_path = array_map(Array (&$filenames_helper, 'replaceSequences'), $category_path);
foreach ($category_path as $category_order => $category_name) {
$this->Application->SetVar('m_cat_id', $parent_id);
// get virtual category first, when possible
$sql = 'SELECT ' . $object->IDField . '
FROM ' . $object->TableName . '
WHERE
(
Filename = ' . $this->Conn->qstr($safe_category_path[$category_order]) . ' OR
Filename = ' . $this->Conn->qstr( $filenames_helper->replaceSequences('_Auto: ' . $category_name) ) . '
) AND
(ParentId = ' . $parent_id . ') AND
(ThemeId = 0 OR ThemeId = ' . $theme_id . ')
ORDER BY ThemeId ASC';
$parent_id = $this->Conn->GetOne($sql);
if ($parent_id === false) {
// page not found
$template = implode('/', array_slice($safe_category_path, 0, $category_order + 1));
// don't process system templates in sub-categories
$system = $this->_templateFound($template, $theme_id) && (strpos($template, '/') === false);
if (!$this->_prepareAutoPage($object, $category_name, $theme_id, $system ? SMS_MODE_FORCE : false)) {
// page was not created
break;
}
$parent_id = $object->GetID();
}
}
$this->Application->SetVar('m_cat_id', $backup_category_id);
$category_ids[ implode('||', $category_path) ] = $parent_id;
return $parent_id;
}
/**
* Returns theme name by it's id. Used in structure page creation.
*
* @param int $theme_id
* @return string
*/
function _getThemeName($theme_id)
{
static $themes = null;
if (!isset($themes)) {
$theme_config = $this->Application->getUnitConfig('theme');
$id_field = $theme_config->getIDField();
$table_name = $theme_config->getTableName();
$sql = 'SELECT Name, ' . $id_field . '
FROM ' . $table_name . '
WHERE Enabled = 1';
$themes = $this->Conn->GetCol($sql, $id_field);
}
return array_key_exists($theme_id, $themes) ? $themes[$theme_id] : false;
}
/**
* Resets SMS-menu cache
*
* @param kEvent $event
*/
function OnResetCMSMenuCache($event)
{
if ($this->Application->GetVar('ajax') == 'yes') {
$event->status = kEvent::erSTOP;
}
$this->_resetMenuCache();
$event->SetRedirectParam('action_completed', 1);
}
/**
* Performs reset of category-related caches (menu, structure dropdown, template mapping)
*
* @return void
* @access protected
*/
protected function _resetMenuCache()
{
// reset cms menu cache (all variables are automatically rebuild, when missing)
if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) {
$this->Application->rebuildCache('master:cms_menu', kCache::REBUILD_LATER, CacheSettings::$cmsMenuRebuildTime);
$this->Application->rebuildCache('master:StructureTree', kCache::REBUILD_LATER, CacheSettings::$structureTreeRebuildTime);
$this->Application->rebuildCache('master:template_mapping', kCache::REBUILD_LATER, CacheSettings::$templateMappingRebuildTime);
}
else {
$this->Application->rebuildDBCache('cms_menu', kCache::REBUILD_LATER, CacheSettings::$cmsMenuRebuildTime);
$this->Application->rebuildDBCache('StructureTree', kCache::REBUILD_LATER, CacheSettings::$structureTreeRebuildTime);
$this->Application->rebuildDBCache('template_mapping', kCache::REBUILD_LATER, CacheSettings::$templateMappingRebuildTime);
}
}
/**
* Updates structure config
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterConfigRead(kEvent $event)
{
parent::OnAfterConfigRead($event);
if (defined('IS_INSTALL') && IS_INSTALL) {
// skip any processing, because Categories table doesn't exists until install is finished
$this->addViewPermissionJoin($event);
return ;
}
$site_config_helper = $this->Application->recallObject('SiteConfigHelper');
/* @var $site_config_helper SiteConfigHelper */
$settings = $site_config_helper->getSettings();
$root_category = $this->Application->getBaseCategory();
$config = $event->getUnitConfig();
// set root category
$config->addSectionAdjustments(Array (
'in-portal:browse' => Array (
'url' => Array ('m_cat_id' => $root_category),
'late_load' => Array ('m_cat_id' => $root_category),
'onclick' => 'checkCatalog(' . $root_category . ')',
),
'in-portal:browse_site' => Array (
'url' => Array ('editing_mode' => $settings['default_editing_mode']),
)
));
// prepare structure dropdown
$category_helper = $this->Application->recallObject('CategoryHelper');
/* @var $category_helper CategoryHelper */
$fields = $config->getFields();
$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;
// adds "Inherit From Parent" option to "Template" field
$fields['Template']['options'] = Array (CATEGORY_TEMPLATE_INHERIT => $this->Application->Phrase('la_opt_InheritFromParent'));
$config->setFields($fields);
if ($this->Application->isAdmin) {
// don't sort by Front-End sorting fields
$config_mapping = $config->getConfigMapping();
$remove_keys = Array ('DefaultSorting1Field', 'DefaultSorting2Field', 'DefaultSorting1Dir', 'DefaultSorting2Dir');
foreach ($remove_keys as $remove_key) {
unset($config_mapping[$remove_key]);
}
$config->setConfigMapping($config_mapping);
}
else {
// sort by parent path on Front-End only
$config->setListSortingsBySpecial('', Array (
'ForcedSorting' => Array ('CurrentSort' => 'asc'),
));
}
$this->addViewPermissionJoin($event);
// add grids for advanced view (with primary category column)
foreach (Array ('Default', 'Radio') as $process_grid) {
$grid_data = $config->getGridByName($process_grid);
$grid_data['Fields']['CachedNavbar'] = Array ('title' => 'la_col_Path', 'data_block' => 'grid_parent_category_td', 'filter_block' => 'grid_like_filter');
$config->addGrids($grid_data, $process_grid . 'ShowAll');
}
}
/**
* Adds permission table table JOIN clause only, when advanced catalog view permissions enabled.
*
* @param kEvent $event Event.
*
* @return self
* @access protected
*/
protected function addViewPermissionJoin(kEvent $event)
{
if ( $this->Application->ConfigValue('CheckViewPermissionsInCatalog') ) {
$join_clause = 'LEFT JOIN ' . TABLE_PREFIX . 'CategoryPermissionsCache perm ON perm.CategoryId = %1$s.CategoryId';
}
else {
$join_clause = '';
}
$config = $event->getUnitConfig();
foreach ( $config->getListSQLSpecials() as $special ) {
$list_sql = str_replace('{PERM_JOIN}', $join_clause, $config->getListSQLsBySpecial($special));
$config->setListSQLsBySpecial($special, $list_sql);
}
return $this;
}
/**
* Returns folders, that can contain design templates
*
* @return array
* @access protected
*/
protected function getDesignFolders()
{
$ret = Array ('tf.FilePath = "/designs"', 'tf.FilePath = "/platform/designs"');
foreach ($this->Application->ModuleInfo as $module_info) {
$ret[] = 'tf.FilePath = "/' . $module_info['TemplatePath'] . 'designs"';
}
return array_unique($ret);
}
/**
* Removes this item and it's children (recursive) from structure dropdown
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterItemLoad(kEvent $event)
{
parent::OnAfterItemLoad($event);
if ( !$this->Application->isAdmin ) {
// calculate priorities dropdown only for admin
return;
}
$object = $event->getObject();
/* @var $object kDBItem */
// remove this category & it's children from dropdown
$sql = 'SELECT ' . $object->IDField . '
FROM ' . $event->getUnitConfig()->getTableName() . '
WHERE ParentPath LIKE "' . $object->GetDBField('ParentPath') . '%"';
$remove_categories = $this->Conn->GetCol($sql);
$options = $object->GetFieldOption('ParentId', 'options');
foreach ($remove_categories as $remove_category) {
unset($options[$remove_category]);
}
$object->SetFieldOption('ParentId', 'options', $options);
}
/**
* Occurs after creating item
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterItemCreate(kEvent $event)
{
parent::OnAfterItemCreate($event);
$object = $event->getObject();
/* @var $object CategoriesItem */
// need to update path after category is created, so category is included in that path
$fields_hash = $object->buildParentBasedFields();
$this->Conn->doUpdate($fields_hash, $object->TableName, $object->IDField . ' = ' . $object->GetID());
$object->SetDBFieldsFromHash($fields_hash);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnAfterRebuildThemes($event)
{
$sql = 'SELECT t.ThemeId, CONCAT( tf.FilePath, \'/\', tf.FileName ) AS Path, tf.FileMetaInfo
FROM ' . TABLE_PREFIX . 'ThemeFiles AS tf
LEFT JOIN ' . TABLE_PREFIX . 'Themes AS t ON t.ThemeId = tf.ThemeId
WHERE t.Enabled = 1 AND tf.FileType = 1
AND (
SELECT COUNT(CategoryId)
FROM ' . TABLE_PREFIX . 'Categories c
WHERE CONCAT(\'/\', c.Template, \'.tpl\') = CONCAT( tf.FilePath, \'/\', tf.FileName ) AND (c.ThemeId = t.ThemeId)
) = 0 ';
$files = $this->Conn->Query($sql, 'Path');
if ( !$files ) {
// all possible pages are already created
return;
}
kUtil::setResourceLimit();
$dummy = $this->Application->recallObject($event->Prefix . '.rebuild', NULL, Array ('skip_autoload' => true));
/* @var $dummy CategoriesItem */
$error_count = 0;
foreach ($files as $a_file => $file_info) {
$status = $this->_prepareAutoPage($dummy, $a_file, $file_info['ThemeId'], SMS_MODE_FORCE, unserialize($file_info['FileMetaInfo'])); // create system page
if ( !$status ) {
$error_count++;
}
}
if ( $this->Application->ConfigValue('CategoryPermissionRebuildMode') == CategoryPermissionRebuild::SILENT ) {
$updater = $this->Application->makeClass('kPermCacheUpdater');
/* @var $updater kPermCacheUpdater */
$updater->OneStepRun();
}
$this->_resetMenuCache();
if ( $error_count ) {
// allow user to review error after structure page creation
$event->MasterEvent->redirect = false;
}
}
/**
* Processes OnMassMoveUp, OnMassMoveDown events
*
* @param kEvent $event
*/
function OnChangePriority($event)
{
$this->Application->SetVar('priority_prefix', $event->getPrefixSpecial());
$event->CallSubEvent('priority:' . $event->Name);
$this->Application->StoreVar('RefreshStructureTree', 1);
$this->_resetMenuCache();
}
/**
* Completely recalculates priorities in current category
*
* @param kEvent $event
*/
function OnRecalculatePriorities($event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return;
}
$this->Application->SetVar('priority_prefix', $event->getPrefixSpecial());
$event->CallSubEvent('priority:' . $event->Name);
$this->_resetMenuCache();
}
/**
* Update Preview Block for FCKEditor
*
* @param kEvent $event
*/
function OnUpdatePreviewBlock($event)
{
$event->status = kEvent::erSTOP;
$string = $this->Application->unescapeRequestVariable($this->Application->GetVar('preview_content'));
$category_helper = $this->Application->recallObject('CategoryHelper');
/* @var $category_helper CategoryHelper */
$string = $category_helper->replacePageIds($string);
$this->Application->StoreVar('_editor_preview_content_', $string);
}
/**
* Makes simple search for categories
* based on keywords string
*
* @param kEvent $event
*/
function OnSimpleSearch($event)
{
$event->redirect = false;
$search_table = TABLE_PREFIX.'ses_'.$this->Application->GetSID().'_'.TABLE_PREFIX.'Search';
$keywords = $this->Application->unescapeRequestVariable(trim($this->Application->GetVar('keywords')));
- $query_object = $this->Application->recallObject('HTTPQuery');
+ $query_object = $this->Application->recallObject('kHTTPQuery');
/* @var $query_object kHTTPQuery */
$sql = 'SHOW TABLES LIKE "'.$search_table.'"';
if ( !isset($query_object->Get['keywords']) && !isset($query_object->Post['keywords']) && $this->Conn->Query($sql) ) {
// used when navigating by pages or changing sorting in search results
return;
}
if(!$keywords || strlen($keywords) < $this->Application->ConfigValue('Search_MinKeyword_Length'))
{
$this->Conn->Query('DROP TABLE IF EXISTS '.$search_table);
$this->Application->SetVar('keywords_too_short', 1);
return; // if no or too short keyword entered, doing nothing
}
$this->Application->StoreVar('keywords', $keywords);
$this->saveToSearchLog($keywords, 0); // 0 - simple search, 1 - advanced search
$keywords = strtr($keywords, Array('%' => '\\%', '_' => '\\_'));
$event->setPseudoClass('_List');
$object = $event->getObject();
/* @var $object kDBList */
$config = $event->getUnitConfig();
$this->Application->SetVar($event->getPrefixSpecial().'_Page', 1);
$lang = $this->Application->GetVar('m_lang');
$items_table = $config->getTableName();
$module_name = 'In-Portal';
$sql = 'SELECT *
FROM ' . $this->Application->getUnitConfig('confs')->getTableName() . '
WHERE ModuleName = ' . $this->Conn->qstr($module_name) . ' AND SimpleSearch = 1';
$search_config = $this->Conn->Query($sql, 'FieldName');
$field_list = array_keys($search_config);
$join_clauses = Array();
// field processing
$weight_sum = 0;
$alias_counter = 0;
$custom_fields = $config->getCustomFields();
if ($custom_fields) {
$custom_table = $this->Application->getUnitConfig($event->Prefix . '-cdata')->getTableName();
$join_clauses[] = ' LEFT JOIN '.$custom_table.' custom_data ON '.$items_table.'.ResourceId = custom_data.ResourceId';
}
// what field in search config becomes what field in sql (key - new field, value - old field (from searchconfig table))
$search_config_map = Array();
foreach ($field_list as $key => $field) {
$local_table = TABLE_PREFIX.$search_config[$field]['TableName'];
$weight_sum += $search_config[$field]['Priority']; // counting weight sum; used when making relevance clause
// processing multilingual fields
if ( !$search_config[$field]['CustomFieldId'] && $object->GetFieldOption($field, 'formatter') == 'kMultiLanguage' ) {
$field_list[$key.'_primary'] = 'l'.$this->Application->GetDefaultLanguageId().'_'.$field;
$field_list[$key] = 'l'.$lang.'_'.$field;
if (!isset($search_config[$field]['ForeignField'])) {
$field_list[$key.'_primary'] = $local_table.'.'.$field_list[$key.'_primary'];
$search_config_map[ $field_list[$key.'_primary'] ] = $field;
}
}
// processing fields from other tables
$foreign_field = $search_config[$field]['ForeignField'];
if ( $foreign_field ) {
$exploded = explode(':', $foreign_field, 2);
if ($exploded[0] == 'CALC') {
// ignoring having type clauses in simple search
unset($field_list[$key]);
continue;
}
else {
$multi_lingual = false;
if ($exploded[0] == 'MULTI') {
$multi_lingual = true;
$foreign_field = $exploded[1];
}
$exploded = explode('.', $foreign_field); // format: table.field_name
$foreign_table = TABLE_PREFIX.$exploded[0];
$alias_counter++;
$alias = 't'.$alias_counter;
if ($multi_lingual) {
$field_list[$key] = $alias.'.'.'l'.$lang.'_'.$exploded[1];
$field_list[$key.'_primary'] = 'l'.$this->Application->GetDefaultLanguageId().'_'.$field;
$search_config_map[ $field_list[$key] ] = $field;
$search_config_map[ $field_list[$key.'_primary'] ] = $field;
}
else {
$field_list[$key] = $alias.'.'.$exploded[1];
$search_config_map[ $field_list[$key] ] = $field;
}
$join_clause = str_replace('{ForeignTable}', $alias, $search_config[$field]['JoinClause']);
$join_clause = str_replace('{LocalTable}', $items_table, $join_clause);
$join_clauses[] = ' LEFT JOIN '.$foreign_table.' '.$alias.'
ON '.$join_clause;
}
}
else {
// processing fields from local table
if ($search_config[$field]['CustomFieldId']) {
$local_table = 'custom_data';
// search by custom field value on current language
$custom_field_id = array_search($field_list[$key], $custom_fields);
$field_list[$key] = 'l'.$lang.'_cust_'.$custom_field_id;
// search by custom field value on primary language
$field_list[$key.'_primary'] = $local_table.'.l'.$this->Application->GetDefaultLanguageId().'_cust_'.$custom_field_id;
$search_config_map[ $field_list[$key.'_primary'] ] = $field;
}
$field_list[$key] = $local_table.'.'.$field_list[$key];
$search_config_map[ $field_list[$key] ] = $field;
}
}
// keyword string processing
$search_helper = $this->Application->recallObject('SearchHelper');
/* @var $search_helper kSearchHelper */
$where_clause = Array ();
foreach ($field_list as $field) {
if (preg_match('/^' . preg_quote($items_table, '/') . '\.(.*)/', $field, $regs)) {
// local real field
$filter_data = $search_helper->getSearchClause($object, $regs[1], $keywords, false);
if ($filter_data) {
$where_clause[] = $filter_data['value'];
}
}
elseif (preg_match('/^custom_data\.(.*)/', $field, $regs)) {
$custom_field_name = 'cust_' . $search_config_map[$field];
$filter_data = $search_helper->getSearchClause($object, $custom_field_name, $keywords, false);
if ($filter_data) {
$where_clause[] = str_replace('`' . $custom_field_name . '`', $field, $filter_data['value']);
}
}
else {
$where_clause[] = $search_helper->buildWhereClause($keywords, Array ($field));
}
}
$where_clause = '((' . implode(') OR (', $where_clause) . '))'; // 2 braces for next clauses, see below!
$where_clause = $where_clause . ' AND (' . $items_table . '.Status = ' . STATUS_ACTIVE . ')';
if ($event->MasterEvent && $event->MasterEvent->Name == 'OnListBuild') {
$sub_search_ids = $event->MasterEvent->getEventParam('ResultIds');
if ( $sub_search_ids !== false ) {
if ( $sub_search_ids ) {
$where_clause .= 'AND (' . $items_table . '.ResourceId IN (' . implode(',', $sub_search_ids) . '))';
}
else {
$where_clause .= 'AND FALSE';
}
}
}
// exclude template based sections from search results (ie. registration)
if ( $this->Application->ConfigValue('ExcludeTemplateSectionsFromSearch') ) {
$where_clause .= ' AND ' . $items_table . '.ThemeId = 0';
}
// making relevance clause
$positive_words = $search_helper->getPositiveKeywords($keywords);
$this->Application->StoreVar('highlight_keywords', serialize($positive_words));
$revelance_parts = Array();
reset($search_config);
foreach ($positive_words as $keyword_index => $positive_word) {
$positive_word = $search_helper->transformWildcards($positive_word);
$positive_words[$keyword_index] = $this->Conn->escape($positive_word);
}
foreach ($field_list as $field) {
if (!array_key_exists($field, $search_config_map)) {
$map_key = $search_config_map[$items_table . '.' . $field];
}
else {
$map_key = $search_config_map[$field];
}
$config_elem = $search_config[ $map_key ];
$weight = $config_elem['Priority'];
// search by whole words only ([[:<:]] - word boundary)
/*$revelance_parts[] = 'IF('.$field.' REGEXP "[[:<:]]('.implode(' ', $positive_words).')[[:>:]]", '.$weight.', 0)';
foreach ($positive_words as $keyword) {
$revelance_parts[] = 'IF('.$field.' REGEXP "[[:<:]]('.$keyword.')[[:>:]]", '.$weight.', 0)';
}*/
// search by partial word matches too
$revelance_parts[] = 'IF('.$field.' LIKE "%'.implode(' ', $positive_words).'%", '.$weight_sum.', 0)';
foreach ($positive_words as $keyword) {
$revelance_parts[] = 'IF('.$field.' LIKE "%'.$keyword.'%", '.$weight.', 0)';
}
}
$revelance_parts = array_unique($revelance_parts);
$conf_postfix = $config->getSearchConfigPostfix();
$rel_keywords = $this->Application->ConfigValue('SearchRel_Keyword_'.$conf_postfix) / 100;
$rel_pop = $this->Application->ConfigValue('SearchRel_Pop_'.$conf_postfix) / 100;
$rel_rating = $this->Application->ConfigValue('SearchRel_Rating_'.$conf_postfix) / 100;
$relevance_clause = '('.implode(' + ', $revelance_parts).') / '.$weight_sum.' * '.$rel_keywords;
if ($rel_pop && $object->isField('Hits')) {
$relevance_clause .= ' + (Hits + 1) / (MAX(Hits) + 1) * '.$rel_pop;
}
if ($rel_rating && $object->isField('CachedRating')) {
$relevance_clause .= ' + (CachedRating + 1) / (MAX(CachedRating) + 1) * '.$rel_rating;
}
// building final search query
if (!$this->Application->GetVar('do_not_drop_search_table')) {
$this->Conn->Query('DROP TABLE IF EXISTS '.$search_table); // erase old search table if clean k4 event
$this->Application->SetVar('do_not_drop_search_table', true);
}
$search_table_exists = $this->Conn->Query('SHOW TABLES LIKE "'.$search_table.'"');
if ($search_table_exists) {
$select_intro = 'INSERT INTO '.$search_table.' (Relevance, ItemId, ResourceId, ItemType, EdPick) ';
}
else {
$select_intro = 'CREATE TABLE '.$search_table.' AS ';
}
$edpick_clause = $config->getFieldByName('EditorsPick') ? $items_table.'.EditorsPick' : '0';
$sql = $select_intro.' SELECT '.$relevance_clause.' AS Relevance,
'.$items_table.'.'.$config->getIDField().' AS ItemId,
'.$items_table.'.ResourceId,
'.$config->getItemType().' AS ItemType,
'.$edpick_clause.' AS EdPick
FROM '.$object->TableName.'
'.implode(' ', $join_clauses).'
WHERE '.$where_clause.'
GROUP BY '.$items_table.'.'.$config->getIDField().' ORDER BY Relevance DESC';
$this->Conn->Query($sql);
if ( !$search_table_exists ) {
$sql = 'ALTER TABLE ' . $search_table . '
ADD INDEX (ResourceId),
ADD INDEX (Relevance)';
$this->Conn->Query($sql);
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnSubSearch($event)
{
// keep search results from other items after doing a sub-search on current item type
$this->Application->SetVar('do_not_drop_search_table', true);
$ids = Array ();
$search_table = TABLE_PREFIX . 'ses_' . $this->Application->GetSID() . '_' . TABLE_PREFIX . 'Search';
$sql = 'SHOW TABLES LIKE "' . $search_table . '"';
if ( $this->Conn->Query($sql) ) {
$item_type = $event->getUnitConfig()->getItemType();
// 1. get ids to be used as search bounds
$sql = 'SELECT DISTINCT ResourceId
FROM ' . $search_table . '
WHERE ItemType = ' . $item_type;
$ids = $this->Conn->GetCol($sql);
// 2. delete previously found ids
$sql = 'DELETE FROM ' . $search_table . '
WHERE ItemType = ' . $item_type;
$this->Conn->Query($sql);
}
$event->setEventParam('ResultIds', $ids);
$event->CallSubEvent('OnSimpleSearch');
}
/**
* Make record to search log
*
* @param string $keywords
* @param int $search_type 0 - simple search, 1 - advanced search
*/
function saveToSearchLog($keywords, $search_type = 0)
{
// don't save keywords for each module separately, just one time
// static variable can't help here, because each module uses it's own class instance !
if (!$this->Application->GetVar('search_logged')) {
$sql = 'UPDATE '.TABLE_PREFIX.'SearchLogs
SET Indices = Indices + 1
WHERE Keyword = '.$this->Conn->qstr($keywords).' AND SearchType = '.$search_type; // 0 - simple search, 1 - advanced search
$this->Conn->Query($sql);
if ($this->Conn->getAffectedRows() == 0) {
$fields_hash = Array('Keyword' => $keywords, 'Indices' => 1, 'SearchType' => $search_type);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX.'SearchLogs');
}
$this->Application->SetVar('search_logged', 1);
}
}
/**
* Load item if id is available
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function LoadItem(kEvent $event)
{
if ( !$this->_isVirtual($event) ) {
parent::LoadItem($event);
return;
}
$object = $event->getObject();
/* @var $object kDBItem */
$id = $this->getPassedID($event);
if ( $object->isLoaded() && !is_array($id) && ($object->GetID() == $id) ) {
// object is already loaded by same id
return;
}
if ( $object->Load($id, null, true) ) {
$actions = $this->Application->recallObject('kActions');
/* @var $actions Params */
$actions->Set($event->getPrefixSpecial() . '_id', $object->GetID());
}
else {
$object->setID($id);
}
}
/**
* Returns constrain for priority calculations
*
* @param kEvent $event
* @return void
* @see PriorityEventHandler
* @access protected
*/
protected function OnGetConstrainInfo(kEvent $event)
{
$constrain = ''; // for OnSave
$event_name = $event->getEventParam('original_event');
$actual_event_name = $event->getEventParam('actual_event');
if ( $actual_event_name == 'OnSavePriorityChanges' || $event_name == 'OnAfterItemLoad' || $event_name == 'OnAfterItemDelete' ) {
$object = $event->getObject();
/* @var $object kDBItem */
$constrain = 'ParentId = ' . $object->GetDBField('ParentId');
}
elseif ( $actual_event_name == 'OnPreparePriorities' ) {
$constrain = 'ParentId = ' . $this->Application->GetVar('m_cat_id');
}
elseif ( $event_name == 'OnSave' ) {
$constrain = '';
}
else {
$constrain = 'ParentId = ' . $this->Application->GetVar('m_cat_id');
}
$event->setEventParam('constrain_info', Array ($constrain, ''));
}
/**
* Parses category part of url, build main part of url
*
* @param int $rewrite_mode Mode in what rewrite listener was called. Possbile two modes: REWRITE_MODE_BUILD, REWRITE_MODE_PARSE.
* @param string $prefix Prefix, that listener uses for system integration
* @param Array $params Params, that are used for url building or created during url parsing.
* @param Array $url_parts Url parts to parse (only for parsing).
* @param bool $keep_events Keep event names in resulting url (only for building).
* @return bool|string|Array Return true to continue to next listener; return false (when building) not to rewrite given prefix; return false (when parsing) to stop processing at this listener.
*/
public function CategoryRewriteListener($rewrite_mode = REWRITE_MODE_BUILD, $prefix, &$params, &$url_parts, $keep_events = false)
{
if ($rewrite_mode == REWRITE_MODE_BUILD) {
return $this->_buildMainUrl($prefix, $params, $keep_events);
}
if ( $this->_parseFriendlyUrl($url_parts, $params) ) {
// friendly urls work like exact match only!
return false;
}
$this->_parseCategory($url_parts, $params);
return true;
}
/**
* Build main part of every url
*
* @param string $prefix_special
* @param Array $params
* @param bool $keep_events
* @return string
*/
protected function _buildMainUrl($prefix_special, &$params, $keep_events)
{
$ret = '';
list ($prefix) = explode('.', $prefix_special);
$rewrite_processor = $this->Application->recallObject('kRewriteUrlProcessor');
/* @var $rewrite_processor kRewriteUrlProcessor */
$processed_params = $rewrite_processor->getProcessedParams($prefix_special, $params, $keep_events);
if ($processed_params === false) {
return '';
}
// add language
if ($processed_params['m_lang'] && ($processed_params['m_lang'] != $rewrite_processor->primaryLanguageId)) {
$language_name = $this->Application->getCache('language_names[%LangIDSerial:' . $processed_params['m_lang'] . '%]');
if ($language_name === false) {
$sql = 'SELECT PackName
FROM ' . TABLE_PREFIX . 'Languages
WHERE LanguageId = ' . $processed_params['m_lang'];
$language_name = $this->Conn->GetOne($sql);
$this->Application->setCache('language_names[%LangIDSerial:' . $processed_params['m_lang'] . '%]', $language_name);
}
$ret .= $language_name . '/';
}
// add theme
if ($processed_params['m_theme'] && ($processed_params['m_theme'] != $rewrite_processor->primaryThemeId)) {
$theme_name = $this->Application->getCache('theme_names[%ThemeIDSerial:' . $processed_params['m_theme'] . '%]');
if ($theme_name === false) {
$sql = 'SELECT Name
FROM ' . TABLE_PREFIX . 'Themes
WHERE ThemeId = ' . $processed_params['m_theme'];
$theme_name = $this->Conn->GetOne($sql);
$this->Application->setCache('theme_names[%ThemeIDSerial:' . $processed_params['m_theme'] . '%]', $theme_name);
}
$ret .= $theme_name . '/';
}
// inject custom url parts made by other rewrite listeners just after language/theme url parts
if ($params['inject_parts']) {
$ret .= implode('/', $params['inject_parts']) . '/';
}
// add category
if ($processed_params['m_cat_id'] > 0 && $params['pass_category']) {
$category_filename = $this->Application->getCategoryCache($processed_params['m_cat_id'], 'filenames');
preg_match('/^Content\/(.*)/i', $category_filename, $regs);
if ($regs) {
$template = array_key_exists('t', $params) ? $params['t'] : false;
if (strtolower($regs[1]) == strtolower($template)) {
// we could have category path like "Content/<template_path>" in this case remove template
$params['pass_template'] = false;
}
$ret .= $regs[1] . '/';
}
$params['category_processed'] = true;
}
// reset category page
$force_page_adding = false;
if (array_key_exists('reset', $params) && $params['reset']) {
unset($params['reset']);
if ($processed_params['m_cat_id']) {
$processed_params['m_cat_page'] = 1;
$force_page_adding = true;
}
}
if ((array_key_exists('category_processed', $params) && $params['category_processed'] && ($processed_params['m_cat_page'] > 1)) || $force_page_adding) {
// category name was added before AND category page number found
$ret = rtrim($ret, '/') . '_' . $processed_params['m_cat_page'] . '/';
}
$template = array_key_exists('t', $params) ? $params['t'] : false;
$category_template = ($processed_params['m_cat_id'] > 0) && $params['pass_category'] ? $this->Application->getCategoryCache($processed_params['m_cat_id'], 'category_designs') : '';
if ((strtolower($template) == '__default__') && ($processed_params['m_cat_id'] == 0)) {
// for "Home" category set template to index when not set
$template = 'index';
}
// remove template from url if it is category index cached template
if ( ($template == $category_template) || (mb_strtolower($template) == '__default__') ) {
// given template is also default template for this category OR '__default__' given
$params['pass_template'] = false;
}
// remove template from url if it is site homepage on primary language & theme
if ( ($template == 'index') && $processed_params['m_lang'] == $rewrite_processor->primaryLanguageId && $processed_params['m_theme'] == $rewrite_processor->primaryThemeId ) {
// given template is site homepage on primary language & theme
$params['pass_template'] = false;
}
if ($template && $params['pass_template']) {
$ret .= $template . '/';
}
return mb_strtolower( rtrim($ret, '/') );
}
/**
* Checks if whole url_parts matches a whole In-CMS page
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
protected function _parseFriendlyUrl($url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
$sql = 'SELECT CategoryId, NamedParentPath
FROM ' . TABLE_PREFIX . 'Categories
WHERE FriendlyURL = ' . $this->Conn->qstr(implode('/', $url_parts));
$friendly = $this->Conn->GetRow($sql);
$rewrite_processor = $this->Application->recallObject('kRewriteUrlProcessor');
/* @var $rewrite_processor kRewriteUrlProcessor */
if ($friendly) {
$vars['m_cat_id'] = $friendly['CategoryId'];
$vars['t'] = preg_replace('/^Content\//i', '', $friendly['NamedParentPath']);
while ($url_parts) {
$rewrite_processor->partParsed( array_shift($url_parts) );
}
return true;
}
return false;
}
/**
* Extracts category part from url
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
protected function _parseCategory($url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
$res = false;
$url_part = array_shift($url_parts);
$category_id = 0;
$last_category_info = false;
$category_path = $url_part == 'content' ? '' : 'content';
$rewrite_processor = $this->Application->recallObject('kRewriteUrlProcessor');
/* @var $rewrite_processor kRewriteUrlProcessor */
do {
$category_path = trim($category_path . '/' . $url_part, '/');
// bb_<topic_id> -> forums/bb_2
if ( !preg_match('/^bb_[\d]+$/', $url_part) && preg_match('/(.*)_([\d]+)$/', $category_path, $rets) ) {
$category_path = $rets[1];
$vars['m_cat_page'] = $rets[2];
}
$sql = 'SELECT CategoryId, SymLinkCategoryId, NamedParentPath
FROM ' . TABLE_PREFIX . 'Categories
WHERE (LOWER(NamedParentPath) = ' . $this->Conn->qstr($category_path) . ') AND (ThemeId = ' . $vars['m_theme'] . ' OR ThemeId = 0)';
$category_info = $this->Conn->GetRow($sql);
if ($category_info !== false) {
$last_category_info = $category_info;
$rewrite_processor->partParsed($url_part);
$url_part = array_shift($url_parts);
$res = true;
}
} while ($category_info !== false && $url_part);
if ($last_category_info) {
// this category is symlink to other category, so use it's url instead
// (used in case if url prior to symlink adding was indexed by spider or was bookmarked)
if ($last_category_info['SymLinkCategoryId']) {
$sql = 'SELECT CategoryId, NamedParentPath
FROM ' . TABLE_PREFIX . 'Categories
WHERE (CategoryId = ' . $last_category_info['SymLinkCategoryId'] . ')';
$category_info = $this->Conn->GetRow($sql);
if ($category_info) {
// web symlinked category was found use it
// TODO: maybe 302 redirect should be made to symlinked category url (all other url parts should stay)
$last_category_info = $category_info;
}
}
// 1. Set virtual page as template, this will be replaced to physical template later in kApplication::Run.
// 2. Don't set CachedTemplate field as template here, because we will loose original page associated with it's cms blocks!
$vars['t'] = mb_strtolower( preg_replace('/^Content\//i', '', $last_category_info['NamedParentPath']));
$vars['m_cat_id'] = $last_category_info['CategoryId'];
$vars['is_virtual'] = true; // for template from POST, strange code there!
}
/*else {
$vars['m_cat_id'] = 0;
}*/
return $res;
}
/**
* Set's new unique resource id to user
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterItemValidate(kEvent $event)
{
$object = $event->getObject();
/* @var $object kDBItem */
$resource_id = $object->GetDBField('ResourceId');
if ( !$resource_id ) {
$object->SetDBField('ResourceId', $this->Application->NextResourceId());
}
}
/**
* Occurs before an item has been cloned
* Id of newly created item is passed as event' 'id' param
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeClone(kEvent $event)
{
parent::OnBeforeClone($event);
$object = $event->getObject();
/* @var $object kDBItem */
$object->SetDBField('ResourceId', 0); // this will reset it
}
}
Index: branches/5.3.x/core/units/statistics/statistics_tag_processor.php
===================================================================
--- branches/5.3.x/core/units/statistics/statistics_tag_processor.php (revision 16155)
+++ branches/5.3.x/core/units/statistics/statistics_tag_processor.php (revision 16156)
@@ -1,350 +1,350 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class StatisticsTagProcessor extends kDBTagProcessor {
var $TagCache = Array(); // parsed tag (in sql queries only) values are cached
var $CurrentSQL = ''; // sql query being currently processed
var $PostFormatting = false; // apply formatting to sql query results
var $PostFormattingParams = Array(); // post formatting params if any
function CalculateValue($params)
{
$object = $this->getObject($params);
$this->CurrentSQL = $object->GetDBField($params['field']);
// 1. replace prefix to actual one
$this->CurrentSQL = str_replace("<%prefix%>", TABLE_PREFIX, $this->CurrentSQL);
// 2. replace all pseudo-tags found in sql with their values
while ( ($tag = $this->FindTag()) != false ) {
$this->CurrentSQL = str_replace('<%'.$tag.'%>', $this->ProcessStatisticTag($tag), $this->CurrentSQL);
}
// 3. query sql and process gathered data
$values = $this->Conn->GetCol($this->CurrentSQL);
if (!$values) return '';
if (!$this->PostFormatting) return array_shift($values);
switch ($this->PostFormatting) {
case 'number':
// simple-specific postformatting
$lang = $this->Application->recallObject('lang.current');
/* @var $lang LanguagesItem */
$value = $lang->formatNumber($value, $this->PostFormattingParams['precision']);
break;
case 'COUNT':
// extended postformatting
$value = count($values);
break;
case 'SUM':
$value = 0;
foreach ($values as $cur_value) {
$value += $cur_value;
}
if ($this->PostFormattingParams['format_as'] == 'file') {
$value = size($value);
}
break;
// other type of information (not from db)
case 'SysFileSize':
$value = size( dir_size(FULL_PATH.'/') );
break;
default: // simple-default postformatting
$value = date($this->PostFormatting, array_shift($values));
break;
}
$this->PostFormatting = false;
$this->PostFormattingParams = Array();
return $value;
}
function FindTag()
{
// finds tag in current sql & returns it if found, false otherwise
$tagOpen = '<%'; $tagClose = '%>'; $tagOpenLen = strlen($tagOpen);
$startPos = strpos($this->CurrentSQL, $tagOpen);
if( $startPos !== false )
{
$endPos = strpos($this->CurrentSQL, $tagClose, $startPos);
return ($endPos > $startPos) ? substr($this->CurrentSQL, $startPos + $tagOpenLen, $endPos - $startPos - $tagOpenLen) : false;
}
return false;
}
function ProcessStatisticTag($tag)
{
$tag = trim($tag);
if (isset($this->TagCache[$tag])) {
return $this->TagCache[$tag];
}
$object = $this->getObject();
/* @var $object kDBItem */
list($tag_name, $tag_params) = explode(' ', $tag, 2); // 1st - function, 2nd .. nth - params
preg_match_all('/([\${}a-zA-Z0-9_.-]+)=(["\']{1,1})(.*?)(?<!\\\)\\2/s', $tag_params, $rets, PREG_SET_ORDER);
$tag_params = Array();
foreach ($rets AS $key => $val){
$tag_params[$val[1]] = str_replace(Array('\\' . $val[2], '+'), Array($val[2], ' '), $val[3]);
}
switch ($tag_name) {
case 'm:config':
// m:config name="<variable_name>"
return $this->Application->ConfigValue($tag_params['name']);
break;
case 'm:post_format':
// m:post_format field="<field_name>" type="<formatting_type>" precision="2"
$lang = $this->Application->recallObject('lang.current');
/* @var $lang LanguagesItem */
switch ($tag_params['type']) {
case 'date':
$this->PostFormatting = $lang->GetDBField('DateFormat');
break;
case 'time':
$this->PostFormatting = $lang->GetDBField('TimeFormat');
break;
case 'currency':
$this->PostFormatting = 'number';
$this->PostFormattingParams['precision'] = $tag_params['precision'];
break;
}
return $tag_params['field'];
break;
case 'm:custom_action':
// m:custom_action sql="empty" action="SysFileSize"
$this->PostFormatting = $tag_params['action'];
return ($tag_params['sql'] == 'empty') ? 'SELECT 1' : $tag_params['sql'];
break;
case 'modules:get_current':
return $object->GetDBField('Module');
break;
case 'm:sql_action':
//m:sql_action sql="SHOW TABLES" action="COUNT" field="*"
$this->PostFormatting = $tag_params['action'];
$this->PostFormattingParams = $tag_params;
return $tag_params['sql'];
break;
case 'link:hit_count':
if ($tag_params['type'] == 'top') {// by now only top is supported
$top_links_count = $this->Application->ConfigValue('Link_TopCount');
$sql = 'SELECT Hits
FROM '.TABLE_PREFIX.'Link
ORDER BY Hits DESC LIMIT 0, '.$top_links_count;
return $this->getLastRecord($sql, 'Hits');
}
break;
case 'article:hit_count':
if ($tag_params['type'] == 'top') {// by now only top is supported
$top_articles_count = $this->Application->ConfigValue('News_VotesToHot');
$min_votes = $this->Application->ConfigValue('News_MinVotes');
$sql = 'SELECT CachedRating
FROM '.TABLE_PREFIX.'News
WHERE CachedVotesQty > '.$min_votes.'
ORDER BY CachedRating DESC LIMIT 0, '.$top_articles_count;
return $this->getLastRecord($sql, 'CachedRating');
}
break;
case 'topic:hit_count':
if ($tag_params['type'] == 'top') {// by now only top is supported
$top_posts_count = $this->Application->ConfigValue('Topic_PostsToPop');
$sql = 'SELECT Views
FROM '.TABLE_PREFIX.'Topic
ORDER BY Views DESC LIMIT 0, '.$top_posts_count;
return $this->getLastRecord($sql, 'Views');
}
break;
}
return '';
}
function getLastRecord($sql, $field)
{
$records = $this->Conn->GetCol($sql);
return count($records) ? array_pop($records) : 0;
}
/**
* Allows to get pending item count for prefix
*
* @param Array $params
* @return int
*/
function CountPending($params)
{
$prefix = $params['prefix'];
$cache_key = 'statistics.pending[%' . $this->Application->incrementCacheSerial($prefix, null, false) . '%]';
$value = $this->Application->getCache($cache_key);
if ($value === false) {
$config = $this->Application->getUnitConfig($prefix);
$statistics_info = $this->_getPendingStatisticsInfo($prefix);
if (!$statistics_info) {
return 0;
}
$status_field = $config->getStatusField(true);
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT COUNT(*)
FROM '. $config->getTableName() .'
WHERE '.$status_field.' = '.$statistics_info['status'];
$value = $this->Conn->GetOne($sql);
$this->Application->setCache($cache_key, $value);
}
return $value;
}
/**
* Returns pending statistics info
*
* @param string $prefix
* @return string
* @access protected
*/
protected function _getPendingStatisticsInfo($prefix)
{
$config = $this->Application->getUnitConfig($prefix);
return getArrayValue($config->getStatisticsInfo(), 'pending');
}
function GetTotalPending()
{
$prefixes = $this->getPendingPrefixes();
$sum = 0;
foreach ($prefixes as $prefix) {
$sum += $this->CountPending( Array('prefix' => $prefix) );
}
return $sum;
}
/**
* Get prefixes, that are allowed to have pending items (check module licenses too)
*
* @return Array
*/
function getPendingPrefixes()
{
- $modules_helper = $this->Application->recallObject('ModulesHelper');
+ $modules_helper = $this->Application->recallObject('kModulesHelper');
/* @var $modules_helper kModulesHelper */
$licensed_modules = array_map('strtolower', $modules_helper->_GetModules());
$sql = 'SELECT LOWER(Module), Prefix
FROM '.TABLE_PREFIX.'ItemTypes it
LEFT JOIN '.TABLE_PREFIX.'Modules m ON m.Name = it.Module
WHERE (m.Loaded = 1) AND (LENGTH(it.ClassName) > 0)';
$prefixes = $this->Conn->GetCol($sql, 'Prefix');
$module_names = array_map('strtolower', array_unique(array_values($prefixes)));
$module_names = array_intersect($module_names, $licensed_modules);
$ret = Array ();
foreach ($prefixes as $prefix => $module_name) {
if (in_array($module_name, $module_names)) {
$ret[] = $prefix;
}
}
return $ret;
}
function PrintPendingStatistics($params)
{
$check_prefixes = $this->getPendingPrefixes();
if (!$check_prefixes) {
return '';
}
$ret = '';
$columns = $params['columns'];
$block_params = $this->prepareTagParams( Array('name' => $this->SelectParam($params, 'render_as,block') ) );
$prefixes = Array();
foreach ($check_prefixes as $prefix) {
$statistics_info = $this->_getPendingStatisticsInfo($prefix);
if ($statistics_info) {
$prefixes[] = $prefix;
}
}
$row_number = 0;
foreach ($prefixes as $i => $prefix) {
$block_params['prefix'] = $prefix;
$statistics_info = $this->_getPendingStatisticsInfo($prefix);
if ($i % $columns == 0) {
$column_number = 1;
$ret .= '<tr>';
}
$block_params['column_number'] = $column_number;
$block_params['is_first'] = $i < $columns ? 1 : 0;
$template = $statistics_info['url']['t'];
unset($statistics_info['url']['t']);
$url = $this->Application->HREF($template, '', $statistics_info['url']);
if ($statistics_info['js_url'] != '#url#') {
$statistics_info['js_url'] = 'javascript:'.$statistics_info['js_url'];
}
$block_params['url'] = str_replace('#url#', $url, $statistics_info['js_url']);
$block_params['icon'] = $statistics_info['icon'];
$block_params['label'] = $statistics_info['label'];
$ret .= $this->Application->ParseBlock($block_params);
$column_number++;
if (($i+1) % $columns == 0) {
$ret .= '</tr>';
}
}
return $ret;
}
- }
\ No newline at end of file
+ }
Index: branches/5.3.x/core/units/helpers/helpers_config.php
===================================================================
--- branches/5.3.x/core/units/helpers/helpers_config.php (revision 16155)
+++ branches/5.3.x/core/units/helpers/helpers_config.php (revision 16156)
@@ -1,78 +1,78 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
$config = Array (
'Prefix' => 'helpers',
'EventHandlerClass' => Array ('class' => 'kEventHandler', 'file' => '', 'build_event' => 'OnBuild'),
'RegisterClasses' => Array (
Array ('pseudo' => 'kMultiLanguageHelper', 'class' => 'kMultiLanguageHelper', 'file' => 'multilanguage_helper.php', 'build_event' => ''),
Array ('pseudo' => 'SearchHelper', 'class' => 'kSearchHelper', 'file' => 'search_helper.php', 'build_event' => ''),
Array ('pseudo' => 'SectionsHelper', 'class' => 'kSectionsHelper', 'file' => 'sections_helper.php', 'build_event' => ''),
Array ('pseudo' => 'PermissionsHelper', 'class' => 'kPermissionsHelper', 'file' => 'permissions_helper.php', 'build_event' => ''),
- Array ('pseudo' => 'ModulesHelper', 'class' => 'kModulesHelper', 'file' => 'modules_helper.php', 'build_event' => ''),
+ Array ('pseudo' => 'kModulesHelper', 'class' => 'kModulesHelper', 'file' => 'modules_helper.php', 'build_event' => ''),
Array ('pseudo' => 'CategoryItemRewrite', 'class' => 'CategoryItemRewrite', 'file' => 'mod_rewrite_helper.php', 'build_event' => ''),
Array ('pseudo' => 'RecursiveHelper', 'class' => 'kRecursiveHelper', 'file' => 'recursive_helper.php', 'build_event' => ''),
Array ('pseudo' => 'FilenamesHelper', 'class' => 'kFilenamesHelper', 'file' => 'filenames_helper.php', 'build_event' => ''),
Array ('pseudo' => 'ClipboardHelper', 'class' => 'kClipboardHelper', 'file' => 'clipboard_helper.php', 'build_event' => ''),
Array ('pseudo' => 'ColumnPickerHelper', 'class' => 'kColumnPickerHelper', 'file' => 'col_picker_helper.php', 'build_event' => ''),
Array ('pseudo' => 'ThemesHelper', 'class' => 'kThemesHelper', 'file' => 'themes_helper.php', 'build_event' => ''),
Array ('pseudo' => 'CaptchaHelper', 'class' => 'kCaptchaHelper', 'file' => 'captcha_helper.php', 'build_event' => ''),
Array ('pseudo' => 'PriorityHelper', 'class' => 'kPriorityHelper', 'file' => 'priority_helper.php', 'build_event' => ''),
Array ('pseudo' => 'CurlHelper', 'class' => 'kCurlHelper', 'file' => 'curl_helper.php', 'build_event' => ''),
Array ('pseudo' => 'CountHelper', 'class' => 'kCountHelper', 'file' => 'count_helper.php', 'build_event' => ''),
Array ('pseudo' => 'ImageHelper', 'class' => 'ImageHelper', 'file' => 'image_helper.php', 'build_event' => ''),
Array ('pseudo' => 'FileHelper', 'class' => 'FileHelper', 'file' => 'file_helper.php', 'build_event' => ''),
Array ('pseudo' => 'CategoryHelper', 'class' => 'CategoryHelper', 'file' => 'category_helper.php', 'build_event' => ''),
Array ('pseudo' => 'kNavigationBar', 'class' => 'kNavigationBar', 'file' => 'navigation_bar.php', 'build_event' => ''),
Array ('pseudo' => 'CSVHelper', 'class' => 'kCSVHelper', 'file' => 'csv_helper.php', 'build_event' => ''),
Array ('pseudo' => 'ChartHelper', 'class' => 'kChartHelper', 'file' => 'chart_helper.php', 'build_event' => ''),
Array ('pseudo' => 'RatingHelper', 'class' => 'RatingHelper', 'file' => 'rating_helper.php', 'build_event' => ''),
Array ('pseudo' => 'FCKHelper', 'class' => 'fckFCKHelper', 'file' => 'fck_helper.php', 'build_event' => ''),
Array ('pseudo' => 'SpamHelper', 'class' => 'SpamHelper', 'file' => 'spam_helper.php', 'build_event' => ''),
Array ('pseudo' => 'TemplateHelper', 'class' => 'TemplateHelper', 'file' => 'template_helper.php', 'build_event' => ''),
Array ('pseudo' => 'MailingListHelper', 'class' => 'MailingListHelper', 'file' => 'mailing_list_helper.php', 'build_event' => ''),
Array ('pseudo' => 'JSONHelper', 'class' => 'JSONHelper', 'file' => 'json_helper.php', 'build_event' => ''),
Array ('pseudo' => 'LanguageImportHelper', 'class' => 'LanguageImportHelper', 'file' => 'language_import_helper.php', 'build_event' => ''),
Array ('pseudo' => 'SkinHelper', 'class' => 'SkinHelper', 'file' => 'skin_helper.php', 'build_event' => ''),
Array ('pseudo' => 'SiteConfigHelper', 'class' => 'SiteConfigHelper', 'file' => 'site_config_helper.php', 'build_event' => ''),
Array ('pseudo' => 'MenuHelper', 'class' => 'MenuHelper', 'file' => 'menu_helper.php', 'build_event' => ''),
Array ('pseudo' => 'InpCustomFieldsHelper', 'class' => 'InpCustomFieldsHelper', 'file' => 'custom_fields_helper.php', 'build_event' => ''),
Array ('pseudo' => 'CountryStatesHelper', 'class' => 'kCountryStatesHelper', 'file' => 'country_states_helper.php', 'build_event' => ''),
Array ('pseudo' => 'BracketsHelper', 'class' => 'kBracketsHelper', 'file' => 'brackets_helper.php', 'build_event' => ''),
Array ('pseudo' => 'kXMLHelper', 'class' => 'kXMLHelper', 'file' => 'xml_helper.php', 'build_event' => ''),
Array ('pseudo' => 'CatItemExportHelper', 'class' => 'kCatDBItemExportHelper', 'file' => 'cat_dbitem_export_helper.php', 'build_event' => ''),
Array ('pseudo' => 'kEmailTemplateHelper', 'class' => 'kEmailTemplateHelper', 'file' => 'email_template_helper.php', 'build_event' => ''),
Array ('pseudo' => 'ListHelper', 'class' => 'ListHelper', 'file' => 'list_helper.php', 'build_event' => ''),
Array ('pseudo' => 'FormSubmissionHelper', 'class' => 'FormSubmissionHelper', 'file' => 'form_submission_helper.php', 'build_event' => ''),
Array ('pseudo' => 'MailboxHelper', 'class' => 'MailboxHelper', 'file' => 'mailbox_helper.php', 'build_event' => ''),
Array ('pseudo' => 'POP3Helper', 'class' => 'POP3Helper', 'file' => 'pop3_helper.php', 'build_event' => ''),
Array ('pseudo' => 'MimeDecodeHelper', 'class' => 'MimeDecodeHelper', 'file' => 'mime_decode_helper.php', 'build_event' => ''),
Array ('pseudo' => 'UserHelper', 'class' => 'UserHelper', 'file' => 'user_helper.php', 'build_event' => ''),
Array ('pseudo' => 'SiteHelper', 'class' => 'SiteHelper', 'file' => 'site_helper.php', 'build_event' => ''),
Array ('pseudo' => 'DeploymentHelper', 'class' => 'DeploymentHelper', 'file' => 'deployment_helper.php', 'build_event' => ''),
Array ('pseudo' => 'PageHelper', 'class' => 'PageHelper', 'file' => 'page_helper.php', 'build_event' => ''),
Array ('pseudo' => 'BackupHelper', 'class' => 'BackupHelper', 'file' => 'backup_helper.php', 'build_event' => ''),
Array ('pseudo' => 'AjaxFormHelper', 'class' => 'AjaxFormHelper', 'file' => 'ajax_form_helper.php', 'build_event' => ''),
Array ('pseudo' => 'kCronHelper', 'class' => 'kCronHelper', 'file' => 'cron_helper.php', 'build_event' => ''),
Array ('pseudo' => 'kUploadHelper', 'class' => 'kUploadHelper', 'file' => 'upload_helper.php', 'build_event' => ''),
),
- );
\ No newline at end of file
+ );
Index: branches/5.3.x/core/units/modules/modules_tag_processor.php
===================================================================
--- branches/5.3.x/core/units/modules/modules_tag_processor.php (revision 16155)
+++ branches/5.3.x/core/units/modules/modules_tag_processor.php (revision 16156)
@@ -1,117 +1,117 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class ModulesTagProcessor extends kDBTagProcessor {
function ModuleInstalled($params)
{
return $this->Application->isModuleEnabled($params['name']);
}
function _hasPrivileges()
{
return $this->Application->RecallVar('user_id') == USER_ROOT;
}
function AlreadyInstalled($params)
{
if (!$this->_hasPrivileges()) {
// don't show licenses status for non-privileged users
return true;
}
$object = $this->getObject($params);
/* @var $object kDBList */
- $modules_helper = $this->Application->recallObject('ModulesHelper');
+ $modules_helper = $this->Application->recallObject('kModulesHelper');
/* @var $modules_helper kModulesHelper */
return $modules_helper->moduleInstalled( $object->GetDBField('Name') );
}
function ModuleLicensed($params)
{
if (!$this->_hasPrivileges()) {
// don't show licenses status for non-privileged users
return true;
}
$object = $this->getObject($params);
/* @var $object kDBList */
- $modules_helper = $this->Application->recallObject('ModulesHelper');
+ $modules_helper = $this->Application->recallObject('kModulesHelper');
/* @var $modules_helper kModulesHelper */
$licensed_modules = array_map('strtolower', $modules_helper->_GetModules());
return in_array(strtolower($object->GetDBField('Name')), $licensed_modules);
}
function PrerequisitesMet($params)
{
return !$this->_getPrerequisitesErrors($params);
}
function _getPrerequisitesErrors($params)
{
static $errors = Array ();
$object = $this->getObject($params);
/* @var $object kDBList */
$module_path = $object->GetDBField('Path');
if (!array_key_exists($module_path, $errors)) {
require_once FULL_PATH . '/core/install/install_toolkit.php';
$toolkit = new kInstallToolkit();
$module_version = $toolkit->GetMaxModuleVersion($module_path);
$errors[$module_path] = $toolkit->CheckPrerequisites($module_path, Array ($module_version), 'standalone');
}
return $errors[$module_path];
}
function ListPrerequisites($params)
{
$errors = $this->_getPrerequisitesErrors($params);
$ret = '';
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
foreach ($errors as $error) {
$block_params['error'] = $error;
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
function InstallLink($params)
{
$object = $this->getObject($params);
/* @var $object kDBList */
$module_path = strtolower( $object->GetDBField('Path') );
$url_params = Array ('redirect' => 1, 'admin' => 1);
return $this->Application->HREF('dummy', '_FRONT_END_', $url_params, $module_path . 'install.php');
}
- }
\ No newline at end of file
+ }
Index: branches/5.3.x/core/units/modules/modules_event_handler.php
===================================================================
--- branches/5.3.x/core/units/modules/modules_event_handler.php (revision 16155)
+++ branches/5.3.x/core/units/modules/modules_event_handler.php (revision 16156)
@@ -1,177 +1,177 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class ModulesEventHandler extends kDBEventHandler {
/**
* Builds item (loads if needed)
*
* Pattern: Prototype Manager
*
* @param kEvent $event
* @access protected
*/
protected function OnItemBuild(kEvent $event)
{
$this->Application->SetVar($event->getPrefixSpecial(true) . '_id', $event->Special);
parent::OnItemBuild($event);
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
* @return void
* @access protected
* @see kDBEventHandler::OnListBuild()
*/
protected function SetCustomQuery(kEvent $event)
{
parent::SetCustomQuery($event);
$object = $event->getObject();
/* @var $object kDBList */
if ( $event->Special ) {
$object->addFilter('current_module', '%1$s.Name = ' . $event->Special);
}
$object->addFilter('not_core', '%1$s.Name <> "Core"');
}
/**
* Define alternative event processing method names
*
* @return void
* @see kEventHandler::$eventMethods
* @access protected
*/
protected function mapEvents()
{
parent::mapEvents();
$this->eventMethods['OnMassApprove'] = 'moduleAction';
$this->eventMethods['OnMassDecline'] = 'moduleAction';
}
/**
* Disabled modules, but not In-Portal
*
* @param kEvent $event
*/
function moduleAction($event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return ;
}
$object = $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
$ids = $this->StoreSelectedIDs($event);
if (!$ids) {
return ;
}
$updated = 0;
$status_field = $event->getUnitConfig()->getStatusField(true);
foreach ($ids as $id) {
$object->Load($id);
if (in_array($id, Array ('In-Portal', 'Core')) || !$object->isLoaded()) {
// don't allow any kind of manupulations with kernel
// approve/decline on not installed module
continue;
}
$enabled = $event->Name == 'OnMassApprove' ? 1 : 0;
$object->SetDBField($status_field, $enabled);
if (!$object->GetChangedFields()) {
// no changes -> skip
continue;
}
if ($object->Update()) {
$updated++;
$sql = 'UPDATE ' . TABLE_PREFIX . 'ImportScripts
SET Status = ' . $enabled . '
WHERE Module = "' . $object->GetDBField('Name') . '"';
$this->Conn->Query($sql);
}
else {
$event->status = kEvent::erFAIL;
$event->redirect = false;
break;
}
}
if ( $updated ) {
$event->status = kEvent::erSUCCESS;
$event->SetRedirectParam('opener', 's');
$this->Application->DeleteUnitCache();
$this->Application->DeleteSectionCache();
$event->SetRedirectParam('RefreshTree', 1);
}
}
/**
* Occurs after list is queried
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnAfterListQuery(kEvent $event)
{
parent::OnAfterListQuery($event);
- $modules_helper = $this->Application->recallObject('ModulesHelper');
+ $modules_helper = $this->Application->recallObject('kModulesHelper');
/* @var $modules_helper kModulesHelper */
$new_modules = $modules_helper->getModules(kModulesHelper::NOT_INSTALLED);
if ( !$new_modules || $this->Application->RecallVar('user_id') != USER_ROOT ) {
return;
}
require_once FULL_PATH . '/core/install/install_toolkit.php';
$toolkit = new kInstallToolkit();
$object = $event->getObject();
/* @var $object kDBList */
foreach ($new_modules as $module) {
$module_record = Array (
'Name' => $toolkit->getModuleName($module),
'Path' => 'modules/' . $module . '/',
'Version' => $toolkit->GetMaxModuleVersion('modules/' . $module . '/'),
'Loaded' => 0,
'BuildDate' => null,
);
$object->addRecord($module_record);
}
}
- }
\ No newline at end of file
+ }
Index: branches/5.3.x/core/install/step_templates/choose_modules.tpl
===================================================================
--- branches/5.3.x/core/install/step_templates/choose_modules.tpl (revision 16155)
+++ branches/5.3.x/core/install/step_templates/choose_modules.tpl (revision 16156)
@@ -1,61 +1,61 @@
<?php
ob_start();
?>
<tr class="table-color2">
<td class="text" colspan="2" valign="middle">
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<input type="checkbox" %3$s name="modules[]" id="module_%1$s" value="%1$s" style="margin-top: 8px;"/>
</td>
<td valign="top">
<label for="module_%1$s" style="">%2$s</label>
<div style="font-weight: bold; color: red;">%4$s</div>
</td>
</tr>
</table>
</td>
</tr>
<?php
$module_tpl = ob_get_clean();
echo '<br/><p>&nbsp;<strong>Please select Modules you would like to install:</strong></p>';
$first_time = $this->GetVar('step') != $this->currentStep; // data from this step was not submitted yet
$selected = $this->GetVar('modules');
if (!$selected) {
// preselect interface modules
$selected = Array ('core');
}
- $modules_helper = $this->Application->recallObject('ModulesHelper');
+ $modules_helper = $this->Application->recallObject('kModulesHelper');
/* @var $modules_helper kModulesHelper */
$licensed_modules = array_map('strtolower', $modules_helper->_GetModules());
$modules = $this->ScanModules();
foreach ($modules as $module) {
$module_version = $this->toolkit->GetMaxModuleVersion('modules/' . $module . '/');
$prerequisites_errors = $this->toolkit->CheckPrerequisites('modules/' . $module . '/', Array ($module_version), 'install');
$license_module = $module;
if (!in_array(strtolower($license_module), $licensed_modules)) {
// when module isn't licensed user can't install it
continue; // option #1: don't show non-licensed modules
array_unshift($prerequisites_errors, 'Module not licensed'); // option #2: show warning under module name
}
if ($prerequisites_errors) {
// disable checkbox, when some of prerequisites not passed
$checked = 'disabled';
}
else {
// preserve user selected checked status
$checked = in_array($module, $selected) || $first_time ? 'checked="checked"' : '';
}
$error_msg = $prerequisites_errors ? implode('<br />', $prerequisites_errors) : '';
$module_string = $this->toolkit->getModuleString($module, $module_version);
echo sprintf($module_tpl, $module, $module_string, $checked, $error_msg);
}
-?>
\ No newline at end of file
+?>
Index: branches/5.3.x/core/install/upgrades.php
===================================================================
--- branches/5.3.x/core/install/upgrades.php (revision 16155)
+++ branches/5.3.x/core/install/upgrades.php (revision 16156)
@@ -1,2501 +1,2501 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
$upgrade_class = 'CoreUpgrades';
/**
* Class, that holds all upgrade scripts for "Core" module
*
*/
class CoreUpgrades extends kUpgradeHelper {
/**
* Tables, that were renamed during 5.2.0 version upgrade
*
* @var Array
* @access private
*/
private $renamedTables = Array (
'ban-rule' => Array ('from' => 'BanRules', 'to' => 'UserBanRules'),
'conf' => Array ('from' => 'ConfigurationValues', 'to' => 'SystemSettings'),
'c' => Array ('from' => 'Category', 'to' => 'Categories'),
'cf' => Array ('from' => 'CustomField', 'to' => 'CustomFields'),
'draft' => Array ('from' => 'Drafts', 'to' => 'FormSubmissionReplyDrafts'),
'email-template' => Array ('from' => 'Events', 'to' => 'EmailEvents'),
'fav' => Array ('from' => 'Favorites', 'to' => 'UserFavorites'),
'img' => Array ('from' => 'Images', 'to' => 'CatalogImages'),
'#file' => Array ('from' => 'ItemFiles', 'to' => 'CatalogFiles'),
'rev' => Array ('from' => 'ItemReview', 'to' => 'CatalogReviews'),
'lang' => Array ('from' => 'Language', 'to' => 'Languages'),
'permission-type' => Array ('from' => 'PermissionConfig', 'to' => 'CategoryPermissionsConfig'),
'phrases' => Array ('from' => 'Phrase', 'to' => 'LanguageLabels'),
'g' => Array ('from' => 'PortalGroup', 'to' => 'UserGroups'),
'user-profile' => Array ('from' => 'PersistantSessionData', 'to' => 'UserPersistentSessionData'),
'u' => Array ('from' => 'PortalUser', 'to' => 'Users'),
'u-cdata' => Array ('from' => 'PortalUserCustomData', 'to' => 'UserCustomData'),
'search' => Array ('from' => 'RelatedSearches', 'to' => 'CategoryRelatedSearches'),
'rel' => Array ('from' => 'Relationship', 'to' => 'CatalogRelationships'),
'search-log' => Array ('from' => 'SearchLog', 'to' => 'SearchLogs'),
'skin' => Array ('from' => 'Skins', 'to' => 'AdminSkins'),
'submission-log' => Array ('from' => 'SubmissionLog', 'to' => 'FormSubmissionReplies'),
'theme' => Array ('from' => 'Theme', 'to' => 'Themes'),
'ug' => Array ('from' => 'UserGroup', 'to' => 'UserGroupRelations'),
'visits' => Array ('from' => 'Visits', 'to' => 'UserVisits'),
'session-log' => Array ('from' => 'SessionLogs', 'to' => 'UserSessionLogs'),
);
/**
* Changes table structure, where multilingual fields of TEXT type are present
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_4_1_0($mode)
{
if ($mode == 'before') {
// don't user after, because In-Portal calls this method too
$this->_toolkit->SaveConfig();
}
if ($mode == 'after') {
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$languages = $ml_helper->getLanguages();
$this->Application->UnitConfigReader->ReReadConfigs();
foreach ($this->Application->UnitConfigReader->getPrefixes() as $prefix) {
$this->updateTextFields($prefix, $languages);
}
}
}
/**
* Moves ReplacementTags functionality from EmailMessage to Events table
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_4_1_1($mode)
{
if ($mode == 'after') {
$sql = 'SELECT ReplacementTags, EventId
FROM '.TABLE_PREFIX.'EmailMessage
WHERE (ReplacementTags IS NOT NULL) AND (ReplacementTags <> "") AND (LanguageId = 1)';
$replacement_tags = $this->Conn->GetCol($sql, 'EventId');
foreach ($replacement_tags as $event_id => $replacement_tag) {
$sql = 'UPDATE '.TABLE_PREFIX.'Events
SET ReplacementTags = '.$this->Conn->qstr($replacement_tag).'
WHERE EventId = '.$event_id;
$this->Conn->Query($sql);
}
// drop moved field from source table
$sql = 'ALTER TABLE '.TABLE_PREFIX.'EmailMessage
DROP `ReplacementTags`';
$this->Conn->Query($sql);
}
}
/**
* Callback function, that makes all ml fields of text type null with same default value
*
* @param string $prefix
* @param Array $languages
*
* @return boolean
*/
function updateTextFields($prefix, $languages)
{
$config = $this->Application->getUnitConfig($prefix);
if ( $config->getTableName() === false || $config->getFields() === false ) {
// invalid config found or prefix not found
return false;
}
$table_name = $config->getTableName();
$table_structure = $this->Conn->Query('DESCRIBE ' . $table_name, 'Field');
if ( !$table_structure ) {
// table not found
return false;
}
$sqls = Array();
foreach ( $config->getFields() as $field => $options ) {
if ( isset($options['formatter']) && $options['formatter'] == 'kMultiLanguage' && !isset($options['master_field']) ) {
// update all l<lang_id>_<field_name> fields (new format)
foreach ( $languages as $language_id ) {
$ml_field = 'l' . $language_id . '_' . $field;
if ( $table_structure[$ml_field]['Type'] == 'text' ) {
$sqls[] = 'CHANGE ' . $ml_field . ' ' . $ml_field . ' TEXT NULL DEFAULT NULL';
}
}
// update <field_name> if found (old format)
if ( isset($table_structure[$field]) && $table_structure[$field]['Type'] == 'text' ) {
$sqls[] = 'CHANGE ' . $field . ' ' . $field . ' TEXT NULL DEFAULT NULL';
}
}
}
if ( $sqls ) {
$sql = 'ALTER TABLE ' . $table_name . ' ' . implode(', ', $sqls);
$this->Conn->Query($sql);
}
return true;
}
/**
* Replaces In-Portal tags in Forgot Password related email events to K4 ones
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_4_2_0($mode)
{
if ($mode == 'after') {
// 1. get event ids based on their name and type combination
$event_names = Array (
'USER.PSWD_' . EmailTemplate::TEMPLATE_TYPE_ADMIN,
'USER.PSWD_' . EmailTemplate::TEMPLATE_TYPE_FRONTEND,
'USER.PSWDC_' . EmailTemplate::TEMPLATE_TYPE_FRONTEND,
);
$event_sql = Array ();
foreach ($event_names as $mixed_event) {
list ($event_name, $event_type) = explode('_', $mixed_event, 2);
$event_sql[] = 'Event = "'.$event_name.'" AND Type = '.$event_type;
}
$sql = 'SELECT EventId
FROM '.TABLE_PREFIX.'Events
WHERE ('.implode(') OR (', $event_sql).')';
$event_ids = implode(',', $this->Conn->GetCol($sql));
// 2. replace In-Portal tags to K4 tags
$replacements = Array (
'<inp:touser _Field="Password" />' => '<inp2:u_ForgottenPassword />',
'<inp:m_confirm_password_link />' => '<inp2:u_ConfirmPasswordLink no_amp="1"/>',
);
foreach ($replacements as $old_tag => $new_tag) {
$sql = 'UPDATE '.TABLE_PREFIX.'EmailMessage
SET Template = REPLACE(Template, '.$this->Conn->qstr($old_tag).', '.$this->Conn->qstr($new_tag).')
WHERE EventId IN ('.$event_ids.')';
$this->Conn->Query($sql);
}
}
}
/**
* Makes admin primary language same as front-end - not needed, done in SQL
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_4_2_1($mode)
{
}
function Upgrade_4_2_2($mode)
{
if ( $mode == 'before' ) {
$sql = 'SELECT LanguageId
FROM ' . TABLE_PREFIX . 'Language
WHERE PrimaryLang = 1';
if ( $this->Conn->GetOne($sql) ) {
return;
}
$this->Conn->Query('UPDATE ' . TABLE_PREFIX . 'Language SET PrimaryLang = 1 ORDER BY LanguageId LIMIT 1');
}
}
/**
* Adds index to "dob" field in "PortalUser" table when it's missing
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_4_3_1($mode)
{
if ($mode == 'after') {
$sql = 'DESCRIBE ' . TABLE_PREFIX . 'PortalUser';
$structure = $this->Conn->Query($sql);
foreach ($structure as $field_info) {
if ($field_info['Field'] == 'dob') {
if (!$field_info['Key']) {
$sql = 'ALTER TABLE ' . TABLE_PREFIX . 'PortalUser
ADD INDEX (dob)';
$this->Conn->Query($sql);
}
break;
}
}
}
}
/**
* Removes duplicate phrases, update file paths in database
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_4_3_9($mode)
{
// 1. find In-Portal old <inp: tags
$sql = 'SELECT EmailMessageId
FROM '.TABLE_PREFIX.'EmailMessage
WHERE Template LIKE \'%<inp:%\'';
$event_ids = implode(',', $this->Conn->GetCol($sql));
// 2. replace In-Portal old <inp: tags to K4 tags
$replacements = Array (
'<inp:m_category_field _Field="Name" _StripHTML="1"' => '<inp2:c_Field name="Name"',
'<inp:touser _Field="password"' => '<inp2:u_Field name="Password_plain"',
'<inp:touser _Field="UserName"' => '<inp2:u_Field name="Login"',
'<inp:touser _Field="' => '<inp2:u_Field name="',
'<inp:m_page_title' => '<inp2:m_BaseUrl',
'<inp:m_theme_url _page="current"' => '<inp2:m_BaseUrl',
'<inp:topic _field="text"' => '<inp2:bb-post_Field name="PostingText"',
'<inp:topic _field="link" _Template="inbulletin/post_list"' => '<inp2:bb_TopicLink template="__default__"',
);
if ($event_ids) {
foreach ($replacements as $old_tag => $new_tag) {
$sql = 'UPDATE '.TABLE_PREFIX.'EmailMessage
SET Template = REPLACE(Template, '.$this->Conn->qstr($old_tag).', '.$this->Conn->qstr($new_tag).')
WHERE EventId IN ('.$event_ids.')';
$this->Conn->Query($sql);
}
}
if ($mode == 'after') {
$this->_insertInPortalData();
$this->_moveDatabaseFolders();
// in case, when In-Portal module is enabled -> turn AdvancedUserManagement on too
if ($this->Application->findModule('Name', 'In-Portal')) {
$sql = 'UPDATE ' . TABLE_PREFIX . 'ConfigurationValues
SET VariableValue = 1
WHERE VariableName = "AdvancedUserManagement"';
$this->Conn->Query($sql);
}
}
if ($mode == 'languagepack') {
$this->_removeDuplicatePhrases();
}
}
function _insertInPortalData()
{
$data = Array (
'ConfigurationAdmin' => Array (
'UniqueField' => 'VariableName',
'Records' => Array (
'AllowDeleteRootCats' => "('AllowDeleteRootCats', 'la_Text_General', 'la_AllowDeleteRootCats', 'checkbox', NULL , NULL , 10.09, 0, 0)",
'Catalog_PreselectModuleTab' => "('Catalog_PreselectModuleTab', 'la_Text_General', 'la_config_CatalogPreselectModuleTab', 'checkbox', NULL, NULL, 10.10, 0, 1)",
'RecycleBinFolder' => "('RecycleBinFolder', 'la_Text_General', 'la_config_RecycleBinFolder', 'text', NULL , NULL , 10.11, 0, 0)",
'AdvancedUserManagement' => "('AdvancedUserManagement', 'la_Text_General', 'la_prompt_AdvancedUserManagement', 'checkbox', NULL, NULL, '10.011', 0, 1)",
),
),
'ConfigurationValues' => Array (
'UniqueField' => 'VariableName',
'Records' => Array (
'AdvancedUserManagement' => "(DEFAULT, 'AdvancedUserManagement', 0, 'In-Portal:Users', 'in-portal:configure_users')",
),
),
'ItemTypes' => Array (
'UniqueField' => 'ItemType',
'Records' => Array (
'1' => "(1, 'In-Portal', 'c', 'Category', 'Name', 'CreatedById', NULL, NULL, 'la_ItemTab_Categories', 1, 'admin/category/addcategory.php', 'clsCategory', 'Category')",
'6' => "(6, 'In-Portal', 'u', 'PortalUser', 'Login', 'PortalUserId', NULL, NULL, '', 0, '', 'clsPortalUser', 'User')",
),
),
'PermissionConfig' => Array (
'UniqueField' => 'PermissionName',
'Records' => Array (
'CATEGORY.ADD' => "(DEFAULT, 'CATEGORY.ADD', 'lu_PermName_Category.Add_desc', 'lu_PermName_Category.Add_error', 'In-Portal')",
'CATEGORY.DELETE' => "(DEFAULT, 'CATEGORY.DELETE', 'lu_PermName_Category.Delete_desc', 'lu_PermName_Category.Delete_error', 'In-Portal')",
'CATEGORY.ADD.PENDING' => "(DEFAULT, 'CATEGORY.ADD.PENDING', 'lu_PermName_Category.AddPending_desc', 'lu_PermName_Category.AddPending_error', 'In-Portal')",
'CATEGORY.MODIFY' => "(DEFAULT, 'CATEGORY.MODIFY', 'lu_PermName_Category.Modify_desc', 'lu_PermName_Category.Modify_error', 'In-Portal')",
'ADMIN' => "(DEFAULT, 'ADMIN', 'lu_PermName_Admin_desc', 'lu_PermName_Admin_error', 'Admin')",
'LOGIN' => "(DEFAULT, 'LOGIN', 'lu_PermName_Login_desc', 'lu_PermName_Admin_error', 'Front')",
'DEBUG.ITEM' => "(DEFAULT, 'DEBUG.ITEM', 'lu_PermName_Debug.Item_desc', '', 'Admin')",
'DEBUG.LIST' => "(DEFAULT, 'DEBUG.LIST', 'lu_PermName_Debug.List_desc', '', 'Admin')",
'DEBUG.INFO' => "(DEFAULT, 'DEBUG.INFO', 'lu_PermName_Debug.Info_desc', '', 'Admin')",
'PROFILE.MODIFY' => "(DEFAULT, 'PROFILE.MODIFY', 'lu_PermName_Profile.Modify_desc', '', 'Admin')",
'SHOWLANG' => "(DEFAULT, 'SHOWLANG', 'lu_PermName_ShowLang_desc', '', 'Admin')",
'FAVORITES' => "(DEFAULT, 'FAVORITES', 'lu_PermName_favorites_desc', 'lu_PermName_favorites_error', 'In-Portal')",
'SYSTEM_ACCESS.READONLY' => "(DEFAULT, 'SYSTEM_ACCESS.READONLY', 'la_PermName_SystemAccess.ReadOnly_desc', 'la_PermName_SystemAccess.ReadOnly_error', 'Admin')",
),
),
'Permissions' => Array (
'UniqueField' => 'Permission;GroupId;Type;CatId',
'Records' => Array (
'LOGIN;12;1;0' => "(DEFAULT, 'LOGIN', 12, 1, 1, 0)",
'in-portal:site.view;11;1;0' => "(DEFAULT, 'in-portal:site.view', 11, 1, 1, 0)",
'in-portal:browse.view;11;1;0' => "(DEFAULT, 'in-portal:browse.view', 11, 1, 1, 0)",
'in-portal:advanced_view.view;11;1;0' => "(DEFAULT, 'in-portal:advanced_view.view', 11, 1, 1, 0)",
'in-portal:reviews.view;11;1;0' => "(DEFAULT, 'in-portal:reviews.view', 11, 1, 1, 0)",
'in-portal:configure_categories.view;11;1;0' => "(DEFAULT, 'in-portal:configure_categories.view', 11, 1, 1, 0)",
'in-portal:configure_categories.edit;11;1;0' => "(DEFAULT, 'in-portal:configure_categories.edit', 11, 1, 1, 0)",
'in-portal:configuration_search.view;11;1;0' => "(DEFAULT, 'in-portal:configuration_search.view', 11, 1, 1, 0)",
'in-portal:configuration_search.edit;11;1;0' => "(DEFAULT, 'in-portal:configuration_search.edit', 11, 1, 1, 0)",
'in-portal:configuration_email.view;11;1;0' => "(DEFAULT, 'in-portal:configuration_email.view', 11, 1, 1, 0)",
'in-portal:configuration_email.edit;11;1;0' => "(DEFAULT, 'in-portal:configuration_email.edit', 11, 1, 1, 0)",
'in-portal:configuration_custom.view;11;1;0' => "(DEFAULT, 'in-portal:configuration_custom.view', 11, 1, 1, 0)",
'in-portal:configuration_custom.add;11;1;0' => "(DEFAULT, 'in-portal:configuration_custom.add', 11, 1, 1, 0)",
'in-portal:configuration_custom.edit;11;1;0' => "(DEFAULT, 'in-portal:configuration_custom.edit', 11, 1, 1, 0)",
'in-portal:configuration_custom.delete;11;1;0' => "(DEFAULT, 'in-portal:configuration_custom.delete', 11, 1, 1, 0)",
'in-portal:users.view;11;1;0' => "(DEFAULT, 'in-portal:users.view', 11, 1, 1, 0)",
'in-portal:user_list.advanced:ban;11;1;0' => "(DEFAULT, 'in-portal:user_list.advanced:ban', 11, 1, 1, 0)",
'in-portal:user_list.advanced:send_email;11;1;0' => "(DEFAULT, 'in-portal:user_list.advanced:send_email', 11, 1, 1, 0)",
'in-portal:user_groups.view;11;1;0' => "(DEFAULT, 'in-portal:user_groups.view', 11, 1, 1, 0)",
'in-portal:user_groups.add;11;1;0' => "(DEFAULT, 'in-portal:user_groups.add', 11, 1, 1, 0)",
'in-portal:user_groups.edit;11;1;0' => "(DEFAULT, 'in-portal:user_groups.edit', 11, 1, 1, 0)",
'in-portal:user_groups.delete;11;1;0' => "(DEFAULT, 'in-portal:user_groups.delete', 11, 1, 1, 0)",
'in-portal:user_groups.advanced:send_email;11;1;0' => "(DEFAULT, 'in-portal:user_groups.advanced:send_email', 11, 1, 1, 0)",
'in-portal:user_groups.advanced:manage_permissions;11;1;0' => "(DEFAULT, 'in-portal:user_groups.advanced:manage_permissions', 11, 1, 1, 0)",
'in-portal:configure_users.view;11;1;0' => "(DEFAULT, 'in-portal:configure_users.view', 11, 1, 1, 0)",
'in-portal:configure_users.edit;11;1;0' => "(DEFAULT, 'in-portal:configure_users.edit', 11, 1, 1, 0)",
'in-portal:user_email.view;11;1;0' => "(DEFAULT, 'in-portal:user_email.view', 11, 1, 1, 0)",
'in-portal:user_email.edit;11;1;0' => "(DEFAULT, 'in-portal:user_email.edit', 11, 1, 1, 0)",
'in-portal:user_custom.view;11;1;0' => "(DEFAULT, 'in-portal:user_custom.view', 11, 1, 1, 0)",
'in-portal:user_custom.add;11;1;0' => "(DEFAULT, 'in-portal:user_custom.add', 11, 1, 1, 0)",
'in-portal:user_custom.edit;11;1;0' => "(DEFAULT, 'in-portal:user_custom.edit', 11, 1, 1, 0)",
'in-portal:user_custom.delete;11;1;0' => "(DEFAULT, 'in-portal:user_custom.delete', 11, 1, 1, 0)",
'in-portal:user_banlist.view;11;1;0' => "(DEFAULT, 'in-portal:user_banlist.view', 11, 1, 1, 0)",
'in-portal:user_banlist.add;11;1;0' => "(DEFAULT, 'in-portal:user_banlist.add', 11, 1, 1, 0)",
'in-portal:user_banlist.edit;11;1;0' => "(DEFAULT, 'in-portal:user_banlist.edit', 11, 1, 1, 0)",
'in-portal:user_banlist.delete;11;1;0' => "(DEFAULT, 'in-portal:user_banlist.delete', 11, 1, 1, 0)",
'in-portal:reports.view;11;1;0' => "(DEFAULT, 'in-portal:reports.view', 11, 1, 1, 0)",
'in-portal:log_summary.view;11;1;0' => "(DEFAULT, 'in-portal:log_summary.view', 11, 1, 1, 0)",
'in-portal:searchlog.view;11;1;0' => "(DEFAULT, 'in-portal:searchlog.view', 11, 1, 1, 0)",
'in-portal:searchlog.delete;11;1;0' => "(DEFAULT, 'in-portal:searchlog.delete', 11, 1, 1, 0)",
'in-portal:sessionlog.view;11;1;0' => "(DEFAULT, 'in-portal:sessionlog.view', 11, 1, 1, 0)",
'in-portal:sessionlog.delete;11;1;0' => "(DEFAULT, 'in-portal:sessionlog.delete', 11, 1, 1, 0)",
'in-portal:emaillog.view;11;1;0' => "(DEFAULT, 'in-portal:emaillog.view', 11, 1, 1, 0)",
'in-portal:emaillog.delete;11;1;0' => "(DEFAULT, 'in-portal:emaillog.delete', 11, 1, 1, 0)",
'in-portal:visits.view;11;1;0' => "(DEFAULT, 'in-portal:visits.view', 11, 1, 1, 0)",
'in-portal:visits.delete;11;1;0' => "(DEFAULT, 'in-portal:visits.delete', 11, 1, 1, 0)",
'in-portal:configure_general.view;11;1;0' => "(DEFAULT, 'in-portal:configure_general.view', 11, 1, 1, 0)",
'in-portal:configure_general.edit;11;1;0' => "(DEFAULT, 'in-portal:configure_general.edit', 11, 1, 1, 0)",
'in-portal:modules.view;11;1;0' => "(DEFAULT, 'in-portal:modules.view', 11, 1, 1, 0)",
'in-portal:mod_status.view;11;1;0' => "(DEFAULT, 'in-portal:mod_status.view', 11, 1, 1, 0)",
'in-portal:mod_status.edit;11;1;0' => "(DEFAULT, 'in-portal:mod_status.edit', 11, 1, 1, 0)",
'in-portal:mod_status.advanced:approve;11;1;0' => "(DEFAULT, 'in-portal:mod_status.advanced:approve', 11, 1, 1, 0)",
'in-portal:mod_status.advanced:decline;11;1;0' => "(DEFAULT, 'in-portal:mod_status.advanced:decline', 11, 1, 1, 0)",
'in-portal:addmodule.view;11;1;0' => "(DEFAULT, 'in-portal:addmodule.view', 11, 1, 1, 0)",
'in-portal:addmodule.add;11;1;0' => "(DEFAULT, 'in-portal:addmodule.add', 11, 1, 1, 0)",
'in-portal:addmodule.edit;11;1;0' => "(DEFAULT, 'in-portal:addmodule.edit', 11, 1, 1, 0)",
'in-portal:tag_library.view;11;1;0' => "(DEFAULT, 'in-portal:tag_library.view', 11, 1, 1, 0)",
'in-portal:configure_themes.view;11;1;0' => "(DEFAULT, 'in-portal:configure_themes.view', 11, 1, 1, 0)",
'in-portal:configure_themes.add;11;1;0' => "(DEFAULT, 'in-portal:configure_themes.add', 11, 1, 1, 0)",
'in-portal:configure_themes.edit;11;1;0' => "(DEFAULT, 'in-portal:configure_themes.edit', 11, 1, 1, 0)",
'in-portal:configure_themes.delete;11;1;0' => "(DEFAULT, 'in-portal:configure_themes.delete', 11, 1, 1, 0)",
'in-portal:configure_styles.view;11;1;0' => "(DEFAULT, 'in-portal:configure_styles.view', 11, 1, 1, 0)",
'in-portal:configure_styles.add;11;1;0' => "(DEFAULT, 'in-portal:configure_styles.add', 11, 1, 1, 0)",
'in-portal:configure_styles.edit;11;1;0' => "(DEFAULT, 'in-portal:configure_styles.edit', 11, 1, 1, 0)",
'in-portal:configure_styles.delete;11;1;0' => "(DEFAULT, 'in-portal:configure_styles.delete', 11, 1, 1, 0)",
'in-portal:configure_lang.advanced:set_primary;11;1;0' => "(DEFAULT, 'in-portal:configure_lang.advanced:set_primary', 11, 1, 1, 0)",
'in-portal:configure_lang.advanced:import;11;1;0' => "(DEFAULT, 'in-portal:configure_lang.advanced:import', 11, 1, 1, 0)",
'in-portal:configure_lang.advanced:export;11;1;0' => "(DEFAULT, 'in-portal:configure_lang.advanced:export', 11, 1, 1, 0)",
'in-portal:tools.view;11;1;0' => "(DEFAULT, 'in-portal:tools.view', 11, 1, 1, 0)",
'in-portal:backup.view;11;1;0' => "(DEFAULT, 'in-portal:backup.view', 11, 1, 1, 0)",
'in-portal:restore.view;11;1;0' => "(DEFAULT, 'in-portal:restore.view', 11, 1, 1, 0)",
'in-portal:export.view;11;1;0' => "(DEFAULT, 'in-portal:export.view', 11, 1, 1, 0)",
'in-portal:main_import.view;11;1;0' => "(DEFAULT, 'in-portal:main_import.view', 11, 1, 1, 0)",
'in-portal:sql_query.view;11;1;0' => "(DEFAULT, 'in-portal:sql_query.view', 11, 1, 1, 0)",
'in-portal:sql_query.edit;11;1;0' => "(DEFAULT, 'in-portal:sql_query.edit', 11, 1, 1, 0)",
'in-portal:server_info.view;11;1;0' => "(DEFAULT, 'in-portal:server_info.view', 11, 1, 1, 0)",
'in-portal:help.view;11;1;0' => "(DEFAULT, 'in-portal:help.view', 11, 1, 1, 0)",
),
),
'SearchConfig' => Array (
'UniqueField' => 'TableName;FieldName;ModuleName',
'Records' => Array (
'Category;NewItem;In-Portal' => "('Category', 'NewItem', 0, 1, 'lu_fielddesc_category_newitem', 'lu_field_newitem', 'In-Portal', 'la_text_category', 18, DEFAULT, 0, 'boolean', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;PopItem;In-Portal' => "('Category', 'PopItem', 0, 1, 'lu_fielddesc_category_popitem', 'lu_field_popitem', 'In-Portal', 'la_text_category', 19, DEFAULT, 0, 'boolean', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;HotItem;In-Portal' => "('Category', 'HotItem', 0, 1, 'lu_fielddesc_category_hotitem', 'lu_field_hotitem', 'In-Portal', 'la_text_category', 17, DEFAULT, 0, 'boolean', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;MetaDescription;In-Portal' => "('Category', 'MetaDescription', 0, 1, 'lu_fielddesc_category_metadescription', 'lu_field_metadescription', 'In-Portal', 'la_text_category', 16, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;ParentPath;In-Portal' => "('Category', 'ParentPath', 0, 1, 'lu_fielddesc_category_parentpath', 'lu_field_parentpath', 'In-Portal', 'la_text_category', 15, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;ResourceId;In-Portal' => "('Category', 'ResourceId', 0, 1, 'lu_fielddesc_category_resourceid', 'lu_field_resourceid', 'In-Portal', 'la_text_category', 14, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;CreatedById;In-Portal' => "('Category', 'CreatedById', 0, 1, 'lu_fielddesc_category_createdbyid', 'lu_field_createdbyid', 'In-Portal', 'la_text_category', 13, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;CachedNavbar;In-Portal' => "('Category', 'CachedNavbar', 0, 1, 'lu_fielddesc_category_cachednavbar', 'lu_field_cachednavbar', 'In-Portal', 'la_text_category', 12, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;CachedDescendantCatsQty;In-Portal' => "('Category', 'CachedDescendantCatsQty', 0, 1, 'lu_fielddesc_category_cacheddescendantcatsqty', 'lu_field_cacheddescendantcatsqty', 'In-Portal', 'la_text_category', 11, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;MetaKeywords;In-Portal' => "('Category', 'MetaKeywords', 0, 1, 'lu_fielddesc_category_metakeywords', 'lu_field_metakeywords', 'In-Portal', 'la_text_category', 10, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;Priority;In-Portal' => "('Category', 'Priority', 0, 1, 'lu_fielddesc_category_priority', 'lu_field_priority', 'In-Portal', 'la_text_category', 9, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;Status;In-Portal' => "('Category', 'Status', 0, 1, 'lu_fielddesc_category_status', 'lu_field_status', 'In-Portal', 'la_text_category', 7, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;EditorsPick;In-Portal' => "('Category', 'EditorsPick', 0, 1, 'lu_fielddesc_category_editorspick', 'lu_field_editorspick', 'In-Portal', 'la_text_category', 6, DEFAULT, 0, 'boolean', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;CreatedOn;In-Portal' => "('Category', 'CreatedOn', 0, 1, 'lu_fielddesc_category_createdon', 'lu_field_createdon', 'In-Portal', 'la_text_category', 5, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;Description;In-Portal' => "('Category', 'Description', 1, 1, 'lu_fielddesc_category_description', 'lu_field_description', 'In-Portal', 'la_text_category', 4, DEFAULT, 2, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;Name;In-Portal' => "('Category', 'Name', 1, 1, 'lu_fielddesc_category_name', 'lu_field_name', 'In-Portal', 'la_text_category', 3, DEFAULT, 2, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;ParentId;In-Portal' => "('Category', 'ParentId', 0, 1, 'lu_fielddesc_category_parentid', 'lu_field_parentid', 'In-Portal', 'la_text_category', 2, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;CategoryId;In-Portal' => "('Category', 'CategoryId', 0, 1, 'lu_fielddesc_category_categoryid', 'lu_field_categoryid', 'In-Portal', 'la_text_category', 0, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;Modified;In-Portal' => "('Category', 'Modified', 0, 1, 'lu_fielddesc_category_modified', 'lu_field_modified', 'In-Portal', 'la_text_category', 20, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'Category;ModifiedById;In-Portal' => "('Category', 'ModifiedById', 0, 1, 'lu_fielddesc_category_modifiedbyid', 'lu_field_modifiedbyid', 'In-Portal', 'la_text_category', 21, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;PortalUserId;In-Portal' => "('PortalUser', 'PortalUserId', -1, 0, 'lu_fielddesc_user_portaluserid', 'lu_field_portaluserid', 'In-Portal', 'la_text_user', 0, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;Login;In-Portal' => "('PortalUser', 'Login', -1, 0, 'lu_fielddesc_user_login', 'lu_field_login', 'In-Portal', 'la_text_user', 1, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;Password;In-Portal' => "('PortalUser', 'Password', -1, 0, 'lu_fielddesc_user_password', 'lu_field_password', 'In-Portal', 'la_text_user', 2, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;tz;In-Portal' => "('PortalUser', 'tz', -1, 0, 'lu_fielddesc_user_tz', 'lu_field_tz', 'In-Portal', 'la_text_user', 17, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;dob;In-Portal' => "('PortalUser', 'dob', -1, 0, 'lu_fielddesc_user_dob', 'lu_field_dob', 'In-Portal', 'la_text_user', 16, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;Modified;In-Portal' => "('PortalUser', 'Modified', -1, 0, 'lu_fielddesc_user_modified', 'lu_field_modified', 'In-Portal', 'la_text_user', 15, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;Status;In-Portal' => "('PortalUser', 'Status', -1, 0, 'lu_fielddesc_user_status', 'lu_field_status', 'In-Portal', 'la_text_user', 14, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;ResourceId;In-Portal' => "('PortalUser', 'ResourceId', -1, 0, 'lu_fielddesc_user_resourceid', 'lu_field_resourceid', 'In-Portal', 'la_text_user', 13, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;Country;In-Portal' => "('PortalUser', 'Country', -1, 0, 'lu_fielddesc_user_country', 'lu_field_country', 'In-Portal', 'la_text_user', 12, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;Zip;In-Portal' => "('PortalUser', 'Zip', -1, 0, 'lu_fielddesc_user_zip', 'lu_field_zip', 'In-Portal', 'la_text_user', 11, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;State;In-Portal' => "('PortalUser', 'State', -1, 0, 'lu_fielddesc_user_state', 'lu_field_state', 'In-Portal', 'la_text_user', 10, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;City;In-Portal' => "('PortalUser', 'City', -1, 0, 'lu_fielddesc_user_city', 'lu_field_city', 'In-Portal', 'la_text_user', 9, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;Street;In-Portal' => "('PortalUser', 'Street', -1, 0, 'lu_fielddesc_user_street', 'lu_field_street', 'In-Portal', 'la_text_user', 8, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;Phone;In-Portal' => "('PortalUser', 'Phone', -1, 0, 'lu_fielddesc_user_phone', 'lu_field_phone', 'In-Portal', 'la_text_user', 7, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;CreatedOn;In-Portal' => "('PortalUser', 'CreatedOn', -1, 0, 'lu_fielddesc_user_createdon', 'lu_field_createdon', 'In-Portal', 'la_text_user', 6, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;Email;In-Portal' => "('PortalUser', 'Email', -1, 0, 'lu_fielddesc_user_email', 'lu_field_email', 'In-Portal', 'la_text_user', 5, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;LastName;In-Portal' => "('PortalUser', 'LastName', -1, 0, 'lu_fielddesc_user_lastname', 'lu_field_lastname', 'In-Portal', 'la_text_user', 4, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
'PortalUser;FirstName;In-Portal' => "('PortalUser', 'FirstName', -1, 0, 'lu_fielddesc_user_firstname', 'lu_field_firstname', 'In-Portal', 'la_text_user', 3, DEFAULT, 0, 'text', NULL, NULL, NULL, NULL, NULL, NULL, NULL)",
),
),
'StatItem' => Array (
'UniqueField' => 'Module;ListLabel',
'Records' => Array (
'In-Portal;la_prompt_ActiveCategories' => "(DEFAULT, 'In-Portal', 'SELECT count(*) FROM <%prefix%>Category WHERE Status=1 ', NULL, 'la_prompt_ActiveCategories', '0', '1')",
'In-Portal;la_prompt_ActiveUsers' => "(DEFAULT, 'In-Portal', 'SELECT count(*) FROM <%prefix%>PortalUser WHERE Status=1 ', NULL, 'la_prompt_ActiveUsers', '0', '1')",
'In-Portal;la_prompt_CurrentSessions' => "(DEFAULT, 'In-Portal', 'SELECT count(*) FROM <%prefix%>UserSession', NULL, 'la_prompt_CurrentSessions', '0', '1')",
'In-Portal;la_prompt_TotalCategories' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) as CategoryCount FROM <%prefix%>Category', NULL, 'la_prompt_TotalCategories', 0, 2)",
'In-Portal;la_prompt_ActiveCategories' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS ActiveCategories FROM <%prefix%>Category WHERE Status = 1', NULL, 'la_prompt_ActiveCategories', 0, 2)",
'In-Portal;la_prompt_PendingCategories' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS PendingCategories FROM <%prefix%>Category WHERE Status = 2', NULL, 'la_prompt_PendingCategories', 0, 2)",
'In-Portal;la_prompt_DisabledCategories' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS DisabledCategories FROM <%prefix%>Category WHERE Status = 0', NULL, 'la_prompt_DisabledCategories', 0, 2)",
'In-Portal;la_prompt_NewCategories' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS NewCategories FROM <%prefix%>Category WHERE (NewItem = 1) OR ( (UNIX_TIMESTAMP() - CreatedOn) <= <%m:config name=\"Category_DaysNew\"%>*86400 AND (NewItem = 2) )', NULL, 'la_prompt_NewCategories', 0, 2)",
'In-Portal;la_prompt_CategoryEditorsPick' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) FROM <%prefix%>Category WHERE EditorsPick = 1', NULL, 'la_prompt_CategoryEditorsPick', 0, 2)",
'In-Portal;la_prompt_NewestCategoryDate' => "(DEFAULT, 'In-Portal', 'SELECT <%m:post_format field=\"MAX(CreatedOn)\" type=\"date\"%> FROM <%prefix%>Category', NULL, 'la_prompt_NewestCategoryDate', 0, 2)",
'In-Portal;la_prompt_LastCategoryUpdate' => "(DEFAULT, 'In-Portal', 'SELECT <%m:post_format field=\"MAX(Modified)\" type=\"date\"%> FROM <%prefix%>Category', NULL, 'la_prompt_LastCategoryUpdate', 0, 2)",
'In-Portal;la_prompt_TopicsUsers' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS TotalUsers FROM <%prefix%>PortalUser', NULL, 'la_prompt_TopicsUsers', 0, 2)",
'In-Portal;la_prompt_UsersActive' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS ActiveUsers FROM <%prefix%>PortalUser WHERE Status = 1', NULL, 'la_prompt_UsersActive', 0, 2)",
'In-Portal;la_prompt_UsersPending' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS PendingUsers FROM <%prefix%>PortalUser WHERE Status = 2', NULL, 'la_prompt_UsersPending', 0, 2)",
'In-Portal;la_prompt_UsersDisabled' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS DisabledUsers FROM <%prefix%>PortalUser WHERE Status = 0', NULL, 'la_prompt_UsersDisabled', 0, 2)",
'In-Portal;la_prompt_NewestUserDate' => "(DEFAULT, 'In-Portal', 'SELECT <%m:post_format field=\"MAX(CreatedOn)\" type=\"date\"%> FROM <%prefix%>PortalUser', NULL, 'la_prompt_NewestUserDate', 0, 2)",
'In-Portal;la_prompt_UsersUniqueCountries' => "(DEFAULT, 'In-Portal', 'SELECT COUNT( DISTINCT LOWER( Country ) ) FROM <%prefix%>PortalUser WHERE LENGTH(Country) > 0', NULL, 'la_prompt_UsersUniqueCountries', 0, 2)",
'In-Portal;la_prompt_UsersUniqueStates' => "(DEFAULT, 'In-Portal', 'SELECT COUNT( DISTINCT LOWER( State ) ) FROM <%prefix%>PortalUser WHERE LENGTH(State) > 0', NULL, 'la_prompt_UsersUniqueStates', 0, 2)",
'In-Portal;la_prompt_TotalUserGroups' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS TotalUserGroups FROM <%prefix%>PortalGroup', NULL, 'la_prompt_TotalUserGroups', 0, 2)",
'In-Portal;la_prompt_BannedUsers' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS BannedUsers FROM <%prefix%>PortalUser WHERE IsBanned = 1', NULL, 'la_prompt_BannedUsers', 0, 2)",
'In-Portal;la_prompt_NonExpiredSessions' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS NonExipedSessions FROM <%prefix%>UserSession WHERE Status = 1', NULL, 'la_prompt_NonExpiredSessions', 0, 2)",
'In-Portal;la_prompt_ThemeCount' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS ThemeCount FROM <%prefix%>Theme', NULL, 'la_prompt_ThemeCount', 0, 2)",
'In-Portal;la_prompt_RegionsCount' => "(DEFAULT, 'In-Portal', 'SELECT COUNT(*) AS RegionsCount FROM <%prefix%>Language', NULL, 'la_prompt_RegionsCount', 0, 2)",
'In-Portal;la_prompt_TablesCount' => "(DEFAULT, 'In-Portal', '<%m:sql_action sql=\"SHOW+TABLES\" action=\"COUNT\" field=\"*\"%>', NULL, 'la_prompt_TablesCount', 0, 2)",
'In-Portal;la_prompt_RecordsCount' => "(DEFAULT, 'In-Portal', '<%m:sql_action sql=\"SHOW+TABLE+STATUS\" action=\"SUM\" field=\"Rows\"%>', NULL, 'la_prompt_RecordsCount', 0, 2)",
'In-Portal;la_prompt_SystemFileSize' => "(DEFAULT, 'In-Portal', '<%m:custom_action sql=\"empty\" action=\"SysFileSize\"%>', NULL, 'la_prompt_SystemFileSize', 0, 2)",
'In-Portal;la_prompt_DataSize' => "(DEFAULT, 'In-Portal', '<%m:sql_action sql=\"SHOW+TABLE+STATUS\" action=\"SUM\" format_as=\"file\" field=\"Data_length\"%>', NULL, 'la_prompt_DataSize', 0, 2)",
),
),
'StylesheetSelectors' => Array (
'UniqueField' => 'SelectorId',
'Records' => Array (
'169' => "(169, 8, 'Calendar''s selected days', '.calendar tbody .selected', 'a:0:{}', '', 1, 'font-weight: bold;\\\r\\nbackground-color: #9ED7ED;\\r\\nborder: 1px solid #83B2C5;', 0)",
'118' => "(118, 8, 'Data grid row', 'td.block-data-row', 'a:0:{}', '', 2, 'border-bottom: 1px solid #cccccc;', 48)",
'81' => "(81, 8, '\"More\" link', 'a.link-more', 'a:0:{}', '', 2, 'text-decoration: underline;', 64)",
'88' => "(88, 8, 'Block data, separated rows', 'td.block-data-grid', 'a:0:{}', '', 2, 'border: 1px solid #cccccc;', 48)",
'42' => "(42, 8, 'Block Header', 'td.block-header', 'a:4:{s:5:\"color\";s:16:\"rgb(0, 159, 240)\";s:9:\"font-size\";s:4:\"20px\";s:11:\"font-weight\";s:6:\"normal\";s:7:\"padding\";s:3:\"5px\";}', 'Block Header', 1, 'font-family: Verdana, Helvetica, sans-serif;', 0)",
'76' => "(76, 8, 'Navigation bar menu', 'tr.head-nav td', 'a:0:{}', '', 1, 'vertical-align: middle;', 0)",
'48' => "(48, 8, 'Block data', 'td.block-data', 'a:2:{s:9:\"font-size\";s:5:\"12px;\";s:7:\"padding\";s:3:\"5px\";}', '', 1, '', 0)",
'78' => "(78, 8, 'Body main style', 'body', 'a:0:{}', '', 1, 'padding: 0px; \\r\\nbackground-color: #ffffff; \\r\\nfont-family: arial, verdana, helvetica; \\r\\nfont-size: small;\\r\\nwidth: auto;\\r\\nmargin: 0px;', 0)",
'58' => "(58, 8, 'Main table', 'table.main-table', 'a:0:{}', '', 1, 'width: 770px;\\r\\nmargin: 0px;\\r\\n/*table-layout: fixed;*/', 0)",
'79' => "(79, 8, 'Block: header of data block', 'span.block-data-grid-header', 'a:0:{}', '', 1, 'font-family: Arial, Helvetica, sans-serif;\\r\\ncolor: #009DF6;\\r\\nfont-size: 12px;\\r\\nfont-weight: bold;\\r\\nbackground-color: #E6EEFF;\\r\\npadding: 6px;\\r\\nwhite-space: nowrap;', 0)",
'64' => "(64, 8, 'Link', 'a', 'a:0:{}', '', 1, '', 0)",
'46' => "(46, 8, 'Product title link', 'a.link-product1', 'a:0:{}', 'Product title link', 1, 'color: #62A1DE;\\r\\nfont-size: 14px;\\r\\nfont-weight: bold;\\r\\nline-height: 20px;\\r\\npadding-bottom: 10px;', 0)",
'75' => "(75, 8, 'Copy of Main path link', 'table.main-path td a:hover', 'a:0:{}', '', 1, 'color: #ffffff;', 0)",
'160' => "(160, 8, 'Current item in navigation bar', '.checkout-step-current', 'a:0:{}', '', 1, 'color: #A20303;\\r\\nfont-weight: bold;', 0)",
'51' => "(51, 8, 'Right block data', 'td.right-block-data', 'a:1:{s:9:\"font-size\";s:4:\"11px\";}', '', 2, 'padding: 7px;\\r\\nbackground: #e3edf6 url(\"/in-commerce4/themes/default/img/bgr_login.jpg\") repeat-y scroll left top;\\r\\nborder-bottom: 1px solid #64a1df;', 48)",
'67' => "(67, 8, 'Pagination bar: text', 'table.block-pagination td', 'a:3:{s:5:\"color\";s:7:\"#8B898B\";s:9:\"font-size\";s:4:\"12px\";s:11:\"font-weight\";s:6:\"normal\";}', '', 1, '', 0)",
'45' => "(45, 8, 'Category link', 'a.subcat', 'a:0:{}', 'Category link', 1, 'color: #2069A4', 0)",
'68' => "(68, 8, 'Pagination bar: link', 'table.block-pagination td a', 'a:3:{s:5:\"color\";s:7:\"#8B898B\";s:9:\"font-size\";s:5:\"12px;\";s:11:\"font-weight\";s:6:\"normal\";}', '', 1, '', 0)",
'69' => "(69, 8, 'Product description in product list', '.product-list-description', 'a:2:{s:5:\"color\";s:7:\"#8B898B\";s:9:\"font-size\";s:4:\"12px\";}', '', 1, '', 0)",
'73' => "(73, 8, 'Main path link', 'table.main-path td a', 'a:0:{}', '', 1, 'color: #d5e231;', 0)",
'83' => "(83, 8, 'Product title link in list (shopping cart)', 'a.link-product-cart', 'a:0:{}', 'Product title link', 1, 'color: #18559C;\\r\\nfont-size: 12px;\\r\\nfont-weight: bold;\\r\\ntext-decoration: none;\\r\\n\\r\\n', 0)",
'72' => "(72, 8, 'Main path block text', 'table.main-path td', 'a:0:{}', '', 1, 'color: #ffffff;\\r\\nfont-size: 10px;\\r\\nfont-weight: normal;\\r\\npadding: 1px;\\r\\n', 0)",
'61' => "(61, 8, 'Block: header of data table', 'td.block-data-grid-header', 'a:6:{s:4:\"font\";s:28:\"Arial, Helvetica, sans-serif\";s:5:\"color\";s:7:\"#009DF6\";s:9:\"font-size\";s:4:\"12px\";s:11:\"font-weight\";s:4:\"bold\";s:16:\"background-color\";s:7:\"#E6EEFF\";s:7:\"padding\";s:3:\"6px\";}', '', 1, 'white-space: nowrap;\\r\\npadding-left: 10px;\\r\\n/*\\r\\nbackground-image: url(/in-commerce4/themes/default/img/bullet1.gif);\\r\\nbackground-position: 10px 12px;\\r\\nbackground-repeat: no-repeat;\\r\\n*/', 0)",
'65' => "(65, 8, 'Link in product list additional row', 'td.product-list-additional a', 'a:1:{s:5:\"color\";s:7:\"#8B898B\";}', '', 2, '', 64)",
'55' => "(55, 8, 'Main table, left column', 'td.main-column-left', 'a:0:{}', '', 1, 'width:180px;\\r\\nborder: 1px solid #62A1DE;\\r\\nborder-top: 0px;', 0)",
'70' => "(70, 8, 'Product title link in list (category)', 'a.link-product-category', 'a:0:{}', 'Product title link', 1, 'color: #18559C;\\r\\nfont-size: 12px;\\r\\nfont-weight: bold;\\r\\ntext-decoration: none;\\r\\n\\r\\n', 0)",
'66' => "(66, 8, 'Pagination bar block', 'table.block-pagination', 'a:0:{}', '', 1, '', 0)",
'49' => "(49, 8, 'Bulleted list inside block', 'td.block-data ul li', 'a:0:{}', '', 1, ' list-style-image: url(/in-commerce4/themes/default/img/bullet2.gif);\\r\\n margin-bottom: 10px;\\r\\n font-size: 11px;', 0)",
'87' => "(87, 8, 'Cart item input form element', 'td.cart-item-atributes input', 'a:0:{}', '', 1, 'border: 1px solid #7BB2E6;', 0)",
'119' => "(119, 8, 'Data grid row header', 'td.block-data-row-hdr', 'a:0:{}', 'Used in order preview', 2, 'background-color: #eeeeee;\\r\\nborder-bottom: 1px solid #dddddd;\\r\\nborder-top: 1px solid #cccccc;\\r\\nfont-weight: bold;', 48)",
'82' => "(82, 8, '\"More\" link image', 'a.link-more img', 'a:0:{}', '', 2, 'text-decoration: none;\\r\\npadding-left: 5px;', 64)",
'63' => "(63, 8, 'Additional info under product description in list', 'td.product-list-additional', 'a:5:{s:5:\"color\";s:7:\"#8B898B\";s:9:\"font-size\";s:4:\"11px\";s:11:\"font-weight\";s:6:\"normal\";s:10:\"border-top\";s:18:\"1px dashed #8B898B\";s:13:\"border-bottom\";s:18:\"1px dashed #8B898B\";}', '', 2, '', 48)",
'43' => "(43, 8, 'Block', 'table.block', 'a:2:{s:16:\"background-color\";s:7:\"#E3EEF9\";s:6:\"border\";s:17:\"1px solid #64A1DF\";}', 'Block', 1, 'border: 0; \\r\\nmargin-bottom: 1px;\\r\\nwidth: 100%;', 0)",
'84' => "(84, 8, 'Cart item cell', 'td.cart-item', 'a:0:{}', '', 1, 'background-color: #F6FAFF;\\r\\nborder-left: 1px solid #ffffff;\\r\\nborder-bottom: 1px solid #ffffff;\\r\\npadding: 4px;', 0)",
'57' => "(57, 8, 'Main table, right column', 'td.main-column-right', 'a:0:{}', '', 1, 'width:220px;\\r\\nborder: 1px solid #62A1DE;\\r\\nborder-top: 0px;', 0)",
'161' => "(161, 8, 'Block for sub categories', 'td.block-data-subcats', 'a:0:{}', '', 2, ' background: #FFFFFF\\r\\nurl(/in-commerce4/themes/default/in-commerce/img/bgr_categories.jpg);\\r\\n background-repeat: no-repeat;\\r\\n background-position: top right;\\r\\nborder-bottom: 5px solid #DEEAFF;\\r\\npadding-left: 10px;', 48)",
'77' => "(77, 8, 'Left block header', 'td.left-block-header', 'a:0:{}', '', 2, 'font-family : verdana, helvetica, sans-serif;\\r\\ncolor : #ffffff;\\r\\nfont-size : 12px;\\r\\nfont-weight : bold;\\r\\ntext-decoration : none;\\r\\nbackground-color: #64a1df;\\r\\npadding: 5px;\\r\\npadding-left: 7px;', 42)",
'80' => "(80, 8, 'Right block data - text', 'td.right-block-data td', 'a:1:{s:9:\"font-size\";s:5:\"11px;\";}', '', 2, '', 48)",
'53' => "(53, 8, 'Right block header', 'td.right-block-header', 'a:0:{}', '', 2, 'font-family : verdana, helvetica, sans-serif;\\r\\ncolor : #ffffff;\\r\\nfont-size : 12px;\\r\\nfont-weight : bold;\\r\\ntext-decoration : none;\\r\\nbackground-color: #64a1df;\\r\\npadding: 5px;\\r\\npadding-left: 7px;', 42)",
'85' => "(85, 8, 'Cart item cell with attributes', 'td.cart-item-attributes', 'a:0:{}', '', 1, 'background-color: #E6EEFF;\\r\\nborder-left: 1px solid #ffffff;\\r\\nborder-bottom: 1px solid #ffffff;\\r\\npadding: 4px;\\r\\ntext-align: center;\\r\\nvertical-align: middle;\\r\\nfont-size: 12px;\\r\\nfont-weight: normal;', 0)",
'86' => "(86, 8, 'Cart item cell with name', 'td.cart-item-name', 'a:0:{}', '', 1, 'background-color: #F6FAFF;\\r\\nborder-left: 1px solid #ffffff;\\r\\nborder-bottom: 1px solid #ffffff;\\r\\npadding: 3px;', 0)",
'47' => "(47, 8, 'Block content of featured product', 'td.featured-block-data', 'a:0:{}', '', 1, 'font-family: Arial,Helvetica,sans-serif;\\r\\nfont-size: 12px;', 0)",
'56' => "(56, 8, 'Main table, middle column', 'td.main-column-center', 'a:0:{}', '', 1, '\\r\\n', 0)",
'50' => "(50, 8, 'Product title link in list', 'a.link-product2', 'a:0:{}', 'Product title link', 1, 'color: #62A1DE;\\r\\nfont-size: 12px;\\r\\nfont-weight: bold;\\r\\ntext-decoration: none;\\r\\n\\r\\n', 0)",
'71' => "(71, 8, 'Main path block', 'table.main-path', 'a:0:{}', '', 1, 'background: #61b0ec url(\"/in-commerce4/themes/default/img/bgr_path.jpg\") repeat-y scroll left top;\\r\\nwidth: 100%;\\r\\nmargin-bottom: 1px;\\r\\nmargin-right: 1px; \\r\\nmargin-left: 1px;', 0)",
'62' => "(62, 8, 'Block: columns header for data table', 'table.block-no-border th', 'a:6:{s:4:\"font\";s:28:\"Arial, Helvetica, sans-serif\";s:5:\"color\";s:7:\"#18559C\";s:9:\"font-size\";s:4:\"11px\";s:11:\"font-weight\";s:4:\"bold\";s:16:\"background-color\";s:7:\"#B4D2EE\";s:7:\"padding\";s:3:\"6px\";}', '', 1, 'text-align: left;', 0)",
'59' => "(59, 8, 'Block without border', 'table.block-no-border', 'a:0:{}', '', 1, 'border: 0px; \\r\\nmargin-bottom: 10px;\\r\\nwidth: 100%;', 0)",
'74' => "(74, 8, 'Main path language selector cell', 'td.main-path-language', 'a:0:{}', '', 1, 'vertical-align: middle;\\r\\ntext-align: right;\\r\\npadding-right: 6px;', 0)",
'171' => "(171, 8, 'Calendar''s highlighted day', '.calendar tbody .hilite', 'a:0:{}', '', 1, 'background-color: #f6f6f6;\\r\\nborder: 1px solid #83B2C5 !important;', 0)",
'175' => "(175, 8, 'Calendar''s days', '.calendar tbody .day', 'a:0:{}', '', 1, 'text-align: right;\\r\\npadding: 2px 4px 2px 2px;\\r\\nwidth: 2em;\\r\\nborder: 1px solid #fefefe;', 0)",
'170' => "(170, 8, 'Calendar''s weekends', '.calendar .weekend', 'a:0:{}', '', 1, 'color: #990000;', 0)",
'173' => "(173, 8, 'Calendar''s control buttons', '.calendar .calendar_button', 'a:0:{}', '', 1, 'color: black;\\r\\nfont-size: 12px;\\r\\nbackground-color: #eeeeee;', 0)",
'174' => "(174, 8, 'Calendar''s day names', '.calendar thead .name', 'a:0:{}', '', 1, 'background-color: #DEEEF6;\\r\\nborder-bottom: 1px solid #000000;', 0)",
'172' => "(172, 8, 'Calendar''s top and bottom titles', '.calendar .title', 'a:0:{}', '', 1, 'color: #FFFFFF;\\r\\nbackground-color: #62A1DE;\\r\\nborder: 1px solid #107DC5;\\r\\nborder-top: 0px;\\r\\npadding: 1px;', 0)",
'60' => "(60, 8, 'Block header for featured product', 'td.featured-block-header', 'a:0:{}', '', 2, '\\r\\n', 42)",
'54' => "(54, 8, 'Right block', 'table.right-block', 'a:0:{}', '', 2, 'background-color: #E3EEF9;\\r\\nborder: 0px;\\r\\nwidth: 100%;', 43)",
'44' => "(44, 8, 'Block content', 'td.block-data-big', 'a:0:{}', 'Block content', 1, ' background: #DEEEF6\\r\\nurl(/in-commerce4/themes/default/img/menu_bg.gif);\\r\\n background-repeat: no-repeat;\\r\\n background-position: top right;\\r\\n', 0)",
),
),
'Stylesheets' => Array (
'UniqueField' => 'StylesheetId',
'Records' => Array (
'8' => "(8, 'Default', 'In-Portal Default Theme', '', 1124952555, 1)",
),
),
'Counters' => Array (
'UniqueField' => 'Name',
'Records' => Array (
'members_count' => "(DEFAULT, 'members_count', 'SELECT COUNT(*) FROM <%PREFIX%>PortalUser WHERE Status = 1', NULL , NULL , '3600', '0', '|PortalUser|')",
'members_online' => "(DEFAULT, 'members_online', 'SELECT COUNT(*) FROM <%PREFIX%>UserSession WHERE PortalUserId > 0', NULL , NULL , '3600', '0', '|UserSession|')",
'guests_online' => "(DEFAULT, 'guests_online', 'SELECT COUNT(*) FROM <%PREFIX%>UserSession WHERE PortalUserId <= 0', NULL , NULL , '3600', '0', '|UserSession|')",
'users_online' => "(DEFAULT, 'users_online', 'SELECT COUNT(*) FROM <%PREFIX%>UserSession', NULL , NULL , '3600', '0', '|UserSession|')",
),
),
);
// check & insert if not found defined before data
foreach ($data as $table_name => $table_info) {
$unique_fields = explode(';', $table_info['UniqueField']);
foreach ($table_info['Records'] as $unique_value => $insert_sql) {
$unique_values = explode(';', $unique_value);
$where_clause = Array ();
foreach ($unique_fields as $field_index => $unique_field) {
$where_clause[] = $unique_field . ' = ' . $this->Conn->qstr($unique_values[$field_index]);
}
$sql = 'SELECT ' . implode(', ', $unique_fields) . '
FROM ' . TABLE_PREFIX . $table_name . '
WHERE (' . implode(') AND (', $where_clause) . ')';
$found = $this->Conn->GetRow($sql);
if ($found) {
$found = implode(';', $found);
}
if ($found != $unique_value) {
$this->Conn->Query('INSERT INTO ' . TABLE_PREFIX . $table_name . ' VALUES ' . $insert_sql);
}
}
}
}
/**
* Removes duplicate phrases per language basis (created during proj-base and in-portal shared installation)
*
*/
function _removeDuplicatePhrases()
{
$config = $this->Application->getUnitConfig('phrases');
$id_field = $config->getIDField();
$table_name = $config->getTableName();
$sql = 'SELECT LanguageId, Phrase, MIN(LastChanged) AS LastChanged, COUNT(*) AS DupeCount
FROM ' . $table_name . '
GROUP BY LanguageId, Phrase
HAVING COUNT(*) > 1';
$duplicate_phrases = $this->Conn->Query($sql);
foreach ($duplicate_phrases as $phrase_record) {
- // 1. keep phrase, that was added first, because it is selected in PhrasesCache::LoadPhraseByLabel
+ // 1. keep phrase, that was added first, because it is selected in kPhraseCache::LoadPhraseByLabel.
$where_clause = Array (
'LanguageId = ' . $phrase_record['LanguageId'],
'Phrase = ' . $this->Conn->qstr($phrase_record['Phrase']),
'LastChanged' . ' = ' . $phrase_record['LastChanged'],
);
$sql = 'SELECT ' . $id_field . '
FROM ' . $table_name . '
WHERE (' . implode(') AND (', $where_clause) . ')';
$phrase_id = $this->Conn->GetOne($sql);
// 2. delete all other duplicates
$where_clause = Array (
'LanguageId = ' . $phrase_record['LanguageId'],
'Phrase = ' . $this->Conn->qstr($phrase_record['Phrase']),
$id_field . ' <> ' . $phrase_id,
);
$sql = 'DELETE FROM ' . $table_name . '
WHERE (' . implode(') AND (', $where_clause) . ')';
$this->Conn->Query($sql);
}
}
function _moveDatabaseFolders()
{
// Tables: PageContent, Images
if ($this->Conn->TableFound('PageContent', true)) {
// 1. replaces "/kernel/user_files/" references in content blocks
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$languages = $ml_helper->getLanguages();
$replace_sql = '%1$s = REPLACE(%1$s, "/kernel/user_files/", "/system/user_files/")';
$update_sqls = Array ();
foreach ($languages as $language_id) {
$update_sqls[] = sprintf($replace_sql, 'l' . $language_id . '_Content');
}
if ($update_sqls) {
$sql = 'UPDATE ' . TABLE_PREFIX . 'PageContent
SET ' . implode(', ', $update_sqls);
$this->Conn->Query($sql);
}
}
// 2. replace path of images uploaded via "Images" tab of category items
$this->_replaceImageFolder('/kernel/images/', '/system/images/');
// 3. replace path of images uploaded via "Images" tab of category items (when badly formatted)
$this->_replaceImageFolder('kernel/images/', 'system/images/');
// 4. replace images uploaded via "In-Bulletin -> Emoticons" section
$this->_replaceImageFolder('in-bulletin/images/emoticons/', 'system/images/emoticons/');
// 5. update backup path in config
$this->_toolkit->saveConfigValues(
Array (
'Backup_Path' => FULL_PATH . '/system/backupdata'
)
);
}
/**
* Replaces mentions of "/kernel/images" folder in Images table
*
* @param string $from
* @param string $to
*/
function _replaceImageFolder($from, $to)
{
$replace_sql = '%1$s = REPLACE(%1$s, "' . $from . '", "' . $to . '")';
$sql = 'UPDATE ' . TABLE_PREFIX . 'Images
SET ' . sprintf($replace_sql, 'ThumbPath') . ', ' . sprintf($replace_sql, 'LocalPath');
$this->Conn->Query($sql);
}
/**
* Update colors in skin (only if they were not changed manually)
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_5_0_0($mode)
{
if ($mode == 'before') {
$this->_removeDuplicatePhrases(); // because In-Commerce & In-Link share some phrases with Proj-CMS
$this->_createProjCMSTables();
$this->_addMissingConfigurationVariables();
}
if ($mode == 'after') {
$this->_fixSkinColors();
$this->_restructureCatalog();
$this->_sortImages();
// $this->_sortConfigurationVariables('In-Portal', 'in-portal:configure_general');
// $this->_sortConfigurationVariables('In-Portal', 'in-portal:configure_advanced');
}
}
function _sortConfigurationVariables($module, $section)
{
$sql = 'SELECT ca.heading, cv.VariableName
FROM ' . TABLE_PREFIX . 'ConfigurationAdmin ca
LEFT JOIN ' . TABLE_PREFIX . 'ConfigurationValues cv USING(VariableName)
WHERE (cv.ModuleOwner = ' . $this->Conn->qstr($module) . ') AND (cv.Section = ' . $this->Conn->qstr($section) . ')
ORDER BY ca.DisplayOrder asc, ca.GroupDisplayOrder asc';
$variables = $this->Conn->GetCol($sql, 'VariableName');
if (!$variables) {
return ;
}
$variables = $this->_groupRecords($variables);
$group_number = 0;
$variable_order = 1;
$prev_heading = '';
foreach ($variables as $variable_name => $variable_heading) {
if ($prev_heading != $variable_heading) {
$group_number++;
$variable_order = 1;
}
$sql = 'UPDATE ' . TABLE_PREFIX . 'ConfigurationAdmin
SET DisplayOrder = ' . $this->Conn->qstr($group_number * 10 + $variable_order / 100) . '
WHERE VariableName = ' . $this->Conn->qstr($variable_name);
$this->Conn->Query($sql);
$variable_order++;
$prev_heading = $variable_heading;
}
}
/**
* Group list records by header, saves internal order in group
*
* @param Array $variables
* @return Array
*/
function _groupRecords($variables)
{
$sorted = Array();
foreach ($variables as $variable_name => $variable_heading) {
$sorted[$variable_heading][] = $variable_name;
}
$variables = Array();
foreach ($sorted as $heading => $heading_records) {
foreach ($heading_records as $variable_name) {
$variables[$variable_name] = $heading;
}
}
return $variables;
}
/**
* Returns module root category
*
* @param string $module_name
* @param string $module_prefix
* @return int
*/
function _getRootCategory($module_name, $module_prefix)
{
// don't cache anything here (like in static variables), because database value is changed on the fly !!!
$sql = 'SELECT RootCat
FROM ' . TABLE_PREFIX . 'Modules
WHERE LOWER(Name) = ' . $this->Conn->qstr( strtolower($module_name) );
$root_category = $this->Conn->GetOne($sql);
// put to cache too, because CategoriesEventHandler::_prepareAutoPage uses kApplication::findModule
$this->Application->ModuleInfo[$module_name]['Name'] = $module_name;
$this->Application->ModuleInfo[$module_name]['RootCat'] = $root_category;
$this->Application->ModuleInfo[$module_name]['Var'] = $module_prefix;
return $root_category;
}
/**
* Move all categories (except "Content") from "Home" to "Content" category and hide them from menu
*
*/
function _restructureCatalog()
{
$root_category = $this->_getRootCategory('Core', 'adm');
$sql = 'SELECT CategoryId
FROM ' . TABLE_PREFIX . 'Category
WHERE ParentId = 0 AND CategoryId <> ' . $root_category;
$top_categories = $this->Conn->GetCol($sql);
if ($top_categories) {
// hide all categories located outside "Content" category from menu
$sql = 'UPDATE ' . TABLE_PREFIX . 'Category
SET IsMenu = 0
WHERE (ParentPath LIKE "|' . implode('|%") OR (ParentPath LIKE "|', $top_categories) . '|%")';
$this->Conn->Query($sql);
// move all top level categories under "Content" category and make them visible in menu
$sql = 'UPDATE ' . TABLE_PREFIX . 'Category
SET IsMenu = 1, ParentId = ' . $root_category . '
WHERE ParentId = 0 AND CategoryId <> ' . $root_category;
$this->Conn->Query($sql);
}
// make sure, that all categories have valid value for Priority field
$priority_helper = $this->Application->recallObject('PriorityHelper');
/* @var $priority_helper kPriorityHelper */
$event = new kEvent('c:OnListBuild');
// update all categories, because they are all under "Content" category now
$sql = 'SELECT CategoryId
FROM ' . TABLE_PREFIX . 'Category';
$categories = $this->Conn->GetCol($sql);
foreach ($categories as $category_id) {
$priority_helper->recalculatePriorities($event, 'ParentId = ' . $category_id);
}
// create initial theme structure in Category table
$this->_toolkit->rebuildThemes();
// make sure, that all system templates have ThemeId set (only possible during platform project upgrade)
$sql = 'SELECT ThemeId
FROM ' . TABLE_PREFIX . 'Theme
WHERE PrimaryTheme = 1';
$primary_theme_id = $this->Conn->GetOne($sql);
if ($primary_theme_id) {
$sql = 'UPDATE ' . TABLE_PREFIX . 'Category
SET ThemeId = ' . $primary_theme_id . '
WHERE IsSystem = 1 AND ThemeId = 0';
$this->Conn->Query($sql);
}
}
/**
* Changes skin colors to match new ones (only in case, when they match default values)
*
*/
function _fixSkinColors()
{
$skin = $this->Application->recallObject('skin', null, Array ('skip_autoload' => 1));
/* @var $skin kDBItem */
$skin->Load(1, 'IsPrimary');
if ($skin->isLoaded()) {
$skin_options = unserialize( $skin->GetDBField('Options') );
$changes = Array (
// option: from -> to
'HeadBgColor' => Array ('#1961B8', '#007BF4'),
'HeadBarColor' => Array ('#FFFFFF', '#000000'),
'HeadColor' => Array ('#CCFF00', '#FFFFFF'),
'TreeColor' => Array ('#006F99', '#000000'),
'TreeHoverColor' => Array ('', '#009FF0'),
'TreeHighHoverColor' => Array ('', '#FFFFFF'),
'TreeHighBgColor' => Array ('#4A92CE', '#4A92CE'),
'TreeBgColor' => Array ('#FFFFFF', '#DCECF6'),
);
$can_change = true;
foreach ($changes as $option_name => $change) {
list ($change_from, $change_to) = $change;
$can_change = $can_change && ($change_from == $skin_options[$option_name]['Value']);
if ($can_change) {
$skin_options[$option_name]['Value'] = $change_to;
}
}
if ($can_change) {
$skin->SetDBField('Options', serialize($skin_options));
$skin->Update();
$skin_helper = $this->Application->recallObject('SkinHelper');
/* @var $skin_helper SkinHelper */
$skin_file = $skin_helper->getSkinPath();
if (file_exists($skin_file)) {
unlink($skin_file);
}
}
}
}
/**
* 1. Set root category not to generate filename automatically and hide it from catalog
* 2. Hide root category of In-Edit and set it's fields
*
* @param int $category_id
*/
function _resetRootCategory($category_id)
{
$fields_hash = Array (
'l1_Name' => 'Content', 'Filename' => 'Content', 'AutomaticFilename' => 0,
'l1_Description' => 'Content', 'Status' => 4,
);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'Category', 'CategoryId = ' . $category_id);
}
function _createProjCMSTables()
{
// 0. make sure, that Content category exists
$root_category = $this->_getRootCategory('Proj-CMS', 'st');
if ($root_category) {
// proj-cms module found -> remove it
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'Modules
WHERE Name = "Proj-CMS"';
$this->Conn->Query($sql);
unset($this->Application->ModuleInfo['Proj-CMS']);
$this->_resetRootCategory($root_category);
// unhide all structure categories
$sql = 'UPDATE ' . TABLE_PREFIX . 'Category
SET Status = 1
WHERE (Status = 4) AND (CategoryId <> ' . $root_category . ')';
$this->Conn->Query($sql);
} else {
$root_category = $this->_getRootCategory('In-Edit', 'cms');
if ($root_category) {
// in-edit module found -> remove it
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'Modules
WHERE Name = "In-Edit"';
$this->Conn->Query($sql);
unset($this->Application->ModuleInfo['In-Edit']);
$this->_resetRootCategory($root_category);
}
}
if (!$root_category) {
// create "Content" category when Proj-CMS/In-Edit module was not installed before
// use direct sql here, because category table structure doesn't yet match table structure in object
$fields_hash = Array (
'l1_Name' => 'Content', 'Filename' => 'Content', 'AutomaticFilename' => 0,
'l1_Description' => 'Content', 'Status' => 4,
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'Category');
$root_category = $this->Conn->getInsertID();
}
$this->_toolkit->deleteCache();
$this->_toolkit->SetModuleRootCategory('Core', $root_category);
// 1. process "Category" table
$structure = $this->Conn->Query('DESCRIBE ' . TABLE_PREFIX . 'Category', 'Field');
if (!array_key_exists('Template', $structure)) {
// fields from "Pages" table were not added to "Category" table (like before "Proj-CMS" module install)
$sql = "ALTER TABLE " . TABLE_PREFIX . "Category
ADD COLUMN Template varchar(255) default NULL,
ADD COLUMN l1_Title varchar(255) default '',
ADD COLUMN l2_Title varchar(255) default '',
ADD COLUMN l3_Title varchar(255) default '',
ADD COLUMN l4_Title varchar(255) default '',
ADD COLUMN l5_Title varchar(255) default '',
ADD COLUMN l1_MenuTitle varchar(255) NOT NULL default '',
ADD COLUMN l2_MenuTitle varchar(255) NOT NULL default '',
ADD COLUMN l3_MenuTitle varchar(255) NOT NULL default '',
ADD COLUMN l4_MenuTitle varchar(255) NOT NULL default '',
ADD COLUMN l5_MenuTitle varchar(255) NOT NULL default '',
ADD COLUMN MetaTitle text,
ADD COLUMN IndexTools text,
ADD COLUMN IsIndex tinyint(1) NOT NULL default '0',
ADD COLUMN IsMenu TINYINT(4) NOT NULL DEFAULT '1',
ADD COLUMN IsSystem tinyint(4) NOT NULL default '0',
ADD COLUMN FormId int(11) default NULL,
ADD COLUMN FormSubmittedTemplate varchar(255) default NULL,
ADD COLUMN l1_Translated tinyint(4) NOT NULL default '0',
ADD COLUMN l2_Translated tinyint(4) NOT NULL default '0',
ADD COLUMN l3_Translated tinyint(4) NOT NULL default '0',
ADD COLUMN l4_Translated tinyint(4) NOT NULL default '0',
ADD COLUMN l5_Translated tinyint(4) NOT NULL default '0',
ADD COLUMN FriendlyURL varchar(255) NOT NULL default '',
ADD INDEX IsIndex (IsIndex),
ADD INDEX l1_Translated (l1_Translated),
ADD INDEX l2_Translated (l2_Translated),
ADD INDEX l3_Translated (l3_Translated),
ADD INDEX l4_Translated (l4_Translated),
ADD INDEX l5_Translated (l5_Translated)";
$this->Conn->Query($sql);
}
if (array_key_exists('Path', $structure)) {
$sql = 'ALTER TABLE ' . TABLE_PREFIX . 'Category
DROP Path';
$this->Conn->Query($sql);
}
// 2. process "PageContent" table
if ($this->Conn->TableFound(TABLE_PREFIX . 'PageContent', true)) {
$structure = $this->Conn->Query('DESCRIBE ' . TABLE_PREFIX . 'PageContent', 'Field');
if (!array_key_exists('l1_Translated', $structure)) {
$sql = "ALTER TABLE " . TABLE_PREFIX . "PageContent
ADD COLUMN l1_Translated tinyint(4) NOT NULL default '0',
ADD COLUMN l2_Translated tinyint(4) NOT NULL default '0',
ADD COLUMN l3_Translated tinyint(4) NOT NULL default '0',
ADD COLUMN l4_Translated tinyint(4) NOT NULL default '0',
ADD COLUMN l5_Translated tinyint(4) NOT NULL default '0'";
$this->Conn->Query($sql);
}
}
// 3. process "FormFields" table
if ($this->Conn->TableFound(TABLE_PREFIX . 'FormFields', true)) {
$structure = $this->Conn->Query('DESCRIBE ' . TABLE_PREFIX . 'FormFields', 'Field');
if (!$structure['FormId']['Key']) {
$sql = "ALTER TABLE " . TABLE_PREFIX . "FormFields
CHANGE Validation Validation TINYINT NOT NULL DEFAULT '0',
ADD INDEX FormId (FormId),
ADD INDEX Priority (Priority),
ADD INDEX IsSystem (IsSystem),
ADD INDEX DisplayInGrid (DisplayInGrid)";
$this->Conn->Query($sql);
}
}
else {
$this->Conn->Query("INSERT INTO " . TABLE_PREFIX . "Permissions VALUES (DEFAULT, 'in-portal:forms.view', 11, 1, 1, 0)");
$this->Conn->Query("INSERT INTO " . TABLE_PREFIX . "Permissions VALUES (DEFAULT, 'in-portal:forms.add', 11, 1, 1, 0)");
$this->Conn->Query("INSERT INTO " . TABLE_PREFIX . "Permissions VALUES (DEFAULT, 'in-portal:forms.edit', 11, 1, 1, 0)");
$this->Conn->Query("INSERT INTO " . TABLE_PREFIX . "Permissions VALUES (DEFAULT, 'in-portal:forms.delete', 11, 1, 1, 0)");
}
// 4. process "FormSubmissions" table
if ($this->Conn->TableFound(TABLE_PREFIX . 'FormSubmissions', true)) {
$structure = $this->Conn->Query('DESCRIBE ' . TABLE_PREFIX . 'FormSubmissions', 'Field');
if (!$structure['SubmissionTime']['Key']) {
$sql = "ALTER TABLE " . TABLE_PREFIX . "FormSubmissions
ADD INDEX SubmissionTime (SubmissionTime)";
$this->Conn->Query($sql);
}
}
else {
$this->Conn->Query("INSERT INTO " . TABLE_PREFIX . "Permissions VALUES (DEFAULT, 'in-portal:submissions.view', 11, 1, 1, 0)");
}
// 5. add missing event
$sql = 'SELECT EventId
FROM ' . TABLE_PREFIX . 'Events
WHERE (Event = "FORM.SUBMITTED") AND (Type = 1)';
$event_id = $this->Conn->GetOne($sql);
if (!$event_id) {
$sql = "INSERT INTO " . TABLE_PREFIX . "Events VALUES (DEFAULT, 'FORM.SUBMITTED', NULL, 1, 0, 'Core:Category', 'la_event_FormSubmitted', 1)";
$this->Conn->Query($sql);
}
$sql = 'SELECT EventId
FROM ' . TABLE_PREFIX . 'Events
WHERE (Event = "FORM.SUBMITTED") AND (Type = 0)';
$event_id = $this->Conn->GetOne($sql);
if (!$event_id) {
$sql = "INSERT INTO " . TABLE_PREFIX . "Events VALUES (DEFAULT, 'FORM.SUBMITTED', NULL, 1, 0, 'Core:Category', 'la_event_FormSubmitted', 0)";
$this->Conn->Query($sql);
}
}
function _addMissingConfigurationVariables()
{
$variables = Array (
'cms_DefaultDesign' => Array (
"INSERT INTO " . TABLE_PREFIX . "ConfigurationAdmin VALUES ('cms_DefaultDesign', 'la_Text_General', 'la_prompt_DefaultDesignTemplate', 'text', NULL, NULL, 10.15, 0, 0)",
"INSERT INTO " . TABLE_PREFIX . "ConfigurationValues VALUES (DEFAULT, 'cms_DefaultDesign', '/platform/designs/general', 'In-Portal', 'in-portal:configure_categories')",
),
'Require_AdminSSL' => Array (
"INSERT INTO " . TABLE_PREFIX . "ConfigurationAdmin VALUES ('Require_AdminSSL', 'la_Text_Website', 'la_config_RequireSSLAdmin', 'checkbox', '', '', 10.105, 0, 1)",
"INSERT INTO " . TABLE_PREFIX . "ConfigurationValues VALUES (DEFAULT, 'Require_AdminSSL', '', 'In-Portal', 'in-portal:configure_advanced')",
),
'UsePopups' => Array (
"INSERT INTO " . TABLE_PREFIX . "ConfigurationAdmin VALUES ('UsePopups', 'la_Text_Website', 'la_config_UsePopups', 'radio', '', '1=la_Yes,0=la_No', 10.221, 0, 0)",
"INSERT INTO " . TABLE_PREFIX . "ConfigurationValues VALUES (DEFAULT, 'UsePopups', '1', 'In-Portal', 'in-portal:configure_advanced')",
),
'UseDoubleSorting' => Array (
"INSERT INTO " . TABLE_PREFIX . "ConfigurationAdmin VALUES ('UseDoubleSorting', 'la_Text_Website', 'la_config_UseDoubleSorting', 'radio', '', '1=la_Yes,0=la_No', 10.222, 0, 0)",
"INSERT INTO " . TABLE_PREFIX . "ConfigurationValues VALUES (DEFAULT, 'UseDoubleSorting', '0', 'In-Portal', 'in-portal:configure_advanced')",
),
'MenuFrameWidth' => Array (
"INSERT INTO " . TABLE_PREFIX . "ConfigurationAdmin VALUES ('MenuFrameWidth', 'la_title_General', 'la_prompt_MenuFrameWidth', 'text', NULL, NULL, 10.31, 0, 0)",
"INSERT INTO " . TABLE_PREFIX . "ConfigurationValues VALUES (DEFAULT, 'MenuFrameWidth', 200, 'In-Portal', 'in-portal:configure_advanced')",
),
'DefaultSettingsUserId' => Array (
"INSERT INTO " . TABLE_PREFIX . "ConfigurationAdmin VALUES ('DefaultSettingsUserId', 'la_title_General', 'la_prompt_DefaultUserId', 'text', NULL, NULL, '10.06', '0', '0')",
"INSERT INTO " . TABLE_PREFIX . "ConfigurationValues VALUES (DEFAULT, 'DefaultSettingsUserId', -1, 'In-Portal:Users', 'in-portal:configure_users')",
),
);
foreach ($variables as $variable_name => $variable_sqls) {
$sql = 'SELECT VariableId
FROM ' . TABLE_PREFIX . 'ConfigurationValues
WHERE VariableName = ' . $this->Conn->qstr($variable_name);
$variable_id = $this->Conn->GetOne($sql);
if ($variable_id) {
continue;
}
foreach ($variable_sqls as $variable_sql) {
$this->Conn->Query($variable_sql);
}
}
}
/**
* Sort images in database (update Priority field)
*
*/
function _sortImages()
{
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'Images
ORDER BY ResourceId ASC , DefaultImg DESC , ImageId ASC';
$images = $this->Conn->Query($sql);
$priority = 0;
$last_resource_id = false;
foreach ($images as $image) {
if ($image['ResourceId'] != $last_resource_id) {
// each item have own priorities among it's images
$priority = 0;
$last_resource_id = $image['ResourceId'];
}
if (!$image['DefaultImg']) {
$priority--;
}
$sql = 'UPDATE ' . TABLE_PREFIX . 'Images
SET Priority = ' . $priority . '
WHERE ImageId = ' . $image['ImageId'];
$this->Conn->Query($sql);
}
}
/**
* Update to 5.0.1
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_5_0_1($mode)
{
if ($mode == 'after') {
// delete old events
$events_to_delete = Array ('CATEGORY.MODIFY', 'CATEGORY.DELETE');
$sql = 'SELECT EventId
FROM ' . TABLE_PREFIX . 'Events
WHERE Event IN ("' . implode('","', $events_to_delete) . '")';
$event_ids = $this->Conn->GetCol($sql);
if ($event_ids) {
$this->_deleteEvents($event_ids);
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'Phrase
WHERE Phrase IN ("la_event_category.modify", "la_event_category.delete")';
$this->Conn->Query($sql);
}
// partially delete events
$sql = 'SELECT EventId
FROM ' . TABLE_PREFIX . 'Events
WHERE (Event IN ("CATEGORY.APPROVE", "CATEGORY.DENY")) AND (Type = ' . EmailTemplate::TEMPLATE_TYPE_ADMIN . ')';
$event_ids = $this->Conn->GetCol($sql);
if ($event_ids) {
$this->_deleteEvents($event_ids);
}
}
}
function _deleteEvents($ids)
{
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'EmailMessage
WHERE EventId IN (' . implode(',', $ids) . ')';
$this->Conn->Query($sql);
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'Events
WHERE EventId IN (' . implode(',', $ids) . ')';
$this->Conn->Query($sql);
}
/**
* Update to 5.0.2-B2; Transforms IsIndex field values to SymLinkCategoryId field
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_5_0_2_B2($mode)
{
// 0 - Regular, 1 - Category Index, 2 - Container
if ($mode == 'before') {
// fix "Content" category
$fields_hash = Array (
'CreatedById' => USER_ROOT,
'CreatedOn' => time(),
'ResourceId' => $this->Application->NextResourceId(),
);
$category_id = $this->Application->findModule('Name', 'Core', 'RootCat');
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'Category', 'CategoryId = ' . $category_id);
// get all categories, marked as category index
$sql = 'SELECT ParentPath, CategoryId
FROM ' . TABLE_PREFIX . 'Category
WHERE IsIndex = 1';
$category_indexes = $this->Conn->GetCol($sql, 'CategoryId');
foreach ($category_indexes as $category_id => $parent_path) {
$parent_path = explode('|', substr($parent_path, 1, -1));
// set symlink to $category_id for each category, marked as container in given category path
$sql = 'SELECT CategoryId
FROM ' . TABLE_PREFIX . 'Category
WHERE CategoryId IN (' . implode(',', $parent_path) . ') AND (IsIndex = 2)';
$category_containers = $this->Conn->GetCol($sql);
if ($category_containers) {
$sql = 'UPDATE ' . TABLE_PREFIX . 'Category
SET SymLinkCategoryId = ' . $category_id . '
WHERE CategoryId IN (' . implode(',', $category_containers) . ')';
$this->Conn->Query($sql);
}
}
}
if ($mode == 'after') {
// scan theme to fill Theme.TemplateAliases and ThemeFiles.TemplateAlias fields
$this->_toolkit->rebuildThemes();
$sql = 'SELECT TemplateAliases, ThemeId
FROM ' . TABLE_PREFIX . 'Theme
WHERE (Enabled = 1) AND (TemplateAliases <> "")';
$template_aliases = $this->Conn->GetCol($sql, 'ThemeId');
$all_template_aliases = Array (); // reversed alias (from real template to alias)
foreach ($template_aliases as $theme_id => $theme_template_aliases) {
$theme_template_aliases = unserialize($theme_template_aliases);
if (!$theme_template_aliases) {
continue;
}
$all_template_aliases = array_merge($all_template_aliases, array_flip($theme_template_aliases));
}
$default_design_replaced = false;
$default_design = trim($this->Application->ConfigValue('cms_DefaultDesign'), '/');
foreach ($all_template_aliases as $from_template => $to_alias) {
// replace default design in configuration variable (when matches alias)
if ($from_template == $default_design) {
// specific alias matched
$sql = 'UPDATE ' . TABLE_PREFIX . 'ConfigurationValues
SET VariableValue = ' . $this->Conn->qstr($to_alias) . '
WHERE VariableName = "cms_DefaultDesign"';
$this->Conn->Query($sql);
$default_design_replaced = true;
}
// replace Category.Template and Category.CachedTemplate fields (when matches alias)
$sql = 'UPDATE ' . TABLE_PREFIX . 'Category
SET Template = ' . $this->Conn->qstr($to_alias) . '
WHERE Template IN (' . $this->Conn->qstr('/' . $from_template) . ',' . $this->Conn->qstr($from_template) . ')';
$this->Conn->Query($sql);
$sql = 'UPDATE ' . TABLE_PREFIX . 'Category
SET CachedTemplate = ' . $this->Conn->qstr($to_alias) . '
WHERE CachedTemplate IN (' . $this->Conn->qstr('/' . $from_template) . ',' . $this->Conn->qstr($from_template) . ')';
$this->Conn->Query($sql);
}
if (!$default_design_replaced) {
// in case if current default design template doesn't
// match any of aliases, then set it to #default_design#
$sql = 'UPDATE ' . TABLE_PREFIX . 'ConfigurationValues
SET VariableValue = "#default_design#"
WHERE VariableName = "cms_DefaultDesign"';
$this->Conn->Query($sql);
}
// replace data in category custom fields used for category item template storage
$rewrite_processor = $this->Application->recallObject('kRewriteUrlProcessor');
/* @var $rewrite_processor kRewriteUrlProcessor */
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
$custom_field_id = $rewrite_processor->getItemTemplateCustomField($module_info['Var']);
if (!$custom_field_id) {
continue;
}
foreach ($all_template_aliases as $from_template => $to_alias) {
$sql = 'UPDATE ' . TABLE_PREFIX . 'CategoryCustomData
SET l1_cust_' . $custom_field_id . ' = ' . $this->Conn->qstr($to_alias) . '
WHERE l1_cust_' . $custom_field_id . ' = ' . $this->Conn->qstr($from_template);
$this->Conn->Query($sql);
}
}
}
}
/**
* Update to 5.0.3-B2; Moves CATEGORY.* permission from module root categories to Content category
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_5_0_3_B2($mode)
{
if ($mode == 'before') {
// get permissions
$sql = 'SELECT PermissionName
FROM ' . TABLE_PREFIX . 'PermissionConfig
WHERE PermissionName LIKE "CATEGORY.%"';
$permission_names = $this->Conn->GetCol($sql);
// get groups
$sql = 'SELECT GroupId
FROM ' . TABLE_PREFIX . 'PortalGroup';
$user_groups = $this->Conn->GetCol($sql);
$user_group_count = count($user_groups);
// get module root categories
$sql = 'SELECT RootCat
FROM ' . TABLE_PREFIX . 'Modules';
$module_categories = $this->Conn->GetCol($sql);
$module_categories[] = 0;
$module_categories = implode(',', array_unique($module_categories));
$permissions = $delete_permission_ids = Array ();
foreach ($permission_names as $permission_name) {
foreach ($user_groups as $group_id) {
$sql = 'SELECT PermissionId
FROM ' . TABLE_PREFIX . 'Permissions
WHERE (Permission = ' . $this->Conn->qstr($permission_name) . ') AND (PermissionValue = 1) AND (GroupId = ' . $group_id . ') AND (`Type` = 0) AND (CatId IN (' . $module_categories . '))';
$permission_ids = $this->Conn->GetCol($sql);
if ($permission_ids) {
if (!array_key_exists($permission_name, $permissions)) {
$permissions[$permission_name] = Array ();
}
$permissions[$permission_name][] = $group_id;
$delete_permission_ids = array_merge($delete_permission_ids, $permission_ids);
}
}
}
if ($delete_permission_ids) {
// here we can delete some of permissions that will be added later
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'Permissions
WHERE PermissionId IN (' . implode(',', $delete_permission_ids) . ')';
$this->Conn->Query($sql);
}
$home_category = $this->Application->findModule('Name', 'Core', 'RootCat');
foreach ($permissions as $permission_name => $permission_groups) {
// optimize a bit
$has_everyone = in_array(15, $permission_groups);
if ($has_everyone || (!$has_everyone && count($permission_groups) == $user_group_count - 1)) {
// has permission for "Everyone" group OR allowed in all groups except "Everyone" group
// so remove all other explicitly allowed permissions
$permission_groups = Array (15);
}
foreach ($permission_groups as $group_id) {
$fields_hash = Array (
'Permission' => $permission_name,
'GroupId' => $group_id,
'PermissionValue' => 1,
'Type' => 0, // category-based permission,
'CatId' => $home_category,
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'Permissions');
}
}
$updater = $this->Application->makeClass('kPermCacheUpdater');
/* @var $updater kPermCacheUpdater */
$updater->OneStepRun();
}
}
/**
* Update to 5.1.0-B1; Makes email message fields multilingual
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_5_1_0_B1($mode)
{
if ( $mode == 'before' ) {
$this->_renameTables('from');
// migrate email events
$table_structure = $this->Conn->Query('DESCRIBE ' . TABLE_PREFIX . 'Events', 'Field');
if (!array_key_exists('Headers', $table_structure)) {
$sql = 'ALTER TABLE ' . TABLE_PREFIX . 'Events
ADD `Headers` TEXT NULL AFTER `ReplacementTags`,
ADD `MessageType` VARCHAR(4) NOT NULL default "text" AFTER `Headers`';
$this->Conn->Query($sql);
}
// alter here, because kMultiLanguageHelper::createFields
// method, called after will expect that to be in database
$sql = 'ALTER TABLE ' . TABLE_PREFIX . 'Events
ADD AllowChangingSender TINYINT NOT NULL DEFAULT "0" AFTER MessageType ,
ADD CustomSender TINYINT NOT NULL DEFAULT "0" AFTER AllowChangingSender ,
ADD SenderName VARCHAR(255) NOT NULL DEFAULT "" AFTER CustomSender ,
ADD SenderAddressType TINYINT NOT NULL DEFAULT "0" AFTER SenderName ,
ADD SenderAddress VARCHAR(255) NOT NULL DEFAULT "" AFTER SenderAddressType ,
ADD AllowChangingRecipient TINYINT NOT NULL DEFAULT "0" AFTER SenderAddress ,
ADD CustomRecipient TINYINT NOT NULL DEFAULT "0" AFTER AllowChangingRecipient ,
ADD Recipients TEXT AFTER CustomRecipient,
ADD INDEX (AllowChangingSender),
ADD INDEX (CustomSender),
ADD INDEX (SenderAddressType),
ADD INDEX (AllowChangingRecipient),
ADD INDEX (CustomRecipient)';
$this->Conn->Query($sql);
// create multilingual fields for phrases and email events
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$ml_helper->createFields('phrases');
$ml_helper->createFields('email-template');
$languages = $ml_helper->getLanguages();
if ($this->Conn->TableFound(TABLE_PREFIX . 'EmailMessage', true)) {
$email_template_helper = $this->Application->recallObject('kEmailTemplateHelper');
/* @var $email_template_helper kEmailTemplateHelper */
foreach ($languages as $language_id) {
$sql = 'SELECT EmailMessageId, Template, EventId
FROM ' . TABLE_PREFIX . 'EmailMessage
WHERE LanguageId = ' . $language_id;
$translations = $this->Conn->Query($sql, 'EventId');
foreach ($translations as $event_id => $translation_data) {
$parsed = $email_template_helper->parseTemplate($translation_data['Template'], 'html');
$fields_hash = Array (
'l' . $language_id . '_Subject' => $parsed['Subject'],
'l' . $language_id . '_Body' => $parsed['HtmlBody'],
);
if ( $parsed['Headers'] ) {
$fields_hash['Headers'] = $parsed['Headers'];
}
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'Events', 'EventId = ' . $event_id);
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'EmailMessage
WHERE EmailMessageId = ' . $translation_data['EmailMessageId'];
$this->Conn->Query($sql);
}
}
}
// migrate phrases
$temp_table = $this->Application->GetTempName(TABLE_PREFIX . 'Phrase');
$sqls = Array (
'DROP TABLE IF EXISTS ' . $temp_table,
'CREATE TABLE ' . $temp_table . ' LIKE ' . TABLE_PREFIX . 'Phrase',
'ALTER TABLE ' . $temp_table . ' DROP LanguageId, DROP Translation',
'ALTER IGNORE TABLE ' . $temp_table . ' DROP INDEX LanguageId_2',
'ALTER TABLE ' . $temp_table . ' DROP PhraseId',
'ALTER TABLE ' . $temp_table . ' ADD PhraseId INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST',
);
foreach ($sqls as $sql) {
$this->Conn->Query($sql);
}
$already_added = Array ();
$primary_language_id = $this->Application->GetDefaultLanguageId();
foreach ($languages as $language_id) {
$sql = 'SELECT Phrase, PhraseKey, Translation AS l' . $language_id . '_Translation, PhraseType, LastChanged, LastChangeIP, Module
FROM ' . TABLE_PREFIX . 'Phrase
WHERE LanguageId = ' . $language_id;
$phrases = $this->Conn->Query($sql, 'Phrase');
foreach ($phrases as $phrase => $fields_hash) {
if (array_key_exists($phrase, $already_added)) {
$this->Conn->doUpdate($fields_hash, $temp_table, 'PhraseId = ' . $already_added[$phrase]);
}
else {
$this->Conn->doInsert($fields_hash, $temp_table);
$already_added[$phrase] = $this->Conn->getInsertID();
}
}
// in case some phrases were found in this language, but not in primary language -> copy them
if ($language_id != $primary_language_id) {
$sql = 'UPDATE ' . $temp_table . '
SET l' . $primary_language_id . '_Translation = l' . $language_id . '_Translation
WHERE l' . $primary_language_id . '_Translation IS NULL';
$this->Conn->Query($sql);
}
}
$this->Conn->Query('DROP TABLE IF EXISTS ' . TABLE_PREFIX . 'Phrase');
$this->Conn->Query('RENAME TABLE ' . $temp_table . ' TO ' . TABLE_PREFIX . 'Phrase');
$this->_updateCountryStatesTable();
$this->_replaceConfigurationValueSeparator();
// save "config.php" in php format, not ini format as before
$this->_toolkit->SaveConfig();
}
if ($mode == 'after') {
$this->_transformEmailRecipients();
$this->_fixSkinColors();
}
}
/**
* Makes sure we rename tables to legacy names before doing other upgrades before 5.2.0-B1 upgrade
*
* @param string $name
* @param Array $arguments
*/
public function __call($name, $arguments)
{
if ( substr($name, 0, 12) == 'Upgrade_5_1_' && $arguments[0] == 'before' ) {
$this->_renameTables('from');
}
if ( substr($name, 0, 13) == 'Upgrade_5_2_0' && $arguments[0] == 'before' ) {
$this->_renameTables('to');
}
}
/**
* Move country/state translations from Phrase to CountryStates table
*
*/
function _updateCountryStatesTable()
{
// refactor StdDestinations table
$sql = 'RENAME TABLE ' . TABLE_PREFIX . 'StdDestinations TO ' . TABLE_PREFIX . 'CountryStates';
$this->Conn->Query($sql);
$sql = 'ALTER TABLE ' . TABLE_PREFIX . 'CountryStates
CHANGE DestId CountryStateId INT(11) NOT NULL AUTO_INCREMENT,
CHANGE DestType Type INT(11) NOT NULL DEFAULT \'1\',
CHANGE DestParentId StateCountryId INT(11) NULL DEFAULT NULL,
CHANGE DestAbbr IsoCode CHAR(3) NOT NULL DEFAULT \'\',
CHANGE DestAbbr2 ShortIsoCode CHAR(2) NULL DEFAULT NULL,
DROP INDEX DestType,
DROP INDEX DestParentId,
ADD INDEX (`Type`),
ADD INDEX (StateCountryId)';
$this->Conn->Query($sql);
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$ml_helper->createFields('country-state');
$languages = $ml_helper->getLanguages();
foreach ($languages as $language_id) {
$sub_select = ' SELECT l' . $language_id . '_Translation
FROM ' . TABLE_PREFIX . 'Phrase
WHERE Phrase = DestName';
$sql = 'UPDATE ' . TABLE_PREFIX . 'CountryStates
SET l' . $language_id . '_Name = (' . $sub_select . ')';
$this->Conn->Query($sql);
}
$sql = 'ALTER TABLE ' . TABLE_PREFIX . 'CountryStates
DROP DestName';
$this->Conn->Query($sql);
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'Phrase
WHERE Phrase LIKE ' . $this->Conn->qstr('la_country_%') . ' OR Phrase LIKE ' . $this->Conn->qstr('la_state_%');
$this->Conn->Query($sql);
}
/**
* Makes configuration values dropdowns use "||" as separator
*
*/
function _replaceConfigurationValueSeparator()
{
$custom_field_helper = $this->Application->recallObject('InpCustomFieldsHelper');
/* @var $custom_field_helper InpCustomFieldsHelper */
$sql = 'SELECT ValueList, VariableName
FROM ' . TABLE_PREFIX . 'ConfigurationAdmin
WHERE ValueList LIKE "%,%"';
$variables = $this->Conn->GetCol($sql, 'VariableName');
foreach ($variables as $variable_name => $value_list) {
$ret = Array ();
$options = $custom_field_helper->GetValuesHash($value_list, ',', false);
foreach ($options as $option_key => $option_title) {
if (substr($option_key, 0, 3) == 'SQL') {
$ret[] = $option_title;
}
else {
$ret[] = $option_key . '=' . $option_title;
}
}
$fields_hash = Array (
'ValueList' => implode(VALUE_LIST_SEPARATOR, $ret),
);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'ConfigurationAdmin', 'VariableName = ' . $this->Conn->qstr($variable_name));
}
}
/**
* Transforms "FromUserId" into Sender* and Recipients columns
*
*/
function _transformEmailRecipients()
{
$sql = 'SELECT FromUserId, Type, EventId
FROM ' . TABLE_PREFIX . 'Events
WHERE FromUserId IS NOT NULL AND (FromUserId <> ' . USER_ROOT . ')';
$events = $this->Conn->Query($sql, 'EventId');
$minput_helper = $this->Application->recallObject('MInputHelper');
/* @var $minput_helper MInputHelper */
foreach ($events as $event_id => $event_data) {
$sql = 'SELECT Login
FROM ' . TABLE_PREFIX . 'PortalUser
WHERE PortalUserId = ' . $event_data['FromUserId'];
$username = $this->Conn->GetOne($sql);
if (!$username) {
continue;
}
if ($event_data['Type'] == EmailTemplate::TEMPLATE_TYPE_FRONTEND) {
// from user
$fields_hash = Array (
'CustomSender' => 1,
'SenderAddressType' => EmailTemplate::ADDRESS_TYPE_USER,
'SenderAddress' => $username
);
}
if ($event_data['Type'] == EmailTemplate::TEMPLATE_TYPE_ADMIN) {
// to user
$records = Array (
Array (
'RecipientType' => EmailTemplate::RECIPIENT_TYPE_TO,
'RecipientName' => '',
'RecipientAddressType' => EmailTemplate::ADDRESS_TYPE_USER,
'RecipientAddress' => $username
)
);
$fields_hash = Array (
'CustomRecipient' => 1,
'Recipients' => $minput_helper->prepareMInputXML($records, array_keys( reset($records) ))
);
}
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'Events', 'EventId = ' . $event_id);
}
$this->Conn->Query('ALTER TABLE ' . TABLE_PREFIX . 'Events DROP FromUserId');
}
/**
* Update to 5.1.0; Fixes refferer of form submissions
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_5_1_0($mode)
{
if ( $mode == 'before' ) {
$this->_renameTables('from');
}
if ( $mode == 'after' ) {
$base_url = $this->Application->BaseURL();
$sql = 'UPDATE ' . TABLE_PREFIX . 'FormSubmissions
SET ReferrerURL = REPLACE(ReferrerURL, ' . $this->Conn->qstr($base_url) . ', "/")';
$this->Conn->Query($sql);
}
}
/**
* Update to 5.1.1-B1; Transforms DisplayToPublic logic
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_5_1_1_B1($mode)
{
if ( $mode == 'before' ) {
$this->_renameTables('from');
}
if ($mode == 'after') {
$this->processDisplayToPublic();
}
}
function processDisplayToPublic()
{
$profile_mapping = Array (
'pp_firstname' => 'FirstName',
'pp_lastname' => 'LastName',
'pp_dob' => 'dob',
'pp_email' => 'Email',
'pp_phone' => 'Phone',
'pp_street' => 'Street',
'pp_city' => 'City',
'pp_state' => 'State',
'pp_zip' => 'Zip',
'pp_country' => 'Country',
);
$fields = array_keys($profile_mapping);
$fields = $this->Conn->qstrArray($fields);
$where_clause = 'VariableName IN (' . implode(',', $fields) . ')';
// 1. get user, that have saved their profile at least once
$sql = 'SELECT DISTINCT PortalUserId
FROM ' . TABLE_PREFIX . 'PersistantSessionData
WHERE ' . $where_clause;
$users = $this->Conn->GetCol($sql);
foreach ($users as $user_id) {
// 2. convert to new format
$sql = 'SELECT VariableValue, VariableName
FROM ' . TABLE_PREFIX . 'PersistantSessionData
WHERE (PortalUserId = ' . $user_id . ') AND ' . $where_clause;
$user_variables = $this->Conn->GetCol($sql, 'VariableName');
// go through mapping to preserve variable order
$value = Array ();
foreach ($profile_mapping as $from_name => $to_name) {
if (array_key_exists($from_name, $user_variables) && $user_variables[$from_name]) {
$value[] = $to_name;
}
}
if ($value) {
$fields_hash = Array (
'DisplayToPublic' => '|' . implode('|', $value) . '|',
);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'PortalUser', 'PortalUserId = ' . $user_id);
}
// 3. delete old style variables
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'PersistantSessionData
WHERE (PortalUserId = ' . $user_id . ') AND ' . $where_clause;
$this->Conn->Query($sql);
}
}
/**
* Update to 5.1.3; Merges column and field phrases
*
* @param string $mode when called mode {before, after)
*/
function Upgrade_5_1_3($mode)
{
if ( $mode == 'before' ) {
$this->_renameTables('from');
}
if ( $mode == 'after' ) {
$this->moveTranslation('LA_COL_', 'LA_FLD_', 'ColumnTranslation');
}
}
/**
* Makes sure table names match upgrade script
*
* @param string $key
* @return void
* @access private
*/
private function _renameTables($key)
{
foreach ($this->renamedTables as $prefix => $table_info) {
$this->Application->getUnitConfig($prefix)->setTableName(TABLE_PREFIX . $table_info[$key]);
}
}
/**
* Update to 5.2.0-B1; Transform list sortings storage
*
* @param string $mode when called mode {before, after)
*/
public function Upgrade_5_2_0_B1($mode)
{
if ( $mode == 'before' ) {
$this->_renameTables('to');
}
if ( $mode == 'after' ) {
$this->transformSortings();
$this->moveTranslation('LA_COL_', 'LA_FLD_', 'ColumnTranslation'); // because of "la_col_ItemPrefix" phrase
$this->moveTranslation('LA_HINT_', 'LA_FLD_', 'HintTranslation');
$this->moveTranslation('LA_HINT_', 'LA_CONFIG_', 'HintTranslation');
$this->moveTranslation('LA_HINT_', 'LA_TITLE_', 'HintTranslation');
$this->createPageRevisions();
}
}
/**
* Transforms a way, how list sortings are stored
*
* @return void
*/
function transformSortings()
{
$sql = 'SELECT VariableName, PortalUserId
FROM ' . TABLE_PREFIX . 'UserPersistentSessionData
WHERE VariableName LIKE "%_Sort1.%"';
$sortings = $this->Conn->Query($sql);
foreach ($sortings AS $sorting) {
if ( !preg_match('/^(.*)_Sort1.(.*)$/', $sorting['VariableName'], $regs) ) {
continue;
}
$user_id = $sorting['PortalUserId'];
$prefix_special = $regs[1] . '_';
$view_name = '.' . $regs[2];
$old_variable_names = Array (
$prefix_special . 'Sort1' . $view_name, $prefix_special . 'Sort1_Dir' . $view_name,
$prefix_special . 'Sort2' . $view_name, $prefix_special . 'Sort2_Dir' . $view_name,
);
$old_variable_names = $this->Conn->qstrArray($old_variable_names);
$sql = 'SELECT VariableValue, VariableName
FROM ' . TABLE_PREFIX . 'UserPersistentSessionData
WHERE PortalUserId = ' . $user_id . ' AND VariableName IN (' . implode(',', $old_variable_names) . ')';
$sorting_data = $this->Conn->GetCol($sql, 'VariableName');
// prepare & save new sortings
$new_sorting = Array (
'Sort1' => $sorting_data[$prefix_special . 'Sort1' . $view_name],
'Sort1_Dir' => $sorting_data[$prefix_special . 'Sort1_Dir' . $view_name],
);
if ( isset($sorting_data[$prefix_special . 'Sort2' . $view_name]) ) {
$new_sorting['Sort2'] = $sorting_data[$prefix_special . 'Sort2' . $view_name];
$new_sorting['Sort2_Dir'] = $sorting_data[$prefix_special . 'Sort2_Dir' . $view_name];
}
$fields_hash = Array (
'PortalUserId' => $user_id,
'VariableName' => $prefix_special . 'Sortings' . $view_name,
'VariableValue' => serialize($new_sorting),
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'UserPersistentSessionData');
// delete sortings, that were already processed
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'UserPersistentSessionData
WHERE PortalUserId = ' . $user_id . ' AND VariableName IN (' . implode(',', $old_variable_names) . ')';
$this->Conn->Query($sql);
}
}
/**
* Merges several phrases into one (e.g. la_col_ + la_hint_ into designated columns of la_fld_ phrases)
*
* @param string $source_prefix
* @param string $target_prefix
* @param string $db_column
* @return void
* @access protected
*/
public function moveTranslation($source_prefix, $target_prefix, $db_column)
{
$source_phrases = $this->getPhrasesByMask($source_prefix . '%');
$target_phrases = $this->getPhrasesByMask($target_prefix . '%');
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$delete_ids = Array ();
$ml_helper->createFields('phrases');
$languages = $ml_helper->getLanguages();
$phrase_table = $this->Application->getUnitConfig('phrases')->getTableName();
foreach ($source_phrases as $phrase_key => $phrase_info) {
$target_phrase_key = $target_prefix . substr($phrase_key, strlen($source_prefix));
if ( !isset($target_phrases[$target_phrase_key]) ) {
continue;
}
$fields_hash = Array ();
// copy column phrase main translation into field phrase column translation
foreach ($languages as $language_id) {
$fields_hash['l' . $language_id . '_' . $db_column] = $phrase_info['l' . $language_id . '_Translation'];
}
$delete_ids[] = $phrase_info['PhraseId'];
$this->Conn->doUpdate($fields_hash, $phrase_table, 'PhraseId = ' . $target_phrases[$target_phrase_key]['PhraseId']);
}
// delete all column phrases, that were absorbed by field phrases
if ( $delete_ids ) {
$sql = 'DELETE FROM ' . $phrase_table . '
WHERE PhraseId IN (' . implode(',', $delete_ids) . ')';
$this->Conn->Query($sql);
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'PhraseCache';
$this->Conn->Query($sql);
}
}
/**
* Returns phrases by mask
*
* @param string $mask
* @return Array
* @access protected
*/
protected function getPhrasesByMask($mask)
{
$sql = 'SELECT *
FROM ' . $this->Application->getUnitConfig('phrases')->getTableName() . '
WHERE PhraseKey LIKE ' . $this->Conn->qstr($mask);
return $this->Conn->Query($sql, 'PhraseKey');
}
protected function createPageRevisions()
{
$sql = 'SELECT DISTINCT PageId
FROM ' . TABLE_PREFIX . 'PageContent';
$page_ids = $this->Conn->GetCol($sql);
foreach ($page_ids as $page_id) {
$fields_hash = Array (
'PageId' => $page_id,
'RevisionNumber' => 1,
'IsDraft' => 0,
'FromRevisionId' => 0,
'CreatedById' => USER_ROOT,
'CreatedOn' => time(),
'Status' => STATUS_ACTIVE,
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'PageRevisions');
$fields_hash = Array (
'RevisionId' => $this->Conn->getInsertID(),
);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'PageContent', 'PageId = ' . $page_id);
}
}
/**
* Update to 5.2.0-B3; Introduces separate field for plain-text e-mail event translations
*
* @param string $mode when called mode {before, after)
*/
public function Upgrade_5_2_0_B3($mode)
{
if ( $mode == 'before' ) {
$this->_renameTables('to');
}
if ( $mode == 'after' ) {
$this->_splitEmailBody();
$this->_migrateCommonFooter();
}
}
/**
* Splits e-mail body into HTML and Text fields
*
* @return void
* @access private
*/
private function _splitEmailBody()
{
$config = $this->Application->getUnitConfig('email-template');
$id_field = $config->getIDField();
$table_name = $config->getTableName();
$fields = $this->Conn->Query('DESCRIBE ' . $table_name, 'Field');
if ( !isset($fields['l1_Body']) ) {
// column dropped - nothing to convert anymore
return;
}
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$languages = $ml_helper->getLanguages();
$ml_helper->createFields('email-template');
$sql = 'SELECT *
FROM ' . $table_name;
$email_events = $this->Conn->Query($sql);
// 1. move data to new columns
foreach ($email_events as $email_event) {
$fields_hash = Array ();
$translation_field = $email_event['MessageType'] == 'html' ? 'HtmlBody' : 'PlainTextBody';
foreach ($languages as $language_id) {
$fields_hash['l' . $language_id . '_' . $translation_field] = $email_event['l' . $language_id . '_Body'];
}
if ( $fields_hash ) {
$this->Conn->doUpdate($fields_hash, $table_name, $id_field . ' = ' . $email_event[$id_field]);
}
}
// 2. drop old columns
$drops = Array ('DROP COLUMN MessageType');
foreach ($languages as $language_id) {
$lang_field = 'l' . $language_id . '_Body';
if ( isset($fields[$lang_field]) ) {
$drops[] = 'DROP COLUMN ' . $lang_field;
}
}
$this->Conn->Query('ALTER TABLE ' . $table_name . ' ' . implode(', ', $drops));
}
/**
* Transforms COMMON.FOOTER e-mail event into new field in Languages table
*/
private function _migrateCommonFooter()
{
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$languages = $ml_helper->getLanguages();
$event_table = $this->Application->getUnitConfig('email-template')->getTableName();
$sql = 'SELECT *
FROM ' . $event_table . '
WHERE Event = "COMMON.FOOTER"';
$footer_data = $this->Conn->GetRow($sql);
if ( !$footer_data ) {
return;
}
$primary_language_id = $this->Application->GetDefaultLanguageId();
$table_name = $this->Application->getUnitConfig('lang')->getTableName();
foreach ($languages as $language_id) {
$is_primary = $language_id == $primary_language_id;
$fields_hash = Array (
'HtmlEmailTemplate' => $this->_appendEmailDesignBody($footer_data['l' . $language_id . '_HtmlBody'], $is_primary),
'TextEmailTemplate' => $this->_appendEmailDesignBody($footer_data['l' . $language_id . '_PlainTextBody'], $is_primary),
);
$this->Conn->doUpdate($fields_hash, $table_name, 'LanguageId = ' . $language_id);
}
$sql = 'DELETE FROM ' . $event_table . '
WHERE EventId = ' . $footer_data['EventId'];
$this->Conn->Query($sql);
}
/**
* Adds "$body" to given string
*
* @param string $string
* @param bool $is_primary for primary language
* @return string
* @access private
*/
private function _appendEmailDesignBody($string, $is_primary)
{
if ( !$string ) {
return $is_primary ? '$body' : $string;
}
return '$body' . "\n" . str_replace(Array ("\r\n", "\r"), "\n", $string);
}
/**
* Update to 5.2.0-RC1
*
* @param string $mode when called mode {before, after)
*/
public function Upgrade_5_2_0_RC1($mode)
{
if ( $mode != 'before' ) {
return;
}
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
// make some promo block fields translatable
$ml_helper->createFields('promo-block');
$table_name = $this->Application->getUnitConfig('promo-block')->getTableName();
$table_structure = $this->Conn->Query('DESCRIBE ' . $table_name, 'Field');
if ( isset($table_structure['Title']) ) {
$sql = 'UPDATE ' . $table_name . '
SET l' . $this->Application->GetDefaultLanguageId() . '_Title = Title';
$this->Conn->Query($sql);
$sql = 'ALTER TABLE ' . $table_name . ' DROP Title';
$this->Conn->Query($sql);
}
// fix e-mail event translations
$languages = $ml_helper->getLanguages();
$table_name = $this->Application->getUnitConfig('email-template')->getTableName();
$change_fields = Array ('Subject', 'HtmlBody', 'PlainTextBody');
foreach ($languages as $language_id) {
foreach ($change_fields as $change_field) {
$change_field = 'l' . $language_id . '_' . $change_field;
$sql = "UPDATE " . $table_name . "
SET {$change_field} = REPLACE({$change_field}, '<inp2:m_BaseUrl/>', '<inp2:m_Link template=\"index\"/>')
WHERE {$change_field} LIKE '%m_BaseURL%'";
$this->Conn->Query($sql);
}
}
// add new ml columns to phrases/e-mail events
$ml_helper->createFields('phrases');
$ml_helper->createFields('email-template');
}
/**
* Update to 5.2.0
*
* @param string $mode when called mode {before, after)
*/
public function Upgrade_5_2_0($mode)
{
if ( $mode != 'after' ) {
return;
}
$table_name = $this->Application->getUnitConfig('c')->getTableName();
$sql = 'SELECT NamedParentPath, CachedTemplate, CategoryId
FROM ' . $table_name;
$categories = $this->Conn->GetIterator($sql);
foreach ($categories as $category_data) {
$fields_hash = Array (
'NamedParentPathHash' => kUtil::crc32(mb_strtolower(preg_replace('/^Content\//i', '', $category_data['NamedParentPath']))),
'CachedTemplateHash' => kUtil::crc32(mb_strtolower($category_data['CachedTemplate'])),
);
$this->Conn->doUpdate($fields_hash, $table_name, 'CategoryId = ' . $category_data['CategoryId']);
}
$rebuild_mode = $this->Application->ConfigValue('QuickCategoryPermissionRebuild') ? CategoryPermissionRebuild::SILENT : CategoryPermissionRebuild::AUTOMATIC;
$this->Application->SetConfigValue('CategoryPermissionRebuildMode', $rebuild_mode);
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'SystemSettings WHERE VariableName = "QuickCategoryPermissionRebuild"';
$this->Conn->Query($sql);
$this->_updateScheduledTaskRunSchedule();
}
/**
* Transforms RunInterval into RunSchedule column for Scheduled Tasks
*
* @return void
* @access protected
*/
protected function _updateScheduledTaskRunSchedule()
{
// minute hour day_of_month month day_of_week
$config = $this->Application->getUnitConfig('scheduled-task');
$id_field = $config->getIDField();
$table_name = $config->getTableName();
$sql = 'SELECT RunInterval, ' . $id_field . '
FROM ' . $table_name;
$run_intervals = $this->Conn->GetCol($sql, $id_field);
$ranges = Array (0 => 'min', 1 => 'hour', 2 => 'day', 3 => 'month');
$range_values = Array ('min' => 60, 'hour' => 60, 'day' => 24, 'month' => 30);
$range_masks = Array ('min' => '*/%s * * * *', 'hour' => '0 */%s * * *', 'day' => '0 0 */%s * *', 'month' => '0 0 1 */%s *');
foreach ($run_intervals as $scheduled_task_id => $interval) {
$mask_index = 'month';
foreach ($ranges as $range_index => $range_name) {
$range_value = $range_values[$range_name];
if ( $interval >= $range_value ) {
$interval = ceil($interval / $range_value);
}
else {
$mask_index = $ranges[$range_index - 1];
break;
}
}
$run_schedule = sprintf($range_masks[$mask_index], $interval);
if ( $run_schedule == '0 0 */7 * *' ) {
// once in 7 days = once in a week
$run_schedule = '0 0 * * 0';
}
$run_schedule = preg_replace('/(\*\/1( |$))/', '*\\2', $run_schedule);
$fields_hash = Array ('RunSchedule' => $run_schedule);
$this->Conn->doUpdate($fields_hash, $table_name, $id_field . ' = ' . $scheduled_task_id);
}
// drop RunInterval column
$this->Conn->Query('ALTER TABLE ' . $table_name . ' DROP RunInterval');
}
/**
* Update to 5.2.1-B1
*
* @param string $mode when called mode {before, after)
*/
public function Upgrade_5_2_1_B1($mode)
{
if ( $mode != 'after' ) {
return;
}
$this->_updateUserPasswords();
}
protected function _updateUserPasswords()
{
$user_table = $this->Application->getUnitConfig('u')->getTableName();
$sql = 'SELECT Password, PortalUserId
FROM ' . $user_table . '
WHERE PasswordHashingMethod = ' . PasswordHashingMethod::MD5;
$user_passwords = $this->Conn->GetColIterator($sql, 'PortalUserId');
if ( !count($user_passwords) ) {
// no users at all or existing users have converted passwords already
return;
}
kUtil::setResourceLimit();
$password_formatter = $this->Application->recallObject('kPasswordFormatter');
/* @var $password_formatter kPasswordFormatter */
foreach ($user_passwords as $user_id => $user_password) {
$fields_hash = Array (
'Password' => $password_formatter->hashPassword($user_password, '', PasswordHashingMethod::MD5_AND_PHPPASS),
'PasswordHashingMethod' => PasswordHashingMethod::MD5_AND_PHPPASS,
);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'Users', 'PortalUserId = ' . $user_id);
}
}
/**
* Update to 5.3.0-B1
*
* @param string $mode when called mode {before, after)
*/
public function Upgrade_5_3_0_B1($mode)
{
if ( $mode == 'before' ) {
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
// add new ml columns to phrases/e-mail events
$ml_helper->createFields('phrases');
$ml_helper->createFields('emailevents');
}
elseif ( $mode == 'after' ) {
$sql = 'SELECT Event, ScheduledTaskId
FROM ' . TABLE_PREFIX . 'ScheduledTasks';
$scheduled_tasks = $this->Conn->GetCol($sql, 'ScheduledTaskId');
foreach ( $scheduled_tasks as $id => $event_string ) {
$event = new kEvent($event_string);
$module = $event->getUnitConfig()->getModule();
$this->Conn->doUpdate(
array('Module' => $module),
TABLE_PREFIX . 'ScheduledTasks',
'ScheduledTaskId = ' . $id
);
}
$this->updateDeploymentLog();
$this->migrateSSLSettings();
$this->migrateSSLSiteDomains();
}
}
/**
* Upgrades project deployment log.
*
* @return void
*/
protected function updateDeploymentLog()
{
$log = $this->Application->recallObject('module-deployment-log', null, array('skip_autoload' => true));
/* @var $log kDBItem */
$table_name = $this->Application->getUnitConfig('mod')->getTableName();
$structure = $this->Conn->Query('DESCRIBE ' . $table_name, 'Field');
if ( !isset($structure['AppliedDBRevisions']) ) {
return;
}
$sql = 'SELECT Name, Path, AppliedDBRevisions
FROM ' . $table_name . '
WHERE COALESCE(AppliedDBRevisions, "") <> ""';
$modules = $this->Conn->Query($sql, 'Name');
foreach ( $modules as $module_name => $module_data ) {
$filename = FULL_PATH . DIRECTORY_SEPARATOR . $module_data['Path'] . 'install/project_upgrades.sql';
if ( !file_exists($filename) ) {
continue;
}
$sqls = file_get_contents($filename);
preg_match_all("/# r([\d]+)([^\:]*):(.*?)(\n|$)/s", $sqls, $matches, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
if ( !$matches ) {
continue;
}
$revisions = array_flip(explode(',', $module_data['AppliedDBRevisions']));
foreach ( $matches as $match ) {
$revision_number = $match[1][0];
if ( !isset($revisions[$revision_number]) ) {
continue;
}
$log->Clear();
$log->SetDBField('Module', $module_name);
$log->SetDBField('RevisionNumber', $revision_number);
$log->SetDBField('RevisionTitle', trim($match[3][0]));
$log->SetDBField('IPAddress', '0.0.0.0');
$log->SetDBField('Output', 'IMPORTED');
$log->Create();
}
}
$sql = 'ALTER TABLE ' . $table_name . ' DROP AppliedDBRevisions';
$this->Conn->Query($sql);
}
/**
* Migrates SSL settings to new format.
*
* @return void
*/
protected function migrateSSLSettings()
{
$mapping = array(
'SSL_URL' => 'SSLDomain',
'AdminSSL_URL' => 'AdminSSLDomain',
);
foreach ( $mapping as $old_setting => $new_setting ) {
$old_value = $this->Application->ConfigValue($old_setting);
if ( $old_value ) {
$this->Application->SetConfigValue($new_setting, parse_url($old_value, PHP_URL_HOST));
}
$sql = 'DELETE FROM ' . $this->Application->getUnitConfig('conf')->getTableName() . '
WHERE VariableName = ' . $this->Conn->qstr($old_setting);
$this->Conn->Query($sql);
}
}
/**
* Migrates SSL site domains.
*
* @return void
*/
protected function migrateSSLSiteDomains()
{
/** @var kDBItem $object */
$object = $this->Application->recallObject('site-domain.migrate', null, array('skip_autoload' => true));
$sql = 'SELECT *
FROM ' . $this->Application->getUnitConfig('site-domain')->getTableName() . '
WHERE SSLDomainName <> ""';
$site_domains = $this->Conn->Query($sql);
foreach ( $site_domains as $site_domain ) {
if ( strpos($site_domain['SSLDomainName'], '//') === false ) {
continue;
}
// use object to reset domain cache as well
$object->LoadFromHash($site_domain);
$object->SetDBField('SSLDomainName', parse_url($site_domain['SSLDomainName'], PHP_URL_HOST));
$object->Update();
}
}
- }
\ No newline at end of file
+ }
Index: branches/5.3.x/core/install/install_toolkit.php
===================================================================
--- branches/5.3.x/core/install/install_toolkit.php (revision 16155)
+++ branches/5.3.x/core/install/install_toolkit.php (revision 16156)
@@ -1,1028 +1,1028 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
/**
* Upgrade sqls are located using this mask
*
*/
define('UPGRADES_FILE', FULL_PATH.'/%sinstall/upgrades.%s');
/**
* Prerequisit check classes are located using this mask
*
*/
define('PREREQUISITE_FILE', FULL_PATH.'/%sinstall/prerequisites.php');
/**
* Format of version identificator in upgrade files (normal, beta, release candidate)
*
*/
define('VERSION_MARK', '# ===== v ([\d]+\.[\d]+\.[\d]+|[\d]+\.[\d]+\.[\d]+-B[\d]+|[\d]+\.[\d]+\.[\d]+-RC[\d]+) =====');
if (!defined('GET_LICENSE_URL')) {
/**
* Url used for retrieving user licenses from Intechnic licensing server
*
*/
define('GET_LICENSE_URL', 'http://www.in-portal.com/license.php');
}
/**
* Misc functions, that are required during installation, when
*
*/
class kInstallToolkit {
/**
* Reference to kApplication class object
*
* @var kApplication
*/
var $Application = null;
/**
* Connection to database
*
* @var IDBConnection
*/
var $Conn = null;
/**
* System config object to access data from config.php
*
* @var Array
*/
public $systemConfig;
/**
* Installator instance
*
* @var kInstallator
*/
var $_installator = null;
function kInstallToolkit()
{
$this->systemConfig = new kSystemConfig(true, false);
if ( class_exists('kApplication') ) {
// auto-setup in case of separate module install
$this->Application =& kApplication::Instance();
$this->Application->Init(); // needed for standalone module install
$this->Conn =& $this->Application->GetADODBConnection();
}
}
/**
* Sets installator
*
* @param kInstallator $instance
*/
function setInstallator(&$instance)
{
$this->_installator =& $instance;
}
/**
* Checks prerequisities before module install or upgrade
*
* @param string $module_path
* @param string $versions
* @param string $mode upgrade mode = {install, standalone, upgrade}
* @return bool
*/
function CheckPrerequisites($module_path, $versions, $mode)
{
if ( !$versions ) {
return Array ();
}
$prerequisite_object =& $this->getPrerequisiteObject($module_path);
/* @var $prerequisite_object InPortalPrerequisites */
// some errors possible
return is_object($prerequisite_object) ? $prerequisite_object->CheckPrerequisites($versions, $mode) : Array ();
}
/**
* Call prerequisites method
*
* @param string $module_path
* @param string $method
* @return array
*/
function CallPrerequisitesMethod($module_path, $method)
{
$prerequisite_object =& $this->getPrerequisiteObject($module_path);
/* @var $prerequisite_object InPortalPrerequisites */
return is_object($prerequisite_object) ? $prerequisite_object->$method() : false;
}
/**
* Returns prerequisite object to be used for checks
*
* @param string $module_path
* @return kHelper
* @access protected
*/
protected function &getPrerequisiteObject($module_path)
{
static $prerequisite_classes = Array ();
$prerequisites_file = sprintf(PREREQUISITE_FILE, $module_path);
if ( !file_exists($prerequisites_file) ) {
$false = false;
return $false;
}
if ( !isset($prerequisite_classes[$module_path]) ) {
// save class name, because 2nd time
// (in after call $prerequisite_class variable will not be present)
include_once $prerequisites_file;
$prerequisite_classes[$module_path] = $prerequisite_class;
}
$prerequisite_object = new $prerequisite_classes[$module_path]();
/* @var $prerequisite_object InPortalPrerequisites */
if ( method_exists($prerequisite_object, 'setToolkit') ) {
$prerequisite_object->setToolkit($this);
}
return $prerequisite_object;
}
/**
* Processes one license, received from server
*
* @param string $file_data
*/
function processLicense($file_data)
{
- $modules_helper = $this->Application->recallObject('ModulesHelper');
+ $modules_helper = $this->Application->recallObject('kModulesHelper');
/* @var $modules_helper kModulesHelper */
$file_data = explode('Code==:', $file_data);
$file_data[0] = str_replace('In-Portal License File - do not edit!' . "\n", '', $file_data[0]);
$file_data = array_map('trim', $file_data);
if ($modules_helper->verifyLicense($file_data[0])) {
$this->systemConfig->set('License', 'Intechnic', $file_data[0]);
if (array_key_exists(1, $file_data)) {
$this->systemConfig->set('LicenseCode', 'Intechnic', $file_data[1]);
}
else {
$this->systemConfig->set('LicenseCode', 'Intechnic');
}
$this->systemConfig->save();
}
else {
// invalid license received from licensing server
$this->_installator->errorMessage = 'Invalid License File';
}
}
/**
* Saves given configuration values to database
*
* @param Array $config
*/
function saveConfigValues($config)
{
foreach ($config as $config_var => $value) {
$sql = 'UPDATE ' . TABLE_PREFIX . 'SystemSettings
SET VariableValue = ' . $this->Conn->qstr($value) . '
WHERE VariableName = ' . $this->Conn->qstr($config_var);
$this->Conn->Query($sql);
}
}
/**
* Sets module version to passed
*
* @param string $module_name
* @param string|bool $module_path
* @param string|bool $version
*/
function SetModuleVersion($module_name, $module_path = false, $version = false)
{
if ($version === false) {
if (!$module_path) {
throw new Exception('Module path must be given to "SetModuleVersion" method to auto-detect version');
}
$version = $this->GetMaxModuleVersion($module_path);
}
// get table prefix from config, because application may not be available here
$table_prefix = $this->systemConfig->get('TablePrefix', 'Database');
if ($module_name == 'kernel') {
$module_name = 'in-portal';
}
$sql = 'UPDATE ' . $table_prefix . 'Modules
SET Version = "' . $version . '", BuildDate = ' . time() . '
WHERE LOWER(Name) = "' . strtolower($module_name) . '"';
$this->Conn->Query($sql);
}
/**
* Sets module root category to passed
*
* @param string $module_name
* @param int $category_id
*/
function SetModuleRootCategory($module_name, $category_id = 0)
{
// get table prefix from config, because application may not be available here
$table_prefix = $this->systemConfig->get('TablePrefix', 'Database');
if ($module_name == 'kernel') {
$module_name = 'in-portal';
}
$sql = 'UPDATE ' . $table_prefix . 'Modules
SET RootCat = ' . $category_id . '
WHERE LOWER(Name) = "' . strtolower($module_name) . '"';
$this->Conn->Query($sql);
}
/**
* Returns maximal version of given module by scanning it's upgrade scripts
*
* @param string $module_path
* @return string
*/
function GetMaxModuleVersion($module_path)
{
$module_path = rtrim(mb_strtolower($module_path), '/');
$upgrades_file = sprintf(UPGRADES_FILE, $module_path . '/', 'sql');
if (!file_exists($upgrades_file)) {
// no upgrade file
return '5.0.0';
}
$sqls = file_get_contents($upgrades_file);
$versions_found = preg_match_all('/'.VERSION_MARK.'/s', $sqls, $regs);
if (!$versions_found) {
// upgrades file doesn't contain version definitions
return '5.0.0';
}
return end($regs[1]);
}
/**
* Runs SQLs from file
*
* @param string $filename
* @param mixed $replace_from
* @param mixed $replace_to
*/
function RunSQL($filename, $replace_from = null, $replace_to = null)
{
if (!file_exists(FULL_PATH.$filename)) {
return ;
}
$sqls = file_get_contents(FULL_PATH.$filename);
if (!$this->RunSQLText($sqls, $replace_from, $replace_to)) {
if (is_object($this->_installator)) {
$this->_installator->Done();
}
else {
if (isset($this->Application)) {
$this->Application->Done();
}
exit;
}
}
}
/**
* Runs SQLs from string
*
* @param string $sqls
* @param mixed $replace_from
* @param mixed $replace_to
* @param int $start_from
* @return bool
*/
function RunSQLText(&$sqls, $replace_from = null, $replace_to = null, $start_from = 0)
{
$table_prefix = $this->systemConfig->get('TablePrefix', 'Database');
// add prefix to all tables
if (strlen($table_prefix) > 0) {
$replacements = Array ('INSERT INTO ', 'UPDATE ', 'ALTER TABLE ', 'DELETE FROM ', 'REPLACE INTO ');
foreach ($replacements as $replacement) {
$sqls = str_replace($replacement, $replacement . $table_prefix, $sqls);
}
}
$sqls = str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ' . $table_prefix, $sqls);
$sqls = str_replace('DROP TABLE ', 'DROP TABLE IF EXISTS ' . $table_prefix, $sqls);
$sqls = str_replace('<%TABLE_PREFIX%>', $table_prefix, $sqls);
$primary_language = is_object($this->Application) ? $this->Application->GetDefaultLanguageId() : 1;
$sqls = str_replace('<%PRIMARY_LANGUAGE%>', $primary_language, $sqls);
if (isset($replace_from) && isset($replace_to)) {
// replace something additionally, e.g. module root category
$sqls = str_replace($replace_from, $replace_to, $sqls);
}
$sqls = str_replace("\r\n", "\n", $sqls); // convert to linux line endings
$no_comment_sqls = preg_replace("/#\s([^;]*?)\n/is", '', $sqls); // remove all comments "#" on new lines
if ($no_comment_sqls === null) {
// "ini.pcre.backtrack-limit" reached and error happened
$sqls = explode(";\n", $sqls . "\n"); // ensures that last sql won't have ";" in it
$sqls = array_map('trim', $sqls);
// remove all comments "#" on new lines (takes about 2 seconds for 53000 sqls)
$sqls = preg_replace("/#\s([^;]*?)/", '', $sqls);
}
else {
$sqls = explode(";\n", $no_comment_sqls . "\n"); // ensures that last sql won't have ";" in it
$sqls = array_map('trim', $sqls);
}
$sql_count = count($sqls);
$db_collation = $this->systemConfig->get('DBCollation', 'Database');
for ($i = $start_from; $i < $sql_count; $i++) {
$sql = $sqls[$i];
if (!$sql || (substr($sql, 0, 1) == '#')) {
continue; // usually last line
}
if (substr($sql, 0, 13) == 'CREATE TABLE ' && $db_collation) {
// it is CREATE TABLE statement -> add collation
$sql .= ' COLLATE \'' . $db_collation . '\'';
}
$this->Conn->Query($sql);
if ($this->Conn->getErrorCode() != 0) {
if (is_object($this->_installator)) {
$this->_installator->errorMessage = 'Error: ('.$this->Conn->getErrorCode().') '.$this->Conn->getErrorMsg().'<br /><br />Last Database Query:<br /><textarea cols="70" rows="10" readonly>'.htmlspecialchars($sql, ENT_QUOTES, 'UTF-8').'</textarea>';
$this->_installator->LastQueryNum = $i + 1;
}
return false;
}
}
return true;
}
/**
* Performs clean language import from given xml file
*
* @param string $lang_file
* @param bool $upgrade
* @todo Import for "core/install/english.lang" (322KB) takes 18 seconds to work on Windows
*/
function ImportLanguage($lang_file, $upgrade = false)
{
$lang_file = FULL_PATH.$lang_file.'.lang';
if (!file_exists($lang_file)) {
return ;
}
$language_import_helper = $this->Application->recallObject('LanguageImportHelper');
/* @var $language_import_helper LanguageImportHelper */
if ( !$upgrade ) {
$language_import_helper->setOption(LanguageImportHelper::OVERWRITE_EXISTING);
}
$language_import_helper->performImport($lang_file, '|0|1|2|', '');
}
/**
* Converts module version in format X.Y.Z[-BN/-RCM] to signle integer
*
* @param string $version
* @return int
*/
function ConvertModuleVersion($version)
{
if (preg_match('/(.*)-(B|RC)([\d]+)/', $version, $regs)) {
// -B<M> or RC-<N>
$parts = explode('.', $regs[1]);
$parts[] = $regs[2] == 'B' ? 1 : 2; // B reliases goes before RC releases
$parts[] = $regs[3];
}
else {
// releases without B/RC marks go after any B/RC releases
$parts = explode('.', $version . '.3.100');
}
$bin = '';
foreach ($parts as $part_index => $part) {
if ($part_index == 3) {
// version type only can be 1/2/3 (11 in binary form), so don't use padding at all
$pad_count = 2;
}
else {
$pad_count = 8;
}
$bin .= str_pad(decbin($part), $pad_count, '0', STR_PAD_LEFT);
}
return bindec($bin);
}
/**
* Returns themes, found in system
*
* @param bool $rebuild
* @return int
*/
function getThemes($rebuild = false)
{
if ($rebuild) {
$this->rebuildThemes();
}
$theme_config = $this->Application->getUnitConfig('theme');
$id_field = $theme_config->getIDField();
$sql = 'SELECT Name, ' . $id_field . '
FROM ' . $theme_config->getTableName() . '
ORDER BY Name ASC';
return $this->Conn->GetCol($sql, $id_field);
}
/**
* Checks if system config is present and is not empty
*
* @return bool
*/
function systemConfigFound()
{
return $this->systemConfig->exists();
}
/**
* Checks if given section is present in config
*
* @param string $section
* @return bool
*/
function sectionFound($section)
{
return $this->systemConfig->sectionFound($section);
}
/**
* Returns formatted module name based on it's root folder
*
* @param string $module_folder
* @return string
*/
function getModuleName($module_folder)
{
return implode('-', array_map('ucfirst', explode('-', $module_folder)));
}
/**
* Returns information about module (based on "install/module_info.xml" file)
*
* @param string $module_name
* @return Array
*/
function getModuleInfo($module_name)
{
if ( $module_name == 'core' ) {
$info_file = FULL_PATH . '/' . $module_name . '/install/module_info.xml';
}
else {
$info_file = MODULES_PATH . '/' . $module_name . '/install/module_info.xml';
}
if ( !file_exists($info_file) ) {
return Array ();
}
$ret = Array ();
$module_info = simplexml_load_file($info_file);
if ( $module_info === false ) {
// non-valid xml file
return Array ();
}
foreach ($module_info as $node) {
/* @var $node SimpleXMLElement */
$ret[strtolower($node->getName())] = trim($node);
}
return $ret;
}
/**
* Returns nice module string to be used on install/upgrade screens
*
* @param string $module_name
* @param string $version_string
* @return string
*/
function getModuleString($module_name, $version_string)
{
// image (if exists) <description> (<name> <version>)
$ret = Array ();
$module_info = $this->getModuleInfo($module_name);
if (array_key_exists('name', $module_info) && $module_info['name']) {
$module_name = $module_info['name'];
}
else {
$module_name = $this->getModuleName($module_name);
}
if (array_key_exists('image', $module_info) && $module_info['image']) {
$image_src = $module_info['image'];
if (!preg_match('/^(http|https):\/\//', $image_src)) {
// local image -> make absolute url
$image_src = $this->Application->BaseURL() . $image_src;
}
$ret[] = '<img src="' . $image_src . '" alt="' . htmlspecialchars($module_name, ENT_QUOTES, 'UTF-8') . '" title="' . htmlspecialchars($module_name, ENT_QUOTES, 'UTF-8') . '" style="vertical-align:middle; margin: 3px 0 3px 5px"/>';
}
if (array_key_exists('description', $module_info) && $module_info['description']) {
$ret[] = $module_info['description'];
}
else {
$ret[] = $module_name;
}
$ret[] = '(' . $module_name . ' ' . $version_string . ')';
return implode(' ', $ret);
}
/**
* Creates module root category in "Home" category using given data and returns it
*
* @param string $name
* @param string $description
* @param string $category_template
* @param string $category_icon
* @return kDBItem
*/
function &createModuleCategory($name, $description, $category_template = null, $category_icon = null)
{
static $fields = null;
if ( !isset($fields) ) {
$ml_formatter = $this->Application->recallObject('kMultiLanguage');
/* @var $ml_formatter kMultiLanguage */
$fields['name'] = $ml_formatter->LangFieldName('Name');
$fields['description'] = $ml_formatter->LangFieldName('Description');
}
$category = $this->Application->recallObject('c', null, Array ('skip_autoload' => true));
/* @var $category kDBItem */
$category_fields = Array (
$fields['name'] => $name, 'Filename' => $name, 'AutomaticFilename' => 1,
$fields['description'] => $description, 'Status' => STATUS_ACTIVE, 'Priority' => -9999,
// prevents empty link to module category on spearate module install
'NamedParentPath' => 'Content/' . $name,
);
$category_fields['ParentId'] = $this->Application->getBaseCategory();
if ( isset($category_template) ) {
$category_fields['Template'] = $category_template;
$category_fields['CachedTemplate'] = $category_template;
}
if ( isset($category_icon) ) {
$category_fields['UseMenuIconUrl'] = 1;
$category_fields['MenuIconUrl'] = $category_icon;
}
$category->Clear();
$category->SetDBFieldsFromHash($category_fields);
$category->Create();
$priority_helper = $this->Application->recallObject('PriorityHelper');
/* @var $priority_helper kPriorityHelper */
$event = new kEvent('c:OnListBuild');
// ensure, that newly created category has proper value in Priority field
$priority_helper->recalculatePriorities($event, 'ParentId = ' . $category_fields['ParentId']);
// update Priority field in object, becase "CategoriesItem::Update" method will be called
// from "kInstallToolkit::setModuleItemTemplate" and otherwise will set 0 to Priority field
$sql = 'SELECT Priority
FROM ' . $category->TableName . '
WHERE ' . $category->IDField . ' = ' . $category->GetID();
$category->SetDBField('Priority', $this->Conn->GetOne($sql));
return $category;
}
/**
* Sets category item template into custom field for given prefix
*
* @param kDBItem $category
* @param string $prefix
* @param string $item_template
*/
function setModuleItemTemplate(&$category, $prefix, $item_template)
{
$this->Application->removeObject('c-cdata');
// recreate all fields, because custom fields are added during install script
$category->Configure();
$category->SetDBField('cust_' . $prefix .'_ItemTemplate', $item_template);
$category->Update();
}
/**
* Link custom field records with search config records + create custom field columns
*
* @param string $module_folder
* @param string $prefix
* @param int $item_type
*/
function linkCustomFields($module_folder, $prefix, $item_type)
{
$module_folder = strtolower($module_folder);
$module_name = $module_folder;
if ( $module_folder == 'kernel' ) {
$module_name = 'in-portal';
$module_folder = 'core';
}
$db =& $this->Application->GetADODBConnection();
$sql = 'SELECT FieldName, CustomFieldId
FROM ' . TABLE_PREFIX . 'CustomFields
WHERE Type = ' . $item_type . ' AND IsSystem = 0'; // config is not read here yet :( $this->Application->getUnitConfig('p')->getItemType();
$custom_fields = $db->GetCol($sql, 'CustomFieldId');
foreach ($custom_fields as $cf_id => $cf_name) {
$sql = 'UPDATE ' . TABLE_PREFIX . 'SearchConfig
SET CustomFieldId = ' . $cf_id . '
WHERE (TableName = "CustomFields") AND (LOWER(ModuleName) = "' . $module_name . '") AND (FieldName = ' . $db->qstr($cf_name) . ')';
$db->Query($sql);
}
$this->Application->refreshModuleInfo(); // this module configs are now processed
// because of configs was read only from installed before modules (in-portal), then reread configs
$this->Application->UnitConfigReader->scanModules(MODULES_PATH . DIRECTORY_SEPARATOR . $module_folder);
// create correct columns in CustomData table
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$ml_helper->createFields($prefix . '-cdata', true);
}
/**
* Deletes cache, useful after separate module install and installator last step
*
* @param bool $refresh_permissions
* @return void
*/
function deleteCache($refresh_permissions = false)
{
$this->Application->HandleEvent(new kEvent('adm:OnResetMemcache')); // not in DB = 100% invalidate
$this->Application->HandleEvent(new kEvent('adm:OnResetConfigsCache'));
$this->Application->HandleEvent(new kEvent('adm:OnResetSections'));
$this->Application->HandleEvent(new kEvent('c:OnResetCMSMenuCache'));
$this->Conn->Query('DELETE FROM ' . TABLE_PREFIX . 'CachedUrls');
if ( $refresh_permissions ) {
$rebuild_mode = $this->Application->ConfigValue('CategoryPermissionRebuildMode');
if ( $rebuild_mode == CategoryPermissionRebuild::SILENT ) {
// refresh permission without progress bar
$updater = $this->Application->makeClass('kPermCacheUpdater');
/* @var $updater kPermCacheUpdater */
$updater->OneStepRun();
}
elseif ( $rebuild_mode == CategoryPermissionRebuild::AUTOMATIC ) {
// refresh permissions with ajax progress bar (when available)
$this->Application->setDBCache('ForcePermCacheUpdate', 1);
}
}
}
/**
* Deletes all temp tables (from active sessions too)
*
*/
function deleteEditTables()
{
$table_prefix = $this->systemConfig->get('TablePrefix', 'Database');
$tables = $this->Conn->GetCol('SHOW TABLES');
$mask_edit_table = '/' . $table_prefix . 'ses_(.*)_edit_(.*)/';
$mask_search_table = '/' . $table_prefix . 'ses_(.*?)_(.*)/';
foreach ($tables as $table) {
if ( preg_match($mask_edit_table, $table, $rets) || preg_match($mask_search_table, $table, $rets) ) {
$this->Conn->Query('DROP TABLE IF EXISTS ' . $table);
}
}
}
/**
* Perform redirect after separate module install
*
* @param string $module_folder
* @param bool $refresh_permissions
*/
function finalizeModuleInstall($module_folder, $refresh_permissions = false)
{
$this->SetModuleVersion(basename($module_folder), $module_folder);
if (!$this->Application->GetVar('redirect')) {
return ;
}
$themes_helper = $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
// use direct query, since module isn't yet in kApplication::ModuleInfo array
$sql = 'SELECT Name
FROM ' . TABLE_PREFIX . 'Modules
WHERE Path = ' . $this->Conn->qstr(rtrim($module_folder, '/') . '/');
$module_name = $this->Conn->GetOne($sql);
$themes_helper->synchronizeModule($module_name);
$ml_helper = $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$ml_helper->massCreateFields();
$this->deleteCache($refresh_permissions);
$url_params = Array (
'pass' => 'm', 'admin' => 1,
'RefreshTree' => 1, 'index_file' => 'index.php',
);
$this->Application->Redirect('modules/modules_list', $url_params);
}
/**
* Performs rebuild of themes
*
*/
function rebuildThemes()
{
$this->Application->HandleEvent(new kEvent('adm:OnRebuildThemes'));
}
/**
* Checks that file is writable by group or others
*
* @param string $file
* @return boolean
*/
function checkWritePermissions($file)
{
if (DIRECTORY_SEPARATOR == '\\') {
// windows doen't allow to check permissions (always returns null)
return null;
}
$permissions = fileperms($file);
return $permissions & 0x0010 || $permissions & 0x0002;
}
/**
* Upgrades primary skin to the latest version
*
* @param Array $module_info
* @return string|bool
*/
function upgradeSkin($module_info)
{
$upgrades_file = sprintf(UPGRADES_FILE, $module_info['Path'], 'css');
$data = file_get_contents($upgrades_file);
// get all versions with their positions in file
$versions = Array ();
preg_match_all('/(' . VERSION_MARK . ')/s', $data, $matches, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
$from_version_int = $this->ConvertModuleVersion($module_info['FromVersion']);
foreach ($matches as $index => $match) {
$version_int = $this->ConvertModuleVersion($match[2][0]);
if ( $version_int < $from_version_int ) {
// only process versions, that were released after currently used version
continue;
}
$start_pos = $match[0][1] + strlen($match[0][0]);
$end_pos = array_key_exists($index + 1, $matches) ? $matches[$index + 1][0][1] : mb_strlen($data);
$patch_data = str_replace("\r\n", "\n", substr($data, $start_pos, $end_pos - $start_pos));
$versions[] = Array (
'Version' => $match[2][0],
// fixes trimmed leading spaces by modern text editor
'Data' => ltrim( str_replace("\n\n", "\n \n", $patch_data) ),
);
}
if ( !$versions ) {
// not skin changes -> quit
return true;
}
$primary_skin = $this->Application->recallObject('skin.primary', null, Array ('skip_autoload' => true));
/* @var $primary_skin kDBItem */
$primary_skin->Load(1, 'IsPrimary');
if ( !$primary_skin->isLoaded() ) {
// we always got primary skin, but just in case
return false;
}
$temp_handler = $this->Application->recallObject('skin_TempHandler', 'kTempTablesHandler');
/* @var $temp_handler kTempTablesHandler */
// clone current skin
$cloned_ids = $temp_handler->CloneItems('skin', '', Array ($primary_skin->GetID()));
if ( !$cloned_ids ) {
// can't clone
return false;
}
$skin = $this->Application->recallObject('skin.tmp', null, Array ('skip_autoload' => true));
/* @var $skin kDBItem */
$skin->Load($cloned_ids[0]);
// save css to temp file (for patching)
$skin_file = tempnam('/tmp', 'skin_css_');
$fp = fopen($skin_file, 'w');
fwrite($fp, str_replace("\r\n", "\n", $skin->GetDBField('CSS')));
fclose($fp);
$output = Array ();
$patch_file = tempnam('/tmp', 'skin_patch_');
foreach ($versions as $version_info) {
// for each left version get it's patch and apply to temp file
$fp = fopen($patch_file, 'w');
fwrite($fp, $version_info['Data']);
fclose($fp);
$output[ $version_info['Version'] ] = shell_exec('patch ' . $skin_file . ' ' . $patch_file . ' 2>&1') . "\n";
}
// place temp file content into cloned skin
$skin->SetDBField('Name', 'Upgraded to ' . $module_info['ToVersion']);
$skin->SetDBField('CSS', file_get_contents($skin_file));
$skin->Update();
unlink($skin_file);
unlink($patch_file);
$has_errors = false;
foreach ($output as $version => $version_output) {
$version_errors = trim(preg_replace("/(^|\n)(patching file .*?|Hunk #.*?\.)(\n|$)/m", '', $version_output));
if ( $version_errors ) {
$has_errors = true;
$output[$version] = trim(preg_replace("/(^|\n)(patching file .*?)(\n|$)/m", '', $output[$version]));
}
else {
unset($output[$version]);
}
}
if ( !$has_errors ) {
// copy patched css back to primary skin
$primary_skin->SetDBField('CSS', $skin->GetDBField('CSS'));
$primary_skin->Update();
// delete temporary skin record
$temp_handler->DeleteItems('skin', '', Array ($skin->GetID()));
return true;
}
// put clean skin from new version
$skin->SetDBField('CSS', file_get_contents(FULL_PATH . '/core/admin_templates/incs/style_template.css'));
$skin->Update();
// return output in case of errors
return $output;
}
/**
* Returns cache handlers, that are working
*
* @param string $current
* @return Array
*/
public function getWorkingCacheHandlers($current = null)
{
if ( !isset($current) ) {
$current = $this->systemConfig->get('CacheHandler', 'Misc');
}
$cache_handler = $this->Application->makeClass('kCache');
$cache_handlers = Array (
'Fake' => 'None', 'Memcache' => 'Memcached', 'XCache' => 'XCache', 'Apc' => 'Alternative PHP Cache'
);
foreach ($cache_handlers AS $class_prefix => $title) {
$handler_class = $class_prefix . 'CacheHandler';
if ( !class_exists($handler_class) ) {
unset($cache_handlers[$class_prefix]);
}
else {
$handler = new $handler_class($cache_handler, 'localhost:11211');
/* @var $handler FakeCacheHandler */
if ( !$handler->isWorking() ) {
if ( $current == $class_prefix ) {
$cache_handlers[$class_prefix] .= ' (offline)';
}
else {
unset($cache_handlers[$class_prefix]);
}
}
}
}
return $cache_handlers;
}
/**
* Returns compression engines, that are working
*
* @param string $current
* @return Array
*/
public function getWorkingCompressionEngines($current = null)
{
if ( !isset($current) ) {
$current = $this->systemConfig->get('CompressionEngine', 'Misc');
}
$output = shell_exec('java -version 2>&1');
$compression_engines = Array ('' => 'None', 'yui' => 'YUICompressor (Java)', 'php' => 'PHP-based');
if ( stripos($output, 'java version') === false ) {
if ( $current == 'yui' ) {
$compression_engines['yui'] .= ' (offline)';
}
else {
unset($compression_engines['yui']);
}
}
return $compression_engines;
}
- }
\ No newline at end of file
+ }
Index: branches/5.3.x/core/install/cache/.gitignore
===================================================================
--- branches/5.3.x/core/install/cache/.gitignore (nonexistent)
+++ branches/5.3.x/core/install/cache/.gitignore (revision 16156)
@@ -0,0 +1 @@
+file_hashes.php
Index: branches/5.3.x/core/install/cache/class_structure.php
===================================================================
--- branches/5.3.x/core/install/cache/class_structure.php (nonexistent)
+++ branches/5.3.x/core/install/cache/class_structure.php (revision 16156)
@@ -0,0 +1,341 @@
+<?php
+// @codingStandardsIgnoreFile
+
+/**
+ * This file is automatically @generated. Use 'php tools/build_class_map.php' to rebuild it.
+ */
+return array(
+ 'cache_format' => 1,
+ 'classes' => array(
+ 'AdminEventsHandler' => '/core/units/admin/admin_events_handler.php',
+ 'AdminTagProcessor' => '/core/units/admin/admin_tag_processor.php',
+ 'AjaxFormHelper' => '/core/units/helpers/ajax_form_helper.php',
+ 'ApcCacheHandler' => '/core/kernel/utility/cache.php',
+ 'BackupHelper' => '/core/units/helpers/backup_helper.php',
+ 'BaseSession' => '/core/kernel/session/session.php',
+ 'BaseSessionStorage' => '/core/kernel/session/session_storage.php',
+ 'CacheSettings' => '/core/kernel/startup.php',
+ 'CaptchaEventHandler' => '/core/units/captcha/captcha_eh.php',
+ 'CategoriesEventHandler' => '/core/units/categories/categories_event_handler.php',
+ 'CategoriesItem' => '/core/units/categories/categories_item.php',
+ 'CategoriesTagProcessor' => '/core/units/categories/categories_tag_processor.php',
+ 'CategoryHelper' => '/core/units/helpers/category_helper.php',
+ 'CategoryItemRewrite' => '/core/units/helpers/mod_rewrite_helper.php',
+ 'CategoryItemsEventHandler' => '/core/units/category_items/category_items_event_handler.php',
+ 'CategoryItemsTagProcessor' => '/core/units/category_items/category_items_tag_processor.php',
+ 'CategoryPermissionRebuild' => '/core/kernel/constants.php',
+ 'ChangeLog' => '/core/kernel/constants.php',
+ 'ChangeLogEventHandler' => '/core/units/logs/change_logs/change_log_eh.php',
+ 'ChangeLogTagProcessor' => '/core/units/logs/change_logs/change_log_tp.php',
+ 'ColumnSet' => '/core/units/helpers/col_picker_helper.php',
+ 'ConfigSearchEventHandler' => '/core/units/config_search/config_search_event_handler.php',
+ 'ConfigSearchTagProcessor' => '/core/units/config_search/config_search_tag_processor.php',
+ 'ConfigurationEventHandler' => '/core/units/configuration/configuration_event_handler.php',
+ 'ConfigurationItem' => '/core/units/configuration/configuration.php',
+ 'ConfigurationTagProcessor' => '/core/units/configuration/configuration_tag_processor.php',
+ 'ConfigurationValidator' => '/core/units/configuration/configuration_validator.php',
+ 'ContentEventHandler' => '/core/units/content/content_eh.php',
+ 'ContentTagProcessor' => '/core/units/content/content_tp.php',
+ 'CoreUpgrades' => '/core/install/upgrades.php',
+ 'CountryStateEventHandler' => '/core/units/country_states/country_state_eh.php',
+ 'CssMinifyHelper' => '/core/units/helpers/minifiers/css_minify_helper.php',
+ 'CustomDataEventHandler' => '/core/units/custom_data/custom_data_event_handler.php',
+ 'CustomFieldsEventHandler' => '/core/units/custom_fields/custom_fields_event_handler.php',
+ 'CustomFieldsTagProcessor' => '/core/units/custom_fields/custom_fields_tag_processor.php',
+ 'Debugger' => '/core/kernel/utility/debugger.php',
+ 'DebuggerUtil' => '/core/kernel/utility/debugger.php',
+ 'DeploymentHelper' => '/core/units/helpers/deployment_helper.php',
+ 'DraftEventHandler' => '/core/units/forms/drafts/draft_eh.php',
+ 'EditPickerHelper' => '/core/units/helpers/controls/edit_picker_helper.php',
+ 'EmailDelivery' => '/core/kernel/constants.php',
+ 'EmailLogEventHandler' => '/core/units/logs/email_logs/email_log_eh.php',
+ 'EmailLogStatus' => '/core/kernel/constants.php',
+ 'EmailLogTagProcessor' => '/core/units/logs/email_logs/email_log_tp.php',
+ 'EmailQueueEventHandler' => '/core/units/email_queue/email_queue_eh.php',
+ 'EmailQueueTagProcessor' => '/core/units/email_queue/email_queue_tp.php',
+ 'EmailTemplate' => '/core/kernel/constants.php',
+ 'EmailTemplateEventHandler' => '/core/units/email_templates/email_template_eh.php',
+ 'EmailTemplateTagProcessor' => '/core/units/email_templates/email_template_tp.php',
+ 'FakeCacheHandler' => '/core/kernel/utility/cache.php',
+ 'FavoritesEventHandler' => '/core/units/favorites/favorites_eh.php',
+ 'FckEventHandler' => '/core/units/fck/fck_eh.php',
+ 'FckTagProcessor' => '/core/units/fck/fck_tp.php',
+ 'FileEventHandler' => '/core/units/files/file_eh.php',
+ 'FileHelper' => '/core/units/helpers/file_helper.php',
+ 'FileTagProcessor' => '/core/units/files/file_tp.php',
+ 'FormFieldEventHandler' => '/core/units/forms/form_fields/form_field_eh.php',
+ 'FormFieldsTagProcessor' => '/core/units/forms/form_fields/form_fields_tp.php',
+ 'FormSubmissionHelper' => '/core/units/helpers/form_submission_helper.php',
+ 'FormSubmissionTagProcessor' => '/core/units/forms/form_submissions/form_submission_tp.php',
+ 'FormSubmissionsEventHandler' => '/core/units/forms/form_submissions/form_submissions_eh.php',
+ 'FormsEventHandler' => '/core/units/forms/forms/forms_eh.php',
+ 'FormsTagProcessor' => '/core/units/forms/forms/forms_tp.php',
+ 'GeoCodeHelper' => '/core/units/helpers/geocode_helper.php',
+ 'GroupTagProcessor' => '/core/units/groups/group_tp.php',
+ 'GroupsEventHandler' => '/core/units/groups/groups_event_handler.php',
+ 'IDBConnection' => '/core/kernel/db/i_db_connection.php',
+ 'ImageEventHandler' => '/core/units/images/image_event_handler.php',
+ 'ImageHelper' => '/core/units/helpers/image_helper.php',
+ 'ImageTagProcessor' => '/core/units/images/image_tag_processor.php',
+ 'ImagesItem' => '/core/units/images/images.php',
+ 'InPortalPrerequisites' => '/core/install/prerequisites.php',
+ 'InpCustomFieldsHelper' => '/core/units/helpers/custom_fields_helper.php',
+ 'Intechnic\\InPortal\\Core\\kernel\\utility\\ClassDiscovery\\ClassDetector' => '/core/kernel/utility/ClassDiscovery/ClassDetector.php',
+ 'Intechnic\\InPortal\\Core\\kernel\\utility\\ClassDiscovery\\ClassMapBuilder' => '/core/kernel/utility/ClassDiscovery/ClassMapBuilder.php',
+ 'Intechnic\\InPortal\\Core\\kernel\\utility\\ClassDiscovery\\CodeFolderFilterIterator' => '/core/kernel/utility/ClassDiscovery/CodeFolderFilterIterator.php',
+ 'ItemFilterEventHandler' => '/core/units/filters/item_filter_eh.php',
+ 'ItemFilterTagProcessor' => '/core/units/filters/item_filter_tp.php',
+ 'JSONHelper' => '/core/units/helpers/json_helper.php',
+ 'JsMinifyHelper' => '/core/units/helpers/minifiers/js_minify_helper.php',
+ 'Language' => '/core/kernel/constants.php',
+ 'LanguageImportHelper' => '/core/units/helpers/language_import_helper.php',
+ 'LanguagesEventHandler' => '/core/units/languages/languages_event_handler.php',
+ 'LanguagesItem' => '/core/units/languages/languages_item.php',
+ 'LanguagesTagProcessor' => '/core/units/languages/languages_tag_processor.php',
+ 'LeftJoinOptimizer' => '/core/kernel/db/dblist.php',
+ 'ListHelper' => '/core/units/helpers/list_helper.php',
+ 'LoginResult' => '/core/kernel/constants.php',
+ 'MInputHelper' => '/core/units/helpers/controls/minput_helper.php',
+ 'MailboxHelper' => '/core/units/helpers/mailbox_helper.php',
+ 'MailingList' => '/core/kernel/constants.php',
+ 'MailingListEventHandler' => '/core/units/mailing_lists/mailing_list_eh.php',
+ 'MailingListHelper' => '/core/units/helpers/mailing_list_helper.php',
+ 'MailingListTagProcessor' => '/core/units/mailing_lists/mailing_list_tp.php',
+ 'MaintenanceMode' => '/core/kernel/startup.php',
+ 'MassImageResizer' => '/core/units/admin/admin_events_handler.php',
+ 'MemcacheCacheHandler' => '/core/kernel/utility/cache.php',
+ 'MenuHelper' => '/core/units/helpers/menu_helper.php',
+ 'MimeDecodeHelper' => '/core/units/helpers/mime_decode_helper.php',
+ 'MinifyHelper' => '/core/units/helpers/minifiers/minify_helper.php',
+ 'ModuleDeploymentLog' => '/core/kernel/constants.php',
+ 'ModuleDeploymentLogEventHandler' => '/core/units/logs/module_deployment_logs/module_deployment_log_eh.php',
+ 'ModulesEventHandler' => '/core/units/modules/modules_event_handler.php',
+ 'ModulesTagProcessor' => '/core/units/modules/modules_tag_processor.php',
+ 'NParser' => '/core/kernel/nparser/nparser.php',
+ 'NParserCompiler' => '/core/kernel/nparser/compiler.php',
+ 'POP3Helper' => '/core/units/helpers/pop3_helper.php',
+ 'PageHelper' => '/core/units/helpers/page_helper.php',
+ 'PageRevisionEventHandler' => '/core/units/page_revisions/page_revision_eh.php',
+ 'PageRevisionTagProcessor' => '/core/units/page_revisions/page_revision_tp.php',
+ 'Params' => '/core/kernel/utility/params.php',
+ 'ParserException' => '/core/kernel/nparser/nparser.php',
+ 'PasswordHash' => '/core/kernel/utility/php_pass.php',
+ 'PasswordHashingMethod' => '/core/kernel/constants.php',
+ 'PermissionTypeEventHandler' => '/core/units/permission_types/permission_type_eh.php',
+ 'PermissionsEventHandler' => '/core/units/permissions/permissions_event_handler.php',
+ 'PermissionsTagProcessor' => '/core/units/permissions/permissions_tag_processor.php',
+ 'PhraseTagProcessor' => '/core/units/phrases/phrase_tp.php',
+ 'PhrasesEventHandler' => '/core/units/phrases/phrases_event_handler.php',
+ 'PriorityEventHandler' => '/core/units/priorites/priority_eh.php',
+ 'PromoBlockEventHandler' => '/core/units/promo_blocks/promo_block_eh.php',
+ 'PromoBlockGroupEventHandler' => '/core/units/promo_block_groups/promo_block_group_eh.php',
+ 'PromoBlockGroupTagProcessor' => '/core/units/promo_block_groups/promo_block_group_tp.php',
+ 'PromoBlockTagProcessor' => '/core/units/promo_blocks/promo_block_tp.php',
+ 'PromoBlockType' => '/core/kernel/constants.php',
+ 'RatingHelper' => '/core/units/helpers/rating_helper.php',
+ 'RelatedSearchEventHandler' => '/core/units/related_searches/related_searches_event_handler.php',
+ 'RelatedSearchTagProcessor' => '/core/units/related_searches/related_searches_tag_processor.php',
+ 'RelationshipEventHandler' => '/core/units/relationship/relationship_event_handler.php',
+ 'RelationshipTagProcessor' => '/core/units/relationship/relationship_tp.php',
+ 'ReviewsEventHandler' => '/core/units/reviews/reviews_event_handler.php',
+ 'ReviewsTagProcessor' => '/core/units/reviews/reviews_tag_processor.php',
+ 'ScheduledTask' => '/core/kernel/constants.php',
+ 'ScheduledTaskEventHandler' => '/core/units/scheduled_tasks/scheduled_task_eh.php',
+ 'SelectorsEventHandler' => '/core/units/selectors/selectors_event_handler.php',
+ 'SelectorsItem' => '/core/units/selectors/selectors_item.php',
+ 'SelectorsTagProcessor' => '/core/units/selectors/selectors_tag_processor.php',
+ 'Session' => '/core/kernel/session/inp_session.php',
+ 'SessionLogEventHandler' => '/core/units/logs/session_logs/session_log_eh.php',
+ 'SessionStorage' => '/core/kernel/session/inp_session_storage.php',
+ 'SiteConfigEventHandler' => '/core/units/sections/site_config_eh.php',
+ 'SiteConfigHelper' => '/core/units/helpers/site_config_helper.php',
+ 'SiteConfigTagProcessor' => '/core/units/sections/site_config_tp.php',
+ 'SiteDomainEventHandler' => '/core/units/site_domains/site_domain_eh.php',
+ 'SiteHelper' => '/core/units/helpers/site_helper.php',
+ 'SkinEventHandler' => '/core/units/skins/skin_eh.php',
+ 'SkinHelper' => '/core/units/helpers/skin_helper.php',
+ 'SpamHelper' => '/core/units/helpers/spam_helper.php',
+ 'SpamReportEventHandler' => '/core/units/spam_reports/spam_report_eh.php',
+ 'SpamReportTagProcessor' => '/core/units/spam_reports/spam_report_tp.php',
+ 'StatisticsEventHandler' => '/core/units/statistics/statistics_event_handler.php',
+ 'StatisticsTagProcessor' => '/core/units/statistics/statistics_tag_processor.php',
+ 'StorageEngine' => '/core/kernel/constants.php',
+ 'StylesheetsEventHandler' => '/core/units/stylesheets/stylesheets_event_handler.php',
+ 'StylesheetsItem' => '/core/units/stylesheets/stylesheets_item.php',
+ 'SubmissionFormField' => '/core/kernel/constants.php',
+ 'SubmissionLogEventHandler' => '/core/units/forms/submission_log/submission_log_eh.php',
+ 'SubmissionLogTagProcessor' => '/core/units/forms/submission_log/submission_log_tp.php',
+ 'SystemEventSubscriptionEventHandler' => '/core/units/system_event_subscriptions/system_event_subscription_eh.php',
+ 'SystemEventSubscriptionTagProcessor' => '/core/units/system_event_subscriptions/system_event_subscription_tp.php',
+ 'SystemLogEventHandler' => '/core/units/logs/system_logs/system_log_eh.php',
+ 'SystemLogTagProcessor' => '/core/units/logs/system_logs/system_log_tp.php',
+ 'TemplateHelper' => '/core/units/helpers/template_helper.php',
+ 'TemplatesCache' => '/core/kernel/nparser/template_cache.php',
+ 'ThemeFileEventHandler' => '/core/units/theme_files/theme_file_eh.php',
+ 'ThemeItem' => '/core/units/themes/theme_item.php',
+ 'ThemesEventHandler' => '/core/units/themes/themes_eh.php',
+ 'ThemesTagProcessor' => '/core/units/themes/themes_tag_processor.php',
+ 'ThesaurusEventHandler' => '/core/units/thesaurus/thesaurus_eh.php',
+ 'ThesaurusTagProcessor' => '/core/units/thesaurus/thesaurus_tp.php',
+ 'TranslationSaveMode' => '/core/kernel/constants.php',
+ 'TranslatorEventHandler' => '/core/units/translator/translator_event_handler.php',
+ 'TranslatorTagProcessor' => '/core/units/translator/translator_tp.php',
+ 'UnitConfigDecorator' => '/core/units/admin/admin_events_handler.php',
+ 'UserGroupsEventHandler' => '/core/units/user_groups/user_groups_eh.php',
+ 'UserHelper' => '/core/units/helpers/user_helper.php',
+ 'UserProfileEventHandler' => '/core/units/user_profile/user_profile_eh.php',
+ 'UserProfileTagProcessor' => '/core/units/user_profile/user_profile_tp.php',
+ 'UserType' => '/core/kernel/constants.php',
+ 'UsersEventHandler' => '/core/units/users/users_event_handler.php',
+ 'UsersItem' => '/core/units/users/users_item.php',
+ 'UsersSyncronize' => '/core/units/users/users_syncronize.php',
+ 'UsersSyncronizeManager' => '/core/units/users/users_syncronize.php',
+ 'UsersTagProcessor' => '/core/units/users/users_tag_processor.php',
+ 'VisitsEventHandler' => '/core/units/visits/visits_event_handler.php',
+ 'VisitsList' => '/core/units/visits/visits_list.php',
+ 'VisitsTagProcessor' => '/core/units/visits/visits_tag_processor.php',
+ 'XCacheCacheHandler' => '/core/kernel/utility/cache.php',
+ 'XMLIterator' => '/core/units/helpers/xml_helper5.php',
+ '_BlockTag' => '/core/kernel/nparser/ntags.php',
+ '_Tag_Cache' => '/core/kernel/nparser/ntags.php',
+ '_Tag_Capture' => '/core/kernel/nparser/ntags.php',
+ '_Tag_Comment' => '/core/kernel/nparser/ntags.php',
+ '_Tag_Compress' => '/core/kernel/nparser/ntags.php',
+ '_Tag_DefaultParam' => '/core/kernel/nparser/ntags.php',
+ '_Tag_DefineElement' => '/core/kernel/nparser/ntags.php',
+ '_Tag_If' => '/core/kernel/nparser/ntags.php',
+ '_Tag_IfDataExists' => '/core/kernel/nparser/ntags.php',
+ '_Tag_IfNot' => '/core/kernel/nparser/ntags.php',
+ '_Tag_Include' => '/core/kernel/nparser/ntags.php',
+ '_Tag_Param' => '/core/kernel/nparser/ntags.php',
+ '_Tag_RenderElement' => '/core/kernel/nparser/ntags.php',
+ '_Tag_RenderElements' => '/core/kernel/nparser/ntags.php',
+ '_Tag_SetParam' => '/core/kernel/nparser/ntags.php',
+ 'clsCachedPermissions' => '/core/units/categories/cache_updater.php',
+ 'clsRecursionStack' => '/core/units/categories/cache_updater.php',
+ 'fckFCKHelper' => '/core/units/helpers/fck_helper.php',
+ 'kApplication' => '/core/kernel/application.php',
+ 'kArray' => '/core/kernel/utility/params.php',
+ 'kBase' => '/core/kernel/kbase.php',
+ 'kBracketsHelper' => '/core/units/helpers/brackets_helper.php',
+ 'kCCDateFormatter' => '/core/kernel/utility/formatters/ccdate_formatter.php',
+ 'kCSSDefaults' => '/core/units/pdf/css_defaults.php',
+ 'kCSVHelper' => '/core/units/helpers/csv_helper.php',
+ 'kCache' => '/core/kernel/utility/cache.php',
+ 'kCacheHandler' => '/core/kernel/utility/cache.php',
+ 'kCacheManager' => '/core/kernel/managers/cache_manager.php',
+ 'kCaptchaHelper' => '/core/units/helpers/captcha_helper.php',
+ 'kCatDBEventHandler' => '/core/kernel/db/cat_event_handler.php',
+ 'kCatDBItem' => '/core/kernel/db/cat_dbitem.php',
+ 'kCatDBItemExportHelper' => '/core/units/helpers/cat_dbitem_export_helper.php',
+ 'kCatDBList' => '/core/kernel/db/cat_dblist.php',
+ 'kCatDBTagProcessor' => '/core/kernel/db/cat_tag_processor.php',
+ 'kChangesFormatter' => '/core/units/logs/change_logs/changes_formatter.php',
+ 'kChartHelper' => '/core/units/helpers/chart_helper.php',
+ 'kClipboardHelper' => '/core/units/helpers/clipboard_helper.php',
+ 'kColumnPickerHelper' => '/core/units/helpers/col_picker_helper.php',
+ 'kCookieHasher' => '/core/kernel/utility/cookie_hasher.php',
+ 'kCountHelper' => '/core/units/helpers/count_helper.php',
+ 'kCountryStatesHelper' => '/core/units/helpers/country_states_helper.php',
+ 'kCronField' => '/core/units/helpers/cron_helper.php',
+ 'kCronHelper' => '/core/units/helpers/cron_helper.php',
+ 'kCurlHelper' => '/core/units/helpers/curl_helper.php',
+ 'kCustomFieldFormatter' => '/core/kernel/utility/formatters/customfield_formatter.php',
+ 'kDBBase' => '/core/kernel/kbase.php',
+ 'kDBConnection' => '/core/kernel/db/db_connection.php',
+ 'kDBConnectionDebug' => '/core/kernel/db/db_connection.php',
+ 'kDBEventHandler' => '/core/kernel/db/db_event_handler.php',
+ 'kDBItem' => '/core/kernel/db/dbitem.php',
+ 'kDBList' => '/core/kernel/db/dblist.php',
+ 'kDBLoadBalancer' => '/core/kernel/db/db_load_balancer.php',
+ 'kDBTagProcessor' => '/core/kernel/db/db_tag_processor.php',
+ 'kDateFormatter' => '/core/kernel/utility/formatters/date_formatter.php',
+ 'kEmail' => '/core/kernel/utility/email.php',
+ 'kEmailSendingHelper' => '/core/kernel/utility/email_send.php',
+ 'kEmailTemplateHelper' => '/core/units/helpers/email_template_helper.php',
+ 'kErrorHandlerStack' => '/core/kernel/utility/logger.php',
+ 'kEvent' => '/core/kernel/utility/event.php',
+ 'kEventHandler' => '/core/kernel/event_handler.php',
+ 'kEventManager' => '/core/kernel/event_manager.php',
+ 'kExceptionHandlerStack' => '/core/kernel/utility/logger.php',
+ 'kFactory' => '/core/kernel/utility/factory.php',
+ 'kFactoryException' => '/core/kernel/utility/factory.php',
+ 'kFilenamesHelper' => '/core/units/helpers/filenames_helper.php',
+ 'kFilesizeFormatter' => '/core/kernel/utility/formatters/filesize_formatter.php',
+ 'kFormatter' => '/core/kernel/utility/formatters/formatter.php',
+ 'kHTTPQuery' => '/core/kernel/utility/http_query.php',
+ 'kHandlerStack' => '/core/kernel/utility/logger.php',
+ 'kHelper' => '/core/kernel/kbase.php',
+ 'kHookManager' => '/core/kernel/managers/hook_manager.php',
+ 'kInstallToolkit' => '/core/install/install_toolkit.php',
+ 'kInstallator' => '/core/install.php',
+ 'kLEFTFormatter' => '/core/kernel/utility/formatters/left_formatter.php',
+ 'kLogger' => '/core/kernel/utility/logger.php',
+ 'kMainTagProcessor' => '/core/kernel/processors/main_processor.php',
+ 'kModulesHelper' => '/core/units/helpers/modules_helper.php',
+ 'kMultiLanguage' => '/core/kernel/utility/formatters/multilang_formatter.php',
+ 'kMultiLanguageHelper' => '/core/units/helpers/multilanguage_helper.php',
+ 'kMultipleFilter' => '/core/kernel/utility/filters.php',
+ 'kMySQLQuery' => '/core/kernel/db/db_connection.php',
+ 'kMySQLQueryCol' => '/core/kernel/db/db_connection.php',
+ 'kNavigationBar' => '/core/units/helpers/navigation_bar.php',
+ 'kNoPermissionException' => '/core/kernel/kbase.php',
+ 'kOpenerStack' => '/core/kernel/utility/opener_stack.php',
+ 'kOptionsFormatter' => '/core/kernel/utility/formatters/options_formatter.php',
+ 'kPDFElemFactory' => '/core/units/pdf/pdf_helper.php',
+ 'kPDFElement' => '/core/units/pdf/pdf_helper.php',
+ 'kPDFHelper' => '/core/units/pdf/pdf_helper.php',
+ 'kPDFImage' => '/core/units/pdf/pdf_image.php',
+ 'kPDFLine' => '/core/units/pdf/pdf_helper.php',
+ 'kPDFRenderer' => '/core/units/pdf/pdf_renderer.php',
+ 'kPDFStylesheet' => '/core/units/pdf/pdf_styles.php',
+ 'kPDFTable' => '/core/units/pdf/pdf_table.php',
+ 'kPDFTableRow' => '/core/units/pdf/pdf_table.php',
+ 'kPDFTextElement' => '/core/units/pdf/pdf_text.php',
+ 'kPasswordFormatter' => '/core/kernel/utility/formatters/password_formatter.php',
+ 'kPermCacheUpdater' => '/core/units/categories/cache_updater.php',
+ 'kPermissionsHelper' => '/core/units/helpers/permissions_helper.php',
+ 'kPhraseCache' => '/core/kernel/languages/phrases_cache.php',
+ 'kPictureFormatter' => '/core/kernel/utility/formatters/upload_formatter.php',
+ 'kPlainUrlProcessor' => '/core/kernel/managers/plain_url_processor.php',
+ 'kPriorityHelper' => '/core/units/helpers/priority_helper.php',
+ 'kRecursiveHelper' => '/core/units/helpers/recursive_helper.php',
+ 'kRedirectException' => '/core/kernel/kbase.php',
+ 'kRequestManager' => '/core/kernel/managers/request_manager.php',
+ 'kRewriteUrlProcessor' => '/core/kernel/managers/rewrite_url_processor.php',
+ 'kScheduledTaskManager' => '/core/kernel/managers/scheduled_task_manager.php',
+ 'kSearchHelper' => '/core/units/helpers/search_helper.php',
+ 'kSectionsHelper' => '/core/units/helpers/sections_helper.php',
+ 'kSerializedFormatter' => '/core/kernel/utility/formatters/serialized_formatter.php',
+ 'kSocket' => '/core/kernel/utility/socket.php',
+ 'kSubscriptionAnalyzer' => '/core/units/system_event_subscriptions/system_event_subscription_tp.php',
+ 'kSubscriptionItem' => '/core/kernel/managers/subscription_manager.php',
+ 'kSubscriptionManager' => '/core/kernel/managers/subscription_manager.php',
+ 'kSystemConfig' => '/core/kernel/utility/system_config.php',
+ 'kSystemConfigException' => '/core/kernel/utility/system_config.php',
+ 'kTCPDFRenderer' => '/core/units/pdf/pdf_renderer_tcpdf.php',
+ 'kTagProcessor' => '/core/kernel/processors/tag_processor.php',
+ 'kTempHandlerSubTable' => '/core/kernel/utility/temp_handler.php',
+ 'kTempHandlerTable' => '/core/kernel/utility/temp_handler.php',
+ 'kTempHandlerTopTable' => '/core/kernel/utility/temp_handler.php',
+ 'kTempTablesHandler' => '/core/kernel/utility/temp_handler.php',
+ 'kThemesHelper' => '/core/units/helpers/themes_helper.php',
+ 'kUnitConfig' => '/core/kernel/utility/unit_config.php',
+ 'kUnitConfigCloner' => '/core/kernel/utility/unit_config_cloner.php',
+ 'kUnitConfigReader' => '/core/kernel/utility/unit_config_reader.php',
+ 'kUnitFormatter' => '/core/kernel/utility/formatters/unit_formatter.php',
+ 'kUpgradeHelper' => '/core/install/upgrade_helper.php',
+ 'kUploadFormatter' => '/core/kernel/utility/formatters/upload_formatter.php',
+ 'kUploadHelper' => '/core/units/helpers/upload_helper.php',
+ 'kUploaderException' => '/core/units/helpers/upload_helper.php',
+ 'kUrlManager' => '/core/kernel/managers/url_manager.php',
+ 'kUrlProcessor' => '/core/kernel/managers/url_processor.php',
+ 'kUtil' => '/core/kernel/globals.php',
+ 'kValidator' => '/core/kernel/utility/validator.php',
+ 'kXMLHelper' => '/core/units/helpers/xml_helper.php',
+ 'kXMLNode' => '/core/units/helpers/xml_helper.php',
+ 'kXMLNode5' => '/core/units/helpers/xml_helper5.php',
+ 'kZendPDFRenderer' => '/core/units/pdf/pdf_renderer_zend.php',
+ 'kiCacheable' => '/core/kernel/interfaces/cacheable.php',
+ ),
+);
Property changes on: branches/5.3.x/core/install/cache/class_structure.php
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Index: branches/5.3.x/core/install/cache
===================================================================
--- branches/5.3.x/core/install/cache (nonexistent)
+++ branches/5.3.x/core/install/cache (revision 16156)
Property changes on: branches/5.3.x/core/install/cache
___________________________________________________________________
Added: svn:ignore
## -0,0 +1 ##
+file_hashes.php
Index: branches/5.3.x/core/install.php
===================================================================
--- branches/5.3.x/core/install.php (revision 16155)
+++ branches/5.3.x/core/install.php (revision 16156)
@@ -1,1777 +1,1777 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_STRICT);
define('IS_INSTALL', 1);
define('ADMIN', 1);
define('FULL_PATH', realpath(dirname(__FILE__).'/..') );
define('REL_PATH', '/core');
// run installator
$install_engine = new kInstallator();
$install_engine->Init();
$install_engine->Run();
$install_engine->Done();
class kInstallator {
/**
* Reference to kApplication class object
*
* @var kApplication
*/
var $Application = null;
/**
* Connection to database
*
* @var IDBConnection
*/
var $Conn = null;
/**
* XML file containing steps information
*
* @var string
*/
var $StepDBFile = '';
/**
* Step name, that currently being processed
*
* @var string
*/
var $currentStep = '';
/**
* Steps list (preset) to use for current installation
*
* @var string
*/
var $stepsPreset = '';
/**
* Installation steps to be done
*
* @var Array
*/
var $steps = Array (
'fresh_install' => Array ('sys_requirements', 'check_paths', 'db_config', 'select_license', /*'download_license',*/ 'select_domain', 'root_password', 'choose_modules', 'post_config', 'sys_config', 'select_theme', 'security', 'finish'),
'clean_reinstall' => Array ('install_setup', 'sys_requirements', 'check_paths', 'clean_db', 'db_config', 'select_license', /*'download_license',*/ 'select_domain', 'root_password', 'choose_modules', 'post_config', 'sys_config', 'select_theme', 'security', 'finish'),
'already_installed' => Array ('check_paths', 'install_setup'),
'upgrade' => Array ('check_paths', 'install_setup', 'sys_config', 'upgrade_modules', 'skin_upgrade', 'security', 'finish'),
'update_license' => Array ('check_paths', 'install_setup', 'select_license', /*'download_license',*/ 'select_domain', 'security', 'finish'),
'update_config' => Array ('check_paths', 'install_setup', 'sys_config', 'security', 'finish'),
'db_reconfig' => Array ('check_paths', 'install_setup', 'db_reconfig', 'security', 'finish'),
'sys_requirements' => Array ('check_paths', 'install_setup', 'sys_requirements', 'security', 'finish')
);
/**
* Steps, that doesn't required admin to be logged-in to proceed
*
* @var Array
*/
var $skipLoginSteps = Array ('sys_requirements', 'check_paths', 'select_license', /*'download_license',*/ 'select_domain', 'root_password', 'choose_modules', 'post_config', 'select_theme', 'security', 'finish', -1);
/**
* Steps, on which kApplication should not be initialized, because of missing correct db table structure
*
* @var Array
*/
var $skipApplicationSteps = Array ('sys_requirements', 'check_paths', 'clean_db', 'db_config', 'db_reconfig' /*, 'install_setup'*/); // remove install_setup when application will work separately from install
/**
* Folders that should be writeable to continue installation. $1 - main writeable folder from config.php ("/system" by default)
*
* @var Array
*/
var $writeableFolders = Array (
'$1',
'$1/.restricted',
'$1/images',
'$1/images/pending',
'$1/images/emoticons', // for "In-Bulletin"
'$1/user_files',
'$1/cache',
);
/**
* Contains last error message text
*
* @var string
*/
var $errorMessage = '';
/**
* Base path for includes in templates
*
* @var string
*/
var $baseURL = '';
/**
* Holds number of last executed query in the SQL
*
* @var int
*/
var $LastQueryNum = 0;
/**
* Dependencies, that should be used in upgrade process
*
* @var Array
*/
var $upgradeDepencies = Array ();
/**
* Log of upgrade - list of upgraded modules and their versions
*
* @var Array
*/
var $upgradeLog = Array ();
/**
* Common tools required for installation process
*
* @var kInstallToolkit
*/
var $toolkit = null;
function Init()
{
include_once(FULL_PATH . REL_PATH . '/kernel/kbase.php'); // required by kDBConnection class
include_once(FULL_PATH . REL_PATH . '/kernel/utility/multibyte.php'); // emulating multi-byte php extension
include_once(FULL_PATH . REL_PATH . '/kernel/utility/system_config.php');
require_once(FULL_PATH . REL_PATH . '/install/install_toolkit.php'); // toolkit required for module installations to installator
$this->toolkit = new kInstallToolkit();
$this->toolkit->setInstallator($this);
$this->StepDBFile = FULL_PATH.'/'.REL_PATH.'/install/steps_db.xml';
$this->baseURL = 'http://' . $_SERVER['HTTP_HOST'] . $this->toolkit->systemConfig->get('WebsitePath', 'Misc') . '/core/install/';
set_error_handler( Array(&$this, 'ErrorHandler') );
if ( $this->toolkit->systemConfigFound() ) {
// if config.php found, then check his write permission too
$this->writeableFolders[] = $this->toolkit->systemConfig->get('WriteablePath', 'Misc') . '/config.php';
}
$this->currentStep = $this->GetVar('step');
// can't check login on steps where no application present anyways :)
$this->skipLoginSteps = array_unique(array_merge($this->skipLoginSteps, $this->skipApplicationSteps));
$this->SelectPreset();
if (!$this->currentStep) {
$this->SetFirstStep(); // sets first step of current preset
}
$this->InitStep();
}
function SetFirstStep()
{
reset($this->steps[$this->stepsPreset]);
$this->currentStep = current($this->steps[$this->stepsPreset]);
}
/**
* Selects preset to proceed based on various criteria
*
*/
function SelectPreset()
{
$preset = $this->GetVar('preset');
if ($this->toolkit->systemConfigFound()) {
// only at installation first step
$status = $this->CheckDatabase(false);
if ($status && $this->AlreadyInstalled()) {
// if already installed, then all future actions need login to work
$this->skipLoginSteps = Array ('check_paths', -1);
if (!$preset) {
$preset = 'already_installed';
$this->currentStep = '';
}
}
}
if ($preset === false) {
$preset = 'fresh_install'; // default preset
}
$this->stepsPreset = $preset;
}
/**
* Returns variable from request
*
* @param string $name
* @param mixed $default
* @return string|bool
* @access private
*/
private function GetVar($name, $default = false)
{
if ( array_key_exists($name, $_COOKIE) ) {
return $_COOKIE[$name];
}
if ( array_key_exists($name, $_POST) ) {
return $_POST[$name];
}
return array_key_exists($name, $_GET) ? $_GET[$name] : $default;
}
/**
* Sets new value for request variable
*
* @param string $name
* @param mixed $value
* @return void
* @access private
*/
private function SetVar($name, $value)
{
$_POST[$name] = $value;
}
/**
* Performs needed intialization of data, that step requires
*
*/
function InitStep()
{
$require_login = !in_array($this->currentStep, $this->skipLoginSteps);
$this->InitApplication($require_login);
if ($require_login) {
// step require login to proceed
if (!$this->Application->LoggedIn()) {
$this->stepsPreset = 'already_installed';
$this->currentStep = 'install_setup'; // manually set 2nd step, because 'check_paths' step doesn't contain login form
// $this->SetFirstStep();
}
}
switch ($this->currentStep) {
case 'sys_requirements':
$required_checks = Array (
'php_version', 'composer', 'curl', 'simplexml', 'freetype', 'gd_version',
'jpeg', 'mysql', 'json', 'date.timezone', 'output_buffering',
);
$check_results = $this->toolkit->CallPrerequisitesMethod('core/', 'CheckSystemRequirements');
$required_checks = array_diff($required_checks, array_keys( array_filter($check_results) ));
if ( $required_checks ) {
// php-based checks failed - show error
$this->errorMessage = '<br/>Installation can not continue until all required environment parameters are set correctly';
}
elseif ( $this->GetVar('js_enabled') === false ) {
// can't check JS without form submit - set some fake error, so user stays on this step
$this->errorMessage = '&nbsp;';
}
elseif ( !$this->GetVar('js_enabled') || !$this->GetVar('cookies_enabled') ) {
// js/cookies disabled
$this->errorMessage = '<br/>Installation can not continue until all required environment parameters are set correctly';
}
break;
case 'check_paths':
$writeable_base = $this->toolkit->systemConfig->get('WriteablePath', 'Misc');
foreach ($this->writeableFolders as $folder_path) {
$file_path = FULL_PATH . str_replace('$1', $writeable_base, $folder_path);
if (file_exists($file_path) && !is_writable($file_path)) {
$this->errorMessage = '<br/>Installation can not continue until all required permissions are set correctly';
break;
}
}
break;
case 'clean_db':
// don't use Application, because all tables will be erased and it will crash
$sql = 'SELECT Path
FROM ' . TABLE_PREFIX . 'Modules';
$modules = $this->Conn->GetCol($sql);
foreach ($modules as $module_folder) {
$remove_file = '/' . $module_folder . 'install/remove_schema.sql';
if (file_exists(FULL_PATH . $remove_file)) {
$this->toolkit->RunSQL($remove_file);
}
}
$this->toolkit->deleteEditTables();
$this->currentStep = $this->GetNextStep();
break;
case 'db_config':
case 'db_reconfig':
$fields = Array (
'DBType', 'DBHost', 'DBName', 'DBUser',
'DBUserPassword', 'DBCollation', 'TablePrefix'
);
// set fields
foreach ($fields as $field_name) {
$submit_value = $this->GetVar($field_name);
if ($submit_value !== false) {
$this->toolkit->systemConfig->set($field_name, 'Database', $submit_value);
}
/*else {
$this->toolkit->systemConfig->set($field_name, 'Database', '');
}*/
}
break;
case 'download_license':
$license_source = $this->GetVar('license_source');
if ($license_source !== false && $license_source != 1) {
// previous step was "Select License" and not "Download from Intechnic" option was selected
$this->currentStep = $this->GetNextStep();
}
break;
case 'choose_modules':
// if no modules found, then proceed to next step
$modules = $this->ScanModules();
if (!$modules) {
$this->currentStep = $this->GetNextStep();
}
break;
case 'select_theme':
// put available theme list in database
$this->toolkit->rebuildThemes();
break;
case 'upgrade_modules':
// get installed modules from db and compare their versions to upgrade script
$modules = $this->GetUpgradableModules();
if (!$modules) {
$this->currentStep = $this->GetNextStep();
}
break;
case 'skin_upgrade':
if ($this->Application->RecallVar('SkinUpgradeLog') === false) {
// no errors during skin upgrade -> skip this step
$this->currentStep = $this->GetNextStep();
}
break;
case 'install_setup':
if ( $this->Application->TableFound(TABLE_PREFIX . 'UserSession', true) ) {
// update to 5.2.0 -> rename session table before using it
// don't rename any other table here, since their names could be used in upgrade script
$this->Conn->Query('RENAME TABLE ' . TABLE_PREFIX . 'UserSession TO ' . TABLE_PREFIX . 'UserSessions');
$this->Conn->Query('RENAME TABLE ' . TABLE_PREFIX . 'SessionData TO ' . TABLE_PREFIX . 'UserSessionData');
}
$next_preset = $this->Application->GetVar('next_preset');
if ($next_preset !== false) {
$user_helper = $this->Application->recallObject('UserHelper');
/* @var $user_helper UserHelper */
$username = $this->Application->GetVar('login');
$password = $this->Application->GetVar('password');
if ($username == 'root') {
// verify "root" user using configuration settings
$login_result = $user_helper->loginUser($username, $password);
if ($login_result != LoginResult::OK) {
$error_phrase = $login_result == LoginResult::NO_PERMISSION ? 'la_no_permissions' : 'la_invalid_password';
$this->errorMessage = $this->Application->Phrase($error_phrase) . '. If you don\'t know your username or password, contact Intechnic Support';
}
}
else {
// non "root" user -> verify using licensing server
$url_params = Array (
'login' => md5($username),
'password' => md5($password),
'action' => 'check',
'license_code' => base64_encode( $this->toolkit->systemConfig->get('LicenseCode', 'Intechnic') ),
'version' => '4.3.0',//$this->toolkit->GetMaxModuleVersion('core/'),
'domain' => base64_encode($_SERVER['HTTP_HOST']),
);
$curl_helper = $this->Application->recallObject('CurlHelper');
/* @var $curl_helper kCurlHelper */
$curl_helper->SetRequestData($url_params);
$file_data = $curl_helper->Send(GET_LICENSE_URL);
if ( !$curl_helper->isGoodResponseCode() ) {
$this->errorMessage = 'In-Portal servers temporarily unavailable. Please contact <a href="mailto:support@in-portal.com">In-Portal support</a> personnel directly.';
}
elseif (substr($file_data, 0, 5) == 'Error') {
$this->errorMessage = substr($file_data, 6) . ' If you don\'t know your username or password, contact Intechnic Support';
}
if ($this->errorMessage == '') {
$user_helper->loginUserById(USER_ROOT);
}
}
if ($this->errorMessage == '') {
// processed with redirect to selected step preset
if (!isset($this->steps[$next_preset])) {
$this->errorMessage = 'Preset "'.$next_preset.'" not yet implemented';
}
else {
$this->stepsPreset = $next_preset;
}
}
}
else {
// if preset was not choosen, then raise error
$this->errorMessage = 'Please select action to perform';
}
break;
case 'security':
// perform write check
if ($this->Application->GetVar('skip_security_check')) {
// administrator intensionally skips security checks
break;
}
$write_check = true;
$check_paths = Array ('/', '/index.php', $this->toolkit->systemConfig->get('WriteablePath', 'Misc') . '/config.php', ADMIN_DIRECTORY . '/index.php');
foreach ($check_paths as $check_path) {
$path_check_status = $this->toolkit->checkWritePermissions(FULL_PATH . $check_path);
if (is_bool($path_check_status) && $path_check_status) {
$write_check = false;
break;
}
}
// script execute check
if (file_exists(WRITEABLE . '/install_check.php')) {
unlink(WRITEABLE . '/install_check.php');
}
$fp = fopen(WRITEABLE . '/install_check.php', 'w');
fwrite($fp, "<?php\n\techo 'OK';\n");
fclose($fp);
$curl_helper = $this->Application->recallObject('CurlHelper');
/* @var $curl_helper kCurlHelper */
$output = $curl_helper->Send($this->Application->BaseURL() . ltrim(WRITEBALE_BASE, '/') . '/install_check.php');
unlink(WRITEABLE . '/install_check.php');
$execute_check = ($output !== 'OK');
$directive_check = true;
$ini_vars = Array ('register_globals' => false, 'open_basedir' => true, 'allow_url_fopen' => false);
foreach ($ini_vars as $var_name => $var_value) {
$current_value = ini_get($var_name);
if (($var_value && !$current_value) || (!$var_value && $current_value)) {
$directive_check = false;
break;
}
}
if (!$write_check || !$execute_check || !$directive_check) {
$this->errorMessage = true;
}
/*else {
$this->currentStep = $this->GetNextStep();
}*/
break;
}
$this->PerformValidation(); // returns validation status (just in case)
}
/**
* Validates data entered by user
*
* @return bool
*/
function PerformValidation()
{
if ($this->GetVar('step') != $this->currentStep) {
// just redirect from previous step, don't validate
return true;
}
$status = true;
switch ($this->currentStep) {
case 'db_config':
case 'db_reconfig':
// 1. check if required fields are filled
$section_name = 'Database';
$required_fields = Array ('DBType', 'DBHost', 'DBName', 'DBUser', 'DBCollation');
foreach ($required_fields as $required_field) {
if (!$this->toolkit->systemConfig->get($required_field, $section_name)) {
$status = false;
$this->errorMessage = 'Please fill all required fields';
break;
}
}
if ( !$status ) {
break;
}
// 2. check permissions, that use have in this database
$status = $this->CheckDatabase(($this->currentStep == 'db_config') && !$this->GetVar('UseExistingSetup'));
break;
case 'select_license':
$license_source = $this->GetVar('license_source');
if ($license_source == 2) {
// license from file -> file must be uploaded
$upload_error = $_FILES['license_file']['error'];
if ($upload_error != UPLOAD_ERR_OK) {
$this->errorMessage = 'Missing License File';
}
}
elseif (!is_numeric($license_source)) {
$this->errorMessage = 'Please select license';
}
$status = $this->errorMessage == '';
break;
case 'root_password':
// check, that password & verify password match
$password = $this->Application->GetVar('root_password');
$password_verify = $this->Application->GetVar('root_password_verify');
if ($password != $password_verify) {
$this->errorMessage = 'Passwords does not match';
}
elseif (mb_strlen($password) < 4) {
$this->errorMessage = 'Root Password must be at least 4 characters';
}
$status = $this->errorMessage == '';
break;
case 'choose_modules':
break;
case 'upgrade_modules':
$modules = $this->Application->GetVar('modules');
if (!$modules) {
$modules = Array ();
$this->errorMessage = 'Please select module(-s) to ' . ($this->currentStep == 'choose_modules' ? 'install' : 'upgrade');
}
// check interface module
$upgrade_data = $this->GetUpgradableModules();
if (array_key_exists('core', $upgrade_data) && !in_array('core', $modules)) {
// core can be upgraded, but isn't selected
$this->errorMessage = 'Please select "Core" as interface module';
}
$status = $this->errorMessage == '';
break;
}
return $status;
}
/**
* Perform installation step actions
*
*/
function Run()
{
if ($this->errorMessage) {
// was error during data validation stage
return ;
}
switch ($this->currentStep) {
case 'db_config':
case 'db_reconfig':
// store db configuration
$sql = 'SHOW COLLATION
LIKE \''.$this->toolkit->systemConfig->get('DBCollation', 'Database').'\'';
$collation_info = $this->Conn->Query($sql);
if ($collation_info) {
$this->toolkit->systemConfig->set('DBCharset', 'Database', $collation_info[0]['Charset']);
// database is already connected, that's why set collation on the fly
$this->Conn->Query('SET NAMES \''.$this->toolkit->systemConfig->get('DBCharset', 'Database').'\' COLLATE \''.$this->toolkit->systemConfig->get('DBCollation', 'Database').'\'');
}
$this->toolkit->systemConfig->save();
if ($this->currentStep == 'db_config') {
if ($this->GetVar('UseExistingSetup')) {
// abort clean install and redirect to already_installed
$this->stepsPreset = 'already_installed';
break;
}
// import base data into new database, not for db_reconfig
$this->toolkit->RunSQL('/core/install/install_schema.sql');
$this->toolkit->RunSQL('/core/install/install_data.sql');
// create category using sql, because Application is not available here
$table_name = $this->toolkit->systemConfig->get('TablePrefix', 'Database') . 'IdGenerator';
$this->Conn->Query('UPDATE ' . $table_name . ' SET lastid = lastid + 1');
$resource_id = $this->Conn->GetOne('SELECT lastid FROM ' . $table_name);
if ($resource_id === false) {
$this->Conn->Query('INSERT INTO '.$table_name.' (lastid) VALUES (2)');
$resource_id = 2;
}
// can't use USER_ROOT constant, since Application isn't available here
$fields_hash = Array (
'l1_Name' => 'Content', 'l1_MenuTitle' => 'Content', 'Filename' => 'Content',
'AutomaticFilename' => 0, 'CreatedById' => -1, 'CreatedOn' => time(),
'ResourceId' => $resource_id - 1, 'l1_Description' => 'Content', 'Status' => 4,
);
$this->Conn->doInsert($fields_hash, $this->toolkit->systemConfig->get('TablePrefix', 'Database') . 'Categories');
$this->toolkit->SetModuleRootCategory('Core', $this->Conn->getInsertID());
// set module "Core" version after install (based on upgrade scripts)
$this->toolkit->SetModuleVersion('Core', 'core/');
// for now we set "In-Portal" module version to "Core" module version (during clean install)
$this->toolkit->SetModuleVersion('In-Portal', 'core/');
}
break;
case 'select_license':
// reset memory cache, when application is first available (on fresh install and clean reinstall steps)
$this->Application->HandleEvent(new kEvent('adm:OnResetMemcache'));
$license_source = $this->GetVar('license_source');
switch ($license_source) {
case 1: // Download from Intechnic
break;
case 2: // Upload License File
$file_data = array_map('trim', file($_FILES['license_file']['tmp_name']));
if ((count($file_data) == 3) && $file_data[1]) {
- $modules_helper = $this->Application->recallObject('ModulesHelper');
+ $modules_helper = $this->Application->recallObject('kModulesHelper');
/* @var $modules_helper kModulesHelper */
if ($modules_helper->verifyLicense($file_data[1])) {
$this->toolkit->systemConfig->set('License', 'Intechnic', $file_data[1]);
$this->toolkit->systemConfig->set('LicenseCode', 'Intechnic', $file_data[2]);
$this->toolkit->systemConfig->save();
}
else {
$this->errorMessage = 'Invalid License File';
}
}
else {
$this->errorMessage = 'Invalid License File';
}
break;
case 3: // Use Existing License
$license_hash = $this->toolkit->systemConfig->get('License', 'Intechnic');
if ($license_hash) {
- $modules_helper = $this->Application->recallObject('ModulesHelper');
+ $modules_helper = $this->Application->recallObject('kModulesHelper');
/* @var $modules_helper kModulesHelper */
if (!$modules_helper->verifyLicense($license_hash)) {
$this->errorMessage = 'Invalid or corrupt license detected';
}
}
else {
// happens, when browser's "Back" button is used
$this->errorMessage = 'Missing License File';
}
break;
case 4: // Skip License (Local Domain Installation)
if ($this->toolkit->sectionFound('Intechnic')) {
// remove any previous license information
$this->toolkit->systemConfig->set('License', 'Intechnic');
$this->toolkit->systemConfig->set('LicenseCode', 'Intechnic');
$this->toolkit->systemConfig->save();
}
break;
}
break;
case 'download_license':
$license_login = $this->GetVar('login');
$license_password = $this->GetVar('password');
$license_id = $this->GetVar('licenses');
$curl_helper = $this->Application->recallObject('CurlHelper');
/* @var $curl_helper kCurlHelper */
if (strlen($license_login) && strlen($license_password) && !$license_id) {
// Here we determine weather login is ok & check available licenses
$url_params = Array (
'login' => md5($license_login),
'password' => md5($license_password),
'version' => $this->toolkit->GetMaxModuleVersion('core/'),
'domain' => base64_encode($_SERVER['HTTP_HOST']),
);
$curl_helper->SetRequestData($url_params);
$file_data = $curl_helper->Send(GET_LICENSE_URL);
if (!$file_data) {
// error connecting to licensing server
$this->errorMessage = 'Unable to connect to the Intechnic server! Please try again later!';
}
else {
if (substr($file_data, 0, 5) == 'Error') {
// after processing data server returned error
$this->errorMessage = substr($file_data, 6);
}
else {
// license received
if (substr($file_data, 0, 3) == 'SEL') {
// we have more, then one license -> let user choose
$this->SetVar('license_selection', base64_encode( substr($file_data, 4) )); // we received html with radio buttons with names "licenses"
$this->errorMessage = 'Please select which license to use';
}
else {
// we have one license
$this->toolkit->processLicense($file_data);
}
}
}
}
else if (!$license_id) {
// licenses were not queried AND user/password missing
$this->errorMessage = 'Incorrect Username or Password. If you don\'t know your username or password, contact Intechnic Support';
}
else {
// Here we download license
$url_params = Array (
'license_id' => md5($license_id),
'dlog' => md5($license_login),
'dpass' => md5($license_password),
'version' => $this->toolkit->GetMaxModuleVersion('core/'),
'domain' => base64_encode($_SERVER['HTTP_HOST']),
);
$curl_helper->SetRequestData($url_params);
$file_data = $curl_helper->Send(GET_LICENSE_URL);
if (!$file_data) {
// error connecting to licensing server
$this->errorMessage = 'Unable to connect to the Intechnic server! Please try again later!';
}
else {
if (substr($file_data, 0, 5) == 'Error') {
// after processing data server returned error
$this->errorMessage = substr($file_data, 6);
}
else {
$this->toolkit->processLicense($file_data);
}
}
}
break;
case 'select_domain':
- $modules_helper = $this->Application->recallObject('ModulesHelper');
+ $modules_helper = $this->Application->recallObject('kModulesHelper');
/* @var $modules_helper kModulesHelper */
// get domain name as entered by user on the form
$domain = $this->GetVar('domain') == 1 ? $_SERVER['HTTP_HOST'] : str_replace(' ', '', $this->GetVar('other'));
$license_hash = $this->toolkit->systemConfig->get('License', 'Intechnic');
if ($license_hash) {
// when license present, then extract domain from it
$license_hash = base64_decode($license_hash);
list ( , , $license_keys) = $modules_helper->_ParseLicense($license_hash);
$license_domain = $license_keys[0]['domain'];
}
else {
// when license missing, then use current domain or domain entered by user
$license_domain = $domain;
}
if ($domain != '') {
if (strstr($domain, $license_domain) || $modules_helper->_IsLocalSite($domain)) {
$this->toolkit->systemConfig->set('Domain', 'Misc', $domain);
$this->toolkit->systemConfig->save();
}
else {
$this->errorMessage = 'Domain name entered does not match domain name in the license!';
}
}
else {
$this->errorMessage = 'Please enter valid domain!';
}
break;
case 'sys_config':
$config_data = $this->GetVar('system_config');
foreach ($config_data as $section => $section_vars) {
foreach ($section_vars as $var_name => $var_value) {
$this->toolkit->systemConfig->set($var_name, $section, $var_value);
}
}
$this->toolkit->systemConfig->save();
break;
case 'root_password':
// update root password in database
$password_formatter = $this->Application->recallObject('kPasswordFormatter');
/* @var $password_formatter kPasswordFormatter */
$config_values = Array (
'RootPass' => $password_formatter->hashPassword($this->Application->GetVar('root_password')),
'Backup_Path' => FULL_PATH . $this->toolkit->systemConfig->get('WriteablePath', 'Misc') . DIRECTORY_SEPARATOR . 'backupdata',
'DefaultEmailSender' => 'portal@' . $this->toolkit->systemConfig->get('Domain', 'Misc')
);
$site_timezone = date_default_timezone_get();
if ($site_timezone) {
$config_values['Config_Site_Time'] = $site_timezone;
}
$this->toolkit->saveConfigValues($config_values);
$user_helper = $this->Application->recallObject('UserHelper');
/* @var $user_helper UserHelper */
// login as "root", when no errors on password screen
$user_helper->loginUser('root', $this->Application->GetVar('root_password'));
// import base language for core (english)
$this->toolkit->ImportLanguage('/core/install/english');
// make sure imported language is set as active in session, created during installation
$this->Application->Session->SetField('Language', 1);
// set imported language as primary
$lang = $this->Application->recallObject('lang.-item', null, Array('skip_autoload' => true));
/* @var $lang LanguagesItem */
$lang->Load(1); // fresh install => ID=1
$lang->setPrimary(true); // for Front-End
break;
case 'choose_modules':
// run module install scripts
$modules = $this->Application->GetVar('modules');
if ($modules) {
foreach ($modules as $module) {
$install_file = MODULES_PATH.'/'.$module.'/install.php';
if (file_exists($install_file)) {
include_once($install_file);
}
}
}
// update category cache
$updater = $this->Application->makeClass('kPermCacheUpdater');
/* @var $updater kPermCacheUpdater */
$updater->OneStepRun();
break;
case 'post_config':
$this->toolkit->saveConfigValues( $this->GetVar('config') );
break;
case 'select_theme':
// 1. mark theme, that user is selected
$theme_id = $this->GetVar('theme');
$theme_config = $this->Application->getUnitConfig('theme');
$theme_table = $theme_config->getTableName();
$theme_id_field = $theme_config->getIDField();
$sql = 'UPDATE ' . $theme_table . '
SET Enabled = 1, PrimaryTheme = 1
WHERE ' . $theme_id_field . ' = ' . $theme_id;
$this->Conn->Query($sql);
$this->toolkit->rebuildThemes(); // rescan theme to create structure after theme is enabled !!!
// install theme dependent demo data
if ($this->Application->GetVar('install_demo_data')) {
$sql = 'SELECT Name
FROM ' . $theme_table . '
WHERE ' . $theme_id_field . ' = ' . $theme_id;
$theme_name = $this->Conn->GetOne($sql);
$site_path = $this->toolkit->systemConfig->get('WebsitePath','Misc') . '/';
$file_helper = $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
if ($module_name == 'In-Portal') {
continue;
}
$template_path = '/themes' . '/' . $theme_name . '/' . $module_info['TemplatePath'];
$this->toolkit->RunSQL( $template_path . '_install/install_data.sql', Array('{ThemeId}', '{SitePath}'), Array($theme_id, $site_path) );
if ( file_exists(FULL_PATH . $template_path . '_install/images') ) {
// copy theme demo images into writable path accessible by FCKEditor
$file_helper->copyFolderRecursive(FULL_PATH . $template_path . '_install/images' . DIRECTORY_SEPARATOR, WRITEABLE . '/user_files/Images');
}
}
}
break;
case 'upgrade_modules':
// get installed modules from db and compare their versions to upgrade script
$modules = $this->Application->GetVar('modules');
if ($modules) {
$upgrade_data = $this->GetUpgradableModules();
$start_from_query = $this->Application->GetVar('start_from_query');
$this->upgradeDepencies = $this->getUpgradeDependencies($modules, $upgrade_data);
if ($start_from_query !== false) {
$this->upgradeLog = unserialize( $this->Application->RecallVar('UpgradeLog') );
}
else {
$start_from_query = 0;
$this->upgradeLog = Array ('ModuleVersions' => Array ());
// remember each module version, before upgrade scripts are executed
foreach ($modules as $module_name) {
$module_info = $upgrade_data[$module_name];
$this->upgradeLog['ModuleVersions'][$module_name] = $module_info['FromVersion'];
}
$this->Application->RemoveVar('UpgradeLog');
}
// 1. perform "php before", "sql", "php after" upgrades
foreach ($modules as $module_name) {
$module_info = $upgrade_data[$module_name];
/*echo '<h2>Upgrading "' . $module_info['Name'] . '" to "' . $module_info['ToVersion'] . '"</h2>' . "\n";
flush();*/
if (!$this->RunUpgrade($module_info['Name'], $module_info['ToVersion'], $upgrade_data, $start_from_query)) {
$this->Application->StoreVar('UpgradeLog', serialize($this->upgradeLog));
$this->Done();
}
// restore upgradable module version (makes sense after sql error processing)
$upgrade_data[$module_name]['FromVersion'] = $this->upgradeLog['ModuleVersions'][$module_name];
}
// 2. import language pack, perform "languagepack" upgrade for all upgraded versions
foreach ($modules as $module_name) {
$module_info = $upgrade_data[$module_name];
$sqls =& $this->getUpgradeQueriesFromVersion($module_info['Path'], $module_info['FromVersion']);
preg_match_all('/' . VERSION_MARK . '/s', $sqls, $regs);
// import module language pack
$this->toolkit->ImportLanguage('/' . $module_info['Path'] . 'install/english', true);
// perform advanced language pack upgrade
foreach ($regs[1] as $version) {
$this->RunUpgradeScript($module_info['Path'], $version, 'languagepack');
}
}
// 3. update all theme language packs
$themes_helper = $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$themes_helper->synchronizeModule(false);
// 4. upgrade admin skin
if (in_array('core', $modules)) {
$skin_upgrade_log = $this->toolkit->upgradeSkin($upgrade_data['core']);
if ($skin_upgrade_log === true) {
$this->Application->RemoveVar('SkinUpgradeLog');
}
else {
$this->Application->StoreVar('SkinUpgradeLog', serialize($skin_upgrade_log));
}
// for now we set "In-Portal" module version to "Core" module version (during upgrade)
$this->toolkit->SetModuleVersion('In-Portal', false, $upgrade_data['core']['ToVersion']);
}
}
break;
case 'finish':
// delete cache
$this->toolkit->deleteCache();
$this->toolkit->rebuildThemes();
// compile admin skin, so it will be available in 3 frames at once
$skin_helper = $this->Application->recallObject('SkinHelper');
/* @var $skin_helper SkinHelper */
$skin = $this->Application->recallObject('skin', null, Array ('skip_autoload' => true));
/* @var $skin kDBItem */
$skin->Load(1, 'IsPrimary');
$skin_helper->compile($skin);
// set installation finished mark
if ($this->Application->ConfigValue('InstallFinished') === false) {
$fields_hash = Array (
'VariableName' => 'InstallFinished',
'VariableValue' => 1,
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX.'SystemSettings');
}
$random_string = $this->Application->ConfigValue('RandomString');
if ( !$random_string ) {
$user_helper = $this->Application->recallObject('UserHelper');
/* @var $user_helper UserHelper */
$random_string = $user_helper->generateRandomString(64, true, true);
$this->Application->SetConfigValue('RandomString', $random_string);
}
break;
}
if ($this->errorMessage) {
// was error during run stage
return ;
}
$this->currentStep = $this->GetNextStep();
$this->InitStep(); // init next step (that will be shown now)
$this->InitApplication();
if ($this->currentStep == -1) {
// step after last step -> redirect to admin
$user_helper = $this->Application->recallObject('UserHelper');
/* @var $user_helper UserHelper */
$user_helper->logoutUser();
$this->Application->Redirect($user_helper->event->redirect, $user_helper->event->getRedirectParams(), '', 'index.php');
}
}
function getUpgradeDependencies($modules, &$upgrade_data)
{
$dependencies = Array ();
foreach ($modules as $module_name) {
$module_info = $upgrade_data[$module_name];
$upgrade_object =& $this->getUpgradeObject($module_info['Path']);
if (!is_object($upgrade_object)) {
continue;
}
foreach ($upgrade_object->dependencies as $dependent_version => $version_dependencies) {
if (!$version_dependencies) {
// module is independent -> skip
continue;
}
list ($parent_name, $parent_version) = each($version_dependencies);
if (!array_key_exists($parent_name, $dependencies)) {
// parent module
$dependencies[$parent_name] = Array ();
}
if (!array_key_exists($parent_version, $dependencies[$parent_name])) {
// parent module versions, that are required by other module versions
$dependencies[$parent_name][$parent_version] = Array ();
}
$dependencies[$parent_name][$parent_version][] = Array ($module_info['Name'] => $dependent_version);
}
}
return $dependencies;
}
/**
* Returns database queries, that should be executed to perform upgrade from given to lastest version of given module path
*
* @param string $module_path
* @param string $from_version
* @return string
*/
function &getUpgradeQueriesFromVersion($module_path, $from_version)
{
$upgrades_file = sprintf(UPGRADES_FILE, $module_path, 'sql');
$sqls = file_get_contents($upgrades_file);
$version_mark = preg_replace('/(\(.*?\))/', $from_version, VERSION_MARK);
// get only sqls from next (relative to current) version to end of file
$start_pos = strpos($sqls, $version_mark);
$sqls = substr($sqls, $start_pos);
return $sqls;
}
function RunUpgrade($module_name, $to_version, &$upgrade_data, &$start_from_query)
{
$module_info = $upgrade_data[ strtolower($module_name) ];
$sqls =& $this->getUpgradeQueriesFromVersion($module_info['Path'], $module_info['FromVersion']);
preg_match_all('/(' . VERSION_MARK . ')/s', $sqls, $matches, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
foreach ($matches as $index => $match) {
// upgrade version
$version = $match[2][0];
if ($this->toolkit->ConvertModuleVersion($version) > $this->toolkit->ConvertModuleVersion($to_version)) {
// only upgrade to $to_version, not further
break;
}
if (!in_array($module_name . ':' . $version, $this->upgradeLog)) {
if ($this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Upgrading "' . $module_name . '" to "' . $version . '" version: BEGIN.');
}
/*echo 'Upgrading "' . $module_name . '" to "' . $version . '".<br/>' . "\n";
flush();*/
// don't upgrade same version twice
$start_pos = $match[0][1] + strlen($match[0][0]);
$end_pos = array_key_exists($index + 1, $matches) ? $matches[$index + 1][0][1] : strlen($sqls);
$version_sqls = substr($sqls, $start_pos, $end_pos - $start_pos);
if ($start_from_query == 0) {
$this->RunUpgradeScript($module_info['Path'], $version, 'before');
}
if (!$this->toolkit->RunSQLText($version_sqls, null, null, $start_from_query)) {
$this->errorMessage .= '<input type="hidden" name="start_from_query" value="' . $this->LastQueryNum . '">';
$this->errorMessage .= '<br/>Module "' . $module_name . '" upgrade to "' . $version . '" failed.';
$this->errorMessage .= '<br/>Click Continue button below to skip this query and go further<br/>';
return false;
}
else {
// reset query counter, when all queries were processed
$start_from_query = 0;
}
$this->RunUpgradeScript($module_info['Path'], $version, 'after');
if ($this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Upgrading "' . $module_name . '" to "' . $version . '" version: END.');
}
// remember, that we've already upgraded given version
$this->upgradeLog[] = $module_name . ':' . $version;
}
if (array_key_exists($module_name, $this->upgradeDepencies) && array_key_exists($version, $this->upgradeDepencies[$module_name])) {
foreach ($this->upgradeDepencies[$module_name][$version] as $dependency_info) {
list ($dependent_module, $dependent_version) = each($dependency_info);
if (!$this->RunUpgrade($dependent_module, $dependent_version, $upgrade_data, $start_from_query)) {
return false;
}
}
}
// only mark module as updated, when all it's dependent modules are upgraded
$this->toolkit->SetModuleVersion($module_name, false, $version);
}
return true;
}
/**
* Run upgrade PHP scripts for module with specified path
*
* @param string $module_path
* @param Array $version
* @param string $mode upgrade mode = {before,after,languagepack}
*/
function RunUpgradeScript($module_path, $version, $mode)
{
$upgrade_object =& $this->getUpgradeObject($module_path);
if (!is_object($upgrade_object)) {
return ;
}
$upgrade_method = 'Upgrade_' . str_replace(Array ('.', '-'), '_', $version);
if (method_exists($upgrade_object, $upgrade_method)) {
$upgrade_object->$upgrade_method($mode);
}
}
/**
* Returns upgrade class for given module path
*
* @param string $module_path
* @return kUpgradeHelper
*/
function &getUpgradeObject($module_path)
{
static $upgrade_classes = Array ();
$upgrades_file = sprintf(UPGRADES_FILE, $module_path, 'php');
if (!file_exists($upgrades_file)) {
$false = false;
return $false;
}
if (!isset($upgrade_classes[$module_path])) {
require_once(FULL_PATH . REL_PATH . '/install/upgrade_helper.php');
// save class name, because 2nd time (in after call)
// $upgrade_class variable will not be present
include_once $upgrades_file;
$upgrade_classes[$module_path] = $upgrade_class;
}
$upgrade_object = new $upgrade_classes[$module_path]();
/* @var $upgrade_object CoreUpgrades */
$upgrade_object->setToolkit($this->toolkit);
return $upgrade_object;
}
/**
* Initialize kApplication
*
* @param bool $force initialize in any case
*/
function InitApplication($force = false)
{
if (($force || !in_array($this->currentStep, $this->skipApplicationSteps)) && !isset($this->Application)) {
// step is allowed for application usage & it was not initialized in previous step
global $start, $debugger, $dbg_options;
include_once(FULL_PATH.'/core/kernel/startup.php');
$this->Application =& kApplication::Instance();
$this->toolkit->Application =& kApplication::Instance();
$this->includeModuleConstants();
$this->Application->Init();
$this->Conn =& $this->Application->GetADODBConnection();
$this->toolkit->Conn =& $this->Application->GetADODBConnection();
}
}
/**
* When no modules installed, then pre-include all modules contants, since they are used in unit configs
*
*/
function includeModuleConstants()
{
$modules = $this->ScanModules();
foreach ($modules as $module_path) {
$constants_file = MODULES_PATH . '/' . $module_path . '/constants.php';
if ( file_exists($constants_file) ) {
kUtil::includeOnce($constants_file);
}
}
}
/**
* Show next step screen
*
* @param string $error_message
* @return void
*/
function Done($error_message = null)
{
if ( isset($error_message) ) {
$this->errorMessage = $error_message;
}
include_once (FULL_PATH . '/' . REL_PATH . '/install/incs/install.tpl');
if ( isset($this->Application) ) {
$this->Application->Done();
}
exit;
}
function ConnectToDatabase()
{
include_once FULL_PATH . '/core/kernel/db/i_db_connection.php';
include_once FULL_PATH . '/core/kernel/db/db_connection.php';
$required_keys = Array ('DBType', 'DBUser', 'DBName');
foreach ($required_keys as $required_key) {
if (!$this->toolkit->systemConfig->get($required_key, 'Database')) {
// one of required db connection settings missing -> abort connection
return false;
}
}
$this->Conn = new kDBConnection($this->toolkit->systemConfig->get('DBType', 'Database'), Array(&$this, 'DBErrorHandler'));
$this->Conn->setup($this->toolkit->systemConfig->getData());
// setup toolkit too
$this->toolkit->Conn =& $this->Conn;
return !$this->Conn->hasError();
}
/**
* Checks if core is already installed
*
* @return bool
*/
function AlreadyInstalled()
{
$table_prefix = $this->toolkit->systemConfig->get('TablePrefix', 'Database');
$settings_table = $this->TableExists('ConfigurationValues') ? 'ConfigurationValues' : 'SystemSettings';
$sql = 'SELECT VariableValue
FROM ' . $table_prefix . $settings_table . '
WHERE VariableName = "InstallFinished"';
return $this->TableExists($settings_table) && $this->Conn->GetOne($sql);
}
function CheckDatabase($check_installed = true)
{
// perform various check type to database specified
// 1. user is allowed to connect to database
// 2. user has all types of permissions in database
// 3. database environment settings met minimum requirements
if (mb_strlen($this->toolkit->systemConfig->get('TablePrefix', 'Database')) > 7) {
$this->errorMessage = 'Table prefix should not be longer than 7 characters';
return false;
}
// connect to database
$status = $this->ConnectToDatabase();
if ($status) {
// if connected, then check if all sql statements work
$sql_tests[] = 'DROP TABLE IF EXISTS test_table';
$sql_tests[] = 'CREATE TABLE test_table(test_col mediumint(6))';
$sql_tests[] = 'LOCK TABLES test_table WRITE';
$sql_tests[] = 'INSERT INTO test_table(test_col) VALUES (5)';
$sql_tests[] = 'UPDATE test_table SET test_col = 12';
$sql_tests[] = 'UNLOCK TABLES';
$sql_tests[] = 'ALTER TABLE test_table ADD COLUMN new_col varchar(10)';
$sql_tests[] = 'SELECT * FROM test_table';
$sql_tests[] = 'DELETE FROM test_table';
$sql_tests[] = 'DROP TABLE IF EXISTS test_table';
foreach ($sql_tests as $sql_test) {
$this->Conn->Query($sql_test);
if ($this->Conn->getErrorCode() != 0) {
$status = false;
break;
}
}
if ($status) {
// if statements work & connection made, then check table existance
if ($check_installed && $this->AlreadyInstalled()) {
$this->errorMessage = 'An In-Portal Database already exists at this location';
return false;
}
$requirements_error = Array ();
$db_check_results = $this->toolkit->CallPrerequisitesMethod('core/', 'CheckDBRequirements');
if ( !$db_check_results['version'] ) {
$requirements_error[] = '- MySQL Version is below 5.0';
}
if ( !$db_check_results['packet_size'] ) {
$requirements_error[] = '- MySQL Packet Size is below 1 MB';
}
if ( $requirements_error ) {
$this->errorMessage = 'Connection successful, but following system requirements were not met:<br/>' . implode('<br/>', $requirements_error);
return false;
}
}
else {
// user has insufficient permissions in database specified
$this->errorMessage = 'Permission Error: ('.$this->Conn->getErrorCode().') '.$this->Conn->getErrorMsg();
return false;
}
}
else {
// was error while connecting
if (!$this->Conn) return false;
$this->errorMessage = 'Connection Error: ('.$this->Conn->getErrorCode().') '.$this->Conn->getErrorMsg();
return false;
}
return true;
}
/**
* Checks if all passed tables exists
*
* @param string $tables comma separated tables list
* @return bool
*/
function TableExists($tables)
{
$prefix = $this->toolkit->systemConfig->get('TablePrefix', 'Database');
$all_found = true;
$tables = explode(',', $tables);
foreach ($tables as $table_name) {
$sql = 'SHOW TABLES LIKE "'.$prefix.$table_name.'"';
if (count($this->Conn->Query($sql)) == 0) {
$all_found = false;
break;
}
}
return $all_found;
}
/**
* Returns modules list found in modules folder
*
* @return Array
*/
function ScanModules()
{
static $modules = null;
if ( !isset($modules) ) {
// use direct include, because it's called before kApplication::Init, that creates class factory
kUtil::includeOnce( KERNEL_PATH . kApplication::MODULE_HELPER_PATH );
$modules_helper = new kModulesHelper();
$modules = $modules_helper->getModules();
}
return $modules;
}
/**
* Virtually place module under "modules" folder or it won't be recognized during upgrade to 5.1.0 version
*
* @param string $name
* @param string $path
* @param string $version
* @return string
*/
function getModulePath($name, $path, $version)
{
if ($name == 'Core') {
// don't transform path for Core module
return $path;
}
if (!preg_match('/^modules\//', $path)) {
// upgrade from 5.0.x/1.0.x to 5.1.x/1.1.x
return 'modules/' . $path;
}
return $path;
}
/**
* Returns list of modules, that can be upgraded
*
*/
function GetUpgradableModules()
{
$ret = Array ();
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
if ($module_name == 'In-Portal') {
// don't show In-Portal, because it shares upgrade scripts with Core module
continue;
}
$module_info['Path'] = $this->getModulePath($module_name, $module_info['Path'], $module_info['Version']);
$upgrades_file = sprintf(UPGRADES_FILE, $module_info['Path'], 'sql');
if (!file_exists($upgrades_file)) {
// no upgrade file
continue;
}
$sqls = file_get_contents($upgrades_file);
$versions_found = preg_match_all('/'.VERSION_MARK.'/s', $sqls, $regs);
if (!$versions_found) {
// upgrades file doesn't contain version definitions
continue;
}
$to_version = end($regs[1]);
$this_version = $this->toolkit->ConvertModuleVersion($module_info['Version']);
if ($this->toolkit->ConvertModuleVersion($to_version) > $this_version) {
// destination version is greather then current
foreach ($regs[1] as $version) {
if ($this->toolkit->ConvertModuleVersion($version) > $this_version) {
$from_version = $version;
break;
}
}
$version_info = Array (
'FromVersion' => $from_version,
'ToVersion' => $to_version,
);
$ret[ strtolower($module_name) ] = array_merge($module_info, $version_info);
}
}
return $ret;
}
/**
* Returns content to show for current step
*
* @return string
*/
function GetStepBody()
{
$step_template = FULL_PATH.'/core/install/step_templates/'.$this->currentStep.'.tpl';
if (file_exists($step_template)) {
ob_start();
include_once ($step_template);
return ob_get_clean();
}
return '{step template "'.$this->currentStep.'" missing}';
}
/**
* Parses step information file, cache result for current step ONLY & return it
*
* @return Array
*/
function &_getStepInfo()
{
static $info = Array('help_title' => null, 'step_title' => null, 'help_body' => null, 'queried' => false);
if (!$info['queried']) {
$fdata = file_get_contents($this->StepDBFile);
$parser = xml_parser_create();
xml_parse_into_struct($parser, $fdata, $values, $index);
xml_parser_free($parser);
foreach ($index['STEP'] as $section_index) {
$step_data =& $values[$section_index];
if ($step_data['attributes']['NAME'] == $this->currentStep) {
$info['step_title'] = $step_data['attributes']['TITLE'];
if (isset($step_data['attributes']['HELP_TITLE'])) {
$info['help_title'] = $step_data['attributes']['HELP_TITLE'];
}
else {
// if help title not set, then use step title
$info['help_title'] = $step_data['attributes']['TITLE'];
}
$info['help_body'] = trim($step_data['value']);
break;
}
}
$info['queried'] = true;
}
return $info;
}
/**
* Returns particular information abou current step
*
* @param string $info_type
* @return string
*/
function GetStepInfo($info_type)
{
$step_info =& $this->_getStepInfo();
if (isset($step_info[$info_type])) {
return $step_info[$info_type];
}
return '{step "'.$this->currentStep.'"; param "'.$info_type.'" missing}';
}
/**
* Returns passed steps titles
*
* @param Array $steps
* @return Array
* @see kInstaller:PrintSteps
*/
function _getStepTitles($steps)
{
$fdata = file_get_contents($this->StepDBFile);
$parser = xml_parser_create();
xml_parse_into_struct($parser, $fdata, $values, $index);
xml_parser_free($parser);
$ret = Array ();
foreach ($index['STEP'] as $section_index) {
$step_data =& $values[$section_index];
if (in_array($step_data['attributes']['NAME'], $steps)) {
$ret[ $step_data['attributes']['NAME'] ] = $step_data['attributes']['TITLE'];
}
}
return $ret;
}
/**
* Returns current step number in active steps_preset.
* Value can't be cached, because same step can have different number in different presets
*
* @return int
*/
function GetStepNumber()
{
return array_search($this->currentStep, $this->steps[$this->stepsPreset]) + 1;
}
/**
* Returns step name to process next
*
* @return string
*/
function GetNextStep()
{
$next_index = $this->GetStepNumber();
if ($next_index > count($this->steps[$this->stepsPreset]) - 1) {
return -1;
}
return $this->steps[$this->stepsPreset][$next_index];
}
/**
* Returns step name, that was processed before this step
*
* @return string
*/
function GetPreviousStep()
{
$next_index = $this->GetStepNumber() - 1;
if ($next_index < 0) {
$next_index = 0;
}
return $this->steps[$this->stepsPreset][$next_index];
}
/**
* Prints all steps from active steps preset and highlights current step
*
* @param string $active_tpl
* @param string $passive_tpl
* @return string
*/
function PrintSteps($active_tpl, $passive_tpl)
{
$ret = '';
$step_titles = $this->_getStepTitles($this->steps[$this->stepsPreset]);
foreach ($this->steps[$this->stepsPreset] as $step_name) {
$template = $step_name == $this->currentStep ? $active_tpl : $passive_tpl;
$ret .= sprintf($template, $step_titles[$step_name]);
}
return $ret;
}
/**
* Installation error handler for sql errors
*
* @param int $code
* @param string $msg
* @param string $sql
* @return bool
* @access private
*/
function DBErrorHandler($code, $msg, $sql)
{
$this->errorMessage = 'Query: <br />'.htmlspecialchars($sql, ENT_QUOTES, 'UTF-8').'<br />execution result is error:<br />['.$code.'] '.$msg;
return true;
}
/**
* Installation error handler
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @param Array|string $errcontext
*/
function ErrorHandler($errno, $errstr, $errfile = '', $errline = 0, $errcontext = '')
{
if ($errno == E_USER_ERROR) {
// only react on user fatal errors
$this->Done($errstr);
}
}
/**
* Checks, that given button should be visible on current installation step
*
* @param string $name
* @return bool
*/
function buttonVisible($name)
{
$button_visibility = Array (
'continue' => $this->GetNextStep() != -1 || ($this->stepsPreset == 'already_installed'),
'refresh' => in_array($this->currentStep, Array ('sys_requirements', 'check_paths', 'security')),
'back' => in_array($this->currentStep, Array (/*'select_license',*/ 'download_license', 'select_domain')),
);
if ($name == 'any') {
foreach ($button_visibility as $button_name => $button_visible) {
if ($button_visible) {
return true;
}
}
return false;
}
return array_key_exists($name, $button_visibility) ? $button_visibility[$name] : true;
}
}
Index: branches/5.3.x/composer.json
===================================================================
--- branches/5.3.x/composer.json (revision 16155)
+++ branches/5.3.x/composer.json (revision 16156)
@@ -1,5 +1,7 @@
{
+ "name": "In-Portal",
"require-dev": {
- "aik099/coding-standard": "dev-in-portal"
+ "aik099/coding-standard": "dev-in-portal",
+ "nikic/php-parser": "~1.2"
}
}
Index: branches/5.3.x/tools/build_class_map.php
===================================================================
--- branches/5.3.x/tools/build_class_map.php (nonexistent)
+++ branches/5.3.x/tools/build_class_map.php (revision 16156)
@@ -0,0 +1,19 @@
+<?php
+
+use Intechnic\InPortal\Core\kernel\utility\ClassDiscovery\ClassMapBuilder;
+
+if ( PHP_SAPI !== 'cli' ) {
+ exit(64);
+}
+
+$_GET['full_init'] = 0; // See INP-1413.
+
+define('FULL_PATH', realpath(dirname(__FILE__) . '/..'));
+require_once (FULL_PATH . '/core/kernel/startup.php');
+
+$application =& kApplication::Instance();
+$application->Init();
+
+foreach ( ClassMapBuilder::createBuilders() as $class_map_builder ) {
+ $class_map_builder->build();
+}
Property changes on: branches/5.3.x/tools/build_class_map.php
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+LF
\ No newline at end of property
Index: branches/5.3.x/composer.lock
===================================================================
--- branches/5.3.x/composer.lock (revision 16155)
+++ branches/5.3.x/composer.lock (revision 16156)
@@ -1,59 +1,105 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "hash": "4c8225aabf36f626240d5615afd5c66b",
+ "hash": "746788fc49ab32d05ca2bd837e9f0a1f",
"packages": [],
"packages-dev": [
{
"name": "aik099/coding-standard",
"version": "dev-in-portal",
"source": {
"type": "git",
"url": "https://github.com/aik099/CodingStandard.git",
- "reference": "e389596b9b1fc8f67f1cca34a1c8bdd9d85104d0"
+ "reference": "3680b3926f0e936dc95de82dece9310b97c73e97"
},
"dist": {
"type": "zip",
- "url": "https://api.github.com/repos/aik099/CodingStandard/zipball/e389596b9b1fc8f67f1cca34a1c8bdd9d85104d0",
- "reference": "e389596b9b1fc8f67f1cca34a1c8bdd9d85104d0",
+ "url": "https://api.github.com/repos/aik099/CodingStandard/zipball/3680b3926f0e936dc95de82dece9310b97c73e97",
+ "reference": "3680b3926f0e936dc95de82dece9310b97c73e97",
"shasum": ""
},
"require-dev": {
"squizlabs/php_codesniffer": "~2.0"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause"
],
"authors": [
{
"name": "Alexander Obuhovich",
"email": "aik.bold@gmail.com"
}
],
"description": "The PHP_CodeSniffer coding standard I'm using on all of my projects.",
"keywords": [
"PHP_CodeSniffer",
"codesniffer"
],
- "time": "2014-12-29 12:08:25"
+ "time": "2015-04-10 07:05:51"
+ },
+ {
+ "name": "nikic/php-parser",
+ "version": "v1.2.2",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/nikic/PHP-Parser.git",
+ "reference": "08f97eb4efa029e2fafb6d8c98b71731bf0cf621"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/08f97eb4efa029e2fafb6d8c98b71731bf0cf621",
+ "reference": "08f97eb4efa029e2fafb6d8c98b71731bf0cf621",
+ "shasum": ""
+ },
+ "require": {
+ "ext-tokenizer": "*",
+ "php": ">=5.3"
+ },
+ "type": "library",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.2-dev"
+ }
+ },
+ "autoload": {
+ "files": [
+ "lib/bootstrap.php"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "BSD-3-Clause"
+ ],
+ "authors": [
+ {
+ "name": "Nikita Popov"
+ }
+ ],
+ "description": "A PHP parser written in PHP",
+ "keywords": [
+ "parser",
+ "php"
+ ],
+ "time": "2015-04-03 14:33:59"
}
],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
"aik099/coding-standard": 20
},
"prefer-stable": false,
+ "prefer-lowest": false,
"platform": [],
"platform-dev": []
}

Event Timeline