PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/accounts/ldapadmin/htdocs/js/phplayersmenu/lib/PHPLIB.php

https://github.com/azeckoski/az-php-sandbox
PHP | 567 lines | 267 code | 61 blank | 239 comment | 47 complexity | f946665f70fda04325ea077a5ae693be MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. // vim: set expandtab tabstop=4 shiftwidth=4:
  3. // This code that was derived from the original PHPLIB Template class
  4. // is copyright by Kristian Koehntopp, NetUSE AG and was released
  5. // under the LGPL.
  6. //
  7. // Authors: Kristian Koehntopp <kris@koehntopp.de> (original from PHPLIB)
  8. // Bjoern Schotte <bjoern@rent-a-phpwizard.de> (PEARification)
  9. // Martin Jansen <mj@php.net> (PEAR conformance)
  10. //
  11. // $Id: PHPLIB.php,v 1.1.2.1 2005/11/27 03:55:50 wurley Exp $
  12. //
  13. //require_once "PEAR.php";
  14. /**
  15. * Converted PHPLIB Template class
  16. *
  17. * For those who want to use PHPLIB's fine template class,
  18. * here's a PEAR conforming class with the original PHPLIB
  19. * template code from phplib-stable CVS. Original author
  20. * was Kristian Koehntopp <kris@koehntopp.de>
  21. *
  22. * @author Bjoern Schotte <bjoern@rent-a-phpwizard.de>
  23. * @author Martin Jansen <mj@php.net> (PEAR conformance)
  24. * @version 1.0
  25. */
  26. class Template_PHPLIB
  27. {
  28. /**
  29. * If set, echo assignments
  30. * @var bool
  31. */
  32. var $debug = false;
  33. /**
  34. * $file[handle] = "filename";
  35. * @var array
  36. */
  37. var $file = array();
  38. /**
  39. * fallback paths that should be defined in a child class
  40. * @var array
  41. */
  42. var $file_fallbacks = array();
  43. /**
  44. * Relative filenames are relative to this pathname
  45. * @var string
  46. */
  47. var $root = "";
  48. /*
  49. * $_varKeys[key] = "key"
  50. * @var array
  51. */
  52. var $_varKeys = array();
  53. /**
  54. * $_varVals[key] = "value";
  55. * @var array
  56. */
  57. var $_varVals = array();
  58. /**
  59. * "remove" => remove undefined variables
  60. * "comment" => replace undefined variables with comments
  61. * "keep" => keep undefined variables
  62. * @var string
  63. */
  64. var $unknowns = "remove";
  65. /**
  66. * "yes" => halt, "report" => report error, continue, "no" => ignore error quietly
  67. * @var string
  68. */
  69. var $haltOnError = "report";
  70. /**
  71. * The last error message is retained here
  72. * @var string
  73. * @see halt
  74. */
  75. var $_lastError = "";
  76. /**
  77. * Constructor
  78. *
  79. * @access public
  80. * @param string template root directory
  81. * @param string how to handle unknown variables
  82. * @param array fallback paths
  83. */
  84. function Template_PHPLIB($root = ".", $unknowns = "remove", $fallback="")
  85. {
  86. $this->setRoot($root);
  87. $this->setUnknowns($unknowns);
  88. if (is_array($fallback)) $this->file_fallbacks = $fallback;
  89. }
  90. /**
  91. * Sets the template directory
  92. *
  93. * @access public
  94. * @param string new template directory
  95. * @return bool
  96. */
  97. function setRoot($root)
  98. {
  99. if (!is_dir($root)) {
  100. $this->halt("setRoot: $root is not a directory.");
  101. return false;
  102. }
  103. $this->root = $root;
  104. return true;
  105. }
  106. /**
  107. * What to do with unknown variables
  108. *
  109. * three possible values:
  110. *
  111. * - "remove" will remove unknown variables
  112. * (don't use this if you define CSS in your page)
  113. * - "comment" will replace undefined variables with comments
  114. * - "keep" will keep undefined variables as-is
  115. *
  116. * @access public
  117. * @param string unknowns
  118. */
  119. function setUnknowns($unknowns = "keep")
  120. {
  121. $this->unknowns = $unknowns;
  122. }
  123. /**
  124. * Set appropriate template files
  125. *
  126. * With this method you set the template files you want to use.
  127. * Either you supply an associative array with key/value pairs
  128. * where the key is the handle for the filname and the value
  129. * is the filename itself, or you define $handle as the file name
  130. * handle and $filename as the filename if you want to define only
  131. * one template.
  132. *
  133. * @access public
  134. * @param mixed handle for a filename or array with handle/name value pairs
  135. * @param string name of template file
  136. * @return bool
  137. */
  138. function setFile($handle, $filename = "")
  139. {
  140. if (!is_array($handle)) {
  141. if ($filename == "") {
  142. $this->halt("setFile: For handle $handle filename is empty.");
  143. return false;
  144. }
  145. $this->file[$handle] = $this->_filename($filename);
  146. } else {
  147. reset($handle);
  148. while (list($h, $f) = each($handle)) {
  149. $this->file[$h] = $this->_filename($f);
  150. }
  151. }
  152. }
  153. /**
  154. * Set a block in the appropriate template handle
  155. *
  156. * By setting a block like that:
  157. *
  158. * &lt;!-- BEGIN blockname --&gt;
  159. * html code
  160. * &lt;!-- END blockname --&gt;
  161. *
  162. * you can easily do repeating HTML code, i.e. output
  163. * database data nice formatted into a HTML table where
  164. * each DB row is placed into a HTML table row which is
  165. * defined in this block.
  166. * It extracts the template $handle from $parent and places
  167. * variable {$name} instead.
  168. *
  169. * @access public
  170. * @param string parent handle
  171. * @param string block name handle
  172. * @param string variable substitution name
  173. */
  174. function setBlock($parent, $handle, $name = "")
  175. {
  176. if (!$this->_loadFile($parent)) {
  177. $this->halt("setBlock: unable to load $parent.");
  178. return false;
  179. }
  180. if ($name == "") {
  181. $name = $handle;
  182. }
  183. $str = $this->getVar($parent);
  184. $reg = "/[ \t]*<!--\s+BEGIN $handle\s+-->\s*?\n?(\s*.*?\n?)\s*<!--\s+END $handle\s+-->\s*?\n?/sm";
  185. preg_match_all($reg, $str, $m);
  186. $str = preg_replace($reg, "{" . "$name}", $str);
  187. if (isset($m[1][0])) $this->setVar($handle, $m[1][0]);
  188. $this->setVar($parent, $str);
  189. }
  190. /**
  191. * Set corresponding substitutions for placeholders
  192. *
  193. * @access public
  194. * @param string name of a variable that is to be defined or an array of variables with value substitution as key/value pairs
  195. * @param string value of that variable
  196. * @param boolean if true, the value is appended to the variable's existing value
  197. */
  198. function setVar($varname, $value = "", $append = false)
  199. {
  200. if (!is_array($varname)) {
  201. if (!empty($varname))
  202. if ($this->debug) print "scalar: set *$varname* to *$value*<br>\n";
  203. $this->_varKeys[$varname] = $this->_varname($varname);
  204. ($append) ? $this->_varVals[$varname] .= $value : $this->_varVals[$varname] = $value;
  205. } else {
  206. reset($varname);
  207. while (list($k, $v) = each($varname)) {
  208. if (!empty($k))
  209. if ($this->debug) print "array: set *$k* to *$v*<br>\n";
  210. $this->_varKeys[$k] = $this->_varname($k);
  211. ($append) ? $this->_varVals[$k] .= $v : $this->_varVals[$k] = $v;
  212. }
  213. }
  214. }
  215. /**
  216. * Substitute variables in handle $handle
  217. *
  218. * @access public
  219. * @param string name of handle
  220. * @return mixed string substituted content of handle
  221. */
  222. function subst($handle)
  223. {
  224. if (!$this->_loadFile($handle)) {
  225. $this->halt("subst: unable to load $handle.");
  226. return false;
  227. }
  228. return @str_replace($this->_varKeys, $this->_varVals, $this->getVar($handle));
  229. }
  230. /**
  231. * Same as subst but printing the result
  232. *
  233. * @access public
  234. * @brother subst
  235. * @param string handle of template
  236. * @return bool always false
  237. */
  238. function pSubst($handle)
  239. {
  240. print $this->subst($handle);
  241. return false;
  242. }
  243. /**
  244. * Parse handle into target
  245. *
  246. * Parses handle $handle into $target, eventually
  247. * appending handle at $target if $append is defined
  248. * as TRUE.
  249. *
  250. * @access public
  251. * @param string target handle to parse into
  252. * @param string which handle should be parsed
  253. * @param boolean append it to $target or not?
  254. * @return string parsed handle
  255. */
  256. function parse($target, $handle, $append = false)
  257. {
  258. if (!is_array($handle)) {
  259. $str = $this->subst($handle);
  260. ($append) ? $this->setVar($target, $this->getVar($target) . $str) : $this->setVar($target, $str);
  261. } else {
  262. reset($handle);
  263. while (list(, $h) = each($handle)) {
  264. $str = $this->subst($h);
  265. $this->setVar($target, $str);
  266. }
  267. }
  268. return $str;
  269. }
  270. /**
  271. * Same as parse, but printing it.
  272. *
  273. * @access public
  274. * @brother parse
  275. * @param string target to parse into
  276. * @param string handle which should be parsed
  277. * @param should $handle be appended to $target?
  278. * @return bool
  279. */
  280. function pParse($target, $handle, $append = false)
  281. {
  282. print $this->finish($this->parse($target, $handle, $append));
  283. return false;
  284. }
  285. /**
  286. * Return all defined variables and their values
  287. *
  288. * @access public
  289. * @return array with all defined variables and their values
  290. */
  291. function getVars()
  292. {
  293. reset($this->_varKeys);
  294. while (list($k, ) = each($this->_varKeys)) {
  295. $result[$k] = $this->getVar($k);
  296. }
  297. return $result;
  298. }
  299. /**
  300. * Return one or more specific variable(s) with their values.
  301. *
  302. * @access public
  303. * @param mixed array with variable names or one variable name as a string
  304. * @return mixed array of variable names with their values or value of one specific variable
  305. */
  306. function getVar($varname)
  307. {
  308. if (!is_array($varname)) {
  309. if (isset($this->_varVals[$varname])) {
  310. return $this->_varVals[$varname];
  311. } else {
  312. return "";
  313. }
  314. } else {
  315. reset($varname);
  316. while (list($k, ) = each($varname)) {
  317. $result[$k] = (isset($this->_varVals[$k])) ? $this->_varVals[$k] : "";
  318. }
  319. return $result;
  320. }
  321. }
  322. /**
  323. * Get undefined values of a handle
  324. *
  325. * @access public
  326. * @param string handle name
  327. * @return mixed false if an error occured or the undefined values
  328. */
  329. function getUndefined($handle)
  330. {
  331. if (!$this->_loadFile($handle)) {
  332. $this->halt("getUndefined: unable to load $handle.");
  333. return false;
  334. }
  335. preg_match_all("/{([^ \t\r\n}]+)}/", $this->getVar($handle), $m);
  336. $m = $m[1];
  337. if (!is_array($m)) {
  338. return false;
  339. }
  340. reset($m);
  341. while (list(, $v) = each($m)) {
  342. if (!isset($this->_varKeys[$v])) {
  343. $result[$v] = $v;
  344. }
  345. }
  346. if (isset($result) && count($result)) {
  347. return $result;
  348. } else {
  349. return false;
  350. }
  351. }
  352. /**
  353. * Finish string
  354. *
  355. * @access public
  356. * @param string string to finish
  357. * @return finished, i.e. substituted string
  358. */
  359. function finish($str)
  360. {
  361. switch ($this->unknowns) {
  362. case "remove":
  363. $str = preg_replace('/{[^ \t\r\n}]+}/', "", $str);
  364. break;
  365. case "comment":
  366. $str = preg_replace('/{([^ \t\r\n}]+)}/', "<!-- Template $handle: Variable \\1 undefined -->", $str);
  367. break;
  368. }
  369. return $str;
  370. }
  371. /**
  372. * Print variable to the browser
  373. *
  374. * @access public
  375. * @param string name of variable to print
  376. */
  377. function p($varname)
  378. {
  379. print $this->finish($this->getVar($varname));
  380. }
  381. /**
  382. * Get finished variable
  383. *
  384. * @access public public
  385. * @param string variable to get
  386. * @return string string with finished variable
  387. */
  388. function get($varname)
  389. {
  390. return $this->finish($this->getVar($varname));
  391. }
  392. /**
  393. * Complete filename
  394. *
  395. * Complete filename, i.e. testing it for slashes
  396. *
  397. * @access private
  398. * @param string filename to be completed
  399. * @return string completed filename
  400. */
  401. function _filename($filename)
  402. {
  403. // if (substr($filename, 0, 1) != "/") {
  404. // $filename = $this->root."/".$filename;
  405. // }
  406. if (file_exists($filename)) return $filename;
  407. if (is_array($this->file_fallbacks) && count($this->file_fallbacks) > 0) {
  408. reset($this->file_fallbacks);
  409. while (list(,$v) = each($this->file_fallbacks)) {
  410. if (file_exists($v.basename($filename))) return $v.basename($filename);
  411. }
  412. $this->halt(sprintf("filename: file %s does not exist in the fallback paths %s.",$filename,implode(",",$this->file_fallbacks)));
  413. return false;
  414. } else {
  415. $this->halt(sprintf("filename: file %s does not exist.",$filename));
  416. return false;
  417. }
  418. return $filename;
  419. }
  420. /**
  421. * Protect a replacement variable
  422. *
  423. * @access private
  424. * @param string name of replacement variable
  425. * @return string replaced variable
  426. */
  427. function _varname($varname)
  428. {
  429. return "{".$varname."}";
  430. }
  431. /**
  432. * load file defined by handle if it is not loaded yet
  433. *
  434. * @access private
  435. * @param string handle
  436. * @return bool FALSE if error, true if all is ok
  437. */
  438. function _loadFile($handle)
  439. {
  440. if (isset($this->_varKeys[$handle]) and !empty($this->_varVals[$handle])) {
  441. return true;
  442. }
  443. if (!isset($this->file[$handle])) {
  444. $this->halt("loadfile: $handle is not a valid handle.");
  445. return false;
  446. }
  447. $filename = $this->file[$handle];
  448. if (function_exists("file_get_contents")) {
  449. $str = file_get_contents($filename);
  450. } else {
  451. if (!$fp = @fopen($filename,"r")) {
  452. $this->halt("loadfile: couldn't open $filename");
  453. return false;
  454. }
  455. $str = fread($fp,filesize($filename));
  456. fclose($fp);
  457. }
  458. if ($str=='') {
  459. $this->halt("loadfile: While loading $handle, $filename does not exist or is empty.");
  460. return false;
  461. }
  462. $this->setVar($handle, $str);
  463. return true;
  464. }
  465. /**
  466. * Error function. Halt template system with message to show
  467. *
  468. * @access public
  469. * @param string message to show
  470. * @return bool
  471. */
  472. function halt($msg)
  473. {
  474. $this->_lastError = $msg;
  475. if ($this->haltOnError != "no") {
  476. // return $this->haltMsg($msg);
  477. $this->haltMsg($msg);
  478. }
  479. if ($this->haltOnError == "yes") {
  480. die("<b>Halted.</b>");
  481. }
  482. return false;
  483. }
  484. /**
  485. * printf error message to show
  486. *
  487. * @access public
  488. * @param string message to show
  489. * @return object PEAR error object
  490. */
  491. function haltMsg($msg)
  492. {
  493. // PEAR::raiseError(sprintf("<b>Template Error:</b> %s<br>\n", $msg));
  494. printf("<b>Template Error:</b> %s<br>\n", $msg);
  495. }
  496. }
  497. ?>