Conn =& $this->Application->GetADODBConnection(); $this->Order = $order; $this->Admin = $this->Application->IsAdmin(); // better cache this value, not to calculate it each time in foreach } function Init($prefix, $special) { $this->AddAllVars(); $this->specialsToRemove = $this->Get('remove_specials'); if($this->specialsToRemove) { $this->_Params = $this->removeSpecials($this->_Params); } ini_set('magic_quotes_gpc', 0); } 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); $this->processQueryString(); break; case 'P': $this->Post = $this->AddVars($_POST); $this->convertPostEvents(); 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; } } if ($this->Application->RewriteURLs()) { if( $this->Application->isDebugMode() ) { $this->Application->Debugger->profileStart('url_parsing'); $this->processRewriteURL(); $description = 'Parsing MOD_REWRITE url (template: '.$this->Get('t').')'; $this->Application->Debugger->profileFinish('url_parsing', $description); } else { $this->processRewriteURL(); } } else { $this->Application->VerifyLanguageId(); $this->Application->VerifyThemeId(); } } function convertFiles() { if (!$_FILES) { return false; } $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 ); } else { $normal_files[$file_name] = $file_info; } } if(!$tmp) return false; $files = $_FILES; $_FILES = Array(); foreach($tmp as $prefix => $prefix_files) { $anchor =& $_FILES; foreach($prefix_files['keys'] as $key) { $anchor =& $anchor[$key]; } foreach($prefix_files['value'] as $field_name) { unset($inner_anchor); unset($copy); $work_copy = $prefix_files['keys']; foreach($file_keys as $file_key) { $inner_anchor =& $files[$prefix][$file_key]; if (isset($copy)) { $work_copy = $copy; } else { $copy = $work_copy; } array_shift($work_copy); 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 * */ function convertPostEvents() { $events = $this->Get('events'); if( is_array($events) ) { foreach ($events as $prefix_special => $event_name) { if($event_name) $this->Set($prefix_special.'_event', $event_name); } } } function finalizeParsing($passed = Array(), $module_params = Array() ) { if ($passed) { $event_manger =& $this->Application->recallObject('EventManager'); foreach ($passed as $passed_prefix) { $event_manger->setQueryMap($passed_prefix); } } // get joined version (env var + mod rewrite parsed) $passed = $this->Application->GetVar('passed'); $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() { // directory_1_2_3/sc1/inlink/detail/3/l1_ka_asd.html $url = $this->Get('_mod_rw_url_'); if( substr($url, -5) == '.html' ) $url = substr($url, 0, strlen($url) - 5 ); $url_parts = $url ? explode('/', $url) : Array(); $process_module = true; if($this->Get('rewrite') == 'on' || !$url_parts) { // set default values $defaults = Array('m_cat_id' => 0, 'm_cat_page' => 1, 'm_opener' => 's'); foreach ($defaults as $default_key => $default_value) { if ($this->Get($default_key) == null) { $this->Set($default_key, $default_value); } } } if(!$url_parts) { $this->Application->Phrases = new PhrasesCache(); $this->Application->VerifyLanguageId(); $this->Application->Phrases->Init('phrases'); $this->Application->VerifyThemeId(); $this->Set('t', $this->getDefaultTemplate('') ); $this->finalizeParsing(Array('m')); return false; } else { $this->Set('t', ''); } $url_part = array_shift($url_parts); // match language $sql = 'SELECT LanguageId FROM '.TABLE_PREFIX.'Language WHERE LOWER(PackName) = '.$this->Conn->qstr($url_part).' AND Enabled = 1'; $language_id = $this->Conn->GetOne($sql); $this->Application->Phrases = new PhrasesCache(); if($language_id) { $this->Set('m_lang', $language_id); $url_part = $url_parts ? array_shift($url_parts) : ''; // prepare next url part for parsing } $this->Application->VerifyLanguageId(); // $this->Get('m_lang') ); // match theme if($url_part) { $sql = 'SELECT ThemeId FROM '.TABLE_PREFIX.'Theme WHERE LOWER(Name) = '.$this->Conn->qstr($url_part).' AND Enabled = 1'; $theme_id = $this->Conn->GetOne($sql); if($theme_id) { $this->Set('m_theme', $theme_id); $url_part = $url_parts ? array_shift($url_parts) : ''; // prepare next url part for parsing } } $this->Application->VerifyThemeId(); // verify anyway - will set default if not found!!! // match category $category_id = 0; if($url_part) { $category_stack = Array(); $category_found = false; $category_path = ''; $rets = Array(); // just in case someone has used this variable before do { $category_path = trim($category_path.'/'.$url_part, '/'); if( preg_match('/(.*)_([\d]+)$/', $category_path, $rets) ) { $category_path = $rets[1]; $this->Set('m_cat_page', $rets[2]); } $sql = 'SELECT CategoryId FROM '.TABLE_PREFIX.'Category WHERE NamedParentPath = '.$this->Conn->qstr($category_path); array_push($category_stack, $this->Conn->GetOne($sql) ); $category_found = end($category_stack); if($category_found) $url_part = array_shift($url_parts); }while ($category_found && $url_part); if (count($category_stack)) { $category_id = array_pop($category_stack); // remove last not found category if($category_id === false) { $category_id = array_pop($category_stack); } if($category_id) { $this->Set('m_cat_id', $category_id); } } elseif (!$category_found && getArrayValue($rets, 2)) { $url_part = array_shift($url_parts); } } if (!$url_part) { // no more parts left in url $process_module = false; $sql = 'SELECT CachedCategoryTemplate FROM '.TABLE_PREFIX.'Category WHERE CategoryId = '.$category_id; $category_template = $this->Conn->GetOne($sql); if ($category_template) { $category_template = explode('/', $category_template); $url_part = array_shift($category_template); $url_parts = $category_template; } else { $url_part = 'index'; } } elseif ($url_part && count($url_parts) <= 1 && $category_id) { // only filename left, no other parts $process_module = false; $sql = 'SELECT ParentPath, CachedItemTemplate, CachedCategoryTemplate FROM '.TABLE_PREFIX.'Category WHERE CategoryId = '.$category_id; $category_data = $this->Conn->GetRow($sql); $root_category_id = array_shift( explode('|', substr($category_data['ParentPath'], 1, -1)) ); $module_info = $this->Application->findModule('RootCat', $root_category_id); if ($module_info) { $module_prefix = $module_info['Var']; $module_event = new kEvent($module_prefix.':ParseEnv', Array('url_parts' => array_merge(Array($url_part), $url_parts)) ); $this->Application->HandleEvent($module_event); if ($module_event->status == erSUCCESS && $this->Get($module_prefix.'_id')) { $item_template = $category_data['CachedItemTemplate']; if ($item_template) { $url_parts = explode('/', $item_template); array_push($url_parts, $url_part); // save item's filename as not processed $url_part = array_shift($url_parts); } } elseif (!$module_event->getEventParam('url_parts')) { // parseEnv has processed that param $url_part = ''; $category_template = $category_data['CachedCategoryTemplate']; if ($category_template) { $category_template = explode('/', $category_template); $url_part = array_shift($category_template); $url_parts = $category_template; } else { $url_part = 'index'; } } } } // match module $next_template = $this->Get('next_template'); if($url_part || $next_template) { if($next_template) { $next_template_parts = explode('/', $next_template); $module_folder = array_shift($next_template_parts); } else { $module_folder = $url_part; } foreach ($this->Application->ModuleInfo as $module_name => $module_data) { if( trim($module_data['TemplatePath'], '/') == $module_folder ) { $module_prefix = $module_data['Var']; break; } } } // match template $template_path = ''; $template_found = false; if($url_part) { // search for template in real template records array_unshift($url_parts, $url_part); $template_parts = $url_parts; $url_parts = Array(); do { $template_path = implode('/', $template_parts); $t_parts['path'] = dirname($template_path) == '.' ? '' : '/'.dirname($template_path); $t_parts['file'] = basename($template_path); $sql = 'SELECT FileId FROM '.TABLE_PREFIX.'ThemeFiles WHERE (FilePath = '.$this->Conn->qstr($t_parts['path']).') AND (FileName = '.$this->Conn->qstr($t_parts['file'].'.tpl').')'; // $sql = 'SELECT FileId FROM '.TABLE_PREFIX.'ThemeFiles WHERE CONCAT(FilePath, "/", FileName) = '.$this->Conn->qstr('/'.$template_path.'.tpl'); $template_found = $this->Conn->GetOne($sql); if(!$template_found) { array_unshift( $url_parts, array_pop($template_parts) ); } }while (!$template_found && $template_parts); // try to find template in virtual templates in case if such ability exists if ($this->Application->isModuleEnabled('In-CMS') && !$template_found) { $template_parts = $url_parts; $url_parts = Array(); do { $template_path = implode('/', $template_parts); $sql = 'SELECT PageId FROM '.TABLE_PREFIX.'Pages WHERE Path = '.$this->Conn->qstr($template_path); $template_found = $this->Conn->GetOne($sql); if(!$template_found) { array_unshift( $url_parts, array_pop($template_parts) ); } }while (!$template_found && $template_parts); } } // guess template if no existing template found if(!$template_found && isset($module_folder) && $module_folder) { // 1. try index template of module $sql = 'SELECT FileId FROM '.TABLE_PREFIX.'ThemeFiles WHERE CONCAT(FilePath, "/", FileName) = '.$this->Conn->qstr('/'.$module_folder.'/index.tpl'); $template_found = $this->Conn->GetOne($sql); if($template_found) { $template_path = $module_folder.'/index'; array_shift($url_parts); } else { // 2. return error template then $template_found = true; $template_path = $this->Application->ConfigValue('ErrorTemplate'); if(!$template_path) $template_path = 'error_notfound'; header('HTTP/1.0 404 Not Found'); } } $this->Set('t', $this->getDefaultTemplate($template_found ? $template_path : '') ); // pass params left to module $this->Application->Phrases->Init('phrases'); $passed = Array('m'); $module_params = Array(); if ( isset($module_prefix) ) { $passed[] = $module_prefix; $module_event = new kEvent($module_prefix.':ParseEnv', Array('url_parts' => $url_parts) ); if ($process_module) { $this->Application->HandleEvent($module_event); } $item_id = $this->Get($module_prefix.'_id'); $module_params = Array($module_prefix.'_id' => $item_id ? $item_id : '0' ); if ($module_event->status == erFAIL) { $not_found = $this->Application->ConfigValue('ErrorTemplate'); $this->Set('t', $not_found ? $not_found : 'error_notfound' ); } } $this->finalizeParsing($passed, $module_params); } function getDefaultTemplate($t) { $t = $this->getTemplateName( trim($t, '/') ); if (!$t) $t = 'index'; return trim($t, '/'); } function extractSIDAndTemplate(&$parts) { if ( defined('INPORTAL_ENV') && INPORTAL_ENV ) // SID-TEMPLATE { $sub_parts = array_shift($parts); list($sid, $t) = explode('-', $sub_parts, 2); // Save Session ID if($sid) { $this->Set('sid', $sid); $this->Get['sid'] = $sid; } // Save Template Name $this->Set('t', $this->getDefaultTemplate($t) ); } else // SID:TEMPLATE { // Save Session ID $sid = array_shift($parts); if ($sid) $this->Set('sid', $sid); // Save Template Name $t = array_shift($parts); $this->Set('t', $this->getDefaultTemplate($t) ); } } /** * Process QueryString only, create * events, ids, based on config * set template name and sid in * desired application variables. * * @access private */ function processQueryString() { // env=SID:TEMPLATE:m-1-1-1-1:l0-0-0:n-0-0-0:bb-0-0-1-1-1-0 $env_var = $this->Get(ENV_VAR_NAME); if($env_var) { $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') ) { $this->extractSIDAndTemplate($parts); } if($parts) { $query_maps = Array(); $event_manger =& $this->Application->recallObject('EventManager'); $passed = Array(); foreach($parts as $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); $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 = $event_manger->setQueryMap($prefix_special); // 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; $this->Set($prefix_special.'_'.$var_name, $val); } } } } } else { $t=$this->getTemplateName('index'); $this->Set('t', $t); } } /** * Decides what template name to * use from $_GET or from $_POST * * @param string $querystring_template * @return string * @access private */ function getTemplateName($querystring_template) { $t_from_post = $this->Get('t'); $t = $t_from_post ? $t_from_post : $querystring_template; if ( is_numeric($t) ) { $t = $this->Conn->GetOne(' SELECT CONCAT(FilePath, \'/\', FileName) FROM '.TABLE_PREFIX.'ThemeFiles WHERE FileId = '.$t); } $t = preg_replace('/\.tpl$/', '', $t); return $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) { $this->_Params = array_merge_recursive2($this->_Params, Array($key=>$value)); } return $array; } function StripSlashes($array) { //if( !get_magic_quotes_gpc() ) return $array; foreach($array as $key=>$value) { if( is_array($value) ) { $array[$key] = $this->StripSlashes($value); } else { // $value = iconv('UTF-8', 'windows-1257', $value); if( get_magic_quotes_gpc() ) $value = stripslashes($value); if(!$this->Admin) $value = htmlspecialchars($value); $array[$key] = $value; } //$array[$key]=is_array($value)?$this->StripSlashes($value):stripslashes($value); } return $array; } /** * Returns all $_GET array excluding system parameters, that are not allowed to be passed through generated urls * * @return Array */ function getRedirectParams() { $unset_vars = Array(ENV_VAR_NAME, 'rewrite', '_mod_rw_url_', 'Action'); $ret = $this->Get; foreach ($unset_vars as $var_name) { if( isset($ret[$var_name]) ) unset( $ret[$var_name] ); } return $ret; } function writeRequestLog($filename) { $folder_path = dirname(FULL_PATH.'/'.$filename); if (is_writable($folder_path)) { $fp = fopen(FULL_PATH.'/'.$filename, 'a'); if ($fp) { $session =& $this->Application->recallObject('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"; $data .= "_GET:\n".print_r($this->Get, true)."_POST:\n".print_r($this->Post, true)."_COOKIE:\n".print_r($this->Cookie, true)."\n".str_repeat('=', 100)."\n"; fwrite($fp, $data); fclose($fp); } else { trigger_error('Requrest Log directory not writable', E_USER_WARNING); } } else { trigger_error('Requrest Log directory not writable', E_USER_WARNING); } } } ?>