Guest User

PHP fix for AC-web post 2

a guest
Sep 23rd, 2010
1,172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.49 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Copyright (C) 2002-2004 Oliver Hitz <[email protected]>
  5.  *
  6.  */
  7.  
  8. class Template
  9. {
  10.  
  11.   var $structure;
  12.   var $content;
  13.  
  14.   var $filename = "";
  15.  
  16.   function Template($filename)
  17.   {
  18.     $this->filename = $filename;
  19.  
  20.     $this->structure = array();
  21.     $this->structure["name"] = "main";
  22.     $this->structure["body"] = $this->loadfile($this->filename);
  23.     $this->structure["blocks"] = array();
  24.     $this->structure["values"] = array();
  25.     $this->parse($this->structure);   /** JEREMY, I removed the & symbol here, it was  $this->parse(&$this->structure); **/
  26.     $this->content = array();
  27.     $this->content["blocks"] = array();
  28.     $this->content["values"] = array();
  29.   }
  30.  
  31.   function parse(&$structure)
  32.   {
  33.     while (preg_match("|<!--\ if\(([^),]+)(,([^)]+))?\)\ -->|si", $structure["body"], $m)) {
  34.       $name = $m[1];
  35.       $sort = $m[3];
  36.  
  37.       $block = array();
  38.       $block["name"] = $m[1];
  39.       $block["sort"] = $m[3];
  40.       $block["blocks"] = array();
  41.       $block["used"] = 0;
  42.       $reg = "|<!--\ if\(".$name."(,[^)]*)?\)\ -->(.*)<!--\ endif\(".$name."\)\ -->|si";
  43.       if (!preg_match($reg, $structure["body"], $m)) {
  44.     $this->error("block `".$name."' does not have if() AND endif().");
  45.       }
  46.       $reg2 = "|<!--\ if\(".$name."(,[^)]*)?\)\ -->(.*)<!--\ else\(".$name."\)\ -->(.*)<!--\ endif\(".$name."\)\ -->|si";
  47.  
  48.       if (preg_match($reg2, $structure["body"], $m2)) {
  49.     $block["body"] = $m2[2];
  50.     $block["empty"] = $m2[3];
  51.     $structure["body"] = preg_replace($reg2, "{block:".$name."}", $structure["body"]);
  52.       } else {
  53.     $block["body"] = $m[2];
  54.     $structure["body"] = preg_replace($reg, "{block:".$name."}", $structure["body"]);
  55.       }
  56.       $this->parse($block);  /** JEREMY, I removed the & symbol here, it was  $this->parse(&$block); **/
  57.      
  58.       $structure["blocks"][$name] = $block;
  59.     }
  60.   }
  61.  
  62.   function showContent()
  63.   {
  64.     print "<ul>";
  65.     $this->_showContent($this->content);  /** JEREMY, I removed the & symbol here, it was  $this->_showContent(&$this->content); **/
  66.     print "</ul>";
  67.   }
  68.  
  69.   function _showContent(&$content)
  70.   {
  71.     print "<li>values: (".implode(",", $content["values"]).")</li>";
  72.     print "<li>blocks:";
  73.     print "<ul>";
  74.     reset ($content["blocks"]);
  75.     while (list($name, $blocks) = each($content["blocks"])) {
  76.       print "<li>name: ".$name;
  77.       print "<ul>";
  78.       for ($i = 0; $i < count($blocks); $i++) {
  79.     $this->_showContent($blocks[$i]);
  80.       }
  81.       print "</ul>";
  82.       print "</li>";
  83.     }
  84.     print "</ul>";
  85.     print "</li>";
  86.   }
  87.  
  88.  
  89.   function showStructure()
  90.   {
  91.     print "<ul>";
  92.     $this->_showStructure($this->structure);  /** JEREMY, I removed the & symbol here, it was  $this->_showStructure(&$this->structure); **/
  93.     print "</ul>";
  94.   }
  95.  
  96.   function _showStructure(&$structure)
  97.   {
  98.     print "<li>name: ".$structure["name"]."</li>";
  99.     print "<li>sort: ".$structure["sort"]."</li>";
  100.     print "<li>body: ".htmlentities($structure["body"])."</li>";
  101.     print "<li>blocks:";
  102.     print "<ul>";
  103.     reset ($structure["blocks"]);
  104.     while (list($blockname, $block) = each($structure["blocks"])) {
  105.       $this->_showStructure($block);
  106.     }
  107.     print "</ul>";
  108.     print "</li>";
  109.   }
  110.  
  111.   function toString()
  112.   {
  113.     return preg_replace(array("|&#x7b;|", "|&#x7d;|", "|&#x3a;|", "|&#x7c;|"),
  114.             array("{", "}", ":", "|"),
  115.             $this->_toString($this->structure, $this->content, 0));  /** JEREMY, I removed the & symbol here, it was  $this->_toString(&$this->structure, &$this->content, 0)); **/
  116.   }
  117.  
  118.   function _toString(&$structure, &$content, $count, $max = -1)
  119.   {
  120.     global $HTTP_SERVER_VARS;
  121.  
  122.     $txt = $structure["body"];
  123.  
  124.     // Replace all user variables
  125.     reset($content["values"]);
  126.     while (list($key, $val) = each($content["values"])) {
  127.       if (!is_numeric($key)) {
  128.     if (!is_string($val)) {
  129.       $val = "".$val;
  130.     }
  131.     $val = preg_replace(array("|{|", "|}|", "|:|", "/\|/"),
  132.                 array("&#x7b;", "&#x7d;", "&#x3a;", "&#x7c;"),
  133.                 $val);
  134.     $txt = preg_replace("|{".$key."}|si", $val, $txt);
  135.       }
  136.     }
  137.  
  138.     // Replace all predefined variables
  139.     $txt = preg_replace("|{sid}|", session_id(), $txt);
  140.     $txt = preg_replace("|{script}|", $HTTP_SERVER_VARS["SCRIPT_NAME"], $txt);
  141.     $txt = preg_replace("|{count:1}|si", "".($count+1), $txt);
  142.     $txt = preg_replace("|{count:0}|si", "".($count), $txt);
  143.  
  144.     // Clear unused variables
  145.     $txt = preg_replace("|{[a-zA-Z_-]+}|", "", $txt);
  146.  
  147.     while (true) {
  148.       if (preg_match("|{quotenl:([^{}]+)}|si", $txt, $m)) {
  149.     $v = preg_replace("|\n|", "\\n", $content["values"][$m[1]]);
  150.     $v = preg_replace("|\r|", "", $v);
  151.     $txt = preg_replace("|{quotenl:".$m[1]."}|si", $v, $txt);
  152.       } else if (preg_match("|{quotejs:([^{}]+)}|si", $txt, $m)) {
  153.     $v = preg_replace("|\"|", "\\\"", $content["values"][$m[1]]);
  154.     $v = preg_replace("|\n|", "\\n", $v);
  155.     $v = preg_replace("|\r|", "", $v);
  156.     $txt = preg_replace("|{quotejs:".$m[1]."}|si", $v, $txt);
  157.       } else if (preg_match("|{quotexml:([^{}]+)}|si", $txt, $m)) {
  158.     $v = preg_replace("|&|", "&amp;", $content["values"][$m[1]]);
  159.     $v = preg_replace("|<|", "&lt;", $v);
  160.     $v = preg_replace("|>|", "&gt;", $v);
  161.     $txt = preg_replace("|{quotexml:".$m[1]."}|si", $v, $txt);
  162.       } else if (preg_match("|{csv:([^{}]*)}|si", $txt, $m)) {
  163.     $q = "\"".preg_replace("|\"|", "\"\"", $content["values"][$m[1]])."\"";
  164.     $q = preg_replace("|\n|", "\\n", $q);
  165.     $q = preg_replace("|\r|", "", $q);
  166.     $txt = preg_replace("|{csv:".$m[1]."}|si", $q, $txt);
  167.       } else if (preg_match("|{base64:([^{}]+)}|si", $txt, $m)) {
  168.     $v = base64_encode($m[1]);
  169.     $txt = preg_replace("|{base64:".$this->quotere($m[1])."}|si", $v, $txt);
  170.       } else if (preg_match("|{url:([^{}]+)}|si", $txt, $m)) {
  171.     $txt = preg_replace("|{url:".$this->quotere($m[1])."}|si", urlencode($content["values"][$m[1]]), $txt);
  172.       } else if (preg_match("|{format:([^:{}]+):([^{}]+)}|si", $txt, $m)) {
  173.     $txt = preg_replace("|{format:".$m[1].":".$m[2]."}|si", sprintf($m[2], $content["values"][$m[1]]), $txt);
  174.       } else if (preg_match("|{date:([^:{}]+):([^{}]+)}|si", $txt, $m)) {
  175.     $txt = preg_replace("|{date:".$m[1].":".$m[2]."}|si", date($m[2], $content["values"][$m[1]]), $txt);
  176.       } else if (preg_match("|{ifequal:([^:{}]+):([^:{}]*):([^{}]+)}|si", $txt, $m)) {
  177.     if ($m[1] == $m[2]) {
  178.       $txt = preg_replace("|{ifequal:".$this->quotere($m[1]).":".$this->quotere($m[2]).":([^{}]+)}|si", "\\1", $txt);
  179.     } else {
  180.       $txt = preg_replace("|{ifequal:".$this->quotere($m[1]).":".$this->quotere($m[2]).":([^{}]+)}|si", "", $txt);
  181.     }
  182.       } else if (preg_match("|{ifnequal:([^:{}]+):([^:{}]*):([^{}]+)}|si", $txt, $m)) {
  183.     if ($m[1] != $m[2]) {
  184.       $txt = preg_replace("|{ifnequal:".$this->quotere($m[1]).":".$this->quotere($m[2]).":([^{}]+)}|si", "\\1", $txt);
  185.     } else {
  186.       $txt = preg_replace("|{ifnequal:".$this->quotere($m[1]).":".$this->quotere($m[2]).":([^{}]+)}|si", "", $txt);
  187.     }
  188.       } else if (preg_match("|{ifeq:([^:{}]+):([^:{}]*):([^{}]+)}|si", $txt, $m)) {
  189.     if ($content["values"][$m[1]] == $m[2]) {
  190.       $txt = preg_replace("|{ifeq:".$m[1].":".$this->quotere($m[2]).":([^{}]+)}|si", "\\1", $txt);
  191.     } else {
  192.       $txt = preg_replace("|{ifeq:".$m[1].":".$this->quotere($m[2]).":([^{}]+)}|si", "", $txt);
  193.     }
  194.       } else if (preg_match("|{ifne:([^:{}]+):([^:{}]*):([^{}]+)}|si", $txt, $m)) {
  195.     if ($content["values"][$m[1]] != $m[2]) {
  196.       $txt = preg_replace("|{ifne:".$m[1].":".$this->quotere($m[2]).":([^{}]+)}|si", "\\1", $txt);
  197.     } else {
  198.       $txt = preg_replace("|{ifne:".$m[1].":".$this->quotere($m[2]).":([^{}]+)}|si", "", $txt);
  199.     }
  200.       } else if (preg_match("|{ifset:([^:{}]+):([^{}]*)}|si", $txt, $m)) {
  201.     if ($content["values"][$m[1]] != "" && $content["values"][$m[1]] != "0") {
  202.       $txt = preg_replace("|{ifset:".$m[1].":([^{}]*)}|si", "\\1", $txt);
  203.     } else {
  204.       $txt = preg_replace("|{ifset:".$m[1].":([^{}]*)}|si", "", $txt);
  205.     }
  206.       } else if (preg_match("|{ifnotset:([^:{}]+):([^{}]+)}|si", $txt, $m)) {
  207.     if ($content["values"][$m[1]] == "" || $content["values"][$m[1]] == "0") {
  208.       $txt = preg_replace("|{ifnotset:".$m[1].":([^{}]+)}|si", "\\1", $txt);
  209.     } else {
  210.       $txt = preg_replace("|{ifnotset:".$m[1].":([^{}]+)}|si", "", $txt);
  211.     }
  212.       } else if (preg_match("|{iflast:([^{}]+)}|si", $txt, $m)) {
  213.     if ($count == $max-1) {
  214.       $txt = preg_replace("|{iflast:".$this->quotere($m[1])."}|si", $m[1], $txt);
  215.     } else {
  216.       $txt = preg_replace("|{iflast:".$this->quotere($m[1])."}|si", "", $txt);
  217.     }
  218.       } else if (preg_match("|{ifnotlast:([^{}]+)}|si", $txt, $m)) {
  219.     if ($count == $max-1) {
  220.       $txt = preg_replace("|{ifnotlast:".$this->quotere($m[1])."}|si", "", $txt);
  221.     } else {
  222.       $txt = preg_replace("|{ifnotlast:".$this->quotere($m[1])."}|si", $m[1], $txt);
  223.     }
  224.       } else if (preg_match("|{ifnotfirst:([^{}]+)}|si", $txt, $m)) {
  225.     if ($count == 0) {
  226.       $txt = preg_replace("|{ifnotfirst:".$this->quotere($m[1])."}|si", "", $txt);
  227.     } else {
  228.       $txt = preg_replace("|{ifnotfirst:".$this->quotere($m[1])."}|si", $m[1], $txt);
  229.     }
  230.       } else if (preg_match("|{ifoddposition:([^{}]+)}|si", $txt, $m)) {
  231.     if ($count % 2 == 1) {
  232.       $txt = preg_replace("|{ifoddposition:".$this->quotere($m[1])."}|si", $m[1], $txt);
  233.     } else {
  234.       $txt = preg_replace("|{ifoddposition:".$this->quotere($m[1])."}|si", "", $txt);
  235.     }
  236.       } else if (preg_match("|{ifevenposition:([^{}]+)}|si", $txt, $m)) {
  237.     if ($count % 2 == 0) {
  238.       $txt = preg_replace("|{ifevenposition:".$this->quotere($m[1])."}|si", $m[1], $txt);
  239.     } else {
  240.       $txt = preg_replace("|{ifevenposition:".$this->quotere($m[1])."}|si", "", $txt);
  241.     }
  242.       } else if (preg_match("|{block:([^{}]+)}|si", $txt, $m)) {
  243.     $blockname = $m[1];
  244.     $block = $structure["blocks"][$blockname];
  245.     if ($block["sort"] != "" && count($content["blocks"][$blockname]) > 0) {
  246.       $this->compareField = $block["sort"];
  247.       usort($content["blocks"][$blockname], array($this, "compare"));
  248.     }
  249.     $rep = "";
  250.     if (count($content["blocks"][$blockname]) > 0) {
  251.       for ($i = 0; $i < count($content["blocks"][$blockname]); $i++) {
  252.         $rep .= $this->_toString($block,
  253.                      $content["blocks"][$blockname][$i],
  254.                      $i,
  255.                      count($content["blocks"][$blockname]));
  256.       }
  257.     } else if (isset($block["empty"])) {
  258.       $rep = $block["empty"];
  259.     }
  260.     $txt = preg_replace("|{block:".$blockname."}|si", $rep, $txt);
  261.       } else {
  262.     break;
  263.       }
  264.     }
  265.      
  266.     return $txt;
  267.   }
  268.  
  269.   function quotere($r)
  270.   {
  271.     return preg_replace(array("/\"/", "/\|/", "/\(/", "/\)/"),
  272.             array("\\\"", "\\|", "\\(", "\\)"),
  273.             $r);
  274.   }
  275.  
  276.   var $compareField;
  277.  
  278.   function compare($a, $b)
  279.   {
  280.     $an = $a["values"][$this->compareField];
  281.     $bn = $b["values"][$this->compareField];
  282.  
  283.     return strcoll(strtolower($an), strtolower($bn));
  284.   }
  285.  
  286.   function setVar($variable, $value)
  287.   {
  288.     if (is_array($value)) {
  289.       if (is_array($value[0])) {
  290.     foreach ($value as $v) {
  291.       $this->gotoNext($variable);
  292.       $this->setVar($variable, $v);
  293.     }
  294.       } else {
  295.     if ($variable != "") {
  296.       $variable = $variable.".";
  297.     }
  298.     foreach ($value as $k => $v) {
  299.       $this->_setVar($this->content, $variable.$k, $v);
  300.     }
  301.       }
  302.     } else {
  303.       // Convert $value to a string
  304.       $value = $value."";
  305.       $this->_setVar($this->content, $variable, $value);
  306.     }
  307.   }
  308.  
  309.   function _setVar(&$content, $variable, $value)
  310.   {
  311.     if (preg_match("|([^\.]+)\.(.*)|si", $variable, $m)) {
  312.       $parent = $m[1];
  313.       $var    = $m[2];
  314.  
  315.       if (empty($content["blocks"][$parent])) {
  316.     $content["blocks"][$parent] = array();
  317.     $content["blocks"][$parent][0] = array();
  318.       }
  319.       $i = count($content["blocks"][$parent])-1;
  320.       $this->_setVar($content["blocks"][$parent][$i], $var, $value);
  321.     } else {
  322.       if (empty($content["values"])) {
  323.     $content["values"] = array();
  324.       }
  325.       $content["values"][$variable] = $value;
  326.     }
  327.   }
  328.  
  329.   function gotoNext($variable)
  330.   {
  331.     $this->_gotoNext($this->content, $variable);  /** JEREMY, I removed the & symbol here, it was  $this->_gotoNext(&$this->content, $variable); **/
  332.   }
  333.  
  334.   function _gotoNext(&$content, $variable)
  335.   {
  336.     if (preg_match("|([^\.]+)\.(.*)|si", $variable, $m)) {
  337.       $parent = $m[1];
  338.       $var    = $m[2];
  339.  
  340.       if (empty($content["blocks"][$parent])) {
  341.     $content["blocks"][$parent] = array();
  342.     $content["blocks"][$parent][0] = array();
  343.       }
  344.       $i = count($content["blocks"][$parent])-1;
  345.       $this->_gotoNext($content["blocks"][$parent][$i], $var);  /** JEREMY, I removed the & symbol here, it was  $this->_gotoNext(&$content["blocks"][$parent][$i], $var); **/
  346.     } else {
  347.       if (empty($content["blocks"][$variable])) {
  348.     $content["blocks"][$variable] = array();
  349.       }
  350.       $content["blocks"][$variable][] = array("values" => array(),
  351.                           "blocks" => array());
  352.     }
  353.   }
  354.  
  355.   function loadfile($filename)
  356.   {
  357.    
  358.     $fd = fopen($filename, "r") or die("<br><br><font color=red>Your current style is missing some files, doesn't exists or its outdated. Please use LATEST core and module version.</font><br>This is how to manually update your custom modules - open module and replace contents: .html with .php<br><br><a href='./include/styleupdater.php'>1) try style fixer/updater tool (will fix outdated style)</a><br><br>2) If style fixer tool doesnt work, please go to config.php and change variable for style to:<br><br><font face=\"Courier New\", Courier, monospace>\$style='default';<br></red>");
  359.     $content = fread($fd, filesize($filename));
  360.     $content = substr($content, 14); //strip out php exit
  361.     fclose($fd);
  362.     return $filename2[1].$content;
  363.   }
  364.  
  365.   function error($message)
  366.   {
  367.     die($this->filename.": ".$message);
  368.   }
  369. }
Advertisement
Add Comment
Please, Sign In to add comment