Source for file mr_test.php

Documentation is available at mr_test.php

  1. <?php
  2. /**
  3.  * Testscript for Advanced Mod Rewrite Plugin.
  4.  *
  5.  * The goal of this testscript is to provide an easy way for a variance comparison
  6.  * of created SEO URLs against their resolved parts.
  7.  *
  8.  * This testscript fetches the full category and article structure of actual
  9.  * Contenido installation, creates the SEO URLs for each existing category/article
  10.  * and resolves the generated URLs.
  11.  *
  12.  * Usage:
  13.  * ------
  14.  * 1. Install the Advanced Mod Rewrite Plugin
  15.  * 2. Set your desired plugin settings by using the Advanced Mod Rewrite area
  16.  *    in the Backend
  17.  * 3. Copy this file to your client directory, it is normally named '/cms/'
  18.  * 4. Exclude this file from rewriting, add following line in your .htaccess
  19.  *    RewriteRule ^cms/mr_test.php.*$ - [L]
  20.  * 5. Browse to http://host/cms/mr_test.php
  21.  * 6. Disable the script after finished testing, see line 31
  22.  *
  23.  * @author      Murat Purc <murat@purc.de>
  24.  * @copyright   © Murat Purc 2008
  25.  * @package     Contenido
  26.  * @subpackage  ModRewrite
  27.  */
  28.  
  29.  
  30. // Uncomment following line, if you're finished with the tests
  31. #die('Testscript disabled');
  32.  
  33.  
  34. ####################################################################################################
  35. ##### Contenido front_content.php header (compressed)
  36.  
  37. if (!defined("CON_FRAMEWORK")) {define("CON_FRAMEWORK"true);}
  38. include_once ('../contenido/classes/class.security.php');
  39. Contenido_Security::checkRequests();
  40. include_once ("config.php");
  41. include_once ($contenido_path."includes/startup.php");
  42. if ($cfg['http_params_check']['enabled'=== true{
  43.     cInclude('classes''class.httpinputvalidator.php');
  44.     $oHttpInputValidator new HttpInputValidator($cfg["path"]["contenido"$cfg["path"]["includes"'/config.http_check.php');
  45. }
  46. cInclude("includes""functions.con.php");
  47. cInclude("includes""functions.con2.php");
  48. cInclude("includes""functions.api.php");
  49. cInclude("includes""functions.pathresolver.php");
  50. if ($contenido){
  51.     page_open(array ('sess' => 'Contenido_Session''auth' => 'Contenido_Challenge_Crypt_Auth''perm' => 'Contenido_Perm'));
  52.     i18nInit($cfg["path"]["contenido"].$cfg["path"]["locale"]$belang);
  53. else {
  54.     page_open(array ('sess' => 'Contenido_Frontend_Session''auth' => 'Contenido_Frontend_Challenge_Crypt_Auth''perm' => 'Contenido_Perm'));
  55. }
  56. require_once $cfg['path']['contenido'$cfg['path']['includes''functions.includePluginConf.php';
  57. $db new DB_Contenido;
  58. $sess->register("cfgClient");
  59. $sess->register("errsite_idcat");
  60. $sess->register("errsite_idart");
  61. $sess->register("encoding");
  62. if ($cfgClient["set"!= "set"rereadClients()}
  63. $sql "SELECT idlang, encoding FROM ".$cfg["tab"]["lang"];
  64. $db->query($sql);
  65. while ($db->next_record()) {
  66.     $encoding[$db->f("idlang")$db->f("encoding");
  67. }
  68. if (is_numeric($tmpchangelang&& $tmpchangelang 0{
  69.     $savedlang $lang;
  70.     $lang $tmpchangelang;
  71. }
  72. if (isset($changeclient&& !is_numeric($changeclient)) unset($changeclient)}
  73. if (isset($client&& !is_numeric($client)) unset($client)}
  74. if (isset($changelang&& !is_numeric($changelang)) unset($changelang)}
  75. if (isset($lang&& !is_numeric($lang)) unset($lang)}
  76. if (isset($changeclient)){
  77.     $client $changeclient;
  78.     unset($lang$load_lang);
  79. }
  80. if (isset($changelang)) $lang $changelang;
  81. if (!isset($client)) $client $load_client}
  82. if (!isset($lang)) {
  83.     if(isset($load_lang)){
  84.         $lang $load_lang;
  85.     else {
  86.         $sql "SELECT B.idlang FROM ".$cfg["tab"]["clients_lang"]." AS A, ".$cfg["tab"]["lang"]." AS B WHERE A.idclient='".Contenido_Security::toInteger($client)."' AND A.idlang = B.idlang LIMIT 0,1";
  87.         $db->query($sql);
  88.         $db->next_record();
  89.         $lang $db->f("idlang");
  90.     }
  91. }
  92. if (!$sess->is_registered("lang") ) $sess->register("lang");
  93. if (!$sess->is_registered("client") ) $sess->register("client");
  94. if (isset ($username)) $auth->login_if(true)}
  95. header("Content-Type: text/html; charset={$encoding[$lang]}");
  96. if (isset ($logout)) {
  97.     $auth->logout(true);
  98.     $auth->unauth(true);
  99.     $auth->auth["uname""nobody";
  100. }
  101. if (file_exists("config.local.php")) @include("config.local.php")}
  102. $errurl  "front_content.php?client=$client&idcat=".$errsite_idcat[$client]."&idart=".$errsite_idart[$client]."&lang=$lang&error=1";
  103. $errsite 'Location: ' CEC_Hook::execute('Contenido.Frontend.CreateURL'$errurl);
  104. if (isset($mr_preprocessedPageError)) {
  105.     unset($mr_preprocessedPageError);
  106.     mr_header($errsite);
  107. }
  108. if (isset ($path)) {
  109.     if ($cfg["urlpathresolve"== true)     {
  110.         $idcat prResolvePathViaURLNames($path);
  111.     else {
  112.         $idcat prResolvePathViaCategoryNames($path);
  113.     }
  114. }
  115.  
  116.  
  117. ####################################################################################################
  118. ##### Plugin related area
  119.  
  120.  
  121. // set debug configuration, if you want to see the debug toolbar
  122. $options array(
  123.     'enable'                    => true,
  124.     'ressource_urls'            => array('/contenido/logs/errorlog.txt'),
  125.     'dump_super_globals'        => array('$_GET''$_POST''$_COOKIE''$_SESSION'),
  126.     'ignore_empty_superglobals' => true,
  127.     'magic_word'                => 'foobar'
  128. );
  129. $GLOBALS['mpDebug']->setConfig($options);
  130.  
  131. $GLOBALS['mpDebug']->addDebug($cfg['mod_rewrite']'mr_test.php $cfg[mod_rewrite]');
  132.  
  133.  
  134. ####################################################################################################
  135. ##### Form processing / Controller
  136.  
  137.  
  138. $mrTestNoOptionSelected false;
  139. if (!_getRequest('idart'&& !_getRequest('idcat'&& !_getRequest('idcatart'&& !_getRequest('idartlang')) {
  140.     $mrTestNoOptionSelected true;
  141. }
  142.  
  143. $aData['form']['idart_chk']     (_getRequest('idart')) ' checked="checked"' '';
  144. $aData['form']['idcat_chk']     (_getRequest('idcat')) ' checked="checked"' '';
  145. $aData['form']['idcatart_chk']  (_getRequest('idcatart')) ' checked="checked"' '';
  146. $aData['form']['idartlang_chk'(_getRequest('idartlang')) ' checked="checked"' '';
  147. $aData['form']['maxitems']      _getRequest('maxitems'200);
  148.  
  149.  
  150. $oMrTestAction new ModRewriteTestActions();
  151.  
  152. if ($mrTestNoOptionSelected{
  153.     $oMrTestAction->executeIndex();
  154. else {
  155.     $oMrTestAction->executeTest();
  156. }
  157.  
  158.  
  159. if (isset($GLOBALS['mpDebug'])) {
  160.     $aData['content'.= $GLOBALS['mpDebug']->getResults(false);
  161. }
  162.  
  163.  
  164. ####################################################################################################
  165. ##### Contenido front_content.php footer
  166.  
  167. if (file_exists("config.after.php")) {@include("config.after.php");}
  168. if (isset ($savedlang)){$lang $savedlang;}
  169. page_close();
  170.  
  171.  
  172.  
  173. ####################################################################################################
  174. #### Template / View
  175.  
  176. echo <<<HTML
  177. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  178.    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  179. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="de">
  180. <head>
  181.     <title>Test - Contenido Plugin Advanced Mod Rewrite</title>
  182.     <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=iso-8859-1" />
  183.     <style type="text/css">
  184.     body { margin:0; padding:0; background:#fff; color:#000; font-family:arial,helvetica,sans-serif; font-size:12px; }
  185.     #wrap { margin:15px 10px; }
  186.     h1 { font-size:16px; }
  187.     form { width:70%; border:1px solid #999; background:#eaeaea; padding:0.5em; margin-bottom:15px; }
  188.     fieldset { margin-bottom:10px; }
  189.     fieldset p { margin:0; }
  190.     form .chk { width:24%; float:left; }
  191.     .left { float:left; }
  192.     .right { float:right; }
  193.     .clear { clear:both; font-size:0pt !important; height:0pt !important; line-height:0pt !important; }
  194.     </style>
  195. </head>
  196. <body>
  197. <div id="wrap">
  198.  
  199. <h1>Advanced Mod Rewrite test</h1>
  200.  
  201. <form name="mr_test" id="mr_test" action="{$_SERVER['PHP_SELF']}">
  202.     <fieldset>
  203.         <legend>Parameter to add</legend>
  204.         <p></p>
  205.         <div class="chk">
  206.             <input type="checkbox" id="idart" name="idart" value="1"{$aData['form']['idart_chk']} /><label for="idart">idart</label>
  207.         </div>
  208.         <div class="chk">
  209.             <input type="checkbox" id="idcat" name="idcat" value="1"{$aData['form']['idcat_chk']} /><label for="idcat">idcat</label>
  210.         </div>
  211.         <div class="chk">
  212.             <input type="checkbox" id="idcatart" name="idcatart" value="1"{$aData['form']['idcatart_chk']} /><label for="idcatart">idcatart</label>
  213.         </div>
  214.         <div class="chk">
  215.             <input type="checkbox" id="idartlang" name="idartlang" value="1"{$aData['form']['idartlang_chk']} /><label for="idartlang">idartlang</label>
  216.         </div>
  217.         <br class="clear" />
  218.     </fieldset>
  219.     <div style="margin:0.3em 0;">
  220.         <div class="left">
  221.             <label for="maxitems">Max items to process: </label><input type="text" id="maxitems" maxlength="4" name="maxitems" value="{$aData['form']['maxitems']}" />
  222.         </div>
  223.         <div class="right" style="padding-right:5px;">
  224.             <input type="submit" name="test" value="run test" />
  225.         </div>
  226.         <br class="clear" />
  227.     </div>
  228. </form>
  229.  
  230. {$aData['content']}
  231.  
  232. </div>
  233. </body>
  234. </html>
  235. HTML;
  236.  
  237.  
  238. ####################################################################################################
  239. ##### Classes / Functions
  240.  
  241.  
  242. /**
  243.  * Simple action controller.
  244.  *
  245.  * @author      Murat Purc <murat@purc.de>
  246.  * @copyright   © Murat Purc 2008
  247.  * @package     Contenido
  248.  * @subpackage  ModRewriteTest
  249.  */
  250.  
  251.     private $data;
  252.  
  253.     function __construct({
  254.         $this->data = $GLOBALS['aData'];
  255.     }
  256.  
  257.     /**
  258.      * Execute index action
  259.      */
  260.     function executeIndex({
  261.         $this->data['content''<pre>
  262.  
  263. Select several options from above and run the test.
  264.  
  265. <b>The result will be a outputlist like:</b>
  266.  
  267. Navigationame
  268. builder in:    The URL beeing passed to the URL builder
  269. builder out:   Result of rebuild URL, should be consistent with your settings
  270. resolved url:  Resolved URL (the URL beeing returned by URL builder) by ModRewriteController
  271. resolved data: Some resolved variables like $client, $lang, etc.
  272.  
  273. <b>Example:</b>
  274.  
  275. Navigationame
  276. builder in:    front_content.php?idcat=1&foo=bar
  277. builder out:   /deutsch-hauptnavigation.index.html?foo=bar
  278. resolved url:  /deutsch/hauptnavigation/index.html?foo=bar
  279. resolved data: client=1; lang=1; changelang=1; idcat=1
  280. </pre>';
  281.  
  282.     }
  283.  
  284.     /**
  285.      * Execute test action
  286.      */
  287.     function executeTest({
  288.         $this->data['content''';
  289.  
  290.         // Array for testcases
  291.         $aTests  array();
  292.  
  293.         // Instance of mr test
  294.         $oMRTest new ModRewriteTest();
  295.  
  296.         // Fetch complete Contenido page structure
  297.         $aStruct $oMRTest->fetchFullStructure();
  298.         $GLOBALS['mpDebug']->addDebug($aStruct'mr_test.php $aStruct');
  299.  
  300.         // Loop through the structure and compose testcases
  301.         foreach ($aStruct as $idcat => $aCat{
  302.             // category
  303.             $aTests[array(
  304.                 'url'   => $oMRTest->composeURL($aCat'c'),
  305.                 'level' => $aCat['level'],
  306.                 'name'  => $aCat['name']
  307.             );
  308.  
  309.             foreach ($aCat['articles'as $idart => $aArt{
  310.                 // articles
  311.                 $aTests[array(
  312.                     'url'  => $oMRTest->composeURL($aArt'a'),
  313.                     'level' => $aCat['level'],
  314.                     'name' => $aCat['name'' :: ' $aArt['title']
  315.                 );
  316.             }
  317.         }
  318.  
  319.         // compose content
  320.         $this->data['content''<pre>';
  321.  
  322.         foreach ($aTests as $p => $v{
  323.             $url  mr_build_new_url($v['url']);
  324.             $arr  $oMRTest->resolveUrl($url);
  325.             $pref str_repeat('    '$v['level']);
  326.             $this->data['content'.= "\n"
  327.                 . $pref '<b>' $v['name''</b>' "\n"
  328.                 . $pref 'builder in:    ' $v['url'"\n"
  329.                 . $pref 'builder out:   <a href="' $url '" target="_blank">' $url '</a>'  "\n"
  330.                 . $pref 'resolved url:  ' $oMRTest->getResolvedUrl("\n"
  331.                 . $pref 'resolved data: ' $oMRTest->getReadableResolvedData($arr"\n";
  332.         }
  333.         $this->data['content'.= '</pre>';
  334.  
  335.     }
  336.  
  337. }
  338.  
  339.  
  340. /**
  341.  * Advanced Mod Rewrite test class.
  342.  *
  343.  * @author      Murat Purc <murat@purc.de>
  344.  * @copyright   © Murat Purc 2008
  345.  * @package     Contenido
  346.  * @subpackage  ModRewriteTest
  347.  */
  348. class ModRewriteTest {
  349.  
  350.     /**
  351.      * @var  array  Global $cfg array
  352.      */
  353.     private $cfg;
  354.  
  355.     /**
  356.      * @var  array  Global $cfg['tab'] array
  357.      */
  358.     private $cfgTab;
  359.  
  360.  
  361.     /**
  362.      * @var  int  Max items to process
  363.      */
  364.     private $maxItems;
  365.  
  366.     /**
  367.      * @var  string  Actual resolved url
  368.      */
  369.     private $sResolvedUrl;
  370.  
  371.  
  372.     /**
  373.      * Constuctor
  374.      */
  375.     function __construct({
  376.         $this->cfg    = $GLOBALS['cfg'];
  377.         $this->cfgTab = $GLOBALS['cfg']['tab'];
  378.         $this->maxItems = $GLOBALS['aData']['form']['maxitems'];
  379.     }
  380.  
  381.  
  382.     /**
  383.      * Returns resolved URL
  384.      *
  385.      * @return  string  Resolved URL
  386.      */
  387.     function getResolvedUrl(){
  388.         return $this->sResolvedUrl;
  389.     }
  390.  
  391.     /**
  392.      * Fetchs full structure of the installation (categories and articles) and returns it back.
  393.      *
  394.      * @param   int    $idclient  Client id
  395.      * @param   int    $idlang    Language id
  396.      * @return  array  Full structure as follows
  397.      *  <code>
  398.      *    $arr[idcat] = Category dataset
  399.      *    $arr[idcat]['articles'][idart] = Article dataset
  400.      *  </code>
  401.      */
  402.     function fetchFullStructure($idclient=null$idlang=null{
  403.  
  404.         $db  new DB_Contenido();
  405.         $db2 new DB_Contenido();
  406.  
  407.         if (!$idclient || (int) $idclient == 0{
  408.             $idclient $GLOBALS['client'];
  409.         }
  410.         if (!$idlang || (int) $idlang == 0{
  411.             $idlang $GLOBALS['lang'];
  412.         }
  413.  
  414.         $aTab $this->cfgTab;
  415.  
  416.         $aStruct array();
  417.  
  418.         $sql "SELECT
  419.                     *
  420.                 FROM
  421.                     " $aTab['cat_tree'" AS a,
  422.                     " $aTab['cat_lang'" AS b,
  423.                     " $aTab['cat'" AS c
  424.                 WHERE
  425.                     a.idcat = b.idcat AND
  426.                     c.idcat = a.idcat AND
  427.                     c.idclient = '".$idclient."' AND
  428.                     b.idlang = '".$idlang."'
  429.                 ORDER BY
  430.                     a.idtree";
  431.  
  432.         $db->query($sql);
  433.  
  434.         $loop    false;
  435.         $counter 0;
  436.  
  437.         while ($db->next_record()) {
  438.  
  439.             if (++$counter == $this->maxItems{
  440.                 break// break this loop
  441.             }
  442.  
  443.             $idcat $db->f('idcat');
  444.             $aStruct[$idcat$db->Record;
  445.             $aStruct[$idcat]['articles'array();
  446.  
  447.             if ($this->cfg['is_start_compatible'== true{
  448.                 $compStatement ' a.is_start DESC, ';
  449.             else {
  450.                 $compStatement '';
  451.             }
  452.  
  453.             $sql2 "SELECT
  454.                          *
  455.                      FROM
  456.                          ".$aTab['cat_art']."  AS a,
  457.                          ".$aTab['art']."      AS b,
  458.                          ".$aTab['art_lang']." AS c
  459.                      WHERE
  460.                          a.idcat = '".$idcat."' AND
  461.                          b.idart = a.idart AND
  462.                          c.idart = a.idart AND
  463.                          c.idlang = '".$idlang."' AND
  464.                          b.idclient = '".$idclient."'
  465.                      ORDER BY
  466.                          " $compStatement "
  467.                          c.title ASC";
  468.  
  469.             $db2->query($sql2);
  470.  
  471.             while ($db2->next_record()) {
  472.                 $idart $db2->f('idart');
  473.                 $aStruct[$idcat]['articles'][$idart$db2->Record;
  474.                 if (++$counter == $this->maxItems{
  475.                     break 2// break this and also superior loop
  476.                 }
  477.             }
  478.         }
  479.  
  480.         return $aStruct;
  481.     }
  482.  
  483.  
  484.     /**
  485.      * Creates an URL using passed data.
  486.      *
  487.      * The result is used to generate seo urls...
  488.      *
  489.      * @param  array  $arr  Assoziative array with some data as follows:
  490.      *  <code>
  491.      *  $arr['idcat']
  492.      *  $arr['idart']
  493.      *  $arr['idcatart']
  494.      *  $arr['idartlang']
  495.      *  </code>
  496.      * @param  string  $type  Either 'c' or 'a' (category or article). If set to
  497.      *                         'c' only the parameter idcat will be added to the URL
  498.      */
  499.     function composeURL($arr$type{
  500.         $type ($type == 'a''a' 'c';
  501.  
  502.         $param array();
  503.  
  504.         if ($type == 'c'{
  505.             $param['idcat=' $arr['idcat'];
  506.         else {
  507.             if (_getRequest('idart')) {
  508.                 $param['idart=' $arr['idart'];
  509.             }
  510.             if (_getRequest('idcat')) {
  511.                 $param['idcat=' $arr['idcat'];
  512.             }
  513.             if (_getRequest('idcatart')) {
  514.                 $param['idcatart=' $arr['idcatart'];
  515.             }
  516.             if (_getRequest('idartlang')) {
  517.                 $param['idartlang=' $arr['idartlang'];
  518.             }
  519.         }
  520.         $param['foo=bar';
  521.         return 'front_content.php?' implode('&amp;'$param);
  522.  
  523.     }
  524.  
  525.  
  526.     /**
  527.      * Resolves variables of an page (idcat, idart, idclient, idlang, etc.) by
  528.      * processing passed url using ModRewriteController
  529.      *
  530.      * @param   string  $url  Url to resolve
  531.      * @return  array   Assoziative array with resolved data
  532.      */
  533.     public function resolveUrl($url){
  534.  
  535.         // some globals to reset
  536.         $aGlobs array(
  537.             'mr_preprocessedPageError''client''changeclient''lang',
  538.             'changelang''idart''idcat'
  539.         );
  540.         foreach ($aGlobs as $p => $k{
  541.             if (isset($GLOBALS[$k])) unset($GLOBALS[$k])}
  542.         }
  543.  
  544.         $aReturn array();
  545.  
  546.         // create an mod rewrite controller instance and execute processing
  547.         $oMRController new ModRewriteController($url);
  548.         $oMRController->execute();
  549.  
  550.         if ($oMRController->errorOccured()) {
  551.  
  552.             // an error occured (idcat and or idart couldn't catched by controller)
  553.             $aReturn['mr_preprocessedPageError'1;
  554.  
  555.             $this->sResolvedUrl = '';
  556.  
  557.         else {
  558.  
  559.             // set some global variables
  560.  
  561.             $this->sResolvedUrl = $oMRController->getResolvedUrl();
  562.  
  563.             if ($oMRController->getClient()) {
  564.                 $aReturn['client'$oMRController->getClient();
  565.             }
  566.  
  567.             if ($oMRController->getChangeClient()) {
  568.                 $aReturn['changeclient'$oMRController->getChangeClient();
  569.             }
  570.  
  571.             if ($oMRController->getLang()) {
  572.                 $aReturn['lang'$oMRController->getLang();
  573.             }
  574.  
  575.             if ($oMRController->getChangeLang()) {
  576.                 $aReturn['changelang'$oMRController->getChangeLang();
  577.             }
  578.  
  579.             if ($oMRController->getIdArt()) {
  580.                 $aReturn['idart'$oMRController->getIdArt();
  581.             }
  582.  
  583.             if ($oMRController->getIdCat()) {
  584.                 $aReturn['idcat'$oMRController->getIdCat();
  585.             }
  586.  
  587.             if ($oMRController->getPath()) {
  588.                 $aReturn['path'$oMRController->getPath();
  589.             }
  590.  
  591.         }
  592.  
  593.         return $aReturn;
  594.     }
  595.  
  596.  
  597.     /**
  598.      * Creates a readable string from passed resolved data array.
  599.      *
  600.      * @param   array   Assoziative array with resolved data
  601.      * @return  string  Readable resolved data
  602.      */
  603.     public function getReadableResolvedData(array $data{
  604.         // compose resolved string
  605.         $ret '';
  606.         foreach ($data as $k => $v{
  607.             $ret .= $k '=' $v '; ';
  608.         }
  609.         $ret substr($ret0strlen($ret)-2);
  610.         return $ret;
  611.     }
  612.  
  613. }
  614.  
  615.  
  616. /**
  617.  * Minimalistic'n simple way to get request variables.
  618.  *
  619.  * Checks occurance in $_GET, then in $_POST. Uses trim() and strip_tags() to preclean data.
  620.  *
  621.  * @param   string  $key      Name of var to get
  622.  * @param   mixed   $default  Default value to return
  623.  * @return  mixed   The value
  624.  */
  625. function _getRequest($key$default=null{
  626.     static $cache;
  627.     if (!isset($cache)) {
  628.         $cache array();
  629.     }
  630.     if (isset($cache[$key])) {
  631.         return $cache[$key];
  632.     }
  633.     if (isset($_GET[$key])) {
  634.         $val $_GET[$key];
  635.     elseif (isset($_POST[$key])) {
  636.         $val $_POST[$key];
  637.     else {
  638.         $val $default;
  639.     }
  640.     $cache[$keystrip_tags(trim($val));
  641.     return $cache[$key];
  642. }

Documentation generated on Tue, 12 Aug 2008 00:06:52 +0200 by phpDocumentor 1.4.0