QFox2/Fox2.php
author foxel
Wed, 30 Dec 2009 16:25:48 +0600
changeset 34 0e05a7ed0206
parent 33 fe2836c510cf
child 35 1d100abd8030
permissions -rw-r--r--
модификация процесса обработки redefined.vis
для корректного переопределения блоков из частей стиля, отличных от common
Для указания переопределяемой части нужно вставить строку "<<part '{имя части}'>>' перед блоками, фактически переопределяющими эту часть стиля
     1 <?php
     2 
     3 // -------------------------------------------------------------------------- \\
     4 // This is the main QuickFox 2 file                                           \\
     5 //                                                       (c) LION 2007 - 2008 \\
     6 // -------------------------------------------------------------------------- \\
     7 
     8 // Simple InUSE Check
     9 if ( !defined('QF_STARTED') )
    10         die('Hacking attempt');
    11 
    12 if (defined('QF_FOX_STARTED'))
    13     die('Scripting error');
    14 
    15 define('QF_FOX_STARTED', true);
    16 
    17 define('QUICKFOX_DIR', dirname(__FILE__).'/');
    18 
    19 // data directories constants
    20 define('QF_INC_ROOT',    QF_DATA_ROOT.'includes/');
    21 define('QF_MODULES_DIR', QF_DATA_ROOT.'modules/');
    22 define('QF_PAGES_DIR',   QF_INC_ROOT.'pages/');
    23 define('QF_SCRIPTS_DIR', QF_INC_ROOT.'scripts/');
    24 
    25 // Defining some usefull constants
    26 define('QF_FOX2_LOGIN_MASK', '^[0-9\w_\+\-=\(\)\[\] ]{3,16}$');
    27 define('QF_FOX2_SERVICE_PERIOD', 60);  // 1 minute service check period
    28 define('QF_FOX2_RESULT_LIFETIME', 3600);
    29 define('QF_FOX2_MAXULEVEL', 7);
    30 
    31 define('QF_FOX2_URLTEMPS_CACHENAME', 'FOX2.URLTEMPS');
    32 define('QF_FOX2_URLTEMPS_RW_CACHENAME', 'FOX2.RW_URLTEMPS');
    33 
    34 class Fox2
    35 {
    36     var $URL_temps;
    37     var $URL_domain = null;
    38     var $VIS_redefs = Array();
    39     var $err_traced = Array();
    40 
    41     function Fox2()
    42     {
    43         global $QF;
    44         if ($CL_Config = qf_file_get_carray(QUICKFOX_DIR.'modules.qfc'))
    45         {
    46             foreach ($CL_Config as $mod => $cfg)
    47             {
    48                 $cfg = explode('|', $cfg);
    49                 if (isset($cfg[1]) && $cfg[1])
    50                     $QF->Register_Module($mod, QUICKFOX_DIR.trim($cfg[1]), trim($cfg[0]));
    51                 else
    52                     $QF->Register_Module($mod, QUICKFOX_DIR.trim($cfg[0]));
    53             }
    54         }
    55 
    56     }
    57 
    58     function _Start()
    59     {
    60         global $QF;
    61         $GLOBALS['FOX'] =& $this;
    62 
    63         //qf_file_put_contents('acc'.$QF->Timer->time, qf_array_definition($_REQUEST));
    64 
    65         $QF->Timer->Time_Log('FOX2 module started');
    66 
    67         if ($QF->GPC->Get_Bin('drop_cache', QF_GPC_GET))
    68             $QF->Cache->Clear();
    69 
    70         $QF->LNG->Load_Language('common');
    71         //$QF->Config->Set('linked_domains', Array('gallery' => 'www.quickfox.dev'), 'fox2');
    72         //$QF->Config->Set('basic_domain', 'quickfox.dev', 'fox2');
    73 
    74         // starting neded modules
    75         $QF->Run_Module('DSets');
    76         $QF->Run_Module('User');
    77 
    78         $revision = $QF->DSets->Get_DSet_Value('dev_rev_info', 'qf2');
    79         header('X-Powered-By: QuickFox 2 ['.$revision.'] (PHP/'.PHP_VERSION.')');
    80 
    81         // url reparsing
    82         $rw_id = $QF->GPC->Get_String('rw_id', QF_GPC_GET, QF_STR_WORD);
    83         $rw_data = $QF->GPC->Get_String('rw_data', QF_GPC_GET, QF_STR_LINE);
    84         // working with multidomain
    85         if ($QF->Config->Get('tgl_multidomain', 'fox2'))
    86         {
    87             $d_schemas = $QF->Config->Get('domain_schemas', 'fox2');
    88             $domain = strtolower($QF->HTTP->SrvName);
    89             if (!is_array($d_schemas))
    90                 $d_schemas = Array();
    91 
    92             if (!isset($d_schemas[$domain]))
    93             {
    94                 $d_schemas[$domain] = '';
    95                 $QF->Config->Set('domain_schemas', $d_schemas, 'fox2', true);
    96             }
    97             elseif ($d_schemas[$domain])
    98                 $QF->Config->Select_Scheme($d_schemas[$domain]);
    99 
   100             if (($domains = $QF->Config->Get('linked_domains', 'fox2'))
   101                 && ($basic_domain = $QF->Config->Get('basic_domain', 'fox2'))
   102                 && is_array($domains) && count($domains))
   103             {
   104                 list($link) = array_keys($domains, $domain);
   105                 if ($link)
   106                 {
   107                     $this->URL_domain = $basic_domain;
   108                     if ($rw_id && $rw_id != $link)
   109                         $rw_data = $rw_id.'/'.$rw_data;
   110                     $rw_id = $link;
   111                 }
   112             }
   113         }
   114         // rewrited url reparsing
   115         if ($rw_id)
   116         {
   117             $this->_Parse_RW($rw_id, $rw_data);
   118             $QF->Events->Set_On_Event('HTTP_HTML_parse', Array(&$this, 'HTML_FullURLs') );
   119             $QF->Events->Set_On_Event('HTML_block_parse', Array(&$this, 'HTML_FullURLs') );
   120         }
   121 
   122         // running the services
   123         $next_start = $QF->Config->Get('service_nextstart', 'temp');
   124         if ($next_start < $QF->Timer->time)
   125         {
   126             $QF->Run_Module('Services');
   127             $QF->Config->Set('service_nextstart', ($QF->Timer->time + QF_FOX2_RESULT_LIFETIME), 'temp', true);
   128         }
   129 
   130         // registering extended modules
   131         if ($r_mods = $QF->DSets->Get_DSet('fox_modules'))
   132             foreach ($r_mods as $r_mod => $r_cfg)
   133             {
   134                 $r_cfg = explode('|', $r_cfg);
   135                 if (isset($r_cfg[1]) && $r_cfg[1])
   136                     $QF->Register_Module($r_mod, QF_MODULES_DIR.trim($r_cfg[1]), trim($r_cfg[0]));
   137                 else
   138                     $QF->Register_Module($r_mod, QF_MODULES_DIR.trim($r_cfg[0]));
   139             }
   140 
   141         //$this->_Parse_PathInfo();
   142 
   143         if ($domain = $QF->Config->Get('cookie_domain'))
   144             $QF->HTTP->Set_Cookie_Domain($domain);
   145 
   146         $QF->Events->Set_On_Event('VIS_PreParse',  Array(&$this, 'On_VIS_Prep') );
   147         $QF->Events->Set_On_Event('EJS_PreParse',  Array(&$this, 'On_EJS_Prep') );
   148         if ($QF->Config->Get('vis_redefined', 'fox2'))
   149             $QF->Events->Set_On_Event('VIS_RawParse',  Array(&$this, '_VISUserMods_Add') );
   150 
   151         // running any autoruns from packages
   152         if ($ar_datas = $QF->DSets->Get_DSet('fox_autoruns'))
   153             foreach ($ar_datas as $arun)
   154                 if (isset($arun['module'], $arun['method']))
   155                 {
   156                     $QF->Run_Module($arun['module']);
   157                     qf_func_call(Array(&$QF->$arun['module'], $arun['method']));
   158                 }
   159 
   160 
   161         // first try to determine if we are running a datapage
   162         if ($data_page = $QF->GPC->Get_String('sr', QF_GPC_GET, QF_STR_WORD))
   163             $this->Run_Data($data_page);
   164 
   165         // open session
   166         $QF->Session->Open_Session();
   167         if ($QF->Config->Get('sid_urls', 'session', true) && $QF->Config->Get('cookie_check', 'session') && $QF->Session->Get_Status(QF_SESSION_USEURL | QF_SESSION_LOADED) == QF_SESSION_USEURL)
   168             $QF->HTTP->Redirect($QF->HTTP->Request);
   169 
   170         // then try to determine if we are running an AJAX parser
   171         if ($aj_page = $QF->GPC->Get_String('aj', QF_GPC_GET, QF_STR_WORD))
   172             $QF->User->is_spider || $this->Run_AJAX($aj_page);
   173 
   174         // then try to run script if needed
   175         if ($script = $QF->GPC->Get_String('script', QF_GPC_POST, QF_STR_WORD))
   176             $QF->User->is_spider || $this->Run_Script($script);
   177 
   178         // if we don't need to run scripts or datapages we will draw a normal page
   179         $QF->Run_Module('VIS');
   180         $vis_style = $QF->Config->Get('vis_style', '', 'qf_def');
   181         $vis_style = explode('|', $vis_style);
   182         if (!isset($vis_style[1]))
   183             $vis_style[1] = QF_KERNEL_VIS_COMMON;
   184         $QF->VIS->Configure(Array(
   185             'root_node' => 'GLOBAL_HTMLPAGE',
   186             'style' => $vis_style[0],
   187             'CSS' => $vis_style[1],
   188             ), false);
   189 
   190 
   191         if ($QF->Config->Get('css_separate'))
   192         {
   193             $QF->VIS->Configure(Array('force_append' => false));
   194             $QF->VIS->Add_Data(0, 'META', '<link rel="stylesheet" type="text/css" href="'.$this->Gen_URL('fox2_css_data', $vis_style, true).'" />');
   195         }
   196         $this->Link_JScript('common');
   197         $this->Link_JScript('effects');
   198 
   199         $QF->VIS->Set_VConsts(Array('MAXULEVEL' => QF_FOX2_MAXULEVEL));
   200         $QF->VIS->Load_Templates();
   201 
   202         $page = $QF->GPC->Get_String('st', QF_GPC_GET, QF_STR_WORD);
   203 
   204         if (!$page)
   205             $page = $QF->Config->Get('index_dpage', 'fox2', 'pages');
   206 
   207         $this->Show_Page($page);
   208     }
   209 
   210     function Run_Data($name)
   211     {
   212         global $QF;
   213 
   214         if (connection_aborted())
   215             trigger_error('FOX: connection was aborted at client side', E_USER_WARNING);
   216         if (($data = $QF->DSets->Get_DSet_Value('fox_datapages', $name)) && isset($data['module'], $data['method']))
   217         {
   218             $QF->Run_Module($data['module']);
   219             qf_func_call(Array(&$QF->$data['module'], $data['method']));
   220             trigger_error('FOX: datapage "'.$name.'" did not end execution by it\'s own', E_USER_NOTICE);
   221         }
   222         else
   223             trigger_error('FOX: there is no "'.$name.'" datapage data', E_USER_WARNING);
   224 
   225         header ($QF->HTTP->SERVER["SERVER_PROTOCOL"].' 501 Not Implemented');
   226         $QF->HTTP->Redirect(($QF->HTTP->Referer && !$QF->HTTP->ExtRef) ? $QF->HTTP->Referer : QF_INDEX);
   227     }
   228 
   229     function Run_AJAX($name)
   230     {
   231         global $QF;
   232 
   233         $AJAX_DATA = null;
   234         $AJAX_STATUS = 200;
   235 
   236         if (connection_aborted())
   237             trigger_error('FOX: connection was aborted at client side', E_USER_WARNING);
   238         elseif ($name == 'PING') // session pinger works
   239         {
   240 
   241         }
   242         elseif (($data = $QF->DSets->Get_DSet_Value('fox_ajax_scripts', $name)) && isset($data['module'], $data['method']))
   243         {
   244             $QF->Run_Module($data['module']);
   245             $AJAX_DATA = qf_func_call_ref(Array(&$QF->$data['module'], $data['method']), $AJAX_STATUS);
   246         }
   247         else
   248         {
   249             trigger_error('FOX: there is no "'.$name.'" AJAX parser file', E_USER_WARNING);
   250             $AJAX_STATUS = 404;
   251             $AJAX_DATA = 'Parser Not Found';
   252         }
   253 
   254         $AJAX_DATA = '{ status: '.intval($AJAX_STATUS).', data: '.qf_value_JS_definition($AJAX_DATA).' }';
   255         if ($QF->GPC->Get_String('AJMethod', QF_GPC_GET, QF_STR_WORD) == 'form')
   256         {
   257             $AJAX_ID = $QF->GPC->Get_String('AJID', QF_GPC_GET, QF_STR_WORD);
   258             $AJAX_DATA = 'top && top.QF_AJAX && top.QF_AJAX.Form_Ready(\''.$AJAX_ID.'\', '.$AJAX_DATA.' );';
   259             $AJAX_DATA = '<?xml version="1.0"?>
   260                 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
   261                 <html xmlns="http://www.w3.org/1999/xhtml">
   262                 <head><!--Meta-Content-Type--></head><body><script type="text/javascript">
   263                 // JavaScript Starts Here <![CDATA[
   264                 '.$AJAX_DATA.'
   265                 //]]> JavaScript ends here
   266                 </script></body></html>';
   267         }
   268         else
   269             $QF->HTTP->do_HTML = false;
   270 
   271         $QF->HTTP->Clear();
   272         $QF->HTTP->Write($AJAX_DATA);
   273         $QF->HTTP->Send_Buffer($QF->Session->Get('recode_out'));
   274     }
   275 
   276     function Run_Script($name)
   277     {
   278         global $QF;
   279 
   280         if (connection_aborted())
   281             trigger_error('FOX: connection was aborted at client side', E_USER_WARNING);
   282         elseif ($data = $QF->DSets->Get_DSet_Value('fox_scripts', $name))
   283         {
   284             Ignore_User_Abort(True);
   285 
   286             $result = false;
   287 
   288             if (isset($data['module'], $data['method']))
   289             {
   290                 $QF->Run_Module($data['module']);
   291                 $result = qf_func_call(Array(&$QF->$data['module'], $data['method']));
   292                 if (!$result)
   293                 {
   294                     trigger_error('FOX: script "'.$file.'" did not set result by it\'s own', E_USER_NOTICE);
   295                     $result = Array(Lang('SCR_FINISHED_NORES'));
   296                 }
   297                 elseif (!is_array($result))
   298                     $result = Array($result);
   299             }
   300             elseif (isset($data['include']) && ($file = QF_SCRIPTS_DIR.$data['include'].'.php') && file_exists($file))
   301             {
   302                 include ($file);
   303                 trigger_error('FOX: script-include "'.$file.'" did not end execution by it\'s own', E_USER_NOTICE);
   304                 $result = Array(Lang('SCR_FINISHED_NORES'));
   305             }
   306             else
   307             {
   308                 $result = Array(Lang('ERR_SCR_CODE_NOTFOUND'), '', true);
   309                 trigger_error('FOX: there is no "'.$name.'" script data', E_USER_WARNING);
   310             }
   311 
   312             call_user_func_array(Array(&$this, 'Set_Result'), $result);
   313         }
   314         else
   315             trigger_error('FOX: there is no "'.$name.'" script data', E_USER_WARNING);
   316     }
   317 
   318     function Show_Page($pg)
   319     {
   320         global $QF;
   321 
   322         // running prepage autoruns from packages
   323         if ($ar_datas = $QF->DSets->Get_DSet('fox_prepages'))
   324             foreach ($ar_datas as $arun)
   325                 if (isset($arun['module'], $arun['method']))
   326                 {
   327                     $QF->Run_Module($arun['module']);
   328                     qf_func_call(Array(&$QF->$arun['module'], $arun['method']), $pg);
   329                 }
   330 
   331         $QF->VIS->Add_Data_Array(0, Array(
   332             'SITE_NAME' => ($site_name = $QF->Config->Get('site_name')) ? htmlspecialchars($site_name) : 'QuickFox 2',
   333             ) );
   334         if ($adv = $QF->Config->Get('adv_data', 'visual'))
   335             $QF->VIS->Add_Data(0, 'ADV', $adv);
   336 
   337 
   338         if ((list($data, $pkg) = $QF->DSets->Get_DSet_Value('fox_pages', $pg, true)) && isset($data['module'], $data['method']))
   339         {
   340             // redirecting on multidomain redirection enabled
   341             if ($QF->Config->Get('tgl_multidomain', 'fox2') && $QF->Config->Get('tgl_mdomain_redir', 'fox2') && ($domains = $QF->Config->Get('package_domains', 'fox2')) && is_array($domains) && count($domains))
   342             {
   343                 if (isset($domains[$pkg]))
   344                 {
   345                     $domain = $domains[$pkg];
   346                     $domain = preg_replace('#^http\:(?:/)*|/$#i', '', $domain);
   347                     if ($domain != $QF->HTTP->SrvName)
   348                     {
   349                         $url = qf_full_url($QF->HTTP->Request, false, $domain);
   350                         $QF->HTTP->Redirect($url);
   351                     }
   352                 }
   353             }
   354 
   355             $QF->Run_Module($data['module']);
   356             $p_subtitle = $p_title = '';
   357             $d_result = false;
   358             $d_status = 200;
   359             $pg_node = qf_func_call_arr(Array(&$QF->$data['module'], $data['method']), Array(&$p_title, &$p_subtitle, &$d_result, &$d_status));
   360             if ($pg_node)
   361                 $QF->VIS->Append_Node($pg_node, 'PAGE_CONT', 0);
   362             if ($p_title)
   363                 $QF->VIS->Add_Data(0, 'PAGE_TITLE', $p_title);
   364             if ($p_subtitle)
   365                 $QF->VIS->Add_Data(0, 'PAGE_SUBTITLE', $p_subtitle);
   366             if ($d_status != 200)
   367                 $QF->HTTP->Set_Status($d_status);
   368             if ($d_result && is_array($d_result))
   369                 call_user_method_array('Draw_Result', $this, $d_result);
   370         }
   371         elseif (($file = QF_PAGES_DIR.$pg.'_inc.php') && file_exists($file))
   372             include ($file);
   373         else
   374         {
   375             $this->Draw_Result(Lang('ERR_INCLUDE_LOAD'), ($QF->HTTP->Referer && !$QF->HTTP->ExtRef) ? $QF->HTTP->Referer : QF_INDEX, true);
   376             $QF->HTTP->Set_Status(404);
   377         }
   378 
   379 
   380         $this->Draw_menu();
   381         if ($QF->User->UID || ($QF->Config->Get('show_login_pan') && !$QF->User->is_spider))
   382             $this->Draw_Panel('login');
   383 
   384 
   385         if ($recode = $QF->GPC->Get_String('recode', QF_GPC_GET, QF_STR_WORD))
   386             $QF->Session->Set('recode_out', $recode);
   387         elseif ($recode !== null)
   388             $QF->Session->Drop('recode_out');
   389 
   390         // running postpage autoruns from packages
   391         if ($ar_datas = $QF->DSets->Get_DSet('fox_postpages'))
   392             foreach ($ar_datas as $arun)
   393                 if (isset($arun['module'], $arun['method']))
   394                 {
   395                     $QF->Run_Module($arun['module']);
   396                     qf_func_call(Array(&$QF->$arun['module'], $arun['method']), $pg, $pg_node);
   397                 }
   398 
   399         $QF->HTTP->Clear();
   400         $QF->HTTP->Write($QF->VIS->Make_HTML());
   401         $QF->HTTP->Send_Buffer($QF->Session->Get('recode_out'));
   402     }
   403 
   404     function Draw_Menu()
   405     {
   406         global $QF;
   407         $menubts = $QF->Config->Get('menu_buttons', 'visual');
   408         $cur_butt = false;
   409         if (is_array($menubts))
   410             foreach ($menubts as $butt)
   411                 if (is_array($butt) && qf_str_is_url($butt['url']))
   412                 {
   413                     $butt['url'] = qf_full_url($butt['url'], true, $this->URL_domain);
   414                     if (isset($butt['is_sub']) && $butt['is_sub'] && $cur_butt)
   415                         $QF->VIS->Add_Node('MENU_SUBBUTTON', 'SUBS', $cur_butt, $butt);
   416                     else
   417                         $cur_butt = $QF->VIS->Add_Node('MENU_BUTTON', 'MENU_ITEMS', 0, $butt);
   418                 }
   419     }
   420 
   421     function Draw_Panel($name)
   422     {
   423         global $QF;
   424 
   425 
   426         if (($data = $QF->DSets->Get_DSet_Value('fox_panels', $name)) && isset($data['module'], $data['method']))
   427         {
   428             $CUR_PANEL = $QF->VIS->Add_Node('PANEL_BODY', 'PANELS', 0, false, $name.'_panel');
   429             $QF->Run_Module($data['module']);
   430             qf_func_call(Array(&$QF->$data['module'], $data['method']), $CUR_PANEL);
   431             return true;
   432         }
   433         else
   434             return false;
   435     }
   436 
   437 
   438     function Gen_ASCode()
   439     {
   440         global $QF;
   441 
   442         $new_code = qf_short_uid();
   443 
   444         if ($QF->Session->Set('QFox2_ASCode', $new_code))
   445             return $new_code;
   446 
   447         return false;
   448     }
   449 
   450     function Check_ASCode($code)
   451     {
   452         global $QF;
   453 
   454         $real_code = $QF->Session->Get('QFox2_ASCode');
   455 
   456         return ($code == $real_code);
   457     }
   458 
   459 
   460     function Set_Result($text, $redir_to = '', $is_err = false, $code = '')
   461     {
   462         global $QF;
   463 
   464         $res_id = qf_short_uid('res');
   465 
   466         $QF->DBase->Do_Delete('results', 'WHERE time < '.($QF->Timer->time - QF_FOX2_RESULT_LIFETIME));
   467         if (!$QF->Session->SID)
   468             $QF->Session->Open_Session();
   469         $ins_data = Array(
   470             'res_id'   => $res_id,
   471             'code'     => $code,
   472             'text'     => (string) $text,
   473             'is_err'   => ($is_err) ? 1 : 0,
   474             'tr_errs'  => ($is_err) ? implode('|', qf_array_parse($this->err_traced, 'dechex')) : '',
   475             'time'     => $QF->Timer->time,
   476             'got_at'   => $QF->HTTP->Request,
   477             'redir_to' => $redir_to,
   478             'u_sid'    => $QF->Session->SID,
   479             'u_id'     => $QF->User->UID,
   480             );
   481 
   482         if ($QF->DBase->Do_Insert('results', $ins_data))
   483             $QF->HTTP->Redirect($this->Gen_URL('fox2_showresult_page', Array($res_id)));
   484         else
   485         {
   486             trigger_error('FOX: error setting result data', E_USER_WARNING);
   487             // if user must be regirected we'll try do this without setting result data
   488             if ($redir_to)
   489                 $QF->HTTP->Redirect($redir_to);
   490         }
   491 
   492     }
   493 
   494     function Trace_Error($err_code)
   495     {
   496         global $QF;
   497         $this->err_traced[] = (int) $err_code;
   498         if ($QF->Config->Get('log_errors', 'loggers', 1))
   499             $this->Log_Event($err_code, 'err_log');
   500     }
   501 
   502     function Link_JScript($name)
   503     {
   504         global $QF;
   505         static $separate = null;
   506         static $linked = Array();
   507 
   508         if (!isset($QF->VIS))
   509             return false;
   510 
   511         if (is_null($separate))
   512             $separate = (bool) $QF->Config->Get('css_separate');
   513 
   514         if (in_array($name, $linked))
   515             return true;
   516 
   517         $linked[] = $name;
   518 
   519         if ($separate)
   520             return $QF->VIS->Add_Data(0, 'JS_BLOCKS', '<script type="text/javascript" src="'.$this->Gen_URL('fox2_js_data', $name, true).'" ></script>');
   521         else
   522             return $QF->VIS->Load_EJS($name);
   523     }
   524 
   525     function Describe_ErrCodes($descr_errs = false)
   526     {
   527         global $QF;
   528         if (!$descr_errs)
   529             $descr_errs = $this->err_traced;
   530 
   531         if (is_array($descr_errs) && count($descr_errs))
   532         {
   533             $output = Array();
   534             $packs = Array();
   535             foreach ($descr_errs as $err_code)
   536             {
   537                 $err_code = '0x'.dechex($err_code);
   538                 $info = $pkg = null;
   539                 list ($info, $pkg) = $QF->DSets->Get_DSet_Value('err_messages', $err_code, true);
   540 
   541                 if (!$info)
   542                     continue;
   543                 if (isset($info['sys']) && $info['sys'])
   544                     continue;
   545 
   546                 if (!in_array($pkg, $packs))
   547                 {
   548                     array_push($packs, $pkg);
   549                     $QF->LNG->Load_Language($pkg.'_errs');
   550                 }
   551 
   552 
   553                 $message = $info['mess'];
   554                 if (substr($message, 0, 2) == 'L_')
   555                     $message = $QF->LNG->Lang(substr($message, 2));
   556 
   557                 $output[$err_code] = $message;
   558             }
   559 
   560             return $output;
   561         }
   562         return false;
   563     }
   564 
   565     function Draw_Result($text, $redir_to = '', $is_err = false, $descr_errs = false)
   566     {
   567         global $QF;
   568 
   569         $hurl = '';
   570 
   571         if ($redir_to)
   572         {
   573             $url = qf_full_url($redir_to, false, $this->URL_domain);
   574             $QF->Events->Call_Event_Ref('HTTP_URL_Parse', $url );
   575             $hurl = strtr($url, Array('&' => '&amp;'));
   576 
   577             header('Refresh: 7; URL="'.$url.'"');
   578         }
   579         $res_node = $QF->VIS->Add_Node('FOX_RESULT_WINDOW', 'PAGE_CONT', 0, Array(
   580             'text'    => $text,
   581             'is_err'  => ($is_err) ? 1 : null,
   582             'redir_url' => $hurl,
   583             ) );
   584         $QF->VIS->Add_Data(0, 'PAGE_TITLE', ($is_err) ? Lang('RES_CAPT_ERR') : Lang('RES_CAPT'));
   585 
   586         if ($descr_errs = $this->Describe_ErrCodes($descr_errs))
   587         {
   588             $err_nodes = Array();
   589             foreach($descr_errs as $code => $mess)
   590                 $err_nodes[] = Array('ERRCODE' => $code, 'MESSAGE' => $mess);
   591 
   592             if (count($err_nodes))
   593                 $QF->VIS->Add_Node_Array('FOX_RESULT_ERR_ITEM', 'ERRORS', $res_node, $err_nodes);
   594         }
   595     }
   596 
   597     function Gen_Pages($pages, $cur_page, $params = false)
   598     {
   599         if (!is_array($params))
   600             $params = Array();
   601 
   602         if ($pages < 2)
   603             return false;
   604 
   605         $cur_page = max(1, min($cur_page, $pages));
   606 
   607         $draw_pages = Array();
   608         $pp = False;
   609         for($stt = 1; $stt <= $pages; $stt++)
   610         {
   611             if ($stt <= 4 || $stt >= ($pages - 3) || Abs($stt - $cur_page) < 3) {
   612                 $draw_pages[] = $params + Array('PAGE' => $stt, 'CUR' => ($stt == $cur_page) ? true : null);
   613                 $pp = true;
   614             }
   615             elseif ($pp)
   616             {
   617                 $draw_pages[] = Array('SEPAR' => true);
   618                 $pp = false;
   619             }
   620         }
   621 
   622         return $draw_pages;
   623     }
   624 
   625     function Log_Event($event, $log_name = 'common')
   626     {
   627         global $QF;
   628         $log_name = substr(preg_replace('#\W#', '_', $log_name), 0, 32);
   629         $ins_data = Array(
   630             'log_id'    => $log_name,
   631             'time'      => $QF->Timer->time,
   632             'event'     => $event,
   633             'cl_ip'     => $QF->HTTP->IP_int,
   634             'cl_req'    => $QF->HTTP->Request,
   635             'cl_uagent' => $QF->HTTP->UAgent,
   636             );
   637         return $QF->DBase->Do_Insert('fox_logs', $ins_data);
   638     }
   639 
   640     function Gen_URL($url_id, $params = Array(), $with_amps = false, $no_enc = false)
   641     {
   642         global $QF;
   643         static $vars;
   644         if (!$vars)
   645             $vars = Array(
   646                 '{QF_SID}' => $QF->Session->SID,
   647                 '{TIME}' => $QF->Timer->time,
   648                 );
   649 
   650         if (!is_array($params))
   651             $params = Array($params);
   652         if (!$url_id)
   653             return QF_INDEX;
   654 
   655         $url_id = strtoupper($url_id);
   656 
   657         if (!is_array($this->URL_temps))
   658             $this->_Load_URLS();
   659 
   660         if (isset($this->URL_temps[$url_id]))
   661         {
   662             if (!$no_enc)
   663                 $params = qf_array_parse($params, 'qf_url_encode_part');
   664 
   665             $string = strtr($this->URL_temps[$url_id], $vars);
   666             $masks = Array();
   667             $a_params = $r_params = Array();
   668 
   669             $count = preg_match_all('#\%(\d+)\$|\%\w#', $string, $masks, PREG_PATTERN_ORDER);
   670             if ($count > 0)
   671             {
   672                 $masks = $masks[1];
   673                 $count = max($count, max($masks));
   674                 $r_params = explode('|', str_repeat('|', $count));
   675             }
   676 
   677             foreach($params as $key => $param)
   678             {
   679                 if (is_int($key))
   680                     $r_params[$key] = $param;
   681                 else
   682                     $a_params[] = qf_url_encode_part($key).'='.$param;
   683             }
   684             $string = qf_sprintf_arr($string, $r_params);
   685             if (count($a_params))
   686                 $string.= ((strstr($string, '?')) ? '&' : '?').implode('&', $a_params);
   687 
   688             $string = ($with_amps) ? preg_replace('#\&(?![A-z]+;)#', '&amp;', $string) : str_replace('&amp;', '&', $string);
   689             return $string;
   690         }
   691         else
   692             return '#';
   693     }
   694 
   695     function On_VIS_Prep(&$indata, $type = false)
   696     {
   697         if (!is_array($this->URL_temps))
   698             $this->_Load_URLS();
   699 
   700         $indata = preg_replace_callback('#\{(?>(F|R)?URL:((?:\w+|\"[^\"]+\"|\|)+))\}#',Array(&$this, '_VISParse_URL_CB'),$indata);
   701     }
   702 
   703     function On_EJS_Prep(&$indata, $type = false)
   704     {
   705         if (!is_array($this->URL_temps))
   706             $this->_Load_URLS();
   707 
   708         $indata = preg_replace_callback('#\{(?>(F|R)?URL:((?:\w+|\"[^\"]+\"|\|)+))\}#',Array(&$this, '_VISParse_URL_CB'),$indata);
   709     }
   710 
   711     function _VISUserMods_Add(&$indata, $style, $part)
   712     {
   713         global $QF;
   714 
   715         if (!isset($this->VIS_redefs[$style]) && !$this->_VISUserMods_Preload($style))
   716             return;
   717 
   718         if (isset($this->VIS_redefs[$style]) && isset($this->VIS_redefs[$style][$part]))
   719             $indata.= $this->VIS_redefs[$style][$part];
   720     }
   721 
   722     /*function _VISUserMods_Preload()
   723     {
   724         global $QF;
   725         $QF->VIS->Load_Templates('user_redefined');
   726     } */
   727 
   728     function _VISUserMods_Preload($style)
   729     {
   730         global $QF;
   731         if (!$QF->Check_Module('VIS'))
   732             return false;
   733 
   734         $style = strtolower($style);
   735         if (isset($this->VIS_redefs[$style]))
   736             return true;
   737 
   738         $cachename = QF_KERNEL_VIS_VPREFIX.$style.'_redefs';
   739         $cfile = QF_STYLES_DIR.$style.'/redefined.vis';
   740         if ($data = $QF->Cache->Get($cachename))
   741             $this->VIS_redefs[$style] = $data;
   742         elseif ($data = qf_file_get_contents($cfile))
   743         {
   744             preg_match_all('#\<\<part \'(\w+)\'\>\>|[^\<]+|\<#', $data, $struct, PREG_SET_ORDER);
   745             $data = Array();
   746             $p_name = QF_KERNEL_VIS_COMMON;
   747             foreach ($struct as $part)
   748             {
   749                 if ($part[1])
   750                     $p_name = strtolower($part[1]);
   751                 elseif (isset($data[$p_name]))
   752                     $data[$p_name].= $part[0];
   753                 else
   754                     $data[$p_name] = $part[0];
   755             }
   756             $QF->Cache->Set($cachename, $data);
   757             $this->VIS_redefs[$style] = $data;
   758             return true;
   759         }
   760         else
   761             return false;
   762     }
   763 
   764     function _VISParse_URL_CB($matches)
   765     {
   766         $code = $matches[2];
   767         $code = explode('|', $code);
   768 
   769         if (!($url_id = strtoupper($code[0])))
   770             return '#';
   771         if (!isset($this->URL_temps[$url_id]))
   772             return '#';
   773 
   774         if ($params = array_slice($code, 1))
   775         {
   776         	foreach ($params as $id => $val)
   777                 $params[$id] = (is_numeric($val{0})) ? (int) $val : (($val{0} == '"') ? rawurlencode(substr($val, 1, -1)) : '{URLEN:'.$val.'}');
   778         }
   779         else
   780             $params = Array();
   781 
   782         $url = $this->Gen_URL($url_id, $params, true, true);
   783         if ($matches[1] == 'F')
   784             $url = qf_full_url($url, true, $this->URL_domain);
   785         elseif ($matches[1] == 'R') // && $url{0} != '/'
   786         {
   787             $comps = parse_url($url);
   788             if (!$comps['scheme'])
   789                 $url = '{QF_ROOT}'.$url;
   790         }
   791 
   792         return $url;
   793     }
   794 
   795     function _Load_URLS()
   796     {
   797         global $QF;
   798         static $consts = Array(
   799             '{QF_INDEX}' => QF_INDEX,
   800             );
   801 
   802 
   803         if (count($this->URL_temps))
   804             return false;
   805 
   806         $cachename = ($QF->Config->Get('gen_rwurls'))
   807             ? QF_FOX2_URLTEMPS_RW_CACHENAME : QF_FOX2_URLTEMPS_CACHENAME;
   808 
   809         if ($data = $QF->Cache->Get($cachename))
   810             $this->URL_temps = $data;
   811         elseif (list($data, $pkgs) = $QF->DSets->Get_DSet('urltemplates', true))
   812         {
   813             if ($QF->Config->Get('gen_rwurls') && (list($rwdata, $rwpkgs) = $QF->DSets->Get_DSet('mod_rw_urlts', true)))
   814             {
   815                 $data = $rwdata + $data;
   816                 $pkgs = $rwpkgs + $pkgs;
   817             }
   818 
   819             $data = str_replace(array_keys($consts), array_values($consts), $data);
   820 
   821 
   822             if ($QF->Config->Get('tgl_multidomain', 'fox2'))
   823             {
   824                 $domains  = $QF->Config->Get('package_domains', 'fox2');
   825                 $ldomains = $QF->Config->Get('linked_domains', 'fox2');
   826                 $basedomain = $QF->Config->Get('basic_domain', 'fox2');
   827                 if (!is_array($domains))
   828                     $domains  = Array();
   829                 if (!is_array($ldomains) || !$basedomain)
   830                     $ldomains = Array();
   831 
   832                 if (count($domains) || count($ldomains))
   833                 {
   834                     $domains  = preg_replace('#^http\:(?:/)*|/$#i', '', $domains);
   835                     $ldomains = preg_replace('#^http\:(?:/)*|/$#i', '', $ldomains);
   836                     $basedomain = preg_replace('#^http\:(?:/)*|/$#i', '', $basedomain);
   837 
   838                     foreach ($data as $key => $val)
   839                     {
   840                         $cur_pkg = $pkgs[$key];
   841                         list($cur_link, $cur_url) = explode('/', $val, 2);
   842                         if ($cur_link && isset($ldomains[$cur_link]))
   843                         {
   844                             $domain = $ldomains[$cur_link];
   845                             $val = qf_full_url($cur_url, true, $domain);
   846                             $data[$key] = $val;
   847                         }
   848                         elseif (isset($domains[$cur_pkg]))
   849                         {
   850                             $domain = $domains[$cur_pkg];
   851                             $val = qf_full_url($val, true, $domain);
   852                             $data[$key] = $val;
   853                         }
   854                     }
   855                 }
   856             }
   857 
   858             $QF->Cache->Set($cachename, $data);
   859             $this->URL_temps = $data;
   860         }
   861         else
   862             $this->URL_temps = Array();
   863 
   864         return true;
   865     }
   866 
   867     function _Parse_PathInfo()
   868     {
   869         global $QF;
   870         if (!($pt_info = $QF->HTTP->PtInfo))
   871             return false;
   872 
   873         $pt_info = explode('/', $pt_info);
   874         $pt_id   = $pt_info[0];
   875         $pt_parse = $QF->DSets->Get_DSet_Value('ptinfo_masks', $pt_id);
   876         if (is_array($pt_parse))
   877         {
   878             $pt_parse = array_values($pt_parse);
   879             $pt_data = Array();
   880             $imax = min(count($pt_parse), count($pt_info));
   881             for ($i=0; $i<$imax; $i++)
   882                 $pt_data[$pt_parse[$i]] = $pt_info[$i];
   883 
   884             trigger_error(qf_array_definition($pt_data), E_USER_WARNING);
   885 
   886             $QF->GPC->Set_Raws($pt_data, QF_GPC_GET);
   887         }
   888     }
   889 
   890 
   891     function _Parse_RW($rw_id, $rw_data)
   892     {
   893         global $QF;
   894 
   895         if (!$rw_id)
   896             return false;
   897 
   898         $rw_mask = $QF->DSets->Get_DSet_Value('RW_masks', $rw_id);
   899         $rw_data = preg_replace('#\.\w*$#D', '', $rw_data);
   900 
   901         if (!$rw_mask)
   902         {
   903             $QF->GPC->Set_Raws(Array('st' => $rw_id, 'dpath' => $rw_data), QF_GPC_GET);
   904         }
   905         elseif (is_array($rw_mask))
   906         {
   907             $rw_data = explode('/', $rw_data);
   908             $rw_mask = preg_replace('#\$(\d+)#e', '(isset(\$rw_data[\1])) ? \$rw_data[\1] : ""', $rw_mask);
   909 
   910             $QF->GPC->Set_Raws($rw_mask, QF_GPC_GET);
   911         }
   912         else
   913             return false;
   914 
   915         return true;
   916     }
   917 
   918     function HTML_FullURLs(&$buffer)
   919     {
   920         $buffer = preg_replace_callback('#(<(a|form|img|link|script)\s+[^>]*?)(href|action|src)\s*=\s*(\"([^\"<>\(\)]*)\"|\'([^\'<>\(\)]*)\'|[^\s<>\(\)]+)#i', Array(&$this, '_FullURLs_Parse_Callback'), $buffer);
   921     }
   922 
   923     function _FullURLs_Parse_Callback($vars)
   924     {
   925         Global $QF;
   926         if (!is_array($vars))
   927             return false;
   928 
   929         if (isset($vars[6]))
   930         {
   931             $url = $vars[6];
   932             $bounds = '\'';
   933         }
   934         elseif (isset($vars[5]))
   935         {
   936             $url = $vars[5];
   937             $bounds = '"';
   938         }
   939         else
   940         {
   941             $url = $vars[4];
   942             $bounds = '';
   943         }
   944 
   945         if (qf_str_is_url($url) == 2)
   946             $url = qf_full_url($url, true, $this->URL_domain);
   947 
   948         return $vars[1].$vars[3].'='.$bounds.$url.$bounds;
   949 
   950     }
   951 
   952 }
   953 
   954 $QF->Register_Module('FOX', __FILE__, 'Fox2');
   955 $QF->Run_Module('FOX');
   956 
   957 ?>