PageRenderTime 769ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/include/template2.inc.php

http://becontent.googlecode.com/
PHP | 1344 lines | 1037 code | 257 blank | 50 comment | 187 complexity | 857a3da0e38201bdd7d9f96cddad2f94 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. DEFINE("SQUARE", 1);
  3. DEFINE("CURL", 2);
  4. /*
  5. Copyright 2006 (C) Alfonso Pierantonio
  6. This library can freely be used and distributed
  7. provided that you send and email to
  8. alfonso [at] di.univaq.it
  9. specifying [beContent] in the subject and the following
  10. information
  11. 1. name, surname
  12. 2. affiliation
  13. 3. commercial/personal
  14. 4. website where is in use
  15. Please consider to make a donation with payPal at the
  16. following address
  17. alfonso [at] di.univaq.it
  18. Thank you!
  19. */
  20. define('DEBUG', "DEBUG");
  21. Class Template {
  22. var
  23. $template_file,
  24. $buffer,
  25. $foreach,
  26. $content,
  27. $pars,
  28. $debug,
  29. $parsed,
  30. $foreach_counter,
  31. $cache,
  32. $tags,
  33. $escaped_tags;
  34. function Template($filename = "") {
  35. if (file_exists($filename)) {
  36. $this->template_file = $filename;
  37. } else {
  38. if (count($GLOBALS['config']['languages'])>0) {
  39. $new_filename = substr($filename, 0, strpos($filename, "dtml"));
  40. $new_filename .= "dtml/".$GLOBALS['config']['currentlanguage'];
  41. $new_filename .= substr($filename, strpos($filename, "dtml")+4);
  42. $filename = $new_filename;
  43. $this->template_file = $filename;
  44. }
  45. }
  46. $this->foreach = new ForeachCode();
  47. $result = preg_match("~\/?([^<>\/]+)\z~Us",$filename, $token);
  48. if ($result) $this->template_file = $token[1];
  49. $this->cache = new Cache($this->template_file);
  50. $this->debug = $debug;
  51. $this->parsed = FALSE;
  52. $this->foreach_counter = 1;
  53. if ($filename != "") {
  54. if (file_exists($filename)) {
  55. $fp = fopen ($filename, "r");
  56. $this->buffer = fread($fp, filesize($filename));
  57. fclose($fp);
  58. } else {
  59. $this->buffer = $filename;
  60. }
  61. }
  62. $i=0;
  63. $this->setTagStyle(SQUARE);
  64. }
  65. function setTagStyle($style) {
  66. switch ($style) {
  67. case CURL:
  68. $this->tags['open'] = "{";
  69. $this->tags['close'] = "}";
  70. $this->escaped_tags['open'] = "{";
  71. $this->escaped_tags['close'] = "}";
  72. break;
  73. case SQUARE:
  74. default:
  75. $this->tags['open'] = "<[";
  76. $this->tags['close'] = "]>";
  77. $this->escaped_tags['open'] = "<\[";
  78. $this->escaped_tags['close'] = "\]>";
  79. break;
  80. }
  81. }
  82. function setTemplate($content) {
  83. $this->buffer = $content;
  84. }
  85. ////////////////////////////////////////////////////////////////*KAISERSOSE*/////////////////////////////////////////////////////////////////////////////////////////
  86. function checkPlaceholders($buffer){
  87. $temp_buffer = $buffer;
  88. $root = preg_replace("~<\[foreach\]>.*<\[\/foreach\]>~Us","",$temp_buffer,-1);
  89. do{
  90. $result1 = preg_match("~<\[foreach\]>(.*)<\[\/foreach\]>~Us",$temp_buffer,$token);
  91. if ($result1) {
  92. $temp_buffer = preg_replace("~<\[foreach\]>.*<\[\/foreach\]>~Us","",$temp_buffer,1);
  93. $result2 = preg_match_all("~<\[(?!foreach|\/foreach)(.+)(::(.*))?\]>~Us",$token[1],$token2,PREG_PATTERN_ORDER);
  94. if ($result2) {
  95. foreach($token2[1] as $name){
  96. $result3 = preg_match("~<\[foreach\]>(.*){$this->escaped_tags['open']}$name(::(.*))?{$this->escaped_tags['close']}(.*)<\[\/foreach\]>~Us",$temp_buffer,$token3); if ($result3) {
  97. trigger_error("cannot define variables with the same name in different foreach construct: (<[$name]>)", E_USER_ERROR);
  98. }
  99. $result4 = preg_match("~{$this->escaped_tags['open']}$name(::(.*))?{$this->escaped_tags['close']}~Us",$root,$token4);
  100. if ($result4){
  101. trigger_error("cannot define variables with the same name in foreach construct and out of them: (<[$name]>)", E_USER_ERROR);
  102. }
  103. }
  104. }
  105. }
  106. }while ($result1);
  107. }
  108. function checkForeachConstructs($buffer){
  109. $temp_buffer = $buffer;
  110. $counter_foreach_opened = 0;
  111. $counter_foreach_closed = 0;
  112. do{
  113. $result = preg_match("~(<\[foreach\]>)|(<\[\/foreach\]>)~Us",$temp_buffer,$token);
  114. if($result){
  115. if($token[1] == "<[foreach]>") $counter_foreach_opened++;
  116. else $counter_foreach_closed++;
  117. $temp_buffer = preg_replace("~<\[foreach\]>|<\[\/foreach\]>~Us","",$temp_buffer,1);
  118. }
  119. if ($counter_foreach_opened < $counter_foreach_closed){
  120. $diff = $counter_foreach_closed - $counter_foreach_opened;
  121. trigger_error("cannot define <i><[/foreach]></i> close tag without <i><[foreach]></i> open tag",E_USER_ERROR);
  122. }
  123. } while ($result);
  124. if($counter_foreach_opened > $counter_foreach_closed){
  125. $diff = $counter_foreach_opened - $counter_foreach_closed;
  126. trigger_error("cannot define <i><[foreach]></i> open tag without <i><[/foreach]></i> close tag",E_USER_ERROR);
  127. }
  128. }
  129. function checkTagLibraries($buffer) {
  130. $temp_buffer = $buffer;
  131. do {
  132. $result = preg_match("~{$this->escaped_tags['open']}(.+)(::(.*)){$this->escaped_tags['close']}~U",$temp_buffer,$token);
  133. if ($result and (count($token)>1)) {
  134. if ($token[3]=="") {
  135. trigger_error("undefined selector of tagLibrary for <i><[$token[1]]></i> variable",E_USER_ERROR);
  136. } else {
  137. $result2 = preg_match("~\A::\S+~Us",$token[2]);
  138. if (!$result2) {
  139. trigger_error("undefined selector of tagLibrary for <i><[$token[1]]></i> variable",E_USER_ERROR);
  140. }
  141. $result3 = preg_match("~\A::(?!library)~Us",$token[2]);
  142. if (!$result3) {
  143. trigger_error("cannot use string <i>library</i> as a selector for <i><[$token[1]]></i> variable",E_USER_ERROR);
  144. }
  145. $result3 = preg_match("~(\"+|\s+)library\s*=\s*\"?\S*\"?~Us",$token[2]);
  146. if (!$result3) {
  147. trigger_error("undefined tagLibrary for <i><[$token[1]]></i> variable",E_USER_WARNING);
  148. }
  149. }
  150. }
  151. $temp_buffer = preg_replace("~{$this->escaped_tags['open']}.+(::.+)?{$this->escaped_tags['close']}~Us",$temp_buffer,"",1);
  152. } while ($result);
  153. }
  154. function setContent($name,$value, $pars = ""){
  155. if(gettype($name) != "string") {
  156. trigger_error('$name cannot be a '.gettype($name).' must be a string', E_USER_WARNING);
  157. }
  158. $this->content[0][]=$name;
  159. $this->content[1][]=$value;
  160. $this->content[2] = $pars;
  161. }
  162. function loadContent($content){
  163. $contentKeys=$content[0];
  164. $contentValue=$content[1];
  165. for($i=0;$i<count($contentKeys);$i++){
  166. $placeholderName = $contentKeys[$i];
  167. $placeholderValue = $contentValue[$i];
  168. if($foreachCode = $this->foreach->getForeachCode($this->foreach->getForeachName($placeholderName))){// Se è un contenuto iterato
  169. $parsedContent = $this->transformContent($contentKeys[$i],$contentValue[$i],$foreachCode);
  170. if (is_array($parsedContent)) {
  171. foreach($parsedContent as $currentParsedContentName=>$currentParsedContentValue){
  172. $finalContent[0][] = $currentParsedContentName;
  173. $finalContent[1][] = $currentParsedContentValue;
  174. }
  175. }
  176. } else {// Se non è un contenuto iterato
  177. $parsedContent = $this->transformContent($contentKeys[$i],$contentValue[$i],$this->buffer);
  178. if (is_array($parsedContent)) {
  179. foreach($parsedContent as $currentParsedContentName=>$currentParsedContentValue){
  180. $finalContent[0][] = $currentParsedContentName;
  181. $finalContent[1][] = $currentParsedContentValue;
  182. }
  183. }
  184. }
  185. }
  186. return $finalContent;
  187. }
  188. function loadEmptyContent($buffer){
  189. $finalContent = NULL;
  190. $result = preg_match_all("~<\[(?!foreach\d+_\d+|\/foreach\d+_\d+)(.+)\]>~Us",$buffer,$token,PREG_SET_ORDER);
  191. if($result){
  192. foreach($token as $placeholder){
  193. $result = preg_match("~([^:]+)~",$placeholder[1],$token2);
  194. if($result){
  195. $placeholderName = $token2[1];
  196. $parsedContent = $this->transformContent($placeholderName,NULL,$buffer);
  197. if (is_array($parsedContent)) {
  198. foreach($parsedContent as $currentParsedContentName=>$currentParsedContentValue){
  199. $finalContent[0][] = $currentParsedContentName;
  200. $finalContent[1][] = $currentParsedContentValue;
  201. }
  202. }
  203. }
  204. }
  205. for($i=0;$i<count($finalContent[0]);$i++){
  206. $currentParsedContentName = $finalContent[0][$i];
  207. $currentParsedContentValue = $finalContent[1][$i];
  208. $buffer = preg_replace("~{$this->escaped_tags['open']}$currentParsedContentName{$this->escaped_tags['close']}~Us",$currentParsedContentValue,$buffer,1);
  209. }
  210. }
  211. return $buffer;
  212. }
  213. function transformContent($name,$data,$buffer){ //Interpretazione, e codifica di placeholder complessi
  214. static $library_obj,
  215. $library,
  216. $selectors;
  217. $simple_pattern = "~<\[($name)\]>~Us";
  218. $complex_pattern = "~<\[(($name)::(\w+))\]>~Us";
  219. $complex_pattern_param = "~<\[(($name)::(\S+)\s+([^\]]+))\]>~Us";
  220. #$complex_pattern_param = "~<\[(($name)::(\S+)\s+([^\]>]+))\]>~Us";
  221. $par_pattern="~(\S+)=\"?([^\"]+)\"?~s";
  222. $replacement = "";
  223. $value = NULL;
  224. $result = preg_match($complex_pattern_param,$buffer,$token);
  225. do {
  226. if ($result) {
  227. $selector = $token[3];
  228. $result = preg_match("~(library=\"?([^\"\s]+)\"?)~s",$token[4],$token2);
  229. if ($result) { //Trovato parametro libreria
  230. $library=$token2[2];
  231. $token[4]=preg_replace("~library=\"?([^\"\s]+)\"?~s","",$token[4],-1);
  232. }
  233. $result = preg_match_all("~(\w+)=\"?([^\"]+)\"?~s",$token[4],$token3,PREG_SET_ORDER);
  234. #$result = preg_match_all("~(\w+)=\"?([^\"<>\s]+)\"?~s",$token[4],$token3,PREG_SET_ORDER);
  235. if ($result) {//Trovati altri parametri
  236. foreach($token3 as $item) {
  237. $params[] = $item[1];
  238. $params_value[] = $item[2];
  239. }
  240. for($i=0;$i<count($params);$i++){
  241. $parameter[$params[$i]] = $params_value[$i];
  242. }
  243. } else { //Non ci sono altri parametri
  244. $parameter[] = "";
  245. }
  246. if ($library_obj) {
  247. if (get_class($library_obj) != strtolower($library)) {
  248. if (!file_exists("include/tags/$library.inc.php")) {
  249. trigger_error("Library <b>$library</b> does not exists!",E_USER_ERROR);
  250. }
  251. include_once("include/tags/$library.inc.php");
  252. eval("\$library_obj = new \$library();");
  253. $selectors = $library_obj->getSelectors();
  254. }
  255. #$key = array_search($selector,$selectors);
  256. $key = array_search(strtolower($selector),$selectors);
  257. if (!$key) {
  258. trigger_error("selector <b>$selector</b> does not exist in library <b>$library</b>",E_USER_ERROR);
  259. }
  260. $value[$token[1]] = $library_obj->apply($name,$data,$parameter,$selector);
  261. } else {
  262. if (!file_exists("include/tags/$library.inc.php")) {
  263. trigger_error("Library <b>$library</b> does not exists!",E_USER_ERROR);
  264. } else {
  265. include_once("include/tags/$library.inc.php");
  266. eval("\$library_obj = new \$library();");
  267. $selectors = $library_obj->getSelectors();
  268. $key = array_search(strtolower($selector),$selectors);
  269. /*
  270. Just added strtolower, not sure is right but works.
  271. */
  272. if (!$key) {
  273. trigger_error("selector <b>$selector</b> does not exist in library <b>$library</b>",E_USER_ERROR);
  274. }
  275. $value[$token[1]] = $library_obj->apply($name,$data,$parameter,$selector);
  276. }
  277. }
  278. $token[1] = $this->string_to_pattern($token[1]);
  279. $buffer = preg_replace("~<\[$token[1]\]>~Us","",$buffer,-1);
  280. $result = preg_match($complex_pattern_param,$buffer,$token);
  281. } else {// Non ci sono placeholder $name con selettore e parametri
  282. $result = preg_match($complex_pattern,$buffer,$token);
  283. if ($result) {//Trovato placeholder $name con selettore senza parametri
  284. $parameter[] = "";
  285. $selector = $token[3];
  286. if ($library_obj) {
  287. if (get_class($library_obj) != strtolower($library)) {
  288. if (!file_exists("include/tags/$library.inc.php")) {
  289. trigger_error("Library <b>$library</b> does not exists!",E_USER_ERROR);
  290. }
  291. include_once("include/tags/$library.inc.php");
  292. eval("\$library_obj = new \$library();");
  293. $selectors = $library_obj->getSelectors();
  294. }
  295. $key = array_search($selector,$selectors);
  296. if (!$key) {
  297. trigger_error("selector <b>$selector</b> does not exist in library <b>$library</b>",E_USER_ERROR);
  298. }
  299. $value[$token[1]] = $library_obj->apply($name,$data,$parameter,$selector);
  300. } else {
  301. if (!file_exists("include/tags/$library.inc.php")) {
  302. trigger_error("Library <b>$library</b> does not exists!",E_USER_ERROR);
  303. }
  304. include_once("include/tags/$library.inc.php");
  305. eval("\$library_obj = new \$library();");
  306. $selectors = $library_obj->getSelectors();
  307. $key = array_search($selector,$selectors);
  308. if (!$key) {
  309. trigger_error("selector <b>$selector</b> does not exist in library <b>$library</b>",E_USER_ERROR);
  310. }
  311. $value[$token[1]] = $library_obj->apply($name,$data,$parameter,$selector);
  312. }
  313. $buffer = preg_replace("~<\[$token[1]\]>~Us","",$buffer,-1);
  314. $result = preg_match($complex_pattern,$buffer,$token);
  315. } else {//Non ci sono placeholder $name complessi
  316. $result = preg_match($simple_pattern,$buffer,$token);
  317. if ($result) {// Trovato placeholder $name semplice
  318. $value[$name] = $data;
  319. $buffer = preg_replace("~<\[$name\]>~Us","",$buffer,-1);
  320. $result = preg_match("~<\[($name)\]>~Us",$buffer,$token);
  321. }
  322. }
  323. }
  324. } while ($result);
  325. return $value;
  326. }
  327. function string_to_pattern($pattern){
  328. $escape_array=Array('\"','\.');
  329. foreach($escape_array as $char){
  330. $pattern = preg_replace("~$char~",$char,$pattern,-1);
  331. }
  332. return $pattern;
  333. }
  334. function parse(){
  335. if ($this->template_file == "frame-public.html") {
  336. if (($_REQUEST['mode'] == "ajax") or
  337. ($_REQUEST['mode'] == "compact")) {
  338. foreach($this->content[0] as $index => $name) {
  339. if ($name == "body") {
  340. $position = $index;
  341. }
  342. }
  343. $this->buffer = $this->content[1][$position];
  344. } else {
  345. $this->common();
  346. }
  347. }
  348. #$this->setContent("script", basename($_SERVER['SCRIPT_NAME']));
  349. $this->setContent("skin", $GLOBALS['config']['skin']);
  350. $this->setContent("base", $GLOBALS['config']['base']);
  351. $this->setContent("username", $_SESSION['user']['username']);
  352. $this->setContent("user_name", $_SESSION['user']['name']);
  353. $this->setContent("user_surname", $_SESSION['user']['surname']);
  354. $this->setContent("email", $_SESSION['user']['email']);
  355. if ($this->cache->caching == TRUE){
  356. if ($this->cache->cacheFileExists()) {
  357. if ($this->cache->checkCacheFile()) {
  358. $this->buffer = $this->cache->getCacheFileContent();
  359. } else {
  360. $this->checkPlaceholders($this->buffer);
  361. $this->checkForeachConstructs($this->buffer);
  362. $this->checkTagLibraries($this->buffer);
  363. $this->buffer = $this->foreach->parseForeach($this->buffer);
  364. $this->content = $this->loadContent($this->content);
  365. $this->buffer = $this->foreach->bindAll($this->content,$this->buffer);
  366. $this->buffer = $this->loadEmptyContent($this->buffer);
  367. $this->buffer = $this->cache->buildCacheFile($this->buffer,$this->template_file);
  368. }
  369. } else {
  370. $this->checkPlaceholders($this->buffer);
  371. $this->checkForeachConstructs($this->buffer);
  372. $this->checkTagLibraries($this->buffer);
  373. $this->buffer = $this->foreach->parseForeach($this->buffer);
  374. $this->content = $this->loadContent($this->content);
  375. $this->buffer = $this->foreach->bindAll($this->content,$this->buffer);
  376. $this->buffer = $this->loadEmptyContent($this->buffer);
  377. $this->buffer = $this->cache->buildCacheFile($this->buffer,$this->template_file);
  378. }
  379. } else {
  380. /* The following are used for checking Errors! */
  381. $this->checkPlaceholders($this->buffer);
  382. $this->checkForeachConstructs($this->buffer);
  383. $this->checkTagLibraries($this->buffer);
  384. $this->buffer = $this->foreach->parseForeach($this->buffer);
  385. $this->content = $this->loadContent($this->content);
  386. $this->buffer = $this->foreach->bindAll($this->content,$this->buffer);
  387. $this->buffer = $this->loadEmptyContent($this->buffer);
  388. }
  389. $this->parsed = true;
  390. }
  391. /* ALFONSO ON */
  392. function getExtension() {
  393. #$extension = "\n<!-- being generated inclusions -->\n";
  394. $files = array();
  395. foreach(get_declared_classes() as $k => $v) {
  396. if (strtolower(get_parent_class($v)) == "taglibrary") {
  397. $methods = get_class_methods($v); //print_r($methods);
  398. if (in_array("includejs",$methods)) {
  399. eval("\$files[] = ".$v."::includeJS();");
  400. }
  401. }
  402. }
  403. for($i=0;$i<count($files);$i++) {
  404. $extension .= "<script type=\"text/javascript\" src=\"{$files[$i]}\"></script>\n";
  405. }
  406. $files = array();
  407. foreach(get_declared_classes() as $k => $v) {
  408. if (strtolower(get_parent_class($v)) == "taglibrary") {
  409. $methods = get_class_methods($v); //print_r($methods);
  410. if (in_array("includestyle",$methods)) {
  411. eval("\$files[] = ".$v."::includeStyle();");
  412. }
  413. }
  414. }
  415. for($i=0;$i<count($files);$i++) {
  416. $extension .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"{files[$i]}\" />\n";
  417. }
  418. $local_buffer = "";
  419. foreach(get_declared_classes() as $k => $v) {
  420. if (strtolower(get_parent_class($v)) == "taglibrary") {
  421. $methods = get_class_methods($v); //print_r($methods);
  422. if (in_array("injectjs",$methods)) {
  423. eval("\$local_buffer .= ".$v."::injectJS();");
  424. if ($local_buffer != "") {
  425. $local_buffer .= "\n\n";
  426. }
  427. }
  428. }
  429. }
  430. if ($local_buffer != "") {
  431. $extension .= "<script>\n{$local_buffer}";
  432. $extension .= "</script>\n";
  433. }
  434. $local_buffer = "";
  435. foreach(get_declared_classes() as $k => $v) {
  436. if (strtolower(get_parent_class($v)) == "taglibrary") {
  437. $methods = get_class_methods($v); //print_r($methods);
  438. if (in_array("injectstyle",$methods)) {
  439. eval("\$local_buffer .= ".$v."::injectStyle();");
  440. if ($local_buffer != "") {
  441. $local_buffer .= "\n\n";
  442. }
  443. }
  444. }
  445. }
  446. if ($local_buffer != "") {
  447. $extension .= "<style>\n{$local_buffer}";
  448. $extension .= "</style> <!-- end generated inclusions -->\n";
  449. }
  450. return $extension;
  451. }
  452. /* ALFONSO OFF */
  453. function display($buffer){
  454. echo $buffer;
  455. }
  456. function get() {
  457. if (!$this->parsed) {
  458. $this->parse();
  459. }
  460. $this->buffer = preg_replace("~<\[(?!foreach\d+_\d+|\/foreach\d+_\d+).+\]>~Us","",$this->buffer,-1);
  461. $this->buffer = preg_replace("~<\[foreach\d+_\d+\]>~Us","",$this->buffer,-1);
  462. $this->buffer = preg_replace("~<\[\/foreach\d+_\d+\]>~Us","",$this->buffer,-1);
  463. return $this->buffer;
  464. }
  465. function close(){
  466. $this->setContent("style", $this->getExtension());
  467. if (!$this->parsed) {
  468. $this->parse();
  469. }
  470. #echo $this->getExtension();exit;
  471. $this->buffer = preg_replace("~<\[(?!foreach\d+_\d+|\/foreach\d+_\d+).+\]>~Us","",$this->buffer,-1);
  472. /*$this->buffer = preg_replace("~<\[foreach\d+_\d+\]>~Us","",$this->buffer,-1);
  473. $this->buffer = preg_replace("~<\[\/foreach\d+_\d+\]>~Us","",$this->buffer,-1);*/
  474. $pos = strpos($this->buffer, "</head>");
  475. $result = $this->buffer;
  476. if ($pos != false) {
  477. $pre = substr($this->buffer, 0, $pos);
  478. $post = substr($this->buffer,$pos+7);
  479. $result = $pre.$this->getExtension()."</head>\n".$post;
  480. }
  481. $this->display($result);
  482. }
  483. function setCacheLifeTime($lifetime){
  484. $this->cache->setCacheLifetime($lifetime);
  485. }
  486. function caching($flag){
  487. $this->cache->caching($flag);
  488. }
  489. function common() {
  490. if (function_exists("shared")) {
  491. shared();
  492. }
  493. }
  494. }
  495. Class Cache{
  496. var $caching,
  497. $cache_dir,
  498. $cache_file,
  499. $cache_file_lifetime;
  500. function Cache($template_file){
  501. $this->cache_dir = $this->setDefaultCacheDir();
  502. $this->caching = FALSE;
  503. $this->cache_file_lifetime = 60;
  504. $this->template_file_name = $template_file;
  505. $this->cache_file = $this->setDefaultCacheFile();
  506. }
  507. function setDefaultCacheFile(){
  508. $cache_dir = "cache";
  509. $cache_file = $cache_dir."/".$this->template_file_name."_temp.php";
  510. return $cache_file;
  511. }
  512. function setDefaultCacheDir(){
  513. $cache_dir ="cache";
  514. return $cache_dir;
  515. }
  516. function caching($flag){
  517. if(gettype($flag) == "boolean") $this->caching = $flag;
  518. else trigger_error('$flag cannot be a '.gettype($flag).' must be a boolean',E_USER_WARNING);
  519. if (!$flag){
  520. if($this->cacheFileExists()){
  521. if (PHP_OS == "WINNT" or PHP_OS == "WIN32") {
  522. $temp_dir = $this->getDefaultCacheDirName();
  523. $temp_file = $this->getDefaultCacheFileName();
  524. exec("del $temp_dir\\$temp_file",$output,$result);
  525. if($result !=0){
  526. trigger_error("cannot remove <i>$this->cache_file</i> cache file",E_USER_WARNING);
  527. }
  528. }
  529. else {
  530. exec("del $this->cache_file",$output,$result);
  531. if($result !=0){
  532. trigger_error("cannot remove <i>$this->cache_file</i> cache file",E_USER_WARNING);
  533. }
  534. }
  535. exec("rmdir $this->cache_dir",$output,$result);
  536. if($result !=0){
  537. trigger_error("cannot remove <i>$this->cache_dir</i> directory",E_USER_WARNING);
  538. }
  539. }
  540. }
  541. else{
  542. exec("mkdir $this->cache_dir",$output,$result);
  543. if($result !=0 and !$this->cacheFileExists()){
  544. trigger_error("cannot create <i>$this->cache_dir</i> directory",E_USER_WARNING);
  545. }
  546. }
  547. }
  548. function buildCacheFile($buffer,$template_file){
  549. $cfile = fopen($this->cache_file,"w");
  550. $info_cache_file = fstat($cfile);
  551. $buffer = "<!-- This page has been generated the first time on ".date("d/m/Y",$info_cache_file[10])." at ".date("H:i:s",$info_cache_file[10]).". Last generation on ".date("d/m/Y",$info_cache_file[9])." at ".date("H:i:s",$info_cache_file[9])." -->".$buffer;
  552. fwrite($cfile,$buffer);
  553. fclose($cfile);
  554. return $buffer;
  555. }
  556. function getCacheFileContent(){
  557. $cfile = fopen($this->cache_file,"r");
  558. $buffer = fread($cfile,filesize($this->cache_file));
  559. fclose($cfile);
  560. return $buffer;
  561. }
  562. function cacheFileExists(){
  563. return file_exists($this->cache_file);
  564. }
  565. function checkCacheFile(){
  566. $cfile = fopen($this->cache_file,"r");
  567. $info_cache_file = fstat($cfile);
  568. $lifetime = (time())-($info_cache_file[9]);
  569. if($this->cache_file_lifetime<$lifetime){
  570. fclose($cfile);
  571. return FALSE;
  572. }
  573. else{
  574. fclose($cfile);
  575. return TRUE;
  576. }
  577. }
  578. function setCacheLifetime($lifetime){
  579. if(gettype($lifetime) == "integer") $this->cache_file_lifetime = $lifetime;
  580. else trigger_error('$lifetime cannot be a '.gettype($lifetime).' must be an integer',E_USER_WARNING);
  581. }
  582. function setCacheDir($dir){
  583. if(gettype($dir) == "string") $this->cache_dir = $dir;
  584. else trigger_error('$dir cannot be a '.gettype($dir).' must be a string',E_USER_WARNING);
  585. }
  586. function getDefaultCacheDirName(){
  587. return $this->cache_dir;
  588. }
  589. function getDefaultCacheFileName(){
  590. $result = preg_match("~\/(.+)~",$this->cache_file,$token);
  591. return $token[1];
  592. }
  593. }
  594. Class TagLibrary {
  595. var $selectors;
  596. function TagLibrary(){
  597. $this->selectors = get_class_methods($this);
  598. }
  599. function apply($name,$data,$pars,$selector){
  600. $result = call_user_func(array($this,$selector),$name,$data,$pars);
  601. return $result;
  602. }
  603. function getSelectors(){
  604. return $this->selectors;
  605. }
  606. }
  607. Class ForeachCode {
  608. var $foreachCodeArray,
  609. $placeholderArray,
  610. $foreachHierarchyArray,
  611. $deep;
  612. function ForeachCode(){
  613. $this->deep=0;
  614. $this->foreachCounter=0;
  615. }
  616. function parseForeach($buffer){
  617. $buffer = $this->getForeachEnumeration($buffer);
  618. $result = preg_match_all("~<\[(foreach(\d+)_(\d+))\]>(.+)<\[\/foreach\\2_\\3\]>~Us",$buffer,$token,PREG_SET_ORDER);
  619. if($result){
  620. foreach($token as $currentForeach){
  621. $this->fillForeachArray($currentForeach[1],$currentForeach[4]); //Alimento gli array per il binding dei contenuti
  622. $buffer = preg_replace("~<\[$currentForeach[1]\]>.+<\[\/$currentForeach[1]\]>~Us","<[$currentForeach[1]]><!--FOREACH CODE--><[/$currentForeach[1]]>",$buffer,-1);
  623. }
  624. }
  625. return $buffer;
  626. }
  627. function getForeachEnumeration($buffer){
  628. $deep = -1;
  629. $ID = 0;
  630. $IDarray[0] = 1;
  631. $result = preg_match("~<\[\/?foreach\]>~Us",$buffer,$foreachOpenTag);
  632. while($result){
  633. if($foreachOpenTag[0]=="<[foreach]>"){
  634. $deep++;
  635. $ID++;
  636. array_push($IDarray,$ID);
  637. $buffer = preg_replace("~<\[foreach\]>~Us","<[foreach".$deep."_".$ID."]>",$buffer,1);
  638. }
  639. else if($foreachOpenTag[0]=="<[/foreach]>"){
  640. $lastForeachID = array_pop($IDarray);
  641. $buffer = preg_replace("~<\[\/foreach\]>~Us","<[/foreach".$deep."_".$lastForeachID."]>",$buffer,1);
  642. $deep=$deep-1;
  643. }
  644. $result = preg_match("~<\[\/?foreach\]>~Us",$buffer,$foreachOpenTag);
  645. }
  646. return $buffer;
  647. }
  648. function fillForeachArray($foreachName,$foreachCode){
  649. //Pulisco il codice del foreach eliminando quello dei foreach annidati in modo da considerare solo i placeholder a questo livello
  650. $result = preg_match_all("~<\[(foreach(\d+)_(\d+))\]>.+<\[\/foreach\\2_\\3\]>~Us",$foreachCode,$token2,PREG_SET_ORDER);
  651. if ($result){//currentForeachChild[1] = foreachChildName
  652. foreach($token2 as $currentForeachChild){
  653. $foreachChildName = $currentForeachChild[1];
  654. $foreachCleanedCode = preg_replace("~<\[$foreachChildName\]>.+<\[\/$foreachChildName\]>~Us","<[$foreachChildName]>FOREACH_CODE<[/$foreachChildName]>",$foreachCode,1);
  655. }
  656. //Alimento array dei foreach con il foreach corrente
  657. $this->foreachCodeArray[$foreachName] = $foreachCleanedCode;
  658. }// Fine pulizia
  659. else{// Se non ci sono foreach annidati non serve pulire il codice
  660. $foreachCleanedCode = $foreachCode; // Mi serve per cercare solo i placeholdea questo livello nel prossimo if
  661. $this->foreachCodeArray[$foreachName] = $foreachCode;
  662. }
  663. //Alimento array dei placeholder nel foreach
  664. $result = preg_match_all("~<\[(?!foreach\d+_\d+|\/foreach\d+_\d+)(.+)\]>~Us",$foreachCleanedCode,$token,PREG_SET_ORDER);
  665. if ($result){//currentPlaceholder[1] = placeholderName
  666. foreach($token as $currentPlaceholder){
  667. $placeholderName = $currentPlaceholder[1];
  668. $this->setForeachPlaceholder($foreachName,$placeholderName);
  669. }
  670. }// Fine alimentazione array dei placeholder nel foreach
  671. //Alimento array dei nomi dei foreach annidati nel foreach corrente e invoco questa funzione ricorsiva per i foreach figli
  672. $result = preg_match_all("~<\[(foreach(\d+)_(\d+))\]>(.+)<\[\/foreach\\2_\\3\]>~Us",$foreachCode,$token2,PREG_SET_ORDER);
  673. if ($result){//currentForeachChild[1] = foreachChildName
  674. foreach($token2 as $currentForeachChild){
  675. $foreachChildName = $currentForeachChild[1];
  676. $foreachChildCode = $currentForeachChild[4];
  677. //Ricorsione
  678. $this->foreachHierarchyArray[$foreachName][] = $foreachChildName;
  679. $this->fillForeachArray($foreachChildName,$foreachChildCode);
  680. }
  681. }// Fine
  682. }
  683. function bindAll($content,$buffer){
  684. if($content){
  685. $contentKeys=$content[0];
  686. $contentValue=$content[1];
  687. }
  688. else{
  689. return $buffer;
  690. }
  691. for($i=0;$i<count($contentValue);$i++){// Istanzio i placeholder non iterati
  692. $currentContentValue=$contentValue[$i];
  693. $currentContentName=$contentKeys[$i];
  694. $buffer = $this->setSimplePlaceholderValue($currentContentName,$currentContentValue,$buffer);
  695. }
  696. $result = preg_match_all("~<\[(foreach\d+_\d+)\]>.+<\[\/\\1\]>~Us",$buffer,$token,PREG_SET_ORDER);
  697. if($result){
  698. foreach($token as $currentForeachName){//Istanzio i contenuti dei foreach
  699. $temp_content = $content; //Evito di eliminare tutti i contenuti con il primo foreach
  700. $currentForeachCode = $this->getForeachCode($currentForeachName[1]);
  701. $currentForeachChilds = $this->getForeachChilds($currentForeachName[1]);
  702. $currentForeachPlaceholders = $this->getForeachPlaceholders($currentForeachName[1]);
  703. $buffer = $this->setForeachBindedCode($currentForeachName[1],$buffer,$this->bindForeach($currentForeachName[1],$currentForeachCode,$currentForeachPlaceholders,$currentForeachChilds,$temp_content));
  704. }
  705. }
  706. return $buffer;
  707. }
  708. function bindForeach($foreachName,$foreachCode,$foreachPlaceholders,$foreachChilds,&$content){
  709. reset($content);
  710. if($content[0]){//Ci sono contenuti in content
  711. if($foreachChilds){//Il foreach ha annidamenti
  712. $currentContentName=reset($content[0]);
  713. $currentContentValue=reset($content[1]);
  714. do{
  715. if($this->isForeachPlaceholder($currentContentName)){//E' un contenuto da iterare
  716. if($this->isTherePlaceholder($currentContentName,$foreachName)){//E' un contenuto per questo foreach
  717. $foreachCode = $this->setPlaceholderValue($currentContentName,$currentContentValue,$foreachName,$foreachCode);//inserisco
  718. array_shift($content[0]);
  719. array_shift($content[1]);
  720. $currentContentName=reset($content[0]);
  721. $currentContentValue=reset($content[1]);
  722. }
  723. else{//Non è un contenuto per questo foreach
  724. if($this->isRelativeForeachPlaceholder($currentContentName,$foreachName)){
  725. //E' un placeholder di un foreach figlio o discendente
  726. if($this->isDescendantForeachPlaceholder($currentContentName,$foreachName)){
  727. //Invoco ricorsivamente la funzione per il foreach figlio da cui discende l'elemento
  728. $child = $this->getChildFromDescendantPlaceholder($currentContentName,$foreachName);
  729. $foreachCode = $this->setForeachBindedCode($child,$foreachCode,$this->bindForeach($child,$this->getForeachCode($child),$this->getForeachPlaceholders($child),$this->getForeachChilds($child),$content));
  730. if($content[0]){
  731. $currentContentName=reset($content[0]);
  732. $currentContentValue=reset($content[1]);
  733. }
  734. }
  735. else if($this->isBrotherForeachPlaceholder($currentContentName,$foreachName)){
  736. return $foreachCode;
  737. }
  738. else{
  739. return $foreachCode;
  740. }
  741. }
  742. else{
  743. array_shift($content[0]);
  744. array_shift($content[1]);
  745. if($content[0]){
  746. $currentContentName=reset($content[0]);
  747. $currentContentValue=reset($content[1]);
  748. }
  749. }
  750. }
  751. }
  752. else{//Non è un contenuto da iterare, vado avanti
  753. array_shift($content[0]);
  754. array_shift($content[1]);
  755. if($content[0]){
  756. $currentContentName=reset($content[0]);
  757. $currentContentValue=reset($content[1]);
  758. }
  759. }
  760. }while($content[0]);
  761. return $foreachCode;
  762. }
  763. else{// Il foreach non ha annidamenti
  764. $currentContentName=reset($content[0]);
  765. $currentContentValue=reset($content[1]);
  766. do{
  767. if($this->isForeachPlaceholder($currentContentName)){//E' un contenuto da iterare
  768. if($this->isTherePlaceholder($currentContentName,$foreachName)){//E' un contenuto per questo foreach
  769. $foreachCode = $this->setPlaceholderValue($currentContentName,$currentContentValue,$foreachName,$foreachCode);//inserisco
  770. array_shift($content[0]);
  771. array_shift($content[1]);
  772. $currentContentName=reset($content[0]);
  773. $currentContentValue=reset($content[1]);
  774. }
  775. else{//Non è un contenuto per questo foreach
  776. if($this->isRelativeForeachPlaceholder($currentContentName,$foreachName)){
  777. if($this->isBrotherForeachPlaceholder($currentContentName,$foreachName)){
  778. return $foreachCode;
  779. }
  780. else{
  781. return $foreachCode;
  782. }
  783. }
  784. else{//Se questo contenuto non appartiene a questa iterazione vado avanti
  785. array_shift($content[0]);
  786. array_shift($content[1]);
  787. if($content[0]){
  788. $currentContentName=reset($content[0]);
  789. $currentContentValue=reset($content[1]);
  790. }
  791. }
  792. }
  793. }
  794. else{//Non è un contenuto da iterare, vado avanti
  795. array_shift($content[0]);
  796. array_shift($content[1]);
  797. if($content[0]){
  798. $currentContentName=reset($content[0]);
  799. $currentContentValue=reset($content[1]);
  800. }
  801. }
  802. }while($content[0]);
  803. return $foreachCode;
  804. }
  805. }
  806. return $foreachCode;
  807. }
  808. function getForeachDeep($foreachName){
  809. $result = preg_match("~foreach(\d+)_(\d+)~Us",$foreachName,$infoPlaceholderName);
  810. if($result){
  811. return $infoPlaceholderName[1];
  812. }
  813. return NULL;
  814. }
  815. function getForeachID($foreachName){
  816. $result = preg_match("~foreach(\d+)_(\d+)~Us",$foreachName,$infoPlaceholderName);
  817. if($result){
  818. return $infoPlaceholderName[2];
  819. }
  820. return NULL;
  821. }
  822. function getForeachChilds($foreachName){
  823. if(!$this->foreachHierarchyArray) return NULL;
  824. foreach(array_keys($this->foreachHierarchyArray) as $foreachParent){
  825. if($foreachName==$foreachParent) return $this->foreachHierarchyArray[$foreachName];
  826. }
  827. return NULL;
  828. }
  829. function getForeachPlaceholders($foreachName){
  830. return $this->placeholderArray[$foreachName];
  831. }
  832. function getForeachCode($foreachName){
  833. if($foreachName){
  834. return $this->foreachCodeArray[$foreachName];
  835. }
  836. else return NULL;
  837. }
  838. function getForeachName($placeholderName){
  839. if (is_array($this->placeholderArray)) {
  840. foreach($this->placeholderArray as $currentForeachName=>$currentPlaceholderSet){//Cerco il foreach corrispondente a questo Placeholder
  841. foreach($currentPlaceholderSet as $currentPlaceholder){
  842. $result = preg_match("~(.+)::(.+)~Us",$placeholderName,$token);
  843. $result2 = preg_match("~(.+)::(.+)~Us",$currentPlaceholder,$token2);
  844. if($result && $result2){
  845. if($token[1]==$token2[1]){
  846. return $currentForeachName;
  847. }
  848. }
  849. else if($result2){
  850. if($token2[1]==$placeholderName){
  851. return $currentForeachName;
  852. }
  853. }
  854. else if($result){
  855. if($token[1]==$currentPlaceholder){
  856. return $currentForeachName;
  857. }
  858. }
  859. else{
  860. if($currentPlaceholder==$placeholderName){
  861. return $currentForeachName;
  862. }
  863. }
  864. }
  865. }
  866. }
  867. return NULL;
  868. }
  869. function getChildFromDescendantPlaceholder($placeholderName,$foreachName){//Ritorna il nome del foreach figlio da cui discende l'elemento se esiste
  870. $check = FALSE;
  871. $foreachChilds = $this->getForeachChilds($foreachName);
  872. if($foreachChilds){// il foreach corrente NON è una foglia
  873. foreach($foreachChilds as $child){ //Se il prossimo foreach è un mio discendente
  874. foreach($this->getForeachPlaceholders($child) as $currentPlaceholder){
  875. if($placeholderName==$currentPlaceholder) return $child;
  876. }
  877. $check = $this->isDescendantForeachPlaceholder($placeholderName,$child);
  878. if($check) return $child;
  879. }
  880. return NULL;
  881. }
  882. else{//il foreach corrente è una foglia
  883. return NULL;
  884. }
  885. }
  886. function getRootForeach($foreachName){
  887. $deep = $this->getForeachDeep($foreachName);
  888. $ID = $this->getForeachID($foreachName);
  889. if($deep==0) return $foreachName;
  890. foreach($this->foreachCodeArray as $currentForeachName=>$currentForeachValue){
  891. if($this->getForeachDeep($currentForeachName)==0 && $this->getForeachID($currentForeachName)<$ID){
  892. $foreachRootArray[$this->getForeachID($currentForeachName)]=$currentForeachName;
  893. }
  894. }
  895. $rootForeach = $foreachRootArray[max(array_keys($foreachRootArray))];
  896. return $rootForeach;
  897. }
  898. function isForeachPlaceholder($placeholderName){
  899. foreach($this->placeholderArray as $currentForeachName=>$currentPlaceholderSet){//Cerco il foreach corrispondente a questo Placeholder
  900. foreach($currentPlaceholderSet as $currentPlaceholder){
  901. if ($placeholderName==$currentPlaceholder){
  902. return TRUE;
  903. }
  904. }
  905. }
  906. return FALSE;
  907. }
  908. function isTherePlaceholder($placeholderName,$foreachName){
  909. $foreachPlaceholders = $this->getForeachPlaceholders($foreachName);
  910. foreach($foreachPlaceholders as $currentPlaceholder){
  911. if($placeholderName==$currentPlaceholder){
  912. return TRUE;
  913. }
  914. }
  915. return FALSE;
  916. }
  917. function isRelativeForeachPlaceholder($placeholderName,$foreachName){
  918. $rootForeach = $this->getRootForeach($foreachName);
  919. foreach($this->getForeachPlaceholders($rootForeach) as $rootPlaceholderName){
  920. if($rootPlaceholderName==$placeholderName){
  921. return TRUE;
  922. }
  923. }
  924. if($this->isDescendantForeachPlaceholder($placeholderName,$rootForeach)){
  925. return TRUE;
  926. }
  927. else{
  928. return FALSE;
  929. }
  930. }
  931. function isDescendantForeachPlaceholder($placeholderName,$foreachName){//Controlla se esiste un discendente che contiene questo placeholder
  932. $check = FALSE;
  933. $foreachChilds = $this->getForeachChilds($foreachName);
  934. if($foreachChilds){// il foreach corrente NON è una foglia
  935. foreach($foreachChilds as $child){ //Se il prossimo foreach è un mio discendente
  936. foreach($this->getForeachPlaceholders($child) as $currentPlaceholder){
  937. if($placeholderName==$currentPlaceholder) return TRUE;
  938. }
  939. $check = $this->isDescendantForeachPlaceholder($placeholderName,$child);
  940. if($check) return TRUE;
  941. }
  942. return FALSE;
  943. }
  944. else{//il foreach corrente è una foglia
  945. return FALSE;
  946. }
  947. }
  948. function isBrotherForeachPlaceholder($placeholderName,$foreachName){
  949. $checkForeachName = $this->getForeachName($placeholderName);
  950. //se il prossimo foreach ha lo stesso genitore allora è al mio stesso livello
  951. foreach($this->foreachHierarchyArray as $currentForeachName=>$currentForeachChilds){
  952. foreach($currentForeachChilds as $child){
  953. if($child==$foreachName){
  954. $foreachParent = $currentForeachName;
  955. break;
  956. }
  957. }
  958. }
  959. foreach($this->getForeachChilds($foreachParent) as $child){
  960. if($child==$checkForeachName) return TRUE;
  961. }
  962. return FALSE;
  963. }
  964. function setPlaceholderValue($placeholderName,$placeholderValue,$foreachName,$foreachCode){
  965. $result = preg_match("~<\[$placeholderName\]>~Us",$foreachCode);
  966. if($result){//Se c'è il placeholder lo istanzio
  967. $foreachCode = preg_replace("~<\[$placeholderName\]>~Us",$placeholderValue,$foreachCode,-1);
  968. }
  969. else{// Se non c'è controllo se si trova nel codice del foreach pulito, nel caso in cui lo appendo
  970. $foreachCode=preg_replace("~<\[(foreach\d+_\d+)\]>.+<\[\/\\1\]>~Us","",$foreachCode,-1);//Elimino i vecchi foreach annidati, sono in una nuova iterazione
  971. $result = preg_match("~<\[$placeholderName\]>~Us",$this->getForeachCode($foreachName));
  972. if($result){
  973. $foreachCode = $foreachCode.(preg_replace("~<\[$placeholderName\]>~Us",$placeholderValue,$this->getForeachCode($foreachName),-1));
  974. }
  975. }
  976. return $foreachCode;
  977. }
  978. function setSimplePlaceholderValue($placeholderName,$placeholderValue,$buffer){
  979. $result = preg_match("~<\[$placeholderName\]>~Us",$buffer);
  980. if($result){
  981. $buffer = preg_replace("~<\[$placeholderName\]>~Us","".$placeholderValue,$buffer,-1);
  982. }
  983. return $buffer;
  984. }
  985. function setForeachBindedCode($foreachName,$buffer,$code){
  986. $result = preg_match("~<\[$foreachName\]>(.+)<\[\/$foreachName\]>~Us",$buffer,$token);
  987. if($result){
  988. $buffer = preg_replace("~<\[$foreachName\]>.+<\[\/$foreachName\]>~Us",$code,$buffer,1);
  989. }
  990. return $buffer;
  991. }
  992. function setForeachPlaceholder($foreachName,$placeholder){
  993. $this->placeholderArray[$foreachName][] = $placeholder;
  994. }
  995. }
  996. Class Skin extends Template {
  997. var
  998. $name,
  999. $templates,
  1000. $placeholders;
  1001. function Skin($skin = "") {
  1002. if ($skin == "") {
  1003. $skin = $GLOBALS['config']['skin'];
  1004. } else {
  1005. $GLOBALS['config']['skin'] = $skin;
  1006. }
  1007. $GLOBALS["current_skin"] = $skin;
  1008. $this->name = $skin;
  1009. if (class_exists("Auth")) {
  1010. Template::Template("skins/{$skin}/dtml/frame-private.html");
  1011. } else {
  1012. Template::Template("skins/{$skin}/dtml/frame-public.html");
  1013. }
  1014. $this->setContent("skin", $skin);
  1015. $this->setContent("base", $GLOBALS['config']['base']);
  1016. $this->setContent("server", $_SERVER['SERVER_NAME']);
  1017. }
  1018. function addSkin($placeholder, $template) {
  1019. $this->templates[$template] =& new Template("skins/{$this->name}/dtml/{$template}.html");
  1020. $this->placeholders[$template] = $placeholder;
  1021. }
  1022. function setSkinContent($template, $name, $value) {
  1023. #echo get_class($this->templates[$template])
  1024. $this->templates[$template]->setContent($name, $value);
  1025. }
  1026. function close() {
  1027. if (is_array($this->templates)) {
  1028. foreach($this->templates as $name => $template) {
  1029. $this->setContent($this->placeholders[$name], $template->get());
  1030. }
  1031. }
  1032. Template::close();
  1033. }
  1034. }
  1035. Class Skinlet extends Template {
  1036. function Skinlet($template) {
  1037. if (!strpos($template, ".")) {
  1038. Template::Template("skins/{$GLOBALS['current_skin']}/dtml/{$template}.html");
  1039. } else {
  1040. Template::Template("skins/{$GLOBALS['current_skin']}/dtml/{$template}");
  1041. }
  1042. }
  1043. }
  1044. ?>