Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Tue, May 20, 4:49 AM

in-portal

This file is larger than 256 KB, so syntax highlighting was skipped.
Index: branches/5.2.x/core/kernel/session/inp_session.php
===================================================================
--- branches/5.2.x/core/kernel/session/inp_session.php (revision 14627)
+++ branches/5.2.x/core/kernel/session/inp_session.php (revision 14628)
@@ -1,81 +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 InpSession extends Session
{
+ /**
+ * Enter description here...
+ *
+ * @var InpSessionStorage
+ * @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) {
+
+ if ( !$cookie_name ) {
$cookie_name = 'sid';
}
$admin_session = ($this->Application->isAdmin && $special !== 'front') || ($special == 'admin');
- if ($admin_session) {
+ if ( $admin_session ) {
$cookie_name = 'adm_' . $cookie_name;
}
$this->SetCookieName($cookie_name);
-
$this->SetCookieDomain(SERVER_NAME);
- if ($admin_session) {
+ if ( $admin_session ) {
$mode = self::smAUTO;
}
- elseif (defined('IS_INSTALL') && IS_INSTALL) {
+ elseif ( defined('IS_INSTALL') && IS_INSTALL ) {
$mode = self::smCOOKIES_ONLY;
}
else {
$ses_mode = $this->Application->ConfigValue('CookieSessions');
- if ($ses_mode == 2) $mode = self::smAUTO;
- if ($ses_mode == 1) $mode = self::smCOOKIES_ONLY;
- if ($ses_mode == 0) $mode = self::smGET_ONLY;
+ 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);
- if (!$this->Application->isAdmin && $this->GetField('PortalUserId') <= 0) {
- $group_list = $this->Application->ConfigValue('User_GuestGroup').','.$this->Application->ConfigValue('User_LoggedInGroup');
+ if ( !$this->Application->isAdmin && $this->GetField('PortalUserId') <= 0 ) {
+ $group_list = $this->Application->ConfigValue('User_GuestGroup') . ',' . $this->Application->ConfigValue('User_LoggedInGroup');
$this->SetField('GroupId', $this->Application->ConfigValue('User_GuestGroup'));
$this->SetField('GroupList', $group_list);
}
}
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.2.x/core/kernel/session/session.php
===================================================================
--- branches/5.2.x/core/kernel/session/session.php (revision 14627)
+++ branches/5.2.x/core/kernel/session/session.php (revision 14628)
@@ -1,1044 +1,1049 @@
<?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 {
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
+ * @access protected
*/
- var $Storage;
+ 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);
$this->CheckIfCookiesAreOn();
if ($this->CookiesEnabled) $_COOKIE['cookies_on'] = 1;
$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 agent 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;
$expire_event = new kEvent('u:OnSessionExpire');
$this->Application->HandleEvent($expire_event);
}
}
/**
* This is redirect from https to http or via versa
*
* @return bool
*/
function IsHTTPSRedirect()
{
$http_referer = array_key_exists('HTTP_REFERER', $_SERVER) ? $_SERVER['HTTP_REFERER'] : false;
return (
( PROTOCOL == 'https://' && preg_match('#http:\/\/#', $http_referer) )
||
( PROTOCOL == 'http://' && preg_match('#https:\/\/#', $http_referer) )
);
}
/**
* 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) {
+ if ( $this->Mode == self::smGET_ONLY ) {
//we don't need to bother checking if we would not use it
$this->CookiesEnabled = false;
- return;
+ return false;
}
$http_query =& $this->Application->recallObject('HTTPQuery');
+ /* @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->IsHTTPSRedirect() && $get_sid) || $this->getFlashSID()) { // Redirect from http to https on different domain OR flash uploader
+ if ( ($this->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->IsHTTPSRedirect() || $this->getFlashSID()) {
+ if ( !$cookies_on || $this->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->IsHTTPSRedirect()) {
+ if ( !$is_install && $this->_checkCookieReferer() && !$this->Application->GetVar('admin') && !$this->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, adodb_mktime() + 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 < adodb_mktime()) {
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)
);
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, $value, adodb_mktime() - 3600, $this->CookiePath, $old_style_domain, $this->CookieSecure);
}
}
setcookie($name, $value, $expires, $this->CookiePath, $this->CookieDomain, $this->CookieSecure);
}
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 < adodb_mktime()) {
// 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');
/* @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');
+ /* @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()
{
list ($usec, $sec) = explode(' ', microtime());
$sid_part_1 = substr($usec, 4, 4);
$sid_part_2 = mt_rand(1, 9);
$sid_part_3 = substr($sec, 6, 4);
$digit_one = substr($sid_part_1, 0, 1);
if ($digit_one == 0) {
$digit_one = mt_rand(1, 9);
$sid_part_1 = preg_replace('/^0/', '', $sid_part_1);
$sid_part_1 = $digit_one . $sid_part_1;
}
$this->setSID($sid_part_1 . $sid_part_2 . $sid_part_3);
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 = adodb_mktime() + $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('UserSession');
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);
$_COOKIE[$this->CookieName] = $this->SID; // for compatibility with in-portal
}
function RemoveSessionCookie()
{
$this->SetCookie($this->CookieName, '');
$this->SetCookie($this->CookieName.'_live', '');
$_COOKIE[$this->CookieName] = null; // for compatibility with in-portal
}
/**
* 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() );
}
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->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);
}
}
$this->Application->Debugger->dumpVars($session_data);
if (!$this->RecallVar('admin')) {
// dump real keys (only for front-end)
$data_keys = array_keys($session_data);
$optional_keys = array_keys($this->OptionalData);
$real_keys = array_diff($data_keys, $optional_keys);
if ($real_keys) {
$ret = '';
foreach ($real_keys as $real_key) {
$ret .= '[' . $real_key . '] = [' . $session_data[$real_key] . ']<br/>';
}
$this->Application->Debugger->appendHTML('Real Keys:<br/> ' . $ret);
}
}
}
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']).'|'.mb_substr($last_env, mb_strlen(ENV_VAR_NAME) + 1);
$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, null, false);
$last_template = basename($_SERVER['PHP_SELF']).'|'.mb_substr($last_env, mb_strlen(ENV_VAR_NAME) + 1);
// save last_template in persistant 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 (isset($this->Application->HttpQuery->Get['section'])) {
// check directly in GET, bacause 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 mistical purposes (customizations may be)
$this->StoreVar('last_url', $_SERVER['REQUEST_URI']); // needed by ord:StoreContinueShoppingLink
$this->StoreVar('last_env', mb_substr($last_env, mb_strlen(ENV_VAR_NAME)+1));
$save_last_template = array_key_exists('save_last_template', $params) ? $params['save_last_template'] : true;
if ($save_last_template) {
// save last template here, becase section & module could be added before
$this->StoreVar(rtrim('last_template_popup_'.$wid, '_'), $last_template);
}
}
function getLastTemplateENV($t, $params = null, $encode = true)
{
if (!isset($params)) {
$params = Array ();
}
$params['__URLENCODE__'] = 1; // uses "&" instead of "&amp;" for url part concatenation + replaces "\" to "%5C" (works in HTML)
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);
$ret = $this->Application->BuildEnv($t, $params, 'all');
if (!$encode) {
// cancels 2nd part of replacements, that URLENCODE does
$ret = str_replace('%5C', '\\', $ret);
}
return $ret;
}
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]);
}
}
function StorePersistentVar($name, $value, $optional = false)
{
$this->Storage->StorePersistentVar($name, $value, $optional);
}
function LoadPersistentVars()
{
$this->Storage->LoadPersistentVars();
}
function StoreVarDefault($name, $value, $optional=false)
{
$tmp = $this->RecallVar($name);
if($tmp === false || $tmp == '')
{
$this->StoreVar($name, $value, $optional);
}
}
function RecallVar($name, $default = false)
{
$ret = $this->Data->Get($name);
return ($ret === false) ? $default : $ret;
}
function RecallPersistentVar($name, $default = false)
{
return $this->Storage->RecallPersistentVar($name, $default);
}
function RemoveVar($name)
{
$this->Storage->RemoveFromData($name);
$this->Data->Remove($name);
}
function RemovePersistentVar($name)
{
return $this->Storage->RemovePersistentVar($name);
}
/**
* Ignores session varible value set before
*
* @param string $name
*/
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;
}
return $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
*
* @return Array
* @access private
*/
function DeleteSessions($session_ids, $delete_reason = SESSION_LOG_EXPIRED)
{
return $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;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/session/session_storage.php
===================================================================
--- branches/5.2.x/core/kernel/session/session_storage.php (revision 14627)
+++ branches/5.2.x/core/kernel/session/session_storage.php (revision 14628)
@@ -1,473 +1,490 @@
<?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
*
*/
class SessionStorage 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);
}
- function StoreSession($to_database = true)
+ /**
+ * 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)) {
- return false;
+ if ( defined('IS_INSTALL') && IS_INSTALL && $to_database && !$this->Application->TableFound($this->TableName) ) {
+ return;
}
$fields_hash = $this->GetSessionDefaults();
- if ($to_database) {
+ if ( $to_database ) {
$this->Conn->doInsert($fields_hash, $this->TableName);
}
foreach ($fields_hash as $field_name => $field_value) {
$this->SetField($field_name, $field_value);
}
}
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'] != $_SERVER['REMOTE_ADDR'])) {
// 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 Session $session
* @param string $var_name
* @param mixed $default
*/
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()) );
}
- function SaveData()
+ /**
+ * Saves changes in session to database using single REPLACE query
+ *
+ * @return void
+ * @access public
+ */
+ public function SaveData()
{
- if(!$this->Session->SID) return false; // can't save without sid
+ if ( !$this->Session->SID ) {
+ // can't save without sid
+ return ;
+ }
+ $replace = '';
$ses_data = $this->Session->Data->GetParams();
- $replace = '';
- foreach ($ses_data as $key => $value)
- {
- if ( isset($this->OriginalData[$key]) && $this->OriginalData[$key] == $value)
- {
+ 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));
+ 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;
+
+ if ( $replace != '' ) {
+ $query = ' REPLACE INTO ' . $this->SessionDataTable . ' (' . $this->IDField . ', ' . $this->DataVarField . ', ' . $this->DataValueField . ') VALUES ' . $replace;
$this->Conn->Query($query);
}
- if ($this->ChangedDirectVars) {
- $changes = array();
+ if ( $this->ChangedDirectVars ) {
+ $changes = Array ();
+
foreach ($this->ChangedDirectVars as $var) {
- $changes[] = $var.' = '.$this->Conn->qstr($this->DirectVars[$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());
+
+ $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 . ' > ' . adodb_mktime();
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->getUnitOption('session-log', 'TableName');
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.'PersistantSessionData
WHERE PortalUserId = '.$user_id;
$this->PersistentVars = $this->Conn->GetCol($sql, 'VariableName');
}
else {
$this->PersistentVars = Array ();
}
}
/**
* Stores variable to persistent session
*
* @param Session $session
* @param string $var_name
* @param mixed $var_value
* @param bool $optional
*/
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.'PersistantSessionData
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.'PersistantSessionData', $key_clause);
}
else {
$this->Conn->doInsert($fields_hash, TABLE_PREFIX.'PersistantSessionData');
}
}
/**
* Gets persistent variable
*
* @param Session $session
* @param string $var_name
* @param mixed $default
* @return mixed
*/
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.'PersistantSessionData
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;
}
else {
return $default;
}
}
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 . 'PersistantSessionData
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 protected
*/
protected function HasField($name) { }
/**
* Returns field values
*
* @return Array
* @access protected
*/
protected function GetFieldValues() { }
/**
* Returns unformatted field value
*
* @param string $field
* @return string
* @access protected
*/
protected function GetDBField($field) { }
/**
* Returns true, when list/item was queried/loaded
*
* @return bool
* @access protected
*/
protected function isLoaded() { }
/**
* Returns specified field value from all selected rows.
* Don't affect current record index
*
* @param string $field
* @return Array
* @access protected
*/
protected function GetCol($field) { }
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/db/cat_tag_processor.php
===================================================================
--- branches/5.2.x/core/kernel/db/cat_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/kernel/db/cat_tag_processor.php (revision 14628)
@@ -1,915 +1,923 @@
<?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 kCatDBTagProcessor extends kDBTagProcessor {
/**
* Permission Helper
*
* @var kPermissionsHelper
*/
var $PermHelper = null;
public function __construct()
{
parent::__construct();
$this->PermHelper = $this->Application->recallObject('PermissionsHelper');
}
function ItemIcon($params)
{
$grids = $this->Application->getUnitOption($this->Prefix, 'Grids');
$grid = $grids[ $params['grid'] ];
if (!array_key_exists('Icons', $grid)) {
return '';
}
$icons = $grid['Icons'];
if (array_key_exists('name', $params)) {
$icon_name = $params['name'];
return array_key_exists($icon_name, $icons) ? $icons[$icon_name] : '';
}
$status_fields = $this->Application->getUnitOption($this->Prefix, 'StatusField');
if (!$status_fields) {
return $icons['default'];
}
$object =& $this->getObject($params);
/* @var $object kDBList */
$value = $object->GetDBField($status_fields[0]); // sets base status icon
if ($value == STATUS_ACTIVE) {
// if( $object->HasField('IsPop') && $object->GetDBField('IsPop') ) $value = 'POP';
// if( $object->HasField('IsHot') && $object->GetDBField('IsHot') ) $value = 'HOT';
if( $object->HasField('IsNew') && $object->GetDBField('IsNew') ) $value = 'NEW';
// if( $object->HasField('EditorsPick') && $object->GetDBField('EditorsPick') ) $value = 'PICK';
}
return array_key_exists($value, $icons) ? $icons[$value] : $icons['default'];
}
/**
* Allows to create valid mod-rewrite compatible link to module item
*
* @param Array $params
* @param string $id_prefix
* @return string
*/
function ItemLink($params, $id_prefix = null)
{
if ($this->Application->isAdmin) {
// link from Admin to Front-end
$params['index_file'] = 'index.php';
$params['prefix'] = '_FRONT_END_';
if ( $this->Application->ConfigValue('UseModRewrite') ) {
$params['__MOD_REWRITE__'] = 1;
}
}
if ( !isset($params['pass']) ) {
$params['pass'] = 'm,'.$this->Prefix;
}
$item_id = isset($params[$id_prefix.'_id']) && $params[$id_prefix.'_id'];
if (!$item_id) {
$item_id = $this->Application->GetVar($this->getPrefixSpecial().'_id');
if (!$item_id) {
$item_id = $this->Application->GetVar($this->Prefix.'_id');
}
}
$object =& $this->getObject($params);
/* @var $object kDBItem */
$params['m_cat_page'] = 1;
$params['m_cat_id'] = $object->GetDBField('CategoryId');
$params['pass_category'] = 1;
$params[$this->Prefix . '_id'] = $item_id ? $item_id : $object->GetID();
return $this->Application->ProcessParsedTag('m', 't', $params);
}
/**
* Builds link for browsing current item on Front-End
*
* @param Array $params
* @return string
*/
function PageBrowseLink($params)
{
$themes_helper =& $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$site_config_helper =& $this->Application->recallObject('SiteConfigHelper');
/* @var $site_config_helper SiteConfigHelper */
$settings = $site_config_helper->getSettings();
$params['editing_mode'] = $settings['default_editing_mode'];
$params['m_theme'] = $themes_helper->getCurrentThemeId();
$params['index_file'] = 'index.php';
$params['prefix'] = '_FRONT_END_';
$params['admin'] = 1;
if ($this->Application->ConfigValue('UseModRewrite')) {
$params['__MOD_REWRITE__'] = 1;
}
return $this->ItemLink($params);
}
function CategoryPath($params)
{
if ($this->Application->isAdminUser) {
// path for module root category in admin
if (!isset($params['cat_id'])) {
$params['cat_id'] = $this->Application->RecallVar($params['session_var'], 0);
}
}
else {
// path for category item category in front-end
$object =& $this->getObject($params);
$params['cat_id'] = $object->GetDBField('CategoryId');
}
return $this->Application->ProcessParsedTag('c', 'CategoryPath', $params);
}
function BuildListSpecial($params)
{
if ($this->Special != '') return $this->Special;
if ( isset($params['parent_cat_id']) ) {
$parent_cat_id = $params['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');
}
}
$recursive = isset($params['recursive']);
$list_unique_key = $this->getUniqueListKey($params).$recursive;
if ($list_unique_key == '') {
return parent::BuildListSpecial($params);
}
return crc32($parent_cat_id.$list_unique_key);
}
function CatalogItemCount($params)
{
$params['skip_quering'] = true;
$object =& $this->GetList($params);
return $object->GetRecordsCount(false) != $object->GetRecordsCount() ? $object->GetRecordsCount().' / '.$object->GetRecordsCount(false) : $object->GetRecordsCount();
}
function ListReviews($params)
{
- $prefix = $this->Prefix.'-rev';
- $review_tag_processor =& $this->Application->recallObject($prefix.'.item_TagProcessor');
+ $prefix = $this->Prefix . '-rev';
+
+ $review_tag_processor =& $this->Application->recallObject($prefix . '.item_TagProcessor');
+ /* @var $review_tag_processor kDBTagProcessor */
+
return $review_tag_processor->PrintList($params);
}
function ReviewCount($params)
{
$review_tag_processor =& $this->Application->recallObject('rev.item_TagProcessor');
+ /* @var $review_tag_processor kDBTagProcessor */
+
return $review_tag_processor->TotalRecords($params);
}
function InitCatalogTab($params)
{
$tab_params['mode'] = $this->Application->GetVar('tm'); // single/multi selection possible
$tab_params['special'] = $this->Application->GetVar('ts'); // use special for this tab
$tab_params['dependant'] = $this->Application->GetVar('td'); // is grid dependant on categories grid
// set default params (same as in catalog)
if ($tab_params['mode'] === false) $tab_params['mode'] = 'multi';
if ($tab_params['special'] === false) $tab_params['special'] = '';
if ($tab_params['dependant'] === false) $tab_params['dependant'] = 'yes';
// pass params to block with tab content
$params['name'] = $params['render_as'];
$special = $tab_params['special'] ? $tab_params['special'] : $this->Special;
$params['prefix'] = trim($this->Prefix.'.'.$special, '.');
$prefix_append = $this->Application->GetVar('prefix_append');
if ($prefix_append) {
$params['prefix'] .= $prefix_append;
}
$default_grid = array_key_exists('default_grid', $params) ? $params['default_grid'] : 'Default';
$radio_grid = array_key_exists('radio_grid', $params) ? $params['radio_grid'] : 'Radio';
$params['cat_prefix'] = trim('c.'.$special, '.');
$params['tab_mode'] = $tab_params['mode'];
$params['grid_name'] = ($tab_params['mode'] == 'multi') ? $default_grid : $radio_grid;
$params['tab_dependant'] = $tab_params['dependant'];
$params['show_category'] = $tab_params['special'] == 'showall' ? 1 : 0; // this is advanced view -> show category name
if ($special == 'showall' || $special == 'user') {
$params['grid_name'] .= 'ShowAll';
}
// use $pass_params to be able to pass 'tab_init' parameter from m_ModuleInclude tag
return $this->Application->ParseBlock($params, 1);
}
/**
* Show CachedNavbar of current item primary category
*
* @param Array $params
* @return string
*/
function CategoryName($params)
{
// show category cachednavbar of
$object =& $this->getObject($params);
$category_id = isset($params['cat_id']) ? $params['cat_id'] : $object->GetDBField('CategoryId');
$cache_key = 'category_paths[%CIDSerial:' . $category_id . '%][%PhrasesSerial%][Adm:' . (int)$this->Application->isAdmin . ']';
$category_path = $this->Application->getCache($cache_key);
if ($category_path === false) {
// not chached
if ($category_id > 0) {
$cached_navbar = preg_replace('/^(Content&\|&|Content)/i', '', $object->GetField('CachedNavbar'));
$category_path = trim($this->CategoryName( Array('cat_id' => 0) ).' > '.str_replace('&|&', ' > ', $cached_navbar), ' > ');
}
else {
$category_path = $this->Application->Phrase(($this->Application->isAdmin ? 'la_' : 'lu_') . 'rootcategory_name');
}
$this->Application->setCache($cache_key, $category_path);
}
return $category_path;
}
/**
* Allows to determine if original value should be shown
*
* @param Array $params
* @return bool
*/
function DisplayOriginal($params)
{
// original id found & greather then zero + show original
$display_original = isset($params['display_original']) && $params['display_original'];
$owner_field = $this->Application->getUnitOption($this->Prefix, 'OwnerField');
if (!$owner_field) {
$owner_field = 'CreatedById';
}
$object =& $this->getObject($params);
$perm_value = $this->PermHelper->ModifyCheckPermission($object->GetDBField($owner_field), $object->GetDBField('CategoryId'), $this->Prefix);
return $display_original && ($perm_value == 1) && $this->Application->GetVar($this->Prefix.'.original_id');
}
/**
* Checks if user have one of required permissions
*
* @param Array $params
* @return bool
*/
function HasPermission($params)
{
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$params['raise_warnings'] = 0;
$object =& $this->getObject($params);
/* @var $object kCatDBItem */
// 1. category restriction
$params['cat_id'] = $object->isLoaded() ? $object->GetDBField('ParentPath') : $this->Application->GetVar('m_cat_id');
// 2. owner restriction
$owner_field = $this->Application->getUnitOption($this->Prefix, 'OwnerField');
if (!$owner_field) {
$owner_field = 'CreatedById';
}
$is_owner = $object->GetDBField($owner_field) == $this->Application->RecallVar('user_id');
return $perm_helper->TagPermissionCheck($params, $is_owner);
}
/**
* Creates link to current category or to module root category, when current category is home
*
* @param Array $params
* @return string
*/
function SuggestItemLink($params)
{
if (!isset($params['cat_id'])) {
$params['cat_id'] = $this->Application->GetVar('m_cat_id');
}
if ($params['cat_id'] == 0) {
$params['cat_id'] = $this->Application->findModule('Var', $this->Prefix, 'RootCat');
}
$params['m_cat_page'] = 1;
return $this->Application->ProcessParsedTag('c', 'CategoryLink', $params);
}
/**
* Allows to detect if item has any additional images available
*
* @param Array $params
* @return string
*/
function HasAdditionalImages($params)
{
$object =& $this->getObject($params);
/* @var $object kDBItem */
$cache_key = $object->Prefix . '_additional_images[%' . $this->Application->incrementCacheSerial($object->Prefix, $object->GetID(), false) . '%]';
$ret = $this->Application->getCache($cache_key);
if ($ret === false) {
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT ImageId
FROM ' . $this->Application->getUnitOption('img', 'TableName') . '
WHERE ResourceId = ' . $object->GetDBField('ResourceId') . ' AND DefaultImg != 1 AND Enabled = 1';
$ret = $this->Conn->GetOne($sql) ? 1 : 0;
$this->Application->setCache($cache_key, $ret);
}
return $ret;
}
/**
* Checks that item is pending
*
* @param Array $params
* @return bool
*/
function IsPending($params)
{
$object =& $this->getObject($params);
$pending_status = Array (STATUS_PENDING, STATUS_PENDING_EDITING);
return in_array($object->GetDBField('Status'), $pending_status);
}
function IsFavorite($params)
{
static $favorite_status = Array ();
$object =& $this->getObject($params);
/* @var $object kDBList */
if (!isset($favorite_status[$this->Special])) {
$resource_ids = $object->GetCol('ResourceId');
$user_id = $this->Application->RecallVar('user_id');
$sql = 'SELECT FavoriteId, ResourceId
FROM '.$this->Application->getUnitOption('fav', 'TableName').'
WHERE (PortalUserId = '.$user_id.') AND (ResourceId IN ('.implode(',', $resource_ids).'))';
$favorite_status[$this->Special] = $this->Conn->GetCol($sql, 'ResourceId');
}
return isset($favorite_status[$this->Special][$object->GetDBField('ResourceId')]);
}
/**
* Returns item's editors pick status (using not formatted value)
*
* @param Array $params
* @return bool
*/
function IsEditorsPick($params)
{
$object =& $this->getObject($params);
return $object->GetDBField('EditorsPick') == 1;
}
function FavoriteToggleLink($params)
{
$fav_prefix = $this->Prefix.'-fav';
$params['pass'] = implode(',', Array('m', $this->Prefix, $fav_prefix));
$params[$fav_prefix.'_event'] = 'OnFavoriteToggle';
return $this->ItemLink($params);
}
/**
* Checks if item is passed in url
*
* @param Array $params
* @return bool
*/
function ItemAvailable($params)
{
return $this->Application->GetVar($this->getPrefixSpecial().'_id') > 0;
}
function SortingSelected($params)
{
$list =& $this->GetList($params);
$user_sorting_start = $this->getUserSortIndex();
// remove language prefix from $current_sorting_field
$current_sorting_field = preg_replace('/^l[\d]+_(.*)/', '\\1', $list->GetOrderField($user_sorting_start));
$current_sorting = $current_sorting_field . '|' . $list->GetOrderDirection($user_sorting_start);
return strtolower($current_sorting) == strtolower($params['sorting']) ? $params['selected'] : '';
}
function CombinedSortingDropDownName($params)
{
$list =& $this->GetList($params);
if ($list->isMainList()) {
return parent::CombinedSortingDropDownName($params);
}
return $list->Prefix . '_CombinedSorting';
}
/**
* Prepares name for field with event in it (used only on front-end)
*
* @param Array $params
* @return string
*/
function SubmitName($params)
{
$list =& $this->GetList($params);
if ($list->isMainList()) {
return parent::SubmitName($params);
}
return 'events[' . $list->Prefix . '][' . $params['event'] . ']';
}
/**
* Returns prefix + any word (used for shared between categories per page settings)
*
* @param Array $params
* @return string
*/
function VarName($params)
{
$list =& $this->GetList($params);
if ($list->isMainList()) {
return parent::VarName($params);
}
return $list->Prefix . '_' . $params['type'];
}
/**
* Checks if we are viewing module root category
*
* @param Array $params
* @return bool
*/
function IsModuleHome($params)
{
$root_category = $this->Application->findModule('Var', $this->Prefix, 'RootCat');
return $root_category == $this->Application->GetVar('m_cat_id');
}
/**
* Dynamic votes indicator
*
* @param Array $params
*
* @return string
*/
function VotesIndicator($params)
{
$object =& $this->getObject($params);
/* @var $object kDBItem */
$rating_helper =& $this->Application->recallObject('RatingHelper');
/* @var $rating_helper RatingHelper */
$small_style = array_key_exists('small_style', $params) ? $params['small_style'] : false;
return $rating_helper->ratingBar($object, true, '', $small_style);
}
function RelevanceIndicator($params)
{
$object =& $this->getObject($params);
$search_results_table = TABLE_PREFIX.'ses_'.$this->Application->GetSID().'_'.TABLE_PREFIX.'Search';
$sql = 'SELECT Relevance
FROM '.$search_results_table.'
WHERE ResourceId = '.$object->GetDBField('ResourceId');
$percents_off = (int)(100 - (100 * $this->Conn->GetOne($sql)));
$percents_off = ($percents_off < 0) ? 0 : $percents_off;
if ($percents_off) {
$params['percent_off'] = $percents_off;
$params['percent_on'] = 100 - $percents_off;
$params['name'] = $this->SelectParam($params, 'relevance_normal_render_as,block_relevance_normal');
}
else {
$params['name'] = $this->SelectParam($params, 'relevance_full_render_as,block_relevance_full');
}
return $this->Application->ParseBlock($params);
}
function SearchResultField($params)
{
$ret = $this->Field($params);
$keywords = unserialize( $this->Application->RecallVar('highlight_keywords') );
$opening = $this->Application->ParseBlock( Array('name' => $this->SelectParam($params, 'highlight_opening_render_as,block_highlight_opening')) );
$closing = $this->Application->ParseBlock( Array('name' => $this->SelectParam($params, 'highlight_closing_render_as,block_highlight_closing')) );
foreach ($keywords as $index => $keyword) {
$keywords[$index] = preg_quote($keyword, '/');
}
return preg_replace('/('.implode('|', $keywords).')/i', $opening.'\\1'.$closing, $ret);
}
/**
* Shows keywords, that user searched
*
* @param Array $params
* @return bool
*/
function SearchKeywords($params)
{
$keywords = $this->Application->GetVar('keywords');
$sub_search = $this->Application->GetVar('search_type') == 'subsearch';
return ($keywords !== false) && !$sub_search ? $keywords : $this->Application->RecallVar('keywords');
}
function AdvancedSearchForm($params)
{
$search_table = $this->Application->getUnitOption('confs', 'TableName');
$module_name = $this->Application->findModule('Var', $this->Prefix, 'Name');
$sql = 'SELECT *
FROM '.$search_table.'
WHERE (ModuleName = '.$this->Conn->qstr($module_name).') AND (AdvancedSearch = 1)
ORDER BY DisplayOrder';
$search_config = $this->Conn->Query($sql);
$ret = '';
foreach ($search_config as $record) {
$params['name'] = $this->SelectParam($params, 'and_or_render_as,and_or_block');
$params['field'] = $record['FieldName'];
$params['andor'] = $this->Application->ParseBlock($params);
$params['name'] = $this->SelectParam($params, $record['FieldType'].'_render_as,'.$record['FieldType'].'_block');
$params['caption'] = $this->Application->Phrase($record['DisplayName']);
$ret .= $this->Application->ParseBlock($params);
}
return $ret;
}
/**
* Returns last modification date of items in category / system
*
* @param Array $params
* @return string
*/
function LastUpdated($params)
{
- $category_id = (int)$this->Application->GetVar('m_cat_id');
- $local = array_key_exists('local', $params) && ($category_id > 0) ? $params['local'] : false;
+ $category_id = (int)$this->Application->GetVar('m_cat_id');
+ $local = array_key_exists('local', $params) && ($category_id > 0) ? $params['local'] : false;
$serial_name1 = $this->Application->incrementCacheSerial('c', $local ? $category_id : null, false);
$serial_name2 = $this->Application->incrementCacheSerial($this->Prefix, null, false);
$cache_key = 'categoryitems_last_updated[%' . $serial_name1 . '%][%' . $serial_name2 . '%]';
$row_data = $this->Application->getCache($cache_key);
- if ($row_data === false) {
- if ($local && ($category_id > 0)) {
+ if ( $row_data === false ) {
+ if ( $local && ($category_id > 0) ) {
// scan only current category & it's children
- list ($tree_left, $tree_right) = $this->Application->getTreeIndex($category_id);
+ list ($tree_left, $tree_right) = $this->Application->getTreeIndex($category_id);
- $sql = 'SELECT MAX(item_table.Modified) AS ModDate, MAX(item_table.CreatedOn) AS NewDate
+ $sql = 'SELECT MAX(item_table.Modified) AS ModDate, MAX(item_table.CreatedOn) AS NewDate
FROM ' . $this->Application->getUnitOption($this->Prefix, 'TableName') . ' item_table
LEFT JOIN ' . TABLE_PREFIX . 'CategoryItems ci ON (item_table.ResourceId = ci.ItemResourceId)
LEFT JOIN ' . TABLE_PREFIX . 'Category c ON c.CategoryId = ci.CategoryId
WHERE c.TreeLeft BETWEEN ' . $tree_left . ' AND ' . $tree_right;
- }
+ }
else {
// scan all categories in system
$sql = 'SELECT MAX(Modified) AS ModDate, MAX(CreatedOn) AS NewDate
FROM ' . $this->Application->getUnitOption($this->Prefix, 'TableName');
- }
+ }
- $this->Conn->nextQueryCachable = true;
+ $this->Conn->nextQueryCachable = true;
$row_data = $this->Conn->GetRow($sql);
$this->Application->setCache($cache_key, $row_data);
}
- if (!$row_data) {
- return '';
- }
-
- $date = $row_data[ $row_data['NewDate'] > $row_data['ModDate'] ? 'NewDate' : 'ModDate' ];
-
- // format date
- $format = isset($params['format']) ? $params['format'] : '_regional_DateTimeFormat';
- if (preg_match("/_regional_(.*)/", $format, $regs)) {
+ if ( !$row_data ) {
+ return '';
+ }
+
+ $date = $row_data[$row_data['NewDate'] > $row_data['ModDate'] ? 'NewDate' : 'ModDate'];
+
+ // format date
+ $format = isset($params['format']) ? $params['format'] : '_regional_DateTimeFormat';
+
+ if ( preg_match("/_regional_(.*)/", $format, $regs) ) {
$lang =& $this->Application->recallObject('lang.current');
- if ($regs[1] == 'DateTimeFormat') {
+ /* @var $lang LanguagesItem */
+
+ if ( $regs[1] == 'DateTimeFormat' ) {
// combined format
- $format = $lang->GetDBField('DateFormat').' '.$lang->GetDBField('TimeFormat');
+ $format = $lang->GetDBField('DateFormat') . ' ' . $lang->GetDBField('TimeFormat');
}
else {
// simple format
$format = $lang->GetDBField($regs[1]);
}
}
return adodb_date($format, $date);
}
/**
* Counts category item count in system (not category-dependent)
*
* @param Array $params
* @return int
*/
function ItemCount($params)
{
$count_helper =& $this->Application->recallObject('CountHelper');
/* @var $count_helper kCountHelper */
$today_only = isset($params['today']) && $params['today'];
return $count_helper->ItemCount($this->Prefix, $today_only);
}
function CategorySelector($params)
{
$category_id = isset($params['category_id']) && is_numeric($params['category_id']) ? $params['category_id'] : false;
if ($category_id === false) {
// if category id not given use module root category
$category_id = $this->Application->findModule('Var', $this->Prefix, 'RootCat');
}
$id_field = $this->Application->getUnitOption('c', 'IDField');
$title_field = $this->Application->getUnitOption('c', 'TitleField');
$table_name = $this->Application->getUnitOption('c', 'TableName');
$count_helper =& $this->Application->recallObject('CountHelper');
/* @var $count_helper kCountHelper */
list ($view_perm, $view_filter) = $count_helper->GetPermissionClause('c', 'perm_cache');
// get category list (permission based)
$sql = 'SELECT c.'.$title_field.' AS CategoryName, c.'.$id_field.', c.l' . $this->Application->GetVar('m_lang') . '_CachedNavbar AS CachedNavbar
FROM '.$table_name.' c
INNER JOIN '.TABLE_PREFIX.'PermCache perm_cache ON c.CategoryId = perm_cache.CategoryId
WHERE (ParentId = '.$category_id.') AND ('.$view_filter.') AND (perm_cache.PermId = '.$view_perm.') AND (c.Status = '.STATUS_ACTIVE.')
ORDER BY c.'.$title_field.' ASC';
$categories = $this->Conn->Query($sql, $id_field);
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
$block_params['strip_nl'] = 2;
$ret = '';
foreach ($categories as $category_id => $category_data) {
// print category
$block_params['separator'] = isset($params['category_id']) ? $params['separator'] : ''; // return original separator, remove separator for top level categories
$block_params['category_id'] = $category_id;
$block_params['category_name'] = $category_data['CategoryName'];
$cached_navbar = preg_replace('/^(Content&\|&|Content)/i', '', $category_data['CachedNavbar']);
$block_params['full_path'] = str_replace('&|&', ' > ', $cached_navbar);
$ret .= $this->Application->ParseBlock($block_params);
// print it's children
$block_params['separator'] = '&nbsp;&nbsp;&nbsp;'.$params['separator'];
$ret .= $this->CategorySelector($block_params);
}
return $ret;
}
function PrintMoreCategories($params)
{
$object =& $this->getObject();
/* @var $object kDBItem */
$category_ids = $this->Field($params);
if (!$category_ids) {
return '';
}
$category_ids = explode('|', substr($category_ids, 1, -1));
$id_field = $this->Application->getUnitOption('c', 'IDField');
$title_field = $this->Application->getUnitOption('c', 'TitleField');
$table_name = $this->Application->getUnitOption('c', 'TableName');
$sql = 'SELECT '.$title_field.' AS CategoryName, '.$id_field.', l' . $this->Application->GetVar('m_lang') . '_CachedNavbar AS CachedNavbar
FROM '.$table_name.'
WHERE '.$id_field.' IN ('.implode(',', $category_ids).')';
$categories = $this->Conn->Query($sql, $id_field);
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
$ret = '';
foreach ($categories as $category_id => $category_data) {
$block_params['category_id'] = $category_id;
$block_params['category_name'] = $category_data['CategoryName'];
$cached_navbar = preg_replace('/^(Content&\|&|Content)/i', '', $category_data['CachedNavbar']);
$block_params['full_path'] = str_replace('&|&', ' > ', $cached_navbar);
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
function DownloadFileLink($params)
{
$params[$this->getPrefixSpecial().'_event'] = 'OnDownloadFile';
return $this->ItemLink($params);
}
function ImageSrc($params)
{
list ($ret, $tag_processed) = $this->processAggregatedTag('ImageSrc', $params, $this->getPrefixSpecial());
return $tag_processed ? $ret : false;
}
/**
* Registers hit for item (one time per session)
*
* @param Array $params
*/
function RegisterHit($params)
{
$object =& $this->getObject();
/* @var $object kCatDBItem */
if ($object->isLoaded()) {
$object->RegisterHit();
}
}
/**
* Returns link to item's author public profile
*
* @param Array $params
* @return string
*/
function ProfileLink($params)
{
$object =& $this->getObject($params);
$owner_field = array_key_exists('owner_field', $params) ? $params['owner_field'] : 'CreatedById';
$params['user_id'] = $object->GetDBField($owner_field);
unset($params['owner_field']);
return $this->Application->ProcessParsedTag('m', 'Link', $params);
}
/**
* Checks, that "view in browse mode" functionality available
*
* @param Array $params
* @return bool
*/
function BrowseModeAvailable($params)
{
$valid_special = $valid_special = $params['Special'] != 'user';
$not_selector = $this->Application->GetVar('type') != 'item_selector';
return $valid_special && $not_selector;
}
/**
* Returns a link for editing product
*
* @param Array $params
* @return string
*/
function ItemEditLink($params)
{
$object =& $this->getObject();
/* @var $object kDBList */
$edit_template = $this->Application->getUnitOption($this->Prefix, 'AdminTemplatePath') . '/' . $this->Application->getUnitOption($this->Prefix, 'AdminTemplatePrefix') . 'edit';
$url_params = Array (
'm_opener' => 'd',
$this->Prefix.'_mode' => 't',
$this->Prefix.'_event' => 'OnEdit',
$this->Prefix.'_id' => $object->GetID(),
'm_cat_id' => $object->GetDBField('CategoryId'),
'pass' => 'all,'.$this->Prefix,
'no_pass_through' => 1,
);
return $this->Application->HREF($edit_template,'', $url_params);
}
function LanguageVisible($params)
{
$field = $this->SelectParam($params, 'name,field');
preg_match('/l([\d]+)_(.*)/', $field, $regs);
$params['name'] = $regs[2];
return $this->HasLanguageError($params) || $this->Application->GetVar('m_lang') == $regs[1];
}
function HasLanguageError($params)
{
static $languages = null;
if (!isset($languages)) {
$sql = 'SELECT ' . $this->Application->getUnitOption('lang', 'IDField') . '
FROM ' . $this->Application->getUnitOption('lang', 'TableName') . '
WHERE Enabled = 1';
$languages = $this->Conn->GetCol($sql);
}
$field = $this->SelectParam($params, 'name,field');
$object =& $this->getObject($params);
/* @var $object kDBItem */
foreach ($languages as $language_id) {
$check_field = 'l' . $language_id . '_' . $field;
if ($object->GetErrorMsg($check_field, false)) {
return true;
}
}
return false;
}
/**
* Returns list of categories, that have add/edit permission for current category item type
*
* @param Array $params
* @return string
*/
function AllowedCategoriesJSON($params)
{
if ($this->Application->RecallVar('user_id') == USER_ROOT) {
$categories = true;
}
else {
$object =& $this->getObject($params);
/* @var $object kDBItem */
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$perm_prefix = $this->Application->getUnitOption($this->Prefix, 'PermItemPrefix');
$categories = $perm_helper->getPermissionCategories($perm_prefix . '.' . ($object->IsNewItem() ? 'ADD' : 'MODIFY'));
}
$json_helper =& $this->Application->recallObject('JSONHelper');
/* @var $json_helper JSONHelper */
return $json_helper->encode($categories);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/db/cat_event_handler.php
===================================================================
--- branches/5.2.x/core/kernel/db/cat_event_handler.php (revision 14627)
+++ branches/5.2.x/core/kernel/db/cat_event_handler.php (revision 14628)
@@ -1,2679 +1,2814 @@
<?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 standart permission mapping
*
*/
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'),
'OnCopy' => Array ('self' => true),
'OnDownloadFile' => Array ('self' => 'view'),
'OnCancelAction' => Array ('self' => true),
'OnItemBuild' => Array ('self' => true),
'OnMakeVote' => Array ('self' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Load item if id is available
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
function LoadItem(&$event)
{
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$id = $this->getPassedID($event);
- if ($object->Load($id)) {
+
+ if ( $object->Load($id) ) {
$actions =& $this->Application->recallObject('kActions');
- $actions->Set($event->getPrefixSpecial().'_id', $object->GetID() );
+ /* @var $actions Params */
+
+ $actions->Set($event->getPrefixSpecial() . '_id', $object->GetID());
$use_pending_editing = $this->Application->getUnitOption($event->Prefix, 'UsePendingEditing');
- if ($use_pending_editing && $event->Special != 'original') {
- $this->Application->SetVar($event->Prefix.'.original_id', $object->GetDBField('OrgId'));
+
+ if ( $use_pending_editing && $event->Special != 'original' ) {
+ $this->Application->SetVar($event->Prefix . '.original_id', $object->GetDBField('OrgId'));
}
}
else {
$object->setID($id);
}
}
/**
- * Checks permissions of user
+ * Checks user permission to execute given $event
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$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
*/
- function OnCopy(&$event)
+ 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
*/
- function OnCut(&$event)
+ 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
*/
- function OnPaste(&$event)
+ protected function OnPaste(&$event)
{
- if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) || !$this->_checkPastePermission($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 false;
+ if ( !$clipboard_data['cut'] && !$clipboard_data['copy'] ) {
+ return;
}
- if ($clipboard_data['copy']) {
- $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
+ if ( $clipboard_data['copy'] ) {
+ $temp =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler');
/* @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));
+ 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
*/
- function OnMassDelete(&$event)
+ protected function OnMassDelete(&$event)
{
- if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
+ if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) {
$event->status = kEvent::erFAIL;
return;
}
- $event->status=kEvent::erSUCCESS;
-
$ids = $this->StoreSelectedIDs($event);
- $to_delete = array();
- if ($recycle_bin = $this->Application->ConfigValue('RecycleBinFolder')) {
- $rb =& $this->Application->recallObject('c.recycle', null, array('skip_autoload' => true));
+ $to_delete = Array ();
+
+ if ( $recycle_bin = $this->Application->ConfigValue('RecycleBinFolder') ) {
+ $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));
+
+ $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'))) {
+
+ if ( preg_match('/^' . preg_quote($rb->GetDBField('ParentPath'), '/') . '/', $object->GetDBField('ParentPath')) ) {
$to_delete[] = $id;
continue;
}
+
$object->MoveToCat($recycle_bin);
}
+
$ids = $to_delete;
}
- $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
+ $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler');
+ /* @var $temp_handler kTempTablesHandler */
$event->setEventParam('ids', $ids);
$this->customProcessing($event, 'before');
$ids = $event->getEventParam('ids');
- if($ids)
- {
- $temp->DeleteItems($event->Prefix, $event->Special, $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 = $this->Application->getUnitOption($event->Prefix, 'IDField');
$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'
);
$type = $this->Application->GetVar('search_type', 'simple');
if ($keywords = $event->getEventParam('keyword_string')) {
// 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 LEFT JOIN %1$s ON %1$s.ResourceId = search_result.ResourceId';
$sql = str_replace('FROM %1$s', $search_sql, $object->SelectClause);
$object->SetSelectSQL($sql);
$object->addCalculatedField('Relevance', 'search_result.Relevance');
$object->AddOrderField('search_result.Relevance', 'desc', true);
$type_clauses['search']['include'] = 'PrimaryCat = 1 AND ('.TABLE_PREFIX.'Category.Status = '.STATUS_ACTIVE.')';
$type_clauses['search']['except'] = 'PrimaryCat = 1 AND ('.TABLE_PREFIX.'Category.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->getUnitOption('rel', 'TableName');
$item_type = (int)$this->Application->getUnitOption($event->Prefix, 'ItemType');
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).') 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->getUnitOption('fav', 'TableName').'
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.'Category.TreeLeft BETWEEN '.$tree_indexes['TreeLeft'].' AND '.$tree_indexes['TreeRight'];
}
/**
- * Apply filters to list
+ * Apply any custom changes to list's sql query
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
parent::SetCustomQuery($event);
$object =& $event->getObject();
/* @var $object kDBList */
// add category filter if needed
if ($event->Special != 'showall' && $event->Special != 'user') {
if ($event->getEventParam('parent_cat_id') !== false) {
$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);
}
// add permission filter
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 itemlist 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($event->Prefix, 'perm');
$object->addFilter('perm_filter2', $view_filter);
}
$object->addFilter('perm_filter', 'perm.PermId = '.$view_perm);
$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 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 = $this->Application->getUnitOption($object->Prefix, 'UsePendingEditing');
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 . 'Category.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 kCatDBItem $object
+ * @param kDBItem|kDBList $object
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function prepareObject(&$object, &$event)
+ protected function prepareObject(&$object, &$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 = $this->Application->getUnitOption($event->Prefix, 'ItemPropertyMappings');
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)');
}
- function CalculateHotLimit(&$event)
+ /**
+ * Calculates hot limit for current item's table
+ *
+ * @param kEvent $event
+ * @return float
+ * @access protected
+ */
+ protected function CalculateHotLimit(&$event)
{
$property_map = $this->Application->getUnitOption($event->Prefix, 'ItemPropertyMappings');
- if (!$property_map) {
- return;
+ 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 '.$this->Application->getUnitOption($event->Prefix, 'TableName').'
- ORDER BY '.$click_field.' DESC
- LIMIT '.$last_hot.', 1';
+ $sql = 'SELECT ' . $click_field . '
+ FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
+ 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)) {
+ 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
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
parent::OnBeforeItemUpdate($event);
$object =& $event->getObject();
/* @var $object kCatDBItem */
// update hits field
$property_map = $this->Application->getUnitOption($event->Prefix, 'ItemPropertyMappings');
- if ($property_map) {
+ 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 '.$this->Application->getUnitOption($event->Prefix, 'TableName').'
- WHERE FLOOR('.$click_field.') = '.$object->GetDBField($click_field);
- $hits = ( $res = $this->Conn->GetOne($sql) ) ? $res + 0.000001 : $object->GetDBField($click_field);
+ 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 ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
+ 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) {
+ if ( $object->GetOriginalField('CategoryId') != $target_category ) {
$object->MoveToCat($target_category);
}
}
/**
- * Load price from temp table if product mode is temp table
+ * Occurs after loading item, 'id' parameter
+ * allows to get id of item that was loaded
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemLoad(&$event)
+ protected function OnAfterItemLoad(&$event)
{
+ parent::OnAfterItemLoad($event);
+
$special = substr($event->Special, -6);
$object =& $event->getObject();
/* @var $object kCatDBItem */
- if ($special == 'import' || $special == 'export') {
+ if ( $special == 'import' || $special == 'export' ) {
$image_data = $object->getPrimaryImageData();
- if ($image_data) {
+ if ( $image_data ) {
$thumbnail_image = $image_data[$image_data['LocalThumb'] ? 'ThumbPath' : 'ThumbUrl'];
- if ($image_data['SameImages']) {
+ 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']);
}
}
- // substituiting pending status value for pending editing
- if ($object->HasField('OrgId') && $object->GetDBField('OrgId') > 0 && $object->GetDBField('Status') == -2) {
- $options = $object->GetFieldOption('Status', 'options');
+ // 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;
+ 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 ( array_key_exists('MoreCategories', $object->VirtualFields) ) {
+ 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).'|' : '');
+ $object->SetDBField('MoreCategories', $item_categories ? '|' . implode('|', $item_categories) . '|' : '');
}
}
+ /**
+ * Occurs after updating item
+ *
+ * @param kEvent $event
+ * @access public
+ */
function OnAfterItemUpdate(&$event)
{
$this->CalculateHotLimit($event);
- if ( substr($event->Special, -6) == 'import') {
+ if ( substr($event->Special, -6) == 'import' ) {
$this->setCustomExportColumns($event);
}
$object =& $event->getObject();
- /* @var $object kDBItem */
+ /* @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') {
+ 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) {
+ if ( $this->Application->isAdminUser && $recycle_bin ) {
$sql = 'SELECT CategoryId
FROM ' . $this->Application->getUnitOption('ci', 'TableName') . '
WHERE ItemResourceId = ' . $object->GetDBField('ResourceId') . ' AND PrimaryCat = 1';
$primary_category = $this->Conn->GetOne($sql);
- if ($primary_category == $recycle_bin) {
+ if ( $primary_category == $recycle_bin ) {
$event->CallSubEvent('OnAfterItemDelete');
}
}
}
/**
- * sets values for import process
+ * Sets values for import process
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemCreate(&$event)
+ protected function OnAfterItemCreate(&$event)
{
- if ( substr($event->Special, -6) == 'import') {
+ parent::OnAfterItemCreate($event);
+
+ if ( substr($event->Special, -6) == 'import' ) {
$this->setCustomExportColumns($event);
}
if ( !$this->Application->isAdmin ) {
$object =& $event->getObject();
- /* @var $object kDBItem */
+ /* @var $object kCatDBItem */
$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') {
+ 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.'SearchLog
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.'SearchLog');
}
$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 = kUtil::unhtmlentities( trim($this->Application->GetVar('keywords')) );
$query_object =& $this->Application->recallObject('HTTPQuery');
+ /* @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 */
$this->Application->SetVar($event->getPrefixSpecial().'_Page', 1);
$lang = $this->Application->GetVar('m_lang');
$items_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$module_name = $this->Application->findModule('Var', $event->Prefix, 'Name');
$sql = 'SELECT *
FROM ' . $this->Application->getUnitOption('confs', 'TableName') . '
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 = $this->Application->getUnitOption($event->Prefix, 'CustomFields');
if ($custom_fields) {
$custom_table = $this->Application->getUnitOption($event->Prefix.'-cdata', 'TableName');
$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) {
- $options = $object->getFieldOptions($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 (getArrayValue($options, 'formatter') == 'kMultiLanguage') {
+ if ( $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
if ($foreign_field = $search_config[$field]['ForeignField']) {
$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.'Category ON '.TABLE_PREFIX.'Category.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') {
if ($event->MasterEvent->getEventParam('ResultIds')) {
$where_clause .= ' AND '.$items_table.'.ResourceId IN ('.implode(',', $event->MasterEvent->getEventParam('ResultIds')).')';
}
}
// 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 = $this->Application->getUnitOption($event->Prefix, 'SearchConfigPostfix');
$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 = $this->Application->getUnitOption($event->Prefix.'.EditorsPick', 'Fields') ? $items_table.'.EditorsPick' : '0';
$sql = $select_intro.' SELECT '.$relevance_clause.' AS Relevance,
'.$items_table.'.'.$this->Application->getUnitOption($event->Prefix, 'IDField').' AS ItemId,
'.$items_table.'.ResourceId,
'.$this->Application->getUnitOption($event->Prefix, 'ItemType').' AS ItemType,
'.$edpick_clause.' AS EdPick
FROM '.$object->TableName.'
'.implode(' ', $join_clauses).'
WHERE '.$where_clause.'
GROUP BY '.$items_table.'.'.$this->Application->getUnitOption($event->Prefix, 'IDField').' ORDER BY Relevance DESC';
$res = $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)
{
$search_table = TABLE_PREFIX.'ses_'.$this->Application->GetSID().'_'.TABLE_PREFIX.'Search';
$sql = 'SHOW TABLES LIKE "'.$search_table.'"';
if($this->Conn->Query($sql))
{
$sql = 'SELECT DISTINCT ResourceId FROM '.$search_table;
$ids = $this->Conn->GetCol($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');
- if(!isset($query_object->Post['andor']))
- {
- return; // used when navigating by pages or changing sorting in search results
+ /* @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->getUnitOption('confs', 'TableName').'
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);
$items_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$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 = $this->Application->getUnitOption($event->Prefix, 'CustomFields');
if ($custom_fields) {
$custom_table = $this->Application->getUnitOption($event->Prefix.'-cdata', 'TableName');
$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
-
- $options = $object->getFieldOptions($field);
$local_table = TABLE_PREFIX.$record['TableName'];
$weight_sum += $record['Priority']; // counting weight sum; used when making relevance clause
// processing multilingual fields
- if (getArrayValue($options, 'formatter') == 'kMultiLanguage') {
+ if ( $object->GetFieldOption($field, 'formatter') == 'kMultiLanguage' ) {
$field_name = 'l'.$lang.'_'.$field;
}
else {
$field_name = $field;
}
// processing fields from other tables
if ($foreign_field = $record['ForeignField']) {
$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 = $this->Application->getUnitOption($event->Prefix, 'SearchConfigPostfix');
$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 = $this->Application->getUnitOption($event->Prefix, 'IDField');
$fields = $this->Application->getUnitOption($event->Prefix, 'Fields');
$pick_field = isset($fields['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] = kUtil::unhtmlentities( $keywords[$field] );
if ($keywords[$field]) {
$condition = sprintf($condition_patterns['is'], $field_name, $this->Conn->qstr( $keywords[$field] ));
}
break;
case 'multiselect':
$keywords[$field] = kUtil::unhtmlentities( $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] = kUtil::unhtmlentities( $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) {
$property_mappings = $this->Application->getUnitOption($this->Prefix, 'ItemPropertyMappings');
$items_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
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 = adodb_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 = adodb_mktime() - $time_mapping[$keywords[$field]];
}
$condition = $field_name.' > '.$min_time;
}
break;
}
return $condition;
}
- function getHuman($type, $search_data)
+ /**
+ * 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
* @access private
* @see OnListBuild
*/
function SetPagination(&$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
*/
function OnExport(&$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 */
$event = new kEvent($event->getPrefixSpecial().':OnDummy');
$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);
$url_params = Array(
't' => 'catalog/catalog',
'm_cat_id' => $this->Application->RecallVar('ImportCategory'),
'anchor' => 'tab-' . $event->Prefix,
);
$this->Application->EventManager->openerStackChange($url_params);
$event->SetRedirectParam('opener', 'u');
}
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
*/
function getCustomExportColumns(&$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
*/
- function restorePrimaryImage(&$event)
+ protected function restorePrimaryImage(&$event)
{
$object =& $event->getObject();
+ /* @var $object kCatDBItem */
$has_image_info = $object->GetDBField('ImageAlt') && ($object->GetDBField('ThumbnailImage') || $object->GetDBField('FullImage'));
- if (!$has_image_info) {
- return false;
+ if ( !$has_image_info ) {
+ return ;
}
$image_data = $object->getPrimaryImageData();
- $image =& $this->Application->recallObject('img', null, Array('skip_autoload' => true));
- if ($image_data) {
+ $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'));
}
$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') );
+ 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')) {
+ 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') );
+ $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()) {
+ if ( $image->isLoaded() ) {
$image->Update();
}
else {
$image->Create();
}
}
function isURL($path)
{
return preg_match('#(http|https)://(.*)#', $path);
}
/**
* Prepares item for import/export operations
*
* @param kEvent $event
*/
function OnNew(&$event)
{
parent::OnNew($event);
- if ($event->Special == 'import' || $event->Special == 'export') {
+ 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)
{
- $selected_ids = $this->Application->GetVar('selected_ids');
-
$dst_field = $this->Application->RecallVar('dst_field');
+ $selected_ids = $this->Application->GetVar('selected_ids');
- if ($dst_field == 'ItemCategory') {
+ 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']);
-// $this->Application->StoreVar($event->getPrefixSpecial().'_ForceNotValid', 1); // not to loose import/export values on form refresh
$url_params = Array (
$event->getPrefixSpecial() . '_id' => 0,
$event->getPrefixSpecial() . '_event' => 'OnExportBegin',
-// 'm_opener' => 's',
);
$this->Application->EventManager->openerStackChange($url_params);
}
$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) {
+ $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) );
+ $object =& $event->getObject(Array ('skip_autoload' => true));
+ /* @var $object kDBItem */
+
$object->SetFieldsFromHash($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));
+ $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->setRedirectParams(Array('pass' => 'all,'.$event->GetPrefixSpecial()), true);
+ $event->setRedirectParams(Array ('pass' => 'all,' . $event->getPrefixSpecial()), true);
$event->redirect = $this->Application->GetVar('cancel_template');
}
/* === 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 */
$user_id = $object->GetDBField($id_field);
$options = $object->GetFieldOptions($id_field);
- if (isset($options['options'][$user_id])) {
+
+ if ( isset($options['options'][$user_id]) ) {
$object->SetDBField($cached_field, $options['options'][$user_id]);
}
else {
$id_field = $this->Application->getUnitOption('u', 'IDField');
$table_name = $this->Application->getUnitOption('u', 'TableName');
$sql = 'SELECT Login
- FROM '.$table_name.'
- WHERE '.$id_field.' = '.$user_id;
+ FROM ' . $table_name . '
+ WHERE ' . $id_field . ' = ' . $user_id;
$object->SetDBField($cached_field, $this->Conn->GetOne($sql));
}
}
/**
- * Saves item beeing edited into temp table
+ * 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
*/
- function OnPreSave(&$event)
+ protected function OnPreSave(&$event)
{
parent::OnPreSave($event);
+
$use_pending_editing = $this->Application->getUnitOption($event->Prefix, 'UsePendingEditing');
- if ($event->status == kEvent::erSUCCESS && $use_pending_editing) {
+
+ if ( $event->status == kEvent::erSUCCESS && $use_pending_editing ) {
// decision: clone or not clone
$object =& $event->getObject();
- if ($object->GetID() == 0 || $object->GetDBField('OrgId') > 0) {
+ /* @var $object kCatDBItem */
+
+ if ( $object->GetID() == 0 || $object->GetDBField('OrgId') > 0 ) {
// new items or cloned items shouldn't be cloned again
- return true;
+ 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) {
+ 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');
- $cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array($object->GetID()), null, null, null, true);
- $ci_table = $this->Application->GetTempName(TABLE_PREFIX.'CategoryItems');
+ $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler');
+ /* @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];
+ 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';
+ $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');
+ $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];
+ $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]);
+ $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()));
+ $temp_handler->DeleteItems($event->Prefix, $event->Special, Array ($object->GetID()));
}
}
}
/**
- * Sets default expiration based on module setting
+ * Sets item's owner field
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnPreCreate(&$event)
+ protected function OnPreCreate(&$event)
{
parent::OnPreCreate($event);
- if ($event->status == kEvent::erSUCCESS) {
- $object =& $event->getObject();
- $owner_field = $this->getOwnerField($event->Prefix);
-
- $object->SetDBField($owner_field, $this->Application->RecallVar('user_id'));
+ 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'));
}
/**
* Occures before original item of item in pending editing got deleted (for hooking only)
*
* @param kEvent $event
*/
function OnBeforeDeleteOriginal(&$event)
{
}
/**
- * Occures before an item is cloneded
- * Id of ORIGINAL item is passed as event' 'id' param
- * Do not call object' Update method in this event, just set needed fields!
+ * 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
*/
- function OnBeforeClone(&$event)
+ protected function OnBeforeClone(&$event)
{
- if ($this->Application->GetVar('ResetCatBeforeClone')) {
+ parent::OnBeforeClone($event);
+
+ if ( $this->Application->GetVar('ResetCatBeforeClone') ) {
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$object->SetDBField('CategoryId', null);
}
}
/**
* Set status for new category item based on user permission in category
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
+ parent::OnBeforeItemCreate($event);
+
$object =& $event->getObject();
- /* @var $object kDBItem */
+ /* @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))) {
+ 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 ( $object->Validate() ) {
$object->SetDBField('ResourceId', $this->Application->NextResourceId());
}
- if (!$this->Application->isAdmin) {
+ 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 = $this->Application->getUnitOption($event->Prefix, 'UsePendingEditing');
if (!$use_pending_editing) {
return ;
}
$object =& $event->getObject();
- /* @var $object kDBItem */
+ /* @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
*/
function OnCreate(&$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
*/
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
*/
function OnUpdate(&$event)
{
$use_pending = $this->Application->getUnitOption($event->Prefix, 'UsePendingEditing');
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');
/* @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);
// 1a. delete record from CategoryItems (about cloned item) that was automatically created during call of Create method of kCatDBItem
$ci_table = $this->Application->getUnitOption('ci', 'TableName');
$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);
}
// 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);
}
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->isAdminUser || $event->status != kEvent::erSUCCESS) {
return ;
}
// prepare redirect template
$object =& $event->getObject();
$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 = $this->Application->getUnitOption($event->Prefix, 'PermItemPrefix');
$owner_field = $this->getOwnerField($event->Prefix);
$owner_id = $object->GetDBField($owner_field);
switch ($event->Name) {
case 'OnCreate':
$event_suffix = $is_active ? 'ADD' : 'ADD.PENDING';
$this->Application->EmailEventAdmin($perm_prefix.'.'.$event_suffix); // there are no ADD.PENDING event for admin :(
$this->Application->EmailEventUser($perm_prefix.'.'.$event_suffix, $owner_id);
break;
case 'OnUpdate':
$event_suffix = $is_active ? 'MODIFY' : 'MODIFY.PENDING';
$user_id = is_numeric( $object->GetDBField('ModifiedById') ) ? $object->GetDBField('ModifiedById') : $owner_id;
$this->Application->EmailEventAdmin($perm_prefix.'.'.$event_suffix); // there are no ADD.PENDING event for admin :(
$this->Application->EmailEventUser($perm_prefix.'.'.$event_suffix, $user_id);
break;
}
}
/**
- * Apply same processing to each item beeing selected in grid
+ * Apply same processing to each item being selected in grid
*
* @param kEvent $event
- * @access private
+ * @return void
+ * @access protected
*/
- function iterateItems(&$event)
+ protected function iterateItems(&$event)
{
- if ($event->Name != 'OnMassApprove' && $event->Name != 'OnMassDecline') {
- return parent::iterateItems($event);
+ if ( $event->Name != 'OnMassApprove' && $event->Name != 'OnMassDecline' ) {
+ parent::iterateItems($event);
}
- if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
+ if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) {
$event->status = kEvent::erFAIL;
- return;
+ return ;
}
- $object =& $event->getObject( Array('skip_autoload' => true) );
- /* @var $object kCatDBItem */
+ $object =& $event->getObject(Array ('skip_autoload' => true));
+ /* @var $object kCatDBItem */
$ids = $this->StoreSelectedIDs($event);
- if ($ids) {
+ if ( $ids ) {
foreach ($ids as $id) {
+ $ret = true;
$object->Load($id);
- switch ($event->Name) {
+ switch ( $event->Name ) {
case 'OnMassApprove':
$ret = $object->ApproveChanges();
break;
case 'OnMassDecline':
$ret = $object->DeclineChanges();
break;
}
- if (!$ret) {
+ if ( !$ret ) {
$event->status = kEvent::erFAIL;
$event->redirect = false;
break;
}
}
}
$this->clearSelectedIDs($event);
}
/**
* Deletes items & preserves clean env
*
* @param kEvent $event
*/
function OnDelete(&$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
*/
function checkItemStatus(&$event)
{
$object =& $event->getObject();
if (!$object->isLoaded()) {
$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
* @access private
* @see OnListBuild
*/
function SetSorting(&$event)
{
if (!$this->Application->isAdmin) {
$event->setEventParam('same_special', true);
}
parent::SetSorting($event);
}
/**
* Returns current per-page setting for list
*
* @param kEvent $event
* @return int
*/
function getPerPage(&$event)
{
if (!$this->Application->isAdmin) {
$event->setEventParam('same_special', true);
}
return parent::getPerPage($event);
}
function getOwnerField($prefix)
{
$owner_field = $this->Application->getUnitOption($prefix, 'OwnerField');
if (!$owner_field) {
$owner_field = 'CreatedById';
}
return $owner_field;
}
/**
* Creates virtual image fields for item
*
* @param kEvent $event
*/
function OnAfterConfigRead(&$event)
{
parent::OnAfterConfigRead($event);
if (defined('IS_INSTALL') && IS_INSTALL) {
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);
// add grids for advanced view (with primary category column)
$grids = $this->Application->getUnitOption($this->Prefix, 'Grids');
$process_grids = Array ('Default', 'Radio');
foreach ($process_grids as $process_grid) {
$grid_data = $grids[$process_grid];
$grid_data['Fields']['CachedNavbar'] = Array ('title' => 'la_col_Path', 'data_block' => 'grid_primary_category_td', 'filter_block' => 'grid_like_filter');
$grids[$process_grid . 'ShowAll'] = $grid_data;
}
$this->Application->setUnitOption($this->Prefix, 'Grids', $grids);
// 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 = $this->Application->getUnitOption($event->Prefix, 'VirtualFields');
$virtual_fields['CategoryId']['default'] = (int)$this->Application->GetVar('m_cat_id');
$virtual_fields['CategoryId']['options'] = $category_helper->getStructureTreeAsOptions();
$this->Application->setUnitOption($event->Prefix, 'VirtualFields', $virtual_fields);
}
function changeSortings(&$event)
{
$remove_sortings = Array ();
- if (!$this->Application->isAdmin) {
+ if ( !$this->Application->isAdmin ) {
// remove Pick sorting on Front-end, when not required
- $config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping');
+ $config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping', Array ());
- if (!isset($config_mapping['ForceEditorPick']) || !$this->Application->ConfigValue($config_mapping['ForceEditorPick'])) {
+ 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 ;
+ if ( !$remove_sortings ) {
+ return;
}
$list_sortings = $this->Application->getUnitOption($event->Prefix, 'ListSortings', Array ());
+ /* @var $list_sortings Array */
foreach ($list_sortings as $special => $sorting_fields) {
foreach ($remove_sortings as $sorting_field) {
unset($list_sortings[$special]['ForcedSorting'][$sorting_field]);
}
}
$this->Application->setUnitOption($event->Prefix, 'ListSortings', $list_sortings);
}
/**
* Returns file contents associated with item
*
* @param kEvent $event
*/
function OnDownloadFile(&$event)
{
$object =& $event->getObject();
- /* @var $object kDBItem */
+ /* @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 kDBItem */
+ /* @var $object kCatDBItem */
$object->Load( $this->Application->GetVar('id') );
echo $rating_helper->makeVote($object);
}
/**
* [HOOK] Allows to add cloned subitem to given prefix
*
* @param kEvent $event
*/
function OnCloneSubItem(&$event)
{
parent::OnCloneSubItem($event);
if ($event->MasterEvent->Prefix == 'fav') {
$clones = $this->Application->getUnitOption($event->MasterEvent->Prefix, 'Clones');
$subitem_prefix = $event->Prefix . '-' . $event->MasterEvent->Prefix;
$clones[$subitem_prefix]['ParentTableKey'] = 'ResourceId';
$clones[$subitem_prefix]['ForeignKey'] = 'ResourceId';
$this->Application->setUnitOption($event->MasterEvent->Prefix, 'Clones', $clones);
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/application.php
===================================================================
--- branches/5.2.x/core/kernel/application.php (revision 14627)
+++ branches/5.2.x/core/kernel/application.php (revision 14628)
@@ -1,2716 +1,2716 @@
<?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 incapsulates 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 {
/**
* Is true, when Init method was called already, prevents double initialization
*
* @var bool
*/
var $InitDone = false;
/**
* Holds internal NParser object
* @access private
* @var NParser
*/
var $Parser;
/**
* Holds parser output buffer
* @access private
* @var string
*/
var $HTML;
/**
* Prevents request from beeing proceeded twice in case if application init is called mere then one time
*
* @var bool
* @todo This is not good anyway (by Alex)
*/
var $RequestProcessed = false;
/**
* The main Factory used to create
* almost any class of kernel and
* modules
*
* @access private
* @var kFactory
*/
var $Factory;
/**
* Template names, that will be used instead of regular templates
*
* @var Array
*/
var $ReplacementTemplates = Array ();
/**
* Mod-Rewrite listeners used during url building and parsing
*
* @var Array
*/
var $RewriteListeners = Array ();
/**
* Reference to debugger
*
* @var Debugger
*/
var $Debugger = null;
/**
* Holds all phrases used
* in code and template
*
* @var PhrasesCache
*/
var $Phrases;
/**
* Modules table content, key - module name
*
* @var Array
*/
var $ModuleInfo = Array();
/**
* Holds DBConnection
*
* @var kDBConnection
*/
var $Conn = null;
/**
* Maintains list of user-defined error handlers
*
* @var Array
*/
var $errorHandlers = Array();
/**
* Maintains list of user-defined exception handlers
*
* @var Array
*/
var $exceptionHandlers = Array();
// performance needs:
/**
* Holds a refererence to httpquery
*
* @var kHttpQuery
*/
var $HttpQuery = null;
/**
* Holds a reference to UnitConfigReader
*
* @var kUnitConfigReader
*/
var $UnitConfigReader = null;
/**
* Holds a reference to Session
*
* @var Session
*/
var $Session = null;
/**
* Holds a ref to kEventManager
*
* @var kEventManager
*/
var $EventManager = null;
/**
* Holds a ref to kUrlManager
*
* @var kUrlManager
* @access protected
*/
protected $UrlManager = null;
/**
* Ref for TemplatesChache
*
* @var TemplatesCache
*/
var $TemplatesCache = null;
var $CompilationCache = array(); //used when compiling templates
var $CachedProcessors = array(); //used when running compiled templates
var $LambdaElements = 1; // for autonumbering unnamed RenderElements [any better place for this prop? KT]
/**
* Holds current NParser tag while parsing, can be used in error messages to display template file and line
*
* @var _BlockTag
*/
var $CurrentNTag = null;
/**
* Object of unit caching class
*
* @var kCacheManager
*/
var $cacheManager = null;
/**
* Tells, that administrator has authenticated in administrative console
* Should be used to manipulate data change OR data restrictions!
*
* @var bool
*/
var $isAdminUser = false;
/**
* Tells, that admin version of "index.php" was used, nothing more!
* Should be used to manipulate data display!
*
* @var bool
*/
var $isAdmin = false;
/**
* Instance of site domain object
*
* @var kDBItem
*/
var $siteDomain = null;
/**
* Prevent kApplication class to be created directly, only via Instance method
*
*/
protected function __construct()
{
}
/**
* 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
* @access public
* @return kApplication
*/
public static function &Instance()
{
static $instance = false;
if (!$instance) {
$class = defined('APPLICATION_CLASS') ? APPLICATION_CLASS : 'kApplication';
$instance = new $class();
$instance->Application =& $instance;
}
return $instance;
}
/**
* Initializes the Application
*
* @access public
* @see kHTTPQuery
* @see Session
* @see TemplatesCache
* @return bool Was Init actually made now or before
*/
public function Init()
{
if ( $this->InitDone ) {
return false;
}
$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:');
}
if ( !$this->isDebugMode() && !kUtil::constOn('DBG_ZEND_PRESENT') ) {
error_reporting(0);
ini_set('display_errors', 0);
}
if ( !kUtil::constOn('DBG_ZEND_PRESENT') ) {
$error_handler = set_error_handler(Array (&$this, 'handleError'));
if ( $error_handler ) {
// wrap around previous error handler, if any was set
$this->errorHandlers[] = $error_handler;
}
$exception_handler = set_exception_handler(Array (&$this, 'handleException'));
if ( $exception_handler ) {
// wrap around previous exception handler, if any was set
$this->exceptionHandlers[] = $exception_handler;
}
}
$this->Factory = new kFactory();
$this->registerDefaultClasses();
$vars = kUtil::parseConfig(true);
$db_class = isset($vars['Databases']) ? 'kDBLoadBalancer' : 'kDBConnection';
$this->Conn =& $this->Factory->makeClass($db_class, Array (SQL_TYPE, Array (&$this, 'handleSQLError')));
$this->Conn->setup($vars);
$this->cacheManager =& $this->makeClass('kCacheManager');
$this->cacheManager->InitCache();
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('Before UnitConfigReader');
}
$this->UnitConfigReader =& $this->makeClass('kUnitConfigReader');
$this->UnitConfigReader->scanModules(MODULES_PATH);
$this->registerModuleConstants();
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('After UnitConfigReader');
}
define('MOD_REWRITE', $this->ConfigValue('UseModRewrite') && !$this->isAdmin ? 1 : 0);
$this->HttpQuery =& $this->recallObject('HTTPQuery');
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');
}
if ( !$this->RecallVar('UserGroups') ) {
$user_groups = trim($this->Session->GetField('GroupList'), ',');
if ( !$user_groups ) {
$user_groups = $this->ConfigValue('User_GuestGroup');
}
$this->Session->SetField('GroupList', $user_groups);
$this->StoreVar('UserGroups', $user_groups, true); // true for optional
}
$this->UrlManager->LoadStructureTemplateMapping();
$this->HttpQuery->AfterInit();
$this->Session->ValidateExpired();
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 ) {
putenv('TZ=' . $site_timezone);
}
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('Loaded cache and phrases');
}
$this->ValidateLogin(); // must be called before AfterConfigRead, because current user should be available there
$this->UnitConfigReader->AfterConfigRead();
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->appendTimestamp('Processed AfterConfigRead');
}
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);
}
$language =& $this->recallObject('lang.current', null, Array ('live_table' => true));
/* @var $language LanguagesItem */
if ( preg_match('/utf-8/', $language->GetDBField('Charset')) ) {
setlocale(LC_ALL, 'en_US.UTF-8');
mb_internal_encoding('UTF-8');
}
if ( defined('DEBUG_MODE') && $this->isDebugMode() ) {
$this->Debugger->profileFinish('kernel4_startup');
}
$this->InitDone = true;
$this->HandleEvent(new kEvent('adm:OnStartup'));
return true;
}
function InitManagers()
{
if ($this->InitDone) {
throw new Exception('Duplicate call of ' . __METHOD__, E_USER_ERROR);
return ;
}
$this->UrlManager =& $this->makeClass('kUrlManager');
$this->EventManager =& $this->makeClass('EventManager');
$this->Phrases =& $this->makeClass('kPhraseCache');
$this->RegisterDefaultBuildEvents();
}
/**
* Returns module information. Searches module by requested field
*
* @param string $field
* @param mixed $value
* @param string field value to returns, if not specified, then return all fields
* @param string field to return
* @return Array
*/
function findModule($field, $value, $return_field = null)
{
$found = false;
foreach ($this->ModuleInfo as $module_name => $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')) {
$this->registerModuleConstants();
return ;
}
$modules_helper =& $this->makeClass('ModulesHelper');
/* @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();
}
/**
* Checks if passed language id if valid and sets it to primary otherwise
*
*/
function VerifyLanguageId()
{
$language_id = $this->GetVar('m_lang');
if (!$language_id) {
$language_id = 'default';
}
$this->SetVar('lang.current_id', $language_id);
$this->SetVar('m_lang', $language_id);
$lang_mode = $this->GetVar('lang_mode');
$this->SetVar('lang_mode', '');
$lang =& $this->recallObject('lang.current');
/* @var $lang kDBItem */
if (!$lang->isLoaded() || (!$this->isAdmin && !$lang->GetDBField('Enabled'))) {
if (!defined('IS_INSTALL')) {
$this->ApplicationDie('Unknown or disabled language');
}
}
$this->SetVar('lang_mode',$lang_mode);
}
/**
* Checks if passed theme id if valid and sets it to primary otherwise
*
*/
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);
}
function GetFrontThemePath($force=0)
{
static $path = null;
if ( !$force && isset($path) ) {
return $path;
}
$theme_id = $this->GetVar('m_theme');
if ( !$theme_id ) {
// $theme_id = $this->GetDefaultThemeId(1); //1 to force front-end mode!
$theme_id = 'default';
}
$this->SetVar('m_theme', $theme_id);
$this->SetVar('theme.current_id', $theme_id); // KOSTJA: this is to fool theme' getPassedId
$theme =& $this->recallObject('theme.current');
/* @var $theme ThemeItem */
if ( !$theme->isLoaded() || !$theme->GetDBField('Enabled') ) {
return false;
}
// assign & then return, since it's static variable
$path = '/themes/' . $theme->GetDBField('Name');
return $path;
}
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
$table = $this->getUnitOption('lang', 'TableName');
$id_field = $this->getUnitOption('lang', 'IDField');
$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;
}
function GetDefaultThemeId($force_front=0)
{
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;
$sql = 'SELECT ' . $this->getUnitOption('theme', 'IDField') . '
FROM ' . $this->getUnitOption('theme', 'TableName') . '
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
*/
function GetPrimaryCurrency()
{
$cache_key = 'primary_currency[%CurrSerial%][%SiteDomainSerial%]:' . $this->siteDomainField('DomainId');
$currency_iso = $this->getCache($cache_key);
if ($currency_iso === false) {
if ($this->isModuleEnabled('In-Commerce')) {
$this->Conn->nextQueryCachable = true;
$currency_id = $this->siteDomainField('PrimaryCurrencyId');
$sql = 'SELECT ISO
FROM ' . $this->getUnitOption('curr', 'TableName') . '
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
*/
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');
/* @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 ItemController, GridController and LoginController
*
* Called automatically while initializing Application
* @access private
* @return void
*/
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', 'kiCacheable');
$this->registerClass('kEventManager', KERNEL_PATH . '/event_manager.php', 'EventManager', 'kiCacheable');
$this->registerClass('kHookManager', KERNEL_PATH . '/managers/hook_manager.php', null, 'kiCacheable');
$this->registerClass('kAgentManager', KERNEL_PATH . '/managers/agent_manager.php', null, 'kiCacheable');
$this->registerClass('kRequestManager', KERNEL_PATH . '/managers/request_manager.php');
$this->registerClass('kUrlManager', KERNEL_PATH . '/managers/url_manager.php');
$this->registerClass('kCacheManager', KERNEL_PATH . '/managers/cache_manager.php', null, 'kiCacheable');
$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('kUnitConfigReader', KERNEL_PATH . '/utility/unit_config_reader.php');
// Params class descendants
$this->registerClass('kArray', KERNEL_PATH . '/utility/params.php');
$this->registerClass('Params', KERNEL_PATH . '/utility/params.php');
$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', 'Params');
// session
$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', 'kTagProcessor');
$this->registerClass('kDBTagProcessor', KERNEL_PATH . '/db/db_tag_processor.php', null, 'kTagProcessor');
$this->registerClass('kCatDBTagProcessor', KERNEL_PATH . '/db/cat_tag_processor.php', null, 'kDBTagProcessor');
$this->registerClass('NParser', KERNEL_PATH . '/nparser/nparser.php');
$this->registerClass('TemplatesCache', KERNEL_PATH . '/nparser/template_cache.php', null, Array ('kHelper', 'kDBTagProcessor'));
// database
$this->registerClass('kDBConnection', 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', null, 'kDBItem');
$this->registerClass('kDBList', KERNEL_PATH . '/db/dblist.php');
$this->registerClass('kCatDBList', KERNEL_PATH . '/db/cat_dblist.php', null, 'kDBList');
$this->registerClass('kDBEventHandler', KERNEL_PATH . '/db/db_event_handler.php');
$this->registerClass('kCatDBEventHandler', KERNEL_PATH . '/db/cat_event_handler.php', null, 'kDBEventHandler');
// email sending
$this->registerClass('kEmailSendingHelper', KERNEL_PATH . '/utility/email_send.php', 'EmailSender', 'kHelper');
$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 . '/../units/helpers/modules_helper.php', 'ModulesHelper');
}
function RegisterDefaultBuildEvents()
{
$this->EventManager->registerBuildEvent('kTempTablesHandler', 'OnTempHandlerBuild');
}
/**
* 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)
{
return $this->cacheManager->getCategoryCache($category_id, $name);
}
/**
* Returns caching type (none, memory, temporary)
*
* @return int
* @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
* @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);
}
/**
* Adds new value to cache $cache_name and identified by key $key
*
* @param int $key key name to add to cache
* @param mixed $value value of chached record
* @param int $expiration when value expires (0 - doesn't expire)
* @access public
*/
public function setCache($key, $value, $expiration = 0)
{
return $this->cacheManager->setCache($key, $value, $expiration);
}
/**
* Sets rebuilding mode for given cache
*
* @param string $name
* @param int $mode
* @param int $max_rebuilding_time
*/
public function rebuildCache($name, $mode = null, $max_rebuilding_time = 0)
{
$this->cacheManager->rebuildCache($name, $mode, $max_rebuilding_time);
}
/**
* Deletes key from cache
*
* @param string $key
* @access public
*/
public function deleteCache($key)
{
$this->cacheManager->deleteCache($key);
}
/**
* Reset's all memory cache at once
*
* @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
* @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
*/
public function rebuildDBCache($name, $mode = null, $max_rebuilding_time = 0)
{
$this->cacheManager->rebuildDBCache($name, $mode, $max_rebuilding_time);
}
/**
* Deletes key from database cache
*
* @param string $name
* @access public
*/
public function deleteDBCache($name)
{
$this->cacheManager->deleteDBCache($name);
}
/**
* Registers each module specific constants if any found
*
*/
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_name => $module_info) {
- $contants_file = FULL_PATH . '/' . $module_info['Path'] . 'constants.php';
+ $constants_file = FULL_PATH . '/' . $module_info['Path'] . 'constants.php';
- if (file_exists($contants_file)) {
- kUtil::includeOnce($contants_file);
+ if (file_exists($constants_file)) {
+ kUtil::includeOnce($constants_file);
}
}
return true;
}
function ProcessRequest()
{
if ( defined('DEBUG_MODE') && $this->isDebugMode() && kUtil::constOn('DBG_SHOW_HTTPQUERY') ) {
$this->Debugger->appendHTML('HTTPQuery:');
$this->Debugger->dumpVars($this->HttpQuery->_Params);
}
$this->EventManager->ProcessRequest();
$this->EventManager->runAgents(reBEFORE);
$this->RequestProcessed = true;
}
/**
* 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.
* @access public
* @return void
*/
function Run()
{
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')) {
// viewing front-end through admin's frame
$admin_session =& $this->recallObject('Session.admin');
/* @var $admin_session Session */
$user = (int)$admin_session->RecallVar('user_id'); // in case, when no valid admin session found
$perm_helper =& $this->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
if ($perm_helper->CheckUserPermission($user, 'CATEGORY.MODIFY', 0, $this->getBaseCategory())) {
// user can edit cms blocks
$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();
if (!$this->RequestProcessed) $this->ProcessRequest();
$this->InitParser();
$t = $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:');
}
}
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.
* @access public
* @return void
*/
function Done()
{
$this->HandleEvent( new kEvent('adm:OnBeforeShutdown') );
$debug_mode = defined('DEBUG_MODE') && $this->isDebugMode();
if ($debug_mode && kUtil::constOn('DBG_PROFILE_MEMORY')) {
$this->Debugger->appendMemoryUsage('Application before Done:');
}
if ($debug_mode) {
$this->EventManager->runAgents(reAFTER);
$this->Session->SaveData();
if (kUtil::constOn('DBG_CACHE')) {
$this->cacheManager->printStatistics();
}
$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;
}
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 {
echo $this->HTML;
}
$this->cacheManager->UpdateApplicationCache();
flush();
if (!$debug_mode) {
$this->EventManager->runAgents(reAFTER);
$this->Session->SaveData();
}
if (defined('DBG_CAPTURE_STATISTICS') && DBG_CAPTURE_STATISTICS && !$this->isAdmin) {
$this->_storeStatistics();
}
}
/**
* Stores script execution statistics to database
*
*/
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'] = adodb_mktime();
$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'] = adodb_mktime();
$this->Conn->doInsert($data, TABLE_PREFIX . 'StatisticsCapture');
}
}
/**
* Calculates average time for statistics
*
* @param Array $data
* @param string $field_prefix
* @param float $current_value
*/
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;
}
}
function logSlowQuery($slow_sql, $time)
{
$query_crc = 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'] = adodb_mktime();
$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'] = adodb_mktime();
$this->Conn->doInsert($data, TABLE_PREFIX . 'SlowSqlCapture');
}
}
/**
* Checks if output compression options is available
*
* @return string
*/
function UseOutputCompression()
{
if (kUtil::constOn('IS_INSTALL') || kUtil::constOn('DBG_ZEND_PRESENT') || kUtil::constOn('SKIP_OUT_COMPRESSION')) {
return false;
}
return $this->ConfigValue('UseOutputCompression') && function_exists('gzencode') && strstr($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip');
}
// Facade
/**
* Returns current session id (SID)
* @access public
* @return int
*/
function GetSID()
{
$session =& $this->recallObject('Session');
/* @var $session Session */
return $session->GetID();
}
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;
}
/**
* Returns ALL variables passed to the script as GET/POST/COOKIE
*
* @access public
* @return array
*/
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>
*
* This method is formerly known as $this->Session->SetProperty.
* @param string $var Variable name to set
* @param mixed $val Variable value
* @access public
* @return void
*/
public function SetVar($var,$val)
{
$this->HttpQuery->Set($var, $val);
}
/**
* Deletes kHTTPQuery variable
*
* @param string $var
* @todo think about method name
*/
function DeleteVar($var)
{
return $this->HttpQuery->Remove($var);
}
/**
* Deletes Session variable
*
* @param string $var
*/
function RemoveVar($var)
{
return $this->Session->RemoveVar($var);
}
function RemovePersistentVar($var)
{
return $this->Session->RemovePersistentVar($var);
}
/**
* Restores Session variable to it's db version
*
* @param string $var
*/
function RestoreVar($var)
{
return $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.
*
* @see SimpleSession
* @access public
* @param string $var Variable name
* @param mixed $default Default value to return if no $var variable found in session
* @return mixed
*/
function RecallVar($var,$default=false)
{
return $this->Session->RecallVar($var,$default);
}
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.
* @see RecallVar
* @access public
* @param string $var Variable name
* @param mixed $val Variable value
*/
function StoreVar($var, $val, $optional = false)
{
$session =& $this->recallObject('Session');
$this->Session->StoreVar($var, $val, $optional);
}
function StorePersistentVar($var, $val, $optional = false)
{
$this->Session->StorePersistentVar($var, $val, $optional);
}
function StoreVarDefault($var, $val, $optional=false)
{
$session =& $this->recallObject('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
* @access public
* @param string $var HTTP Query (GPC) variable name
* @param mixed $ses_var Session variable name
* @param mixed $default Default variable value
*/
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
*
* @see LinkVar
* @access public
* @param string $var HTTP Query (GPC) variable name
* @param mixed $ses_var Session variable name
* @param mixed $default Default variable value
* @return mixed
*/
function GetLinkedVar($var, $ses_var = null, $default = '')
{
$this->LinkVar($var, $ses_var, $default);
return $this->GetVar($var);
}
function ProcessParsedTag($prefix, $tag, $params)
{
$processor = $this->Parser->GetProcessor($prefix);
/* @var $processor kDBTagProcessor */
return $processor->ProcessParsedTag($tag, $params, $prefix);
}
/**
* Return ADODB Connection object
*
* Returns ADODB Connection object already connected to the project database, configurable in config.php
* @access public
* @return kDBConnection
*/
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 Array $pass_params Forces to pass current parser params to this block/template. Use with cauntion, because you can accidently pass "block_no_data" parameter.
* @param bool $as_template
* @return string
*/
function ParseBlock($params, $pass_params = 0, $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
*/
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
*/
function IncludeTemplate($params)
{
return $this->Parser->IncludeTemplate($params, isset($params['is_silent']) ? 1 : 0);
}
/**
* Return href for template
*
* @access public
* @param string $t Template path
* @var string $prefix index.php prefix - could be blank, 'admin'
*/
public function HREF($t, $prefix = '', $params = null, $index_file = null)
{
return $this->UrlManager->HREF($t, $prefix, $params, $index_file);
}
function getPhysicalTemplate($template)
{
return $this->UrlManager->getPhysicalTemplate($template);
}
/**
* Returns variables with values that should be passed throught with this link + variable list
*
* @param Array $params
* @return Array
*/
function getPassThroughVariables(&$params)
{
return $this->UrlManager->getPassThroughVariables($params);
}
function BuildEnv($t, $params, $pass = 'all', $pass_events = false, $env_var = true)
{
return $this->UrlManager->BuildEnv($t, $params, $pass, $pass_events, $env_var);
}
function BaseURL($prefix = '', $ssl = null, $add_port = true)
{
if ($ssl === null) {
// stay on same encryption level
return PROTOCOL . SERVER_NAME . ($add_port && defined('PORT') ? ':' . PORT : '') . rtrim(BASE_PATH, '/') . $prefix . '/';
}
if ($ssl) {
// going from http:// to https://
$base_url = $this->isAdmin ? $this->ConfigValue('AdminSSL_URL') : false;
if (!$base_url) {
$ssl_url = $this->siteDomainField('SSLUrl');
$base_url = $ssl_url !== false ? $ssl_url : $this->ConfigValue('SSL_URL');
}
return rtrim($base_url, '/') . $prefix . '/';
}
// going from https:// to http://
$domain = $this->siteDomainField('DomainName');
if ($domain === false) {
$domain = DOMAIN;
}
return 'http://' . $domain . ($add_port && defined('PORT') ? ':' . PORT : '') . rtrim($this->ConfigValue('Site_Path'), '/') . $prefix . '/';
}
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';
}
$params['__URLENCODE__'] = 1;
$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 true;
}
else {
if ($this->GetVar('ajax') == 'yes' && $t != $this->GetVar('t')) {
// redirection to other then current template during ajax request
kUtil::safeDefine('DBG_SKIP_REPORTING', 1);
echo '#redirect#' . $location;
}
elseif (headers_sent() != '') {
// some output occured -> 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 */
$this->HandleEvent( new kEvent('adm:OnBeforeShutdown') );
$session->SaveData();
ob_end_flush();
exit;
}
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
*/
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.
*
* @access private
*/
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');
}
if ( ($user_id != USER_GUEST) && defined('DBG_REQUREST_LOG') && DBG_REQUREST_LOG ) {
$this->HttpQuery->writeRequestLog(DBG_REQUREST_LOG);
}
if ( $user_id != USER_GUEST ) {
// normal users + root
$this->LoadPersistentVars();
}
}
/**
* Loads current user persistent session data
*
*/
function LoadPersistentVars()
{
$this->Session->LoadPersistentVars();
}
/**
* Returns configuration option value by name
*
* @param string $name
* @return string
*/
function ConfigValue($name)
{
return $this->cacheManager->ConfigValue($name);
}
function SetConfigValue($name, $value)
{
return $this->cacheManager->SetConfigValue($name, $value);
}
/**
* Allows to process any type of event
*
* @param kEvent $event
* @param Array $params
* @param Array $specific_params
* @access public
* @author Alex
*/
function HandleEvent(&$event, $params = null, $specific_params = null)
{
if ( isset($params) ) {
$event = new kEvent($params, $specific_params);
}
$this->EventManager->HandleEvent($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
* @param Array $dependecies List of classes required for this class functioning
* @access public
* @author Alex
*/
function registerClass($real_class, $file, $pseudo_class = null, $dependecies = Array() )
{
$this->Factory->registerClass($real_class, $file, $pseudo_class, $dependecies);
}
/**
* 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
*/
function unregisterClass($real_class, $pseudo_class = null)
{
$this->Factory->unregisterClass($real_class, $pseudo_class);
}
/**
* Add $class_name to required classes list for $depended_class class.
* All required class files are included before $depended_class file is included
*
* @param string $depended_class
* @param string $class_name
* @author Alex
*/
function registerDependency($depended_class, $class_name)
{
$this->Factory->registerDependency($depended_class, $class_name);
}
/**
* Add new agent
*
* @param string $short_name name to be used to store last maintenace run info
* @param string $event_name
* @param int $run_interval run interval in seconds
* @param int $type before or after agent
* @param int $status
* @access public
*/
public function registerAgent($short_name, $event_name, $run_interval, $type = reBEFORE, $status = STATUS_ACTIVE)
{
$this->EventManager->registerAgent($short_name, $event_name, $run_interval, $type, $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
* @author Kostja
*/
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 ())
{
$result =& $this->Factory->getObject($name, $pseudo_class, $event_params, $arguments);
return $result;
}
/**
* Returns tag processor for prefix specified
*
* @param string $prefix
* @return kDBTagProcessor
*/
function &recallTagProcessor($prefix)
{
$this->InitParser(); // because kDBTagProcesor is in NParser dependencies
$result =& $this->recallObject($prefix . '_TagProcessor');
return $result;
}
/**
* Checks if object with prefix passes was already created in factory
*
* @param string $name object presudo_class, prefix
* @return bool
* @author Kostja
*/
function hasObject($name)
{
return isset($this->Factory->Storage[$name]);
}
/**
* Removes object from storage by given name
*
* @param string $name Object's name in the Storage
* @author Kostja
*/
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 ())
{
$result =& $this->Factory->makeClass($pseudo_class, $arguments);
return $result;
}
/**
* 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 $ssl Force ssl link to be build
* @return bool
*/
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;
}
/**
* Reads unit (specified by $prefix)
* option specified by $option
*
* @param string $prefix
* @param string $option
* @param mixed $default
* @return string
* @access public
*/
public function getUnitOption($prefix, $option, $default = false)
{
return $this->UnitConfigReader->getUnitOption($prefix, $option, $default);
}
/**
* Set's new unit option value
*
* @param string $prefix
* @param string $name
* @param string $value
* @access public
*/
public function setUnitOption($prefix, $option, $value)
{
return $this->UnitConfigReader->setUnitOption($prefix,$option,$value);
}
/**
* Read all unit with $prefix options
*
* @param string $prefix
* @return Array
* @access public
*/
public function getUnitOptions($prefix)
{
return $this->UnitConfigReader->getUnitOptions($prefix);
}
/**
* Returns true if config exists and is allowed for reading
*
* @param string $prefix
* @return bool
*/
public function prefixRegistred($prefix)
{
return $this->UnitConfigReader->prefixRegistred($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
* @access 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 private
* @author Alex
*/
function handleSQLError($code, $msg, $sql)
{
if ( isset($this->Debugger) ) {
$long_error_msg = '<span class="debug_error">' . $msg . ' (' . $code . ')</span><br/><a href="javascript:$Debugger.SetClipboard(\'' . htmlspecialchars($sql) . '\');"><strong>SQL</strong></a>: ' . $this->Debugger->formatSQL($sql);
$long_id = $this->Debugger->mapLongError($long_error_msg);
$error_msg = mb_substr($msg . ' (' . $code . ') [' . $sql . ']', 0, 1000) . ' #' . $long_id;
if ( kUtil::constOn('DBG_SQL_FAILURE') && !defined('IS_INSTALL') ) {
throw new Exception($error_msg);
}
else {
$this->Debugger->appendTrace();
}
}
else {
// when not debug mode, then fatal database query won't break anything
$error_msg = '<strong>SQL Error</strong> in sql: ' . $sql . ', code <strong>' . $code . '</strong> (' . $msg . ')';
}
trigger_error($error_msg, E_USER_WARNING);
return true;
}
/**
* Default error handler
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @param Array $errcontext
* @return bool
* @access public
*/
public function handleError($errno, $errstr, $errfile = null, $errline = null, $errcontext = Array ())
{
$this->errorLogSilent($errno, $errstr, $errfile, $errline);
$debug_mode = defined('DEBUG_MODE') && DEBUG_MODE;
$skip_reporting = defined('DBG_SKIP_REPORTING') && DBG_SKIP_REPORTING;
if ( !$this->errorHandlers || ($debug_mode && $skip_reporting) ) {
// when debugger absent OR it's present, but we actually can't see it's error report (e.g. during ajax request)
if ( $errno == E_USER_ERROR ) {
$this->errorDisplayFatal('<strong>Fatal Error: </strong>' . "{$errstr} in {$errfile} on line {$errline}");
}
if ( !$this->errorHandlers ) {
return true;
}
}
$res = false;
foreach ($this->errorHandlers as $handler) {
if ( is_array($handler) ) {
$object =& $handler[0];
$method = $handler[1];
$res = $object->$method($errno, $errstr, $errfile, $errline, $errcontext);
}
else {
$res = $handler($errno, $errstr, $errfile, $errline, $errcontext);
}
}
return $res;
}
/**
* Handles exception
*
* @param Exception $exception
* @return bool
* @access public
*/
public function handleException($exception)
{
// transform exception to regular error (no need to rewrite existing error handlers)
$errno = $exception->getCode();
$errstr = $exception->getMessage();
$errfile = $exception->getFile();
$errline = $exception->getLine();
$this->errorLogSilent($errno, $errstr, $errfile, $errline);
$debug_mode = defined('DEBUG_MODE') && DEBUG_MODE;
$skip_reporting = defined('DBG_SKIP_REPORTING') && DBG_SKIP_REPORTING;
if ( !$this->exceptionHandlers || ($debug_mode && $skip_reporting) ) {
// when debugger absent OR it's present, but we actually can't see it's error report (e.g. during ajax request)
$this->errorDisplayFatal('<strong>' . get_class($exception) . ': </strong>' . "{$errstr} in {$errfile} on line {$errline}");
if ( !$this->exceptionHandlers ) {
return true;
}
}
$res = false;
foreach ($this->exceptionHandlers as $handler) {
if ( is_array($handler) ) {
$object =& $handler[0];
$method = $handler[1];
$res = $object->$method($exception);
}
else {
$res = $handler($exception);
}
}
return $res;
}
/**
* Silently saves each given error message to "silent_log.txt" file, when silent log mode is enabled
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
* @access protected
*/
protected function errorLogSilent($errno, $errstr = '', $errfile = '', $errline = null)
{
if ( !defined('SILENT_LOG') || !SILENT_LOG ) {
return;
}
if ( !(defined('DBG_IGNORE_STRICT_ERRORS') && DBG_IGNORE_STRICT_ERRORS && defined('E_STRICT') && ($errno == E_STRICT)) ) {
$time = adodb_date('d/m/Y H:i:s');
$fp = fopen((defined('RESTRICTED') ? RESTRICTED : FULL_PATH) . '/silent_log.txt', 'a');
fwrite($fp, '[' . $time . '] #' . $errno . ': ' . strip_tags($errstr) . ' in [' . $errfile . '] on line ' . $errline . "\n");
fclose($fp);
}
}
/**
* Displays div with given error message
*
* @param string $msg
* @return void
* @access protected
*/
protected function errorDisplayFatal($msg)
{
$margin = $this->isAdmin ? '8px' : 'auto';
echo '<div style="background-color: #FEFFBF; margin: ' . $margin . '; padding: 10px; border: 2px solid red; text-align: center">' . $msg . '</div>';
exit;
}
/**
* Prints trace, when debug mode is not available
*
* @param bool $return_result
* @param int $skip_levels
* @return string
* @access public
*/
public function printTrace($return_result = false, $skip_levels = 1)
{
$ret = Array ();
$trace = debug_backtrace(false);
for ($i = 0; $i < $skip_levels; $i++) {
array_shift($trace);
}
foreach ($trace as $level => $trace_info) {
if ( isset($trace_info['class']) ) {
$object = $trace_info['class'];
}
elseif ( isset($trace_info['object']) ) {
$object = get_class($trace_info['object']);
}
else {
$object = '';
}
$args = '';
$type = isset($trace_info['type']) ? $trace_info['type'] : '';
if ( isset($trace_info['args']) ) {
foreach ($trace_info['args'] as $argument) {
if ( is_object($argument) ) {
$args .= get_class($argument) . ' instance, ';
}
else {
$args .= is_array($argument) ? 'Array' : substr($argument, 0, 10) . ' ..., ';
}
}
$args = substr($args, 0, -2);
}
$ret[] = '#' . $level . ' ' . $object . $type . $trace_info['function'] . '(' . $args . ') called at [' . $trace_info['file'] . ':' . $trace_info['line'] . ']';
}
if ( $return_result ) {
return implode("\n", $ret);
}
echo implode("\n", $ret);
return '';
}
/**
* Returns & blocks next ResourceId available in system
*
* @return int
* @access public
* @author Alex
*/
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 subtable prefix passes
* OR prefix, that has been found in REQUEST and some how is parent of passed subtable prefix
*
* @param string $current_prefix
* @param string $real_top if set to true will return real topmost prefix, regardless of its id is passed or not
* @return string
* @access public
* @author Kostja / Alex
*/
function GetTopmostPrefix($current_prefix, $real_top = false)
{
// 1. get genealogical tree of $current_prefix
$prefixes = Array ($current_prefix);
while ( $parent_prefix = $this->getUnitOption($current_prefix, 'ParentPrefix') ) {
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_event_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
*/
function &EmailEventAdmin($email_event_name, $to_user_id = null, $send_params = Array ())
{
$event =& $this->EmailEvent($email_event_name, EmailEvent::EVENT_TYPE_ADMIN, $to_user_id, $send_params);
return $event;
}
/**
* Triggers email event of type User
*
* @param string $email_event_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
*/
function &EmailEventUser($email_event_name, $to_user_id = null, $send_params = Array ())
{
$event =& $this->EmailEvent($email_event_name, EmailEvent::EVENT_TYPE_FRONTEND, $to_user_id, $send_params);
return $event;
}
/**
* Triggers general email event
*
* @param string $email_event_name
* @param int $email_event_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
*/
function &EmailEvent($email_event_name, $email_event_type, $to_user_id = null, $send_params = Array ())
{
$params = Array (
'EmailEventName' => $email_event_name,
'EmailEventToUserId' => $to_user_id,
'EmailEventType' => $email_event_type,
'DirectSendParams' => $send_params,
);
if (array_key_exists('use_special', $send_params)) {
$event_str = 'emailevents.' . $send_params['use_special'] . ':OnEmailEvent';
}
else {
$event_str = 'emailevents:OnEmailEvent';
}
$this->HandleEvent($event, $event_str, $params);
return $event;
}
/**
* Allows to check if user in this session is logged in or not
*
* @return bool
*/
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
*/
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);
}
/**
* Set's any field of current visit
*
* @param string $field
* @param mixed $value
*/
function setVisitField($field, $value)
{
if ($this->isAdmin || !$this->ConfigValue('UseVisitorTracking')) {
// admin logins are not registred 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
*/
function isInstalled()
{
return $this->InitDone && (count($this->ModuleInfo) > 0);
}
/**
* Allows to determine if module is installed & enabled
*
* @param string $module_name
* @return bool
*/
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 mixed
*/
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
*/
function GetTempName($table, $wid = '')
{
return $this->GetTempTablePrefix($wid) . $table;
}
function GetTempTablePrefix($wid = '')
{
if (preg_match('/prefix:(.*)/', $wid, $regs)) {
$wid = $this->GetTopmostWid($regs[1]);
}
return TABLE_PREFIX . 'ses_' . $this->GetSID() . ($wid ? '_' . $wid : '') . '_edit_';
}
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
* @return bool
*/
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
*/
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;
}
}
function CheckProcessors($processors)
{
foreach ($processors as $a_processor)
{
if (!isset($this->CachedProcessors[$a_processor])) {
$this->CachedProcessors[$a_processor] =& $this->recallObject($a_processor.'_TagProcessor');
}
}
}
function ApplicationDie($message = '')
{
$message = ob_get_clean().$message;
if ($this->isDebugMode()) {
$message .= $this->Debugger->printReport(true);
}
echo $this->UseOutputCompression() ? gzencode($message, DBG_COMPRESSION_LEVEL) : $message;
exit;
}
/* moved from MyApplication */
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 . 'UserGroup
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 agents supported)
*
* @return bool
*/
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 presense in database
*
* @param string $table_name
* @return bool
*/
function TableFound($table_name)
{
return $this->Conn->TableFound($table_name);
}
/**
* 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 parmeters)
* @param bool $multiple_results
* @return mixed
*/
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)
{
$lang =& $this->recallObject('lang.current');
/* @var $lang LanguagesItem */
header('Content-type: text/xml; charset=' . $lang->GetDBField('Charset'));
return $xml_version ? '<?xml version="' . $xml_version . '" encoding="' . $lang->GetDBField('Charset') . '"?>' : '';
}
/**
* Returns category tree
*
* @param int $category_id
* @return Array
*/
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
*/
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'];
}
function DeleteUnitCache($include_sections = false)
{
$this->cacheManager->DeleteUnitCache($include_sections);
}
/**
* Sets data from cache to object
*
* @param Array $data
* @access public
*/
public function 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 (
'Application.ReplacementTemplates' => $this->ReplacementTemplates,
'Application.RewriteListeners' => $this->RewriteListeners,
'Application.ModuleInfo' => $this->ModuleInfo,
);
}
public function delayUnitProcessing($method, $params)
{
$this->cacheManager->delayUnitProcessing($method, $params);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/managers/url_manager.php
===================================================================
--- branches/5.2.x/core/kernel/managers/url_manager.php (revision 14627)
+++ branches/5.2.x/core/kernel/managers/url_manager.php (revision 14628)
@@ -1,636 +1,645 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2010 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 kUrlManager extends kBase {
/**
* Physical template name mapping to their template names based on structure
*
* @var Array
*/
protected $structureTemplateMapping = Array ();
/**
* Return href for template
*
* @access public
* @param string $t Template path
* @var string $prefix index.php prefix - could be blank, 'admin'
*/
public function HREF($t, $prefix = '', $params = null, $index_file = null)
{
static $theme_id = null;
if (!isset($theme_id)) {
$theme_id = $this->Application->GetVar('m_theme');
}
if (!$t) {
// when template not specified, use current
$t = $this->Application->GetVar('t');
}
$t = preg_replace('/^Content\//i', '', $t);
if (substr($t, -4) == '.tpl') {
// cut template extension (deprecated link format)
$t = substr($t, 0, strlen($t) - 4);
}
if (substr($t, 0, 3) == 'id:') {
// link to structure page using it's id
$params['m_cat_id'] = substr($t, 3);
$t = $this->structureTemplateMapping[$t];
}
if (array_key_exists('use_section', $params)) {
$use_section = $params['use_section'];
unset($params['use_section']);
}
if (isset($use_section) && $use_section && array_key_exists($t . ':' . $theme_id, $this->structureTemplateMapping)) {
// structure template corresponding to given physical template
$t = $this->structureTemplateMapping[$t . ':' . $theme_id];
unset($params['use_section']);
}
if (preg_match('/external:(.*)/', $t, $rets)) {
// external url
return $rets[1];
}
if ($this->Application->isAdmin && $prefix == '') $prefix = ADMIN_DIRECTORY;
if ($this->Application->isAdmin && $prefix == '_FRONT_END_') $prefix = '';
$index_file = $this->getIndexFile($prefix, $index_file, $params);
if (isset($params['_auto_prefix_'])) {
unset($params['_auto_prefix_']); // this is parser-related param, do not need to pass it here
}
$ssl = isset($params['__SSL__']) ? $params['__SSL__'] : null;
if ($ssl !== null) {
$session =& $this->Application->recallObject('Session');
/* @var $session Session */
$target_url = rtrim($this->Application->BaseURL('', $ssl, false), '/');
$cookie_url = trim($session->CookieDomain . $session->CookiePath, '/.');
// set session to GET_ONLY, to pass sid only if sid is REAL AND session is set
if (!preg_match('#' . preg_quote($cookie_url) . '#', $target_url) && $session->SessionSet) {
// when SSL<->NON-SSL redirect to different domain pass SID in url
$session->SetMode(Session::smGET_ONLY);
}
}
if (isset($params['opener']) && $params['opener'] == 'u') {
$wid = $this->Application->GetVar('m_wid');
$stack_name = rtrim('opener_stack_'.$wid, '_');
$opener_stack = $this->Application->RecallVar($stack_name);
if ($opener_stack && $opener_stack != serialize(Array())) {
$opener_stack = unserialize($opener_stack);
list($index_file, $env) = explode('|', $opener_stack[count($opener_stack) - 1]);
$ret = $this->Application->BaseURL($prefix, $ssl).$index_file.'?'.ENV_VAR_NAME.'='.$env;
if ( getArrayValue($params,'escape') ) $ret = addslashes($ret);
if (isset($params['m_opener']) && $params['m_opener'] == 'u') {
array_pop($opener_stack);
if (!$opener_stack) {
$this->Application->RemoveVar($stack_name);
// remove popups last templates, because popup is closing now
$this->Application->RemoveVar('last_template_'.$wid);
$this->Application->RemoveVar('last_template_popup_'.$wid);
// don't save popups last templates again :)
$this->Application->SetVar('skip_last_template', 1);
}
else {
$this->Application->StoreVar($stack_name, serialize($opener_stack));
}
/*// store window relations
$window_relations = $this->Application->RecallVar('window_relations');
$window_relations = $window_relations ? unserialize($window_relations) : Array ();
if (array_key_exists($wid, $window_relations)) {
unset($window_relations[$wid]);
$this->Application->StoreVar('window_relations', serialize($window_relations));
}*/
}
return $ret;
}
else {
//define('DBG_REDIRECT', 1);
$t = $this->Application->GetVar('t');
}
}
$pass = isset($params['pass']) ? $params['pass'] : '';
// pass events with url
$pass_events = false;
if( isset($params['pass_events']) )
{
$pass_events = $params['pass_events'];
unset($params['pass_events']);
}
$map_link = '';
if( isset($params['anchor']) )
{
$map_link = '#'.$params['anchor'];
unset($params['anchor']);
}
if ( isset($params['no_amp']) )
{
$params['__URLENCODE__'] = $params['no_amp'];
unset($params['no_amp']);
}
$no_rewrite = false;
if( isset($params['__NO_REWRITE__']) )
{
$no_rewrite = true;
unset($params['__NO_REWRITE__']);
}
$force_rewrite = false;
if( isset($params['__MOD_REWRITE__']) )
{
$force_rewrite = true;
unset($params['__MOD_REWRITE__']);
}
$force_no_sid = false;
if( isset($params['__NO_SID__']) )
{
$force_no_sid = true;
unset($params['__NO_SID__']);
}
// append pass through variables to each link to be build
$params = array_merge($this->getPassThroughVariables($params), $params);
if ($force_rewrite || ($this->Application->RewriteURLs($ssl) && !$no_rewrite)) {
static $rewrite_listeners_done = false;
if (!$rewrite_listeners_done) {
$mod_rewrite_helper =& $this->Application->recallObject('ModRewriteHelper');
/* @var $mod_rewrite_helper kModRewriteHelper */
$mod_rewrite_helper->initRewriteListeners();
$rewrite_listeners_done = true;
}
$session =& $this->Application->recallObject('Session');
if ($session->NeedQueryString() && !$force_no_sid) {
$params['sid'] = $this->Application->GetSID();
}
$url = $this->BuildEnv_NEW($t, $params, $pass, $pass_events);
$ret = $this->Application->BaseURL($prefix, $ssl).$url.$map_link;
}
else {
unset($params['pass_category']); // we don't need to pass it when mod_rewrite is off
$env = $this->BuildEnv($t, $params, $pass, $pass_events);
$ret = $this->Application->BaseURL($prefix, $ssl).$index_file.'?'.$env.$map_link;
}
return $ret;
}
/**
* Returns variables with values that should be passed throught with this link + variable list
*
* @param Array $params
* @return Array
*/
function getPassThroughVariables(&$params)
{
static $cached_pass_through = null;
if (isset($params['no_pass_through']) && $params['no_pass_through']) {
unset($params['no_pass_through']);
return Array();
}
// because pass through is not changed during script run, then we can cache it
if (is_null($cached_pass_through)) {
$cached_pass_through = Array();
$pass_through = $this->Application->GetVar('pass_through');
if ($pass_through) {
// names of variables to pass to each link
$cached_pass_through['pass_through'] = $pass_through;
$pass_through = explode(',', $pass_through);
foreach ($pass_through as $pass_through_var) {
$cached_pass_through[$pass_through_var] = $this->Application->GetVar($pass_through_var);
}
}
}
return $cached_pass_through;
}
function BuildEnv($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;
}
}
$session =& $this->Application->recallObject('Session');
+ /* @var $session Session */
+
$ssl = isset($params['__SSL__']) ? $params['__SSL__'] : 0;
$sid = $session->NeedQueryString() && !$this->Application->RewriteURLs($ssl) ? $this->Application->GetSID() : '';
// if (getArrayValue($params,'admin') == 1) $sid = $this->Application->GetSID();
$ret = '';
if ($env_var) {
$ret = ENV_VAR_NAME.'=';
}
$ret .= $sid . '-'; // SID-TEMPLATE
$encode = false;
if (isset($params['__URLENCODE__'])) {
$encode = $params['__URLENCODE__'];
unset($params['__URLENCODE__']);
}
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)) {
$mod_rw_helper =& $this->Application->recallObject('ModRewriteHelper');
/* @var $mod_rw_helper kModRewriteHelper */
$t = $mod_rw_helper->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';
}
}
$ret .= $t.':'.$this->BuildModuleEnv('m', $params, $pass_events).$env_string;
unset($params['pass'], $params['opener'], $params['m_event']);
if (array_key_exists('escape', $params) && $params['escape']) {
$ret = addslashes($ret);
unset($params['escape']);
}
if ($params) {
$params_str = '';
$join_string = $encode ? '&' : '&amp;';
foreach ($params as $param => $value) {
$params_str .= $join_string . $param . '=' . $value;
}
$ret .= $params_str;
}
if ($encode) {
$ret = str_replace('\\', '%5C', $ret);
}
return $ret;
}
function BuildEnv_NEW($t, $params, $pass='all', $pass_events = 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 = '';
$encode = false;
if (isset($params['__URLENCODE__'])) {
$encode = $params['__URLENCODE__'];
unset($params['__URLENCODE__']);
}
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->getUnitOption($prefix, 'CatalogItem');
if (array_key_exists($prefix, $this->Application->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;
}
}
// rewrited url part
$url_part = $this->BuildModuleEnv_NEW($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->BuildModuleEnv($pass_element, $params, $pass_events);
}
}
else {
$env .= ':' . $this->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_NEW('m', $params, $pass_events) . '/' . $ret;
$cat_processed = array_key_exists('category_processed', $params) && $params['category_processed'];
// remove tempporary 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']);
if (array_key_exists('escape', $params) && $params['escape']) {
$ret = addslashes($ret);
unset($params['escape']);
}
$ret = str_replace('%2F', '/', urlencode($ret));
if ($params) {
$params_str = '';
$join_string = $encode ? '&' : '&amp;';
foreach ($params as $param => $value) {
$params_str .= $join_string . $param . '=' . $value;
}
$ret .= '?' . substr($params_str, strlen($join_string));
}
if ($encode) {
$ret = str_replace('\\', '%5C', $ret);
}
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
*/
- function BuildModuleEnv($prefix_special, &$params, $pass_events = false)
+ protected function BuildModuleEnv($prefix_special, &$params, $pass_events = false)
{
list($prefix) = explode('.', $prefix_special);
- $query_vars = $this->Application->getUnitOption($prefix, 'QueryString');
+ $query_vars = $this->Application->getUnitOption($prefix, 'QueryString', Array ());
+ /* @var $query_vars Array */
- //if pass events is off and event is not implicity passed
- if( !$pass_events && !isset($params[$prefix_special.'_event']) ) {
- $params[$prefix_special.'_event'] = ''; // remove event from url if requested
+ //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 '';
+ if ( !$query_vars ) {
+ return '';
+ }
- $tmp_string = Array(0 => $prefix_special);
- foreach($query_vars as $index => $var_name)
- {
+ $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] );
+ $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();
+ $escaped = array ();
foreach ($tmp_string as $tmp_val) {
- $escaped[] = str_replace(Array('-',':'), Array('\-','\:'), $tmp_val);
+ $escaped[] = str_replace(Array ('-', ':'), Array ('\-', '\:'), $tmp_val);
}
$ret = implode('-', $escaped);
- if ($this->Application->getUnitOption($prefix, 'PortalStyleEnv') == true)
- {
- $ret = preg_replace('/^([a-zA-Z]+)-([0-9]+)-(.*)/','\\1\\2-\\3', $ret);
+ if ( $this->Application->getUnitOption($prefix, 'PortalStyleEnv') == true ) {
+ $ret = preg_replace('/^([a-zA-Z]+)-([0-9]+)-(.*)/', '\\1\\2-\\3', $ret);
}
+
return $ret;
}
function BuildModuleEnv_NEW($prefix_special, &$params, $keep_events = false)
{
list ($prefix) = explode('.', $prefix_special);
$url_parts = Array ();
$listener = $this->Application->RewriteListeners[$prefix][0];
$ret = $listener[0]->$listener[1](REWRITE_MODE_BUILD, $prefix_special, $params, $url_parts, $keep_events);
return $ret;
}
/**
* Returns sorted array of passed prefixes (to build url from)
*
* @param string $pass
* @return Array
*/
function getPassInfo($pass = 'all')
{
if (!$pass) $pass = 'all';
$pass = trim(
preg_replace(
'/(?<=,|\\A)all(?=,|\\z)/',
trim($this->Application->GetVar('passed'), ','),
trim($pass, ',')
),
',');
if (!$pass) {
return Array();
}
$pass_info = array_unique( explode(',', $pass) ); // array( prefix[.special], prefix[.special] ...
// we need to keep that sorting despite the sorting below, because this sorts prefixes with same priority by name
sort($pass_info, SORT_STRING); // to be prefix1,prefix1.special1,prefix1.special2,prefix3.specialX
foreach ($pass_info as $prefix) {
list ($prefix_only, ) = explode('.', $prefix, 2);
$sorted[$prefix] = $this->Application->getUnitOption($prefix_only, 'RewritePriority', 0);
}
asort($sorted, SORT_NUMERIC);
$pass_info = array_keys($sorted);
// ensure that "m" prefix is at the beginning
$main_index = array_search('m', $pass_info);
if ($main_index !== false) {
unset($pass_info[$main_index]);
array_unshift($pass_info, 'm');
}
return $pass_info;
}
/**
* Returns index file, that could be passed as parameter to method, as parameter to tag and as constant or not passed at all
*
* @param string $prefix
* @param string $index_file
* @param Array $params
* @return string
*/
function getIndexFile($prefix, $index_file, &$params)
{
if (isset($params['index_file'])) {
$index_file = $params['index_file'];
unset($params['index_file']);
return $index_file;
}
if (isset($index_file)) {
return $index_file;
}
if (defined('INDEX_FILE')) {
return INDEX_FILE;
}
$cut_prefix = trim(BASE_PATH, '/').'/'.trim($prefix, '/');
return trim(preg_replace('/'.preg_quote($cut_prefix, '/').'(.*)/', '\\1', $_SERVER['PHP_SELF']), '/');
}
function getPhysicalTemplate($template)
{
return array_search($template, $this->structureTemplateMapping);
}
/**
* Loads template mapping for Front-End
*
*/
function LoadStructureTemplateMapping()
{
if (!$this->Application->isAdmin) {
$category_helper =& $this->Application->recallObject('CategoryHelper');
/* @var $category_helper CategoryHelper */
$this->structureTemplateMapping = $category_helper->getTemplateMapping();
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/managers/cache_manager.php
===================================================================
--- branches/5.2.x/core/kernel/managers/cache_manager.php (revision 14627)
+++ branches/5.2.x/core/kernel/managers/cache_manager.php (revision 14628)
@@ -1,741 +1,740 @@
<?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 ConfigurationValues table
*
* @var Array
* @access protected
*/
protected $configVariables = Array();
/**
* IDs of config variables used in current run (for caching)
*
* @var Array
* @access protected
*/
protected $configIDs = Array ();
/**
* IDs of config variables retirieved from 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 (),
'registerAgent' => Array (),
'registerHook' => Array (),
'registerBuildEvent' => Array (),
'registerAggregateTag' => Array (),
);
/**
* 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' => adodb_mktime(),
'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 ' . TABLE_PREFIX . 'ConfigurationValues
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)
{
if ($name == 'Smtp_AdminMailFrom') {
$res = $this->Application->siteDomainField('AdminEmail');
if ($res) {
return $res;
}
}
if ( array_key_exists($name, $this->configVariables) ) {
return $this->configVariables[$name];
}
if ( defined('IS_INSTALL') && IS_INSTALL && !$this->Application->TableFound('ConfigurationValues') ) {
return false;
}
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT VariableId, VariableValue
FROM ' . TABLE_PREFIX . 'ConfigurationValues
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;
}
function SetConfigValue($name, $value)
{
$this->configVariables[$name] = $value;
$fields_hash = Array ('VariableValue' => $value);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'ConfigurationValues', 'VariableName = ' . $this->Conn->qstr($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->Factory->setFromCache($cache);
$this->Application->UnitConfigReader->setFromCache($cache);
$this->Application->EventManager->setFromCache($cache);
$aggregator =& $this->Application->recallObject('TagsAggregator', 'kArray');
/* @var $aggregator kArray */
$aggregator->setFromCache($cache);
$this->setFromCache($cache);
$this->Application->setFromCache($cache);
unset($cache);
return true;
-
}
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->rebuildCache('master:configs_parsed', kCache::REBUILD_NOW, CacheSettings::$unitCacheRebuildTime);
}
else {
$this->Application->rebuildDBCache('configs_parsed', kCache::REBUILD_NOW, CacheSettings::$unitCacheRebuildTime);
}
return false;
}
/**
* Empties factory and event manager cache (without storing changes)
*/
public function EmptyUnitCache()
{
$cache_keys = Array (
'Factory.Files', 'Factory.realClasses', 'Factory.Dependencies',
'EventManager.buildEvents', 'EventManager.beforeHooks',
'EventManager.afterHooks', 'EventManager.beforeRegularEvents',
'EventManager.afterRegularEvents'
);
$empty_cache = Array ();
foreach ($cache_keys as $cache_key) {
$empty_cache[$cache_key] = Array ();
}
$this->Application->Factory->setFromCache($empty_cache);
$this->Application->EventManager->setFromCache($empty_cache);
// otherwise ModulesHelper 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->Factory->getToCache(),
$this->Application->UnitConfigReader->getToCache(),
$this->Application->EventManager->getToCache(),
$aggregator->getToCache(),
$this->getToCache(),
$this->Application->getToCache()
);
$cache_rebuild_by = SERVER_NAME . ' (' . getenv('REMOTE_ADDR') . ') - ' . adodb_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 (including unit config locations)
*
* @param bool $include_sections
* @access public
*/
public function DeleteUnitCache($include_sections = false)
{
if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) {
$this->Application->rebuildCache('master:configs_parsed', kCache::REBUILD_LATER, CacheSettings::$unitCacheRebuildTime);
}
else {
$this->Application->rebuildDBCache('configs_parsed', kCache::REBUILD_LATER, CacheSettings::$unitCacheRebuildTime);
}
if ($include_sections) {
if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) {
$this->Application->rebuildCache('master:sections_parsed', kCache::REBUILD_LATER, CacheSettings::$sectionsParsedRebuildTime);
}
else {
$this->Application->rebuildDBCache('sections_parsed', kCache::REBUILD_LATER, CacheSettings::$sectionsParsedRebuildTime);
}
}
}
/**
* Preloads 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',
'Email_As_Login',
// output related
'UseModRewrite',
'UseContentLanguageNegotiation',
'UseOutputCompression',
'OutputCompressionLevel',
'Config_Site_Time',
'SystemTagCache',
// tracking related
'UseChangeLog',
'UseVisitorTracking',
'ModRewriteUrlEnding',
'ForceModRewriteUrlEnding',
'UseCronForRegularEvent',
);
$escaped_config_vars = array_map(Array (&$this->Conn, 'qstr'), $config_vars);
$sql = 'SELECT VariableId, VariableName, VariableValue
FROM ' . TABLE_PREFIX . 'ConfigurationValues
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
*
* @param Array $data
* @access public
*/
public function setFromCache(&$data)
{
$this->configVariables = $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)
*
* @return int
* @access public
*/
public function isCachingType($caching_type)
{
return $this->cacheHandler->getCachingType() == $caching_type;
}
/**
* Prints caching statistics
*
* @access public
*/
public function printStatistics()
{
$this->cacheHandler->printStatistics();
}
/**
* 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);
}
/**
* Adds new value to cache $cache_name and identified by key $key
*
* @param int $key key name to add to cache
* @param mixed $value value of chached record
* @param int $expiration when value expires (0 - doesn't expire)
* @access public
*/
public function setCache($key, $value, $expiration = 0)
{
return $this->cacheHandler->setCache($key, $value, $expiration);
}
/**
* Sets rebuilding mode for given cache
*
* @param string $name
* @param int $mode
* @param int $max_rebuilding_time
*/
public function rebuildCache($name, $mode = null, $max_rebuilding_time = 0)
{
$this->cacheHandler->rebuildCache($name, $mode, $max_rebuilding_time);
}
/**
* Deletes key from cache
*
* @param string $key
* @access public
*/
public function deleteCache($key)
{
$this->cacheHandler->delete($key);
}
/**
* Reset's all memory cache at once
*
* @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)
{
if ( $this->_getDBCache($name . '_rebuild') ) {
// cache rebuild requested -> rebuild now
$this->deleteDBCache($name . '_rebuild');
return false;
}
// no serials in cache key OR cache is outdated
$wait_seconds = $max_rebuild_seconds;
while (true) {
$cache = $this->_getDBCache($name);
$rebuilding = $this->_getDBCache($name . '_rebuilding');
if ( ($cache === false) && (!$rebuilding || $wait_seconds == 0) ) {
// cache missing and nobody rebuilding it -> rebuild; enough waiting for cache to be ready
return false;
}
elseif ( $cache !== false ) {
// cache present -> return it
return $cache;
}
$wait_seconds -= kCache::WAIT_STEP;
sleep(kCache::WAIT_STEP);
}
return false;
}
/**
* Returns value from database cache
*
* @param string $name key name
* @return mixed
* @access protected
*/
protected function _getDBCache($name)
{
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT Data, Cached, LifeTime
FROM ' . TABLE_PREFIX . 'Cache
WHERE VarName = ' . $this->Conn->qstr($name);
$data = $this->Conn->GetRow($sql);
if ($data) {
$lifetime = (int)$data['LifeTime']; // in seconds
if (($lifetime > 0) && ($data['Cached'] + $lifetime < adodb_mktime())) {
// delete expired
$this->Conn->nextQueryCachable = true;
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'Cache
WHERE VarName = ' . $this->Conn->qstr($name);
$this->Conn->Query($sql);
return false;
}
return $data['Data'];
}
return false;
}
/**
* Sets value to database cache
*
* @param string $name
* @param mixed $value
* @param int|bool $expiration
* @access public
*/
public function setDBCache($name, $value, $expiration = false)
{
$this->deleteDBCache($name . '_rebuilding');
$this->_setDBCache($name, $value, $expiration);
}
/**
* Sets value to database cache
*
* @param string $name
* @param mixed $value
* @param int|bool $expiration
* @access protected
*/
protected function _setDBCache($name, $value, $expiration = false)
{
if ((int)$expiration <= 0) {
$expiration = -1;
}
$fields_hash = Array (
'VarName' => $name,
'Data' => &$value,
'Cached' => adodb_mktime(),
'LifeTime' => (int)$expiration,
);
$this->Conn->nextQueryCachable = true;
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'Cache', 'REPLACE');
}
/**
* Sets rebuilding mode for given cache
*
* @param string $name
* @param int $mode
* @param int $max_rebuilding_time
*/
public function rebuildDBCache($name, $mode = null, $max_rebuilding_time = 0)
{
if ( !isset($mode) || $mode == kCache::REBUILD_NOW ) {
$this->_setDBCache($name . '_rebuilding', 1, $max_rebuilding_time);
$this->deleteDBCache($name . '_rebuild');
}
elseif ( $mode == kCache::REBUILD_LATER ) {
$this->_setDBCache($name . '_rebuild', 1, 0);
$this->deleteDBCache($name . '_rebuilding');
}
}
/**
* Deletes key from database cache
*
* @param string $name
* @access public
*/
public function deleteDBCache($name)
{
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'Cache
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
* @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 . 'Category
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.2.x/core/kernel/managers/request_manager.php
===================================================================
--- branches/5.2.x/core/kernel/managers/request_manager.php (revision 14627)
+++ branches/5.2.x/core/kernel/managers/request_manager.php (revision 14628)
@@ -1,435 +1,440 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2010 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 kRequestManager extends kBase {
/**
* Processes request
*
* @access public
*/
public function ProcessRequest()
{
$this->processOpener();
$events = $this->getEvents();
$all_passed = $this->getAllPassed($events);
// set "all_passed" before kApplication::GetTopmostPrefix method call !
$this->Application->SetVar('all_passed', implode(',', $all_passed));
foreach ($events as $prefix_special => $event_name) {
$event =& $this->runEvent($prefix_special, $event_name);
if ( $event->status == kEvent::erSTOP ) {
// event requested to stop processing at this point
kUtil::safeDefine('DBG_SKIP_REPORTING', 1);
$this->Application->Session->SaveData();
exit;
}
if ( $event->status == kEvent::erPERM_FAIL ) {
$this->processPermissionError($event);
}
if ( ($event->status == kEvent::erSUCCESS || $event->status == kEvent::erPERM_FAIL) && $this->canRedirect($event) ) {
$this->performRedirect($event);
}
}
$this->Application->SetVar('events', $events);
$this->Application->SetVar('passed', implode(',', $all_passed));
}
/**
* Returns event names, given in request (post, get)
*
* @return Array
* @access protected
*/
protected function getEvents()
{
$post_events = $this->getEventsFromPost();
return $post_events ? $post_events : $this->getEventsFromGet();
}
/**
* Get all passed prefixes
*
* @param Array $events
* @return Array
* @access protected
*/
protected function getAllPassed($events)
{
$ret = explode(',', $this->Application->GetVar('passed'));
foreach ($events as $prefix_special => $event_name) {
if (!$event_name) {
continue;
}
if ($this->Application->isAdmin) {
array_push($ret, $prefix_special);
}
else {
// don't add special on Front-end because of category item list special is autogenerated
$prefix_special = explode('.', $prefix_special);
array_push($ret, $prefix_special[0]);
}
}
return $ret;
}
/**
- * Creates and runs event
+ * Creates and runs event. Returns false, when prefix of given event isn't registered
*
- * @return kEvent
+ * @param string $prefix_special
+ * @param string $event_name
+ *
+ * @return kEvent|false
* @access protected
*/
protected function &runEvent($prefix_special, $event_name)
{
$event = new kEvent($prefix_special . ':' . $event_name);
if ( preg_match('/(.*?)-(.*)/', $event->Prefix, $regs) && $this->Application->prefixRegistred($regs[1]) ) {
// this is event from cloned config -> load parent config to create valid clone
$this->Application->UnitConfigReader->loadConfig($regs[1]);
$this->Application->UnitConfigReader->runAfterConfigRead($regs[1]);
}
if ( !$this->Application->EventManager->verifyEventPrefix($event, true) ) {
- return ;
+ $false = false;
+
+ return $false;
}
$event->setEventParam('top_prefix', $this->Application->GetTopmostPrefix($event->Prefix, true));
$event_handler =& $this->Application->recallObject($event->Prefix . '_EventHandler');
/* @var $event_handler kEventHandler */
- if (($this->Application->RecallVar('user_id') == USER_ROOT) || $event_handler->CheckPermission($event)) {
+ if ( ($this->Application->RecallVar('user_id') == USER_ROOT) || $event_handler->CheckPermission($event) ) {
$this->Application->HandleEvent($event);
}
return $event;
}
/**
* Processes case, when event finished with permission error
*
* @param kEvent $event
* @access protected
*/
protected function processPermissionError(&$event)
{
// should do redirect but to no_permissions template
$event->redirect = $this->Application->isAdmin ? 'no_permission' : $this->Application->ConfigValue('NoPermissionTemplate');
$event->SetRedirectParam('pass', 'm');
$themes_helper =& $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$event->SetRedirectParam( 'm_cat_id', $themes_helper->getPageByTemplate($event->redirect) );
// restore stuff, that processOpener() changed
$wid = $this->Application->GetVar('m_wid');
$this->Application->RestoreVar( rtrim('opener_stack_' . $wid, '_') );
// don't save last_template, because no_permission template does js history.back and could cause invalid opener_stack content
$this->Application->SetVar('skip_last_template', 1);
}
/**
* Performs redirect after event execution
*
* @param kEvent $event
* @access protected
*/
protected function performRedirect(&$event)
{
// we need to pass category if the action was submitted to self-template, with the category passed
// and it has not explicly set redirect template or pass_cateogry param
if ( $this->samePageRedirect($event) && ($event->getEventParam('pass_category') === false) && $this->Application->GetVar('m_cat_id') ) {
$event->SetRedirectParam('pass_category', 1);
}
$wid = $this->Application->GetVar('m_wid');
if ($wid && $event->getRedirectParam('opener') == 'u') {
$event->SetRedirectParam('opener', 's'); // because Application->HREF will react differently when 'opener' = 'u'
$event->redirect = defined('CLOSE_POPUP_TPL') ? CLOSE_POPUP_TPL : 'incs/close_popup';
}
$this->Application->Redirect($event->redirect, $event->getRedirectParams(), '', $event->redirectScript);
}
/**
* Checks, if redirect can be made
*
* @param kEvent $event
* @return bool
* @access protected
*/
protected function canRedirect(&$event)
{
return $this->samePageRedirect($event) || strlen($event->redirect) > 0;
}
/**
* Checks, that current template will be displayed after redirect
*
* @param kEvent $event
* @return bool
* @access protected
*/
protected function samePageRedirect(&$event)
{
return $event->redirect === true || $event->redirect == $this->Application->GetVar('t');
}
/**
* Returns event names given in GET
*
* @return Array
* @access protected
*/
protected function getEventsFromGet()
{
$events = Array ();
$discovered_units = $this->Application->HttpQuery->getDiscoveredUnits(false);
foreach ($discovered_units as $prefix_special => $query_string) {
$query_string = array_flip($query_string);
if ( !isset($query_string['event']) ) {
continue;
}
$event_name = $this->Application->GetVar($prefix_special . '_event');
// we need to check for pre 5.1.0 url format, because of "PerPage"
// query string part (that was added in place of "event" query
// string part) is available only since 5.1.0 version
if ($event_name && !is_numeric($event_name)) {
$events[$prefix_special] = $event_name;
}
}
return $events;
}
/**
* Returns event names given in POST
*
* @return Array
* @access protected
*/
protected function getEventsFromPost()
{
$ret = Array ();
$events = $this->Application->GetVar('events');
if (!$events) {
return Array ();
}
foreach ($events as $prefix_special => $event_name) {
if (!$event_name) {
continue;
}
if ( is_array($event_name) ) {
// HTML-input names like "events[prefix.special][event_name]", input value don't matter
$event_name = key($event_name);
$this->Application->SetVar($prefix_special . '_event', $event_name);
}
// HTML-input names like "events[prefix.special]", input value is event name
$ret[$prefix_special] = $event_name;
}
return $ret;
}
/**
* Processes window opener stack
*
* @access protected
*/
protected function processOpener()
{
$wid = $this->Application->GetVar('m_wid');
$opener_action = $this->Application->GetVar('m_opener');
$opener_stack = $this->Application->RecallVar(rtrim('opener_stack_'.$wid, '_'));
$opener_stack = $opener_stack ? unserialize($opener_stack) : Array();
switch ($opener_action) {
case 'r':
// "reset" opener stack
$opener_stack = Array ();
break;
case 'd':
// "down/push" new template to opener stack, deeplevel++
if ( $this->Application->GetVar('front') ) {
$front_session =& $this->Application->recallObject('Session.front');
/* @var $front_session Session */
array_push( $opener_stack, '../' . $front_session->RecallVar('last_template') );
}
else {
array_push( $opener_stack, $this->Application->RecallVar('last_template') );
}
break;
case 'u':
// "up/pop" last template from opener stack, deeplevel--
array_pop($opener_stack);
break;
case 'p':
// pop-up - generate new wid
$parent_wid = $this->Application->GetVar('m_wid'); // window_id of popup's parent window
$popup_wid = (int)$this->Application->RecallVar('last_wid') + 1;
$this->Application->StoreVar('last_wid', $popup_wid);
$this->Application->SetVar('m_wid', $popup_wid);
if ( $this->Application->GetVar('front') ) {
$front_session =& $this->Application->recallObject('Session.front');
/* @var $front_session Session */
$last_template = '../' . $front_session->RecallVar( rtrim('last_template_popup_' . $parent_wid, '_') );
}
else {
if ( $this->Application->GetVar('merge_opener_stack') ) {
// get last template from parent (that was closed) window opener stack
$parent_opener_stack_name = rtrim('opener_stack_' . $parent_wid, '_');
$parent_opener_stack = unserialize( $this->Application->RecallVar($parent_opener_stack_name) );
$last_template = array_pop($parent_opener_stack);
if ($parent_opener_stack) {
$this->Application->StoreVar( $parent_opener_stack_name, serialize($parent_opener_stack) );
}
else {
$this->Application->RemoveVar( $parent_opener_stack_name );
}
}
else {
$last_template = $this->Application->RecallVar( rtrim('last_template_popup_' . $parent_wid, '_') );
}
}
$opener_stack = Array ($last_template);
$this->Application->SetVar('m_opener', 's');
$wid = $popup_wid;
/*// store window relations
$window_relations = $this->Application->RecallVar('window_relations');
$window_relations = $window_relations ? unserialize($window_relations) : Array ();
$window_relations[$popup_wid] = $parent_wid;
$this->Application->StoreVar('window_relations', serialize($window_relations));*/
break;
default:
// "s/0," stay on same deep level
break;
}
$this->Application->SetVar('m_opener', 's');
$this->Application->StoreVar(rtrim('opener_stack_' . $wid, '_'), serialize($opener_stack), !$opener_stack); // empty stack is optional
}
/**
* Allows to add new element to opener stack
*
* @param string $template
* @param Array $params
* @param string $pass
* @access public
*/
public function openerStackPush($template, $params, $pass = 'all', $wid = null)
{
if (!isset($wid)) {
$wid = $this->Application->GetVar('m_wid');
}
/*// get parent window wid, when this was popup
$window_relations = $this->Application->RecallVar('window_relations');
$window_relations = $window_relations ? unserialize($window_relations) : Array ();
$wid = array_key_exists($wid, $window_relations) ? $window_relations[$wid] : false;*/
// get opener stack
$stack_name = rtrim('opener_stack_' . $wid, '_');
$opener_stack = $this->Application->RecallVar($stack_name);
$opener_stack = $opener_stack ? unserialize($opener_stack) : Array ();
// change opener stack
$default_params = Array ('m_opener' => 'u', '__URLENCODE__' => 1);
if (!$this->Application->ConfigValue('UsePopups') && $wid) {
// remove wid to show combined header block in editing window
$default_params['m_wid'] = '';
// move last popup's opener stack elemenent to main window's opener stack
if ($opener_stack) {
list ($index_file, $env) = explode('|', $opener_stack[ count($opener_stack) - 1 ], 2);
$main_params = $this->Application->HttpQuery->processQueryString($env, 'pass');
$main_template = $main_params['t'];
unset($main_params['t']);
$main_params = array_merge($main_params, $default_params);
$this->openerStackPush($main_template, $main_params, $main_params['pass'], '');
}
}
$redirect_params = array_merge($default_params, $params);
$new_level = $this->Application->BuildEnv($template, $redirect_params, $pass, true);
array_push($opener_stack, 'index.php|' . ltrim($new_level, ENV_VAR_NAME . '=') );
$this->Application->StoreVar($stack_name, serialize($opener_stack));
}
/**
* Allows to change last element in opener stack
*
* @param string $template
* @param Array $params
* @param string $pass
* @access public
*/
public function openerStackChange($params = Array(), $pass_events = true, $wid = null)
{
if (!isset($wid)) {
$wid = $this->Application->GetVar('m_wid');
}
// get opener stack
$stack_name = rtrim('opener_stack_' . $wid, '_');
$opener_stack = $this->Application->RecallVar($stack_name);
$opener_stack = $opener_stack ? unserialize($opener_stack) : Array ();
// change opener stack
list ($index_file, $env) = explode('|', $opener_stack[ count($opener_stack) - 1 ], 2);
$vars = $this->Application->HttpQuery->processQueryString($env, 'pass');
$vars = array_merge($vars, $params);
// save opener stack
$new_level = $this->Application->BuildEnv($vars['t'], $vars, $vars['pass'], $pass_events, false);
$opener_stack[ count($opener_stack) - 1 ] = $index_file . '|' . $new_level;
$this->Application->StoreVar($stack_name, serialize($opener_stack));
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/event_manager.php
===================================================================
--- branches/5.2.x/core/kernel/event_manager.php (revision 14627)
+++ branches/5.2.x/core/kernel/event_manager.php (revision 14628)
@@ -1,367 +1,369 @@
<?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 kEventManager extends kBase implements kiCacheable {
/**
* Instance of hook manager
*
* @var kHookManager
* @access protected
*/
protected $Hooks = null;
/**
* Instance of agent manager
*
* @var kAgentManager
* @access protected
*/
protected $Agents = null;
/**
* Instance of request manager
*
* @var kRequestManager
* @access protected
*/
protected $Request = null;
/**
* Build events registred for pseudo classes.
* key - pseudo class, value - event name
*
* @var Array
* @access protected
*/
protected $buildEvents = Array ();
/**
* Event recursion tracking stack
*
* @var Array
* @access protected
*/
protected $recursionStack = Array ();
/**
* Creates new instance of kEventManager class
*
*/
public function __construct()
{
parent::__construct();
$this->Hooks =& $this->Application->makeClass('kHookManager');
$this->Agents =& $this->Application->makeClass('kAgentManager');
$this->Request =& $this->Application->makeClass('kRequestManager');
}
/**
* Sets data from cache to object
*
* @param Array $data
* @access public
*/
public function setFromCache(&$data)
{
$this->Hooks->setFromCache($data);
$this->Agents->setFromCache($data);
$this->buildEvents = $data['EventManager.buildEvents'];
}
/**
* Gets object data for caching
*
* @return Array
* @access public
*/
public function getToCache()
{
return array_merge(
$this->Hooks->getToCache(),
$this->Agents->getToCache(),
Array (
'EventManager.buildEvents' => $this->buildEvents,
)
);
}
/**
* Returns information about registered agents
*
* @param bool $from_cache
* @return Array
* @access public
*/
public function getAgents($from_cache = false)
{
return $this->Agents->getAll($from_cache);
}
/**
* Add new agent
*
* @param string $short_name name to be used to store last maintenace run info
* @param string $event_name
* @param int $run_interval run interval in seconds
* @param int $type before or after agent
* @param int $status
* @access public
*/
public function registerAgent($short_name, $event_name, $run_interval, $type = reBEFORE, $status = STATUS_ACTIVE)
{
$this->Agents->add($short_name, $event_name, $run_interval, $type, $status);
}
/**
* Run registred agents with specified event type
*
* @param int $event_type
* @param bool $from_cron
* @access public
*/
public function runAgents($event_type = reBEFORE, $from_cron = false)
{
$this->Agents->runAgents($event_type, $from_cron);
}
/**
* Runs agent based on given data
*
* @param Array $agent_data
* @return bool
* @access public
*/
public function runAgent($agent_data)
{
return $this->Agents->runAgent($agent_data);
}
/**
* 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->Hooks->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->buildEvents[$pseudo_class] = $event_name;
}
/**
* Runs build event for given $pseudo_class instance, when defined
*
* @param string $prefix_special
* @param string $pseudo_class
* @param Array $event_params
* @access public
*/
public function runBuildEvent($prefix_special, $pseudo_class, $event_params)
{
if ( !isset($this->buildEvents[$pseudo_class]) ) {
return ;
}
$event = new kEvent($prefix_special . ':' . $this->buildEvents[$pseudo_class], $event_params);
$this->HandleEvent($event);
}
/**
* Check if event is called twice, that causes recursion
*
* @param kEvent $event
* @return bool
* @access protected
*/
protected function isRecursion(&$event)
{
$event_key = $event->getPrefixSpecial() . ':' . $event->Name;
return in_array($event_key, $this->recursionStack);
}
/**
* Adds event to recursion stack
*
* @param kEvent $event
* @access protected
*/
protected function pushEvent(&$event)
{
$event_key = $event->getPrefixSpecial() . ':' . $event->Name;
array_push($this->recursionStack, $event_key);
}
/**
* Removes event from recursion stack
*
* @access protected
*/
protected function popEvent()
{
array_pop($this->recursionStack);
}
/**
* Allows to process any type of event
*
* @param kEvent $event
* @access public
*/
public function HandleEvent(&$event)
{
if ( $this->isRecursion($event) || !$this->verifyEventPrefix($event) ) {
return ;
}
$this->pushEvent($event);
if (!$event->SkipBeforeHooks) {
$this->Hooks->runHooks($event, hBEFORE);
if ($event->status == kEvent::erFATAL) {
return ;
}
}
$event_handler =& $this->Application->recallObject($event->Prefix . '_EventHandler');
/* @var $event_handler kEventHandler */
$event_handler->processEvent($event);
if ($event->status == kEvent::erFATAL) {
return ;
}
if (!$event->SkipAfterHooks) {
$this->Hooks->runHooks($event, hAFTER);
}
$this->popEvent();
}
/**
* Checks if event prefix is valid
*
* @param kEvent $event
* @param bool $is_fatal
* @return string
* @access public
*/
public function verifyEventPrefix(&$event, $is_fatal = false)
{
if ( !$this->Application->prefixRegistred($event->Prefix) ) {
$this->Application->UnitConfigReader->loadConfig($event->Prefix);
if ( !$this->Application->prefixRegistred($event->Prefix) ) {
$error_msg = 'Prefix <strong>' . $event->Prefix . '</strong> not registred (requested event <strong>' . $event->Name . '</strong>)';
if ($is_fatal) {
throw new Exception($error_msg);
}
else {
trigger_error($error_msg, E_USER_WARNING);
}
return false;
}
}
return true;
}
/**
* Processes request
*
* @access public
*/
public function ProcessRequest()
{
$this->Request->ProcessRequest();
}
/**
* Allows to add new element to opener stack
*
* @param string $template
* @param Array $params
* @param string $pass
* @access public
*/
public function openerStackPush($template, $params, $pass = 'all', $wid = null)
{
$this->Request->openerStackPush($template, $params, $pass, $wid);
}
/**
* Allows to change last element in opener stack
*
* @param string $template
* @param Array $params
* @param string $pass
* @access public
*/
public function openerStackChange($params = Array(), $pass_events = true, $wid = null)
{
$this->Request->openerStackChange($params, $pass_events, $wid);
}
/**
* Set's new event for $prefix_special
* passed
*
* @param string $prefix_special
* @param string $event_name
* @access public
*/
public function setEvent($prefix_special,$event_name)
{
$actions =& $this->Application->recallObject('kActions');
- $actions->Set('events['.$prefix_special.']',$event_name);
+ /* @var $actions Params */
+
+ $actions->Set('events[' . $prefix_special . ']', $event_name);
}
/**
* Allows to determine, that required event is beeing processed right now
*
* @param string $event_key Event name in format prefix[.special]:event_name
* @return bool
* @access public
*/
public function eventRunning($event_key)
{
return array_search($event_key, $this->recursionStack) !== false;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/constants.php
===================================================================
--- branches/5.2.x/core/kernel/constants.php (revision 14627)
+++ branches/5.2.x/core/kernel/constants.php (revision 14628)
@@ -1,164 +1,164 @@
<?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!');
// item statuses
define('STATUS_DISABLED', 0);
define('STATUS_ACTIVE', 1);
define('STATUS_PENDING', 2);
define('STATUS_PENDING_EDITING', -2);
// sections
define('stTREE', 1);
define('stTAB', 2);
define('smHIDE', 0); // always hide section from tree
define('smNORMAL', 1); // show section even, if they were marked as smDEBUG or smSUPER_ADMIN before
define('smDEBUG', 2); // show section in debug mode only
define('smSUPER_ADMIN', 4); // show section in super admin & debug mode
// permission types
define('ptCATEGORY', 0);
define('ptSYSTEM', 1);
define('EDIT_MARK', '&|edit|&'); // replace this sequence inside filters to SID[_main_wid]
$application =& kApplication::Instance();
$spacer_url = $application->BaseURL().'core/admin_templates/img/spacer.gif';
define('SPACER_URL', $spacer_url);
if (!$application->isAdmin) {
- // don't show debugger buttons on front (if not overrided in "debug.php")
+ // don't show debugger buttons on front (if not overridden in "debug.php")
kUtil::safeDefine('DBG_TOOLBAR_BUTTONS', 0);
}
// common usage regular expressions
define('REGEX_EMAIL_USER', '[-a-zA-Z0-9!\#$%&*+\/=?^_`{|}~.]+');
define('REGEX_EMAIL_DOMAIN', '[a-zA-Z0-9]{1}[-.a-zA-Z0-9_]*\.[a-zA-Z]{2,6}');
define('ALLOW_DEFAULT_SETTINGS', '_USE_DEFAULT_USER_DATA_'); //Allow persistent vars to take data from default user's persistent data
class ChangeLog {
const CREATE = 1;
const UPDATE = 2;
const DELETE = 3;
}
// Separator for ValueList fields
define('VALUE_LIST_SEPARATOR', '||');
// template editing modes
define('EDITING_MODE_BROWSE', 1); // no changes, front-end as users see it
define('EDITING_MODE_CONTENT', 2); // content blocks + phrase editing
define('EDITING_MODE_DESIGN', 3); // all other blocks
class Agent {
const AGENT_TYPE_USER = 1;
const AGENT_TYPE_SYSTEM = 2;
const LAST_RUN_SUCCEDED = 1;
const LAST_RUN_FAILED = 0;
const LAST_RUN_RUNNING = 2;
}
// place for product file uploads (sort of "/system/images" but for all other files)
define('ITEM_FILES_PATH', WRITEBALE_BASE . '/downloads/');
class MailingList {
const NOT_PROCESSED = 1;
const PARTIALLY_PROCESSED = 2;
const PROCESSED = 3;
const CANCELLED = 4;
}
// theme file statuses (related to structure creation process)
define('SMS_MODE_AUTO', 1);
define('SMS_MODE_FORCE', 2);
// Means, that actual category Template field value should inherited from parent category
define('CATEGORY_TEMPLATE_INHERIT', '#inherit#');
define('REWRITE_MODE_BUILD', 1);
define('REWRITE_MODE_PARSE', 2);
define('SESSION_LOG_ACTIVE', 0);
define('SESSION_LOG_LOGGED_OUT', 1);
define('SESSION_LOG_EXPIRED', 2);
class LoginResult {
const OK = 0;
const INVALID_LOGIN = 1;
const INVALID_PASSWORD = 2;
const BANNED = 3;
const NO_PERMISSION = 4;
}
define('DESTINATION_TYPE_COUNTRY', 1);
define('DESTINATION_TYPE_STATE', 2);
class SubmissionFormField {
const VISIBILITY_EVERYONE = 1;
const VISIBILITY_UNREGISTERED = 2;
const COMMUNICATION_ROLE_NAME = 1;
const COMMUNICATION_ROLE_EMAIL = 2;
const COMMUNICATION_ROLE_SUBJECT = 3;
const COMMUNICATION_ROLE_BODY = 4;
}
// form submission statuses
define('SUBMISSION_REPLIED', 1); // submission was replied by admin
define('SUBMISSION_NOT_REPLIED', 2); // submission has no client replies (no messages at all)
define('SUBMISSION_NEW_EMAIL', 3); // submission have new reply/email from client
define('SUBMISSION_BOUNCE', 4); // submission have bounce from client
// submission log statuses
define('SUBMISSION_LOG_SENT', 1);
define('SUBMISSION_LOG_BOUNCE', 2);
define('SUBMISSION_LOG_REPLIED', 1);
define('SUBMISSION_LOG_ATTACHMENT_PATH', WRITEBALE_BASE . '/user_files/submission_log/');
define('TIMENOW', adodb_mktime()); // for faster message processing
// site domains
define('SITE_DOMAIN_REDIRECT_CURRENT', 1);
define('SITE_DOMAIN_REDIRECT_EXTERNAL', 2);
class EmailEvent {
const EVENT_TYPE_FRONTEND = 0;
const EVENT_TYPE_ADMIN = 1;
const ADDRESS_TYPE_EMAIL = 1;
const ADDRESS_TYPE_USER = 2;
const ADDRESS_TYPE_GROUP = 3;
const RECIPIENT_TYPE_TO = 1;
const RECIPIENT_TYPE_CC = 2;
const RECIPIENT_TYPE_BCC = 3;
}
define('PAGE_TYPE_VIRTUAL', 1);
define('PAGE_TYPE_TEMPLATE', 2);
define('reBEFORE', 1);
define('reAFTER', 2);
define('hBEFORE', 1);
define('hAFTER', 2);
class UserType {
const USER = 0;
const ADMIN = 1;
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/processors/tag_processor.php
===================================================================
--- branches/5.2.x/core/kernel/processors/tag_processor.php (revision 14627)
+++ branches/5.2.x/core/kernel/processors/tag_processor.php (revision 14628)
@@ -1,333 +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 kTagProcessor extends kBase {
/**
* Returns joined prefix and special if any
*
* @param bool $from_submit if true, then joins prefix & special by "_", uses "." otherwise
* @return string
* @access public
*/
public function getPrefixSpecial($from_submit = false)
{
if (!$from_submit) {
return parent::getPrefixSpecial();
}
return rtrim($this->Prefix . '_' . $this->Special, '_');
}
/**
* Processes tag
*
- * @param Tag $tag
+ * @param _BlockTag $tag
* @return string
* @access public
*/
function ProcessTag(&$tag)
{
return $this->ProcessParsedTag($tag->Tag, $tag->NP, $tag->getPrefixSpecial());
}
- function CheckTag($tag, $prefix)
+ /**
+ * Checks, that tag is implemented in this tag processor
+ *
+ * @param string $tag
+ * @param string $prefix
+ *
+ * @return bool
+ * @access public
+ */
+ public function CheckTag($tag, $prefix)
{
- $Method = $tag;
- if(method_exists($this, $Method))
- {
+ $method = $tag;
+
+ if ( method_exists($this, $method) ) {
return true;
}
- else {
- if ($this->Application->hasObject('TagsAggregator')) {
- $aggregator =& $this->Application->recallObject('TagsAggregator');
- $tmp = $this->Application->processPrefix($prefix);
- $tag_mapping = $aggregator->GetArrayValue($tmp['prefix'], $Method);
- if ($tag_mapping) {
- return true;
- }
+
+ if ( $this->Application->hasObject('TagsAggregator') ) {
+ $aggregator =& $this->Application->recallObject('TagsAggregator');
+ /* @var $aggregator kArray */
+
+ $tmp = $this->Application->processPrefix($prefix);
+ $tag_mapping = $aggregator->GetArrayValue($tmp['prefix'], $method);
+
+ if ( $tag_mapping ) {
+ return true;
}
}
+
+ return false;
}
function FormCacheKey($tag, $params, $prefix)
{
// link tag to it's template
$reg_exp = '/^' . preg_quote(FULL_PATH, '/') . '/';
$template_path = preg_replace($reg_exp, '', $this->Application->Parser->TempalteFullPath, 1);
$element = 'file=' . $template_path . ':' . $prefix . '_' . $tag . '_' . crc32( serialize($params) );
return $this->Application->Parser->FormCacheKey($element);
}
function ProcessParsedTag($tag, $params, $prefix, $file='unknown', $line=0)
{
$Method = $tag;
if (method_exists($this, $Method)) {
if (defined('DEBUG_MODE') && defined('DBG_SHOW_TAGS') && DBG_SHOW_TAGS && $this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Processing PreParsed Tag '.$Method.' in '.$this->Prefix);
}
list ($prefix_only, ) = explode('.', $prefix);
$this->Application->Parser->PrefixesInUse[$prefix_only] = 1;
+ $cache_key = '';
$backup_prefix = $this->Prefix;
$backup_special = $this->Special;
if ($this->Application->Parser->CachingEnabled && array_key_exists('cache_timeout', $params)) {
// individual tag caching
$cache_key = $this->FormCacheKey($tag, $params, $prefix);
$res = $this->Application->Parser->getCache($cache_key);
if ($res !== false) {
return $res;
}
}
$original_params = $params;
$flag_values = $this->PreparePostProcess($params);
// pass_params for non ParseBlock tags :)
if ($flag_values['pass_params']) {
$params = array_merge($this->Application->Parser->Params, $params);
}
$ret = $this->$Method($params);
$this->Init($backup_prefix, $backup_special);
$ret = $this->PostProcess($ret, $flag_values);
if ($this->Application->Parser->CachingEnabled && $flag_values['cache_timeout']) {
$this->Application->Parser->setCache($cache_key, $ret, (int)$flag_values['cache_timeout']);
}
return $ret;
}
else {
list ($ret, $tag_found) = $this->processAggregatedTag($tag, $params, $prefix, $file, $line);
if ($tag_found) {
return $ret;
}
$error_tag = Array ('file' => $file, 'line' => $line);
throw new ParserException('Undefined tag: <strong>' . $prefix . ':' . $tag . '</strong>', 0, null, $error_tag);
return false;
}
}
function processAggregatedTag($tag, $params, $prefix, $file = 'unknown', $line = 0)
{
- if ($this->Application->hasObject('TagsAggregator')) {
+ if ( $this->Application->hasObject('TagsAggregator') ) {
$Method = $tag;
$aggregator =& $this->Application->recallObject('TagsAggregator');
+ /* @var $aggregator kArray */
+
$tmp = $this->Application->processPrefix($prefix);
$tag_mapping = $aggregator->GetArrayValue($tmp['prefix'], $Method);
- if ($tag_mapping) {
+
+ if ( $tag_mapping ) {
// aggregated tag defined
$tmp = $this->Application->processPrefix($tag_mapping[0]);
- $__tag_processor = $tmp['prefix'].'_TagProcessor';
+ $__tag_processor = $tmp['prefix'] . '_TagProcessor';
$processor =& $this->Application->recallObject($__tag_processor);
/* @var $processor kTagProcessor */
$processor->Init($tmp['prefix'], getArrayValue($tag_mapping, 2) ? $tag_mapping[2] : $tmp['special']);
$params['original_tag'] = $Method; // allows to define same method for different aggregated tags in same tag processor
$params['PrefixSpecial'] = $this->getPrefixSpecial(); // $prefix;
$ret = $processor->ProcessParsedTag($tag_mapping[1], $params, $prefix);
- if (isset($params['result_to_var'])) {
+ if ( isset($params['result_to_var']) ) {
$this->Application->Parser->SetParam($params['result_to_var'], $ret);
$ret = '';
}
return Array ($ret, true);
}
else {
// aggregated tag not defined
$error_tag = Array ('file' => $file, 'line' => $line);
throw new ParserException('Undefined aggregated tag <strong>' . $prefix . ':' . $Method . '</strong> (in ' . get_class($this) . ' tag processor)', 0, null, $error_tag);
}
}
return Array ('', false);
}
function PreparePostProcess(&$params)
{
$flags = Array('js_escape', 'equals_to', 'result_to_var', 'pass_params', 'html_escape', 'strip_nl', 'trim', 'cache_timeout');
$flag_values = Array();
foreach ($flags as $flag_name) {
$flag_values[$flag_name] = false;
if (isset($params[$flag_name])) {
$flag_values[$flag_name] = $params[$flag_name];
unset($params[$flag_name]);
}
}
return $flag_values;
}
function PostProcess($ret, $flag_values)
{
if ($flag_values['html_escape']) {
$ret = htmlspecialchars($ret);
}
if ($flag_values['js_escape']) {
$ret = addslashes($ret);
$ret = str_replace(Array("\r", "\n"), Array('\r', '\n'), $ret);
$ret = str_replace('</script>', "</'+'script>", $ret);
}
if ($flag_values['strip_nl']) {
// 1 - strip \r,\n; 2 - strip tabs too
$ret = preg_replace($flag_values['strip_nl'] == 2 ? "/[\r\n\t]/" : "/[\r\n]/", '', $ret);
}
if ($flag_values['trim']) {
$ret = trim($ret);
}
// TODO: in new parser implement this parameter in compiled code (by Alex)
if ($flag_values['equals_to'] !== false) {
$equals_to = explode('|', $flag_values['equals_to']);
$ret = in_array($ret, $equals_to);
}
if ($flag_values['result_to_var']) {
$this->Application->Parser->SetParam($flag_values['result_to_var'], $ret);
$ret = '';
}
return $ret;
}
- /**
- * Not tag, method for parameter
- * selection from list in this TagProcessor
- *
- * @param Array $params
- * @param string $possible_names
- * @return string
- * @access public
- */
- function SelectParam($params, $possible_names = null)
- {
- if (!isset($possible_names)) {
- // select 1st parameter non-empty parameter value
- $possible_names = explode(',', $params['possible_names']);
- foreach ($possible_names as $param_name) {
- $value = $this->Application->Parser->GetParam($param_name);
- if (((string)$value != '') && ((string)$value != '0')) {
- return $value;
+ /**
+ * Not tag, method for parameter
+ * selection from list in this TagProcessor
+ *
+ * @param Array $params
+ * @param string $possible_names
+ * @return string|bool
+ * @access protected
+ */
+ protected function SelectParam($params, $possible_names = null)
+ {
+ if ( !isset($possible_names) ) {
+ // select 1st parameter non-empty parameter value
+ $possible_names = explode(',', $params['possible_names']);
+
+ foreach ($possible_names as $param_name) {
+ $value = $this->Application->Parser->GetParam($param_name);
+ $string_value = (string)$value;
+
+ if ( ($string_value != '') && ($string_value != '0') ) {
+ return $value;
+ }
}
- }
- return '';
- }
+ return false;
+ }
- if (!is_array($params)) {
- // really happens?
- return;
- }
+ if ( !is_array($possible_names) ) {
+ $possible_names = explode(',', $possible_names);
+ }
- if (!is_array($possible_names)) {
- $possible_names = explode(',', $possible_names);
- }
+ foreach ($possible_names as $name) {
+ if ( isset($params[$name]) ) {
+ return $params[$name];
+ }
+ }
- foreach ($possible_names as $name) {
- if( isset($params[$name]) ) return $params[$name];
+ return false;
}
- return false;
- }
-
/**
* Returns templates path for module, which is gathered from prefix module
*
* @param Array $params
* @return string
* @author Alex
*/
function ModulePath($params)
{
$force_module = getArrayValue($params, 'module');
if ($force_module) {
if ($force_module == '#session#') {
$force_module = preg_replace('/([^:]*):.*/', '\1', $this->Application->RecallVar('module'));
if (!$force_module) $force_module = 'core';
}
else {
$force_module = mb_strtolower($force_module);
}
if ($force_module == 'core') {
$module_folder = 'core';
}
else {
$module_folder = trim( $this->Application->findModule('Name', $force_module, 'Path'), '/');
}
}
else {
$module_folder = $this->Application->getUnitOption($this->Prefix, 'ModuleFolder');
}
return '../../'.$module_folder.'/admin_templates/';
}
}
/*class ProcessorsPool {
var $Processors = Array();
var $Application;
var $Prefixes = Array();
var $S;
function ProcessorsPool()
{
$this->Application =& KernelApplication::Instance();
$this->S =& $this->Application->Session;
}
function RegisterPrefix($prefix, $path, $class)
{
// echo " RegisterPrefix $prefix, $path, $class <br>";
$prefix_item = Array(
'path' => $path,
'class' => $class
);
$this->Prefixes[$prefix] = $prefix_item;
}
function CreateProcessor($prefix, &$tag)
{
// echo " prefix : $prefix <br>";
if (!isset($this->Prefixes[$prefix]))
$this->Application->ApplicationDie ("<b>Filepath and ClassName for prefix $prefix not defined while processing ".htmlspecialchars($tag->GetFullTag())."!</b>");
include_once($this->Prefixes[$prefix]['path']);
$ClassName = $this->Prefixes[$prefix]['class'];
$a_processor = new $ClassName($prefix);
$this->SetProcessor($prefix, $a_processor);
}
function SetProcessor($prefix, &$a_processor)
{
$this->Processors[$prefix] =& $a_processor;
}
function &GetProcessor($prefix, &$tag)
{
if (!isset($this->Processors[$prefix]))
$this->CreateProcessor($prefix, $tag);
return $this->Processors[$prefix];
}
}*/
\ No newline at end of file
Index: branches/5.2.x/core/kernel/processors/main_processor.php
===================================================================
--- branches/5.2.x/core/kernel/processors/main_processor.php (revision 14627)
+++ branches/5.2.x/core/kernel/processors/main_processor.php (revision 14628)
@@ -1,1169 +1,1184 @@
<?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
*
* @return string
* @access public
*/
function Base_Ref($params)
{
return '<base href="'.$this->TemplatesBase($params).'/" />';
}
/**
* 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']);
return $this->Application->HREF($template, $prefix, $params);
}
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 = htmlspecialchars($ret);
}
if (getArrayValue($params, 'urlencode')) {
$ret = urlencode($ret);
}
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');
$ret = $this->Application->GetVar($name, '');
if (array_key_exists('no_html_escape', $params) && $params['no_html_escape']) {
return kUtil::unhtmlentities($ret);
}
return $ret;
}
/**
* 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');
return $this->Application->ConfigValue($config_name);
}
- function ConfigEquals($params)
+ /**
+ * 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) == getArrayValue($params, 'value');
+
+ return $this->Application->ConfigValue($option) == $params['value'];
}
/**
* Creates all hidden fields
* needed for kernel_form
*
* @param Array $params
* @return string
- * @access public
+ * @access protected
*/
- function DumpSystemInfo($params)
+ 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
* @author Alex
*/
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');
$no_editing = array_key_exists('no_editing', $params) && $params['no_editing'];
$translation = $this->Application->Phrase($phrase_name, !$no_editing);
if (isset($params['escape']) && $params['escape']) {
$translation = htmlspecialchars($translation, ENT_QUOTES);
$translation = addslashes($translation);
}
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'];
}
if ( preg_match("/^".str_replace('/', '\/', $test_templ)."/i", $this->Application->GetVar('t'))) {
return $if_true;
}
else {
return $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_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');
if ($next_t = getArrayValue($params, 'next_template')) {
$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.'PortalGroup
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 (array_key_exists('pass_category', $params)) {
$redirect_params['pass_category'] = $params['pass_category'];
}
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' => urlencode('external:' . $_SERVER['REQUEST_URI']),
);
}
else {
$redirect_params['next_template'] = $t;
}
if ( $this->Application->LoggedIn() && !$group_access) {
$this->Application->Redirect($params['no_group_perm_template'], $redirect_params);
}
$this->Application->Redirect($params['login_template'], $redirect_params);
}
}
- function IsMember($params)
+ /**
+ * Checks, that user belongs to a group with a given name
+ *
+ * @param Array $params
+ * @return bool
+ */
+ protected function IsMember($params)
{
- $group = getArrayValue($params, 'group');
-
$sql = 'SELECT GroupId
- FROM '.TABLE_PREFIX.'PortalGroup
- WHERE Name = '.$this->Conn->qstr($group);
+ FROM ' . TABLE_PREFIX . 'PortalGroup
+ WHERE Name = ' . $this->Conn->qstr($params['group']);
$group_id = $this->Conn->GetOne($sql);
- if ($group_id) {
+ if ( $group_id ) {
$groups = explode(',', $this->Application->RecallVar('UserGroups'));
- $group_access = in_array($group_id, $groups);
+
+ return in_array($group_id, $groups);
}
- return $group_access;
+
+ return false;
}
/**
* Checks if SSL is on and redirects to SSL URL if needed
* If SSL_URL 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 unknown_type $params
+ * @param Array $params
*/
- function CheckSSL($params)
+ protected function CheckSSL($params)
{
$ssl = $this->Application->isAdmin ? $this->Application->ConfigValue('AdminSSL_URL') : false;
if ( !$ssl ) {
// not in admin or admin ssl url is empty
$ssl_url = $this->Application->siteDomainField('SSLUrl');
$ssl = $ssl_url !== false ? $ssl_url : $this->Application->ConfigValue('SSL_URL');
}
if ( !$ssl || ($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');
/* @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, mktime() + $params['cache']) . ' GMT';
$last_modified = mktime();
header('Cache-Control: public, cache, max-age=' . $params['cache']);
header("Expires: $expiration");
header('Pragma: public');
// Getting headers sent by the client.
$headers = $this->_requestHeaders();
// 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
set_time_limit(0);
ini_set('memory_limit', -1);
if ( !$this->Application->GetVar('debug') ) {
return $this->Application->XMLHeader(getArrayValue($params, 'xml_version'));
}
$lang =& $this->Application->recallObject('lang.current');
/* @var $lang LanguagesItem */
header('Content-type: text/html; charset=' . $lang->GetDBField('Charset'));
return '';
}
protected function _requestHeaders()
{
if ( function_exists('apache_request_headers') ) {
// If apache_request_headers() exists...
$headers = apache_request_headers();
if ($headers) {
return $headers; // And works... Use it
}
}
$headers = Array ();
foreach (array_keys($_SERVER) as $skey) {
if (substr($skey, 0, 5) == 'HTTP_') {
$headername = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($skey, 0, 5)))));
$headers[$headername] = $_SERVER[$skey];
}
}
return $headers;
}
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.'ConfigurationValues
WHERE VariableName = "PageHitCounter"';
$page_counter = (int)$this->Conn->GetOne($sql);
$sql = 'UPDATE LOW_PRIORITY '.TABLE_PREFIX.'ConfigurationValues
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 adodb_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;
}
}
Index: branches/5.2.x/core/kernel/utility/formatters/filesize_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/filesize_formatter.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/formatters/filesize_formatter.php (revision 14628)
@@ -1,38 +1,47 @@
<?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.
*/
class kFilesizeFormatter extends kFormatter {
+ /**
+ * Formats value of a given field
+ *
+ * @param string $value
+ * @param string $field_name
+ * @param kDBItem|kDBList $object
+ * @param string $format
+ * @return string
+ */
function Format($value, $field_name, &$object, $format=null)
{
if ($value >= 1099511627776) {
$return = round($value / 1024 / 1024 / 1024 / 1024, 2);
$suffix = "Tb";
} elseif ($value >= 1073741824) {
$return = round($value / 1024 / 1024 / 1024, 2);
$suffix = "Gb";
} elseif ($value >= 1048576) {
$return = round($value / 1024 / 1024, 2);
$suffix = "Mb";
} elseif ($value >= 1024) {
$return = round($value / 1024, 2);
$suffix = "Kb";
} else {
$return = $value;
$suffix = "B";
}
$return .= ' '.$suffix;
return $return;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/formatters/left_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/left_formatter.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/formatters/left_formatter.php (revision 14628)
@@ -1,78 +1,100 @@
<?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.
*/
/**
* Replacement for kOptionsFormatter in case if options
* should be selected from database. Use this formatter
* only in case if formatter attached field is in edit form.
*
* For usage in grid just use LEFT JOIN clause to table
* where requested options are located.
*/
class kLEFTFormatter extends kFormatter {
+ /**
+ * Formats value of a given field
+ *
+ * @param string $value
+ * @param string $field_name
+ * @param kDBItem|kDBList $object
+ * @param string $format
+ * @return string
+ */
function Format($value, $field_name, &$object, $format=null)
{
- if ( is_null($value) ) return '';
+ if ( is_null($value) ) {
+ return '';
+ }
$options = $object->GetFieldOptions($field_name);
- if ( isset($format) ) $options['format'] = $format;
+ if ( isset($format) ) {
+ $options['format'] = $format;
+ }
- if( !isset($options['options'][$value]) )
- {
+ if ( !isset($options['options'][$value]) ) {
// required option is not defined in config => query for it
- $sql = sprintf($options['left_sql'],$options['left_title_field'],$options['left_key_field'], $db->escape($value));
+ $sql = sprintf($options['left_sql'], $options['left_title_field'], $options['left_key_field'], $this->Conn->escape($value));
$options['options'][$value] = $this->Conn->GetOne($sql);
- if ($options['options'][$value] === false) return $value;
+
+ if ( $options['options'][$value] === false ) {
+ return $value;
+ }
}
+
return $options['options'][$value];
}
/**
- * Parse value from form submit
+ * Performs basic type validation on form field value
*
* @param mixed $value
* @param string $field_name
* @param kDBItem $object
* @return mixed
+ * @access public
*/
- function Parse($value, $field_name, &$object)
+ public function Parse($value, $field_name, &$object)
{
- if ($value == '') return NULL;
+ if ( $value == '' ) {
+ return NULL;
+ }
$options = $object->GetFieldOptions($field_name);
$found = isset($options['options']) ? array_search($value, $options['options']) : false;
- if ($found !== false) {
+
+ if ( $found !== false ) {
// requested option found among field options
return $found;
}
// requested option is not found in field options -> query for it
- $sql = sprintf($options['left_sql'], $options['left_key_field'], $options['left_title_field'], $db->escape($value));
+ $sql = sprintf($options['left_sql'], $options['left_key_field'], $options['left_title_field'], $this->Conn->escape($value));
$found = $this->Conn->GetOne($sql);
- if ($found !== false) {
+
+ if ( $found !== false ) {
// option successfully retrieved from db -> cache it
$options['options'][$found] = $value;
}
$skip_errors = array_key_exists('skip_errors', $options) && $options['skip_errors'];
- if ($found === false && !$skip_errors) {
+
+ if ( $found === false && !$skip_errors ) {
// option not found at all -> return not formatted value & set error
$object->SetError($field_name, 'invalid_option', 'la_error_InvalidOption');
return $value;
}
return $found;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/formatters/options_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/options_formatter.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/formatters/options_formatter.php (revision 14628)
@@ -1,94 +1,114 @@
<?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.
*/
class kOptionsFormatter extends kFormatter {
+ /**
+ * Formats value of a given field
+ *
+ * @param string $value
+ * @param string $field_name
+ * @param kDBItem|kDBList $object
+ * @param string $format
+ * @return string
+ */
function Format($value, $field_name, &$object, $format=null)
{
if ( is_null($value) ) {
return '';
}
$field_options = $object->GetFieldOptions($field_name);
if (!array_key_exists('options', $field_options) || !is_array($field_options['options'])) {
trigger_error('Options not defined for <strong>'.$object->Prefix.'</strong> field <strong>'.$field_name.'</strong>', E_USER_WARNING);
return $value;
}
$options = $field_options['options'];
$use_phrases = array_key_exists('use_phrases', $field_options) ? $field_options['use_phrases'] : false;
if (strpos($value, '|') !== false) {
// multiple checkboxes OR multiselect
$values = Array ();
$values_unsorted = explode('|', substr($value, 1, -1) );
// 1. sort values using options order from unit config
$key_indexes = array_keys($options);
foreach ($values_unsorted as $value) {
$values[ array_search($value, $key_indexes) ] = $value;
}
ksort($values);
// 2. convert values to titles
$labels = Array ();
foreach ($values as $value) {
$label = $this->formatOption($value, $options, $use_phrases);
if ($label) {
$labels[] = $label;
}
}
return implode($format, $labels);
}
else {
return $this->formatOption($value, $options, $use_phrases);
}
}
function formatOption($value, $options, $use_phrases = true)
{
$label = getArrayValue($options, $value);
if ($label !== false) {
// option_id found in options array
return $use_phrases ? $this->Application->Phrase($label) : $label;
}
else {
// option_id not found
return "$value" == "0" ? '' : $value;
}
}
- function Parse($value, $field_name, &$object)
+ /**
+ * Performs basic type validation on form field value
+ *
+ * @param mixed $value
+ * @param string $field_name
+ * @param kDBItem $object
+ * @return mixed
+ * @access public
+ */
+ public function Parse($value, $field_name, &$object)
{
- if ($value == '') return NULL;
+ if ( $value == '' ) {
+ return NULL;
+ }
+ $found = $option_key = false;
$options = $object->GetFieldOptions($field_name);
$use_phrases = getArrayValue($options, 'use_phrases');
- $found = false;
foreach ($options['options'] as $option_key => $option_value) {
- if ($use_phrases) {
+ if ( $use_phrases ) {
$option_value = $this->Application->Phrase($option_value);
}
- if ($option_value == $value) {
+ if ( $option_value == $value ) {
$found = true;
break;
}
}
return $found ? $option_key : $value;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/formatters/customfield_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/customfield_formatter.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/formatters/customfield_formatter.php (revision 14628)
@@ -1,42 +1,51 @@
<?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.
*/
/**
* Like kMultiLanguage formatter for LEFT JOINed custom fields
*
*/
class kCustomFieldFormatter extends kFormatter
{
+ /**
+ * Formats value of a given field
+ *
+ * @param string $value
+ * @param string $field_name
+ * @param kDBItem|kDBList $object
+ * @param string $format
+ * @return string
+ */
function Format($value, $field_name, &$object, $format=null)
{
$options = $object->GetFieldOptions($field_name);
$master_field = isset($options['master_field']) ? $options['master_field'] : false;
if (!$master_field) {
return $value;
}
$format = isset($format) ? $format : ( isset($options['format']) ? $options['format'] : null);
if ($value == '' && $format != 'no_default') { // try to get default language value
$def_lang_value = $object->GetDBField($master_field);
if ($def_lang_value == '') return NULL;
return $def_lang_value; //return value from default language
}
return $value;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/formatters/password_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/password_formatter.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/formatters/password_formatter.php (revision 14628)
@@ -1,145 +1,156 @@
<?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.
*/
class kPasswordFormatter extends kFormatter
{
/**
* The method is supposed to alter config options or cofigure object in some way based on its usage of formatters
* The methods is called for every field with formatter defined when configuring item.
* Could be used for adding additional VirtualFields to an object required by some special Formatter
*
* @param string $field_name
* @param array $field_options
* @param kDBBase $object
*/
function PrepareOptions($field_name, &$field_options, &$object)
{
if ( isset( $field_options['verify_field'] ) ) {
$add_fields = Array ();
$options = Array ('master_field' => $field_name, 'formatter' => 'kPasswordFormatter');
$copy_options = Array ('encryption_method', 'salt', 'required', 'skip_empty');
foreach ($copy_options as $copy_option) {
if (array_key_exists($copy_option, $field_options)) {
$options[$copy_option] = $field_options[$copy_option];
}
}
$add_fields[ $field_options['verify_field'] ] = $options;
$add_fields[$field_name.'_plain'] = Array('type'=>'string', 'error_field'=>$field_name);
$add_fields[ $field_options['verify_field'].'_plain' ] = Array('type'=>'string', 'error_field'=>$field_options['verify_field'] );
$virtual_fields = $object->getVirtualFields();
$add_fields = kUtil::array_merge_recursive($add_fields, $virtual_fields);
$object->setVirtualFields($add_fields);
}
}
+ /**
+ * Formats value of a given field
+ *
+ * @param string $value
+ * @param string $field_name
+ * @param kDBItem|kDBList $object
+ * @param string $format
+ * @return string
+ */
function Format($value, $field_name, &$object, $format=null)
{
return $value;
}
/**
* Performs password & verify password field validation
*
* @param mixed $value
* @param string $field_name
* @param kDBItem $object
- * @return string
+ * @return mixed
+ * @access public
*/
- function Parse($value, $field_name, &$object)
+ public function Parse($value, $field_name, &$object)
{
$options = $object->GetFieldOptions($field_name);
$flip_count = 0;
$fields_set = true;
+ $password_field = $verify_field = '';
$fields = Array ('master_field', 'verify_field');
-
+
// 1. collect values from both Password and VerifyPassword fields
while ($flip_count < 2) {
if ( getArrayValue($options, $fields[0]) ) {
$tmp_field = $options[ $fields[0] ];
$object->SetDBField($field_name.'_plain', $value);
if ( !$object->GetFieldOption($tmp_field, $fields[1].'_set') ) {
$object->SetFieldOption($tmp_field, $fields[1].'_set', true);
}
$password_field = $options[ $fields[0] ];
$verify_field = $field_name;
}
$fields = array_reverse($fields);
$flip_count++;
}
$salt = $object->GetFieldOption($password_field, 'salt', false, '');
if ($object->GetFieldOption($password_field, 'verify_field_set') && $object->GetFieldOption($verify_field, 'master_field_set')) {
$new_password = $object->GetDBField($password_field . '_plain');
$verify_password = $object->GetDBField($verify_field . '_plain');
if ($new_password == '' && $verify_password == '') {
// both passwords are empty -> keep old password
if ($object->GetDBField($password_field) != $this->EncryptPassword('', $salt)) {
if ($options['encryption_method'] == 'plain') {
return $value;
}
return $this->EncryptPassword($value);
}
else {
return $value;
}
}
// determine admin or front
$phrase_error_prefix = $this->Application->isAdmin ? 'la' : 'lu';
if ($new_password != $verify_password) {
// passwords don't match (no matter what is their length)
$object->SetError($verify_field, 'passwords_do_not_match', $phrase_error_prefix.'_passwords_do_not_match');
}
$min_length = $this->Application->ConfigValue('Min_Password'); // for error message too
$min_length = $object->GetFieldOption($password_field, 'min_length', false, $min_length);
if (mb_strlen($new_password) < $min_length) {
$error_msg = '+' . sprintf($this->Application->Phrase($phrase_error_prefix.'_passwords_too_short'), $min_length); // + -> not phrase
$object->SetError($password_field, 'passwords_min_length', $error_msg);
}
}
if ($value == '') {
return $object->GetDBField($field_name);
}
if ($options['encryption_method'] == 'plain') {
return $value;
}
return $this->EncryptPassword($value, $salt);
}
function EncryptPassword($value, $salt=null)
{
if (!isset($salt) || !$salt) {
// if empty salt, assume, that it's not passed at all
return md5($value);
}
return md5(md5($value).$salt);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/formatters/ccdate_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/ccdate_formatter.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/formatters/ccdate_formatter.php (revision 14628)
@@ -1,83 +1,100 @@
<?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.
*/
/**
* Credit card expiration date formatter
*
*/
class kCCDateFormatter extends kFormatter
{
/**
* The method is supposed to alter config options or cofigure object in some way based on its usage of formatters
* The methods is called for every field with formatter defined when configuring item.
* Could be used for adding additional VirtualFields to an object required by some special Formatter
*
* @param string $field_name
* @param array $field_options
* @param kDBBase $object
*/
function PrepareOptions($field_name, &$field_options, &$object)
{
$add_fields = Array();
$i = 1;
$options = Array('00' => '');
while($i <= 12)
{
$options[ sprintf('%02d',$i) ] = sprintf('%02d',$i);
$i++;
}
$add_fields[ $field_options['month_field'] ] = Array('formatter'=>'kOptionsFormatter', 'options' => $options, 'not_null' => true, 'default' => '00');
$add_fields[ $field_options['year_field'] ] = Array('type' => 'string', 'default' => '');
$virtual_fields = $object->getVirtualFields();
$add_fields = kUtil::array_merge_recursive($add_fields, $virtual_fields);
$object->setVirtualFields($add_fields);
}
- function UpdateSubFields($field, $value, &$options, &$object)
+ /**
+ * Used for split fields like timestamp -> date, time
+ * Called from DBItem to update sub fields values after loading item
+ *
+ * @param string $field
+ * @param string $value
+ * @param Array $options
+ * @param kDBItem $object
+ * @return void
+ * @access public
+ */
+ public function UpdateSubFields($field, $value, &$options, &$object)
{
- if(!$value) return false;
+ if ( !$value ) {
+ return ;
+ }
+
$date = explode('/', $value);
- $object->SetDBField( $options['month_field'], $date[0] );
- $object->SetDBField( $options['year_field'], $date[1] );
+ $object->SetDBField($options['month_field'], $date[0]);
+ $object->SetDBField($options['year_field'], $date[1]);
}
/**
* Will work in future if we could attach 2 formatters to one field
*
- * @param string $value
+ * @param mixed $value
* @param string $field_name
* @param kDBItem $object
- * @return string
+ * @return mixed
+ * @access public
*/
- function Parse($value, $field_name, &$object)
+ public function Parse($value, $field_name, &$object)
{
-// if ( is_null($value) ) return '';
-
$options = $object->GetFieldOptions($field_name);
$month = $object->GetDirtyField($options['month_field']);
$year = $object->GetDirtyField($options['year_field']);
- if( !(int)$month && !(int)$year ) return NULL;
+ if ( !(int)$month && !(int)$year ) {
+ return NULL;
+ }
+
$is_valid = ($month >= 1 && $month <= 12) && ($year >= 0 && $year <= 99);
- if (!$is_valid) {
+ if ( !$is_valid ) {
$object->SetError($field_name, 'bad_type');
}
- return $month.'/'.$year;
+
+ return $month . '/' . $year;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/formatters/upload_formatter.php
===================================================================
--- branches/5.2.x/core/kernel/utility/formatters/upload_formatter.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/formatters/upload_formatter.php (revision 14628)
@@ -1,390 +1,399 @@
<?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.
*/
class kUploadFormatter extends kFormatter
{
var $DestinationPath;
var $FullPath;
/**
* File helper reference
*
* @var FileHelper
*/
var $fileHelper = null;
public function __construct()
{
parent::__construct();
$this->fileHelper =& $this->Application->recallObject('FileHelper');
if ($this->DestinationPath) {
$this->FullPath = FULL_PATH.$this->DestinationPath;
}
}
/**
* Processes file uploads from form
*
* @param mixed $value
* @param string $field_name
* @param kDBItem $object
* @return mixed
* @access public
*/
public function Parse($value, $field_name, &$object)
{
$ret = !is_array($value) ? $value : '';
$options = $object->GetFieldOptions($field_name);
if (getArrayValue($options, 'upload_dir')) {
$this->DestinationPath = $options['upload_dir'];
$this->FullPath = FULL_PATH.$this->DestinationPath;
}
$this->fileHelper->CheckFolder($this->FullPath);
// SWF Uploader
if (is_array($value) && isset($value['tmp_ids'])) {
if ($value['tmp_deleted']) {
$deleted = explode('|', $value['tmp_deleted']);
$upload = explode('|', $value['upload']);
$n_upload = array();
// $n_ids = array();
foreach ($upload as $name) {
if (in_array($name, $deleted)) continue;
$n_upload[] = $name;
// $n_ids[] = $name;
}
$value['upload'] = implode('|', $n_upload);
// $value['tmp_ids'] = implode('|', $n_ids);
}
if (!$value['tmp_ids']) {
// no pending files -> return already uploded files
return getArrayValue($value, 'upload');
}
$swf_uploaded_ids = explode('|', $value['tmp_ids']);
$swf_uploaded_names = explode('|', $value['tmp_names']);
$existing = $value['upload'] ? explode('|', $value['upload']) : array();
if (isset($options['multiple'])) {
$max_files = $options['multiple'] == false ? 1 : $options['multiple'];
}
else {
$max_files = 1;
}
$fret = array();
// don't delete uploaded file, when it's name matches delete file name
$var_name = $object->getPrefixSpecial().'_file_pending_actions'.$this->Application->GetVar('m_wid');
$schedule = $this->Application->RecallVar($var_name);
$schedule = $schedule ? unserialize($schedule) : Array();
$files2delete = Array();
foreach ($schedule as $data) {
if ($data['action'] == 'delete') {
$files2delete[] = $data['file'];
}
}
for ($i=0; $i<min($max_files, count($swf_uploaded_ids)); $i++) {
$real_name = $swf_uploaded_names[$i];
$real_name = $this->_ensureUniqueFilename($this->FullPath, $real_name, $files2delete);
$file_name = $this->FullPath.$real_name;
$tmp_file = WRITEABLE . '/tmp/' . $swf_uploaded_ids[$i].'_'.$swf_uploaded_names[$i];
rename($tmp_file, $file_name);
@chmod($file_name, 0666);
$fret[] = getArrayValue($options, 'upload_dir') ? $real_name : $this->DestinationPath.$real_name;
}
$fret = array_merge($existing, $fret);
return implode('|', $fret);
}
// SWF Uploader END
if (getArrayValue($value, 'upload') && getArrayValue($value, 'error') == UPLOAD_ERR_NO_FILE) {
// file was not uploaded this time, but was uploaded before, then use previously uploaded file (from db)
return getArrayValue($value, 'upload');
}
if (is_array($value) && count($value) > 1 && $value['size']) {
if (is_array($value) && $value['error'] === UPLOAD_ERR_OK) {
$max_filesize = isset($options['max_size']) ? $options['max_size'] : MAX_UPLOAD_SIZE;
+ // we can get mime type based on file content and no use one, provided by the client
+// $value['type'] = kUtil::mimeContentType($value['tmp_name']);
+
if ( getArrayValue($options, 'file_types') && !$this->extensionMatch($value['name'], $options['file_types']) ) {
// match by file extensions
$error_params = Array (
'file_name' => $value['name'],
'file_types' => $options['file_types'],
);
+
$object->SetError($field_name, 'bad_file_format', 'la_error_InvalidFileFormat', $error_params);
}
- elseif (getArrayValue($options, 'allowed_types') && !in_array($value['type'], $options['allowed_types'])) {
+ elseif ( getArrayValue($options, 'allowed_types') && !in_array($value['type'], $options['allowed_types']) ) {
// match by mime type provided by web-browser
$error_params = Array (
'file_type' => $value['type'],
'allowed_types' => $options['allowed_types'],
);
+
$object->SetError($field_name, 'bad_file_format', 'la_error_InvalidFileFormat', $error_params);
}
- elseif ($value['size'] > $max_filesize) {
+ elseif ( $value['size'] > $max_filesize ) {
$object->SetError($field_name, 'bad_file_size', 'la_error_FileTooLarge');
}
- elseif (!is_writable($this->FullPath)) {
+ elseif ( !is_writable($this->FullPath) ) {
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
else {
$real_name = $this->_ensureUniqueFilename($this->FullPath, $value['name']);
- $file_name = $this->FullPath.$real_name;
- if (!move_uploaded_file($value['tmp_name'], $file_name)) {
+ $file_name = $this->FullPath . $real_name;
+ if ( !move_uploaded_file($value['tmp_name'], $file_name) ) {
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
else {
@chmod($file_name, 0666);
- if (getArrayValue($options, 'size_field')) {
+
+ if ( getArrayValue($options, 'size_field') ) {
$object->SetDBField($options['size_field'], $value['size']);
}
- if (getArrayValue($options, 'orig_name_field')) {
+
+ if ( getArrayValue($options, 'orig_name_field') ) {
$object->SetDBField($options['orig_name_field'], $value['name']);
}
- if (getArrayValue($options, 'content_type_field')) {
+
+ if ( getArrayValue($options, 'content_type_field') ) {
$object->SetDBField($options['content_type_field'], $value['type']);
}
- $ret = getArrayValue($options, 'upload_dir') ? $real_name : $this->DestinationPath.$real_name;
+
+ $ret = getArrayValue($options, 'upload_dir') ? $real_name : $this->DestinationPath . $real_name;
// delete previous file, when new file is uploaded under same field
/*$previous_file = isset($value['upload']) ? $value['upload'] : false;
if ($previous_file && file_exists($this->FullPath.$previous_file)) {
unlink($this->FullPath.$previous_file);
}*/
}
}
}
else {
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file');
}
}
- if ((count($value) > 1) && $value['error'] && ($value['error'] != UPLOAD_ERR_NO_FILE)) {
+ if ( (count($value) > 1) && $value['error'] && ($value['error'] != UPLOAD_ERR_NO_FILE) ) {
$object->SetError($field_name, 'cant_save_file', 'la_error_cant_save_file', $value);
}
return $ret;
}
/**
* Checks, that given file name has on of provided file extensions
*
* @param string $filename
* @param string $file_types
* @return bool
* @access protected
*/
protected function extensionMatch($filename, $file_types)
{
if ( preg_match_all('/\*\.(.*?)(;|$)/', $file_types, $regs) ) {
$file_extension = mb_strtolower( pathinfo($filename, PATHINFO_EXTENSION) );
$file_extensions = array_map('mb_strtolower', $regs[1]);
return in_array($file_extension, $file_extensions);
}
return true;
}
function getSingleFormat($format)
{
$single_mapping = Array (
'file_urls' => 'full_url',
'file_paths' => 'full_path',
'file_sizes' => 'file_size',
'files_resized' => 'resize',
'img_sizes' => 'img_size',
'wms' => 'wm',
);
return $single_mapping[$format];
}
/**
* Return formatted file url,path or size (or same for multiple files)
*
* @param string $value
* @param string $field_name
* @param kDBItem|kDBList $object
* @param string $format
* @return string
*/
function Format($value, $field_name, &$object, $format = null)
{
if (is_null($value)) {
return '';
}
$options = $object->GetFieldOptions($field_name);
if (!isset($format)) {
$format = isset($options['format']) ? $options['format'] : false;
}
if ($format && preg_match('/(file_urls|file_paths|file_names|file_sizes|img_sizes|files_resized|wms)(.*)/', $format, $regs)) {
if (!$value || $format == 'file_names') {
// storage format matches display format OR no value
return $value;
}
$ret = Array ();
$files = explode('|', $value);
$format = $this->getSingleFormat($regs[1]).$regs[2];
foreach ($files as $a_file) {
$ret[] = $this->GetFormatted($a_file, $field_name, $object, $format);
}
return implode('|', $ret);
}
$tc_value = $this->TypeCast($value, $options);
if( ($tc_value === false) || ($tc_value != $value) ) return $value; // for leaving badly formatted date on the form
// force direct links for case, when non-swf uploader is used
return $this->GetFormatted($tc_value, $field_name, $object, $format, true);
}
/**
* Return formatted file url,path or size
*
* @param string $value
* @param string $field_name
* @param kDBItem $object
* @param string $format
* @param bool $force_direct_links
* @return string
*/
function GetFormatted($value, $field_name, &$object, $format = null, $force_direct_links = null)
{
if (!$format) {
return $value;
}
$options = $object->GetFieldOptions($field_name);
$upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath;
if (preg_match('/resize:([\d]*)x([\d]*)/', $format, $regs)) {
$image_helper =& $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
if (array_key_exists('include_path', $options) && $options['include_path']) {
// relative path is already included in field
$upload_dir = '';
}
return $image_helper->ResizeImage($value ? FULL_PATH . str_replace('/', DIRECTORY_SEPARATOR, $upload_dir) . $value : '', $format);
}
switch ($format) {
case 'full_url':
if ( isset($force_direct_links) ) {
$direct_links = $force_direct_links;
}
else {
$direct_links = isset($options['direct_links']) ? $options['direct_links'] : false;
}
if ( $direct_links ) {
return $this->fileHelper->pathToUrl(FULL_PATH . $upload_dir . $value);
}
else {
$url_params = Array (
'no_amp' => 1, 'pass' => 'm,'.$object->Prefix,
$object->Prefix . '_event' => 'OnViewFile',
'file' => rawurlencode($value), 'field' => $field_name
);
return $this->Application->HREF('', '', $url_params);
}
break;
case 'full_path':
return FULL_PATH . str_replace('/', DIRECTORY_SEPARATOR, $upload_dir) . $value;
break;
case 'file_size':
return filesize(FULL_PATH . str_replace('/', DIRECTORY_SEPARATOR, $upload_dir) . $value);
break;
case 'img_size':
$image_helper =& $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
$image_info = $image_helper->getImageInfo(FULL_PATH . str_replace('/', DIRECTORY_SEPARATOR, $upload_dir) . $value);
return $image_info ? $image_info[3] : '';
break;
}
return sprintf($format, $value);
}
/**
* Ensures, that uploaded file will not overwrite any of previously uploaded files with same name
*
* @param string $path
* @param string $name
* @param Array $forbidden_names
* @return string
*/
function _ensureUniqueFilename($path, $name, $forbidden_names = Array())
{
$parts = pathinfo($name);
$ext = '.' . $parts['extension'];
$filename = mb_substr($parts['basename'], 0, -mb_strlen($ext));
$new_name = $filename . $ext;
while (file_exists($path . '/' . $new_name) || in_array(rtrim($path, '/') . '/' . $new_name, $forbidden_names)) {
if (preg_match('/(' . preg_quote($filename, '/') . '_)([0-9]*)(' . preg_quote($ext, '/') . ')/', $new_name, $regs)) {
$new_name = $regs[1] . ($regs[2] + 1) . $regs[3];
}
else {
$new_name = $filename . '_1' . $ext;
}
}
return $new_name;
}
}
class kPictureFormatter extends kUploadFormatter
{
public function __construct()
{
$this->NakeLookupPath = IMAGES_PATH; // used ?
$this->DestinationPath = kUtil::constOn('ADMIN') ? IMAGES_PENDING_PATH : IMAGES_PATH;
parent::__construct();
}
function GetFormatted($value, $field_name, &$object, $format = null, $force_direct_links = null)
{
if ($format == 'img_size') {
$upload_dir = isset($options['upload_dir']) ? $options['upload_dir'] : $this->DestinationPath;
$img_path = FULL_PATH.'/'.$upload_dir.$value;
$image_info = @getimagesize($img_path);
return ' width="'.$image_info[0].'" height="'.$image_info[1].'"';
}
return parent::GetFormatted($value, $field_name, $object, $format, $force_direct_links);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/temp_handler.php
===================================================================
--- branches/5.2.x/core/kernel/utility/temp_handler.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/temp_handler.php (revision 14628)
@@ -1,960 +1,1013 @@
<?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 kTempTablesHandler extends kBase {
var $Tables = Array();
/**
* Master table name for temp handler
*
* @var string
* @access private
*/
var $MasterTable = '';
/**
* IDs from master table
*
* @var Array
* @access private
*/
var $MasterIDs = Array();
var $AlreadyProcessed = Array();
var $DroppedTables = Array();
var $FinalRefs = Array();
+ var $TableIdCounter = 0;
+
var $CopiedTables = Array();
+ /**
+ * Foreign key cache
+ *
+ * @var Array
+ */
+ var $FKeysCache = Array ();
/**
* IDs of newly cloned items (key - prefix.special, value - array of ids)
*
* @var Array
*/
var $savedIDs = Array();
/**
* Window ID of current window
*
* @var mixed
*/
var $WindowID = '';
function SetTables($tables)
{
// set tablename as key for tables array
$ret = Array();
$this->Tables = $tables;
$this->MasterTable = $tables['TableName'];
}
function saveID($prefix, $special = '', $id = null)
{
if (!isset($this->savedIDs[$prefix.($special ? '.' : '').$special])) {
$this->savedIDs[$prefix.($special ? '.' : '').$special] = array();
}
if (is_array($id)) {
foreach ($id as $tmp_id => $live_id) {
$this->savedIDs[$prefix.($special ? '.' : '').$special][$tmp_id] = $live_id;
}
}
else {
$this->savedIDs[$prefix.($special ? '.' : '').$special][] = $id;
}
}
/**
* Get temp table name
*
* @param string $table
* @return string
*/
function GetTempName($table)
{
return $this->Application->GetTempName($table, $this->WindowID);
}
function GetTempTablePrefix()
{
return $this->Application->GetTempTablePrefix($this->WindowID);
}
/**
* Return live table name based on temp table name
*
* @param string $temp_table
* @return string
*/
function GetLiveName($temp_table)
{
return $this->Application->GetLiveName($temp_table);
}
function IsTempTable($table)
{
return $this->Application->IsTempTable($table);
}
/**
* Return temporary table name for master table
*
* @return string
* @access public
*/
function GetMasterTempName()
{
return $this->GetTempName($this->MasterTable);
}
function CreateTempTable($table)
{
$query = sprintf("CREATE TABLE %s SELECT * FROM %s WHERE 0",
$this->GetTempName($table),
$table);
$this->Conn->Query($query);
}
function BuildTables($prefix, $ids)
{
$this->WindowID = $this->Application->GetVar('m_wid');
$this->TableIdCounter = 0;
$tables = Array(
'TableName' => $this->Application->getUnitOption($prefix, 'TableName'),
'IdField' => $this->Application->getUnitOption($prefix, 'IDField'),
'IDs' => $ids,
'Prefix' => $prefix,
'TableId' => $this->TableIdCounter++,
);
/*$parent_prefix = $this->Application->getUnitOption($prefix, 'ParentPrefix');
if ($parent_prefix) {
$tables['ForeignKey'] = $this->Application->getUnitOption($prefix, 'ForeignKey');
$tables['ParentPrefix'] = $parent_prefix;
$tables['ParentTableKey'] = $this->Application->getUnitOption($prefix, 'ParentTableKey');
}*/
- $this->FinalRefs[ $tables['TableName'] ] = $tables['TableId']; // don't forget to add main table to FinalRefs too
+ $this->FinalRefs[ $tables['TableName'] ] = $tables['TableId']; // don't forget to add main table to FinalRefs too
+
+ $sub_items = $this->Application->getUnitOption($prefix, 'SubItems', Array ());
+ /* @var $sub_items Array */
- $SubItems = $this->Application->getUnitOption($prefix,'SubItems');
- if (is_array($SubItems)) {
- foreach ($SubItems as $prefix) {
+ if ( is_array($sub_items) ) {
+ foreach ($sub_items as $prefix) {
$this->AddTables($prefix, $tables);
}
}
+
$this->SetTables($tables);
}
/**
* Searches through TempHandler tables info for required prefix
*
* @param string $prefix
* @param Array $master
* @return mixed
*/
function SearchTable($prefix, $master = null)
{
if (is_null($master)) {
$master = $this->Tables;
}
if ($master['Prefix'] == $prefix) {
return $master;
}
if (isset($master['SubTables'])) {
foreach ($master['SubTables'] as $sub_table) {
$found = $this->SearchTable($prefix, $sub_table);
if ($found !== false) {
return $found;
}
}
}
return false;
}
function AddTables($prefix, &$tables)
{
- if (!$this->Application->prefixRegistred($prefix)) {
+ if ( !$this->Application->prefixRegistred($prefix) ) {
// allows to skip subitem processing if subitem module not enabled/installed
return ;
}
$tmp = Array(
'TableName' => $this->Application->getUnitOption($prefix,'TableName'),
'IdField' => $this->Application->getUnitOption($prefix,'IDField'),
'ForeignKey' => $this->Application->getUnitOption($prefix,'ForeignKey'),
'ParentPrefix' => $this->Application->getUnitOption($prefix, 'ParentPrefix'),
'ParentTableKey' => $this->Application->getUnitOption($prefix,'ParentTableKey'),
'Prefix' => $prefix,
'AutoClone' => $this->Application->getUnitOption($prefix,'AutoClone'),
'AutoDelete' => $this->Application->getUnitOption($prefix,'AutoDelete'),
'TableId' => $this->TableIdCounter++,
);
$this->FinalRefs[ $tmp['TableName'] ] = $tmp['TableId'];
- $constrain = $this->Application->getUnitOption($prefix,'Constrain');
- if ($constrain)
- {
+ $constrain = $this->Application->getUnitOption($prefix, 'Constrain');
+ if ( $constrain ) {
$tmp['Constrain'] = $constrain;
- $this->FinalRefs[ $tmp['TableName'].$tmp['Constrain'] ] = $tmp['TableId'];
+ $this->FinalRefs[ $tmp['TableName'] . $tmp['Constrain'] ] = $tmp['TableId'];
}
- $SubItems = $this->Application->getUnitOption($prefix,'SubItems');
- $same_sub_counter = 1;
- if( is_array($SubItems) )
- {
- foreach($SubItems as $prefix)
- {
+ $sub_items = $this->Application->getUnitOption($prefix, 'SubItems', Array ());
+ /* @var $sub_items Array */
+
+ if ( is_array($sub_items) ) {
+ foreach ($sub_items as $prefix) {
$this->AddTables($prefix, $tmp);
}
}
if ( !is_array(getArrayValue($tables, 'SubTables')) ) {
- $tables['SubTables'] = array();
+ $tables['SubTables'] = Array ();
}
$tables['SubTables'][] = $tmp;
}
function CloneItems($prefix, $special, $ids, $master = null, $foreign_key = null, $parent_prefix = null, $skip_filenames = false)
{
if (!isset($master)) $master = $this->Tables;
// recalling by different name, because we may get kDBList, if we recall just by prefix
if (!preg_match('/(.*)-item$/', $special)) {
$special .= '-item';
}
$object =& $this->Application->recallObject($prefix.'.'.$special, $prefix, Array('skip_autoload' => true));
- /* @var $object kDBItem */
+ /* @var $object kCatDBItem */
$object->PopulateMultiLangFields();
foreach ($ids as $id) {
$mode = 'create';
if ( $cloned_ids = getArrayValue($this->AlreadyProcessed, $master['TableName']) ) {
// if we have already cloned the id, replace it with cloned id and set mode to update
// update mode is needed to update second ForeignKey for items cloned by first ForeignKey
if ( getArrayValue($cloned_ids, $id) ) {
$id = $cloned_ids[$id];
$mode = 'update';
}
}
$object->Load($id);
$original_values = $object->GetFieldValues();
if (!$skip_filenames) {
$object->NameCopy($master, $foreign_key);
}
elseif ($master['TableName'] == $this->MasterTable) {
// kCatDBItem class only has this attribute
$object->useFilenames = false;
}
if (isset($foreign_key)) {
$master_foreign_key_field = is_array($master['ForeignKey']) ? $master['ForeignKey'][$parent_prefix] : $master['ForeignKey'];
$object->SetDBField($master_foreign_key_field, $foreign_key);
}
if ($mode == 'create') {
$this->RaiseEvent('OnBeforeClone', $master['Prefix'], $special, Array($object->GetId()), $foreign_key);
}
$object->inCloning = true;
$res = $mode == 'update' ? $object->Update() : $object->Create();
$object->inCloning = false;
if ($res)
{
if ( $mode == 'create' && is_array( getArrayValue($master, 'ForeignKey')) ) {
// remember original => clone mapping for dual ForeignKey updating
$this->AlreadyProcessed[$master['TableName']][$id] = $object->GetId();
}
if ($mode == 'create') {
$this->RaiseEvent('OnAfterClone', $master['Prefix'], $special, Array($object->GetId()), $foreign_key, array('original_id' => $id) );
$this->saveID($master['Prefix'], $special, $object->GetID());
}
if ( is_array(getArrayValue($master, 'SubTables')) ) {
foreach($master['SubTables'] as $sub_table) {
if (!getArrayValue($sub_table, 'AutoClone')) continue;
$sub_TableName = $object->IsTempTable() ? $this->GetTempName($sub_table['TableName']) : $sub_table['TableName'];
$foreign_key_field = is_array($sub_table['ForeignKey']) ? $sub_table['ForeignKey'][$master['Prefix']] : $sub_table['ForeignKey'];
$parent_key_field = is_array($sub_table['ParentTableKey']) ? $sub_table['ParentTableKey'][$master['Prefix']] : $sub_table['ParentTableKey'];
if (!$foreign_key_field || !$parent_key_field) continue;
$query = 'SELECT '.$sub_table['IdField'].' FROM '.$sub_TableName.'
WHERE '.$foreign_key_field.' = '.$original_values[$parent_key_field];
if (isset($sub_table['Constrain'])) $query .= ' AND '.$sub_table['Constrain'];
$sub_ids = $this->Conn->GetCol($query);
if ( is_array(getArrayValue($sub_table, 'ForeignKey')) ) {
// $sub_ids could containt newly cloned items, we need to remove it here
// to escape double cloning
$cloned_ids = getArrayValue($this->AlreadyProcessed, $sub_table['TableName']);
if ( !$cloned_ids ) $cloned_ids = Array();
$new_ids = array_values($cloned_ids);
$sub_ids = array_diff($sub_ids, $new_ids);
}
$parent_key = $object->GetDBField($parent_key_field);
$this->CloneItems($sub_table['Prefix'], $special, $sub_ids, $sub_table, $parent_key, $master['Prefix']);
}
}
}
}
if (!$ids) {
$this->savedIDs[$prefix.($special ? '.' : '').$special] = Array();
}
return $this->savedIDs[$prefix.($special ? '.' : '').$special];
}
function DeleteItems($prefix, $special, $ids, $master=null, $foreign_key=null)
{
if (!isset($master)) $master = $this->Tables;
if( strpos($prefix,'.') !== false ) list($prefix,$special) = explode('.', $prefix, 2);
$prefix_special = rtrim($prefix.'.'.$special, '.');
//recalling by different name, because we may get kDBList, if we recall just by prefix
$recall_prefix = $prefix_special.($special ? '' : '.').'-item';
$object =& $this->Application->recallObject($recall_prefix, $prefix, Array('skip_autoload' => true));
/* @var $object kDBItem */
foreach ($ids as $id)
{
$object->Load($id);
$original_values = $object->GetFieldValues();
if( !$object->Delete($id) ) continue;
if ( is_array(getArrayValue($master, 'SubTables')) ) {
foreach($master['SubTables'] as $sub_table) {
if (!getArrayValue($sub_table, 'AutoDelete')) continue;
$sub_TableName = $object->IsTempTable() ? $this->GetTempName($sub_table['TableName']) : $sub_table['TableName'];
$foreign_key_field = is_array($sub_table['ForeignKey']) ? getArrayValue($sub_table, 'ForeignKey', $master['Prefix']) : $sub_table['ForeignKey'];
$parent_key_field = is_array($sub_table['ParentTableKey']) ? getArrayValue($sub_table, 'ParentTableKey', $master['Prefix']) : $sub_table['ParentTableKey'];
if (!$foreign_key_field || !$parent_key_field) continue;
$query = 'SELECT '.$sub_table['IdField'].' FROM '.$sub_TableName.'
WHERE '.$foreign_key_field.' = '.$original_values[$parent_key_field];
$sub_ids = $this->Conn->GetCol($query);
$parent_key = $object->GetDBField(is_array($sub_table['ParentTableKey']) ? $sub_table['ParentTableKey'][$prefix] : $sub_table['ParentTableKey']);
$this->DeleteItems($sub_table['Prefix'], $special, $sub_ids, $sub_table, $parent_key);
}
}
}
}
function DoCopyLiveToTemp($master, $ids, $parent_prefix=null)
{
// when two tables refers the same table as sub-sub-table, and ForeignKey and ParentTableKey are arrays
// the table will be first copied by first sub-table, then dropped and copied over by last ForeignKey in the array
// this should not do any problems :)
if ( !preg_match("/.*\.[0-9]+/", $master['Prefix']) ) {
if( $this->DropTempTable($master['TableName']) )
{
$this->CreateTempTable($master['TableName']);
}
}
if (is_array($ids)) {
$ids = join(',', $ids);
}
$table_sig = $master['TableName'].(isset($master['Constrain']) ? $master['Constrain'] : '');
if ($ids != '' && !in_array($table_sig, $this->CopiedTables)) {
if ( getArrayValue($master, 'ForeignKey') ) {
if ( is_array($master['ForeignKey']) ) {
$key_field = $master['ForeignKey'][$parent_prefix];
}
else {
$key_field = $master['ForeignKey'];
}
}
else {
$key_field = $master['IdField'];
}
$query = 'INSERT INTO '.$this->GetTempName($master['TableName']).'
SELECT * FROM '.$master['TableName'].'
WHERE '.$key_field.' IN ('.$ids.')';
if (isset($master['Constrain'])) $query .= ' AND '.$master['Constrain'];
$this->Conn->Query($query);
$this->CopiedTables[] = $table_sig;
$query = 'SELECT '.$master['IdField'].' FROM '.$master['TableName'].'
WHERE '.$key_field.' IN ('.$ids.')';
if (isset($master['Constrain'])) $query .= ' AND '.$master['Constrain'];
$this->RaiseEvent( 'OnAfterCopyToTemp', $master['Prefix'], '', $this->Conn->GetCol($query) );
}
if ( getArrayValue($master, 'SubTables') ) {
foreach ($master['SubTables'] as $sub_table) {
$parent_key = is_array($sub_table['ParentTableKey']) ? $sub_table['ParentTableKey'][$master['Prefix']] : $sub_table['ParentTableKey'];
if (!$parent_key) continue;
if ( $ids != '' && $parent_key != $key_field ) {
$query = 'SELECT '.$parent_key.' FROM '.$master['TableName'].'
WHERE '.$key_field.' IN ('.$ids.')';
$sub_foreign_keys = join(',', $this->Conn->GetCol($query));
}
else {
$sub_foreign_keys = $ids;
}
$this->DoCopyLiveToTemp($sub_table, $sub_foreign_keys, $master['Prefix']);
}
}
}
function GetForeignKeys($master, $sub_table, $live_id, $temp_id=null)
{
$mode = 1; //multi
if (!is_array($live_id)) {
$live_id = Array($live_id);
$mode = 2; //single
}
if (isset($temp_id) && !is_array($temp_id)) $temp_id = Array($temp_id);
if ( isset($sub_table['ParentTableKey']) ) {
if ( is_array($sub_table['ParentTableKey']) ) {
$parent_key_field = $sub_table['ParentTableKey'][$master['Prefix']];
}
else {
$parent_key_field = $sub_table['ParentTableKey'];
}
}
else {
$parent_key_field = $master['IdField'];
}
if ( $cached = getArrayValue($this->FKeysCache, $master['TableName'].'.'.$parent_key_field) ) {
if ( array_key_exists(serialize($live_id), $cached) ) {
list($live_foreign_key, $temp_foreign_key) = $cached[serialize($live_id)];
if ($mode == 1) {
return $live_foreign_key;
}
else {
return Array($live_foreign_key[0], $temp_foreign_key[0]);
}
}
}
if ($parent_key_field != $master['IdField']) {
$query = 'SELECT '.$parent_key_field.' FROM '.$master['TableName'].'
WHERE '.$master['IdField'].' IN ('.join(',', $live_id).')';
$live_foreign_key = $this->Conn->GetCol($query);
if (isset($temp_id)) {
// because DoCopyTempToOriginal resets negative IDs to 0 in temp table (one by one) before copying to live
$temp_key = $temp_id < 0 ? 0 : $temp_id;
$query = 'SELECT '.$parent_key_field.' FROM '.$this->GetTempName($master['TableName']).'
WHERE '.$master['IdField'].' IN ('.join(',', $temp_key).')';
$temp_foreign_key = $this->Conn->GetCol($query);
}
else {
$temp_foreign_key = Array();
}
}
else {
$live_foreign_key = $live_id;
$temp_foreign_key = $temp_id;
}
$this->FKeysCache[$master['TableName'].'.'.$parent_key_field][serialize($live_id)] = Array($live_foreign_key, $temp_foreign_key);
if ($mode == 1) {
return $live_foreign_key;
}
else {
return Array($live_foreign_key[0], $temp_foreign_key[0]);
}
}
- function DoCopyTempToOriginal($master, $parent_prefix = null, $current_ids = Array())
+ /**
+ * Copies data from temp to live table and returns IDs of copied records
+ *
+ * @param Array $master
+ * @param string $parent_prefix
+ * @param Array $current_ids
+ * @return Array
+ * @access public
+ */
+ public function DoCopyTempToOriginal($master, $parent_prefix = null, $current_ids = Array())
{
- if (!$current_ids) {
- $query = 'SELECT '.$master['IdField'].' FROM '.$this->GetTempName($master['TableName']);
- if (isset($master['Constrain'])) $query .= ' WHERE '.$master['Constrain'];
+ if ( !$current_ids ) {
+ $query = 'SELECT ' . $master['IdField'] . ' FROM ' . $this->GetTempName($master['TableName']);
+
+ if ( isset($master['Constrain']) ) {
+ $query .= ' WHERE ' . $master['Constrain'];
+ }
+
$current_ids = $this->Conn->GetCol($query);
}
- $table_sig = $master['TableName'].(isset($master['Constrain']) ? $master['Constrain'] : '');
+ $table_sig = $master['TableName'] . (isset($master['Constrain']) ? $master['Constrain'] : '');
if ($current_ids) {
// delete all ids from live table - for MasterTable ONLY!
// because items from Sub Tables get deteleted in CopySubTablesToLive !BY ForeignKey!
- if ($master['TableName'] == $this->MasterTable) {
- $this->RaiseEvent( 'OnBeforeDeleteFromLive', $master['Prefix'], '', $current_ids );
+ if ( $master['TableName'] == $this->MasterTable ) {
+ $this->RaiseEvent('OnBeforeDeleteFromLive', $master['Prefix'], '', $current_ids);
- $query = 'DELETE FROM '.$master['TableName'].' WHERE '.$master['IdField'].' IN ('.join(',', $current_ids).')';
+ $query = 'DELETE FROM ' . $master['TableName'] . ' WHERE ' . $master['IdField'] . ' IN (' . join(',', $current_ids) . ')';
$this->Conn->Query($query);
}
if ( getArrayValue($master, 'SubTables') ) {
- if( in_array($table_sig, $this->CopiedTables) || $this->FinalRefs[$table_sig] != $master['TableId'] ) return;
+ if ( in_array($table_sig, $this->CopiedTables) || $this->FinalRefs[$table_sig] != $master['TableId'] ) {
+ return Array ();
+ }
- foreach($current_ids AS $id)
- {
- $this->RaiseEvent( 'OnBeforeCopyToLive', $master['Prefix'], '', Array($id) );
+ foreach ($current_ids AS $id) {
+ $this->RaiseEvent('OnBeforeCopyToLive', $master['Prefix'], '', Array ($id));
//reset negative ids to 0, so autoincrement in live table works fine
- if($id < 0)
- {
- $query = 'UPDATE '.$this->GetTempName($master['TableName']).'
- SET '.$master['IdField'].' = 0
- WHERE '.$master['IdField'].' = '.$id;
- if (isset($master['Constrain'])) $query .= ' AND '.$master['Constrain'];
+ if ( $id < 0 ) {
+ $query = ' UPDATE ' . $this->GetTempName($master['TableName']) . '
+ SET ' . $master['IdField'] . ' = 0
+ WHERE ' . $master['IdField'] . ' = ' . $id;
+
+ if ( isset($master['Constrain']) ) {
+ $query .= ' AND ' . $master['Constrain'];
+ }
+
$this->Conn->Query($query);
$id_to_copy = 0;
}
- else
- {
+ else {
$id_to_copy = $id;
}
//copy current id_to_copy (0 for new or real id) to live table
- $query = 'INSERT INTO '.$master['TableName'].'
- SELECT * FROM '.$this->GetTempName($master['TableName']).'
- WHERE '.$master['IdField'].' = '.$id_to_copy;
+ $query = ' INSERT INTO ' . $master['TableName'] . '
+ SELECT * FROM ' . $this->GetTempName($master['TableName']) . '
+ WHERE ' . $master['IdField'] . ' = ' . $id_to_copy;
$this->Conn->Query($query);
+
$insert_id = $id_to_copy == 0 ? $this->Conn->getInsertID() : $id_to_copy;
- $this->saveID($master['Prefix'], '', array($id => $insert_id));
- $this->RaiseEvent( 'OnAfterCopyToLive', $master['Prefix'], '', Array($insert_id), null, array('temp_id' => $id) );
+ $this->saveID($master['Prefix'], '', array ($id => $insert_id));
+ $this->RaiseEvent('OnAfterCopyToLive', $master['Prefix'], '', Array ($insert_id), null, Array ('temp_id' => $id));
$this->UpdateForeignKeys($master, $insert_id, $id);
//delete already copied record from master temp table
- $query = 'DELETE FROM '.$this->GetTempName($master['TableName']).'
- WHERE '.$master['IdField'].' = '.$id_to_copy;
- if (isset($master['Constrain'])) $query .= ' AND '.$master['Constrain'];
+ $query = ' DELETE FROM ' . $this->GetTempName($master['TableName']) . '
+ WHERE ' . $master['IdField'] . ' = ' . $id_to_copy;
+
+ if ( isset($master['Constrain']) ) {
+ $query .= ' AND ' . $master['Constrain'];
+ }
+
$this->Conn->Query($query);
}
$this->CopiedTables[] = $table_sig;
// when all of ids in current master has been processed, copy all sub-tables data
$this->CopySubTablesToLive($master, $current_ids);
}
- elseif( !in_array($table_sig, $this->CopiedTables) && ($this->FinalRefs[$table_sig] == $master['TableId']) ) { //If current master doesn't have sub-tables - we could use mass operations
- // We don't need to delete items from live here, as it get deleted in the beggining of the method for MasterTable
+ elseif ( !in_array($table_sig, $this->CopiedTables) && ($this->FinalRefs[$table_sig] == $master['TableId']) ) { //If current master doesn't have sub-tables - we could use mass operations
+ // We don't need to delete items from live here, as it get deleted in the beginning of the method for MasterTable
// or in parent table processing for sub-tables
+ $live_ids = Array ();
$this->RaiseEvent('OnBeforeCopyToLive', $master['Prefix'], '', $current_ids);
- $live_ids = array();
foreach ($current_ids as $an_id) {
- if ($an_id > 0) {
+ if ( $an_id > 0 ) {
$live_ids[$an_id] = $an_id;
// positive (already live) IDs will be copied in on query all togather below,
// so we just store it here
continue;
}
- else { // zero or negaitve ids should be copied one by one to get their InsertId
- // reseting to 0 so it get inserted into live table with autoincrement
- $query = 'UPDATE '.$this->GetTempName($master['TableName']).'
- SET '.$master['IdField'].' = 0
- WHERE '.$master['IdField'].' = '.$an_id;
+ else { // zero or negative ids should be copied one by one to get their InsertId
+ // resetting to 0 so it get inserted into live table with autoincrement
+ $query = ' UPDATE ' . $this->GetTempName($master['TableName']) . '
+ SET ' . $master['IdField'] . ' = 0
+ WHERE ' . $master['IdField'] . ' = ' . $an_id;
// constrain is not needed here because ID is already unique
$this->Conn->Query($query);
// copying
- $query = 'INSERT INTO '.$master['TableName'].'
- SELECT * FROM '.$this->GetTempName($master['TableName']).'
- WHERE '.$master['IdField'].' = 0';
+ $query = ' INSERT INTO ' . $master['TableName'] . '
+ SELECT * FROM ' . $this->GetTempName($master['TableName']) . '
+ WHERE ' . $master['IdField'] . ' = 0';
$this->Conn->Query($query);
+
$live_ids[$an_id] = $this->Conn->getInsertID(); //storing newly created live id
//delete already copied record from master temp table
- $query = 'DELETE FROM '.$this->GetTempName($master['TableName']).'
- WHERE '.$master['IdField'].' = 0';
+ $query = ' DELETE FROM ' . $this->GetTempName($master['TableName']) . '
+ WHERE ' . $master['IdField'] . ' = 0';
$this->Conn->Query($query);
$this->UpdateChangeLogForeignKeys($master, $live_ids[$an_id], $an_id);
}
}
// copy ALL records to live table
- $query = 'INSERT INTO '.$master['TableName'].'
- SELECT * FROM '.$this->GetTempName($master['TableName']);
- if (isset($master['Constrain'])) $query .= ' WHERE '.$master['Constrain'];
+ $query = ' INSERT INTO ' . $master['TableName'] . '
+ SELECT * FROM ' . $this->GetTempName($master['TableName']);
+
+ if ( isset($master['Constrain']) ) {
+ $query .= ' WHERE ' . $master['Constrain'];
+ }
+
$this->Conn->Query($query);
$this->CopiedTables[] = $table_sig;
$this->RaiseEvent('OnAfterCopyToLive', $master['Prefix'], '', $live_ids);
$this->saveID($master['Prefix'], '', $live_ids);
-
// no need to clear temp table - it will be dropped by next statement
}
}
- if ($this->FinalRefs[ $master['TableName'] ] != $master['TableId']) return;
+ if ( $this->FinalRefs[ $master['TableName'] ] != $master['TableId'] ) {
+ return Array ();
+ }
/*if ( is_array(getArrayValue($master, 'ForeignKey')) ) { //if multiple ForeignKeys
if ( $master['ForeignKey'][$parent_prefix] != end($master['ForeignKey']) ) {
return; // Do not delete temp table if not all ForeignKeys have been processed (current is not the last)
}
}*/
+
$this->DropTempTable($master['TableName']);
$this->Application->resetCounters($master['TableName']);
- if (!isset($this->savedIDs[ $master['Prefix'] ])) {
- $this->savedIDs[ $master['Prefix'] ] = Array();
+ if ( !isset($this->savedIDs[ $master['Prefix'] ]) ) {
+ $this->savedIDs[ $master['Prefix'] ] = Array ();
}
return $this->savedIDs[ $master['Prefix'] ];
}
/**
* Create separate connection for locking purposes
*
* @return kDBConnection
*/
function &_getSeparateConnection()
{
static $connection = null;
if (!isset($connection)) {
$connection =& $this->Application->makeClass( 'kDBConnection', Array (SQL_TYPE, Array (&$this->Application, 'handleSQLError')) );
+ /* @var $connection kDBConnection */
+
$connection->debugMode = $this->Application->isDebugMode();
$connection->Connect(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB, true);
}
return $connection;
}
function UpdateChangeLogForeignKeys($master, $live_id, $temp_id)
{
if ($live_id == $temp_id) {
return ;
}
$prefix = $master['Prefix'];
$main_prefix = $this->Application->GetTopmostPrefix($prefix);
$ses_var_name = $main_prefix . '_changes_' . $this->Application->GetTopmostWid($this->Prefix);
$changes = $this->Application->RecallVar($ses_var_name);
$changes = $changes ? unserialize($changes) : Array ();
foreach ($changes as $key => $rec) {
if ($rec['Prefix'] == $prefix && $rec['ItemId'] == $temp_id) {
// main item change log record
$changes[$key]['ItemId'] = $live_id;
}
if ($rec['MasterPrefix'] == $prefix && $rec['MasterId'] == $temp_id) {
// sub item change log record
$changes[$key]['MasterId'] = $live_id;
}
if (in_array($prefix, $rec['ParentPrefix']) && $rec['ParentId'][$prefix] == $temp_id) {
// parent item change log record
$changes[$key]['ParentId'][$prefix] = $live_id;
if (array_key_exists('DependentFields', $rec)) {
// these are fields from table of $rec['Prefix'] table!
// when one of dependent fields goes into idfield of it's parent item, that was changed
$parent_table_key = $this->Application->getUnitOption($rec['Prefix'], 'ParentTableKey');
$parent_table_key = is_array($parent_table_key) ? $parent_table_key[$prefix] : $parent_table_key;
if ($parent_table_key == $master['IdField']) {
$foreign_key = $this->Application->getUnitOption($rec['Prefix'], 'ForeignKey');
$foreign_key = is_array($foreign_key) ? $foreign_key[$prefix] : $foreign_key;
$changes[$key]['DependentFields'][$foreign_key] = $live_id;
}
}
}
}
$this->Application->StoreVar($ses_var_name, serialize($changes));
}
function UpdateForeignKeys($master, $live_id, $temp_id)
{
$this->UpdateChangeLogForeignKeys($master, $live_id, $temp_id);
foreach ($master['SubTables'] as $sub_table) {
$foreign_key_field = is_array($sub_table['ForeignKey']) ? getArrayValue($sub_table, 'ForeignKey', $master['Prefix']) : $sub_table['ForeignKey'];
if (!$foreign_key_field) {
continue;
}
list ($live_foreign_key, $temp_foreign_key) = $this->GetForeignKeys($master, $sub_table, $live_id, $temp_id);
//Update ForeignKey in sub TEMP table
if ($live_foreign_key != $temp_foreign_key) {
$query = 'UPDATE '.$this->GetTempName($sub_table['TableName']).'
SET '.$foreign_key_field.' = '.$live_foreign_key.'
WHERE '.$foreign_key_field.' = '.$temp_foreign_key;
if (isset($sub_table['Constrain'])) $query .= ' AND '.$sub_table['Constrain'];
$this->Conn->Query($query);
}
}
}
function CopySubTablesToLive($master, $current_ids) {
foreach ($master['SubTables'] as $sub_table) {
$table_sig = $sub_table['TableName'].(isset($sub_table['Constrain']) ? $sub_table['Constrain'] : '');
// delete records from live table by foreign key, so that records deleted from temp table
// get deleted from live
if (count($current_ids) > 0 && !in_array($table_sig, $this->CopiedTables) ) {
$foreign_key_field = is_array($sub_table['ForeignKey']) ? getArrayValue($sub_table, 'ForeignKey', $master['Prefix']) : $sub_table['ForeignKey'];
if (!$foreign_key_field) continue;
$foreign_keys = $this->GetForeignKeys($master, $sub_table, $current_ids);
if (count($foreign_keys) > 0) {
$query = 'SELECT '.$sub_table['IdField'].' FROM '.$sub_table['TableName'].'
WHERE '.$foreign_key_field.' IN ('.join(',', $foreign_keys).')';
if (isset($sub_table['Constrain'])) $query .= ' AND '.$sub_table['Constrain'];
if ( $this->RaiseEvent( 'OnBeforeDeleteFromLive', $sub_table['Prefix'], '', $this->Conn->GetCol($query), $foreign_keys ) ){
$query = 'DELETE FROM '.$sub_table['TableName'].'
WHERE '.$foreign_key_field.' IN ('.join(',', $foreign_keys).')';
if (isset($sub_table['Constrain'])) $query .= ' AND '.$sub_table['Constrain'];
$this->Conn->Query($query);
}
}
}
//sub_table passed here becomes master in the method, and recursively updated and copy its sub tables
$this->DoCopyTempToOriginal($sub_table, $master['Prefix']);
}
}
- function RaiseEvent($name, $prefix, $special, $ids, $foreign_key = null, $add_params = null)
+ /**
+ * Raises event using IDs, that are currently being processed in temp handler
+ *
+ * @param string $name
+ * @param string $prefix
+ * @param string $special
+ * @param Array $ids
+ * @param string $foreign_key
+ * @param Array $add_params
+ * @return bool
+ * @access protected
+ */
+ protected function RaiseEvent($name, $prefix, $special, $ids, $foreign_key = null, $add_params = null)
{
- if ( !is_array($ids) ) return ;
+ if ( !is_array($ids) ) {
+ return true;
+ }
- $event_key = $prefix.($special ? '.' : '').$special.':'.$name;
+ $event_key = $prefix . ($special ? '.' : '') . $special . ':' . $name;
$event = new kEvent($event_key);
- if (isset($foreign_key)) {
+ if ( isset($foreign_key) ) {
$event->setEventParam('foreign_key', $foreign_key);
}
$set_temp_id = ($name == 'OnAfterCopyToLive') && (!is_array($add_params) || !array_key_exists('temp_id', $add_params));
- foreach($ids as $index => $id) {
+ foreach ($ids as $index => $id) {
$event->setEventParam('id', $id);
- if ($set_temp_id) {
+ if ( $set_temp_id ) {
$event->setEventParam('temp_id', $index);
}
- if (is_array($add_params)) {
+ if ( is_array($add_params) ) {
foreach ($add_params as $name => $val) {
$event->setEventParam($name, $val);
}
}
$this->Application->HandleEvent($event);
}
return $event->status == kEvent::erSUCCESS;
}
function DropTempTable($table)
{
if( in_array($table, $this->DroppedTables) ) return false;
$query = sprintf("DROP TABLE IF EXISTS %s",
$this->GetTempName($table)
);
array_push($this->DroppedTables, $table);
$this->DroppedTables = array_unique($this->DroppedTables);
$this->Conn->Query($query);
return true;
}
function PrepareEdit()
{
$this->DoCopyLiveToTemp($this->Tables, $this->Tables['IDs']);
if ($this->Application->getUnitOption($this->Tables['Prefix'],'CheckSimulatniousEdit')) {
$this->CheckSimultaniousEdit();
}
}
function SaveEdit($master_ids = Array())
{
// SessionKey field is required for deleting records from expired sessions
$conn =& $this->_getSeparateConnection();
$sleep_count = 0;
do {
- // aquire lock
+ // acquire lock
$conn->ChangeQuery('LOCK TABLES '.TABLE_PREFIX.'Semaphores WRITE');
$sql = 'SELECT SessionKey
FROM ' . TABLE_PREFIX . 'Semaphores
WHERE (MainPrefix = ' . $conn->qstr($this->Tables['Prefix']) . ')';
$another_coping_active = $conn->GetOne($sql);
if ($another_coping_active) {
// another user is coping data from temp table to live -> release lock and try again after 1 second
$conn->ChangeQuery('UNLOCK TABLES');
$sleep_count++;
sleep(1);
}
} while ($another_coping_active && ($sleep_count <= 30));
if ($sleep_count > 30) {
// another coping process failed to finished in 30 seconds
$error_message = $this->Application->Phrase('la_error_TemporaryTableCopyingFailed');
$this->Application->SetVar('_temp_table_message', $error_message);
return false;
}
// mark, that we are coping from temp to live right now, so other similar attempt (from another script) will fail
$fields_hash = Array (
'SessionKey' => $this->Application->GetSID(),
'Timestamp' => adodb_mktime(),
'MainPrefix' => $this->Tables['Prefix'],
);
$conn->doInsert($fields_hash, TABLE_PREFIX.'Semaphores');
$semaphore_id = $conn->getInsertID();
// unlock table now to prevent permanent lock in case, when coping will end with SQL error in the middle
$conn->ChangeQuery('UNLOCK TABLES');
$ids = $this->DoCopyTempToOriginal($this->Tables, null, $master_ids);
// remove mark, that we are coping from temp to live
$conn->Query('LOCK TABLES '.TABLE_PREFIX.'Semaphores WRITE');
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'Semaphores
WHERE SemaphoreId = ' . $semaphore_id;
$conn->ChangeQuery($sql);
$conn->ChangeQuery('UNLOCK TABLES');
return $ids;
}
function CancelEdit($master=null)
{
if (!isset($master)) $master = $this->Tables;
$this->DropTempTable($master['TableName']);
if ( getArrayValue($master, 'SubTables') ) {
foreach ($master['SubTables'] as $sub_table) {
$this->CancelEdit($sub_table);
}
}
}
/**
* Checks, that someone is editing selected records and returns true, when no one.
*
* @param Array $ids
*
* @return bool
*/
function CheckSimultaniousEdit($ids = null)
{
$tables = $this->Conn->GetCol('SHOW TABLES');
$mask_edit_table = '/' . TABLE_PREFIX . 'ses_(.*)_edit_' . $this->MasterTable . '$/';
$my_sid = $this->Application->GetSID();
$my_wid = $this->Application->GetVar('m_wid');
$ids = implode(',', isset($ids) ? $ids : $this->Tables['IDs']);
$sids = Array ();
if (!$ids) {
return true;
}
foreach ($tables as $table) {
if ( preg_match($mask_edit_table, $table, $rets) ) {
$sid = preg_replace('/(.*)_(.*)/', '\\1', $rets[1]); // remove popup's wid from sid
if ($sid == $my_sid) {
if ($my_wid) {
// using popups for editing
if (preg_replace('/(.*)_(.*)/', '\\2', $rets[1]) == $my_wid) {
// don't count window, that is being opened right now
continue;
}
}
else {
// not using popups for editing -> don't count my session tables
continue;
}
}
$sql = 'SELECT COUNT(' . $this->Tables['IdField'] . ')
FROM ' . $table . '
WHERE ' . $this->Tables['IdField'] . ' IN (' . $ids . ')';
$found = $this->Conn->GetOne($sql);
if (!$found || in_array($sid, $sids)) {
continue;
}
$sids[] = $sid;
}
}
if ($sids) {
// detect who is it
$sql = 'SELECT
CONCAT(IF (s.PortalUserId = ' . USER_ROOT . ', \'root\',
IF (s.PortalUserId = ' . USER_GUEST . ', \'Guest\',
CONCAT(FirstName, \' \', LastName, \' (\', Login, \')\')
)
), \' IP: \', s.IpAddress, \'\') FROM ' . TABLE_PREFIX . 'UserSession AS s
LEFT JOIN ' . TABLE_PREFIX . 'PortalUser AS u
ON u.PortalUserId = s.PortalUserId
WHERE s.SessionKey IN (' . implode(',', $sids) . ')';
$users = $this->Conn->GetCol($sql);
if ($users) {
$this->Application->SetVar('_simultanious_edit_message',
sprintf($this->Application->Phrase('la_record_being_edited_by'), join(",\n", $users))
);
return false;
}
}
return true;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/http_query.php
===================================================================
--- branches/5.2.x/core/kernel/utility/http_query.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/http_query.php (revision 14628)
@@ -1,844 +1,846 @@
<?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 kHTTPQuery extends Params {
/**
* Cache of QueryString parameters
* from config, that are represented
* in enviroment variable
*
* @var Array
*/
protected $discoveredUnits = Array ();
/**
* $_POST vars
*
* @var Array
* @access private
*/
var $Post;
/**
* $_GET vars
*
* @var Array
* @access private
*/
var $Get;
/**
* $_COOKIE vars
*
* @var Array
* @access private
*/
var $Cookie;
/**
* $_SERVER vars
*
* @var Array
* @access private
*/
var $Server;
/**
* $_ENV vars
*
* @var Array
* @access private
*/
var $Env;
/**
* Order in what write
* all vars together in
* the same array
*
* @var string
*/
var $Order;
/**
* Uploaded files info
*
* @var Array
* @access private
*/
var $Files;
var $specialsToRemove = Array();
/**
* SessionID is given via "sid" variable in query string
*
* @var bool
*/
var $_sidInQueryString = false;
/**
* Loads info from $_POST, $_GET and
* related arrays into common place
*
* @param string $order
* @return HTTPQuery
* @access public
*/
public function __construct($order = 'CGPF')
{
parent::__construct();
$this->Order = $order;
if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
// when AJAX request is made from jQuery, then create ajax variable,
// so any logic based in it (like redirects) will not break down
$_GET['ajax'] = 'yes';
}
$this->AddAllVars();
$this->specialsToRemove = $this->Get('remove_specials');
if ($this->specialsToRemove) {
foreach ($this->specialsToRemove as $prefix_special => $flag) {
if ($flag && strpos($prefix_special, '.') === false) {
unset($this->specialsToRemove[$prefix_special]);
trigger_error('Incorrect usage of "<strong>remove_specials['.$prefix_special.']</strong>" field (no special found)', E_USER_NOTICE);
}
}
$this->_Params = $this->removeSpecials($this->_Params);
}
ini_set('magic_quotes_gpc', 0);
}
/**
* Discovers unit form request and returns it's QueryString option on success
*
* @param string $prefix_special
*
* @return Array|bool
* @access protected
*/
protected function discoverUnit($prefix_special)
{
list($prefix) = explode('.', $prefix_special);
$query_string = $this->getQueryString($prefix);
if ($query_string) {
// only units with QueryString option can be discovered
$this->discoveredUnits[$prefix_special] = $query_string;
return $query_string;
}
unset( $this->discoveredUnits[$prefix] );
return false;
}
/**
* Returns units, passed in request
*
* @param bool $prefix_special_only
* @return Array
* @access protected
*/
public function getDiscoveredUnits($prefix_special_only = true)
{
return $prefix_special_only ? array_keys( $this->discoveredUnits ) : $this->discoveredUnits;
}
/**
* Returns QueryMap for requested unit config.
* In case if unit config is a clone, then get parent item's (from prefix) config to create clone
*
* @param string $prefix
* @return Array
* @access protected
*/
protected function getQueryString($prefix)
{
$ret = $this->Application->getUnitOption($prefix, 'QueryString', Array ());
if ( !$ret && preg_match('/(.*?)-(.*)/', $prefix, $regs) ) {
// "#prefix" (new format), "prefix" (old format)
return $this->_getQueryString('#' . $regs[2]);
}
return $ret;
}
/**
* Returns query string (with safety check against missing prefixes)
*
* @param string $prefix
* @return Array
*/
private function _getQueryString($prefix)
{
if ( $this->Application->prefixRegistred($prefix) ) {
return $this->Application->getUnitOption($prefix, 'QueryString');
}
return substr($prefix, 0, 1) == '#' ? $this->_getQueryString( substr($prefix, 1) ) : Array ();
}
function removeSpecials($array)
{
$ret = Array();
$removed = false;
foreach ($this->specialsToRemove as $prefix_special => $flag) {
if ($flag) {
$removed = true;
list ($prefix,$special) = explode('.', $prefix_special, 2);
foreach ($array as $key => $val) {
$new_key = preg_match("/^".$prefix."[._]{1}".$special."(.*)/", $key, $regs) ? $prefix.$regs[1] : $key;
$ret[$new_key] = is_array($val) ? $this->removeSpecials($val) : $val;
}
}
}
return $removed ? $ret : $array;
}
/**
* All all requested vars to
* common storage place
*
* @access private
*/
function AddAllVars()
{
for ($i=0; $i < strlen($this->Order); $i++)
{
$current = $this->Order[$i];
switch ($current) {
case 'G':
$this->Get = $this->AddVars($_GET);
if (array_key_exists('sid', $_GET)) {
$this->_sidInQueryString = true;
}
$vars = $this->processQueryString( $this->Get(ENV_VAR_NAME) );
if (array_key_exists('sid', $vars)) {
// used by Session::GetPassedSIDValue
$this->Get['sid'] = $vars['sid'];
}
$this->AddParams($vars);
break;
case 'P':
$this->Post = $this->AddVars($_POST);
$this->convertPostEvents();
$this->_processPostEnvVariables();
break;
case 'C':
$this->Cookie = $this->AddVars($_COOKIE);
break;
case 'E';
$this->Env = $this->AddVars($_ENV);
break;
case 'S';
$this->Server = $this->AddVars($_SERVER);
break;
case 'F';
$this->convertFiles();
$this->Files = $this->MergeVars($_FILES, false); //do not strip slashes!
break;
}
}
// $this->AfterInit();
}
/**
* Allow POST variables, that names were transformed by PHP ("." replaced with "_") to
* override variables, that were virtually created through enviroment variable parsing
*
*/
function _processPostEnvVariables()
{
$passed = $this->Get('passed');
if (!$passed) {
return ;
}
$passed = explode(',', $passed);
foreach ($passed as $prefix_special) {
if (strpos($prefix_special, '.') === false) {
continue;
}
list ($prefix, $special) = explode('.', $prefix_special);
$query_map = $this->getQueryString($prefix);
$post_prefix_special = $prefix . '_' . $special;
foreach ($query_map as $index => $var_name) {
if (array_key_exists($post_prefix_special . '_' . $var_name, $this->Post)) {
$this->Set($prefix_special . '_' . $var_name, $this->Post[$post_prefix_special . '_' . $var_name]);
}
}
}
}
function AfterInit()
{
$rewrite_url = $this->Get('_mod_rw_url_');
if ($this->Application->RewriteURLs() || $rewrite_url) {
// maybe call onafterconfigread here
if (defined('DEBUG_MODE') && $this->Application->isDebugMode()) {
$this->Application->Debugger->profileStart('url_parsing', 'Parsing <b>MOD_REWRITE</b> url');
$this->processRewriteURL();
$description = 'Parsing <b>MOD_REWRITE</b> url (template: <b>' . $this->Get('t') . '</b>)';
$this->Application->Debugger->profileFinish('url_parsing', $description);
}
else {
$this->processRewriteURL();
}
if ( !$rewrite_url && $this->rewriteRedirectRequired() ) {
// rewrite url is missing (e.g. not a script from tools folder)
$url_params = $this->getRedirectParams();
// no idea about how to check, that given template require category to be passed with it, so pass anyway
$url_params['pass_category'] = 1;
$url_params['response_code'] = 301; // Moved Permanently
$this->Application->Redirect('', $url_params);
}
}
else {
$this->Application->VerifyThemeId();
$this->Application->VerifyLanguageId();
}
}
/**
* Checks, that non-rewrite url was visited and it's automatic rewrite is required
*
* @return bool
*/
function rewriteRedirectRequired()
{
$redirect_conditions = Array (
!$this->Application->Session->IsHTTPSRedirect(), // not https <-> http redirect
!$this->refererIsOurSite(), // referer doesn't match ssl path or non-ssl domain (same for site domains)
!defined('GW_NOTIFY'), // not in payment gateway notification script
preg_match('/[\/]{0,1}index.php[\/]{0,1}/', $_SERVER['PHP_SELF']), // "index.php" was visited
$this->Get('t') != 'index', // not on index page
);
$perform_redirect = true;
foreach ($redirect_conditions as $redirect_condition) {
$perform_redirect = $perform_redirect && $redirect_condition;
if (!$perform_redirect) {
return false;
}
}
return true;
}
/**
* Checks, that referer is out site
*
* @return bool
*/
function refererIsOurSite()
{
if ( !array_key_exists('HTTP_REFERER', $_SERVER) ) {
// no referer -> don't care what happens
return false;
}
$site_helper =& $this->Application->recallObject('SiteHelper');
/* @var $site_helper SiteHelper */
$found = false;
$http_referer = $_SERVER['HTTP_REFERER'];
preg_match('/^(.*?):\/\/(.*?)(\/|$)/', $http_referer, $regs); // 1 - protocol, 2 - domain
if ($regs[1] == 'https') {
$found = $site_helper->getDomainByName('SSLUrl', $http_referer) > 0;
if (!$found) {
// check if referer starts with our ssl url
$ssl_url = $this->Application->ConfigValue('SSL_URL');
$found = $ssl_url && preg_match('/^' . preg_quote($ssl_url, '/') . '/', $http_referer);
}
}
else {
$found = $site_helper->getDomainByName('DomainName', $regs[2]) > 0;
if (!$found) {
$found = $regs[2] == DOMAIN;
}
}
return $found;
}
function convertFiles()
{
- if (!$_FILES)
- {
- return false;
+ if ( !$_FILES ) {
+ return ;
}
- $file_keys = Array('error','name','size','tmp_name','type');
+ $tmp = Array ();
+ $file_keys = Array ('error', 'name', 'size', 'tmp_name', 'type');
- $tmp = Array();
- foreach($_FILES as $file_name => $file_info)
- {
- if( is_array($file_info['error']) )
- {
- $tmp[$file_name] = $this->getArrayLevel( $file_info['error'], $file_name );
+ foreach ($_FILES as $file_name => $file_info) {
+ if ( is_array($file_info['error']) ) {
+ $tmp[$file_name] = $this->getArrayLevel($file_info['error'], $file_name);
}
- else
- {
+ else {
$normal_files[$file_name] = $file_info;
}
}
- if(!$tmp) return false;
+ if ( !$tmp ) {
+ return ;
+ }
$files = $_FILES;
- $_FILES = Array();
+ $_FILES = Array ();
- foreach($tmp as $prefix => $prefix_files)
- {
+ foreach ($tmp as $prefix => $prefix_files) {
$anchor =& $_FILES;
- foreach($prefix_files['keys'] as $key)
- {
+ foreach ($prefix_files['keys'] as $key) {
$anchor =& $anchor[$key];
}
- foreach($prefix_files['value'] as $field_name)
- {
- unset($inner_anchor);
- unset($copy);
+
+ foreach ($prefix_files['value'] as $field_name) {
+ unset($inner_anchor, $copy);
$work_copy = $prefix_files['keys'];
- foreach($file_keys as $file_key)
- {
+
+ foreach ($file_keys as $file_key) {
$inner_anchor =& $files[$prefix][$file_key];
- if (isset($copy))
- {
+
+ if ( isset($copy) ) {
$work_copy = $copy;
}
- else
- {
+ else {
$copy = $work_copy;
}
+
array_shift($work_copy);
- foreach($work_copy as $prefix_file_key)
- {
+ foreach ($work_copy as $prefix_file_key) {
$inner_anchor =& $inner_anchor[$prefix_file_key];
}
+
$anchor[$field_name][$file_key] = $inner_anchor[$field_name];
}
}
}
-
// keys: img_temp, 0, values: LocalPath, ThumbPath
}
function getArrayLevel(&$level, $prefix='')
{
$ret['keys'] = $prefix ? Array($prefix) : Array();
$ret['value'] = Array();
foreach($level as $level_key => $level_value)
{
if( is_array($level_value) )
{
$ret['keys'][] = $level_key;
$tmp = $this->getArrayLevel($level_value);
$ret['keys'] = array_merge($ret['keys'], $tmp['keys']);
$ret['value'] = array_merge($ret['value'], $tmp['value']);
}
else
{
$ret['value'][] = $level_key;
}
}
return $ret;
}
/**
- * Owerwrites GET events with POST events in case if they are set and not empty
+ * Overwrites GET events with POST events in case if they are set and not empty
*
+ * @return void
+ * @access protected
*/
- function convertPostEvents()
+ protected function convertPostEvents()
{
- $events = $this->Get('events');
- if (is_array($events)) {
+ $events = $this->Get('events', Array ());
+ /* @var $events Array */
+
+ if ( is_array($events) ) {
+ $events = array_filter($events);
+
foreach ($events as $prefix_special => $event_name) {
- if ($event_name) {
- $this->Set($prefix_special.'_event', $event_name);
- }
+ $this->Set($prefix_special . '_event', $event_name);
}
}
}
function finalizeParsing($passed = Array(), $module_params = Array() )
{
if ($passed) {
foreach ($passed as $passed_prefix) {
$this->discoverUnit($passed_prefix); // from mod-rewrite url parsing
}
$this->Set('passed', implode(',', $this->getDiscoveredUnits()));
}
// get joined version (env var + mod rewrite parsed)
$passed = $this->Application->GetVar('passed');
if (!array_key_exists('editing_mode', $module_params)) {
$module_params['editing_mode'] = '';
}
$module_params['__URLENCODE__'] = 1;
$env = $this->Application->BuildEnv( $this->Get('t'), $module_params, $passed, false, false);
$this->Set(ENV_VAR_NAME, $env);
$_REQUEST['env'] = $_GET['env'] = $env; // for capability with old in-portal code
}
function processRewriteURL()
{
$mod_rw_helper =& $this->Application->recallObject('ModRewriteHelper');
+ /* @var $mod_rw_helper kModRewriteHelper */
+
$mod_rw_helper->processRewriteURL();
}
function getDefaultTemplate($t)
{
$t = $this->getTemplateName( trim($t, '/') );
if (!$t) $t = 'index';
return trim($t, '/');
}
function extractSIDAndTemplate(&$parts)
{
$vars = Array ();
$sub_parts = array_shift($parts);
list ($sid, $t) = explode('-', $sub_parts, 2);
if ($sid) {
// Save Session ID
$this->Set('sid', $sid);
$vars['sid'] = $sid;
}
// Save Template Name
$vars['t'] = $this->getDefaultTemplate($t);
return $vars;
}
/**
* Process QueryString only, create
* events, ids, based on config
* set template name and sid in
* desired application variables.
*
* @param string $env_var enviroment string value
*
* @access public
*/
function processQueryString($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
$vars = Array ();
if ($env_var) {
$more_vars = strpos($env_var, '&');
if ($more_vars !== false) {
parse_str(substr($env_var, $more_vars + 1), $vars);
$env_var = substr($env_var, 0, $more_vars);
}
// 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->Get('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));
}
}
else {
$t = $this->getTemplateName('index');
$vars['t'] = $t;
}
return $vars;
}
/**
* Converts enviroment part into variable array (based on query map for given prefix)
*
* @param string $mixed_part
* @return Array
*/
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);
}
$prefix_special = array_shift($mixed_part); // l.pick, l
$query_map = $this->discoverUnit($prefix_special); // from $_GET['env']
$vars = Array ();
// 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);
}
/**
* Removes tpl part from template name + resolved template ID to name
*
* @param string $t
* @return string
* @access private
*/
function getTemplateName($t)
{
if (array_key_exists('t', $this->Get) && $this->Get['t']) {
// template name is passed directly in url (GET method)
$t = $this->Get['t'];
}
// if t was set through env, even in mod_rewrite mode!
if ($this->Get('env') && $this->Application->RewriteURLs() && $this->Get('t')) {
$t = $this->Get('t');
}
return preg_replace('/\.tpl$/', '', $t);
}
/**
* Saves variables from array specified
* into common variable storage place
*
* @param Array $array
* @return Array
* @access private
*/
function AddVars($array)
{
$array = $this->StripSlashes($array);
foreach($array as $key => $value)
{
$this->Set($key,$value);
}
return $array;
}
function MergeVars($array, $strip_slashes = true)
{
if ($strip_slashes) {
$array = $this->StripSlashes($array);
}
foreach($array as $key => $value_array) {
// $value_array is an array too
$this->_Params = kUtil::array_merge_recursive($this->_Params, Array ($key => $value_array));
}
return $array;
}
function StripSlashes($array)
{
static $magic_quotes = null;
if (!isset($magic_quotes)) {
$magic_quotes = get_magic_quotes_gpc();
}
foreach ($array as $key => $value) {
if (is_array($value)) {
$array[$key] = $this->StripSlashes($value);
}
else {
if ($magic_quotes) {
$value = stripslashes($value);
}
if (!$this->Application->isAdmin) {
$value = htmlspecialchars($value);
}
$array[$key] = $value;
}
}
return $array;
}
/**
* Returns all $_GET array excluding system parameters, that are not allowed to be passed through generated urls
*
* @param bool $access_error Method is called during no_permission, require login, session expiration link preparation
* @return Array
*/
function getRedirectParams($access_error = false)
{
$vars = $this->Get;
$unset_vars = Array (ENV_VAR_NAME, 'rewrite', '_mod_rw_url_', 'Action');
if (!$this->_sidInQueryString) {
$unset_vars[] = 'sid';
}
// remove system variables
foreach ($unset_vars as $var_name) {
if (array_key_exists($var_name, $vars)) {
unset($vars[$var_name]);
}
}
if ($access_error) {
// place 1 of 2 (also in UsersEventHandler::OnSessionExpire)
$vars = $this->_removePassThroughVariables($vars);
}
// transform arrays
return $this->_transformArrays($vars);
}
/**
* Removes all pass_though variables from redirect params
*
* @param Array $url_params
* @return Array
*/
function _removePassThroughVariables($url_params)
{
$pass_through = array_key_exists('pass_through', $url_params) ? $url_params['pass_through'] : '';
if (!$pass_through) {
return $url_params;
}
$pass_through = explode(',', $pass_through . ',pass_through');
foreach ($pass_through as $pass_through_var) {
unset($url_params[$pass_through_var]);
}
$url_params['no_pass_through'] = 1; // this way kApplication::HREF won't add them again
return $url_params;
}
function _transformArrays($array, $level_prefix = '')
{
$ret = Array ();
foreach ($array as $var_name => $var_value) {
$new_var_name = $level_prefix ? $level_prefix . '[' . $var_name . ']' : $var_name;
if (is_array($var_value)) {
$ret = array_merge($ret, $this->_transformArrays($var_value, $new_var_name));
}
else {
$ret[$new_var_name] = $var_value;
}
}
return $ret;
}
function writeRequestLog($filename)
{
$log_file = (defined('RESTRICTED') ? RESTRICTED : FULL_PATH) . '/' . $filename;
- if ( is_writable( dirname($log_file) ) ) {
+ if ( is_writable(dirname($log_file)) ) {
$fp = fopen($log_file, 'a');
- if ($fp) {
+ if ( $fp ) {
$session =& $this->Application->recallObject('Session');
+ /* @var $session Session */
+
$user_id = $session->GetField('PortalUserId');
$admin_mark = $this->Application->isAdmin ? 'ADMIN' : 'FRONT';
- $data = '['.date('D M d H:i:s Y').'] '.$admin_mark.'; ip: '.$_SERVER['REMOTE_ADDR'].'; user_id: '.$user_id.'; sid: '.$this->Application->GetSID().'; request: '."\n";
- if ($this->Get) {
- $data .= "_GET:\n".print_r($this->Get, true);
+ $data = '[' . date('D M d H:i:s Y') . '] ' . $admin_mark . '; ip: ' . $_SERVER['REMOTE_ADDR'] . '; user_id: ' . $user_id . '; sid: ' . $this->Application->GetSID() . '; request: ' . "\n";
+ if ( $this->Get ) {
+ $data .= "_GET:\n" . print_r($this->Get, true);
}
- if ($this->Post) {
- $data .= "_POST:\n".print_r($this->Post, true);
+ if ( $this->Post ) {
+ $data .= "_POST:\n" . print_r($this->Post, true);
}
- if ($this->Cookie) {
- $data .= "_COOKIE:\n".print_r($this->Cookie, true);
+ if ( $this->Cookie ) {
+ $data .= "_COOKIE:\n" . print_r($this->Cookie, true);
}
- $data .= str_repeat('=', 100)."\n";
+ $data .= str_repeat('=', 100) . "\n";
fwrite($fp, $data);
fclose($fp);
}
else {
- trigger_error('Requrest Log directory not writable', E_USER_WARNING);
+ trigger_error('Request Log directory not writable', E_USER_WARNING);
}
}
else {
- trigger_error('Requrest Log directory not writable', E_USER_WARNING);
+ trigger_error('Request Log directory not writable', E_USER_WARNING);
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/filters.php
===================================================================
--- branches/5.2.x/core/kernel/utility/filters.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/filters.php (revision 14628)
@@ -1,120 +1,120 @@
<?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 kMultipleFilter
{
var $type = kDBList::FLT_TYPE_AND;
var $filters = Array();
/**
* Creates empty multiple filter
*
* @param int $type
* @return MultipleFilter
* @access public
*/
function __construct($type = kDBList::FLT_TYPE_AND)
{
$this->setType($type);
}
/**
* Enter description here...
*
* @param int $new_type
*/
function setType($new_type)
{
$this->type = $new_type;
}
/**
* Adds new or replaces old filter with same name
*
* @param string $name
- * @param mixed $clause kMultipleFilter object or where clause ifself
+ * @param string|kMultipleFilter $clause kMultipleFilter object or where clause itself
* @access public
*/
function addFilter($name, $clause)
{
if( is_object($clause) && $clause->hasFilters() )
{
$this->filters[$name] = $clause->getSQL();
}
elseif( !is_object($clause) && strlen($clause) )
{
$this->filters[$name] = $clause;
}
}
/**
* Returns filter by name
*
* @param string $name
* @return string
*/
function getFilter($name)
{
return isset($this->filters[$name]) ? $this->filters[$name] : false;
}
/**
* Removes specified filter from filters list
*
* @param string $name
* @access public
*/
function removeFilter($name)
{
unset($this->filters[$name]);
}
/**
* Remove all added filters
*
* @access public
*/
function clearFilters()
{
$this->filters = Array();
}
/**
* Build where clause based on added filters and multiple filter type
*
* @return string
* @access public
*/
function getSQL()
{
$filter_count = count($this->filters);
if(!$filter_count) return '';
return '('.implode(') '.$this->type.' (',$this->filters).')';
}
/**
* Allows to check if some filters are added to multiple filter
*
* @return bool
* @access public
*/
function hasFilters()
{
return $this->filters ? true : false;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/params.php
===================================================================
--- branches/5.2.x/core/kernel/utility/params.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/params.php (revision 14628)
@@ -1,178 +1,179 @@
<?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 Params extends kBase {
var $_Params = Array();
/**
* Creates class instance
*
* @param string $params_str
* @access public
*/
public function __construct($params_str = null)
{
parent::__construct();
if($params_str != '') {
$this->SplitParamsStr($params_str);
}
}
/**
* Splits tag params into associative array
*
* @param string $params_str
* @access private
*/
function SplitParamsStr($params_str)
{
// $re = preg_quote('([\${}a-zA-Z0-9_.-#\[\]]+)=(["\']{1,1})(.*?)(?<!\\)\2','/');
// preg_match_all('/'.$re.'/s', $params_str, $rets, PREG_SET_ORDER);
preg_match_all('/([\${}a-zA-Z0-9_.\\-\\\\#\\[\\]]+)=(["\']{1,1})(.*?)(?<!\\\)\\2/s', $params_str, $rets, PREG_SET_ORDER);
$values = Array();
foreach ($rets AS $key => $val){
$values[$val[1]] = str_replace('\\' . $val[2], $val[2], $val[3]);
}
$this->AddParams($values);
}
/**
* Sets new parameter value
*
* @param string $name
* @param string $val
+ * @return void
* @access public
*/
- function Set($name, $val)
+ public function Set($name, $val)
{
$this->_Params[$name] = $val;
}
/**
* Removes parameter
*
* @param string $name
* @access public
*/
function Remove($name)
{
unset($this->_Params[$name]);
}
/**
* Gets parameter value by parameter name
*
* @param string $name Name of variable to retrieve
- * @param int $default default value returned in case if varible not present
+ * @param mixed $default default value returned in case if variable not present
* @return string
* @access public
*/
- function Get($name, $default = false)
+ public function Get($name, $default = false)
{
return isset($this->_Params[$name]) ? $this->_Params[$name] : $default;
}
/**
* Mass parameter setting from hash
*
* @param Array $params
* @access public
*/
function AddParams($params)
{
if (!is_array($params)) return;
/*if (count($this->_Params) == 0) {
$this->_Params = $params;
}
else {*/
foreach ($params as $name => $val)
// $this->Set(strtolower($name), $val);
$this->Set($name, $val);
//}
}
/**
* Return all paramters as hash
*
* @return Array
* @access public
*/
function GetParams()
{
return $this->_Params;
}
}
class kArray extends kBase implements kiCacheable {
var $_Array;
/**
* Returns array value with any deep key
*
* @return mixed
* @todo array_unshift doesn't accept parameters by reference, fix it's usage in this method
*/
function GetArrayValue()
{
$args = func_get_args();
kUtil::array_unshift_ref($args, $this->_Array);
return call_user_func_array('getArrayValue', $args);
}
function SetArrayValue()
{
$args = func_get_args();
$value = array_pop($args);
$arr =& $this->_Array;
for ($i=0; $i<count($args); $i++) {
$key = $args[$i];
if ( !isset($arr[$key]) || !is_array($arr[$key]) ) {
$arr[$key] = Array();
}
$arr =& $arr[$key];
}
$arr = $value;
}
/**
* Sets data from cache to object
*
* @param Array $data
* @access public
*/
public function setFromCache(&$data)
{
$this->_Array = $data['TagsAggregator.data'];
}
/**
* Gets object data for caching
*
* @access public
* @return Array
*/
public function getToCache()
{
return Array (
'TagsAggregator.data' => $this->_Array,
);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/utility/unit_config_reader.php
===================================================================
--- branches/5.2.x/core/kernel/utility/unit_config_reader.php (revision 14627)
+++ branches/5.2.x/core/kernel/utility/unit_config_reader.php (revision 14628)
@@ -1,1055 +1,1056 @@
<?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 kUnitConfigReader extends kBase implements kiCacheable {
/**
* Configs reader
*
* @var Array
* @access private
*/
var $configData = Array();
var $configFiles = Array();
var $CacheExpired = false;
var $prefixFiles = array();
var $ProcessAllConfigs = false;
var $FinalStage = false;
var $StoreCache = false;
var $AfterConfigProcessed = array();
/**
* Escaped directory separator for using in regular expressions
*
* @var string
*/
var $_directorySeparator = '';
/**
* Regular expression for detecting module folder
*
* @var string
*/
var $_moduleFolderRegExp = '';
/**
* Folders to skip during unit config search
*
* @var Array
*/
var $_skipFolders = Array ('CVS', '.svn', 'admin_templates', 'libchart');
/**
* Creates instance of unit config reader
*
*/
public function __construct()
{
parent::__construct();
$this->_directorySeparator = preg_quote(DIRECTORY_SEPARATOR);
$editor_path = explode('/', trim(EDITOR_PATH, '/'));
$this->_skipFolders[] = array_pop($editor_path); // last of cmseditor folders
$this->_moduleFolderRegExp = '#' . $this->_directorySeparator . '(core|modules' . $this->_directorySeparator . '.*?)' . $this->_directorySeparator . '#';
}
/**
* Sets data from cache to object
*
* @param Array $data
* @access public
*/
public function setFromCache(&$data)
{
$this->prefixFiles = $data['ConfigReader.prefixFiles'];
}
/**
* Gets object data for caching
*
* @access public
* @return Array
*/
public function getToCache()
{
return Array (
'ConfigReader.prefixFiles' => $this->prefixFiles,
);
}
function scanModules($folderPath, $cache = true)
{
if (defined('IS_INSTALL') && IS_INSTALL && !defined('FORCE_CONFIG_CACHE')) {
// disable config caching during installation
$cache = false;
}
if ($cache) {
$restored = $this->Application->cacheManager->LoadUnitCache();
if ($restored) {
if ( defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode() ) {
$this->Application->Debugger->appendHTML('UnitConfigReader: Restoring Cache');
}
return;
}
}
if ( defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode() ) {
$this->Application->Debugger->appendHTML('UnitConfigReader: Generating Cache');
}
$this->ProcessAllConfigs = true;
$this->includeConfigFiles($folderPath, $cache);
$this->ParseConfigs();
// tell AfterConfigRead to store cache if needed
// can't store it here because AfterConfigRead needs ability to change config data
$this->StoreCache = $cache;
if (!$this->Application->InitDone) {
// scanModules is called multiple times during installation process
$this->Application->InitManagers();
}
$this->Application->cacheManager->applyDelayedUnitProcessing();
}
function findConfigFiles($folderPath, $level = 0)
{
// if FULL_PATH = "/" ensure, that all "/" in $folderPath are not deleted
$reg_exp = '/^' . preg_quote(FULL_PATH, '/') . '/';
$folderPath = preg_replace($reg_exp, '', $folderPath, 1); // this make sense, since $folderPath may NOT contain FULL_PATH
$base_folder = FULL_PATH . $folderPath . DIRECTORY_SEPARATOR;
$sub_folders = glob($base_folder . '*', GLOB_ONLYDIR);
if (!$sub_folders) {
return ;
}
if ($level == 0) {
// don't scan Front-End themes because of extensive directory structure
$sub_folders = array_diff($sub_folders, Array ($base_folder . 'themes', $base_folder . 'tools'));
}
foreach ($sub_folders as $full_path) {
$sub_folder = substr($full_path, strlen($base_folder));
if (in_array($sub_folder, $this->_skipFolders)) {
continue;
}
if (preg_match('/^\./', $sub_folder)) {
// don't scan ".folders"
continue;
}
$config_name = $this->getConfigName($folderPath . DIRECTORY_SEPARATOR . $sub_folder);
if (file_exists(FULL_PATH . $config_name)) {
$this->configFiles[] = $config_name;
}
$this->findConfigFiles($full_path, $level + 1);
}
}
function includeConfigFiles($folderPath, $cache = true)
{
$this->Application->refreshModuleInfo();
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$data = $this->Application->getCache('master:config_files', false, CacheSettings::$unitCacheRebuildTime);
}
else {
$data = $this->Application->getDBCache('config_files', CacheSettings::$unitCacheRebuildTime);
}
if ( $data ) {
$this->configFiles = unserialize($data);
if ( !defined('DBG_VALIDATE_CONFIGS') && !DBG_VALIDATE_CONFIGS ) {
shuffle($this->configFiles);
}
}
else {
if ( $cache ) {
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->rebuildCache('master:config_files', kCache::REBUILD_NOW, CacheSettings::$unitCacheRebuildTime);
}
else {
$this->Application->rebuildDBCache('config_files', kCache::REBUILD_NOW, CacheSettings::$unitCacheRebuildTime);
}
}
$this->findConfigFiles(FULL_PATH . DIRECTORY_SEPARATOR . 'core'); // search from core directory
$this->findConfigFiles($folderPath); // search from modules directory
if ( $cache ) {
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->setCache('master:config_files', serialize($this->configFiles));
}
else {
$this->Application->setDBCache('config_files', serialize($this->configFiles));
}
}
}
foreach ($this->configFiles as $filename) {
$prefix = $this->PreloadConfigFile($filename);
if (!$prefix) {
throw new Exception('Prefix not defined in config file <strong>' . $filename . '</strong>');
}
}
if ($cache) {
unset($this->configFiles);
}
}
/**
* Process all read config files - called ONLY when there is no cache!
*
*/
function ParseConfigs()
{
// 1. process normal configs and their dependencies
$prioritized_configs = array();
foreach ($this->configData as $prefix => $config) {
if (isset($config['ConfigPriority'])) {
$prioritized_configs[$prefix] = $config['ConfigPriority'];
continue;
}
$this->parseConfig($prefix);
}
foreach ($this->configData as $prefix => $config) {
$this->ProcessDependencies($prefix);
$this->postProcessConfig($prefix, 'AggregateConfigs', 'sub_prefix');
$clones = $this->postProcessConfig($prefix, 'Clones', 'prefix');
}
// 2. process prioritized configs and their dependencies
asort($prioritized_configs);
foreach ($prioritized_configs as $prefix => $priority) {
$this->parseConfig($prefix);
}
foreach ($prioritized_configs as $prefix => $priority) {
$this->ProcessDependencies($prefix);
}
}
function AfterConfigRead($store_cache = null)
{
// if (!$this->ProcessAllConfigs) return ;
$this->FinalStage = true;
foreach ($this->configData as $prefix => $config) {
$this->runAfterConfigRead($prefix);
}
if ( !isset($store_cache) ) {
// $store_cache not overridden -> use global setting
$store_cache = $this->StoreCache;
}
if ($store_cache || (defined('IS_INSTALL') && IS_INSTALL)) {
// cache is not stored during install, but dynamic clones should be processed in any case
$this->processDynamicClones();
$this->retrieveCollections();
}
if ($store_cache) {
$this->_sortRewriteListeners();
$after_event = new kEvent('adm:OnAfterCacheRebuild');
$this->Application->HandleEvent($after_event);
$this->Application->cacheManager->UpdateUnitCache();
if (defined('DEBUG_MODE') && DEBUG_MODE && defined('DBG_VALIDATE_CONFIGS') && DBG_VALIDATE_CONFIGS) {
// validate configs here to have changes from OnAfterConfigRead hooks to prefixes
foreach ($this->configData as $prefix => $config) {
if (!isset($config['TableName'])) continue;
$this->ValidateConfig($prefix);
}
}
}
}
/**
* Sort rewrite listeners according to RewritePriority (non-prioritized listeners goes first)
*
*/
function _sortRewriteListeners()
{
$listeners = Array ();
$prioritized_listeners = Array ();
// process non-prioritized listeners
foreach ($this->Application->RewriteListeners as $prefix => $listener_data) {
if ($listener_data['priority'] === false) {
$listeners[$prefix] = $listener_data;
}
else {
$prioritized_listeners[$prefix] = $listener_data['priority'];
}
}
// process prioritized listeners
asort($prioritized_listeners, SORT_NUMERIC);
foreach ($prioritized_listeners as $prefix => $priority) {
$listeners[$prefix] = $this->Application->RewriteListeners[$prefix];
}
$this->Application->RewriteListeners = $listeners;
}
/**
* Re-reads all configs
*
*/
function ReReadConfigs()
{
$this->Application->cacheManager->EmptyUnitCache();
// parse all configs
$this->ProcessAllConfigs = true;
$this->AfterConfigProcessed = Array ();
$this->includeConfigFiles(MODULES_PATH, false);
$this->ParseConfigs();
$this->AfterConfigRead(false);
$this->processDynamicClones();
// don't call kUnitConfigReader::retrieveCollections since it
// will overwrite what we already have in kApplication class instance
}
/**
* Process clones, that were defined via OnAfterConfigRead event
*
*/
function processDynamicClones()
{
$new_clones = Array();
foreach ($this->configData as $prefix => $config) {
$clones = $this->postProcessConfig($prefix, 'Clones', 'prefix');
if ($clones) {
$new_clones = array_merge($new_clones, $clones);
}
}
// execute delayed methods for cloned unit configs
$this->Application->cacheManager->applyDelayedUnitProcessing();
// call OnAfterConfigRead for cloned configs
$new_clones = array_unique($new_clones);
foreach ($new_clones as $prefix) {
$this->runAfterConfigRead($prefix);
}
}
/**
* Process all collectable unit config options here to also catch ones, defined from OnAfterConfigRead events
*
*/
function retrieveCollections()
{
foreach ($this->configData as $prefix => $config) {
// collect replacement templates
if (array_key_exists('ReplacementTemplates', $config) && $config['ReplacementTemplates']) {
$this->Application->ReplacementTemplates = array_merge($this->Application->ReplacementTemplates, $config['ReplacementTemplates']);
}
// collect rewrite listeners
if (array_key_exists('RewriteListener', $config) && $config['RewriteListener']) {
$rewrite_listeners = $config['RewriteListener'];
if (!is_array($rewrite_listeners)) {
// when one method is used to build and parse url
$rewrite_listeners = Array ($rewrite_listeners, $rewrite_listeners);
}
foreach ($rewrite_listeners as $index => $rewrite_listener) {
if (strpos($rewrite_listener, ':') === false) {
$rewrite_listeners[$index] = $prefix . '_EventHandler:' . $rewrite_listener;
}
}
$rewrite_priority = array_key_exists('RewritePriority', $config) ? $config['RewritePriority'] : false;
$this->Application->RewriteListeners[$prefix] = Array ('listener' => $rewrite_listeners, 'priority' => $rewrite_priority);
}
}
}
/**
* Register nessasary classes
* This method should only process the data which is cached!
*
* @param string $prefix
* @access private
*/
function parseConfig($prefix)
{
$this->parseClasses($prefix);
$this->parseAgents($prefix);
$this->parseHooks($prefix);
$this->parseAggregatedTags($prefix);
}
protected function parseClasses($prefix)
{
$config =& $this->configData[$prefix];
$register_classes = $this->getClasses($prefix);
foreach ($register_classes as $class_info) {
// remember class dependencies
$class_name = $class_info['class'];
$require_classes = isset($class_info['require_classes']) ? $class_info['require_classes'] : Array ();
if ($require_classes) {
$require_classes = (array)$require_classes;
if ( !isset($config['_Dependencies'][$class_name]) ) {
$config['_Dependencies'][$class_name] = Array ();
}
$config['_Dependencies'][$class_name] = array_merge($config['_Dependencies'][$class_name], $require_classes);
}
// register class
$this->Application->registerClass(
$class_name,
$config['BasePath'] . DIRECTORY_SEPARATOR . $class_info['file'],
$class_info['pseudo']
);
if ( isset($class_info['build_event']) && $class_info['build_event'] ) {
$this->Application->delayUnitProcessing('registerBuildEvent', Array ($class_info['pseudo'], $class_info['build_event']));
}
}
}
protected function parseAgents($prefix)
{
$config =& $this->configData[$prefix];
if ( !isset($config['RegularEvents']) || !$config['RegularEvents'] ) {
return ;
}
$regular_events = $config['RegularEvents'];
foreach ($regular_events as $short_name => $regular_event_info) {
$event_status = array_key_exists('Status', $regular_event_info) ? $regular_event_info['Status'] : STATUS_ACTIVE;
$this->Application->delayUnitProcessing('registerAgent', Array ( $short_name, $config['Prefix'] . ':' . $regular_event_info['EventName'], $regular_event_info['RunInterval'], $regular_event_info['Type'], $event_status ));
}
}
protected function parseHooks($prefix)
{
$config =& $this->configData[$prefix];
if ( !isset($config['Hooks']) || !$config['Hooks'] ) {
return ;
}
$hooks = $config['Hooks'];
foreach ($hooks as $hook) {
if ( isset($config['ParentPrefix']) && ($hook['HookToPrefix'] == $config['ParentPrefix']) ) {
trigger_error('Depricated Hook Usage [prefix: <strong>' . $config['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'] = $config['Prefix'];
}
if ( isset($config['ParentPrefix']) ) {
// new: allow to set hook to parent prefix what ever it is
if ($hook['HookToPrefix'] == '#PARENT#') {
$hook['HookToPrefix'] = $config['ParentPrefix'];
}
if ($hook['DoPrefix'] == '#PARENT#') {
$hook['DoPrefix'] = $config['ParentPrefix'];
}
}
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'] == '' ? $config['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($prefix)
{
$config =& $this->configData[$prefix];
$aggregated_tags = isset($config['AggregateTags']) ? $config['AggregateTags'] : Array ();
foreach ($aggregated_tags as $aggregate_tag) {
if ( isset($config['ParentPrefix']) ) {
if ($aggregate_tag['AggregateTo'] == $config['ParentPrefix']) {
trigger_error('Depricated Aggregate Tag Usage [prefix: <b>'.$config['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'] = $config['ParentPrefix'];
}
}
$aggregate_tag['LocalPrefix'] = $config['Prefix'];
$this->Application->delayUnitProcessing('registerAggregateTag', Array ($aggregate_tag));
}
}
function ValidateConfig($prefix)
{
global $debugger;
$config =& $this->configData[$prefix];
$tablename = $config['TableName'];
$float_types = Array ('float', 'double', 'numeric');
$table_found = $this->Conn->Query('SHOW TABLES LIKE "'.$tablename.'"');
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>$tablename</strong> missing, but prefix <b>".$config['Prefix']."</b> requires it!");
$debugger->WarningCount++;
return ;
}
$res = $this->Conn->Query('DESCRIBE '.$tablename);
$config_link = $debugger->getFileLink(FULL_PATH.$this->prefixFiles[$config['Prefix']], 1, $config['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 ();
$tablename = preg_replace('/^'.preg_quote(TABLE_PREFIX, '/').'(.*)/', '\\1', $tablename); // remove table prefix
// validate unit config field declaration in relation to database table structure
foreach ($res as $field) {
$f_name = $field['Field'];
if (getArrayValue($config, 'Fields')) {
if (preg_match('/l[\d]+_[\w]/', $f_name)) {
// skip multilingual fields
continue;
}
if (!array_key_exists ($f_name, $config['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 = $config['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 != $config['IDField'] && !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 == $config['IDField'] && $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
if ( array_key_exists('VirtualFields', $config) ) {
foreach ($config['VirtualFields'] 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 ( array_key_exists('CalculatedFields', $config) ) {
foreach ($config['CalculatedFields'] as $special => $calculated_fields) {
foreach ($calculated_fields as $calculated_field => $calculated_field_expr) {
if ( !isset($config['VirtualFields'][$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> ('.$tablename.') 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++;
}
}
function varDump($value)
{
return '<strong>'.var_export($value, true).'</strong> of '.gettype($value);
}
protected function ProcessDependencies($prefix)
{
$config =& $this->configData[$prefix];
- $deps = getArrayValue($config, '_Dependencies');
+ $dependencies = getArrayValue($config, '_Dependencies');
+ /* @var $dependencies Array */
- if (!$deps) {
+ if ( !$dependencies ) {
return ;
}
- foreach ($deps as $real_class => $requires) {
+ foreach ($dependencies as $real_class => $requires) {
foreach ($requires as $class) {
$this->Application->registerDependency($real_class, $class);
}
}
unset($config['_Dependencies']);
}
function postProcessConfig($prefix, $config_key, $dst_prefix_var)
{
$main_config =& $this->configData[$prefix];
- $sub_configs = isset($main_config[$config_key]) && $main_config[$config_key] ? $main_config[$config_key] : false; // getArrayValue($main_config, $config_key);
- if (!$sub_configs) {
- return array();
+ $sub_configs = isset($main_config[$config_key]) && $main_config[$config_key] ? $main_config[$config_key] : Array ();
+ if ( !$sub_configs ) {
+ return Array ();
}
unset($main_config[$config_key]);
$processed = array();
foreach ($sub_configs as $sub_prefix => $sub_config) {
if ($config_key == 'AggregateConfigs' && !isset($this->configData[$sub_prefix])) {
$this->loadConfig($sub_prefix);
}
$sub_config['Prefix'] = $sub_prefix;
$this->configData[$sub_prefix] = kUtil::array_merge_recursive($this->configData[$$dst_prefix_var], $sub_config);
// when merging empty array to non-empty results non-empty array, but empty is required
foreach ($sub_config as $sub_key => $sub_value) {
if (!$sub_value) {
unset($this->configData[$sub_prefix][$sub_key]);
}
}
if ($config_key == 'Clones') {
$this->prefixFiles[$sub_prefix] = $this->prefixFiles[$prefix];
}
$this->postProcessConfig($sub_prefix, $config_key, $dst_prefix_var);
if ($config_key == 'AggregateConfigs') {
$processed = array_merge($this->postProcessConfig($sub_prefix, 'Clones', 'prefix'), $processed);
}
elseif ($this->ProcessAllConfigs) {
$this->parseConfig($sub_prefix);
}
array_push($processed, $sub_prefix);
}
if (!$prefix) {
// configs, that used only for cloning & not used ifself
unset($this->configData[$prefix]);
}
return array_unique($processed);
}
function PreloadConfigFile($filename)
{
$config_found = file_exists(FULL_PATH . $filename) && $this->configAllowed($filename);
if (defined('DEBUG_MODE') && DEBUG_MODE && defined('DBG_PROFILE_INCLUDES') && DBG_PROFILE_INCLUDES) {
- if ( in_array($filename, get_required_files()) ) {
+ if ( in_array($filename, get_included_files()) ) {
return '';
}
global $debugger;
if ($config_found) {
$file = FULL_PATH . $filename;
$file_crc = crc32($file);
$debugger->ProfileStart('inc_' . $file_crc, $file);
include_once($file);
$debugger->ProfileFinish('inc_' . $file_crc);
$debugger->profilerAddTotal('includes', 'inc_' . $file_crc);
}
}
elseif ($config_found) {
include_once(FULL_PATH . $filename);
}
if ($config_found) {
if (isset($config) && $config) {
// config file is included for 1st time -> save it's content for future processing
$prefix = array_key_exists('Prefix', $config) ? $config['Prefix'] : '';
preg_match($this->_moduleFolderRegExp, $filename, $rets);
$config['ModuleFolder'] = str_replace(DIRECTORY_SEPARATOR, '/', $rets[1]);
$config['BasePath'] = dirname(FULL_PATH . $filename);
if (array_key_exists('AdminTemplatePath', $config)) {
// append template base folder for admin templates path of this prefix
$module_templates = $rets[1] == 'core' ? '' : substr($rets[1], 8) . '/';
$config['AdminTemplatePath'] = $module_templates . $config['AdminTemplatePath'];
}
if (array_key_exists($prefix, $this->prefixFiles) && ($this->prefixFiles[$prefix] != $filename)) {
trigger_error(
'Single unit config prefix "<strong>' . $prefix . '</strong>" ' .
'is used in multiple unit config files: ' .
'"<strong>' . $this->prefixFiles[$prefix] . '</strong>", "<strong>' . $filename . '</strong>"',
E_USER_WARNING
);
}
$this->configData[$prefix] = $config;
$this->prefixFiles[$prefix] = $filename;
return $prefix;
}
elseif ($prefix = array_search($filename, $this->prefixFiles)) {
// attempt is made to include config file twice or more, but include_once prevents that,
// but file exists on hdd, then it is already saved to all required arrays, just return it's prefix
return $prefix;
}
}
return 'dummy';
}
function loadConfig($prefix)
{
if ( !isset($this->prefixFiles[$prefix]) ) {
throw new Exception('Configuration file for prefix <strong>' . $prefix . '</strong> is unknown');
return ;
}
$file = $this->prefixFiles[$prefix];
$prefix = $this->PreloadConfigFile($file);
if ($this->FinalStage) {
// run prefix OnAfterConfigRead so all
// hooks to it can define their clonses
$this->runAfterConfigRead($prefix);
}
$clones = $this->postProcessConfig($prefix, 'AggregateConfigs', 'sub_prefix');
$clones = array_merge($this->postProcessConfig($prefix, 'Clones', 'prefix'), $clones);
if ($this->FinalStage) {
$clones = array_unique($clones);
foreach ($clones as $a_prefix) {
$this->runAfterConfigRead($a_prefix);
}
}
}
function runAfterConfigRead($prefix)
{
if (in_array($prefix, $this->AfterConfigProcessed)) {
return ;
}
$this->Application->HandleEvent( new kEvent($prefix . ':OnAfterConfigRead') );
if (!(defined('IS_INSTALL') && IS_INSTALL)) {
// allow to call OnAfterConfigRead multiple times during install
array_push($this->AfterConfigProcessed, $prefix);
}
}
/**
* Reads unit (specified by $prefix)
* option specified by $option
*
* @param string $prefix
* @param string $name
* @param mixed $default
* @return string
* @access public
*/
function getUnitOption($prefix, $name, $default = false)
{
if (preg_match('/(.*)\.(.*)/', $prefix, $rets)) {
if (!isset($this->configData[$rets[1]])) {
$this->loadConfig($rets[1]);
}
$ret = isset($this->configData[$rets[1]][$name][$rets[2]]) ? $this->configData[$rets[1]][$name][$rets[2]] : false;
// $ret = getArrayValue($this->configData, $rets[1], $name, $rets[2]);
}
else {
if (!isset($this->configData[$prefix])) {
$this->loadConfig($prefix);
}
$ret = isset($this->configData[$prefix][$name]) ? $this->configData[$prefix][$name] : false;
// $ret = getArrayValue($this->configData, $prefix, $name);
}
return $ret === false ? $default : $ret;
}
/**
* Read all unit with $prefix options
*
* @param string $prefix
* @return Array
* @access public
*/
function getUnitOptions($prefix)
{
if (!isset($this->configData[$prefix])) {
$this->loadConfig($prefix);
}
return $this->configData[$prefix];
}
/**
* Set's new unit option value
*
* @param string $prefix
* @param string $name
* @param string $value
* @access public
*/
function setUnitOption($prefix, $name, $value)
{
if (preg_match('/(.*)\.(.*)/', $prefix, $rets)) {
if (!isset($this->configData[$rets[1]])) {
$this->loadConfig($rets[1]);
}
$this->configData[$rets[1]][$name][$rets[2]] = $value;
}
else {
if (!isset($this->configData[$prefix])) {
$this->loadConfig($prefix);
}
$this->configData[$prefix][$name] = $value;
}
}
protected function getClasses($prefix)
{
$config =& $this->configData[$prefix];
$class_params = Array ('ItemClass', 'ListClass', 'EventHandlerClass', 'TagProcessorClass');
$register_classes = isset($config['RegisterClasses']) ? $config['RegisterClasses'] : Array ();
foreach ($class_params as $param_name) {
if ( !isset($config[$param_name]) ) {
continue;
}
$config[$param_name]['pseudo'] = $this->getPseudoByOptionName($param_name, $prefix);
$register_classes[] = $config[$param_name];
}
return $register_classes;
}
protected function getPseudoByOptionName($option_name, $prefix)
{
$pseudo_class_map = Array (
'ItemClass' => '%s',
'ListClass' => '%s_List',
'EventHandlerClass' => '%s_EventHandler',
'TagProcessorClass' => '%s_TagProcessor'
);
return sprintf($pseudo_class_map[$option_name], $prefix);
}
/**
* Get's config file name based
* on folder name supplied
*
* @param string $folderPath
* @return string
* @access private
*/
function getConfigName($folderPath)
{
return $folderPath . DIRECTORY_SEPARATOR . basename($folderPath) . '_config.php';
}
/**
* Checks if config file is allowed for includion (if module of config is installed)
*
* @param string $config_path relative path from in-portal directory
*/
function configAllowed($config_path)
{
static $module_paths = null;
if (defined('IS_INSTALL') && IS_INSTALL) {
// at installation start no modules in db and kernel configs could not be read
return true;
}
if (preg_match('#^' . $this->_directorySeparator . 'core#', $config_path)) {
// always allow to include configs from "core" module's folder
return true;
}
if (!$this->Application->ModuleInfo) {
return false;
}
if (!isset($module_paths)) {
$module_paths = Array ();
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
$module_paths[] = str_replace('/', DIRECTORY_SEPARATOR, rtrim($module_info['Path'], '/'));
}
$module_paths = array_unique($module_paths);
}
preg_match($this->_moduleFolderRegExp, $config_path, $rets);
// config file path starts with module folder path
return in_array($rets[1], $module_paths);
}
/**
* Returns true if config exists and is allowed for reading
*
* @param string $prefix
* @return bool
*/
function prefixRegistred($prefix)
{
return isset($this->prefixFiles[$prefix]) ? true : false;
}
/**
* Returns config file for given prefix
*
* @param string $prefix
* @return string
*/
function getPrefixFile($prefix)
{
return array_key_exists($prefix, $this->prefixFiles) ? $this->prefixFiles[$prefix] : false;
}
function iterateConfigs($callback_function, $params)
{
$this->includeConfigFiles(MODULES_PATH); //make sure to re-read all configs
$this->AfterConfigRead();
foreach ($this->configData as $prefix => $config_data) {
$callback_function[0]->$callback_function[1]($prefix, $config_data, $params);
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/event_handler.php
===================================================================
--- branches/5.2.x/core/kernel/event_handler.php (revision 14627)
+++ branches/5.2.x/core/kernel/event_handler.php (revision 14628)
@@ -1,191 +1,191 @@
<?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!');
/**
* Note:
* 1. When adressing variables from submit containing
* Prefix_Special as part of their name use
* $event->getPrefixSpecial(true) instead of
* $event->getPrefixSpecial() as usual. This is due PHP
* is converting "." symbols in variable names during
* submit info "_". $event->getPrefixSpecial optional
* 1st parameter returns correct corrent Prefix_Special
* for variables beeing submitted such way (e.g. variable
* name that will be converted by PHP: "users.read_only_id"
* will be submitted as "users_read_only_id".
*
* 2. When using $this->Application->LinkVar on variables submitted
* from the form which contains $Prefix_Special then note 1st item.
* Example: LinkVar($event->getPrefixSpecial(true).'_varname', $event->getPrefixSpecial().'_varname')
*
*/
/**
* Default event handler. Mostly abstract class
*
*/
class kEventHandler extends kBase {
/**
* In case if event should be handled with method, which name differs from
* event name, then it should be specified here.
* key - event name, value - event method
*
* @var Array
* @access protected
*/
protected $eventMethods = Array ();
/**
* Defines mapping vs event names and permission names
*
* @var Array
* @access protected
*/
protected $permMapping = Array ();
public function __construct()
{
parent::__construct();
$this->mapEvents();
$this->mapPermissions();
}
/**
* Define alternative event processing method names
*
* @see kEventHandler::$eventMethods
* @access protected
*/
protected function mapEvents()
{
}
/**
* Allows to override standart permission mapping
*
* @access protected
* @see kEventHandler::$permMapping
*/
protected function mapPermissions()
{
}
/**
* Returns prefix and special (when present) joined by a "."
*
* @return string
* @access private
*/
public function getPrefixSpecial()
{
throw new Exception('Usage of getPrefixSpecial() method is forbidden in kEventHandler class children. Use $event->getPrefixSpecial(true); instead');
}
/**
* Executes event, specified in $event variable
*
* @param kEvent $event
* @access public
*/
public function processEvent(&$event)
{
$event_name = $event->Name;
if ( array_key_exists($event_name, $this->eventMethods) ) {
$event_name = $this->eventMethods[$event_name];
}
if ( method_exists($this, $event_name) ) {
$this->$event_name($event);
}
else {
throw new Exception('Event <strong>' . $event->Name . '</strong> not implemented in class <strong>' . get_class($this) . '</strong>');
}
}
/**
* Sample dummy event
*
* @param kEvent $event
* @access protected
*/
protected function OnBuild(&$event)
{
}
/**
* Returns to previous template in opener stack
*
* @param kEvent $event
* @access protected
*/
protected function OnGoBack(&$event)
{
$url = $this->Application->RecallVar('export_finish_url');
if ($url) {
$this->Application->Redirect('external:' . $url);
}
$event->SetRedirectParam('opener', 'u');
}
/**
- * Apply some special processing to object beeing
+ * Apply some special processing to object being
* recalled before using it in other events that
* call prepareObject
*
- * @param kDBBase $object
+ * @param kDBItem|kDBList $object
* @param kEvent $event
+ * @return void
* @access protected
*/
protected function prepareObject(&$object, &$event)
{
}
/**
* Checks user permission to execute given $event
*
* @param kEvent $event
- * @access public
- *
* @return bool
+ * @access public
*/
public function CheckPermission(&$event)
{
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
return $perm_helper->CheckEventPermission($event, $this->permMapping);
}
/**
* Occurs, when config was parsed, allows to change config data dynamically
*
* @param kEvent $event
*/
protected function OnAfterConfigRead(&$event)
{
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/nparser/nparser.php
===================================================================
--- branches/5.2.x/core/kernel/nparser/nparser.php (revision 14627)
+++ branches/5.2.x/core/kernel/nparser/nparser.php (revision 14628)
@@ -1,1173 +1,1202 @@
<?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!');
include_once(KERNEL_PATH.'/nparser/ntags.php');
define('TAG_NAMESPACE', 'inp2:');
define('TAG_NAMESPACE_LENGTH', 5);
class NParser extends kBase {
var $Stack = Array ();
var $Level = 0;
var $Buffers = array();
var $InsideComment = false;
/**
* Parse tags inside HTML comments
*
* @var bool
*/
var $SkipComments = true;
var $Params = array();
var $ParamsStack = array();
var $ParamsLevel = 0;
var $Definitions = '';
/**
* Holds dynamic elements to function names mapping during execution
*
* @var Array
*/
var $Elements = Array ();
/**
* Holds location of element definitions inside templates.
* key - element function name, value - array of 2 keys: {from_pos, to_pos}
*
* @var Array
*/
var $ElementLocations = Array ();
var $DataExists = false;
var $TemplateName = null;
var $TempalteFullPath = null;
var $CachePointers = Array ();
var $Cachable = Array ();
/**
+ * Deep level during parsing
+ *
+ * @var int
+ */
+ var $CacheLevel = 0;
+
+ /**
* Caching in templates enabled
*
* @var bool
*/
var $CachingEnabled = false;
/**
* Completely cache given page
*
* @var bool
*/
var $FullCachePage = false;
/**
* Prefixes, that are used on current page
*
* @var Array
*/
var $PrefixesInUse = Array ();
/**
* Parser parameter names, that are created via m_Capture tag are listed here
*
* @var Array
*/
var $Captures = array();
/**
* Phrases, used on "Edit" buttons, that parser adds during block decoration
*
* @var Array
*/
var $_btnPhrases = Array ();
/**
* Mod-rewrite system enabled
*
* @var bool
*/
var $RewriteUrls = false;
/**
* Current user is logged-in
*
* @var bool
*/
var $UserLoggedIn = false;
/**
* Creates template parser object
*
* @access public
*/
public function __construct()
{
parent::__construct();
if (defined('EDITING_MODE') && (EDITING_MODE == EDITING_MODE_DESIGN)) {
$this->_btnPhrases['design'] = $this->Application->Phrase('la_btn_EditDesign', false, true);
$this->_btnPhrases['block'] = $this->Application->Phrase('la_btn_EditBlock', false, true);
}
$this->RewriteUrls = $this->Application->RewriteURLs();
$this->UserLoggedIn = $this->Application->LoggedIn();
// cache only Front-End templated, when memory caching is available and template caching is enabled in configuration
$this->CachingEnabled = !$this->Application->isAdmin && $this->Application->ConfigValue('SystemTagCache') && $this->Application->isCachingType(CACHING_TYPE_MEMORY);
}
function Compile($pre_parsed, $template_name = 'unknown')
{
$data = file_get_contents($pre_parsed['tname']);
if (!$this->CompileRaw($data, $pre_parsed['tname'], $template_name)) {
// compilation failed during errors in template
// trigger_error('Template "<strong>' . $template_name . '</strong>" not compiled because of errors', E_USER_WARNING);
return false;
}
// saving compiled version (only when compilation was successful)
$this->Application->TemplatesCache->saveTemplate($pre_parsed['fname'], $this->Buffers[0]);
return true;
}
function Parse($raw_template, $name = null)
{
$this->CompileRaw($raw_template, $name);
ob_start();
$_parser =& $this;
eval('?'.'>'.$this->Buffers[0]);
return ob_get_clean();
}
function CompileRaw($data, $t_name, $template_name = 'unknown')
{
$code = "extract (\$_parser->Params);\n";
$code .= "\$_parser->ElementLocations['{$template_name}'] = Array('template' => '{$template_name}', 'start_pos' => 0, 'end_pos' => " . strlen($data) . ");\n";
// $code .= "__@@__DefinitionsMarker__@@__\n";
// $code .= "if (!\$this->CacheStart('".abs(crc32($t_name))."_0')) {\n";
$this->Buffers[0] = '<?'."php $code ?>\n";
$this->Cacheable[0] = true;
$this->Definitions = '';
// finding all the tags
$reg = '(.*?)(<[\\/]?)' . TAG_NAMESPACE . '([^>]*?)([\\/]?>)(\r\n){0,1}';
preg_match_all('/'.$reg.'/s', $data, $results, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
$this->InsideComment = false;
foreach ($results as $tag_data) {
$tag = array(
'opening' => $tag_data[2][0],
'tag' => $tag_data[3][0],
'closing' => $tag_data[4][0],
'line' => substr_count(substr($data, 0, $tag_data[2][1]), "\n")+1,
'pos' => $tag_data[2][1],
'file' => $t_name,
'template' => $template_name,
);
// the idea is to count number of comment openings and closings before current tag
// if the numbers do not match we inverse the status of InsideComment
if ($this->SkipComments && (substr_count($tag_data[1][0], '<!--') != substr_count($tag_data[1][0], '-->'))) {
$this->InsideComment = !$this->InsideComment;
}
// appending any text/html data found before tag
$this->Buffers[$this->Level] .= $tag_data[1][0];
if (!$this->InsideComment) {
$tmp_tag = $this->Application->CurrentNTag;
$this->Application->CurrentNTag = $tag;
if ($this->ProcessTag($tag) === false) {
$this->Application->CurrentNTag = $tmp_tag;
return false;
}
$this->Application->CurrentNTag = $tmp_tag;
}
else {
$this->Buffers[$this->Level] .= $tag_data[2][0].$tag_data[3][0].$tag_data[4][0];
}
}
if ($this->Level > 0) {
$error_tag = Array (
'file' => $this->Stack[$this->Level]->Tag['file'],
'line' => $this->Stack[$this->Level]->Tag['line'],
);
throw new ParserException('Unclosed tag opened by ' . $this->TagInfo($this->Stack[$this->Level]->Tag), 0, null, $error_tag);
return false;
}
// appending text data after last tag (after its closing pos),
// if no tag was found at all ($tag_data is not set) - append the whole $data
$this->Buffers[$this->Level] .= isset($tag_data) ? substr($data, $tag_data[4][1]+strlen($tag_data[4][0])) : $data;
$this->Buffers[$this->Level] = preg_replace('/<!--##(.*?)##-->/s', '', $this->Buffers[$this->Level]); // remove hidden comments IB#23065
// $this->Buffers[$this->Level] .= '<?'.'php '."\n\$_parser->CacheEnd();\n}\n"." ?".">\n";
// $this->Buffers[$this->Level] = str_replace('__@@__DefinitionsMarker__@@__', $this->Definitions, $this->Buffers[$this->Level]);
return true;
}
function SplitParamsStr($params_str)
{
preg_match_all('/([\${}a-zA-Z0-9_.\\-\\\\#\\[\\]]+)=(["\']{1,1})(.*?)(?<!\\\)\\2/s', $params_str, $rets, PREG_SET_ORDER);
$values = Array();
// we need to replace all occurences of any current param $key with {$key} for correct variable substitution
foreach ($rets AS $key => $val){
$values[$val[1]] = str_replace('\\' . $val[2], $val[2], $val[3]);
}
return $values;
}
function SplitTag($tag)
{
if (!preg_match('/([^_ \t\r\n]*)[_]?([^ \t\r\n]*)[ \t\r\n]*(.*)$$/s', $tag['tag'], $parts)) {
// this is virtually impossible, but just in case
throw new ParserException('Incorrect tag format: ' . $tag['tag'], 0, null, $tag);
return false;
}
$splited['prefix'] = $parts[2] ? $parts[1] : '__auto__';
$splited['name'] = $parts[2] ? $parts[2] : $parts[1];
$splited['attrs'] = $parts[3];
return $splited;
}
function ProcessTag($tag)
{
$splited = $this->SplitTag($tag);
if ($splited === false) {
return false;
}
$tag = array_merge($tag, $splited);
$tag['processed'] = false;
$tag['NP'] = $this->SplitParamsStr($tag['attrs']);
$o = '';
$tag['is_closing'] = $tag['opening'] == '</' || $tag['closing'] == '/>';
if (class_exists('_Tag_'.$tag['name'])) { // block tags should have special handling class
if ($tag['opening'] == '<') {
$class = '_Tag_'.$tag['name'];
$instance = new $class($tag);
$instance->Parser =& $this;
/* @var $instance _BlockTag */
$this->Stack[++$this->Level] =& $instance;
$this->Buffers[$this->Level] = '';
$this->Cachable[$this->Level] = true;
$open_code = $instance->Open($tag);
if ($open_code === false) {
return false;
}
$o .= $open_code;
}
if ($tag['is_closing']) { // not ELSE here, because tag may be <empty/> and still has a handler-class
if ($this->Level == 0) {
$dump = array();
foreach ($this->Stack as $instance) {
$dump[] = $instance->Tag;
}
if ( $this->Application->isDebugMode() ) {
$this->Application->Debugger->dumpVars($dump);
}
$error_msg = 'Closing tag without an opening: ' . $this->TagInfo($tag) . ' - <strong>probably opening tag was removed or nested tags error</strong>';
throw new ParserException($error_msg, 0, null, $tag);
return false;
}
if ($this->Stack[$this->Level]->Tag['name'] != $tag['name']) {
$opening_tag = $this->Stack[$this->Level]->Tag;
$error_msg = ' Closing tag ' . $this->TagInfo($tag) . ' does not match
opening tag at current nesting level
(' . $this->TagInfo($opening_tag) . ' opened at line ' . $opening_tag['line'] . ')';
throw new ParserException($error_msg, 0, null, $tag);
return false;
}
$o .= $this->Stack[$this->Level]->Close($tag); // DO NOT use $this->Level-- here because it's used inside Close
$this->Level--;
}
}
else { // regular tags - just compile
if (!$tag['is_closing']) {
$error_msg = 'Tag without a handler: ' . $this->TagInfo($tag) . ' - <strong>probably missing &lt;empty <span style="color: red">/</span>&gt; tag closing</strong>';
throw new ParserException($error_msg, 0, null, $tag);
return false;
}
if ($this->Level > 0) $o .= $this->Stack[$this->Level]->PassThrough($tag);
if (!$tag['processed']) {
$compiled = $this->CompileTag($tag);
if ($compiled === false) return false;
if (isset($tag['NP']['cachable']) && (!$tag['NP']['cachable'] || $tag['NP']['cachable'] == 'false')) {
$this->Cachable[$this->Level] = false;
}
$o .= '<?'.'php ' . $compiled . " ?>\n";
// $o .= '<?'.'php ';
// $o .= (isset($tag['NP']['cachable']) && (!$tag['NP']['cachable'] || $tag['NP']['cachable'] == 'false')) ? $this->BreakCache($compiled, $this->GetPointer($tag)) : $compiled;
// $o .= " ?".">\n";
}
}
$this->Buffers[$this->Level] .= $o;
return true;
}
function GetPointer($tag)
{
return abs(crc32($tag['file'])).'_'.$tag['line'];
}
function BreakCache($code, $pointer, $condition='')
{
return "\$_parser->CacheEnd();\n}\n" . $code."\nif ( !\$_parser->CacheStart('{$pointer}'" . ($condition ? ", {$condition}" : '') . ") ) {\n";
}
function TagInfo($tag, $with_params=false)
{
return "<b>{$tag['prefix']}_{$tag['name']}".($with_params ? ' '.$tag['attrs'] : '')."</b>";
}
function CompileParamsArray($arr)
{
$to_pass = 'Array(';
foreach ($arr as $name => $val) {
$to_pass .= '"'.$name.'" => "'.str_replace('"', '\"', $val).'",';
}
$to_pass .= ')';
return $to_pass;
}
function CompileTag($tag)
{
$code = '';
$to_pass = $this->CompileParamsArray($tag['NP']);
if ($tag['prefix'] == '__auto__') {
$prefix = $this->GetParam('PrefixSpecial');
$code .= '$_p_ =& $_parser->GetProcessor($PrefixSpecial);'."\n";
$code .= 'echo $_p_->ProcessParsedTag(\''.$tag['name'].'\', '.$to_pass.', "$PrefixSpecial", \''.$tag['file'].'\', '.$tag['line'].');'."\n";
}
else {
$prefix = $tag['prefix'];
$code .= '$_p_ =& $_parser->GetProcessor("'.$tag['prefix'].'");'."\n";
$code .= 'echo $_p_->ProcessParsedTag(\''.$tag['name'].'\', '.$to_pass.', "'.$tag['prefix'].'", \''.$tag['file'].'\', '.$tag['line'].');'."\n";
}
if (array_key_exists('result_to_var', $tag['NP']) && $tag['NP']['result_to_var']) {
$code .= "\$params['{$tag['NP']['result_to_var']}'] = \$_parser->GetParam('{$tag['NP']['result_to_var']}');\n";
$code .= "\${$tag['NP']['result_to_var']} = \$params['{$tag['NP']['result_to_var']}'];\n";
}
if ($prefix && strpos($prefix, '$') === false) {
$p =& $this->GetProcessor($prefix);
if (!is_object($p) || !$p->CheckTag($tag['name'], $tag['prefix'])) {
$error_msg = 'Unknown tag: ' . $this->TagInfo($tag) . ' - <strong>incorrect tag name or prefix</strong>';
throw new ParserException($error_msg, 0, null, $tag);
return false;
}
}
return $code;
}
function CheckTemplate($t, $silent = null)
{
$pre_parsed = $this->Application->TemplatesCache->GetPreParsed($t);
if (!$pre_parsed) {
if (!$silent) {
throw new ParserException('Cannot include "<strong>' . $t . '</strong>" - file does not exist');
}
return false;
}
$force_compile = defined('DBG_NPARSER_FORCE_COMPILE') && DBG_NPARSER_FORCE_COMPILE;
if (!$pre_parsed || !$pre_parsed['active'] || $force_compile) {
$inc_parser = new NParser();
if ($force_compile) {
// remove Front-End theme markings during total compilation
$t = preg_replace('/^theme:.*?\//', '', $t);
}
if (!$inc_parser->Compile($pre_parsed, $t)) {
return false;
}
}
return $pre_parsed;
}
function Run($t, $silent = null)
{
if ((strpos($t, '../') !== false) || (trim($t) !== $t)) {
// when relative paths or special chars are found template names from url, then it's hacking attempt
return false;
}
$pre_parsed = $this->CheckTemplate($t, $silent);
if (!$pre_parsed) {
return false;
}
$backup_template = $this->TemplateName;
$backup_fullpath = $this->TempalteFullPath;
$this->TemplateName = $t;
$this->TempalteFullPath = $pre_parsed['tname'];
if (!isset($backup_template) && $this->CachingEnabled && !$this->UserLoggedIn && !EDITING_MODE) {
// this is main page template -> check for page-based aggressive caching settings
$output =& $this->RunMainPage($pre_parsed);
}
else {
$output =& $this->Application->TemplatesCache->runTemplate($this, $pre_parsed);
}
$this->TemplateName = $backup_template;
$this->TempalteFullPath = $backup_fullpath;
return $output;
}
function &RunMainPage($pre_parsed)
{
$page =& $this->Application->recallObject('st.-virtual');
/* @var $page kDBItem */
if ($page->isLoaded()) {
// page found in database
$debug_mode = $this->Application->isDebugMode(); // don't cache debug output
$template_path = preg_replace('/^' . preg_quote(FULL_PATH, '/') . '/', '', $this->TempalteFullPath, 1);
$element = ($debug_mode ? 'DEBUG_MODE:' : '') . 'file=' . $template_path;
$this->FullCachePage = $page->GetDBField('EnablePageCache');
if ($this->FullCachePage && $page->GetDBField('PageCacheKey')) {
// page caching enabled -> try to get from cache
$cache_key = $this->FormCacheKey($element, $page->GetDBField('PageCacheKey'));
$output = $this->getCache($cache_key);
if ($output !== false) {
return $output;
}
}
// page not cached OR cache expired
$output =& $this->Application->TemplatesCache->runTemplate($this, $pre_parsed);
$this->generatePageCacheKey($page);
if ($this->FullCachePage && $page->GetDBField('PageCacheKey')) {
$cache_key = $this->FormCacheKey($element, $page->GetDBField('PageCacheKey'));
$this->setCache($cache_key, $output, (int)$page->GetDBField('PageExpiration'));
}
}
else {
// page not found in database
$output =& $this->Application->TemplatesCache->runTemplate($this, $pre_parsed);
}
return $output;
}
/**
* Generate page caching key based on prefixes used on it + prefix IDs passed in url
*
* @param kDBItem $page
*/
function generatePageCacheKey(&$page)
{
if (!$page->isLoaded() || $page->GetDBField('OverridePageCacheKey')) {
return ;
}
$page_cache_key = Array ();
// nobody resets "m" prefix serial, don't count no user too
unset($this->PrefixesInUse['m'], $this->PrefixesInUse['u']);
if (array_key_exists('st', $this->PrefixesInUse)) {
// prefix "st" serial will never be changed
unset($this->PrefixesInUse['st']);
$this->PrefixesInUse['c'] = 1;
}
$prefix_ids = Array ();
$prefixes = array_keys($this->PrefixesInUse);
asort($prefixes);
foreach ($prefixes as $index => $prefix) {
$id = $this->Application->GetVar($prefix . '_id');
if (is_numeric($id)) {
if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Found: "' . $prefix . '_id" = ' . $id . ' during PageCacheKey forming.');
}
$prefix_ids[] = $prefix;
unset($prefixes[$index]);
}
}
if ($prefix_ids) {
$page_cache_key[] = 'prefix_id:' . implode(',', $prefix_ids);
}
if ($prefixes) {
$page_cache_key[] = 'prefix:' . implode(',', $prefixes);
}
$page_cache_key = implode(';', $page_cache_key);
if ($page_cache_key != $page->GetOriginalField('PageCacheKey')) {
if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Canging PageCacheKey from "<strong>' . $page->GetOriginalField('PageCacheKey') . '</strong>" to "<strong>' . $page_cache_key . '</strong>".');
}
$page->SetDBField('PageCacheKey', $page_cache_key);
// don't use kDBItem::Update(), because it will change ModifiedById to current front-end user
$sql = 'UPDATE ' . $page->TableName . '
SET PageCacheKey = ' . $page->Conn->qstr($page_cache_key) . '
WHERE ' . $page->IDField . ' = ' . $page->GetID();
$page->Conn->Query($sql);
// increment serial, because we issue direct sql above!
$this->Application->incrementCacheSerial('c');
$this->Application->incrementCacheSerial('c', $page->GetID());
}
}
+ /**
+ * Creates tag processor and stores it in local cache + factory
+ *
+ * @param string $prefix
+ * @return kTagProcessor
+ */
function &GetProcessor($prefix)
{
- static $Processors = array();
- if (!isset($Processors[$prefix])) {
- $Processors[$prefix] = $this->Application->recallObject($prefix.'_TagProcessor');
+ static $processors = Array ();
+
+ if ( !isset($processors[$prefix]) ) {
+ $processors[$prefix] = $this->Application->recallObject($prefix . '_TagProcessor');
}
- return $Processors[$prefix];
+ return $processors[$prefix];
}
- function SelectParam($params, $possible_names)
+ /**
+ * Not tag. Method for parameter selection from list in this TagProcessor
+ *
+ * @param Array $params
+ * @param Array $possible_names
+ *
+ * @return string
+ * @access protected
+ */
+ protected function SelectParam($params, $possible_names)
{
- if (!is_array($params)) return;
- if (!is_array($possible_names))
+ if ( !is_array($params) ) {
+ return '';
+ }
- $possible_names = explode(',', $possible_names);
- foreach ($possible_names as $name)
- {
- if( isset($params[$name]) ) return $params[$name];
+ if ( !is_array($possible_names) ) {
+ $possible_names = explode(',', $possible_names);
}
- return false;
+
+ foreach ($possible_names as $name) {
+ if ( isset($params[$name]) ) {
+ return $params[$name];
+ }
+ }
+
+ return '';
}
function SetParams($params)
{
$this->Params = $params;
$keys = array_keys($this->Params);
}
function GetParam($name)
{
return isset($this->Params[$name]) ? $this->Params[$name] : false;
}
function SetParam($name, $value)
{
$this->Params[$name] = $value;
}
function PushParams($params)
{
$this->ParamsStack[$this->ParamsLevel++] = $this->Params;
$this->Params = $params;
}
function PopParams()
{
$this->Params = $this->ParamsStack[--$this->ParamsLevel];
}
function ParseBlock($params, $pass_params=false)
{
if (array_key_exists('cache_timeout', $params) && $params['cache_timeout']) {
$ret = $this->getCache( $this->FormCacheKey('element_' . $params['name']) );
if ($ret) {
return $ret;
}
}
if (substr($params['name'], 0, 5) == 'html:') {
return substr($params['name'], 5);
}
if (!array_key_exists($params['name'], $this->Elements) && array_key_exists('default_element', $params)) {
// when given element not found, but default element name given, then render it instead
$params['name'] = $params['default_element'];
unset($params['default_element']);
return $this->ParseBlock($params, $pass_params);
}
$original_params = $params;
if ($pass_params || isset($params['pass_params'])) $params = array_merge($this->Params, $params);
$this->PushParams($params);
$data_exists_bak = $this->DataExists;
// if we are parsing design block and we have block_no_data - we need to wrap block_no_data into design,
// so we should set DataExists to true manually, otherwise the design block will be skipped because of data_exists in params (by Kostja)
//
// keep_data_exists is used by block RenderElement (always added in ntags.php), to keep the DataExists value
// from inside-content block, otherwise when parsing the design block DataExists will be reset to false resulting missing design block (by Kostja)
//
// Inside-content block parsing result is given to design block in "content" parameter (ntags.php) and "keep_data_exists"
// is only passed, when parsing design block. In case, when $this->DataExists is set to true, but
// zero-length content (in 2 cases: method NParser::CheckNoData set it OR really empty block content)
// is returned from inside-content block, then design block also should not be shown (by Alex)
$this->DataExists = (isset($params['keep_data_exists']) && isset($params['content']) && $params['content'] != '' && $this->DataExists) || (isset($params['design']) && isset($params['block_no_data']) && $params['name'] == $params['design']);
if (!array_key_exists($params['name'], $this->Elements)) {
$pre_parsed = $this->Application->TemplatesCache->GetPreParsed($params['name']);
if ($pre_parsed) {
$ret = $this->IncludeTemplate($params);
if (array_key_exists('no_editing', $params) && $params['no_editing']) {
// when individual render element don't want to be edited
return $ret;
}
return defined('EDITING_MODE') ? $this->DecorateBlock($ret, $params, true) : $ret;
}
$trace_results = debug_backtrace();
$error_tag = Array (
'file' => $trace_results[0]['file'],
'line' => $trace_results[0]['line'],
);
$error_msg = '<strong>Rendering of undefined element ' . $params['name'] . '</strong>';
throw new ParserException($error_msg, 0, null, $error_tag);
return false;
}
$m_processor =& $this->GetProcessor('m');
$flag_values = $m_processor->PreparePostProcess($params);
$f_name = $this->Elements[$params['name']];
$ret = $f_name($this, $params);
$ret = $m_processor->PostProcess($ret, $flag_values);
$block_params = $this->Params; // input parameters, but modified inside rendered block
$this->PopParams();
if (array_key_exists('result_to_var', $flag_values) && $flag_values['result_to_var']) {
// when "result_to_var" used inside ParseBlock, then $$result_to_var parameter is set inside ParseBlock,
// but not outside it as expected and got lost at all after PopParams is called, so make it work by
// setting it's value on current parameter deep level (from where ParseBlock was called)
$this->SetParam($flag_values['result_to_var'], $block_params[ $flag_values['result_to_var'] ]);
}
$this->CheckNoData($ret, $params);
$this->DataExists = $data_exists_bak || $this->DataExists;
if (array_key_exists('cache_timeout', $original_params) && $original_params['cache_timeout']) {
$cache_key = $this->FormCacheKey('element_' . $original_params['name']);
$this->setCache($cache_key, $ret, (int)$original_params['cache_timeout']);
}
if (array_key_exists('no_editing', $block_params) && $block_params['no_editing']) {
// when individual render element don't want to be edited
return $ret;
}
return defined('EDITING_MODE') ? $this->DecorateBlock($ret, $params) : $ret;
}
/**
* Checks, that given block is defined
*
* @param string $name
* @return bool
*/
function blockFound($name)
{
return array_key_exists($name, $this->Elements);
}
function DecorateBlock($block_content, $block_params, $is_template = false)
{
static $used_ids = Array (), $base_url = null;
if (!isset($base_url)) {
$base_url = $this->Application->BaseURL();
}
// $prepend = '[name: ' . $block_params['name'] . '] [params: ' . implode(', ', array_keys($block_params)) . ']';
$decorate = false;
$design = false;
if (EDITING_MODE == EDITING_MODE_DESIGN) {
$decorate = true;
if ($is_template) {
// content inside pair RenderElement tag
}
else {
if (strpos($block_params['name'], '__capture_') === 0) {
// capture tag (usually inside pair RenderElement)
$decorate = false;
}
elseif (array_key_exists('content', $block_params)) {
// pair RenderElement (on template, were it's used)
$design = true;
}
}
}
if (!$decorate) {
return $block_content;
}
/*else {
$block_content = $prepend . $block_content;
}*/
$block_name = $block_params['name'];
$function_name = $is_template ? $block_name : $this->Elements[$block_name];
$block_title = '';
if (array_key_exists($function_name, $this->Application->Parser->ElementLocations)) {
$element_location = $this->Application->Parser->ElementLocations[$function_name];
$block_title .= $element_location['template'] . '.tpl';
$block_title .= ' (' . $element_location['start_pos'] . ' - ' . $element_location['end_pos'] . ')';
}
// ensure unique id for every div (used from print lists)
$container_num = 1;
$container_id = 'parser_block[' . $function_name . ']';
while (in_array($container_id . '_' . $container_num, $used_ids)) {
$container_num++;
}
$container_id .= '_' . $container_num;
$used_ids[] = $container_id;
// prepare parameter string
$param_string = $block_name . ':' . $function_name;
if ($design) {
$btn_text = $this->_btnPhrases['design'];
$btn_class = 'cms-edit-design-btn';
$btn_container_class = 'block-edit-design-btn-container';
$btn_name = 'design';
}
else {
$btn_text = $this->_btnPhrases['block'];
$btn_class = 'cms-edit-block-btn';
$btn_container_class = 'block-edit-block-btn-container';
$btn_name = 'content';
}
$block_editor = '
<div id="' . $container_id . '" params="' . $param_string . '" class="' . $btn_container_class . '" title="' . htmlspecialchars($block_title) . '">
<div class="' . $btn_class . '">
<div class="cms-btn-image">
<img src="' . $base_url . 'core/admin_templates/img/top_frame/icons/' . $btn_name . '_mode.png" width="15" height="16" alt=""/>
</div>
<div class="cms-btn-text" id="' . $container_id . '_btn">' . $btn_text . '</div>
</div>
<div class="cms-btn-content">
%s
</div>
</div>';
// 1 - text before, 2 - open tag, 3 - open tag attributes, 4 - content inside tag, 5 - closing tag, 6 - text after closing tag
if (preg_match('/^(\s*)<(td|span)(.*?)>(.*)<\/(td|span)>(.*)$/is', $block_content, $regs)) {
// div inside span -> put div outside span
return $regs[1] . '<' . $regs[2] . ' ' . $regs[3] . '>' . str_replace('%s', $regs[4], $block_editor) . '</' . $regs[5] . '>' . $regs[6];
}
return str_replace('%s', $block_content, $block_editor);
}
function IncludeTemplate($params, $silent=null)
{
$t = is_array($params) ? $this->SelectParam($params, 't,template,block,name') : $params;
$cache_timeout = array_key_exists('cache_timeout', $params) ? $params['cache_timeout'] : false;
if ($cache_timeout) {
$cache_key = $this->FormCacheKey('template:' . $t);
$ret = $this->getCache($cache_key);
if ($ret !== false) {
return $ret;
}
}
$t = preg_replace('/\.tpl$/', '', $t);
$data_exists_bak = $this->DataExists;
$this->DataExists = false;
if (!isset($silent) && array_key_exists('is_silent', $params)) {
$silent = $params['is_silent'];
}
if (isset($params['pass_params'])) {
// ability to pass params from block to template
$params = array_merge($this->Params, $params);
}
$this->PushParams($params);
$ret = $this->Run($t, $silent);
$this->PopParams();
$this->CheckNoData($ret, $params);
$this->DataExists = $data_exists_bak || $this->DataExists;
if ($cache_timeout) {
$this->setCache($cache_key, $ret, (int)$cache_timeout);
}
return $ret;
}
function CheckNoData(&$ret, $params)
{
if (array_key_exists('data_exists', $params) && $params['data_exists'] && !$this->DataExists) {
$block_no_data = isset($params['BlockNoData']) ? $params['BlockNoData'] : (isset($params['block_no_data']) ? $params['block_no_data'] : false);
if ($block_no_data) {
$ret = $this->ParseBlock(array('name'=>$block_no_data));
}
else {
$ret = '';
}
}
}
function getCache($name)
{
if (!$this->CachingEnabled) {
return false;
}
$ret = $this->Application->getCache($name, false);
if (preg_match('/^\[DE_MARK:(.*?)\]$/', substr($ret, -11), $regs)) {
$this->DataExists = $regs[1] ? true : false;
$ret = substr($ret, 0, -11);
}
return $ret;
}
function setCache($name, $value, $expiration = 0)
{
if (!$this->CachingEnabled) {
return false;
}
// remeber DataExists in cache, because after cache will be restored
// it will not be available naturally (no tags, that set it will be called)
$value .= '[DE_MARK:' . (int)$this->DataExists . ']';
return $this->Application->setCache($name, $value, $expiration);
}
function FormCacheKey($element, $key_string = '')
{
if (strpos($key_string, 'guest_only') !== false && $this->UserLoggedIn) {
// don't cache, when user is logged-in "guest_only" is specified in key
return '';
}
$parts = Array ();
// 1. replace INLINE variable (from request) into key parts
if (preg_match_all('/\(%(.*?)\)/', $key_string, $regs)) {
// parts in form "(%variable_name)" were found
foreach ($regs[1] as $variable_name) {
$variable_value = $this->Application->GetVar($variable_name);
$key_string = str_replace('(%' . $variable_name . ')', $variable_value, $key_string);
}
}
// 2. replace INLINE serial numbers (they may not be related to any prefix at all)
// Serial number also could be composed of inline variables!
if (preg_match_all('/\[%(.*?)%\]/', $key_string, $regs)) {
// format "[%LangSerial%]" - prefix-wide serial in case of any change in "lang" prefix
// format "[%LangIDSerial:5%]" - one id-wide serial in case of data, associated with given id was changed
// format "[%CiIDSerial:ItemResourceId:5%]" - foreign key-based serial in case of data, associated with given foreign key was changed
foreach ($regs[1] as $serial_name) {
$serial_value = $this->Application->getCache('[%' . $serial_name . '%]');
$key_string = str_replace('[%' . $serial_name . '%]', '[%' . $serial_name . '=' . $serial_value . '%]', $key_string);
}
}
/*
Always add:
===========
* "var:m_lang" - show content on current language
* "var:t" - template from url, used to differ multiple pages using same physical template (like as design)
* "var:admin,editing_mode" - differ cached content when different editing modes are used
* "var:m_cat_id,m_cat_page" - pass current category
* "var:page,per_page,sort_by" - list pagination/sorting parameters
* "prefix:theme-file" - to be able to reset all cached templated using "Rebuild Theme Files" function
* "prefix:phrases" - use latest phrase translations
* "prefix:conf" - output could slighly differ based on configuration settings
*/
$key_string = rtrim('var:m_lang,t,admin,editing_mode,m_cat_id,m_cat_page,page,per_page,sort_by;prefix:theme-file,phrases,conf;' . $key_string, ';');
$keys = explode(';', $key_string);
/*
Possible parts of a $key_string (all can have multiple occurencies):
====================================================================
* prefix:<prefixA>[,<prefixB>,<prefixC>] - include global serial for given prefix(-es)
* skip_prefix:<prefix1>[,<prefix2>,<prefix3>] - exclude global serial for given prefix(-es)
* prefix_id:<prefixA>[,<prefixB>,<prefixC>] - include id-based serial for given prefix(-es)
* skip_prefix_id:<prefix1>[,<prefix2>,<prefix3>] - exclude id-based serial for given prefix(-es)
* var:<aaa>[,<bbb>,<ccc>] - include request variable value(-s)
* skip_var:<varA>[,<varB>,<varC>] - exclude request variable value(-s)
* (%variable_name) - include request variable value (only value without variable name ifself, like in "var:variable_name")
* [%SerialName%] - use to retrieve serial value in free form
*/
// 3. get variable names, prefixes and prefix ids, that should be skipped
$skip_prefixes = $skip_prefix_ids = $skip_variables = Array ();
foreach ($keys as $index => $key) {
if (preg_match('/^(skip_var|skip_prefix|skip_prefix_id):(.*?)$/i', $key, $regs)) {
unset($keys[$index]);
$tmp_parts = explode(',', $regs[2]);
switch ($regs[1]) {
case 'skip_var':
$skip_variables = array_merge($skip_variables, $tmp_parts);
break;
case 'skip_prefix':
$skip_prefixes = array_merge($skip_prefixes, $tmp_parts);
break;
case 'skip_prefix_id':
$skip_prefix_ids = array_merge($skip_prefix_ids, $tmp_parts);
break;
}
}
}
$skip_prefixes = array_unique($skip_prefixes);
$skip_variables = array_unique($skip_variables);
$skip_prefix_ids = array_unique($skip_prefix_ids);
// 4. process keys
foreach ($keys as $key) {
if (preg_match('/^(var|prefix|prefix_id):(.*?)$/i', $key, $regs)) {
$tmp_parts = explode(',', $regs[2]);
switch ($regs[1]) {
case 'var':
// format: "var:country_id" will become "country_id=<country_id>"
$tmp_parts = array_diff($tmp_parts, $skip_variables);
foreach ($tmp_parts as $variable_name) {
$variable_value = $this->Application->GetVar($variable_name);
if ($variable_value !== false) {
$parts[] = $variable_name . '=' . $variable_value;
}
}
break;
case 'prefix':
// format: "prefix:country" will become "[%CountrySerial%]"
$tmp_parts = array_diff($tmp_parts, $skip_prefixes);
foreach ($tmp_parts as $prefix) {
$serial_name = $this->Application->incrementCacheSerial($prefix, null, false);
$parts[] = '[%' . $serial_name . '=' . $this->Application->getCache($serial_name) . '%]';
if (!$this->RewriteUrls) {
// add env-style page and per-page variable, when mod-rewrite is off
$prefix_variables = Array ($prefix . '_Page', $prefix . '_PerPage');
foreach ($prefix_variables as $variable_name) {
$variable_value = $this->Application->GetVar($variable_name);
if ($variable_value !== false) {
$parts[] = $variable_name . '=' . $variable_value;
}
}
}
}
break;
case 'prefix_id':
// format: "id:country" will become "[%CountryIDSerial:5%]"
$tmp_parts = array_diff($tmp_parts, $skip_prefix_ids);
foreach ($tmp_parts as $prefix_id) {
$id = $this->Application->GetVar($prefix_id . '_id');
if ($id !== false) {
$serial_name = $this->Application->incrementCacheSerial($prefix_id, $id, false);
$parts[] = '[%' . $serial_name . '=' . $this->Application->getCache($serial_name) . '%]';
}
}
break;
}
}
elseif ($key == 'currency') {
// based on current currency
$parts[] = 'curr_iso=' . $this->Application->RecallVar('curr_iso');
}
elseif ($key == 'groups') {
// based on logged-in user groups
$parts[] = 'groups=' . $this->Application->RecallVar('UserGroups');
}
elseif ($key == 'guest_only') {
// we know this key, but process it at method beginning
}
else {
throw new ParserException('Unknown key part "<strong>' . $key . '</strong>" used in "<strong>key</strong>" parameter of <inp2:m_Cache key="..."/> tag');
}
}
// 5. add unique given cache key identifier on this page
$parts[] = $element;
$key = implode(':', $parts);
if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Parser Key: ' . $key);
}
return 'parser_' . crc32($key);
}
function PushPointer($pointer, $key)
{
$cache_key = $this->FullCachePage || !$this->CachingEnabled ? '' : $this->FormCacheKey('pointer:' . $pointer, $key);
$this->CachePointers[++$this->CacheLevel] = $cache_key;
return $this->CachePointers[$this->CacheLevel];
}
function PopPointer()
{
return $this->CachePointers[$this->CacheLevel--];
}
function CacheStart($pointer, $key)
{
$pointer = $this->PushPointer($pointer, $key);
if ($pointer) {
$ret = $this->getCache($pointer);
$debug_mode = defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode();
if ($ret !== false) {
echo $debug_mode ? '<!-- CACHED OUTPUT START -->' . $ret . '<!-- /CACHED OUTPUT END -->' : $ret;
$this->PopPointer();
return true;
}
if ($debug_mode) {
echo '<!-- NO CACHE FOR POINTER: ' . $pointer . ' -->';
}
}
ob_start();
return false;
}
function CacheEnd($expiration = 0)
{
$ret = ob_get_clean();
$pointer = $this->PopPointer();
if ($pointer) {
$res = $this->setCache($pointer, $ret, $expiration);
if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
echo '<!-- STORING CACHE FOR POINTER: ' . $pointer . ' [' . $res . '] -->';
}
}
echo $ret;
}
/**
* Performs compression of given files or text
*
* @param mixed $data
* @param bool $raw_script
* @param string $file_extension
* @return string
*/
function CompressScript($data, $raw_script = false, $file_extension = '')
{
$minify_helper =& $this->Application->recallObject('MinifyHelper');
/* @var $minify_helper MinifyHelper */
if ($raw_script) {
$minify_helper->compressString($data, $file_extension);
return $data;
}
return $minify_helper->CompressScriptTag($data);
}
}
class ParserException extends Exception {
public function __construct($message = null, $code = 0, Exception $previous = null, $tag = null)
{
parent::__construct($message, $code, $previous);
if ( isset($tag) ) {
$this->file = $tag['file'];
$this->line = $tag['line'];
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/kernel/nparser/template_cache.php
===================================================================
--- branches/5.2.x/core/kernel/nparser/template_cache.php (revision 14627)
+++ branches/5.2.x/core/kernel/nparser/template_cache.php (revision 14628)
@@ -1,444 +1,447 @@
<?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 TemplatesCache extends kHelper {
/**
* Base path for searching templates
*
* @var string
*/
var $BasePath;
/**
* Force templates cache to search templates on front-end:
* true - search for theme name in template name
* false - don't search anywhere
* name - theme name to prepend for each template name
*
* @var mixed
*/
var $forceThemeName = false;
/**
* Compile templates to database
*
* @var bool
*/
var $_compileToDatabase = false;
/**
* Compress compiled templates
*
* @var bool
*/
var $_compressOutput = false;
/**
* Template locations of each module
*
* @var Array
*/
var $_modulePaths = Array ();
public function __construct()
{
parent::__construct();
$this->BasePath = FULL_PATH . THEMES_PATH;
$this->_compileToDatabase = defined('SAFE_MODE') && SAFE_MODE;
$this->_compressOutput = $this->Application->ConfigValue('UseTemplateCompression');
if ($this->Application->isAdmin) {
// prepare module template paths for quick access
$module_paths = Array ();
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
$module_paths[ mb_strtolower($module_name) ] = rtrim($module_info['Path'], '/');
}
$this->_modulePaths = $module_paths;
}
}
/**
* Based on template name gets it's location on disk and owner module
*
* @param string $filename
* @return Array 0 - path on disk, 1 - template name
*/
function GetTemplatePaths($filename)
{
if ($this->Application->isAdmin && array_key_exists($filename, $this->Application->ReplacementTemplates)) {
// process admin template replacement
$filename = $this->Application->ReplacementTemplates[$filename];
}
// allows to use non-replaced version of replaced template
$filename = preg_replace('/^original:(.*)/', '\\1', $filename);
if (preg_match('#^[\/]{0,1}([^\/]*)\/(.*)#', $filename, $regs)) {
$first_dir = $regs[1];
$module_filename = $regs[2];
}
else {
$first_dir = '';
$module_filename = $filename;
}
if (is_string($this->forceThemeName)) {
// when defined, then all templates are read from given theme name
$first_dir = 'theme:' . $this->forceThemeName . ($first_dir ? '/' . $first_dir : '');
}
if ($this->Application->isAdmin && array_key_exists($first_dir, $this->_modulePaths)) {
// template belongs to one of the modules
$path = FULL_PATH . '/' . $this->_modulePaths[$first_dir] . '/admin_templates';
}
elseif ($this->forceThemeName && preg_match('/^theme:(.*)/', $first_dir, $regs)) {
// ability to use Front-End templates in admin (only during mass compilation)
$path = FULL_PATH . '/themes/' . $regs[1];
}
else {
// template from "core" module
$path = $this->BasePath;
$module_filename = $first_dir . '/' . $module_filename;
}
return Array ($path, $module_filename);
}
/**
* Returns template filename by given template name
*
* @param string $filename
* @return string
*/
function GetRealFilename($filename)
{
list ($path, $module_filename) = $this->GetTemplatePaths($filename);
return $path.'/'.trim($module_filename, '/');
}
/**
* Checks, that given template exists on disk
*
* @param string $filename
* @return bool
*/
function TemplateExists($filename)
{
if ((strpos($filename, '../') !== false) || (trim($filename) !== $filename)) {
// when relative paths or special chars are found template names from url, then it's hacking attempt
return false;
}
$real_file = $this->GetRealFilename( strtolower($filename) );
if (substr($real_file, -4) != '.tpl') {
// add ".tpl" file extension, when not specified in template name
$real_file .= '.tpl';
}
return file_exists($real_file);
}
/**
* Returns information about template compilation status
*
* @param string $template
* @return Array
*/
function GetPreParsed($template)
{
$real_name = $this->GetRealFilename($template);
$fname = str_replace(FULL_PATH, WRITEABLE . '/cache', $real_name . '.php');
$tname = $real_name . '.tpl';
if (!file_exists($tname)) {
// given template doesn't exist
return false;
}
if ($this->_compileToDatabase) {
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'Cache
WHERE VarName = "' . $fname . '"';
$cached = $this->Conn->GetRow($sql);
if ($cached !== false && $cached['Cached'] > filemtime($tname)) {
return Array ('active' => 1, 'fname' => $fname, 'tname' => $tname, 'mode' => 'db', 'content' => $cached['Data']);
}
}
else {
if (file_exists($fname) && file_exists($tname) && filemtime($fname) > filemtime($tname)) {
return Array ('active' => 1, 'fname' => $fname, 'tname' => $tname, 'mode' => 'file');
}
if (!file_exists($fname)) {
// make sure to create directory if pre-parsed file does not exist
$this->CheckDir(dirname($fname), WRITEABLE . '/cache');
}
}
// when compiled template is expired or doesn't exist
return Array ('active' => 0, 'fname' => $fname, 'tname' => $tname, 'mode' => 'file');
}
/**
* Saves compiled template version to database or disk
*
* @param string $filename
* @param string $compiled_template
*/
function saveTemplate($filename, &$compiled_template)
{
if ($this->_compileToDatabase) {
$fields_hash = Array (
'VarName' => $filename,
'Data' => &$compiled_template,
'Cached' => adodb_mktime(),
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'Cache', 'REPLACE');
return ;
}
$fp = fopen($filename, 'w');
if ($this->_compressOutput) {
$compiled_template = $this->_compress($compiled_template);
}
if ( !fwrite($fp, $compiled_template) ) {
throw new ParserException('Saving compiled template failed');
}
fclose($fp);
}
/**
* Runs template and returns result (template already should be compiled by now)
*
* @param NParser $_parser
* @param Array $pre_parsed
* @return string
*/
function &runTemplate(&$_parser, &$pre_parsed)
{
ob_start();
if ($this->_compileToDatabase) {
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'Cache
WHERE VarName = "' . $pre_parsed['fname'] . '"';
$cached = $this->Conn->GetRow($sql);
if (($cached !== false) && ($cached['Cached'] > filemtime($pre_parsed['tname']))) {
eval('?' . '>' . $cached['Data']);
}
}
else {
if ($pre_parsed['mode'] == 'file') {
include($pre_parsed['fname']);
}
else {
eval('?' . '>' . $pre_parsed['content']);
}
}
$output = ob_get_clean();
return $output;
}
/**
* Compress given php code
*
* @param string $src
* @return string
*/
function _compress($src) {
// Whitespaces left and right from this signs can be ignored
static $IW = array(
T_CONCAT_EQUAL, // .=
T_DOUBLE_ARROW, // =>
T_BOOLEAN_AND, // &&
T_BOOLEAN_OR, // ||
T_IS_EQUAL, // ==
T_IS_NOT_EQUAL, // != or <>
T_IS_SMALLER_OR_EQUAL, // <=
T_IS_GREATER_OR_EQUAL, // >=
T_INC, // ++
T_DEC, // --
T_PLUS_EQUAL, // +=
T_MINUS_EQUAL, // -=
T_MUL_EQUAL, // *=
T_DIV_EQUAL, // /=
T_IS_IDENTICAL, // ===
T_IS_NOT_IDENTICAL, // !==
T_DOUBLE_COLON, // ::
T_PAAMAYIM_NEKUDOTAYIM, // ::
T_OBJECT_OPERATOR, // ->
T_DOLLAR_OPEN_CURLY_BRACES, // ${
T_AND_EQUAL, // &=
T_MOD_EQUAL, // %=
T_XOR_EQUAL, // ^=
T_OR_EQUAL, // |=
T_SL, // <<
T_SR, // >>
T_SL_EQUAL, // <<=
T_SR_EQUAL, // >>=
);
$tokens = token_get_all($src);
$new = "";
$c = sizeof($tokens);
$iw = false; // ignore whitespace
$ih = false; // in HEREDOC
$ls = ""; // last sign
$ot = null; // open tag
for ($i = 0; $i < $c; $i++) {
$token = $tokens[$i];
if (is_array($token)) {
list ($tn, $ts) = $token; // tokens: number, string, line
$tname = token_name($tn);
if ($tn == T_INLINE_HTML) {
$new .= $ts;
$iw = false;
} else {
if ($tn == T_OPEN_TAG) {
if (strpos($ts, " ") || strpos($ts, "\n") || strpos($ts, "\t") || strpos($ts, "\r")) {
$ts = rtrim($ts);
}
$ts .= " ";
$new .= $ts;
$ot = T_OPEN_TAG;
$iw = true;
} elseif ($tn == T_OPEN_TAG_WITH_ECHO) {
$new .= $ts;
$ot = T_OPEN_TAG_WITH_ECHO;
$iw = true;
} elseif ($tn == T_CLOSE_TAG) {
if ($ot == T_OPEN_TAG_WITH_ECHO) {
$new = rtrim($new, "; ");
} else {
$ts = " ".$ts;
}
$new .= $ts;
$ot = null;
$iw = false;
} elseif (in_array($tn, $IW)) {
$new .= $ts;
$iw = true;
} elseif ($tn == T_CONSTANT_ENCAPSED_STRING || $tn == T_ENCAPSED_AND_WHITESPACE) {
if ($ts[0] == '"') {
$ts = addcslashes($ts, "\n\t\r");
}
$new .= $ts;
$iw = true;
} elseif ($tn == T_WHITESPACE) {
$nt = @$tokens[$i+1];
if (!$iw && (!is_string($nt) || $nt == '$') && !in_array($nt[0], $IW)) {
$new .= " ";
}
$iw = false;
} elseif ($tn == T_START_HEREDOC) {
$new .= "<<<S\n";
$iw = false;
$ih = true; // in HEREDOC
} elseif ($tn == T_END_HEREDOC) {
$new .= "S;";
$iw = true;
$ih = false; // in HEREDOC
for ($j = $i + 1; $j < $c; $j++) {
if (is_string($tokens[$j]) && $tokens[$j] == ";") {
$i = $j;
break;
} else if ($tokens[$j][0] == T_CLOSE_TAG) {
break;
}
}
} elseif ($tn == T_COMMENT || $tn == T_DOC_COMMENT) {
$iw = true;
} else {
/*if (!$ih) {
// this also lowecases attribute names :(
$ts = strtolower($ts);
}*/
$new .= $ts;
$iw = false;
}
}
$ls = "";
} else {
if (($token != ";" && $token != ":") || $ls != $token) {
$new .= $token;
$ls = $token;
}
$iw = true;
}
}
return $new;
}
/**
* Recursive mkdir
*
* @param string $dir
* @param string $base_path base path to directory where folders should be created in
+ * @return bool
+ * @access protected
*/
- function CheckDir($dir, $base_path = '')
+ protected function CheckDir($dir, $base_path = '')
{
- if (file_exists($dir)) {
+ if ( file_exists($dir) ) {
return true;
}
- else {
- // remove $base_path from beggining because it is already created during install
- $dir = preg_replace('/^' . preg_quote($base_path . '/', '/').'/', '', $dir, 1);
- $segments = explode('/', $dir);
- $cur_path = $base_path;
-
- foreach ($segments as $segment) {
- // do not add leading / for windows paths (c:\...)
- $cur_path .= preg_match('/^[a-zA-Z]{1}:/', $segment) ? $segment : '/' . $segment;
-
- if ( !file_exists($cur_path) ) {
- if ( !mkdir($cur_path) ) {
- return false;
- }
+
+ // remove $base_path from beginning because it is already created during install
+ $dir = preg_replace('/^' . preg_quote($base_path . '/', '/') . '/', '', $dir, 1);
+ $segments = explode('/', $dir);
+ $cur_path = $base_path;
+
+ foreach ($segments as $segment) {
+ // do not add leading / for windows paths (c:\...)
+ $cur_path .= preg_match('/^[a-zA-Z]{1}:/', $segment) ? $segment : '/' . $segment;
+
+ if ( !file_exists($cur_path) ) {
+ if ( !mkdir($cur_path) ) {
+ return false;
}
}
}
+
+ return false;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/agents/agent_eh.php
===================================================================
--- branches/5.2.x/core/units/agents/agent_eh.php (revision 14627)
+++ branches/5.2.x/core/units/agents/agent_eh.php (revision 14628)
@@ -1,178 +1,181 @@
<?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 AgentEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnMassCancel' => Array ('self' => 'add|edit'),
'OnRunAgents' => Array ('self' => 'add|edit'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* [HOOK] Refreshes agents list in database based on cached data from unit configs
*
* @param kEvent $event
*/
function OnRefreshAgents(&$event)
{
$regular_events = $this->Application->EventManager->getAgents(true);
$object =& $event->getObject( Array ('skip_autoload' => true) );
/* @var $object kDBItem */
$processed_ids = Array ();
$agents = $this->Conn->Query($object->GetSelectSQL(), 'AgentName');
foreach ($regular_events as $run_mode => $events) {
foreach ($events as $agent_name => $agent_params) {
if ( !isset($agents[$agent_name]) ) {
$fields_hash = Array (
'Event' => $agent_params['EventName'],
'AgentName' => $agent_name,
'AgentType' => Agent::AGENT_TYPE_SYSTEM,
'Status' => array_key_exists('Status', $agent_params) ? $agent_params['Status'] : STATUS_ACTIVE,
'RunInterval' => $agent_params['RunInterval'],
'RunMode' => $run_mode,
);
$object->Clear();
$object->SetDBFieldsFromHash($fields_hash);
$object->Create();
}
else {
$object->LoadFromHash( $agents[$agent_name] );
}
$processed_ids[] = $object->GetID();
}
}
// delete all non-processed agents (ones, that were deleted from unit configs)
$sql = 'SELECT ' . $object->IDField . '
FROM ' . $object->TableName . '
WHERE (AgentType = ' . Agent::AGENT_TYPE_SYSTEM . ') AND (' . $object->IDField . ' NOT IN (' . implode(',', $processed_ids) . '))';
$delete_ids = $this->Conn->GetCol($sql);
if ($delete_ids) {
$temp_handler =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
/* @var $temp_handler kTempTablesHandler */
$temp_handler->DeleteItems($event->Prefix, $event->Special, $delete_ids);
}
$this->Application->removeObject($event->getPrefixSpecial());
}
/**
* Don't allow to delete other user's messages
*
* @param kEvent $event
+ * @param string $type
+ * @return void
+ * @access protected
*/
- function customProcessing(&$event, $type)
+ protected function customProcessing(&$event, $type)
{
if ($event->Name == 'OnMassDelete' && $type == 'before') {
if ($this->Application->isDebugMode()) {
// allow to delete system agents in debug mode
return ;
}
$ids = $event->getEventParam('ids');
if ($ids) {
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT ' . $id_field . '
FROM ' . $table_name . '
WHERE ' . $id_field . ' IN (' . implode(',', $ids) . ') AND AgentType <> ' . Agent::AGENT_TYPE_SYSTEM;
$allowed_ids = $this->Conn->GetCol($sql);
$event->setEventParam('ids', $allowed_ids);
}
}
}
/**
* Cancels agents, that are currenty running
*
* @param kEvent $event
*/
function OnMassCancel(&$event)
{
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
$object =& $event->getObject( Array ('skip_autoload' => true) );
/* @var $object kDBItem */
foreach ($ids as $id) {
$object->Load($id);
if ($object->GetDBField('LastRunStatus') == Agent::LAST_RUN_RUNNING) {
// only changes status, doesn't affect currency running agents
$object->SetDBField('LastRunStatus', Agent::LAST_RUN_FAILED);
$object->Update();
}
}
}
$this->clearSelectedIDs($event);
}
/**
* Runs selected agents
*
* @param kEvent $event
*/
function OnRunAgents(&$event)
{
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
$object =& $event->getObject( Array ('skip_autoload' => true) );
/* @var $object kDBItem */
$where_clause = Array (
$object->TableName . '.' . $object->IDField . ' IN (' . implode(',', $ids) . ')',
$object->TableName . '.Status = ' . STATUS_ACTIVE,
$object->TableName . '.LastRunStatus <> ' . Agent::LAST_RUN_RUNNING,
);
$sql = $object->GetSelectSQL() . '
WHERE (' . implode(') AND (', $where_clause) . ')';
$agents = $this->Conn->Query($sql);
foreach ($agents as $agent_data) {
$agent_data['EventName'] = $agent_data['Event'];
$this->Application->EventManager->runAgent($agent_data);
}
}
$this->clearSelectedIDs($event);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/visits/visits_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/visits/visits_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/visits/visits_tag_processor.php (revision 14628)
@@ -1,169 +1,183 @@
<?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 VisitsTagProcessor extends kDBTagProcessor {
function UserFound($params)
{
$virtual_users = Array(USER_ROOT, USER_GUEST, 0);
- $object =& $this->Application->recallObject( $this->getPrefixSpecial(), $this->Prefix, $params );
+
+ $object =& $this->getObject($params);
+ /* @var $object kDBItem */
+
return !in_array( $object->GetDBField( $params['user_field'] ) , $virtual_users );
}
-
- function UserLink($params)
+ /**
+ * Returns link for user editing
+ *
+ * @param Array $params
+ *
+ * @return string
+ * @access protected
+ */
+ protected function UserLink($params)
{
- $object =& $this->Application->recallObject( $this->getPrefixSpecial(), $this->Prefix, $params );
- $user_id = $object->GetDBField( $params['user_field'] );
+ $object =& $this->getObject($params);
+ /* @var $object kDBItem */
- if ($user_id) {
- $url_params = Array (
- 'm_opener' => 'd',
- 'u_mode' => 't',
- 'u_event' => 'OnEdit',
- 'u_id' => $user_id,
- 'pass' => 'all,u'
- );
+ $user_id = $object->GetDBField( $params['user_field'] );
- return $this->Application->HREF($params['edit_template'], '', $url_params);
+ if (!$user_id) {
+ return '';
}
+
+ $url_params = Array (
+ 'm_opener' => 'd',
+ 'u_mode' => 't',
+ 'u_event' => 'OnEdit',
+ 'u_id' => $user_id,
+ 'pass' => 'all,u'
+ );
+
+ return $this->Application->HREF($params['edit_template'], '', $url_params);
}
function getDateLimitClause($field)
{
$search_filter = $this->Application->RecallVar( $this->getPrefixSpecial().'_search_filter');
if($search_filter)
{
$search_filter = unserialize($search_filter);
return $search_filter[$field]['value'];
}
return '';
}
function AffiliateOrderInfo($params)
{
$list =& $this->GetList($params);
$date_limit = str_replace($list->TableName, 'vis', $this->getDateLimitClause('VisitDate') );
$affil_table = $this->Application->getUnitOption('affil', 'TableName');
$affil_idfield = $this->Application->getUnitOption('affil', 'IDField');
$sql = 'SELECT '.$affil_idfield.' FROM '.$affil_table.' WHERE PortalUserId = '.$this->Application->RecallVar('user_id');
$affiliate_id = $this->Conn->GetOne($sql);
$sql = 'SELECT COUNT(ord.OrderId) AS OrderCount
FROM '.$list->TableName.' vis
LEFT JOIN '.TABLE_PREFIX.'Orders ord ON ord.VisitId = vis.VisitId
WHERE (vis.AffiliateId = '.$affiliate_id.') AND (ord.Status = '.ORDER_STATUS_PROCESSED.')'.($date_limit ? ' AND '.$date_limit : '');
$result = $this->Conn->GetRow($sql);
$sql = 'SELECT COUNT(*) FROM '.$list->TableName.' vis
WHERE AffiliateId = '.$affiliate_id.($date_limit ? ' AND '.$date_limit : '');
$result['TotalVisitors'] = $this->Conn->GetOne($sql);
$result['OrderTotalAmount'] = $list->getTotal('OrderTotalAmount', 'SUM');
$result['OrderAffiliateCommission'] = $list->getTotal('OrderAffiliateCommission', 'SUM');
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
$format_fields = Array('OrderTotalAmount', 'OrderAffiliateCommission');
if (array_key_exists('currency', $params) && $params['currency']) {
$iso = $this->GetISO($params['currency']);
foreach($format_fields as $format_field)
{
$format = $list->GetFieldOption($format_field, 'format');
$value = sprintf($format, $result[$format_field]);
$value = $this->ConvertCurrency($value, $iso);
$value = $this->AddCurrencySymbol($value, $iso);
$result[$format_field] = $value;
}
}
$block_params = array_merge($block_params, $result);
return $this->Application->ParseBlock($block_params);
}
function ListVisitors($params)
{
$o = '';
$params['render_as'] = $params['item_render_as'];
$o_visitors = $this->PrintList2($params);
if($o_visitors)
{
$header = '';
$footer = '';
$block_params = $this->prepareTagParams($params);
$header_block = getArrayValue($params, 'header_render_as');
if($header_block)
{
$block_params['name'] = $header_block;
$header = $this->Application->ParseBlock($block_params);
}
$footer_block = getArrayValue($params, 'footer_render_as');
if($footer_block)
{
$block_params['name'] = $footer_block;
$footer = $this->Application->ParseBlock($block_params);
}
$o = $header.$o_visitors.$footer;
}
else
{
$visitors_params = array('name' => $params['empty_myvisitors_render_as']);
$o = $this->Application->ParseBlock($visitors_params);
}
return $o;
}
/**
* Enter description here...
*
* @param string $params
* @return kDBList
*/
function &GetList($params)
{
$list_name = $this->SelectParam($params, 'list_name,name');
if (!$list_name) {
$list_name = $this->Application->Parser->GetParam('list_name');
}
$types = $this->SelectParam($params, 'types');
$special='';
if ($types=='myvisitororders' || $types=='myvisitors'){
$special = 'incommerce';
$names_mapping = $this->Application->GetVar('NamesToSpecialMapping');
$names_mapping[$this->Prefix][$list_name] = $special;
$this->Application->SetVar('NamesToSpecialMapping', $names_mapping);
}
return parent::GetList($params);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/visits/visits_event_handler.php
===================================================================
--- branches/5.2.x/core/units/visits/visits_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/visits/visits_event_handler.php (revision 14628)
@@ -1,137 +1,138 @@
<?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 VisitsEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnItemBuild' => Array('self' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Registers user visit to site
*
* @param kEvent $event
+ *
+ * @return void
+ * @access protected
*/
- function OnRegisterVisit(&$event)
+ protected function OnRegisterVisit(&$event)
{
- if ($this->Application->isAdmin || !$this->Application->ConfigValue('UseVisitorTracking')) {
- // admin logins are not registred in visits list
- return true;
- }
-
- if ($this->Application->RecallVar('visit_id')) {
- return true;
- }
-
- $object =& $event->getObject( Array('skip_autoload' => true) );
- $object->SetDBField('VisitDate_date', adodb_mktime() );
- $object->SetDBField('VisitDate_time', adodb_mktime() );
- $object->SetDBField('Referer', getArrayValue($_SERVER, 'HTTP_REFERER') );
- $object->SetDBField('IPAddress', $_SERVER['REMOTE_ADDR'] );
-
- if ($object->Create()) {
- $this->Application->StoreVar('visit_id', $object->GetID() );
- $this->Application->SetVar('visits_id', $object->GetID() );
+ if ( $this->Application->isAdmin || !$this->Application->ConfigValue('UseVisitorTracking') || $this->Application->RecallVar('visit_id') ) {
+ // admin logins are not registered in visits list
+ return ;
+ }
+
+ $object =& $event->getObject(Array ('skip_autoload' => true));
+ /* @var $object kDBItem */
+
+ $object->SetDBField('VisitDate_date', adodb_mktime());
+ $object->SetDBField('VisitDate_time', adodb_mktime());
+ $object->SetDBField('Referer', getArrayValue($_SERVER, 'HTTP_REFERER'));
+ $object->SetDBField('IPAddress', $_SERVER['REMOTE_ADDR']);
+
+ if ( $object->Create() ) {
+ $this->Application->StoreVar('visit_id', $object->GetID());
+ $this->Application->SetVar('visits_id', $object->GetID());
}
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
+ * @return void
* @access protected
- * @see OnListBuild
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
+ /* @var $object kDBList */
$types = $event->getEventParam('types');
- if($types == 'myvisitors')
- {
+ if ( $types == 'myvisitors' ) {
$user_id = $this->Application->RecallVar('user_id');
- $object->addFilter('myitems_user1','au.PortalUserId = '.$user_id);
- $object->addFilter('myitems_user2','au.PortalUserId >0');
+ $object->addFilter('myitems_user1', 'au.PortalUserId = ' . $user_id);
+ $object->addFilter('myitems_user2', 'au.PortalUserId >0');
//$object->AddGroupByField('VisitDate');
$object->AddGroupByField('%1$s.VisitId');
-
}
- if($types == 'myvisitororders' && $event->Special == 'incommerce')
- {
+ if ( $types == 'myvisitororders' && $event->Special == 'incommerce' ) {
$user_id = $this->Application->RecallVar('user_id');
- $object->addFilter('myitems_orders','ord.OrderId IS NOT NULL');
- $object->addFilter('myitems_user1','au.PortalUserId = '.$user_id);
- $object->addFilter('myitems_user2','au.PortalUserId >0');
- $object->addFilter('myitems_orders_processed','ord.Status = 4');
+ $object->addFilter('myitems_orders', 'ord.OrderId IS NOT NULL');
+ $object->addFilter('myitems_user1', 'au.PortalUserId = ' . $user_id);
+ $object->addFilter('myitems_user2', 'au.PortalUserId >0');
+ $object->addFilter('myitems_orders_processed', 'ord.Status = 4');
}
}
/**
- * Apply some special processing to
- * object beeing recalled before using
- * it in other events that call prepareObject
+ * Apply some special processing to object being
+ * recalled before using it in other events that
+ * call prepareObject
*
- * @param kBase $object
+ * @param kDBItem|kDBList $object
* @param kEvent $event
+ * @return void
* @access protected
*/
- function prepareObject(&$object, &$event)
+ protected function prepareObject(&$object, &$event)
{
$types = $event->getEventParam('types');
if(method_exists($object, 'AddGroupByField'))
{
if( ($types == 'myvisitors' || !$types) && $object->Special == 'incommerce')
{
$object->addCalculatedField('OrderTotalAmountSum', 'SUM(IF(ord.Status = 4, ord.SubTotal+ord.ShippingCost+ord.VAT, 0))');
$object->addCalculatedField('OrderAffiliateCommissionSum', 'SUM( IF(ord.Status = 4,ord.AffiliateCommission,0))');
$object->addCalculatedField('OrderCountByVisit', 'SUM( IF(ord.Status = 4, 1, 0) )');
}
if (!$types){
$object->AddGroupByField('%1$s.VisitId');
}
}
}
/**
* [HOOK] Updates user_id in current visit
*
* @param kEvent $event
*/
function OnUserLogin(&$event)
{
if ($event->MasterEvent->status == kEvent::erSUCCESS) {
$user_id = $this->Application->RecallVar('user_id');
if ($user_id > 0) {
// for real users only, not root,guest
$this->Application->setVisitField('PortalUserId', $user_id);
}
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/thesaurus/thesaurus_eh.php
===================================================================
--- branches/5.2.x/core/units/thesaurus/thesaurus_eh.php (revision 14627)
+++ branches/5.2.x/core/units/thesaurus/thesaurus_eh.php (revision 14628)
@@ -1,36 +1,39 @@
<?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 ThesaurusEventHandler extends kDBEventHandler {
/**
* Shows only thesaurus terms, that matches searched keywords
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
parent::SetCustomQuery($event);
$object =& $event->getObject();
/* @var $object kDBList */
if (!$this->Application->isAdminUser) {
$keywords = kUtil::unhtmlentities( trim($this->Application->GetVar('keywords')) );
$object->addFilter('search_filter', '%1$s.SearchTerm LIKE ' . $this->Conn->qstr($keywords).' OR %1$s.SearchTerm LIKE ' . $this->Conn->qstr($keywords . '_'));
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/theme_files/theme_file_eh.php
===================================================================
--- branches/5.2.x/core/units/theme_files/theme_file_eh.php (revision 14627)
+++ branches/5.2.x/core/units/theme_files/theme_file_eh.php (revision 14628)
@@ -1,222 +1,228 @@
<?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 ThemeFileEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnLoadBlock' => Array ('subitem' => true),
'OnSaveBlock' => Array ('subitem' => true),
'OnSaveLayout' => Array ('subitem' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
- * Changes permission section to one from REQUEST, not from config
+ * Checks user permission to execute given $event
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
if ($event->Name == 'OnLoadBlock' || $event->Name == 'OnSaveBlock') {
return $this->Application->isAdminUser;
}
return parent::CheckPermission($event);
}
/**
* Loads template contents into virtual field
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemLoad(&$event)
+ protected function OnAfterItemLoad(&$event)
{
parent::OnAfterItemLoad($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$filename = $this->_getTemplatePath($object);
- if (file_exists($filename)) {
+ if ( file_exists($filename) ) {
$object->SetDBField('FileContents', file_get_contents($filename));
}
else {
$object->SetError('FileContents', 'template_file_missing', 'la_error_TemplateFileMissing');
}
}
/**
* Trim contents of edited template
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
parent::OnBeforeItemUpdate($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$file_data = $object->GetDBField('FileContents');
$file_data = str_replace("\r\n", "\n", $file_data);
$file_data = str_replace("\r", "\n", $file_data);
$object->SetDBField('FileContents', trim($file_data));
}
/**
* Saves updated content to template
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
parent::OnAfterItemUpdate($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$filename = $this->_getTemplatePath($object);
if (file_exists($filename) && is_writable($filename)) {
$fp = fopen($filename, 'w');
fwrite($fp, $object->GetDBField('FileContents'));
fclose($fp);
$themes_helper =& $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$meta_info = $themes_helper->parseTemplateMetaInfo($filename);
$file_description = array_key_exists('desc', $meta_info) ? $meta_info['desc'] : '';
$object->SetDBField('Description', $file_description);
$object->SetDBField('FileMetaInfo', serialize($meta_info));
$object->Update();
}
}
/**
* Returns full path to template file
*
* @param kDBItem $object
* @return string
*/
function _getTemplatePath(&$object)
{
$theme =& $this->Application->recallObject('theme');
/* @var $theme kDBItem */
$path = FULL_PATH . '/themes/' . $theme->GetDBField('Name');
$path .= $object->GetDBField('FilePath') . '/' . $object->GetDBField('FileName');
return $path;
}
/**
* Loads block data based on it's name in request
*
* @param kEvent $event
*/
function OnLoadBlock(&$event)
{
parent::OnNew($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$template_helper =& $this->Application->recallObject('TemplateHelper');
/* @var $template_helper TemplateHelper */
$template_helper->InitHelper($object);
$object->SetDBField('FileName', $template_helper->blockInfo('template_file'));
$object->SetDBField('BlockPosition', $template_helper->blockInfo('start_pos') . ' - ' . $template_helper->blockInfo('end_pos'));
$object->SetDBField('FileContents', $template_helper->blockInfo('content'));
}
/**
* Saves changed template block
*
* @param kEvent $event
*/
function OnSaveBlock(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if ($items_info) {
list ($id, $field_values) = each($items_info);
$object->SetFieldsFromHash($field_values);
$object->setID($id);
}
$status = $object->Validate();
$template_helper =& $this->Application->recallObject('TemplateHelper');
/* @var $template_helper TemplateHelper */
$template_helper->InitHelper($object);
$status = $status && $template_helper->saveBlock($object);
if ($status) {
$event->SetRedirectParam('opener', 'u');
}
else {
$event->status = kEvent::erFAIL;
}
}
/**
* Saves layout on given template
*
* @param kEvent $event
*/
function OnSaveLayout(&$event)
{
$event->status = kEvent::erSTOP;
if (($this->Application->GetVar('ajax') != 'yes') || (EDITING_MODE != EDITING_MODE_DESIGN)) {
return ;
}
$target_order = $this->Application->GetVar('target_order');
$template_helper =& $this->Application->recallObject('TemplateHelper');
/* @var $template_helper TemplateHelper */
if ($template_helper->moveTemplateElements($target_order)) {
echo 'OK';
return ;
}
echo 'FAILED';
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/categories/categories_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/categories/categories_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/categories/categories_tag_processor.php (revision 14628)
@@ -1,1999 +1,2009 @@
<?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.
*/
class CategoriesTagProcessor extends kDBTagProcessor {
function SubCatCount($params)
{
$object =& $this->getObject($params);
if (isset($params['today']) && $params['today']) {
$sql = 'SELECT COUNT(*)
FROM '.$object->TableName.'
WHERE (ParentPath LIKE "'.$object->GetDBField('ParentPath').'%") AND (CreatedOn > '.(adodb_mktime() - 86400).')';
return $this->Conn->GetOne($sql) - 1;
}
return $object->GetDBField('CachedDescendantCatsQty');
}
/**
* Returns category count in system
*
* @param Array $params
* @return int
*/
function CategoryCount($params)
{
$count_helper =& $this->Application->recallObject('CountHelper');
/* @var $count_helper kCountHelper */
$today_only = isset($params['today']) && $params['today'];
return $count_helper->CategoryCount($today_only);
}
function IsNew($params)
{
$object =& $this->getObject($params);
return $object->GetDBField('IsNew') ? 1 : 0;
}
function IsPick($params)
{
return $this->IsEditorsPick($params);
}
/**
* Returns item's editors pick status (using not formatted value)
*
* @param Array $params
* @return bool
*/
function IsEditorsPick($params)
{
$object =& $this->getObject($params);
return $object->GetDBField('EditorsPick') == 1;
}
function ItemIcon($params)
{
$grids = $this->Application->getUnitOption($this->Prefix, 'Grids');
$grid = $grids[ $params['grid'] ];
if (!array_key_exists('Icons', $grid)) {
return '';
}
$icons = $grid['Icons'];
$icon_prefix = array_key_exists('icon_prefix', $params)? $params['icon_prefix'] : 'icon16_';
if (array_key_exists('name', $params)) {
$icon_name = $params['name'];
return array_key_exists($icon_name, $icons) ? $icons[$icon_name] : '';
}
$object =& $this->getObject($params);
/* @var $object kDBList */
if ($object->GetDBField('ThemeId') > 0) {
if (!$object->GetDBField('IsMenu')) {
return $icon_prefix . 'section_menuhidden_system.png';
}
return $icon_prefix . 'section_system.png';
}
$status = $object->GetDBField('Status');
if ($status == STATUS_DISABLED) {
return $icon_prefix . 'section_disabled.png';
}
if (!$object->GetDBField('IsMenu')) {
return $icon_prefix . 'section_menuhidden.png';
}
if ($status == STATUS_PENDING) {
return $icon_prefix . 'section_pending.png';
}
if ($object->GetDBField('IsNew') && ($icon_prefix == 'icon16_')) {
return $icon_prefix . 'section_new.png'; // show gris icon only in grids
}
return $icon_prefix . 'section.png';
}
function ItemCount($params)
{
$object =& $this->getObject($params);
/* @var $object kDBItem */
$ci_table = $this->Application->getUnitOption('ci', 'TableName');
$sql = 'SELECT COUNT(*)
FROM ' . $object->TableName . ' c
LEFT JOIN ' . $ci_table . ' ci ON c.CategoryId = ci.CategoryId
WHERE (c.TreeLeft BETWEEN ' . $object->GetDBField('TreeLeft') . ' AND ' . $object->GetDBField('TreeRight') . ') AND NOT (ci.CategoryId IS NULL)';
return $this->Conn->GetOne($sql);
}
function ListCategories($params)
{
return $this->PrintList2($params);
}
function RootCategoryName($params)
{
return $this->Application->ProcessParsedTag('m', 'RootCategoryName', $params);
}
function CheckModuleRoot($params)
{
$module_name = getArrayValue($params, 'module') ? $params['module'] : 'In-Commerce';
$module_root_cat = $this->Application->findModule('Name', $module_name, 'RootCat');
$additional_cats = $this->SelectParam($params, 'add_cats');
if ($additional_cats) {
$additional_cats = explode(',', $additional_cats);
}
else {
$additional_cats = array();
}
if ($this->Application->GetVar('m_cat_id') == $module_root_cat || in_array($this->Application->GetVar('m_cat_id'), $additional_cats)) {
$home_template = getArrayValue($params, 'home_template');
if (!$home_template) return;
$this->Application->Redirect($home_template, Array('pass'=>'all'));
};
}
function CategoryPath($params)
{
$category_helper =& $this->Application->recallObject('CategoryHelper');
/* @var $category_helper CategoryHelper */
return $category_helper->NavigationBar($params);
}
/**
* Shows category path to specified category
*
* @param Array $params
* @return string
*/
function FieldCategoryPath($params)
{
$object =& $this->getObject();
/* @var $object kDBItem */
$field = $this->SelectParam($params, 'name,field');
$category_id = $object->GetDBField($field);
if ($category_id) {
$params['cat_id'] = $category_id;
return $this->CategoryPath($params);
}
return '';
}
function CurrentCategoryName($params)
{
$cat_object =& $this->Application->recallObject($this->getPrefixSpecial(), $this->Prefix.'_List');
+ /* @var $cat_object kDBList */
+
$sql = 'SELECT '.$this->getTitleField().'
FROM '.$cat_object->TableName.'
WHERE CategoryId = '.(int)$this->Application->GetVar('m_cat_id');
return $this->Conn->GetOne($sql);
}
/**
* Returns current category name
*
* @param Array $params
* @return string
* @todo Find where it's used
*/
function CurrentCategory($params)
{
return $this->CurrentCategoryName($params);
}
function getTitleField()
{
$ml_formatter =& $this->Application->recallObject('kMultiLanguage');
+ /* @var $ml_formatter kMultiLanguage */
+
return $ml_formatter->LangFieldName('Name');
}
/**
* Returns symlinked category for given category
*
- * @param $category_id
+ * @param int $category_id
+ * @return int
*/
function getCategorySymLink($category_id)
{
if (!$category_id) {
// don't bother to get symlink for "Home" category
return $category_id;
}
$cache_key = 'category_symlinks[%CSerial%]';
$cache = $this->Application->getCache($cache_key);
if ($cache === false) {
$id_field = $this->Application->getUnitOption($this->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($this->Prefix, 'TableName');
// get symlinked categories, that are not yet deleted
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT c1.SymLinkCategoryId, c1.' . $id_field . '
FROM ' . $table_name . ' c1
JOIN ' . $table_name . ' c2 ON c1.SymLinkCategoryId = c2.' . $id_field;
$cache = $this->Conn->GetCol($sql, $id_field);
$this->Application->setCache($cache_key, $cache);
}
return array_key_exists($category_id, $cache) ? $cache[$category_id] : $category_id;
}
function CategoryLink($params)
{
$category_id = getArrayValue($params, 'cat_id');
if ($category_id === false) {
$category_id = $this->Application->GetVar($this->getPrefixSpecial() . '_id');
}
if ("$category_id" == 'Root') {
$category_id = $this->Application->findModule('Name', $params['module'], 'RootCat');
}
elseif ("$category_id" == 'current') {
$category_id = $this->Application->GetVar('m_cat_id');
}
if (!array_key_exists('direct_link', $params) || !$params['direct_link']) {
$category_id = $this->getCategorySymLink( (int)$category_id );
}
else {
unset($params['direct_link']);
}
$physical_template = $this->Application->getPhysicalTemplate('id:' . $category_id);
if ( ($physical_template !== false) && preg_match('/external:(.*)/', $physical_template, $rets) ) {
// external url
return $rets[1];
}
unset($params['cat_id'], $params['module']);
$new_params = Array ('pass' => 'm', 'm_cat_id' => $category_id, 'pass_category' => 1);
$params = array_merge($params, $new_params);
return $this->Application->ProcessParsedTag('m', 't', $params);
}
function CategoryList($params)
{
//$object =& $this->Application->recallObject( $this->getPrefixSpecial() , $this->Prefix.'_List', $params );
$object =& $this->GetList($params);
if ($object->GetRecordsCount() == 0)
{
if (isset($params['block_no_cats'])) {
$params['name'] = $params['block_no_cats'];
return $this->Application->ParseBlock($params);
}
else {
return '';
}
}
if (isset($params['block'])) {
return $this->PrintList($params);
}
else {
$params['block'] = $params['block_main'];
if (isset($params['block_row_start'])) {
$params['row_start_block'] = $params['block_row_start'];
}
if (isset($params['block_row_end'])) {
$params['row_end_block'] = $params['block_row_end'];
}
return $this->PrintList2($params);
}
}
function Meta($params)
{
$object =& $this->Application->recallObject($this->Prefix); // .'.-item'
/* @var $object CategoriesItem */
$meta_type = $params['name'];
if ($object->isLoaded()) {
// 1. get module prefix by current category
$category_helper =& $this->Application->recallObject('CategoryHelper');
/* @var $category_helper CategoryHelper */
$category_path = explode('|', substr($object->GetDBField('ParentPath'), 1, -1));
$module_info = $category_helper->getCategoryModule($params, $category_path);
// In-Edit & Proj-CMS module prefixes doesn't have custom field with item template
if ($module_info && $module_info['Var'] != 'adm' && $module_info['Var'] != 'st') {
// 2. get item template by current category & module prefix
$mod_rewrite_helper = $this->Application->recallObject('ModRewriteHelper');
/* @var $mod_rewrite_helper kModRewriteHelper */
$category_params = Array (
'CategoryId' => $object->GetID(),
'ParentPath' => $object->GetDBField('ParentPath'),
);
$item_template = $mod_rewrite_helper->GetItemTemplate($category_params, $module_info['Var']);
if ($this->Application->GetVar('t') == $item_template) {
// we are located on item's details page
$item =& $this->Application->recallObject($module_info['Var']);
/* @var $item kCatDBItem */
// 3. get item's meta data
$value = $item->GetField('Meta'.$meta_type);
if ($value) {
return $value;
}
}
// 4. get category meta data
$value = $object->GetField('Meta'.$meta_type);
if ($value) {
return $value;
}
}
}
// 5. get default meta data
switch ($meta_type) {
case 'Description':
$config_name = 'Category_MetaDesc';
break;
case 'Keywords':
$config_name = 'Category_MetaKey';
break;
}
return $this->Application->ConfigValue($config_name);
}
function BuildListSpecial($params)
{
if (($this->Special != '') && !is_numeric($this->Special)) {
// When recursive category list is printed (like in sitemap), then special
// should be generated even if it's already present. Without it list on this
// level will erase list on previous level, because it will be stored in same object.
return $this->Special;
}
if ( isset($params['parent_cat_id']) ) {
$parent_cat_id = $params['parent_cat_id'];
}
else {
$parent_cat_id = $this->Application->GetVar($this->Prefix.'_id');
if (!$parent_cat_id) {
$parent_cat_id = $this->Application->GetVar('m_cat_id');
}
if (!$parent_cat_id) {
$parent_cat_id = 0;
}
}
$list_unique_key = $this->getUniqueListKey($params);
// check for "admin" variable, because we are parsing front-end template from admin when using template editor feature
if ($this->Application->GetVar('admin') || !$this->Application->isAdmin) {
// add parent category to special, when on Front-End,
// because there can be many category lists on same page
$list_unique_key .= $parent_cat_id;
}
if ($list_unique_key == '') {
return parent::BuildListSpecial($params);
}
return crc32($list_unique_key);
}
function IsCurrent($params)
{
$object =& $this->getObject($params);
if ($object->GetID() == $this->Application->GetVar('m_cat_id')) {
return true;
}
else {
return false;
}
}
/**
* Substitutes category in last template base on current category
* This is required becasue when you navigate catalog using AJAX, last_template is not updated
* but when you open item edit from catalog last_template is used to build opener_stack
* So, if we don't substitute m_cat_id in last_template, after saving item we'll get redirected
* to the first category we've opened, not the one we navigated to using AJAX
*
* @param Array $params
*/
function UpdateLastTemplate($params)
{
$category_id = $this->Application->GetVar('m_cat_id');
$wid = $this->Application->GetVar('m_wid');
list($index_file, $env) = explode('|', $this->Application->RecallVar(rtrim('last_template_'.$wid, '_')), 2);
$vars_backup = Array ();
$vars = $this->Application->HttpQuery->processQueryString( str_replace('%5C', '\\', $env) );
foreach ($vars as $var_name => $var_value) {
$vars_backup[$var_name] = $this->Application->GetVar($var_name);
$this->Application->SetVar($var_name, $var_value);
}
// update required fields
$this->Application->SetVar('m_cat_id', $category_id);
$this->Application->Session->SaveLastTemplate($params['template']);
foreach ($vars_backup as $var_name => $var_value) {
$this->Application->SetVar($var_name, $var_value);
}
}
function GetParentCategory($params)
{
$parent_id = $this->Application->getBaseCategory();
$category_id = $this->Application->GetVar('m_cat_id');
if ($category_id != $parent_id) {
$sql = 'SELECT ParentId
FROM ' . $this->Application->getUnitOption($this->Prefix, 'TableName') . '
WHERE ' . $this->Application->getUnitOption($this->Prefix, 'IDField') . ' = ' . $category_id;
$parent_id = $this->Conn->GetOne($sql);
}
return $parent_id;
}
function InitCacheUpdater($params)
{
kUtil::safeDefine('CACHE_PERM_CHUNK_SIZE', 30);
$continue = $this->Application->GetVar('continue');
$total_cats = (int) $this->Conn->GetOne('SELECT COUNT(*) FROM '.TABLE_PREFIX.'Category');
if ($continue === false && $total_cats > CACHE_PERM_CHUNK_SIZE) {
// first step, if category count > CACHE_PERM_CHUNK_SIZE, then ask for cache update
return true;
}
if ($continue === false) {
// if we don't have to ask, then assume user selected "Yes" in permcache update dialog
$continue = 1;
}
$updater =& $this->Application->makeClass('kPermCacheUpdater', Array($continue));
/* @var $updater kPermCacheUpdater */
if ($continue === '0') { // No in dialog
$updater->clearData();
$this->Application->Redirect($params['destination_template']);
}
$ret = false; // don't ask for update
if ($continue == 1) { // Initial run
$updater->setData();
}
if ($continue == 2) { // Continuing
// called from AJAX request => returns percent
$needs_more = true;
while ($needs_more && $updater->iteration <= CACHE_PERM_CHUNK_SIZE) {
// until proceeeded in this step category count exceeds category per step limit
$needs_more = $updater->DoTheJob();
}
if ($needs_more) {
// still some categories are left for next step
$updater->setData();
}
else {
// all done, update left tree and redirect
$updater->SaveData();
$this->Application->HandleEvent($event, 'c:OnResetCMSMenuCache');
$this->Application->RemoveVar('PermCache_UpdateRequired');
$this->Application->StoreVar('RefreshStructureTree', 1);
$this->Application->Redirect($params['destination_template']);
}
$ret = $updater->getDonePercent();
}
return $ret;
}
/**
* Parses warning block, but with style="display: none;". Used during permissions saving from AJAX
*
* @param Array $params
* @return string
+ * @access protected
*/
- function SaveWarning($params)
+ protected function SaveWarning($params)
{
- if ($this->Prefix == 'st') {
+ if ( $this->Prefix == 'st' ) {
// don't use this method for other prefixes then Category, that use this tag processor
return parent::SaveWarning($params);
}
$main_prefix = getArrayValue($params, 'main_prefix');
- if ($main_prefix && $main_prefix != '$main_prefix') {
+ if ( $main_prefix && $main_prefix != '$main_prefix' ) {
$top_prefix = $main_prefix;
}
else {
$top_prefix = $this->Application->GetTopmostPrefix($this->Prefix);
}
- $temp_tables = substr($this->Application->GetVar($top_prefix.'_mode'), 0, 1) == 't';
- $modified = $this->Application->RecallVar($top_prefix.'_modified');
+ $temp_tables = substr($this->Application->GetVar($top_prefix . '_mode'), 0, 1) == 't';
+ $modified = $this->Application->RecallVar($top_prefix . '_modified');
- if (!$temp_tables) {
- $this->Application->RemoveVar($top_prefix.'_modified');
+ if ( !$temp_tables ) {
+ $this->Application->RemoveVar($top_prefix . '_modified');
return '';
}
$block_name = $this->SelectParam($params, 'render_as,name');
- if ($block_name) {
+ if ( $block_name ) {
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $block_name;
$block_params['edit_mode'] = $temp_tables ? 1 : 0;
$block_params['display'] = $temp_tables && $modified ? 1 : 0;
return $this->Application->ParseBlock($block_params);
}
- else {
- return $temp_tables && $modified ? 1 : 0;
- }
- return ;
+
+ return $temp_tables && $modified ? 1 : 0;
}
/**
* Allows to detect if this prefix has something in clipboard
*
* @param Array $params
* @return bool
*/
function HasClipboard($params)
{
$clipboard = $this->Application->RecallVar('clipboard');
if ($clipboard) {
$clipboard = unserialize($clipboard);
foreach ($clipboard as $prefix => $clipboard_data) {
foreach ($clipboard_data as $mode => $ids) {
if (count($ids)) return 1;
}
}
}
return 0;
}
/**
* Allows to detect if root category being edited
*
* @param Array $params
*/
function IsRootCategory($params)
{
$object =& $this->getObject($params);
+ /* @var $object CategoriesItem */
+
return $object->IsRoot();
}
/**
* Returns home category id
*
* @param Array $params
* @return int
*/
function HomeCategory($params)
{
return $this->Application->getBaseCategory();
}
/**
* Used for disabling "Home" and "Up" buttons in category list
*
* @param Array $params
* @return bool
*/
function ModuleRootCategory($params)
{
return $this->Application->GetVar('m_cat_id') == $this->Application->getBaseCategory();
}
function CatalogItemCount($params)
{
$params['skip_quering'] = true;
$object =& $this->GetList($params);
return $object->GetRecordsCount(false) != $object->GetRecordsCount() ? $object->GetRecordsCount().' / '.$object->GetRecordsCount(false) : $object->GetRecordsCount();
}
function InitCatalog($params)
{
$tab_prefixes = $this->Application->GetVar('tp'); // {all, <prefixes_list>, none}
if ($tab_prefixes === false) $tab_prefixes = 'all';
$skip_prefixes = isset($params['skip_prefixes']) && $params['skip_prefixes'] ? explode(',', $params['skip_prefixes']) : Array();
$replace_main = isset($params['replace_m']) && $params['replace_m'];
// get all prefixes available
$prefixes = Array();
foreach ($this->Application->ModuleInfo as $module_name => $module_data) {
$prefix = $module_data['Var'];
if ($prefix == 'adm'/* || $prefix == 'm'*/) continue;
if ($prefix == 'm' && $replace_main) {
$prefix = 'c';
}
$prefixes[] = $prefix;
}
if ($tab_prefixes == 'none') {
$skip_prefixes = array_unique(array_merge($skip_prefixes, $prefixes));
unset($skip_prefixes[ array_search($replace_main ? 'c' : 'm', $skip_prefixes) ]);
}
elseif ($tab_prefixes != 'all') {
// prefix list here
$tab_prefixes = explode(',', $tab_prefixes); // list of prefixes that should stay
$skip_prefixes = array_unique(array_merge($skip_prefixes, array_diff($prefixes, $tab_prefixes)));
}
$params['name'] = $params['render_as'];
$params['skip_prefixes'] = implode(',', $skip_prefixes);
return $this->Application->ParseBlock($params);
}
/**
* Determines, that printed category/menu item is currently active (will also match parent category)
*
* @param Array $params
* @return bool
*/
function IsActive($params)
{
static $current_path = null;
- if (!isset($current_path)) {
+ if ( !isset($current_path) ) {
$sql = 'SELECT ParentPath
FROM ' . TABLE_PREFIX . 'Category
WHERE CategoryId = ' . (int)$this->Application->GetVar('m_cat_id');
$current_path = $this->Conn->GetOne($sql);
}
- if (array_key_exists('parent_path', $params)) {
+ if ( array_key_exists('parent_path', $params) ) {
$test_path = $params['parent_path'];
}
else {
$template = $params['template'];
- if ($template) {
+ if ( $template ) {
// when using from "c:CachedMenu" tag
$sql = 'SELECT ParentPath
FROM ' . TABLE_PREFIX . 'Category
WHERE NamedParentPath = ' . $this->Conn->qstr('Content/' . $template);
$test_path = $this->Conn->GetOne($sql);
}
else {
// when using from "c:PrintList" tag
$cat_id = array_key_exists('cat_id', $params) && $params['cat_id'] ? $params['cat_id'] : false;
- if ($cat_id === false) {
+ if ( $cat_id === false ) {
// category not supplied -> get current from PrintList
$category =& $this->getObject($params);
}
else {
- if ("$cat_id" == 'Root') {
+ if ( "$cat_id" == 'Root' ) {
$cat_id = $this->Application->findModule('Name', $params['module'], 'RootCat');
}
$category =& $this->Application->recallObject($this->Prefix . '.-c' . $cat_id, $this->Prefix, Array ('skip_autoload' => true));
+ /* @var $category CategoriesItem */
+
$category->Load($cat_id);
}
$test_path = $category->GetDBField('ParentPath');
}
}
return strpos($current_path, $test_path) !== false;
}
/**
* Checks if user have one of required permissions
*
* @param Array $params
* @return bool
*/
function HasPermission($params)
{
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$params['raise_warnings'] = 0;
$object =& $this->getObject($params);
/* @var $object kDBItem */
$params['cat_id'] = $object->isLoaded() ? $object->GetDBField('ParentPath') : $this->Application->GetVar('m_cat_id');
return $perm_helper->TagPermissionCheck($params);
}
/**
* Prepares name for field with event in it (used only on front-end)
*
* @param Array $params
* @return string
*/
function SubmitName($params)
{
return 'events[' . $this->Prefix . '][' . $params['event'] . ']';
}
/**
* Returns last modification date of items in category / system
*
* @param Array $params
* @return string
*/
function LastUpdated($params)
{
$category_id = (int)$this->Application->GetVar('m_cat_id');
$local = array_key_exists('local', $params) && ($category_id > 0) ? $params['local'] : false;
$serial_name = $this->Application->incrementCacheSerial('c', $local ? $category_id : null, false);
$cache_key = 'category_last_updated[%' . $serial_name . '%]';
$row_data = $this->Application->getCache($cache_key);
- if ($row_data === false) {
- if ($local && ($category_id > 0)) {
+ if ( $row_data === false ) {
+ if ( $local && ($category_id > 0) ) {
// scan only current category & it's children
list ($tree_left, $tree_right) = $this->Application->getTreeIndex($category_id);
$sql = 'SELECT MAX(Modified) AS ModDate, MAX(CreatedOn) AS NewDate
FROM ' . TABLE_PREFIX . 'Category
WHERE TreeLeft BETWEEN ' . $tree_left . ' AND ' . $tree_right;
}
else {
// scan all categories in system
$sql = 'SELECT MAX(Modified) AS ModDate, MAX(CreatedOn) AS NewDate
FROM ' . TABLE_PREFIX . 'Category';
}
$this->Conn->nextQueryCachable = true;
$row_data = $this->Conn->GetRow($sql);
$this->Application->setCache($cache_key, $row_data);
}
- if (!$row_data) {
+ if ( !$row_data ) {
return '';
}
- $date = $row_data[ $row_data['NewDate'] > $row_data['ModDate'] ? 'NewDate' : 'ModDate' ];
+ $date = $row_data[$row_data['NewDate'] > $row_data['ModDate'] ? 'NewDate' : 'ModDate'];
// format date
$format = isset($params['format']) ? $params['format'] : '_regional_DateTimeFormat';
- if (preg_match("/_regional_(.*)/", $format, $regs)) {
+
+ if ( preg_match("/_regional_(.*)/", $format, $regs) ) {
$lang =& $this->Application->recallObject('lang.current');
- if ($regs[1] == 'DateTimeFormat') {
+ /* @var $lang LanguagesItem */
+
+ if ( $regs[1] == 'DateTimeFormat' ) {
// combined format
$format = $lang->GetDBField('DateFormat') . ' ' . $lang->GetDBField('TimeFormat');
}
else {
// simple format
$format = $lang->GetDBField($regs[1]);
}
}
return adodb_date($format, $date);
}
function CategoryItemCount($params)
{
$object =& $this->getObject($params);
/* @var $object kDBList */
$params['cat_id'] = $object->GetID();
$count_helper =& $this->Application->recallObject('CountHelper');
/* @var $count_helper kCountHelper */
return $count_helper->CategoryItemCount($params['prefix'], $params);
}
/**
* Returns prefix + any word (used for shared between categories per page settings)
*
* @param Array $params
* @return string
*/
function VarName($params)
{
return $this->Prefix.'_'.$params['type'];
}
/**
* Checks if current category is valid symbolic link to another category
*
* @param Array $params
* @return string
*/
function IsCategorySymLink($params)
{
$object =& $this->getObject($params);
/* @var $object kDBList */
$sym_category_id = $object->GetDBField('SymLinkCategoryId');
if (is_null($sym_category_id))
{
return false;
}
$id_field = $this->Application->getUnitOption($this->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($this->Prefix, 'TableName');
$sql = 'SELECT '.$id_field.'
FROM '.$table_name.'
WHERE '.$id_field.' = '.$sym_category_id;
return $this->Conn->GetOne($sql)? true : false;
}
/**
* Returns module prefix based on root category for given
*
* @param Array $params
* @return string
*/
function GetModulePrefix($params)
{
$object =& $this->getObject($params);
/* @var $object kDBItem */
$parent_path = explode('|', substr($object->GetDBField('ParentPath'), 1, -1));
$category_helper =& $this->Application->recallObject('CategoryHelper');
/* @var $category_helper CategoryHelper */
$module_info = $category_helper->getCategoryModule($params, $parent_path);
return $module_info['Var'];
}
function ImageSrc($params)
{
list ($ret, $tag_processed) = $this->processAggregatedTag('ImageSrc', $params, $this->getPrefixSpecial());
return $tag_processed ? $ret : false;
}
function PageLink($params)
{
$params['m_cat_page'] = $this->Application->GetVar($this->getPrefixSpecial() . '_Page');
return parent::PageLink($params);
}
/**
* Returns spelling suggestions against search keyword
*
* @param Array $params
* @return string
+ * @access protected
*/
- function SpellingSuggestions($params)
+ protected function SpellingSuggestions($params)
{
$keywords = kUtil::unhtmlentities( trim($this->Application->GetVar('keywords')) );
- if (!$keywords) {
- return ;
+ if ( !$keywords ) {
+ return '';
}
// 1. try to get already cached suggestion
$cache_key = 'search.suggestion[%SpellingDictionary%]:' . $keywords;
$suggestion = $this->Application->getCache($cache_key);
- if ($suggestion !== false) {
+ if ( $suggestion !== false ) {
return $suggestion;
}
$table_name = $this->Application->getUnitOption('spelling-dictionary', 'TableName');
// 2. search suggestion in database
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT SuggestedCorrection
FROM ' . $table_name . '
WHERE MisspelledWord = ' . $this->Conn->qstr($keywords);
$suggestion = $this->Conn->GetOne($sql);
- if ($suggestion !== false) {
+ if ( $suggestion !== false ) {
$this->Application->setCache($cache_key, $suggestion);
+
return $suggestion;
}
// 3. suggestion not found in database, ask webservice
$app_id = $this->Application->ConfigValue('YahooApplicationId');
$url = 'http://search.yahooapis.com/WebSearchService/V1/spellingSuggestion?appid=' . $app_id . '&query=';
$curl_helper =& $this->Application->recallObject('CurlHelper');
/* @var $curl_helper kCurlHelper */
- $xml_data = $curl_helper->Send($url . urlencode($keywords));
+ $xml_data = $curl_helper->Send( $url . urlencode($keywords) );
$xml_helper =& $this->Application->recallObject('kXMLHelper');
/* @var $xml_helper kXMLHelper */
$root_node =& $xml_helper->Parse($xml_data);
+ /* @var $root_node kXMLNode */
$result = $root_node->FindChild('RESULT');
/* @var $result kXMLNode */
- if (is_object($result)) {
+ if ( is_object($result) ) {
// webservice responded -> save in local database
- $fields_hash = Array (
- 'MisspelledWord' => $keywords,
- 'SuggestedCorrection' => $result->Data,
- );
+ $fields_hash = Array ('MisspelledWord' => $keywords, 'SuggestedCorrection' => $result->Data);
$this->Conn->doInsert($fields_hash, $table_name);
$this->Application->setCache($cache_key, $result->Data);
return $result->Data;
}
return '';
}
/**
* Shows link for searching by suggested word
*
* @param Array $params
* @return string
*/
function SuggestionLink($params)
{
$params['keywords'] = $this->SpellingSuggestions($params);
return $this->Application->ProcessParsedTag('m', 'Link', $params);
}
function InitCatalogTab($params)
{
$tab_params['mode'] = $this->Application->GetVar('tm'); // single/multi selection possible
$tab_params['special'] = $this->Application->GetVar('ts'); // use special for this tab
$tab_params['dependant'] = $this->Application->GetVar('td'); // is grid dependant on categories grid
// set default params (same as in catalog)
if ($tab_params['mode'] === false) $tab_params['mode'] = 'multi';
if ($tab_params['special'] === false) $tab_params['special'] = '';
if ($tab_params['dependant'] === false) $tab_params['dependant'] = 'yes';
// pass params to block with tab content
$params['name'] = $params['render_as'];
$special = $tab_params['special'] ? $tab_params['special'] : $this->Special;
$params['prefix'] = trim($this->Prefix.'.'.$special, '.');
$prefix_append = $this->Application->GetVar('prefix_append');
if ($prefix_append) {
$params['prefix'] .= $prefix_append;
}
$default_grid = array_key_exists('default_grid', $params) ? $params['default_grid'] : 'Default';
$radio_grid = array_key_exists('radio_grid', $params) ? $params['radio_grid'] : 'Radio';
$params['cat_prefix'] = trim('c.'.($tab_params['special'] ? $tab_params['special'] : $this->Special), '.');
$params['tab_mode'] = $tab_params['mode'];
$params['grid_name'] = ($tab_params['mode'] == 'multi') ? $default_grid : $radio_grid;
$params['tab_dependant'] = $tab_params['dependant'];
$params['show_category'] = $tab_params['special'] == 'showall' ? 1 : 0; // this is advanced view -> show category name
if ($special == 'showall' || $special == 'user') {
$params['grid_name'] .= 'ShowAll';
}
// use $pass_params to be able to pass 'tab_init' parameter from m_ModuleInclude tag
return $this->Application->ParseBlock($params, 1);
}
/**
* Show CachedNavbar of current item primary category
*
* @param Array $params
* @return string
*/
function CategoryName($params)
{
// show category cachednavbar of
$object =& $this->getObject($params);
$category_id = isset($params['cat_id']) ? $params['cat_id'] : $object->GetDBField('CategoryId');
$cache_key = 'category_paths[%CIDSerial:' . $category_id . '%][%PhrasesSerial%][Adm:' . (int)$this->Application->isAdmin . ']';
$category_path = $this->Application->getCache($cache_key);
if ($category_path === false) {
// not chached
if ($category_id > 0) {
$cached_navbar = $object->GetField('CachedNavbar');
if ($category_id == $object->GetDBField('ParentId')) {
// parent category cached navbar is one element smaller, then current ones
$cached_navbar = explode('&|&', $cached_navbar);
array_pop($cached_navbar);
$cached_navbar = implode('&|&', $cached_navbar);
}
else {
// no relation with current category object -> query from db
$language_id = (int)$this->Application->GetVar('m_lang');
if (!$language_id) {
$language_id = 1;
}
$sql = 'SELECT l' . $language_id . '_CachedNavbar
FROM ' . $object->TableName . '
WHERE ' . $object->IDField . ' = ' . $category_id;
$cached_navbar = $this->Conn->GetOne($sql);
}
$cached_navbar = preg_replace('/^(Content&\|&|Content)/i', '', $cached_navbar);
$category_path = trim($this->CategoryName( Array('cat_id' => 0) ).' > '.str_replace('&|&', ' > ', $cached_navbar), ' > ');
}
else {
$category_path = $this->Application->Phrase(($this->Application->isAdmin ? 'la_' : 'lu_') . 'rootcategory_name');
}
$this->Application->setCache($cache_key, $category_path);
}
return $category_path;
}
// structure related
/**
* Returns page object based on requested params
*
* @param Array $params
- * @return PagesItem
+ * @return CategoriesItem
*/
function &_getPage($params)
{
$page =& $this->Application->recallObject($this->Prefix . '.-virtual', null, $params);
/* @var $page kDBItem */
// 1. load by given id
$page_id = array_key_exists('page_id', $params) ? $params['page_id'] : false;
if ($page_id) {
if ($page_id != $page->GetID()) {
// load if different
$page->Load($page_id);
}
return $page;
}
// 2. load by template
$template = array_key_exists('page', $params) ? $params['page'] : '';
if (!$template) {
$template = $this->Application->GetVar('t');
}
// different path in structure AND design template differes from requested template
$structure_path_match = strtolower( $page->GetDBField('NamedParentPath') ) == strtolower('Content/' . $template);
$design_match = $page->GetDBField('CachedTemplate') == $template;
if (!$structure_path_match && !$design_match) {
// Same sql like in "c:getPassedID". Load, when current page object doesn't match requested page object
$themes_helper =& $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$page_id = $themes_helper->getPageByTemplate($template);
$page->Load($page_id);
}
return $page;
}
/**
* Returns requested content block content of current or specified page
*
* @param Array $params
* @return string
*/
function ContentBlock($params)
{
$num = getArrayValue($params, 'num');
if (!$num) {
return 'NO CONTENT NUM SPECIFIED';
}
$page =& $this->_getPage($params);
/* @var $page kDBItem */
if (!$page->isLoaded()) {
// page is not created yet => all blocks are empty
return '';
}
$page_id = $page->GetID();
$content =& $this->Application->recallObject('content.-block', null, Array ('skip_autoload' => true));
/* @var $content kDBItem */
$data = Array ('PageId' => $page_id, 'ContentNum' => $num);
$content->Load($data);
if (!$content->isLoaded()) {
// bug: missing content blocks are created even if user have no SMS-management rights
$content->SetFieldsFromHash($data);
$content->Create();
}
$edit_code_before = $edit_code_after = '';
if (EDITING_MODE == EDITING_MODE_CONTENT) {
$bg_color = isset($params['bgcolor']) ? $params['bgcolor'] : '#ffffff';
$url_params = Array (
'pass' => 'm,c,content',
'm_opener' => 'd',
'c_id' => $page->GetID(),
'content_id' => $content->GetID(),
'front' => 1,
'admin' => 1,
'__URLENCODE__' => 1,
'__NO_REWRITE__'=> 1,
'escape' => 1,
'index_file' => 'index.php',
// 'bgcolor' => $bg_color,
// '__FORCE_SID__' => 1
);
// link from Front-End to admin, don't remove "index.php"
$edit_url = $this->Application->HREF('categories/edit_content', ADMIN_DIRECTORY, $url_params, 'index.php');
$edit_code_before = '
<div class="cms-edit-btn-container">
<div class="cms-edit-btn" onclick="$form_name=\'kf_cont_'.$content->GetID().'\'; std_edit_item(\'content\', \'categories/edit_content\');">
<div class="cms-btn-image">
<img src="' . $this->Application->BaseURL() . 'core/admin_templates/img/top_frame/icons/content_mode.png" width="15" height="16" alt=""/>
</div>
<div class="cms-btn-text">' . $this->Application->Phrase('la_btn_EditContent', false, true) . ' '.(defined('DEBUG_MODE') && DEBUG_MODE ? " - #{$num}" : '').'</div>
</div>
<div class="cms-btn-content">';
$edit_form = '<form method="POST" style="display: inline; margin: 0px" name="kf_cont_'.$content->GetID().'" id="kf_cont_'.$content->GetID().'" action="'.$edit_url.'">';
$edit_form .= '<input type="hidden" name="c_id" value="'.$page->GetID().'"/>';
$edit_form .= '<input type="hidden" name="content_id" value="'.$content->GetID().'"/>';
$edit_form .= '<input type="hidden" name="front" value="1"/>';
$edit_form .= '<input type="hidden" name="bgcolor" value="'.$bg_color.'"/>';
$edit_form .= '<input type="hidden" name="m_lang" value="'.$this->Application->GetVar('m_lang').'"/>';
$edit_form .= '</form>';
$edit_code_after = '</div></div>';
if (array_key_exists('forms_later', $params) && $params['forms_later']) {
$all_forms = $this->Application->GetVar('all_forms');
$this->Application->SetVar('all_forms', $all_forms . $edit_form);
}
else {
$edit_code_after .= $edit_form;
}
}
if ($this->Application->GetVar('_editor_preview_') == 1) {
$data = $this->Application->RecallVar('_editor_preview_content_');
} else {
$data = $content->GetField('Content');
}
$data = $edit_code_before . $this->_transformContentBlockData($data, $params) . $edit_code_after;
if ($data != '') {
$this->Application->Parser->DataExists = true;
}
return $data;
}
/**
* Apply all kinds of content block data transformations without rewriting ContentBlock tag
*
* @param string $data
* @return string
*/
function _transformContentBlockData(&$data, $params)
{
return $data;
}
/**
* Returns current page name or page based on page/page_id parameters
*
* @param Array $params
* @return string
* @todo Used?
*/
function PageName($params)
{
$page =& $this->_getPage($params);
return $page->GetDBField('Name');
}
/**
* Returns current/given page information
*
* @param Array $params
* @return string
*/
function PageInfo($params)
{
$page =& $this->_getPage($params);
switch ($params['type']) {
case 'title':
$db_field = 'Title';
break;
case 'htmlhead_title':
$db_field = 'Name';
break;
case 'meta_title':
$db_field = 'MetaTitle';
break;
case 'menu_title':
$db_field = 'MenuTitle';
break;
case 'meta_keywords':
$db_field = 'MetaKeywords';
$cat_field = 'Keywords';
break;
case 'meta_description':
$db_field = 'MetaDescription';
$cat_field = 'Description';
break;
case 'tracking':
case 'index_tools':
if (!EDITING_MODE) {
$tracking = $page->GetDBField('IndexTools');
return $tracking ? $tracking : $this->Application->ConfigValue('cms_DefaultTrackingCode');
}
// no break here on purpose
default:
return '';
}
$default = isset($params['default']) ? $params['default'] : '';
$val = $page->GetField($db_field);
if (!$default) {
if ($this->Application->isModuleEnabled('In-Portal')) {
if (!$val && ($params['type'] == 'meta_keywords' || $params['type'] == 'meta_description')) {
// take category meta if it's not set for the page
return $this->Application->ProcessParsedTag('c', 'Meta', Array('name' => $cat_field));
}
}
}
if (isset($params['force_default']) && $params['force_default']) {
return $default;
}
if (preg_match('/^_Auto:/', $val)) {
$val = $default;
/*if ($db_field == 'Title') {
$page->SetDBField($db_field, $default);
$page->Update();
}*/
}
elseif ($page->GetID() == false) {
return $default;
}
return $val;
}
/**
* Includes admin css and js, that are required for cms usage on Front-Edn
*
- *
* @param Array $params
* @return string
+ * @access protected
*/
- function EditingScripts($params)
+ protected function EditingScripts($params)
{
- if ($this->Application->GetVar('admin_scripts_included') || !EDITING_MODE) {
- return ;
+ if ( $this->Application->GetVar('admin_scripts_included') || !EDITING_MODE ) {
+ return '';
}
$this->Application->SetVar('admin_scripts_included', 1);
$js_url = $this->Application->BaseURL() . 'core/admin_templates/js';
$minify_helper =& $this->Application->recallObject('MinifyHelper');
/* @var $minify_helper MinifyHelper */
$to_compress = Array (
$js_url . '/jquery/thickbox/thickbox.css',
$js_url . '/../incs/cms.css',
);
- $css_compressed = $minify_helper->CompressScriptTag( Array ('files' => implode('|', $to_compress)) );
+ $css_compressed = $minify_helper->CompressScriptTag(Array ('files' => implode('|', $to_compress)));
$ret = '<link rel="stylesheet" href="' . $css_compressed . '" type="text/css" media="screen"/>' . "\n";
- if (EDITING_MODE == EDITING_MODE_DESIGN) {
+ if ( EDITING_MODE == EDITING_MODE_DESIGN ) {
$ret .= ' <style type="text/css" media="all">
div.movable-element .movable-header { cursor: move; }
</style>';
}
-
$ret .= '<script type="text/javascript" src="' . $js_url . '/jquery/jquery.pack.js"></script>' . "\n";
$ret .= '<script type="text/javascript" src="' . $js_url . '/jquery/jquery-ui.custom.min.js"></script>' . "\n";
$to_compress = Array (
$js_url . '/is.js',
$js_url . '/application.js',
$js_url . '/script.js',
$js_url . '/jquery/thickbox/thickbox.js',
$js_url . '/template_manager.js',
);
$js_compressed = $minify_helper->CompressScriptTag( Array ('files' => implode('|', $to_compress)) );
$ret .= '<script type="text/javascript" src="' . $js_compressed . '"></script>' . "\n";
$ret .= '<script language="javascript">' . "\n";
$ret .= "TB.pathToImage = '" . $js_url . "/jquery/thickbox/loadingAnimation.gif';" . "\n";
$template = $this->Application->GetVar('t');
$theme_id = $this->Application->GetVar('m_theme');
$url_params = Array ('block' => '#BLOCK#', 'theme-file_event' => '#EVENT#', 'theme_id' => $theme_id, 'source' => $template, 'pass' => 'all,theme-file', 'front' => 1, 'm_opener' => 'd', '__NO_REWRITE__' => 1, 'no_amp' => 1);
$edit_template_url = $this->Application->HREF('themes/template_edit', ADMIN_DIRECTORY, $url_params, 'index.php');
$url_params = Array ('theme-file_event' => 'OnSaveLayout', 'source' => $template, 'pass' => 'all,theme-file', '__NO_REWRITE__' => 1, 'no_amp' => 1);
$save_layout_url = $this->Application->HREF('index', '', $url_params);
$this_url = $this->Application->HREF('', '', Array ('editing_mode' => '#EDITING_MODE#', '__NO_REWRITE__' => 1, 'no_amp' => 1));
$ret .= "var aTemplateManager = new TemplateManager('" . $edit_template_url . "', '" . $this_url . "', '" . $save_layout_url . "', " . (int)EDITING_MODE . ");\n";
$ret .= "var main_title = '" . addslashes( $this->Application->ConfigValue('Site_Name') ) . "';" . "\n";
$use_popups = (int)$this->Application->ConfigValue('UsePopups');
$ret .= "var \$use_popups = " . ($use_popups > 0 ? 'true' : 'false') . ";\n";
$ret .= "var \$modal_windows = " . ($use_popups == 2 ? 'true' : 'false') . ";\n";
- if (EDITING_MODE != EDITING_MODE_BROWSE) {
+ if ( EDITING_MODE != EDITING_MODE_BROWSE ) {
$ret .= "var base_url = '" . $this->Application->BaseURL() . "';" . "\n";
$ret .= 'TB.closeHtml = \'<img src="' . $js_url . '/../img/close_window15.gif" width="15" height="15" style="border-width: 0px;" alt="close"/><br/>\';' . "\n";
- $url_params = Array('m_theme' => '', 'pass' => 'm', 'm_opener' => 'r', '__NO_REWRITE__' => 1, 'no_amp' => 1);
+ $url_params = Array ('m_theme' => '', 'pass' => 'm', 'm_opener' => 'r', '__NO_REWRITE__' => 1, 'no_amp' => 1);
$browse_url = $this->Application->HREF('catalog/catalog', ADMIN_DIRECTORY, $url_params, 'index.php');
$browse_url = preg_replace('/&(admin|editing_mode)=[\d]/', '', $browse_url);
$ret .= '
var topmost = window.top;
topmost.document.title = document.title + \' - ' . addslashes($this->Application->Phrase('la_AdministrativeConsole', false)) . '\';
- t = \''.$this->Application->GetVar('t').'\';
+ t = \'' . $this->Application->GetVar('t') . '\';
if (window.parent.frames["menu"] != undefined) {
if ( $.isFunction(window.parent.frames["menu"].SyncActive) ) {
window.parent.frames["menu"].SyncActive("' . $browse_url . '");
}
}
';
}
$ret .= '</script>' . "\n";
- if (EDITING_MODE != EDITING_MODE_BROWSE) {
+ if ( EDITING_MODE != EDITING_MODE_BROWSE ) {
// add form, so admin scripts could work
$ret .= '<form id="kernel_form" name="kernel_form" enctype="multipart/form-data" method="post" action="' . $browse_url . '">
<input type="hidden" name="MAX_FILE_SIZE" id="MAX_FILE_SIZE" value="' . MAX_UPLOAD_SIZE . '" />
<input type="hidden" name="sid" id="sid" value="' . $this->Application->GetSID() . '" />
</form>';
}
return $ret;
}
/**
* Prints "Edit Page" button on cms page
*
* @param Array $params
* @return string
*/
function EditPage($params)
{
if (!EDITING_MODE) {
return '';
}
$display_mode = array_key_exists('mode', $params) ? $params['mode'] : false;
$edit_code = '';
$page =& $this->_getPage($params);
if (!$page->isLoaded() || (($display_mode != 'end') && (EDITING_MODE == EDITING_MODE_BROWSE))) {
// when "EditingScripts" tag is not used, make sure, that scripts are also included
return $this->EditingScripts($params);
}
// show "EditPage" button only for pages, that exists in structure
if ($display_mode != 'end') {
$edit_btn = '';
if (EDITING_MODE == EDITING_MODE_CONTENT) {
$url_params = Array(
'pass' => 'm,c',
'm_opener' => 'd',
'c_id' => $page->GetID(),
'c_mode' => 't',
'c_event' => 'OnEdit',
'front' => 1,
'__URLENCODE__' => 1,
'__NO_REWRITE__'=> 1,
'index_file' => 'index.php',
);
$edit_url = $this->Application->HREF('categories/categories_edit', ADMIN_DIRECTORY, $url_params);
$edit_btn .= '
<div class="cms-section-properties-btn"' . ($display_mode === false ? ' style="margin: 0px;"' : '') . ' onmouseover="window.status=\'' . addslashes($edit_url) . '\'; return true" onclick="$form_name=\'kf_'.$page->GetID().'\'; std_edit_item(\'c\', \'categories/categories_edit\');">
<div class="cms-btn-image">
<img src="' . $this->Application->BaseURL() . 'core/admin_templates/img/top_frame/icons/section_properties.png" width="15" height="16" alt=""/>
</div>
<div class="cms-btn-text">' . $this->Application->Phrase('la_btn_SectionProperties', false, true) . '</div>
</div>' . "\n";
} elseif (EDITING_MODE == EDITING_MODE_DESIGN) {
$url_params = Array(
'pass' => 'm,theme,theme-file',
'm_opener' => 'd',
'theme_id' => $this->Application->GetVar('m_theme'),
'theme_mode' => 't',
'theme_event' => 'OnEdit',
'theme-file_id' => $this->_getThemeFileId(),
'front' => 1,
'__URLENCODE__' => 1,
'__NO_REWRITE__'=> 1,
'index_file' => 'index.php',
);
$edit_url = $this->Application->HREF('themes/file_edit', ADMIN_DIRECTORY, $url_params);
$edit_btn .= '
<div class="cms-layout-btn-container"' . ($display_mode === false ? ' style="margin: 0px;"' : '') . '>
<div class="cms-save-layout-btn" onclick="aTemplateManager.saveLayout(); return false;">
<div class="cms-btn-image">
<img src="' . $this->Application->BaseURL() . 'core/admin_templates/img/top_frame/icons/save_button.gif" width="16" height="16" alt=""/>
</div>
<div class="cms-btn-text">' . $this->Application->Phrase('la_btn_SaveChanges', false, true) . '</div>
</div>
<div class="cms-cancel-layout-btn" onclick="aTemplateManager.cancelLayout(); return false;">
<div class="cms-btn-image">
<img src="' . $this->Application->BaseURL() . 'core/admin_templates/img/top_frame/icons/cancel_button.gif" width="16" height="16" alt=""/>
</div>
<div class="cms-btn-text">' . $this->Application->Phrase('la_btn_Cancel', false, true) . '</div>
</div>
</div>
<div class="cms-section-properties-btn"' . ($display_mode === false ? ' style="margin: 0px;"' : '') . ' onmouseover="window.status=\'' . addslashes($edit_url) . '\'; return true" onclick="$form_name=\'kf_'.$page->GetID().'\'; std_edit_item(\'theme\', \'themes/file_edit\');">
<div class="cms-btn-image">
<img src="' . $this->Application->BaseURL() . 'core/admin_templates/img/top_frame/icons/section_properties.png" width="15" height="16" alt=""/>
</div>
<div class="cms-btn-text">' . $this->Application->Phrase('la_btn_SectionTemplate', false, true) . '</div>
</div>' . "\n";
}
if ($display_mode == 'start') {
// button with border around the page
$edit_code .= '<div class="cms-section-properties-btn-container">' . $edit_btn . '<div class="cms-btn-content">';
}
else {
// button without border around the page
$edit_code .= $edit_btn;
}
}
if ($display_mode == 'end') {
// draw border around the page
$edit_code .= '</div></div>';
}
if ($display_mode != 'end') {
$edit_code .= '<form method="POST" style="display: inline; margin: 0px" name="kf_'.$page->GetID().'" id="kf_'.$page->GetID().'" action="'.$edit_url.'"></form>';
// when "EditingScripts" tag is not used, make sure, that scripts are also included
$edit_code .= $this->EditingScripts($params);
}
return $edit_code;
}
function _getThemeFileId()
{
$template = $this->Application->GetVar('t');
if (!$this->Application->TemplatesCache->TemplateExists($template) && !$this->Application->isAdmin) {
$cms_handler =& $this->Application->recallObject($this->Prefix . '_EventHandler');
/* @var $cms_handler CategoriesEventHandler */
$template = ltrim($cms_handler->GetDesignTemplate(), '/');
}
$file_path = dirname($template) == '.' ? '' : '/' . dirname($template);
$file_name = basename($template);
$sql = 'SELECT FileId
FROM ' . TABLE_PREFIX . 'ThemeFiles
WHERE (ThemeId = ' . (int)$this->Application->GetVar('m_theme') . ') AND (FilePath = ' . $this->Conn->qstr($file_path) . ') AND (FileName = ' . $this->Conn->qstr($file_name . '.tpl') . ')';
return $this->Conn->GetOne($sql);
}
/**
* Builds site menu
*
* @param Array $params
* @return string
*/
function CachedMenu($params)
{
$menu_helper =& $this->Application->recallObject('MenuHelper');
/* @var $menu_helper MenuHelper */
return $menu_helper->menuTag($this->getPrefixSpecial(), $params);
}
/**
* Trick to allow some kind of output formatting when using CachedMenu tag
*
* @param Array $params
* @return bool
*/
function SplitColumn($params)
{
return $this->Application->GetVar($params['i']) > ceil($params['total'] / $params['columns']);
}
/**
* Returns direct children count of given category
*
* @param Array $params
* @return int
*/
function HasSubCats($params)
{
$sql = 'SELECT COUNT(*)
FROM ' . TABLE_PREFIX . 'Category
WHERE ParentId = ' . $params['cat_id'];
return $this->Conn->GetOne($sql);
}
/**
* Prints sub-pages of given/current page.
*
* @param Array $params
* @return string
* @todo This could be reached by using "parent_cat_id" parameter. Only difference here is new block parameter "path". Need to rewrite.
*/
function PrintSubPages($params)
{
$list =& $this->Application->recallObject($this->getPrefixSpecial(), $this->Prefix.'_List', $params);
/* @var $list kDBList */
$category_id = array_key_exists('category_id', $params) ? $params['category_id'] : $this->Application->GetVar('m_cat_id');
$list->addFilter('current_pages', TABLE_PREFIX . 'CategoryItems.CategoryId = ' . $category_id);
$list->Query();
$list->GoFirst();
$o = '';
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
while (!$list->EOL()) {
$block_params['path'] = $list->GetDBField('Path');
$o .= $this->Application->ParseBlock($block_params);
$list->GoNext();
}
return $o;
}
/**
* Builds link for browsing current page on Front-End
*
* @param Array $params
* @return string
*/
function PageBrowseLink($params)
{
$object =& $this->getObject($params);
$themes_helper =& $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$site_config_helper =& $this->Application->recallObject('SiteConfigHelper');
/* @var $site_config_helper SiteConfigHelper */
$settings = $site_config_helper->getSettings();
$url_params = Array (
'm_cat_id' => $object->GetID(),
'm_theme' => $themes_helper->getCurrentThemeId(),
'editing_mode' => $settings['default_editing_mode'],
'pass' => 'm',
'admin' => 1,
'index_file' => 'index.php'
);
if ($this->Application->ConfigValue('UseModRewrite')) {
$url_params['__MOD_REWRITE__'] = 1;
}
return $this->Application->HREF($object->GetDBField('NamedParentPath'), '_FRONT_END_', $url_params);
}
function DirectLink($params)
{
$object =& $this->getObject($params);
$themes_helper =& $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$url_params = Array (
'm_cat_id' => $object->GetID(),
'm_theme' => $themes_helper->getCurrentThemeId(),
'pass' => 'm',
'index_file' => 'index.php',
'authkey' => $object->GetDBField('DirectLinkAuthKey'),
);
if ($this->Application->ConfigValue('UseModRewrite')) {
$url_params['__MOD_REWRITE__'] = 1;
}
return $this->Application->HREF($object->GetDBField('NamedParentPath'), '_FRONT_END_', $url_params);
}
/**
* Builds link to cms page (used?)
*
* @param Array $params
* @return string
*/
function ContentPageLink($params)
{
$object =& $this->getObject($params);
$params['t'] = $object->GetDBField('NamedParentPath');
$params['m_cat_id'] = 0;
return $this->Application->ProcessParsedTag('m', 'Link', $params);
}
/**
* Prepares cms page description for search result page
*
* @param Array $params
* @return string
*/
function SearchDescription($params)
{
$object =& $this->getObject($params);
$desc = $object->GetField('MetaDescription');
if (!$desc) {
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'PageContent
WHERE PageId = ' . $object->GetID() . ' AND ContentNum = 1';
$content = $this->Conn->GetRow($sql);
if ($content['l'.$this->Application->GetVar('m_lang').'_Content']) {
$desc = $content['l'.$this->Application->GetVar('m_lang').'_Content'];
}
else {
$desc = $content['l'.$this->Application->GetDefaultLanguageId().'_Content'];
}
}
return mb_substr($desc, 0, 300).(mb_strlen($desc) > 300 ? '...' : '');
}
/**
* Simplified version of "c:CategoryLink" for "c:PrintList"
*
* @param Array $params
* @return string
* @todo Used? Needs refactoring.
*/
function EnterCatLink($params)
{
$object =& $this->getObject($params);
$url_params = Array ('pass' => 'm', 'm_cat_id' => $object->GetID());
return $this->Application->HREF($params['template'], '', $url_params);
}
/**
* Simplified version of "c:CategoryPath", that do not use blocks for rendering
*
* @param Array $params
* @return string
* @todo Used? Maybe needs to be removed.
*/
function PagePath($params)
{
$object =& $this->getObject($params);
$path = $object->GetField('CachedNavbar');
if ($path) {
$items = explode('&|&', $path);
array_shift($items);
return implode(' -&gt; ', $items);
}
return '';
}
/**
* Returns configuration variable value
*
* @param Array $params
* @return string
* @todo Needs to be replaced with "m:GetConfig" tag; Not used now (were used on structure_edit.tpl).
*/
function AllowManualFilenames($params)
{
return $this->Application->ConfigValue('ProjCMSAllowManualFilenames');
}
/**
* Draws path to current page (each page can be link to it)
*
* @param Array $params
* @return string
*/
function CurrentPath($params)
{
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $block_params['render_as'];
$object =& $this->Application->recallObject($this->Prefix);
/* @var $object kDBItem */
$category_ids = explode('|', substr($object->GetDBField('ParentPath'), 1, -1));
$id_field = $this->Application->getUnitOption($this->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($this->Prefix, 'TableName');
$language = (int)$this->Application->GetVar('m_lang');
if (!$language) {
$language = 1;
}
$sql = 'SELECT l'.$language.'_Name AS Name, NamedParentPath
FROM '.$table_name.'
WHERE '.$id_field.' IN ('.implode(',', $category_ids).')';
$categories_data = $this->Conn->Query($sql);
$ret = '';
foreach ($categories_data as $index => $category_data) {
if ($category_data['Name'] == 'Content') {
continue;
}
$block_params['title'] = $category_data['Name'];
$block_params['template'] = preg_replace('/^Content\//i', '', $category_data['NamedParentPath']);
$block_params['is_first'] = $index == 1; // because Content is 1st element
$block_params['is_last'] = $index == count($categories_data) - 1;
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
/**
* Synonim to PrintList2 for "onlinestore" theme
*
* @param Array $params
* @return string
*/
function ListPages($params)
{
return $this->PrintList2($params);
}
/**
* Returns information about parser element locations in template
*
* @param Array $params
* @return mixed
*/
function BlockInfo($params)
{
if (!EDITING_MODE) {
return '';
}
$template_helper =& $this->Application->recallObject('TemplateHelper');
/* @var $template_helper TemplateHelper */
return $template_helper->blockInfo( $params['name'] );
}
/**
* Hide all editing tabs except permission tab, when editing "Home" (ID = 0) category
*
* @param Array $params
*/
function ModifyUnitConfig($params)
{
$root_category = $this->Application->RecallVar('IsRootCategory_' . $this->Application->GetVar('m_wid'));
if (!$root_category) {
return ;
}
$edit_tab_presets = $this->Application->getUnitOption($this->Prefix, 'EditTabPresets');
$edit_tab_presets['Default'] = Array (
'permissions' => $edit_tab_presets['Default']['permissions'],
);
$this->Application->setUnitOption($this->Prefix, 'EditTabPresets', $edit_tab_presets);
}
/**
* Prints catalog export templates
*
* @param Array $params
* @return string
*/
function PrintCatalogExportTemplates($params)
{
$prefixes = explode(',', $params['prefixes']);
$ret = Array ();
foreach ($prefixes as $prefix) {
if ($this->Application->prefixRegistred($prefix)) {
$module_path = $this->Application->getUnitOption($prefix, 'ModuleFolder') . '/';
$module_name = $this->Application->findModule('Path', $module_path, 'Name');
$ret[$prefix] = mb_strtolower($module_name) . '/export';
}
}
$json_helper =& $this->Application->recallObject('JSONHelper');
/* @var $json_helper JSONHelper */
return $json_helper->encode($ret);
}
/**
* Checks, that "view in browse mode" functionality available
*
* @param Array $params
* @return bool
*/
function BrowseModeAvailable($params)
{
$valid_special = $params['Special'] != 'user';
$not_selector = $this->Application->GetVar('type') != 'item_selector';
return $valid_special && $not_selector;
}
/**
* Returns a link for editing product
*
* @param Array $params
* @return string
*/
function ItemEditLink($params)
{
$object =& $this->getObject();
/* @var $object kDBList */
$edit_template = $this->Application->getUnitOption($this->Prefix, 'AdminTemplatePath') . '/' . $this->Application->getUnitOption($this->Prefix, 'AdminTemplatePrefix') . 'edit';
$url_params = Array (
'm_opener' => 'd',
$this->Prefix.'_mode' => 't',
$this->Prefix.'_event' => 'OnEdit',
$this->Prefix.'_id' => $object->GetID(),
'm_cat_id' => $object->GetDBField('ParentId'),
'pass' => 'all,'.$this->Prefix,
'no_pass_through' => 1,
);
return $this->Application->HREF($edit_template,'', $url_params);
}
function RelevanceIndicator($params)
{
$object =& $this->getObject($params);
$search_results_table = TABLE_PREFIX.'ses_'.$this->Application->GetSID().'_'.TABLE_PREFIX.'Search';
$sql = 'SELECT Relevance
FROM '.$search_results_table.'
WHERE ResourceId = '.$object->GetDBField('ResourceId');
$percents_off = (int)(100 - (100 * $this->Conn->GetOne($sql)));
$percents_off = ($percents_off < 0) ? 0 : $percents_off;
if ($percents_off) {
$params['percent_off'] = $percents_off;
$params['percent_on'] = 100 - $percents_off;
$params['name'] = $this->SelectParam($params, 'relevance_normal_render_as,block_relevance_normal');
}
else {
$params['name'] = $this->SelectParam($params, 'relevance_full_render_as,block_relevance_full');
}
return $this->Application->ParseBlock($params);
}
/**
* Returns list of categories, that have category add/edit permission
*
* @param Array $params
* @return string
*/
function AllowedCategoriesJSON($params)
{
if ($this->Application->RecallVar('user_id') == USER_ROOT) {
$categories = true;
}
else {
$object =& $this->getObject($params);
/* @var $object kDBItem */
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$perm_prefix = $this->Application->getUnitOption($this->Prefix, 'PermItemPrefix');
$categories = $perm_helper->getPermissionCategories($perm_prefix . '.' . ($object->IsNewItem() ? 'ADD' : 'MODIFY'));
}
$json_helper =& $this->Application->recallObject('JSONHelper');
/* @var $json_helper JSONHelper */
return $json_helper->encode($categories);
}
function PageEditable($params)
{
if ($this->Application->isDebugMode()) {
return true;
}
$object =& $this->getObject($params);
/* @var $object kDBItem */
return !$object->GetDBField('Protected');
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/categories/categories_item.php
===================================================================
--- branches/5.2.x/core/units/categories/categories_item.php (revision 14627)
+++ branches/5.2.x/core/units/categories/categories_item.php (revision 14628)
@@ -1,278 +1,303 @@
<?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 CategoriesItem extends kDBItem
{
- function Create($force_id = false, $system_create = false)
+
+ /**
+ * Creates a record in the database table with current item' values
+ *
+ * @param mixed $force_id Set to TRUE to force creating of item's own ID or to value to force creating of passed id. Do not pass 1 for true, pass exactly TRUE!
+ * @param bool $system_create
+ * @return bool
+ * @access public
+ */
+ public function Create($force_id = false, $system_create = false)
{
// set parent category first, so filename generation could use it
$parent_category = $this->GetDBField('ParentId') > 0 ? $this->GetDBField('ParentId') : $this->Application->GetVar('m_cat_id');
$this->SetDBField('ParentId', $parent_category);
$this->checkFilename();
$this->generateFilename();
if ($this->Validate()) {
// TODO: such approach will not respect changes from CategoryEventHandler::OnBeforeItemCreate event
$this->SetDBField('ResourceId', $this->Application->NextResourceId());
}
// TODO: move to CategoryEventHandler::OnBeforeItemCreate
$is_admin = $this->Application->isAdminUser;
if ((!$this->IsTempTable() && !$is_admin) || ($is_admin && !$this->GetDBField('CreatedById'))) {
$this->SetDBField('CreatedById', $this->Application->RecallVar('user_id'));
}
$ret = parent::Create($force_id, $system_create);
if ($ret) {
// TODO: move to CategoryEventHandler::OnAfterItemCreate method
$sql = 'UPDATE %s SET ParentPath = %s WHERE CategoryId = %s';
$parent_path = $this->buildParentPath();
$this->Conn->Query( sprintf($sql, $this->TableName, $this->Conn->qstr($parent_path), $this->GetID() ) );
$this->SetDBField('ParentPath', $parent_path);
}
return $ret;
}
- function Update($id=null, $system_update = false)
+ /**
+ * Updates previously loaded record with current item' values
+ *
+ * @access public
+ * @param int $id Primary Key Id to update
+ * @param bool $system_update
+ * @return bool
+ * @access public
+ */
+ public function Update($id = null, $system_update = false)
{
$this->checkFilename();
$this->generateFilename();
return parent::Update($id, $system_update);
}
function buildParentPath()
{
$parent_id = $this->GetDBField('ParentId');
if ($parent_id == 0) {
$parent_path = '|';
}
else {
$cat_table = $this->Application->getUnitOption($this->Prefix, 'TableName');
$sql = 'SELECT ParentPath FROM '.$cat_table.' WHERE CategoryId = %s';
$parent_path = $this->Conn->GetOne( sprintf($sql, $parent_id) );
}
return $parent_path.$this->GetID().'|';
}
/**
* replace not allowed symbols with "_" chars + remove duplicate "_" chars in result
*
* @param string $string
* @return string
*/
function stripDisallowed($string)
{
$filenames_helper =& $this->Application->recallObject('FilenamesHelper');
/* @var $filenames_helper kFilenamesHelper */
$string = $filenames_helper->replaceSequences($string);
return $this->checkAutoFilename($string);
}
function checkFilename()
{
if ($this->GetDBField('AutomaticFilename')) {
// filename will be generated from scratch, don't check anything here
return ;
}
elseif ($this->GetDBField('Type') == PAGE_TYPE_TEMPLATE) {
// system page with AutomaticFilename checkbox unchecked -> compatibility with Proj-CMS <= 4.3.9 (when "/" were allowed in Filename)
return ;
}
$filename = $this->GetDBField('Filename');
$this->SetDBField('Filename', $this->stripDisallowed($filename));
}
function checkAutoFilename($filename)
{
static $current_theme = null;
if (!$filename) {
return $filename;
}
if (!isset($current_theme)) {
$themes_helper =& $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$current_theme = (int)$themes_helper->getCurrentThemeId();
}
$escape_char = $this->Application->ConfigValue('FilenameSpecialCharReplacement');
$item_id = !$this->GetID() ? 0 : $this->GetID();
$item_theme = $this->GetDBField('ThemeId');
if (!$item_theme) {
// user creates category manually, that's why ThemeId = 0 -> use current admin theme instead
$item_theme = $current_theme;
}
$unique_clause = '(Filename = %s) AND (ThemeId = ' . $item_theme . ' OR ThemeId = 0) AND (ParentId = ' . $this->GetDBField('ParentId') . ')';
$sql_mask = ' SELECT ' . $this->IDField . '
FROM %s
WHERE ' . sprintf($unique_clause, $this->Conn->qstr($filename));
// check temp table
$sql_temp = sprintf($sql_mask, $this->TableName);
$found_temp_ids = $this->Conn->GetCol($sql_temp);
// check live table
$sql_live = sprintf($sql_mask, $this->Application->GetLiveName($this->TableName));
$found_live_ids = $this->Conn->GetCol($sql_live);
$found_item_ids = array_unique( array_merge($found_temp_ids, $found_live_ids) );
$has_page = preg_match('/(.*)_([\d]+)([a-z]*)$/', $filename, $rets);
$duplicates_found = (count($found_item_ids) > 1) || ($found_item_ids && $found_item_ids[0] != $item_id);
if ($duplicates_found || $has_page) {// other category has same filename as ours OR we have filename, that ends with _number
$append = $duplicates_found ? $escape_char . 'a' : '';
if ($has_page) {
$filename = $rets[1].'_'.$rets[2];
$append = $rets[3] ? $rets[3] : $escape_char . 'a';
}
// check live & temp table
$sql_temp = ' SELECT ' . $this->IDField . '
FROM ' . $this->TableName . '
WHERE ' . $unique_clause . ' AND (' . $this->IDField . ' != ' . $item_id . ')';
$sql_live = ' SELECT ' . $this->IDField . '
FROM ' . $this->Application->GetLiveName($this->TableName) . '
WHERE ' . $unique_clause . ' AND (' . $this->IDField . ' != ' . $item_id . ')';
while ( $this->Conn->GetOne( sprintf($sql_temp, $this->Conn->qstr($filename.$append)) ) > 0 ||
$this->Conn->GetOne( sprintf($sql_live, $this->Conn->qstr($filename.$append)) ) > 0 )
{
if (mb_substr($append, -1) == 'z') $append .= 'a';
$append = mb_substr($append, 0, mb_strlen($append) - 1) . chr( ord( mb_substr($append, -1) ) + 1 );
}
return $filename . $append;
}
return $filename;
}
/**
* Generate item's filename based on it's title field value
*
- * @return string
+ * @return void
+ * @access protected
*/
- function generateFilename()
+ protected function generateFilename()
{
- if ( !$this->GetDBField('AutomaticFilename') && $this->GetDBField('Filename') ) return false;
+ if ( !$this->GetDBField('AutomaticFilename') && $this->GetDBField('Filename') ) {
+ return ;
+ }
$ml_formatter =& $this->Application->recallObject('kMultiLanguage');
- $name = $this->stripDisallowed( $this->GetDBField( $ml_formatter->LangFieldName('Name', true) ) );
+ /* @var $ml_formatter kMultiLanguage */
+
+ $name = $this->stripDisallowed( $this->GetDBField($ml_formatter->LangFieldName('Name', true)) );
- if ( $name != $this->GetDBField('Filename') ) $this->SetDBField('Filename', $name);
+ if ( $name != $this->GetDBField('Filename') ) {
+ $this->SetDBField('Filename', $name);
+ }
}
/**
* Allows to detect if root category being edited
*
* @param Array $params
*/
function IsRoot()
{
return $this->Application->RecallVar('IsRootCategory_'.$this->Application->GetVar('m_wid'));
}
/**
* Sets correct name to Home category while editing it
*
* @return bool
*/
function IsNewItem()
{
if ($this->IsRoot() && $this->Prefix == 'c') {
$title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField');
$category_name = $this->Application->Phrase(($this->Application->isAdmin ? 'la_' : 'lu_') . 'rootcategory_name');
$this->SetDBField($title_field, $category_name);
return false;
}
return parent::IsNewItem();
}
/**
* Sets new name for item in case if it is beeing copied
* in same table
*
* @param array $master Table data from TempHandler
* @param int $foreign_key ForeignKey value to filter name check query by
* @access private
*/
public function NameCopy($master=null, $foreign_key=null, $title_field=null, $format='Copy %1$s of %2$s')
{
if (!isset($title_field)) {
$title_field = $this->Application->getUnitOption($this->Prefix, 'TitleField');
if (!$title_field || isset($this->CalculatedFields[$title_field]) ) return;
}
$new_name = $this->GetDBField($title_field);
$cat_id = $this->Application->GetVar('m_cat_id');
$this->SetDBField('ParentId', $cat_id);
$original_checked = false;
do {
if ( preg_match('/Copy ([0-9]*) *of (.*)/', $new_name, $regs) ) {
$new_name = 'Copy '.($regs[1]+1).' of '.$regs[2];
}
elseif ($original_checked) {
$new_name = 'Copy of '.$new_name;
}
// if we are cloning in temp table this will look for names in temp table,
// since object' TableName contains correct TableName (for temp also!)
// if we are cloning live - look in live
$query = ' SELECT ' . $title_field . '
FROM ' . $this->TableName . '
WHERE ParentId = ' . (int)$cat_id . ' AND ' . $title_field . ' = ' . $this->Conn->qstr($new_name);
$foreign_key_field = getArrayValue($master, 'ForeignKey');
$foreign_key_field = is_array($foreign_key_field) ? $foreign_key_field[ $master['ParentPrefix'] ] : $foreign_key_field;
if ($foreign_key_field && isset($foreign_key)) {
$query .= ' AND '.$foreign_key_field.' = '.$foreign_key;
}
$res = $this->Conn->GetOne($query);
/*// if not found in live table, check in temp table if applicable
if ($res === false && $object->Special == 'temp') {
$query = 'SELECT '.$name_field.' FROM '.$this->GetTempName($master['TableName']).'
WHERE '.$name_field.' = '.$this->Conn->qstr($new_name);
$res = $this->Conn->GetOne($query);
}*/
$original_checked = true;
} while ($res !== false);
$this->SetDBField($title_field, $new_name);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/themes/themes_eh.php
===================================================================
--- branches/5.2.x/core/units/themes/themes_eh.php (revision 14627)
+++ branches/5.2.x/core/units/themes/themes_eh.php (revision 14628)
@@ -1,195 +1,202 @@
<?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 ThemesEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnChangeTheme' => Array('self' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
- * Permission check override
+ * Checks user permission to execute given $event
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
if ($event->Name == 'OnItemBuild') {
// check permission without using $event->getSection(),
// so first cache rebuild won't lead to "ldefault_Name" field being used
return true;
}
return parent::CheckPermission($event);
}
/**
* Allows to set selected theme as primary
*
* @param kEvent $event
*/
function OnSetPrimary(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return;
}
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
$id = array_shift($ids);
$this->setPrimary($id);
$rebuild_event = new kEvent('adm:OnRebuildThemes');
$this->Application->HandleEvent($rebuild_event);
}
$this->clearSelectedIDs($event);
}
function setPrimary($id)
{
$id_field = $this->Application->getUnitOption($this->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($this->Prefix, 'TableName');
$sql = 'UPDATE '.$table_name.'
SET PrimaryTheme = 0';
$this->Conn->Query($sql);
$sql = 'UPDATE '.$table_name.'
SET PrimaryTheme = 1, Enabled = 1
WHERE '.$id_field.' = '.$id;
$this->Conn->Query($sql);
}
/**
* Set's primary theme (when checkbox used on editing form)
*
* @param kEvent $event
*/
function OnAfterCopyToLive(&$event)
{
$object =& $this->Application->recallObject($event->Prefix.'.-item', null, Array('skip_autoload' => true, 'live_table' => true));
/* @var $object kDBItem */
$object->Load($event->getEventParam('id'));
if ($object->GetDBField('PrimaryTheme')) {
$this->setPrimary($event->getEventParam('id'));
}
}
/**
* Also rebuilds theme files, when enabled theme is saved
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnSave(&$event)
+ protected function OnSave(&$event)
{
parent::OnSave($event);
- if (($event->status != kEvent::erSUCCESS) || !$event->getEventParam('ids')) {
+ if ( ($event->status != kEvent::erSUCCESS) || !$event->getEventParam('ids') ) {
return ;
}
$ids = $event->getEventParam('ids');
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT COUNT(*)
FROM ' . $table_name . '
WHERE ' . $id_field . ' IN (' . $ids . ') AND (Enabled = 1)';
$enabled_themes = $this->Conn->GetOne($sql);
- if ($enabled_themes) {
+ if ( $enabled_themes ) {
$rebuild_event = new kEvent('adm:OnRebuildThemes');
$this->Application->HandleEvent($rebuild_event);
}
}
/**
* Allows to change the theme
*
* @param kEvent $event
*/
function OnChangeTheme(&$event)
{
if ($this->Application->isAdminUser) {
// for structure theme dropdown
$this->Application->StoreVar('theme_id', $this->Application->GetVar('theme'));
$this->Application->StoreVar('RefreshStructureTree', 1);
return ;
}
$this->Application->SetVar('t', 'index');
$this->Application->SetVar('m_cat_id', 0);
if (MOD_REWRITE) {
$mod_rewrite_helper =& $this->Application->recallObject('ModRewriteHelper');
/* @var $mod_rewrite_helper kModRewriteHelper */
$mod_rewrite_helper->removePages();
}
$this->Application->SetVar('m_theme', $this->Application->GetVar('theme'));
}
/**
* Apply system filter to themes list
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
parent::SetCustomQuery($event);
$object =& $event->getObject();
/* @var $object kDBList */
if (in_array($event->Special, Array ('enabled', 'selected', 'available')) || !$this->Application->isAdminUser) {
// "enabled" special or Front-End
$object->addFilter('enabled_filter', '%1$s.Enabled = ' . STATUS_ACTIVE);
}
// site domain theme picker
if ($event->Special == 'selected' || $event->Special == 'available') {
$edit_picker_helper =& $this->Application->recallObject('EditPickerHelper');
/* @var $edit_picker_helper EditPickerHelper */
$edit_picker_helper->applyFilter($event, 'Themes');
}
// apply domain-based theme filtering
$themes = $this->Application->siteDomainField('Themes');
if (strlen($themes)) {
$themes = explode('|', substr($themes, 1, -1));
$object->addFilter('domain_filter', '%1$s.ThemeId IN (' . implode(',', $themes) . ')');
}
}
}
Index: branches/5.2.x/core/units/country_states/country_state_eh.php
===================================================================
--- branches/5.2.x/core/units/country_states/country_state_eh.php (revision 14627)
+++ branches/5.2.x/core/units/country_states/country_state_eh.php (revision 14628)
@@ -1,110 +1,117 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2010 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 CountryStateEventHandler extends kDBEventHandler {
/**
* Applies edit picker filters
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
parent::SetCustomQuery($event);
$object =& $event->getObject();
/* @var $object kDBList */
if (($event->Special == 'selected') || ($event->Special == 'available')) {
$edit_picker_helper =& $this->Application->recallObject('EditPickerHelper');
/* @var $edit_picker_helper EditPickerHelper */
$edit_picker_helper->applyFilter($event, 'Countries');
// only countries
$object->addFilter('type_filter', '%1$s.Type = ' . DESTINATION_TYPE_COUNTRY);
}
}
/**
* Makes sure, that state country is always specified
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
parent::OnBeforeItemCreate($event);
$this->_setRequired($event);
}
/**
* Makes sure, that state country is always specified
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
parent::OnBeforeItemUpdate($event);
$this->_setRequired($event);
}
/**
* Makes sure, that state country is always specified
*
* @param kEvent $event
*/
function _setRequired(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$field_options = $object->GetFieldOptions('IsoCode');
if ($object->GetDBField('Type') == DESTINATION_TYPE_STATE) {
$object->setRequired('StateCountryId');
$field_options['unique'] = Array ('Type', 'StateCountryId');
}
else {
$object->setRequired('StateCountryId', false);
$field_options['unique'] = Array ('Type');
}
$object->SetFieldOptions('IsoCode', $field_options);
}
/**
* Don't allow to delete countries, that have states
*
* @param kEvent $event
*/
function OnBeforeItemDelete(&$event)
{
parent::OnBeforeItemDelete($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
/* @var $cs_helper kCountryStatesHelper */
if ($cs_helper->CountryHasStates( $object->GetDBField('IsoCode') )) {
$event->status = kEvent::erFAIL;
return ;
}
}
}
Index: branches/5.2.x/core/units/skins/skin_eh.php
===================================================================
--- branches/5.2.x/core/units/skins/skin_eh.php (revision 14627)
+++ branches/5.2.x/core/units/skins/skin_eh.php (revision 14628)
@@ -1,144 +1,149 @@
<?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 SkinEventHandler extends kDBEventHandler {
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnItemBuild' => Array ('self' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* With "primary" special loads primary skin
*
* @param kEvent $event
* @return int
*/
function getPassedID(&$event)
{
if ($event->Special == 'primary') {
return Array ('IsPrimary' => 1);
}
return parent::getPassedID($event);
}
/**
* Allows to set selected theme as primary
*
* @param kEvent $event
*/
function OnSetPrimary(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return;
}
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
$id = array_shift($ids);
$this->setPrimary($id);
}
$this->clearSelectedIDs($event);
}
function setPrimary($id)
{
$id_field = $this->Application->getUnitOption($this->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($this->Prefix, 'TableName');
$sql = 'UPDATE '.$table_name.'
SET IsPrimary = 0';
$this->Conn->Query($sql);
$sql = 'UPDATE '.$table_name.'
SET IsPrimary = 1
WHERE '.$id_field.' = '.$id;
$this->Conn->Query($sql);
}
/**
* Don't make cloned skin primary
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeClone(&$event)
+ protected function OnBeforeClone(&$event)
{
parent::OnBeforeClone($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$object->SetDBField('IsPrimary', 0);
}
/**
* Re-compile skin, after it's changed (live table only)
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
parent::OnAfterItemUpdate($event);
$object =& $event->getObject();
/* @var $object kDBItem */
if (!$object->IsTempTable()) {
$skin_helper =& $this->Application->recallObject('SkinHelper');
/* @var $skin_helper SkinHelper */
$skin_helper->compile($object);
}
}
/**
* [HOOK] Compile stylesheet file based on theme definitions
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnCompileStylesheet(&$event)
+ protected function OnCompileStylesheet(&$event)
{
- $object =& $event->getObject( Array('skip_autoload' => true) );
+ $object =& $event->getObject( Array ('skip_autoload' => true) );
+ /* @var $object kDBItem */
+
$object->SwitchToLive();
$ids = $event->MasterEvent->getEventParam('ids');
- if (!is_array($ids)) {
- $ids = explode(',', $ids);
+ if ( !is_array($ids) ) {
+ $ids = explode(',', $ids);
}
- if (!$ids) {
- return false;
+ if ( !$ids ) {
+ return ;
}
$skin_helper =& $this->Application->recallObject('SkinHelper');
/* @var $skin_helper SkinHelper */
- foreach($ids as $id) {
+ foreach ($ids as $id) {
$object->Load($id);
$skin_helper->compile($object);
}
}
-
}
\ No newline at end of file
Index: branches/5.2.x/core/units/translator/translator_event_handler.php
===================================================================
--- branches/5.2.x/core/units/translator/translator_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/translator/translator_event_handler.php (revision 14628)
@@ -1,135 +1,174 @@
<?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 TranslatorEventHandler extends kDBEventHandler
{
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnChangeLanguage' => Array('subitem' => 'add|edit'),
'OnSaveAndClose' => Array('subitem' => 'add|edit'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Check permission of item, that being translated
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
list($prefix, $field) = $this->getPrefixAndField($event);
$top_prefix = $this->Application->GetTopmostPrefix($prefix, true);
$event->setEventParam('top_prefix', $top_prefix);
return parent::CheckPermission($event);
}
/**
* Returns prefix and field being translated
*
* @param kEvent $event
*/
function getPrefixAndField(&$event)
{
$field = $this->Application->GetVar($event->getPrefixSpecial(true).'_field');
if (strpos($field,':') !== false) {
list($prefix, $field) = explode(':', $field);
}
else {
$prefix = $this->Application->GetVar($event->getPrefixSpecial(true).'_prefix');
}
return Array($prefix, $field);
}
- function OnLoad(&$event)
+ /**
+ * Loads record to be translated
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnLoad(&$event)
{
list($obj_prefix, $field) = $this->getPrefixAndField($event);
$object =& $this->Application->recallObject($obj_prefix);
- $translator =& $this->Application->recallObject($event->getPrefixSpecial());
+ /* @var $object kDBItem */
+
+ $translator =& $event->getObject();
+ /* @var $translator kDBItem */
$def_lang = $this->Application->GetDefaultLanguageId();
$current_lang = $translator->GetDBField('Language');
if (!$current_lang) $current_lang = $this->Application->RecallVar('trans_lang');
if (!$current_lang) $current_lang = $this->Application->GetVar('m_lang');
/*if ($current_lang == $def_lang) {
$current_lang = $def_lang + 1;
}*/
$this->Application->StoreVar('trans_lang', $current_lang); //remember translation language for user friendlyness
$translator->SetID(1);
$translator->SetDBField('Original', $object->GetDBField('l'.$this->Application->GetVar('m_lang').'_'.$field));
$translator->SetDBField('Language', $current_lang);
$translator->SetDBField('SwitchLanguage', $current_lang);
$translator->SetDBField('Translation', $object->GetDBField('l'.$current_lang.'_'.$field));
$cur_lang =& $this->Application->recallObject('lang.current');
+ /* @var $cur_lang LanguagesItem */
+
$cur_lang->Load($current_lang);
$translator->SetDBField('Charset', $cur_lang->GetDBField('Charset'));
$event->redirect = false;
}
- function OnSaveAndClose(&$event)
+ /**
+ * Saves changes into temporary table and closes editing window
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnSaveAndClose(&$event)
{
$event->CallSubEvent('OnPreSave');
- $this->finalizePopup($event);
+
+ $event->SetRedirectParam('opener', 'u');
}
- function OnPreSave(&$event)
+ /**
+ * 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(&$event)
{
- $translator =& $this->Application->recallObject($event->getPrefixSpecial());
+ $translator =& $event->getObject();
+ /* @var $translator kDBItem */
- $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
- if($items_info) $field_values = array_shift($items_info);
-
- $translator->SetFieldsFromHash($field_values);
+ $translator->SetFieldsFromHash( $this->getSubmittedFields($event) );
list($obj_prefix, $field) = $this->getPrefixAndField($event);
$object =& $this->Application->recallObject($obj_prefix);
/* @var $object kDBItem */
$lang = $translator->GetDBField('Language');
- $object->SetFieldOptions('l'.$lang.'_'.$field, Array ());
- $object->SetDBField('l'.$lang.'_'.$field, $translator->GetDBField('Translation'));
+ $object->SetFieldOptions('l' . $lang . '_' . $field, Array ());
+ $object->SetDBField('l' . $lang . '_' . $field, $translator->GetDBField('Translation'));
$this->RemoveRequiredFields($object);
$object->Update();
}
- function OnChangeLanguage(&$event)
+ /**
+ * Changes current language in translation popup
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnChangeLanguage(&$event)
{
$event->CallSubEvent('OnPreSave');
- $translator =& $this->Application->recallObject($event->getPrefixSpecial());
- $translator->SetDBField('Language', $translator->GetDBField('SwitchLanguage'));
+
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
+
+ $object->SetDBField('Language', $object->GetDBField('SwitchLanguage'));
+
$event->CallSubEvent('OnLoad');
$event->redirect = false;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/statistics/statistics_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/statistics/statistics_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/statistics/statistics_tag_processor.php (revision 14628)
@@ -1,328 +1,333 @@
<?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 = adodb_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;
}
}
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) {
$statistics_info = $this->Application->getUnitOption($prefix.'.pending', 'StatisticsInfo');
if (!$statistics_info) {
return 0;
}
$table = $this->Application->getUnitOption($prefix, 'TableName');
$status_field = array_shift( $this->Application->getUnitOption($prefix, 'StatusField') );
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT COUNT(*)
FROM '.$table.'
WHERE '.$status_field.' = '.$statistics_info['status'];
$value = $this->Conn->GetOne($sql);
$this->Application->setCache($cache_key, $value);
}
return $value;
}
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');
/* @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->Application->getUnitOption($prefix.'.pending', 'StatisticsInfo');
if ($statistics_info) {
$prefixes[] = $prefix;
}
}
$row_number = 0;
foreach ($prefixes as $i => $prefix) {
$block_params['prefix'] = $prefix;
$statistics_info = $this->Application->getUnitOption($prefix.'.pending', 'StatisticsInfo');
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.2.x/core/units/statistics/statistics_event_handler.php
===================================================================
--- branches/5.2.x/core/units/statistics/statistics_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/statistics/statistics_event_handler.php (revision 14628)
@@ -1,40 +1,44 @@
<?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 StatisticsEventHandler extends kDBEventHandler {
/**
- * Allows to show statistics only of requrested type
+ * Allows to show statistics only of requested type
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
+ /* @var $object kDBList */
if ($event->Special == 'summary') {
$object->addFilter('summary_filter', '%1$s.AdminSummary = 1');
}
$module_list = '';
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
$module_list .= $this->Conn->qstr($module_name).',';
}
$module_list = substr($module_list, 0, -1);
$object->addFilter('status_filter', '%1$s.Module IN ('.$module_list.')');
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/related_searches/related_searches_event_handler.php
===================================================================
--- branches/5.2.x/core/units/related_searches/related_searches_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/related_searches/related_searches_event_handler.php (revision 14628)
@@ -1,36 +1,37 @@
<?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 RelatedSearchEventHandler extends kDBEventHandler
{
/**
* Initializes new relation
*
* @param kEvent $event
*/
function OnNew(&$event)
{
parent::OnNew($event);
$object =& $event->getObject();
+ /* @var $object kDBItem */
$table_info = $object->getLinkedInfo();
$source_itemtype = $this->Application->getUnitOption($table_info['ParentPrefix'], 'ItemType');
$object->SetDBField('ItemType', $source_itemtype);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/custom_fields/custom_fields_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/custom_fields/custom_fields_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/custom_fields/custom_fields_tag_processor.php (revision 14628)
@@ -1,160 +1,164 @@
<?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 CustomFieldsTagProcessor extends kDBTagProcessor {
/**
* Return LEFT JOINed custom field name from main item config
*
* @param Array $params
* @return string
*/
function GetMainField($params)
{
$object =& $this->getObject($params);
$append = isset($params['append']) && $params['append'] ? $params['append'] : '';
return 'cust_'.$object->GetDBField('FieldName').$append;
}
function CustomField($params)
{
$params['name'] = $this->GetMainField($params);
$source_prefix = $this->Application->Parser->GetParam('SourcePrefix');
return $this->Application->ProcessParsedTag($source_prefix, 'Field', $params);
}
function CustomFormat($params)
{
$params['name'] = $this->GetMainField($params);
$source_prefix = $this->Application->Parser->GetParam('SourcePrefix');
return $this->Application->ProcessParsedTag($source_prefix, 'Format', $params);
}
function CustomInputName($params)
{
$params['name'] = $this->GetMainField($params);
$source_prefix = $this->Application->Parser->GetParam('SourcePrefix');
return $this->Application->ProcessParsedTag($source_prefix, 'InputName', $params);
}
function setParamValue(&$params, $param_name)
{
// $deep_level if GetParam = 1 used in case if PrintList is called during parsing "grid" block (=> +1 to deep_level)
if (!isset($params[$param_name])) {
$params[$param_name] = $this->Application->Parser->GetParam($param_name, 1);
}
return $params[$param_name];
}
/**
* Prints list content using block specified
*
* @param Array $params
* @return string
* @access public
*/
function PrintList($params)
{
$this->setParamValue($params, 'SourcePrefix');
$this->setParamValue($params, 'value_field');
$list =& $this->GetList($params);
$id_field = $this->Application->getUnitOption($this->Prefix,'IDField');
$list->Query();
$o = '';
$list->GoFirst();
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $this->SelectParam($params, 'render_as,block');
$block_params['pass_params'] = 'true';
$prev_heading = '';
$display_original = false;
$source_prefix = getArrayValue($params, 'SourcePrefix');
- if ($source_prefix) {
+ $source_object = $original_object = null;
+
+ if ( $source_prefix ) {
$source_object =& $this->Application->recallObject($source_prefix, null, Array ('raise_warnings' => 0)); // it's possible, that in some cases object will not be loaded
/* @var $source_object kCatDBItem */
- $display_original = $this->Application->ProcessParsedTag($source_prefix, 'DisplayOriginal', Array('display_original' => $this->setParamValue($params, 'display_original')));
+ $display_original = $this->Application->ProcessParsedTag($source_prefix, 'DisplayOriginal', Array ('display_original' => $this->setParamValue($params, 'display_original')));
}
- if ($display_original) {
+ if ( $display_original ) {
$block_params['display_original'] = $display_original;
$block_params['original_title'] = $this->setParamValue($params, 'original_title');
- $original_object =& $this->Application->recallObject($source_prefix.'.original', null, Array ('raise_warnings' => 0)); // it's possible, that in some cases object will not be loaded
+
+ $original_object =& $this->Application->recallObject($source_prefix . '.original', null, Array ('raise_warnings' => 0)); // it's possible, that in some cases object will not be loaded
+ /* @var $original_object kCatDBItem */
}
if ($this->Special == 'general') {
$list->groupRecords('Heading');
}
$i = 0;
- while (!$list->EOL())
- {
+ while ( !$list->EOL() ) {
$block_params['is_last'] = ($i == $list->GetSelectedCount() - 1);
$block_params['not_last'] = !$block_params['is_last']; // for front-end
- $this->Application->SetVar( $this->getPrefixSpecial().'_id', $list->GetDBField($id_field) ); // for edit/delete links using GET
+ $this->Application->SetVar($this->getPrefixSpecial() . '_id', $list->GetDBField($id_field)); // for edit/delete links using GET
- if ($source_prefix) {
+ if ( $source_prefix ) {
$field_name = 'cust_' . $list->GetDBField('FieldName');
-
+
$formatter = $source_object->GetFieldOption($field_name, 'formatter');
$language_prefix = $formatter == 'kMultiLanguage' ? 'l' . $this->Application->GetVar('m_lang') . '_' : '';
-
- $list->SetDBField($params['value_field'], $source_object->GetDBField($language_prefix . 'cust_'.$list->GetDBField('FieldName')));
- if ($display_original) {
- $list->SetDBField('OriginalValue', $original_object->GetField('cust_'.$list->GetDBField('FieldName')));
+ $list->SetDBField($params['value_field'], $source_object->GetDBField($language_prefix . 'cust_' . $list->GetDBField('FieldName')));
+
+ if ( $display_original ) {
+ $list->SetDBField('OriginalValue', $original_object->GetField('cust_' . $list->GetDBField('FieldName')));
}
- $block_params['field'] = $block_params['virtual_field'] = 'cust_'.$list->GetDBField('FieldName');
- $block_params['show_heading'] = ($prev_heading != $list->GetDBField('Heading') ) ? 1 : 0;
+ $block_params['field'] = $block_params['virtual_field'] = 'cust_' . $list->GetDBField('FieldName');
+ $block_params['show_heading'] = ($prev_heading != $list->GetDBField('Heading')) ? 1 : 0;
$list->SetDBField('DirectOptions', $source_object->GetFieldOption($field_name, 'options'));
}
- $o.= $this->Application->ParseBlock($block_params);
+ $o .= $this->Application->ParseBlock($block_params);
$prev_heading = $list->GetDBField('Heading');
$list->GoNext();
$i++;
}
- $this->Application->SetVar( $this->getPrefixSpecial().'_id', '');
+ $this->Application->SetVar($this->getPrefixSpecial() . '_id', '');
+
return $o;
}
/**
* If data was modfied & is in TempTables mode, then parse block with name passed;
* remove modification mark if not in TempTables mode
*
* @param Array $params
* @return string
* @access public
* @author Alexey
*/
function SaveWarning($params)
{
$source_prefix = array_key_exists('SourcePrefix', $params) ? $params['SourcePrefix'] : false;
if ($source_prefix && $source_prefix == 'c') {
return $this->Application->ProcessParsedTag('c', 'SaveWarning', $params);
}
return parent::SaveWarning($params);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/custom_fields/custom_fields_event_handler.php
===================================================================
--- branches/5.2.x/core/units/custom_fields/custom_fields_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/custom_fields/custom_fields_event_handler.php (revision 14628)
@@ -1,311 +1,347 @@
<?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 CustomFieldsEventHandler extends kDBEventHandler {
/**
* Changes permission section to one from REQUEST, not from config
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
$sql = 'SELECT Prefix
FROM '.TABLE_PREFIX.'ItemTypes
WHERE ItemType = '.$this->Conn->qstr( $this->Application->GetVar('cf_type') );
$main_prefix = $this->Conn->GetOne($sql);
$section = $this->Application->getUnitOption($main_prefix.'.custom', 'PermSection');
$event->setEventParam('PermSection', $section);
return parent::CheckPermission($event);
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
+ * @return void
* @access protected
- * @see OnListBuild
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
/* @var $object kDBList */
$item_type = $this->Application->GetVar('cf_type');
if (!$item_type) {
$prefix = $event->getEventParam('SourcePrefix');
$item_type = $this->Application->getUnitOption($prefix, 'ItemType');
}
if ($event->Special == 'general') {
$object->addFilter('generaltab_filter', '%1$s.OnGeneralTab = 1');
}
if ($item_type) {
- $hidden_fields = array_map(Array(&$this->Conn, 'qstr'), $this->_getHiddenFiels($event));
+ $hidden_fields = array_map(Array(&$this->Conn, 'qstr'), $this->_getHiddenFields($event));
if ($hidden_fields) {
$object->addFilter('hidden_filter', '%1$s.FieldName NOT IN (' . implode(',', $hidden_fields) . ')');
}
$object->addFilter('itemtype_filter', '%1$s.Type = '.$item_type);
}
if (!($this->Application->isDebugMode() && $this->Application->isAdminUser)) {
$object->addFilter('user_filter', '%1$s.IsSystem = 0');
}
}
/**
* Returns prefix, that custom fields are printed for
*
* @param kEvent $event
* @return string
*/
function _getSourcePrefix(&$event)
{
$prefix = $event->getEventParam('SourcePrefix');
if (!$prefix) {
$sql = 'SELECT Prefix
FROM ' . TABLE_PREFIX . 'ItemTypes
WHERE ItemType = ' . $this->Conn->qstr( $this->Application->GetVar('cf_type') );
$prefix = $this->Conn->GetOne($sql);
}
return $prefix;
}
/**
* Get custom fields, that should no be shown anywhere
*
* @param kEvent $event
* @return Array
+ * @access protected
*/
- function _getHiddenFiels(&$event)
+ protected function _getHiddenFields(&$event)
{
$prefix = $this->_getSourcePrefix($event);
+ $hidden_fields = Array ();
$virtual_fields = $this->Application->getUnitOption($prefix, 'VirtualFields', Array ());
$custom_fields = $this->Application->getUnitOption($prefix, 'CustomFields', Array ());
+ /* @var $custom_fields Array */
- $hidden_fields = Array ();
foreach ($custom_fields as $custom_field) {
$check_field = 'cust_' . $custom_field;
$show_mode = array_key_exists('show_mode', $virtual_fields[$check_field]) ? $virtual_fields[$check_field]['show_mode'] : true;
- if (($show_mode === false) || (($show_mode === smDEBUG) && !(defined('DEBUG_MODE') && DEBUG_MODE))) {
+ if ( ($show_mode === false) || (($show_mode === smDEBUG) && !(defined('DEBUG_MODE') && DEBUG_MODE)) ) {
$hidden_fields[] = $custom_field;
}
}
return $hidden_fields;
}
/**
* Prevents from duplicate item creation
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
+ parent::OnBeforeItemCreate($event);
+
$object =& $event->getObject();
+ /* @var $object kDBItem */
- $live_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT COUNT(*)
- FROM '.$live_table.'
- WHERE FieldName = '.$this->Conn->qstr($object->GetDBField('FieldName')).' AND Type = '.$object->GetDBField('Type');
+ FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
+ WHERE FieldName = ' . $this->Conn->qstr($object->GetDBField('FieldName')) . ' AND Type = ' . $object->GetDBField('Type');
$found = $this->Conn->GetOne($sql);
- if ($found) {
+ if ( $found ) {
$event->status = kEvent::erFAIL;
$object->SetError('FieldName', 'duplicate', 'la_error_CustomExists');
}
}
/**
- * Occurse after deleting item, id of deleted item
+ * Occurs after deleting item, id of deleted item
* is stored as 'id' param of event
*
* @param kEvent $event
* @access public
*/
function OnAfterItemDelete(&$event)
{
$object =& $event->getObject();
- $main_prefix = $this->getPrefixByItemType($object->GetDBField('Type'));
+ /* @var $object kDBItem */
+
+ $main_prefix = $this->getPrefixByItemType( $object->GetDBField('Type') );
$ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
// call main item config to clone cdata table
$this->Application->getUnitOption($main_prefix, 'TableName');
- $ml_helper->deleteField($main_prefix.'-cdata', $event->getEventParam('id'));
+ $ml_helper->deleteField($main_prefix . '-cdata', $event->getEventParam('id'));
}
/**
* Get config prefix based on item type
*
- * @param unknown_type $item_type
- * @return unknown
+ * @param int $item_type
+ * @return string
+ * @access protected
*/
- function getPrefixByItemType($item_type)
+ protected function getPrefixByItemType($item_type)
{
$sql = 'SELECT Prefix
- FROM '.TABLE_PREFIX.'ItemTypes
- WHERE ItemType = '.$item_type;
+ FROM ' . TABLE_PREFIX . 'ItemTypes
+ WHERE ItemType = ' . $item_type;
+
return $this->Conn->GetOne($sql);
}
/**
- * Enter description here...
+ * Creates new database columns, once custom field is successfully created
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnSaveCustomField(&$event)
+ protected function OnSaveCustomField(&$event)
{
- if ($event->MasterEvent->status != kEvent::erSUCCESS) {
- return false;
+ if ( $event->MasterEvent->status != kEvent::erSUCCESS ) {
+ return ;
}
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$main_prefix = $this->getPrefixByItemType($object->GetDBField('Type'));
$ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
// call main item config to clone cdata table
define('CUSTOM_FIELD_ADDED', 1); // used in cdata::scanCustomFields method
$this->Application->getUnitOption($main_prefix, 'TableName');
- $ml_helper->createFields($main_prefix.'-cdata');
+ $ml_helper->createFields($main_prefix . '-cdata');
}
- function OnMassDelete(&$event)
+ /**
+ * 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(&$event)
{
parent::OnMassDelete($event);
- $event->setRedirectParams(Array('opener' => 's'), true);
+
+ $event->SetRedirectParam('opener', 's');
}
/**
* Prepare temp tables for creating new item
* but does not create it. Actual create is
* done in OnPreSaveCreated
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnPreCreate(&$event)
+ protected function OnPreCreate(&$event)
{
parent::OnPreCreate($event);
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$object->SetDBField('Type', $this->Application->GetVar('cf_type'));
}
/**
* Prepares ValueList field's value as xml for editing
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemLoad(&$event)
+ protected function OnAfterItemLoad(&$event)
{
parent::OnAfterItemLoad($event);
$object =& $event->getObject();
/* @var $object kDBItem */
- if (!in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes())) {
+ if ( !in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes()) ) {
return ;
}
$custom_field_helper =& $this->Application->recallObject('InpCustomFieldsHelper');
/* @var $custom_field_helper InpCustomFieldsHelper */
- $options = $custom_field_helper->GetValuesHash( $object->GetDBField('ValueList'), VALUE_LIST_SEPARATOR, false );
+ $options = $custom_field_helper->GetValuesHash($object->GetDBField('ValueList'), VALUE_LIST_SEPARATOR, false);
$records = Array ();
$option_key = key($options);
- if ($option_key === '' || $option_key == 0) {
+ if ( $option_key === '' || $option_key == 0 ) {
// remove 1st empty option, and add it later, when options will be saved, but allow string option keys
unset($options[$option_key]); // keep index, don't use array_unshift!
}
foreach ($options as $option_key => $option_title) {
$records[] = Array ('OptionKey' => $option_key, 'OptionTitle' => $option_title);
}
$minput_helper =& $this->Application->recallObject('MInputHelper');
/* @var $minput_helper MInputHelper */
$xml = $minput_helper->prepareMInputXML($records, Array ('OptionKey', 'OptionTitle'));
$object->SetDBField('Options', $xml);
}
/**
* Returns custom field element types, that will use minput control
*
- * @return unknown
+ * @return Array
+ * @access protected
*/
- function _getMultiElementTypes()
+ protected function _getMultiElementTypes()
{
return Array ('select', 'multiselect', 'radio');
}
/**
* Saves minput content to ValueList field
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
parent::OnBeforeItemUpdate($event);
$object =& $event->getObject();
/* @var $object kDBItem */
- if (!in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes())) {
+ if ( !in_array($object->GetDBField('ElementType'), $this->_getMultiElementTypes()) ) {
return ;
}
$minput_helper =& $this->Application->recallObject('MInputHelper');
/* @var $minput_helper MInputHelper */
$ret = $object->GetDBField('ElementType') == 'select' ? Array ('' => '=+') : Array ();
$records = $minput_helper->parseMInputXML($object->GetDBField('Options'));
- if ($object->GetDBField('SortValues')) {
+ if ( $object->GetDBField('SortValues') ) {
usort($records, Array (&$this, '_sortValues'));
ksort($records);
}
foreach ($records as $record) {
- if (substr($record['OptionKey'], 0, 3) == 'SQL') {
+ if ( substr($record['OptionKey'], 0, 3) == 'SQL' ) {
$ret[] = $record['OptionTitle'];
}
else {
$ret[] = $record['OptionKey'] . '=' . $record['OptionTitle'];
}
}
$object->SetDBField('ValueList', implode(VALUE_LIST_SEPARATOR, $ret));
}
function _sortValues($record_a, $record_b)
{
return strcasecmp($record_a['OptionTitle'], $record_b['OptionTitle']);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/favorites/favorites_eh.php
===================================================================
--- branches/5.2.x/core/units/favorites/favorites_eh.php (revision 14627)
+++ branches/5.2.x/core/units/favorites/favorites_eh.php (revision 14628)
@@ -1,102 +1,104 @@
<?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 FavoritesEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnFavoriteToggle' => Array('subitem' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Adds/removes item from favorites
*
* @param kEvent $event
*/
function OnFavoriteToggle(&$event)
{
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$parent_object =& $this->Application->recallObject($parent_prefix);
/* @var $parent_object kDBItem */
if (!$parent_object->isLoaded() || !$this->Application->CheckPermission('FAVORITES', 0, $parent_object->GetDBField('ParentPath'))) {
$event->status = kEvent::erPERM_FAIL;
return ;
}
$user_id = $this->Application->RecallVar('user_id');
$sql = 'SELECT FavoriteId
FROM '.$this->Application->getUnitOption($event->Prefix, 'TableName').'
WHERE (PortalUserId = '.$user_id.') AND (ResourceId = '.$parent_object->GetDBField('ResourceId').')';
$favorite_id = $this->Conn->GetOne($sql);
$object =& $event->getObject(Array('skip_autoload' => true));
/* @var $object kDBItem */
if ($favorite_id) {
$object->Delete($favorite_id);
}
else {
$object->Create();
}
$event->SetRedirectParam('pass', 'm,'.$parent_prefix);
}
/**
* Prepares Favorite record fields
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$user_id = $this->Application->RecallVar('user_id');
$object->SetDBField('PortalUserId', $user_id);
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$parent_object =& $this->Application->recallObject($parent_prefix);
/* @var $parent_object kDBItem */
$object->SetDBField('ResourceId', $parent_object->GetDBField('ResourceId'));
$object->SetDBField('ItemTypeId', $this->Application->getUnitOption($parent_prefix, 'ItemType'));
}
/**
* [HOOK] Deletes favorite record to item, that is beeing deleted
*
* @param kEvent $event
*/
function OnDeleteFavoriteItem(&$event)
{
$main_object =& $event->MasterEvent->getObject();
$sql = 'DELETE FROM '.$this->Application->getUnitOption($event->Prefix, 'TableName').'
WHERE ResourceId = '.$main_object->GetDBField('ResourceId');
$this->Conn->Query($sql);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/files/file_eh.php
===================================================================
--- branches/5.2.x/core/units/files/file_eh.php (revision 14627)
+++ branches/5.2.x/core/units/files/file_eh.php (revision 14628)
@@ -1,87 +1,107 @@
<?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 FileEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnDownloadFile' => Array('subitem' => 'view'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Remembers user, who is created file record. Makes file primary if no other files are uploaded.
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
+ parent::OnBeforeItemCreate($event);
+
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$object->SetDBField('CreatedById', $this->Application->RecallVar('user_id'));
}
/**
* Resets primary file mark when more then one file is marked as primary
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
+ parent::OnBeforeItemUpdate($event);
+
$object =& $event->getObject();
+ /* @var $object kDBItem */
- if (!$object->GetDBField('FileName')) {
+ if ( !$object->GetDBField('FileName') ) {
$object->SetDBField('FileName', basename($object->GetDBField('FilePath')));
}
}
- function SetCustomQuery(&$event)
+ /**
+ * Apply any custom changes to list's sql query
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
+ */
+ protected function SetCustomQuery(&$event)
{
parent::SetCustomQuery($event);
$object =& $event->getObject();
-
+ /* @var $object kDBList */
+
if (!$this->Application->isAdminUser) {
$object->addFilter('active_filter', '%1$s.Status = '.STATUS_ACTIVE);
}
}
/**
* Returns file contents associated with item
*
* @param kEvent $event
*/
function OnDownloadFile(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$file_helper =& $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
$filename = $object->GetField('FilePath', 'full_path');
$file_helper->DownloadFile($filename);
$event->status = kEvent::erSTOP;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/email_message_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/email_message_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/email_message_helper.php (revision 14628)
@@ -1,75 +1,76 @@
<?php
class EmailMessageHelper extends kHelper {
/**
* Extracts Subject, Headers, Body fields from email message translation
*
* @param string $text
* @return Array
*/
function parseTemplate($text)
{
+ $line_id = 1;
$ret = Array ('Subject' => '', 'Headers' => '', 'Body' => '');
$headers = Array();
$lines = explode("\n", $text); // "\n" is lost in process
foreach ($lines as $line_id => $line) {
if (strlen(trim($line)) == 0 || ($line == '.')) {
break;
}
$parts = explode(':', $line, 2);
if (strtolower($parts[0]) == 'subject') {
$ret['Subject'] = trim($parts[1]);
}
else {
$headers[] = $line;
}
}
$ret['Headers'] = $headers ? implode("\n", $headers) : null; // it's null field
$lines = array_slice($lines, $line_id + 1);
// add "\n", that was lost before
$ret['Body'] = implode("\n", $lines);
return $ret;
}
/**
* Prepares email event content for language pack export
*
* @param Array $fields_hash
* @return string
*/
function buildTemplate($fields_hash)
{
if (!implode('', $fields_hash)) {
return '';
}
$ret = array_key_exists('Headers', $fields_hash) ? $fields_hash['Headers'] : '';
if ($ret) {
$ret .= "\n";
}
$ret = $this->_removeTrailingCRLF($ret);
$ret .= 'Subject: ' . $fields_hash['Subject'] . "\n\n";
$ret .= $fields_hash['Body'];
return $ret;
}
/**
* Remove trailing CR/LF chars from string
*
* @param string $string
* @return string
*/
function _removeTrailingCRLF($string)
{
return preg_replace('/(\n|\r)+/',"\\1",$string);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/spam_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/spam_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/spam_helper.php (revision 14628)
@@ -1,167 +1,170 @@
<?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 SpamHelper extends kHelper {
/**
* Table name where spam control information is keeped
*
* @var string
*/
var $TableName;
/**
* RecourceId field of current item
*
* @var int
*/
var $ResourceId = 0;
/**
* ResourceId from all items in list
*
* @var Array
*/
var $ListResourceIDs = Array ();
/**
* Type of information to put into spam control
*
* @var string
*/
var $DataType = '';
/**
* Default spam control record expiration
*
* @var int
*/
var $Expiration = 0;
var $ExpirationCache = Array ();
public function __construct()
{
parent::__construct();
$this->TableName = TABLE_PREFIX.'SpamControl';
}
/**
* Initializes helper for concrete item
*
* @param int $resource_id
* @param string $data_type
* @param int $expiration
*/
function InitHelper($resource_id, $data_type, $expiration, $list_resource_ids = Array ())
{
$this->DataType = $data_type;
$this->ResourceId = $resource_id;
$this->ListResourceIDs = $list_resource_ids ? $list_resource_ids : Array ($resource_id);
if (preg_match('/(.*):(.*)/', $expiration, $regs)) {
$delay_value = $this->Application->ConfigValue($regs[1]);
$delay_interval = $this->Application->ConfigValue($regs[2]);
$expiration = $delay_value * $delay_interval;
}
$this->Expiration = adodb_mktime() + $expiration;
}
/**
* Returns WHERE clause that identified each spam control record
*
* @param bool $as_array return result as array, not string
*
* @return string
*/
function GetKeyClause($as_array = false)
{
$user_id = $this->Application->RecallVar('user_id');
if ($user_id == 0) {
$user_id = USER_GUEST;
}
$keys = Array (
'IPaddress' => $_SERVER['REMOTE_ADDR'],
'PortalUserId' => $user_id,
'DataType' => $this->DataType,
);
if ($as_array) {
$keys['ItemResourceId'] = $this->ResourceId;
return $keys;
}
$ret = '';
foreach ($keys as $field_name => $field_value) {
$ret .= '(' . $field_name . ' = ' . $this->Conn->qstr($field_value) . ') AND ';
}
$ret .= '(ItemResourceId IN (' . implode(',', $this->ListResourceIDs) . '))';
return $ret;
}
/**
* Allows to add current item in spam control
*
*/
function AddToSpamControl()
{
$fields_hash = $this->GetKeyClause(true);
$fields_hash['Expire'] = $this->Expiration;
$this->Conn->doInsert($fields_hash, $this->TableName);
if (!array_key_exists($this->DataType, $this->ExpirationCache)) {
$this->ExpirationCache[$this->DataType][$this->ResourceId] = $this->Expiration;
}
}
/**
* Allows to check if current item is in spam control
*
* @return bool
*/
function InSpamControl()
{
if (!array_key_exists($this->DataType, $this->ExpirationCache)) {
$key_clause = $this->GetKeyClause();
$sql = 'SELECT Expire, ItemResourceId
FROM '.$this->TableName.'
WHERE '.$key_clause;
$this->ExpirationCache[$this->DataType] = $this->Conn->GetCol($sql, 'ItemResourceId');
}
+ else {
+ $key_clause = '';
+ }
$cache =& $this->ExpirationCache[$this->DataType];
$expires = array_key_exists($this->ResourceId, $cache) ? $cache[$this->ResourceId] : false;
if ($expires && $expires < adodb_mktime()) {
// spam control record is expired
$sql = 'DELETE FROM '.$this->TableName.'
WHERE '.$key_clause;
$this->Conn->Query($sql);
return false;
}
return $expires ? true : false;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/skin_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/skin_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/skin_helper.php (revision 14628)
@@ -1,232 +1,235 @@
<?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 SkinHelper extends kHelper {
/**
* Allows to read some skin fields and build link to admin skin file.
* Will automatically compile skin file, when missing.
*
* @param Array $params
* @return string
*/
function AdminSkinTag($params)
{
if (array_key_exists('type', $params)) {
// returns given field of skin
return $this->_getStyleField( $params['type'] );
}
$style_info = $this->_getStyleInfo();
if (file_exists( $this->getSkinPath() )) {
// returns last compiled skin
$ret = $this->getSkinPath(true);
}
else {
// search for previously compiled skin
$last_compiled = $this->_getLastCompiled( mb_strtolower($style_info['Name']) );
if ($last_compiled) {
// found
$ret = $this->getSkinPath(true, $last_compiled);
}
else {
// not found (try to compile on the fly)
$skin =& $this->Application->recallObject('skin.-item', null, Array ('skip_autoload' => true));
/* @var $skin kDBItem */
$skin->Load(1, 'IsPrimary');
$last_compiled = $this->compile($skin);
$ret = $last_compiled ? $this->getSkinPath(true, $last_compiled) : '';
}
}
if (array_key_exists('file_only', $params) && $params['file_only']) {
return $ret;
}
return '<link rel="stylesheet" rev="stylesheet" href="' . $ret . '" type="text/css" media="screen"/>';
}
/**
* Compiles given skin object
*
* @param kDBItem $object
*/
function compile(&$object)
{
$ret = $object->GetDBField('CSS');
$options = $object->GetDBField('Options');
$options = unserialize($options);
$options['base_url'] = Array ('Value' => rtrim(BASE_PATH, '/'));
foreach ($options as $key => $row) {
$ret = str_replace('@@' . $key . '@@', $row['Value'], $ret);
}
$compile_ts = adodb_mktime();
$css_file = $this->_getStylesheetPath() . DIRECTORY_SEPARATOR . 'admin-' . mb_strtolower($object->GetDBField('Name')) . '-' . $compile_ts . '.css';
$fp = fopen($css_file, 'w');
if (!$fp) {
return false;
}
$prev_css = $this->_getStylesheetPath() . '/admin-' . mb_strtolower($object->GetDBField('Name')) . '-' . $object->GetDBField('LastCompiled') . '.css';
if (file_exists($prev_css)) {
unlink($prev_css);
}
fwrite($fp, $ret);
fclose($fp);
$sql = 'UPDATE ' . $object->TableName . '
SET LastCompiled = ' . $compile_ts . '
WHERE ' . $object->IDField . ' = ' . $object->GetID();
$this->Conn->Query($sql);
$this->Application->incrementCacheSerial('skin');
$this->Application->incrementCacheSerial('skin', $object->GetID());
return $compile_ts;
}
/**
* Returns fields of primary admin skin
*
* @return Array
+ * @access protected
*/
- function _getStyleInfo()
+ protected function _getStyleInfo()
{
$cache_key = 'primary_skin_info[%SkinSerial%]';
$ret = $this->Application->getCache($cache_key);
if ($ret === false) {
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'Skins
WHERE IsPrimary = 1';
$ret = $this->Conn->GetRow($sql);
$this->Application->setCache($cache_key, $ret);
}
return $ret;
}
/**
* Returns requested field value of primary admin skin
*
* @param string $field
* @return string
*/
function _getStyleField($field)
{
if ($field == 'logo') {
// old style method of calling
$field = 'Logo';
}
$style_info = $this->_getStyleInfo();
if (!$style_info[$field]) {
return '';
}
$image_fields = Array ('Logo', 'LogoBottom', 'LogoLogin');
if (in_array($field, $image_fields)) {
return $this->_getStylesheetPath(true) . '/' . $style_info[$field];
}
return $style_info[$field];
}
/**
* Returns path, where compiled skin and it's image files are stored
*
* @param bool $url
* @return string
+ * @access protected
*/
- function _getStylesheetPath($url = false)
+ protected function _getStylesheetPath($url = false)
{
if ($url) {
return $this->Application->BaseURL( str_replace(DIRECTORY_SEPARATOR, '/', WRITEBALE_BASE) ) . 'user_files';
}
return WRITEABLE . DIRECTORY_SEPARATOR . 'user_files';
}
/**
* Returns full path to primary admin skin using given or last compiled date
*
- * @param string $url
+ * @param string|bool $url
* @param int $compile_date
* @return string
+ * @access public
*/
- function getSkinPath($url = false, $compile_date = null)
+ public function getSkinPath($url = false, $compile_date = null)
{
$style_info = $this->_getStyleInfo();
- if (!isset($compile_date)) {
+ if ( !isset($compile_date) ) {
$compile_date = $style_info['LastCompiled'];
}
$style_name = 'admin-' . mb_strtolower($style_info['Name']) . '-' . $compile_date . '.css';
return $this->_getStylesheetPath($url) . ($url ? '/' : DIRECTORY_SEPARATOR) . $style_name;
}
/**
* Returns maximal compilation date for given skin name
*
* @param string $style_name
* @return int
*/
function _getLastCompiled($style_name)
{
$last_compiled = 0;
$dh = dir($this->_getStylesheetPath() . DIRECTORY_SEPARATOR);
while (false !== ($file = $dh->read())) {
if ( preg_match('/admin-(.*)-([\d]+).css/', $file, $rets) ) {
if ($rets[1] == $style_name && $rets[2] > $last_compiled) {
$last_compiled = $rets[2];
}
}
}
$dh->close();
return $last_compiled;
}
/**
* Deletes all compiled versions of all skins
*
*/
function deleteCompiled()
{
$dh = dir($this->_getStylesheetPath() . DIRECTORY_SEPARATOR);
while (false !== ($file = $dh->read())) {
if ( preg_match('/admin-(.*)-([\d]+).css/', $file, $rets) ) {
unlink($dh->path . $file);
}
}
$dh->close();
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/geocode_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/geocode_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/geocode_helper.php (revision 14628)
@@ -1,186 +1,189 @@
<?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 GeoCodeHelper extends kHelper {
/**
* Returns Longitude & Latitude by US address provided
*
* @param Array $data
* @return Array
*/
function QueryCoordinates($address, $city, $state, $zip)
{
$user = $this->Application->ConfigValue('GeoCodeUser');
$pass = $this->Application->ConfigValue('GeoCodePass');
$rie_path = sprintf(FULL_PATH.'/tools/rie/rie -u %s -p %s -g "%s" -l',
$user,
$pass,
$address.'|'.$city.'|'.$state.'|'.$zip
);
exec($rie_path, $geo_array, $code);
if ($code == 0) {
$out_data = explode('|', $geo_array[2]);
// include_once(FULL_PATH.'/compat/array_combine.php');
$assoc_data = array_combine(explode('|', $geo_array[1]), $out_data);
$lon = abs($out_data[8]); // set to positive, because required by SQL formula
$lat = $out_data[7];
$zip4 = $out_data[9];
$dpbc = $out_data[10];
$carrier = $out_data[11];
}
else {
$lon = '';
$lat = '';
$zip4 = '';
$dpbc = '';
$carrier = '';
$assoc_data = Array();
}
return Array($lon, $lat, $zip4, $dpbc, $carrier, serialize($assoc_data));
}
function getTag($tag, $xml)
{
$open_tag_pos = strpos($xml, '<'.$tag.'>');
$close_tag_pos = strpos($xml, '</'.$tag.'>');
if (!$open_tag_pos || !$close_tag_pos)
{
return '';
}
$tag_length = strlen($tag) + 2;
return substr($xml, $open_tag_pos + $tag_length, $close_tag_pos - $open_tag_pos - $tag_length);
}
function QueryCoordinatesFromGoogle($address, $city, $state, $zip)
{
// 1908 Pike pl, Seattle, WA
// http://maps.google.com/maps/geo?
// ABQIAAAAzNbTbxHki-PAnXzsrA7z2hR0fs2_a3JecCfKmMFhGT8VtEjV7xRV8rMK1czaEH2ZG3eiYJMuej_vnQ
$qaddress = $address.', '.$city.', '.$state;
$request_url = $this->Application->ConfigValue('GoogleMapsURL').'output=xml&key='.
$this->Application->ConfigValue('GoogleMapsKey').'&q='.urlencode($qaddress);
$curl_helper =& $this->Application->recallObject('CurlHelper');
/* @var $curl_helper kCurlHelper */
$delay = 0;
while (true)
{
$xml = $curl_helper->Send($request_url);
if (strpos($xml, '<code>620</code>')) {
$delay += 100000;
} elseif (strpos($xml, '<code>200</code>')) {
// get latitude, longitude and zip from xml-answer
$a_coords = explode(',', $this->getTag('coordinates', $xml));
$lat = $a_coords[1];
$lon = abs($a_coords[0]); // set to positive, because required by SQL formula
$zip4 = $this->getTag('PostalCodeNumber', $xml);
$dpbc = '';
$carrier = '';
$assoc_data = Array();
break;
} else {
$lon = '';
$lat = '';
$zip4 = '';
$dpbc = '';
$carrier = '';
$assoc_data = Array();
break;
}
usleep($delay);
}
return Array($lon, $lat, $zip4, $dpbc, $carrier, serialize($assoc_data));
}
/**
* Try to find lon, lat by address return false if failed
*
* @param string $address
* @param string $city
* @param string $state
* @param int $zip
- * @return Array (lon, lat)
+ * @param bool $no_cache
+ * @param bool $force_cache
+ * @return Array|bool (lon, lat)
+ * @access public
*/
- function GetCoordinates($address, $city, $state, $zip, $no_cache = false, $force_cache = false)
+ public function GetCoordinates($address, $city, $state, $zip, $no_cache = false, $force_cache = false)
{
- if (!$zip && !$state) {
+ if ( !$zip && !$state ) {
// if state or zip missing then do nothing
return false;
}
$zip_info = $no_cache ? false : $this->GetFromCache($address, $city, $state, $zip);
-// $zip_info = $this->GetFromCache($address, $city, $state, $zip);
- if (!$zip_info && !$force_cache) {
+ if ( !$zip_info && !$force_cache ) {
list($lon, $lat, $zip4, $dpbc, $carrier, $geocode_answer) = $this->QueryCoordinatesFromGoogle($address, $city, $state, $zip);
- if ($lon != '' && $lat != '') {
+ if ( $lon != '' && $lat != '' ) {
// valid position returned by geocode => add to cache
$fields_hash = Array(
- 'zipcode' => $zip4,
- 'address' => $address,
- 'city' => $city,
- 'state' => $state,
- 'lat' => $lat,
- 'lon' => $lon,
- 'zip4' => $zip4,
- 'dpbc' => $dpbc,
- 'carrier' => $carrier,
- 'geocode_answer' => $geocode_answer,
- );
- $this->Conn->doInsert($fields_hash, TABLE_PREFIX.'ZipCodes');
- return Array($lon, $lat, $zip4, $dpbc, $carrier);
+ 'zipcode' => $zip4,
+ 'address' => $address,
+ 'city' => $city,
+ 'state' => $state,
+ 'lat' => $lat,
+ 'lon' => $lon,
+ 'zip4' => $zip4,
+ 'dpbc' => $dpbc,
+ 'carrier' => $carrier,
+ 'geocode_answer' => $geocode_answer,
+ );
+
+ $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'ZipCodes');
+ return Array ($lon, $lat, $zip4, $dpbc, $carrier);
}
else {
// bad case, rie call failed => no data retrieved
return false;
}
}
- return Array($zip_info['lon'], $zip_info['lat'], getArrayValue($zip_info, 'zip4'), getArrayValue($zip_info, 'dpbc'), getArrayValue($zip_info, 'carrier'));
+ return Array ($zip_info['lon'], $zip_info['lat'], getArrayValue($zip_info, 'zip4'), getArrayValue($zip_info, 'dpbc'), getArrayValue($zip_info, 'carrier'));
}
/**
* Try to find cached lon, lat by address
*
* @param string $address
* @param string $city
* @param string $state
* @param int $zip
- * @return Array (lon, lat)
+ * @return Array|bool (lon, lat)
*/
function GetFromCache($address, $city, $state, $zip)
{
$zip = substr($zip, 0, 5); // use only first 5 digits
$sql = 'SELECT lon, lat
- FROM '.TABLE_PREFIX.'ZipCodes
- WHERE zipcode = '.$this->Conn->qstr($zip);
+ FROM ' . TABLE_PREFIX . 'ZipCodes
+ WHERE zipcode = ' . $this->Conn->qstr($zip);
return $this->Conn->GetRow($sql);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/json_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/json_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/json_helper.php (revision 14628)
@@ -1,167 +1,173 @@
<?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 JSONHelper extends kHelper {
// var $match = Array ('\\', "\n", "\t", "\r", "\b", "\f", '"');
// var $replace = Array ('\\\\', '\\n', '\\t', '\\r', '\\b', '\\f', '\"');
// \x{2028} is some strange char found in GTA responses... it breaks javascript
var $match = Array ('/\\\\/u', '/\\"/u', '/\\t/u', '/\\n/u', '/\\r/u', '/\\x{2028}/u');
var $replace = Array ('\\\\\\\\', '\\"', '\\t', '\\n', '\\r', '');
/**
* Object constructor
*
* @return JSONHelper
*/
function JSONHelper() {
}
/**
* Parse structure to JSON
*
* @param mixed $data
* @return string
*/
function parse($data)
{
return '('.$this->encode($data).')';
}
/**
* Converts PHP variable to JSON string
* Can convert any PHP variable with any content to correct JSON structure
*
* @param mixed $data Data to convert
* @return string
*/
function encode($data)
{
$type = $this->getType($data);
- switch ($type) {
+ switch ( $type ) {
case 'object':
- $data = '{'.$this->processData($data, $type).'}';
+ $data = '{' . $this->processData($data, $type) . '}';
break;
case 'array':
- $data = '['.$this->processData($data, $type).']';
+ $data = '[' . $this->processData($data, $type) . ']';
break;
default:
- if (is_int($data) || is_float($data)) {
+ if ( is_int($data) || is_float($data) ) {
$data = (string)$data;
- } elseif (is_string($data)) {
- $data = '"'.$this->escape($data).'"';
- } elseif (is_bool($data)) {
+ }
+ elseif ( is_string($data) ) {
+ $data = '"' . $this->escape($data) . '"';
+ }
+ elseif ( is_bool($data) ) {
$data = $data ? 'true' : 'false';
- } else {
+ }
+ else {
$data = 'null';
}
}
+
return $data;
}
/**
* Enter description here...
*
- * @param unknown_type $input
- * @param unknown_type $type
- * @return unknown
+ * @param Array $data
+ * @param string $type
+ * @return string
*/
function processData($data, $type)
{
- $output = Array();
+ $output = Array ();
// If data is an object - it should be converted as a key => value pair
- if ($type == 'object'){
- foreach($data as $key => $value) {
- $output[] = '"'.$key.'": '.$this->encode($value);
+ if ( $type == 'object' ) {
+ foreach ($data as $key => $value) {
+ $output[] = '"' . $key . '": ' . $this->encode($value);
}
- } else {
- foreach($data as $key => $value) {
+ }
+ else {
+ foreach ($data as $value) {
$output[] = $this->encode($value);
}
}
- return implode(',', $output);;
+
+ return implode(',', $output);
}
/**
* Function determines type of variable
*
- * @param unknown_type $data
- * @return unknown
+ * @param mixed $data
+ * @return string
*/
function getType(&$data)
{
if (is_object($data)) {
$type = 'object';
} elseif (is_array($data)) {
// If array is assoc it should be processed as an object
if($this->is_assoc($data)) {
$type = 'object';
} else {
$type = 'array';
}
} elseif (is_numeric($data)) {
$type = 'number';
} elseif(is_string($data)) {
$type = 'string';
} elseif(is_bool($data)) {
$type = 'boolean';
} elseif(is_null($data)) {
$type = 'null';
} else {
$type = 'string';
}
return $type;
}
/**
* Function determines if array is associative
*
* @param array $array
* @return bool
*/
function is_assoc($array)
{
// Arrays are defined as integer-indexed arrays starting at index 0, where
// the last index is (count($array) -1); any deviation from that is
// considered an associative array, and will be encoded as such (from Zend Framework).
return !empty($array) && (array_keys($array) !== range(0, count($array) - 1));
}
/**
* Escapes special characters
* Function escapes ", \, /, \n and \r symbols so that not to cause JavaScript error or
* data loss
*
* @param string $string
* @return string
*/
function escape($string)
{
$string = preg_replace($this->match, $this->replace, $string);
return $string;
// Escape certain ASCII characters:
// 0x08 => \b
// 0x0c => \f
// return str_replace(array(chr(0x08), chr(0x0C)), array('\b', '\f'), $string);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/mailing_list_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/mailing_list_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/mailing_list_helper.php (revision 14628)
@@ -1,284 +1,292 @@
<?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 MailingListHelper extends kHelper {
var $_mailingId = false;
/**
* Adds new email from given mailing to emails queue
*
* @param string $email
* @param int $mailing_id
* @param Array $mailing_data
*/
function queueEmail($email, $mailing_id, &$mailing_data)
{
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
if ($this->_mailingId != $mailing_id) {
if (is_numeric($this->_mailingId)) {
// clear fields after previous mailing processing
$esender->Clear();
}
// 1. set headers same for all emails
list ($mailing_data['FromName'], $mailing_data['FromEmail']) = $this->_getSenderData($mailing_data);
$esender->SetFrom($mailing_data['FromEmail'], $mailing_data['FromName']);
$esender->SetSubject($mailing_data['Subject']);
$esender->SetBody($mailing_data['MessageHtml'], $mailing_data['MessageText']);
// 2. add attachment if any
$attachments = $mailing_data['Attachments'] ? explode('|', $mailing_data['Attachments']) : Array ();
foreach ($attachments as $attachment) {
$esender->AddAttachment(FULL_PATH . ITEM_FILES_PATH . $attachment);
}
$this->_mailingId = $mailing_id;
}
// 3. set recipient specific fields
$esender->SetTo($email, $email);
$esender->Deliver(null, $mailing_id, false);
// 4. write to log
$log_fields_hash = Array (
'fromuser' => $mailing_data['FromName'] . '<' . $mailing_data['FromEmail'] . '>',
'addressto' => $email,
'subject' => $mailing_data['Subject'],
'timestamp' => adodb_mktime(),
'EventParams' => serialize( Array ('MailingId' => $mailing_id) ),
);
$this->Conn->doInsert($log_fields_hash, TABLE_PREFIX . 'EmailLog');
}
/**
* Returns mass mail sender name & email
*
+ * @param Array $mailing_data
* @return Array
+ * @access protected
*/
- function _getSenderData(&$mailing_data)
+ protected function _getSenderData(&$mailing_data)
{
$is_root = true;
- if ($mailing_data['PortalUserId'] > 0) {
+ $email_address = $name = '';
+
+ if ( $mailing_data['PortalUserId'] > 0 ) {
$sender =& $this->Application->recallObject('u.-item', null, Array ('skip_autoload' => true));
/* @var $sender UsersItem */
$sender->Load($mailing_data['PortalUserId']);
$email_address = $sender->GetDBField('Email');
- $name = trim( $sender->GetDBField('FirstName') . ' ' . $sender->GetDBField('LastName') );
+ $name = trim($sender->GetDBField('FirstName') . ' ' . $sender->GetDBField('LastName'));
$is_root = false;
}
- if ($is_root || !$email_address) {
+ if ( $is_root || !$email_address ) {
$email_address = $this->Application->ConfigValue('Smtp_AdminMailFrom');
}
- if ($is_root || !$name) {
- $name = strip_tags( $this->Application->ConfigValue('Site_Name') );
+ if ( $is_root || !$name ) {
+ $name = strip_tags($this->Application->ConfigValue('Site_Name'));
}
return Array ($name, $email_address);
}
/**
* Generates recipients emails based on "To" field value
*
* @param int $id
* @param Array $fields_hash
*/
function generateRecipients($id, $fields_hash)
{
$recipients = explode(';', $fields_hash['To']);
// 1. group recipients by types
$recipients_grouped = Array ();
foreach ($recipients as $recipient) {
if (strpos($recipient, '_') !== false) {
list ($recipient_type, $recipient_id) = explode('_', $recipient);
}
else {
$recipient_type = 'direct';
$recipient_id = $recipient;
}
if (!array_key_exists($recipient_type, $recipients_grouped)) {
$recipients_grouped[$recipient_type] = Array ();
}
$recipients_grouped[$recipient_type][] = $recipient_id;
}
// for each group convert ids to names
$recpient_emails = Array ();
foreach ($recipients_grouped as $recipient_type => $group_recipients) {
$recpient_emails = array_merge($recpient_emails, $this->_getRecipientEmails($recipient_type, $group_recipients));
}
$recpient_emails = array_unique($recpient_emails);
return Array (
'ToParsed' => serialize($recpient_emails),
'EmailsTotal' => count($recpient_emails),
);
}
function _getRecipientEmails($recipient_type, $recipient_ids)
{
if (strpos($recipient_type, '.') !== false) {
// remove special
list ($recipient_type, ) = explode('.', $recipient_type);
}
if ($recipient_type != 'u' && $recipient_type != 'g') {
- // theese are already emails
+ // these are already emails
return $recipient_ids;
}
switch ($recipient_type) {
case 'u':
$sql = 'SELECT Email
FROM ' . TABLE_PREFIX . 'PortalUser
WHERE (PortalUserId IN (' . implode(',', $recipient_ids) . ')) AND (Email <> "")';
break;
case 'g':
$sql = 'SELECT u.Email
FROM ' . TABLE_PREFIX . 'UserGroup ug
LEFT JOIN ' . TABLE_PREFIX . 'PortalUser u ON u.PortalUserId = ug.PortalUserId
WHERE (ug.GroupId IN (' . implode(',', $recipient_ids) . ')) AND (u.Email <> "")';
break;
+
+ default:
+ $sql = '';
+ break;
}
return $this->Conn->GetCol($sql);
}
function getRecipientNames($recipient_type, $recipient_ids)
{
if (strpos($recipient_type, '.') !== false) {
// remove special
list ($recipient_type, ) = explode('.', $recipient_type);
}
switch ($recipient_type) {
case 'u':
$title_field = 'Email';
break;
case 'g':
$title_field = 'Name';
break;
default:
$title_field = false;
break;
}
if ($title_field === false || !$recipient_ids) {
return $recipient_ids;
}
$id_field = $this->Application->getUnitOption($recipient_type, 'IDField');
$table_name = $this->Application->getUnitOption($recipient_type, 'TableName');
$sql = 'SELECT ' . $title_field . '
FROM ' . $table_name . '
WHERE ' . $id_field . ' IN (' . implode(',', $recipient_ids) . ')';
return $this->Conn->GetCol($sql);
}
/**
* Updates information about sent email count based on given totals by mailings
*
* @param Array $mailing_totals
*/
function _updateSentTotals($mailing_totals)
{
if (array_key_exists(0, $mailing_totals)) {
// don't update sent email count for mails queued directly (not via mailing lists)
unset($mailing_totals[0]);
}
$id_field = $this->Application->getUnitOption('mailing-list', 'IDField');
$table_name = $this->Application->getUnitOption('mailing-list', 'TableName');
// update sent email count for each processed mailing
foreach ($mailing_totals as $mailing_id => $mailing_total) {
$sql = 'UPDATE ' . $table_name . '
SET EmailsSent = EmailsSent + ' . $mailing_total . '
WHERE ' . $id_field . ' = ' . $mailing_id;
$this->Conn->Query($sql);
}
// mark mailings, that were processed completely
$sql = 'UPDATE ' . $table_name . '
SET Status = ' . MailingList::PROCESSED . '
WHERE (Status = ' . MailingList::PARTIALLY_PROCESSED . ') AND (EmailsSent = EmailsTotal)';
$this->Conn->Query($sql);
}
/**
* Sent given messages from email queue
*
* @param Array $messages
*/
function processQueue(&$messages)
{
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
$queue_table = $this->Application->getUnitOption('email-queue', 'TableName');
$i = 0;
$message = Array ();
$mailing_totals = Array ();
$message_count = count($messages);
while ($i < $message_count) {
$message[0] = unserialize($messages[$i]['MessageHeaders']);
$message[1] =& $messages[$i]['MessageBody'];
$delivered = $esender->Deliver($message, true); // immediate send!
if ($delivered) {
// send succseeded, delete from queue
$sql = 'DELETE FROM ' . $queue_table . '
WHERE EmailQueueId = ' . $messages[$i]['EmailQueueId'];
$this->Conn->Query($sql);
$mailing_id = $messages[$i]['MailingId'];
if (!array_key_exists($mailing_id, $mailing_totals)) {
$mailing_totals[$mailing_id] = 0;
}
$mailing_totals[$mailing_id]++;
}
else {
// send failed, increment retries counter
$sql = 'UPDATE ' . $queue_table . '
SET SendRetries = SendRetries + 1, LastSendRetry = ' . adodb_mktime() . '
WHERE EmailQueueId = ' . $messages[$i]['EmailQueueId'];
$this->Conn->Query($sql);
}
$i++;
}
$this->_updateSentTotals($mailing_totals);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/list_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/list_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/list_helper.php (revision 14628)
@@ -1,125 +1,125 @@
<?php
class ListHelper extends kHelper {
/**
* Detects, that current sorting of the list is not default
*
* @param kDBList $list
* @return bool
*/
function hasUserSorting(&$list)
{
static $cache = Array ();
if (array_key_exists($list->getPrefixSpecial(), $cache)) {
return $cache[ $list->getPrefixSpecial() ];
}
$user_sorting_start = $this->getUserSortIndex($list);
$sorting_configs = $this->Application->getUnitOption($list->Prefix, 'ConfigMapping', Array ());
$list_sortings = $this->Application->getUnitOption($list->Prefix, 'ListSortings', Array ());
$sorting_prefix = getArrayValue($list_sortings, $list->Special) ? $list->Special : '';
if (array_key_exists('DefaultSorting1Field', $sorting_configs)) {
$list_sortings[$sorting_prefix]['Sorting'] = Array (
$this->Application->ConfigValue($sorting_configs['DefaultSorting1Field']) => $this->Application->ConfigValue($sorting_configs['DefaultSorting1Dir']),
$this->Application->ConfigValue($sorting_configs['DefaultSorting2Field']) => $this->Application->ConfigValue($sorting_configs['DefaultSorting2Dir']),
);
}
$sorting = getArrayValue($list_sortings, $sorting_prefix, 'Sorting');
$sort_fields = is_array($sorting) ? array_keys($sorting) : Array ();
for ($order_number = 0; $order_number < 2; $order_number++) {
// currect sorting in list
$sorting_pos = $user_sorting_start + $order_number;
$current_order_field = $list->GetOrderField($sorting_pos, true);
$current_order_direction = $list->GetOrderDirection($sorting_pos, true);
if (!$current_order_field || !$current_order_direction) {
// no sorting defined for this sorting position
continue;
}
// remove language prefix from field
$field_options = $list->GetFieldOptions($current_order_field);
if (array_key_exists('formatter', $field_options) && $field_options['formatter'] == 'kMultiLanguage') {
// remove language prefix
$current_order_field = preg_replace('/^l[\d]+_(.*)/', '\\1', $current_order_field);
}
// user sorting found
if (array_key_exists($order_number, $sort_fields)) {
// default sorting found
$default_order_field = $sort_fields[$order_number];
$default_order_direction = $sorting[$default_order_field]; // because people can write
if ($current_order_field != $default_order_field || strcasecmp($current_order_direction, $default_order_direction) != 0) {
// #1. user sorting differs from default sorting -> changed
$cache[ $list->getPrefixSpecial() ] = true;
return true;
}
}
else {
// #2. user sorting + no default sorting -> changed
$cache[ $list->getPrefixSpecial() ] = true;
return true;
}
}
// #3. user sorting match default or not defined -> not changed
$cache[ $list->getPrefixSpecial() ] = false;
return false;
}
/**
* Returns default per-page value for given prefix
*
* @param string $prefix
* @param int $default
* @return int
*/
function getDefaultPerPage($prefix, $default = 10)
{
$ret = false;
$config_mapping = $this->Application->getUnitOption($prefix, 'ConfigMapping');
if ($config_mapping) {
if (!array_key_exists('PerPage', $config_mapping)) {
- trigger_error('Incorrect mapping of <span class="debug_error">PerPage</span> key in config for prefix <strong>' . $event->Prefix . '</strong>', E_USER_WARNING);
+ trigger_error('Incorrect mapping of <span class="debug_error">PerPage</span> key in config for prefix <strong>' . $prefix . '</strong>', E_USER_WARNING);
}
$per_page = $this->Application->ConfigValue($config_mapping['PerPage']);
if ($per_page) {
return $per_page;
}
}
// none of checked above per-page locations are useful, then try default value
return $default;
}
/**
* Returns index where 1st changable sorting field begins
*
* @param kDBList $list
* @return int
* @todo This is copy of kDBTagProcessor::getUserSortIndex method.
* Can't call helper there, because it will slow down grid output
* when we have a lot of columns
*/
function getUserSortIndex(&$list)
{
$list_sortings = $this->Application->getUnitOption($list->Prefix, 'ListSortings', Array ());
$sorting_prefix = getArrayValue($list_sortings, $list->Special) ? $list->Special : '';
$user_sorting_start = 0;
if ( $forced_sorting = getArrayValue($list_sortings, $sorting_prefix, 'ForcedSorting') ) {
$user_sorting_start = count($forced_sorting);
}
return $user_sorting_start;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/language_import_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/language_import_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/language_import_helper.php (revision 14628)
@@ -1,1097 +1,1101 @@
<?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.
*/
/**
* Language pack format version description
*
* v1
* ==========
* All language properties are separate nodes inside <LANGUAGE> node. There are
* two more nodes PHRASES and EVENTS for phrase and email event translations.
*
* v2
* ==========
* All data, that will end up in Language table is now attributes of LANGUAGE node
* and is name exactly as field name, that will be used to store that data.
*
* v4
* ==========
* Hint & Column translation added to each phrase translation
*/
defined('FULL_PATH') or die('restricted access!');
define('LANG_OVERWRITE_EXISTING', 1);
define('LANG_SKIP_EXISTING', 2);
class LanguageImportHelper extends kHelper {
/**
* Current Language in import
*
* @var LanguagesItem
*/
var $lang_object = null;
/**
* Current user's IP address
*
* @var string
*/
var $ip_address = '';
/**
* Event type + name mapping to id (from system)
*
* @var Array
*/
var $events_hash = Array ();
/**
* Language pack import mode
*
* @var int
*/
var $import_mode = LANG_SKIP_EXISTING;
/**
* Language IDs, that were imported
*
* @var Array
*/
var $_languages = Array ();
/**
* Temporary table names to perform import on
*
* @var Array
*/
var $_tables = Array ();
/**
* Phrase types allowed for import/export operations
*
* @var Array
*/
var $phrase_types_allowed = Array ();
/**
* Encoding, used for language pack exporting
*
* @var string
*/
var $_exportEncoding = 'base64';
/**
* Exported data limits (all or only specified ones)
*
* @var Array
*/
var $_exportLimits = Array (
'phrases' => false,
'emailevents' => false,
);
/**
* Debug language pack import process
*
* @var bool
*/
var $_debugMode = false;
/**
* Latest version of language pack format. Versions are not backwards compatible!
*
* @var int
*/
var $_latestVersion = 4;
/**
* Prefix-based serial numbers, that should be changed after import is finished
*
* @var Array
*/
var $changedPrefixes = Array ();
public function __construct()
{
parent::__construct();
// "core/install/english.lang", phrase count: 3318, xml parse time on windows: 10s, insert time: 0.058s
set_time_limit(0);
ini_set('memory_limit', -1);
$this->lang_object =& $this->Application->recallObject('lang.import', null, Array ('skip_autoload' => true));
if (!(defined('IS_INSTALL') && IS_INSTALL)) {
// perform only, when not in installation mode
$this->_updateEventsCache();
}
$this->ip_address = getenv('HTTP_X_FORWARDED_FOR') ? getenv('HTTP_X_FORWARDED_FOR') : getenv('REMOTE_ADDR');
// $this->_debugMode = $this->Application->isDebugMode();
}
/**
* Performs import of given language pack (former Parse method)
*
* @param string $filename
* @param string $phrase_types
* @param Array $module_ids
* @param int $import_mode
* @return bool
*/
function performImport($filename, $phrase_types, $module_ids, $import_mode = LANG_SKIP_EXISTING)
{
// define the XML parsing routines/functions to call based on the handler path
if (!file_exists($filename) || !$phrase_types /*|| !$module_ids*/) {
return false;
}
if ($this->_debugMode) {
$start_time = microtime(true);
$this->Application->Debugger->appendHTML(__CLASS__ . '::' . __FUNCTION__ . '("' . $filename . '")');
}
if (defined('IS_INSTALL') && IS_INSTALL) {
// new events could be added during module upgrade
$this->_updateEventsCache();
}
$this->_initImportTables();
$phrase_types = explode('|', substr($phrase_types, 1, -1) );
// $module_ids = explode('|', substr($module_ids, 1, -1) );
$this->phrase_types_allowed = array_flip($phrase_types);
$this->import_mode = $import_mode;
$this->_parseXML($filename);
// copy data from temp tables to live
foreach ($this->_languages as $language_id) {
$this->_performUpgrade($language_id, 'phrases', 'PhraseKey', Array ('l%s_Translation', 'l%s_HintTranslation', 'l%s_ColumnTranslation', 'PhraseType'));
$this->_performUpgrade($language_id, 'emailevents', 'EventId', Array ('l%s_Subject', 'Headers', 'MessageType', 'l%s_Body'));
$this->_performUpgrade($language_id, 'country-state', 'CountryStateId', Array ('l%s_Name'));
}
$this->_initImportTables(true);
$this->changedPrefixes = array_unique($this->changedPrefixes);
foreach ($this->changedPrefixes as $prefix) {
$this->Application->incrementCacheSerial($prefix);
}
if ($this->_debugMode) {
$this->Application->Debugger->appendHTML(__CLASS__ . '::' . __FUNCTION__ . '("' . $filename . '"): ' . (microtime(true) - $start_time));
}
return true;
}
/**
* Creates XML file with exported language data (former Create method)
*
* @param string $filename filename to export into
* @param Array $phrase_types phrases types to export from modules passed in $module_ids
* @param Array $language_ids IDs of languages to export
* @param Array $module_ids IDs of modules to export phrases from
*/
function performExport($filename, $phrase_types, $language_ids, $module_ids)
{
$fp = fopen($filename,'w');
if (!$fp || !$phrase_types || !$module_ids || !$language_ids) {
return false;
}
$phrase_types = explode('|', substr($phrase_types, 1, -1) );
$module_ids = explode('|', substr($module_ids, 1, -1) );
$ret = '<LANGUAGES Version="' . $this->_latestVersion . '">' . "\n";
$export_fields = $this->_getExportFields();
$email_message_helper =& $this->Application->recallObject('EmailMessageHelper');
/* @var $email_message_helper EmailMessageHelper */
// get languages
$sql = 'SELECT *
FROM ' . $this->Application->getUnitOption('lang','TableName') . '
WHERE LanguageId IN (' . implode(',', $language_ids) . ')';
$languages = $this->Conn->Query($sql, 'LanguageId');
// get phrases
$phrase_modules = $module_ids;
array_push($phrase_modules, ''); // for old language packs without module
$phrase_modules = array_map(Array (&$this->Conn, 'qstr'), $phrase_modules);
// apply phrase selection limit
if ($this->_exportLimits['phrases']) {
$escaped_phrases = array_map(Array (&$this->Conn, 'qstr'), $this->_exportLimits['phrases']);
$limit_where = 'Phrase IN (' . implode(',', $escaped_phrases) . ')';
}
else {
$limit_where = 'TRUE';
}
$sql = 'SELECT *
FROM ' . $this->Application->getUnitOption('phrases','TableName') . '
WHERE PhraseType IN (' . implode(',', $phrase_types) . ') AND Module IN (' . implode(',', $phrase_modules) . ') AND ' . $limit_where . '
ORDER BY Phrase';
$phrases = $this->Conn->Query($sql, 'PhraseId');
// email events
$module_sql = preg_replace('/(.*),/U', 'INSTR(Module,\'\\1\') OR ', implode(',', $module_ids) . ',');
// apply event selection limit
if ($this->_exportLimits['emailevents']) {
$escaped_email_events = array_map(Array (&$this->Conn, 'qstr'), $this->_exportLimits['emailevents']);
$limit_where = '`Event` IN (' . implode(',', $escaped_email_events) . ')';
}
else {
$limit_where = 'TRUE';
}
$sql = 'SELECT *
FROM ' . $this->Application->getUnitOption('emailevents', 'TableName') . '
WHERE `Type` IN (' . implode(',', $phrase_types) . ') AND (' . substr($module_sql, 0, -4) . ') AND ' . $limit_where . '
ORDER BY `Event`, `Type`';
$events = $this->Conn->Query($sql, 'EventId');
if (in_array('Core', $module_ids)) {
// countries
$sql = 'SELECT *
FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . '
WHERE Type = ' . DESTINATION_TYPE_COUNTRY . '
ORDER BY `IsoCode`';
$countries = $this->Conn->Query($sql, 'CountryStateId');
// states
$sql = 'SELECT *
FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . '
WHERE Type = ' . DESTINATION_TYPE_STATE . '
ORDER BY `IsoCode`';
$states = $this->Conn->Query($sql, 'CountryStateId');
foreach ($states as $state_id => $state_data) {
$country_id = $state_data['StateCountryId'];
if (!array_key_exists('States', $countries[$country_id])) {
$countries[$country_id]['States'] = Array ();
}
$countries[$country_id]['States'][] = $state_id;
}
}
foreach ($languages as $language_id => $language_info) {
// language
$ret .= "\t" . '<LANGUAGE Encoding="' . $this->_exportEncoding . '"';
foreach ($export_fields as $export_field) {
$ret .= ' ' . $export_field . '="' . htmlspecialchars($language_info[$export_field]) . '"';
}
$ret .= '>' . "\n";
// filename replacements
$replacements = $language_info['FilenameReplacements'];
if ($replacements) {
$ret .= "\t\t" . '<REPLACEMENTS>';
$ret .= $this->_exportEncoding == 'base64' ? base64_encode($replacements) : '<![CDATA[' . $replacements . ']]>';
$ret .= '</REPLACEMENTS>' . "\n";
}
// phrases
if ($phrases) {
$ret .= "\t\t" . '<PHRASES>' . "\n";
foreach ($phrases as $phrase_id => $phrase) {
$translation = $phrase['l' . $language_id . '_Translation'];
$hint_translation = $phrase['l' . $language_id . '_HintTranslation'];
$column_translation = $phrase['l' . $language_id . '_ColumnTranslation'];
if (!$translation) {
// phrase is not translated on given language
continue;
}
if ( $this->_exportEncoding == 'base64' ) {
$data = base64_encode($translation);
$hint_translation = base64_encode($hint_translation);
$column_translation = base64_encode($column_translation);
}
else {
$data = '<![CDATA[' . $translation . ']]>';
$hint_translation = htmlspecialchars($hint_translation);
$column_translation = htmlspecialchars($column_translation);
}
$attributes = Array (
'Label="' . $phrase['Phrase'] . '"',
'Module="' . $phrase['Module'] . '"',
'Type="' . $phrase['PhraseType'] . '"'
);
if ( $phrase['l' . $language_id . '_HintTranslation'] ) {
$attributes[] = 'Hint="' . $hint_translation . '"';
}
if ( $phrase['l' . $language_id . '_ColumnTranslation'] ) {
$attributes[] = 'Column="' . $column_translation . '"';
}
$ret .= "\t\t\t" . '<PHRASE ' . implode(' ', $attributes) . '>' . $data . '</PHRASE>' . "\n";
}
$ret .= "\t\t" . '</PHRASES>' . "\n";
}
// email events
if ($events) {
$ret .= "\t\t" . '<EVENTS>' . "\n";
foreach ($events as $event_id => $event) {
$fields_hash = Array (
'Headers' => $event['Headers'],
'Subject' => $event['l' . $language_id . '_Subject'],
'Body' => $event['l' . $language_id . '_Body'],
);
$template = $email_message_helper->buildTemplate($fields_hash);
if (!$template) {
// email event is not translated on given language
continue;
}
$data = $this->_exportEncoding == 'base64' ? base64_encode($template) : '<![CDATA[' . $template . ']]>';
$ret .= "\t\t\t" . '<EVENT MessageType="' . $event['MessageType'] . '" Event="' . $event['Event'] . '" Type="' . $event['Type'] . '">' . $data . '</EVENT>'."\n";
}
$ret .= "\t\t" . '</EVENTS>' . "\n";
}
if (in_array('Core', $module_ids) && $countries) {
$ret .= "\t\t" . '<COUNTRIES>' . "\n";
foreach ($countries as $country_id => $country_data) {
$translation = $country_data['l' . $language_id . '_Name'];
if (!$translation) {
// country is not translated on given language
continue;
}
$data = $this->_exportEncoding == 'base64' ? base64_encode($translation) : $translation;
if (array_key_exists('States', $country_data)) {
$ret .= "\t\t\t" . '<COUNTRY Iso="' . $country_data['IsoCode'] . '" Translation="' . $data . '">' . "\n";
foreach ($country_data['States'] as $state_id) {
$translation = $states[$state_id]['l' . $language_id . '_Name'];
if (!$translation) {
// state is not translated on given language
continue;
}
$data = $this->_exportEncoding == 'base64' ? base64_encode($translation) : $translation;
$ret .= "\t\t\t\t" . '<STATE Iso="' . $states[$state_id]['IsoCode'] . '" Translation="' . $data . '"/>' . "\n";
}
$ret .= "\t\t\t" . '</COUNTRY>' . "\n";
}
else {
$ret .= "\t\t\t" . '<COUNTRY Iso="' . $country_data['IsoCode'] . '" Translation="' . $data . '"/>' . "\n";
}
}
$ret .= "\t\t" . '</COUNTRIES>' . "\n";
}
$ret .= "\t" . '</LANGUAGE>' . "\n";
}
$ret .= '</LANGUAGES>';
fwrite($fp, $ret);
fclose($fp);
return true;
}
/**
* Sets language pack encoding (not charset) used during export
*
* @param string $encoding
*/
function setExportEncoding($encoding)
{
$this->_exportEncoding = $encoding;
}
/**
* Sets language pack data limits for export
*
* @param mixed $phrases
* @param mixed $email_events
*/
function setExportLimits($phrases, $email_events)
{
if (!is_array($phrases)) {
$phrases = str_replace(',', "\n", $phrases);
$phrases = preg_replace("/\n+/", "\n", str_replace("\r", '', trim($phrases)));
$phrases = $phrases ? array_map('trim', explode("\n", $phrases)) : Array ();
}
if (!is_array($email_events)) {
$email_events = str_replace(',', "\n", $email_events);
$email_events = preg_replace("/\n+/", "\n", str_replace("\r", '', trim($email_events)));
$email_events = $email_events ? array_map('trim', explode("\n", $email_events)) : Array ();
}
$this->_exportLimits = Array ('phrases' => $phrases, 'emailevents' => $email_events);
}
/**
* Performs upgrade of given language pack part
*
* @param int $language_id
* @param string $prefix
* @param string $unique_field
- * @param string $data_fields
+ * @param Array $data_fields
*/
function _performUpgrade($language_id, $prefix, $unique_field, $data_fields)
{
$live_records = $this->_getTableData($language_id, $prefix, $unique_field, $data_fields[0], false);
$temp_records = $this->_getTableData($language_id, $prefix, $unique_field, $data_fields[0], true);
if (!$temp_records) {
// no data for given language
return ;
}
// perform insert for records, that are missing in live table
$to_insert = array_diff($temp_records, $live_records);
if ($to_insert) {
$to_insert = array_map(Array (&$this->Conn, 'qstr'), $to_insert);
$sql = 'INSERT INTO ' . $this->Application->getUnitOption($prefix, 'TableName') . '
SELECT *
FROM ' . $this->_tables[$prefix] . '
WHERE ' . $unique_field . ' IN (' . implode(',', $to_insert) . ')';
$this->Conn->Query($sql);
// new records were added
$this->changedPrefixes[] = $prefix;
}
// perform update for records, that are present in live table
$to_update = array_diff($temp_records, $to_insert);
if ($to_update) {
$to_update = array_map(Array (&$this->Conn, 'qstr'), $to_update);
$sql = 'UPDATE ' . $this->Application->getUnitOption($prefix, 'TableName') . ' live
SET ';
foreach ($data_fields as $index => $data_field) {
$data_field = sprintf($data_field, $language_id);
$sql .= ' live.' . $data_field . ' = (
SELECT temp' . $index . '.' . $data_field . '
FROM ' . $this->_tables[$prefix] . ' temp' . $index . '
WHERE temp' . $index . '.' . $unique_field . ' = live.' . $unique_field . '
),';
}
$sql = substr($sql, 0, -1); // cut last comma
$where_clause = Array (
// this won't make any difference, but just in case
$unique_field . ' IN (' . implode(',', $to_update) . ')',
);
if ($this->import_mode == LANG_SKIP_EXISTING) {
// empty OR not set
$data_field = sprintf($data_fields[0], $language_id);
$where_clause[] = '(' . $data_field . ' = "") OR (' . $data_field . ' IS NULL)';
}
if ($where_clause) {
$sql .= "\n" . 'WHERE (' . implode(') AND (', $where_clause) . ')';
}
$this->Conn->Query($sql);
if ($this->Conn->getAffectedRows() > 0) {
// existing records were updated
$this->changedPrefixes[] = $prefix;
}
}
}
/**
* Returns data from given table used for language pack upgrade
*
* @param int $language_id
* @param string $prefix
* @param string $unique_field
+ * @param string $data_field
* @param bool $temp_mode
* @return Array
*/
function _getTableData($language_id, $prefix, $unique_field, $data_field, $temp_mode = false)
{
$data_field = sprintf($data_field, $language_id);
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
if ($temp_mode) {
// for temp table get only records, that have contents on given language (not empty and isset)
$sql = 'SELECT ' . $unique_field . '
FROM ' . $this->Application->GetTempName($table_name, 'prefix:' . $prefix) . '
WHERE (' . $data_field . ' <> "") AND (' . $data_field . ' IS NOT NULL)';
}
else {
// for live table get all records, no matter on what language
$sql = 'SELECT ' . $unique_field . '
FROM ' . $table_name;
}
return $this->Conn->GetCol($sql);
}
function _parseXML($filename)
{
if ($this->_debugMode) {
$start_time = microtime(true);
$this->Application->Debugger->appendHTML(__CLASS__ . '::' . __FUNCTION__ . '("' . $filename . '")');
}
$fdata = file_get_contents($filename);
$xml_parser =& $this->Application->recallObject('kXMLHelper');
/* @var $xml_parser kXMLHelper */
$root_node =& $xml_parser->Parse($fdata);
if (!is_object($root_node) || !is_a($root_node, 'kXMLNode')) {
// invalid language pack contents
return false;
}
if ($root_node->Children) {
$this->_processLanguages($root_node->firstChild);
}
if ($this->_debugMode) {
$this->Application->Debugger->appendHTML(__CLASS__ . '::' . __FUNCTION__ . '("' . $filename . '"): ' . (microtime(true) - $start_time));
}
return true;
}
/**
* Creates temporary tables, used during language import
*
* @param bool $drop_only
*/
function _initImportTables($drop_only = false)
{
$this->_tables['phrases'] = $this->_prepareTempTable('phrases', $drop_only);
$this->_tables['emailevents'] = $this->_prepareTempTable('emailevents', $drop_only);
$this->_tables['country-state'] = $this->_prepareTempTable('country-state', $drop_only);
}
/**
* Create temp table for prefix, if table already exists, then delete it and create again
*
* @param string $prefix
+ * @param bool $drop_only
+ * @return string Name of created temp table
+ * @access protected
*/
- function _prepareTempTable($prefix, $drop_only = false)
+ protected function _prepareTempTable($prefix, $drop_only = false)
{
- $idfield = $this->Application->getUnitOption($prefix, 'IDField');
+ $id_field = $this->Application->getUnitOption($prefix, 'IDField');
$table = $this->Application->getUnitOption($prefix,'TableName');
$temp_table = $this->Application->GetTempName($table);
$sql = 'DROP TABLE IF EXISTS %s';
$this->Conn->Query( sprintf($sql, $temp_table) );
if (!$drop_only) {
$sql = 'CREATE TABLE ' . $temp_table . ' SELECT * FROM ' . $table . ' WHERE 0';
$this->Conn->Query($sql);
$sql = 'ALTER TABLE %1$s CHANGE %2$s %2$s INT(11) NOT NULL DEFAULT "0"';
- $this->Conn->Query( sprintf($sql, $temp_table, $idfield) );
+ $this->Conn->Query( sprintf($sql, $temp_table, $id_field) );
switch ($prefix) {
case 'phrases':
$unique_field = 'PhraseKey';
break;
case 'emailevents':
$unique_field = 'EventId';
break;
case 'country-state':
$unique_field = 'CountryStateId';
break;
default:
throw new Exception('Unknown prefix "<strong>' . $prefix . '</strong>" during language pack import');
break;
}
$sql = 'ALTER TABLE ' . $temp_table . ' ADD UNIQUE (' . $unique_field . ')';
$this->Conn->Query($sql);
}
return $temp_table;
}
/**
* Prepares mapping between event name+type and their ids in database
*
*/
function _updateEventsCache()
{
$sql = 'SELECT EventId, CONCAT(Event,"_",Type) AS EventMix
FROM ' . TABLE_PREFIX . 'Events';
$this->events_hash = $this->Conn->GetCol($sql, 'EventMix');
}
/**
* Returns language fields to be exported
*
* @return Array
*/
function _getExportFields()
{
return Array (
'PackName', 'LocalName', 'DateFormat', 'TimeFormat', 'InputDateFormat', 'InputTimeFormat',
'DecimalPoint', 'ThousandSep', 'Charset', 'UnitSystem', 'Locale', 'UserDocsUrl'
);
}
/**
* Processes parsed XML
*
* @param kXMLNode $language_node
*/
function _processLanguages(&$language_node)
{
if (array_key_exists('VERSION', $language_node->Parent->Attributes)) {
// version present -> use it
$version = $language_node->Parent->Attributes['VERSION'];
}
else {
// version missing -> guess it
if (is_object($language_node->FindChild('DATEFORMAT'))) {
$version = 1;
}
elseif (array_key_exists('CHARSET', $language_node->Attributes)) {
$version = 2;
}
}
if ($version == 1) {
$field_mapping = Array (
'DATEFORMAT' => 'DateFormat',
'TIMEFORMAT' => 'TimeFormat',
'INPUTDATEFORMAT' => 'InputDateFormat',
'INPUTTIMEFORMAT' => 'InputTimeFormat',
'DECIMAL' => 'DecimalPoint',
'THOUSANDS' => 'ThousandSep',
'CHARSET' => 'Charset',
'UNITSYSTEM' => 'UnitSystem',
'DOCS_URL' => 'UserDocsUrl',
);
}
else {
$export_fields = $this->_getExportFields();
}
do {
$language_id = false;
$fields_hash = Array (
'PackName' => $language_node->Attributes['PACKNAME'],
'LocalName' => $language_node->Attributes['PACKNAME'],
'Encoding' => $language_node->Attributes['ENCODING'],
'Charset' => 'utf-8',
);
if ($version > 1) {
foreach ($export_fields as $export_field) {
$attribute_name = strtoupper($export_field);
if (array_key_exists($attribute_name, $language_node->Attributes)) {
$fields_hash[$export_field] = $language_node->Attributes[$attribute_name];
}
}
}
$sub_node =& $language_node->firstChild;
/* @var $sub_node kXMLNode */
do {
switch ($sub_node->Name) {
case 'PHRASES':
if ($sub_node->Children) {
if (!$language_id) {
$language_id = $this->_processLanguage($fields_hash);
}
if ($this->_debugMode) {
$start_time = microtime(true);
}
$this->_processPhrases($sub_node->firstChild, $language_id, $fields_hash['Encoding']);
if ($this->_debugMode) {
$this->Application->Debugger->appendHTML(__CLASS__ . '::' . '_processPhrases: ' . (microtime(true) - $start_time));
}
}
break;
case 'EVENTS':
if ($sub_node->Children) {
if (!$language_id) {
$language_id = $this->_processLanguage($fields_hash);
}
$this->_processEvents($sub_node->firstChild, $language_id, $fields_hash['Encoding']);
}
break;
case 'COUNTRIES':
if ($sub_node->Children) {
if (!$language_id) {
$language_id = $this->_processLanguage($fields_hash);
}
$this->_processCountries($sub_node->firstChild, $language_id, $fields_hash['Encoding']);
}
break;
case 'REPLACEMENTS':
// added since v2
$replacements = $sub_node->Data;
if ($fields_hash['Encoding'] != 'plain') {
$replacements = base64_decode($replacements);
}
$fields_hash['FilenameReplacements'] = $replacements;
break;
default:
if ($version == 1) {
$fields_hash[ $field_mapping[$sub_node->Name] ] = $sub_node->Data;
}
break;
}
} while (($sub_node =& $sub_node->NextSibling()));
} while (($language_node =& $language_node->NextSibling()));
}
/**
* Performs phases import
*
* @param kXMLNode $phrase_node
* @param int $language_id
* @param string $language_encoding
*/
function _processPhrases(&$phrase_node, $language_id, $language_encoding)
{
static $other_translations = Array ();
if ($this->Application->isDebugMode()) {
$this->Application->Debugger->profileStart('L[' . $language_id . ']P', 'Language: ' . $language_id . '; Phrases Import');
}
do {
$phrase_key = mb_strtoupper($phrase_node->Attributes['LABEL']);
$fields_hash = Array (
'Phrase' => $phrase_node->Attributes['LABEL'],
'PhraseKey' => $phrase_key,
'PhraseType' => $phrase_node->Attributes['TYPE'],
'Module' => array_key_exists('MODULE', $phrase_node->Attributes) ? $phrase_node->Attributes['MODULE'] : 'Core',
'LastChanged' => adodb_mktime(),
'LastChangeIP' => $this->ip_address,
);
$translation = $phrase_node->Data;
$hint_translation = isset($phrase_node->Attributes['HINT']) ? $phrase_node->Attributes['HINT'] : '';
$column_translation = isset($phrase_node->Attributes['COLUMN']) ? $phrase_node->Attributes['COLUMN'] : '';
if (array_key_exists($fields_hash['PhraseType'], $this->phrase_types_allowed)) {
if ($language_encoding != 'plain') {
$translation = base64_decode($translation);
$hint_translation = base64_decode($hint_translation);
$column_translation = base64_decode($column_translation);
}
if (array_key_exists($phrase_key, $other_translations)) {
$other_translations[$phrase_key]['l' . $language_id . '_Translation'] = $translation;
$other_translations[$phrase_key]['l' . $language_id . '_HintTranslation'] = $hint_translation;
$other_translations[$phrase_key]['l' . $language_id . '_ColumnTranslation'] = $column_translation;
}
else {
$other_translations[$phrase_key] = Array (
'l' . $language_id . '_Translation' => $translation,
'l' . $language_id . '_HintTranslation' => $hint_translation,
'l' . $language_id . '_ColumnTranslation' => $column_translation,
);
}
$fields_hash = array_merge($fields_hash, $other_translations[$phrase_key]);
$this->Conn->doInsert($fields_hash, $this->_tables['phrases'], 'REPLACE', false);
}
} while (($phrase_node =& $phrase_node->NextSibling()));
if ($this->Application->isDebugMode()) {
$this->Application->Debugger->profileFinish('L[' . $language_id . ']P', 'Language: ' . $language_id . '; Phrases Import');
}
$this->Conn->doInsert($fields_hash, $this->_tables['phrases'], 'REPLACE');
}
/**
* Performs email event import
*
* @param kXMLNode $event_node
* @param int $language_id
* @param string $language_encoding
*/
function _processEvents(&$event_node, $language_id, $language_encoding)
{
static $other_translations = Array ();
if ($this->Application->isDebugMode()) {
$this->Application->Debugger->profileStart('L[' . $language_id . ']E', 'Language: ' . $language_id . '; Events Import');
}
$email_message_helper =& $this->Application->recallObject('EmailMessageHelper');
/* @var $email_message_helper EmailMessageHelper */
do {
$event_id = $this->_getEventId($event_node->Attributes['EVENT'], $event_node->Attributes['TYPE']);
if ($event_id) {
if ($language_encoding == 'plain') {
$template = rtrim($event_node->Data);
}
else {
$template = base64_decode($event_node->Data);
}
$parsed = $email_message_helper->parseTemplate($template);
$fields_hash = Array (
'EventId' => $event_id,
'Event' => $event_node->Attributes['EVENT'],
'Type' => $event_node->Attributes['TYPE'],
'MessageType' => $event_node->Attributes['MESSAGETYPE'],
);
if (array_key_exists($event_id, $other_translations)) {
$other_translations[$event_id]['l' . $language_id . '_Subject'] = $parsed['Subject'];
$other_translations[$event_id]['l' . $language_id . '_Body'] = $parsed['Body'];
}
else {
$other_translations[$event_id] = Array (
'l' . $language_id . '_Subject' => $parsed['Subject'],
'l' . $language_id . '_Body' => $parsed['Body'],
);
}
if ($parsed['Headers']) {
$other_translations[$event_id]['Headers'] = $parsed['Headers'];
}
elseif (!$parsed['Headers'] && !array_key_exists('Headers', $other_translations[$event_id])) {
$other_translations[$event_id]['Headers'] = $parsed['Headers'];
}
$fields_hash = array_merge($fields_hash, $other_translations[$event_id]);
$this->Conn->doInsert($fields_hash, $this->_tables['emailevents'], 'REPLACE', false);
}
} while (($event_node =& $event_node->NextSibling()));
if ($this->Application->isDebugMode()) {
$this->Application->Debugger->profileFinish('L[' . $language_id . ']E', 'Language: ' . $language_id . '; Events Import');
}
if (isset($fields_hash)) {
// at least one email event in language pack was found in database
$this->Conn->doInsert($fields_hash, $this->_tables['emailevents'], 'REPLACE');
}
}
/**
* Performs country_state translation import
*
* @param kXMLNode $country_state_node
* @param int $language_id
* @param string $language_encoding
*/
function _processCountries(&$country_state_node, $language_id, $language_encoding, $process_states = false)
{
static $other_translations = Array ();
do {
if ($process_states) {
$country_state_id = $this->_getStateId($country_state_node->Parent->Attributes['ISO'], $country_state_node->Attributes['ISO']);
}
else {
$country_state_id = $this->_getCountryId($country_state_node->Attributes['ISO']);
}
if ($country_state_id) {
if ($language_encoding == 'plain') {
$translation = rtrim($country_state_node->Attributes['TRANSLATION']);
}
else {
$translation = base64_decode($country_state_node->Attributes['TRANSLATION']);
}
$fields_hash = Array (
'CountryStateId' => $country_state_id,
);
if (array_key_exists($country_state_id, $other_translations)) {
$other_translations[$country_state_id]['l' . $language_id . '_Name'] = $translation;
}
else {
$other_translations[$country_state_id] = Array (
'l' . $language_id . '_Name' => $translation,
);
}
$fields_hash = array_merge($fields_hash, $other_translations[$country_state_id]);
$this->Conn->doInsert($fields_hash, $this->_tables['country-state'], 'REPLACE', false);
if (!$process_states && $country_state_node->Children) {
$this->_processCountries($country_state_node->firstChild, $language_id, $language_encoding, true);
}
}
} while (($country_state_node =& $country_state_node->NextSibling()));
$this->Conn->doInsert($fields_hash, $this->_tables['country-state'], 'REPLACE');
}
/**
* Creates/updates language based on given fields and returns it's id
*
* @param Array $fields_hash
* @return int
*/
function _processLanguage($fields_hash)
{
// 1. get language from database
$sql = 'SELECT ' . $this->lang_object->IDField . '
FROM ' . $this->lang_object->TableName . '
WHERE PackName = ' . $this->Conn->qstr($fields_hash['PackName']);
$language_id = $this->Conn->GetOne($sql);
if ($language_id) {
// 2. language found -> update, when allowed
$this->lang_object->Load($language_id);
if ($this->import_mode == LANG_OVERWRITE_EXISTING) {
// update live language record based on data from xml
$this->lang_object->SetFieldsFromHash($fields_hash);
$this->lang_object->Update();
}
}
else {
// 3. language not found -> create
$this->lang_object->SetFieldsFromHash($fields_hash);
$this->lang_object->SetDBField('Enabled', STATUS_ACTIVE);
if ($this->lang_object->Create()) {
$language_id = $this->lang_object->GetID();
if (defined('IS_INSTALL') && IS_INSTALL) {
// language created during install becomes admin interface language
$this->lang_object->setPrimary(true, true);
}
}
}
// 4. collect ID of every processed language
if (!in_array($language_id, $this->_languages)) {
$this->_languages[] = $language_id;
}
return $language_id;
}
/**
* Returns event id based on it's name and type
*
* @param string $event_name
* @param string $event_type
* @return int
*/
function _getEventId($event_name, $event_type)
{
$cache_key = $event_name . '_' . $event_type;
return array_key_exists($cache_key, $this->events_hash) ? $this->events_hash[$cache_key] : 0;
}
/**
* Returns country id based on it's 3letter ISO code
*
* @param string $iso
* @return int
*/
function _getCountryId($iso)
{
static $cache = null;
if (!isset($cache)) {
$sql = 'SELECT CountryStateId, IsoCode
FROM ' . TABLE_PREFIX . 'CountryStates
WHERE Type = ' . DESTINATION_TYPE_COUNTRY;
$cache = $this->Conn->GetCol($sql, 'IsoCode');
}
return array_key_exists($iso, $cache) ? $cache[$iso] : false;
}
/**
* Returns state id based on 3letter country ISO code and 2letter state ISO code
*
* @param string $country_iso
* @param string $state_iso
* @return int
*/
function _getStateId($country_iso, $state_iso)
{
static $cache = null;
if (!isset($cache)) {
$sql = 'SELECT CountryStateId, CONCAT(StateCountryId, "-", IsoCode) AS IsoCode
FROM ' . TABLE_PREFIX . 'CountryStates
WHERE Type = ' . DESTINATION_TYPE_STATE;
$cache = $this->Conn->GetCol($sql, 'IsoCode');
}
$country_id = $this->_getCountryId($country_iso);
return array_key_exists($country_id . '-' . $state_iso, $cache) ? $cache[$country_id . '-' . $state_iso] : false;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/category_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/category_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/category_helper.php (revision 14628)
@@ -1,613 +1,620 @@
<?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 CategoryHelper extends kHelper {
/**
* Structure tree for ParentId field in category or category items
*
* @var Array
*/
var $_structureTree = null;
/**
* ID of primary language (only for caching)
*
* @var int
*/
var $_primaryLanguageId = false;
/**
* Prints category path using given blocks. Also supports used defined path elements at the end.
*
* @param Array $params
* @return string
*/
function NavigationBar($params)
{
$params['is_first'] = 1;
$main_category_id = isset($params['cat_id']) ? $params['cat_id'] : $this->Application->GetVar('m_cat_id');
if (array_key_exists('shift', $params) && $params['shift']) {
$home_element = '';
$params['shift']--;
}
else {
$home_element = $this->getHomeCategoryPath($params, $main_category_id);
unset($params['is_first']);
}
if (!getArrayValue($params, 'titles') && !getArrayValue($params, 'templates')) {
// no static templates given, show only category path
return $home_element . $this->getCategoryPath($main_category_id, $params);
}
$navigation_parts = $this->getNavigationParts($params['titles'], $params['templates']);
$ret = '';
$block_params = Array (); //$params; // sort of TagProcessor:prepareTagParams
$block_params['no_editing'] = 1;
$block_params['category'] = 0;
$block_params['separator'] = $params['separator'];
$show_category = getArrayValue($params, 'show_category');
$current_template = $this->Application->GetVar('t');
$physical_template = $this->Application->getPhysicalTemplate($current_template);
if ($physical_template !== false) {
// replace menu template name with it's actual template name on disk
list ($current_template) = explode(':', $physical_template, 2);
}
foreach ($navigation_parts as $template => $title) {
$block_params['template'] = $template;
if ($title == '__item__') {
if ($show_category) {
$ret .= $this->getCategoryPath($main_category_id, $params);
$show_category = false;
}
$category_path = $this->getCategoryParentPath($main_category_id);
$module_info = $this->getCategoryModule($params, array_keys($category_path));
if (!$module_info) {
continue;
}
$module_prefix = $module_info['Var'];
$object =& $this->Application->recallObject($module_prefix);
/* @var $object kCatDBItem */
$title_field = $this->Application->getUnitOption($module_prefix, 'TitleField');
$block_params['title'] = $object->GetField($title_field);
$block_params['prefix'] = $module_prefix;
$block_params['current'] = 0;
$block_params['name'] = $this->SelectParam($params, 'module_item_render_as,render_as');
}
else {
$block_params['current'] = ($template == $current_template);
$block_params['title'] = $this->Application->Phrase($title);
$block_params['name'] = $template == $current_template ? $params['current_render_as'] : $params['render_as'];
}
$ret .= $this->Application->ParseBlock($block_params);
}
if ($show_category) {
$params['no_current'] = true;
return $home_element . ($show_category ? $this->getCategoryPath($main_category_id, $params) : '') . $ret;
}
return $home_element . $ret;
}
/**
* Get navigation parts
*
* @param Array $titles
* @param Array $templates
* @return Array
*/
function getNavigationParts($titles, $templates)
{
$titles = explode(',', $titles);
$templates = explode(',', $templates);
$ret = Array ();
foreach ($templates as $template_pos => $template) {
$ret[$template] = $titles[$template_pos];
}
return $ret;
}
/**
* Renders path to given category using given blocks.
*
* @param int $main_category_id
* @param Array $params
* @return string
*/
function getCategoryPath($main_category_id, $params)
{
$category_path = $this->getCategoryParentPath($main_category_id);
if (!$category_path) {
// in "Home" category
return '';
}
if (array_key_exists('shift', $params) && $params['shift']) {
array_splice($category_path, 0, $params['shift']);
}
$module_info = $this->getCategoryModule($params, array_keys($category_path));
$module_category_id = $module_info['RootCat'];
$module_item_id = $this->Application->GetVar($module_info['Var'].'_id');
$ret = '';
$block_params['category'] = 1;
$block_params['no_editing'] = 1;
if (array_key_exists('is_first', $params)) {
$block_params['is_first'] = $params['is_first'];
}
$block_params['separator'] = $params['separator'];
$no_current = isset($params['no_current']) && $params['no_current'];
$backup_category_id = $this->Application->GetVar('c_id');
foreach ($category_path as $category_id => $category_name) {
$block_params['cat_id'] = $category_id;
$block_params['cat_name'] = $block_params['title'] = $category_name;
if ($no_current) {
$block_params['current'] = 0;
}
else {
$block_params['current'] = ($main_category_id == $category_id) && !$module_item_id ? 1 : 0;
}
$block_params['is_module_root'] = $category_id == $module_category_id ? 1 : 0;
$block_params['name'] = $this->SelectParam($params, 'render_as,block');
// which block to parse as current ?
if ($block_params['is_module_root']) {
$block_params['name'] = $this->SelectParam($params, 'module_root_render_as,render_as');
$block_params['module_index'] = $module_info['TemplatePath'].'index';
}
if ($block_params['current']) {
$block_params['name'] = $this->SelectParam($params, 'current_render_as,render_as');
}
$this->Application->SetVar('c_id', $category_id);
$ret .= $this->Application->ParseBlock($block_params);
if (array_key_exists('is_first', $block_params)) {
unset($block_params['is_first']);
}
}
$this->Application->SetVar('c_id', $backup_category_id);
return $ret;
}
/**
* Returns module information based on given module name or current category (relative to module root categories)
*
* @param Array $params
* @param Array $category_ids category parent path (already as array)
* @return Array
*/
function getCategoryModule($params, $category_ids)
{
if (isset($params['module'])) {
// get module by name specified
$module_info = $this->Application->findModule('Name', $params['module']);
}
elseif ($category_ids) {
// get module by category path
$module_root_categories = $this->getModuleRootCategories();
$common_categories = array_intersect($category_ids, $module_root_categories);
$module_category_id = array_shift($common_categories); // get 1st common category
$module_info = $this->Application->findModule('RootCat', $module_category_id);
}
return $module_info;
}
/**
* Renders path to top catalog category
*
* @param Array $params
* @param int $current_category
* @return string
*/
function getHomeCategoryPath($params, $current_category)
{
$block_params['cat_id'] = $this->Application->getBaseCategory();
$block_params['no_editing'] = 1;
$block_params['current'] = $current_category == $block_params['cat_id'] ? 1 : 0;
$block_params['separator'] = $params['separator'];
$block_params['is_first'] = $params['is_first'];
$block_params['cat_name'] = $this->Application->Phrase(($this->Application->isAdmin ? 'la_' : 'lu_') . 'rootcategory_name');
$block_params['name'] = $this->SelectParam($params, 'root_cat_render_as,render_as');
return $this->Application->ParseBlock($block_params);
}
/**
* Returns root categories from all modules
*
* @return Array
*/
function getModuleRootCategories()
{
static $root_categories = null;
if (!isset($root_categories)) {
$root_categories = Array ();
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
array_push($root_categories, $module_info['RootCat']);
}
$root_categories = array_unique($root_categories);
}
return $root_categories;
}
/**
* Returns given category's parent path as array of id=>name elements
*
* @param int $main_category_id
* @return Array
*/
function getCategoryParentPath($main_category_id)
{
- if ($main_category_id == 0) {
+ if ( $main_category_id == 0 ) {
// don't query path for "Home" category
return Array ();
}
$cache_key = 'parent_paths_named[%CIDSerial:' . $main_category_id . '%]';
$cached_path = $this->Application->getCache($cache_key);
- if ($cached_path === false) {
+ if ( $cached_path === false ) {
$ml_formatter =& $this->Application->recallObject('kMultiLanguage');
+ /* @var $ml_formatter kMultiLanguage */
+
$navbar_field = $ml_formatter->LangFieldName('CachedNavBar');
$id_field = $this->Application->getUnitOption('c', 'IDField');
$table_name = $this->Application->getUnitOption('c', 'TableName');
$this->Conn->nextQueryCachable = true;
- $sql = 'SELECT '.$navbar_field.', ParentPath
- FROM '.$table_name.'
- WHERE '.$id_field.' = '.$main_category_id;
+ $sql = 'SELECT ' . $navbar_field . ', ParentPath
+ FROM ' . $table_name . '
+ WHERE ' . $id_field . ' = ' . $main_category_id;
$category_data = $this->Conn->GetRow($sql);
$cached_path = Array ();
$skip_category = $this->Application->getBaseCategory();
- if ($category_data) {
+ if ( $category_data ) {
$category_names = explode('&|&', $category_data[$navbar_field]);
$category_ids = explode('|', substr($category_data['ParentPath'], 1, -1));
foreach ($category_ids as $category_index => $category_id) {
- if ($category_id == $skip_category) {
+ if ( $category_id == $skip_category ) {
continue;
}
$cached_path[$category_id] = $category_names[$category_index];
}
}
$this->Application->setCache($cache_key, $cached_path);
}
return $cached_path;
}
- /**
- * Not tag, method for parameter
- * selection from list in this TagProcessor
+ /**
+ * Not tag. Method for parameter selection from list in this TagProcessor
*
* @param Array $params
- * @param string $possible_names
+ * @param Array $possible_names
+ *
* @return string
- * @access public
+ * @access protected
*/
- function SelectParam($params, $possible_names)
+ protected function SelectParam($params, $possible_names)
{
- if (!is_array($params)) return;
- if (!is_array($possible_names))
+ if ( !is_array($params) ) {
+ return '';
+ }
+ if ( !is_array($possible_names) ) {
+ $possible_names = explode(',', $possible_names);
+ }
- $possible_names = explode(',', $possible_names);
- foreach ($possible_names as $name)
- {
- if( isset($params[$name]) ) return $params[$name];
+ foreach ($possible_names as $name) {
+ if ( isset($params[$name]) ) {
+ return $params[$name];
+ }
}
- return false;
+
+ return '';
}
/**
* Converts multi-dimensional category structure in one-dimensional option array (category_id=>category_name)
*
* @param Array $data
* @param int $parent_category_id
* @param int_type $language_id
* @param int $theme_id
* @param int $level
* @return Array
*/
function _printChildren(&$data, $parent_category_id, $language_id, $theme_id, $level = 0)
{
if ($data['ThemeId'] != $theme_id && $data['ThemeId'] != 0) {
// don't show system templates from different themes
return Array ();
}
$category_language = $data['l' . $language_id . '_Name'] ? $language_id : $this->_primaryLanguageId;
$ret = Array($parent_category_id => str_repeat('-', $level).' '.$data['l' . $category_language . '_Name']);
if ($data['children']) {
$level++;
foreach ($data['children'] as $category_id => $category_data) {
// numeric keys
$ret = kUtil::array_merge_recursive($ret, $this->_printChildren($data['children'][$category_id], $category_id, $language_id, $theme_id, $level));
}
}
return $ret;
}
/**
* Returns information about children under parent path (recursive)
*
* @param int $parent_category_id
* @param Array $languages
* @return Array
*/
function _getChildren($parent_category_id, $languages)
{
static $items_by_parent = null, $parent_mapping = null;
if ( !isset($items_by_parent) ) {
$fields = $items_by_parent = Array ();
foreach ($languages as $language_id) {
$fields[] = 'l' . $language_id . '_Name';
}
$sql = 'SELECT CategoryId AS id, ' . implode(', ', $fields) . ', ParentId, ThemeId
FROM ' . $this->Application->getUnitOption('c', 'TableName') . '
ORDER BY Priority DESC';
$items = $this->Conn->Query($sql, 'id');
foreach ($items as $item_id => $item_data) {
$item_parent_id = $item_data['ParentId'];
unset($item_data['ParentId']);
if ( !array_key_exists($item_parent_id, $items_by_parent) ) {
$items_by_parent[$item_parent_id] = Array ();
}
$item_data['children'] = false;
$parent_mapping[$item_id] = $item_parent_id;
$items_by_parent[$item_parent_id][$item_id] = $item_data;
}
}
$data = $items_by_parent[ $parent_mapping[$parent_category_id] ][$parent_category_id];
$categories = array_key_exists($parent_category_id, $items_by_parent) ? $items_by_parent[$parent_category_id] : Array ();
foreach ($categories as $category_id => $category_data) {
if ($category_id == $parent_category_id) {
// don't process myself - prevents recursion
continue;
}
$data['children'][$category_id] = $this->_getChildren($category_id, $languages);
}
return $data;
}
/**
* Generates OR retrieves from cache structure tree
*
* @return Array
*/
function &_getStructureTree()
{
// get cached version of structure tree
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$data = $this->Application->getCache('master:StructureTree', false, CacheSettings::$structureTreeRebuildTime);
}
else {
$data = $this->Application->getDBCache('StructureTree', CacheSettings::$structureTreeRebuildTime);
}
if ($data) {
$data = unserialize($data);
return $data;
}
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->rebuildCache('master:StructureTree', kCache::REBUILD_NOW, CacheSettings::$structureTreeRebuildTime);
}
else {
$this->Application->rebuildDBCache('StructureTree', kCache::REBUILD_NOW, CacheSettings::$structureTreeRebuildTime);
}
// generate structure tree from scratch
$ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$languages = $ml_helper->getLanguages();
$root_category = $this->Application->getBaseCategory();
$data = $this->_getChildren($root_category, $languages);
if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) {
$this->Application->setCache('master:StructureTree', serialize($data));
}
else {
$this->Application->setDBCache('StructureTree', serialize($data));
}
return $data;
}
function getTemplateMapping()
{
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$data = $this->Application->getCache('master:template_mapping', false, CacheSettings::$templateMappingRebuildTime);
}
else {
$data = $this->Application->getDBCache('template_mapping', CacheSettings::$templateMappingRebuildTime);
}
if ( $data ) {
return unserialize($data);
}
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->rebuildCache('master:template_mapping', kCache::REBUILD_NOW, CacheSettings::$templateMappingRebuildTime);
}
else {
$this->Application->rebuildDBCache('template_mapping', kCache::REBUILD_NOW, CacheSettings::$templateMappingRebuildTime);
}
$sql = 'SELECT
IF(c.`Type` = ' . PAGE_TYPE_TEMPLATE . ', CONCAT(c.Template, ":", c.ThemeId), CONCAT("id:", c.CategoryId)) AS SrcTemplate,
LOWER(
IF(
c.SymLinkCategoryId IS NOT NULL,
(SELECT cc.NamedParentPath FROM ' . TABLE_PREFIX . 'Category AS cc WHERE cc.CategoryId = c.SymLinkCategoryId),
c.NamedParentPath
)
) AS DstTemplate,
c.UseExternalUrl, c.ExternalUrl
FROM ' . TABLE_PREFIX . 'Category AS c
WHERE c.Status = ' . STATUS_ACTIVE;
$pages = $this->Conn->Query($sql, 'SrcTemplate');
$mapping = Array ();
$base_url = $this->Application->BaseURL();
foreach ($pages as $src_template => $page) {
// process external url, before placing in cache
if ($page['UseExternalUrl']) {
$external_url = $page['ExternalUrl'];
if (!preg_match('/^(.*?):\/\/(.*)$/', $external_url)) {
// url without protocol will be relative url to our site
$external_url = $base_url . $external_url;
}
$dst_template = 'external:' . $external_url;
}
else {
$dst_template = preg_replace('/^Content\//i', '', $page['DstTemplate']);
}
$mapping[$src_template] = $dst_template;
}
if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) {
$data = $this->Application->setCache('master:template_mapping', serialize($mapping));
}
else {
$this->Application->setDBCache('template_mapping', serialize($mapping));
}
return $mapping;
}
/**
* Returns category structure as field option list
*
* @return Array
*/
function getStructureTreeAsOptions()
{
if ((defined('IS_INSTALL') && IS_INSTALL) || !$this->Application->isAdmin) {
// no need to create category structure during install
// OR on Front-End, because it's not used there
return Array ();
}
if (isset($this->_structureTree)) {
return $this->_structureTree;
}
$themes_helper =& $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$data = $this->_getStructureTree();
$theme_id = (int)$themes_helper->getCurrentThemeId();
$root_category = $this->Application->getBaseCategory();
$this->_primaryLanguageId = $this->Application->GetDefaultLanguageId();
$this->_structureTree = $this->_printChildren($data, $root_category, $this->Application->GetVar('m_lang'), $theme_id);
return $this->_structureTree;
}
/**
* Replace links like "@@ID@@" to actual template names in given text
*
* @param string $text
* @return string
*/
function replacePageIds($text)
{
if (!preg_match_all('/@@(\\d+)@@/', $text, $regs)) {
return $text;
}
$page_ids = $regs[1];
$sql = 'SELECT NamedParentPath, CategoryId
FROM ' . TABLE_PREFIX . 'Category
WHERE CategoryId IN (' . implode(',', $page_ids) . ')';
$templates = $this->Conn->GetCol($sql, 'CategoryId');
foreach ($page_ids as $page_id) {
if (!array_key_exists($page_id, $templates)) {
// internal page was deleted, but link to it was found in given content block data
continue;
}
$url_params = Array ('m_cat_id' => $page_id, 'pass' => 'm');
$page_url = $this->Application->HREF(strtolower($templates[$page_id]), '', $url_params);
/*if ($this->Application->isAdmin) {
$page_url = preg_replace('/&(admin|editing_mode)=[\d]/', '', $page_url);
}*/
$text = str_replace('@@' . $page_id . '@@', $page_url, $text);
}
return $text;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/user_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/user_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/user_helper.php (revision 14628)
@@ -1,487 +1,500 @@
<?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 UserHelper extends kHelper {
/**
* Event to be used during login processings
*
* @var kEvent
*/
var $event = null;
/**
* Performs user login and returns the result
*
* @param string $username
* @param string $password
* @param bool $dry_run
* @param bool $remember_login
* @param string $remember_login_cookie
* @return int
*/
function loginUser($username, $password, $dry_run = false, $remember_login = false, $remember_login_cookie = '')
{
if (!isset($this->event)) {
$this->event = new kEvent('u:OnLogin');
}
if (!$password && !$remember_login_cookie) {
return LoginResult::INVALID_PASSWORD;
}
$object =& $this->getUserObject();
// process "Save Username" checkbox
if ($this->Application->isAdmin) {
$save_username = $this->Application->GetVar('cb_save_username') ? $username : '';
$this->Application->Session->SetCookie('save_username', $save_username, strtotime('+1 year'));
// cookie will be set on next refresh, but refresh won't occur if
// login error present, so duplicate cookie in kHTTPQuery
$this->Application->SetVar('save_username', $save_username);
}
// logging in "root" (admin only)
$super_admin = ($username == 'super-root') && $this->verifySuperAdmin();
if ($this->Application->isAdmin && ($username == 'root') || ($super_admin && $username == 'super-root')) {
$root_password = $this->Application->ConfigValue('RootPass');
+
$password_formatter =& $this->Application->recallObject('kPasswordFormatter');
+ /* @var $password_formatter kPasswordFormatter */
if ($root_password != $password_formatter->EncryptPassword($password, 'b38')) {
return LoginResult::INVALID_PASSWORD;
}
$user_id = USER_ROOT;
$object->Clear($user_id);
$object->SetDBField('Login', 'root');
if (!$dry_run) {
$this->loginUserById($user_id, $remember_login_cookie);
if ($super_admin) {
$this->Application->StoreVar('super_admin', 1);
}
// reset counters
$this->Application->resetCounters('UserSession');
$this->_processLoginRedirect('root', $password);
$this->_processInterfaceLanguage();
}
return LoginResult::OK;
}
$user_id = $this->getUserId($username, $password, $remember_login_cookie);
if ($user_id) {
$object->Load($user_id);
if (!$this->checkBanRules($object)) {
return LoginResult::BANNED;
}
if ($object->GetDBField('Status') == STATUS_ACTIVE) {
if ( !$this->checkLoginPermission() ) {
return LoginResult::NO_PERMISSION;
}
if (!$dry_run) {
$this->loginUserById($user_id, $remember_login_cookie);
if ($remember_login) {
// remember username & password when "Remember Login" checkbox us checked (when user is using login form on Front-End)
$this->Application->Session->SetCookie('remember_login', $username . '|' . md5($password), strtotime('+1 month'));
}
if (!$remember_login_cookie) {
// reset counters
$this->Application->resetCounters('UserSession');
$this->_processLoginRedirect($username, $password);
$this->_processInterfaceLanguage();
}
}
return LoginResult::OK;
}
else {
$pending_template = $this->Application->GetVar('pending_disabled_template');
if ($pending_template !== false && !$dry_run) {
// when user found, but it's not yet approved redirect hit to notification template
$this->event->redirect = $pending_template;
return LoginResult::OK;
}
else {
// when no notification template given return an error
return LoginResult::INVALID_PASSWORD;
}
}
}
if (!$dry_run) {
$this->event->SetRedirectParam('pass', 'all');
// $this->event->SetRedirectParam('pass_category', 1); // to test
}
return LoginResult::INVALID_PASSWORD;
}
/**
* Login username by it's PortalUserId
*
* @param int $user_id
* @param bool $remember_login_cookie
*/
function loginUserById($user_id, $remember_login_cookie = false)
{
$object =& $this->getUserObject();
$this->Application->StoreVar('user_id', $user_id);
$this->Application->SetVar('u.current_id', $user_id);
$this->Application->Session->SetField('PortalUserId', $user_id);
if ($user_id != USER_ROOT) {
$groups = $this->Application->RecallVar('UserGroups');
$this->Application->Session->SetField('GroupId', reset( explode(',', $groups) ));
$this->Application->Session->SetField('GroupList', $groups);
}
$this->Application->LoadPersistentVars();
if (!$remember_login_cookie) {
// don't change last login time when auto-login is used
$this_login = (int)$this->Application->RecallPersistentVar('ThisLogin');
$this->Application->StorePersistentVar('LastLogin', $this_login);
$this->Application->StorePersistentVar('ThisLogin', adodb_mktime());
}
$this->Application->HandleEvent($dummy, 'u:OnAfterLogin');
}
/**
* Checks login permission
*
* @return bool
*/
function checkLoginPermission()
{
$object =& $this->getUserObject();
$groups = $object->getMembershipGroups(true);
if (!$groups) {
$groups = Array();
}
if ( $object->GetDBField('UserType') == UserType::USER ) {
array_push($groups, $this->Application->ConfigValue('User_NewGroup') );
}
elseif ( $object->GetDBField('UserType') == UserType::ADMIN ) {
array_push($groups, $this->Application->ConfigValue('User_AdminGroup') );
}
// store groups, because kApplication::CheckPermission will use them!
array_push($groups, $this->Application->ConfigValue('User_LoggedInGroup') );
$this->Application->StoreVar( 'UserGroups', implode(',', $groups), true ); // true for optional
return $this->Application->CheckPermission($this->Application->isAdmin ? 'ADMIN' : 'LOGIN', 1);
}
/**
* Performs user logout
*
*/
function logoutUser()
{
if (!isset($this->event)) {
$this->event = new kEvent('u:OnLogout');
}
$sync_manager =& $this->Application->recallObject('UsersSyncronizeManager', null, Array(), Array ('InPortalSyncronize'));
+ /* @var $sync_manager UsersSyncronizeManager */
+
$sync_manager->performAction('LogoutUser');
$this->Application->HandleEvent($dummy, 'u:OnBeforeLogout');
$user_id = USER_GUEST;
$this->Application->SetVar('u.current_id', $user_id);
+
$object =& $this->Application->recallObject('u.current', null, Array('skip_autoload' => true));
+ /* @var $object UsersItem */
+
$object->Load($user_id);
$this->Application->DestroySession();
$this->Application->StoreVar('user_id', $user_id, true);
$this->Application->Session->SetField('PortalUserId', $user_id);
$group_list = $this->Application->ConfigValue('User_GuestGroup') . ',' . $this->Application->ConfigValue('User_LoggedInGroup');
$this->Application->StoreVar('UserGroups', $group_list, true);
$this->Application->Session->SetField('GroupList', $group_list);
if ($this->Application->ConfigValue('UseJSRedirect')) {
$this->event->SetRedirectParam('js_redirect', 1);
}
$this->Application->resetCounters('UserSession');
$this->Application->Session->SetCookie('remember_login', '', strtotime('-1 hour'));
// don't pass user prefix on logout, since resulting url will have broken "env"
$this->event->SetRedirectParam('pass', MOD_REWRITE ? 'm' : 'all');
}
/**
* Returns user id based on given criteria
*
* @param string $username
* @param string $password
* @param string $remember_login_cookie
* @return int
*/
function getUserId($username, $password, $remember_login_cookie)
{
$password = md5($password);
if ($remember_login_cookie) {
list ($username, $password) = explode('|', $remember_login_cookie); // 0 - username, 1 - md5(password)
}
$sql = 'SELECT PortalUserId
FROM ' . TABLE_PREFIX . 'PortalUser
WHERE (Email = %1$s OR Login = %1$s) AND (Password = %2$s)';
return $this->Conn->GetOne( sprintf($sql, $this->Conn->qstr($username), $this->Conn->qstr($password) ) );
}
/**
* Process all required data and redirect logged-in user
*
* @param string $username
* @param string $password
*/
function _processLoginRedirect($username, $password)
{
// set next template
$next_template = $this->Application->GetVar('next_template');
if ($next_template) {
$this->event->redirect = $next_template;
}
// process IIS redirect
if ($this->Application->ConfigValue('UseJSRedirect')) {
$this->event->SetRedirectParam('js_redirect', 1);
}
- // syncronize login
+ // synchronize login
$sync_manager =& $this->Application->recallObject('UsersSyncronizeManager', null, Array(), Array ('InPortalSyncronize'));
+ /* @var $sync_manager UsersSyncronizeManager */
+
$sync_manager->performAction('LoginUser', $username, $password);
}
/**
- * Sets correct interface language after sucessful login, based on user settings
+ * Sets correct interface language after successful login, based on user settings
*
- * @param kEvent $event
+ * @return void
+ * @access protected
*/
- function _processInterfaceLanguage()
+ protected function _processInterfaceLanguage()
{
- if (!$this->Application->isAdmin) {
- return ;
+ if ( !$this->Application->isAdmin ) {
+ return;
}
$is_root = $this->Application->RecallVar('user_id') == USER_ROOT;
$object =& $this->getUserObject();
$user_language_id = $is_root ? $this->Application->RecallPersistentVar('AdminLanguage') : $object->GetDBField('AdminLanguage');
$sql = 'SELECT LanguageId, IF(LanguageId = ' . (int)$user_language_id . ', 2, AdminInterfaceLang) AS SortKey
FROM ' . TABLE_PREFIX . 'Language
WHERE Enabled = 1
HAVING SortKey <> 0
ORDER BY SortKey DESC';
$language_info = $this->Conn->GetRow($sql);
$language_id = $language_info && $language_info['LanguageId'] ? $language_info['LanguageId'] : $user_language_id;
- if ($user_language_id != $language_id) {
- // first admin login OR language was delelted or disabled
- if ($is_root) {
+ if ( $user_language_id != $language_id ) {
+ // first admin login OR language was deleted or disabled
+ if ( $is_root ) {
$this->Application->StorePersistentVar('AdminLanguage', $language_id);
}
else {
$object->SetDBField('AdminLanguage', $language_id);
$object->Update();
}
}
$this->event->SetRedirectParam('m_lang', $language_id); // data
$this->Application->Session->SetField('Language', $language_id); // interface
}
/**
* Checks that user is allowed to use super admin mode
*
* @return bool
*/
function verifySuperAdmin()
{
$sa_mode = kUtil::ipMatch(defined('SA_IP') ? SA_IP : '');
return $sa_mode || $this->Application->isDebugMode();
}
/**
- * Returns user object, used during login processings
+ * Returns user object, used during login processing
*
* @return UsersItem
+ * @access public
*/
- function &getUserObject()
+ public function &getUserObject()
{
$prefix_special = $this->Application->isAdmin ? 'u.current' : 'u'; // "u" used on front not to change theme
+
$object =& $this->Application->recallObject($prefix_special, null, Array('skip_autoload' => true));
-
+ /* @var $object UsersItem */
+
return $object;
}
/**
* Checks, if given user fields matches at least one of defined ban rules
*
* @param kDBItem $object
* @return bool
*/
function checkBanRules(&$object)
{
$table = $this->Application->getUnitOption('ban-rule', 'TableName');
if (!$this->Conn->TableFound($table)) {
// when ban table not found -> assume user is ok by default
return true;
}
$sql = 'SELECT *
FROM ' . $table . '
WHERE ItemType = 6 AND Status = ' . STATUS_ACTIVE . '
ORDER BY Priority DESC';
$rules = $this->Conn->Query($sql);
$found = false;
foreach ($rules as $rule) {
$field = $rule['ItemField'];
$this_value = mb_strtolower( $object->GetDBField($field) );
$test_value = mb_strtolower( $rule['ItemValue'] );
switch ( $rule['ItemVerb'] ) {
case 1: // is
if ($this_value == $test_value) {
$found = true;
}
break;
case 2: // is not
if ($this_value != $test_value) {
$found = true;
}
break;
case 3: // contains
if ( strstr($this_value, $test_value) ) {
$found = true;
}
break;
case 4: // not contains
if ( !strstr($this_value, $test_value) ) {
$found = true;
}
break;
case 7: // exists
if ( strlen($this_value) > 0 ) {
$found = true;
}
break;
case 8: // unique
if ( $this->_checkValueExist($field, $this_value) ) {
$found = true;
}
break;
}
if ( $found ) {
// check ban rules, until one of them matches
if ( $rule['RuleType'] ) {
// invert rule type
$found = false;
}
break;
}
}
return !$found;
}
/**
* Checks if value is unique in Users table against the specified field
*
* @param string $field
* @param string $value
* @return string
*/
function _checkValueExist($field, $value)
{
$sql = 'SELECT *
FROM ' . $this->Application->getUnitOption('u', 'TableName') . '
WHERE '. $field .' = ' . $this->Conn->qstr($value);
return $this->Conn->GetOne($sql);
}
public function validateUserCode($user_code, $code_type, $expiration_timeout = null)
{
$expiration_timeouts = Array (
'forgot_password' => 'config:Users_AllowReset',
'activation' => 'config:UserEmailActivationTimeout',
'custom' => '',
);
if ( !$user_code ) {
return 'code_is_not_valid';
}
$sql = 'SELECT PwRequestTime, PortalUserId
FROM ' . TABLE_PREFIX . 'PortalUser
WHERE PwResetConfirm = ' . $this->Conn->qstr( trim($user_code) );
$user_info = $this->Conn->GetRow($sql);
if ( $user_info === false ) {
return 'code_is_not_valid';
}
$expiration_timeout = isset($expiration_timeout) ? $expiration_timeout : $expiration_timeouts[$code_type];
if ( preg_match('/^config:(.*)$/', $expiration_timeout, $regs) ) {
$expiration_timeout = $this->Application->ConfigValue( $regs[1] );
}
if ( $expiration_timeout && $user_info['PwRequestTime'] < strtotime('-' . $expiration_timeout . ' minutes') ) {
return 'code_expired';
}
return $user_info['PortalUserId'];
}
}
Index: branches/5.2.x/core/units/helpers/controls/minput_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/controls/minput_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/controls/minput_helper.php (revision 14628)
@@ -1,223 +1,227 @@
<?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 MInputHelper extends kHelper {
-
/**
- * Returns user education table name
+ * Returns table for given prefix
*
- * @param kDBItem $object
+ * @param string $prefix
+ * @param bool $temp
* @return string
+ * @access protected
*/
- function getTable($prefix, $temp=false)
+ protected function getTable($prefix, $temp = false)
{
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
- return $temp ? $this->Application->GetTempName($table_name, 'prefix:'.$prefix) : $table_name;
+
+ return $temp ? $this->Application->GetTempName($table_name, 'prefix:' . $prefix) : $table_name;
}
function prepareMInputXML($records, $use_fields)
{
$xml = '';
foreach ($records as $record) {
$xml .= '<record>';
foreach ($record as $field_name => $field_value) {
if (!in_array($field_name, $use_fields)) {
continue;
}
$xml .= '<field name="' . $field_name . '">' . htmlspecialchars($field_value) . '</field>';
}
$xml .= '</record>';
}
return $xml ? '<records>'.$xml.'</records>' : '';
}
/**
* Returns validation errors in XML format
*
* @param kDBItem $object
* @param Array $fields_hash
* @return string
*/
function prepareErrorsXML(&$object, $fields_hash)
{
$xml = '';
$errors = Array ();
foreach ($fields_hash as $field_name => $field_value) {
if (!$object->ValidateField($field_name)) {
$field_options = $object->GetFieldOptions($field_name);
$error_field = array_key_exists('error_field', $field_options) ? $field_options['error_field'] : $field_name;
$errors[$error_field] = '<field name="'.$error_field.'">'.$object->GetErrorMsg($error_field, false).'</field>';
}
}
return '<errors>'.implode('', $errors).'</errors>';
}
/**
* Validates MInput control fields
*
* @param kEvent $event
*/
function OnValidateMInputFields(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$items_info = $this->Application->GetVar($event->getPrefixSpecial(true));
if ($items_info) {
list ($id, $field_values) = each($items_info);
foreach ($field_values as $field_name => $field_value) {
$object->SetField($field_name, $field_value);
}
$event_mapping = Array (
'AddRecord' => 'OnBeforeItemCreate',
'SaveRecord' => 'OnBeforeItemUpdate',
);
$request_type = $this->Application->GetVar('request_type');
if (array_key_exists($request_type, $event_mapping)) {
$event->CallSubEvent($event_mapping[$request_type]);
}
echo $this->prepareErrorsXML($object, $field_values);
}
$event->status = kEvent::erSTOP;
}
function parseMInputXML($xml)
{
$xml_helper =& $this->Application->recallObject('kXMLHelper');
/* @var $xml_helper kXMLHelper */
$root_node =& $xml_helper->Parse($xml);
$root_node =& $root_node->FindChild('records');
- if (!$root_node || !$root_node->firstChild) {
+
+ if ( !$root_node || !$root_node->firstChild ) {
return false;
}
$records = Array ();
$current_node = $root_node->firstChild;
/* @var $current_node kXMLNode */
do {
- $record = Array();
+ $record = Array ();
$sub_node =& $current_node->firstChild;
/* @var $current_node kXMLNode */
do {
- $record[$sub_node->Attributes['NAME']] = $sub_node->Data;
-
- }while ( ($sub_node =& $sub_node->NextSibling()) );
+ $record[ $sub_node->Attributes['NAME'] ] = $sub_node->Data;
+ } while ( ($sub_node =& $sub_node->NextSibling()) );
$records[] = $record;
- } while (($current_node =& $current_node->NextSibling()));
+ } while ( ($current_node =& $current_node->NextSibling()) );
return $records;
}
/**
* Loads selected values from sub_prefix to main item virtual field.
* Called from OnAfterItemLoad of main prefix.
*
* @param kEvent $event
* @param string $store_field main item's field name, to store values into
* @param string $sub_prefix prefix used to store info about selected items
* @param Array $use_fields fields, used in value string building
*/
function LoadValues(&$event, $store_field, $sub_prefix, $use_fields)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$sub_item =& $this->Application->recallObject($sub_prefix, null, Array('skip_autoload' => true));
/* @var $sub_item kDBItem */
$foreign_key = $this->Application->getUnitOption($sub_prefix, 'ForeignKey');
$sql = 'SELECT *
FROM '.$this->getTable($sub_prefix, $object->IsTempTable()).'
WHERE '.$foreign_key.' = '.$object->GetID();
$selected_items = $this->Conn->Query($sql);
$field_names = array_keys( $sub_item->GetFieldValues() );
foreach ($selected_items as $key => $fields_hash) {
$sub_item->Clear();
$sub_item->SetDBFieldsFromHash($fields_hash);
// to fill *_date and *_time fields from main date fields
$sub_item->UpdateFormattersSubFields();
foreach ($field_names as $field) {
$field_options = $sub_item->GetFieldOptions($field);
$formatter = array_key_exists('formatter', $field_options) ? $field_options['formatter'] : false;
if ($formatter == 'kDateFormatter') {
$selected_items[$key][$field] = $sub_item->GetField($field, $field_options['input_format']);
}
else {
$selected_items[$key][$field] = $sub_item->GetDBField($field);
}
}
}
$object->SetDBField($store_field, $this->prepareMInputXML($selected_items, $use_fields));
}
/**
* Saves data from minput control to subitem table (used from subitem hook)
*
* @param kEvent $sub_event
* @param string $store_field
*/
function SaveValues(&$sub_event, $store_field)
{
$main_object =& $sub_event->MasterEvent->getObject();
+ /* @var $main_object kDBItem */
+
$affected_field = $main_object->GetDBField($store_field);
- $object =& $this->Application->recallObject($sub_event->getPrefixSpecial(), null, Array('skip_autoload' => true));
- /*@var $object kDBItem*/
+ $object =& $this->Application->recallObject($sub_event->getPrefixSpecial(), null, Array ('skip_autoload' => true));
+ /* @var $object kDBItem */
$sub_table = $object->TableName;
$foreign_key = $this->Application->getUnitOption($sub_event->Prefix, 'ForeignKey');
$sql = 'DELETE FROM '.$sub_table.'
WHERE '.$foreign_key.' = '.$main_object->GetID();
$this->Conn->Query($sql);
if ($affected_field) {
$records = $this->parseMInputXML($affected_field);
$main_id = $main_object->GetID();
foreach ($records as $fields_hash) {
$object->Clear();
$fields_hash[$foreign_key] = $main_id;
$object->SetDBFieldsFromHash($fields_hash);
$object->Create();
}
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/controls/edit_picker_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/controls/edit_picker_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/controls/edit_picker_helper.php (revision 14628)
@@ -1,177 +1,186 @@
<?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 EditPickerHelper extends kHelper {
function getTable($prefix, $temp=false)
{
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
return $temp ? $this->Application->GetTempName($table_name, 'prefix:'.$prefix) : $table_name;
}
/**
* Applies filter for multiple lists in inp_edit_picker control.
* Called from SetCustomQuery of prefix, that contains all available items.
*
* @param kEvent $event
* @param string $storage_field main item's field name, where values are located
*/
function applyFilter(&$event, $storage_field)
{
if ($event->Special != 'selected' && $event->Special != 'available') {
return ;
}
if ($storage_field != $event->getEventParam('link_to_field')) {
return ;
}
$object =& $event->getObject();
/* @var $object kDBList */
$main_object =& $this->Application->recallObject($event->getEventParam('link_to_prefix'));
/* @var $main_object kDBItem */
$selected_items = $main_object->GetDBField($storage_field);
if ($selected_items) {
$filter_type = $event->Special == 'selected' ? 'IN' : 'NOT IN';
$selected_items = explode('|', substr($selected_items, 1, -1));
$filter_clause = '%1$s.' . $object->IDField.' '.$filter_type.' ('.implode(',', $selected_items).')';
}
else {
$filter_clause = ($event->Special == 'selected') ? 'FALSE' : 'TRUE';
}
$constrain = $this->_getConstrain($main_object, $storage_field, 'filter');
if ($constrain) {
$filter_clause .= ' AND (' . $constrain . ')';
}
$object->addFilter('edit_picker_filter', $filter_clause);
}
/**
* Loads selected values from sub_prefix to main item virtual field.
* Called from OnAfterItemLoad of main prefix.
*
* @param kEvent $event
* @param string $store_field main item's field name, to store values into
* @param string $source_field prefix and it's field used to store info about selected items (format: prefix.field)
*/
function LoadValues(&$event, $store_field, $source_field)
{
$object =& $event->getObject();
/* @var $object kDBItem */
list ($sub_prefix, $sub_prefix_field) = explode('.', $source_field);
$foreign_key = $this->Application->getUnitOption($sub_prefix, 'ForeignKey');
$sql = 'SELECT '.$sub_prefix_field.'
FROM '.$this->getTable($sub_prefix, $object->IsTempTable()).'
WHERE '.$foreign_key.' = '.$object->GetID();
$constrain = $this->_getConstrain($object, $store_field, 'load');
if ($constrain) {
$sql .= ' AND (' . $sub_prefix_field . ' IN (' . $constrain . '))';
}
$selected_items = array_unique($this->Conn->GetCol($sql));
$object->SetDBField($store_field, $selected_items ? '|'.implode('|', $selected_items).'|' : '');
}
/**
- * Saves value to subitem's table
+ * Saves value to sub-item's table
*
* @param kEvent $sub_event
* @param string $store_field main item's field name, to get values from
* @param string $sub_prefix_field check already existing records by this field
*/
function SaveValues(&$sub_event, $store_field, $sub_prefix_field)
{
$main_object =& $sub_event->MasterEvent->getObject();
/* @var $main_object kDBItem */
$affected_field = $main_object->GetDBField($store_field);
$object =& $this->Application->recallObject($sub_event->getPrefixSpecial(), null, Array('skip_autoload' => true));
/* @var $object kDBItem */
$sub_table = $object->TableName;
$foreign_key = $this->Application->getUnitOption($sub_event->Prefix, 'ForeignKey');
// 1. get previous values from db
$sql = 'SELECT ' . $sub_prefix_field . '
FROM ' . $sub_table . '
WHERE '.$foreign_key.' = '.$main_object->GetID();
$constrain = $this->_getConstrain($main_object, $store_field, 'save');
if ($constrain) {
$sql .= ' AND (' . $sub_prefix_field . ' IN (' . $constrain . '))';
}
$old_values = $this->Conn->GetCol($sql);
// 2. get new values from form
$new_values = $affected_field ? explode('|', substr($affected_field, 1, -1)) : Array ();
$records_to_add = array_diff($new_values, $old_values);
$records_to_delete = array_diff($old_values, $new_values);
if ($records_to_delete && $main_object->isLoaded()) {
$where_clause = Array (
$foreign_key . ' = ' . $main_object->GetID(),
$sub_prefix_field . ' IN (' . implode(',', $records_to_delete) . ')',
);
$sql = 'SELECT ' . $object->IDField . '
FROM ' . $sub_table . '
WHERE (' . implode(') AND (', $where_clause) . ')';
$delete_ids = $this->Conn->GetCol($sql);
foreach ($delete_ids as $delete_id) {
$object->Delete($delete_id);
}
}
if ($records_to_add) {
$main_id = $main_object->GetID();
foreach ($records_to_add as $add_id) {
$object->Clear();
$object->SetDBField($foreign_key, $main_id);
$object->SetDBField($sub_prefix_field, $add_id);
$object->Create();
}
}
}
+ /**
+ * Returns constrain for picker options query
+ *
+ * @param kDBItem $object
+ * @param string $store_field
+ * @param string $mode
+ * @return bool|string
+ */
function _getConstrain(&$object, $store_field, $mode = 'filter')
{
$field_options = $object->GetFieldOptions($store_field);
- $constrain = array_key_exists('option_constrain', $field_options) ? $field_options['option_constrain'] : false;
+ $constrain = array_key_exists('option_constrain', $field_options) ? $field_options['option_constrain']
+ : false;
- if ($mode == 'filter') {
+ if ( $mode == 'filter' ) {
// filter on edit form
return $constrain;
}
- elseif ($constrain) {
+ elseif ( $constrain ) {
// load or save
return sprintf($field_options['options_sql'], $field_options['option_key_field']);
}
return false;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/deployment_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/deployment_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/deployment_helper.php (revision 14628)
@@ -1,386 +1,386 @@
<?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 DeploymentHelper extends kHelper {
private $moduleName = '';
private $revisionSqls = Array ();
private $revisionDependencies = Array ();
private $appliedRevisions = Array ();
private $dryRun = false;
private $lineEnding = PHP_EOL;
- public function DeploymentHelper()
+ public function __construct()
{
- parent::kHelper();
+ parent::__construct();
set_time_limit(0);
ini_set('memory_limit', -1);
}
private function loadAppliedRevisions()
{
$sql = 'SELECT AppliedDBRevisions
FROM ' . TABLE_PREFIX . 'Modules
WHERE Name = ' . $this->Conn->qstr( $this->moduleName );
$revisions = $this->Conn->GetOne($sql);
$this->appliedRevisions = $revisions ? explode(',', $revisions) : Array ();
}
private function saveAppliedRevisions()
{
// maybe optimize
sort($this->appliedRevisions);
$fields_hash = Array (
'AppliedDBRevisions' => implode(',', $this->appliedRevisions),
);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'Modules', '`Name` = ' . $this->Conn->qstr($this->moduleName));
}
public function deployAll($dry_run = false)
{
$this->dryRun = $dry_run;
$this->lineEnding = $dry_run ? '<br/>' . PHP_EOL : PHP_EOL;
$ret = true;
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
$this->moduleName = $module_name;
if ( !file_exists($this->getModuleFile('project_upgrades.sql')) ) {
continue;
}
$ret = $ret && $this->deploy($module_name);
}
if (!$this->dryRun) {
$this->resetCaches();
$this->refreshThemes();
}
return $ret;
}
/**
* Deploys pending changes to a site
*
*/
private function deploy($module_name)
{
echo 'Deploying Module "' . $module_name . '":' . $this->lineEnding;
echo 'Upgrading Database ... ';
if ( !$this->upgradeDatabase() ) {
return false;
}
echo 'OK' . $this->lineEnding;
$this->importLanguagePack();
echo 'Done.' . $this->lineEnding;
return true;
}
/**
* Import latest languagepack (without overwrite)
*
*/
private function importLanguagePack()
{
$language_import_helper =& $this->Application->recallObject('LanguageImportHelper');
/* @var $language_import_helper LanguageImportHelper */
echo 'Importing LanguagePack ... ';
$filename = $this->getModuleFile('english.lang');
$language_import_helper->performImport($filename, '|0|1|2|', $this->moduleName, LANG_SKIP_EXISTING);
echo 'OK' . $this->lineEnding;
}
/**
* Resets unit and section cache
*
*/
private function resetCaches()
{
// 2. reset unit config cache (so new classes get auto-registered)
echo 'Resetting Unit Config Cache ... ';
$admin_event = new kEvent('adm:OnResetConfigsCache');
$this->Application->HandleEvent($admin_event);
echo 'OK' . $this->lineEnding;
// 3. reset sections cache
echo 'Resetting Sections Cache ... ';
$admin_event = new kEvent('adm:OnResetSections');
$this->Application->HandleEvent($admin_event);
echo 'OK' . $this->lineEnding;
}
/**
* Rebuild theme files
*
*/
private function refreshThemes()
{
echo 'Rebuilding Theme Files ... ';
$admin_event = new kEvent('adm:OnRebuildThemes');
$this->Application->HandleEvent($admin_event);
echo 'OK' . $this->lineEnding;
}
/**
* Runs database upgrade script
*
* @return bool
*/
private function upgradeDatabase()
{
$this->loadAppliedRevisions();
$this->Conn->errorHandler = Array(&$this, 'handleSqlError');
if ( !$this->collectDatabaseRevisions() || !$this->checkRevisionDependencies() ) {
return false;
}
$applied = $this->applyRevisions();
$this->saveAppliedRevisions();
return $applied;
}
/**
* Collects database revisions from "project_upgrades.sql" file.
*
* @return bool
*/
private function collectDatabaseRevisions()
{
$filename = $this->getModuleFile('project_upgrades.sql');
if ( !file_exists($filename) ) {
return true;
}
$sqls = file_get_contents($filename);
preg_match_all("/# r([\d]+)([^\:]*):.*?(\n|$)/s", $sqls, $matches, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
if (!$matches) {
echo 'No Database Revisions Found' . $this->lineEnding;
return false;
}
foreach ($matches as $index => $match) {
$revision = $match[1][0];
if ( $this->revisionApplied($revision) ) {
// skip applied revisions
continue;
}
if ( isset($this->revisionSqls[$revision]) ) {
// duplicate revision among non-applied ones
echo 'Duplicate revision ' . $revision . ' found' . $this->lineEnding;
return false;
}
// get revision sqls
$start_pos = $match[0][1] + strlen($match[0][0]);
$end_pos = isset($matches[$index + 1]) ? $matches[$index + 1][0][1] : strlen($sqls);
$revision_sqls = substr($sqls, $start_pos, $end_pos - $start_pos);
if (!$revision_sqls) {
// resision without sqls
continue;
}
$this->revisionSqls[$revision] = $revision_sqls;
$revision_lependencies = $this->parseRevisionDependencies($match[2][0]);
if ($revision_lependencies) {
$this->revisionDependencies[$revision] = $revision_lependencies;
}
}
ksort($this->revisionSqls);
ksort($this->revisionDependencies);
return true;
}
/**
* Checks that all dependent revisions are either present now OR were applied before
*
* @return bool
*/
private function checkRevisionDependencies()
{
foreach ($this->revisionDependencies as $revision => $revision_dependencies) {
foreach ($revision_dependencies as $revision_dependency) {
if ( $this->revisionApplied($revision_dependency) ) {
// revision dependend upon already applied -> depencency fulfilled
continue;
}
if ($revision_dependency >= $revision) {
echo 'Revision ' . $revision . ' has incorrect dependency to revision ' . $revision_dependency . '. Only dependencies to older revisions are allowed!' . $this->lineEnding;
return false;
}
if ( !isset($this->revisionSqls[$revision_dependency]) ) {
echo 'Revision ' . $revision . ' depends on missing revision ' . $revision_dependency . '!' . $this->lineEnding;
return false;
}
}
}
return true;
}
/**
* Runs all pending sqls
*
* @return bool
*/
private function applyRevisions()
{
if (!$this->revisionSqls) {
return true;
}
if ($this->dryRun) {
$this->appliedRevisions = array_merge($this->appliedRevisions, array_keys($this->revisionSqls));
return true;
}
echo $this->lineEnding;
foreach ($this->revisionSqls as $revision => $sqls) {
echo 'Processing DB Revision: #' . $revision . ' ... ';
$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
$sqls = explode(";\n", $no_comment_sqls . "\n"); // ensures that last sql won't have ";" in it
$sqls = array_map('trim', $sqls);
foreach ($sqls as $index => $sql) {
if (!$sql || (substr($sql, 0, 1) == '#')) {
continue; // usually last line
}
$this->Conn->Query($sql);
if ( $this->Conn->hasError() ) {
// consider revisions with errors applied
$this->appliedRevisions[] = $revision;
return false;
}
}
$this->appliedRevisions[] = $revision;
echo 'OK' . $this->lineEnding;
}
return true;
}
/**
* Error handler for sql errors
*
* @param int $code
* @param string $msg
* @param string $sql
* @return bool
*/
public function handleSqlError($code, $msg, $sql)
{
echo 'Error (#' . $code . ': ' . $msg . ') during SQL processing:' . $this->lineEnding . $sql . $this->lineEnding;
echo 'Please execute rest of sqls in this revision by hand and run deployment script again.' . $this->lineEnding;
return true;
}
/**
* Checks if given revision was already applied
*
* @param int $revision
* @return bool
*/
private function revisionApplied($revision)
{
foreach ($this->appliedRevisions as $applied_revision) {
// revision range
$applied_revision = explode('-', $applied_revision, 2);
if ( !isset($applied_revision[1]) ) {
// convert single revision to revision range
$applied_revision[1] = $applied_revision[0];
}
if ( $revision >= $applied_revision[0] && $revision <= $applied_revision[1] ) {
return true;
}
}
return false;
}
/**
* Returns path to given file in current module install folder
*
* @param string $filename
* @return string
*/
private function getModuleFile($filename)
{
$module_folder = $this->Application->findModule('Name', $this->moduleName, 'Path');
return FULL_PATH . DIRECTORY_SEPARATOR . $module_folder . 'install/' . $filename;
}
/**
* Extracts revisions from string in format "(1,3,5464,23342,3243)"
*
* @param string $string
* @return Array
*/
private function parseRevisionDependencies($string)
{
if (!$string) {
return Array ();
}
$string = explode(',', substr($string, 1, -1));
return array_map('trim', $string);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/mod_rewrite_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/mod_rewrite_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/mod_rewrite_helper.php (revision 14628)
@@ -1,1231 +1,1234 @@
<?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 kModRewriteHelper extends kHelper {
/**
* Holds a refererence to httpquery
*
* @var kHttpQuery
*/
var $HTTPQuery = null;
/**
* Urls parts, that needs to be matched by rewrite listeners
*
* @var Array
*/
var $_partsToParse = Array ();
/**
* Category item prefix, that was found
*
* @var string
*/
var $_modulePrefix = false;
/**
* Template aliases for current theme
*
* @var Array
*/
var $_templateAliases = null;
/**
* Domain-based primary language id
*
* @var int
*/
var $primaryLanguageId = false;
/**
* Domain-based primary theme id
*
* @var int
*/
var $primaryThemeId = false;
/**
* Possible url endings from ModRewriteUrlEnding configuration variable
*
* @var Array
*/
var $_urlEndings = Array ('.html', '/', '');
/**
* Constructor of kModRewriteHelper class
*
* @return kModRewriteHelper
*/
public function __construct()
{
parent::__construct();
$this->HTTPQuery =& $this->Application->recallObject('HTTPQuery');
// domain based primary language
$this->primaryLanguageId = $this->Application->siteDomainField('PrimaryLanguageId');
// domain based primary theme
$this->primaryThemeId = $this->Application->siteDomainField('PrimaryThemeId');
}
function processRewriteURL()
{
$passed = Array ();
$url = $this->HTTPQuery->Get('_mod_rw_url_');
if ($url) {
foreach ($this->_urlEndings as $url_ending) {
if (substr($url, strlen($url) - strlen($url_ending)) == $url_ending) {
$url = substr($url, 0, strlen($url) - strlen($url_ending));
$default_ending = $this->Application->ConfigValue('ModRewriteUrlEnding');
// user manually typed url with different url ending -> redirect to same url with default url ending
if (($url_ending != $default_ending) && $this->Application->ConfigValue('ForceModRewriteUrlEnding')) {
$target_url = $this->Application->BaseURL() . $url . $default_ending;
$this->Application->Redirect('external:' . $target_url, Array ('response_code' => 301));
}
break;
}
}
}
$restored = false;
$cached = $this->_getCachedUrl($url);
if ($cached !== false) {
$vars = $cached['vars'];
$passed = $cached['passed'];
$restored = true;
}
else {
$vars = $this->parseRewriteURL($url);
$passed = $vars['pass']; // also used in bottom of this method
unset($vars['pass']);
$this->_setCachedUrl($url, Array ('vars' => $vars, 'passed' => $passed));
if (array_key_exists('t', $this->HTTPQuery->Post) && $this->HTTPQuery->Post['t']) {
// template from POST overrides template from URL.
$vars['t'] = $this->HTTPQuery->Post['t'];
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);
}
function _getCachedUrl($url)
{
if (!$url) {
return false;
}
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'CachedUrls
WHERE Hash = ' . 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 < adodb_mktime())) {
// delete expired
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'CachedUrls
WHERE UrlId = ' . $data['UrlId'];
$this->Conn->Query($sql);
return false;
}
return unserialize($data['ParsedVars']);
}
return false;
}
function _setCachedUrl($url, $data)
{
if (!$url) {
return ;
}
$vars = $data['vars'];
$passed = $data['passed'];
sort($passed);
// get expiration
if ($vars['m_cat_id'] > 0) {
$sql = 'SELECT PageExpiration
FROM ' . TABLE_PREFIX . 'Category
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' => crc32($url),
'DomainId' => (int)$this->Application->siteDomainField('DomainId'),
'Prefixes' => $prefixes ? '|' . implode('|', $prefixes) . '|' : '',
'ParsedVars' => serialize($data),
'Cached' => adodb_mktime(),
'LifeTime' => isset($expiration) && is_numeric($expiration) ? $expiration : -1
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'CachedUrls');
}
function parseRewriteURL($url)
{
$vars = Array ('pass' => Array ('m'));
$url_parts = $url ? explode('/', trim(mb_strtolower($url, 'UTF-8'), '/')) : Array ();
$this->_partsToParse = $url_parts;
if ( ($this->HTTPQuery->Get('rewrite') == 'on') || !$url_parts ) {
$this->_setDefaultValues($vars);
}
if ( !$url_parts ) {
$this->InitAll();
$vars['t'] = $this->HTTPQuery->getDefaultTemplate('');
return $vars;
}
else {
$vars['t'] = '';
}
$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) ) {
return $vars;
}
$this->_parsePhisycalTemplate($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 ) {
$not_found = $this->Application->ConfigValue('ErrorTemplate');
$vars['t'] = $not_found ? $not_found : 'error_notfound';
$themes_helper =& $this->Application->recallObject('ThemesHelper');
/* @var $themes_helper kThemesHelper */
$vars['m_cat_id'] = $themes_helper->getPageByTemplate($vars['t'], $vars['m_theme']);
header('HTTP/1.0 404 Not Found');
}
return $vars;
}
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
// $this->Application->Phrases->Init('phrases');
}
/**
* Processes url using rewrite listeners
*
* Pattern: Chain of Command
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
function processRewriteListeners(&$url_parts, &$vars)
{
$this->initRewriteListeners();
$page_number = $this->_parsePage($url_parts, $vars);
foreach ($this->Application->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;
}
/**
* Parses real template name from url
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
function _parsePhisycalTemplate($url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
do {
$template_path = implode('/', $url_parts);
$physical_template = $this->Application->getPhysicalTemplate($template_path);
if (($physical_template !== false) && (substr($physical_template, 0, 3) != 'id:')) {
// replace menu template name with it's actual template name on disk
list ($template_path) = explode(':', $physical_template, 2);
}
$t_parts['path'] = dirname($template_path) == '.' ? '' : '/' . dirname($template_path);
$t_parts['file'] = basename($template_path);
$sql = 'SELECT FileId
FROM ' . TABLE_PREFIX . 'ThemeFiles
WHERE (ThemeId = ' . $vars['m_theme'] . ') AND (FilePath = ' . $this->Conn->qstr($t_parts['path']) . ') AND (FileName = ' . $this->Conn->qstr($t_parts['file'] . '.tpl') . ')';
$template_found = $this->Conn->GetOne($sql);
if (!$template_found) {
array_shift($url_parts);
}
} while (!$template_found && $url_parts);
if ($template_found) {
$vars['t'] = $template_path;
$template_parts = explode('/', $template_path);
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
// $themes_helper =& $this->Application->recallObject('ThemesHelper');
// /* @var $themes_helper kThemesHelper */
//
// $vars['m_cat_id'] = $themes_helper->getPageByTemplate($template_path, $vars['m_theme']);
return true;
}
return false;
}
/**
* 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.
*/
function MainRewriteListener($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
*/
function _buildMainUrl($prefix_special, &$params, $keep_events)
{
$ret = '';
list ($prefix) = explode('.', $prefix_special);
$processed_params = $this->getProcessedParams($prefix_special, $params, $keep_events);
if ($processed_params === false) {
return '';
}
// add language
if (!$this->primaryLanguageId) {
// when domain-based language not found -> use site-wide language
$this->primaryLanguageId = $this->Application->GetDefaultLanguageId();
}
if ($processed_params['m_lang'] && ($processed_params['m_lang'] != $this->primaryLanguageId)) {
$language_name = $this->Application->getCache('language_names[%LangIDSerial:' . $processed_params['m_lang'] . '%]');
if ($language_name === false) {
$sql = 'SELECT PackName
FROM ' . TABLE_PREFIX . 'Language
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 (!$this->primaryThemeId) {
// when domain-based theme not found -> use site-wide theme
$this->primaryThemeId = $this->Application->GetDefaultThemeId(true);
}
if ($processed_params['m_theme'] && ($processed_params['m_theme'] != $this->primaryThemeId)) {
$theme_name = $this->Application->getCache('theme_names[%ThemeIDSerial:' . $processed_params['m_theme'] . '%]');
if ($theme_name === false) {
$sql = 'SELECT Name
FROM ' . TABLE_PREFIX . 'Theme
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 OR site homepage
if (($template == $category_template) || (mb_strtolower($template) == '__default__') || ($template == 'index')) {
// given template is also default template for this category OR '__default__' given OR site homepage
$params['pass_template'] = false;
}
if ($template && $params['pass_template']) {
$ret .= $template . '/';
}
return mb_strtolower( rtrim($ret, '/') );
}
/**
* Gets language part from url
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
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 . 'Language
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
*/
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 . 'Theme
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;
}
/**
* Checks if whole url_parts matches a whole In-CMS page
*
* @param array $url_parts
* @return boolean
*/
function _parseFriendlyUrl($url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
$sql = 'SELECT CategoryId, NamedParentPath
FROM ' . TABLE_PREFIX . 'Category
WHERE FriendlyURL = ' . $this->Conn->qstr(implode('/', $url_parts));
$friendly = $this->Conn->GetRow($sql);
if ($friendly) {
$vars['m_cat_id'] = $friendly['CategoryId'];
$vars['t'] = preg_replace('/^Content\//i', '', $friendly['NamedParentPath']);
while ($url_parts) {
$this->partParsed( array_shift($url_parts) );
}
return true;
}
return false;
}
/**
* Set's page (when found) to all modules
*
* @param Array $url_parts
* @param Array $vars
* @return string
*
* @todo Should find a way, how to determine what rewrite listerner page is it
*/
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;
}
/**
* Remove page numbers for all rewrite listeners
*
* @todo Should find a way, how to determine what rewrite listerner page is it
*/
function removePages()
{
/*foreach ($this->Application->RewriteListeners as $prefix => $listener) {
$this->Application->DeleteVar($prefix . '_Page');
}*/
}
/**
* Extracts category part from url
*
* @param Array $url_parts
* @param Array $vars
* @return bool
*/
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';
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 . 'Category
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;
$this->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 . 'Category
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']), 'UTF-8' );
$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;
}
/**
* Builds/parses category item 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 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.
*/
function CategoryItemRewriteListener($rewrite_mode = REWRITE_MODE_BUILD, $prefix, &$params, &$url_parts, $keep_events = false)
{
static $parsed = false;
if ($rewrite_mode == REWRITE_MODE_BUILD) {
return $this->_buildCategoryItemUrl($prefix, $params, $keep_events);
}
if (!$parsed) {
$this->_modulePrefix = $this->_parseCategoryItemUrl($url_parts, $params);
if ($this->_modulePrefix !== false) {
$params['pass'][] = $this->_modulePrefix;
}
$parsed = true;
}
return true;
}
/**
* Build category teim part of url
*
* @param string $prefix_special
* @param Array $params
* @param bool $keep_events
* @return string
*/
function _buildCategoryItemUrl($prefix_special, &$params, $keep_events)
{
static $default_per_page = Array ();
$ret = '';
list ($prefix) = explode('.', $prefix_special);
$processed_params = $this->getProcessedParams($prefix_special, $params, $keep_events);
if ($processed_params === false) {
return '';
}
if (!array_key_exists($prefix, $default_per_page)) {
$list_helper =& $this->Application->recallObject('ListHelper');
/* @var $list_helper ListHelper */
$default_per_page[$prefix] = $list_helper->getDefaultPerPage($prefix);
}
if ($processed_params[$prefix_special . '_id']) {
$category_id = array_key_exists('m_cat_id', $params) ? $params['m_cat_id'] : $this->Application->GetVar('m_cat_id');
// if template is also item template of category, then remove template
$template = array_key_exists('t', $params) ? $params['t'] : false;
$item_template = $this->GetItemTemplate($category_id, $prefix);
if ($template == $item_template || strtolower($template) == '__default__') {
// given template is also default template for this category item or '__default__' given
$params['pass_template'] = false;
}
// get item's filename
if ($prefix == 'bb') {
$ret .= 'bb_' . $processed_params[$prefix_special . '_id'] . '/';
}
else {
$filename = $this->getFilename($prefix, $processed_params[$prefix_special . '_id'], $category_id);
if ($filename !== false) {
$ret .= $filename . '/';
}
}
} else {
if ($processed_params[$prefix_special . '_Page'] == 1) {
// when printing category items and we are on the 1st page -> there is no information about
// category item prefix and $params['pass_category'] will not be added automatically
$params['pass_category'] = true;
}
elseif ($processed_params[$prefix_special . '_Page'] > 1) {
// $ret .= $processed_params[$prefix_special . '_Page'] . '/';
$params['page'] = $processed_params[$prefix_special . '_Page'];
}
$per_page = $processed_params[$prefix_special . '_PerPage'];
if ($per_page && ($per_page != $default_per_page[$prefix])) {
$params['per_page'] = $processed_params[$prefix_special . '_PerPage'];
}
}
return mb_strtolower( rtrim($ret, '/') );
}
/**
* Returns item's filename that corresponds id passed. If possible, then get it from cache
*
* @param string $prefix
* @param int $id
* @param int $category_id
* @return string
*/
function getFilename($prefix, $id, $category_id = null)
{
if ($prefix == 'c') {
throw new Exception('Method "<strong>' . __FUNCTION__ . '</strong>" no longer work with "<strong>c</strong>" prefix. Please use "<strong>getCategoryCache</strong>" method instead');
return false;
}
$category_id = isset($category_id) ? $category_id : $this->Application->GetVar('m_cat_id');
$cache_key = 'filenames[%' . $this->Application->incrementCacheSerial($prefix, $id, false) . '%]:' . (int)$category_id;
$filename = $this->Application->getCache($cache_key);
if ($filename === false) {
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT ResourceId
FROM ' . $this->Application->getUnitOption($prefix, 'TableName') . '
WHERE ' . $this->Application->getUnitOption($prefix, 'IDField') . ' = ' . $this->Conn->qstr($id);
$resource_id = $this->Conn->GetOne($sql);
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT Filename
FROM ' . TABLE_PREFIX . 'CategoryItems
WHERE (ItemResourceId = ' . $resource_id . ') AND (CategoryId = ' . (int)$category_id . ')';
$filename = $this->Conn->GetOne($sql);
if ($filename !== false) {
$this->Application->setCache($cache_key, $filename);
}
}
return $filename;
}
/**
* Sets template and id, corresponding to category item given in url
*
* @param Array $url_parts
* @param Array $vars
* @return bool|string
*/
function _parseCategoryItemUrl(&$url_parts, &$vars)
{
if (!$url_parts) {
return false;
}
$item_filename = end($url_parts);
if (is_numeric($item_filename)) {
// this page, don't process here
return false;
}
if (preg_match('/^bb_([\d]+)/', $item_filename, $regs)) {
// process topics separatly, because they don't use item filenames
array_pop($url_parts);
$this->partParsed($item_filename, 'rtl');
return $this->_parseTopicUrl($regs[1], $vars);
}
// locating the item in CategoryItems by filename to detect its ItemPrefix and its category ParentPath
$sql = 'SELECT ci.ItemResourceId, ci.ItemPrefix, c.ParentPath, ci.CategoryId
FROM ' . TABLE_PREFIX . 'CategoryItems AS ci
LEFT JOIN ' . TABLE_PREFIX . 'Category AS c ON c.CategoryId = ci.CategoryId
WHERE (ci.CategoryId = ' . (int)$vars['m_cat_id'] . ') AND (ci.Filename = ' . $this->Conn->qstr($item_filename) . ')';
$cat_item = $this->Conn->GetRow($sql);
if ($cat_item !== false) {
// item found
$module_prefix = $cat_item['ItemPrefix'];
$item_template = $this->GetItemTemplate($cat_item, $module_prefix);
// converting ResourceId to correpsonding Item id
$module_config = $this->Application->getUnitOptions($module_prefix);
$sql = 'SELECT ' . $module_config['IDField'] . '
FROM ' . $module_config['TableName'] . '
WHERE ResourceId = ' . $cat_item['ItemResourceId'];
$item_id = $this->Conn->GetOne($sql);
array_pop($url_parts);
if ($item_id) {
$this->partParsed($item_filename, 'rtl');
if ($item_template) {
// when template is found in category -> set it
$vars['t'] = $item_template;
}
// we have category item id
$vars[$module_prefix . '_id'] = $item_id;
return $module_prefix;
}
}
return false;
}
/**
* Set's template and topic id corresponding to topic given in url
*
* @param int $topic_id
* @param Array $vars
* @return string
*/
function _parseTopicUrl($topic_id, &$vars)
{
$sql = 'SELECT c.ParentPath, c.CategoryId
FROM ' . TABLE_PREFIX . 'Category AS c
WHERE c.CategoryId = ' . (int)$vars['m_cat_id'];
$cat_item = $this->Conn->GetRow($sql);
$item_template = $this->GetItemTemplate($cat_item, 'bb');
if ($item_template) {
$vars['t'] = $item_template;
}
$vars['bb_id'] = $topic_id;
return 'bb';
}
/**
* Returns enviroment variable values for given prefix (uses directly given params, when available)
*
* @param string $prefix_special
* @param Array $params
* @param bool $keep_events
* @return Array
*/
function getProcessedParams($prefix_special, &$params, $keep_events)
{
list ($prefix) = explode('.', $prefix_special);
- $query_vars = $this->Application->getUnitOption($prefix, 'QueryString');
- if (!$query_vars) {
+ $query_vars = $this->Application->getUnitOption($prefix, 'QueryString', Array ());
+ /* @var $query_vars 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) {
+ 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 ( array_key_exists($prefix_special . '_event', $params) && !$params[$prefix_special . '_event'] ) {
// if empty event, then remove it from url
- unset( $params[$prefix_special . '_event'] );
+ 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)) {
+ 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 $index => $var_name) {
+ 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)) {
+
+ 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
* @return string
*/
function GetItemTemplate($category, $module_prefix)
{
$category_id = is_array($category) ? $category['CategoryId'] : $category;
$cache_key = __CLASS__ . '::' . __FUNCTION__ . '[%CIDSerial:' . $category_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 . 'Category 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 . 'Category 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 . 'Theme
WHERE ThemeId = ' . (int)$themes_helper->getCurrentThemeId();
$template_aliases = $this->Conn->GetOne($sql);
$this->_templateAliases = $template_aliases ? unserialize($template_aliases) : Array ();
}
if ($item_template && array_key_exists($item_template, $this->_templateAliases)) {
$item_template = $this->_templateAliases[$item_template];
}
$this->Application->setCache($cache_key, $item_template);
return $item_template;
}
/**
* Loads all registered rewrite listeners, so they could be quickly accessed later
*
*/
function initRewriteListeners()
{
static $init_done = false;
if ($init_done || count($this->Application->RewriteListeners) == 0) {
// not inited 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);
$listener =& $this->Application->recallObject($listener_prefix);
$this->Application->RewriteListeners[$prefix][$index] = Array (&$listener, $listener_method);
}
}
define('MOD_REWRITE_URL_ENDING', $this->Application->ConfigValue('ModRewriteUrlEnding'));
$init_done = true;
}
/**
* Returns category custom field id, where given module prefix item template name is stored
*
* @param string $module_prefix
* @return int
*/
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 . 'CustomField
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;
}
/**
* Sets default parsed values before actual url parsing (only, for empty url)
*
* @param Array $vars
*/
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;
}
}
}
/**
* Marks url part as parsed
*
* @param string $url_part
* @param string $parse_direction
*/
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);
}
}
}
Index: branches/5.2.x/core/units/helpers/image_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/image_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/image_helper.php (revision 14628)
@@ -1,709 +1,709 @@
<?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 ImageHelper extends kHelper {
/**
* File helper reference
*
* @var FileHelper
*/
var $fileHelper = null;
public function __construct()
{
parent::__construct();
$this->fileHelper =& $this->Application->recallObject('FileHelper');
}
/**
* Parses format string into array
*
* @param string $format sample format: "resize:300x500;wm:inc/wm.png|c|-20"
* @return Array sample result: Array('max_width' => 300, 'max_height' => 500, 'wm_filename' => 'inc/wm.png', 'h_margin' => 'c', 'v_margin' => -20)
*/
function parseFormat($format)
{
$res = Array ();
$format_parts = explode(';', $format);
foreach ($format_parts as $format_part) {
if (preg_match('/resize:(\d*)x(\d*)/', $format_part, $regs)) {
$res['max_width'] = $regs[1];
$res['max_height'] = $regs[2];
}
elseif (preg_match('/wm:([^\|]*)\|([^\|]*)\|([^\|]*)/', $format_part, $regs)) {
$res['wm_filename'] = FULL_PATH.THEMES_PATH.'/'.$regs[1];
$res['h_margin'] = strtolower($regs[2]);
$res['v_margin'] = strtolower($regs[3]);
}
elseif (preg_match('/crop:([^\|]*)\|([^\|]*)/', $format_part, $regs)) {
$res['crop_x'] = strtolower($regs[1]);
$res['crop_y'] = strtolower($regs[2]);
}
elseif ($format_part == 'img_size' || $format_part == 'img_sizes') {
$res['image_size'] = true;
}
elseif (preg_match('/fill:(.*)/', $format_part, $regs)) {
$res['fill'] = $regs[1];
} elseif (preg_match('/default:(.*)/', $format_part, $regs)) {
$res['default'] = FULL_PATH.THEMES_PATH.'/'.$regs[1];
}
}
return $res;
}
/**
* Resized given image to required dimensions & saves resized image to "resized" subfolder in source image folder
*
* @param string $src_image full path to image (on server)
* @param mixed $max_width maximal allowed resized image width or false if no limit
* @param mixed $max_height maximal allowed resized image height or false if no limit
* @return string direct url to resized image
*/
function ResizeImage($src_image, $max_width, $max_height = false)
{
$image_size = false;
if (is_numeric($max_width)) {
$params['max_width'] = $max_width;
$params['max_height'] = $max_height;
}
else {
$params = $this->parseFormat($max_width);
if (array_key_exists('image_size', $params)) {
// image_size param shouldn't affect resized file name (crc part)
$image_size = $params['image_size'];
unset($params['image_size']);
}
}
if ((!$src_image || !file_exists($src_image)) && array_key_exists('default', $params) && !(defined('DBG_IMAGE_RECOVERY') && DBG_IMAGE_RECOVERY)) {
$src_image = $params['default'];
}
if ($params['max_width'] > 0 || $params['max_height'] > 0) {
list ($params['target_width'], $params['target_height'], $needs_resize) = $this->GetImageDimensions($src_image, $params['max_width'], $params['max_height'], $params);
if (!is_numeric($params['max_width'])) {
$params['max_width'] = $params['target_width'];
}
if (!is_numeric($params['max_height'])) {
$params['max_height'] = $params['target_height'];
}
$src_path = dirname($src_image);
$transform_keys = Array ('crop_x', 'crop_y', 'fill', 'wm_filename');
if ($needs_resize || array_intersect(array_keys($params), $transform_keys)) {
// resize required OR watermarking required -> change resulting image name !
$src_path_escaped = preg_replace('/(\\\[\d]+)/', '\\\\\1', $src_path); // escape replacement patterns, like "\<number>"
$dst_image = preg_replace('/^'.preg_quote($src_path, '/').'(.*)\.(.*)$/', $src_path_escaped . DIRECTORY_SEPARATOR . 'resized\\1_' . crc32(serialize($params)) . '.\\2', $src_image);
$this->fileHelper->CheckFolder( dirname($dst_image) );
if (!file_exists($dst_image) || filemtime($src_image) > filemtime($dst_image)) {
// resized image not available OR should be recreated due source image change
$params['dst_image'] = $dst_image;
$image_resized = $this->ScaleImage($src_image, $params);
if (!$image_resized) {
// resize failed, because of server error
$dst_image = $src_image;
}
}
// resize/watermarking ok
$src_image = $dst_image;
}
}
if ($image_size) {
// return only image size (resized or not)
$image_info = $this->getImageInfo($src_image);
return $image_info ? $image_info[3] : '';
}
return $this->fileHelper->pathToUrl($src_image);
}
/**
* Proportionally resizes given image to destination dimensions
*
* @param string $src_image full path to source image (already existing)
* @param string $dst_image full path to destination image (will be created)
* @param int $dst_width destination image width (in pixels)
* @param int $dst_height destination image height (in pixels)
*/
function ScaleImage($src_image, $params)
{
$image_info = $this->getImageInfo($src_image);
if (!$image_info) {
return false;
}
/*list ($params['max_width'], $params['max_height'], $resized) = $this->GetImageDimensions($src_image, $params['max_width'], $params['max_height'], $params);
if (!$resized) {
// image dimensions are smaller or equals to required dimensions
return false;
}*/
if (!$this->Application->ConfigValue('ForceImageMagickResize') && function_exists('imagecreatefromjpeg')) {
// try to resize using GD
$resize_map = Array (
'image/jpeg' => 'imagecreatefromjpeg:imagejpeg:jpg',
'image/gif' => 'imagecreatefromgif:imagegif:gif',
'image/png' => 'imagecreatefrompng:imagepng:png',
'image/bmp' => 'imagecreatefrombmp:imagejpeg:bmp',
'image/x-ms-bmp' => 'imagecreatefrombmp:imagejpeg:bmp',
);
$mime_type = $image_info['mime'];
if (!isset($resize_map[$mime_type])) {
return false;
}
list ($read_function, $write_function, $file_extension) = explode(':', $resize_map[$mime_type]);
// when source image has large dimensions (over 1MB filesize), then 16M is not enough
set_time_limit(0);
ini_set('memory_limit', -1);
$src_image_rs = @$read_function($src_image);
if ($src_image_rs) {
$dst_image_rs = imagecreatetruecolor($params['target_width'], $params['target_height']); // resize target size
$preserve_transparency = ($file_extension == 'gif') || ($file_extension == 'png');
if ($preserve_transparency) {
// preserve transparency of PNG and GIF images
$dst_image_rs = $this->_preserveTransparency($src_image_rs, $dst_image_rs, $image_info[2]);
}
// 1. resize
imagecopyresampled($dst_image_rs, $src_image_rs, 0, 0, 0, 0, $params['target_width'], $params['target_height'], $image_info[0], $image_info[1]);
$watermark_size = 'target';
if (array_key_exists('crop_x', $params) || array_key_exists('crop_y', $params)) {
// 2.1. crop image to given size
$dst_image_rs =& $this->_cropImage($dst_image_rs, $params, $preserve_transparency ? $image_info[2] : false);
$watermark_size = 'max';
} elseif (array_key_exists('fill', $params)) {
// 2.2. fill image margins from resize with given color
$dst_image_rs =& $this->_applyFill($dst_image_rs, $params, $preserve_transparency ? $image_info[2] : false);
$watermark_size = 'max';
}
// 3. apply watermark
$dst_image_rs =& $this->_applyWatermark($dst_image_rs, $params[$watermark_size . '_width'], $params[$watermark_size . '_height'], $params);
if ($write_function == 'imagegif') {
return @$write_function($dst_image_rs, $params['dst_image']);
}
return @$write_function($dst_image_rs, $params['dst_image'], $write_function == 'imagepng' ? 0 : 100);
}
}
else {
// try to resize using ImageMagick
// TODO: implement crop and watermarking using imagemagick
exec('/usr/bin/convert '.$src_image.' -resize '.$params['target_width'].'x'.$params['target_height'].' '.$params['dst_image'], $shell_output, $exec_status);
return $exec_status == 0;
}
return false;
}
/**
* Preserve transparency for GIF and PNG images
*
* @param resource $src_image_rs
* @param resource $dst_image_rs
* @param int $image_type
* @return resource
*/
function _preserveTransparency($src_image_rs, $dst_image_rs, $image_type)
{
$transparent_index = imagecolortransparent($src_image_rs);
// if we have a specific transparent color
if ($transparent_index >= 0) {
// get the original image's transparent color's RGB values
$transparent_color = imagecolorsforindex($src_image_rs, $transparent_index);
// allocate the same color in the new image resource
$transparent_index = imagecolorallocate($dst_image_rs, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
// completely fill the background of the new image with allocated color
imagefill($dst_image_rs, 0, 0, $transparent_index);
// set the background color for new image to transparent
imagecolortransparent($dst_image_rs, $transparent_index);
return $dst_image_rs;
}
// always make a transparent background color for PNGs that don't have one allocated already
if ($image_type == IMAGETYPE_PNG) {
// turn off transparency blending (temporarily)
imagealphablending($dst_image_rs, false);
// create a new transparent color for image
$transparent_color = imagecolorallocatealpha($dst_image_rs, 0, 0, 0, 127);
// completely fill the background of the new image with allocated color
imagefill($dst_image_rs, 0, 0, $transparent_color);
// restore transparency blending
imagesavealpha($dst_image_rs, true);
}
return $dst_image_rs;
}
/**
* Fills margins (if any) of resized are with given color
*
* @param resource $src_image_rs resized image resource
* @param Array $params crop parameters
* @param int $image_type
* @return resource
*/
function &_applyFill(&$src_image_rs, $params, $image_type = false)
{
$x_position = round(($params['max_width'] - $params['target_width']) / 2); // center
$y_position = round(($params['max_height'] - $params['target_height']) / 2); // center
// crop resized image
$fill_image_rs = imagecreatetruecolor($params['max_width'], $params['max_height']);
if ($image_type !== false) {
$fill_image_rs = $this->_preserveTransparency($src_image_rs, $fill_image_rs, $image_type);
}
$fill = $params['fill'];
if (substr($fill, 0, 1) == '#') {
// hexdecimal color
$color = imagecolorallocate($fill_image_rs, hexdec( substr($fill, 1, 2) ), hexdec( substr($fill, 3, 2) ), hexdec( substr($fill, 5, 2) ));
}
else {
// for now we don't support color names, but we will in future
return $src_image_rs;
}
imagefill($fill_image_rs, 0, 0, $color);
imagecopy($fill_image_rs, $src_image_rs, $x_position, $y_position, 0, 0, $params['target_width'], $params['target_height']);
return $fill_image_rs;
}
/**
* Crop given image resource using given params and return resulting image resource
*
* @param resource $src_image_rs resized image resource
* @param Array $params crop parameters
* @param int $image_type
* @return resource
*/
function &_cropImage(&$src_image_rs, $params, $image_type = false)
{
if ($params['crop_x'] == 'c') {
$x_position = round(($params['max_width'] - $params['target_width']) / 2); // center
}
elseif ($params['crop_x'] >= 0) {
$x_position = $params['crop_x']; // margin from left
}
else {
$x_position = $params['target_width'] - ($params['max_width'] - $params['crop_x']); // margin from right
}
if ($params['crop_y'] == 'c') {
$y_position = round(($params['max_height'] - $params['target_height']) / 2); // center
}
elseif ($params['crop_y'] >= 0) {
$y_position = $params['crop_y']; // margin from top
}
else {
$y_position = $params['target_height'] - ($params['max_height'] - $params['crop_y']); // margin from bottom
}
// crop resized image
$crop_image_rs = imagecreatetruecolor($params['max_width'], $params['max_height']);
if ($image_type !== false) {
$crop_image_rs = $this->_preserveTransparency($src_image_rs, $crop_image_rs, $image_type);
}
if (array_key_exists('fill', $params)) {
// fill image margins from resize with given color
$crop_image_rs =& $this->_applyFill($crop_image_rs, $params, $image_type);
}
imagecopy($crop_image_rs, $src_image_rs, $x_position, $y_position, 0, 0, $params['target_width'], $params['target_height']);
return $crop_image_rs;
}
/**
* Apply watermark (transparent PNG image) to given resized image resource
*
* @param resource $src_image_rs
* @param int $max_width
* @param int $max_height
* @param Array $params
* @return resource
*/
function &_applyWatermark(&$src_image_rs, $max_width, $max_height, $params)
{
$watermark_file = array_key_exists('wm_filename', $params) ? $params['wm_filename'] : false;
if (!$watermark_file || !file_exists($watermark_file)) {
// no watermark required, or provided watermark image is missing
return $src_image_rs;
}
$watermark_img_rs = imagecreatefrompng($watermark_file);
list ($watermark_width, $watermark_height) = $this->getImageInfo($watermark_file);
imagealphablending($src_image_rs, true);
if ($params['h_margin'] == 'c') {
$x_position = round($max_width / 2 - $watermark_width / 2); // center
}
elseif ($params['h_margin'] >= 0) {
$x_position = $params['h_margin']; // margin from left
}
else {
$x_position = $max_width - ($watermark_width - $params['h_margin']); // margin from right
}
if ($params['v_margin'] == 'c') {
$y_position = round($max_height / 2 - $watermark_height / 2); // center
}
elseif ($params['v_margin'] >= 0) {
$y_position = $params['v_margin']; // margin from top
}
else {
$y_position = $max_height - ($watermark_height - $params['v_margin']); // margin from bottom
}
imagecopy($src_image_rs, $watermark_img_rs, $x_position, $y_position, 0, 0, $watermark_width, $watermark_height);
return $src_image_rs;
}
/**
* Returns destination image size without actual resizing (useful for <img .../> HTML tag)
*
* @param string $src_image full path to source image (already existing)
* @param int $dst_width destination image width (in pixels)
* @param int $dst_height destination image height (in pixels)
* @param Array $params
* @return Array resized image dimensions (0 - width, 1 - height)
*/
function GetImageDimensions($src_image, $dst_width, $dst_height, $params)
{
$image_info = $this->getImageInfo($src_image);
if (!$image_info) {
return false;
}
$orig_width = $image_info[0];
$orig_height = $image_info[1];
$too_large = is_numeric($dst_width) ? ($orig_width > $dst_width) : false;
$too_large = $too_large || (is_numeric($dst_height) ? ($orig_height > $dst_height) : false);
if ($too_large) {
$width_ratio = $dst_width ? $dst_width / $orig_width : 1;
$height_ratio = $dst_height ? $dst_height / $orig_height : 1;
if (array_key_exists('crop_x', $params) || array_key_exists('crop_y', $params)) {
// resize by smallest inverted radio
$resize_by = $this->_getCropImageMinRatio($image_info, $dst_width, $dst_height);
if ($resize_by === false) {
return Array ($orig_width, $orig_height, false);
}
$ratio = $resize_by == 'width' ? $width_ratio : $height_ratio;
}
else {
$ratio = min($width_ratio, $height_ratio);
}
$width = ceil($orig_width * $ratio);
$height = ceil($orig_height * $ratio);
}
else {
$width = $orig_width;
$height = $orig_height;
}
return Array ($width, $height, $too_large);
}
/**
* Returns ratio type with smaller relation of original size to target size
*
* @param Array $image_info image information from "ImageHelper::getImageInfo"
* @param int $dst_width destination image width (in pixels)
* @param int $dst_height destination image height (in pixels)
* @return Array
*/
function _getCropImageMinRatio($image_info, $dst_width, $dst_height)
{
$width_ratio = $dst_width ? $image_info[0] / $dst_width : 1;
$height_ratio = $dst_height ? $image_info[1] / $dst_height : 1;
$minimal_ratio = min($width_ratio, $height_ratio);
if ($minimal_ratio < 1) {
// ratio is less then 1, image will be enlarged -> don't allow that
return false;
}
return $width_ratio < $height_ratio ? 'width' : 'height';
}
/**
* Returns image dimensions + checks if given file is existing image
*
* @param string $src_image full path to source image (already existing)
* @return mixed
*/
function getImageInfo($src_image)
{
if (!file_exists($src_image)) {
return false;
}
$image_info = @getimagesize($src_image);
if (!$image_info) {
trigger_error('Image <b>'.$src_image.'</b> <span class="debug_error">missing or invalid</span>', E_USER_WARNING);
return false;
}
return $image_info;
}
/**
* Returns maximal image size (width & height) among fields specified
*
* @param kDBItem $object
* @param string $fields
* @param string $format any format, that returns full url (e.g. files_resized:WxH, resize:WxH, full_url, full_urls)
* @return string
*/
function MaxImageSize(&$object, $fields, $format = null)
{
static $cached_sizes = Array ();
$cache_key = $object->getPrefixSpecial().'_'.$object->GetID();
if (!isset($cached_sizes[$cache_key])) {
$images = Array ();
$fields = explode(',', $fields);
foreach ($fields as $field) {
$image_data = $object->GetField($field, $format);
if (!$image_data) {
continue;
}
$images = array_merge($images, explode('|', $image_data));
}
$max_width = 0;
$max_height = 0;
$base_url = rtrim($this->Application->BaseURL(), '/');
foreach ($images as $image_url) {
$image_path = preg_replace('/^'.preg_quote($base_url, '/').'(.*)/', FULL_PATH.'\\1', $image_url);
$image_info = $this->getImageInfo($image_path);
$max_width = max($max_width, $image_info[0]);
$max_height = max($max_height, $image_info[1]);
}
$cached_sizes[$cache_key] = Array ($max_width, $max_height);
}
return $cached_sizes[$cache_key];
}
/**
- * Puts existing item images (from subitem) to virtual fields (in main item)
+ * Puts existing item images (from sub-item) to virtual fields (in main item)
*
- * @param kCatDBItem $object
+ * @param kCatDBItem|kDBItem $object
*/
function LoadItemImages(&$object)
{
if (!$this->_canUseImages($object)) {
return ;
}
$max_image_count = $this->Application->ConfigValue($object->Prefix.'_MaxImageCount');
$sql = 'SELECT *
FROM '.TABLE_PREFIX.'Images
WHERE ResourceId = '.$object->GetDBField('ResourceId').'
ORDER BY Priority DESC
LIMIT 0, ' . (int)$max_image_count;
$item_images = $this->Conn->Query($sql);
$image_counter = 1;
foreach ($item_images as $item_image) {
$image_path = $item_image['ThumbPath'];
if ($item_image['DefaultImg'] == 1 || $item_image['Name'] == 'main') {
// process primary image separately
if ( $object->isField('PrimaryImage') ) {
$object->SetDBField('PrimaryImage', $image_path);
$object->SetOriginalField('PrimaryImage', $image_path);
$object->SetFieldOption('PrimaryImage', 'original_field', $item_image['Name']);
$this->_loadCustomFields($object, $item_image, 0);
}
continue;
}
if (abs($item_image['Priority'])) {
// use Priority as image counter, when specified
$image_counter = abs($item_image['Priority']);
}
if ( $object->isField('Image'.$image_counter) ) {
$object->SetDBField('Image'.$image_counter, $image_path);
$object->SetOriginalField('Image'.$image_counter, $image_path);
$object->SetFieldOption('Image'.$image_counter, 'original_field', $item_image['Name']);
$this->_loadCustomFields($object, $item_image, $image_counter);
}
$image_counter++;
}
}
/**
* Saves newly uploaded images to external image table
*
- * @param kCatDBItem $object
+ * @param kCatDBItem|kDBItem $object
*/
function SaveItemImages(&$object)
{
if (!$this->_canUseImages($object)) {
return ;
}
$table_name = $this->Application->getUnitOption('img', 'TableName');
$max_image_count = $this->Application->getUnitOption($object->Prefix, 'ImageCount'); // $this->Application->ConfigValue($object->Prefix.'_MaxImageCount');
$i = 0;
while ($i < $max_image_count) {
$field = $i ? 'Image'.$i : 'PrimaryImage';
$field_options = $object->GetFieldOptions($field);
$image_src = $object->GetDBField($field);
if ($image_src) {
if (isset($field_options['original_field'])) {
$key_clause = 'Name = '.$this->Conn->qstr($field_options['original_field']).' AND ResourceId = '.$object->GetDBField('ResourceId');
if ($object->GetDBField('Delete'.$field)) {
// if item was cloned, then new filename is in db (not in $image_src)
$sql = 'SELECT ThumbPath
FROM '.$table_name.'
WHERE '.$key_clause;
$image_src = $this->Conn->GetOne($sql);
if (@unlink(FULL_PATH.$image_src)) {
$sql = 'DELETE FROM '.$table_name.'
WHERE '.$key_clause;
$this->Conn->Query($sql);
}
}
else {
// image record found -> update
$fields_hash = Array (
'ThumbPath' => $image_src,
);
$this->_saveCustomFields($object, $fields_hash, $i);
$this->Conn->doUpdate($fields_hash, $table_name, $key_clause);
}
}
else {
// image record not found -> create
$fields_hash = Array (
'ResourceId' => $object->GetDBField('ResourceId'),
'Name' => $field,
'AltName' => $field,
'Enabled' => STATUS_ACTIVE,
'DefaultImg' => $i ? 0 : 1, // first image is primary, others not primary
'ThumbPath' => $image_src,
'Priority' => ($i == 0)? 0 : $i * (-1),
);
$this->_saveCustomFields($object, $fields_hash, $i);
$this->Conn->doInsert($fields_hash, $table_name);
$field_options['original_field'] = $field;
$object->SetFieldOptions($field, $field_options);
}
}
$i++;
}
}
/**
* Adds ability to load custom fields along with main image field
*
- * @param kCatDBItem $object
+ * @param kCatDBItem|kDBItem $object
* @param Array $fields_hash
* @param int $counter 0 - primary image, other number - additional image number
*/
function _loadCustomFields(&$object, $fields_hash, $counter)
{
$field_name = $counter ? 'Image' . $counter . 'Alt' : 'PrimaryImageAlt';
$object->SetDBField($field_name, (string)$fields_hash['AltName']);
}
/**
* Adds ability to save custom field along with main image save
*
- * @param kCatDBItem $object
+ * @param kCatDBItem|kDBItem $object
* @param Array $fields_hash
* @param int $counter 0 - primary image, other number - additional image number
*/
function _saveCustomFields(&$object, &$fields_hash, $counter)
{
$field_name = $counter ? 'Image' . $counter . 'Alt' : 'PrimaryImageAlt';
$fields_hash['AltName'] = (string)$object->GetDBField($field_name);
}
/**
* Checks, that item can use image upload capabilities
*
- * @param kCatDBItem $object
+ * @param kCatDBItem|kDBItem $object
* @return bool
*/
function _canUseImages(&$object)
{
$prefix = $object->Prefix == 'p' ? 'img' : $object->Prefix . '-img';
return $this->Application->prefixRegistred($prefix);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/rating_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/rating_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/rating_helper.php (revision 14628)
@@ -1,195 +1,197 @@
<?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 RatingHelper extends kHelper {
/**
* One star width/height in pixels
*
* @var int
*/
var $ratingUnitWidth = 25;
var $ratingSmallUnitWidth = 20;
/**
* Maximal star count
*
* @var int
*/
var $ratingMaximal = 5;
var $_phrases = Array (
'current_rating' => 'lu_CurrentRating',
'vote_title' => 'lu_VoteTitle',
'vote_count' => 'lu_VoteCount',
'invalid_rating' => 'lu_InvalidRating',
'already_voted' => 'lu_AlreadyVoted',
'thanks_for_voting' => 'lu_ThanksForVoting',
);
/**
* Draws rating bar for a given category item
*
* @param kDBItem $object
* @param bool $show_div
* @param string $additional_msg
+ * @param string $additional_style
* @return string
+ * @access public
*/
- function ratingBar(&$object, $show_div = true, $additional_msg = '', $additional_style = '')
+ public function ratingBar(&$object, $show_div = true, $additional_msg = '', $additional_style = '')
{
$perm_prefix = $this->Application->getUnitOption($object->Prefix, 'PermItemPrefix');
$static = !$this->Application->CheckPermission($perm_prefix . '.RATE', 0, $object->GetDBField('CategoryId'));
$total_votes = $object->GetDBField('CachedVotesQty');
$total_rating = $object->GetDBField('CachedRating') * $total_votes;
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$config_mapping = $this->Application->getUnitOption($object->Prefix, 'ConfigMapping');
- $review_settings = $config_mapping['RatingDelayValue'].':'.$config_mapping['RatingDelayInterval'];
+ $review_settings = $config_mapping['RatingDelayValue'] . ':' . $config_mapping['RatingDelayInterval'];
$spam_helper->InitHelper($object->GetDBField('ResourceId'), 'Rating', $review_settings, $object->GetCol('ResourceId'));
$user_voted = $spam_helper->InSpamControl();
// now draw the rating bar
- $unit_selected_width = $additional_style? $this->ratingSmallUnitWidth : $this->ratingUnitWidth;
+ $unit_selected_width = $additional_style ? $this->ratingSmallUnitWidth : $this->ratingUnitWidth;
$rating_width = $total_votes ? @number_format($total_rating / $total_votes, 2) * $unit_selected_width : 0;
$rating1 = $total_votes ? @number_format($total_rating / $total_votes, 1) : 0;
$rating2 = $total_votes ? @number_format($total_rating / $total_votes, 2) : 0;
$rater = '<span class="inline-rating">
<ul class="star-rating '.$additional_style.'" style="width: ' . $unit_selected_width * $this->ratingMaximal . 'px;">
- <li class="current-rating" style="width: ' . $rating_width . 'px;">' . $this->_replaceInPhrase('current_rating', Array ('<strong>' . $rating2 . '</strong>', $this->ratingMaximal)) . '</li>'."\n";;
+ <li class="current-rating" style="width: ' . $rating_width . 'px;">' . $this->_replaceInPhrase('current_rating', Array ('<strong>' . $rating2 . '</strong>', $this->ratingMaximal)) . '</li>'."\n";
- if (!$static && !$user_voted) {
+ if ( !$static && !$user_voted ) {
// allow to set rating when not static and user not voted before
for ($ncount = 1; $ncount <= $this->ratingMaximal; $ncount++) {
- $rater .= '<li><a href="#vote-' . $ncount . '" onclick="aRatingManager.makeVote(' . $ncount . ', \'' . $object->Prefix . '\', ' . $object->GetID() . ', \''.$additional_style.'\'); return false;" title="' . $this->_replaceInPhrase('vote_title', Array ($ncount, $this->ratingMaximal)) . '" class="r' . $ncount . '-unit rater" rel="nofollow">' . $ncount . '</a></li>'."\n";
+ $rater .= '<li><a href="#vote-' . $ncount . '" onclick="aRatingManager.makeVote(' . $ncount . ', \'' . $object->Prefix . '\', ' . $object->GetID() . ', \'' . $additional_style . '\'); return false;" title="' . $this->_replaceInPhrase('vote_title', Array ($ncount, $this->ratingMaximal)) . '" class="r' . $ncount . '-unit rater" rel="nofollow">' . $ncount . '</a></li>' . "\n";
}
}
$msg_class = Array ();
- if ($static) {
+ if ( $static ) {
$msg_class[] = 'static';
}
- if ($user_voted) {
+ if ( $user_voted ) {
$msg_class[] = 'voted';
}
$rater .= ' </ul></span>';
// this part is disabled for now, will be addressed once properly review
// $rater .= ' <p class="' . implode(' ', $msg_class) . '">' .
- $this->_replaceInPhrase('vote_title', Array('<strong>'.$rating1.'</strong>', $this->ratingMaximal)) . ' ('. $this->_replaceInPhrase('vote_count', Array($total_votes)) . ') </p>';
+ $this->_replaceInPhrase('vote_title', Array ('<strong>' . $rating1 . '</strong>', $this->ratingMaximal)) . ' (' . $this->_replaceInPhrase('vote_count', Array ($total_votes)) . ') </p>';
- $rater .= '&nbsp;<span class="' . implode(' ', $msg_class) . '">'.$additional_msg.'</span>';
+ $rater .= '&nbsp;<span class="' . implode(' ', $msg_class) . '">' . $additional_msg . '</span>';
- if ($show_div) {
+ if ( $show_div ) {
// adds div around rating stars (when drawing rating first time)
$rater = '<div class="inline-rating" id="page_rating_' . $object->GetID() . '">' . $rater . '</div>';
}
return $rater;
}
/**
* Saves user's vote, when allowed
*
* @param kDBItem $object
* @return string
*/
function makeVote(&$object)
{
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$config_mapping = $this->Application->getUnitOption($object->Prefix, 'ConfigMapping');
$review_settings = $config_mapping['RatingDelayValue'].':'.$config_mapping['RatingDelayInterval'];
$spam_helper->InitHelper($object->GetDBField('ResourceId'), 'Rating', $review_settings, $object->GetCol('ResourceId'));
if (!$object->isLoaded() || $spam_helper->InSpamControl()) {
return '@err:' . $this->_replaceInPhrase('already_voted');
}
$perm_prefix = $this->Application->getUnitOption($object->Prefix, 'PermItemPrefix');
$can_rate = $this->Application->CheckPermission($perm_prefix . '.RATE', 0, $object->GetDBField('CategoryId'));
$rating = (int)$this->Application->GetVar('rating'); // not numeric rating is from GoogleBot :(
$additional_style = $this->Application->GetVar('size');
if (($rating <= 0) || ($rating > $this->ratingMaximal) || !$can_rate) {
return '@err:' . $this->_replaceInPhrase('invalid_rating');
}
// save current rating
$fields_hash = Array (
'ItemId' => $object->GetID(),
'RatingValue' => $rating,
'IPAddress' => $_SERVER['REMOTE_ADDR'],
'CreatedOn' => adodb_mktime(),
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX.'ItemRating');
// recalculate average rating
$votes_count = $object->GetDBField('CachedVotesQty');
$avg_rating = $object->GetDBField('CachedRating');
$avg_rating = round((($votes_count * $avg_rating) + $rating) / ($votes_count + 1), 2);
$object->SetDBField('CachedRating', "$avg_rating");
$object->Update();
$sql = 'UPDATE '.$object->TableName.'
SET CachedVotesQty = CachedVotesQty + 1
WHERE '.$object->IDField.' = '.$object->GetID();
$this->Conn->Query($sql);
$object->SetDBField('CachedVotesQty', $object->GetDBField('CachedVotesQty') + 1); // for using in template
// prevent user from voting too quickly
$spam_helper->AddToSpamControl();
return $this->ratingBar($object, false, '<span class="thanks">' . $this->_replaceInPhrase('thanks_for_voting') . '</span>', $additional_style);
}
/*function purgeVotes()
{
$expired = adodb_mktime() - 86400 * $this->Application->ConfigValue('Timeout_Rating'); // 3600
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'ItemRating
WHERE CreatedOn < ' . $expired;
$this->Conn->Query($sql);
}*/
/**
* Performs sprintf on phrase translation using given variables
*
* @param string $phrase
* @param Array $arguments
* @return string
*/
function _replaceInPhrase($phrase, $arguments = Array ())
{
$value = $this->Application->Phrase($this->_phrases[$phrase]);
if ($arguments) {
return vsprintf($value, $arguments);
}
return $value;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/country_states_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/country_states_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/country_states_helper.php (revision 14628)
@@ -1,196 +1,205 @@
<?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 kCountryStatesHelper extends kHelper
{
/**
* Returns countries, that have states
*
* @return Array
*/
function getCountriesWithStates()
{
static $cache = null;
if (!isset($cache)) {
$table_name = $this->Application->getUnitOption('country-state', 'TableName');
$sql = 'SELECT DISTINCT cname.IsoCode, cid.StateCountryId
FROM ' . $table_name . ' cid
JOIN ' . $table_name . ' cname ON cname.CountryStateId = cid.StateCountryId
WHERE cid.StateCountryId IS NOT NULL';
$cache = $this->Conn->GetCol($sql, 'StateCountryId');
}
return $cache;
}
/**
* Checks, that country with given 3symbol ISO code has states
*
* @param string $country_code
* @return bool
*/
function CountryHasStates($country_code)
{
return $country_code ? in_array($country_code, $this->getCountriesWithStates()) : false;
}
/**
* Prepares states dropdown based on country selected
*
* @param kEvent $event
* @param string $state_field
* @param string $country_field
*/
function PopulateStates(&$event, $state_field, $country_field)
{
static $cache = Array ();
$object =& $event->getObject();
/* @var $object kDBItem */
$country_iso = $object->GetDBField($country_field);
if (!$country_iso) {
return ;
}
if (!array_key_exists($country_iso, $cache)) {
$country_id = $this->getCountryStateId($country_iso, DESTINATION_TYPE_COUNTRY);
if (!$country_id) {
return ;
}
// don't use GetVar('m_lang') since it's always equals to default language on editing form in admin
$current_language = $this->Application->Phrases->LanguageId;
$primary_language = $this->Application->GetDefaultLanguageId();
$sql = 'SELECT IF(l' . $current_language . '_Name = "", l' . $primary_language . '_Name, l' . $current_language . '_Name) AS Name, IsoCode
FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . '
WHERE (Type = ' . DESTINATION_TYPE_STATE . ') AND (StateCountryId = ' . $country_id . ')
ORDER BY Name ASC';
$cache[$country_iso] = $this->Conn->GetCol($sql, 'IsoCode');
}
$field_options = $object->GetFieldOptions($state_field);
$field_options['options'] = $cache[$country_iso];
$field_options['options'][''] = '';
$object->SetFieldOptions($state_field, $field_options);
}
/**
* Returns valid state ISO code for state name and country code passed
*
* @param string $state_name
* @param string $country_iso
* @return string
*/
function getStateIso($state_name, $country_iso)
{
if (!$this->CountryHasStates($country_iso)) {
return $state_name;
}
$table_name = $this->Application->getUnitOption('country-state', 'TableName');
$country_id = $this->getCountryStateId($country_iso, DESTINATION_TYPE_COUNTRY);
// don't use GetVar('m_lang') since it's always equals to default language on editing form in admin
$current_language = $this->Application->Phrases->LanguageId;
$primary_language = $this->Application->GetDefaultLanguageId();
$sql = 'SELECT IsoCode
FROM ' . $table_name . '
WHERE (Type = ' . DESTINATION_TYPE_STATE . ') AND (StateCountryId = %1$s) AND
(
(IsoCode = %2$s) OR (UPPER(l%3$s_Name) = %2$s) OR (UPPER(l%4$s_Name) = %2$s)
)';
$state_name = trim( mb_strtoupper($state_name) );
$sql = sprintf($sql, $country_id, $this->Conn->qstr($state_name), $current_language, $primary_language);
return $this->Conn->GetOne($sql);
}
+ /**
+ * Checks, that entered state matches entered country
+ *
+ * @param kEvent $event
+ * @param string $state_field
+ * @param string $country_field
+ * @param bool $auto_required
+ * @return void
+ */
function CheckStateField(&$event, $state_field, $country_field, $auto_required = true)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$country_iso = $object->GetDBField($country_field);
- if ($auto_required) {
+ if ( $auto_required ) {
$object->setRequired($state_field, $this->CountryHasStates($country_iso));
}
$state = $object->GetDBField($state_field);
- if ($country_iso && $state) {
+ if ( $country_iso && $state ) {
$state_iso = $this->getStateIso($state, $country_iso);
- if ($state_iso !== false) {
+ if ( $state_iso !== false ) {
// replace state name with it's ISO code
$object->SetDBField($state_field, $state_iso);
}
else {
// state not found by name -> report error
$object->SetError($state_field, 'invalid_state', 'la_invalid_state');
}
}
}
/**
* Returns country/state id based on given iso code and it's type
*
* @param string $iso_code
* @param int $type
* @return int
*/
function getCountryStateId($iso_code, $type)
{
$sql = 'SELECT ' . $this->Application->getUnitOption('country-state', 'IDField') . '
FROM ' . $this->Application->getUnitOption('country-state', 'TableName') . '
WHERE (Type = ' . $type . ') AND (IsoCode = ' . $this->Conn->qstr($iso_code) . ')';
return (int)$this->Conn->GetOne($sql);
}
/**
* Returns 3 symbols ISO code from 2 symbols ISO code or otherwise, when $from_short parameter is used
*
* @param string $iso_code
* @param bool $from_short
* @return string
*/
function getCountryIso($iso_code, $from_short = false)
{
if ($from_short) {
$sql = 'SELECT IsoCode
FROM ' . TABLE_PREFIX . 'CountryStates
WHERE ShortIsoCode = ' . $this->Conn->qstr($iso_code) . ' AND `Type` = ' . DESTINATION_TYPE_COUNTRY;
}
else {
$sql = 'SELECT ShortIsoCode
FROM ' . TABLE_PREFIX . 'CountryStates
WHERE IsoCode = ' . $this->Conn->qstr($iso_code) . ' AND `Type` = ' . DESTINATION_TYPE_COUNTRY;
}
return $this->Conn->GetOne($sql);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/count_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/count_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/count_helper.php (revision 14628)
@@ -1,288 +1,290 @@
<?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 kCountHelper extends kHelper {
/**
* 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 parmeters)
* @param bool $multiple_results
* @return mixed
*/
function getCounter($name, $params = Array (), $query_name = null, $multiple_results = false)
{
$this->resetExpiredCounters();
$clone_counter = false;
$query_name = isset($query_name) ? $query_name : rtrim($name.'-'.implode('-', array_values($params)), '-');
$sql = 'SELECT *
FROM '.TABLE_PREFIX.'Counters
WHERE Name = '.$this->Conn->qstr($query_name);
$count_data = $this->Conn->GetRow($sql);
if (!$count_data && ($query_name != $name)) {
// cloned record not found -> search for origial one
$sql = 'SELECT *
FROM '.TABLE_PREFIX.'Counters
WHERE Name = '.$this->Conn->qstr($name);
$count_data = $this->Conn->GetRow($sql);
$clone_counter = true;
}
if (is_null($count_data['CountValue'])) {
$params['PREFIX'] = TABLE_PREFIX;
$count_sql = $count_data['CountQuery'];
foreach ($params as $replace_from => $replace_to) {
$count_sql = str_replace('<%'.$replace_from.'%>', $replace_to, $count_sql);
}
if ($multiple_results) {
$count_data['CountValue'] = serialize($this->Conn->GetCol($count_sql));
}
else {
$count_data['CountValue'] = $this->Conn->GetOne($count_sql);
}
if ($clone_counter && !$count_data['IsClone']) {
// don't clone clones
$count_data['CounterId'] = 0;
$count_data['Name'] = $query_name;
$count_data['IsClone'] = 1;
}
$count_data['LastCounted'] = adodb_mktime();
$this->Conn->doInsert($count_data, TABLE_PREFIX.'Counters', 'REPLACE');
}
return $multiple_results ? unserialize($count_data['CountValue']) : $count_data['CountValue'];
}
/**
- * Resets counter, whitch are affected by one of specified tables
+ * 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
*/
- function resetCounters($tables)
+ public function resetCounters($tables)
{
$tables = explode(',', $tables);
$prefix_length = strlen(TABLE_PREFIX);
foreach ($tables as $index => $table) {
// remove prefixes
if (substr($table, 0, $prefix_length) == TABLE_PREFIX) {
$table = substr($table, $prefix_length);
}
$tables[$index] = '(TablesAffected LIKE "%|'.$table.'|%")';
}
$sql = 'UPDATE '.TABLE_PREFIX.'Counters
SET CountValue = NULL
WHERE '.implode(' OR ', $tables);
$this->Conn->Query($sql);
}
function resetExpiredCounters()
{
static $reset = false;
if (!$reset) {
// reset expired counts
$sql = 'UPDATE '.TABLE_PREFIX.'Counters
SET CountValue = NULL
WHERE (LifeTime > 0) AND (LastCounted < '.adodb_mktime().' - LifeTime)';
$this->Conn->Query($sql);
$reset = true;
}
}
/**
* Counts items (of specific type) in category & subcategories
*
* @param string $prefix
* @param Array $params
* @param mixed $count_sql
* @return int
*/
function CategoryItemCount($prefix, $params, $count_sql = null)
{
if (!$this->Application->findModule('Var', $prefix)) {
// this is not module item
return $this->Application->ProcessParsedTag($prefix, 'CategoryItemCount', $params);
}
// 1. get root category for counting
$category_id = isset($params['cat_id']) ? $params['cat_id'] : false;
if (!is_numeric($category_id)) {
$category_id = $this->Application->GetVar('m_cat_id');
}
if (!isset($count_sql)) {
$count_sql = 'COUNT(*)';
}
$where_clauses = Array ();
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
// 1. count category items
$sql = 'SELECT '.$count_sql.'
FROM '.$table_name.' item_table
INNER JOIN '.TABLE_PREFIX.'CategoryItems ci ON (ci.ItemResourceId = item_table.ResourceId)
INNER JOIN '.TABLE_PREFIX.'Category c ON (ci.CategoryId = c.CategoryId)';
// 2. count items from subcategories
if ($category_id > 0) {
// get subcategories of required category
$tmp_sql = 'SELECT TreeLeft, TreeRight
FROM '.TABLE_PREFIX.'Category
WHERE CategoryId = '.$category_id;
$tree_info = $this->Conn->GetRow($tmp_sql);
$where_clauses[] = 'c.TreeLeft BETWEEN '.$tree_info['TreeLeft'].' AND '.$tree_info['TreeRight'];
}
$where_clauses[] = 'item_table.Status = '.STATUS_ACTIVE;
if (isset($params['today']) && $params['today']) {
$today = adodb_mktime(0,0,0, adodb_date('m'), adodb_date('d'), adodb_date('Y'));
$where_clauses[] = 'item_table.CreatedOn >= '.$today;
}
$sql .= ' WHERE ('.implode(') AND (', $where_clauses).')';
return (int)$this->Conn->GetOne($sql);
}
/**
* Counts items (of specific type) from all categories
*
* @param string $prefix
* @param bool $today
* @return int
*/
function ItemCount($prefix, $today = false, $count_sql = null)
{
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
if (!isset($count_sql)) {
$count_sql = 'COUNT(*)';
}
$sql = 'SELECT '.$count_sql.'
FROM '.$table_name.' item_table
INNER JOIN '.TABLE_PREFIX.'CategoryItems ci ON ci.ItemResourceId = item_table.ResourceId
INNER JOIN '.TABLE_PREFIX.'Category c ON c.CategoryId = ci.CategoryId
INNER JOIN '.TABLE_PREFIX.'PermCache perm_cache ON ci.CategoryId = perm_cache.CategoryId';
list ($view_perm, $view_filter) = $this->GetPermissionClause($prefix, 'perm_cache');
$where_clauses = Array (
$view_filter, 'perm_cache.PermId = '.$view_perm, 'ci.PrimaryCat = 1', 'c.Status = '.STATUS_ACTIVE,
);
if ($today) {
$today_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y'));
$where_clauses[] = 'item_table.CreatedOn >= '.$today_date;
}
$sql .= ' WHERE ('.implode(') AND (', $where_clauses).')';
return (int)$this->Conn->GetOne($sql);
}
/**
* Returns categories count in system
*
* @param bool $today
* @return int
*/
function CategoryCount($today = false)
{
$cache_key = 'category_count[%CSerial%]';
if ($today) {
$today_date = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), adodb_date('Y'));
$cache_key .= ':date=' . $today_date;
}
$count = $this->Application->getCache($cache_key);
if ($count === false) {
$sql = 'SELECT COUNT(*)
FROM ' . $this->Application->getUnitOption('c', 'TableName') . ' c
INNER JOIN ' . TABLE_PREFIX . 'PermCache perm_cache ON c.CategoryId = perm_cache.CategoryId';
list ($view_perm, $view_filter) = $this->GetPermissionClause('c', 'perm_cache');
$where_clauses = Array (
$view_filter,
'perm_cache.PermId = ' . $view_perm,
'c.Status = ' . STATUS_ACTIVE,
);
if ($today) {
$where_clauses[] = 'c.CreatedOn >= ' . $today_date;
}
$sql .= ' WHERE ('.implode(') AND (', $where_clauses).')';
$count = $this->Conn->GetOne($sql);
if ($count !== false) {
$this->Application->setCache($cache_key, $count);
}
}
return $count;
}
/**
* Returns permission limitation clause for category item lists
*
* @param string $prefix
* @param string $table_alias
* @return Array
*/
function GetPermissionClause($prefix, $table_alias)
{
$permissions_ids = $this->Application->getCache(__CLASS__ . '::' . __FUNCTION__);
if ($permissions_ids === false) {
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT PermissionConfigId, PermissionName
FROM '.TABLE_PREFIX.'PermissionConfig
WHERE PermissionName LIKE "%.VIEW"';
$permissions_ids = $this->Conn->GetCol($sql, 'PermissionName');
$this->Application->setCache(__CLASS__ . '::' . __FUNCTION__, $permissions_ids);
}
$permission_prefix = $this->Application->getUnitOption($prefix, 'PermItemPrefix');
$view_perm = $permissions_ids[$permission_prefix . '.VIEW'];
$groups = explode(',', $this->Application->RecallVar('UserGroups'));
foreach ($groups as $group) {
$view_filters[] = 'FIND_IN_SET('.$group.', '.$table_alias.'.acl)';
}
$view_filter = implode(' OR ', $view_filters);
return Array ($view_perm, $view_filter);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/mailbox_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/mailbox_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/mailbox_helper.php (revision 14628)
@@ -1,493 +1,495 @@
<?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 MailboxHelper extends kHelper {
var $headers = Array ();
var $parsedMessage = Array ();
/**
* Maximal megabytes of data to process
*
* @var int
*/
var $maxMegabytes = 2;
/**
* Maximal message count to process
*
* @var int
*/
var $maxMessages = 50;
/**
* Reads mailbox and gives messages to processing callback
*
* @param Array $connection_info
* @param Array $verify_callback
* @param Array $process_callback
* @param Array $callback_params
* @param bool $include_attachment_contents
* @return string
*/
function process($connection_info, $verify_callback, $process_callback, $callback_params = Array (), $include_attachment_contents = true)
{
$pop3_helper =& $this->Application->makeClass('POP3Helper', Array ($connection_info));
/* @var $pop3_helper POP3Helper */
$connection_status = $pop3_helper->initMailbox();
if (is_string($connection_status)) {
return $connection_status;
}
if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML('Reading MAILBOX: ' . $connection_info['username']);
}
// Figure out if all messages are huge
$only_big_messages = true;
$max_message_size = $this->maxMegabytes * (1024 * 1024);
foreach ($pop3_helper->messageSizes as $message_size) {
if (($message_size <= $max_message_size) && ($max_message_size > 0)) {
$only_big_messages = false;
break;
}
}
$count = $total_size = 0;
foreach ($pop3_helper->messageSizes as $message_number => $message_size) {
// Too many messages?
if (($count++ > $this->maxMessages) && ($this->maxMessages > 0)) {
break;
}
// Message too big?
if (!$only_big_messages && ($message_size > $max_message_size) && ($max_message_size > 0)) {
$this->_displayLogMessage('message <strong>#' . $message_number . '</strong> too big, skipped');
continue;
}
// Processed enough for today?
if (($total_size > $max_message_size) && ($max_message_size > 0)) {
break;
}
$total_size += $message_size;
$pop3_helper->getEmail($message_number, $message_source);
$processed = $this->normalize($message_source, $verify_callback, $process_callback, $callback_params, $include_attachment_contents);
if ($processed) {
// delete message from server immediatly after retrieving & processing
$pop3_helper->deleteEmail($message_number);
$this->_displayLogMessage('message <strong>#' . $message_number . '</strong>: processed');
}
else {
$this->_displayLogMessage('message <strong>#' . $message_number . '</strong>: skipped');
}
}
$pop3_helper->close();
return 'success';
}
/**
* Displays log message
*
* @param string $text
*/
function _displayLogMessage($text)
{
if (defined('DEBUG_MODE') && DEBUG_MODE && $this->Application->isDebugMode()) {
$this->Application->Debugger->appendHTML($text);
}
}
/**
* Takes an RFC822 formatted date, returns a unix timestamp (allowing for zone)
*
* @param string $rfcdate
* @return int
*/
function rfcToTime($rfcdate)
{
$date = strtotime($rfcdate);
if ($date == -1) {
return false;
}
return $date;
}
/**
* Gets recipients from all possible headers
*
* @return string
*/
function getRecipients()
{
$ret = '';
// headers that could contain recipients
$recipient_headers = Array (
'to', 'cc', 'envelope-to', 'resent-to', 'delivered-to',
'apparently-to', 'envelope-to', 'x-envelope-to', 'received',
);
foreach ($recipient_headers as $recipient_header) {
if (!array_key_exists($recipient_header, $this->headers)) {
continue;
}
if (!is_array($this->headers["$recipient_header"])) {
$ret .= ' ' . $this->headers["$recipient_header"];
} else {
$ret .= ' ' . implode(' ', $this->headers["$recipient_header"]);
}
}
return $ret;
}
/**
* "Flattens" the multi-demensinal headers array into a single dimension one
*
* @param Array $input
* @param string $add
* @return Array
*/
function flattenHeadersArray($input, $add = '')
{
$output = Array ();
foreach ($input as $key => $value) {
if (!empty($add)) {
$newkey = ucfirst( strtolower($add) );
} elseif (is_numeric($key)) {
$newkey = '';
} else {
$newkey = ucfirst( strtolower($key) );
}
if (is_array($value)) {
$output = array_merge($output, $this->flattenHeadersArray($value, $newkey));
} else {
$output[] = (!empty($newkey) ? $newkey . ': ' : '') . $value;
}
}
return $output;
}
/**
* Processes given message using given callbacks
*
* @param string $message
* @param Array $verify_callback
* @param Array $process_callback
+ * @param Array $callback_params
* @param bool $include_attachment_contents
* @return bool
+ * @access protected
*/
- function normalize($message, $verify_callback, $process_callback, $callback_params, $include_attachment_contents = true)
+ protected function normalize($message, $verify_callback, $process_callback, $callback_params, $include_attachment_contents = true)
{
// Decode message
$this->decodeMime($message, $include_attachment_contents);
// Init vars; $good will hold all the correct infomation from now on
$good = Array ();
// trim() some stuff now instead of later
$this->headers['from'] = trim($this->headers['from']);
$this->headers['to'] = trim($this->headers['to']);
$this->headers['cc'] = array_key_exists('cc', $this->headers) ? trim($this->headers['cc']) : '';
$this->headers['x-forward-to'] = array_key_exists('x-forward-to', $this->headers) ? $this->headers['x-forward-to'] : '';
$this->headers['subject'] = trim($this->headers['subject']);
$this->headers['received'] = is_array($this->headers['received']) ? $this->headers['received'] : Array ($this->headers['received']);
if (array_key_exists('return-path', $this->headers) && is_array($this->headers['return-path'])) {
$this->headers['return-path'] = implode(' ', $this->flattenHeadersArray($this->headers['return-path']));
}
// Create our own message-ID if it's missing
$message_id = array_key_exists('message-id', $this->headers) ? trim($this->headers['message-id']) : '';
$good['emailid'] = $message_id ? $message_id : md5($message) . "@in-portal";
// Stops us looping in stupid conversations with other mail software
if (isset($this->headers['x-loop-detect']) && $this->headers['x-loop-detect'] > 2) {
return false;
}
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
// Get the return address
$return_path = '';
if (array_key_exists('return-path', $this->headers)) {
$return_path = $esender->ExtractRecipientEmail($this->headers['return-path']);
}
if (!$return_path) {
if (array_key_exists('reply-to', $this->headers)) {
$return_path = $esender->ExtractRecipientEmail( $this->headers['reply-to'] );
}
else {
$return_path = $esender->ExtractRecipientEmail( $this->headers['from'] );
}
}
// Get the sender's name & email
$good['fromemail'] = $esender->ExtractRecipientEmail($this->headers['from']);
$good['fromname'] = $esender->ExtractRecipientName($this->headers['from'], $good['fromemail']);
// Get the list of recipients
if (!$verify_callback[0]->$verify_callback[1]($callback_params)) {
// error: mail is propably spam
return false;
}
// Handle the subject
$good['subject'] = $this->headers['subject'];
// Priorities rock
$good['priority'] = array_key_exists('x-priority', $this->headers) ? (int)$this->headers['x-priority'] : 0;
switch ($good['priority']) {
case 1: case 5: break;
default:
$good['priority'] = 3;
}
// If we have attachments it's about time we tell the user about it
if (array_key_exists('attachments', $this->parsedMessage) && is_array($this->parsedMessage['attachments'])) {
$good['attach'] = count( $this->parsedMessage['attachments'] );
} else {
$good['attach'] = 0;
}
// prepare message text (for replies, etc)
if (isset($this->parsedMessage['text'][0]) && trim($this->parsedMessage['text'][0]['body']) != '') {
$message_body = trim($this->parsedMessage['text'][0]['body']);
$message_type = 'text';
} elseif (isset($this->parsedMessage['html']) && trim($this->parsedMessage['html'][0]['body']) != '') {
$message_body = trim($this->parsedMessage['html'][0]['body']);
$message_type = 'html';
} else {
$message_body = '[no message]';
$message_type = 'text';
}
// remove scripts
$message_body = preg_replace("/<script[^>]*>[^<]+<\/script[^>]*>/is", '', $message_body);
$message_body = preg_replace("/<iframe[^>]*>[^<]*<\/iframe[^>]*>/is", '', $message_body);
if ($message_type == 'html') {
$message_body = $esender->ConvertToText($message_body);
}
$mime_decode_helper =& $this->Application->recallObject('MimeDecodeHelper');
/* @var $mime_decode_helper MimeDecodeHelper */
// convert to site encoding
$message_charset = $this->parsedMessage[$message_type][0]['charset'];
if ($message_charset) {
$good['message'] = $mime_decode_helper->convertEncoding($message_charset, $message_body);
}
if (array_key_exists('delivery-date', $this->headers)) {
// We found the Delivery-Date header (and it's not too far in the future)
$dateline = $this->rfcToTime($this->headers['delivery-date']);
if ($dateline > TIMENOW + 86400) {
unset($dateline);
}
}
// We found the latest date from the received headers
$received_timestamp = $this->headers['received'][0];
$dateline = $this->rfcToTime(trim( substr($received_timestamp, strrpos($received_timestamp, ';') + 1) ));
if ($dateline == $this->rfcToTime(0)) {
unset($dateline);
}
if (!isset($dateline)) {
$dateline = TIMENOW;
}
// save collected data to database
$fields_hash = Array (
'DeliveryDate' => $dateline, // date, when SMTP server received the message
'ReceivedDate' => TIMENOW, // date, when message was retrieved from POP3 server
'CreatedOn' => $this->rfcToTime($this->headers['date']), // date, when created on sender's computer
'ReturnPath' => $return_path,
'FromEmail' => $good['fromemail'],
'FromName' => $good['fromname'],
'To' => $this->headers['to'],
'Subject' => $good['subject'],
'Message' => $good['message'],
'MessageType' => $message_type,
'AttachmentCount' => $good['attach'],
'MessageId' => $good['emailid'],
'Source' => $message,
'Priority' => $good['priority'],
'Size' => strlen($message),
);
return $process_callback[0]->$process_callback[1]($callback_params, $fields_hash);
}
/**
* Function that decodes the MIME message and creates the $this->headers and $this->parsedMessage data arrays
*
* @param string $message
* @param bool $include_attachments
*
*/
function decodeMime($message, $include_attachments = true)
{
$message = preg_replace("/\r?\n/", "\r\n", trim($message));
$mime_decode_helper =& $this->Application->recallObject('MimeDecodeHelper');
/* @var $mime_decode_helper MimeDecodeHelper */
// 1. separate headers from bodies
$mime_decode_helper->InitHelper($message);
$decoded_message = $mime_decode_helper->decode(true, true, true);
// 2. extract attachments
$this->parsedMessage = Array (); // ! reset value
$this->parseOutput($decoded_message, $this->parsedMessage, $include_attachments);
// 3. add "other" attachments (text part, that is not maked as attachment)
if (array_key_exists('text', $this->parsedMessage) && count($this->parsedMessage['text']) > 1) {
for ($attach = 1; $attach < count($this->parsedMessage['text']); $attach++) {
$this->parsedMessage['attachments'][] = Array (
'data' => $this->parsedMessage['text']["$attach"]['body'],
);
}
}
$this->headers = $this->parsedMessage['headers']; // ! reset value
if (empty($decoded_message->ctype_parameters['boundary'])) {
// when no boundary, then assume all message is it's text
$this->parsedMessage['text'][0]['body'] = $decoded_message->body;
}
}
/**
* Returns content-id's from inline attachments in message
*
* @return Array
*/
function getContentIds()
{
$cids = Array();
if (array_key_exists('attachments', $this->parsedMessage) && is_array($this->parsedMessage['attachments'])) {
foreach ($this->parsedMessage['attachments'] as $attachnum => $attachment) {
if (!isset($attachment['headers']['content-id'])) {
continue;
}
$cid = $attachment['headers']['content-id'];
if (substr($cid, 0, 1) == '<' && substr($cid, -1) == '>') {
$cid = substr($cid, 1, -1);
}
$cids["$attachnum"] = $cid;
}
}
return $cids;
}
/**
* Get more detailed information about attachments
*
* @param stdClass $decoded parsed headers & body as object
* @param Array $parts parsed parts
* @param bool $include_attachments
*/
function parseOutput(&$decoded, &$parts, $include_attachments = true)
{
$ctype = strtolower($decoded->ctype_primary . '/' . $decoded->ctype_secondary);
// don't parse attached messages recursevely
if (!empty($decoded->parts) && $ctype != 'message/rfc822') {
for ($i = 0; $i < count($decoded->parts); $i++) {
$this->parseOutput($decoded->parts["$i"], $parts, $include_attachments);
}
} else/*if (!empty($decoded->disposition) && $decoded->disposition != 'inline' or 1)*/ {
switch ($ctype) {
case 'text/plain':
case 'text/html':
if (!empty($decoded->disposition) && ($decoded->disposition == 'attachment')) {
$parts['attachments'][] = Array (
'data' => $include_attachments ? $decoded->body : '',
'filename' => array_key_exists('filename', $decoded->d_parameters) ? $decoded->d_parameters['filename'] : '', // from content-disposition
'filename2' => $decoded->ctype_parameters['name'], // from content-type
'type' => $decoded->ctype_primary, // "text"
'encoding' => $decoded->headers['content-transfer-encoding']
);
} else {
$body_type = $decoded->ctype_secondary == 'plain' ? 'text' : 'html';
$parts[$body_type][] = Array (
'content-type' => $ctype,
'charset' => array_key_exists('charset', $decoded->ctype_parameters) ? $decoded->ctype_parameters['charset'] : 'ISO-8859-1',
'body' => $decoded->body
);
}
break;
case 'message/rfc822':
// another e-mail as attachment
$parts['attachments'][] = Array (
'data' => $include_attachments ? $decoded->body : '',
'filename' => array_key_exists('filename', $decoded->d_parameters) ? $decoded->d_parameters['filename'] : '',
'filename2' => array_key_exists('name', $decoded->ctype_parameters) ? $decoded->ctype_parameters['name'] : $decoded->parts[0]->headers['subject'],
'type' => $decoded->ctype_primary, // "message"
'headers' => $decoded->headers // individual copy of headers with each attachment
);
break;
default:
if (!stristr($decoded->headers['content-type'], 'signature')) {
$parts['attachments'][] = Array (
'data' => $include_attachments ? $decoded->body : '',
'filename' => array_key_exists('filename', $decoded->d_parameters) ? $decoded->d_parameters['filename'] : '', // from content-disposition
'filename2' => $decoded->ctype_parameters['name'], // from content-type
'type' => $decoded->ctype_primary,
'headers' => $decoded->headers // individual copy of headers with each attachment
);
}
}
}
$parts['headers'] = $decoded->headers; // headers of next parts overwrite previous part headers
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/multilanguage_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/multilanguage_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/multilanguage_helper.php (revision 14628)
@@ -1,371 +1,377 @@
<?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!');
/**
* Performs action on multilingual fields
*
*/
class kMultiLanguageHelper extends kHelper {
/**
* Maximal language id
*
* @var int
*/
protected $languageCount = 0;
/**
* Languages created in system
*
* @var Array
*/
protected $languagesIDs = Array ();
/**
* Structure of table, that is currently processed
*
* @var Array
*/
var $curStructure = Array();
/**
* Field, to get structure information from
*
* @var string
*/
var $curSourceField = false;
/**
* Indexes used in table of 32
*
* @var int
*/
var $curIndexCount = 0;
/**
* Fields from config, that are currently used
*
* @var Array
*/
var $curFields = Array();
/**
* Updates language count in system (always is divisible by 5)
*
*/
protected function initLanguageCount()
{
static $init_made = false;
if (!$init_made) {
$this->languagesIDs = $this->getActualLanguages();
$this->languageCount = max( max($this->languagesIDs), 5 );
$init_made = true;
}
}
/**
* Returns language ids, that can be used
*
* @return Array
*/
protected function getActualLanguages()
{
$cache_key = 'language_ids[%LangSerial%]';
$ret = $this->Application->getCache($cache_key);
if ($ret === false) {
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT ' . $this->Application->getUnitOption('lang', 'IDField') . '
FROM ' . $this->Application->getUnitOption('lang', 'TableName');
$ret = $this->Conn->GetCol($sql);
$this->Application->setCache($cache_key, $ret);
}
return $ret;
}
/**
* Checks if language with specified id is created
*
* @param int $language_id
* @return bool
*/
protected function LanguageFound($language_id)
{
return in_array($language_id, $this->languagesIDs) || $language_id <= 5;
}
/**
* Returns list of processable languages
*
* @return Array
*/
public function getLanguages()
{
static $languages = null;
if ( !isset($languages) ) {
$languages = Array ();
$this->initLanguageCount();
for ($language_id = 1; $language_id <= $this->languageCount; $language_id++) {
if ( $this->LanguageFound($language_id) ) {
$languages[] = $language_id;
}
}
}
return $languages;
}
function scanTable($mask)
{
$i = 0;
$fields_found = 0;
$fields = array_keys($this->curStructure);
foreach ($fields as $field_name) {
if (preg_match($mask, $field_name)) {
$fields_found++;
}
}
return $fields_found;
}
function readTableStructure($table_name, $refresh = false)
{
// if ($refresh || !getArrayValue($structure_status, $prefix.'.'.$table_name)) {
$this->curStructure = $this->Conn->Query('DESCRIBE '.$table_name, 'Field');
$this->curIndexCount = count($this->Conn->Query('SHOW INDEXES FROM '.$table_name));
// }
}
/**
* Creates missing multilanguage fields in table by specified prefix
*
* @param string $prefix
* @param bool $refresh Forces config field structure to be re-read from database
+ * @return void
*/
function createFields($prefix, $refresh = false)
{
- if ($refresh && preg_match('/(.*)-cdata$/', $prefix, $regs)) {
+ if ( $refresh && preg_match('/(.*)-cdata$/', $prefix, $regs) ) {
// call main item config to clone cdata table
$this->Application->UnitConfigReader->loadConfig($regs[1]);
$this->Application->UnitConfigReader->runAfterConfigRead($prefix);
}
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
$this->curFields = $this->Application->getUnitOption($prefix, 'Fields');
- if (!($table_name && $this->curFields) || ($table_name && !$this->Conn->TableFound($table_name))) {
+ if ( !($table_name && $this->curFields) || ($table_name && !$this->Conn->TableFound($table_name)) ) {
// invalid config found or prefix not found
- return true;
+ return ;
}
$this->initLanguageCount();
- $sqls = Array();
+ $sqls = Array ();
$this->readTableStructure($table_name, $refresh);
- foreach($this->curFields as $field_name => $field_options)
- {
- if (getArrayValue($field_options, 'formatter') == 'kMultiLanguage') {
- if (isset($field_options['master_field'])) {
+ foreach ($this->curFields as $field_name => $field_options) {
+ if ( getArrayValue($field_options, 'formatter') == 'kMultiLanguage' ) {
+ if ( isset($field_options['master_field']) ) {
unset($this->curFields[$field_name]);
continue;
}
$this->setSourceField($field_name);
- if ($this->languageCount > 0) {
+ if ( $this->languageCount > 0 ) {
// `l77_Name` VARCHAR( 255 ) NULL DEFAULT '0';
- $field_mask = Array();
- $field_mask['name'] = 'l%s_'.$field_name;
+ $field_mask = Array ();
+ $field_mask['name'] = 'l%s_' . $field_name;
$field_mask['null'] = getArrayValue($field_options, 'not_null') ? 'NOT NULL' : 'NULL';
- if ($this->curSourceField) {
- $default_value = $this->getFieldParam('Default') != 'NULL' ? $this->Conn->qstr($this->getFieldParam('Default')) : $this->getFieldParam('Default');
+ if ( $this->curSourceField ) {
+ $default_value = $this->getFieldParam('Default') != 'NULL'
+ ? $this->Conn->qstr($this->getFieldParam('Default'))
+ : $this->getFieldParam('Default');
$field_mask['type'] = $this->getFieldParam('Type');
}
else {
- $default_value = is_null($field_options['default']) ? 'NULL' : $this->Conn->qstr($field_options['default']);
+ $default_value = is_null($field_options['default']) ? 'NULL'
+ : $this->Conn->qstr($field_options['default']);
$field_mask['type'] = $field_options['db_type'];
}
- $field_mask['default'] = ($field_mask['null'] == 'NOT NULL' && $default_value == 'NULL') ? '' : 'DEFAULT '.$default_value;
+ $field_mask['default'] = ($field_mask['null'] == 'NOT NULL' && $default_value == 'NULL') ? ''
+ : 'DEFAULT ' . $default_value;
- if (strtoupper($field_mask['type']) == 'TEXT') {
+ if ( strtoupper($field_mask['type']) == 'TEXT' ) {
// text fields in mysql doesn't have default value
- $field_mask = $field_mask['name'].' '.$field_mask['type'].' '.$field_mask['null'];
+ $field_mask = $field_mask['name'] . ' ' . $field_mask['type'] . ' ' . $field_mask['null'];
}
else {
- $field_mask = $field_mask['name'].' '.$field_mask['type'].' '.$field_mask['null'].' '.$field_mask['default'];
+ $field_mask = $field_mask['name'] . ' ' . $field_mask['type'] . ' ' . $field_mask['null'] . ' ' . $field_mask['default'];
}
$alter_sqls = $this->generateAlterSQL($field_mask, 1, $this->languageCount);
- if ($alter_sqls) {
- $sqls[] = 'ALTER TABLE '.$table_name.' '.$alter_sqls;
+ if ( $alter_sqls ) {
+ $sqls[] = 'ALTER TABLE ' . $table_name . ' ' . $alter_sqls;
}
}
}
}
foreach ($sqls as $sql_query) {
$this->Conn->Query($sql_query);
}
}
/**
* Creates missing multilanguage fields in table by specified prefix
*
* @param string $prefix
* @param int $src_language
* @param int $dst_language
+ * @return void
+ * @access public
*/
- function copyMissingData($prefix, $src_language, $dst_language)
+ public function copyMissingData($prefix, $src_language, $dst_language)
{
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
$this->curFields = $this->Application->getUnitOption($prefix, 'Fields');
- if (!($table_name && $this->curFields) || ($table_name && !$this->Conn->TableFound($table_name))) {
+ if ( !($table_name && $this->curFields) || ($table_name && !$this->Conn->TableFound($table_name)) ) {
// invalid config found or prefix not found
- return true;
+ return ;
}
foreach ($this->curFields as $field_name => $field_options) {
$formatter = isset($field_options['formatter']) ? $field_options['formatter'] : '';
if ( ($formatter == 'kMultiLanguage') && !isset($field_options['master_field']) ) {
$sql = 'UPDATE ' . $table_name . '
SET l' . $dst_language . '_' . $field_name . ' = l' . $src_language . '_' . $field_name . '
WHERE l' . $dst_language . '_' . $field_name . ' = "" OR l' . $dst_language . '_' . $field_name . ' IS NULL';
$this->Conn->Query($sql);
}
}
}
function deleteField($prefix, $custom_id)
{
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
$sql = 'DESCRIBE '.$table_name.' "l%_cust_'.$custom_id.'"';
$fields = $this->Conn->GetCol($sql);
$sql = 'ALTER TABLE '.$table_name.' ';
$sql_template = 'DROP COLUMN %s, ';
foreach ($fields as $field_name) {
$sql .= sprintf($sql_template, $field_name);
}
$this->Conn->Query( substr($sql, 0, -2) );
}
/**
* Returns parameter requested of current source field
*
* @param string $param_name
* @return string
*/
function getFieldParam($param_name)
{
return $this->curStructure[$this->curSourceField][$param_name];
}
/**
* Detects field name to create other fields from
*
* @param string $field_name
*/
function setSourceField($field_name)
{
$ret = $this->scanTable('/^l[\d]+_'.preg_quote($field_name, '/').'$/');
if (!$ret) {
// no multilingual fields at all (but we have such field without language prefix)
$original_found = $this->scanTable('/^'.preg_quote($field_name, '$/').'/');
$this->curSourceField = $original_found ? $field_name : false;
}
else {
$this->curSourceField = 'l1_'.$field_name;
}
}
/**
* Returns ALTER statement part for adding required fields to table
*
* @param string $field_mask sql mask for creating field with correct definition (type & size)
* @param int $start_index add new fields starting from this index
* @param int $create_count create this much new multilingual field translations
* @return string
*/
function generateAlterSQL($field_mask, $start_index, $create_count)
{
static $single_lang = null;
if (!isset($single_lang)) {
// if single language mode, then create indexes only on primary columns
$table_name = $this->Application->getUnitOption('lang', 'TableName');
$sql = 'SELECT COUNT(*)
FROM '.$table_name.'
WHERE Enabled = 1';
// if language count = 0, then assume it's multi language mode
$single_lang = $this->Conn->GetOne($sql) == 1;
}
$ret = '';
$ml_field = preg_replace('/l(.*?)_(.*?) (.*)/', '\\2', $field_mask);
$i_count = $start_index + $create_count;
while ($start_index < $i_count) {
if (isset($this->curStructure['l'.$start_index.'_'.$ml_field]) || (!$this->LanguageFound($start_index)) ) {
$start_index++;
continue;
}
$prev_index = $start_index - 1;
do {
list($prev_field,$type) = explode(' ', sprintf($field_mask, $prev_index) );
} while ($prev_index > 0 && !$this->LanguageFound($prev_index--));
if (substr($prev_field, 0, 3) == 'l0_') {
$prev_field = substr($prev_field, 3, strlen($prev_field));
if (!$this->curSourceField) {
// get field name before this one
$fields = array_keys($this->curFields);
// $prev_field = key(end($this->curStructure));
$prev_field = $fields[array_search($prev_field, $fields) - 1];
if (getArrayValue($this->curFields[$prev_field], 'formatter') == 'kMultiLanguage') {
$prev_field = 'l'.$this->languageCount.'_'.$prev_field;
}
}
}
$field_expression = sprintf($field_mask, $start_index);
$ret .= 'ADD COLUMN '.$field_expression.' AFTER `'.$prev_field.'`, ';
if ($this->curIndexCount < 32 && ($start_index == $this->Application->GetDefaultLanguageId() || !$single_lang)) {
// create index for primary language column + for all others (if multiple languages installed)
list($field_name, $field_params) = explode(' ', $field_expression, 2);
$index_type = isset($this->curFields[$ml_field]['index_type']) ? $this->curFields[$prev_field]['index_type'] : 'string';
$ret .= $index_type == 'string' ? 'ADD INDEX (`'.$field_name.'` (5) ), ' : 'ADD INDEX (`'.$field_name.'`), ';
$this->curIndexCount++;
}
$start_index++;
}
return preg_replace('/, $/', ';', $ret);
}
}
Index: branches/5.2.x/core/units/helpers/brackets_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/brackets_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/brackets_helper.php (revision 14628)
@@ -1,468 +1,476 @@
<?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 kBracketsHelper extends kHelper {
/**
* Field name holding minimal amount
*
* @var string
*/
var $min_field = '';
/**
* Field name holding maximal amount
*
* @var string
*/
var $max_field = '';
/**
* Default values to be set to automtically created price brackets
*
* @var Array
*/
var $default_values = Array();
var $defaultStartValue = 1;
/**
* Decimal separator
*
* @var string
*/
var $_decimalSeparator = '';
/**
* Thousands separator
*
* @var string
*/
var $_thousandsSeparator = '';
/**
* Current language
*
* @var LanguagesItem
*/
var $_language = null;
public function __construct()
{
parent::__construct();
$this->_language =& $this->Application->recallObject('lang.current');
/* @var $lang kDBItem */
$this->_decimalSeparator = $this->_language->GetDBField('DecimalPoint');
$this->_thousandsSeparator = $this->_language->GetDBField('ThousandSep');
}
function InitHelper($min_field, $max_field, $default_values, $default_start_value = null)
{
$this->min_field = $min_field;
$this->max_field = $max_field;
$this->default_values = $default_values;
if (isset($default_start_value)) {
$this->defaultStartValue = $default_start_value;
}
}
/**
* Converts number to operatable form
*
* @param string $value
* @return float
*/
function _parseNumber($value)
{
$value = str_replace($this->_thousandsSeparator, '', $value);
$value = str_replace($this->_decimalSeparator, '.', $value);
return $value;
}
/**
* Returns brackets from form with all numbers parsed
*
* @param kEvent $event
* @return Array
*/
function getBrackets(&$event)
{
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
return $this->parseBrackets($items_info);
}
function parseBrackets($brackets)
{
if (!$brackets) {
return $brackets;
}
foreach ($brackets as $id => $field_values) {
if (strlen($brackets[$id][$this->min_field])) {
$brackets[$id][$this->min_field] = (float)$this->_parseNumber($brackets[$id][$this->min_field]);
}
if (strlen($brackets[$id][$this->max_field])) {
$brackets[$id][$this->max_field] = (float)$this->_parseNumber($brackets[$id][$this->max_field]);
}
}
return $brackets;
}
/**
* Formats given brackets and sets them back to request
*
* @param kEvent $event
* @param Array $brackets
*/
function setBrackets(&$event, $brackets)
{
$brackets = $this->formatBrackets($brackets);
$this->Application->SetVar($event->getPrefixSpecial(true), $brackets);
}
function formatBrackets($brackets)
{
if (!$brackets) {
return $brackets;
}
foreach ($brackets as $id => $field_values) {
if (strlen($brackets[$id][$this->min_field])) {
$brackets[$id][$this->min_field] = $this->_language->formatNumber($brackets[$id][$this->min_field]);
}
if (strlen($brackets[$id][$this->max_field])) {
$brackets[$id][$this->max_field] = $this->_language->formatNumber($brackets[$id][$this->max_field]);
}
}
return $brackets;
}
/**
* Adds 5 more empty brackets to brackets
*
* @param kEvent $event
*/
function OnMoreBrackets(&$event)
{
$field_values = $this->getBrackets($event);
$object =& $event->getObject();
foreach($field_values as $id => $record)
{
if($record[$this->max_field] == '&#8734;') $field_values[$id][$this->max_field] = -1;
}
$new_id = (int)$this->Conn->GetOne('SELECT MIN('.$object->IDField.') FROM '.$object->TableName);
if($new_id > 0) $new_id = 0;
do
{
$new_id--;
}while( $this->arraySearch($field_values, $object->IDField, $new_id) );
$last_max_qty = $this->Conn->GetOne('SELECT MAX('.$this->max_field.') FROM '.$object->TableName);
$min_qty = $this->Conn->GetOne('SELECT MIN('.$this->max_field.') FROM '.$object->TableName);
if($min_qty == -1) $last_max_qty = -1;
if(!$last_max_qty) $last_max_qty = $this->defaultStartValue;
for($i = $new_id; $i > $new_id - 5; $i--)
{
$field_values[$i][$object->IDField] = $i;
$field_values[$i][$this->min_field] = ($i == $new_id-4 && $last_max_qty != -1) ? $last_max_qty : '';
$field_values[$i][$this->max_field] = ($i == $new_id-4 && $last_max_qty != -1) ? -1 : '';
$field_values[$i] = array_merge($field_values[$i], $this->default_values);
}
$event->CallSubEvent('OnPreSaveBrackets');
$this->setBrackets($event, $field_values);
}
/**
* Adds infinity bracket
*
* @param kEvent $event
*/
function OnInfinity(&$event)
{
$object =& $event->getObject();
$infinite_exists = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$object->TableName.' WHERE '.$this->max_field.' = -1');
$field_values = $this->getBrackets($event);
/*if(is_array($field_values))
{
foreach($field_values as $values)
{
$infinite_exists = $infinite_exists || ($values[$this->max_field] == -1);
}
}*/
if ($infinite_exists == 0) {
reset($field_values);
$last_bracket = end($field_values);
$new_id = (int)$this->Conn->GetOne('SELECT MIN('.$object->IDField.') FROM '.$object->TableName);
$brackets_exist = (int)$this->Conn->GetOne('SELECT COUNT(*) FROM '.$object->TableName);
if($new_id > 0) $new_id = 0;
do
{
$new_id--;
}while( $this->arraySearch($field_values, $object->IDField, $new_id) );
$infinite_bracket[$object->IDField] = $new_id;
$infinite_bracket[$this->min_field] = ($brackets_exist > 0) ? $last_bracket[$this->max_field] : $this->defaultStartValue;
$infinite_bracket[$this->max_field] = '-1';
$infinite_bracket = array_merge($infinite_bracket, $this->default_values);
$field_values[$new_id] = $infinite_bracket;
reset($field_values);
$this->setBrackets($event, $field_values);
}
}
/**
* Saves brackets to database
*
* @param kEvent $event
*/
function OnPreSaveBrackets(&$event)
{
$items_info = $this->getBrackets($event);
if ($items_info) {
$object =& $event->getObject();
/* @var $object kDBItem */
$linked_info = $object->getLinkedInfo();
$stored_ids = $this->Conn->GetCol('SELECT '.$object->IDField.' FROM '.$object->TableName.' WHERE '.$linked_info['ForeignKey'].' = '.$linked_info['ParentId']);
uasort($items_info, Array(&$this, 'compareBrackets') );
foreach ($items_info as $item_id => $values) {
if (in_array($item_id, $stored_ids)) { //if it's already exist
$object->Load($item_id);
$object->SetFieldsFromHash($values);
if (!$object->Validate()) {
unset($stored_ids[array_search($item_id, $stored_ids)]);
$event->redirect = false;
continue;
}
if( $object->Update($item_id) )
{
$event->status = kEvent::erSUCCESS;
}
else
{
$event->status = kEvent::erFAIL;
$event->redirect = false;
break;
}
unset( $stored_ids[ array_search($item_id, $stored_ids) ] );
}
else {
$object->Clear();
$object->SetFieldsFromHash($values);
$object->SetDBField($linked_info['ForeignKey'], $linked_info['ParentId']);
if ($object->Create()) {
$event->status = kEvent::erSUCCESS;
}
}
}
// delete
foreach ($stored_ids as $stored_id)
{
$this->Conn->Query('DELETE FROM '.$object->TableName.' WHERE '.$object->IDField.' = '.$stored_id);
}
}
}
+ /**
+ * Sorts brackets and returns sorted array
+ *
+ * @param kEvent $event
+ * @return Array
+ */
function arrangeBrackets(&$event)
{
$object =& $event->getObject();
+ /* @var $object kDBItem */
$temp = $this->getBrackets($event);
- foreach($temp as $id => $record)
- {
- if( $record[$this->max_field] == '&#8734;' )
- {
+ foreach ($temp as $id => $record) {
+ if ( $record[$this->max_field] == '&#8734;' ) {
$temp[$id][$this->max_field] = -1;
}
}
$temp_orig = $temp;
reset($temp);
if( is_array($temp) )
{
// array to store max values (2nd column)
$end_values = Array();
// get minimal value of Min
$first_elem = current($temp);
$start = $first_elem[$this->min_field];
if (!strlen($start)) {
$start = $this->defaultStartValue;
}
foreach($temp as $id => $record)
{
if(
// MAX is less than start
($record[$this->max_field] <= $start && $record[$this->max_field] != -1) ||
// Max is empty
!strlen($record[$this->max_field]) ||
// Max already defined in $end_values
(array_search($record[$this->max_field], $end_values) !== false)
) { // then delete from brackets list
unset($temp[$id]);
}
else { // this is when ok - add to end_values list
$end_values[] = $record[$this->max_field];
}
}
// sort brackets by 2nd column (Max values)
- uasort($temp, Array(&$this, 'compareBrackets') );
+ uasort($temp, Array (&$this, 'compareBrackets'));
reset($temp);
$first_item = each($temp);
$first_item_key = $first_item['key'];
$linked_info = $object->getLinkedInfo();
$sql = 'SELECT %s FROM %s WHERE %s = %s';
$ids = $this->Conn->GetCol( sprintf($sql, $object->IDField, $object->TableName, $linked_info['ForeignKey'], $linked_info['ParentId']) );
- if( is_array($ids) )
- {
- usort($ids, Array(&$this, 'sortBracketIDs') );
+
+ if ( is_array($ids) ) {
+ usort($ids, Array (&$this, 'sortBracketIDs'));
}
// $min_id = min( min($ids) - 1, -1 );
- foreach($temp as $key => $record)
- {
+ foreach ($temp as $key => $record) {
$temp[$key][$this->min_field] = $start;
$start = $temp[$key][$this->max_field];
}
}
$this->setBrackets($event, $temp);
return $temp;
}
function compareBrackets($bracket1, $bracket2) // ap_bracket_comp
{
$bracket1_min = $bracket1[$this->min_field];
$bracket1_max = $bracket1[$this->max_field];
$bracket2_min = $bracket2[$this->min_field];
$bracket2_max = $bracket2[$this->max_field];
// limits
if( ($bracket1_min != '') && ($bracket1_max == '') && ($bracket2_min != '') && ($bracket2_max != '') ) return 1;
if( ($bracket1_min != '') && ($bracket1_max == '') && ($bracket2_min == '') && ($bracket2_max == '') ) return -1;
if( ($bracket1_max == '') && ($bracket2_max != '') ) return 1;
if( ($bracket1_max != '') && ($bracket2_max == '') ) return -1;
if( ( ($bracket1_max > $bracket2_max) && ($bracket2_max != -1) ) || ( ($bracket1_max == -1) && ($bracket2_max != -1) ) )
{
return 1;
}
elseif( ($bracket1_max < $bracket2_max) || ( ($bracket2_max == -1) && ($bracket1_max != -1) ) )
{
return -1;
}
else
{
return 0;
}
}
function sortBracketIDs($first_id, $second_id) // pr_bracket_id_sort
{
$first_abs = abs($first_id);
$second_abs = abs($second_id);
$first_sign = ($first_id == 0) ? 0 : $first_id / $first_abs;
$second_sign = ($second_id == 0) ? 0 : $second_id / $second_abs;
if($first_sign != $second_sign)
{
if($first_id > $second_id)
{
$bigger =& $first_abs;
$smaller =& $second_abs;
}
else
{
$bigger =& $second_abs;
$smaller =& $first_abs;
}
$smaller = $bigger + $smaller;
}
return ($first_abs > $second_abs) ? 1 : ($first_abs < $second_abs ? -1 : 0);
}
/**
* Searches through submitted grid data to find record with specific value in specific field
*
* @param Array $records // grid data from REQUEST
* @param string $field
* @param string $value
* @return bool
*/
function arraySearch($records, $field, $value) // check_array
{
foreach ($records as $record)
{
if ($record[$field] == $value)
{
return true;
}
}
return false;
}
/**
- * Replate infinity mark with -1 before saving to db
+ * Replace infinity mark with -1 before saving to db
*
* @param kEvent $event
*/
function replaceInfinity(&$event)
{
$object =& $event->getObject();
- if($object->GetDBField($this->max_field) == '&#8734;') $object->SetDBField($this->max_field, -1);
+ /* @var $object kDBItem */
+
+ if ( $object->GetDBField($this->max_field) == '&#8734;' ) {
+ $object->SetDBField($this->max_field, -1);
+ }
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/minifiers/minify_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/minifiers/minify_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/minifiers/minify_helper.php (revision 14628)
@@ -1,263 +1,264 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2010 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 MinifyHelper extends kHelper {
/**
* Debug mode mark
*
* @var bool
*/
var $debugMode = false;
/**
* Path to compress information file
*
* @var string
*/
var $infoFile = '';
/**
* Compress information
*
* @var Array
*/
var $compressInfo = Array ();
public function __construct()
{
parent::__construct();
$this->debugMode = $this->Application->isDebugMode(false);
$this->infoFile = WRITEABLE . '/cache/compress_info.txt';
if ( file_exists($this->infoFile) ) {
$this->compressInfo = unserialize( file_get_contents($this->infoFile) );
}
}
/**
* When used as non-block tag, then compress given files and return url to result
*
- * @param Array $files
+ * @param Array $params
* @return string
+ * @access public
*/
- function CompressScriptTag($params)
+ public function CompressScriptTag($params)
{
// put to queue
- if (array_key_exists('to', $params)) {
+ if ( array_key_exists('to', $params) ) {
$files = $this->Application->GetVar($params['to'], '');
$this->Application->SetVar($params['to'], $files . '|' . $params['files']);
return '';
}
- if (array_key_exists('from', $params)) {
+ if ( array_key_exists('from', $params) ) {
// get from queue
$files = $this->Application->GetVar($params['from']);
}
else {
// get from tag
$files = $params['files'];
}
$files = $this->_getTemplatePaths( array_map('trim', explode('|', $files)) );
$extension = pathinfo($files[0], PATHINFO_EXTENSION);
$hash = ($this->debugMode ? 'd' : 'c') . '_' . $this->_getHash($files);
$file_mask = 'cache/' . $hash . '_%s.' . $extension;
$was_compressed = array_key_exists($hash, $this->compressInfo);
- if ($was_compressed) {
+ if ( $was_compressed ) {
$current_file = WRITEABLE . '/' . sprintf($file_mask, $this->compressInfo[$hash]);
- if (!file_exists($current_file)) {
+ if ( !file_exists($current_file) ) {
// when info exists, but file doesn't -> re-compress
$was_compressed = false;
}
- if ($this->debugMode) {
+ if ( $this->debugMode ) {
// check if any of listed files was changed since compressed file was created (only, when in debug mode)
foreach ($files as $file) {
- if (filemtime($file) > $this->compressInfo[$hash]) {
+ if ( filemtime($file) > $this->compressInfo[$hash] ) {
$was_compressed = false;
break;
}
}
}
}
- if (!$was_compressed) {
+ if ( !$was_compressed ) {
$string = '';
$path_length = strlen(FULL_PATH) + 1;
foreach ($files as $file) {
// add filename before for easier debugging
- if ($this->debugMode) {
+ if ( $this->debugMode ) {
$string .= '/* === File: ' . substr($file, $path_length) . ' === */' . "\n";
$string .= '/* ' . str_repeat('=', strlen(substr($file, $path_length)) + 14) . ' */' . "\n\n";
}
// add file content
$string .= file_get_contents($file) . "\n\n";
}
// remove previous version of compressed file
- if (isset($current_file)) {
- if (file_exists($current_file)) {
+ if ( isset($current_file) ) {
+ if ( file_exists($current_file) ) {
unlink($current_file);
}
}
// replace templates base
$templates_base = $this->Application->ProcessParsedTag('m', 'TemplatesBase', Array ());
$templates_base = preg_replace('/^' . preg_quote($this->Application->BaseURL(), '/') . '/', BASE_PATH . '/', $templates_base);
$string = str_replace('@templates_base@', rtrim($templates_base, '/'), $string);
// compress collected data
$this->compressInfo[$hash] = adodb_mktime();
- if (!$this->debugMode) {
+ if ( !$this->debugMode ) {
// don't compress merged js/css file in debug mode to allow js/css debugging
$this->compressString($string, $extension);
}
// save compressed file
$fp = fopen(WRITEABLE . '/' . sprintf($file_mask, $this->compressInfo[$hash]), 'w');
fwrite($fp, $string);
fclose($fp);
$this->_saveInfo();
}
// got compressed file -> use it
return $this->Application->BaseURL( str_replace(DIRECTORY_SEPARATOR, '/', WRITEBALE_BASE)) . sprintf($file_mask, $this->compressInfo[$hash]);
}
/**
* Returns hash string based on given files
*
* @param Array $files
* @return int
*/
function _getHash($files)
{
$hash = $files;
if ($this->Application->isAdmin) {
array_unshift($hash, 'A:1');
}
else {
array_unshift($hash, 'A:0;T:' . $this->Application->GetVar('m_theme'));
}
return crc32( implode('|', $hash) );
}
/**
* Saves file with compress information
*
*/
function _saveInfo()
{
$fp = fopen($this->infoFile, 'w');
fwrite($fp, serialize($this->compressInfo));
fclose($fp);
}
/**
* Deletes compression info file
*
* @todo also delete all listed there files
*/
function delete()
{
if (file_exists($this->infoFile)) {
unlink($this->infoFile);
}
}
/**
* Compress $string based on $extension
*
* @param string $string
* @param string $extension
*/
function compressString(&$string, $extension)
{
$tmp_file = tempnam('/tmp', 'to_compress_');
$fp = fopen($tmp_file, 'w');
fwrite($fp, $string);
fclose($fp);
$command = 'java -jar ' . dirname(__FILE__) . DIRECTORY_SEPARATOR . 'yuicompressor-2.4.2.jar --type ' . $extension . ' --charset utf-8 ' . $tmp_file;
$compressed_string = shell_exec($command);
unlink($tmp_file);
if (!is_null($compressed_string) && $compressed_string) {
$string = $compressed_string;
return ;
}
// failed to compress using YUICompressor (maybe java not installed)
if ($extension == 'js') {
$minifier =& $this->Application->makeClass('JsMinifyHelper');
/* @var $minifier JsMinifyHelper */
$string = $minifier->minify($string);
}
elseif ($extension == 'css') {
$minifier =& $this->Application->makeClass('CssMinifyHelper');
/* @var $minifier CssMinifyHelper */
$string = $minifier->minify($string);
}
}
/**
* Get full paths on disk for each of given templates
*
* @param Array $templates
* @return Array
*/
function _getTemplatePaths($templates)
{
$ret = Array ();
$reg_exp = '/^' . preg_quote($this->Application->BaseURL(), '/') . '(.*)/';
foreach ($templates as $template) {
if (!$template) {
continue;
}
if (preg_match($reg_exp, $template, $regs)) {
// full url (from current domain) to a file
$path = FULL_PATH . '/' . $regs[1];
}
else {
list ($path, $module_filename) = $this->Application->TemplatesCache->GetTemplatePaths($template);
$path .= DIRECTORY_SEPARATOR . $module_filename;
}
$ret[] = $path;
}
return $ret;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/minifiers/js_minify_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/minifiers/js_minify_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/minifiers/js_minify_helper.php (revision 14628)
@@ -1,291 +1,306 @@
<?php
/**
* PHP implementation of Douglas Crockford's JSMin.
*
* This is a direct port of jsmin.c to PHP with a few PHP performance tweaks and
* modifications to preserve some comments (see below). Also, rather than using
* stdin/stdout, JsMinifyHelper->minify() accepts a string as input and returns
* another string as output.
*
* Comments containing IE conditional compilation are preserved, as are multi-line
* comments that begin with "/*!" (for documentation purposes). In the latter case
* newlines are inserted around the comment to enhance readability.
*
* Permission is hereby granted to use this version of the library under the
* same terms as jsmin.c, which has the following license:
*
* --
* Copyright (c) 2002 Douglas Crockford (www.crockford.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* --
*
* @package JSMin
* @author Ryan Grove <ryan@wonko.com> (PHP port)
* @author Steve Clay <steve@mrclay.org> (modifications + cleanup)
* @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp)
* @author Alexander Obuhovich <aik.bold@gmail.com> (PHP4,In-Portal compatibility)
* @copyright 2002 Douglas Crockford <douglas@crockford.com> (jsmin.c)
* @copyright 2008 Ryan Grove <ryan@wonko.com> (PHP port)
* @license http://opensource.org/licenses/mit-license.php MIT License
* @link http://code.google.com/p/jsmin-php/
*/
defined('FULL_PATH') or die('restricted access!');
class JsMinifyHelper extends kHelper {
const ORD_LF = 10;
const ORD_SPACE = 32;
const ACTION_KEEP_A = 1;
const ACTION_DELETE_A = 2;
const ACTION_DELETE_A_B = 3;
protected $a = "\n";
protected $b = '';
protected $input = '';
protected $inputIndex = 0;
protected $inputLength = 0;
protected $lookAhead = null;
protected $output = '';
/**
* Perform minification, return result
+ *
+ * @param string $input
+ * @return string
*/
public function minify($input)
{
$this->input = str_replace("\r\n", "\n", $input);
$this->inputLength = strlen($this->input);
if ($this->output !== '') { // min already run
return $this->output;
}
$this->action(self::ACTION_DELETE_A_B);
while ($this->a !== null) {
// determine next command
$command = self::ACTION_KEEP_A; // default
if ($this->a === ' ') {
if (! $this->isAlphaNum($this->b)) {
$command = self::ACTION_DELETE_A;
}
} elseif ($this->a === "\n") {
if ($this->b === ' ') {
$command = self::ACTION_DELETE_A_B;
} elseif (false === strpos('{[(+-', $this->b)
&& ! $this->isAlphaNum($this->b)) {
$command = self::ACTION_DELETE_A;
}
} elseif (! $this->isAlphaNum($this->a)) {
if ($this->b === ' '
|| ($this->b === "\n"
&& (false === strpos('}])+-"\'', $this->a)))) {
$command = self::ACTION_DELETE_A_B;
}
}
$this->action($command);
}
$this->output = trim($this->output);
return $this->output;
}
/**
* ACTION_KEEP_A = Output A. Copy B to A. Get the next B.
* ACTION_DELETE_A = Copy B to A. Get the next B.
* ACTION_DELETE_A_B = Get the next B.
+ *
+ * @param int $command
+ * @return void
+ * @access protected
*/
protected function action($command)
{
switch ($command) {
case self::ACTION_KEEP_A:
$this->output .= $this->a;
// fallthrough
case self::ACTION_DELETE_A:
$this->a = $this->b;
if ($this->a === "'" || $this->a === '"') { // string literal
$str = $this->a; // in case needed for exception
while (true) {
$this->output .= $this->a;
$this->a = $this->get();
if ($this->a === $this->b) { // end quote
break;
}
if (ord($this->a) <= self::ORD_LF) {
throw new Exception('Unterminated String: ' . var_export($str, true));
}
$str .= $this->a;
if ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
$str .= $this->a;
}
}
}
// fallthrough
case self::ACTION_DELETE_A_B:
$this->b = $this->next();
if ($this->b === '/' && $this->isRegexpLiteral()) { // RegExp literal
$this->output .= $this->a . $this->b;
$pattern = '/'; // in case needed for exception
while (true) {
$this->a = $this->get();
$pattern .= $this->a;
if ($this->a === '/') { // end pattern
break; // while (true)
} elseif ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
$pattern .= $this->a;
} elseif (ord($this->a) <= self::ORD_LF) {
throw new Exception('Unterminated RegExp: '. var_export($pattern, true));
}
$this->output .= $this->a;
}
$this->b = $this->next();
}
// end case ACTION_DELETE_A_B
}
}
protected function isRegexpLiteral()
{
if (false !== strpos("\n{;(,=:[!&|?", $this->a)) { // we aren't dividing
return true;
}
if (' ' === $this->a) {
$length = strlen($this->output);
if ($length < 2) { // weird edge case
return true;
}
// you can't divide a keyword
if (preg_match('/(?:case|else|in|return|typeof)$/', $this->output, $m)) {
if ($this->output === $m[0]) { // odd but could happen
return true;
}
// make sure it's a keyword, not end of an identifier
$charBeforeKeyword = substr($this->output, $length - strlen($m[0]) - 1, 1);
if (! $this->isAlphaNum($charBeforeKeyword)) {
return true;
}
}
}
return false;
}
/**
* Get next char. Convert ctrl char to space.
*/
protected function get()
{
$c = $this->lookAhead;
$this->lookAhead = null;
if ($c === null) {
if ($this->inputIndex < $this->inputLength) {
$c = $this->input[$this->inputIndex];
$this->inputIndex += 1;
} else {
return null;
}
}
if ($c === "\r" || $c === "\n") {
return "\n";
}
if (ord($c) < self::ORD_SPACE) { // control char
return ' ';
}
return $c;
}
/**
* Get next char. If is ctrl character, translate to a space or newline.
*/
protected function peek()
{
$this->lookAhead = $this->get();
return $this->lookAhead;
}
/**
* Is $c a letter, digit, underscore, dollar sign, escape, or non-ASCII?
+ *
+ * @param string $c
+ * @return bool
+ * @access protected
*/
protected function isAlphaNum($c)
{
return (preg_match('/^[0-9a-zA-Z_\\$\\\\]$/', $c) || ord($c) > 126);
}
protected function singleLineComment()
{
$comment = '';
while (true) {
$get = $this->get();
$comment .= $get;
if (ord($get) <= self::ORD_LF) { // EOL reached
// if IE conditional comment
if (preg_match('/^\\/@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
return "/{$comment}";
}
return $get;
}
}
+
+ return '';
}
protected function multipleLineComment()
{
$this->get();
$comment = '';
while (true) {
$get = $this->get();
if ($get === '*') {
if ($this->peek() === '/') { // end of comment reached
$this->get();
// if comment preserved by YUI Compressor
if (0 === strpos($comment, '!')) {
return "\n/*" . substr($comment, 1) . "*/\n";
}
// if IE conditional comment
if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) {
return "/*{$comment}*/";
}
return ' ';
}
} elseif ($get === null) {
throw new Exception('Unterminated Comment: ' . var_export('/*' . $comment, true));
}
$comment .= $get;
}
+
+ return '';
}
/**
* Get the next character, skipping over comments.
* Some comments may be preserved.
*/
protected function next()
{
$get = $this->get();
if ($get !== '/') {
return $get;
}
switch ($this->peek()) {
case '/': return $this->singleLineComment();
case '*': return $this->multipleLineComment();
default: return $get;
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/custom_fields_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/custom_fields_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/custom_fields_helper.php (revision 14628)
@@ -1,135 +1,137 @@
<?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!');
/**
* Enter description here...
*
* @todo rewrite
*/
class InpCustomFieldsHelper extends kHelper {
/**
* Parses given option string and returns associative array
*
* @param string $values_list
* @param string $separator
* @param bool $parse
* @return Array
*/
function GetValuesHash($values_list, $separator = VALUE_LIST_SEPARATOR, $parse = true)
{
$values_list = trim($this->ParseConfigSQL($values_list, $separator, $parse), $separator);
if (!$values_list) {
// no options, then return empty array
return Array();
}
$optionValuesTmp = explode($separator, $values_list);
$optionValues = Array();
if (substr_count($values_list, '=') != count($optionValuesTmp)) {
if ( $this->Application->isDebugMode() ) {
$this->Application->Debugger->appendTrace();
}
trigger_error('Invalid symbol in ValueList field [' . substr($values_list, 0, 100) . ' ...]' , E_USER_WARNING);
return Array ();
}
if ($parse) {
// normal way
foreach ($optionValuesTmp as $optionValue) {
list ($key, $val) = explode('=', $optionValue);
$val = substr($val, 0, 1) == '+' ? substr($val, 1) : $this->Application->Phrase($val);
$optionValues[$key] = $val;
}
}
else {
// during custom field editing
foreach ($optionValuesTmp as $optionValue) {
list ($key, $val) = explode('=', $optionValue);
if (substr($key, 0, 3) == 'SQL') {
$val = base64_decode( str_replace('_', '=', substr($val, 1)) );
}
$optionValues[$key] = $val;
}
}
return $optionValues;
}
/**
- * Replace SQL's in valueList with appropriate queried values
+ * Replace SQLs in valueList with appropriate queried values
*
* @param string $valueString
* @param string $separator
+ * @param bool $parse_sqls
* @return string
+ * @access public
* @todo Apply refactoring to embedded vars stuff
*/
- function ParseConfigSQL($valueString, $separator = VALUE_LIST_SEPARATOR, $parse_sqls = true)
+ public function ParseConfigSQL($valueString, $separator = VALUE_LIST_SEPARATOR, $parse_sqls = true)
{
if ($parse_sqls) {
$string = trim( str_replace(Array('<PREFIX>', '%3$s'), Array (TABLE_PREFIX, $this->Application->GetVar('m_lang')), $valueString) );
}
else {
$string = $valueString;
}
if (preg_match_all('/<SQL([+]{0,1})>(.*?)<\/SQL>/', $string, $regs)) {
$i = 0;
$sql_count = count($regs[0]);
while ($i < $sql_count) {
if ($parse_sqls) {
$replacement = $this->_queryConfigSQL($regs[2][$i], $regs[1][$i], $separator);
}
else {
$sql = base64_encode('<SQL'.$regs[1][$i].'>'.$regs[2][$i].'</SQL>');
$replacement = 'SQL' . $i . '=+' . str_replace('=', '_', $sql);
}
$string = str_replace('<SQL'.$regs[1][$i].'>'.$regs[2][$i].'</SQL>', $replacement, $string);
$i++;
}
$string = preg_replace('/[' . preg_quote($separator, '/') . ']+/', $separator, $string); // trim trailing separators inside string
}
return $string;
}
/**
* Transforms given sql into value list string
*
* @param string $sql
* @param string $plus
* @param string $separator
* @return string
*/
function _queryConfigSQL($sql, $plus = '', $separator = VALUE_LIST_SEPARATOR)
{
$values = $this->Conn->Query($sql);
foreach ($values as $index => $value) {
$values[$index] = $value['OptionValue'] . '=' . $plus . $value['OptionName'];
}
return implode($separator, $values);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/filenames_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/filenames_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/filenames_helper.php (revision 14628)
@@ -1,183 +1,186 @@
<?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 kFilenamesHelper extends kHelper {
/**
* Character, that should replace disallowed symbols in filename
*
* @var string
*/
var $_escapeChar = '_';
/**
* Sequences, that should be replaced with mathing sequences from _filenameReplaceTo property
*
* @var Array
*/
var $_filenameReplaceFrom = Array ();
/**
* Sequences, that will replace matching sequences from _filenameReplaceFrom property
*
* @var Array
*/
var $_filenameReplaceTo = Array ();
public function __construct()
{
parent::__construct();
$this->_escapeChar = $this->Application->ConfigValue('FilenameSpecialCharReplacement');
$language =& $this->Application->recallObject('lang.primary');
/* @var $language kDBItem */
$replacements = $language->GetDBField('FilenameReplacements');
if ($replacements) {
$replacements = explode("\r\n", $replacements);
foreach ($replacements as $replacement) {
list ($replace_from, $replace_to) = explode('=', $replacement);
$this->_filenameReplaceFrom[] = trim($replace_from);
$this->_filenameReplaceTo[] = trim($replace_to);
}
}
}
function replaceSequences($filename)
{
$not_allowed = Array (
' ', '\\', '/', ':', '*', '?', '"', '<', '>', '|', '`',
'~', '!', '@', '#', '$', '%', '^', '&', '(', ')', '~',
'+', '=', '-', '{', '}', ']', '[', "'", ';', '.', ',', "\r", "\n"
);
if ($this->_filenameReplaceFrom) {
// replace predefined sequences
$filename = str_replace($this->_filenameReplaceFrom, $this->_filenameReplaceTo, $filename);
}
$filename = str_replace($not_allowed, $this->_escapeChar, $filename);
$filename = preg_replace('/(' . $this->_escapeChar . '+)/', $this->_escapeChar, $filename);
return trim($filename, $this->_escapeChar); // remove trailing escape chars
}
/**
- * replace not allowed symbols with "_" chars + remove duplicate "_" chars in result
+ * Replace not allowed symbols with "_" chars + remove duplicate "_" chars in result
*
- * @param string $string
+ * @param string $table
+ * @param string $id_field
+ * @param int $item_id
+ * @param string $filename
* @return string
*/
function stripDisallowed($table, $id_field, $item_id, $filename)
{
$filename = $this->replaceSequences($filename);
return $this->checkAutoFilename($table, $id_field, $item_id, $filename);
}
function checkAutoFilename($table, $id_field, $item_id, $filename)
{
if (!$filename) {
return $filename;
}
$item_id = !$item_id ? 0 : $item_id;
if ( $this->Application->GetLiveName($table) == TABLE_PREFIX.'CategoryItems' ) {
$sql_mask = ' SELECT CategoryId
FROM %1$s
WHERE ItemResourceId = %2$s';
$item_categories_cur = $this->Conn->GetCol( sprintf($sql_mask, $table, $item_id) );
if ( $this->Application->IsTempTable($table) ) {
$item_categories_live = $this->Conn->GetCol( sprintf($sql_mask, $this->Application->GetLiveName($table), $item_id) );
}
else {
$item_categories_live = Array ();
}
$item_categories = array_unique(array_merge($item_categories_cur, $item_categories_live));
if (!$item_categories) {
$item_categories = Array( $this->Application->GetVar('m_cat_id') ); // this may happen when creating new item
}
$cat_filter = ' AND CategoryId IN (' . implode(',', $item_categories) . ')';
}
else {
$cat_filter = '';
}
// check current table (temp or live)
$sql_mask = ' SELECT ' . $id_field . '
FROM %1$s
WHERE Filename = ' . $this->Conn->qstr($filename) . $cat_filter;
$found_temp_ids = $this->Conn->GetCol( sprintf($sql_mask, $table) );
// check live table if current is temp
if ( $this->Application->IsTempTable($table) ) {
$found_live_ids = $this->Conn->GetCol( sprintf($sql_mask, $this->Application->GetLiveName($table)) );
}
else {
$found_live_ids = Array ();
}
$found_item_ids = array_unique( array_merge($found_temp_ids, $found_live_ids) );
$has_page = preg_match('/(.*)_([\d]+)([a-z]*)$/', $filename, $rets);
$duplicates_found = (count($found_item_ids) > 1) || ($found_item_ids && $found_item_ids[0] != $item_id);
if ($duplicates_found || $has_page) {// other category has same filename as ours OR we have filename, that ends with _number
$append = $duplicates_found ? $this->_escapeChar . 'a' : '';
if ($has_page) {
$filename = $rets[1].'_'.$rets[2];
$append = $rets[3] ? $rets[3] : $this->_escapeChar . 'a';
}
// check live & temp table
$sql_cur = 'SELECT ' . $id_field . '
FROM ' . $table . '
WHERE (Filename = %s) AND (' . $id_field . ' != ' . $item_id . ')' . $cat_filter;
if ( $this->Application->IsTempTable($table) ) {
$sql_live = ' SELECT ' . $id_field . '
FROM ' . $this->Application->GetLiveName($table) . '
WHERE (Filename = %s) AND (' . $id_field . ' != ' . $item_id . ')' . $cat_filter;
}
else {
$sql_live = false;
}
while ( $this->Conn->GetOne( sprintf($sql_cur, $this->Conn->qstr($filename.$append)) ) > 0 ||
( $sql_live
&&
( $this->Conn->GetOne( sprintf($sql_live, $this->Conn->qstr($filename.$append)) ) > 0 )
)
)
{
if (mb_substr($append, -1) == 'z') $append .= 'a';
$append = mb_substr($append, 0, mb_strlen($append) - 1) . chr( ord( mb_substr($append, -1) ) + 1 );
}
return $filename.$append;
}
return $filename;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/menu_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/menu_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/menu_helper.php (revision 14628)
@@ -1,386 +1,389 @@
<?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 MenuHelper extends kHelper {
/**
* Cached version of site menu
*
* @var Array
*/
var $Menu = null;
/**
* Parent path mapping used in CachedMenu tag
*
* @var Array
*/
var $parentPaths = Array ();
/**
* Builds site menu
*
+ * @param string $prefix_special
* @param Array $params
+ *
* @return string
+ * @access public
*/
- function menuTag($prefix_special, $params)
+ public function menuTag($prefix_special, $params)
{
list ($menu, $root_path) = $this->_prepareMenu();
$cat = $this->_getCategoryId($params);
$parent_path = array_key_exists($cat, $this->parentPaths) ? $this->parentPaths[$cat] : '';
$parent_path = str_replace($root_path, '', $parent_path); // menu starts from module path
$levels = explode('|', trim($parent_path, '|'));
if ($levels[0] === '') {
$levels = Array ();
}
if (array_key_exists('level', $params) && $params['level'] > count($levels)) {
// current level is deeper, then requested level
- return ;
+ return '';
}
$level = max(array_key_exists('level', $params) ? $params['level'] - 1 : count($levels) - 1, 0);
$parent = array_key_exists($level, $levels) ? $levels[$level] : 0;
$cur_menu =& $menu;
$menu_path = array_slice($levels, 0, $level + 1);
foreach ($menu_path as $elem) {
$cur_menu =& $cur_menu['c' . $elem]['sub_items'];
}
$block_params = $this->prepareTagParams($prefix_special, $params);
$block_params['name'] = $params['render_as'];
$this->Application->SetVar('cur_parent_path', $parent_path);
$real_cat_id = $this->Application->GetVar('m_cat_id');
if (!is_array($cur_menu) || !$cur_menu) {
// no menus on this level
return '';
}
$ret = '';
$cur_item = 1;
$cur_menu = $this->_removeNonMenuItems($cur_menu);
$block_params['total_items'] = count($cur_menu);
foreach ($cur_menu as $page) {
$block_params = array_merge(
$block_params,
$this->_prepareMenuItem($page, $real_cat_id, $root_path)
);
$block_params['is_last'] = $cur_item == $block_params['total_items'];
$block_params['is_first'] = $cur_item == 1;
$ret .= $this->Application->ParseBlock($block_params);
$cur_item++;
}
$this->Application->SetVar('m_cat_id', $real_cat_id);
return $ret;
}
/**
* Builds cached menu version
*
* @return Array
*/
function _prepareMenu()
{
static $root_cat = null;
static $root_path = null;
if (!$root_cat) {
$root_cat = $this->Application->getBaseCategory();
$cache_key = 'parent_paths[%CIDSerial:' . $root_cat . '%]';
$root_path = $this->Application->getCache($cache_key);
if ($root_path === false) {
$this->Conn->nextQueryCachable = true;
$sql = 'SELECT ParentPath
FROM ' . TABLE_PREFIX . 'Category
WHERE CategoryId = ' . $root_cat;
$root_path = $this->Conn->GetOne($sql);
$this->Application->setCache($cache_key, $root_path);
}
}
if ( !$this->Menu ) {
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$menu = $this->Application->getCache('master:cms_menu', false, CacheSettings::$cmsMenuRebuildTime);
}
else {
$menu = $this->Application->getDBCache('cms_menu', CacheSettings::$cmsMenuRebuildTime);
}
if ($menu) {
$menu = unserialize($menu);
$this->parentPaths = $menu['parentPaths'];
}
else {
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->rebuildCache('master:cms_menu', kCache::REBUILD_NOW, CacheSettings::$cmsMenuRebuildTime);
}
else {
$this->Application->rebuildDBCache('cms_menu', kCache::REBUILD_NOW, CacheSettings::$cmsMenuRebuildTime);
}
$menu = $this->_altBuildMenuStructure(Array ('CategoryId' => $root_cat, 'ParentPath' => $root_path));
$menu['parentPaths'] = $this->parentPaths;
if ($this->Application->isCachingType(CACHING_TYPE_MEMORY)) {
$this->Application->setCache('master:cms_menu', serialize($menu));
}
else {
$this->Application->setDBCache('cms_menu', serialize($menu));
}
}
unset($menu['parentPaths']);
$this->Menu = $menu;
}
return Array ($this->Menu, $root_path);
}
/**
* Returns category id based tag parameters
*
* @param Array $params
* @return int
*/
function _getCategoryId($params)
{
$cat = isset($params['category_id']) && $params['category_id'] != '' ? $params['category_id'] : $this->Application->GetVar('m_cat_id');
if ("$cat" == 'parent') {
$this_category =& $this->Application->recallObject('c');
/* @var $this_category kDBItem */
$cat = $this_category->GetDBField('ParentId');
}
elseif ($cat == 0) {
$cat = $this->Application->getBaseCategory();
}
return $cat;
}
/**
* Prepares cms menu item block parameters
*
* @param Array $page
* @param int $real_cat_id
* @param string $root_path
* @return Array
*/
function _prepareMenuItem($page, $real_cat_id, $root_path)
{
static $language_id = null;
static $primary_language_id = null;
static $template = null;
if (!isset($language_id)) {
$language_id = $this->Application->GetVar('m_lang');
$primary_language_id = $this->Application->GetDefaultLanguageId();
$template = $this->Application->GetVar('t');
}
$active = $category_active = false;
$title = $page['l' . $language_id . '_ItemName'] ? $page['l' . $language_id . '_ItemName'] : $page['l' . $primary_language_id . '_ItemName'];
if ($page['ItemType'] == 'cat') {
if (array_key_exists($real_cat_id, $this->parentPaths)) {
$active = strpos($this->parentPaths[$real_cat_id], $page['ParentPath']) !== false;
}
elseif ($page['ItemPath'] == $template) {
// physical template in menu
$active = true;
}
$category_active = $page['CategoryId'] == $real_cat_id;
}
/*if ($page['ItemType'] == 'cat_index') {
$check_path = str_replace($root_path, '', $page['ParentPath']);
$active = strpos($parent_path, $check_path) !== false;
}
if ($page['ItemType'] == 'page') {
$active = $page['ItemPath'] == preg_replace('/^Content\//i', '', $this->Application->GetVar('t'));
}*/
$block_params = Array (
'title' => $title,
'template' => $page['ItemPath'],
'active' => $active,
'category_active' => $category_active, // new
'parent_path' => $page['ParentPath'],
'parent_id' => $page['ParentId'],
'cat_id' => $page['CategoryId'],
'item_type' => $page['ItemType'],
'page_id' => $page['ItemId'],
'use_section' => ($page['Type'] == PAGE_TYPE_TEMPLATE) && ($page['ItemPath'] != 'index'),
'has_sub_menu' => isset($page['sub_items']) && count($page['sub_items']) > 0,
'external_url' => $page['UseExternalUrl'] ? $page['ExternalUrl'] : false, // for backward compatibility
'menu_icon' => $page['UseMenuIconUrl'] ? $page['MenuIconUrl'] : false,
);
return $block_params;
}
/**
* Returns only items, that are visible in menu
*
* @param Array $menu
* @return Array
*/
function _removeNonMenuItems($menu)
{
$theme_id = $this->Application->GetVar('m_theme');
foreach ($menu as $menu_index => $menu_item) {
// $menu_index is in "cN" format, where N is category id
if (!$menu_item['IsMenu'] || $menu_item['Status'] != STATUS_ACTIVE || ($menu_item['ThemeId'] != $theme_id && $menu_item['ThemeId'] != 0)) {
// don't show sections, that are not from menu OR system templates from other themes
unset($menu[$menu_index]);
}
}
return $menu;
}
/**
* Builds cache for children of given category (no matter, what menu status is)
*
* @param Array $parent
* @return Array
*/
function _altBuildMenuStructure($parent)
{
static $lang_part = null;
if (!isset($lang_part)) {
$ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$lang_part = '';
$languages = $ml_helper->getLanguages();
foreach ($languages as $language_id) {
$lang_part .= 'c.l' . $language_id . '_MenuTitle AS l' . $language_id . '_ItemName,' . "\n";
}
}
// Sub-categories from current category
$items = $this->getSubCategories( $parent['CategoryId'] );
// sort menu items
uasort($items, Array (&$this, '_menuSort'));
// store menu items
$the_items = Array();
foreach ($items as $index => $an_item) {
$the_items[ $an_item['ItemId'] ] = $an_item;
$this->parentPaths[ $an_item['CategoryId'] ] = $an_item['ParentPath'];
}
// process submenus of each menu
$items = $the_items;
foreach ($items as $key => $menu_item) {
if ($menu_item['CategoryId'] == $parent['CategoryId']) {
// don't process myself - prevents recursion
continue;
}
$sub_items = $this->_altBuildMenuStructure($menu_item);
if ($sub_items) {
$items[$key]['sub_items'] = $sub_items;
}
}
return $items;
}
function getSubCategories($parent_id)
{
static $items_by_parent = null;
if (!isset($items_by_parent)) {
$ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$lang_part = '';
$items_by_parent = Array ();
$languages = $ml_helper->getLanguages();
foreach ($languages as $language_id) {
$lang_part .= 'c.l' . $language_id . '_MenuTitle AS l' . $language_id . '_ItemName,' . "\n";
}
// Sub-categories from current category
$sql = 'SELECT
c.CategoryId AS CategoryId,
CONCAT(\'c\', c.CategoryId) AS ItemId,
c.Priority AS ItemPriority,
' . $lang_part . '
IF(c.`Type` = ' . PAGE_TYPE_TEMPLATE . ', c.Template, CONCAT("id:", c.CategoryId)) AS ItemPath,
c.ParentPath AS ParentPath,
c.ParentId As ParentId,
\'cat\' AS ItemType,
c.IsMenu, c.Type, c.ThemeId, c.UseExternalUrl, c.ExternalUrl, c.UseMenuIconUrl, c.MenuIconUrl,
c.Status
FROM ' . TABLE_PREFIX . 'Category AS c';
$items = $this->Conn->Query($sql, 'ItemId');
foreach ($items as $item_id => $item_data) {
$item_parent_id = $item_data['ParentId'];
if ( !array_key_exists($item_parent_id, $items_by_parent) ) {
$items_by_parent[$item_parent_id] = Array ();
}
$items_by_parent[$item_parent_id][$item_id] = $item_data;
}
}
return array_key_exists($parent_id, $items_by_parent) ? $items_by_parent[$parent_id] : Array ();
}
/**
* Method for sorting pages by priority in decending order
*
* @param Array $a
* @param Array $b
* @return int
*/
function _menuSort($a, $b)
{
if ($a['ItemPriority'] == $b['ItemPriority']) {
return 0;
}
return ($a['ItemPriority'] < $b['ItemPriority']) ? 1 : -1; //descending
}
}
Index: branches/5.2.x/core/units/helpers/recursive_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/recursive_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/recursive_helper.php (revision 14628)
@@ -1,223 +1,227 @@
<?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 kRecursiveHelper extends kHelper {
function DeleteCategory($category_id, $prefix='c')
{
$id_field = $this->Application->getUnitOption($prefix, 'IDField');
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
$sql = 'SELECT '.$id_field.'
FROM '.$table_name.'
WHERE ParentId = '.$category_id;
$sub_categories = $this->Conn->GetCol($sql);
if ($sub_categories) {
foreach ($sub_categories as $sub_category_id) {
$this->DeleteCategory($sub_category_id);
}
}
$ci_table = $this->Application->getUnitOption('ci', 'TableName');
// 1. remove category items from this category if it is supplemental (non-primary) category to them
$sql = 'DELETE FROM '.$ci_table.'
WHERE ('.$id_field.' = '.$category_id.') AND (PrimaryCat = 0)';
$this->Conn->Query($sql);
$temp_handler =& $this->Application->recallObject($prefix.'_TempHandler', 'kTempTablesHandler');
-
+ /* @var $temp_handler kTempTablesHandler */
+
// 2. delete items this have this category as primary
$delete_ids = $this->getCategoryItems($category_id, true);
foreach ($delete_ids as $item_prefix => $resource_ids) {
if (!$item_prefix) {
// not ItemPrefix filled -> old categoryitem linking
continue;
}
$item_ids = $this->GetItemIDs($item_prefix, $resource_ids);
$temp_handler->BuildTables($item_prefix, $item_ids);
$temp_handler->DeleteItems($item_prefix, '', $item_ids);
}
// 3. delete this category
$temp_handler->BuildTables($prefix, Array($category_id));
$temp_handler->DeleteItems($prefix, '', Array($category_id));
}
/**
* Converts resource ids list to id field list for given prefix
*
* @param string $prefix
* @param Array $resource_ids
* @return Array
*/
function GetItemIDs($prefix, $resource_ids)
{
if (!$resource_ids) {
return Array();
}
$id_field = $this->Application->getUnitOption($prefix, 'IDField');
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
$sql = 'SELECT '.$id_field.'
FROM '.$table_name.'
WHERE ResourceId IN ('.implode(',', $resource_ids).')';
return $this->Conn->GetCol($sql);
}
// moves selected categories to destination category
function MoveCategories($category_ids, $dest_category_id)
{
if (!$category_ids) return ;
$id_field = $this->Application->getUnitOption('c', 'IDField');
$table_name = $this->Application->getUnitOption('c', 'TableName');
// do not move categories into their children
$sql = 'SELECT ParentPath
FROM '.$table_name.'
WHERE '.$id_field.' = '.$dest_category_id;
$dest_parent_path = explode('|', substr($this->Conn->GetOne($sql), 1, -1));
$child_categories = array_intersect($dest_parent_path, $category_ids); // get categories, then can't be moved
$category_ids = array_diff($category_ids, $child_categories); // remove them from movable categories list
if ($category_ids) {
$sql = 'UPDATE '.$table_name.'
SET ParentId = '.$dest_category_id.'
WHERE '.$id_field.' IN ('.implode(',', $category_ids).')';
$this->Conn->Query($sql);
}
}
/**
- * Complete cloning or category with subcategories and subitems
+ * Complete cloning or category with subcategories and sub-items
*
* @param int $category_id
+ * @param string $prefix
*/
function PasteCategory($category_id, $prefix = 'c')
{
$backup_category_id = $this->Application->GetVar('m_cat_id');
$src_parent_path = $this->_getParentPath($category_id);
$dst_parent_path = $this->_getParentPath($backup_category_id);
- if (substr($dst_parent_path, 0, strlen($src_parent_path)) == $src_parent_path) {
+ if ( substr($dst_parent_path, 0, strlen($src_parent_path)) == $src_parent_path ) {
// target path contains source path -> recursion
- return ;
+ return;
}
// 1. clone category
- $temp_handler =& $this->Application->recallObject($prefix.'_TempHandler', 'kTempTablesHandler');
+ $temp_handler =& $this->Application->recallObject($prefix . '_TempHandler', 'kTempTablesHandler');
/* @var $temp_handler kTempTablesHandler*/
- $temp_handler->BuildTables($prefix, Array($category_id));
- $new_category_id = array_pop( $temp_handler->CloneItems($prefix, '', Array($category_id)) );
+ $temp_handler->BuildTables($prefix, Array ($category_id));
+ $new_category_id = array_pop($temp_handler->CloneItems($prefix, '', Array ($category_id)));
$this->Application->SetVar('m_cat_id', $new_category_id);
$id_field = $this->Application->getUnitOption($prefix, 'IDField');
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
// 2. assign supplemental items to current category to new category
$paste_ids = $this->getCategoryItems($category_id, false);
foreach ($paste_ids as $item_prefix => $resource_ids) {
- if (!$item_prefix) {
+ if ( !$item_prefix ) {
// not ItemPrefix filled -> old categoryitem linking
continue;
}
- $item_object =& $this->Application->recallObject($item_prefix.'.-item', null, Array('skip_autoload' => true));
+ $item_object =& $this->Application->recallObject($item_prefix . '.-item', null, Array ('skip_autoload' => true));
+ /* @var $item_object kCatDBItem */
+
foreach ($resource_ids as $item_resource_id) {
$item_object->Load($item_resource_id, 'ResourceId');
$item_object->assignToCategory($new_category_id, false);
}
}
// 3. clone items that have current category as primary
$paste_ids = $this->getCategoryItems($category_id, true);
foreach ($paste_ids as $item_prefix => $resource_ids) {
- if (!$item_prefix) {
+ if ( !$item_prefix ) {
// not ItemPrefix filled -> old categoryitem linking
continue;
}
// 2. clone items from current category (for each prefix separately)
$item_ids = $this->GetItemIDs($item_prefix, $resource_ids);
$temp_handler->BuildTables($item_prefix, $item_ids);
$temp_handler->CloneItems($item_prefix, '', $item_ids);
}
// 4. do same stuff for each subcategory
- $sql = 'SELECT '.$id_field.'
- FROM '.$table_name.'
- WHERE ParentId = '.$category_id;
+ $sql = 'SELECT ' . $id_field . '
+ FROM ' . $table_name . '
+ WHERE ParentId = ' . $category_id;
$sub_categories = $this->Conn->GetCol($sql);
- if ($sub_categories) {
+ if ( $sub_categories ) {
foreach ($sub_categories as $sub_category_id) {
$this->PasteCategory($sub_category_id, $prefix);
}
}
$this->Application->SetVar('m_cat_id', $backup_category_id);
}
/**
* Returns grouped category items
*
* @param int $category_id
* @param bool $item_primary_category
* @return Array
*/
function getCategoryItems($category_id, $item_primary_category = true)
{
$ci_table = $this->Application->getUnitOption('ci', 'TableName');
$sql = 'SELECT ItemPrefix, ItemResourceId
FROM '.$ci_table.'
WHERE (CategoryId = '.$category_id.') AND (PrimaryCat = '.($item_primary_category ? 1 : 0).')';
$category_items = $this->Conn->GetCol($sql, 'ItemResourceId');
$item_ids = Array();
foreach ($category_items as $resource_id => $item_prefix) {
$item_ids[$item_prefix][] = $resource_id;
}
return $item_ids;
}
/**
* Returns parent path for given category
*
* @param int $category_id
* @return Array
*/
function _getParentPath($category_id)
{
static $cache = Array ();
if (!array_key_exists($category_id, $cache)) {
$sql = 'SELECT ParentPath
FROM ' . TABLE_PREFIX . 'Category
WHERE CategoryId = ' . $category_id;
$cache[$category_id] = $this->Conn->GetOne($sql);
}
return $cache[$category_id];
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/modules_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/modules_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/modules_helper.php (revision 14628)
@@ -1,441 +1,446 @@
<?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 kModulesHelper extends kHelper {
function getWhereClause()
{
$where_clause = Array('Loaded = 1');
if (!$this->Application->isAdmin) {
// no license checks on front-end
return implode(' AND ', $where_clause);
}
$modules = $this->_GetModules();
if ($modules) {
foreach ($modules as $module_index => $module) {
$modules[$module_index] = $this->Conn->qstr($module);
}
$where_clause[] = 'Name IN ('.implode(',', $modules).')';
}
return implode(' AND ', $where_clause);
}
function _EnableCookieSID()
{
$session =& $this->Application->recallObject('Session');
+ /* @var $session Session */
+
return $session->CookiesEnabled;
}
function _IsSpider($UserAgent)
{
global $robots;
$lines = file(FULL_PATH.'/robots_list.txt');
if (!is_array($robots)) {
$robots = Array();
for($i = 0; $i < count($lines); $i++) {
$l = $lines[$i];
$p = explode("\t", $l, 3);
$robots[] = $p[2];
}
}
return in_array($UserAgent, $robots);
}
function _MatchIp($ip1, $ip2)
{
$matched = TRUE;
$ip = explode('.', $ip1);
$MatchIp = explode('.', $ip2);
for ($i = 0; $i < count($ip); $i++) {
if($i == count($MatchIp)) break;
if (trim($ip[$i]) != trim($MatchIp[$i]) || trim($ip[$i]) == '*') {
$matched = FALSE;
break;
}
}
return $matched;
}
function _IpAccess($IpAddress, $AllowList, $DenyList)
{
$allowed = explode(',', $AllowList);
$denied = explode(',', $DenyList);
$MatchAllowed = FALSE;
for ($x = 0; $x < count($allowed); $x++) {
$ip = explode('.', $allowed[$x]);
$MatchAllowed = $this->_MatchIp($IpAddress, $allowed[$x]);
if ($MatchAllowed)
break;
}
$MatchDenied = FALSE;
for ($x = 0; $x < count($denied); $x++) {
$ip = explode('.', $denied[$x]);
$MatchDenied = $this->_MatchIp($IpAddress, $denied[$x]);
if ($MatchDenied)
break;
}
$Result = (($MatchAllowed && !$MatchDenied) || (!$MatchAllowed && !$MatchDenied) ||
($MatchAllowed && $MatchDenied));
return $Result;
}
/**
* Leaves only domain part from hostname (e.g. extract "intechnic.lv" from "test.intechnic.lv")
* Used for admin login license check
*
* @param string $d
* @return string
*/
function _StripDomainHost($d)
{
$IsIp = false;
$dotcount = substr_count($d, '.');
if ($dotcount == 3) {
$IsIp = true;
for ($x = 0; $x < strlen($d); $x++) {
if (!is_numeric(substr($d, $x, 1)) && substr($d, $x, 1) != '.')
{
$IsIp = false;
break;
}
}
}
if ($dotcount > 1 && !$IsIp) {
$p = explode('.', $d);
$ret = $p[count($p) - 2].'.'.$p[count($p) - 1];
}
else {
$ret = $d;
}
return $ret;
}
/**
* When logging into admin then check only last 2 parts of host name VS domain in license
*
* @param string $user_domain
* @param string $license_domain
* @return int
*/
function _CheckDomain($user_domain, $license_domain)
{
if ($this->Application->isAdmin) {
$user_domain = $this->_StripDomainHost($user_domain);
return preg_match('/(.*)'.preg_quote($user_domain, '/').'$/', $license_domain);
}
else {
return preg_match('/(.*)'.preg_quote($license_domain, '/').'$/', $user_domain);
}
}
/**
* Returns modules list, that are in license
*
* @return Array
*/
function _GetModules()
{
static $modules = null;
if (isset($modules)) {
return $modules;
}
$modules = Array();
$vars = kUtil::getConfigVars();
$license = array_key_exists('License', $vars) ? base64_decode($vars['License']) : false;
if ($license) {
list ( , , $i_Keys) = $this->_ParseLicense($license);
$domain = $this->_GetDomain($vars);
if (!$this->_IsLocalSite($domain)) {
for ($x = 0; $x < count($i_Keys); $x++) {
$key = $i_Keys[$x];
if ($this->_CheckDomain($domain, $key['domain'])) {
// used hostname is subdomain or matches domain from license
$modules = explode(',', $key['mod']);
}
}
}
else {
// all already installed modules are licensed for localhost
$modules = array_keys($this->Application->ModuleInfo);
}
}
// all modules starting from "in-" doesn't require license
$base_modules = Array ('Core', 'In-Portal', 'Custom');
$modules = array_merge($modules, $base_modules, $this->_getFreeModules($vars));
$modules = array_unique( array_map('strtolower', $modules) );
return $modules;
}
/**
* Get all modules, that don't require licensing
*
+ * @param Array $vars
* @return Array
+ * @access protected
*/
- function _getFreeModules($vars)
+ protected function _getFreeModules($vars)
{
$skip_modules = Array ('.', '..');
$domain = $this->_GetDomain($vars);
- if (!$this->_IsLocalSite($domain)) {
- array_push($skip_modules, 'in-commerce', 'in-auction');
- }
+ if ( !$this->_IsLocalSite($domain) ) {
+ array_push($skip_modules, 'in-commerce', 'in-auction');
+ }
$ret = Array ();
- $folder = dir(MODULES_PATH);
+ $folder = dir(MODULES_PATH);
- if ($folder === false) {
- return Array ();
- }
+ if ( $folder === false ) {
+ return Array ();
+ }
- while (($entry = $folder->read()) !== false) {
+ while ( ($entry = $folder->read()) !== false ) {
$entry_lowercased = strtolower($entry);
- if (!is_dir($folder->path . '/' . $entry) || in_array($entry_lowercased, $skip_modules)) {
+
+ if ( !is_dir($folder->path . '/' . $entry) || in_array($entry_lowercased, $skip_modules) ) {
continue;
}
$ret[] = $entry_lowercased;
}
$folder->close();
return $ret;
}
/**
* Allows to determine if module is licensed
*
* @param string $name
* @return bool
*/
function _ModuleLicensed($name)
{
$modules = $this->_GetModules();
return in_array($name, $modules);
}
/**
* Returns domain from licences (and direct in case of install script)
*
* @return string
*/
function _GetDomain($vars)
{
return isset($vars['Domain']) ? $vars['Domain'] : SERVER_NAME;
}
function _keyED($txt, $encrypt_key)
{
$encrypt_key = md5($encrypt_key);
$ctr = 0;
$tmp = '';
for ($i = 0; $i < strlen($txt); $i++) {
if ($ctr == strlen($encrypt_key)) $ctr = 0;
$tmp .= substr($txt, $i, 1) ^ substr($encrypt_key, $ctr, 1);
$ctr++;
}
return $tmp;
}
function _decrypt($txt, $key)
{
$txt = $this->_keyED($txt,$key);
$tmp = '';
for ($i = 0; $i < strlen($txt); $i++) {
$md5 = substr($txt, $i, 1);
$i++;
$tmp .= (substr($txt, $i, 1) ^ $md5);
}
return $tmp;
}
function LoadFromRemote()
{
return '';
}
function DLid()
{
die($GLOBALS['lid']."\n");
}
function _LoadLicense($LoadRemote = false)
{
$f = FULL_PATH.'/intechnic.php';
if ($this->_falseIsLocalSite($f)) $ret = true;
if (file_exists($f)) {
$contents = file($f);
$data = base64_decode($contents[1]);
}
else {
if ($LoadRemote) return $LoadFromRemote;
}
return $data;
}
function _VerifyKey($domain, $k)
{
$key = md5($domain);
$lkey = substr($key, 0, strlen($key) / 2);
$rkey = substr($key, strlen($key) / 2);
$r = $rkey.$lkey;
if ($k == $r) return true;
return false;
}
function _ParseLicense($txt)
{
// global $i_User, $i_Pswd, $i_Keys;
if (!$this->_falseIsLocalSite($txt)) {
$nah = false;
}
$data = $this->_decrypt($txt, 'beagle');
$i_User = $i_Pswd = '';
$i_Keys = Array();
$lines = explode("\n", $data);
for ($x = 0; $x < count($lines); $x++) {
$l = $lines[$x];
$p = explode('=', $l, 2);
switch($p[0]) {
case 'Username':
$i_User = $p[1];
break;
case 'UserPass':
$i_Pswd = $p[1];
break;
default:
if (substr($p[0], 0, 3) == 'key') {
$parts = explode('|', $p[1]);
if ($this->_VerifyKey($parts[0], $parts[1])) {
unset($K);
$k['domain'] = $parts[0];
$k['key'] = $parts[1];
$k['desc'] = $parts[2];
$k['mod'] = $parts[3];
$i_Keys[] = $k;
}
}
break;
}
}
return Array ($i_User, $i_Pswd, $i_Keys);
}
function _GetObscureValue($i)
{
if ($i == 'x') return 0254; $z = '';
if ($i == 'z') return 0x7F.'.';
if ($i == 'c') return '--code--';
if ($i >= 5 && $i < 7) return $this->_GetObscureValue($z)*$this->_GetObscureValue('e');
if ($i > 30) return Array(0x6c,0x6f,0x63,0x61,0x6c,0x68,0x6f,0x73,0x74);
if ($i > 20) return 99;
if ($i > 10) return '.'.($this->_GetObscureValue(6.5)+1);
if ($i == 'a') return 0xa;
}
function _Chr($val)
{
$x = $this->_GetObscureValue(25);
$f = chr($x).chr($x+5).chr($x+15);
return $f($val);
}
function _IsLocalSite($domain)
{
$ee = $this->_GetObscureValue(35); $yy = '';
foreach ($ee as $e) $yy .= $this->_Chr($e);
$localb = FALSE;
if(substr($domain,0,3)==$this->_GetObscureValue('x'))
{
$b = substr($domain,0,6);
$p = explode(".",$domain);
$subnet = $p[1];
if($p[1]>15 && $p[1]<32)
$localb=TRUE;
}
$zz = $this->_GetObscureValue('z').$this->_GetObscureValue(5).'.'.(int)$this->_GetObscureValue(7).$this->_GetObscureValue(12);
$ff = $this->_GetObscureValue('z')+65;
$hh = $ff-0x18;
if($domain==$yy || $domain==$zz || substr($domain,0,7)==$ff.$this->_Chr(46).$hh ||
substr($domain,0,3)==$this->_GetObscureValue('a').$this->_Chr(46) || $localb || strpos($domain,".")==0)
{
return TRUE;
}
return FALSE;
}
function _falseIsLocalSite($domain)
{
$localb = FALSE;
if(substr($domain,0,3)=="172")
{
$b = substr($domain,0,6);
$p = explode(".",$domain);
$subnet = $p[1];
if($p[1]>15 && $p[1]<32)
$localb=TRUE;
}
if($domain=="localhost" || $domain=="127.0.0.1" || substr($domain,0,7)=="192.168" ||
substr($domain,0,3)=="10." || $localb || strpos($domain,".")==0)
{
return TRUE;
}
return FALSE;
}
function verifyLicense($license_hash)
{
$license_hash = base64_decode($license_hash);
list ($license_user, $license_password, ) = $this->_ParseLicense($license_hash);
return strlen($license_user) && strlen($license_password);
}
function moduleInstalled($module_name)
{
static $modules = null;
if (is_null($modules)) {
$sql = 'SELECT LOWER(Name)
FROM ' . $this->Application->getUnitOption('mod', 'TableName');
$modules = $this->Conn->GetCol($sql);
}
if ($module_name == 'kernel') {
$module_name = 'in-portal';
}
return in_array(strtolower($module_name), $modules);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/permissions_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/permissions_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/permissions_helper.php (revision 14628)
@@ -1,781 +1,784 @@
<?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 kPermissionsHelper extends kHelper {
/**
* Current set of permissions for group being edited
*
* @var Array
*/
var $Permissions = Array();
function LoadPermissions($group_id, $cat_id, $type = 1, $prefix = '')
{
$perm_table = $this->Application->getUnitOption('perm', 'TableName');
$perm_table = $this->Application->GetTempName($perm_table, 'prefix:'.$prefix);
$sql = 'SELECT *
FROM '.$perm_table.'
WHERE (GroupId = '.$group_id.') AND (CatId = '.$cat_id.') AND (Type = '.$type.')';
$permissions = $this->Conn->Query($sql, 'Permission');
$this->Permissions = Array();
foreach ($permissions as $perm_name => $perm_options) {
$perm_record['value'] = $perm_options['PermissionValue'];
$perm_record['id'] = $perm_options['PermissionId'];
$this->Permissions[$perm_name] = $perm_record;
}
}
function getPermissionValue($perm_name)
{
return isset($this->Permissions[$perm_name]) ? $this->Permissions[$perm_name]['value'] : 0;
}
function getPermissionID($perm_name)
{
return isset($this->Permissions[$perm_name]) ? $this->Permissions[$perm_name]['id'] : 0;
}
/**
* This is old permission like ADMIN or LOGIN
*
* @param string $section_name
* @param string $perm_name
* @return bool
*/
function isOldPermission($section_name, $perm_name)
{
return $section_name == 'in-portal:root' && $perm_name != 'view';
}
/**
* Returns permission names to check based on event name and item prefix (main item or subitem)
*
* @param kEvent $event
* @return Array
*/
function getPermissionByEvent(&$event, $perm_mapping)
{
$top_prefix = $event->getEventParam('top_prefix');
$pefix_type = ($top_prefix == $event->Prefix) ? 'self' : 'subitem';
$perm_mapping = getArrayValue($perm_mapping, $event->Name);
if (!$perm_mapping[$pefix_type]) {
throw new Exception('Permission mappings not defined for event <strong>' . $top_prefix . ' <- ' . $event->Prefix . ':' . $event->Name . '</strong>');
}
if ($perm_mapping[$pefix_type] === true) {
// event is defined in mapping but is not checked by permissions
return true;
}
return explode('|', $perm_mapping[$pefix_type]);
}
/**
* Common event permission checking method
*
* @param kEvent $event
*/
function CheckEventPermission(&$event, $perm_mapping)
{
$section = $event->getSection();
if (preg_match('/^CATEGORY:(.*)/', $section)) {
return $this->CheckEventCategoryPermission($event, $perm_mapping);
}
$top_prefix = $event->getEventParam('top_prefix');
$check_perms = $this->getPermissionByEvent($event, $perm_mapping);
if ($check_perms === true) {
// event is defined in mapping but is not checked by permissions
return true;
}
$perm_status = false;
foreach ($check_perms as $perm_name) {
// check if at least one of required permissions is set
if ($perm_name == 'debug' && $this->Application->isDebugMode(false)) {
// universal "debug" permission
return true;
}
$perm_name = $section.'.'.$perm_name;
$perm_status = $this->CheckPermission($perm_name, 1);
if (($perm_name == $section.'.add') && $perm_status && ($top_prefix == $event->Prefix)) {
// main item, add permission allowed, but ID is > 0, then deny permission
// how to get id here
}
if ($perm_status) {
return $perm_status;
}
}
return $this->finalizePermissionCheck($event, $perm_status);
}
/**
* Returns owner + primary category for each item (used for permission checking)
*
* @param string $prefix
* @param string $ids
* @param bool $temp_mode
* @return Array
* @author Alex
*/
function GetCategoryItemData($prefix, $ids, $temp_mode = false)
{
if (is_array($ids)) {
$ids = implode(',', $ids);
}
$id_field = $this->Application->getUnitOption($prefix, 'IDField');
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
$ci_table = $this->Application->getUnitOption('ci', 'TableName');
if ($temp_mode) {
$table_name = $this->Application->GetTempName($table_name, 'prefix:' . $prefix);
$ci_table = $this->Application->GetTempName($ci_table, 'prefix:' . $prefix);
}
$owner_field = $this->Application->getUnitOption($prefix, 'OwnerField');
if (!$owner_field) {
$owner_field = 'CreatedById';
}
$sql = 'SELECT item_table.'.$id_field.', item_table.'.$owner_field.' AS CreatedById, ci.CategoryId
FROM '.$table_name.' item_table
LEFT JOIN '.$ci_table.' ci ON ci.ItemResourceId = item_table.ResourceId
WHERE item_table.'.$id_field.' IN ('.$ids.') AND (ci.PrimaryCat = 1)';
return $this->Conn->Query($sql, $id_field);
}
/**
* Check category-based permissions for category items
*
* @param kEvent $event
*/
function _frontCheckEventCategoryPermission(&$event, $event_perm_mapping)
{
// mapping between specific permissions and common permissions
static $perm_mapping = Array(
'add' => 'ADD', 'add.pending' => 'ADD.PENDING', 'edit' => 'MODIFY',
'edit.pending' => 'MODIFY.PENDING', 'delete' => 'DELETE', 'view' => 'VIEW',
'debug' => 'DEBUG'
);
$top_prefix = $event->getEventParam('top_prefix');
$event_handler =& $this->Application->recallObject($event->Prefix.'_EventHandler');
/* @var $event_handler kCatDBEventHandler */
$raise_warnings = $event->getEventParam('raise_warnings');
$event->setEventParam('raise_warnings', 0);
if ($event->Prefix != $top_prefix) {
$top_event = new kEvent($top_prefix.':'.$event->Name);
$id = $event_handler->getPassedID($top_event);
}
else {
$id = $event_handler->getPassedID($event);
}
$event->setEventParam('raise_warnings', $raise_warnings);
$owner_id = USER_ROOT; // owner is root if not detected
if (!$id) {
// item being created -> check by current (before editing started, saved in OnPreCreate event) category permissions
// note: category in session is placed on catalog data import start
$category_id = $this->Application->isAdmin ? $this->Application->RecallVar('m_cat_id') : $this->Application->GetVar('m_cat_id');
}
elseif ($top_prefix == 'c' || $top_prefix == 'st') {
$category_id = $id;
}
else {
// item being edited -> check by it's primary category permissions
$items_info = $this->GetCategoryItemData($top_prefix, $id);
if ($items_info) {
$category_id = $items_info[$id]['CategoryId'];
$owner_id = $items_info[$id]['CreatedById'];
}
else {
// item wasn't found in database
$category_id = $this->Application->GetVar('m_cat_id');
}
}
// specific permission check for pending & owner permissions: begin
$uploader_events = Array ('OnUploadFile', 'OnDeleteFile', 'OnViewFile');
if (in_array($event->Name, $uploader_events)) {
// don't recall target object during uploader-related, because OnItemLoad will use incorrect
// $user_id in Firefox (during Flash problems session will be used from Internet Exploere)
$new_item = false;
}
else {
$new_item = $this->Application->isAdminUser && $event_handler->isNewItemCreate($event) ? true : false;
$check_status = $this->checkCombinedPermissions($event, $owner_id, (int)$category_id, $new_item);
}
if (isset($check_status)) {
return $this->finalizePermissionCheck($event, $check_status);
}
// specific permission check for pending & owner permissions: end
$perm_status = false;
$check_perms = $this->getPermissionByEvent($event, $event_perm_mapping);
if ($check_perms === true) {
// event is defined in mapping but is not checked by permissions
return true;
}
$item_prefix = $this->Application->getUnitOption($top_prefix, 'PermItemPrefix');
foreach ($check_perms as $perm_name) {
// check if at least one of required permissions is set
if (!isset($perm_mapping[$perm_name])) {
// not mapped permission (e.g. advanced:approve) -> skip
continue;
}
if ($perm_name == 'debug' && $this->Application->isDebugMode(false)) {
// universal "debug" permission
return true;
}
$perm_name = $item_prefix.'.'.$perm_mapping[$perm_name];
$perm_status = $this->CheckPermission($perm_name, 0, (int)$category_id);
if ($perm_status) {
return $perm_status;
}
}
return $this->finalizePermissionCheck($event, $perm_status);
}
/**
* Finalizes permission checking (with additional debug output, when in debug mode)
*
* @param kEvent $event
* @param bool $perm_status
* @return bool
*/
function finalizePermissionCheck(&$event, $perm_status)
{
if (!$perm_status) {
if (MOD_REWRITE) {
// $event->SetRedirectParam('m_cat_id', 0); // category means nothing on admin login screen
$event->SetRedirectParam('next_template', urlencode('external:' . $_SERVER['REQUEST_URI']));
}
else {
$event->SetRedirectParam('next_template', $this->Application->GetVar('t'));
}
if ($this->Application->isDebugMode()) {
// for debugging purposes
$event->SetRedirectParam('section', $event->getSection());
$event->SetRedirectParam('main_prefix', $event->getEventParam('top_prefix'));
$event->SetRedirectParam('event_name', $event->Name);
}
$event->status = kEvent::erPERM_FAIL;
}
return $perm_status;
}
/**
* Allows to check combined permissions (*.owner, *.pending) for add/modify/delete operations from admin & front-end
*
* @param kEvent $event
* @param int $owner_id
* @param int $category_id
* @param bool $new_item
* @return mixed
*/
function checkCombinedPermissions(&$event, $owner_id, $category_id, $new_item = false)
{
$ret = null; // true/false when used, null when not used
$top_prefix = $event->getEventParam('top_prefix');
// check admin permission
if (substr($event->Name, 0, 9) == 'OnPreSave') {
if ($new_item) {
$ret = $this->AddCheckPermission($category_id, $top_prefix);
}
else {
// add & modify because $new_item is false, when item is aready created & then saved in temp table (even with 0 id)
$ret = $this->AddCheckPermission($category_id, $top_prefix) ||
$this->ModifyCheckPermission($owner_id, $category_id, $top_prefix);
}
}
// check front-end permissions
switch ($event->Name) {
case 'OnCreate':
$ret = $this->AddCheckPermission($category_id, $top_prefix);
break;
case 'OnUpdate':
$ret = $this->ModifyCheckPermission($owner_id, $category_id, $top_prefix);
break;
case 'OnDelete':
case 'OnMassDelete':
$ret = $this->DeleteCheckPermission($owner_id, $category_id, $top_prefix);
break;
}
if ($ret === 0) {
// permission check failed (user has no permission)
$event->status = kEvent::erPERM_FAIL;
}
return $ret;
}
/**
* Simplified permission check for category items, when adding/editing them from advanced view.
*
* @param kEvent $event
* @return mixed
*/
function CheckEventCategoryPermission(&$event, $event_perm_mapping)
{
if (!$this->Application->isAdmin) {
// check front-end permission by old scheme
return $this->_frontCheckEventCategoryPermission($event, $event_perm_mapping);
}
if (substr($event->Name, 0, 9) == 'OnPreSave') {
// check separately, because permission mapping is not defined for OnPreSave* events
$check_perms = Array ('add', 'edit');
}
else {
$check_perms = $this->getPermissionByEvent($event, $event_perm_mapping);
}
if ($check_perms === true) {
// event is defined in mapping but is not checked by permissions
return true;
}
// 1. most of events does require admin login only
$perm_status = $this->Application->isAdminUser;
// 2. in case, when event require more, then "view" right, then restrict it to temporary tables only
if (!in_array('view', $check_perms)) {
$perm_status = $perm_status && $this->Application->IsTempMode($event->Prefix, $event->Special);
}
return $this->finalizePermissionCheck($event, $perm_status);
}
function TagPermissionCheck($params, $is_owner = false)
{
$perm_prefix = getArrayValue($params, 'perm_prefix');
$perm_event = getArrayValue($params, 'perm_event');
$permission_groups = getArrayValue($params, 'permissions');
if ($permission_groups && !$perm_event) {
// check permissions by permission names in current category
$permission_groups = explode('|', $permission_groups);
$group_has_permission = false;
$perm_category = isset($params['cat_id']) ? $params['cat_id'] : $this->Application->GetVar('m_cat_id');
if ($perm_prefix) {
// use primary category of item with id from {perm_prefix}_id as base for permission checking
$perm_category = $this->getPrimaryCategory($perm_prefix);
}
$is_system = isset($params['system']) && $params['system'] ? 1 : 0;
foreach ($permission_groups as $permission_group) {
$permissions = explode(',', $permission_group);
$has_permission = true;
foreach ($permissions as $permission) {
$owner_checked = (strpos($permission, '.OWNER.') !== false) ? $is_owner : true;
$has_permission = $has_permission && $this->CheckPermission($permission, $is_system, $perm_category) && $owner_checked;
}
$group_has_permission = $group_has_permission || $has_permission;
if ($group_has_permission) {
return true;
}
}
return false;
}
elseif ($perm_event) {
// check permission by event name
list ($prefix, $event) = explode(':', $perm_event);
+
$event_handler =& $this->Application->recallObject($prefix.'_EventHandler');
+ /* @var $event_handler kEventHandler */
+
return $event_handler->CheckPermission( new kEvent($perm_event) );
}
return true;
}
/**
* Returns item's primary category (get item_id from request)
*
* @param string $prefix
* @return int
*/
function getPrimaryCategory($prefix)
{
$id_field = $this->Application->getUnitOption($prefix, 'IDField');
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
$id = $this->Application->GetVar($prefix.'_id');
if (!$id) {
return $this->Application->GetVar('m_cat_id');
}
$sql = 'SELECT ResourceId
FROM '.$table_name.'
WHERE '.$id_field.' = '.(int)$id;
$resource_id = $this->Conn->GetOne($sql);
$sql = 'SELECT CategoryId
FROM '.$this->Application->getUnitOption('ci', 'TableName').'
WHERE ItemResourceId = '.$resource_id.' AND PrimaryCat = 1';
return $this->Conn->GetOne($sql);
}
/**
* Returns no permission template to redirect to
*
* @param Array $params
* @return Array
*/
function getPermissionTemplate($params)
{
$t = $this->Application->GetVar('t');
if ($next_t = getArrayValue($params, 'next_template')) {
$t = $next_t;
}
$redirect_params = $this->Application->HttpQuery->getRedirectParams(true);
if (array_key_exists('pass_category', $params)) {
$redirect_params['pass_category'] = $params['pass_cateogry'];
}
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, // category means nothing on admin login screen
'next_template' => urlencode('external:' . $_SERVER['REQUEST_URI']),
);
}
else {
$redirect_params['next_template'] = $t;
}
if ($this->Application->isAdmin) {
$redirect_params['m_wid'] = ''; // remove wid, otherwise parent window may add wid to its name breaking all the frameset (for <a> targets)
$redirect_params['pass'] = 'm'; // don't pass any other (except "m") prefixes to admin login template
}
if (!$this->Application->LoggedIn()) {
$redirect_template = array_key_exists('login_template', $params) ? $params['login_template'] : '';
if (!$redirect_template && $this->Application->isAdmin) {
$redirect_template = 'login';
}
}
else {
if (array_key_exists('no_permissions_template', $params)) {
$redirect_template = $params['no_permissions_template'];
}
else {
$redirect_template = $this->Application->isAdmin ? 'no_permission' : $this->Application->ConfigValue('NoPermissionTemplate');
}
if ($this->Application->isDebugMode()) {
$redirect_params['from_template'] = 1;
$redirect_params['perms'] = $params[ isset($params['permissions']) ? 'permissions' : 'perm_event' ];
}
}
if (isset($params['index_file']) && $params['index_file']) {
$redirect_params['index_file'] = $params['index_file'];
}
return Array ($redirect_template, $redirect_params);
}
/**
* Check current user permissions based on it's group permissions in specified category (for non-system permissions) or just checks if system permission is set
*
* @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
*/
function CheckPermission($name, $type = 1, $cat_id = null)
{
$user_id = $this->Application->RecallVar('user_id');
return $this->CheckUserPermission($user_id, $name, $type, $cat_id);
}
function CheckUserPermission($user_id, $name, $type = 1, $cat_id = null)
{
if ($user_id == USER_ROOT) {
// "root" is allowed anywhere
return $name == 'SYSTEM_ACCESS.READONLY' ? 0 : 1;
}
if (!isset($cat_id)) {
$cat_id = $this->Application->GetVar('m_cat_id');
}
if ($type == 1) {
// "system" permission are always checked per "Home" category (ID = 0)
$cat_id = 0;
}
elseif ("$cat_id" === "0") {
$cat_id = $this->Application->getBaseCategory();
}
// perm cache is build only based on records in db, that's why if permission is not explicitly denied, then
// that (perm cache creator) code thinks that it is allowed & adds corresponding record and code below will
// return incorrect results
if ($user_id == $this->Application->RecallVar('user_id')) {
$groups = explode(',', $this->Application->RecallVar('UserGroups'));
}
else { // checking not current user
$sql = 'SELECT GroupId
FROM '.TABLE_PREFIX.'UserGroup
WHERE (PortalUserId = '.$user_id.') AND ( (MembershipExpires IS NULL) OR ( MembershipExpires >= UNIX_TIMESTAMP() ) )';
$groups = $this->Conn->GetCol($sql);
array_push($groups, $this->Application->ConfigValue('User_LoggedInGroup') );
}
$cache_key = $name . '|' . $type . '|' . $cat_id . '|' . implode(',', $groups);
$perm_value = $this->Application->getCache('permissions[%' . ($type == 1 ? 'G' : 'C') . 'PermSerial%]:' . $cache_key);
if ($perm_value !== false) {
return $perm_value;
}
if (preg_match('/(.*)\.VIEW$/', $name) && ($type == 0)) {
// cached view permission of category: begin
if (strpos($cat_id, '|') !== false) {
$category_path = explode('|', substr($cat_id, 1, -1));
$cat_id = end($category_path);
}
$sql = 'SELECT PermissionConfigId
FROM '.TABLE_PREFIX.'PermissionConfig
WHERE PermissionName = '.$this->Conn->qstr($name);
$perm_id = $this->Conn->GetOne($sql);
$sql = 'SELECT PermId
FROM '.TABLE_PREFIX.'PermCache
WHERE (PermId = '.$perm_id.') AND (CategoryId = '.(int)$cat_id.')';
$view_filters = Array();
foreach ($groups as $group) {
$view_filters[] = 'FIND_IN_SET('.$group.', ACL)';
}
$sql .= ' AND ('.implode(' OR ', $view_filters).')';
$perm_value = $this->Conn->GetOne($sql) ? 1 : 0;
$this->Application->setCache('permissions[%CPermSerial%]:' . $cache_key, $perm_value);
return $perm_value;
// cached view permission of category: end
}
if (is_numeric($cat_id) && $cat_id == 0) {
$cat_hierarchy = Array(0);
}
else {
if (strpos($cat_id, '|') !== false) {
$cat_hierarchy = $cat_id;
}
else {
$sql = 'SELECT ParentPath
FROM '.$this->Application->getUnitOption('c', 'TableName').'
WHERE CategoryId = '.$cat_id;
$cat_hierarchy = $this->Conn->GetOne($sql);
if ($cat_hierarchy === false) {
// category was deleted, but refrence to it stays in other tables -> data integrity is broken
$cat_hierarchy = '|' . $this->Application->getBaseCategory() . '|';
}
}
$cat_hierarchy = explode('|', substr($cat_hierarchy, 1, -1));
$cat_hierarchy = array_reverse($cat_hierarchy);
array_push($cat_hierarchy, 0);
}
$perm_value = 0;
$groups = implode(',',$groups);
foreach ($cat_hierarchy as $category_id) {
$sql = 'SELECT SUM(PermissionValue)
FROM '.TABLE_PREFIX.'Permissions
WHERE Permission = "'.$name.'" AND CatId = '.$category_id.' AND GroupId IN ('.$groups.') AND Type = '.$type;
$res = $this->Conn->GetOne($sql);
if ($res !== false && !is_null($res)) {
$perm_value = $res ? 1 : 0;
break;
}
}
$this->Application->setCache('permissions[%' . ($type == 1 ? 'G' : 'C') . 'PermSerial%]:' . $cache_key, $perm_value);
return $perm_value;
}
/**
* Returns categories, where given permission is set to "1"
*
* @param string $permission_name
* @return Array
*/
function getPermissionCategories($permission_name)
{
$groups = $this->Application->RecallVar('UserGroups');
// get categories, where given permission is explicitely defined
$sql = 'SELECT SUM(PermissionValue), CatId
FROM ' . TABLE_PREFIX . 'Permissions
WHERE Permission = "' . $permission_name . '" AND GroupId IN (' . $groups . ') AND Type = 0
GROUP BY CatId';
$permissions = $this->Conn->GetCol($sql, 'CatId');
// get all categories along with their parent path
$sql = 'SELECT ParentPath, CategoryId
FROM ' . TABLE_PREFIX . 'Category';
$parent_paths = $this->Conn->GetCol($sql, 'CategoryId');
foreach ($parent_paths as $category_id => $parent_path) {
if (array_key_exists($category_id, $permissions)) {
// permission for given category is set explicitly
continue;
}
$perm_value = 0;
$parent_path = explode('|', substr($parent_path, 1, -1));
$parent_path = array_reverse($parent_path);
array_push($parent_path, 0);
foreach ($parent_path as $parent_category_id) {
if (array_key_exists($parent_category_id, $permissions)) {
$perm_value = $permissions[$parent_category_id] ? 1 : 0;
break;
}
}
$permissions[$category_id] = $perm_value;
}
// remove categories, where given permissions is denied
foreach ($permissions as $category_id => $perm_value) {
if (!$perm_value) {
unset($permissions[$category_id]);
}
}
return array_keys($permissions);
}
/**
* Allows to check MODIFY & OWNER.MODFY +/- PENDING permission combinations on item
*
* @param int $owner_id user_id, that is owner of the item
* @param int $category_id primary category of item
* @param string $prefix prefix of item
* @return int {0 - no MODIFY permission, 1 - has MODIFY permission, 2 - has MODIFY.PENDING permission}
*/
function ModifyCheckPermission($owner_id, $category_id, $prefix)
{
$perm_prefix = $this->Application->getUnitOption($prefix, 'PermItemPrefix');
$live_modify = $this->CheckPermission($perm_prefix.'.MODIFY', ptCATEGORY, $category_id);
if ($live_modify) {
return 1;
}
else if ($this->CheckPermission($perm_prefix.'.MODIFY.PENDING', ptCATEGORY, $category_id)) {
return 2;
}
if ($owner_id == $this->Application->RecallVar('user_id')) {
// user is item's OWNER -> check this permissions first
$live_modify = $this->CheckPermission($perm_prefix.'.OWNER.MODIFY', ptCATEGORY, $category_id);
if ($live_modify) {
return 1;
}
else if ($this->CheckPermission($perm_prefix.'.OWNER.MODIFY.PENDING', ptCATEGORY, $category_id)) {
return 2;
}
}
return 0;
}
/**
* Allows to check DELETE & OWNER.DELETE permission combinations on item
*
* @param int $owner_id user_id, that is owner of the item
* @param int $category_id primary category of item
* @param string $prefix prefix of item
* @return int {0 - no DELETE permission, 1 - has DELETE/OWNER.DELETE permission}
*/
function DeleteCheckPermission($owner_id, $category_id, $prefix)
{
$perm_prefix = $this->Application->getUnitOption($prefix, 'PermItemPrefix');
$live_delete = $this->CheckPermission($perm_prefix.'.DELETE', ptCATEGORY, $category_id);
if ($live_delete) {
return 1;
}
if ($owner_id == $this->Application->RecallVar('user_id')) {
// user is item's OWNER -> check this permissions first
$live_delete = $this->CheckPermission($perm_prefix.'.OWNER.DELETE', ptCATEGORY, $category_id);
if ($live_delete) {
return 1;
}
}
return 0;
}
/**
* Allows to check ADD +/- PENDING permission combinations on item
*
* @param int $category_id primary category of item
* @param string $prefix prefix of item
* @return int {0 - no ADD permission, 1 - has ADD permission, 2 - has ADD.PENDING permission}
*/
function AddCheckPermission($category_id, $prefix)
{
$perm_prefix = $this->Application->getUnitOption($prefix, 'PermItemPrefix');
$live_add = $this->CheckPermission($perm_prefix.'.ADD', ptCATEGORY, $category_id);
if ($live_add) {
return 1;
}
else if ($this->CheckPermission($perm_prefix.'.ADD.PENDING', ptCATEGORY, $category_id)) {
return 2;
}
return 0;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/template_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/template_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/template_helper.php (revision 14628)
@@ -1,437 +1,438 @@
<?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.
*/
class TemplateHelper extends kHelper {
/**
* parser element location information
*
* @var Array
*/
var $_blockLocation = Array ();
/**
* Block name, that will be used
*
* @var string
*/
var $_blockName = '';
/**
* Function name, that represents compiled block
*
* @var sting
*/
var $_functionName = '';
/**
* Errors found during template parsing
*
* @var Array
*/
var $_parseErrors = Array ();
/**
* Source template, that is being edited
*
* @var string
*/
var $_sourceTemplate = '';
var $_initMade = false;
/**
* Performs init ot helper
*
* @param kDBItem $object
*/
function InitHelper(&$object)
{
if ($this->_initMade) {
return ;
}
// 1. get block information
$block_info = $this->Application->GetVar('block');
list ($this->_blockName, $this->_functionName) = explode(':', $block_info);
$this->_parseTemplate($object);
if (array_key_exists($this->_functionName, $this->Application->Parser->ElementLocations)) {
$this->_blockLocation = $this->Application->Parser->ElementLocations[$this->_functionName];
}
$this->_initMade = true;
}
function _getSourceTemplate()
{
// get source template
$t = $this->Application->GetVar('source');
if (!$this->Application->TemplatesCache->TemplateExists($t)) {
$cms_handler =& $this->Application->recallObject('st_EventHandler');
- /* @var $cms_handler StructureEventHandler */
+ /* @var $cms_handler CategoriesEventHandler */
$t = ltrim($cms_handler->GetDesignTemplate($t), '/');
}
$this->_sourceTemplate = $t;
}
function _getThemeName()
{
$theme_id = (int)$this->Application->GetVar('theme_id');
$sql = 'SELECT Name
FROM ' . $this->Application->getUnitOption('theme', 'TableName') . '
WHERE ' . $this->Application->getUnitOption('theme', 'IDField') . ' = ' . $theme_id;
return $this->Conn->GetOne($sql);
}
/**
* Render source template to get parse errors OR it's element locations
*
* @param kDBItem $object
* @param string $append
* @return bool
*/
function _parseTemplate(&$object, $append = '')
{
try {
// 1. parse template
$this->Application->InitParser( $this->_getThemeName() ); // we have no parser when saving block content
$this->_getSourceTemplate();
// 2. design templates have leading "/" in the beginning
$this->Application->Parser->Run($this->_sourceTemplate . $append);
}
catch (ParserException $e) {
$this->_parseErrors[] = Array ('msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine());
}
if ($this->_parseErrors) {
if ($this->_isMainTemplate()) {
// 2.1. delete temporary file, that was parsed
$filename = $this->_getTemplateFile(false, $append . '.tpl');
if (!unlink($filename)) {
$error_file = $this->_getTemplateFile(true, $append . '.tpl');
$object->SetError('FileContents', 'template_delete_failed', '+Failed to delete temporary template "<strong>' . $error_file . '</strong>"');
return false;
}
}
else {
// 2.2. restore backup
if (!rename($this->_getTemplateFile(false, '.tpl.bak'), $this->_getTemplateFile(false))) {
$error_file = $this->_getTemplateFile(true);
$object->SetError('FileContents', 'template_restore_failed', '+Failed to restore template "<strong>' . $error_file . '</strong>" from backup.');
return false;
}
}
return false;
}
return true;
}
/**
* Move elements in template and save changes, when possible
*
* @param Array $target_order
* @return bool
*/
function moveTemplateElements($target_order)
{
// 2. parse template
$this->Application->InitParser(); // we have no parser when saving block content
$this->_getSourceTemplate();
$filename = $this->Application->TemplatesCache->GetRealFilename($this->_sourceTemplate) . '.tpl';
if (!is_writable($filename)) {
// we can't save changes, don't bother calculating new template contents
return false;
}
$data = file_get_contents($filename);
$line_ending = strpos($data, "\r") !== false ? "\r\n" : "\n";
// 1. get location of movable areas
$mask = '';
$start_pos = 0;
- $elements = Array ();
+ $elements = $area = Array ();
$areas = $this->_getDivPairs($data, 'movable-area');
+
foreach ($areas as $area_index => $area) {
// 1.1. get locations of all movable elements inside given area
$area_content = substr($area['data'], $area['open_len'], -$area['close_len']);
$elements = array_merge($elements, $this->_getDivPairs($area_content, 'movable-element', $area_index, $area['open_pos'] + $area['open_len']));
// 1.2. prepare mask to place movable elements into (don't include movable area div ifself)
$mask .= "\t" . substr($data, $start_pos, $area['open_pos'] + $area['open_len'] - $start_pos) . $line_ending . "\t\t" . '#AREA' . $area_index . '#' . $line_ending;
$start_pos = $area['close_pos'] - $area['close_len'];
}
$mask = trim($mask . "\t" . substr($data, $area['close_pos'] - $area['close_len']));
if (!$elements) {
// no elements found
return false;
}
foreach ($areas as $area_index => $area) {
$area_content = '';
$target_elements = $target_order[$area_index];
foreach ($target_order[$area_index] as $old_location) {
$area_content .= $elements[$old_location]['data'] . $line_ending . "\t\t";
}
$mask = str_replace('#AREA' . $area_index . '#', trim($area_content), $mask);
}
$fp = fopen($filename, 'w');
fwrite($fp, $mask);
fclose($fp);
return true;
}
/**
* Extracts div pairs with given class from given text
*
* @param string $data
* @param string $class
* @param int $area
* @param int $offset
* @return Array
*/
function _getDivPairs(&$data, $class, $area = null, $offset = 0)
{
preg_match_all('/(<div[^>]*>)|(<\/div>)/s', $data, $divs, PREG_SET_ORDER + PREG_OFFSET_CAPTURE);
$deep_level = 0;
$pairs = Array ();
$skip_count = Array (); // by deep level!
foreach ($divs as $div) {
if (strpos($div[0][0], '/') === false) {
// opening div
$skip_count[$deep_level] = 0;
if (strpos($div[0][0], $class) !== false) {
// ours opening (this deep level) -> save
$pair = Array ('open_pos' => $div[0][1], 'open_len' => strlen($div[0][0]));
}
else {
// not ours opening -> skip next closing (this deep level)
$skip_count[$deep_level]++;
}
$deep_level++;
}
else {
// closing div
$deep_level--;
if ($skip_count[$deep_level] == 0) {
// nothing to skip (this deep level) -> save
$pair['close_len'] = strlen($div[0][0]);
$pair['close_pos'] = $div[0][1] + $pair['close_len'];
$pair['data'] = substr($data, $pair['open_pos'], $pair['close_pos'] - $pair['open_pos']);
if (isset($area)) {
$pair['open_pos'] += $offset;
$pair['close_pos'] += $offset;
// index indicates area
$pairs['a' . $area . 'e' . count($pairs)] = $pair;
}
else {
$pairs[] = $pair;
}
}
else {
// skip closing div as requested
$skip_count[$deep_level]--;
}
}
}
return $pairs;
}
/**
* Returns information about parser element locations in template
*
* @param Array $params
* @return mixed
*/
function blockInfo($info_type)
{
switch ($info_type) {
case 'block_name':
return $this->_blockName;
break;
case 'function_name':
return $this->_functionName;
break;
case 'start_pos':
case 'end_pos':
case 'template':
if (!array_key_exists($info_type, $this->_blockLocation)) {
// invalid block name
return 'invalid block name';
}
return $this->_blockLocation[$info_type];
break;
case 'template_file':
return $this->_getTemplateFile(true);
break;
case 'content':
$template_body = file_get_contents( $this->_getTemplateFile() );
$length = $this->_blockLocation['end_pos'] - $this->_blockLocation['start_pos'];
return substr($template_body, $this->_blockLocation['start_pos'], $length);
break;
}
return 'undefined';
}
/**
* Main template being edited (parse copy, instead of original)
*
* @return bool
*/
function _isMainTemplate()
{
return $this->_blockLocation['template'] == $this->_sourceTemplate;
}
/**
* Returns filename, that contains template, where block is located
*
* @return string
*/
function _getTemplateFile($relative = false, $extension = '.tpl')
{
$filename = $this->Application->TemplatesCache->GetRealFilename( $this->_blockLocation['template'] ) . $extension;
if ($relative) {
$filename = preg_replace('/^' . preg_quote(FULL_PATH, '/') . '/', '', $filename, 1);
}
return $filename;
}
/**
* Saves new version of block to template, where it's located
*
* @param kDBItem $object
*/
function saveBlock(&$object)
{
$main_template = $this->_isMainTemplate();
$filename = $this->_getTemplateFile(false);
// 1. get new template content
$new_template_body = $this->_getNewTemplateContent($object, $filename, $lines_before);
if (is_bool($new_template_body) && ($new_template_body === true)) {
// when nothing changed -> stop processing
return true;
}
// 2. backup original template
if (!$main_template && !copy($filename, $filename . '.bak')) {
// backup failed
$error_file = $this->_getTemplateFile(true, '.tpl.bak');
$object->SetError('FileContents', 'template_backup_failed', '+Failed to create backup template "<strong>' . $error_file . '</strong>" backup.');
return false;
}
// 3. save changed template
$save_filename = $this->_getTemplateFile(false, $main_template ? '.tmp.tpl' : '.tpl');
$fp = fopen($save_filename, 'w');
if (!$fp) {
// backup template create failed OR existing template save
$error_file = $this->_getTemplateFile(true, $main_template ? '.tmp.tpl' : '.tpl');
$object->SetError('FileContents', 'template_changes_save_failed', '+Failed to save template "<strong>' . $error_file . '</strong>" changes.');
return false;
}
fwrite($fp, $new_template_body);
fclose($fp);
// 3. parse template to check for errors
$this->_parseTemplate($object, $main_template ? '.tmp' : '');
if ($this->_parseErrors) {
$error_msg = Array ();
foreach ($this->_parseErrors as $error_data) {
if (preg_match('/line ([\d]+)/', $error_data['msg'], $regs)) {
// another line number inside message -> patch it
$error_data['msg'] = str_replace('line ' . $regs[1], 'line ' . ($regs[1] - $lines_before), $error_data['msg']);
}
$error_msg[] = $error_data['msg'] . ' at line ' . ($error_data['line'] - $lines_before);
}
$object->SetError('FileContents', 'template_syntax_error', '+Template syntax errors:<br/>' . implode('<br/>', $error_msg));
return false;
}
if ($main_template) {
// 4.1. replace original file with temporary
if (!rename($this->_getTemplateFile(false, '.tmp.tpl'), $filename)) {
// failed to save new content to original template
$error_file = $this->_getTemplateFile(true);
$object->SetError('FileContents', 'template_save_failed', '+Failed to save template "<strong>' . $error_file . '</strong>".');
return false;
}
}
else {
// 4.2. delete backup
unlink( $this->_getTemplateFile(false, '.tpl.bak') );
}
return true;
}
/**
* Returns new template content of "true", when nothing is changed
*
* @param kDBItem $object
* @param string $filename
* @param int $lines_before
* @return mixed
*/
function _getNewTemplateContent(&$object, $filename, &$lines_before)
{
$new_content = $object->GetDBField('FileContents');
$template_body = file_get_contents($filename);
$lines_before = substr_count(substr($template_body, 0, $this->_blockLocation['start_pos']), "\n");
$new_template_body = substr($template_body, 0, $this->_blockLocation['start_pos']) .
$new_content .
substr($template_body, $this->_blockLocation['end_pos']);
return crc32($template_body) == crc32($new_template_body) ? true : $new_template_body;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/form_submission_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/form_submission_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/form_submission_helper.php (revision 14628)
@@ -1,119 +1,120 @@
<?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 FormSubmissionHelper extends kHelper {
/**
* Role names for easy usage via FormField tag
*
* @var Array
*/
var $roleNames = Array (
'name' => SubmissionFormField::COMMUNICATION_ROLE_NAME,
'email' => SubmissionFormField::COMMUNICATION_ROLE_EMAIL,
'subject' => SubmissionFormField::COMMUNICATION_ROLE_SUBJECT,
'body' => SubmissionFormField::COMMUNICATION_ROLE_BODY,
);
/**
* Returns submission field based on given role
*
* @param kDBItem $form_submission
* @param string $role
* @return string
*/
function getFieldByRole(&$form_submission, $role, $formatted = false, $format = null)
{
static $cache = Array ();
$form_id = $form_submission->GetDBField('FormId');
if (!array_key_exists($form_id, $cache)) {
$id_field = $this->Application->getUnitOption('formflds', 'IDField');
$table_name = $this->Application->getUnitOption('formflds', 'TableName');
$sql = 'SELECT ' . $id_field . ', EmailCommunicationRole
FROM ' . $table_name . '
WHERE FormId = ' . $form_id . ' AND EmailCommunicationRole <> 0';
$cache[$form_id] = $this->Conn->GetCol($sql, 'EmailCommunicationRole');
}
// convert string representation of role to numeric
if (!is_numeric($role)) {
$role = strtolower($role);
$role = array_key_exists($role, $this->roleNames) ? $this->roleNames[$role] : false;
}
// get field by role
$field_id = array_key_exists($role, $cache[$form_id]) ? $cache[$form_id][$role] : false;
if ($field_id) {
return $formatted ? $form_submission->GetField('fld_' . $field_id, $format) : $form_submission->GetDBField('fld_' . $field_id);
}
return false;
}
/**
* Returns submission field based on given name
*
* @param kDBItem $form_submission
* @param string $name
* @return string
+ * @todo Might not work correctly!
*/
function getFieldByName(&$form_submission, $name, $formatted = false, $format = null)
{
static $cache = Array ();
$form_id = $form_submission->GetDBField('FormId');
if (!array_key_exists($form_id, $cache)) {
$id_field = $this->Application->getUnitOption('formflds', 'IDField');
$table_name = $this->Application->getUnitOption('formflds', 'TableName');
$sql = 'SELECT ' . $id_field . ', FieldName
FROM ' . $table_name . '
WHERE FormId = ' . $form_id;
$cache[$form_id] = $this->Conn->GetCol($sql, 'FieldName');
}
if ($field_id) {
return $formatted ? $form_submission->GetField('fld_' . $field_id, $format) : $form_submission->GetDBField('fld_' . $field_id);
}
return false;
}
/**
* Returns form object field based on form submission
*
* @param $form_submission kDBItem
* @return kDBItem
*/
function &getForm(&$form_submission)
{
$form_id = $form_submission->GetDBField('FormId');
$form =& $this->Application->recallObject('form', null, Array ('skip_autoload' => true));
/* @var $form kDBItem */
- if (!$form->isLoaded() || ($form->GetID() != $form_id)) {
+ if ( !$form->isLoaded() || ($form->GetID() != $form_id) ) {
$form->Load($form_id);
}
return $form;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/themes_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/themes_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/themes_helper.php (revision 14628)
@@ -1,549 +1,550 @@
<?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 kThemesHelper extends kHelper {
/**
* Where all themes are located
*
* @var string
*/
var $themesFolder = '';
/**
* List of theme names, found on system
*
* @var Array
*/
var $_themeNames = Array ();
/**
* Temporary array when all theme files from db are stored
*
* @var Array
*/
var $themeFiles = Array ();
public function __construct()
{
parent::__construct();
$this->themesFolder = FULL_PATH.'/themes';
}
/**
* Updates file system changes to database for selected theme
*
* @param string $theme_name
*
* @return mixed returns ID of created/used theme or false, if none created
*/
function refreshTheme($theme_name)
{
if (!file_exists($this->themesFolder . '/' . $theme_name)) {
// requested theme was not found on hdd
return false;
}
$id_field = $this->Application->getUnitOption('theme', 'IDField');
$table_name = $this->Application->getUnitOption('theme', 'TableName');
$sql = 'SELECT *
FROM ' . $table_name . '
WHERE Name = ' . $this->Conn->qstr($theme_name);
$theme_info = $this->Conn->GetRow($sql);
if ($theme_info) {
$theme_id = $theme_info[$id_field];
$theme_enabled = $theme_info['Enabled'];
}
else {
$theme_id = $theme_enabled = false;
}
$this->themeFiles = Array ();
if ($theme_id) {
if (!$theme_enabled) {
// don't process existing theme files, that are disabled
return $theme_id;
}
// reset found mark for every themes file (if theme is not new)
$sql = 'UPDATE '.TABLE_PREFIX.'ThemeFiles
SET FileFound = 0
WHERE ThemeId = '.$theme_id;
$this->Conn->Query($sql);
// get all theme files from db
$sql = 'SELECT FileId, CONCAT(FilePath, "/", FileName) AS FullPath
FROM '.TABLE_PREFIX.'ThemeFiles
WHERE ThemeId = '.$theme_id;
$this->themeFiles = $this->Conn->GetCol($sql, 'FullPath');
}
else {
// theme was not found in db, but found on hdd -> create new
$theme_info = Array (
'Name' => $theme_name,
'Enabled' => 0,
'Description' => $theme_name,
'PrimaryTheme' => 0,
'CacheTimeout' => 3600, // not in use right now
'StylesheetId' => 0, // not in use right now
'LanguagePackInstalled' => 0
);
$this->Conn->doInsert($theme_info, $table_name);
$theme_id = $this->Conn->getInsertID();
if (!$theme_enabled) {
// don't process newly created theme files, because they are disabled
return $theme_id;
}
}
$this->_themeNames[$theme_id] = $theme_name;
$theme_path = $this->themesFolder.'/'.$theme_name;
$this->FindThemeFiles('', $theme_path, $theme_id); // search from base theme directory
// delete file records from db, that were not found on hdd
$sql = 'DELETE FROM '.TABLE_PREFIX.'ThemeFiles
WHERE ThemeId = '.$theme_id.' AND FileFound = 0';
$this->Conn->Query($sql);
// install language packs, associated with theme (if any found and wasn't aready installed)
if (!$theme_info['LanguagePackInstalled']) {
$this->installThemeLanguagePack($theme_path);
$fields_hash = Array (
'LanguagePackInstalled' => 1,
);
$this->Conn->doUpdate($fields_hash, $table_name, $id_field . ' = ' . $theme_id);
}
$fields_hash = Array (
'TemplateAliases' => serialize( $this->getTemplateAliases($theme_id, $theme_path) ),
);
$this->Conn->doUpdate($fields_hash, $table_name, $id_field . ' = ' . $theme_id);
return $theme_id;
}
/**
* Installs module(-s) language pack for given theme
*
* @param string $theme_path
- * @param string $module_name
+ * @param string|bool $module_name
+ * @return void
*/
function installThemeLanguagePack($theme_path, $module_name = false)
{
- if ($module_name === false) {
+ if ( $module_name === false ) {
$modules = $this->Application->ModuleInfo;
}
else {
$modules = Array ($module_name => $this->Application->ModuleInfo[$module_name]);
}
$language_import_helper =& $this->Application->recallObject('LanguageImportHelper');
/* @var $language_import_helper LanguageImportHelper */
foreach ($modules as $module_name => $module_info) {
- if ($module_name == 'In-Portal') {
+ if ( $module_name == 'In-Portal' ) {
continue;
}
$lang_file = $theme_path . '/' . $module_info['TemplatePath'] . '_install/english.lang';
- if (file_exists($lang_file)) {
+ if ( file_exists($lang_file) ) {
$language_import_helper->performImport($lang_file, '|0|', '', LANG_SKIP_EXISTING);
}
}
}
/**
* Returns template aliases from "/_install/theme.xml" files in theme
*
* @param int $theme_id
* @param string $theme_path
+ * @return Array
+ * @access protected
*/
- function getTemplateAliases($theme_id, $theme_path)
+ protected function getTemplateAliases($theme_id, $theme_path)
{
$template_aliases = Array ();
$xml_parser =& $this->Application->recallObject('kXMLHelper');
/* @var $xml_parser kXMLHelper */
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
- if ($module_name == 'In-Portal') {
+ if ( $module_name == 'In-Portal' ) {
continue;
}
$xml_file = $theme_path . '/' . $module_info['TemplatePath'] . '_install/theme.xml';
- if (file_exists($xml_file)) {
+ if ( file_exists($xml_file) ) {
$xml_data = file_get_contents($xml_file);
$root_node =& $xml_parser->Parse($xml_data);
- if (!is_object($root_node) || !is_a($root_node, 'kXMLNode') || !$root_node->Children) {
+ if ( !is_object($root_node) || !is_a($root_node, 'kXMLNode') || !$root_node->Children ) {
// broken xml OR no aliases defined
continue;
}
$current_node =& $root_node->firstChild;
do {
$template_path = trim($current_node->Data);
- $alias = '#' . $module_info['TemplatePath'] . strtolower($current_node->Name). '#';
+ $alias = '#' . $module_info['TemplatePath'] . strtolower($current_node->Name) . '#';
// remember alias in global theme mapping
$template_aliases[$alias] = $template_path;
// store alias in theme file record to use later in design dropdown
- $t_parts = Array (
- 'path' => dirname($template_path) == '.' ? '' : '/' . dirname($template_path),
- 'file' => basename($template_path),
- );
+ $t_parts = Array ('path' => dirname($template_path) == '.' ? ''
+ : '/' . dirname($template_path), 'file' => basename($template_path),);
$sql = 'UPDATE ' . TABLE_PREFIX . 'ThemeFiles
SET TemplateAlias = ' . $this->Conn->qstr($alias) . '
WHERE (ThemeId = ' . $theme_id . ') AND (FilePath = ' . $this->Conn->qstr($t_parts['path']) . ') AND (FileName = ' . $this->Conn->qstr($t_parts['file'] . '.tpl') . ')';
$this->Conn->Query($sql);
- } while (($current_node =& $current_node->NextSibling()));
+ } while ( ($current_node =& $current_node->NextSibling()) );
}
}
return $template_aliases;
}
/**
* Installs given module language pack and refreshed it from all themes
*
* @param string $module_name
*/
function syncronizeModule($module_name)
{
$sql = 'SELECT `Name`, ThemeId
FROM ' . TABLE_PREFIX . 'Theme';
$themes = $this->Conn->GetCol($sql, 'ThemeId');
if (!$themes) {
return ;
}
foreach ($themes as $theme_id => $theme_name) {
$theme_path = $this->themesFolder . '/' . $theme_name;
// install language pack
$this->installThemeLanguagePack($theme_path, $module_name);
// update TemplateAliases mapping
$fields_hash = Array (
'TemplateAliases' => serialize( $this->getTemplateAliases($theme_id, $theme_path) ),
);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'Theme', 'ThemeId = ' . $theme_id);
}
}
/**
* Searches for new templates (missing in db) in spefied folder
*
* @param string $folder_path subfolder of searchable theme
* @param string $theme_path theme path from web server root
* @param int $theme_id id of theme we are scanning
*/
function FindThemeFiles($folder_path, $theme_path, $theme_id, $auto_structure_mode = 1)
{
$fh = opendir($theme_path.$folder_path.'/');
// always ingore design and element templates
$ignore = Array ('^CVS$', '^\.svn$', '\.des\.tpl$', '\.elm\.tpl$');
$sms_ingore = $theme_path . $folder_path . '/.smsignore';
if (file_exists($sms_ingore)) {
$ignore = array_merge($ignore, file($sms_ingore));
}
while (($filename = readdir($fh))) {
if ($filename == '.' || $filename == '..') continue;
$auto_structure = $auto_structure_mode;
foreach ($ignore as $pattern) {
if (preg_match('/'.str_replace('/', '\\/', trim($pattern)).'/', $filename)) {
$auto_structure = 2;
break;
}
}
$full_path = $theme_path.$folder_path.'/'.$filename;
if (is_dir($full_path)) {
$this->FindThemeFiles($folder_path.'/'.$filename, $theme_path, $theme_id, $auto_structure);
}
elseif (substr($filename, -4) == '.tpl') {
$file_path = $folder_path.'/'.$filename;
$meta_info = $this->_getTemplateMetaInfo(trim($file_path, '/'), $theme_id);
$file_id = isset($this->themeFiles[$file_path]) ? $this->themeFiles[$file_path] : false;
$file_description = array_key_exists('desc', $meta_info) ? $meta_info['desc'] : '';
if ($file_id) {
// file was found in db & on hdd -> mark as existing
$fields_hash = Array (
'FileFound' => 1,
'Description' => $file_description,
'FileType' => $auto_structure,
'FileMetaInfo' => serialize($meta_info),
);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX . 'ThemeFiles', 'FileId = ' . $file_id);
}
else {
// file was found on hdd, but missing in db -> create new file record
$fields_hash = Array (
'ThemeId' => $theme_id,
'FileName' => $filename,
'FilePath' => $folder_path,
'Description' => $file_description,
'FileType' => $auto_structure, // 1 - built-in, 0 - custom (not in use right now), 2 - skipped in structure
'FileMetaInfo' => serialize($meta_info),
'FileFound' => 1,
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX.'ThemeFiles');
$this->themeFiles[$file_path] = $this->Conn->getInsertID();
}
// echo 'FilePath: [<strong>'.$folder_path.'</strong>]; FileName: [<strong>'.$filename.'</strong>]; IsNew: [<strong>'.($file_id > 0 ? 'NO' : 'YES').'</strong>]<br />';
}
}
}
/**
* Returns template information (name, description, path) from it's header comment
*
* @param string $template
* @param int $theme_id
* @return Array
*/
function _getTemplateMetaInfo($template, $theme_id)
{
static $init_made = false;
if (!$init_made) {
$this->Application->InitParser(true);
$init_made = true;
}
$template = 'theme:' . $this->_themeNames[$theme_id] . '/' . $template;
$template_file = $this->Application->TemplatesCache->GetRealFilename($template); // ".tpl" was added before
return $this->parseTemplateMetaInfo($template_file);
}
function parseTemplateMetaInfo($template_file)
{
if (!file_exists($template_file)) {
// when template without info it's placed in top category
return Array ();
}
$template_data = file_get_contents($template_file);
if (substr($template_data, 0, 6) == '<!--##') {
// template starts with comment in such format
/*<!--##
<NAME></NAME>
<DESC></DESC>
<SECTION>||</SECTION>
##-->*/
$comment_end = strpos($template_data, '##-->');
if ($comment_end === false) {
// badly formatted comment
return Array ();
}
$comment = trim( substr($template_data, 6, $comment_end - 6) );
if (preg_match_all('/<(NAME|DESC|SECTION)>(.*?)<\/(NAME|DESC|SECTION)>/is', $comment, $regs)) {
$ret = Array ();
foreach ($regs[1] as $param_order => $param_name) {
$ret[ strtolower($param_name) ] = trim($regs[2][$param_order]);
}
if (array_key_exists('section', $ret) && $ret['section']) {
$category_path = explode('||', $ret['section']);
$category_path = array_map('trim', $category_path);
$ret['section'] = implode('||', $category_path);
}
return $ret;
}
}
return Array ();
}
/**
* Updates file system changes to database for all themes (including new ones)
*
*/
function refreshThemes()
{
$themes_found = Array();
$skip_filenames = Array ('.', '..', 'CVS', '.svn');
$fh = opendir($this->themesFolder.'/');
while (($filename = readdir($fh))) {
if (in_array($filename, $skip_filenames)) {
continue;
}
if (is_dir($this->themesFolder.'/'.$filename)) {
$theme_id = $this->refreshTheme($filename);
if ($theme_id) {
$themes_found[] = $theme_id;
// increment serial of updated themes
$this->Application->incrementCacheSerial('theme', $theme_id);
}
}
}
$id_field = $this->Application->getUnitOption('theme', 'IDField');
$table_name = $this->Application->getUnitOption('theme', 'TableName');
// 1. only one theme found -> enable it and make primary
/*if (count($themes_found) == 1) {
$sql = 'UPDATE ' . $table_name . '
SET Enabled = 1, PrimaryTheme = 1
WHERE ' . $id_field . ' = ' . current($themes_found);
$this->Conn->Query($sql);
}*/
// 2. if none themes found -> delete all from db OR delete all except of found themes
$sql = 'SELECT '.$id_field.'
FROM '.$table_name;
if ($themes_found) {
$sql .= ' WHERE '.$id_field.' NOT IN ('.implode(',', $themes_found).')';
}
$theme_ids = $this->Conn->GetCol($sql);
$this->deleteThemes($theme_ids);
foreach ($theme_ids as $theme_id) {
// increment serial of deleted themes
$this->Application->incrementCacheSerial('theme', $theme_id);
}
$this->Application->incrementCacheSerial('theme');
$this->Application->incrementCacheSerial('theme-file');
$minify_helper =& $this->Application->recallObject('MinifyHelper');
/* @var $minify_helper MinifyHelper */
$minify_helper->delete();
}
/**
* Deletes themes with ids passed from db
*
* @param Array $theme_ids
*/
function deleteThemes($theme_ids)
{
if (!$theme_ids) {
return ;
}
$id_field = $this->Application->getUnitOption('theme', 'IDField');
$table_name = $this->Application->getUnitOption('theme', 'TableName');
$sql = 'DELETE FROM '.$table_name.'
WHERE '.$id_field.' IN ('.implode(',', $theme_ids).')';
$this->Conn->Query($sql);
$sql = 'DELETE FROM '.TABLE_PREFIX.'ThemeFiles
WHERE '.$id_field.' IN ('.implode(',', $theme_ids).')';
$this->Conn->Query($sql);
}
/**
* Returns current theme (also works in admin)
*
* @return int
*/
function getCurrentThemeId()
{
static $theme_id = null;
if (isset($theme_id)) {
return $theme_id;
}
if ($this->Application->isAdmin) {
// get theme, that user selected in catalog
$theme_id = $this->Application->RecallVar('theme_id');
if ($theme_id === false) {
// query, because "m_theme" is always empty in admin
$id_field = $this->Application->getUnitOption('theme', 'IDField');
$table_name = $this->Application->getUnitOption('theme', 'TableName');
$sql = 'SELECT ' . $id_field . '
FROM ' . $table_name . '
WHERE (PrimaryTheme = 1) AND (Enabled = 1)';
$theme_id = $this->Conn->GetOne($sql);
}
return $theme_id;
}
// use current theme, because it's available on Front-End
$theme_id = $this->Application->GetVar('m_theme');
if (!$theme_id) {
// happens in mod-rewrite mode, then requested template is not found
$theme_id = $this->Application->GetDefaultThemeId();
}
return $theme_id;
}
/**
* Returns page id based on given template
*
* @param string $template
* @param int $theme_id
* @return int
*/
function getPageByTemplate($template, $theme_id = null)
{
if (!isset($theme_id)) {
// during mod-rewrite url parsing current theme
// is not available to kHTTPQuery class, so don't use it
$theme_id = (int)$this->getCurrentThemeId();
}
$sql = 'SELECT ' . $this->Application->getUnitOption('c', 'IDField') . '
FROM ' . $this->Application->getUnitOption('c', 'TableName') . '
WHERE
(
(NamedParentPath = ' . $this->Conn->qstr('Content/' . $template) . ') OR
(`Type` = ' . PAGE_TYPE_TEMPLATE . ' AND CachedTemplate = ' . $this->Conn->qstr($template) . ')
)
AND (ThemeId = ' . $theme_id . ($theme_id > 0 ? ' OR ThemeId = 0' : '') . ')';
return $this->Conn->GetOne($sql);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/sections_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/sections_helper.php (revision 14627)
+++ branches/5.2.x/core/units/helpers/sections_helper.php (revision 14628)
@@ -1,377 +1,381 @@
<?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!');
/**
* Processes sections info from the configs
*
*/
class kSectionsHelper extends kHelper {
/**
* Holds information about all sections
*
* @var Array
*/
var $Tree = Array();
/**
* Checks, that we are in debug mode
*
* @var bool
*/
var $debugMode = false;
/**
* Checks, that we are in super admin mode
*
* @var bool
*/
var $superAdminMode = false;
public function __construct()
{
parent::__construct();
$this->debugMode = $this->Application->isDebugMode();
$this->superAdminMode = $this->Application->RecallVar('super_admin');
}
/**
* Set's prefix and special
*
* @param string $prefix
* @param string $special
* @access public
*/
public function Init($prefix, $special)
{
parent::Init($prefix, $special);
$this->BuildTree();
}
/**
* Builds xml for tree in left frame in admin
*
- * @param Array $params
+ * @return void
+ * @access public
*/
- function BuildTree()
+ public function BuildTree()
{
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$data = $this->Application->getCache('master:sections_parsed', false, CacheSettings::$sectionsParsedRebuildTime);
}
else {
$data = $this->Application->getDBCache('sections_parsed', CacheSettings::$sectionsParsedRebuildTime);
}
if ( $data ) {
$this->Tree = unserialize($data);
return ;
}
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->rebuildCache('master:sections_parsed', kCache::REBUILD_NOW, CacheSettings::$sectionsParsedRebuildTime);
}
else {
$this->Application->rebuildDBCache('sections_parsed', kCache::REBUILD_NOW, CacheSettings::$sectionsParsedRebuildTime);
}
if ( !defined('IS_INSTALL') || !IS_INSTALL ) {
// don't reread all configs during install, because they are reread on every install step
$this->Application->UnitConfigReader->ReReadConfigs();
}
$this->Tree = Array ();
// 1. build base tree (don't update parent with children list yet)
// 1.1. process prefixes without priority
$prioritized_prefixes = Array ();
$prefixes = array_keys($this->Application->UnitConfigReader->configData);
foreach ($prefixes as $prefix) {
$config =& $this->Application->UnitConfigReader->configData[$prefix];
- if (array_key_exists('ConfigPriority', $config)) {
+ if ( array_key_exists('ConfigPriority', $config) ) {
$prioritized_prefixes[$prefix] = $config['ConfigPriority'];
continue;
}
+
$this->_processPrefixSections($prefix);
}
// 2. process prefixes with priority
asort($prioritized_prefixes);
foreach ($prioritized_prefixes as $prefix => $priority) {
$this->_processPrefixSections($prefix);
}
- // 2. apply section ajustments
+ // 2. apply section adjustments
foreach ($prefixes as $prefix) {
$config =& $this->Application->UnitConfigReader->configData[$prefix];
- $section_ajustments = getArrayValue($config, 'SectionAdjustments');
- if (!$section_ajustments) continue;
+ $section_adjustments = getArrayValue($config, 'SectionAdjustments');
+ /* @var $section_adjustments Array */
- foreach ($section_ajustments as $section_name => $ajustment_params) {
- if (is_array($ajustment_params)) {
- if (!array_key_exists($section_name, $this->Tree)) {
- // don't process ajustments for non-existing sections
+ if ( !$section_adjustments ) {
+ continue;
+ }
+
+ foreach ($section_adjustments as $section_name => $adjustment_params) {
+ if ( is_array($adjustment_params) ) {
+ if ( !array_key_exists($section_name, $this->Tree) ) {
+ // don't process adjustments for non-existing sections
continue;
}
- $this->Tree[$section_name] = kUtil::array_merge_recursive($this->Tree[$section_name], $ajustment_params);
+ $this->Tree[$section_name] = kUtil::array_merge_recursive($this->Tree[$section_name], $adjustment_params);
}
else {
// then remove section
unset($this->Tree[$section_name]);
}
}
}
// 3.
foreach ($this->Tree as $section_name => $section_params) {
// 3.1. update parent -> children references
$parent_section = $section_params['parent'];
$section_order = "{$section_params['priority']}";
- if (!isset($parent_section)) {
+ if ( !isset($parent_section) ) {
// don't process parent section of "in-portal:root" section
continue;
}
- if (!array_key_exists('children', $this->Tree[$parent_section])) {
+ if ( !array_key_exists('children', $this->Tree[$parent_section]) ) {
$this->Tree[$parent_section]['children'] = Array ();
}
- if (array_key_exists($section_order, $this->Tree[$parent_section]['children'])) {
- trigger_error(
- 'Section "<strong>' . $section_name . '</strong>" has replaced section "<strong>' .
- $this->Tree[$parent_section]['children'][$section_order] .
- '</strong>" (parent section: "<strong>' . $parent_section .
- '</strong>"; duplicate priority: <strong>' . $section_order . '</strong>)',
- E_USER_WARNING
- );
+ if ( array_key_exists($section_order, $this->Tree[$parent_section]['children']) ) {
+ trigger_error('Section "<strong>' . $section_name . '</strong>" has replaced section "<strong>' . $this->Tree[$parent_section]['children'][$section_order] . '</strong>" (parent section: "<strong>' . $parent_section . '</strong>"; duplicate priority: <strong>' . $section_order . '</strong>)', E_USER_WARNING);
}
$this->Tree[$parent_section]['children'][$section_order] = $section_name;
- if ($section_params['type'] == stTAB) {
+ if ( $section_params['type'] == stTAB ) {
// if this is tab, then mark parent section as TabOnly
$this->Tree[$parent_section]['tabs_only'] = true;
}
- // 3.2. process icons here, because they also can be ajusted
- if (isset($section_params['icon']) && preg_match('/([^:]+):(.*)/', $section_params['icon'], $regs)) {
+ // 3.2. process icons here, because they also can be adjusted
+ if ( isset($section_params['icon']) && preg_match('/([^:]+):(.*)/', $section_params['icon'], $regs) ) {
$this->Tree[$section_name]['icon'] = $regs[2];
$this->Tree[$section_name]['icon_module'] = $regs[1]; // set "icon_module" used in "combined_header" block
- $module_folder = trim( $this->Application->findModule('Name', $regs[1], 'Path'), '/');
+ $module_folder = trim($this->Application->findModule('Name', $regs[1], 'Path'), '/');
- if ($module_folder == '') {
+ if ( $module_folder == '' ) {
$module_folder = 'core';
}
}
else {
$module_folder = $this->Application->getUnitOption($section_params['SectionPrefix'], 'ModuleFolder');
- if (!array_key_exists('icon_module', $section_params)) {
+ if ( !array_key_exists('icon_module', $section_params) ) {
// set "icon_module" used in "combined_header" block
$this->Tree[$section_name]['icon_module'] = $this->Application->findModule('Path', $module_folder . '/', 'Name');
}
}
// this is to display HELP icon instead of missing one.. can be replaced with some other icon to draw attention
- $icon_file = $module_folder.'/admin_templates/img/icons/icon24_'.$this->Tree[$section_name]['icon'];
+ $icon_file = $module_folder . '/admin_templates/img/icons/icon24_' . $this->Tree[$section_name]['icon'];
/*$core_file = FULL_PATH.'/core/admin_templates/img/icons/icon24_' . $this->Tree[$section_name]['icon'].'.png';
if ($module_folder != 'core' && file_exists($core_file) && file_exists(FULL_PATH.'/'.$icon_file.'.png')) {
if (crc32(file_get_contents($core_file)) == crc32(file_get_contents(FULL_PATH.'/'.$icon_file.'.png'))) {
trigger_error('Section "<strong>' . $section_name . '</strong>" uses icon copy from "Core" module', E_USER_NOTICE);
}
}*/
- if (!file_exists(FULL_PATH . '/' . $icon_file . '.png')) {
+ if ( !file_exists(FULL_PATH . '/' . $icon_file . '.png') ) {
$this->Tree[$section_name]['icon'] = 'help';
$this->Tree[$section_name]['icon_module'] = 'core';
}
}
- $this->Application->HandleEvent( new kEvent('adm:OnAfterBuildTree') );
+ $this->Application->HandleEvent(new kEvent('adm:OnAfterBuildTree'));
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->setCache('master:sections_parsed', serialize($this->Tree));
}
else {
$this->Application->setDBCache('sections_parsed', serialize($this->Tree));
}
}
function _processPrefixSections($prefix)
{
$config =& $this->Application->UnitConfigReader->configData[$prefix];
$sections = getArrayValue($config, 'Sections');
- if (!$sections) {
+ /* @var $sections Array */
+
+ if ( !$sections ) {
return ;
}
-// echo 'Prefix: ['.$prefix.'] has ['.count($sections).'] sections<br />';
-
foreach ($sections as $section_name => $section_params) {
// we could also skip not allowed sections here in future
if ( isset($section_params['SectionPrefix']) ) {
$section_prefix = $section_params['SectionPrefix'];
}
elseif ( $this->Application->getUnitOption($prefix, 'SectionPrefix') ) {
$section_prefix = $this->Application->getUnitOption($prefix, 'SectionPrefix');
}
else {
$section_prefix = $prefix;
}
+
$section_params['SectionPrefix'] = $section_prefix;
$section_params['url']['m_opener'] = 'r';
$section_params['url']['no_pass_through'] = 1;
$pass_section = getArrayValue($section_params, 'url', 'pass_section');
- if ($pass_section) {
+ if ( $pass_section ) {
unset($section_params['url']['pass_section']);
$section_params['url']['section'] = $section_name;
- if (!isset($section_params['url']['module'])) {
- $module_name = $this->Application->findModule('Path', $config['ModuleFolder'].'/', 'Name');
+
+ if ( !isset($section_params['url']['module']) ) {
+ $module_name = $this->Application->findModule('Path', $config['ModuleFolder'] . '/', 'Name');
$section_params['url']['module'] = $module_name;
}
}
- if (!isset($section_params['url']['t'])) {
+ if ( !isset($section_params['url']['t']) ) {
$section_params['url']['t'] = 'index';
}
- if (!isset($section_params['onclick'])) {
+ if ( !isset($section_params['onclick']) ) {
$section_params['onclick'] = 'checkEditMode()';
}
- if (!isset($section_params['container'])) {
+ if ( !isset($section_params['container']) ) {
$section_params['container'] = 0; // for js tree printing to xml
}
- $current_data = isset($this->Tree[$section_name]) ? $this->Tree[$section_name] : Array();
+ $current_data = isset($this->Tree[$section_name]) ? $this->Tree[$section_name] : Array ();
- if ($current_data) {
+ if ( $current_data ) {
trigger_error('Section "<strong>' . $section_name . '</strong>" declaration (originally defined in "<strong>' . $current_data['SectionPrefix'] . '</strong>") was overwriten from "<strong>' . $prefix . '</strong>"', E_USER_WARNING);
}
$this->Tree[$section_name] = kUtil::array_merge_recursive($current_data, $section_params);
}
}
/**
* Returns details information about section
*
* @param string $section_name
* @return Array
*/
function &getSectionData($section_name)
{
if (isset($this->Tree[$section_name])) {
$ret =& $this->Tree[$section_name];
}
else {
$ret = Array();
}
return $ret;
}
/**
* Returns first child, that is not a folder
*
* @param string $section_name
- * @param Array $tree
- * @return stirng
+ * @param bool $check_permission
+ * @return string
+ * @access public
*/
- function getFirstChild($section_name, $check_permission = false)
+ public function getFirstChild($section_name, $check_permission = false)
{
$section_data =& $this->getSectionData($section_name);
-
$children = isset($section_data['children']) && $section_data['children'] ? $section_data['children'] : false;
- if ($children) {
+ /* @var $children Array */
+
+ if ( $children ) {
// get 1st child
ksort($children, SORT_NUMERIC);
- foreach ($children as $child_priority => $child_section) {
- if (!$this->sectionVisible($child_section, $check_permission)) {
+ foreach ($children as $child_section) {
+ if ( !$this->sectionVisible($child_section, $check_permission) ) {
continue;
}
break;
}
return $this->getFirstChild($child_section, $check_permission);
}
return $section_name;
}
/**
* Checks if given section is visible by it's definition and optionally by user permission
*
* @param string $section_name
* @param bool $check_permission
* @return bool
*/
function sectionVisible($section_name, $check_permission = false)
{
$section_data =& $this->getSectionData($section_name);
if (isset($section_data['show_mode']) && is_numeric($section_data['show_mode'])) {
$show_mode = $section_data['show_mode'];
// if super admin section -> show in super admin mode & debug mode
$show_section = $show_mode == smNORMAL || ((($show_mode & smSUPER_ADMIN) == smSUPER_ADMIN) && ($this->superAdminMode || $this->debugMode));
if (!$show_section) {
// if section is in debug mode only && debug mode -> show
$show_section = (($show_mode & smDEBUG) == smDEBUG) && $this->debugMode;
}
if (!$show_section) {
// visibility by section definition
return false;
}
}
// visibility by section permission
if ($check_permission) {
$perm_section = $this->getPermSection($section_name);
return $this->Application->CheckPermission($perm_section.'.view');
}
return true;
}
/**
* Returns section for permission checking based on given section
*
* @param string $section_name
* @return string
*/
function getPermSection($section_name)
{
$ret = $section_name;
$section_data =& $this->getSectionData($section_name);
if ($section_data && isset($section_data['perm_prefix'])) {
// this section uses other section permissions
$ret = $this->Application->getUnitOption($section_data['perm_prefix'].'.main', 'PermSection');
}
return $ret;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/selectors/selectors_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/selectors/selectors_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/selectors/selectors_tag_processor.php (revision 14628)
@@ -1,100 +1,118 @@
<?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 SelectorsTagProcessor extends kDBTagProcessor {
function PrintStyle($params)
{
- $object =& $this->Application->recallObject( $this->getPrefixSpecial(), $this->Prefix, $params );
+ $object =& $this->getObject($params);
+ /* @var $object SelectorsItem */
+
$style_data = $object->GetDBField( $params['field'] );
$ret = $object->CompileStyleBody( getArrayValue($params,'inline') ? STYLE_INLINE : STYLE_PREVIEW );
return $ret;
}
/**
* Returns input field name to
* be placed on form (for correct
* event processing)
*
* @param Array $params
* @return string
* @access public
*/
function InputName($params)
{
$ret = parent::InputName($params);
$subfield = getArrayValue($params,'subfield');
if($subfield && $subfield != '$subfield')
{
$ret .= '['.$subfield.']';
}
return $ret;
}
function Field($params)
{
$subfield = getArrayValue($params,'subfield');
if($subfield && $subfield != '$subfield')
{
$params['no_special'] = 'no_special';
}
$ret = parent::Field($params);
if($subfield && $subfield != '$subfield')
{
$ret = getArrayValue($ret,$subfield);
return $ret !== false ? $ret : '';
}
return $ret;
}
- function PredefinedOptions($params)
+ /**
+ * Prints list a all possible field options
+ *
+ * @param Array $params
+ * @return string
+ * @access protected
+ */
+ protected function PredefinedOptions($params)
{
$field = $params['field'];
- $object =& $this->Application->recallObject($this->getPrefixSpecial(),$this->Prefix, $params);
- $value = $object->GetDBField($field);
- $subfield = getArrayValue($params,'subfield');
- if($subfield && $subfield != '$subfield') $value = $value[$subfield];
+ $object =& $this->getObject($params);
+ /* @var $object kDBItem */
- $value_field = getArrayValue($params,'value_field');
- if(!$value_field) $value_field = $field;
+ $value = $object->GetDBField($field);
+ $subfield = getArrayValue($params, 'subfield');
+
+ if ( $subfield && $subfield != '$subfield' ) {
+ $value = $value[$subfield];
+ }
- $options = $object->GetFieldOptions($value_field);
+ $value_field = getArrayValue($params, 'value_field');
+ if ( !$value_field ) {
+ $value_field = $field;
+ }
$block_params['name'] = $params['block'];
$block_params['field'] = $params['field'];
$block_params['pass_params'] = 'true';
$selected_param_name = getArrayValue($params, 'selected_param');
- if (!$selected_param_name) $selected_param_name = $params['selected'];
+ if ( !$selected_param_name ) {
+ $selected_param_name = $params['selected'];
+ }
$selected = $params['selected'];
$o = '';
- foreach ($options['options'] as $key => $val) {
+ $options = $object->GetFieldOption($value_field, 'options');
+
+ foreach ($options as $key => $val) {
$block_params['key'] = $key;
$block_params['option'] = $val;
$block_params['field_name'] = $this->InputName($params);
- $block_params[$selected_param_name] = ( $key == $value ? ' '.$selected : '');
+ $block_params[$selected_param_name] = ($key == $value ? ' ' . $selected : '');
$block_params['PrefixSpecial'] = $this->getPrefixSpecial();
$o .= $this->Application->ParseBlock($block_params, 1);
}
return $o;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/selectors/selectors_event_handler.php
===================================================================
--- branches/5.2.x/core/units/selectors/selectors_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/selectors/selectors_event_handler.php (revision 14628)
@@ -1,380 +1,439 @@
<?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!');
kUtil::safeDefine('STYLE_BASE', 1);
kUtil::safeDefine('STYLE_BLOCK', 2);
class SelectorsEventHandler extends kDBEventHandler
{
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnResetToBase' => Array('subitem' => 'add|edit'),
'OnMassResetToBase' => Array('subitem' => 'add|edit'),
'OnOpenStyleEditor' => Array('subitem' => 'add|edit'),
'OnSaveStyle' => Array('subitem' => 'add|edit'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
- * Occures before an item has been cloned
+ * 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
*/
- function OnBeforeClone(&$event)
+ protected function OnBeforeClone(&$event)
{
+ parent::OnBeforeClone($event);
+
$event->Init($event->Prefix, '-item');
+
$object =& $event->getObject();
+ /* @var $object kDBItem */
$title_field = 'SelectorName';
$new_name = $object->GetDBField($title_field);
$original_checked = false;
$foreign_key = $event->getEventParam('foreign_key'); // in case if whole stylesheet is cloned
- if($foreign_key === false) $foreign_key = $object->GetDBField('StylesheetId'); // in case if selector is copied ifself
+ if ( $foreign_key === false ) {
+ $foreign_key = $object->GetDBField('StylesheetId');
+ } // in case if selector is copied ifself
do {
if ( preg_match('/(.*)-([\d]+)/', $new_name, $regs) ) {
- $new_name = $regs[1].'-'.($regs[2]+1);
+ $new_name = $regs[1] . '-' . ($regs[2] + 1);
}
- elseif ($original_checked) {
- $new_name = $new_name.'-1';
+ elseif ( $original_checked ) {
+ $new_name = $new_name . '-1';
}
// if we are cloning in temp table this will look for names in temp table,
// since object' TableName contains correct TableName (for temp also!)
// if we are cloning live - look in live
- $query = ' SELECT '.$title_field.'
- FROM '.$object->TableName.'
- WHERE '.$title_field.' = '.$this->Conn->qstr($new_name).' AND StylesheetId = '.$foreign_key;
+ $query = ' SELECT ' . $title_field . '
+ FROM ' . $object->TableName . '
+ WHERE ' . $title_field . ' = ' . $this->Conn->qstr($new_name) . ' AND StylesheetId = ' . $foreign_key;
$res = $this->Conn->GetOne($query);
/*// if not found in live table, check in temp table if applicable
if ($res === false && $object->Special == 'temp') {
$query = 'SELECT '.$name_field.' FROM '.$this->GetTempName($master['TableName']).'
WHERE '.$name_field.' = '.$this->Conn->qstr($new_name);
$res = $this->Conn->GetOne($query);
}*/
$original_checked = true;
- } while ($res !== false);
+ } while ( $res !== false );
$object->SetDBField($title_field, $new_name);
}
/**
* Show base styles or block styles
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
- switch ($event->Special)
- {
+ /* @var $object kDBList */
+
+ switch ( $event->Special ) {
case 'base':
$object->addFilter('type_filter', '%1$s.Type = 1');
break;
case 'block':
$object->addFilter('type_filter', '%1$s.Type = 2');
break;
}
}
/**
- * Enter description here...
+ * Occurs before updating item
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
+ parent::OnBeforeItemUpdate($event);
+
$this->SerializeSelectorData($event);
}
/**
- * Enter description here...
+ * Occurs before creating item
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
+ parent::OnBeforeItemCreate($event);
+
$this->SerializeSelectorData($event);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
$this->UnserializeSelectorData($event);
}
/**
- * Enter description here...
+ * Occurs after creating item
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemCreate(&$event)
+ protected function OnAfterItemCreate(&$event)
{
+ parent::OnAfterItemCreate($event);
+
$this->UnserializeSelectorData($event);
}
/**
- * Get's special of main item for linking with subitem
+ * Gets special of main item for linking with sub-item
*
* @param kEvent $event
* @return string
*/
function getMainSpecial(&$event)
{
return '';
}
/**
* Save css-style name & description before opening css editor
*
* @param kEvent $event
*/
function OnOpenStyleEditor(&$event)
{
$this->SaveChanges($event);
$event->redirect = false;
}
/**
* Saves Changes to Item
*
* @param kEvent $event
*/
function SaveChanges(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
+ /* @var $object kDBItem */
- $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
- if($items_info)
- {
- list($id,$field_values) = each($items_info);
- if ($id == 0) {
- $parent_id = getArrayValue($field_values,'ParentId');
- if($parent_id) $object->Load($parent_id);
+ $items_info = $this->Application->GetVar($event->getPrefixSpecial(true));
+ if ( $items_info ) {
+ list($id, $field_values) = each($items_info);
+
+ if ( $id == 0 ) {
+ $parent_id = getArrayValue($field_values, 'ParentId');
+ if ( $parent_id ) {
+ $object->Load($parent_id);
+ }
$object->SetFieldsFromHash($field_values);
$object->Create();
- $this->Application->SetVar($event->getPrefixSpecial().'_id', $object->GetID() );
+ $this->Application->SetVar($event->getPrefixSpecial() . '_id', $object->GetID());
}
else {
$object->Load($id);
$object->SetFieldsFromHash($field_values);
$object->Update();
}
}
}
/**
* Save style changes from style editor
*
* @param kEvent $event
*/
function OnSaveStyle(&$event)
{
$this->SaveChanges($event);
$object =& $event->getObject();
$this->Application->SetVar($event->getPrefixSpecial().'_id', $object->GetId() );
$this->finalizePopup($event);
}
/**
* Extract styles
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemLoad(&$event)
+ protected function OnAfterItemLoad(&$event)
{
+ parent::OnAfterItemLoad($event);
+
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$selector_data = $object->GetDBField('SelectorData');
- if($selector_data)
- {
+
+ if ( $selector_data ) {
$selector_data = unserialize($selector_data);
$object->SetDBField('SelectorData', $selector_data);
}
- else
- {
- $selector_data = Array();
+ else {
+ $selector_data = Array ();
}
+
$this->AddParentProperties($event, $selector_data);
}
/**
* Serialize item before saving to db
*
* @param kEvent $event
*/
function SerializeSelectorData(&$event)
{
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$selector_data = $object->GetDBField('SelectorData');
- if(!$selector_data) $selector_data = Array();
+ if ( !$selector_data ) {
+ $selector_data = Array ();
+ }
$selector_data = $this->RemoveParentProperties($event, $selector_data);
- if( !kUtil::IsSerialized($selector_data) ) $selector_data = serialize($selector_data);
+ if ( !kUtil::IsSerialized($selector_data) ) {
+ $selector_data = serialize($selector_data);
+ }
$object->SetDBField('SelectorData', $selector_data);
}
/**
* Unserialize data back when update was made
*
* @param kEvent $event
*/
function UnserializeSelectorData(&$event)
{
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$selector_data = $object->GetDBField('SelectorData');
- if(!$selector_data) $selector_data = Array();
- if( kUtil::IsSerialized($selector_data) ) $selector_data = unserialize($selector_data);
+
+ if ( !$selector_data ) {
+ $selector_data = Array ();
+ }
+
+ if ( kUtil::IsSerialized($selector_data) ) {
+ $selector_data = unserialize($selector_data);
+ }
$selector_data = $this->AddParentProperties($event, $selector_data);
$object->SetDBField('SelectorData', $selector_data);
}
/**
* Populate options based on temporary table :)
*
* @param kEvent $event
*/
function OnPrepareBaseStyles(&$event)
{
$object =& $event->getObject();
$parent_info = $object->getLinkedInfo();
$title_field = $this->Application->getUnitOption($event->Prefix,'TitleField');
$sql = 'SELECT '.$title_field.', '.$object->IDField.' FROM '.$object->TableName.' WHERE Type = 1 AND StylesheetId = '.$parent_info['ParentId'].' ORDER BY '.$title_field;
$options = $this->Conn->GetCol($sql,$object->IDField);
$object->SetFieldOption('ParentId', 'options', $options);
}
/**
* Remove properties of parent style that match by value from style
*
* @param kEvent $event
*/
function RemoveParentProperties(&$event, $selector_data)
{
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$parent_id = $object->GetDBField('ParentId');
- if($parent_id)
- {
- $sql = 'SELECT SelectorData FROM '.$object->TableName.' WHERE '.$object->IDField.' = '.$parent_id;
+
+ if ( $parent_id ) {
+ $sql = 'SELECT SelectorData
+ FROM ' . $object->TableName . '
+ WHERE ' . $object->IDField . ' = ' . $parent_id;
$base_selector_data = $this->Conn->GetOne($sql);
- if( kUtil::IsSerialized($base_selector_data) ) $base_selector_data = unserialize($base_selector_data);
+ if ( kUtil::IsSerialized($base_selector_data) ) {
+ $base_selector_data = unserialize($base_selector_data);
+ }
- foreach($selector_data as $prop_name => $prop_value)
- {
- if( !$prop_value || getArrayValue($base_selector_data,$prop_name) == $prop_value )
- {
+ foreach ($selector_data as $prop_name => $prop_value) {
+ if ( !$prop_value || getArrayValue($base_selector_data, $prop_name) == $prop_value ) {
unset($selector_data[$prop_name]);
}
}
- $object->SetDBField('SelectorData', $selector_data);
- return $selector_data;
}
- else
- {
- foreach($selector_data as $prop_name => $prop_value)
- {
- if(!$prop_value) unset($selector_data[$prop_name]);
+ else {
+ foreach ($selector_data as $prop_name => $prop_value) {
+ if ( !$prop_value ) {
+ unset($selector_data[$prop_name]);
+ }
}
- $object->SetDBField('SelectorData', $selector_data);
- return $selector_data;
}
+
+ $object->SetDBField('SelectorData', $selector_data);
+
+ return $selector_data;
}
/**
* Add back properties from parent style, that match this style property values
*
* @param kEvent $event
*/
function AddParentProperties(&$event, $selector_data)
{
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$parent_id = $object->GetDBField('ParentId');
- if($parent_id)
- {
- $sql = 'SELECT SelectorData FROM '.$object->TableName.' WHERE '.$object->IDField.' = '.$parent_id;
+ if ( $parent_id ) {
+ $sql = 'SELECT SelectorData
+ FROM ' . $object->TableName . '
+ WHERE ' . $object->IDField . ' = ' . $parent_id;
$base_selector_data = $this->Conn->GetOne($sql);
- if( kUtil::IsSerialized($base_selector_data) ) $base_selector_data = unserialize($base_selector_data);
-
- $selector_data = kUtil::array_merge_recursive($base_selector_data,$selector_data);
+ if ( kUtil::IsSerialized($base_selector_data) ) {
+ $base_selector_data = unserialize($base_selector_data);
+ }
+ $selector_data = kUtil::array_merge_recursive($base_selector_data, $selector_data);
$object->SetDBField('SelectorData', $selector_data);
+
return $selector_data;
}
- return $selector_data;
+
+ return Array ();
}
/**
* Reset Style definition to base style -> no customizations
*
* @param kEvent $event
*/
function OnResetToBase(&$event)
{
$object =& $event->getObject();
+ /* @var $object SelectorsItem */
+
$object->SetFieldsFromHash( $this->getSubmittedFields($event) );
$object->ResetStyle();
- $event->SetRedirectParam('pass', 'all,'.$event->getPrefixSpecial());
+ $event->SetRedirectParam('pass', 'all,' . $event->getPrefixSpecial());
}
/**
* Resets selected styles properties to values of their base classes
*
* @param kEvent $event
*/
function OnMassResetToBase(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
+ /* @var $object SelectorsItem */
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
- if($items_info)
- {
- foreach($items_info as $id => $field_values)
- {
+
+ if ( $items_info ) {
+ foreach ($items_info as $id => $field_values) {
$object->Load($id);
- $object->ResetStyle();
+ $object->ResetStyle();
}
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/custom_data/custom_data_event_handler.php
===================================================================
--- branches/5.2.x/core/units/custom_data/custom_data_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/custom_data/custom_data_event_handler.php (revision 14628)
@@ -1,217 +1,233 @@
<?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 CustomDataEventHandler extends kDBEventHandler {
/**
* [HOOK] Allows to apply custom fields functionality to specific config
* When main item is created, then cdata config is cloned
*
* @param kEvent $event
*/
function OnDefineCustomFields(&$event)
{
// 1. clone customdata table
$clones = $this->Application->getUnitOption('cdata', 'Clones');
$clones[$event->MasterEvent->Prefix.'-cdata'] = Array (
'ParentPrefix' => $event->MasterEvent->Prefix,
'TableName' => $this->Application->getUnitOption($event->MasterEvent->Prefix, 'TableName').'CustomData',
);
$this->Application->setUnitOption('cdata', 'Clones', $clones);
// 2. add custom field information to main item
$this->createCustomFields($event->MasterEvent->Prefix);
}
- function scanCustomFields($prefix)
+ /**
+ * Returns list of custom fields for a given $prefix
+ *
+ * @param $prefix
+ *
+ * @return Array|bool
+ * @access protected
+ */
+ protected function scanCustomFields($prefix)
{
static $custom_fields = Array ();
if (defined('IS_INSTALL') && IS_INSTALL && !$this->Application->TableFound('CustomField')) {
return false;
}
if (!$prefix) {
// prefix not specified
return false;
}
$item_type = $this->Application->getUnitOption($prefix, 'ItemType');
if (!$item_type) {
// no main config of such type
return false;
}
$no_caching = (defined('IS_INSTALL') && IS_INSTALL) || (defined('CUSTOM_FIELD_ADDED') && CUSTOM_FIELD_ADDED);
if (!$custom_fields || $no_caching) {
// query all custom fields at once -> saves 4 sqls queries
if ($no_caching) {
$all_custom_fields = $this->getCustomFields();
}
else {
$cache_key = 'all_custom_fields[%CfSerial%][%ModSerial%]';
$all_custom_fields = $this->Application->getCache($cache_key, false);
if ($all_custom_fields === false) {
$this->Conn->nextQueryCachable = true;
$all_custom_fields = $this->getCustomFields();
$this->Application->setCache($cache_key, $all_custom_fields);
}
}
foreach ($all_custom_fields as $custom_field_id => $custom_field_data) {
$cf_type = $custom_field_data['Type'];
if (!array_key_exists($cf_type, $custom_fields)) {
$custom_fields[$cf_type] = Array ();
}
$custom_fields[$cf_type][$custom_field_id] = $custom_field_data;
}
}
return array_key_exists($item_type, $custom_fields) ? $custom_fields[$item_type] : false;
}
/**
* Returns sorted list of all custom fields
*
* @return Array
*/
function getCustomFields()
{
$sql = 'SELECT *
FROM '.TABLE_PREFIX.'CustomField';
$ret = $this->Conn->Query($sql, 'CustomFieldId');
ksort($ret);
return $ret;
}
/**
* Fills cloned cdata config with data from it's parent
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterConfigRead(&$event)
+ protected function OnAfterConfigRead(&$event)
{
$main_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
- if (!$main_prefix) {
- return false;
+ if ( !$main_prefix ) {
+ return ;
}
$custom_fields = $this->scanCustomFields($main_prefix);
- if (!$custom_fields) {
- return false;
+ if ( !$custom_fields ) {
+ return ;
}
// 2. create fields (for customdata item)
- $fields = $this->Application->getUnitOption($event->Prefix, 'Fields', Array());
- $field_options = Array('type' => 'string', 'formatter' => 'kMultiLanguage', 'db_type' => 'text', 'default' => '');
+ $fields = $this->Application->getUnitOption($event->Prefix, 'Fields', Array ());
+ $field_options = Array ('type' => 'string', 'formatter' => 'kMultiLanguage', 'db_type' => 'text', 'default' => '');
+
foreach ($custom_fields as $custom_id => $custom_params) {
- if (isset($fields['cust_'.$custom_id])) continue;
- $fields['cust_'.$custom_id] = $field_options;
- $fields['cust_'.$custom_id]['force_primary'] = !$custom_params['MultiLingual'];
+ if ( isset($fields['cust_' . $custom_id]) ) {
+ continue;
+ }
+ $fields['cust_' . $custom_id] = $field_options;
+ $fields['cust_' . $custom_id]['force_primary'] = !$custom_params['MultiLingual'];
}
+
$this->Application->setUnitOption($event->Prefix, 'Fields', $fields);
}
/**
* Creates "cust_<custom_name>" virtual fields for main item
*
* @param string $prefix
+ * @return void
+ * @access protected
*/
- function createCustomFields($prefix)
+ protected function createCustomFields($prefix)
{
$custom_fields = $this->scanCustomFields($prefix);
- if (!$custom_fields) {
- return false;
+ if ( !$custom_fields ) {
+ return;
}
- $calculated_fields = Array();
- $virtual_fields = $this->Application->getUnitOption($prefix, 'VirtualFields', Array());
+ $calculated_fields = Array ();
+ $virtual_fields = $this->Application->getUnitOption($prefix, 'VirtualFields', Array ());
$cf_helper =& $this->Application->recallObject('InpCustomFieldsHelper');
/* @var $cf_helper InpCustomFieldsHelper */
$is_install = defined('IS_INSTALL') && IS_INSTALL;
foreach ($custom_fields as $custom_id => $custom_params) {
$custom_name = $custom_params['FieldName'];
- $field_options = Array('type' => 'string', 'default' => $custom_params['DefaultValue']);
+ $field_options = Array ('type' => 'string', 'default' => $custom_params['DefaultValue']);
// raises warnings during 4.3.9 -> 5.0.0 upgrade, no fatal sqls though
if ( $custom_params['IsRequired'] ) {
$field_options['required'] = 1;
}
- $calculated_fields['cust_' . $custom_name] = 'cust.l' . $this->Application->GetDefaultLanguageId() .'_cust_' . $custom_id;
+ $calculated_fields['cust_' . $custom_name] = 'cust.l' . $this->Application->GetDefaultLanguageId() . '_cust_' . $custom_id;
- switch ($custom_params['ElementType']) {
+ switch ( $custom_params['ElementType'] ) {
case 'date':
unset($field_options['options']);
$field_options['formatter'] = 'kDateFormatter';
$field_options['input_time_format'] = '';
$field_options['time_format'] = '';
break;
case 'datetime':
unset($field_options['options']);
$field_options['formatter'] = 'kDateFormatter';
break;
case 'select':
case 'multiselect':
case 'radio':
- if ($custom_params['ValueList']) {
+ if ( $custom_params['ValueList'] ) {
// $is_install check prevents 335 bad phrase sql errors on upgrade to 5.1.0
$field_options['options'] = $is_install ? Array () : $cf_helper->GetValuesHash($custom_params['ValueList']);
$field_options['formatter'] = 'kOptionsFormatter';
$field_options['multiple'] = $custom_params['ElementType'] == 'multiselect';
}
break;
default:
- if ($custom_params['MultiLingual']) {
+ if ( $custom_params['MultiLingual'] ) {
$field_options['formatter'] = 'kMultiLanguage';
$calculated_fields['cust_' . $custom_name] = 'cust.l%2$s_cust_' . $custom_id;
}
break;
}
if ( !isset($virtual_fields['cust_' . $custom_name]) ) {
$virtual_fields['cust_' . $custom_name] = Array ();
}
$virtual_fields['cust_' . $custom_name] = kUtil::array_merge_recursive($field_options, $virtual_fields['cust_' . $custom_name]);
$custom_fields[$custom_id] = $custom_name;
}
- $config_calculated_fields = $this->Application->getUnitOption($prefix, 'CalculatedFields', Array());
+ $config_calculated_fields = $this->Application->getUnitOption($prefix, 'CalculatedFields', Array ());
foreach ($config_calculated_fields as $special => $special_fields) {
- if ($special == '-virtual') {
+ if ( $special == '-virtual' ) {
continue;
}
$config_calculated_fields[$special] = array_merge($config_calculated_fields[$special], $calculated_fields);
}
$this->Application->setUnitOption($prefix, 'CalculatedFields', $config_calculated_fields);
$this->Application->setUnitOption($prefix, 'CustomFields', $custom_fields);
$this->Application->setUnitOption($prefix, 'VirtualFields', $virtual_fields);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/fck/fck_eh.php
===================================================================
--- branches/5.2.x/core/units/fck/fck_eh.php (revision 14627)
+++ branches/5.2.x/core/units/fck/fck_eh.php (revision 14628)
@@ -1,207 +1,217 @@
<?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 FckEventHandler extends kDBEventHandler {
/**
- * Checks permissions of user
+ * Checks user permission to execute given $event
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
if ($this->Application->isAdminUser) {
// this limits all event execution only to logged-in users in admin
return true;
}
return parent::CheckPermission($event);
}
function CreateXmlHeader()
{
ob_end_clean() ;
// Prevent the browser from caching the result.
// Date in the past
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT') ;
// always modified
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT') ;
// HTTP/1.1
header('Cache-Control: no-store, no-cache, must-revalidate') ;
header('Cache-Control: post-check=0, pre-check=0', false) ;
// HTTP/1.0
header('Pragma: no-cache') ;
// Set the response format.
header( 'Content-Type: text/xml; charset=utf-8' ) ;
// Create the XML document header.
}
function OnLoadCmsTree(&$event)
{
$event->status = kEvent::erSTOP;
$this->CreateXmlHeader();
$res = '<?xml version="1.0" encoding="utf-8" ?>'."\n" ;
$res.= "<CmsPages>"."\n";
$lang = $this->Application->GetVar('m_lang');
$st =& $this->Application->recallObject('st.-dummy');
/* @var $st kDBItem */
$st_options = $this->Application->getUnitOption('st'.'.ParentId', 'Fields');
$pages = $st_options['options'];
$page_ids = array_keys($pages);
$sql = 'SELECT NamedParentPath, CategoryId
FROM ' . TABLE_PREFIX . 'Category
WHERE CategoryId IN (' . implode(',', $page_ids) . ')';
$tpls = $this->Application->Conn->GetCol($sql, 'CategoryId');
//$res = '';
foreach ($pages as $id => $title) {
$page_path = preg_replace('/^Content\//i', '', strtolower($tpls[$id]).'.html');
$title = $title.' ('.$page_path.')';
$real_url = $this->Application->HREF($tpls[$id], '_FRONT_END_', array('pass'=>'m'), 'index.php');
$res .= '<CmsPage real_url="'.$real_url.'" path="@@'.$id.'@@" title="'.htmlspecialchars($title,ENT_QUOTES).'" st_id="'.$id.'" serverpath="" />'."\n";
}
$res.= "</CmsPages>";
echo $res;
}
function OnRenameFile(&$event)
{
$event->status = kEvent::erSTOP;
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$old_name = $this->Application->GetVar('old_name');
$new_name = $this->Application->GetVar('new_name');
$folder = $this->Application->GetVar('folder');
$sServerDir = WRITEABLE . '/user_files/' . $folder . '/';
if (!file_exists($sServerDir.$old_name) || !is_file($sServerDir.$old_name)) {
echo 204;
return;
}
$fck_helper =& $this->Application->recallObject('FCKHelper');
- /* @var fck_helper fckFCKHelper*/
+ /* @var $fck_helper fckFCKHelper*/
- if (!$fck_helper->IsAllowedExtension($folder, $new_name)) {
+ if ( !$fck_helper->IsAllowedExtension($folder, $new_name) ) {
echo 203;
return;
}
- if (!rename($sServerDir.$old_name,$sServerDir.$new_name)) {
- // echo $sServerDir.$old_name.' -> '.$sServerDir.$new_name;
+ if ( !rename($sServerDir . $old_name, $sServerDir . $new_name) ) {
+// echo $sServerDir.$old_name.' -> '.$sServerDir.$new_name;
echo 205;
return;
}
echo '0';
}
function OnDeleteFiles(&$event)
{
$event->status = kEvent::erSTOP;
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$files = trim($this->Application->GetVar('files'),'|');
// echo $files;
$a_files = explode('|', $files);
$folder = $this->Application->GetVar('folder');
$sServerDir = WRITEABLE . '/user_files/' . $folder . '/';
foreach ($a_files AS $file) {
@unlink($sServerDir.$file);
}
// print_r($a_files);
}
function OnGetFoldersFilesList(&$event)
{
$this->CreateXmlHeader();
$fck_helper =& $this->Application->recallObject('FCKHelper');
/* @var $fck_helper fckFCKHelper */
$ret = '<?xml version="1.0" encoding="utf-8" ?>'."\n" ;
$ret .= "<content>"."\n";
$ret .= $fck_helper->PrintFolders();
$ret .= $fck_helper->PrintFiles();
$ret .= "</content>"."\n";
echo $ret;
exit;
}
function OnCreateFolder(&$event)
{
$event->status = kEvent::erSTOP;
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$new_folder = $this->Application->GetVar('new_folder');
$current_folder = $this->Application->GetVar('current_folder');
$folderPath = WRITEABLE . '/user_files' . '/' . $current_folder . "/" . $new_folder;
if ( file_exists( $folderPath ) && is_dir($folderPath)) {
echo "101";
}
if ( !file_exists( $folderPath ) )
{
// Turn off all error reporting.
error_reporting( 0 ) ;
// Enable error tracking to catch the error.
ini_set( 'track_errors', '1' ) ;
// To create the folder with 0777 permissions, we need to set umask to zero.
$oldumask = umask(0) ;
mkdir( $folderPath, 0777 ) ;
umask( $oldumask ) ;
$sErrorMsg = $php_errormsg ;
// Restore the configurations.
ini_restore( 'track_errors' ) ;
ini_restore( 'error_reporting' ) ;
if ($sErrorMsg)
echo $sErrorMsg ;
else
echo '0';
}
}
- function OnUploadFile(&$event)
+ /**
+ * Uploads a file from FCK file browser
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnUploadFile(&$event)
{
$event->status = kEvent::erSTOP;
- if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
+ if ( $this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1) ) {
return;
}
$fck_helper =& $this->Application->recallObject('FCKHelper');
- /* @var fck_helper fckFCKHelper*/
+ /* @var $fck_helper fckFCKHelper*/
+
$fck_helper->UploadFile();
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/fck/fck_tp.php
===================================================================
--- branches/5.2.x/core/units/fck/fck_tp.php (revision 14627)
+++ branches/5.2.x/core/units/fck/fck_tp.php (revision 14628)
@@ -1,128 +1,128 @@
<?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 FckTagProcessor extends kDBTagProcessor {
function ReadFolders($files_dir)
{
$folders = glob($files_dir . '*', GLOB_ONLYDIR);
if (!$folders) {
return Array ();
}
$folders = array_map('basename', $folders);
$system_folders = defined('KERNEL_SYSTEM_FOLDERS') ? KERNEL_SYSTEM_FOLDERS : Array ('icons', 'CVS', '.svn');
return array_diff($folders, $system_folders);
}
function ReadFiles($files_dir)
{
$aFiles = array();
$oCurrentFolder = opendir($files_dir) ;
// $KernelSystemFolders = defined('KERNEL_SYSTEM_FOLDERS') ? KERNEL_SYSTEM_FOLDERS : array('icons', 'CVS', '.svn');
while ( $sFile = readdir( $oCurrentFolder ) )
{
if (!is_dir($files_dir . $sFile )) {
$aFiles[] = $sFile;
}
}
closedir( $oCurrentFolder ) ;
return $aFiles;
}
function CheckCreateDefaultFolders()
{
$fck_helper =& $this->Application->recallObject('FCKHelper');
- /* @var fck_helper fckFCKHelper*/
+ /* @var $fck_helper fckFCKHelper */
$default_folders = defined('FCK_DEFAULT_FOLDERS') ? FCK_DEFAULT_FOLDERS : Array ('Files', 'Images', 'Flash', 'Media', 'Documents');
foreach ($default_folders as $index => $folder) {
if (!$fck_helper->CreateFolder($folder)) {
unset($default_folders[$index]);
}
}
if (!$default_folders) {
return '';
}
$ret = '';
foreach ($default_folders as $folder) {
$selected = ($this->Application->GetVar('type') == $folder) ? 'selected' : '';
$ret.= '<option value="' . $folder . '" '.$selected.' />' . $folder;
}
return $ret;
}
function PrintFolders($params)
{
$order_by = $this->Application->GetVar('order_by');
$sort_by = $this->Application->GetVar('sort_by');
$params['folder'] = $this->Application->GetVar('folder');
$files_dir = WRITEABLE."/user_files/".$params['folder']."/";
$aFolders = $this->ReadFolders($files_dir);
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $block_params['render_as'];
$ret = '';
foreach ($aFolders as $k => $v) {
$block_params['folder_name'] = $v;
$block_params['path'] = $params['folder']."/".$v;
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
function PrintFiles($params)
{
$params['folder'] = $this->Application->GetVar('folder');
$files_dir = WRITEABLE . "/user_files/" . $params['folder'] . "/";
$files_url = BASE_PATH . WRITEBALE_BASE . "/user_files/" . $params['folder']."/";
$aFiles = $this->ReadFiles($files_dir);
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $block_params['render_as'];
$ret = '';
$date_format = "m/d/Y h:i A";
$a_ext = Array('ai','avi','bmp','cs','dll','doc','exe','fla','gif','htm','html','jpg','js','mdb','mp3','pdf','png','ppt','rdp','swf','swt','txt','vsd','xls','xml','zip');
foreach ($aFiles as $k => $v) {
$size = filesize( $files_dir . $v ) ;
if ( $size > 0 ) {
$size = round( $size / 1024 );
$size = ($size < 1)? 1:$size;// round( $iFileSize / 1024 ) ;
}
$ext = strtolower( pathinfo($v, PATHINFO_EXTENSION) );
if (in_array($ext, $a_ext)) {
$icon = $ext;
}
else {
$icon = 'default.icon';
}
$block_params['file_name'] = $v;
$block_params['size'] = $size;
$block_params['url'] = $files_url.$v;
$block_params['icon'] = $icon;
$block_params['date'] = date($date_format, filectime( $files_dir . $v ));
// $block_params['path'] = $params['folder']."/".$v;
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/logs/session_logs/session_log_eh.php
===================================================================
--- branches/5.2.x/core/units/logs/session_logs/session_log_eh.php (revision 14627)
+++ branches/5.2.x/core/units/logs/session_logs/session_log_eh.php (revision 14628)
@@ -1,125 +1,128 @@
<?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 SessionLogEventHandler extends kDBEventHandler {
/**
* Opens log for new session
*
* @param kEvent $event
*/
function OnStartSession(&$event)
{
if (!$this->Application->ConfigValue('UseChangeLog')) {
// don't use session log when change log is disabled
return ;
}
$object =& $this->Application->recallObject($event->Prefix, null, Array ('skip_autoload' => 1));
/* @var $object kDBItem */
$fields_hash = Array (
'SessionStart' => adodb_mktime(),
'IP' => $_SERVER['REMOTE_ADDR'],
'PortalUserId' => $this->Application->RecallVar('user_id'),
'SessionId' => $this->Application->GetSID(),
'Status' => SESSION_LOG_ACTIVE,
);
$object->SetDBFieldsFromHash($fields_hash);
$object->UpdateFormattersSubFields();
if ($object->Create()) {
$this->Application->StoreVar('_SessionLogId_', $object->GetID());
}
}
/**
* Closes log for current session
*
* @param kEvent $event
*/
function OnEndSession(&$event)
{
$object =& $this->Application->recallObject($event->Prefix, null, Array ('skip_autoload' => 1));
/* @var $object kDBItem */
$object->Load($this->Application->RecallVar('_SessionLogId_'));
if (!$object->isLoaded()) {
return ;
}
$fields_hash = Array (
'SessionEnd' => adodb_mktime(),
'Status' => SESSION_LOG_LOGGED_OUT,
);
$object->SetDBFieldsFromHash($fields_hash);
$object->UpdateFormattersSubFields();
$object->Update();
}
/**
- * Don't allow to delete other user's messages
+ * Apply custom processing to item
*
* @param kEvent $event
+ * @param string $type
+ * @return void
+ * @access protected
*/
- function customProcessing(&$event, $type)
+ protected function customProcessing(&$event, $type)
{
if ($event->Name == 'OnMassDelete' && $type == 'before') {
$ids = $event->getEventParam('ids');
if ($ids) {
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT ' . $id_field . '
FROM ' . $table_name . '
WHERE ' . $id_field . ' IN (' . implode(',', $ids) . ') AND Status <> ' . SESSION_LOG_ACTIVE;
$allowed_ids = $this->Conn->GetCol($sql);
$event->setEventParam('ids', $allowed_ids);
}
}
}
/**
* Delete changes, related to deleted session
*
* @param kEvent $event
*/
function OnAfterItemDelete(&$event)
{
parent::OnAfterItemDelete($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$sql = 'SELECT ' . $this->Application->getUnitOption('change-log', 'IDField') . '
FROM ' . $this->Application->getUnitOption('change-log', 'TableName') . '
WHERE SessionLogId = ' . $object->GetID();
$related_ids = $this->Conn->GetCol($sql);
if ($related_ids) {
$temp_handler =& $this->Application->recallObject('change-log_TempHandler', 'kTempTablesHandler');
/* @var $temp_handler kTempTablesHandler */
$temp_handler->DeleteItems('change-log', '', $related_ids);
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/logs/change_logs/changes_formatter.php
===================================================================
--- branches/5.2.x/core/units/logs/change_logs/changes_formatter.php (revision 14627)
+++ branches/5.2.x/core/units/logs/change_logs/changes_formatter.php (revision 14628)
@@ -1,75 +1,84 @@
<?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!');
+defined('FULL_PATH') or die('restricted access!');
- class kChangesFormatter extends kFormatter {
+class kChangesFormatter extends kFormatter {
- function Format($value, $field_name, &$object, $format=null)
- {
- if ( is_null($value) ) {
- return '';
- }
+ /**
+ * Formats value of a given field
+ *
+ * @param string $value
+ * @param string $field_name
+ * @param kDBItem|kDBList $object
+ * @param string $format
+ * @return string
+ */
+ function Format($value, $field_name, &$object, $format = null)
+ {
+ if ( is_null($value) ) {
+ return '';
+ }
- $changes = unserialize($value);
+ $changes = unserialize($value);
- $res = '';
- if (!$changes) {
- return ;
- }
+ $res = '';
+ if ( !$changes ) {
+ return '';
+ }
+
+ foreach ($changes as $field => $data) {
+ $fld_translation = $this->Application->Phrase('la_fld_' . $field);
- foreach ($changes as $field => $data) {
- $fld_translation = $this->Application->Phrase('la_fld_'.$field);
+ // remove translation link (added in debug mode)
+ $fld_translation = preg_replace('/<a href="(.*?)".*>(.*?)<\/a>/', '\\2', $fld_translation);
- // remove translation link (added in debug mode)
- $fld_translation = preg_replace('/<a href="(.*?)".*>(.*?)<\/a>/', '\\2', $fld_translation);
+ if ( $fld_translation == '!' . strtoupper('la_fld_' . $field) . '!' ) {
+ // when phrase is not translated use field name as label
+ $fld_translation = $field;
+ }
- if ($fld_translation == '!'.strtoupper('la_fld_'.$field).'!') {
- // when phrase is not translated use field name as label
- $fld_translation = $field;
+ if ( is_array($data) ) {
+ if ( $format == 'auto_cut' ) {
+ $data = array_map(Array (&$this, 'cutValue'), $data);
}
- if (is_array($data)) {
- if ($format == 'auto_cut') {
- $data = array_map(Array (&$this, 'cutValue'), $data);
- }
-
- if (array_key_exists('old', $data) && array_key_exists('new', $data)) {
- $res .= "$fld_translation: {$data['old']} => {$data['new']}<br/>\n";
- }
- else {
- $res .= "$fld_translation: {$data['new']}<br/>\n";
- }
+ if ( array_key_exists('old', $data) && array_key_exists('new', $data) ) {
+ $res .= "$fld_translation: {$data['old']} => {$data['new']}<br/>\n";
}
else {
- if ($format == 'auto_cut') {
- $data = $this->cutValue($data);
- }
-
- $res .= "$fld_translation: {$data}<br/>\n";
+ $res .= "$fld_translation: {$data['new']}<br/>\n";
+ }
+ }
+ else {
+ if ( $format == 'auto_cut' ) {
+ $data = $this->cutValue($data);
}
+ $res .= "$fld_translation: {$data}<br/>\n";
}
- return $res;
}
- function cutValue($data)
- {
- if (strlen($data) > 200) {
- $data = substr($data, 0, 50) . ' ...';
- }
+ return $res;
+ }
- return $data;
+ function cutValue($data)
+ {
+ if ( strlen($data) > 200 ) {
+ $data = substr($data, 0, 50) . ' ...';
}
- }
\ No newline at end of file
+
+ return $data;
+ }
+}
\ No newline at end of file
Index: branches/5.2.x/core/units/images/image_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/images/image_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/images/image_tag_processor.php (revision 14628)
@@ -1,488 +1,497 @@
<?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.
*/
class ImageTagProcessor extends kDBTagProcessor {
/**
* Prepares all image parameters as list block parameters (for easy usage)
*
* @param kDBList $object
* @param Array $block_params
+ * @return void
+ * @access protected
* @author Alex
*/
- function PrepareListElementParams(&$object, &$block_params)
+ protected function PrepareListElementParams(&$object, &$block_params)
{
$image_url = $this->ImageSrc($block_params);
- if (!$image_url) {
+ if ( !$image_url ) {
return ;
}
$parent_prefix = $this->Application->getUnitOption($object->Prefix, 'ParentPrefix');
+
$parent_item =& $this->Application->recallObject($parent_prefix);
+ /* @var $parent_item kDBItem */
$block_params['img_path'] = $image_url;
$image_dimensions = $this->ImageSize($block_params);
- $block_params['img_size'] = $image_dimensions ? $image_dimensions : ' width="'.$block_params['DefaultWidth'].'"';
- $block_params['alt'] = $object->GetField('AltName')? $object->GetField('AltName') : htmlspecialchars($this->getItemTitle($parent_item));
+ $block_params['img_size'] = $image_dimensions ? $image_dimensions : ' width="' . $block_params['DefaultWidth'] . '"';
+ $block_params['alt'] = $object->GetField('AltName') ? $object->GetField('AltName') : htmlspecialchars($this->getItemTitle($parent_item));
$block_params['align'] = array_key_exists('align', $block_params) ? $block_params['align'] : 'left';
}
/**
* Returns value of object's title field
*
* @param kDBItem $object
* @return string
+ * @access protected
*/
- function getItemTitle(&$object)
+ protected function getItemTitle(&$object)
{
$title_field = $this->Application->getUnitOption($object->Prefix, 'TitleField');
+
return $object->GetField($title_field);
}
/**
* [AGGREGATED TAGS] works as <inp2:CatalogItemPrefix_Image, ImageSize, ImageSrc ..../>
*
* @param Array $params
* @return string
*/
function ItemImageTag($params)
{
$this->LoadItemImage($params);
return $this->$params['original_tag']($params);
}
function LargeImageExists($params)
{
$object =& $this->getObject($params);
if ($object->GetDBField('SameImages') == null || $object->GetDBField('SameImages') == 1) {
return false;
}
else {
return true;
}
}
function LoadItemImage($params)
{
$parent_item =& $this->Application->recallObject($params['PrefixSpecial']);
/* @var $parent_item kCatDBItem */
$object =& $this->Application->recallObject($this->getPrefixSpecial(), null, Array('skip_autoload' => true));
/* @var $object kDBItem */
$object->Clear();
// if we need primary thumbnail which is preloaded with category item's list
$is_primary = $this->SelectParam($params, 'primary,Primary');
$image_name = $this->SelectParam($params, 'name,Name');
$image_field = $this->SelectParam($params, 'field,Field'); // ie. virtual names PrimaryImage, Image1, Image2
$image_id = $this->Application->GetVar($this->Prefix.'_id');
if (
// is primary, when primary mark set OR name & field not given
($is_primary || !($image_name || $image_field)) &&
// primary image is preloaded AND direct id not given
$parent_item->isField('ThumbPath') && !$image_id
) {
if (is_null($parent_item->GetDBField('SameImages'))) {
// JOIN definetly failed, because it's not-null column
$object->setLoaded(false);
}
else {
$object->SetDBField('Url', $parent_item->GetDBField('FullUrl'));
$object->SetDBFieldsFromHash($parent_item->GetFieldValues(), Array('AltName', 'SameImages', 'LocalThumb', 'ThumbPath', 'ThumbUrl', 'LocalImage', 'LocalPath'));
if (!$object->GetDBField('AltName')) {
$object->SetDBField('AltName', $this->getItemTitle($parent_item));
}
$object->setLoaded();
}
}
else { // if requested image is not primary thumbnail - load it directly
$id_field = $this->Application->getUnitOption($this->Prefix, 'ForeignKey');
$parent_table_key = $this->Application->getUnitOption($this->Prefix, 'ParentTableKey');
$keys[$id_field] = $parent_item->GetDBField($parent_table_key);
// which image to load?
if ($is_primary) {
// by PrimaryImage mark
$keys['DefaultImg'] = 1;
}
elseif ($image_name) {
// by ImageName
$keys['Name'] = $image_name;
}
elseif ($image_field) {
// by virtual field name in main object
$field_options = $parent_item->GetFieldOptions( $image_field );
$keys['Name'] = isset($field_options['original_field']) ? $field_options['original_field'] : $image_field;
}
elseif ($image_id) {
// by ID
$keys['ImageId'] = $image_id;
}
else {
// by PrimaryImage if no other criteria given
$keys['DefaultImg'] = 1;
}
$object->Load($keys);
if ( $image_field ) {
$image_src = $parent_item->GetDBField( $image_field );
// when image is uploaded to virtual field in main item, but not saved to db
$object->SetDBField('ThumbPath', $image_src);
if (!$object->isLoaded() && $image_src) {
// set fields for displaing new image during main item suggestion with errors
$fields_hash = Array (
'Url' => '',
'ThumbUrl' => '',
'LocalPath' => '',
'SameImages' => 1,
'LocalThumb' => 1,
'LocalImage' => 1,
);
$object->SetDBFieldsFromHash($fields_hash);
$object->setLoaded();
}
}
}
}
function getImageDimension($type, $params)
{
$ret = isset($params['Max'.$type]) ? $params['Max'.$type] : false;
if (!$ret) {
return $ret;
}
$parent_prefix = $this->Application->getUnitOption($this->Prefix, 'ParentPrefix');
if ($ret == 'thumbnail') {
$ret = $this->Application->ConfigValue($parent_prefix.'_ThumbnailImage'.$type);
}
if ($ret == 'fullsize') {
$ret = $this->Application->ConfigValue($parent_prefix.'_FullImage'.$type);
}
return $ret;
}
/**
* Appends "/" to beginning of image path (in case when missing)
*
* @param kDBItem $object
* @todo old in-portal doesn't append first slash, but we do => append first slash for him :)
*/
function makeRelativePaths(&$object)
{
$thumb_path = $object->GetDBField('ThumbPath');
if ($thumb_path && substr($thumb_path, 0, 1) != DIRECTORY_SEPARATOR) {
$object->SetDBField('ThumbPath', DIRECTORY_SEPARATOR . $thumb_path);
}
$local_path = $object->GetDBField('LocalPath');
if ($local_path && substr($local_path, 0, 1) != DIRECTORY_SEPARATOR) {
$object->SetDBField('LocalPath', DIRECTORY_SEPARATOR . $local_path);
}
}
function ImageSrc($params)
{
$object =& $this->getObject($params);
/* @var $object kDBItem */
$this->makeRelativePaths($object);
// show "noimage.gif" when requested image is missing OR was not uploaded
$use_default_image = !(defined('DBG_IMAGE_RECOVERY') && DBG_IMAGE_RECOVERY);
$src_image_url = $this->_getImageUrl($params);
$src_image = $this->_getImagePath($src_image_url);
if (!$object->isLoaded() || ($src_image_url && $src_image)) {
// we can auto-resize image, when it is stored locally
$max_width = $this->getImageDimension('Width', $params);
$max_height = $this->getImageDimension('Height', $params);
$format = array_key_exists('format', $params) ? $params['format'] : false;
if (!$max_width && $format) {
// user watermarks from format param
$max_width = $format;
}
if ($max_width > 0 || $max_height > 0 || $format) {
list ($max_width, $max_height) = $this->_transformParams($params, $max_width, $max_height);
if ($object->isLoaded() && file_exists($src_image)) {
$image_helper =& $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
return $image_helper->ResizeImage($src_image, $max_width, $max_height);
}
elseif ($use_default_image) {
return $this->_getDefaultImage($params, $max_width, $max_height);
}
return $src_image_url;
}
}
if ($src_image_url) {
// convert full url to full path!
$dst_image = $this->_getImagePath($src_image_url);
$image_found = $dst_image ? file_exists($dst_image) : true;
if ($image_found) {
// image isn't deleted OR is stored on remote location
return $src_image_url;
}
}
// return Default Image or false if NOT specified (only for case, when SameImages = 0)
return $use_default_image ? $this->_getDefaultImage($params) : $src_image_url;
}
/**
* Get location on disk for images, stored locally and false for remote images
*
* @param string $src_image
* @return string
*/
function _getImagePath($src_image)
{
if (!$src_image) {
return false;
}
$file_helper =& $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
$dst_image = $file_helper->urlToPath($src_image);
return $dst_image != $src_image ? $dst_image : false;
}
function _getImageUrl($params)
{
$object =& $this->getObject($params);
/* @var $object kDBItem */
$base_url = rtrim($this->Application->BaseURL(), '/');
// if we need thumbnail, or full image is same as thumbnail
$show_thumbnail = $this->SelectParam($params, 'thumbnail,Thumbnail') || // old style
(isset($params['MaxWidth']) && $params['MaxWidth'] == 'thumbnail') || // new style
(isset($params['MaxHeight']) && $params['MaxHeight'] == 'thumbnail');
if ($show_thumbnail || $object->GetDBField('SameImages')) {
// return local image or url
$ret = $object->GetDBField('LocalThumb') ? $base_url . $object->GetDBField('ThumbPath') : $object->GetDBField('ThumbUrl');
}
else { // if we need full which is not the same as thumb
$ret = $object->GetDBField('LocalImage') ? $base_url . $object->GetDBField('LocalPath') : $object->GetDBField('Url');
}
return $ret == $base_url ? '' : $ret;
}
/**
* Transforms Image/ImageSrc aggregated tag parameters into ones, that ResizeImage method understands
*
* @param Array $params
* @param int $max_width
* @param int $max_height
* @return Array
*/
function _transformParams($params, $max_width = false, $max_height = false)
{
$resize_format = 'resize:' . $max_width . 'x' . $max_height;
$crop = $this->SelectParam($params, 'Crop,crop');
if ($crop) {
if (strpos($crop, ';') === false) {
$crop = 'c|c';
}
$max_width = (is_null($max_height) ? $max_width : $resize_format) . ';crop:' . $crop;
$max_height = null;
}
$fill = $this->SelectParam($params, 'Fill,fill');
if ($fill) {
$max_width = (is_null($max_height) ? $max_width : $resize_format) . ';fill:' . $fill;
$max_height = null;
}
$watermark = $this->SelectParam($params, 'Watermark,watermark');
if ($watermark) {
$max_width = (is_null($max_height) ? $max_width : $resize_format) . ';wm:' . $watermark;
$max_height = null;
}
return Array ($max_width, $max_height);
}
/**
* Returns default full url to default images
*
* @param Array $params
* @param int $max_width
* @param int $max_height
* @return string
*/
function _getDefaultImage($params, $max_width = false, $max_height = false)
{
$default_image = $this->SelectParam($params, 'default_image,DefaultImage');
if (!$default_image) {
return '';
}
// show default image, use different base urls for admin and front-end
$base_url = rtrim($this->Application->BaseURL(), '/');
$sub_folder = $this->Application->isAdmin ? rtrim(IMAGES_PATH, '/') : THEMES_PATH;
if (($max_width !== false) || ($max_height !== false)) {
$image_helper =& $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
$src_image = FULL_PATH . $sub_folder . '/' . $default_image;
return $image_helper->ResizeImage($src_image, $max_width, $max_height);
}
return $base_url . $sub_folder . '/' . $default_image;
}
function getFullPath($path)
{
if (!$path) {
return $path;
}
// absolute url
if (preg_match('/^(.*):\/\/(.*)$/U', $path)) {
$file_helper =& $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
return $file_helper->urlToPath($path);
}
// TODO: change to urlToPath usage later
// relative url (we add sort of <inp2:m_TemplatesBase/> does
return FULL_PATH . '/' . mb_substr(THEMES_PATH, 1) . '/' . rawurldecode($path);
}
/**
* Makes size clause for img tag, such as
* ' width="80" height="100"' according to max_width
* and max_heght limits.
*
* @param array $params
* @return string
*/
function ImageSize($params)
{
$img_path = $this->getFullPath($params['img_path']);
$image_helper =& $this->Application->recallObject('ImageHelper');
/* @var $image_helper ImageHelper */
$max_width = $this->getImageDimension('Width', $params);
$max_height = $this->getImageDimension('Height', $params);
$image_dimensions = $image_helper->GetImageDimensions($img_path, $max_width, $max_height, $params);
if (!$image_dimensions) {
return false;
}
return ' width="'.$image_dimensions[0].'" height="'.$image_dimensions[1].'"';
}
/**
* Prepares image parameters & parses block with them (for admin)
*
* @param Array $params
* @return string
+ * @access protected
*/
- function Image($params)
+ protected function Image($params)
{
$image_url = $this->ImageSrc($params);
- if (!$image_url) {
- return ;
+
+ if ( !$image_url ) {
+ return '';
}
$object =& $this->getObject($params);
+ /* @var $object kDBItem */
+
$params['img_path'] = $image_url;
$image_dimensions = $this->ImageSize($params);
- $params['img_size'] = $image_dimensions ? $image_dimensions : ' width="'.$params['DefaultWidth'].'"';
+ $params['img_size'] = $image_dimensions ? $image_dimensions : ' width="' . $params['DefaultWidth'] . '"';
$params['alt'] = htmlspecialchars($object->GetField('AltName')); // really used ?
$params['name'] = $this->SelectParam($params, 'block,render_as');
$params['align'] = array_key_exists('align', $params) ? $params['align'] : 'left';
$params['no_editing'] = 1;
- if (!$object->isLoaded() && !$this->SelectParam($params, 'default_image,DefaultImage')) {
- return false;
+ if ( !$object->isLoaded() && !$this->SelectParam($params, 'default_image,DefaultImage') ) {
+ return '';
}
return $this->Application->ParseBlock($params);
}
-
/**
* Returns url for image in case when image source is url (for admin)
*
* @param Array $params
* @return string
*/
function ImageUrl($params)
{
$object =& $this->getObject($params);
if ($object->GetDBField('SameImages') ? $object->GetDBField('LocalThumb') : $object->GetDBField('LocalImage') ) {
$ret = $this->Application->Phrase(getArrayValue($params,'local_phrase'));
}
else {
$ret = $object->GetDBField('SameImages') ? $object->GetDBField('ThumbUrl') : $object->GetDBField('Url');
}
return $ret;
}
/**
* If data was modfied & is in TempTables mode, then parse block with name passed;
* remove modification mark if not in TempTables mode
*
* @param Array $params
* @return string
* @access public
* @author Alexey
*/
function SaveWarning($params)
{
if ($this->Prefix == 'c-img') {
return $this->Application->ProcessParsedTag('c', 'SaveWarning', $params);
}
return parent::SaveWarning($params);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/images/image_event_handler.php
===================================================================
--- branches/5.2.x/core/units/images/image_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/images/image_event_handler.php (revision 14628)
@@ -1,468 +1,480 @@
<?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 ImageEventHandler extends kDBEventHandler {
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnCleanImages' => Array ('subitem' => true),
'OnCleanResizedImages' => Array ('subitem' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
function mapEvents()
{
parent::mapEvents(); // ensure auto-adding of approve/decine and so on events
$image_events = Array(
'OnAfterCopyToTemp'=>'ImageAction',
'OnBeforeDeleteFromLive'=>'ImageAction',
'OnBeforeCopyToLive'=>'ImageAction',
'OnBeforeItemDelete'=>'ImageAction',
'OnAfterClone'=>'ImageAction',
);
$this->eventMethods = array_merge($this->eventMethods, $image_events);
}
/**
* Get's special of main item for linking with subitem
*
* @param kEvent $event
* @return string
*/
function getMainSpecial(&$event)
{
if ($event->Special == 'list' && !$this->Application->isAdmin) {
// ListImages aggregated tag uses this special
return '';
}
return parent::getMainSpecial($event);
}
- function customProcessing(&$event, $type)
+ /**
+ * Don't allow to delete primary category item image, when there are no more images
+ *
+ * @param kEvent $event
+ * @param string $type
+ * @return void
+ * @access protected
+ */
+ protected function customProcessing(&$event, $type)
{
- $object =& $event->GetObject();
- switch ($type)
- {
- case 'before' :
- if ($object->GetDBField('LocalImage'))
- {
- $object->SetDBField('Url', '');
- }
- else
- {
- $object->SetDBField('LocalPath', '');
- }
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
- if ($object->GetDBField('LocalThumb'))
- {
- $object->SetDBField('ThumbUrl', '');
- }
- else
- {
- $object->SetDBField('ThumbPath', '');
+ if ( $event->Name == 'OnMassDelete' && $type == 'before' ) {
+ $ids = $event->getEventParam('ids');
+
+ $parent_info = $object->getLinkedInfo($event->Special);
+
+ $sql = 'SELECT ImageId
+ FROM ' . $object->TableName . '
+ WHERE DefaultImg = 1 AND ' . $parent_info['ForeignKey'] . ' = ' . $parent_info['ParentId'];
+ $primary_file_id = $this->Conn->GetOne($sql);
+
+ if ( $primary_file_id ) {
+ $file_id_index = array_search($primary_file_id, $ids);
+
+ if ( $file_id_index ) {
+ // allow deleting of primary product file, when there is another file to make primary
+ $sql = 'SELECT COUNT(*)
+ FROM ' . $object->TableName . '
+ WHERE DefaultImg = 0 AND ' . $parent_info['ForeignKey'] . ' = ' . $parent_info['ParentId'];
+ $non_primary_file_count = $this->Conn->GetOne($sql);
+
+ if ( $non_primary_file_count ) {
+ unset($ids[$file_id_index]);
+ }
}
+ }
- if ($object->GetDBField('SameImages'))
- {
+ $event->setEventParam('ids', $ids);
+ }
+
+ switch ( $type ) {
+ case 'before' :
+ // empty unused fields
+ $object->SetDBField($object->GetDBField('LocalImage') ? 'Url' : 'LocalPath', '');
+ $object->SetDBField($object->GetDBField('LocalThumb') ? 'ThumbUrl' : 'ThumbPath', '');
+
+ if ( $object->GetDBField('SameImages') ) {
$object->SetDBField('LocalImage', 1);
$object->SetDBField('LocalPath', '');
$object->SetDBField('Url', '');
}
break;
- case 'after' :
- if ($object->GetDBField('DefaultImg') )
- {
- $sql = 'UPDATE '.$object->TableName.' SET DefaultImg=0 WHERE ResourceId='.
- $object->GetDBField('ResourceId').' AND ImageId<>'.
- $object->GetId();
- $res = $this->Conn->Query($sql);
+
+ case 'after':
+ // make sure, that there is only one primary image for the item
+ if ( $object->GetDBField('DefaultImg') ) {
+ $sql = 'UPDATE ' . $object->TableName . '
+ SET DefaultImg = 0
+ WHERE ResourceId = ' . $object->GetDBField('ResourceId') . ' AND ImageId <> ' . $object->GetID();
+ $this->Conn->Query($sql);
}
break;
- default:
}
}
- function ImageAction(&$event)
+ /**
+ * Performs temp-table related action on current image record
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function ImageAction(&$event)
{
$id = $event->getEventParam('id');
$object =& $this->Application->recallObject($event->Prefix.'.-item', $event->Prefix, Array ('skip_autoload' => true));
/* @var $object kDBItem */
if (in_array($event->Name, Array('OnBeforeDeleteFromLive','OnAfterClone')) ) {
$object->SwitchToLive();
}
elseif ($event->Name == 'OnBeforeItemDelete') {
// keep current table
}
else {
$object->SwitchToTemp();
}
$object->Load($id);
$fields = Array('LocalPath' => 'LocalImage', 'ThumbPath' => 'LocalThumb');
foreach ($fields as $a_field => $mode_field) {
$file = $object->GetField($a_field);
if (!$file) continue;
$source_file = FULL_PATH.$file;
switch ($event->Name) {
// Copy image files to pending dir and update corresponding fields in temp record
// Checking for existing files and renaming if nessessary - two users may upload same pending files at the same time!
case 'OnAfterCopyToTemp':
$new_file = IMAGES_PENDING_PATH . $this->ValidateFileName(FULL_PATH.IMAGES_PENDING_PATH, basename($file));
$dest_file = FULL_PATH.$new_file;
copy($source_file, $dest_file);
$object->SetFieldOption($a_field, 'skip_empty', false);
$object->SetDBField($a_field, $new_file);
break;
// Copy image files to live dir (checking if fileexists and renameing if nessessary)
// and update corresponding fields in temp record (which gets copied to live automatically)
case 'OnBeforeCopyToLive':
if ( $object->GetDBField($mode_field) ) { // if image is local
// rename file if it exists in live folder
$new_file = IMAGES_PATH . $this->ValidateFileName(FULL_PATH.IMAGES_PATH, basename($file));
$dest_file = FULL_PATH.$new_file;
rename($source_file, $dest_file);
}
else { // if image is remote url - remove local file (if any), update local file field with empty value
if (file_exists($source_file)) @unlink($source_file);
$new_file = '';
}
$object->SetFieldOption($a_field, 'skip_empty', false);
$object->SetDBField($a_field, $new_file);
break;
case 'OnBeforeDeleteFromLive': // Delete image files from live folder before copying over from temp
case 'OnBeforeItemDelete': // Delete image files when deleteing Image object
@unlink(FULL_PATH.$file);
break;
case 'OnAfterClone': // Copy files when cloning objects, renaming it on the fly
$path_info = pathinfo($file);
$new_file = $path_info['dirname'].'/'.$this->ValidateFileName(FULL_PATH.$path_info['dirname'], $path_info['basename']);
$dest_file = FULL_PATH . $new_file;
copy($source_file, $dest_file);
$object->SetFieldOption($a_field, 'skip_empty', false);
$object->SetDBField($a_field, $new_file);
break;
}
}
if ( in_array($event->Name, Array('OnAfterClone', 'OnBeforeCopyToLive', 'OnAfterCopyToTemp')) ) {
$object->Update(null, true);
}
}
function ValidateFileName($path, $name)
{
$parts = pathinfo($name);
$ext = '.'.$parts['extension'];
$filename = mb_substr($parts['basename'], 0, -mb_strlen($ext));
$new_name = $filename.$ext;
while ( file_exists($path.'/'.$new_name) )
{
if ( preg_match('/('.preg_quote($filename, '/').'_)([0-9]*)('.preg_quote($ext, '/').')/', $new_name, $regs) ) {
$new_name = $regs[1].($regs[2]+1).$regs[3];
}
else {
$new_name = $filename.'_1'.$ext;
}
}
return $new_name;
}
- /**
- * Enter description here...
+ /**
+ * Sets primary image of user/category/category item
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnSetPrimary(&$event)
+ protected function OnSetPrimary(&$event)
{
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$object->SetDBField('DefaultImg', 1);
$object->Update();
}
/**
- * Enter description here...
+ * Occurs before updating item
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
- $object =& $event->getObject();
-// $parent_info = $object->getLinkedInfo();
- $id = $object->GetDBField('ResourceId');
-// $id = $parent_info['ParentId'] ? $parent_info['ParentId'] : $this->Application->GetVar('p_id');
- $sql = 'SELECT ImageId FROM '.$object->TableName.' WHERE ResourceId='.$id.' AND DefaultImg=1';
- if(!$this->Conn->GetOne($sql))
- {
- $object->SetDBField('DefaultImg', 1);
- }
- if($object->GetDBField('DefaultImg') && $object->Validate())
- {
+ parent::OnBeforeItemUpdate($event);
- $sql = 'UPDATE '.$object->TableName.'
- SET DefaultImg = 0
- WHERE ResourceId = '.$id.' AND ImageId <> '.$object->GetDBField('ImageId');
- $this->Conn->Query($sql);
- $object->SetDBField('Enabled', 1);
- }
+ $this->processImageStatus($event);
}
- function OnAfterItemCreate(&$event)
+ /**
+ * Occurs after creating item
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnAfterItemCreate(&$event)
{
- $event->CallSubEvent('OnBeforeItemUpdate');
+ parent::OnAfterItemCreate($event);
+
+ $this->processImageStatus($event);
+
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$object->Update();
}
/**
- * 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
+ * Occurs before item changed
*
* @param kEvent $event
*/
- function OnMassDelete(&$event)
+ function processImageStatus(&$event)
{
- if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
- $event->status = kEvent::erFAIL;
- return;
- }
-
- $event->status=kEvent::erSUCCESS;
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
- $temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
+ $id = $object->GetDBField('ResourceId');
- $event->setEventParam('ids', $this->StoreSelectedIDs($event) );
- $this->customProcessing($event, 'before');
- $ids = $event->getEventParam('ids');
+ $sql = 'SELECT ImageId
+ FROM ' . $object->TableName . '
+ WHERE ResourceId = ' . $id . ' AND DefaultImg = 1';
+ $primary_image_id = $this->Conn->GetOne($sql);
- $object =& $event->getObject();
- $sql = 'SELECT ImageId FROM '.$object->TableName.' WHERE DefaultImg=1';
- $primary = $this->Conn->GetOne($sql);
- if( $primary && ($key = array_search($primary, $ids)) )
- {
- $sql = 'SELECT ImageId FROM '.$object->TableName.' WHERE DefaultImg=0';
- $res = $this->Conn->Query($sql);
- if($res)
- {
- unset($ids[$key]);
- }
- }
-
- if($ids)
- {
- $temp->DeleteItems($event->Prefix, $event->Special, $ids);
+ if ( !$primary_image_id ) {
+ $object->SetDBField('DefaultImg', 1);
}
- $this->clearSelectedIDs($event);
- }
- /*function OnAfterItemLoad(&$event)
- {
- $object =& $event->getObject();
+ if ( $object->GetDBField('DefaultImg') && $object->Validate() ) {
+ $sql = 'UPDATE ' . $object->TableName . '
+ SET DefaultImg = 0
+ WHERE ResourceId = ' . $id . ' AND ImageId <> ' . $object->GetDBField('ImageId');
+ $this->Conn->Query($sql);
- if ( $object->GetDBField('ThumbPath') || $object->GetDBField('SameImages') )
- {
- // return local image or url
- $path = $object->GetDBField('LocalThumb') ? PROTOCOL.SERVER_NAME.BASE_PATH.$object->GetDBField('ThumbPath') : $object->GetDBField('ThumbUrl');
- if ( $object->GetDBField('LocalThumb') && !file_exists(FULL_PATH.$object->GetDBField('ThumbPath')) ) $path = '';
- }
- else { // if we need full which is not the same as thumb
- $path = $object->GetDBField('LocalImage') ? PROTOCOL.SERVER_NAME.BASE_PATH.$object->GetDBField('LocalPath') : $object->GetDBField('Url');
- if ( $object->GetDBField('LocalImage') && !file_exists(FULL_PATH.$object->GetDBField('LocalPath')) ) $path = '';
+ $object->SetDBField('Enabled', 1);
}
+ }
- $object->SetDBField('ImageUrl', $path);
- }*/
-
- function SetCustomQuery(&$event)
+ /**
+ * Apply any custom changes to list's sql query
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
+ */
+ protected function SetCustomQuery(&$event)
{
parent::SetCustomQuery($event);
$object =& $event->getObject();
/* @var $object kDBList */
if (!$this->Application->isAdminUser) {
$object->addFilter('active', '%1$s.Enabled = 1');
}
$product_id = $event->getEventParam('product_id');
if ($product_id) {
$object->removeFilter('parent_filter');
$sql = 'SELECT ResourceId
FROM '.$this->Application->getUnitOption('p', 'TableName').'
WHERE ProductId = '.$product_id;
$resource_id = (int) $this->Conn->GetOne($sql);
$object->addFilter('product_images', '%1$s.ResourceId = '.$resource_id);
}
/********************************************/
$search_helper =& $this->Application->recallObject('SearchHelper');
/* @var $search_helper kSearchHelper */
$types = $event->getEventParam('types');
$except_types = $event->getEventParam('except');
$type_clauses = $this->getTypeClauses($event);
$search_helper->SetComplexFilter($event, $type_clauses, $types, $except_types);
}
/**
* Return type clauses for list bulding on front
*
* @param kEvent $event
* @return Array
*/
function getTypeClauses(&$event)
{
$type_clauses = Array ();
$type_clauses['additional']['include'] = '%1$s.DefaultImg != 1';
$type_clauses['additional']['except'] = '%1$s.DefaultImg = 1';
$type_clauses['additional']['having_filter'] = false;
return $type_clauses;
}
/**
* [AGENT] Remove unused images from "/system/images" and "/system/images/pending" folders
*
* @param kEvent $event
*/
function OnCleanImages(&$event)
{
// 1. get images, that are currently in use
$active_images = $this->_getActiveImages( $this->Application->getUnitOption('img', 'TableName') );
$active_images[] = 'noimage.gif';
// 2. get images on disk
$this->_deleteUnusedImages(FULL_PATH . IMAGES_PATH, $active_images);
// 3. get images in use from "images/pending" folder
$active_images = $this->_getPendingImages();
// 4. get image on disk
$this->_deleteUnusedImages(FULL_PATH . IMAGES_PENDING_PATH, $active_images);
}
/**
* Gets image filenames (no path) from given table
*
* @param string $image_table
* @return Array
*/
function _getActiveImages($image_table)
{
$sql = 'SELECT LocalPath, ThumbPath
FROM ' . $image_table . '
WHERE COALESCE(LocalPath, "") <> "" OR COALESCE(ThumbPath) <> ""';
$images = $this->Conn->Query($sql);
$active_images = Array ();
foreach ($images as $image) {
if ($image['LocalPath']) {
$active_images[] = basename($image['LocalPath']);
}
if ($image['ThumbPath']) {
$active_images[] = basename($image['ThumbPath']);
}
}
return $active_images;
}
/**
* Gets active images, that are currently beeing edited inside temporary tables
*
* @return Array
*/
function _getPendingImages()
{
$tables = $this->Conn->GetCol('SHOW TABLES');
$mask_edit_table = '/'.TABLE_PREFIX.'ses_(.*)_edit_' . TABLE_PREFIX . 'Images/';
$active_images = Array ();
foreach ($tables as $table) {
if (!preg_match($mask_edit_table, $table)) {
continue;
}
$active_images = array_unique( array_merge($active_images, $this->_getActiveImages($table)) );
}
return $active_images;
}
/**
* Deletes all files in given path, except of given $active_images
*
* @param string $path
* @param Array $active_images
*/
function _deleteUnusedImages($path, &$active_images)
{
$images = glob($path . '*.*');
if ($images) {
$images = array_map('basename', $images);
// delete images, that are on disk, but are not mentioned in Images table
$delete_images = array_diff($images, $active_images);
foreach ($delete_images as $delete_image) {
unlink($path . $delete_image);
}
}
}
/**
* [AGENT] Remove all images from "/system/images/resized" and "/system/images/pending/resized" folders
*
* @param kEvent $event
*/
function OnCleanResizedImages(&$event)
{
$images = glob(FULL_PATH . IMAGES_PATH . 'resized/*.*');
if ($images) {
foreach ($images as $image) {
unlink($image);
}
}
$images = glob(FULL_PATH . IMAGES_PENDING_PATH . 'resized/*.*');
if ($images) {
foreach ($images as $image) {
unlink($image);
}
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/images/images.php
===================================================================
--- branches/5.2.x/core/units/images/images.php (revision 14627)
+++ branches/5.2.x/core/units/images/images.php (revision 14628)
@@ -1,35 +1,42 @@
<?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!');
+defined('FULL_PATH') or die('restricted access!');
- class ImagesItem extends kDBItem {
+class ImagesItem extends kDBItem {
- function Delete($id)
- {
- $this->Load($id);
- $local_path = FULL_PATH.$this->GetDBField('LocalPath');
- $thumb_path = FULL_PATH.$this->GetDBField('ThumbPath');
- if(file_exists($local_path))
- {
- unlink($local_path);
- }
- if(file_exists($thumb_path))
- {
- unlink($thumb_path);
- }
- return parent::Delete($id);
+ /**
+ * Deletes the record from database
+ *
+ * @param int $id
+ * @return bool
+ * @access public
+ */
+ public function Delete($id)
+ {
+ $this->Load($id);
+ $local_path = FULL_PATH . $this->GetDBField('LocalPath');
+ $thumb_path = FULL_PATH . $this->GetDBField('ThumbPath');
+
+ if ( file_exists($local_path) ) {
+ unlink($local_path);
}
- }
\ No newline at end of file
+ if ( file_exists($thumb_path) ) {
+ unlink($thumb_path);
+ }
+
+ return parent::Delete($id);
+ }
+}
\ No newline at end of file
Index: branches/5.2.x/core/units/configuration/configuration_event_handler.php
===================================================================
--- branches/5.2.x/core/units/configuration/configuration_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/configuration/configuration_event_handler.php (revision 14628)
@@ -1,291 +1,298 @@
<?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 ConfigurationEventHandler extends kDBEventHandler {
/**
* Changes permission section to one from REQUEST, not from config
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
$event->setEventParam('PermSection', $this->Application->GetVar('section'));
return parent::CheckPermission($event);
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
+ * @return void
* @access protected
- * @see OnListBuild
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
/* @var $object kDBList */
$module = $this->Application->GetVar('module');
$section = $this->Application->GetVar('section');
$object->addFilter('module_filter', '%1$s.ModuleOwner = '.$this->Conn->qstr($module));
$object->addFilter('section_filter', '%1$s.Section = '.$this->Conn->qstr($section));
if (!$this->Application->ConfigValue('AllowAdminConsoleInterfaceChange')) {
$object->addFilter('interface_change_filter', '%1$s.VariableName <> "AdminConsoleInterface"');
}
if (defined('IS_INSTALL') && IS_INSTALL) {
$object->addFilter('install_filter', '%1$s.Install = 1');
}
$object->addFilter('visible_filter', '%1$s.Heading <> ""');
}
/**
- * Enter description here...
+ * Performs validation of configuration variable value
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
static $default_field_options = null;
+ parent::OnBeforeItemUpdate($event);
+
$object =& $event->getObject();
/* @var $object kDBItem */
// ability to validate each configuration variable separately
- if (!isset($default_field_options)) {
+ if ( !isset($default_field_options) ) {
$default_field_options = $object->GetFieldOptions('VariableValue');
}
$new_field_options = $default_field_options;
$validation = $object->GetDBField('Validation');
- if ($validation) {
+
+ if ( $validation ) {
$new_field_options = array_merge($new_field_options, unserialize($validation));
}
+
$object->SetFieldOptions('VariableValue', $new_field_options);
// if password field is empty, then don't update
- if ($object->GetDBField('ElementType') == 'password') {
- if (trim($object->GetDBField('VariableValue')) == '') {
+ if ( $object->GetDBField('ElementType') == 'password' ) {
+ if ( trim($object->GetDBField('VariableValue')) == '' ) {
$field_options = $object->GetFieldOptions('VariableValue');
$field_options['skip_empty'] = 1;
$object->SetFieldOptions('VariableValue', $field_options);
- }else {
+ }
+ else {
$password_formatter =& $this->Application->recallObject('kPasswordFormatter');
+ /* @var $password_formatter kPasswordFormatter */
+
$object->SetDBField('VariableValue', $password_formatter->EncryptPassword($object->GetDBField('VariableValue'), 'b38'));
}
}
- $field_values = $this->Application->GetVar( $event->getPrefixSpecial(true) );
-
- $state_country_hash = Array (
- 'Comm_State' => 'Comm_Country',
- 'Comm_Shipping_State' => 'Comm_Shipping_Country'
- );
-
$field_name = $object->GetDBField('VariableName');
- if (array_key_exists($field_name, $state_country_hash)) {
+ $field_values = $this->Application->GetVar($event->getPrefixSpecial(true));
+ $state_country_hash = Array ('Comm_State' => 'Comm_Country', 'Comm_Shipping_State' => 'Comm_Shipping_Country');
+
+ if ( array_key_exists($field_name, $state_country_hash) ) {
// if this is state field
$sql = 'SELECT VariableId
FROM ' . $this->Application->getUnitOption('conf', 'TableName') . '
WHERE VariableName = "' . $state_country_hash[$field_name] . '"';
$country_variable_id = $this->Conn->GetOne($sql);
$check_state = $object->GetDBField('VariableValue');
$check_country = $field_values[$country_variable_id]['VariableValue'];
- if (!$check_country || !$check_state) {
- return true;
+ if ( !$check_country || !$check_state ) {
+ return;
}
$cs_helper =& $this->Application->recallObject('CountryStatesHelper');
/* @var $cs_helper kCountryStatesHelper */
$state_iso = $cs_helper->getStateIso($check_state, $check_country);
- if ($state_iso !== false) {
+ if ( $state_iso !== false ) {
$object->SetDBField('VariableValue', $state_iso);
}
else {
// selected state doesn't belong to selected country
$object->SetError('VariableValue', 'invalid_state', 'la_InvalidState');
}
}
- if ($object->GetDBField('VariableName') == 'AdminConsoleInterface') {
+ if ( $object->GetDBField('VariableName') == 'AdminConsoleInterface' ) {
$can_change = $this->Application->ConfigValue('AllowAdminConsoleInterfaceChange');
- if (($object->GetDBField('VariableValue') != $object->GetOriginalField('VariableValue')) && !$can_change) {
+ if ( ($object->GetDBField('VariableValue') != $object->GetOriginalField('VariableValue')) && !$can_change ) {
$object->SetError('VariableValue', 'not_allowed', 'la_error_OperationNotAllowed');
}
-
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
if ($object->GetDBField('ElementType') == 'password') {
if (trim($object->GetDBField('VariableValue')) == '') {
$field_options = $object->GetFieldOptions('VariableValue');
unset($field_options['skip_empty']);
$object->SetFieldOptions('VariableValue', $field_options);
}
}
// allows to check if variable's value was changed now
$variable_name = $object->GetDBField('VariableName');
$variable_value = $object->GetDBField('VariableValue');
$watch_variables = Array (
'Require_AdminSSL', 'AdminSSL_URL', 'AdvancedUserManagement',
'Site_Name', 'AdminConsoleInterface', 'UsePopups'
);
if (in_array($variable_name, $watch_variables)) {
$changed = $this->Application->GetVar($event->getPrefixSpecial() . '_changed', Array ());
if ($variable_value != $object->GetOriginalField('VariableValue')) {
$changed[] = $variable_name;
$this->Application->SetVar($event->getPrefixSpecial() . '_changed', $changed);
}
switch ($variable_name) {
case 'Require_AdminSSL':
case 'AdminSSL_URL':
static $skin_deleted = false;
if (in_array($variable_name, $changed) && !$skin_deleted) {
// when administrative console is moved to SSL mode, then delete skin
$skin_helper =& $this->Application->recallObject('SkinHelper');
/* @var $skin_helper SkinHelper */
$skin_file = $skin_helper->getSkinPath();
if (file_exists($skin_file)) {
unlink($skin_file);
}
$skin_deleted = true;
}
break;
}
}
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnUpdate(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return ;
}
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
// 1. save user selected module root category
$new_category_id = getArrayValue($items_info, 'ModuleRootCategory', 'VariableValue');
if ($new_category_id !== false) {
unset($items_info['ModuleRootCategory']);
}
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
if ($items_info) {
$has_error = false;
foreach ($items_info as $id => $field_values) {
$object->Clear(); // clear validation errors from previous variable
$object->Load($id);
$object->SetFieldsFromHash($field_values);
if (!$object->Update($id)) {
// don't stop when error found !
$has_error = true;
}
}
$event->status = $has_error ? kEvent::erFAIL : kEvent::erSUCCESS;
}
if ($event->status == kEvent::erSUCCESS) {
if ($new_category_id !== false) {
// root category was submitted
$module = $this->Application->GetVar('module');
$root_category_id = $this->Application->findModule('Name', $module, 'RootCat');
if ($root_category_id != $new_category_id) {
// root category differs from one in db
$fields_hash = Array('RootCat' => $new_category_id);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX.'Modules', 'Name = '.$this->Conn->qstr($module));
}
}
// reset cache
$changed = $this->Application->GetVar($event->getPrefixSpecial() . '_changed', Array ());
$require_refresh = Array (
'AdvancedUserManagement', 'Site_Name', 'AdminConsoleInterface', 'UsePopups'
);
$refresh_sections = array_intersect($require_refresh, $changed);
$require_full_refresh = Array ('Site_Name', 'AdminConsoleInterface');
if (array_intersect($require_full_refresh, $changed)) {
$event->SetRedirectParam('refresh_all', 1);
} elseif ($refresh_sections) {
// reset sections too, because of AdvancedUserManagement
$event->SetRedirectParam('refresh_tree', 1);
}
$this->Application->DeleteUnitCache($refresh_sections ? true : false);
}
elseif ($this->Application->GetVar('errors_' . $event->getPrefixSpecial())) {
// because we have list out there, and this is item
$this->Application->removeObject( $event->getPrefixSpecial() );
}
// keeps module and section in REQUEST to ensure, that last admin template will work
$event->SetRedirectParam('module', $this->Application->GetVar('module'));
$event->SetRedirectParam('section', $this->Application->GetVar('section'));
}
/**
* Process items from selector (selected_ids var, key - prefix, value - comma separated ids)
*
* @param kEvent $event
*/
function OnProcessSelected(&$event)
{
$selected_ids = $this->Application->GetVar('selected_ids');
$this->Application->StoreVar('ModuleRootCategory', $selected_ids['c']);
$event->SetRedirectParam('opener', 'u');
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/configuration/configuration.php
===================================================================
--- branches/5.2.x/core/units/configuration/configuration.php (revision 14627)
+++ branches/5.2.x/core/units/configuration/configuration.php (revision 14628)
@@ -1,55 +1,64 @@
<?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!');
+defined('FULL_PATH') or die('restricted access!');
- class ConfigurationItem extends kDBItem {
+class ConfigurationItem extends kDBItem {
- /**
- * Returns key clause for Load,Update,Delete operations
- * Applies current section filter to affect only configuration values,
- * that are allowed by section in get, which is permission checked before
- *
- * @param string $method
- * @param Array $keys_hash
- * @return string
- */
- function GetKeyClause($method=null, $keys_hash = null)
- {
- $keys_hash['Section'] = $this->Application->GetVar('section');
- $keys_hash[$this->IDField] = $this->ID;
- return parent::GetKeyClause($method, $keys_hash);
+ /**
+ * Returns part of SQL WHERE clause identifying the record, ex. id = 25
+ *
+ * @param string $method Child class may want to know who called GetKeyClause, Load(), Update(), Delete() send its names as method
+ * @param Array $keys_hash alternative, then item id, keys hash to load item by
+ * @see kDBItem::Load()
+ * @see kDBItem::Update()
+ * @see kDBItem::Delete()
+ *
+ * @return string
+ * @access protected
+ */
+ protected function GetKeyClause($method = null, $keys_hash = null)
+ {
+ $keys_hash['Section'] = $this->Application->GetVar('section');
+ $keys_hash[$this->IDField] = $this->GetID();
+
+ return parent::GetKeyClause($method, $keys_hash);
+ }
+
+ /**
+ * Set's field error, if pseudo passed not found then create it with message text supplied.
+ * Don't overwrite existing pseudo translation.
+ *
+ * @param string $field
+ * @param string $pseudo
+ * @param string $error_label
+ * @param Array $error_params
+ *
+ * @return bool
+ * @access public
+ */
+ public function SetError($field, $pseudo, $error_label = null, $error_params = null)
+ {
+ if ( !parent::SetError($field, $pseudo, $error_label, $error_params) ) {
+ // this field already has an error -> don't overwrite it
+ return false;
}
- /**
- * Set's field error, if pseudo passed not found then create it with message text supplied.
- * Don't owerrite existing pseudo translation.
- *
- * @param string $field
- * @param string $pseudo
- * @param string $error_label
- */
- function SetError($field, $pseudo, $error_label = null, $error_params = null)
- {
- if (!parent::SetError($field, $pseudo, $error_label, $error_params)) {
- // this field already has an error -> don't overwrite it
- return false;
- }
-
- $list_errors = $this->Application->GetVar('errors_' . $this->getPrefixSpecial(), Array ());
- $list_errors[ $this->GetDBField('VariableName') ] = $this->GetErrorMsg($field);
- $this->Application->SetVar('errors_' . $this->getPrefixSpecial(), $list_errors);
- }
-
- }
\ No newline at end of file
+ $list_errors = $this->Application->GetVar('errors_' . $this->getPrefixSpecial(), Array ());
+ $list_errors[ $this->GetDBField('VariableName') ] = $this->GetErrorMsg($field);
+ $this->Application->SetVar('errors_' . $this->getPrefixSpecial(), $list_errors);
+
+ return true;
+ }
+}
\ No newline at end of file
Index: branches/5.2.x/core/units/content/content_eh.php
===================================================================
--- branches/5.2.x/core/units/content/content_eh.php (revision 14627)
+++ branches/5.2.x/core/units/content/content_eh.php (revision 14628)
@@ -1,37 +1,39 @@
<?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 ContentEventHandler extends kDBEventHandler {
/**
- * Checks permissions of user
+ * Checks user permission to execute given $event
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$user_id = $this->Application->RecallVar('user_id');
// user can change top category
$top_category = $this->Application->getBaseCategory();
$perm_status = $perm_helper->CheckUserPermission($user_id, 'CATEGORY.MODIFY', 0, $top_category);
return $perm_helper->finalizePermissionCheck($event, $perm_status);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/user_groups/user_groups_eh.php
===================================================================
--- branches/5.2.x/core/units/user_groups/user_groups_eh.php (revision 14627)
+++ branches/5.2.x/core/units/user_groups/user_groups_eh.php (revision 14628)
@@ -1,151 +1,154 @@
<?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 UserGroupsEventHandler extends kDBEventHandler {
/**
* Adds user as member for selected groups
*
* @param kEvent $event
*/
function OnProcessSelected(&$event)
{
if ($event->Prefix == 'u-ug') {
$new_groups = $this->Application->GetVar('g');
if (!$new_groups) {
return ;
}
$new_groups = array_keys($new_groups);
// don't insert duplicate group membership record
$user_id = $this->Application->GetVar('u_id');
$table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:u');
$sql = 'SELECT GroupId
FROM '.$table_name.'
WHERE PortalUserId = '.(int)$user_id;
$old_groups = $this->Conn->GetCol($sql);
$new_groups = array_diff($new_groups, $old_groups);
if ($new_groups) {
foreach ($new_groups as $new_group) {
$fields_hash = Array (
'GroupId' => $new_group,
'PortalUserId' => $user_id,
);
$this->Conn->doInsert($fields_hash, $table_name);
}
}
}
elseif ($event->Prefix == 'g-ug') {
$new_users = $this->Application->GetVar('u');
if (!$new_users) {
return ;
}
$new_users = array_keys($new_users);
// don't insert duplicate group membership record
$group_id = $this->Application->GetVar('g_id');
$table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:g');
$sql = 'SELECT PortalUserId
FROM ' . $table_name . '
WHERE GroupId = ' . (int)$group_id;
$old_users = $this->Conn->GetCol($sql);
$new_users = array_diff($new_users, $old_users);
if ($new_users) {
foreach ($new_users as $new_user) {
$fields_hash = Array (
'GroupId' => $group_id,
'PortalUserId' => $new_user,
);
$this->Conn->doInsert($fields_hash, $table_name);
}
}
}
$this->Application->StoreVar($this->Application->GetTopmostPrefix($event->Prefix).'_modified', '1', true); // true for optional
$event->SetRedirectParam('opener', 'u');
}
/**
* Sets primary group for user (in editing only)
*
* @param kEvent $event
*/
function OnSetPrimary(&$event)
{
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
$user =& $this->Application->recallObject('u');
/* @var $user kDBItem */
$user->SetDBField('PrimaryGroupId', array_shift($ids));
$user->Update();
}
$this->clearSelectedIDs($event);
}
/**
* Don't allow primary group record deleting
*
* @param kEvent $event
+ * @param string $type
+ * @return void
+ * @access protected
*/
- function customProcessing(&$event, $type)
+ protected function customProcessing(&$event, $type)
{
if ($event->Name == 'OnMassDelete' && $type == 'before') {
$ids = $event->getEventParam('ids');
if ($ids) {
$object =& $event->getObject( Array('skip_autoload' => true) );
if ($event->Prefix == 'u-ug') {
// allow deleting non-primary group of current user ($ids - groups)
$sql = 'SELECT PrimaryGroupId
FROM ' . $this->Application->GetTempName(TABLE_PREFIX . 'PortalUser', 'prefix:u') . '
WHERE PortalUserId = ' . (int)$this->Application->GetVar('u_id');
$primary_group_id = (int)$this->Conn->GetOne($sql);
$index = array_search($primary_group_id, $ids);
if ($index !== false) {
unset($ids[$index]);
$event->setEventParam('ids', $ids);
}
}
elseif ($event->Prefix == 'g-ug') {
// allow deleting users from group record, then it's not their primary group ($ids - users)
$group_id = (int)$this->Application->GetVar('g_id');
$sql = 'SELECT PortalUserId
FROM ' . TABLE_PREFIX . 'PortalUser' . '
WHERE PortalUserId IN (' . implode(',', $ids) . ') AND PrimaryGroupId = ' . $group_id;
$exclude_users = $this->Conn->GetCol($sql);
$event->setEventParam('ids', array_diff($ids, $exclude_users));
}
}
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/user_groups/user_groups_dbitem.php
===================================================================
--- branches/5.2.x/core/units/user_groups/user_groups_dbitem.php (revision 14627)
+++ branches/5.2.x/core/units/user_groups/user_groups_dbitem.php (revision 14628)
@@ -1,42 +1,42 @@
<?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!');
+defined('FULL_PATH') or die('restricted access!');
- class UserGroups_DBItem extends kDBItem {
+class UserGroups_DBItem extends kDBItem {
- /**
- * Returns part of SQL WHERE clause identifing the record, ex. id = 25
- *
- * @access public
- * @param string $method Child class may want to know who called GetKeyClause, Load(), Update(), Delete() send its names as method
- * @return void
- * @see kDBItem::Load()
- * @see kDBItem::Update()
- * @see kDBItem::Delete()
- */
- function GetKeyClause($method=null, $keys_hash = null)
- {
- $table_info = $this->getLinkedInfo($this->Special, true);
- if ($table_info) {
- // we have parent info, then use it
- $keys_hash = Array(
- $this->IDField => $this->ID,
- $table_info['ForeignKey'] => $table_info['ParentId'],
- );
- }
- return parent::GetKeyClause($method, $keys_hash);
+ /**
+ * Returns part of SQL WHERE clause identifying the record, ex. id = 25
+ *
+ * @param string $method Child class may want to know who called GetKeyClause, Load(), Update(), Delete() send its names as method
+ * @param Array $keys_hash alternative, then item id, keys hash to load item by
+ * @see kDBItem::Load()
+ * @see kDBItem::Update()
+ * @see kDBItem::Delete()
+ *
+ * @return string
+ * @access protected
+ */
+ protected function GetKeyClause($method = null, $keys_hash = null)
+ {
+ $table_info = $this->getLinkedInfo($this->Special, true);
+
+ if ( $table_info ) {
+ // we have parent info, then use it
+ $keys_hash = Array ($this->IDField => $this->GetID(), $table_info['ForeignKey'] => $table_info['ParentId']);
}
- }
\ No newline at end of file
+ return parent::GetKeyClause($method, $keys_hash);
+ }
+}
\ No newline at end of file
Index: branches/5.2.x/core/units/category_items/category_items_event_handler.php
===================================================================
--- branches/5.2.x/core/units/category_items/category_items_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/category_items/category_items_event_handler.php (revision 14628)
@@ -1,139 +1,153 @@
<?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 CategoryItemsEventHander extends kDBEventHandler
{
-
/**
* Setting language dependant navbar as calculated field
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
$ml_formatter =& $this->Application->recallObject('kMultiLanguage');
+ /* @var $ml_formatter kMultiLanguage */
+
$object->addCalculatedField('CategoryName', 'c.'.$ml_formatter->LangFieldName('CachedNavbar'));
}
/**
* Set's new category as primary for product
*
* @param kEvent $event
*/
function OnSetPrimary(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
$ids = $this->StoreSelectedIDs($event);
if($ids)
{
$id = array_shift($ids);
$table_info = $object->getLinkedInfo();
$this->Conn->Query('UPDATE '.$object->TableName.' SET PrimaryCat = 0 WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
$this->Conn->Query('UPDATE '.$object->TableName.' SET PrimaryCat = 1 WHERE ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].') AND (CategoryId = '.$id.')');
}
$event->setRedirectParams(Array('opener' => 's'), true);
}
/**
* Apply custom processing to item
*
* @param kEvent $event
+ * @param string $type
+ * @return void
+ * @access protected
*/
- function customProcessing(&$event, $type)
+ protected function customProcessing(&$event, $type)
{
if($event->Name == 'OnMassDelete')
{
$object =& $event->getObject();
$table_info = $object->getLinkedInfo();
switch ($type)
{
case 'before':
$ids = $event->getEventParam('ids');
if($ids)
{
$ids = $this->Conn->GetCol('SELECT CategoryId FROM '.$object->TableName.' WHERE (PrimaryCat=0) AND ('.$table_info['ForeignKey'].'='.$table_info['ParentId'].') AND CategoryId IN ('.implode(',',$ids).')');
$event->setEventParam('ids',$ids);
}
break;
// not needed because 'before' does not allow to delete primary cat!
/*case 'after':
// set 1st not deleted category as primary
$has_primary = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$object->TableName.' WHERE (PrimaryCat=1) AND ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].')');
if(!$has_primary)
{
$cat_id = $this->Conn->GetOne('SELECT CategoryId FROM '.$object->TableName.' WHERE '.$table_info['ForeignKey'].' = '.$table_info['ParentId']);
$this->Conn->Query('UPDATE '.$object->TableName.' SET PrimaryCat = 1 WHERE ('.$table_info['ForeignKey'].' = '.$table_info['ParentId'].') AND (CategoryId = '.$cat_id.')');
}
break;*/
}
}
}
/**
* Removes primary mark from cloned category items record
*
* @param kEvent $event
*/
function OnAfterClone(&$event)
{
$id = $event->getEventParam('id');
$table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$sql = 'UPDATE %s SET PrimaryCat = 0 WHERE %s = %s';
$this->Conn->Query( sprintf($sql, $table, $id_field, $id) );
}
/**
* Deletes items of requested type from requested categories.
* In case if item is deleted from it's last category, then delete item too.
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnDeleteFromCategory(&$event)
+ protected function OnDeleteFromCategory(&$event)
{
$category_ids = $event->getEventParam('category_ids');
- if(!$category_ids) return false;
+
+ if ( !$category_ids ) {
+ return ;
+ }
$item_prefix = $event->getEventParam('item_prefix');
- $item =& $this->Application->recallObject($item_prefix.'.-item', null, Array('skip_autoload' => true));
+ $item =& $this->Application->recallObject($item_prefix . '.-item', null, Array ('skip_autoload' => true));
+ /* @var $item kCatDBItem */
$ci_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$item_table = $this->Application->getUnitOption($item_prefix, 'TableName');
- $sql = 'SELECT ItemResourceId, CategoryId FROM %1$s INNER JOIN %2$s ON (%1$s.ResourceId = %2$s.ItemResourceId) WHERE CategoryId IN (%3$s)';
- $category_items = $this->Conn->Query( sprintf($sql, $item_table, $ci_table, implode(',', $category_ids) ) );
+ $sql = 'SELECT ItemResourceId, CategoryId
+ FROM %1$s
+ INNER JOIN %2$s ON (%1$s.ResourceId = %2$s.ItemResourceId)
+ WHERE CategoryId IN (%3$s)';
+ $category_items = $this->Conn->Query( sprintf($sql, $item_table, $ci_table, implode(',', $category_ids)) );
- $item_hash = Array();
- foreach($category_items as $ci_row)
- {
+ $item_hash = Array ();
+ foreach ($category_items as $ci_row) {
$item_hash[ $ci_row['ItemResourceId'] ][] = $ci_row['CategoryId'];
}
- foreach($item_hash as $item_resource_id => $delete_category_ids)
- {
+ foreach ($item_hash as $item_resource_id => $delete_category_ids) {
$item->Load($item_resource_id, 'ResourceId');
$item->DeleteFromCategories($delete_category_ids);
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/category_items/category_items_dbitem.php
===================================================================
--- branches/5.2.x/core/units/category_items/category_items_dbitem.php (revision 14627)
+++ branches/5.2.x/core/units/category_items/category_items_dbitem.php (revision 14628)
@@ -1,47 +1,47 @@
<?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 CategoryItems_DBItem extends kDBItem
- {
+defined('FULL_PATH') or die('restricted access!');
+class CategoryItems_DBItem extends kDBItem {
- /**
- * Returns part of SQL WHERE clause identifing the record, ex. id = 25
- *
- * @access public
- * @param string $method Child class may want to know who called GetKeyClause, Load(), Update(), Delete() send its names as method
- * @return void
- * @see kDBItem::Load()
- * @see kDBItem::Update()
- * @see kDBItem::Delete()
- */
- function GetKeyClause($method=null)
- {
- $table_info = $this->getLinkedInfo();
- return '(CategoryId='.$this->ID.' AND '.$table_info['ForeignKey'].'='.$table_info['ParentId'].')';
- }
+ /**
+ * Returns part of SQL WHERE clause identifying the record, ex. id = 25
+ *
+ * @param string $method Child class may want to know who called GetKeyClause, Load(), Update(), Delete() send its names as method
+ * @see kDBItem::Load()
+ * @see kDBItem::Update()
+ * @see kDBItem::Delete()
+ *
+ * @return string
+ * @access protected
+ */
+ protected function GetKeyClause($method = null)
+ {
+ $table_info = $this->getLinkedInfo();
- /**
- * Generate and set new temporary id
- *
- * @access private
- */
- function setTempID()
- {
- // don't set temp id for this item
- }
+ return '(CategoryId=' . $this->GetID() . ' AND ' . $table_info['ForeignKey'] . '=' . $table_info['ParentId'] . ')';
+ }
- }
\ No newline at end of file
+ /**
+ * Generate and set new temporary id
+ *
+ * @return void
+ * @access public
+ */
+ public function setTempID()
+ {
+ // don't set temp id for this item
+ }
+}
\ No newline at end of file
Index: branches/5.2.x/core/units/category_items/category_items_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/category_items/category_items_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/category_items/category_items_tag_processor.php (revision 14628)
@@ -1,45 +1,46 @@
<?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 CategoryItemsTagProcessor extends kDBTagProcessor
{
function CategoryName($params)
{
$field = $this->SelectParam($params, 'name,field');
$object =& $this->getObject($params);
/* @var $object kDBList */
$root_phrase = $this->Application->Phrase(($this->Application->isAdmin ? 'la_' : 'lu_') . 'rootcategory_name');
$cached_navbar = preg_replace('/^(Content&\|&|Content)/i', '', $object->GetDBField($field));
$value = str_replace('&|&', ' > ', $cached_navbar);
$ret = $root_phrase . ($value ? ' > ' : '') . $value;
if ($object->GetDBField('PrimaryCat')) {
$label = $params['primary_title'];
$ret .= ' (' . $this->Application->Phrase($label) . ')';
}
return $ret;
}
function GetMainID()
{
- $object =& $this->Application->recallObject( $this->getPrefixSpecial() );
+ $object =& $this->getObject();
$table_info = $object->getLinkedInfo();
+
return $table_info['ParentId'];
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/groups/groups_event_handler.php
===================================================================
--- branches/5.2.x/core/units/groups/groups_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/groups/groups_event_handler.php (revision 14628)
@@ -1,63 +1,68 @@
<?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 GroupsEventHandler extends kDBEventHandler {
/**
- * Adds grouping by userid
+ * Adds grouping by user id
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
/* @var $object kDBList */
switch ($event->Special) {
case 'user':
$user_id = $this->Application->GetVar('u_id');
if ($user_id !== false) {
// show only groups, that user doesn't belong to
$table_name = $this->Application->GetTempName(TABLE_PREFIX.'UserGroup', 'prefix:u');
$sql = 'SELECT GroupId
FROM ' . $table_name . '
WHERE PortalUserId = ' . (int)$user_id;
$group_ids = $this->Conn->GetCol($sql);
// add system groups
array_push($group_ids, $this->Application->ConfigValue('User_GuestGroup')); // Guest
array_push($group_ids, $this->Application->ConfigValue('User_LoggedInGroup')); // Everyone
$object->addFilter('already_member_filter', '%1$s.GroupId NOT IN ('.implode(',', $group_ids).')');
}
break;
}
}
/**
* Refreshes left tree on save
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnSave(&$event)
+ protected function OnSave(&$event)
{
parent::OnSave($event);
$this->Application->StoreVar('refresh_tree', 1);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/reviews/reviews_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/reviews/reviews_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/reviews/reviews_tag_processor.php (revision 14628)
@@ -1,151 +1,153 @@
<?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 ReviewsTagProcessor extends kDBTagProcessor
{
/**
* Returns a link for editing product
*
* @param Array $params
* @return string
*/
function ItemEditLink($params)
{
$object =& $this->getObject();
/* @var $object kDBList */
$item_prefix = $this->Application->findModule('Name', $object->GetDBField('Module'), 'Var');
$edit_template = $this->Application->getUnitOption($item_prefix, 'AdminTemplatePath') . '/' . $this->Application->getUnitOption($item_prefix, 'AdminTemplatePrefix') . 'edit';
$url_params = Array (
'm_opener' => 'd',
$item_prefix.'_mode' => 't',
$item_prefix.'_event' => 'OnEdit',
$item_prefix.'_id' => $object->GetDBField('CatalogItemId'),
'm_cat_id' => $object->GetDBField('CatalogItemCategory'),
'pass' => 'all,'.$item_prefix,
'no_pass_through' => 1,
);
return $this->Application->HREF($edit_template,'', $url_params);
}
/**
* Get's reuested field value
*
* @param Array $params
* @return string
* @access public
*/
function Field($params)
{
$field = $this->SelectParam($params, 'name,field');
- $object =& $this->Application->recallObject($this->getPrefixSpecial(),$this->Prefix, $params);
+
+ $object =& $this->getObject($params);
+ /* @var $object kDBItem */
if ($field == 'ReviewText') {
if ($object->GetDBField('TextFormat') == 1) {
$params['no_special'] = 'no_special';
}
else {
unset($params['no_special']);
}
}
return parent::Field($params);
}
function AlreadyReviewed($params)
{
$parent_prefix = $this->Application->getUnitOption($this->Prefix, 'ParentPrefix');
$main_object =& $this->Application->recallObject($parent_prefix);
/* @var $main_object kCatDBItem */
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$spam_helper->InitHelper($main_object->GetDBField('ResourceId'), 'Review', 0, $main_object->GetCol('ResourceId'));
return $spam_helper->InSpamControl();
}
function HasError($params)
{
$object =& $this->Application->recallObject($this->getPrefixSpecial(), $this->Prefix, $params);
if (method_exists($object, 'GetErrorMsg')) {
return parent::HasError($params);
}
else {
return 0;
}
}
/**
* Preserve main item id in subitem pagination url
*
* @param Array $params
* @return string
*/
function PageLink($params)
{
$object =& $this->getObject($params);
/* @var kDBList */
$parent_info = $object->getLinkedInfo();
if ($parent_info['ParentId'] > 0) {
$params['pass'] = 'm,'.$this->getPrefixSpecial().','.$parent_info['ParentPrefix'];
}
return parent::PageLink($params);
}
function InitCatalogTab($params)
{
$tab_params['mode'] = $this->Application->GetVar('tm'); // single/multi selection possible
$tab_params['special'] = $this->Application->GetVar('ts'); // use special for this tab
$tab_params['dependant'] = $this->Application->GetVar('td'); // is grid dependant on categories grid
// set default params (same as in catalog)
if ($tab_params['mode'] === false) $tab_params['mode'] = 'multi';
if ($tab_params['special'] === false) $tab_params['special'] = '';
if ($tab_params['dependant'] === false) $tab_params['dependant'] = 'yes';
// pass params to block with tab content
$params['name'] = $params['render_as'];
$params['prefix'] = trim($this->Prefix.'.'.($tab_params['special'] ? $tab_params['special'] : $this->Special), '.');
$params['cat_prefix'] = trim('c.'.($tab_params['special'] ? $tab_params['special'] : $this->Special), '.');
$params['tab_mode'] = $tab_params['mode'];
$params['grid_name'] = ($tab_params['mode'] == 'multi') ? $params['default_grid'] : $params['radio_grid'];
$params['tab_dependant'] = $tab_params['dependant'];
$params['show_category'] = $tab_params['special'] == 'showall' ? 1 : 0; // this is advanced view -> show category name
$params['tab_name'] = $this->Application->GetVar('tab_name');
return $this->Application->ParseBlock($params, 1);
}
/**
* Returns reviews count for each item type (in "Reviews" section)
*
* @param Array $params
* @return string
*/
function CatalogItemCount($params)
{
$params['skip_quering'] = true;
$object =& $this->GetList($params);
return $object->GetRecordsCount(false) != $object->GetRecordsCount() ? $object->GetRecordsCount().' / '.$object->GetRecordsCount(false) : $object->GetRecordsCount();
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/reviews/reviews_event_handler.php
===================================================================
--- branches/5.2.x/core/units/reviews/reviews_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/reviews/reviews_event_handler.php (revision 14628)
@@ -1,565 +1,583 @@
<?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 ReviewsEventHandler extends kDBEventHandler
{
/**
* Get's special of main item for linking with subitem
*
* @param kEvent $event
* @return string
*/
function getMainSpecial(&$event)
{
if ($event->Special == 'product' && !$this->Application->isAdmin) {
// rev.product should auto-link
return '';
}
return parent::getMainSpecial($event);
}
/**
* Checks REVIEW/REVIEW.PENDING permission by main object primary category (not current category)
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
if ($event->Name == 'OnAddReview' || $event->Name == 'OnCreate') {
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$main_object =& $this->Application->recallObject($parent_prefix);
/* @var $main_object kCatDBItem */
$perm_name = $this->getPermPrefix($event).'.REVIEW';
$res = $this->Application->CheckPermission($perm_name, 0, $main_object->GetDBField('CategoryId')) ||
$this->Application->CheckPermission($perm_name.'.PENDING', 0, $main_object->GetDBField('CategoryId'));
if (!$res) {
$event->status = kEvent::erPERM_FAIL;
}
return $res;
}
$check_events = Array (
'OnItemBuild', 'OnUpdate', /*'OnMassApprove', 'OnMassDecline'*/
);
$perm_category = $this->_getReviewCategory($event);
if (in_array($event->Name, $check_events)) {
// check for PRODUCT.VIEW permission
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$perm_prefix = $this->getPermPrefix($event);
if ($perm_category === false) {
// no item id present -> allow
return true;
}
switch ($event->Name) {
case 'OnItemBuild':
$res = $this->Application->CheckPermission($perm_prefix . '.VIEW', 0, $perm_category);
break;
case 'OnUpdate':
case 'OnMassApprove':
case 'OnMassDecline':
$res = $this->Application->CheckPermission($perm_prefix . '.ADD', 0, $perm_category) ||
$this->Application->CheckPermission($perm_prefix . '.MODIFY', 0, $perm_category);
break;
+
+ default:
+ $res = false;
+ break;
}
- if (!$res) {
+ if ( !$res ) {
$event->status = kEvent::erPERM_FAIL;
}
return $res;
}
return parent::CheckPermission($event);
}
/**
* Returns primary category of review's main item
*
* @param kEvent $event
* @return int
*/
function _getReviewCategory(&$event)
{
$items_info = $this->Application->GetVar($event->getPrefixSpecial());
if ($items_info) {
// rev:PresetFormFields is used to initialize new review creation
list ($review_id, ) = each($items_info);
}
else {
// when adding new review in admin
$review_id = false;
}
if (!$review_id) {
return false;
}
$object =& $event->getObject();
/* @var $object kDBItem */
// 1. get main item resource id (use object, because of temp tables in admin)
$sql = 'SELECT ItemId
FROM ' . $object->TableName . '
WHERE ' . $object->IDField . ' = ' . $review_id;
$resource_id = $this->Conn->GetOne($sql);
// 2. set main item id (for permission checks)
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$sql = 'SELECT ' . $this->Application->getUnitOption($parent_prefix, 'IDField') .'
FROM ' . $this->Application->getUnitOption($parent_prefix, 'TableName') .'
WHERE ResourceId = ' . $resource_id;
$this->Application->SetVar($parent_prefix . '_id', $this->Conn->GetOne($sql));
// 3. get main item category
$sql = 'SELECT CategoryId
FROM ' . $this->Application->getUnitOption('ci', 'TableName') .'
WHERE ItemResourceId = ' . $resource_id .' AND PrimaryCat = 1';
return $this->Conn->GetOne($sql);
}
/**
* Returns prefix for permissions
*
* @param kEvent $event
*/
function getPermPrefix(&$event)
{
$main_prefix = $this->Application->GetTopmostPrefix($event->Prefix, true);
// this will return LINK for l, ARTICLE for n, TOPIC for bb, PRODUCT for p
return $this->Application->getUnitOption($main_prefix, 'PermItemPrefix');
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
* @access protected
* @see OnListBuild
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
/* @var $object kDBList */
- if (!$this->Application->isAdminUser) {
- $object->addFilter('active', '%1$s.Status = '.STATUS_ACTIVE);
+ if ( !$this->Application->isAdminUser ) {
+ $object->addFilter('active', '%1$s.Status = ' . STATUS_ACTIVE);
}
- switch ($event->Special)
- {
+ switch ( $event->Special ) {
case 'showall':
$object->clearFilters();
break;
case 'item': // used ?
$object->clearFilters();
- $info = $object->getLinkedInfo();
- $this->Application->setUnitOption($info['ParentPrefix'], 'AutoLoad', true);
- $parent =& $this->Application->recallObject($info['ParentPrefix']);
- $object->addFilter('item_reviews', '%1$s.ItemId = '.$parent->GetDBField('ResourceId'));
+ $parent_info = $object->getLinkedInfo();
+
+ $parent =& $this->Application->recallObject($parent_info['ParentPrefix']);
+ /* @var $parent kDBItem */
+
+ $object->addFilter('item_reviews', '%1$s.ItemId = ' . $parent->GetDBField('ResourceId'));
break;
- case 'products': // used in In-Portal (Sturcture & Data -> Reviews section)
- $object->removeFilter('parent_filter'); // this is important
+ case 'products': // used in In-Portal (Structure & Data -> Reviews section)
+ $object->removeFilter('parent_filter'); // this is important
$object->addFilter('product_reviews', 'pr.ResourceId IS NOT NULL');
break;
-
- /*case 'product':
- $object->addFilter('product_reviews', '%1$s.ItemId = pr.ResourceId'); // for LEFT JOIN
- break;*/
}
- if (preg_match('/(.*)-rev/', $event->Prefix, $regs)) {
+ if ( preg_match('/(.*)-rev/', $event->Prefix, $regs) ) {
// "Structure & Data" -> "Reviews" (section in K4)
$item_type = $this->Application->getUnitOption($regs[1], 'ItemType');
$object->addFilter('itemtype_filter', '%1$s.ItemType = ' . $item_type);
- if ($this->Application->isAdmin) {
+ if ( $this->Application->isAdmin ) {
// temporarily solution so we can see sub-items on separate grid in Admin
$object->removeFilter('parent_filter');
- }
+ }
}
- if ($event->getEventParam('type') == 'current_user') {
-// $object->removeFilter('active');
- $object->addFilter('current_user', '%1$s.CreatedById = '.$this->Application->RecallVar('user_id'));
- $object->addFilter('current_ip', '%1$s.IPAddress = "'.$_SERVER['REMOTE_ADDR'].'"');
+ if ( $event->getEventParam('type') == 'current_user' ) {
+ $object->addFilter('current_user', '%1$s.CreatedById = ' . $this->Application->RecallVar('user_id'));
+ $object->addFilter('current_ip', '%1$s.IPAddress = "' . $_SERVER['REMOTE_ADDR'] . '"');
}
}
/**
* Adds review from front in case if user is logged in
*
* @param kEvent $event
*/
function OnAddReview(&$event)
{
$event->CallSubEvent('OnCreate');
}
/**
* Get new review status on user review permission
*
* @param kEvent $event
* @return int
*/
function getReviewStatus(&$event)
{
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$main_object =& $this->Application->recallObject($parent_prefix);
/* @var $main_object kCatDBItem */
$ret = STATUS_DISABLED;
$perm_name = $this->getPermPrefix($event).'.REVIEW';
if ($this->Application->CheckPermission($perm_name, 0, $main_object->GetDBField('CategoryId'))) {
$ret = STATUS_ACTIVE;
}
else if ($this->Application->CheckPermission($perm_name.'.PENDING', 0, $main_object->GetDBField('CategoryId'))) {
$ret = STATUS_PENDING;
}
return $ret;
}
/**
* Prefills all fields on front-end
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
+ parent::OnBeforeItemCreate($event);
+
$object =& $event->getObject();
/* @var $object kDBItem */
$parent_info = $object->getLinkedInfo();
$item_type = $this->Application->getUnitOption($parent_info['ParentPrefix'], 'ItemType');
$object->SetDBField('IPAddress', $_SERVER['REMOTE_ADDR']);
$object->SetDBField('ItemType', $item_type);
$object->SetDBField('Module', $this->Application->findModule('Var', $parent_info['ParentPrefix'], 'Name'));
- if ($this->Application->isAdminUser) {
+ if ( $this->Application->isAdminUser ) {
// don't perform spam control on admin
return ;
}
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$spam_helper->InitHelper($parent_info['ParentId'], 'Review', 0);
- if ($spam_helper->InSpamControl()) {
+ if ( $spam_helper->InSpamControl() ) {
$event->status = kEvent::erFAIL;
$object->SetError('ReviewText', 'too_frequent', 'lu_ferror_review_duplicate');
- return ;
+ return;
}
$rating = $object->GetDBField('Rating');
- if ($rating < 1 || $rating > 5) {
+ if ( $rating < 1 || $rating > 5 ) {
$object->SetDBField('Rating', null);
}
$object->SetDBField('ItemId', $parent_info['ParentId']); // ResourceId
$object->SetDBField('CreatedById', $this->Application->RecallVar('user_id'));
$object->SetDBField('Status', $this->getReviewStatus($event));
$object->SetDBField('TextFormat', 0); // set plain text format directly
}
/**
* Sets correct rating value
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
+ parent::OnBeforeItemUpdate($event);
+
$object =& $event->getObject();
+ /* @var $object kDBItem */
$rating = $object->GetDBField('Rating');
- if (!$rating) {
+
+ if ( !$rating ) {
$object->SetDBField('Rating', null);
}
}
/**
* Updates item review counter
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemCreate(&$event)
+ protected function OnAfterItemCreate(&$event)
{
+ parent::OnAfterItemCreate($event);
+
$this->updateSubitemCounters($event);
- if (!$this->Application->isAdminUser) {
+ if ( !$this->Application->isAdminUser ) {
$spam_helper =& $this->Application->recallObject('SpamHelper');
/* @var $spam_helper SpamHelper */
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$parent_info = $object->getLinkedInfo();
$config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping');
- $review_settings = $config_mapping['ReviewDelayValue'].':'.$config_mapping['ReviewDelayInterval'];
+ $review_settings = $config_mapping['ReviewDelayValue'] . ':' . $config_mapping['ReviewDelayInterval'];
$spam_helper->InitHelper($parent_info['ParentId'], 'Review', $review_settings);
$spam_helper->AddToSpamControl();
$review_status = $object->GetDBField('Status');
- if ($review_status == STATUS_ACTIVE || $review_status == STATUS_PENDING) {
+ if ( $review_status == STATUS_ACTIVE || $review_status == STATUS_PENDING ) {
$email_event = $this->getPermPrefix($event) . '.REVIEW.' . ($review_status == STATUS_ACTIVE ? 'ADD' : 'ADD.PENDING');
$this->Application->EmailEventUser($email_event, $object->GetDBField('CreatedById'));
$this->Application->EmailEventAdmin($email_event);
}
}
}
/**
* Updates item review counter
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
parent::OnAfterItemUpdate($event);
$this->updateSubitemCounters($event);
$object =& $event->getObject();
/* @var $object kDBItem */
if ($this->Application->isAdminUser && !$object->IsTempTable()) {
// send email on review status change from reviews grid in admin
$review_status = $object->GetDBField('Status');
$process_status = Array (STATUS_ACTIVE, STATUS_DISABLED);
if (($review_status != $object->GetOriginalField('Status')) && in_array($review_status, $process_status)) {
$this->_loadMainObject($event);
$email_event = $this->getPermPrefix($event) . '.REVIEW.' . ($review_status == STATUS_ACTIVE ? 'APPROVE' : 'DENY');
$this->Application->EmailEventUser($email_event, $object->GetDBField('CreatedById'));
}
}
}
/**
* Loads main object of review (link, article, etc.)
*
* @param kEvent $event
* @return kCatDBItem
*/
function _loadMainObject(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$parent_table_key = $this->Application->getUnitOption($event->Prefix, 'ParentTableKey');
$foreign_key = $this->Application->getUnitOption($event->Prefix, 'ForeignKey');
$main_object =& $this->Application->recallObject($parent_prefix, null, Array ('skip_autoload' => true));
/* @var $main_object kDBItem */
$main_object->Load($object->GetDBField($foreign_key), $parent_table_key);
}
/**
* Updates total review counter, cached rating, votes count
*
* @param kEvent $event
*/
function updateSubitemCounters(&$event)
{
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$main_object =& $this->Application->recallObject($parent_prefix, null, Array ('raise_warnings' => 0));
/* @var $main_object kCatDBItem */
if (!$main_object->isLoaded()) {
// deleting main item / cloning main item
return ;
}
$object =& $event->getObject(); // for temp tables
/* @var $object kDBItem */
// 1. update review counter
$sql = 'SELECT COUNT(ReviewId)
FROM '.$object->TableName.'
WHERE ItemId = '.$main_object->GetDBField('ResourceId');
$review_count = $this->Conn->GetOne($sql);
$main_object->SetDBField('CachedReviewsQty', $review_count);
// 2. update votes counter + rating
$rating = $object->GetDBField('Rating');
$avg_rating = $main_object->GetDBField('CachedRating');
$votes_count = $main_object->GetDBField('CachedVotesQty');
switch ($event->Name) {
case 'OnAfterItemCreate': // adding new review with rating
$this->changeRating($avg_rating, $votes_count, $rating, '+');
break;
case 'OnAfterItemDelete':
$this->changeRating($avg_rating, $votes_count, $rating, '-');
break;
case 'OnAfterItemUpdate':
$this->changeRating($avg_rating, $votes_count, $object->GetOriginalField('Rating'), '-');
$this->changeRating($avg_rating, $votes_count, $rating, '+');
break;
}
$main_object->SetDBField('CachedRating', "$avg_rating"); // covert $avg_rating value to string
$main_object->SetDBField('CachedVotesQty', $votes_count);
$main_object->Update();
}
/**
* Changes average rating and votes count based on requested operation
*
* @param float $avg_rating average rating before new vote
* @param int $votes_count votes count before new vote
* @param int $rating new vote (from 1 to 5)
* @param string $operation requested operation (+ / -)
*/
function changeRating(&$avg_rating, &$votes_count, $rating, $operation)
{
if ($rating < 1 || $rating > 5) {
return ;
}
if ($operation == '+') {
$avg_rating = (($avg_rating * $votes_count) + $rating) / ($votes_count + 1);
++$votes_count;
}
else {
$avg_rating = (($avg_rating * $votes_count) - $rating) / ($votes_count - 1);
--$votes_count;
}
}
/**
* Updates main item cached review counter
*
* @param kEvent $event
*/
function OnAfterItemDelete(&$event)
{
$this->updateSubitemCounters($event);
}
/**
* Creates review & redirect to confirmation template
*
* @param kEvent $event
*/
function OnCreate(&$event)
{
parent::OnCreate($event);
if ($event->status != kEvent::erSUCCESS || $this->Application->isAdmin) {
return ;
}
$object =& $event->getObject();
$next_template = $object->GetDBField('Status') == STATUS_ACTIVE ? 'success_template' : 'success_pending_template';
$event->redirect = $this->Application->GetVar($next_template);
$event->SetRedirectParam('opener', 's');
$parent_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$event->SetRedirectParam('pass', 'm,'.$parent_prefix);
}
/**
* Makes left join to item's table, when in separate grid
*
* @param kEvent $event
*/
function OnAfterConfigRead(&$event)
{
parent::OnAfterConfigRead($event);
if (preg_match('/(.*)-rev/', $event->Prefix, $regs) && $this->Application->prefixRegistred($regs[1])) {
// "Structure & Data" -> "Reviews" (section in K4)
// 1. add join to items table (for "Structure & Data" -> "Reviews" section)
$item_table = $this->Application->getUnitOption($regs[1], 'TableName');
$ci_table = $this->Application->getUnitOption('ci', 'TableName');
$list_sqls = $this->Application->getUnitOption($event->Prefix, 'ListSQLs');
$list_sqls[''] .= ' LEFT JOIN '.$item_table.' item_table ON item_table.ResourceId = %1$s.ItemId';
$list_sqls[''] .= ' LEFT JOIN '.$ci_table.' ci ON item_table.ResourceId = ci.ItemResourceId AND ci.PrimaryCat = 1';
$this->Application->setUnitOption($event->Prefix, 'ListSQLs', $list_sqls);
// 2. add calculated field
$calculated_fields = $this->Application->getUnitOption($event->Prefix, 'CalculatedFields');
$calculated_fields['']['CatalogItemName'] = 'item_table.' . $this->getTitleField($regs[1]);
$calculated_fields['']['CatalogItemId'] = 'item_table.' . $this->Application->getUnitOption($regs[1], 'IDField');
$calculated_fields['']['CatalogItemCategory'] = 'ci.CategoryId';
$this->Application->setUnitOption($event->Prefix, 'CalculatedFields', $calculated_fields);
}
}
/**
* Convert TitleField field of kMultiLanguage formatter used for it
*
* @param string $prefix
* @return string
*/
function getTitleField($prefix)
{
$lang_prefix = 'l'.$this->Application->GetVar('m_lang').'_';
$title_field = $this->Application->getUnitOption($prefix, 'TitleField');
$field_options = $this->Application->getUnitOption($prefix.'.'.$title_field, 'Fields');
$formatter_class = isset($field_options['formatter']) ? $field_options['formatter'] : '';
if ($formatter_class == 'kMultiLanguage' && !isset($field_options['master_field'])) {
$title_field = $lang_prefix.$title_field;
}
return $title_field;
}
/**
* Set's new perpage for Category Item Reviews (used on Front-end)
*
* @param kEvent $event
*/
function OnSetPerPage(&$event)
{
parent::OnSetPerPage($event);
$parent_prefix = $event->Application->getUnitOption($event->Prefix, 'ParentPrefix');
$event->SetRedirectParam('pass', 'm,' . $event->getPrefixSpecial() . ',' . $parent_prefix);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/forms/drafts/draft_eh.php
===================================================================
--- branches/5.2.x/core/units/forms/drafts/draft_eh.php (revision 14627)
+++ branches/5.2.x/core/units/forms/drafts/draft_eh.php (revision 14628)
@@ -1,57 +1,58 @@
<?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 DraftEventHandler extends kDBEventHandler
{
-
/**
* Sets user, who created draft
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
parent::OnBeforeItemCreate($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$user_id = $this->Application->RecallVar('user_id');
$object->SetDBField('CreatedById', $user_id);
}
/**
* Allows to load draft, that best matches given form submission
*
* @param kEvent $event
* @return int
*/
function getPassedID(&$event)
{
if ($event->Special == 'related') {
$form_submission =& $this->Application->recallObject('formsubs');
/* @var $form_submission kDBItem */
return Array (
'FormSubmissionId' => $form_submission->GetID(),
'CreatedById' => $this->Application->RecallVar('user_id'),
);
}
return parent::getPassedID($event);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/forms/forms/forms_tp.php
===================================================================
--- branches/5.2.x/core/units/forms/forms/forms_tp.php (revision 14627)
+++ branches/5.2.x/core/units/forms/forms/forms_tp.php (revision 14628)
@@ -1,36 +1,41 @@
<?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 FormsTagProcessor extends kDBTagProcessor{
function CheckBox($params)
{
- $field = $this->SelectParam($params, 'name,field');
- $object =& $this->Application->recallObject($this->getPrefixSpecial(),$this->Prefix, $params);
+ $object =& $this->getObject($params);
+ /* @var $object kDBItem */
- $value = $object->GetDBField($field);
+ $value = $object->GetDBField($this->SelectParam($params, 'name,field'));
- if ($value) return 'checked';
- if (is_null($value)) return $params['default'];
+ if ( $value ) {
+ return 'checked';
+ }
+
+ if ( is_null($value) ) {
+ return $params['default'];
+ }
return '';
}
function MaxUploadSize($params)
{
- return round(MAX_UPLOAD_SIZE/1024/1024).' Mb';
+ return round(MAX_UPLOAD_SIZE / 1024 / 1024) . ' Mb';
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/forms/form_fields/form_field_eh.php
===================================================================
--- branches/5.2.x/core/units/forms/form_fields/form_field_eh.php (revision 14627)
+++ branches/5.2.x/core/units/forms/form_fields/form_field_eh.php (revision 14628)
@@ -1,49 +1,52 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2010 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 FormFieldEventHandler extends kDBEventHandler {
/**
* Returns form field visibility filter
*
* @return string
*/
function getVisiblilityFilter()
{
if ($this->Application->LoggedIn() && !$this->Application->isAdminUser) {
return '%1$s.Visibility = ' . SubmissionFormField::VISIBILITY_EVERYONE;
}
return '';
}
/**
* Shows fields based on user logged-in status
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
parent::SetCustomQuery($event);
$object =& $event->getObject();
/* @var $object kDBList */
$visibility_filter = $this->getVisiblilityFilter();
$object->addFilter('visibility_filter', $visibility_filter);
}
}
Index: branches/5.2.x/core/units/forms/submission_log/submission_log_eh.php
===================================================================
--- branches/5.2.x/core/units/forms/submission_log/submission_log_eh.php (revision 14627)
+++ branches/5.2.x/core/units/forms/submission_log/submission_log_eh.php (revision 14628)
@@ -1,691 +1,699 @@
<?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 SubmissionLogEventHandler extends kDBEventHandler {
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnResendReply' => Array ('subitem' => 'add|edit'),
'OnSaveDraft' => Array ('subitem' => 'add|edit'),
'OnUseDraft' => Array ('subitem' => 'add|edit'),
'OnDeleteDraft' => Array ('subitem' => 'add|edit'),
'OnProcessBounceMail' => Array ('subitem' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
- * Checks event permissions
+ * Checks user permission to execute given $event
*
* @param kEvent $event
* @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
$section = $event->getSection();
$form_id = $this->Application->GetVar('form_id');
if ($form_id) {
// copy form_id to env to be passed info upload links
$this->Application->SetVar($event->getPrefixSpecial() . '_form_id', $form_id);
}
else {
$form_id = $this->Application->GetVar($event->getPrefixSpecial() . '_form_id');
}
$event->setEventParam('PermSection', $section . ':' . $form_id);
return parent::CheckPermission($event);
}
/**
* Prepares new kDBItem object
*
* @param kEvent $event
* @access protected
*/
function OnNew(&$event)
{
parent::OnNew($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$form_submission =& $this->Application->recallObject('formsubs');
/* @var $form_submission kDBItem */
$form_submission_helper =& $this->Application->recallObject('FormSubmissionHelper');
/* @var $form_submission_helper FormSubmissionHelper */
$form =& $form_submission_helper->getForm($form_submission);
-
+ /* @var $form kDBItem */
+
$from_email = $form->GetDBField('ReplyFromEmail');
$to_email = $form_submission_helper->getFieldByRole($form_submission, SubmissionFormField::COMMUNICATION_ROLE_EMAIL);
if ($this->Application->GetVar('client_mode')) {
// debug code for sending email from client
$object->SetDBField('FromEmail', $to_email);
$object->SetDBField('ToEmail', $from_email);
}
else {
$object->SetDBField('FromEmail', $from_email);
$object->SetDBField('ToEmail', $to_email);
}
$object->SetDBField('Cc', $form->GetDBField('ReplyCc'));
$object->SetDBField('Bcc', $form->GetDBField('ReplyBcc'));
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
$org_message =& $this->Application->recallObject($event->Prefix . '.-item', null, Array ('skip_autoload' => true));
/* @var $org_message kDBItem */
$org_message->Load( array_shift($ids) );
// client could reply from different email, so compare to admin email!
if ($org_message->GetDBField('ToEmail') == $from_email) {
// can reply only to client email, not own :)
// transform subject
$message_subject = $org_message->GetDBField('Subject');
if ($message_subject) {
$object->SetDBField('Subject', $this->_transformSubject($message_subject, 'Re'));
}
// add signature
$message_body = $form->GetDBField('ReplyMessageSignature');
if ($org_message->GetDBField('Message')) {
// add replied marks
$message_body .= '> ' . preg_replace('/([\r]*\n)/', '\\1> ', $org_message->GetDBField('Message'));
}
$object->SetDBField('ToEmail', $org_message->GetDBField('FromEmail')); // user client's email from reply
$object->SetDBField('Message', $message_body);
$object->SetDBField('ReplyTo', $org_message->GetID());
}
}
else {
$sql = 'SELECT COUNT(*)
FROM ' . $object->TableName . '
WHERE FormSubmissionId = ' . $form_submission->GetID();
$replies_found = $this->Conn->GetOne($sql);
if (!$replies_found) {
// 1st message from admin -> quote subject & text from feedback
$message_subject = $form_submission_helper->getFieldByRole($form_submission, SubmissionFormField::COMMUNICATION_ROLE_SUBJECT, true);
if ($message_subject) {
$object->SetDBField('Subject', $this->_transformSubject($message_subject, 'Re'));
}
// add signature
$message_body = $form->GetDBField('ReplyMessageSignature');
// add replied marks
$original_message_body = $form_submission_helper->getFieldByRole($form_submission, SubmissionFormField::COMMUNICATION_ROLE_BODY);
if ($original_message_body) {
$message_body .= '> ' . preg_replace('/([\r]*\n)/', '\\1> ', $original_message_body);
}
$object->SetDBField('Message', $message_body);
}
}
$this->clearSelectedIDs($event);
}
/**
* Parses $search string in subject and reformats it
* Used for replying and forwarding
*
* @param string $subject
* @param string $search
* @return string
*/
function _transformSubject($subject, $search = 'Re')
{
$regex = '/'.$search.'(\[([\d]+)\]){0,1}:/i';
preg_match_all($regex, $subject, $regs);
if ($regs[2]) {
$reply_count = 0; // reply count without numbers (equals to "re[1]")
$max_reply_number = 0; // maximal reply number
sort($regs[2], SORT_NUMERIC); // sort ascending (non-numeric replies first)
foreach ($regs[2] as $match) {
if (!$match) {
// found "re:"
$reply_count++;
}
elseif ($match > $max_reply) {
// found "re:[number]"
$max_reply_number = $match;
}
}
return $search.'['.($reply_count + $max_reply_number + 1).']: '.trim(preg_replace($regex, '', $subject));
}
return $search.': '.$subject;
}
/**
* Resends reply, that was not sent last time
*
* @param kEvent $event
*/
function OnResendReply(&$event)
{
$ids = $this->StoreSelectedIDs($event);
if (!$ids) {
return ;
}
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
$sql = 'SELECT f.ReplyFromEmail, sl.' . $object->IDField . '
FROM ' . $object->TableName . ' sl
JOIN ' . $this->Application->getUnitOption('formsubs', 'TableName') . ' fs ON fs.FormSubmissionId = sl.FormSubmissionId
JOIN ' . $this->Application->getUnitOption('form', 'TableName') . ' f ON f.FormId = fs.FormId
WHERE sl.' . $object->IDField . ' IN (' . implode(',', $ids) . ')';
$reply_emails = $this->Conn->GetCol($sql, $object->IDField);
foreach ($ids as $id) {
$object->Load($id);
// allow to send messages, that were successfully sended before :(
if (($object->GetDBField('ToEmail') != $reply_emails[$id]) && ($object->GetDBField('SentStatus') != SUBMISSION_LOG_SENT)) {
$object->SetOriginalField('SentStatus', 0); // reset sent status to update sent date automatically
$this->_sendEmail($object); // resend email here
}
}
$this->clearSelectedIDs($event);
if (!$this->Application->GetVar('from_list')) {
$event->SetRedirectParam('opener', 'u');
}
}
/**
* Updates last operation dates for log record
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
parent::OnBeforeItemCreate($event);
$this->_validateRecipients($event);
$this->_updateStatusDates($event);
}
/**
* Updates last operation dates for log record
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
parent::OnBeforeItemUpdate($event);
$this->_validateRecipients($event);
$this->_updateStatusDates($event);
}
/**
* Validates email recipients
*
* @param kEvent $event
*/
function _validateRecipients(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
$cc = $object->GetDBField('Cc');
if ($cc && ($esender->GetRecipients($cc) === false)) {
$object->SetError('Cc', 'invalid_format');
}
$bcc = $object->GetDBField('Bcc');
if ($bcc && ($esender->GetRecipients($bcc) === false)) {
$object->SetError('Bcc', 'invalid_format');
}
}
/**
* Generates verification code and sets it inside sent message
*
* @param kDBItem $object
* @return string
*/
function _generateVerificationCode(&$object)
{
$code = Array (
$object->GetDBField('FromEmail'),
$object->GetDBField('ToEmail'),
$object->GetID(),
microtime(true)
);
$object->SetDBField('VerifyCode', md5( implode('-', $code) ));
}
/**
* Sends email based on fields from given submission-log record
*
* @param kDBItem $object
*/
function _sendEmail(&$object)
{
if ($this->Application->GetVar('client_mode')) {
return ;
}
if (!$object->GetDBField('VerifyCode')) {
$this->_generateVerificationCode($object);
}
$form_submission =& $this->_getFormSubmission($object);
$form_submission_helper =& $this->Application->recallObject('FormSubmissionHelper');
/* @var $form_submission_helper FormSubmissionHelper */
$form =& $form_submission_helper->getForm($form_submission);
$send_params = Array (
'from_name' => $form->GetDBField('ReplyFromName'),
'from_email' => $object->GetDBField('FromEmail'),
'to_email' => $object->GetDBField('ToEmail'),
'subject' => $object->GetDBField('Subject'),
'message' => $object->GetDBField('Message'),
);
$to_name = $form_submission_helper->getFieldByRole($form_submission, SubmissionFormField::COMMUNICATION_ROLE_NAME);
if ($to_name) {
$send_params['to_name'] = $to_name;
}
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
$esender->SetReturnPath( $form->GetDBField('BounceEmail') );
if ($object->GetDBField('Cc')) {
$recipients = $esender->GetRecipients( $object->GetDBField('Cc') );
foreach ($recipients as $recipient_info) {
$esender->AddCc($recipient_info['Email'], $recipient_info['Name']);
}
}
if ($object->GetDBField('Bcc')) {
$recipients = $esender->GetRecipients( $object->GetDBField('Bcc') );
foreach ($recipients as $recipient_info) {
$esender->AddBcc($recipient_info['Email'], $recipient_info['Name']);
}
}
if ($object->GetDBField('Attachment')) {
$attachments = explode('|', $object->GetField('Attachment', 'file_paths'));
foreach ($attachments as $attachment) {
$esender->AddAttachment($attachment);
}
}
$this->Application->EmailEventAdmin('FORM.SUBMISSION.REPLY.TO.USER', null, $send_params);
// mark as sent after sending is finished
$object->SetDBField('SentStatus', SUBMISSION_LOG_SENT);
// reset bounce status before (re-)sending
$object->SetDBField('BounceInfo', NULL);
$object->SetDBField('BounceDate_date', NULL);
$object->SetDBField('BounceDate_time', NULL);
if ($object->GetDBField('DraftId')) {
$temp_handler =& $this->Application->recallObject('draft_TempHandler', 'kTempTablesHandler');
/* @var $temp_handler kTempTablesHandler */
$temp_handler->DeleteItems('draft', '', Array ($object->GetDBField('DraftId')));
$object->SetDBField('DraftId', 0);
}
$object->Update();
}
/**
* Sends new email after log record was created
* Updates last update time for submission
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemCreate(&$event)
+ protected function OnAfterItemCreate(&$event)
{
parent::OnAfterItemCreate($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$this->_sendEmail($object); // send email
$this->_updateSubmission($event);
$reply_to = $object->GetDBField('ReplyTo');
- if (!$reply_to) {
+ if ( !$reply_to ) {
$reply_to = $this->_getLastMessageId($event, !$this->Application->GetVar('client_mode'));
}
- if ($reply_to) {
+ if ( $reply_to ) {
// this is reply to other message -> mark it as replied
$org_message =& $this->Application->recallObject($event->Prefix . '.-item', null, Array ('skip_autoload' => true));
/* @var $org_message kDBItem */
$org_message->Load($reply_to);
$org_message->SetDBField('ReplyStatus', SUBMISSION_LOG_REPLIED);
$org_message->Update();
}
- if ($this->Application->GetVar('client_mode')) {
+ if ( $this->Application->GetVar('client_mode') ) {
// new reply from client received -> send notification about it
$this->Application->EmailEventAdmin('FORM.SUBMISSION.REPLY.FROM.USER');
}
}
/**
* Returns last message id (client OR admin)
*
* @param kEvent $event
* @param bool $from_client
* @return int
*/
function _getLastMessageId(&$event, $from_client = false)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$form_submission =& $this->_getFormSubmission($object);
$form_submission_helper =& $this->Application->recallObject('FormSubmissionHelper');
/* @var $form_submission_helper FormSubmissionHelper */
$form =& $form_submission_helper->getForm($form_submission);
$reply_email = $form->GetDBField('ReplyFromEmail');
$sql = 'SELECT MAX(' . $object->IDField . ')
FROM ' . $object->TableName . '
WHERE (FormSubmissionId = ' . $form_submission->GetID() . ') AND (ToEmail' . ($from_client ? ' = ' : ' <> ') . $this->Conn->qstr($reply_email) . ')';
return $this->Conn->GetOne($sql);
}
/**
* Updates last update time for submission
*
* @param kEvent $event
*/
function OnAfterItemUpdate(&$event)
{
parent::OnAfterItemUpdate($event);
$this->_updateSubmission($event);
$object =& $event->getObject();
/* @var $object kDBItem */
// send out email event to admin for bouncing
if ( $object->GetOriginalField('SentStatus') != $object->GetDBField('SentStatus')
&& $object->GetDBField('SentStatus') == SUBMISSION_LOG_BOUNCE ) {
$this->Application->EmailEventAdmin('FORM.SUBMISSION.REPLY.FROM.USER.BOUNCED');
}
}
/**
* Sets last sent/reply dates based on field changes in log record
*
* @param kEvent $event
*/
function _updateStatusDates(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$now = adodb_mktime();
$sent_status = $object->GetDBField('SentStatus');
if (($event->Special != 'merge') && ($sent_status == SUBMISSION_LOG_SENT) && ($sent_status != $object->GetOriginalField('SentStatus'))) {
// sent status was set
$object->SetDBField('SentOn_date', $now);
$object->SetDBField('SentOn_time', $now);
}
$reply_status = $object->GetDBField('ReplyStatus');
if (($reply_status == SUBMISSION_LOG_REPLIED) && ($reply_status != $object->GetOriginalField('ReplyStatus'))) {
// sent status was set
$object->SetDBField('RepliedOn_date', $now);
$object->SetDBField('RepliedOn_time', $now);
}
}
/**
* Returns form submission by given event of submission log
*
* @param kDBItem $object
* @return kDBItem
*/
function &_getFormSubmission(&$object)
{
$submission_id = $object->GetDBField('FormSubmissionId');
$form_submission =& $this->Application->recallObject('formsubs.-item', null, Array ('skip_autoload' => true));
/* @var $form_submission kDBItem */
if ($form_submission->isLoaded() && ($form_submission->GetID() == $submission_id)) {
// already loaded AND has needed id
return $form_submission;
}
$form_submission->Load($submission_id);
return $form_submission;
}
/**
* Sets last updated field for form submission
*
* @param kEvent $event
*/
function _updateSubmission(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$form_submission =& $this->_getFormSubmission($object);
// 1. set last updated
$last_updated = max ($object->GetDBField('SentOn'), $object->GetDBField('RepliedOn'));
if ($form_submission->GetDBField('LastUpdatedOn') < $last_updated) {
// don't set smaller last update, that currenly set
$form_submission->SetDBField('LastUpdatedOn_date', $last_updated);
$form_submission->SetDBField('LastUpdatedOn_time', $last_updated);
}
// 2. update submission status
$form_submission_helper =& $this->Application->recallObject('FormSubmissionHelper');
/* @var $form_submission_helper FormSubmissionHelper */
$form =& $form_submission_helper->getForm($form_submission);
$client_responce = $form->GetDBField('ReplyFromEmail') == $object->GetDBField('ToEmail');
$replied = $object->GetDBField('ReplyStatus') == SUBMISSION_LOG_REPLIED;
if (!$client_responce && !$replied) {
// admin sends new email to client
$form_submission->SetDBField('LogStatus', SUBMISSION_REPLIED);
}
elseif ($client_responce) {
// client email becomes replied OR receiving new unreplied email from client
$form_submission->SetDBField('LogStatus', $replied ? SUBMISSION_REPLIED : SUBMISSION_NEW_EMAIL);
}
if ($object->GetDBField('SentStatus') == SUBMISSION_LOG_BOUNCE) {
// propagate bounce status from reply
$form_submission->SetDBField('LogStatus', SUBMISSION_BOUNCE);
}
$form_submission->Update();
}
/**
* Saves current unsent message as draft
*
* @param kEvent $event
*/
function OnSaveDraft(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
$draft =& $this->Application->recallObject('draft', null, Array('skip_autoload' => true));
/* @var $draft kDBItem */
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if ($items_info) {
foreach ($items_info as $id => $field_values) {
$object->setID($id);
$object->SetFieldsFromHash($field_values);
$load_keys = Array (
'FormSubmissionId' => $object->GetDBField('FormSubmissionId'),
'CreatedById' => $this->Application->RecallVar('user_id'),
);
// get existing draft for given submission and user
$draft->Load($load_keys);
$draft->SetDBField('Message', $object->GetDBField('Message'));
if ($draft->isLoaded()) {
$draft->Update();
}
else {
$draft->SetDBFieldsFromHash($load_keys);
$draft->Create();
}
}
}
$this->Application->SetVar($event->getPrefixSpecial() . '_SaveEvent', 'OnCreate');
$event->SetRedirectParam('opener', 'u');
}
/**
* Uses found draft instead of submission reply body
*
* @param kEvent $event
*/
function OnUseDraft(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
$draft =& $this->Application->recallObject('draft', null, Array('skip_autoload' => true));
/* @var $draft kDBItem */
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if ($items_info) {
foreach ($items_info as $id => $field_values) {
$object->setID($id);
$object->SetFieldsFromHash($field_values);
$load_keys = Array (
'FormSubmissionId' => $object->GetDBField('FormSubmissionId'),
'CreatedById' => $this->Application->RecallVar('user_id'),
);
// get existing draft for given submission and user
$draft->Load($load_keys);
if ($draft->isLoaded()) {
$object->SetDBField('Message', $draft->GetDBField('Message'));
$object->SetDBField('DraftId', $draft->GetID());
}
}
}
$this->Application->SetVar($event->getPrefixSpecial() . '_SaveEvent', 'OnCreate');
$event->redirect = false;
}
/**
* Deletes draft, that matches given user and form submission
*
* @param kEvent $event
*/
function OnDeleteDraft(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
$draft =& $this->Application->recallObject('draft', null, Array('skip_autoload' => true));
/* @var $draft kDBItem */
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if ($items_info) {
foreach ($items_info as $id => $field_values) {
$object->setID($id);
$object->SetFieldsFromHash($field_values);
$object->SetDBField('DraftId', 0);
$load_keys = Array (
'FormSubmissionId' => $object->GetDBField('FormSubmissionId'),
'CreatedById' => $this->Application->RecallVar('user_id'),
);
// get existing draft for given submission and user
$draft->Load($load_keys);
if ($draft->isLoaded()) {
$temp_handler =& $this->Application->recallObject('draft_TempHandler', 'kTempTablesHandler');
/* @var $temp_handler kTempTablesHandler */
$temp_handler->DeleteItems('draft', '', Array ($draft->GetID()));
}
}
}
$this->Application->SetVar($event->getPrefixSpecial() . '_SaveEvent', 'OnCreate');
$event->redirect = false;
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/admin/admin_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/admin/admin_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/admin/admin_tag_processor.php (revision 14628)
@@ -1,1136 +1,1157 @@
<?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 AdminTagProcessor extends kDBTagProcessor {
/**
* Allows to execute js script after the page is fully loaded
*
* @param Array $params
* @return string
*/
function AfterScript($params)
{
$after_script = $this->Application->GetVar('after_script');
if ($after_script) {
return '<script type="text/javascript">'.$after_script.'</script>';
}
return '';
}
/**
* Returns section title with #section# keyword replaced with current section
*
* @param Array $params
* @return string
*/
function GetSectionTitle($params)
{
if (array_key_exists('default', $params)) {
return $params['default'];
}
return $this->Application->Phrase( kUtil::replaceModuleSection($params['phrase']) );
}
/**
* Returns section icon with #section# keyword replaced with current section
*
* @param Array $params
* @return string
*/
function GetSectionIcon($params)
{
return kUtil::replaceModuleSection($params['icon']);
}
/**
* Returns version of module by name
*
* @param Array $params
* @return string
*/
function ModuleVersion($params)
{
return $this->Application->findModule('Name', $params['module'], 'Version');
}
/**
* Used in table form section drawing
*
* @param Array $params
* @return string
*/
function DrawTree($params)
{
static $deep_level = 0;
// when processings, then sort children by priority (key of children array)
$ret = '';
$section_name = $params['section_name'];
$params['name'] = $this->SelectParam($params, 'name,render_as,block');
$sections_helper =& $this->Application->recallObject('SectionsHelper');
/* @var $sections_helper kSectionsHelper */
$section_data =& $sections_helper->getSectionData($section_name);
$params['children_count'] = isset($section_data['children']) ? count($section_data['children']) : 0;
$params['deep_level'] = $deep_level++;
$template = $section_data['url']['t'];
unset($section_data['url']['t']);
$section_data['section_url'] = $this->Application->HREF($template, '', $section_data['url']);
$ret .= $this->Application->ParseBlock( array_merge($params, $section_data) );
if (!isset($section_data['children'])) {
return $ret;
}
ksort($section_data['children'], SORT_NUMERIC);
foreach ($section_data['children'] as $section_name) {
if (!$sections_helper->sectionVisible($section_name)) {
continue;
}
$params['section_name'] = $section_name;
$ret .= $this->DrawTree($params);
$deep_level--;
}
return $ret;
}
function SectionInfo($params)
{
$section = $params['section'];
if ($section == '#session#') {
$section = $this->Application->RecallVar('section');
}
$sections_helper =& $this->Application->recallObject('SectionsHelper');
/* @var $sections_helper kSectionsHelper */
$section_data =& $sections_helper->getSectionData($section);
if (!$section_data) {
throw new Exception('Use of undefined section "<strong>' . $section . '</strong>" in "<strong>' . __METHOD__ . '</strong>"');
return '';
}
if (array_key_exists('parent', $params) && $params['parent']) {
do {
$section = $section_data['parent'];
$section_data =& $sections_helper->getSectionData($section);
} while (array_key_exists('use_parent_header', $section_data) && $section_data['use_parent_header']);
}
$info = $params['info'];
switch ($info) {
case 'module_path':
if (isset($params['module']) && $params['module']) {
$module = $params['module'];
}
elseif (isset($section_data['icon_module'])) {
$module = $section_data['icon_module'];
}
else {
$module = '#session#';
}
$res = $this->ModulePath(array('module' => $module));
break;
case 'perm_section':
$res = $sections_helper->getPermSection($section);
break;
case 'label':
if ($section && ($section != 'in-portal:root')) {
// don't translate label for top section, because it's already translated
$no_editing = array_key_exists('no_editing', $params) ? $params['no_editing'] : false;
$res = $this->Application->Phrase($section_data['label'], !$no_editing);
}
else {
$res = '';
}
break;
default:
$res = $section_data[$info];
break;
}
if (array_key_exists('as_label', $params) && $params['as_label']) {
$res = $this->Application->Phrase($res);
}
return $res;
}
function PrintSection($params)
{
$section_name = $params['section_name'];
if ($section_name == '#session#') {
$section_name = $this->Application->RecallVar('section');
}
$sections_helper =& $this->Application->recallObject('SectionsHelper');
/* @var $sections_helper kSectionsHelper */
if (isset($params['use_first_child']) && $params['use_first_child']) {
$section_name = $sections_helper->getFirstChild($section_name, true);
}
$section_data =& $sections_helper->getSectionData($section_name);
$params['name'] = $this->SelectParam($params, 'name,render_as,block');
$params['section_name'] = $section_name;
$template = $section_data['url']['t'];
unset($section_data['url']['t']);
$section_data['section_url'] = $this->Application->HREF($template, '', $section_data['url']);
$ret = $this->Application->ParseBlock( array_merge($params, $section_data) );
return $ret;
}
/**
* Used in XML drawing for tree
*
* @param Array $params
* @return string
*/
function PrintSections($params)
{
// when processings, then sort children by priority (key of children array)
$ret = '';
$section_name = $params['section_name'];
if ($section_name == '#session#') {
$section_name = $this->Application->RecallVar('section');
}
$sections_helper =& $this->Application->recallObject('SectionsHelper');
/* @var $sections_helper kSectionsHelper */
$section_data =& $sections_helper->getSectionData($section_name);
$params['name'] = $this->SelectParam($params, 'name,render_as,block');
if (!isset($section_data['children'])) {
return '';
}
ksort($section_data['children'], SORT_NUMERIC);
foreach ($section_data['children'] as $section_name) {
$params['section_name'] = $section_name;
$section_data =& $sections_helper->getSectionData($section_name);
if (!$sections_helper->sectionVisible($section_name)) {
continue;
}
else {
$show_mode = isset($section_data['show_mode']) ? $section_data['show_mode'] : smNORMAL;
$section_data['debug_only'] = ($show_mode == smDEBUG) || ($show_mode == smSUPER_ADMIN) ? 1 : 0;
}
if (isset($section_data['tabs_only']) && $section_data['tabs_only']) {
$perm_status = false;
$folder_label = $section_data['label'];
ksort($section_data['children'], SORT_NUMERIC);
foreach ($section_data['children'] as $priority => $section_name) {
// if only tabs in this section & none of them have permission, then skip section too
$section_name = $sections_helper->getPermSection($section_name);
$perm_status = $this->Application->CheckPermission($section_name.'.view', 1);
if ($perm_status) {
break;
}
}
if (!$perm_status) {
// no permission for all tabs -> don't display tree node either
continue;
}
$params['section_name'] = $section_name;
$section_data =& $sections_helper->getSectionData($section_name);
$section_data['label'] = $folder_label; // use folder label in tree
$section_data['is_tab'] = 1;
}
else {
$section_name = $sections_helper->getPermSection($section_name);
if (!$this->Application->CheckPermission($section_name.'.view', 1)) continue;
}
$params['children_count'] = isset($section_data['children']) ? count($section_data['children']) : 0;
// remove template, so it doesn't appear as additional parameter in url
$template = $section_data['url']['t'];
unset($section_data['url']['t']);
$section_data['section_url'] = $this->Application->HREF($template, '', $section_data['url']);
$late_load = getArrayValue($section_data, 'late_load');
if ($late_load) {
$t = $late_load['t'];
unset($late_load['t']);
$section_data['late_load'] = $this->Application->HREF($t, '', $late_load);
$params['children_count'] = 99;
}
else {
$section_data['late_load'] = '';
}
// restore template
$section_data['url']['t'] = $template;
$ret .= $this->Application->ParseBlock( array_merge($params, $section_data) );
$params['section_name'] = $section_name;
}
return preg_replace("/\r\n|\n/", '', $ret);
}
function ListSectionPermissions($params)
{
$section_name = isset($params['section_name']) ? $params['section_name'] : $this->Application->GetVar('section_name');
+
$sections_helper =& $this->Application->recallObject('SectionsHelper');
+ /* @var $sections_helper kSectionsHelper */
+
$section_data =& $sections_helper->getSectionData($section_name);
$block_params = array_merge($section_data, Array('name' => $params['render_as'], 'section_name' => $section_name));
$ret = '';
foreach ($section_data['permissions'] as $perm_name) {
if (preg_match('/^advanced:(.*)/', $perm_name) != $params['type']) continue;
$block_params['perm_name'] = $perm_name;
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
function ModuleInclude($params)
{
foreach ($params as $param_name => $param_value) {
$params[$param_name] = kUtil::replaceModuleSection($param_value);
}
- $m =& $this->Application->recallObject('m_TagProcessor');
- return $m->ModuleInclude($params);
+ return $this->Application->ProcessParsedTag('m', 'ModuleInclude', $params);
}
function TodayDate($params)
{
return date($params['format']);
}
function TreeEditWarrning($params)
{
$ret = $this->Application->Phrase($params['label']);
$ret = str_replace(Array('&lt;', '&gt;', 'br/', 'br /', "\n", "\r"), Array('<', '>', 'br', 'br', '', ''), $ret);
if (getArrayValue($params, 'escape')) {
$ret = addslashes($ret);
}
$ret = str_replace('<br>', '\n', $ret);
return $ret;
}
/**
* Draws section tabs using block name passed
*
* @param Array $params
*/
function ListTabs($params)
{
$sections_helper =& $this->Application->recallObject('SectionsHelper');
/* @var $sections_helper kSectionsHelper */
$section_data =& $sections_helper->getSectionData($params['section_name']);
$ret = '';
$block_params = Array('name' => $params['render_as']);
ksort($section_data['children'], SORT_NUMERIC);
foreach ($section_data['children'] as $priority => $section_name) {
$perm_section = $sections_helper->getPermSection($section_name);
if ( !$this->Application->CheckPermission($perm_section.'.view') ) {
continue;
}
$tab_data =& $sections_helper->getSectionData($section_name);
$block_params['t'] = $tab_data['url']['t'];
$block_params['title'] = $tab_data['label'];
$block_params['main_prefix'] = $section_data['SectionPrefix'];
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
/**
* Returns list of module item tabs that have view permission in current category
*
* @param Array $params
*/
function ListCatalogTabs($params)
{
$ret = '';
$special = isset($params['special']) ? $params['special'] : '';
$replace_main = isset($params['replace_m']) && $params['replace_m'];
$skip_prefixes = isset($params['skip_prefixes']) ? explode(',', $params['skip_prefixes']) : Array();
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
$prefix = $module_info['Var'];
if ($prefix == 'm' && $replace_main) {
$prefix = 'c';
}
if (in_array($prefix, $skip_prefixes) || !$this->Application->prefixRegistred($prefix) || !$this->Application->getUnitOption($prefix, 'CatalogItem')) {
continue;
}
$icon = $this->Application->getUnitOption($prefix, 'CatalogTabIcon');
if (strpos($icon, ':') !== false) {
list ($icon_module, $icon) = explode(':', $icon, 2);
}
else {
$icon_module = 'core';
}
$label = $this->Application->getUnitOption($prefix, $params['title_property']);
$block_params['title'] = $label;
$block_params['prefix'] = $prefix;
$block_params['icon_module'] = $icon_module;
$block_params['icon'] = $icon;
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
/**
* Renders inividual catalog tab based on prefix and title_property given
*
* @param Array $params
* @return string
*/
function CatalogTab($params)
{
$icon = $this->Application->getUnitOption($params['prefix'], 'CatalogTabIcon');
if (strpos($icon, ':') !== false) {
list ($icon_module, $icon) = explode(':', $icon, 2);
}
else {
$icon_module = 'core';
}
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
$block_params['icon_module'] = $icon_module;
$block_params['icon'] = $icon;
$block_params['title'] = $this->Application->getUnitOption($params['prefix'], $params['title_property']);
return $this->Application->ParseBlock($block_params);
}
/**
* Allows to construct link for opening any type of catalog item selector
*
* @param Array $params
* @return string
*/
function SelectorLink($params)
{
$mode = 'catalog';
if (isset($params['mode'])) { // {catalog, advanced_view}
$mode = $params['mode'];
unset($params['mode']);
}
$params['t'] = 'catalog/item_selector/item_selector_'.$mode;
$params['m_cat_id'] = $this->Application->getBaseCategory();
$default_params = Array('no_amp' => 1, 'pass' => 'all,'.$params['prefix']);
unset($params['prefix']);
$pass_through = Array();
if (isset($params['tabs_dependant'])) { // {yes, no}
$pass_through['td'] = $params['tabs_dependant'];
unset($params['tabs_dependant']);
}
if (isset($params['selection_mode'])) { // {single, multi}
$pass_through['tm'] = $params['selection_mode'];
unset($params['selection_mode']);
}
if (isset($params['tab_prefixes'])) { // {all, none, <comma separated prefix list>}
$pass_through['tp'] = $params['tab_prefixes'];
unset($params['tab_prefixes']);
}
if ($pass_through) {
// add pass_through to selector url if any
$params['pass_through'] = implode(',', array_keys($pass_through));
$params = array_merge($params, $pass_through);
}
// user can override default parameters (except pass_through of course)
$params = array_merge($default_params, $params);
- $main_processor =& $this->Application->recallObject('m_TagProcessor');
- return $main_processor->T($params);
+ return $this->Application->ProcessParsedTag('m', 'T', $params);
}
function TimeFrame($params)
{
$w = adodb_date('w');
$m = adodb_date('m');
$y = adodb_date('Y');
+
//FirstDayOfWeek is 0 for Sunday and 1 for Monday
$fdow = $this->Application->ConfigValue('FirstDayOfWeek');
- if ($fdow && $w == 0) $w = 7;
- $today_start = adodb_mktime(0,0,0,adodb_date('m'),adodb_date('d'),$y);
- $first_day_of_this_week = $today_start - ($w - $fdow)*86400;
- $first_day_of_this_month = adodb_mktime(0,0,0,$m,1,$y);
- $this_quater = ceil($m/3);
- $this_quater_start = adodb_mktime(0,0,0,$this_quater*3-2,1,$y);
- switch ($params['type']) {
+ if ( $fdow && $w == 0 ) {
+ $w = 7;
+ }
+ $today_start = adodb_mktime(0, 0, 0, adodb_date('m'), adodb_date('d'), $y);
+ $first_day_of_this_week = $today_start - ($w - $fdow) * 86400;
+ $first_day_of_this_month = adodb_mktime(0, 0, 0, $m, 1, $y);
+ $this_quater = ceil($m / 3);
+ $this_quater_start = adodb_mktime(0, 0, 0, $this_quater * 3 - 2, 1, $y);
+
+ switch ( $params['type'] ) {
case 'last_week_start':
- $timestamp = $first_day_of_this_week - 86400*7;
+ $timestamp = $first_day_of_this_week - 86400 * 7;
break;
+
case 'last_week_end':
$timestamp = $first_day_of_this_week - 1;
break;
case 'last_month_start':
- $timestamp = $m == 1 ? adodb_mktime(0,0,0,12,1,$y-1) : adodb_mktime(0,0,0,$m-1,1,$y);
+ $timestamp = $m == 1 ? adodb_mktime(0, 0, 0, 12, 1, $y - 1) : adodb_mktime(0, 0, 0, $m - 1, 1, $y);
break;
+
case 'last_month_end':
- $timestamp = $first_day_of_this_month = adodb_mktime(0,0,0,$m,1,$y) - 1;
+ $timestamp = $first_day_of_this_month = adodb_mktime(0, 0, 0, $m, 1, $y) - 1;
break;
case 'last_quater_start':
- $timestamp = $this_quater == 1 ? adodb_mktime(0,0,0,10,1,$y-1) : adodb_mktime(0,0,0,($this_quater-1)*3-2,1,$y);
+ $timestamp = $this_quater == 1 ? adodb_mktime(0, 0, 0, 10, 1, $y - 1) : adodb_mktime(0, 0, 0, ($this_quater - 1) * 3 - 2, 1, $y);
break;
+
case 'last_quater_end':
$timestamp = $this_quater_start - 1;
break;
case 'last_6_months_start':
- $timestamp = $m <= 6 ? adodb_mktime(0,0,0,$m+6,1,$y-1) : adodb_mktime(0,0,0,$m-6,1,$y);
+ $timestamp = $m <= 6 ? adodb_mktime(0, 0, 0, $m + 6, 1, $y - 1) : adodb_mktime(0, 0, 0, $m - 6, 1, $y);
break;
case 'last_year_start':
- $timestamp = adodb_mktime(0,0,0,1,1,$y-1);
+ $timestamp = adodb_mktime(0, 0, 0, 1, 1, $y - 1);
break;
+
case 'last_year_end':
- $timestamp = adodb_mktime(23,59,59,12,31,$y-1);
+ $timestamp = adodb_mktime(23, 59, 59, 12, 31, $y - 1);
break;
- }
+ default:
+ $timestamp = 0;
+ break;
+ }
- if (isset($params['format'])) {
+ if ( isset($params['format']) ) {
$format = $params['format'];
- if(preg_match("/_regional_(.*)/", $format, $regs))
- {
+
+ if ( preg_match("/_regional_(.*)/", $format, $regs) ) {
$lang =& $this->Application->recallObject('lang.current');
+ /* @var $lang LanguagesItem */
+
$format = $lang->GetDBField($regs[1]);
}
+
return adodb_date($format, $timestamp);
}
return $timestamp;
-
}
/**
* Redirect to cache rebuild template, when required by installator
*
* @param Array $params
*/
function CheckPermCache($params)
{
// we have separate session between install wizard and admin console, so store in cache
$global_mark = $this->Application->getDBCache('ForcePermCacheUpdate');
$local_mark = $this->Application->RecallVar('PermCache_UpdateRequired');
if ($global_mark || $local_mark) {
$this->Application->RemoveVar('PermCache_UpdateRequired');
if ($this->Application->ConfigValue('QuickCategoryPermissionRebuild')) {
$updater =& $this->Application->makeClass('kPermCacheUpdater');
/* @var $updater kPermCacheUpdater */
$updater->OneStepRun();
$this->Application->HandleEvent($event, 'c:OnResetCMSMenuCache');
}
else {
// update with progress bar
return true;
}
}
return false;
}
/**
* Checks if current protocol is SSL
*
* @param Array $params
* @return int
*/
function IsSSL($params)
{
return (PROTOCOL == 'https://')? 1 : 0;
}
function PrintColumns($params)
{
$picker_helper =& $this->Application->RecallObject('ColumnPickerHelper');
$picker_helper->SetGridName($this->Application->GetLinkedVar('grid_name'));
/* @var $picker_helper kColumnPickerHelper */
$main_prefix = $this->Application->RecallVar('main_prefix');
$cols = $picker_helper->LoadColumns($main_prefix);
$this->Application->Phrases->AddCachedPhrase('__FREEZER__', '-------------');
$o = '';
if (isset($params['hidden']) && $params['hidden']) {
foreach ($cols['hidden_fields'] as $col) {
$title = $this->Application->Phrase($cols['titles'][$col]);
$o .= "<option value='$col'>".$title;
}
}
else {
foreach ($cols['order'] as $col) {
if (in_array($col, $cols['hidden_fields'])) continue;
$title = $this->Application->Phrase($cols['titles'][$col]);
$o .= "<option value='$col'>".$title;
}
}
return $o;
}
/**
* Allows to set popup size (key - current template name)
*
* @param Array $params
*/
function SetPopupSize($params)
{
$width = $params['width'];
$height = $params['height'];
if ($this->Application->GetVar('ajax') == 'yes') {
// during AJAX request just output size
die($width.'x'.$height);
}
if (!$this->UsePopups($params)) {
return ;
}
$t = $this->Application->GetVar('t');
$sql = 'SELECT *
FROM '.TABLE_PREFIX.'PopupSizes
WHERE TemplateName = '.$this->Conn->qstr($t);
$popup_info = $this->Conn->GetRow($sql);
if (!$popup_info) {
// create new popup size record
$fields_hash = Array (
'TemplateName' => $t,
'PopupWidth' => $width,
'PopupHeight' => $height,
);
$this->Conn->doInsert($fields_hash, TABLE_PREFIX.'PopupSizes');
}
elseif ($popup_info['PopupWidth'] != $width || $popup_info['PopupHeight'] != $height) {
// popup found and size in tag differs from one in db -> update in db
$fields_hash = Array (
'PopupWidth' => $width,
'PopupHeight' => $height,
);
$this->Conn->doUpdate($fields_hash, TABLE_PREFIX.'PopupSizes', 'PopupId = '.$popup_info['PopupId']);
}
}
/**
* Returns popup size (by template), if not cached, then parse template to get value
*
* @param Array $params
* @return string
*/
function GetPopupSize($params)
{
$t = $this->Application->GetVar('template_name');
$sql = 'SELECT *
FROM '.TABLE_PREFIX.'PopupSizes
WHERE TemplateName = '.$this->Conn->qstr($t);
$popup_info = $this->Conn->GetRow($sql);
if (!$popup_info) {
$this->Application->InitParser();
$this->Application->ParseBlock(array('name' => $t)); // dies when SetPopupSize tag found & in ajax requrest
return '750x400'; // tag SetPopupSize not found in template -> use default size
}
return $popup_info['PopupWidth'].'x'.$popup_info['PopupHeight'];
}
/**
* Allows to check if popups are generally enabled OR to check for "popup" or "modal" mode is enabled
*
* @param Array $params
* @return bool
*/
function UsePopups($params)
{
if ($this->Application->GetVar('_force_popup')) {
return true;
}
$use_popups = (int)$this->Application->ConfigValue('UsePopups');
if (array_key_exists('mode', $params)) {
$mode_mapping = Array ('popup' => 1, 'modal' => 2);
return $use_popups == $mode_mapping[ $params['mode'] ];
}
return $use_popups;
}
function UseToolbarLabels($params)
{
return (int)$this->Application->ConfigValue('UseToolbarLabels');
}
/**
* Checks if debug mode enabled (optionally) and specified constant is on
*
* @param Array $params
* @return bool
* @todo Could be a duplicate of kMainTagProcessor::ConstOn
*/
function ConstOn($params)
{
$constant_name = $this->SelectParam($params, 'name,const');
$debug_mode = isset($params['debug_mode']) && $params['debug_mode'] ? $this->Application->isDebugMode() : true;
return $debug_mode && kUtil::constOn($constant_name);
}
/**
* Builds link to last template in main frame of admin
*
* @param Array $params
* @return string
*/
function MainFrameLink($params)
{
$persistent = isset($params['persistent']) && $params['persistent'];
if ($persistent && $this->Application->ConfigValue('RememberLastAdminTemplate')) {
// check last_template in persistent session
$last_template = $this->Application->RecallPersistentVar('last_template_popup');
}
else {
// check last_template in session
$last_template = $this->Application->RecallVar('last_template_popup'); // because of m_opener=s there
}
if (!$last_template) {
$params['persistent'] = 1;
return $persistent ? false : $this->MainFrameLink($params);
}
list($index_file, $env) = explode('|', $last_template);
$vars = $this->Application->HttpQuery->processQueryString($env, 'pass');
$recursion_templates = Array ('login', 'index', 'no_permission');
if (isset($vars['admin']) && $vars['admin'] == 1) {
// index template doesn't begin recursion on front-end (in admin frame)
$vars['m_theme'] = '';
if (isset($params['m_opener']) && $params['m_opener'] == 'r') {
// front-end link for highlighting purposes
$vars['t'] = 'index';
$vars['m_cat_id'] = $this->Application->getBaseCategory();
}
unset($recursion_templates[ array_search('index', $recursion_templates)]);
}
if (in_array($vars['t'], $recursion_templates)) {
// prevents redirect recursion OR old in-portal pages
$params['persistent'] = 1;
return $persistent ? false : $this->MainFrameLink($params);
}
$vars = array_merge($vars, $params);
$t = $vars['t'];
unset($vars['t'], $vars['persistent']);
// substitute language in link to current (link will work, even when language will be changed)
$vars['m_lang'] = $this->Application->GetVar('m_lang');
return $this->Application->HREF($t, '', $vars, $index_file);
}
/**
* Returns menu frame width or 200 in case, when invalid width specified in config
*
* @param Array $params
* @return string
*/
function MenuFrameWidth($params)
{
$width = (int)$this->Application->ConfigValue('MenuFrameWidth');
return $width > 0 ? $width : 200;
}
function AdminSkin($params)
{
$skin_helper =& $this->Application->recallObject('SkinHelper');
/* @var $skin_helper SkinHelper */
return $skin_helper->AdminSkinTag($params);
}
- function PrintCompileErrors($params)
+ /**
+ * Prints errors, discovered during mass template compilation
+ *
+ * @param $params
+ * @return string
+ * @access protected
+ */
+ protected function PrintCompileErrors($params)
{
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
$errors = $this->Application->RecallVar('compile_errors');
- if (!$errors) {
- return ;
+ if ( !$errors ) {
+ return '';
}
$ret = '';
$errors = unserialize($errors);
foreach ($errors as $an_error) {
$block_params['file'] = str_replace(FULL_PATH, '', $an_error['file']);
$block_params['line'] = $an_error['line'];
$block_params['message'] = $an_error['msg'];
$ret .= $this->Application->ParseBlock($block_params);
}
$this->Application->RemoveVar('compile_errors');
return $ret;
}
function CompileErrorCount($params)
{
$errors = $this->Application->RecallVar('compile_errors');
if (!$errors) {
return 0;
}
return count( unserialize($errors) );
}
function ExportData($params)
{
$export_helper =& $this->Application->recallObject('CSVHelper');
/* @var $export_helper kCSVHelper */
$result = $export_helper->ExportData( $this->SelectParam($params, 'var,name,field') );
return ($result === false) ? '' : $result;
}
function ImportData($params)
{
$import_helper =& $this->Application->recallObject('CSVHelper');
/* @var $import_helper kCSVHelper */
$result = $import_helper->ImportData( $this->SelectParam($params, 'var,name,field') );
return ($result === false) ? '' : $result;
}
function PrintCSVNotImportedLines($params)
{
$import_helper =& $this->Application->recallObject('CSVHelper');
/* @var $import_helper kCSVHelper */
return $import_helper->GetNotImportedLines();
}
/**
* Returns input field name to
* be placed on form (for correct
* event processing)
*
* @param Array $params
* @return string
* @access public
*/
function InputName($params)
{
list($id, $field) = $this->prepareInputName($params);
$ret = $this->getPrefixSpecial().'[0]['.$field.']'; // 0 always, as has no idfield
if( getArrayValue($params, 'as_preg') ) $ret = preg_quote($ret, '/');
return $ret;
}
/**
* Returns list of all backup file dates formatted
* in passed block
*
* @param Array $params
* @return string
* @access public
*/
function PrintBackupDates($params)
{
$datearray = $this->getDirList($this->Application->ConfigValue('Backup_Path'));
$ret = '';
foreach($datearray as $key => $value)
{
$params['backuptimestamp'] = $value['filedate'];
$params['backuptime'] = date('F j, Y, g:i a', $value['filedate']);
$params['backupsize'] = round($value['filesize']/1024/1024, 2); // MBytes
$ret .= $this->Application->ParseBlock($params);
}
return $ret;
}
function getDirList ($dirName)
{
$file_helper =& $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
$file_helper->CheckFolder($dirName);
$fileinfo = array();
$d = dir($dirName);
while($entry = $d->read())
{
if ($entry != "." && $entry != "..")
{
if (!is_dir($dirName."/".$entry) && strpos($entry, 'dump') !== false)
{
$fileinfo[]= Array('filedate' => $this->chopchop($entry),
'filesize' => filesize($dirName. '/'. $entry)
);
}
}
}
$d->close();
rsort($fileinfo);
return $fileinfo;
}
function chopchop ($filename)
{
$p = pathinfo($filename);
$ext = '.'.$p['extension'];
$filename;
$filename = str_replace('dump', '',$filename);
$filename = str_replace($ext, '', $filename);
return $filename;
}
/**
* Returns phpinfo() output
*
* @param Array $params
* @return string
*/
function PrintPHPinfo($params)
{
ob_start();
phpinfo();
return ob_get_clean();
}
function PrintSqlCols($params)
{
$a_data = unserialize($this->Application->GetVar('sql_rows'));
$ret = '';
$block = $params['render_as'];
foreach ($a_data AS $a_row)
{
foreach ($a_row AS $col => $value)
{
$ret .= $this->Application->ParseBlock(Array('name'=>$block, 'value'=>$col));
}
break;
}
return $ret;
}
function PrintSqlRows($params)
{
$a_data = unserialize($this->Application->GetVar('sql_rows'));
$ret = '';
$block = $params['render_as'];
foreach ($a_data AS $a_row)
{
$cells = '';
foreach ($a_row AS $col => $value)
{
$cells .= '<td>'.$value.'</td>';
}
$ret .= $this->Application->ParseBlock(Array('name'=>$block, 'cells'=>$cells));
}
return $ret;
}
/**
* Prints available and enabled import sources using given block
*
* @param Array $params
* @return string
*/
function PrintImportSources($params)
{
$sql = 'SELECT *
FROM ' . TABLE_PREFIX . 'ImportScripts
WHERE (Status = ' . STATUS_ACTIVE . ') AND (Type = "CSV")';
$import_sources = $this->Conn->Query($sql);
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
$ret = '';
foreach ($import_sources as $import_source) {
$block_params['script_id'] = $import_source['ImportId'];
$block_params['script_module'] = mb_strtolower($import_source['Module']);
$block_params['script_name'] = $import_source['Name'];
$block_params['script_prefix'] = $import_source['Prefix'];
$block_params['module_path'] = $this->Application->findModule('Name', $import_source['Module'], 'Path');
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
/**
* Checks, that new window should be opened in "incs/close_popup" template instead of refreshing parent window
*
* @param Array $params
* @return bool
*/
function OpenNewWindow($params)
{
if (!$this->UsePopups($params)) {
return false;
}
$diff = array_key_exists('diff', $params) ? $params['diff'] : 0;
$wid = $this->Application->GetVar('m_wid');
$stack_name = rtrim('opener_stack_' . $wid, '_');
$opener_stack = $this->Application->RecallVar($stack_name);
$opener_stack = $opener_stack ? unserialize($opener_stack) : Array ();
return count($opener_stack) >= 2 - $diff;
}
/**
* Allows to dynamically change current language in template
*
* @param Array $params
*/
function SetLanguage($params)
{
$this->Application->SetVar('m_lang', $params['language_id']);
$this->Application->Phrases->Init('phrases', '', $params['language_id']);
}
/**
* Performs HTTP Authentification for administrative console
*
* @param Array $params
*/
function HTTPAuth($params)
{
if (!$this->Application->ConfigValue('UseHTTPAuth')) {
// http authentification not required
return true;
}
$super_admin_ips = defined('SA_IP') ? SA_IP : false;
$auth_bypass_ips = $this->Application->ConfigValue('HTTPAuthBypassIPs');
if (($auth_bypass_ips && kUtil::ipMatch($auth_bypass_ips)) || ($super_admin_ips && kUtil::ipMatch($super_admin_ips))) {
// user ip is in ip bypass list
return true;
}
if (!array_key_exists('PHP_AUTH_USER', $_SERVER)) {
// ask user to authentificate, when not authentificated before
return $this->_httpAuthentificate();
}
else {
// validate user credentials (browsers remembers user/password
// and sends them each time page is visited, so no need to save
// authentification result in session)
if ($this->Application->ConfigValue('HTTPAuthUsername') != $_SERVER['PHP_AUTH_USER']) {
// incorrect username
return $this->_httpAuthentificate();
}
$password_formatter =& $this->Application->recallObject('kPasswordFormatter');
/* @var $password_formatter kPasswordFormatter */
$password = $password_formatter->EncryptPassword($_SERVER['PHP_AUTH_PW'], 'b38');
if ($this->Application->ConfigValue('HTTPAuthPassword') != $password) {
// incorrect password
return $this->_httpAuthentificate();
}
}
return true;
}
/**
* Ask user to authentificate
*
* @return false
*/
function _httpAuthentificate()
{
$realm = strip_tags( $this->Application->ConfigValue('Site_Name') );
header('WWW-Authenticate: Basic realm="' . $realm . '"');
header('HTTP/1.0 401 Unauthorized');
return false;
}
/**
* Checks, that we are using memory cache
*
* @param Array $params
* @return bool
*/
function MemoryCacheEnabled($params)
{
return $this->Application->isCachingType(CACHING_TYPE_MEMORY);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/config_search/config_search_event_handler.php
===================================================================
--- branches/5.2.x/core/units/config_search/config_search_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/config_search/config_search_event_handler.php (revision 14628)
@@ -1,127 +1,150 @@
<?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 ConfigSearchEventHandler extends kDBEventHandler {
/**
* Changes permission section to one from REQUEST, not from config
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
$module = $this->Application->GetVar('module');
$main_prefix = $this->Application->findModule('Name', $module, 'Var');
$section = $this->Application->getUnitOption($main_prefix.'.search', 'PermSection');
$event->setEventParam('PermSection', $section);
+
return parent::CheckPermission($event);
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
+ * @return void
* @access protected
- * @see OnListBuild
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
-
+ /* @var $object kDBList */
+
// show only items that belong to selected module
$module = $this->Application->GetVar('module');
$object->addFilter('module_filter', '%1$s.ModuleName = '.$this->Conn->qstr($module));
// don't show disabled search items
$object->addFilter('active_filter', '%1$s.SimpleSearch <> -1');
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnUpdate(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return ;
}
parent::OnUpdate($event);
$conf_update = new kEvent('conf:OnUpdate');
$conf_update->redirect = false;
$this->Application->HandleEvent($conf_update);
$event->SetRedirectParam('opener', 's');
// keeps module and section in REQUEST to ensure, that last admin template will work
$event->SetRedirectParam('module', $this->Application->GetVar('module'));
$event->SetRedirectParam('module_key', $this->Application->GetVar('module_key'));
$event->SetRedirectParam('section', $this->Application->GetVar('section'));
}
+ /**
+ * Cancels kDBItem Editing/Creation
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
function OnCancel(&$event)
{
parent::OnCancel($event);
+
$event->SetRedirectParam('opener', 's');
}
/**
- * [HOOK] Enter description here...
+ * [HOOK] Creates search config record corresponding to custom field, that was just created
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnCreateCustomField(&$event)
+ protected function OnCreateCustomField(&$event)
{
$custom_field =& $event->MasterEvent->getObject();
- if ($custom_field->GetDBField('Type') == 6 || $custom_field->GetDBField('IsSystem') == 1) {
+ /* @var $custom_field kDBItem */
+
+ if ( $custom_field->GetDBField('Type') == 6 || $custom_field->GetDBField('IsSystem') == 1 ) {
// user & system custom fields are not searchable
- return false;
+ return ;
}
- $object =& $event->getObject( Array('skip_autoload' => true) );
+ $object =& $event->getObject(Array ('skip_autoload' => true));
+ /* @var $object kDBItem */
$custom_id = $custom_field->GetID();
- if (!$object->isLoaded() || ($object->GetDBField('CustomFieldId') != $custom_id)) {
+ if ( !$object->isLoaded() || ($object->GetDBField('CustomFieldId') != $custom_id) ) {
$object->Load($custom_id, 'CustomFieldId');
}
- $cf_search = Array();
+ $cf_search = Array ();
$element_type = $custom_field->GetDBField('ElementType');
$cf_search['DisplayOrder'] = $custom_field->GetDBField('DisplayOrder');
$cf_search['FieldType'] = $element_type;
$cf_search['DisplayName'] = $custom_field->GetDBField('FieldLabel');
$cf_search['FieldName'] = $custom_field->GetDBField('FieldName');
$cf_search['Description'] = $custom_field->GetDBField('Prompt');
$cf_search['ConfigHeader'] = $custom_field->GetDBField('Heading'); // 'la_Text_CustomFields';
$cf_search['SimpleSearch'] = in_array($element_type, Array ('text', 'range', 'select', 'multiselect')) ? 1 : 0;
$cf_search['TableName'] = 'CustomField';
$sql = 'SELECT Module
- FROM '.TABLE_PREFIX.'ItemTypes
- WHERE ItemType = '.$custom_field->GetDBField('Type');
+ FROM ' . TABLE_PREFIX . 'ItemTypes
+ WHERE ItemType = ' . $custom_field->GetDBField('Type');
- $cf_search['ModuleName'] = $this->Conn->GetOne($sql);
+ $cf_search['ModuleName'] = $this->Conn->GetOne($sql);
$object->SetFieldsFromHash($cf_search);
$object->SetDBField('CustomFieldId', $custom_id);
- $result = $object->isLoaded() ? $object->Update() : $object->Create();
+ if ( $object->isLoaded() ) {
+ $object->Update();
+ }
+ else {
+ $object->Create();
+ }
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/user_profile/user_profile_tp.php
===================================================================
--- branches/5.2.x/core/units/user_profile/user_profile_tp.php (revision 14627)
+++ branches/5.2.x/core/units/user_profile/user_profile_tp.php (revision 14628)
@@ -1,145 +1,146 @@
<?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 UserProfileTagProcessor extends kDBTagProcessor {
function Field($params)
{
$field = $this->SelectParam($params, 'name,field');
$profile_mapping = $this->Application->getUnitOption('u', 'UserProfileMapping');
$user_field = array_key_exists($field, $profile_mapping) ? $profile_mapping[$field] : false;
if (array_key_exists('profile_field', $params) && $params['profile_field']) {
// get variable from mapping
$params['name'] = $user_field;
$value = $this->Application->ProcessParsedTag('u.profile', 'Field', $params);
}
elseif ($user_field) {
// old style variable for displaying fields in public profile (named "pp_*")
$block_params = Array ('name' => 'DisplayToPublic', 'value' => $user_field);
$value = $this->Application->ProcessParsedTag($this->getUserPrefixSpecial(), 'Selected', $block_params);
}
else {
// get variable by name
$value = $this->recallUserProfileVar($field);
}
if (isset($params['checked']) && $params['checked']) {
$checked_value = isset($params['value']) ? $params['value'] : 1;
$value = ($value == $checked_value) ? 'checked' : '';
}
return $value;
}
/**
* Returns prefix and special of user to operate with
*
* @return string
*/
function getUserPrefixSpecial()
{
return $this->Application->GetVar('user_id') ? 'u.profile' : 'u.current';
}
/**
* Allows to get persistent var from other user
*
* @param int $user_id
* @param string $var_name
* @return mixed
*/
function recallUserProfileVar($var_name)
{
static $cache = null;
if (!isset($cache)) {
$user =& $this->Application->recallObject( $this->getUserPrefixSpecial() );
/* @var $user kDBItem */
$sql = 'SELECT VariableValue, VariableName
FROM ' . TABLE_PREFIX . 'PersistantSessionData
WHERE (PortalUserId = ' . $user->GetID() . ')';
$cache = $this->Conn->GetCol($sql, 'VariableName');
}
if (array_key_exists($var_name, $cache)) {
// get variable value from persistent session
return $cache[$var_name];
}
else {
// not found in persistent session -> get default value from config variable with same name
$config_value = $this->Application->ConfigValue($var_name);
if ($config_value !== false) {
return $config_value;
}
}
return false;
}
/**
* Returns visible field count in user profile
*
* @param Array $params
* @return int
*/
function ProfileFieldCount($params)
{
static $field_count = null;
if (!isset($field_count)) {
$user =& $this->Application->recallObject( $this->getUserPrefixSpecial() );
/* @var $user kDBItem */
$display_to_public = $user->GetDBField('DisplayToPublic');
$field_count = $display_to_public ? substr_count($display_to_public, '|') - 1 : 0;
}
return $field_count;
}
/**
* Allows to detect that not all fields were shown
*
* @param Array $params
* @return bool
+ * @access protected
*/
- function NotLastField($params)
+ protected function NotLastField($params)
{
- $counter = (int)$this->Application->GetVar($params['counter']);
+ $counter = (int)$this->Application->GetVar( $params['counter'] );
- return $counter < $this->ProfileFieldCount();
+ return $counter < $this->ProfileFieldCount($params);
}
/**
- * Because of persistant session table doesn't have ids, we use user id as id for each record
+ * Because of persistent session table doesn't have ids, we use user id as id for each record
*
* @param Array $params
* @return Array (id,field)
* @access private
*/
function prepareInputName($params)
{
$params['force_id'] = $this->Application->RecallVar('user_id');
return parent::prepareInputName($params);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/relationship/relationship_event_handler.php
===================================================================
--- branches/5.2.x/core/units/relationship/relationship_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/relationship/relationship_event_handler.php (revision 14628)
@@ -1,275 +1,285 @@
<?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 RelationshipEventHandler extends kDBEventHandler
{
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnProcessSelected' => Array('subitem' => 'add|edit'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Initializes new relation
*
* @param kEvent $event
*/
function OnNew(&$event)
{
parent::OnNew($event);
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$table_info = $object->getLinkedInfo();
$object->SetDBField('SourceId', $table_info['ParentId']);
$source_itemtype = $this->Application->getUnitOption($table_info['ParentPrefix'], 'ItemType');
$object->SetDBField('SourceType', $source_itemtype);
$object->SetDBField('TargetId', $this->Application->GetVar('target_id'));
$object->SetDBField('TargetType', $this->Application->GetVar('target_type'));
$this->OnAfterItemLoad($event);
}
/**
* Add new relation
*
* @param kEvent $event
*/
function OnProcessSelected(&$event)
{
$dst_field = $this->Application->RecallVar('dst_field');
- if ($dst_field == 'TargetId') {
+
+ if ( $dst_field == 'TargetId' ) {
// prepare target_id & target_type
- $object =& $event->getObject( Array('skip_autoload' => true) );
+ $object =& $event->getObject(Array ('skip_autoload' => true));
- $selected_ids = $this->Application->GetVar('selected_ids');
+ $target_id = 0;
$target_prefix = false;
+ $selected_ids = $this->Application->GetVar('selected_ids');
+
foreach ($selected_ids as $selected_prefix => $target_id) {
- if ($target_id > 0) {
+ if ( $target_id > 0 ) {
$target_prefix = $selected_prefix;
break;
}
}
- if (!$target_prefix) {
- $this->finalizePopup($event);
+ if ( !$target_prefix ) {
+ $event->SetRedirectParam('opener', 'u');
return;
}
$sql = 'SELECT ResourceId
- FROM '.$this->Application->getUnitOption($target_prefix, 'TableName').'
- WHERE '.$this->Application->getUnitOption($target_prefix, 'IDField').' IN ('.$target_id.')';
+ FROM ' . $this->Application->getUnitOption($target_prefix, 'TableName') . '
+ WHERE ' . $this->Application->getUnitOption($target_prefix, 'IDField') . ' IN (' . $target_id . ')';
$target_id = $this->Conn->GetOne($sql);
$target_type = $this->Application->getUnitOption($target_prefix, 'ItemType');
// don't add same relation twice
$table_info = $object->getLinkedInfo();
$sql = 'SELECT TargetId
- FROM '.$object->TableName.'
- WHERE (SourceId = '.$table_info['ParentId'].') AND (TargetId = '.$target_id.')';
+ FROM ' . $object->TableName . '
+ WHERE (SourceId = ' . $table_info['ParentId'] . ') AND (TargetId = ' . $target_id . ')';
$duplicate_relation = $this->Conn->GetOne($sql) == $target_id;
- $this->finalizePopup($event);
+ $event->SetRedirectParam('opener', 'u');
- if (!$duplicate_relation) {
+ if ( !$duplicate_relation ) {
// place correct template in opener stack
$source_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
- $template = $this->Application->getUnitOption($source_prefix, 'AdminTemplatePath').'/relations_edit';
+ $template = $this->Application->getUnitOption($source_prefix, 'AdminTemplatePath') . '/relations_edit';
- $redirect_params = Array($event->Prefix.'_event' => 'OnNew', 'target_id' => $target_id, 'm_opener' => 's', 'target_type' => $target_type);
- $this->Application->EventManager->openerStackPush($template, $redirect_params, 'all,'.$event->Prefix);
+ $redirect_params = Array ($event->Prefix . '_event' => 'OnNew', 'target_id' => $target_id, 'm_opener' => 's', 'target_type' => $target_type);
+ $this->Application->EventManager->openerStackPush($template, $redirect_params, 'all,' . $event->Prefix);
}
}
else {
- $this->finalizePopup($event);
+ $event->SetRedirectParam('opener', 'u');
}
}
/**
* Set ItemName & ItemType virtual fields based on loaded item data
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemLoad(&$event)
+ protected function OnAfterItemLoad(&$event)
{
+ parent::OnAfterItemLoad($event);
+
$object =& $event->getObject();
+ /* @var $object kDBItem */
$sql = 'SELECT Prefix
- FROM '.TABLE_PREFIX.'ItemTypes
- WHERE ItemType = '.$object->GetDBField('TargetType');
+ FROM ' . TABLE_PREFIX . 'ItemTypes
+ WHERE ItemType = ' . $object->GetDBField('TargetType');
$target_prefix = $this->Conn->GetOne($sql);
$title_field = $this->getTitleField($target_prefix);
$title_phrase = $this->Application->getUnitOption($target_prefix, 'TitlePhrase');
- $sql = 'SELECT '.$title_field.'
- FROM '.$this->Application->getUnitOption($target_prefix, 'TableName').'
- WHERE ResourceId = '.$object->GetDBField('TargetId');
+ $sql = 'SELECT ' . $title_field . '
+ FROM ' . $this->Application->getUnitOption($target_prefix, 'TableName') . '
+ WHERE ResourceId = ' . $object->GetDBField('TargetId');
$object->SetDBField('ItemName', $this->Conn->GetOne($sql));
$object->SetDBField('ItemType', $this->Application->Phrase($title_phrase));
}
/**
* Creates needed sql query to load list,
* if no query is defined in config for
* special requested, then use default
* query
*
* @param kEvent $event
* @access protected
*/
function ListPrepareQuery(&$event)
{
return $this->BaseQuery($event, 'ListSQLs');
}
/**
* Creates needed sql query to load item,
* if no query is defined in config for
* special requested, then use default
* query
*
* @param kEvent $event
* @access protected
*/
function ItemPrepareQuery(&$event)
{
return $this->BaseQuery($event, 'ItemSQLs');
}
/**
* Get item name & type based on relation type & modules installed
*
* @param kEvent $event
* @param string $sql_field
*/
function BaseQuery(&$event, $sql_field)
{
$sqls = $this->Application->getUnitOption($event->Prefix,$sql_field);
$sql = isset($sqls[$event->Special]) ? $sqls[$event->Special] : $sqls[''];
$configs = $this->extractModulesInfo();
// 2. build sql based on information queried
$sql_templates['ItemName'] = 'IFNULL(%s.%s,\' \')';
$sql_templates['TableJoin'] = 'LEFT JOIN %1$s ON %1$s.ResourceId = rel.TargetId';
$sql_templates['TargetName'] = 'IF(rel.TargetType = %s, \'%s\', %s)';
$sql_parts = Array();
$sql_parts['TargetName'] = "''";
foreach ($configs as $prefix => $config_data) {
$title_field = $this->getTitleField($prefix);
$sql_parts['ItemName'][] = sprintf($sql_templates['ItemName'], $config_data['TableName'], $title_field);
$sql_parts['TableJoin'][] = sprintf($sql_templates['TableJoin'], $config_data['TableName']);
$sql_parts['TargetName'] = sprintf( $sql_templates['TargetName'],
$config_data['ItemType'],
'!'.$config_data['TitlePhrase'].'!',
$sql_parts['TargetName']);
$sql_parts['TargetName'] = str_replace('rel','%1$s',$sql_parts['TargetName']);
}
$object =& $event->getObject();
$vars = Array('#ITEM_NAMES#', '#ITEM_TYPES#');
$replacements = Array( implode(', ',$sql_parts['ItemName']), $sql_parts['TargetName'] );
$calculated_fields = $object->getCalculatedFields();
foreach ($calculated_fields as $field_name => $field_expression) {
$calculated_fields[$field_name] = str_replace($vars, $replacements, $field_expression);
}
$object->setCalculatedFields($calculated_fields);
$sql = str_replace('#ITEM_JOIN#', implode(' ',$sql_parts['TableJoin']), $sql);
$sql = str_replace('rel.','%1$s.',$sql);
return $sql;
}
/**
* Convert TitleField field of kMultiLanguage formatter used for it
*
* @param string $prefix
* @return string
*/
function getTitleField($prefix)
{
$lang_prefix = 'l'.$this->Application->GetVar('m_lang').'_';
$title_field = $this->Application->getUnitOption($prefix, 'TitleField');
$field_options = $this->Application->getUnitOption($prefix.'.'.$title_field, 'Fields');
$formatter_class = isset($field_options['formatter']) ? $field_options['formatter'] : '';
if ($formatter_class == 'kMultiLanguage' && !isset($field_options['master_field'])) {
$title_field = $lang_prefix.$title_field;
}
return $title_field;
}
/**
* Get configs from modules installed
*
* @return Array
* @access private
*/
function extractModulesInfo()
{
// get installed modules & their config info
// maybe we should leave only prefixes, that have "view" permission
$configs = Array();
foreach ($this->Application->ModuleInfo as $module_name => $module_data) {
$prefix = $module_data['Var'];
if ($prefix == 'm') {
$prefix = 'c';
}
if (!$this->Application->prefixRegistred($prefix)) continue;
$configs[$prefix] = $this->Application->getUnitOptions($prefix);
if($configs[$prefix] === false) unset($configs[$prefix]);
if(!isset($configs[$prefix]['CatalogItem']) || !$configs[$prefix]['CatalogItem']) unset($configs[$prefix]);
}
return $configs;
}
/**
* Deletes relations to hooked item from other items
*
* @param kEvent $event
*/
function OnDeleteForeignRelations(&$event)
{
$main_object =& $event->MasterEvent->getObject();
$resource_id = $main_object->GetDBField('ResourceId');
$table = $this->Application->getUnitOption($event->Prefix,'TableName');
$sql = 'DELETE FROM '.$table.' WHERE TargetId = '.$resource_id;
$this->Conn->Query($sql);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/languages/languages_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/languages/languages_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/languages/languages_tag_processor.php (revision 14628)
@@ -1,138 +1,144 @@
<?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 LanguagesTagProcessor extends kDBTagProcessor
{
/**
* Guesses what charset should be used:
* 1. use lang.current_Field field="Charset" by default
* 2. if using temp tables then use lang_Field field="Charset"
*
* @param Array $params
* @return string
*/
function GetCharset($params)
{
$edit_direct = $this->Application->GetVar('phrases_label');
$top_prefix = $this->Application->GetTopmostPrefix($this->Prefix);
if( substr($this->Application->GetVar($top_prefix.'_mode'), 0, 1) == 't' && !$edit_direct )
{
- $object =& $this->getObject();
+ $object =& $this->getObject($params);
+ /* @var $object kDBItem */
+
return $object->GetDBField('Charset');
}
+
$lang_current =& $this->Application->recallObject('lang.current');
+ /* @var $lang_current LanguagesItem */
+
return $lang_current->GetDBField('Charset');
}
/**
* Returns formatted date + time on current language
*
* @param Array $params
* @return string
*/
function CurrentDate($params)
{
$format = $params['format'];
$date = adodb_mktime();
if (strpos($format, 'l') !== false) {
$week_day = $this->Application->Phrase('lu_weekday_'.adodb_date('l'));
$format = str_replace('l', '#@#', $format); // replace with reserved char (preserves translation link)
return str_replace('#@#', $week_day, adodb_date($format, $date));
}
return adodb_date($format, $date);
}
function ListLanguages($params)
{
$this->Init($this->Prefix, 'enabled');
return $this->PrintList2($params);
}
function LanguageName($params)
{
$object =& $this->getObject($params);
return $this->Application->Phrase($params['phrase_prefix'].$object->GetDBField('PackName'));
}
function LanguageLink($params)
{
$object =& $this->getObject($params);
$params['m_lang'] = $object->GetID();
return $this->Application->ProcessParsedTag('m', 'Link', $params);
}
function SelectedLanguage($params)
{
$object =& $this->getObject($params);
/* @var $object kDBList */
if (array_key_exists('type', $params) && $params['type'] == 'data') {
// when using language selector on editing forms
return $object->GetDBField('LanguageId') == $this->Application->GetVar('m_lang');
}
return $object->GetDBField('LanguageId') == $this->Application->Phrases->LanguageId;
}
/**
* Returns path where exported languages should be saved
*
- * @param unknown_type $params
+ * @param Array $params
+ * @return string
+ * @access protected
*/
- function ExportPath($params)
+ protected function ExportPath($params)
{
+ $ret = EXPORT_PATH . '/';
- $ret = EXPORT_PATH.'/';
-
- if( getArrayValue($params,'as_url') )
- {
- $ret = str_replace( FULL_PATH.'/', $this->Application->BaseURL(), $ret);
+ if ( getArrayValue($params, 'as_url') ) {
+ $ret = str_replace(FULL_PATH . '/', $this->Application->BaseURL(), $ret);
}
+
return $ret;
}
/**
* Returns true if system has more then 1 language installed
*
* @param Array $params
* @return bool
*/
function IsMultiLanguage($params)
{
return $this->TotalRecords($params) > 1;
}
function IsPrimaryLanguage($params)
{
return $this->Application->GetDefaultLanguageId() == $this->Application->GetVar('m_lang');
}
/* function Main_IsMetricUnits($params)
{
$object =& $this->Application->recallObject($this->Prefix.'.current');
$measure_system = $object->GetDBField('UnitSystem');
return $measure_system == 1 ? 1 : 0;
}*/
}
\ No newline at end of file
Index: branches/5.2.x/core/units/languages/languages_event_handler.php
===================================================================
--- branches/5.2.x/core/units/languages/languages_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/languages/languages_event_handler.php (revision 14628)
@@ -1,627 +1,649 @@
<?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 LanguagesEventHandler extends kDBEventHandler
{
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnChangeLanguage' => Array('self' => true),
'OnSetPrimary' => Array('self' => 'advanced:set_primary|add|edit'),
'OnImportLanguage' => Array('self' => 'advanced:import'),
'OnExportLanguage' => Array('self' => 'advanced:export'),
'OnExportProgress' => Array('self' => 'advanced:export'),
'OnReflectMultiLingualFields' => Array ('self' => 'view'),
'OnSynchronizeLanguages' => Array ('self' => 'edit'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
- * Permission check override
+ * Checks user permission to execute given $event
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
if ($event->Name == 'OnItemBuild') {
// check permission without using $event->getSection(),
// so first cache rebuild won't lead to "ldefault_Name" field being used
return true;
}
return parent::CheckPermission($event);
}
/**
* Allows to get primary language object
*
* @param kEvent $event
*/
function getPassedID(&$event)
{
if ($event->Special == 'primary') {
return $this->Application->GetDefaultLanguageId();
}
return parent::getPassedID($event);
}
/**
* [HOOK] Updates table structure on new language adding/removing language
*
* @param kEvent $event
*/
function OnReflectMultiLingualFields(&$event)
{
if ($this->Application->GetVar('ajax') == 'yes') {
$event->status = kEvent::erSTOP;
}
if (is_object($event->MasterEvent)) {
if ($event->MasterEvent->status != kEvent::erSUCCESS) {
// only rebuild when all fields are validated
return ;
}
if (($event->MasterEvent->Name == 'OnSave') && !$this->Application->GetVar('new_language')) {
// only rebuild during new language adding
return ;
}
}
$ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
/* @var $ml_helper kMultiLanguageHelper */
$this->Application->UnitConfigReader->ReReadConfigs();
foreach ($this->Application->UnitConfigReader->configData as $prefix => $config_data) {
$ml_helper->createFields($prefix);
}
}
/**
* Allows to set selected language as primary
*
* @param kEvent $event
*/
function OnSetPrimary(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return;
}
$this->StoreSelectedIDs($event);
$ids = $this->getSelectedIDs($event);
if ($ids) {
$id = array_shift($ids);
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object LanguagesItem */
$object->Load($id);
$object->copyMissingData( $object->setPrimary() );
}
}
/**
* [HOOK] Reset primary status of other languages if we are saving primary language
*
* @param kEvent $event
*/
function OnUpdatePrimary(&$event)
{
if ($event->MasterEvent->status != kEvent::erSUCCESS) {
return ;
}
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object LanguagesItem */
$object->SwitchToLive();
// set primary for each languages, that have this checkbox checked
$ids = explode(',', $event->MasterEvent->getEventParam('ids'));
foreach ($ids as $id) {
$object->Load($id);
if ($object->GetDBField('PrimaryLang')) {
$object->copyMissingData( $object->setPrimary(true, false) );
}
if ($object->GetDBField('AdminInterfaceLang')) {
$object->setPrimary(true, true);
}
}
// if no primary language left, then set primary last language (not to load again) from edited list
$sql = 'SELECT '.$object->IDField.'
FROM '.$object->TableName.'
WHERE PrimaryLang = 1';
$primary_language = $this->Conn->GetOne($sql);
if (!$primary_language) {
$object->setPrimary(false, false); // set primary language
}
$sql = 'SELECT '.$object->IDField.'
FROM '.$object->TableName.'
WHERE AdminInterfaceLang = 1';
$primary_language = $this->Conn->GetOne($sql);
if (!$primary_language) {
$object->setPrimary(false, true); // set admin interface language
}
}
/**
* Prefills options with dynamic values
*
* @param kEvent $event
*/
function OnAfterConfigRead(&$event)
{
parent::OnAfterConfigRead($event);
$fields = $this->Application->getUnitOption($event->Prefix, 'Fields');
// set dynamic hints for options in date format fields
$options = $fields['InputDateFormat']['options'];
if ($options) {
foreach ($options as $i => $v) {
$options[$i] = $v . ' (' . adodb_date($i) . ')';
}
$fields['InputDateFormat']['options'] = $options;
}
$options = $fields['InputTimeFormat']['options'];
if ($options) {
foreach ($options as $i => $v) {
$options[$i] = $v . ' (' . adodb_date($i) . ')';
}
$fields['InputTimeFormat']['options'] = $options;
}
$this->Application->setUnitOption($event->Prefix, 'Fields', $fields);
}
/**
- * Occurse before updating item
+ * Occurs before updating item
*
* @param kEvent $event
- * @access public
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$status_fields = $this->Application->getUnitOption($event->Prefix, 'StatusField');
$status_field = array_shift($status_fields);
- if ($object->GetDBField('PrimaryLang') == 1 && $object->GetDBField($status_field) == 0) {
+ if ( $object->GetDBField('PrimaryLang') == 1 && $object->GetDBField($status_field) == 0 ) {
$object->SetDBField($status_field, 1);
}
}
/**
* Shows only enabled languages on front
*
* @param kEvent $event
+ * @return void
+ * @access protected
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
/* @var $object kDBList */
if (in_array($event->Special, Array ('enabled', 'selected', 'available'))) {
$object->addFilter('enabled_filter', '%1$s.Enabled = ' . STATUS_ACTIVE);
}
// site domain language picker
if ($event->Special == 'selected' || $event->Special == 'available') {
$edit_picker_helper =& $this->Application->recallObject('EditPickerHelper');
/* @var $edit_picker_helper EditPickerHelper */
$edit_picker_helper->applyFilter($event, 'Languages');
}
// apply domain-based language filtering
$languages = $this->Application->siteDomainField('Languages');
if (strlen($languages)) {
$languages = explode('|', substr($languages, 1, -1));
$object->addFilter('domain_filter', '%1$s.LanguageId IN (' . implode(',', $languages) . ')');
}
}
/**
* Copy labels from another language
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemCreate(&$event)
+ protected function OnAfterItemCreate(&$event)
{
parent::OnAfterItemCreate($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$src_language = $object->GetDBField('CopyFromLanguage');
- if ($object->GetDBField('CopyLabels') && $src_language) {
+ if ( $object->GetDBField('CopyLabels') && $src_language ) {
$dst_language = $object->GetID();
// 1. schedule data copy after OnSave event is executed
$var_name = $event->getPrefixSpecial() . '_copy_data' . $this->Application->GetVar('m_wid');
$pending_actions = $this->Application->RecallVar($var_name, Array ());
- if ($pending_actions) {
+ if ( $pending_actions ) {
$pending_actions = unserialize($pending_actions);
}
$pending_actions[$src_language] = $dst_language;
$this->Application->StoreVar($var_name, serialize($pending_actions));
$object->SetDBField('CopyLabels', 0);
}
}
/**
* Saves language from temp table to live
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnSave(&$event)
+ protected function OnSave(&$event)
{
parent::OnSave($event);
- if ($event->status != kEvent::erSUCCESS) {
- return ;
+ if ( $event->status != kEvent::erSUCCESS ) {
+ return;
}
$var_name = $event->getPrefixSpecial() . '_copy_data' . $this->Application->GetVar('m_wid');
$pending_actions = $this->Application->RecallVar($var_name, Array ());
- if ($pending_actions) {
+ if ( $pending_actions ) {
$pending_actions = unserialize($pending_actions);
}
// create multilingual columns for phrases & email events table first (actual for 6+ language)
$ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
+ /* @var $ml_helper kMultiLanguageHelper */
+
$ml_helper->createFields('phrases');
$ml_helper->createFields('emailevents');
foreach ($pending_actions as $src_language => $dst_language) {
// phrases import
$sql = 'UPDATE ' . $this->Application->getUnitOption('phrases', 'TableName') . '
SET l' . $dst_language . '_Translation = l' . $src_language . '_Translation';
$this->Conn->Query($sql);
// events import
$sql = 'UPDATE ' . $this->Application->getUnitOption('emailevents', 'TableName') . '
SET
l' . $dst_language . '_Subject = l' . $src_language . '_Subject,
l' . $dst_language . '_Body = l' . $src_language . '_Body';
$this->Conn->Query($sql);
}
$this->Application->RemoveVar($var_name);
$event->CallSubEvent('OnReflectMultiLingualFields');
$event->CallSubEvent('OnUpdatePrimary');
}
/**
* Prepare temp tables for creating new item
* but does not create it. Actual create is
* done in OnPreSaveCreated
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnPreCreate(&$event)
+ protected function OnPreCreate(&$event)
{
parent::OnPreCreate($event);
$object =& $event->getObject();
+ /* @var $object kDBItem */
+
$object->SetDBField('CopyLabels', 1);
- $live_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
- $primary_lang_id = $this->Conn->GetOne('SELECT '.$object->IDField.' FROM '.$live_table.' WHERE PrimaryLang = 1');
+ $sql = 'SELECT ' . $object->IDField . '
+ FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
+ WHERE PrimaryLang = 1';
+ $primary_lang_id = $this->Conn->GetOne($sql);
$object->SetDBField('CopyFromLanguage', $primary_lang_id);
}
/**
* Sets new language mark
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeDeleteFromLive(&$event)
+ protected function OnBeforeDeleteFromLive(&$event)
{
+ parent::OnBeforeDeleteFromLive($event);
+
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
- $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT ' . $id_field . '
- FROM ' . $table_name . '
+ FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
WHERE ' . $id_field . ' = ' . $event->getEventParam('id');
$id = $this->Conn->GetOne($sql);
- if (!$id) {
+ if ( !$id ) {
$this->Application->SetVar('new_language', 1);
}
}
function OnChangeLanguage(&$event)
{
$language_id = $this->Application->GetVar('language');
if ($this->Application->isAdmin) {
// admin data only
$this->Application->SetVar('m_lang', $language_id);
// set new language for this session (admin interface only)
$this->Application->Session->SetField('Language', $language_id);
// remember last user language in administrative console
if ($this->Application->RecallVar('user_id') == USER_ROOT) {
$this->Application->StorePersistentVar('AdminLanguage', $language_id);
}
else {
$object =& $this->Application->recallObject('u.current');
/* @var $object kDBItem */
$object->SetDBField('AdminLanguage', $language_id);
$object->Update();
}
// without this language change in admin will cause erase of last remembered tree section
$this->Application->SetVar('skip_last_template', 1);
}
else {
// changing language on Front-End
$this->Application->SetVar('m_lang', $language_id);
if (MOD_REWRITE) {
$mod_rewrite_helper =& $this->Application->recallObject('ModRewriteHelper');
/* @var $mod_rewrite_helper kModRewriteHelper */
$mod_rewrite_helper->removePages();
}
}
}
/**
* Parse language XML file into temp tables and redirect to progress bar screen
*
* @param kEvent $event
*/
function OnImportLanguage(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return;
}
$items_info = $this->Application->GetVar('phrases_import');
if ($items_info) {
list ($id, $field_values) = each($items_info);
$object =& $this->Application->recallObject('phrases.import', 'phrases', Array('skip_autoload' => true));
/* @var $object kDBItem */
$object->setID($id);
$object->SetFieldsFromHash($field_values);
if (!$object->Validate()) {
$event->status = kEvent::erFAIL;
return ;
}
$filename = $object->GetField('LangFile', 'full_path');
if (!filesize($filename)) {
$object->SetError('LangFile', 'la_empty_file', 'la_EmptyFile');
$event->status = kEvent::erFAIL;
}
$language_import_helper =& $this->Application->recallObject('LanguageImportHelper');
/* @var $language_import_helper LanguageImportHelper */
$language_import_helper->performImport(
$filename,
$object->GetDBField('PhraseType'),
$object->GetDBField('Module'),
$object->GetDBField('ImportOverwrite') ? LANG_OVERWRITE_EXISTING : LANG_SKIP_EXISTING
);
// delete uploaded language pack after import is finished
unlink($filename);
$event->SetRedirectParam('opener', 'u');
}
}
/**
* Stores ids of selected languages and redirects to export language step 1
*
* @param kEvent $event
*/
function OnExportLanguage(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return;
}
$this->Application->setUnitOption('phrases','AutoLoad',false);
$this->StoreSelectedIDs($event);
$this->Application->StoreVar('export_language_ids', implode(',', $this->getSelectedIDs($event)) );
$event->setRedirectParams( Array('phrases.export_event' => 'OnNew', 'pass' => 'all,phrases.export') );
}
/**
* Saves selected languages to xml file passed
*
* @param kEvent $event
*/
function OnExportProgress(&$event)
{
$items_info = $this->Application->GetVar('phrases_export');
if ($items_info) {
list($id, $field_values) = each($items_info);
$object =& $this->Application->recallObject('phrases.export', null, Array('skip_autoload' => true));
/* @var $object kDBItem */
$object->setID($id);
$object->SetFieldsFromHash($field_values);
if (!$object->Validate()) {
$event->status = kEvent::erFAIL;
return ;
}
$file_helper =& $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
$file_helper->CheckFolder(EXPORT_PATH);
if (!is_writable(EXPORT_PATH)) {
$event->status = kEvent::erFAIL;
$object->SetError('LangFile', 'write_error', 'la_ExportFolderNotWritable');
return ;
}
if ( substr($field_values['LangFile'], -5) != '.lang') {
$field_values['LangFile'] .= '.lang';
}
$filename = EXPORT_PATH . '/' . $field_values['LangFile'];
$language_import_helper =& $this->Application->recallObject('LanguageImportHelper');
/* @var $language_import_helper LanguageImportHelper */
if ($object->GetDBField('DoNotEncode')) {
$language_import_helper->setExportEncoding('plain');
}
$language_import_helper->setExportLimits($field_values['ExportPhrases'], $field_values['ExportEmailEvents']);
$lang_ids = explode(',', $this->Application->RecallVar('export_language_ids') );
$language_import_helper->performExport($filename, $field_values['PhraseType'], $lang_ids, $field_values['Module']);
}
$event->redirect = 'regional/languages_export_step2';
$event->SetRedirectParam('export_file', $field_values['LangFile']);
}
/**
* Returns to previous template in opener stack
*
* @param kEvent $event
*/
function OnGoBack(&$event)
{
$event->SetRedirectParam('opener', 'u');
}
function OnScheduleTopFrameReload(&$event)
{
$this->Application->StoreVar('RefreshTopFrame',1);
}
/**
* Do now allow deleting current language
*
* @param kEvent $event
*/
function OnBeforeItemDelete(&$event)
{
- $del_id = $event->getEventParam('id');
- $object =& $event->getObject(array('skip_autload' => true));
- $object->Load($del_id);
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
- if ($object->GetDBField('PrimaryLang') || $object->GetDBField('AdminInterfaceLang') || $del_id == $this->Application->GetVar('m_lang')) {
+ if ( $object->GetDBField('PrimaryLang') || $object->GetDBField('AdminInterfaceLang') || $object->GetID() == $this->Application->GetVar('m_lang') ) {
$event->status = kEvent::erFAIL;
}
}
/**
* Deletes phrases and email events on given language
*
* @param kEvent $event
*/
function OnAfterItemDelete(&$event)
{
parent::OnAfterItemDelete($event);
$object =& $event->getObject();
/* @var $object kDBItem */
// clean Events table
$fields_hash = Array (
'l' . $object->GetID() . '_Subject' => NULL,
'l' . $object->GetID() . '_Body' => NULL,
);
$this->Conn->doUpdate($fields_hash, $this->Application->getUnitOption('emailevents', 'TableName'), 1);
// clean Phrases table
$fields_hash = Array (
'l' . $object->GetID() . '_Translation' => NULL,
);
$this->Conn->doUpdate($fields_hash, $this->Application->getUnitOption('phrases', 'TableName'), 1);
}
/**
* Copy missing phrases across all system languages (starting from primary)
*
* @param kEvent $event
*/
function OnSynchronizeLanguages(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return;
}
// get language list with primary language first
$sql = 'SELECT LanguageId
FROM ' . TABLE_PREFIX . 'Language
ORDER BY PrimaryLang DESC';
$source_langs = $this->Conn->GetCol($sql);
$target_langs = $source_langs;
foreach ($source_langs as $source_id) {
foreach ($target_langs as $target_id) {
if ($source_id == $target_id) {
continue;
}
$sql = 'UPDATE ' . TABLE_PREFIX . 'Phrase
SET l' . $target_id . '_Translation = l' . $source_id . '_Translation
WHERE (l' . $target_id . '_Translation IS NULL) OR (l' . $target_id . '_Translation = "")';
$this->Conn->Query($sql);
}
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/permissions/permissions_event_handler.php
===================================================================
--- branches/5.2.x/core/units/permissions/permissions_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/permissions/permissions_event_handler.php (revision 14628)
@@ -1,248 +1,257 @@
<?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 PermissionsEventHandler extends kDBEventHandler {
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnGroupSavePermissions' => Array('subitem' => 'advanced:manage_permissions'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Save category permissions
*
* @param kEvent $event
*/
function OnCategorySavePermissions(&$event)
{
$group_id = $this->Application->GetVar('current_group_id');
$category_id = $this->Application->GetVar('c_id');
$permissions = $this->Application->GetVar($event->getPrefixSpecial(true));
if (isset($permissions[$group_id])) {
$permissions = $permissions[$group_id];
$object =& $event->getObject( Array('skip_autoload' => true) );
$permissions_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $permissions_helper kPermissionsHelper */
$permissions_helper->LoadPermissions($group_id, $category_id, 0, 'c');
// format: <perm_name>['inherited'] || <perm_name>['value']
$delete_ids = Array();
$create_sql = Array();
$update_sql = Array();
$create_mask = '(%s,%s,'.$group_id.',%s,0,'.$category_id.')';
$new_id = (int)$this->Conn->GetOne('SELECT MIN('.$object->IDField.') FROM '.$object->TableName);
if($new_id > 0) $new_id = 0;
--$new_id;
foreach ($permissions as $perm_name => $perm_data) {
$inherited = $perm_data['inherited'];
$perm_value = isset($perm_data['value']) ? $perm_data['value'] : false;
$perm_id = $permissions_helper->getPermissionID($perm_name);
if ($inherited && ($perm_id != 0)) {
// permission become inherited (+ direct value was set before) => DELETE
$delete_ids[] = $permissions_helper->getPermissionID($perm_name);
}
if (!$inherited) {
// not inherited
if (($perm_id != 0) && ($perm_value != $permissions_helper->getPermissionValue($perm_name))) {
// record was found in db & new value differs from old one => UPDATE
$update_sql[$perm_id] = ' UPDATE '.$object->TableName.'
SET PermissionValue = '.$perm_value.'
WHERE (PermissionId = '.$perm_id.')';
}
if ($perm_id == 0) {
// not found in db, but set directly => INSERT
$create_sql[] = sprintf($create_mask, $new_id--, $this->Conn->qstr($perm_name), $this->Conn->qstr($perm_value));
}
}
// permission state was not changed in all other cases
}
$this->UpdatePermissions($event, $create_sql, $update_sql, $delete_ids);
}
$event->MasterEvent->SetRedirectParam('item_prefix', $this->Application->GetVar('item_prefix'));
$event->MasterEvent->SetRedirectParam('group_id', $this->Application->GetVar('group_id'));
}
/**
* Saves permissions while editing group
*
* @param kEvent $event
+ *
+ * @return void
+ * @access protected
*/
- function OnGroupSavePermissions(&$event)
+ protected function OnGroupSavePermissions(&$event)
{
- if (!$this->Application->CheckPermission('in-portal:user_groups.advanced:manage_permissions', 1)) {
+ if ( !$this->Application->CheckPermission('in-portal:user_groups.advanced:manage_permissions', 1) ) {
// no permission to save permissions
- return false;
+ return ;
}
$permissions = $this->Application->GetVar($event->getPrefixSpecial(true));
- if (!$permissions) {
- return false;
+ if ( !$permissions ) {
+ return ;
}
- $object =& $event->getObject( Array('skip_autoload' => true) );
+ $object =& $event->getObject( Array ('skip_autoload' => true) );
+ /* @var $object kDBItem */
+
$group_id = $this->Application->GetVar('g_id');
$permissions_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $permissions_helper kPermissionsHelper */
$permissions_helper->LoadPermissions($group_id, 0, 1, 'g');
- $delete_ids = Array();
- $create_sql = Array();
- $create_mask = '(%s,%s,'.$group_id.',%s,1,0)';
+ $delete_ids = $create_sql = Array ();
+ $create_mask = '(%s,%s,' . $group_id . ',%s,1,0)';
- $new_id = (int)$this->Conn->GetOne('SELECT MIN('.$object->IDField.') FROM '.$object->TableName);
- if($new_id > 0) $new_id = 0;
+ $new_id = (int)$this->Conn->GetOne('SELECT MIN(' . $object->IDField . ') FROM ' . $object->TableName);
+ if ( $new_id > 0 ) {
+ $new_id = 0;
+ }
--$new_id;
$sections_helper =& $this->Application->recallObject('SectionsHelper');
+ /* @var $sections_helper kSectionsHelper */
+
foreach ($permissions as $section_name => $section_permissions) {
$section_data =& $sections_helper->getSectionData($section_name);
- if ($section_data && isset($section_data['perm_prefix'])) {
+ if ( $section_data && isset($section_data['perm_prefix']) ) {
// using permission from other prefix
- $section_name = $this->Application->getUnitOption($section_data['perm_prefix'].'.main', 'PermSection');
+ $section_name = $this->Application->getUnitOption($section_data['perm_prefix'] . '.main', 'PermSection');
}
foreach ($section_permissions as $perm_name => $perm_value) {
-
- if (!$permissions_helper->isOldPermission($section_name, $perm_name)) {
- $perm_name = $section_name.'.'.$perm_name;
+ if ( !$permissions_helper->isOldPermission($section_name, $perm_name) ) {
+ $perm_name = $section_name . '.' . $perm_name;
}
$db_perm_value = $permissions_helper->getPermissionValue($perm_name);
- if ($db_perm_value == 1 && $perm_value == 0) {
+
+ if ( $db_perm_value == 1 && $perm_value == 0 ) {
// permission was disabled => delete it's record
$delete_ids[] = $permissions_helper->getPermissionID($perm_name);
}
- elseif ($db_perm_value == 0 && $perm_value == 1) {
+ elseif ( $db_perm_value == 0 && $perm_value == 1 ) {
// permission was enabled => created it's record
$create_sql[$perm_name] = sprintf($create_mask, $new_id--, $this->Conn->qstr($perm_name), $this->Conn->qstr($perm_value));
}
// permission state was not changed in all other cases
}
}
- $this->UpdatePermissions($event, $create_sql, Array(), $delete_ids);
+ $this->UpdatePermissions($event, $create_sql, Array (), $delete_ids);
- if ($this->Application->GetVar('advanced_save') == 1) {
+ if ( $this->Application->GetVar('advanced_save') == 1 ) {
// advanced permission popup [save button]
$this->finalizePopup($event);
// $event->redirect = 'incs/just_close';
}
- elseif ($this->Application->GetVar('section_name') != '') {
+ elseif ( $this->Application->GetVar('section_name') != '' ) {
// save simple permissions before opening advanced permission popup
$event->redirect = false;
}
-
}
/**
* Apply modification sqls to permissions table
*
* @param kEvent $event
* @param Array $create_sql
* @param Array $update_sql
* @param Array $delete_ids
*/
function UpdatePermissions(&$event, $create_sql, $update_sql, $delete_ids)
{
$object =& $event->getObject();
/* @var $object kDBItem */
if ($delete_ids) {
$action = ChangeLog::DELETE;
$object->Load($delete_ids[count($delete_ids) - 1]);
$delete_sql = ' DELETE FROM '.$object->TableName.'
WHERE '.$object->IDField.' IN ('.implode(',', $delete_ids).')';
$this->Conn->Query($delete_sql);
}
if ($create_sql) {
$create_sql = ' INSERT INTO '.$object->TableName.'
VALUES '.implode(',', $create_sql);
$this->Conn->Query($create_sql);
$sql = 'SELECT MIN(' . $object->IDField . ')
FROM ' . $object->TableName;
$id = $this->Conn->GetOne($sql);
$action = ChangeLog::CREATE;
$object->Load($id);
}
if ($update_sql) {
foreach ($update_sql as $id => $sql) {
$this->Conn->Query($sql);
}
$action = ChangeLog::UPDATE;
$object->Load($id);
$object->SetDBField('PermissionValue', $object->GetDBField('PermissionValue') ? 0 : 1);
}
if ($delete_ids || $create_sql || $update_sql) {
$object->setModifiedFlag($action);
if ($event->Name == 'OnCategorySavePermissions') {
$this->Application->StoreVar('PermCache_UpdateRequired', 1);
}
}
}
/**
* Don't delete permissions from live table in case of new category creation.
* Called as much times as permission count for categories set, so don't
* perform any sql queries here!
*
* @param kEvent $event
*/
function OnBeforeDeleteFromLive(&$event)
{
- if ($event->Prefix == 'c-perm') {
+ if ( $event->Prefix == 'c-perm' ) {
// only when saving category permissions, not group permissions
$foreign_keys = $event->getEventParam('foreign_key');
- if ((count($foreign_keys) == 1) && ($foreign_keys[0] == 0)) {
+ if ( (count($foreign_keys) == 1) && ($foreign_keys[0] == 0) ) {
// parent item has zero id
$temp_object =& $this->Application->recallObject('c');
- if ($temp_object->isLoaded()) {
+ /* @var $temp_object CategoriesItem */
+
+ if ( $temp_object->isLoaded() ) {
// category with id = 0 found in temp table
$event->status = kEvent::erFAIL;
}
}
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/permissions/permissions_tag_processor.php
===================================================================
--- branches/5.2.x/core/units/permissions/permissions_tag_processor.php (revision 14627)
+++ branches/5.2.x/core/units/permissions/permissions_tag_processor.php (revision 14628)
@@ -1,219 +1,231 @@
<?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 PermissionsTagProcessor extends kDBTagProcessor {
function HasPermission($params)
{
$section_name = $params['section_name'];
$sections_helper =& $this->Application->recallObject('SectionsHelper');
+ /* @var $sections_helper kSectionsHelper */
+
$section_data =& $sections_helper->getSectionData($section_name);
return array_search($params['perm_name'], $section_data['permissions']) !== false;
}
function HasAdvancedPermissions($params)
{
$section_name = $params['section_name'];
$sections_helper =& $this->Application->recallObject('SectionsHelper');
+ /* @var $sections_helper kSectionsHelper */
+
$section_data =& $sections_helper->getSectionData($section_name);
$ret = false;
foreach ($section_data['permissions'] as $perm_name) {
if (preg_match('/^advanced:(.*)/', $perm_name)) {
$ret = true;
break;
}
}
return $ret;
}
function PermissionValue($params)
{
$section_name = $params['section_name'];
$perm_name = $params['perm_name'];
$sections_helper =& $this->Application->recallObject('SectionsHelper');
+ /* @var $sections_helper kSectionsHelper */
+
$section_data =& $sections_helper->getSectionData($section_name);
if ($section_data && isset($section_data['perm_prefix'])) {
// using permission from other prefix
$section_name = $this->Application->getUnitOption($section_data['perm_prefix'].'.main', 'PermSection');
}
$permissions_helper =& $this->Application->recallObject('PermissionsHelper');
+ /* @var $permissions_helper kPermissionsHelper */
+
if (!$permissions_helper->isOldPermission($section_name, $perm_name)) {
$perm_name = $section_name.'.'.$perm_name;
}
return $permissions_helper->getPermissionValue($perm_name);
}
function LoadPermissions($params)
{
$permissions_helper =& $this->Application->recallObject('PermissionsHelper');
$prefix_parts = explode('-', $this->Prefix, 2);
/* @var $permissions_helper kPermissionsHelper */
$permissions_helper->LoadPermissions($this->Application->GetVar('g_id'), 0, 1, 'g');
}
function LevelIndicator($params)
{
return $params['level'] * $params['multiply'];
}
function PrintPermissions($params)
{
$category =& $this->Application->recallObject('c');
/* @var $category kDBItem */
$group_id = $this->Application->GetVar('group_id');
$prefix = $this->Application->GetVar('item_prefix');
$module = $this->Application->findModule('Var', $prefix, 'Name');
$perm_live_table = $this->Application->getUnitOption('c-perm', 'TableName');
$perm_temp_table = $this->Application->GetTempName($perm_live_table, 'prefix:'.$this->Prefix);
if ($category->GetID() == 0) {
$categories = Array(0);
}
else {
$categories = explode('|', substr($category->GetDBField('ParentPath'), 1, -1));
}
if (count($categories) == 1 || $category->GetID() == 0) {
// category located in root category ("Home") => then add it to path virtually
array_unshift($categories, 0);
}
$this_cat = array_pop($categories);
// get permission name + category position in parent path that has value set for that permission
$case = 'MAX(CASE p.CatId';
foreach ($categories as $pos => $cat_id) {
$case .= ' WHEN '.$cat_id.' THEN '.$pos;
}
$case .= ' END) AS InheritedPosition';
$sql = 'SELECT '.$case.', p.Permission AS Perm
FROM '.$perm_live_table.' p
LEFT JOIN '.TABLE_PREFIX.'PermissionConfig pc ON pc.PermissionName = p.Permission
WHERE
p.CatId IN ('.implode(',', $categories).') AND
pc.ModuleId = ' . $this->Conn->qstr($module) . ' AND
(
(p.GroupId = ' . (int)$group_id . ' AND p.Type = 0)
)
GROUP BY Perm';
$perm_positions = $this->Conn->GetCol($sql, 'Perm');
$pos_sql = '';
foreach ($perm_positions as $perm_name => $category_pos) {
$pos_sql .= '(#TABLE_PREFIX#.Permission = "'.$perm_name.'" AND #TABLE_PREFIX#.CatId = '.$categories[$category_pos].') OR ';
}
$pos_sql = $pos_sql ? substr($pos_sql, 0, -4) : '0';
// get all permissions list with iheritence status, inherited category id and permission value
$sql = 'SELECT pc.PermissionName,
pc.Description,
IF (tmp_p.PermissionValue IS NULL AND p.PermissionValue IS NULL,
0,
IF (tmp_p.PermissionValue IS NOT NULL, tmp_p.PermissionValue, p.PermissionValue)
) AS Value,
IF (tmp_p.CatId IS NOT NULL, tmp_p.CatId, IF(p.CatId IS NOT NULL, p.CatId, 0) ) AS InheritedFrom,
IF(tmp_p.CatId = '.$category->GetID().', 0, 1) AS Inherited,
IF(p.PermissionValue IS NOT NULL, p.PermissionValue, 0) AS InheritedValue
FROM '.TABLE_PREFIX.'PermissionConfig pc
LEFT JOIN '.$perm_live_table.' p
ON (p.Permission = pc.PermissionName) AND ('.str_replace('#TABLE_PREFIX#', 'p', $pos_sql).') AND (p.GroupId = '.(int)$group_id.')
LEFT JOIN '.$perm_temp_table.' tmp_p
ON (tmp_p.Permission = pc.PermissionName) AND (tmp_p.CatId = '.$this_cat.') AND (tmp_p.GroupId = '.$group_id.')
WHERE ModuleId = "'.$module.'"';
$permissions = $this->Conn->Query($sql);
$ret = '';
$block_params = $this->prepareTagParams($params);
$block_params['name'] = $params['render_as'];
foreach ($permissions as $perm_record) {
$block_params = array_merge($block_params, $perm_record);
$ret .= $this->Application->ParseBlock($block_params);
}
return $ret;
}
/**
* Print module tab for each module
*
* @param Array $params
* @return string
*/
function PrintTabs($params)
{
$ret = '';
$block_params = $params;
foreach ($this->Application->ModuleInfo as $module_name => $module_data) {
if (!$this->Application->prefixRegistred($module_data['Var']) || !$this->Application->getUnitOption($module_data['Var'], 'CatalogItem')) continue;
$params['item_prefix'] = $module_data['Var'];
$ret .= $this->Application->IncludeTemplate($params);
}
return $ret;
}
/**
* Returns category name by ID
*
* @param Array $params
+ * @return string
+ * @access protected
*/
- function CategoryPath($params)
+ protected function CategoryPath($params)
{
$category_id = $params['cat_id'];
$cache_key = 'category_paths[%CIDSerial:' . $category_id . '%][%PhrasesSerial%][Adm:' . (int)$this->Application->isAdmin . ']';
$category_path = $this->Application->getCache($cache_key);
- if ($category_path === false) {
- // not chached
- if ($category_id > 0) {
+ if ( $category_path === false ) {
+ // not cached
+ if ( $category_id > 0 ) {
$id_field = $this->Application->getUnitOption('c', 'IDField');
$table_name = $this->Application->getUnitOption('c', 'TableName');
+
$ml_formatter =& $this->Application->recallObject('kMultiLanguage');
+ /* @var $ml_formatter kMultiLanguage */
- $sql = 'SELECT '.$ml_formatter->LangFieldName('CachedNavbar').'
- FROM '.$table_name.'
- WHERE '.$id_field.' = '.$category_id;
+ $sql = 'SELECT ' . $ml_formatter->LangFieldName('CachedNavbar') . '
+ FROM ' . $table_name . '
+ WHERE ' . $id_field . ' = ' . $category_id;
$cached_navbar = preg_replace('/^Content(&\|&){0,1}/i', '', $this->Conn->GetOne($sql));
- $category_path = trim($this->CategoryPath( Array('cat_id' => 0) ).' > '.str_replace('&|&', ' > ', $cached_navbar), ' > ');
+ $category_path = trim($this->CategoryPath(Array ('cat_id' => 0)) . ' > ' . str_replace('&|&', ' > ', $cached_navbar), ' > ');
}
else {
$category_path = $this->Application->Phrase(($this->Application->isAdmin ? 'la_' : 'lu_') . 'rootcategory_name');
}
$this->Application->setCache($cache_key, $category_path);
}
return $category_path;
}
function PermInputName($params)
{
return $this->Prefix.'['.$this->Application->GetVar('group_id').']['.$this->Application->Parser->GetParam('PermissionName').']['.$params['sub_key'].']';
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/email_events/email_events_event_handler.php
===================================================================
--- branches/5.2.x/core/units/email_events/email_events_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/email_events/email_events_event_handler.php (revision 14628)
@@ -1,1122 +1,1136 @@
<?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 EmailEventsEventsHandler extends kDBEventHandler
{
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnFrontOnly' => Array ('self' => 'edit'),
'OnSaveSelected' => Array ('self' => 'view'),
'OnProcessEmailQueue' => Array ('self' => 'add|edit'),
'OnSuggestAddress' => Array ('self' => 'add|edit'),
// events only for developers
'OnPreCreate' => Array ('self' => 'debug'),
'OnDelete' => Array ('self' => 'debug'),
'OnDeleteAll' => Array ('self' => 'debug'),
'OnMassDelete' => Array ('self' => 'debug'),
'OnMassApprove' => Array ('self' => 'debug'),
'OnMassDecline' => Array ('self' => 'debug'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Changes permission section to one from REQUEST, not from config
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
$module = $this->Application->GetVar('module');
if (strlen($module) > 0) {
// checking permission when lising module email events in separate section
$module = explode(':', $module, 2);
if (count($module) == 1) {
$main_prefix = $this->Application->findModule('Name', $module[0], 'Var');
}
else {
$exceptions = Array('Category' => 'c', 'Users' => 'u');
$main_prefix = $exceptions[ $module[1] ];
}
$section = $this->Application->getUnitOption($main_prefix.'.email', 'PermSection');
$event->setEventParam('PermSection', $section);
}
// checking permission when listing all email events when editing language
return parent::CheckPermission($event);
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
+ * @return void
* @access protected
- * @see OnListBuild
+ * @see kDBEventHandler::OnListBuild()
*/
- function SetCustomQuery(&$event)
+ protected function SetCustomQuery(&$event)
{
$object =& $event->getObject();
/* @var $object kDBList */
if ($event->Special == 'module') {
$module = $this->Application->GetVar('module');
$object->addFilter('module_filter', '%1$s.Module = '.$this->Conn->qstr($module));
}
if (!$event->Special && !$this->Application->isDebugMode()) {
// no special
$object->addFilter('enabled_filter', '%1$s.Enabled <> ' . STATUS_DISABLED);
}
}
/**
- * Sets event id
+ * Set default headers
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnPreCreate(&$event)
+ protected function OnPreCreate(&$event)
{
parent::OnPreCreate($event);
$object =& $event->getObject();
/* @var $object kDBItem */
$object->SetDBField('Headers', $this->Application->ConfigValue('Smtp_DefaultHeaders'));
}
/**
* Sets status Front-End Only to selected email events
*
* @param kEvent $event
*/
function OnFrontOnly(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return ;
}
$ids = implode(',', $this->StoreSelectedIDs($event));
$table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'UPDATE '.$table_name.'
SET FrontEndOnly = 1
WHERE EventId IN ('.$ids.')';
$this->Conn->Query($sql);
$this->clearSelectedIDs($event);
}
/**
* Sets selected user to email events selected
*
* @param kEvent $event
*/
function OnSelectUser(&$event)
{
if ($event->Special != 'module') {
parent::OnSelectUser($event);
return ;
}
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$event->status = kEvent::erFAIL;
return ;
}
$items_info = $this->Application->GetVar('u');
if ($items_info) {
$user_id = array_shift( array_keys($items_info) );
$selected_ids = $this->getSelectedIDs($event, true);
$ids = $this->Application->RecallVar($event->getPrefixSpecial().'_selected_ids');
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'UPDATE '.$table_name.'
SET '.$this->Application->RecallVar('dst_field').' = '.$user_id.'
WHERE '.$id_field.' IN ('.$ids.')';
$this->Conn->Query($sql);
}
$this->finalizePopup($event);
}
/**
* Saves selected ids to session
*
* @param kEvent $event
*/
function OnSaveSelected(&$event)
{
$this->StoreSelectedIDs($event);
}
/**
* Returns email event object based on given kEvent object
*
* @param kEvent $event
* @return kDBItem
*/
function &_getEmailEvent(&$event)
{
$false = false;
$name = $event->getEventParam('EmailEventName');
$type = $event->getEventParam('EmailEventType');
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
if (!$object->isLoaded() || ($object->GetDBField('Event') != $name || $object->GetDBField('Type') != $type)) {
// get event parameters by name & type
$load_keys = Array ('Event' => $name, 'Type' => $type);
$object->Load($load_keys);
if (!$object->isLoaded() || ($object->GetDBField('Enabled') == STATUS_DISABLED)) {
// event record not found OR is disabled
return $false;
}
if ($object->GetDBField('FrontEndOnly') && $this->Application->isAdmin) {
return $false;
}
}
return $object;
}
/**
* Processes email sender
*
* @param kEvent $event
* @param Array $direct_params
*/
function _processSender(&$event, $direct_params = Array ())
{
$this->Application->removeObject('u.email-from');
$object =& $this->_getEmailEvent($event);
/* @var $object kDBItem */
$email = $name = '';
// set defaults from event
if ($object->GetDBField('CustomSender')) {
$address = $object->GetDBField('SenderAddress');
$address_type = $object->GetDBField('SenderAddressType');
switch ($address_type) {
case EmailEvent::ADDRESS_TYPE_EMAIL:
$email = $address;
break;
case EmailEvent::ADDRESS_TYPE_USER:
$sql = 'SELECT FirstName, LastName, Email, PortalUserId
FROM ' . TABLE_PREFIX . 'PortalUser
WHERE Login = ' . $this->Conn->qstr($address);
$user_info = $this->Conn->GetRow($sql);
if ($user_info) {
// user still exists
$email = $user_info['Email'];
$name = trim($user_info['FirstName'] . ' ' . $user_info['LastName']);
$user =& $this->Application->recallObject('u.email-from', null, Array('skip_autoload' => true));
/* @var $user UsersItem */
$user->Load($user_info['PortalUserId']);
}
break;
}
if ($object->GetDBField('SenderName')) {
$name = $object->GetDBField('SenderName');
}
}
// update with custom data given during event execution
if (array_key_exists('from_email', $direct_params)) {
$email = $direct_params['from_email'];
}
if (array_key_exists('from_name', $direct_params)) {
$name = $direct_params['from_name'];
}
// still nothing, set defaults
if (!$email) {
$email = $this->Application->ConfigValue('Smtp_AdminMailFrom');
}
if (!$name) {
$name = strip_tags( $this->Application->ConfigValue('Site_Name') );
}
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
$esender->SetFrom($email, $name);
return Array ($email, $name);
}
/**
* Processes email recipients
*
* @param kEvent $event
* @param Array $direct_params
*/
function _processRecipients(&$event, $direct_params = Array ())
{
$this->Application->removeObject('u.email-to');
$object =& $this->_getEmailEvent($event);
/* @var $object kDBItem */
$to_email = $to_name = '';
$all_recipients = Array ();
$recipients_xml = $object->GetDBField('Recipients');
if ($recipients_xml) {
$minput_helper =& $this->Application->recallObject('MInputHelper');
/* @var $minput_helper MInputHelper */
// group recipients by type
$records = $minput_helper->parseMInputXML($recipients_xml);
foreach ($records as $record) {
$recipient_type = $record['RecipientType'];
if (!array_key_exists($recipient_type, $all_recipients)) {
$all_recipients[$recipient_type] = Array ();
}
$all_recipients[$recipient_type][] = $record;
}
}
if (!array_key_exists(EmailEvent::RECIPIENT_TYPE_TO, $all_recipients)) {
$all_recipients[EmailEvent::RECIPIENT_TYPE_TO] = Array ();
}
// remove all "To" recipients, when not allowed
$overwrite_to_email = array_key_exists('overwrite_to_email', $direct_params) ? $direct_params['overwrite_to_email'] : false;
if (!$object->GetDBField('CustomRecipient') || $overwrite_to_email) {
$all_recipients[EmailEvent::RECIPIENT_TYPE_TO] = Array ();
}
// update with custom data given during event execution (user_id)
$to_user_id = $event->getEventParam('EmailEventToUserId');
if ($to_user_id > 0) {
$sql = 'SELECT FirstName, LastName, Email
FROM ' . TABLE_PREFIX . 'PortalUser
WHERE PortalUserId = ' . $to_user_id;
$user_info = $this->Conn->GetRow($sql);
if ($user_info) {
$add_recipient = Array (
'RecipientAddressType' => EmailEvent::ADDRESS_TYPE_EMAIL,
'RecipientAddress' => $user_info['Email'],
'RecipientName' => trim($user_info['FirstName'] . ' ' . $user_info['LastName']),
);
array_unshift($all_recipients[EmailEvent::RECIPIENT_TYPE_TO], $add_recipient);
$user =& $this->Application->recallObject('u.email-to', null, Array('skip_autoload' => true));
/* @var $user UsersItem */
$user->Load($to_user_id);
}
}
elseif (is_numeric($to_user_id)) {
// recipient is system user with negative ID (root, guest, etc.) -> send to admin
array_unshift($all_recipients[EmailEvent::RECIPIENT_TYPE_TO], $this->_getDefaultRepipient());
}
// update with custom data given during event execution (email + name)
$add_recipient = Array ();
if (array_key_exists('to_email', $direct_params)) {
$add_recipient['RecipientName'] = '';
$add_recipient['RecipientAddressType'] = EmailEvent::ADDRESS_TYPE_EMAIL;
$add_recipient['RecipientAddress'] = $direct_params['to_email'];
}
if (array_key_exists('to_name', $direct_params)) {
$add_recipient['RecipientName'] = $direct_params['to_name'];
}
if ($add_recipient) {
array_unshift($all_recipients[EmailEvent::RECIPIENT_TYPE_TO], $add_recipient);
}
if (($object->GetDBField('Type') == EmailEvent::EVENT_TYPE_ADMIN) && !$all_recipients[EmailEvent::RECIPIENT_TYPE_TO]) {
// admin email event without direct recipient -> send to admin
array_unshift($all_recipients[EmailEvent::RECIPIENT_TYPE_TO], $this->_getDefaultRepipient());
}
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
$header_mapping = Array (
EmailEvent::RECIPIENT_TYPE_TO => 'To',
EmailEvent::RECIPIENT_TYPE_CC => 'Cc',
EmailEvent::RECIPIENT_TYPE_BCC => 'Bcc',
);
$default_email = $this->Application->ConfigValue('Smtp_AdminMailFrom');
foreach ($all_recipients as $recipient_type => $recipients) {
// add recipients to email
$pairs = Array ();
foreach ($recipients as $recipient) {
$address = $recipient['RecipientAddress'];
$address_type = $recipient['RecipientAddressType'];
$repipient_name = $recipient['RecipientName'];
switch ($address_type) {
case EmailEvent::ADDRESS_TYPE_EMAIL:
$pairs[] = Array ('email' => $address, 'name' => $repipient_name);
break;
case EmailEvent::ADDRESS_TYPE_USER:
$sql = 'SELECT FirstName, LastName, Email
FROM ' . TABLE_PREFIX . 'PortalUser
WHERE Login = ' . $this->Conn->qstr($address);
$user_info = $this->Conn->GetRow($sql);
if ($user_info) {
// user still exists
$name = trim($user_info['FirstName'] . ' ' . $user_info['LastName']);
$pairs[] = Array (
'email' => $user_info['Email'],
'name' => $name ? $name : $repipient_name,
);
}
break;
case EmailEvent::ADDRESS_TYPE_GROUP:
$sql = 'SELECT u.FirstName, u.LastName, u.Email
FROM ' . TABLE_PREFIX . 'PortalGroup g
JOIN ' . TABLE_PREFIX . 'UserGroup ug ON ug.GroupId = g.GroupId
JOIN ' . TABLE_PREFIX . 'PortalUser u ON u.PortalUserId = ug.PortalUserId
WHERE g.Name = ' . $this->Conn->qstr($address);
$users = $this->Conn->Query($sql);
foreach ($users as $user) {
$name = trim($user_info['FirstName'] . ' ' . $user_info['LastName']);
$pairs[] = Array (
'email' => $user_info['Email'],
'name' => $name ? $name : $repipient_name,
);
}
break;
}
}
if (!$pairs) {
continue;
}
if ($recipient_type == EmailEvent::RECIPIENT_TYPE_TO) {
$to_email = $pairs[0]['email'] ? $pairs[0]['email'] : $default_email;
$to_name = $pairs[0]['name'] ? $pairs[0]['name'] : $to_email;
}
$header_name = $header_mapping[$recipient_type];
foreach ($pairs as $pair) {
$email = $pair['email'] ? $pair['email'] : $default_email;
$name = $pair['name'] ? $pair['name'] : $email;
$esender->AddRecipient($header_name, $email, $name);
}
}
return Array ($to_email, $to_name);
}
/**
* This is default recipient, when we can't determine actual one
*
* @return Array
*/
function _getDefaultRepipient()
{
return Array (
'RecipientName' => $this->Application->ConfigValue('Smtp_AdminMailFrom'),
'RecipientAddressType' => EmailEvent::ADDRESS_TYPE_EMAIL,
'RecipientAddress' => $this->Application->ConfigValue('Smtp_AdminMailFrom'),
);
}
/**
* Returns email event message by ID (headers & body in one piece)
*
* @param kEvent $event
* @param int $language_id
*/
function _getMessageBody(&$event, $language_id = null)
{
if (!isset($language_id)) {
$language_id = $this->Application->GetVar('m_lang');
}
$object =& $this->_getEmailEvent($event);
// 1. get message body
$message_body = $this->_formMessageBody($object, $language_id, $object->GetDBField('MessageType'));
// 2. replace tags if needed
$default_replacement_tags = Array (
'<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',
);
$replacement_tags = $object->GetDBField('ReplacementTags');
$replacement_tags = $replacement_tags ? unserialize($replacement_tags) : Array ();
$replacement_tags = array_merge($default_replacement_tags, $replacement_tags);
foreach ($replacement_tags as $replace_from => $replace_to) {
$message_body = str_replace($replace_from, $replace_to, $message_body);
}
return $message_body;
}
/**
* Prepare email message body
*
* @param kDBItem $object
* @param int $language_id
* @return string
*/
function _formMessageBody(&$object, $language_id)
{
$default_language_id = $this->Application->GetDefaultLanguageId();
$fields_hash = Array (
'Headers' => $object->GetDBField('Headers'),
);
// prepare subject
$subject = $object->GetDBField('l' . $language_id . '_Subject');
if (!$subject) {
$subject = $object->GetDBField('l' . $default_language_id . '_Subject');
}
$fields_hash['Subject'] = $subject;
// prepare body
$body = $object->GetDBField('l' . $language_id . '_Body');
if (!$body) {
$body = $object->GetDBField('l' . $default_language_id . '_Body');
}
$fields_hash['Body'] = $body;
$email_message_helper =& $this->Application->recallObject('EmailMessageHelper');
/* @var $email_message_helper EmailMessageHelper */
$ret = $email_message_helper->buildTemplate($fields_hash);
// add footer
$footer = $this->_getFooter($language_id, $object->GetDBField('MessageType'));
if ($ret && $footer) {
$ret .= "\r\n" . $footer;
}
return $ret;
}
/**
* Returns email footer
*
* @param int $language_id
* @param string $message_type
* @return string
*/
function _getFooter($language_id, $message_type)
{
static $footer = null;
if (!isset($footer)) {
$default_language_id = $this->Application->GetDefaultLanguageId();
$sql = 'SELECT l' . $language_id . '_Body, l' . $default_language_id . '_Body
FROM ' . $this->Application->getUnitOption('emailevents', 'TableName') . ' em
WHERE Event = "COMMON.FOOTER"';
$footer_data = $this->Conn->GetRow($sql);
$footer = $footer_data['l' . $language_id . '_Body'];
if (!$footer) {
$footer = $footer_data['l' . $default_language_id . '_Body'];
}
if ($message_type == 'text') {
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
$footer = $esender->ConvertToText($footer);
}
}
return $footer;
}
/**
* Parse message template and return headers (as array) and message body part
*
* @param string $message
* @param Array $direct_params
* @return Array
*/
function ParseMessageBody($message, $direct_params = Array ())
{
$message_language = $this->_getSendLanguage($direct_params);
$this->_changeLanguage($message_language);
$direct_params['message_text'] = isset($direct_params['message']) ? $direct_params['message'] : ''; // parameter alias
// 1. parse template
$this->Application->InitParser();
$parser_params = $this->Application->Parser->Params; // backup parser params
$this->Application->Parser->SetParams( array_merge($parser_params, $direct_params) );
$message = implode('&|&', explode("\n\n", $message, 2)); // preserves double \n in case when tag is located in subject field
$message = $this->Application->Parser->Parse($message, 'email_template', 0);
$this->Application->Parser->SetParams($parser_params); // restore parser params
// 2. replace line endings, that are send with data submitted via request
$message = str_replace("\r\n", "\n", $message); // possible case
$message = str_replace("\r", "\n", $message); // impossible case, but just in case replace this too
// 3. separate headers from body
$message_headers = Array ();
list($headers, $message_body) = explode('&|&', $message, 2);
$category_helper =& $this->Application->recallObject('CategoryHelper');
/* @var $category_helper CategoryHelper */
$message_body = $category_helper->replacePageIds($message_body);
$headers = explode("\n", $headers);
foreach ($headers as $header) {
$header = explode(':', $header, 2);
$message_headers[ trim($header[0]) ] = trim($header[1]);
}
$this->_changeLanguage();
return Array ($message_headers, $message_body);
}
/**
- * Raised when email message shoul be sent
+ * Raised when email message should be sent
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnEmailEvent(&$event)
+ protected function OnEmailEvent(&$event)
{
$email_event_name = $event->getEventParam('EmailEventName');
- if (strpos($email_event_name, '_') !== false) {
+ if ( strpos($email_event_name, '_') !== false ) {
throw new Exception('<span class="debug_error">Invalid email event name</span> <strong>' . $email_event_name . '</strong>. Use only <strong>UPPERCASE characters</strong> and <strong>dots</strong> as email event names');
}
$object =& $this->_getEmailEvent($event);
- if (!is_object($object)) {
+ if ( !is_object($object) ) {
// email event not found OR it's won't be send under given circumstances
- return false;
+ return ;
}
// additional parameters from kApplication->EmailEvent
$send_params = $event->getEventParam('DirectSendParams');
// 1. get information about message sender and recipient
list ($from_email, $from_name) = $this->_processSender($event, $send_params);
list ($to_email, $to_name) = $this->_processRecipients($event, $send_params);
// 2. prepare message to be sent
$message_language = $this->_getSendLanguage($send_params);
$message_template = $this->_getMessageBody($event, $message_language);
- if (!trim($message_template)) {
+ if ( !trim($message_template) ) {
trigger_error('Message template is empty', E_USER_WARNING);
- return false;
+ return ;
}
list ($message_headers, $message_body) = $this->ParseMessageBody($message_template, $send_params);
- if (!trim($message_body)) {
+ if ( !trim($message_body) ) {
trigger_error('Message template is empty after parsing', E_USER_WARNING);
- return false;
+ return ;
}
// 3. set headers & send message
$esender =& $this->Application->recallObject('EmailSender');
/* @var $esender kEmailSendingHelper */
$message_subject = isset($message_headers['Subject']) ? $message_headers['Subject'] : 'Mail message';
$esender->SetSubject($message_subject);
- if ($this->Application->isDebugMode()) {
- // set special header with event name, so it will be easier to determite what's actually was received
+ if ( $this->Application->isDebugMode() ) {
+ // set special header with event name, so it will be easier to determine what's actually was received
$message_headers['X-Event-Name'] = $email_event_name . ' - ' . ($object->GetDBField('Type') == EmailEvent::EVENT_TYPE_ADMIN ? 'ADMIN' : 'USER');
}
foreach ($message_headers as $header_name => $header_value) {
$esender->SetEncodedHeader($header_name, $header_value);
}
$esender->CreateTextHtmlPart($message_body, $object->GetDBField('MessageType') == 'html');
$event->status = $esender->Deliver() ? kEvent::erSUCCESS : kEvent::erFAIL;
- if ($event->status == kEvent::erSUCCESS) {
+ if ( $event->status == kEvent::erSUCCESS ) {
// all keys, that are not used in email sending are written to log record
$send_keys = Array ('from_email', 'from_name', 'to_email', 'to_name', 'message');
foreach ($send_keys as $send_key) {
unset($send_params[$send_key]);
}
$fields_hash = Array (
- 'fromuser' => $from_name.' ('.$from_email.')',
- 'addressto' => $to_name.' ('.$to_email.')',
+ 'fromuser' => $from_name . ' (' . $from_email . ')',
+ 'addressto' => $to_name . ' (' . $to_email . ')',
'subject' => $message_subject,
'timestamp' => adodb_mktime(),
'event' => $email_event_name,
'EventParams' => serialize($send_params),
);
- $this->Conn->doInsert($fields_hash, TABLE_PREFIX.'EmailLog');
+ $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'EmailLog');
}
}
function _getSendLanguage($send_params)
{
if (array_key_exists('language_id', $send_params)) {
return $send_params['language_id'];
}
return $this->Application->GetVar('m_lang');
}
function _changeLanguage($language_id = null)
{
static $prev_language_id = null;
- if (!isset($language_id)) {
+ if ( !isset($language_id) ) {
// restore language
$language_id = $prev_language_id;
}
$this->Application->SetVar('m_lang', $language_id);
+
$language =& $this->Application->recallObject('lang.current');
- /* @var $lang_object kDBItem */
+ /* @var $language LanguagesItem */
$language->Load($language_id);
$this->Application->Phrases->LanguageId = $language_id;
- $this->Application->Phrases->Phrases = Array();
+ $this->Application->Phrases->Phrases = Array ();
$prev_language_id = $language_id; // for restoring it later
}
/**
* Process emails from queue
*
* @param kEvent $event
* @todo Move to MailingList
*/
function OnProcessEmailQueue(&$event)
{
$deliver_count = $event->getEventParam('deliver_count');
if ($deliver_count === false) {
$deliver_count = $this->Application->ConfigValue('MailingListSendPerStep');
if ($deliver_count === false) {
$deliver_count = 10; // 10 emails per script run (if not specified directly)
}
}
$processing_type = $this->Application->GetVar('type');
if ($processing_type = 'return_progress') {
$email_queue_progress = $this->Application->RecallVar('email_queue_progress');
if ($email_queue_progress === false) {
$emails_sent = 0;
$sql = 'SELECT COUNT(*)
FROM ' . TABLE_PREFIX . 'EmailQueue
WHERE (SendRetries < 5) AND (LastSendRetry < ' . strtotime('-2 hours') . ')';
$total_emails = $this->Conn->GetOne($sql);
$this->Application->StoreVar('email_queue_progress', $emails_sent.':'.$total_emails);
}
else {
list ($emails_sent, $total_emails) = explode(':', $email_queue_progress);
}
}
$sql = 'SELECT *
FROM '.TABLE_PREFIX.'EmailQueue
WHERE (SendRetries < 5) AND (LastSendRetry < ' . strtotime('-2 hours') . ')
LIMIT 0,' . $deliver_count;
$messages = $this->Conn->Query($sql);
$message_count = count($messages);
if (!$message_count) {
// no messages left to send in queue
if ($processing_type = 'return_progress') {
$this->Application->RemoveVar('email_queue_progress');
$this->Application->Redirect($this->Application->GetVar('finish_template'));
}
return ;
}
$mailing_list_helper =& $this->Application->recallObject('MailingListHelper');
/* @var $mailing_list_helper MailingListHelper */
$mailing_list_helper->processQueue($messages);
if ($processing_type = 'return_progress') {
$emails_sent += $message_count;
if ($emails_sent >= $total_emails) {
$this->Application->RemoveVar('email_queue_progress');
$this->Application->Redirect($this->Application->GetVar('finish_template'));
}
$this->Application->StoreVar('email_queue_progress', $emails_sent.':'.$total_emails);
$event->status = kEvent::erSTOP;
echo ($emails_sent / $total_emails) * 100;
}
}
/**
* Prefills module dropdown
*
* @param kEvent $event
*/
function OnAfterConfigRead(&$event)
{
parent::OnAfterConfigRead($event);
$options = Array ();
foreach ($this->Application->ModuleInfo as $module_name => $module_info) {
if ($module_name == 'In-Portal') {
continue;
}
$options[$module_name] = $module_name;
}
$fields = $this->Application->getUnitOption($event->Prefix, 'Fields');
$fields['Module']['options'] = $options;
$this->Application->setUnitOption($event->Prefix, 'Fields', $fields);
if ($this->Application->GetVar('regional')) {
$this->Application->setUnitOption($event->Prefix, 'PopulateMlFields', true);
}
}
/**
* Prepare temp tables and populate it
* with items selected in the grid
*
* @param kEvent $event
*/
function OnEdit(&$event)
{
parent::OnEdit($event);
// use language from grid, instead of primary language used by default
$event->SetRedirectParam('m_lang', $this->Application->GetVar('m_lang'));
}
/**
* Fixes default recipient type
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemLoad(&$event)
+ protected function OnAfterItemLoad(&$event)
{
parent::OnAfterItemLoad($event);
$object =& $event->getObject();
/* @var $object kDBItem */
if (!$this->Application->isDebugMode(false)) {
if ($object->GetDBField('AllowChangingRecipient')) {
$object->SetDBField('RecipientType', EmailEvent::RECIPIENT_TYPE_TO);
}
else {
$object->SetDBField('RecipientType', EmailEvent::RECIPIENT_TYPE_CC);
}
}
// process replacement tags
$records = Array ();
$replacement_tags = $object->GetDBField('ReplacementTags');
$replacement_tags = $replacement_tags ? unserialize($replacement_tags) : Array ();
foreach ($replacement_tags as $tag => $replacement) {
$records[] = Array ('Tag' => $tag, 'Replacement' => $replacement);
}
$minput_helper =& $this->Application->recallObject('MInputHelper');
/* @var $minput_helper MInputHelper */
$xml = $minput_helper->prepareMInputXML($records, Array ('Tag', 'Replacement'));
$object->SetDBField('ReplacementTagsXML', $xml);
}
/**
* Performs custom validation + keep read-only fields
*
* @param kEvent $event
*/
function _itemChanged(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
// validate email subject and body for parsing errors
$this->_validateEmailTemplate($object);
// validate sender and recipient addresses
if ($object->GetDBField('CustomSender')) {
$this->_validateAddress($event, 'Sender');
}
$this->_validateAddress($event, 'Recipient');
if (!$this->Application->isDebugMode(false)) {
// only allow to enable/disable event while in debug mode
$to_restore = Array ('Enabled', 'AllowChangingSender', 'AllowChangingRecipient');
if (!$object->GetOriginalField('AllowChangingSender')) {
$to_restore = array_merge($to_restore, Array ('CustomSender', 'SenderName', 'SenderAddressType', 'SenderAddress'));
}
if (!$object->GetOriginalField('AllowChangingRecipient')) {
$to_restore = array_merge($to_restore, Array ('CustomRecipient'/*, 'Recipients'*/));
}
// prevent specific fields from editing
foreach ($to_restore as $restore_field) {
$original_value = $object->GetOriginalField($restore_field);
if ($object->GetDBField($restore_field) != $original_value) {
$object->SetDBField($restore_field, $original_value);
}
}
}
// process replacement tags
if ( $object->GetDBField('ReplacementTagsXML') ) {
$minput_helper =& $this->Application->recallObject('MInputHelper');
/* @var $minput_helper MInputHelper */
$replacement_tags = Array ();
$records = $minput_helper->parseMInputXML( $object->GetDBField('ReplacementTagsXML') );
foreach ($records as $record) {
$replacement_tags[ trim($record['Tag']) ] = trim($record['Replacement']);
}
$object->SetDBField('ReplacementTags', $replacement_tags ? serialize($replacement_tags) : NULL);
}
}
/**
* Validates address using given field prefix
*
* @param kEvent $event
* @param string $field_prefix
*/
function _validateAddress(&$event, $field_prefix)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$address_type = $object->GetDBField($field_prefix . 'AddressType');
$object->setRequired($field_prefix . 'Address', $address_type > 0);
$address = $object->GetDBField($field_prefix . 'Address');
if (!$address) {
// don't validate against empty address
return ;
}
switch ($address_type) {
case EmailEvent::ADDRESS_TYPE_EMAIL:
if (!preg_match('/^(' . REGEX_EMAIL_USER . '@' . REGEX_EMAIL_DOMAIN . ')$/i', $address)) {
$object->SetError($field_prefix . 'Address', 'invalid_email');
}
break;
case EmailEvent::ADDRESS_TYPE_USER:
$sql = 'SELECT PortalUserId
FROM ' . TABLE_PREFIX . 'PortalUser
WHERE Login = ' . $this->Conn->qstr($address);
if (!$this->Conn->GetOne($sql)) {
$object->SetError($field_prefix . 'Address', 'invalid_user');
}
break;
case EmailEvent::ADDRESS_TYPE_GROUP:
$sql = 'SELECT GroupId
FROM ' . TABLE_PREFIX . 'PortalGroup
WHERE Name = ' . $this->Conn->qstr($address);
if (!$this->Conn->GetOne($sql)) {
$object->SetError($field_prefix . 'Address', 'invalid_group');
}
break;
}
}
/**
* Don't allow to enable/disable events in non-debug mode
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
parent::OnBeforeItemCreate($event);
$this->_itemChanged($event);
}
/**
* Don't allow to enable/disable events in non-debug mode
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
parent::OnBeforeItemUpdate($event);
$this->_itemChanged($event);
}
/**
* Suggest address based on typed address and selected address type
*
* @param kEvent $event
*/
function OnSuggestAddress(&$event)
{
$event->status = kEvent::erSTOP;
$address_type = $this->Application->GetVar('type');
$address = $this->Application->GetVar('value');
$limit = $this->Application->GetVar('limit');
if (!$limit) {
$limit = 20;
}
switch ($address_type) {
case EmailEvent::ADDRESS_TYPE_EMAIL:
$field = 'Email';
$table_name = TABLE_PREFIX . 'PortalUser';
break;
case EmailEvent::ADDRESS_TYPE_USER:
$field = 'Login';
$table_name = TABLE_PREFIX . 'PortalUser';
break;
case EmailEvent::ADDRESS_TYPE_GROUP:
$field = 'Name';
$table_name = TABLE_PREFIX . 'PortalGroup';
break;
}
if (isset($field)) {
$sql = 'SELECT DISTINCT ' . $field . '
FROM ' . $table_name . '
WHERE ' . $field . ' LIKE ' . $this->Conn->qstr($address . '%') . '
ORDER BY ' . $field . ' ASC
LIMIT 0,' . $limit;
$data = $this->Conn->GetCol($sql);
}
else {
$data = Array ();
}
$this->Application->XMLHeader();
echo '<suggestions>';
foreach ($data as $item) {
echo '<item>' . htmlspecialchars($item) . '</item>';
}
echo '</suggestions>';
}
/**
* Validates subject and body fields of Email template
* @param kDBItem $object
*/
function _validateEmailTemplate(&$object)
{
$this->parseField($object, 'Subject');
$this->parseField($object, 'Body');
}
/**
* Parses contents of given object field and sets error, when invalid in-portal tags found
* @param kDBItem $object
* @param string $field
* @return void
*/
function parseField(&$object, $field)
{
$this->Application->InitParser();
try {
$this->Application->Parser->CompileRaw($object->GetField($field), 'email_template');
}
catch (ParserException $e) {
if ( $this->Application->isDebugMode() ) {
$this->Application->Debugger->appendHTML('<b style="color: red;">Error in Email Template:</b> ' . $e->getMessage() . ' (line: ' . $e->getLine() . ')');
}
$object->SetError($field, 'parsing_error');
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/email_events/email_event_tp.php
===================================================================
--- branches/5.2.x/core/units/email_events/email_event_tp.php (revision 14627)
+++ branches/5.2.x/core/units/email_events/email_event_tp.php (revision 14628)
@@ -1,88 +1,89 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2010 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 EmailEventTagProcessor extends kDBTagProcessor
{
/**
* Removes "Enabled" column, when not in debug mode
*
* @param Array $params
*/
function ModifyUnitConfig($params)
{
- if (!$this->Application->isDebugMode()) {
- $grids = $this->Application->getUnitOption($this->Prefix, 'Grids');
+ if ( !$this->Application->isDebugMode() ) {
+ $grids = $this->Application->getUnitOption($this->Prefix, 'Grids', Array ());
+ /* @var $grids Array */
foreach ($grids as $grid_name => $grid_data) {
- if (array_key_exists('Enabled', $grid_data['Fields'])) {
+ if ( array_key_exists('Enabled', $grid_data['Fields']) ) {
unset($grids[$grid_name]['Fields']['Enabled']);
}
}
$this->Application->setUnitOption($this->Prefix, 'Grids', $grids);
}
}
/**
* Checks, that field can be edited
*
* @param Array $params
* @return string
*/
function IsEditable($params)
{
if ($this->Application->isDebugMode()) {
return true;
}
$object =& $this->getObject($params);
/* @var $object kDBItem */
return $object->GetDBField($params['check_field']);
}
/**
* Removes "To" options from possible options in "RecipientType" field
*
* @param Array $params
*/
function RemoveToRecipientType($params)
{
$object =& $this->getObject($params);
/* @var $object kDBItem */
$field_options = $object->GetFieldOptions('RecipientType');
unset($field_options['options'][ EmailEvent::RECIPIENT_TYPE_TO ]);
$object->SetFieldOptions('RecipientType', $field_options);
}
/**
* Restores "To" option in possible option list in "RecipientType" field
*
* @param Array $params
*/
function RestoreRecipientType($params)
{
$object =& $this->getObject($params);
/* @var $object kDBItem */
$field_options = $object->GetFieldOptions('RecipientType');
$virtual_fields = $this->Application->getUnitOption($this->Prefix, 'VirtualFields');
$field_options['options'] = $virtual_fields['RecipientType']['options'];
$object->SetFieldOptions('RecipientType', $field_options);
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/stylesheets/stylesheets_event_handler.php
===================================================================
--- branches/5.2.x/core/units/stylesheets/stylesheets_event_handler.php (revision 14627)
+++ branches/5.2.x/core/units/stylesheets/stylesheets_event_handler.php (revision 14628)
@@ -1,38 +1,44 @@
<?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!');
+defined('FULL_PATH') or die('restricted access!');
- class StylesheetsEventHandler extends kDBEventHandler {
+class StylesheetsEventHandler extends kDBEventHandler {
- /**
- * [HOOK] Compile stylesheet file based on theme definitions
- *
- * @param kEvent $event
- */
- function OnCompileStylesheet(&$event)
- {
- $object =& $event->getObject( Array('skip_autoload' => true) );
- $object->SwitchToLive();
-
- $ids = explode(',', $event->MasterEvent->getEventParam('ids') );
-
- if(!$ids) return false;
- foreach($ids as $id)
- {
- $object->Load($id);
- $object->Compile();
- }
+ /**
+ * [HOOK] Compile stylesheet file based on theme definitions
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnCompileStylesheet(&$event)
+ {
+ $object =& $event->getObject( Array ('skip_autoload' => true) );
+ /* @var $object StylesheetsItem */
+
+ $object->SwitchToLive();
+
+ $ids = explode(',', $event->MasterEvent->getEventParam('ids'));
+
+ if ( !$ids ) {
+ return ;
+ }
+
+ foreach ($ids as $id) {
+ $object->Load($id);
+ $object->Compile();
}
- }
\ No newline at end of file
+ }
+}
\ No newline at end of file
Index: branches/5.2.x/core/units/stylesheets/stylesheets_item.php
===================================================================
--- branches/5.2.x/core/units/stylesheets/stylesheets_item.php (revision 14627)
+++ branches/5.2.x/core/units/stylesheets/stylesheets_item.php (revision 14628)
@@ -1,62 +1,64 @@
<?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 StylesheetsItem extends kDBItem
{
function Compile()
{
$selector_item =& $this->Application->recallObject('selectors.item', 'selectors', Array('live_table'=>true, 'skip_autoload' => true) );
+ /* @var $selector_item SelectorsItem */
+
$parent_field = $this->Application->getUnitOption($selector_item->Prefix, 'ForeignKey');
$sql_template = 'SELECT '.$selector_item->IDField.' FROM '.$selector_item->TableName.' WHERE '.$parent_field.' = %s ORDER BY SelectorName ASC';
$selectors_ids = $this->Conn->GetCol( sprintf($sql_template, $this->GetID() ) );
$ret = '/* This file is generated automatically. Don\'t edit it manually ! */'."\n\n";
foreach($selectors_ids as $selector_id)
{
$selector_item->Load($selector_id);
$ret .= $selector_item->CompileStyle()."\n";
}
$ret .= $this->GetDBField('AdvancedCSS');
$compile_ts = adodb_mktime();
$css_path = WRITEABLE . '/stylesheets/';
$file_helper =& $this->Application->recallObject('FileHelper');
/* @var $file_helper FileHelper */
$file_helper->CheckFolder($css_path);
$css_file = $css_path.mb_strtolower($this->GetDBField('Name')).'-'.$compile_ts.'.css';
$fp = fopen($css_file,'w');
if($fp)
{
$prev_css = $css_path.mb_strtolower($this->GetDBField('Name')).'-'.$this->GetDBField('LastCompiled').'.css';
if( file_exists($prev_css) ) unlink($prev_css);
fwrite($fp, $ret);
fclose($fp);
$sql = 'UPDATE '.$this->TableName.' SET LastCompiled = '.$compile_ts.' WHERE '.$this->IDField.' = '.$this->GetID();
$this->Conn->Query($sql);
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/site_domains/site_domain_eh.php
===================================================================
--- branches/5.2.x/core/units/site_domains/site_domain_eh.php (revision 14627)
+++ branches/5.2.x/core/units/site_domains/site_domain_eh.php (revision 14628)
@@ -1,259 +1,272 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2010 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 SiteDomainEventHandler extends kDBEventHandler {
/**
- * Permission check override
+ * Checks user permission to execute given $event
*
* @param kEvent $event
+ * @return bool
+ * @access public
*/
- function CheckPermission(&$event)
+ public function CheckPermission(&$event)
{
if ($event->Name == 'OnItemBuild') {
// check permission without using $event->getSection(),
// so first cache rebuild won't lead to "ldefault_Name" field being used
return true;
}
return parent::CheckPermission($event);
}
/**
* Returns ID of site domain, that matches current url
*
* @param kEvent $event
*/
function getPassedID(&$event)
{
if ($event->Special == 'current') {
if ($this->Application->isAdmin) {
$event->setEventParam('raise_warnings', 0);
}
else {
if (PROTOCOL == 'https://') {
return $this->querySiteDomain('SSLUrl', rtrim($this->Application->BaseURL(), '/'));
}
return $this->querySiteDomain('DomainName', $_SERVER['HTTP_HOST']);
}
}
return parent::getPassedID($event);
}
function querySiteDomain($field, $value)
{
$site_helper =& $this->Application->recallObject('SiteHelper');
/* @var $site_helper SiteHelper */
$site_domains = $site_helper->getSiteDomains();
$domain_by_name = $site_helper->getDomainByName($field, $value);
$domain_by_ip = $site_helper->getDomainByIP();
if ($domain_by_ip) {
$site_domain = $site_domains[$domain_by_ip];
$redirect_mode = $site_domain['RedirectOnIPMatch'];
if (($redirect_mode == SITE_DOMAIN_REDIRECT_EXTERNAL) && ($domain_by_ip == $domain_by_name)) {
// redirect to external url (when visiting protected domain)
$external_url = $site_domain['ExternalUrl'];
if (preg_match('/^http[s]{0,1}:\/\//', $external_url)) {
$this->Application->Redirect('external:' . $external_url);
}
else {
$this->Application->Redirect('external:' . PROTOCOL . $external_url . $_SERVER['REQUEST_URI']);
}
}
elseif (($redirect_mode == SITE_DOMAIN_REDIRECT_CURRENT) && ($domain_by_ip != $domain_by_name)) {
// redirect to a domain detected by IP (when not already on it)
if ((PROTOCOL == 'https://') && !$site_domain['SSLUrlUsesRegExp'] && $site_domain['SSLUrl']) {
// need to remove sub folder from ssl url
$ssl_url = preg_replace('/^(https:\/\/[^\/]*)(\/.*){0,1}$/', '\\1', $site_domain['SSLUrl']);
$this->Application->Redirect('external:' . $ssl_url . $_SERVER['REQUEST_URI']);
}
elseif ((PROTOCOL == 'http://') && !$site_domain['DomainNameUsesRegExp']) {
$this->Application->Redirect('external:http://' . $site_domain['DomainName'] . $_SERVER['REQUEST_URI']);
}
}
return $domain_by_ip;
}
return $domain_by_name;
}
/**
* Load item if id is available
*
* @param kEvent $event
*/
function LoadItem(&$event)
{
- if ($this->Application->isAdmin) {
+ if ( $this->Application->isAdmin ) {
// don't load domain data from cache
parent::LoadItem($event);
- return ;
+ return;
}
$object =& $event->getObject();
/* @var $object kDBItem */
$id = (int)$this->getPassedID($event);
- if ($object->isLoaded() && ($object->GetID() == $id)) {
+ if ( $object->isLoaded() && ($object->GetID() == $id) ) {
// object is already loaded by same id
- return ;
+ return;
}
$site_helper =& $this->Application->recallObject('SiteHelper');
/* @var $site_helper SiteHelper */
$site_domains = $site_helper->getSiteDomains();
$domain_data = array_key_exists($id, $site_domains) ? $site_domains[$id] : false;
- if ($object->LoadFromHash($domain_data)) {
+ if ( $object->LoadFromHash($domain_data) ) {
$actions =& $this->Application->recallObject('kActions');
- $actions->Set($event->getPrefixSpecial().'_id', $object->GetID() );
+ /* @var $actions Params */
+
+ $actions->Set($event->getPrefixSpecial() . '_id', $object->GetID());
}
else {
$object->setID($id);
}
}
/**
* Removes In-Commerce related fields, when it's not installed
*
* @param kEvent $event
*/
function OnAfterConfigRead(&$event)
{
parent::OnAfterConfigRead($event);
if (!$this->Application->isModuleEnabled('In-Commerce')) {
$remove_fields = Array (
'BillingCountry', 'ShippingCountry',
'PrimaryCurrencyId', 'Currencies',
'PrimaryPaymentTypeId', 'PaymentTypes'
);
// remove field definitions
$fields = $this->Application->getUnitOption($event->Prefix, 'Fields');
foreach ($remove_fields as $remove_field) {
unset($fields[$remove_field]);
}
$this->Application->setUnitOption($event->Prefix, 'Fields', $fields);
// remove grid columns
- $grids = $this->Application->getUnitOption($event->Prefix, 'Grids');
-
+ $grids = $this->Application->getUnitOption($event->Prefix, 'Grids', Array ());
+ /* @var $grids Array */
+
foreach ($grids as $grid_name => $grid_info) {
foreach ($remove_fields as $remove_field) {
if (array_key_exists($remove_field, $grid_info['Fields'])) {
unset($grids[$grid_name]['Fields'][$remove_field]);
}
}
}
$this->Application->setUnitOption($event->Prefix, 'Grids', $grids);
}
}
/**
* Delete cached information about site domains
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnSave(&$event)
+ protected function OnSave(&$event)
{
parent::OnSave($event);
- if ($event->status == kEvent::erSUCCESS) {
+ if ( $event->status == kEvent::erSUCCESS ) {
$this->_deleteCache();
}
}
/**
* Deletes cached information about site domains
*/
function _deleteCache()
{
if ( $this->Application->isCachingType(CACHING_TYPE_MEMORY) ) {
$this->Application->rebuildCache('master:domains_parsed', kCache::REBUILD_LATER, CacheSettings::$domainsParsedRebuildTime);
}
else {
$this->Application->rebuildDBCache('domains_parsed', kCache::REBUILD_LATER, CacheSettings::$domainsParsedRebuildTime);
}
$sql = 'DELETE FROM ' . TABLE_PREFIX . 'CachedUrls';
$this->Conn->Query($sql);
}
/**
- * Set's required fields based on redirect mode
+ * Sets required fields based on redirect mode
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnAfterItemLoad(&$event)
+ protected function OnAfterItemLoad(&$event)
{
parent::OnAfterItemLoad($event);
$this->_setRequired($event);
}
/**
* Set's required fields based on redirect mode
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemCreate(&$event)
+ protected function OnBeforeItemCreate(&$event)
{
parent::OnBeforeItemCreate($event);
$this->_setRequired($event);
}
/**
* Set's required fields based on redirect mode
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeItemUpdate(&$event)
+ protected function OnBeforeItemUpdate(&$event)
{
parent::OnBeforeItemUpdate($event);
$this->_setRequired($event);
}
/**
* Set's required fields
*
* @param kEvent $event
*/
function _setRequired(&$event)
{
$object =& $event->getObject();
/* @var $object kDBItem */
$redirect_mode = $object->GetDBField('RedirectOnIPMatch');
$object->setRequired('ExternalUrl', $redirect_mode == SITE_DOMAIN_REDIRECT_EXTERNAL);
$object->setRequired('DomainIPRange', $redirect_mode > 0);
}
}
Index: branches/5.2.x/core/install/install_toolkit.php
===================================================================
--- branches/5.2.x/core/install/install_toolkit.php (revision 14627)
+++ branches/5.2.x/core/install/install_toolkit.php (revision 14628)
@@ -1,1041 +1,1047 @@
<?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 kDBConnection
*/
var $Conn = null;
/**
* Path to config.php
*
* @var string
*/
var $INIFile = '';
/**
* Parsed data from config.php
*
* @var Array
*/
var $systemConfig = Array ();
/**
* Path, used by system to store data on filesystem
*
* @var string
*/
var $defaultWritablePath = '';
/**
* Installator instance
*
* @var kInstallator
*/
var $_installator = null;
function kInstallToolkit()
{
$this->defaultWritablePath = DIRECTORY_SEPARATOR . 'system';
if (class_exists('kApplication')) {
// auto-setup in case of separate module install
$this->Application =& kApplication::Instance();
$this->Conn =& $this->Application->GetADODBConnection();
}
$this->INIFile = FULL_PATH . $this->defaultWritablePath . DIRECTORY_SEPARATOR . 'config.php';
$this->systemConfig = $this->ParseConfig(true);
}
/**
* 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)
{
static $prerequisit_classes = Array ();
$prerequisites_file = sprintf(PREREQUISITE_FILE, $module_path);
- if (!file_exists($prerequisites_file) || !$versions) {
+ if ( !file_exists($prerequisites_file) || !$versions ) {
return Array ();
}
- if (!isset($prerequisit_classes[$module_path])) {
+ if ( !isset($prerequisit_classes[$module_path]) ) {
// save class name, because 2nd time
// (in after call $prerequisite_class variable will not be present)
include_once $prerequisites_file;
$prerequisit_classes[$module_path] = $prerequisite_class;
}
$prerequisite_object = new $prerequisit_classes[$module_path]();
- if (method_exists($prerequisite_object, 'setToolkit')) {
+ /* @var $prerequisite_object InPortalPrerequisites */
+
+ if ( method_exists($prerequisite_object, 'setToolkit') ) {
$prerequisite_object->setToolkit($this);
}
// some errors possible
return $prerequisite_object->CheckPrerequisites($versions, $mode);
}
/**
* Processes one license, received from server
*
* @param string $file_data
*/
function processLicense($file_data)
{
$modules_helper =& $this->Application->recallObject('ModulesHelper');
/* @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->setSystemConfig('Intechnic', 'License', $file_data[0]);
if (array_key_exists(1, $file_data)) {
$this->setSystemConfig('Intechnic', 'LicenseCode', $file_data[1]);
}
else {
$this->setSystemConfig('Intechnic', 'LicenseCode');
}
$this->SaveConfig();
}
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 . 'ConfigurationValues
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 $module_path
* @param string $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');
return ;
}
$version = $this->GetMaxModuleVersion($module_path);
}
// get table prefix from config, because application may not be available here
$table_prefix = $this->getSystemConfig('Database', 'TablePrefix');
if ($module_name == 'kernel') {
$module_name = 'in-portal';
}
// don't use "adodb_mktime" here, because it's not yet included
$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 string $category_id
*/
function SetModuleRootCategory($module_name, $category_id = 0)
{
// get table prefix from config, because application may not be available here
$table_prefix = $this->getSystemConfig('Database', 'TablePrefix');
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_name
* @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
*/
function RunSQLText(&$sqls, $replace_from = null, $replace_to = null, $start_from = 0)
{
$table_prefix = $this->getSystemConfig('Database', 'TablePrefix');
// 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->getSystemConfig('Database', 'DBCollation');
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).'</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 */
$language_import_helper->performImport($lang_file, '|0|1|2|', '', $upgrade ? LANG_SKIP_EXISTING : LANG_OVERWRITE_EXISTING);
}
/**
* 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();
}
$id_field = $this->Application->getUnitOption('theme', 'IDField');
$table_name = $this->Application->getUnitOption('theme', 'TableName');
$sql = 'SELECT Name, ' . $id_field . '
FROM ' . $table_name . '
ORDER BY Name ASC';
return $this->Conn->GetCol($sql, $id_field);
}
function ParseConfig($parse_section = false)
{
if (!file_exists($this->INIFile)) {
return Array ();
}
if (file_exists($this->INIFile) && !is_readable($this->INIFile)) {
die('Could Not Open Ini File');
}
$contents = file($this->INIFile);
if ($contents && $contents[0] == '<' . '?' . 'php die() ?' . ">\n") {
// format of "config.php" file before 5.1.0 version
array_shift($contents);
return $this->parseIniString(implode('', $contents), $parse_section);
}
$_CONFIG = Array ();
require($this->INIFile);
if ($parse_section) {
return $_CONFIG;
}
$ret = Array ();
foreach ($_CONFIG as $section => $section_variables) {
$ret = array_merge($ret, $section_variables);
}
return $ret;
}
/**
* Equivalent for "parse_ini_string" function available since PHP 5.3.0
*
* @param string $ini
* @param bool $process_sections
* @param int $scanner_mode
* @return Array
*/
function parseIniString($ini, $process_sections = false, $scanner_mode = null)
{
# Generate a temporary file.
$tempname = tempnam('/tmp', 'ini');
$fp = fopen($tempname, 'w');
fwrite($fp, $ini);
$ini = parse_ini_file($tempname, !empty($process_sections));
fclose($fp);
@unlink($tempname);
return $ini;
}
function SaveConfig($silent = false)
{
if (!is_writable($this->INIFile) && !is_writable(dirname($this->INIFile))) {
$error_msg = 'Cannot write to "' . $this->INIFile . '" file';
if ($silent) {
trigger_error($error_msg, E_USER_WARNING);
}
else {
throw new Exception($error_msg);
}
return ;
}
$fp = fopen($this->INIFile, 'w');
fwrite($fp, '<' . '?' . 'php' . "\n\n");
foreach ($this->systemConfig as $section_name => $section_data) {
foreach ($section_data as $key => $value) {
fwrite($fp, '$_CONFIG[\'' . $section_name . '\'][\'' . $key . '\'] = \'' . addslashes($value) . '\';' . "\n");
}
fwrite($fp, "\n");
}
fclose($fp);
}
/**
* Sets value to system config (yet SaveConfig must be called to write it to file)
*
* @param string $section
* @param string $key
* @param string $value
*/
function setSystemConfig($section, $key, $value = null)
{
if (isset($value)) {
if (!array_key_exists($section, $this->systemConfig)) {
// create section, when missing
$this->systemConfig[$section] = Array ();
}
// create key in section
$this->systemConfig[$section][$key] = $value;
return ;
}
unset($this->systemConfig[$section][$key]);
}
/**
* Returns information from system config
*
* @return string
*/
function getSystemConfig($section, $key)
{
if (!array_key_exists($section, $this->systemConfig)) {
return false;
}
if (!array_key_exists($key, $this->systemConfig[$section])) {
return false;
}
return $this->systemConfig[$section][$key] ? $this->systemConfig[$section][$key] : false;
}
/**
* Checks if system config is present and is not empty
*
* @return bool
*/
function systemConfigFound()
{
return file_exists($this->INIFile) && $this->systemConfig;
}
/**
* Checks if given section is present in config
*
* @param string $section
* @return bool
*/
function sectionFound($section)
{
return array_key_exists($section, $this->systemConfig);
}
/**
* 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 ();
}
$xml_helper =& $this->Application->recallObject('kXMLHelper');
/* @var $xml_helper kXMLHelper */
$root_node =& $xml_helper->Parse( file_get_contents($info_file) );
if (!is_object($root_node) || !preg_match('/^kxmlnode/i', get_class($root_node)) || ($root_node->Name == 'ERROR')) {
// non-valid xml file
return Array ();
}
$ret = Array ();
$current_node =& $root_node->firstChild;
do {
$ret[ strtolower($current_node->Name) ] = trim($current_node->Data);
} while (($current_node =& $current_node->NextSibling()));
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) . '" title="' . htmlspecialchars($module_name) . '" 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)) {
+ 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)) {
+ if ( isset($category_template) ) {
$category_fields['Template'] = $category_template;
$category_fields['CachedTemplate'] = $category_template;
}
- if (isset($category_icon)) {
+ 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 int $item_type
*/
function linkCustomFields($module_folder, $prefix, $item_type)
{
$module_folder = strtolower($module_folder);
$module_name = $module_folder;
- if ($module_folder == 'kernel') {
+ if ( $module_folder == 'kernel' ) {
$module_name = 'in-portal';
$module_folder = 'core';
}
$db =& $this->Application->GetADODBConnection();
$sql = 'SELECT FieldName, CustomFieldId
FROM ' . TABLE_PREFIX . 'CustomField
WHERE Type = ' . $item_type . ' AND IsSystem = 0'; // config is not read here yet :( $this->Application->getUnitOption('p', 'ItemType');
$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 = "CustomField") 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
*
*/
function deleteCache($refresh_permissions = false)
{
$this->Application->HandleEvent($event, 'adm:OnResetConfigsCache');
$this->Application->HandleEvent($event, 'adm:OnResetSections');
$this->Application->HandleEvent($event, 'c:OnResetCMSMenuCache');
$this->Conn->Query('DELETE FROM ' . TABLE_PREFIX . 'CachedUrls');
if ($refresh_permissions) {
if ($this->Application->ConfigValue('QuickCategoryPermissionRebuild')) {
// refresh permission without progress bar
$updater =& $this->Application->makeClass('kPermCacheUpdater');
/* @var $updater kPermCacheUpdater */
$updater->OneStepRun();
}
else {
// 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->getSystemConfig('Database', 'TablePrefix');
$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 */
$module_name = $this->Application->findModule('Path', rtrim($module_folder, '/') . '/', 'Name');
$themes_helper->syncronizeModule($module_name);
$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($themes_event, '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
+ * @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) {
+ 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) {
+ 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()) {
+ if ( !$primary_skin->isLoaded() ) {
// we always got primary skin, but just in case
- return ;
+ 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) {
+ if ( !$cloned_ids ) {
// can't clone
- return ;
+ return false;
}
$skin =& $this->Application->recallObject('skin.tmp', null, Array ('skip_autoload' => true));
/* @var $skin kDBItem */
- $skin->Load( $cloned_ids[0] );
+ $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) );
+ $version_errors = trim(preg_replace("/(^|\n)(patching file .*?|Hunk #.*?\.)(\n|$)/m", '', $version_output));
- if ($version_errors) {
+ if ( $version_errors ) {
$has_errors = true;
- $output[$version] = trim( preg_replace("/(^|\n)(patching file .*?)(\n|$)/m", '', $output[$version]) );
+ $output[$version] = trim(preg_replace("/(^|\n)(patching file .*?)(\n|$)/m", '', $output[$version]));
}
else {
unset($output[$version]);
}
}
- if (!$has_errors) {
+ 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;
}
}
\ No newline at end of file

Event Timeline