PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/theme.parser.class.php

http://litepublisher.googlecode.com/
PHP | 1318 lines | 1064 code | 218 blank | 36 comment | 163 complexity | db4ca0ba67bc4811e22583f46b7b374b MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0
  1. <?php
  2. /**
  3. * Lite Publisher
  4. * Copyright (C) 2010 - 2013 Vladimir Yushko http://litepublisher.ru/ http://litepublisher.com/
  5. * Dual licensed under the MIT (mit.txt)
  6. * and GPL (gpl.txt) licenses.
  7. **/
  8. class tthemeparser extends tevents {
  9. public $theme;
  10. public $paths;
  11. public $extrapaths;
  12. public $fixsubcount;
  13. private $abouts;
  14. private $sidebar_index;
  15. private $pathmap;
  16. private $parsedtags;
  17. public static function i() {
  18. return getinstance(__class__);
  19. }
  20. protected function create() {
  21. parent::create();
  22. $this->basename = 'themeparser';
  23. $this->addevents('ongetpaths', 'beforeparse', 'parsed', 'onfix');
  24. $this->addmap('extrapaths', array());
  25. $this->data['replacelang'] = false;
  26. $this->data['removephp'] = true;
  27. $this->data['stylebefore'] = true;
  28. $this->fixsubcount = true;
  29. $this->sidebar_index = 0;
  30. $this->pathmap = $this->createpathmap();
  31. }
  32. private function createpathmap() {
  33. $result = array();
  34. $post = 'content.post.filelist.';
  35. $excerpt = 'content.excerpts.excerpt.filelist.';
  36. foreach(array('file', 'image', 'audio', 'video') as $name) {
  37. $key = $post . $name;
  38. $result[$key . "s.$name"] = $key;
  39. $keyexcerpt = $excerpt . $name;
  40. $result[$keyexcerpt . "s.$name"] = $keyexcerpt;
  41. }
  42. $result[$post . 'images.preview'] = $post . 'preview';
  43. $result[$excerpt . 'images.preview'] = $excerpt . 'preview';
  44. return $result;
  45. }
  46. public static function checktheme(ttheme $theme) {
  47. if ($about = self::get_about_wordpress_theme($theme->name)) {
  48. $theme->type = 'wordpress';
  49. return true;
  50. }
  51. return false;
  52. }
  53. public function parse(ttheme $theme) {
  54. $theme->lock();
  55. $this->checkparent($theme->name);
  56. $about = $this->getabout($theme->name);
  57. switch ($about['type']) {
  58. case 'litepublisher3':
  59. case 'litepublisher':
  60. $theme->type = 'litepublisher';
  61. $ver3 = tthemeparserver3::i();
  62. $ver3->parse($theme);
  63. break;
  64. case 'litepublisher4':
  65. $theme->type = 'litepublisher';
  66. $this->parsetheme($theme);
  67. break;
  68. case 'wordpress':
  69. $theme->type = 'wordpress';
  70. break;
  71. }
  72. /*
  73. //save and restore lang section
  74. $lang = tlocal::i();
  75. $sect = $lang->section;
  76. */
  77. $this->parsed($theme);
  78. if ($this->replacelang) $this->doreplacelang($theme);
  79. //$lang->section = $sect;
  80. $theme->unlock();
  81. return true;
  82. }
  83. public function doreplacelang($theme) {
  84. $lang = tlocal::i('comment');
  85. foreach ($theme->templates as $name => $value) {
  86. if (is_string($value)) {
  87. $theme->templates[$name] = $theme->replacelang($value, $lang);
  88. }
  89. }
  90. foreach ($theme->templates['sidebars'] as &$sidebar) {
  91. unset($widget);
  92. foreach ($sidebar as &$widget) {
  93. $widget = $theme->replacelang($widget, $lang);
  94. }
  95. }
  96. }
  97. public function callback_replace_php($m) {
  98. return strtr($m[0], array(
  99. '$' => '&#36;',
  100. '?' => '&#63;',
  101. '(' => '&#40;',
  102. ')' => '&#41;',
  103. '[' => '&#91;',
  104. ']' => '&#93;',
  105. '{' => '&#123;',
  106. '}' => '&#125;'
  107. ));
  108. }
  109. public function callback_restore_php($m) {
  110. return strtr($m[0], array(
  111. '&#36;' => '$',
  112. '&#63;' => '?',
  113. '&#40;' => '(',
  114. '&#41;' => ')',
  115. '&#91;' => '[',
  116. '&#93;' => ']',
  117. '&#123;' => '{',
  118. '&#125;' => '}'
  119. ));
  120. }
  121. public function parsetheme(ttheme $theme) {
  122. $about = $this->getabout($theme->name);
  123. $filename = litepublisher::$paths->themes . $theme->name . DIRECTORY_SEPARATOR . $about['file'];
  124. if (!file_exists($filename)) return $this->error("The requested theme '$theme->name' file $filename not found");
  125. if ($theme->name != 'default') {
  126. $parentname = empty($about['parent']) ? 'default' : $about['parent'];
  127. $parent = ttheme::getinstance($parentname);
  128. $theme->templates = $parent->templates;
  129. $theme->parent = $parent->name;
  130. }
  131. $this->parsedtags = array();
  132. $s = $this->getfile($filename, $about);
  133. $this->parsetags($theme, $s);
  134. $this->afterparse($theme);
  135. }
  136. public function getfile($filename, $about) {
  137. $s = file_get_contents($filename);
  138. if ($s === false) return $this->error(sprintf('Error read "%s" file', $filename));
  139. //strip utf8 signature
  140. $utf = "\xEF\xBB\xBF";
  141. if (strbegin($s, $utf)) $s = substr($s, strlen($utf));
  142. $s = str_replace(array("\r\n", "\r", "\n\n"), "\n", $s);
  143. $s = preg_replace('/%%([a-zA-Z0-9]*+)_(\w\w*+)%%/', '\$$1.$2', $s);
  144. $s = strtr($s, array(
  145. '$options.url$url' => '$link',
  146. '$post.categorieslinks' => '$post.catlinks',
  147. '$post.tagslinks' => '$post.taglinks',
  148. '$post.subscriberss' => '$post.rsslink',
  149. '$post.excerptcategories' => '$post.excerptcatlinks',
  150. '$post.excerpttags' => '$post.excerpttaglinks',
  151. '$options' => '$site',
  152. '$template.sitebar' => '$template.sidebar',
  153. '<!--sitebar-->' => '<!--sidebar-->',
  154. '<!--/sitebar-->' => '<!--/sidebar-->'
  155. ));
  156. //replace $about.*
  157. if (preg_match_all('/\$about\.(\w\w*+)/', $s, $m, PREG_SET_ORDER)) {
  158. $a = array();
  159. foreach ($m as $item) {
  160. $name = $item[1];
  161. if (isset($about[$name])) {
  162. $a[$item[0]] = $about[$name];
  163. //$s = str_replace($item[0], $about[$name], $s);
  164. }
  165. }
  166. $s = strtr($s, $a);
  167. }
  168. return trim($s);
  169. }
  170. public function getabout($name) {
  171. if (!isset($this->abouts)) $this->abouts = array();
  172. if (!isset($this->abouts[$name])) {
  173. $filename = litepublisher::$paths->themes . $name . DIRECTORY_SEPARATOR . 'about.ini';
  174. if (file_exists($filename) && ( $about = parse_ini_file($filename, true))) {
  175. if (empty($about['about']['type'])) $about['about']['type'] = 'litepublisher3';
  176. //join languages
  177. if (isset($about[litepublisher::$options->language])) {
  178. $about['about'] = $about[litepublisher::$options->language] + $about['about'];
  179. }
  180. $this->abouts[$name] = $about['about'];
  181. /*
  182. } elseif ($about = twordpressthemeparser::get_about_wordpress_theme($name)){
  183. $about['type'] = 'wordpress';
  184. $this->abouts[$name] = $about;
  185. */
  186. } else {
  187. $this->abouts[$name] = false;
  188. }
  189. }
  190. return $this->abouts[$name];
  191. }
  192. public function checkparent($name) {
  193. $about = $this->getabout($name);
  194. $parents = array($name);
  195. while (!empty($about['parent'])) {
  196. $name = $about['parent'];
  197. if (in_array($name, $parents)) {
  198. $this->error(sprintf('Theme cicle "%s"', implode(', ', $parents)));
  199. }
  200. $parents[] = $name;
  201. $about = $this->getabout($name);
  202. }
  203. return true;
  204. }
  205. public function changetheme($old, $name) {
  206. $template = ttemplate::i();
  207. if ($about = $this->getabout($old)) {
  208. if (!empty($about['about']['pluginclassname'])) {
  209. $plugins = tplugins::i();
  210. $plugins->delete($old);
  211. }
  212. }
  213. $template->data['theme'] = $name;
  214. $template->path = litepublisher::$paths->themes . $name . DIRECTORY_SEPARATOR ;
  215. $template->url = litepublisher::$site->url . '/themes/'. $template->theme;
  216. $theme = ttheme::getinstance($name);
  217. $about = $this->getabout($name);
  218. if (!empty($about['about']['pluginclassname'])) {
  219. $plugins = tplugins::i();
  220. $plugins->addext($name, $about['about']['pluginclassname'], $about['about']['pluginfilename']);
  221. }
  222. litepublisher::$urlmap->clearcache();
  223. }
  224. public function reparse() {
  225. $theme = ttheme::i();
  226. $theme->lock();
  227. $this->parse($theme);
  228. ttheme::clearcache();
  229. $theme->unlock();
  230. }
  231. //4 ver
  232. public static function find_close($s, $a) {
  233. $brackets = array(
  234. '[' => ']',
  235. '{' => '}',
  236. '(' => ')'
  237. );
  238. $b = $brackets[$a];
  239. $i = strpos($s, $b);
  240. $sub = substr($s, 0, $i);
  241. if (substr_count($sub, $a) == 0) return $i;
  242. while (substr_count($sub, $a) > substr_count($sub, $b)) {
  243. $i = strpos($s, $b, $i + 1);
  244. if ($i === false) die(" The '$b' not found in\n$s");
  245. $sub = substr($s, 0, $i);
  246. }
  247. return $i;
  248. }
  249. public function parsetags(ttheme $theme, $s) {
  250. $this->theme = $theme;
  251. $this->paths = $this->getpaths($theme);
  252. $s = trim($s);
  253. $this->callevent('beforeparse', array($theme, &$s));
  254. if ($this->removephp) {
  255. $s = preg_replace('/\<\?.*?\?\>/ims', '', $s);
  256. } else {
  257. $s = preg_replace_callback('/\<\?(.*?)\?\>/ims', array($this, 'callback_replace_php'), $s);
  258. }
  259. while ($s != '') {
  260. if (preg_match('/^(((\$template|\$custom)?\.?)?\w*+(\.\w\w*+)*)\s*=\s*(\[|\{|\()?/i', $s, $m)) {
  261. $tag = $m[1];
  262. $s = ltrim(substr($s, strlen($m[0])));
  263. if (isset($m[5])) {
  264. $i = self::find_close($s, $m[5]);
  265. } else {
  266. $i = strpos($s, "\n");
  267. }
  268. $value = trim(substr($s, 0, $i));
  269. $s = ltrim(substr($s, $i));
  270. $this->settag($tag, $value);
  271. } else {
  272. if ($i = strpos($s, "\n")) {
  273. $s = ltrim(substr($s, $i));
  274. } else {
  275. $s = '';
  276. }
  277. }
  278. }
  279. }
  280. public function settag($parent, $s) {
  281. if (preg_match('/file\s*=\s*(\w[\w\._\-]*?\.\w\w*+\s*)/i', $s, $m) ||
  282. preg_match('/\@import\s*\(\s*(\w[\w\._\-]*?\.\w\w*+\s*)\)/i', $s, $m)) {
  283. $filename = litepublisher::$paths->themes . $this->theme->name . DIRECTORY_SEPARATOR . $m[1];
  284. if (!file_exists($filename)) $this->error("File '$filename' not found");
  285. $s = $this->getfile($filename, $this->getabout($this->theme->name));
  286. }
  287. if (strbegin($parent, '$template.')) $parent = substr($parent, strlen('$template.'));
  288. if (strbegin($parent, 'sidebar')) {
  289. if (preg_match('/^sidebar(\d)\.?/', $parent, $m)) {
  290. $this->sidebar_index = (int) $m[1];
  291. } else {
  292. $this->sidebar_index = 0;
  293. }
  294. if (!isset($this->theme->templates['sidebars'][$this->sidebar_index])) $this->theme->templates['sidebars'][$this->sidebar_index] = array();
  295. }
  296. if ($this->removephp) {
  297. //if (preg_match('/\<\?.*?\?\>/ims', $s, $m)) dumpvar($m);
  298. $s = preg_replace('/\<\?.*?\?\>/ims', '', $s);
  299. } else {
  300. $s = preg_replace_callback('/\<\?(.*?)\?\>/ims', array($this, 'callback_replace_php'), $s);
  301. }
  302. while (($s != '') && preg_match('/(\$\w*+(\.\w\w*+)?)\s*=\s*(\[|\{|\()?/i', $s, $m)) {
  303. if (!isset($m[3])) {
  304. $this->error('The bracket not found in ' . $s);
  305. }
  306. $tag = $m[1];
  307. $j = strpos($s, $m[0]);
  308. $pre = rtrim(substr($s, 0, $j));
  309. $s= ltrim(substr($s, $j + strlen($m[0])));
  310. $i = self::find_close($s, $m[3]);
  311. $value = trim(substr($s, 0, $i));
  312. $s = ltrim(substr($s, $i + 1));
  313. $info = $this->tagtopath($parent, $tag);
  314. $this->settag($info['path'], $value);
  315. $s = $pre . $info['replace'] . $s;
  316. }
  317. $s = trim($s);
  318. if (!$this->removephp) {
  319. $s = preg_replace_callback('/\<\&\#63;.*?\&\#63;\>/ims', array($this, 'callback_restore_php'), $s);
  320. }
  321. //retranslatepaths
  322. if (isset($this->pathmap[$parent])) $parent = $this->pathmap[$parent];
  323. //set value
  324. if (strbegin($parent, 'sidebar')) {
  325. $this->setwidgetvalue($parent, $s);
  326. } elseif (isset($this->paths[$parent])) {
  327. $this->set_value($parent, $s);
  328. } elseif (($parent == '') || ($parent == '$template')) {
  329. $this->theme->templates['index'] = $s;
  330. //dumpstr($s);
  331. } elseif (strbegin($parent, '$custom') || strbegin($parent, 'custom')) {
  332. $this->setcustom($parent, $s);
  333. } else {
  334. $this->error("The '$parent' tag not found. Content \n$s");
  335. }
  336. }
  337. public function set_value($name, $value) {
  338. $this->parsedtags[] = $name;
  339. switch ($name) {
  340. case 'content.menu':
  341. //fix old ver
  342. $this->theme->templates['content.author'] = str_replace('menu', 'author', $value);
  343. break;
  344. case 'menu.item':
  345. $this->theme->templates['menu.single'] = $value;
  346. $this->theme->templates['menu.current'] = $value;
  347. break;
  348. }
  349. $this->theme->templates[$name] = $value;
  350. }
  351. public function tagtopath($parent, $tag) {
  352. if (strbegin($tag, '$template.sidebar') && (substr_count($tag, '.') == 1)) {
  353. return array(
  354. 'path' => substr($tag, strlen('$template.')),
  355. 'tag' => $tag,
  356. 'replace' => $tag
  357. );
  358. }
  359. if (($parent == '') || ($tag == '$template')) return 'index';
  360. if (strbegin($parent, '$template.')) $parent = substr($parent, strlen('$template.'));
  361. if ($parent == '$template') $parent = '';
  362. foreach ($this->paths as $path => $info) {
  363. if (strbegin($path, $parent)) {
  364. if ($tag == $info['tag']) {
  365. $info['path'] = $path;
  366. return $info;
  367. }
  368. }
  369. }
  370. $name = substr($tag, 1);
  371. $path = $parent . '.' . $name;
  372. if (strbegin($parent, 'sidebar')) {
  373. return array(
  374. 'path' => $path,
  375. 'tag' => $tag,
  376. 'replace' => $tag == '$classes' ? '' : $tag
  377. );
  378. }
  379. if (strbegin($parent, '$custom') || strbegin($parent, 'custom')) {
  380. return array(
  381. 'path' => $path,
  382. 'tag' => $tag,
  383. 'replace' => ''
  384. );
  385. }
  386. $this->error("The '$tag' not found in path '$parent'");
  387. }
  388. private function setwidgetvalue($path, $value) {
  389. if (!strpos($path, '.')) return;
  390. if (!preg_match('/^sidebar(\d?)\.(\w\w*+)(\.\w\w*+)*$/', $path, $m)) $this->error("The '$path' is not a widget path");
  391. $widgetname = $m[2];
  392. if (($widgetname != 'widget') && (!in_array($widgetname, ttheme::getwidgetnames()))) $this->error("Unknown widget '$widgetname' name");
  393. $path = ttheme::getwidgetpath(empty($m[3]) ? '' : $m[3]);
  394. if ($path === false) $this->error("Unknown '$path' widget path");
  395. $this->setwidgetitem($widgetname, $path, $value);
  396. if ($widgetname == 'widget') {
  397. foreach (ttheme::getwidgetnames() as $widgetname) {
  398. if ((($widgetname == 'posts') || ($widgetname == 'comments')) &&
  399. ($path =='.item')) continue;
  400. $this->setwidgetitem($widgetname, $path, $value);
  401. }
  402. }
  403. }
  404. private function setwidgetitem($widgetname, $path, $value) {
  405. //echo "$widgetname, $path<br>";
  406. $sidebar = &$this->theme->templates['sidebars'][$this->sidebar_index];
  407. if (!isset($sidebar[$widgetname])) {
  408. foreach ( array('', '.items', '.item', '.subcount', '.subitems') as $name) {
  409. $sidebar[$widgetname . $name] = isset($sidebar['widget' . $name]) ? $sidebar['widget' . $name] : '';
  410. }
  411. if ($widgetname == 'meta') $sidebar['meta.classes'] = '';
  412. }
  413. $sidebar[$widgetname . $path] = $value;
  414. }
  415. public function setcustom($path, $value) {
  416. $names = explode('.', $path);
  417. if (count($names) < 2) return;
  418. if (($names[0] != '$custom') && ($names[0] != 'custom')) $this->error("The '$path' path is not a custom path");
  419. $name = $names[1];
  420. switch (count($names)) {
  421. case 2:
  422. $this->theme->templates['custom'][$name] = $value;
  423. return;
  424. case 3:
  425. return;
  426. case 4:
  427. $tag = $names[3];
  428. $admin = &$this->theme->templates['customadmin'];
  429. if (!isset($admin[$name])) $admin[$name] = array();
  430. if ($tag == 'values') {
  431. $value = explode(',', $value);
  432. foreach ($value as $i => $v) $value[$i] = trim($v);
  433. }
  434. $admin[$name][$tag] = $value;
  435. return;
  436. }
  437. }
  438. public function afterparse($theme) {
  439. $this->onfix($theme);
  440. $templates = &$this->theme->templates;
  441. $templates['menu.hover'] = isset($templates['menu.hover']) ? ($templates['menu.hover'] == 'true' ? 'true' :
  442. ($templates['menu.hover'] == 'bootstrap' ? 'bootstrap' : 'false')) : 'true';
  443. if (!isset($templates['content.post.templatecomments'])) $templates['content.post.templatecomments'] = '';
  444. if (!isset($templates['content.post.templatecomments.confirmform'])) $this->error('tml not');
  445. $post = 'content.post.';
  446. $excerpt = 'content.excerpts.excerpt.';
  447. //normalize filelist
  448. foreach(array('file', 'image', 'audio', 'video') as $name) {
  449. $key = $post . 'filelist.' . $name;
  450. $itemkey = $key . "s.$name";
  451. if (!isset($templates[$key . 's'])) $templates[$key . 's'] = '$' . $name;
  452. //excerpt
  453. $keyexcerpt = $excerpt . 'filelist.' . $name;
  454. $itemkeyexcerpt = $keyexcerpt . "s.$name";
  455. if (!isset($templates[$keyexcerpt])) $templates[$keyexcerpt] = $templates[$key];
  456. if (!isset($templates[$keyexcerpt . 's'])) $templates[$keyexcerpt . 's'] = $templates[$key . 's'];
  457. }
  458. //fix preview
  459. $key = $post . 'filelist.preview';
  460. $keyexcerpt = $excerpt . 'filelist.preview';
  461. if ( !isset($templates[$keyexcerpt])) $templates[$keyexcerpt] = $templates[$key];
  462. foreach (array('date',
  463. 'filelist', 'filelist.file', 'filelist.image', 'filelist.preview', 'filelist.audio', 'filelist.video',
  464. 'filelist.files', 'filelist.images', 'filelist.audios', 'filelist.videos',
  465. 'catlinks', 'catlinks.item', 'catlinks.divider',
  466. 'taglinks', 'taglinks.item', 'taglinks.divider') as $name) {
  467. if (empty($templates[$excerpt . $name])) {
  468. $templates[$excerpt . $name] = $templates[$post . $name];
  469. }
  470. }
  471. $sidebars = &$this->theme->templates['sidebars'];
  472. $count = substr_count($this->theme->templates['index'], '$template.sidebar');
  473. if (count($sidebars) > $count) array_splice($sidebars, $count , count($sidebars) - $count);
  474. for ($i = 0; $i < $count; $i++) {
  475. $sidebar = &$this->theme->templates['sidebars'][$i];
  476. foreach (ttheme::getwidgetnames() as $widgetname) {
  477. foreach (array('', '.items', '.item', '.subcount', '.subitems') as $name) {
  478. if (empty($sidebar[$widgetname . $name])) $sidebar[$widgetname . $name] = $sidebar['widget' . $name];
  479. }
  480. if ($this->fixsubcount && in_array($widgetname, array('widget', 'categories', 'tags', 'archives'))) {
  481. $v = $sidebar[$widgetname . '.item'];
  482. if (!strpos($v, '$subcount')) $sidebar[$widgetname . '.item'] = str_replace('$subitems', '$subcount$subitems', $v);
  483. }
  484. }
  485. if (is_string($sidebar['meta.classes'])) {
  486. $sidebar['meta.classes'] = self::getmetaclasses($sidebar['meta.classes']);
  487. }
  488. }
  489. //add spaces
  490. foreach (array(
  491. 'content.excerpts.excerpt.taglinks.divider',
  492. 'content.post.taglinks.divider',
  493. 'content.excerpts.excerpt.catlinks.divider',
  494. 'content.post.catlinks.divider'
  495. ) as $k) {
  496. if (substr($templates[$k], -1) != ' ') $templates[$k] .= ' ';
  497. }
  498. $templates['content.post.templatecomments.confirmform'] = str_replace('$lang.formhead', '$lang.checkspam', $templates['content.post.templatecomments.confirmform']);
  499. $form = 'content.post.templatecomments.form';
  500. $templates[$form] = trim(str_replace(
  501. '<script type="text/javascript" src="$site.files$template.jsmerger_comments"></script>', '',
  502. $templates[$form]));
  503. if (!strpos($templates[$form], '$mesg')) $templates[$form] = '<div id="before-commentform">$mesg</div>' . $templates[$form];
  504. $regform = 'content.post.templatecomments.regform';
  505. if (!in_array($regform, $this->parsedtags) && in_array('content.admin.editor', $this->parsedtags)) {
  506. $editor = strtr($templates['content.admin.editor'], array(
  507. '$lang.$name' => $this->replacelang ? tlocal::i('comment')->content : '$lang.content',
  508. '$name' => 'content',
  509. '$value' => ''
  510. ));
  511. $templates[$regform] =
  512. ' <div id="before-commentform">$mesg</div>
  513. <h4 id="respond">$lang.leavereply</h4>
  514. <form action="$site.url/send-comment.php" method="post" id="commentform">'
  515. . $editor .
  516. '<p>
  517. <input type="hidden" name="postid" value="$postid" />
  518. <input type="hidden" name="antispam" value="$antispam" />
  519. <input type="submit" name="submitbutton" id="submitcomment" value="'
  520. . ($this->replacelang ? tlocal::i()->send : '$lang.send' ) .
  521. '" /></p>
  522. </form>';
  523. }
  524. $comment = 'content.post.templatecomments.comments.comment';
  525. $templates[$comment] = str_replace('$moderate',
  526. '<div class="moderationbuttons" data-idcomment="$comment.id" data-idauthor="$comment.author"></div>',
  527. $templates[$comment]);
  528. if ($this->stylebefore) {
  529. foreach (array('index', 'index.home', 'index.post', 'index.tag') as $k) {
  530. if (strpos($templates[$k], '$template.cssmerger_default')) continue;
  531. //insert css merger before theme css
  532. if ($i = strpos($templates[$k], '.css')) {
  533. $i = strrpos(substr($templates[$k], 0, $i), '<');
  534. $css = '<link type="text/css" href="$site.files$template.cssmerger_default" rel="stylesheet" />';
  535. $templates[$k] = substr_replace($templates[$k], $css, $i - 1, 0);
  536. }
  537. }
  538. //fix $template.head
  539. // ignore on installling (class not exists)
  540. if (!defined('litepublisher_mode')|| (litepublisher_mode != 'install')) {
  541. $t = ttemplate::i();
  542. if ((false !== strpos($t->heads, $css)) && (false === strpos($t->heads, "<!--$css-->"))) {
  543. $t->heads = str_replace($css, "<!--$css-->", $t->heads);
  544. $t->save();
  545. }
  546. }
  547. }
  548. //reuse templates
  549. foreach ($templates as $k => $v) {
  550. if (is_string($v) && !strbegin($v, '<') && isset($templates[$v]) && is_string($templates[$v])) {
  551. $templates[$k] = $templates[$v];
  552. }
  553. }
  554. }//method
  555. public static function getmetaclasses($s) {
  556. $result = array('rss' => '', 'comments' => '', 'media' => '', 'foaf' => '', 'profile' => '', 'sitemap' => '');
  557. foreach (explode(',', $s) as $class) {
  558. if ($i = strpos($class, '=')) {
  559. $classname = trim(substr($class, 0, $i));
  560. $value = trim(substr($class, $i + 1));
  561. if ($value != '') $result[$classname] = sprintf('class="%s"', $value);
  562. }
  563. }
  564. return $result;
  565. }
  566. public static function compress(ttheme $theme, $dir = '') {
  567. if ($theme->name == 'default') return false;
  568. $result = '';
  569. if ($dir == '') $dir = litepublisher::$paths->themes . $theme->name . DIRECTORY_SEPARATOR;
  570. $parent = ttheme::getinstance($theme->parent == '' ? 'default' : $theme->parent);
  571. if ($theme->templates['index'] != $parent->templates['index']) {
  572. if (file_put_contents($dir . 'index.tml', $theme->templates['index']) === false) return false;
  573. $result .= '$template = {@import(index.tml)}';
  574. $result .= "\n\n";
  575. }
  576. foreach ($theme->templates as $name => $value) {
  577. if ($name == 'index') continue;
  578. if (is_array($value)) continue;
  579. $value = trim($value);
  580. if ($value == trim($parent->templates[$name])) continue;
  581. if (strend($name, '.date')) {
  582. if (($value == '') || ($value == litepublisher::$options->dateformat) || ($value == tlocal::get('datetime', 'dateformat'))) continue;
  583. }
  584. $result .= "\$template.$name = [$value]\n\n";
  585. }
  586. for ($i =0; $i < count($theme->templates['sidebars']); $i++ ) {
  587. $sidebar = &$theme->templates['sidebars'][$i];
  588. $parentsidebar =&$parent->templates['sidebars'][$i];
  589. foreach ($sidebar as $name => $value) {
  590. if (is_string($value)) {
  591. $value = trim($value);
  592. if ($value == trim($parentsidebar[$name])) continue;
  593. } else {
  594. if (count(array_diff_assoc($value, $parentsidebar[$name])) == 0) continue;
  595. $a = array_map(create_function('$k, $v', 'return "$k=$v";'),
  596. array_keys($value), array_values($value));
  597. $value = implode(',', $a);
  598. }
  599. $result .= $i == 0 ? 'sidebar.' : "sidebar$i.";
  600. $result .= "$name = [$value]\n\n";
  601. }
  602. }
  603. return file_put_contents($dir . 'theme.txt', $result);
  604. }
  605. public function getpaths() {
  606. $extra = $this->extrapaths;
  607. $this->callevent('ongetpaths', array(&$extra));
  608. return $extra + array(
  609. 'index' => array(
  610. 'tag' => '',
  611. 'replace' => ''
  612. ),
  613. 'index.home' => array(
  614. 'tag' => '$template.index.home',
  615. 'replace' => ''
  616. ),
  617. 'index.post' => array(
  618. 'tag' => '$template.index.post',
  619. 'replace' => ''
  620. ),
  621. 'index.tag' => array(
  622. 'tag' => '$template.index.tag',
  623. 'replace' => ''
  624. ),
  625. 'title' => array(
  626. 'tag' => '$template.title',
  627. 'replace' => '$template.title'
  628. ),
  629. 'menu' => array(
  630. 'tag' => '$template.menu',
  631. 'replace' => '$template.menu'
  632. ),
  633. 'menu.hover' => array(
  634. 'tag' => '$hover',
  635. 'replace' => ''
  636. ),
  637. 'menu.item' => array(
  638. 'tag' => '$item',
  639. 'replace' => '$item'
  640. ),
  641. 'menu.current' => array(
  642. 'tag' => '$current',
  643. 'replace' => ''
  644. ),
  645. 'menu.item.submenu' => array(
  646. 'tag' => '$submenu',
  647. 'replace' => '$submenu'
  648. ),
  649. 'menu.single' => array(
  650. 'tag' => '$single',
  651. 'replace' => ''
  652. ),
  653. 'head' => array(
  654. 'tag' => '$template.head',
  655. 'replace' => '$template.head'
  656. ),
  657. 'head.post' => array(
  658. 'tag' => '$post',
  659. 'replace' => ''
  660. ),
  661. 'head.post.prev' => array(
  662. 'tag' => '$prev',
  663. 'replace' => ''
  664. ),
  665. 'head.post.next' => array(
  666. 'tag' => '$next',
  667. 'replace' => ''
  668. ),
  669. 'head.post.rss' => array(
  670. 'tag' => '$rss',
  671. 'replace' => ''
  672. ),
  673. 'head.tags' => array(
  674. 'tag' => '$tags',
  675. 'replace' => ''
  676. ),
  677. 'content' => array(
  678. 'tag' => '$template.content',
  679. 'replace' => '$template.content'
  680. ),
  681. 'content.simple' => array(
  682. 'tag' => '$simple',
  683. 'replace' => ''
  684. ),
  685. 'content.notfound' => array(
  686. 'tag' => '$notfound',
  687. 'replace' => ''
  688. ),
  689. 'content.menu' => array(
  690. 'tag' => '$menu',
  691. 'replace' => ''
  692. ),
  693. 'content.author' => array(
  694. 'tag' => '$author',
  695. 'replace' => ''
  696. ),
  697. 'content.home' => array(
  698. 'tag' => '$home',
  699. 'replace' => ''
  700. ),
  701. 'content.home.midle' => array(
  702. 'tag' => '$midle',
  703. 'replace' => '$midle'
  704. ),
  705. 'content.home.midle.post' => array(
  706. 'tag' => '$post',
  707. 'replace' => '$post'
  708. ),
  709. 'content.post' => array(
  710. 'tag' => '$post',
  711. 'replace' => ''
  712. ),
  713. 'content.post.more' => array(
  714. 'tag' => '$post.more',
  715. 'replace' => ''
  716. ),
  717. 'content.post.rsslink' => array(
  718. 'tag' => '$post.rsslink',
  719. 'replace' => '$post.rsslink'
  720. ),
  721. 'content.post.date' => array(
  722. 'tag' => '$post.date',
  723. 'replace' => '$post.date'
  724. ),
  725. 'content.post.filelist' => array(
  726. 'tag' => '$post.filelist',
  727. 'replace' => '$post.filelist'
  728. ),
  729. 'content.post.filelist.files' => array(
  730. 'tag' => '$files',
  731. 'replace' => '$files'
  732. ),
  733. 'content.post.filelist.file' => array(
  734. 'tag' => '$file',
  735. 'replace' => '$files'
  736. ),
  737. 'content.post.filelist.files.file' => array(
  738. 'tag' => '$file',
  739. 'replace' => '$file'
  740. ),
  741. 'content.post.filelist.image' => array(
  742. 'tag' => '$image',
  743. 'replace' => ''
  744. ),
  745. 'content.post.filelist.images' => array(
  746. 'tag' => '$images',
  747. 'replace' => ''
  748. ),
  749. 'content.post.filelist.images.image' => array(
  750. 'tag' => '$image',
  751. 'replace' => '$image'
  752. ),
  753. 'content.post.filelist.preview' => array(
  754. 'tag' => '$preview',
  755. 'replace' => ''
  756. ),
  757. 'content.post.filelist.images.preview' => array(
  758. 'tag' => '$preview',
  759. 'replace' => ''
  760. ),
  761. 'content.post.filelist.audio' => array(
  762. 'tag' => '$audio',
  763. 'replace' => ''
  764. ),
  765. 'content.post.filelist.audios.audio' => array(
  766. 'tag' => '$audio',
  767. 'replace' => '$audio'
  768. ),
  769. 'content.post.filelist.audios' => array(
  770. 'tag' => '$audios',
  771. 'replace' => ''
  772. ),
  773. 'content.post.filelist.video' => array(
  774. 'tag' => '$video',
  775. 'replace' => ''
  776. ),
  777. 'content.post.filelist.videos' => array(
  778. 'tag' => '$videos',
  779. 'replace' => ''
  780. ),
  781. 'content.post.filelist.videos.video' => array(
  782. 'tag' => '$video',
  783. 'replace' => '$video'
  784. ),
  785. 'content.post.catlinks' => array(
  786. 'tag' => '$post.catlinks',
  787. 'replace' => '$post.catlinks'
  788. ),
  789. 'content.post.catlinks.item' => array(
  790. 'tag' => '$item',
  791. 'replace' => '$items'
  792. ),
  793. 'content.post.catlinks.divider' => array(
  794. 'tag' => '$divider',
  795. 'replace' => ''
  796. ),
  797. 'content.post.taglinks' => array(
  798. 'tag' => '$post.taglinks',
  799. 'replace' => '$post.taglinks'
  800. ),
  801. 'content.post.taglinks.item' => array(
  802. 'tag' => '$item',
  803. 'replace' => '$items'
  804. ),
  805. 'content.post.taglinks.divider' => array(
  806. 'tag' => '$divider',
  807. 'replace' => ''
  808. ),
  809. 'content.post.prevnext' => array(
  810. 'tag' => '$post.prevnext',
  811. 'replace' => '$post.prevnext'
  812. ),
  813. 'content.post.prevnext.prev' => array(
  814. 'tag' => '$prev',
  815. 'replace' => '$prev'
  816. ),
  817. 'content.post.prevnext.next' => array(
  818. 'tag' => '$next',
  819. 'replace' => '$next'
  820. ),
  821. 'content.post.templatecomments' => array(
  822. 'tag' => '$post.templatecomments',
  823. 'replace' => '$post.templatecomments'
  824. ),
  825. 'content.post.templatecomments.closed' => array(
  826. 'tag' => '$closed',
  827. 'replace' => ''
  828. ),
  829. 'content.post.templatecomments.form' => array(
  830. 'tag' => '$form',
  831. 'replace' => ''
  832. ),
  833. 'content.post.templatecomments.regform' => array(
  834. 'tag' => '$regform',
  835. 'replace' => ''
  836. ),
  837. 'content.post.templatecomments.confirmform' => array(
  838. 'tag' => '$confirmform',
  839. 'replace' => ''
  840. ),
  841. 'content.post.templatecomments.moderateform' => array(
  842. 'tag' => '$moderateform',
  843. 'replace' => ''
  844. ),
  845. 'content.post.templatecomments.holdcomments' => array(
  846. 'tag' => '$holdcomments',
  847. 'replace' => ''
  848. ),
  849. 'content.post.templatecomments.comments' => array(
  850. 'tag' => '$comments',
  851. 'replace' => ''
  852. ),
  853. 'content.post.templatecomments.comments.id' => array(
  854. 'tag' => '$id',
  855. 'replace' => ''
  856. ),
  857. 'content.post.templatecomments.comments.idhold' => array(
  858. 'tag' => '$idhold',
  859. 'replace' => ''
  860. ),
  861. 'content.post.templatecomments.comments.count' => array(
  862. 'tag' => '$count',
  863. 'replace' => ''
  864. ),
  865. 'content.post.templatecomments.comments.comment' => array(
  866. 'tag' => '$comment',
  867. 'replace' => '$comment'
  868. ),
  869. 'content.post.templatecomments.comments.comment.class1' => array(
  870. 'tag' => '$class1',
  871. 'replace' => ' $class'
  872. ),
  873. 'content.post.templatecomments.comments.comment.class2' => array(
  874. 'tag' => '$class2',
  875. 'replace' => ' '
  876. ),
  877. 'content.post.templatecomments.comments.comment.date' => array(
  878. 'tag' => '$comment.date',
  879. 'replace' => '$comment.date'
  880. ),
  881. 'content.post.templatecomments.comments.comment.moderate' => array(
  882. 'tag' => '$moderate',
  883. 'replace' => '$moderate'
  884. ),
  885. 'content.post.templatecomments.comments.comment.quotebuttons' => array(
  886. 'tag' => '$quotebuttons',
  887. 'replace' => '$quotebuttons'
  888. ),
  889. 'content.post.templatecomments.pingbacks' => array(
  890. 'tag' => '$pingbacks',
  891. 'replace' => ''
  892. ),
  893. 'content.post.templatecomments.pingbacks.pingback' => array(
  894. 'tag' => '$pingback',
  895. 'replace' => '$pingback'
  896. ),
  897. 'content.excerpts' => array(
  898. 'tag' => '$excerpts',
  899. 'replace' => ''
  900. ),
  901. 'content.excerpts.excerpt' => array(
  902. 'tag' => '$excerpt',
  903. 'replace' => '$excerpt'
  904. ),
  905. 'content.excerpts.excerpt.date' => array(
  906. 'tag' => '$post.excerptdate',
  907. 'replace' => '$post.excerptdate'
  908. ),
  909. 'content.excerpts.excerpt.morelink' => array(
  910. 'tag' => '$post.morelink',
  911. 'replace' => ''
  912. ),
  913. 'content.excerpts.excerpt.filelist' => array(
  914. 'tag' => '$post.excerptfilelist',
  915. 'replace' => '$post.excerptfilelist'
  916. ),
  917. 'content.excerpts.excerpt.filelist.files' => array(
  918. 'tag' => '$files',
  919. 'replace' => '$files'
  920. ),
  921. 'content.excerpts.excerpt.filelist.file' => array(
  922. 'tag' => '$file',
  923. 'replace' => '$files'
  924. ),
  925. 'content.excerpts.excerpt.filelist.files.file' => array(
  926. 'tag' => '$file',
  927. 'replace' => '$file'
  928. ),
  929. 'content.excerpts.excerpt.filelist.image' => array(
  930. 'tag' => '$image',
  931. 'replace' => ''
  932. ),
  933. 'content.excerpts.excerpt.filelist.images' => array(
  934. 'tag' => '$images',
  935. 'replace' => ''
  936. ),
  937. 'content.excerpts.excerpt.filelist.images.image' => array(
  938. 'tag' => '$image',
  939. 'replace' => '$image'
  940. ),
  941. 'content.excerpts.excerpt.filelist.preview' => array(
  942. 'tag' => '$preview',
  943. 'replace' => ''
  944. ),
  945. 'content.excerpts.excerpt.filelist.images.preview' => array(
  946. 'tag' => '$preview',
  947. 'replace' => ''
  948. ),
  949. 'content.excerpts.excerpt.filelist.audio' => array(
  950. 'tag' => '$audio',
  951. 'replace' => ''
  952. ),
  953. 'content.excerpts.excerpt.filelist.audios.audio' => array(
  954. 'tag' => '$audio',
  955. 'replace' => '$audio'
  956. ),
  957. 'content.excerpts.excerpt.filelist.audios' => array(
  958. 'tag' => '$audios',
  959. 'replace' => ''
  960. ),
  961. 'content.excerpts.excerpt.filelist.video' => array(
  962. 'tag' => '$video',
  963. 'replace' => ''
  964. ),
  965. 'content.excerpts.excerpt.filelist.videos' => array(
  966. 'tag' => '$videos',
  967. 'replace' => ''
  968. ),
  969. 'content.excerpts.excerpt.filelist.videos.video' => array(
  970. 'tag' => '$video',
  971. 'replace' => '$video'
  972. ),
  973. 'content.excerpts.excerpt.catlinks' => array(
  974. 'tag' => '$post.excerptcatlinks',
  975. 'replace' => '$post.excerptcatlinks'
  976. ),
  977. 'content.excerpts.excerpt.catlinks.item' => array(
  978. 'tag' => '$item',
  979. 'replace' => '$items'
  980. ),
  981. 'content.excerpts.excerpt.catlinks.divider' => array(
  982. 'tag' => '$divider',
  983. 'replace' => ''
  984. ),
  985. 'content.excerpts.excerpt.taglinks' => array(
  986. 'tag' => '$post.taglinks',
  987. 'replace' => '$post.taglinks'
  988. ),
  989. 'content.excerpts.excerpt.taglinks.item' => array(
  990. 'tag' => '$item',
  991. 'replace' => '$items'
  992. ),
  993. 'content.excerpts.excerpt.taglinks.divider' => array(
  994. 'tag' => '$divider',
  995. 'replace' => ''
  996. ),
  997. 'content.excerpts.lite' => array(
  998. 'tag' => '$lite',
  999. 'replace' => ''
  1000. ),
  1001. 'content.excerpts.lite.excerpt' => array(
  1002. 'tag' => '$excerpt',
  1003. 'replace' => '$excerpt'
  1004. ),
  1005. 'content.navi' => array(
  1006. 'tag' => '$navi',
  1007. 'replace' => ''
  1008. ),
  1009. 'content.navi.prev' => array(
  1010. 'tag' => '$prev',
  1011. 'replace' => '$items'
  1012. ),
  1013. 'content.navi.next' => array(
  1014. 'tag' => '$next',
  1015. 'replace' => ''
  1016. ),
  1017. 'content.navi.link' => array(
  1018. 'tag' => '$link',
  1019. 'replace' => ''
  1020. ),
  1021. 'content.navi.current' => array(
  1022. 'tag' => '$current',
  1023. 'replace' => ''
  1024. ),
  1025. 'content.navi.divider' => array(
  1026. 'tag' => '$divider',
  1027. 'replace' => ''
  1028. ),
  1029. 'content.admin' => array(
  1030. 'tag' => '$admin',
  1031. 'replace' => ''
  1032. ),
  1033. 'content.admin.editor' => array(
  1034. 'tag' => '$editor',
  1035. 'replace' => ''
  1036. ),
  1037. 'content.admin.text' => array(
  1038. 'tag' => '$text',
  1039. 'replace' => ''
  1040. ),
  1041. 'content.admin.password' => array(
  1042. 'tag' => '$password',
  1043. 'replace' => ''
  1044. ),
  1045. 'content.admin.upload' => array(
  1046. 'tag' => '$upload',
  1047. 'replace' => ''
  1048. ),
  1049. 'content.admin.combo' => array(
  1050. 'tag' => '$combo',
  1051. 'replace' => ''
  1052. ),
  1053. 'content.admin.checkbox' => array(
  1054. 'tag' => '$checkbox',
  1055. 'replace' => ''
  1056. ),
  1057. 'content.admin.radioitem' => array(
  1058. 'tag' => '$radioitem',
  1059. 'replace' => ''
  1060. ),
  1061. 'content.admin.radio' => array(
  1062. 'tag' => '$radio',
  1063. 'replace' => ''
  1064. ),
  1065. 'content.admin.hidden' => array(
  1066. 'tag' => '$hidden',
  1067. 'replace' => ''
  1068. ),
  1069. 'content.admin.button' => array(
  1070. 'tag' => '$button',
  1071. 'replace' => ''
  1072. ),
  1073. 'content.admin.submit' => array(
  1074. 'tag' => '$submit',
  1075. 'replace' => ''
  1076. ),
  1077. 'content.admin.form' => array(
  1078. 'tag' => '$form',
  1079. 'replace' => ''
  1080. ),
  1081. 'content.admin.tableclass' => array(
  1082. 'tag' => '$tableclass',
  1083. 'replace' => ''
  1084. ),
  1085. 'custom' => array(
  1086. 'tag' => '$custom',
  1087. 'replace' => ''
  1088. ),
  1089. 'ajaxwidget' => array(
  1090. 'tag' => '$template.ajaxwidget',
  1091. 'replace' => '',
  1092. ),
  1093. 'inlinewidget' => array(
  1094. 'tag' => '$template.inlinewidget',
  1095. 'replace' => ''
  1096. )
  1097. );
  1098. }
  1099. }//class