PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/kernel.posts.php

http://litepublisher.googlecode.com/
PHP | 2606 lines | 2144 code | 394 blank | 68 comment | 389 complexity | 1cbd169dc2bb26428a7e6fb741eb3273 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * Lite Publisher
  4. * Copyright (C) 2010, 2011, 2012, 2013 Vladimir Yushko http://litepublisher.com/
  5. * Dual licensed under the MIT (mit.txt)
  6. * and GPL (gpl.txt) licenses.
  7. **/
  8. //items.posts.class.php
  9. class titemsposts extends titems {
  10. public $tablepost;
  11. public $postprop;
  12. public $itemprop;
  13. public static function i() {
  14. return getinstance(__class__);
  15. }
  16. protected function create() {
  17. parent::create();
  18. $this->basename = 'itemsposts';
  19. $this->table = 'itemsposts';
  20. $this->tablepost = 'posts';
  21. $this->postprop = 'post';
  22. $this->itemprop = 'item';
  23. }
  24. public function add($idpost, $iditem) {
  25. $this->db->insert(array(
  26. $this->postprop => $idpost,
  27. $this->itemprop => $iditem
  28. ));
  29. $this->added();
  30. }
  31. public function exists($idpost, $iditem) {
  32. return $this->db->exists("$this->postprop = $idpost and $this->itemprop = $iditem");
  33. }
  34. public function remove($idpost, $iditem) {
  35. return $this->db->delete("$this->postprop = $idpost and $this->itemprop = $iditem");
  36. }
  37. public function delete($idpost) {
  38. return $this->deletepost($idpost);
  39. }
  40. public function deletepost($idpost) {
  41. $db = $this->db;
  42. $result = $db->res2id($db->query("select $this->itemprop from $this->thistable where $this->postprop = $idpost"));
  43. $db->delete("$this->post = $idpost");
  44. return $result;
  45. }
  46. public function deleteitem($iditem) {
  47. $this->db->delete("$this->itemprop = $iditem");
  48. $this->deleted();
  49. }
  50. public function setitems($idpost, array $items) {
  51. array_clean($items);
  52. $db = $this->db;
  53. $old = $this->getitems($idpost);
  54. $add = array_diff($items, $old);
  55. $delete = array_diff($old, $items);
  56. if (count($delete)) $db->delete("$this->postprop = $idpost and $this->itemprop in (" . implode(', ', $delete) . ')');
  57. if (count($add)) {
  58. $vals = array();
  59. foreach ($add as $iditem) {
  60. $vals[]= "($idpost, $iditem)";
  61. }
  62. $db->exec("INSERT INTO $this->thistable ($this->postprop, $this->itemprop) values " . implode(',', $vals) );
  63. }
  64. return array_merge($old, $add);
  65. }
  66. public function getitems($idpost) {
  67. return litepublisher::$db->res2id(litepublisher::$db->query("select $this->itemprop from $this->thistable where $this->postprop = $idpost"));
  68. }
  69. public function getposts($iditem) {
  70. return litepublisher::$db->res2id(litepublisher::$db->query("select $this->postprop from $this->thistable where $this->itemprop = $iditem"));
  71. }
  72. public function getpostscount($ititem) {
  73. $db = $this->getdb($this->tablepost);
  74. return $db->getcount("$db->prefix$this->tablepost.status = 'published' and id in (select $this->postprop from $this->thistable where $this->itemprop = $ititem)");
  75. }
  76. public function updateposts(array $list, $propname) {
  77. $db = $this->db;
  78. foreach ($list as $idpost) {
  79. $items = $this->getitems($idpost);
  80. $db->table = $this->tablepost;
  81. $db->setvalue($idpost, $propname, implode(', ', $items));
  82. }
  83. }
  84. }//class
  85. class titemspostsowner extends titemsposts {
  86. private $owner;
  87. public function __construct($owner) {
  88. if (!isset($owner)) return;
  89. parent::__construct();
  90. $this->owner = $owner;
  91. $this->table = $owner->table . 'items';
  92. }
  93. public function load() { }
  94. public function save() { $this->owner->save(); }
  95. public function lock() { $this->owner->lock(); }
  96. public function unlock() { $this->owner->unlock(); }
  97. }//class
  98. //post.class.php
  99. class tpost extends titem implements itemplate {
  100. public $childdata;
  101. public $childtable;
  102. public $factory;
  103. public $props;
  104. public $propdata;
  105. public $syncdata;
  106. private $aprev;
  107. private $anext;
  108. private $_meta;
  109. private $_theme;
  110. private $_onid;
  111. public static function i($id = 0) {
  112. $id = (int) $id;
  113. if ($id > 0) {
  114. if (isset(self::$instances['post'][$id])) return self::$instances['post'][$id];
  115. if ($result = self::loadpost($id)) {
  116. self::$instances['post'][$id] = $result;
  117. return $result;
  118. }
  119. return null;
  120. }
  121. return parent::iteminstance(__class__, $id);
  122. }
  123. public static function getinstancename() {
  124. return 'post';
  125. }
  126. public static function getchildtable() {
  127. return '';
  128. }
  129. public static function selectitems(array $items) {
  130. return array();
  131. }
  132. public static function select_child_items($table, array $items) {
  133. if (($table == '') || (count($items) == 0)) return array();
  134. $db = litepublisher::$db;
  135. $childtable = $db->prefix . $table;
  136. $list = implode(',', $items);
  137. return $db->res2items($db->query("select $childtable.*
  138. from $childtable where id in ($list)"));
  139. }
  140. public static function newpost($class) {
  141. if (empty($class)) $class = __class__;
  142. return new $class();
  143. }
  144. protected function create() {
  145. $this->table = 'posts';
  146. $this->props = array();
  147. $this->propdata = array();
  148. $this->syncdata = array();
  149. //last binding, like cache
  150. $this->childtable = call_user_func_array(array(get_class($this), 'getchildtable'), array());
  151. $this->data= array(
  152. 'id' => 0,
  153. 'idview' => 1,
  154. 'idurl' => 0,
  155. 'parent' => 0,
  156. 'author' => 0,
  157. 'revision' => 0,
  158. 'icon' => 0,
  159. 'idperm' => 0,
  160. 'class' => __class__,
  161. 'posted' => 0,
  162. 'modified' => 0,
  163. 'url' => '',
  164. 'title' => '',
  165. 'title2' => '',
  166. 'filtered' => '',
  167. 'excerpt' => '',
  168. 'rss' => '',
  169. 'rawcontent' => false,
  170. 'keywords' => '',
  171. 'description' => '',
  172. 'rawhead' => '',
  173. 'moretitle' => '',
  174. 'categories' => array(),
  175. 'tags' => array(),
  176. 'files' => array(),
  177. 'status' => 'published',
  178. 'comstatus' => litepublisher::$options->comstatus,
  179. 'pingenabled' => litepublisher::$options->pingenabled,
  180. 'password' => '',
  181. 'commentscount' => 0,
  182. 'pingbackscount' => 0,
  183. 'pagescount' => 0,
  184. 'pages' => array()
  185. );
  186. $this->data['childdata'] = &$this->childdata;
  187. $this->factory = litepublisher::$classes->getfactory($this);
  188. $posts = $this->factory->posts;
  189. foreach ($posts->itemcoclasses as $class) {
  190. $coinstance = litepublisher::$classes->newinstance($class);
  191. $coinstance->post = $this;
  192. $this->coinstances[] = $coinstance;
  193. }
  194. }
  195. public function __get($name) {
  196. if ($this->childtable) {
  197. if ($name == 'id') return $this->data['id'];
  198. if (method_exists($this, $get = 'get' . $name)) return $this->$get();
  199. if (array_key_exists($name, $this->childdata)) return $this->childdata[$name];
  200. }
  201. // tags and categories theme tag
  202. switch ($name) {
  203. case 'catlinks':
  204. return $this->get_taglinks('categories', false);
  205. case 'taglinks':
  206. return $this->get_taglinks('tags', false);
  207. case 'excerptcatlinks':
  208. return $this->get_taglinks('categories', true);
  209. case 'excerpttaglinks':
  210. return $this->get_taglinks('tags', true);
  211. }
  212. return parent::__get($name);
  213. }
  214. public function __set($name, $value) {
  215. /*
  216. foreach ($this->props as $props) {
  217. if ($props->set($this, $name, $value)) return true;
  218. }
  219. */
  220. if ($this->childtable) {
  221. if ($name == 'id') return $this->setid($value);
  222. if (method_exists($this, $set = 'set'. $name)) return $this->$set($value);
  223. if (array_key_exists($name, $this->childdata)) {
  224. $this->childdata[$name] = $value;
  225. return true;
  226. }
  227. }
  228. return parent::__set($name, $value);
  229. }
  230. public function __isset($name) {
  231. return parent::__isset($name) || ($this->childtable && array_key_exists($name, $this->childdata) );
  232. }
  233. //db
  234. public function afterdb() {
  235. //$this->childdata['reproduced'] = $this->childdata['reproduced'] == '1';
  236. }
  237. public function beforedb() {
  238. //if ($this->childdata['closed'] == '') $this->childdata['closed'] = sqldate();
  239. }
  240. public function load() {
  241. if ($result = $this->LoadFromDB()) {
  242. foreach ($this->coinstances as $coinstance) $coinstance->load();
  243. }
  244. return $result;
  245. }
  246. protected function LoadFromDB() {
  247. if ($a = self::getassoc($this->id)) {
  248. $this->setassoc($a);
  249. return true;
  250. }
  251. return false;
  252. }
  253. public static function loadpost($id) {
  254. if ($a = self::getassoc($id)) {
  255. $self = self::newpost($a['class']);
  256. $self->setassoc($a);
  257. return $self;
  258. }
  259. return false;
  260. }
  261. public static function getassoc($id) {
  262. $db = litepublisher::$db;
  263. return $db->selectassoc("select $db->posts.*, $db->urlmap.url as url from $db->posts, $db->urlmap
  264. where $db->posts.id = $id and $db->urlmap.id = $db->posts.idurl limit 1");
  265. }
  266. public function setassoc(array $a) {
  267. $trans = $this->factory->gettransform($this);
  268. $trans->setassoc($a);
  269. if ($this->childtable) {
  270. if ($a = $this->getdb($this->childtable)->getitem($this->id)) {
  271. $this->childdata = $a;
  272. $this->afterdb();
  273. }
  274. }
  275. }
  276. public function save() {
  277. if ($this->lockcount > 0) return;
  278. $this->SaveToDB();
  279. foreach ($this->coinstances as $coinstance) $coinstance->save();
  280. }
  281. protected function SaveToDB() {
  282. $this->factory->gettransform($this)->save($this);
  283. if ($this->childtable) {
  284. $this->beforedb();
  285. $this->childdata['id'] = $this->id;
  286. $this->getdb($this->childtable)->updateassoc($this->childdata);
  287. }
  288. }
  289. public function addtodb() {
  290. $id = $this->factory->add($this);
  291. $this->setid($id);
  292. if ($this->childtable) {
  293. $this->beforedb();
  294. $this->childdata['id'] = $id;
  295. $this->getdb($this->childtable)->insert($this->childdata);
  296. }
  297. return $id;
  298. }
  299. public function onid() {
  300. if (isset($this->_onid) && count($this->_onid) > 0) {
  301. foreach ($this->_onid as $call) {
  302. try {
  303. call_user_func ($call, $this);
  304. } catch (Exception $e) {
  305. litepublisher::$options->handexception($e);
  306. }
  307. }
  308. unset($this->_onid);
  309. }
  310. if (isset($this->_meta)) {
  311. $this->_meta->id = $this->id;
  312. $this->_meta->save();
  313. }
  314. }
  315. public function setonid($call) {
  316. if (!is_callable($call)) $this->error('Event onid not callable');
  317. if (isset($this->_onid)) {
  318. $this->_onid[] = $call;
  319. } else {
  320. $this->_onid = array($call);
  321. }
  322. }
  323. public function free() {
  324. foreach ($this->coinstances as $coinstance) $coinstance->free();
  325. unset($this->aprev, $this->anext, $this->_meta, $this->_theme, $this->_onid);
  326. parent::free();
  327. }
  328. public function getcomments() {
  329. return $this->factory->getcomments($this->id);
  330. }
  331. public function getpingbacks() {
  332. return $this->factory->getpingbacks($this->id);
  333. }
  334. public function getprev() {
  335. if (!is_null($this->aprev)) return $this->aprev;
  336. $this->aprev = false;
  337. if ($id = $this->db->findid("status = 'published' and posted < '$this->sqldate' order by posted desc")) {
  338. $this->aprev = self::i($id);
  339. }
  340. return $this->aprev;
  341. }
  342. public function getnext() {
  343. if (!is_null($this->anext)) return $this->anext;
  344. $this->anext = false;
  345. if ($id = $this->db->findid("status = 'published' and posted > '$this->sqldate' order by posted asc")) {
  346. $this->anext = self::i($id);
  347. }
  348. return $this->anext;
  349. }
  350. public function getmeta() {
  351. if (!isset($this->_meta)) $this->_meta = $this->factory->getmeta($this->id);
  352. return $this->_meta;
  353. }
  354. public function Getlink() {
  355. return litepublisher::$site->url . $this->url;
  356. }
  357. public function Setlink($link) {
  358. if ($a = @parse_url($link)) {
  359. if (empty($a['query'])) {
  360. $this->url = $a['path'];
  361. } else {
  362. $this->url = $a['path'] . '?' . $a['query'];
  363. }
  364. }
  365. }
  366. public function settitle($title) {
  367. $this->data['title'] = tcontentfilter::escape(tcontentfilter::unescape($title));
  368. }
  369. public function gettheme() {
  370. ttheme::$vars['post'] = $this;
  371. if (isset($this->_theme)) return $this->_theme;
  372. $this->_theme = isset(ttemplate::i()->view) ? ttemplate::i()->view->theme : tview::getview($this)->theme;
  373. return $this->_theme;
  374. }
  375. public function parsetml($path) {
  376. $theme = $this->theme;
  377. return $theme->parse($theme->templates[$path]);
  378. }
  379. public function getextra() {
  380. $theme = $this->theme;
  381. return $theme->parse($theme->extratml);
  382. }
  383. public function getbookmark() {
  384. return $this->theme->parse('<a href="$post.link" rel="bookmark" title="$lang.permalink $post.title">$post.iconlink$post.title</a>');
  385. }
  386. public function getrsscomments() {
  387. return litepublisher::$site->url . "/comments/$this->id.xml";
  388. }
  389. public function Getpubdate() {
  390. return date('r', $this->posted);
  391. }
  392. public function Setpubdate($date) {
  393. $this->data['posted'] = strtotime($date);
  394. }
  395. public function getsqldate() {
  396. return sqldate($this->posted);
  397. }
  398. public function getidimage() {
  399. if (count($this->files) == 0) return false;
  400. $files = $this->factory->files;
  401. foreach ($this->files as $id) {
  402. $item = $files->getitem($id);
  403. if ('image' == $item['media']) return $id;
  404. }
  405. return false;
  406. }
  407. public function getimage() {
  408. if ($id = $this->getidimage()) {
  409. return $this->factory->files->geturl($id);
  410. }
  411. return false;
  412. }
  413. public function getthumb() {
  414. if (count($this->files) == 0) return false;
  415. $files = $this->factory->files;
  416. foreach ($this->files as $id) {
  417. $item = $files->getitem($id);
  418. if (intval($item['preview'])) return $files->geturl($item['preview']);
  419. }
  420. return false;
  421. }
  422. //template
  423. protected function get_taglinks($name, $excerpt) {
  424. $items = $this->$name;
  425. if (!count($items)) return '';
  426. $theme = $this->theme;
  427. $tmlpath= $excerpt ? 'content.excerpts.excerpt' : 'content.post';
  428. $tmlpath .= $name == 'tags' ? '.taglinks' : '.catlinks';
  429. $tmlitem = $theme->templates[$tmlpath . '.item'];
  430. $tags= strbegin($name, 'tag') ? $this->factory->tags : $this->factory->categories;
  431. $tags->loaditems($items);
  432. $args = new targs();
  433. $list = array();
  434. foreach ($items as $id) {
  435. $item = $tags->getitem($id);
  436. $args->add($item);
  437. if (($item['icon'] == 0) || litepublisher::$options->icondisabled) {
  438. $args->icon = '';
  439. } else {
  440. $files = $this->factory->files;
  441. if ($files->itemexists($item['icon'])) {
  442. $args->icon = $files->geticon($item['icon']);
  443. } else {
  444. $args->icon = '';
  445. }
  446. }
  447. $list[] = $theme->parsearg($tmlitem, $args);
  448. }
  449. $args->items = ' ' . implode($theme->templates[$tmlpath . '.divider'] , $list);
  450. $result = $theme->parsearg($theme->templates[$tmlpath], $args);
  451. $this->factory->posts->callevent('ontags', array($tags, $excerpt, &$result));
  452. return $result;
  453. }
  454. public function getdate() {
  455. return tlocal::date($this->posted, $this->theme->templates['content.post.date']);
  456. }
  457. public function getexcerptdate() {
  458. return tlocal::date($this->posted, $this->theme->templates['content.excerpts.excerpt.date']);
  459. }
  460. public function getday() {
  461. return date($this->posted, 'D');
  462. }
  463. public function getmonth() {
  464. return tlocal::date($this->posted, 'M');
  465. }
  466. public function getyear() {
  467. return date($this->posted, 'Y');
  468. }
  469. public function getmorelink() {
  470. if ($this->moretitle == '') return '';
  471. return $this->parsetml('content.excerpts.excerpt.morelink');
  472. }
  473. public function gettagnames() {
  474. if (count($this->tags) == 0) return '';
  475. $tags = $this->factory->tags;
  476. return implode(', ', $tags->getnames($this->tags));
  477. }
  478. public function settagnames($names) {
  479. $tags = $this->factory->tags;
  480. $this->tags= $tags->createnames($names);
  481. }
  482. public function getcatnames() {
  483. if (count($this->categories) == 0) return '';
  484. $categories = $this->factory->categories;
  485. return implode(', ', $categories->getnames($this->categories));
  486. }
  487. public function setcatnames($names) {
  488. $categories = $this->factory->categories;
  489. $this->categories = $categories->createnames($names);
  490. if (count($this->categories ) == 0) {
  491. $defaultid = $categories->defaultid;
  492. if ($defaultid > 0) $this->data['categories '][] = $dfaultid;
  493. }
  494. }
  495. public function getcategory() {
  496. if (count($this->categories) == 0) return '';
  497. $cats = $this->factory->categories;
  498. return $cats->getname($this->categories[0]);
  499. }
  500. public function getidcat() {
  501. if (($cats = $this->categories) && count($cats)) return $cats[0];
  502. return 0;
  503. }
  504. //ITemplate
  505. public function request($id) {
  506. parent::request((int) $id);
  507. if ($this->status != 'published') {
  508. if (!litepublisher::$options->show_draft_post) return 404;
  509. $groupname = litepublisher::$options->group;
  510. if (($groupname == 'admin') || ($groupname == 'editor')) return;
  511. if ($this->author == litepublisher::$options->user) return;
  512. return 404;
  513. }
  514. }
  515. public function gettitle() {
  516. //if ($this->data['title2'] != '') return $this->data['title2'];
  517. return $this->data['title'];
  518. }
  519. public function gethead() {
  520. $result = $this->rawhead;
  521. ttemplate::i()->ltoptions['idpost'] = $this->id;
  522. $theme = $this->theme;
  523. $result .= $theme->templates['head.post'];
  524. if ($prev = $this->prev) {
  525. ttheme::$vars['prev'] = $prev;
  526. $result .= $theme->templates['head.post.prev'];
  527. }
  528. if ($next = $this->next) {
  529. ttheme::$vars['next'] = $next;
  530. $result .= $theme->templates['head.post.next'];
  531. }
  532. if ($this->hascomm) {
  533. $lang = tlocal::i('comment');
  534. $result .= $theme->templates['head.post.rss'];
  535. }
  536. $result = $theme->parse($result);
  537. $this->factory->posts->callevent('onhead', array($this, &$result));
  538. return $result;
  539. }
  540. public function getanhead() {
  541. $result = '';
  542. $this->factory->posts->callevent('onanhead', array($this, &$result));
  543. return $result;
  544. }
  545. public function getkeywords() {
  546. return empty($this->data['keywords']) ? $this->Gettagnames() : $this->data['keywords'];
  547. }
  548. //fix for file version. For db must be deleted
  549. public function setkeywords($s) {
  550. $this->data['keywords'] = $s;
  551. }
  552. public function getdescription() {
  553. return $this->data['description'];
  554. }
  555. public function getidview() {
  556. return $this->data['idview'];
  557. }
  558. public function setidview($id) {
  559. if ($id != $this->idview) {
  560. $this->data['idview'] = $id;
  561. if ($this->id) $this->db->setvalue($this->id, 'idview', $id);
  562. }
  563. }
  564. public function setid_view($id_view) {
  565. $this->data['idview'] = $id_view;
  566. }
  567. public function geticonurl() {
  568. if ($this->icon == 0) return '';
  569. $files = $this->factory->files;
  570. if ($files->itemexists($this->icon)) return $files->geturl($this->icon);
  571. $this->icon = 0;
  572. $this->save();
  573. return '';
  574. }
  575. public function geticonlink() {
  576. if (($this->icon == 0) || litepublisher::$options->icondisabled) return '';
  577. $files = $this->factory->files;
  578. if ($files->itemexists($this->icon)) return $files->geticon($this->icon);
  579. $this->icon = 0;
  580. $this->save();
  581. return '';
  582. }
  583. public function setfiles(array $list) {
  584. array_clean($list);
  585. $this->data['files'] = $list;
  586. }
  587. public function getfilelist() {
  588. if ((count($this->files) == 0) || ((litepublisher::$urlmap->page > 1) && litepublisher::$options->hidefilesonpage)) return '';
  589. $files = $this->factory->files;
  590. return $files->getfilelist($this->files, false);
  591. }
  592. public function getexcerptfilelist() {
  593. if (count($this->files) == 0) return '';
  594. $files = $this->factory->files;
  595. return $files->getfilelist($this->files, true);
  596. }
  597. public function getindex_tml() {
  598. $theme = $this->theme;
  599. if (!empty($theme->templates['index.post'])) return $theme->templates['index.post'];
  600. return false;
  601. }
  602. public function getcont() {
  603. return $this->parsetml('content.post');
  604. }
  605. public function getcontexcerpt($lite) {
  606. //no use self theme because post in other context
  607. $theme = ttheme::i();
  608. $tml = $lite ? $theme->templates['content.excerpts.lite.excerpt'] : $theme->templates['content.excerpts.excerpt'];
  609. ttheme::$vars['post'] = $this;
  610. return $theme->parse($tml);
  611. }
  612. public function getrsslink() {
  613. if ($this->hascomm) {
  614. return $this->parsetml('content.post.rsslink');
  615. }
  616. return '';
  617. }
  618. public function onrssitem($item) {
  619. }
  620. public function getprevnext() {
  621. $prev = '';
  622. $next = '';
  623. $theme = $this->theme;
  624. if ($prevpost = $this->prev) {
  625. ttheme::$vars['prevpost'] = $prevpost;
  626. $prev = $theme->parse($theme->templates['content.post.prevnext.prev']);
  627. }
  628. if ($nextpost = $this->next) {
  629. ttheme::$vars['nextpost'] = $nextpost;
  630. $next = $theme->parse($theme->templates['content.post.prevnext.next']);
  631. }
  632. if (($prev == '') && ($next == '')) return '';
  633. $result = strtr( $theme->parse($theme->templates['content.post.prevnext']), array(
  634. '$prev' => $prev,
  635. '$next' => $next
  636. ));
  637. unset(ttheme::$vars['prevpost'],ttheme::$vars['nextpost']);
  638. return $result;
  639. }
  640. public function getcommentslink() {
  641. if (($this->comstatus == 'closed') || !litepublisher::$options->commentspull) {
  642. if (($this->commentscount == 0) && (($this->comstatus == 'closed'))) return '';
  643. return sprintf('<a href="%s%s#comments">%s</a>', litepublisher::$site->url, $this->getlastcommenturl(), $this->getcmtcount());
  644. } else {
  645. //inject php code
  646. $l = tlocal::i()->ini['comment'];
  647. $result =sprintf('<?php
  648. echo \'<a href="%s%s#comments">\';
  649. $count = tcommentspull::i()->get(%d);
  650. ',litepublisher::$site->url, $this->getlastcommenturl(), $this->id);
  651. $result .= 'if ($count == 0) {
  652. echo \'' . $l[0] . '\';
  653. } elseif ($count == 1) {
  654. echo \'' . $l[1] . '\';
  655. } else {
  656. echo sprintf(\'' . $l[2] . '\', $count);
  657. }
  658. echo \'</a>\';
  659. ?>';
  660. }
  661. return $result;
  662. }
  663. public function getcmtcount() {
  664. $l = tlocal::i()->ini['comment'];
  665. switch($this->commentscount) {
  666. case 0: return $l[0];
  667. case 1: return $l[1];
  668. default: return sprintf($l[2], $this->commentscount);
  669. }
  670. }
  671. public function gettemplatecomments() {
  672. $result = '';
  673. $page = litepublisher::$urlmap->page;
  674. $countpages = $this->countpages;
  675. if ($countpages > 1) $result .= $this->theme->getpages($this->url, $page, $countpages);
  676. if (($this->commentscount > 0) || ($this->comstatus != 'closed') || ($this->pingbackscount > 0)) {
  677. if (($countpages > 1) && ($this->commentpages < $page)) {
  678. $result .= $this->getcommentslink();
  679. } else {
  680. $result .= $this->factory->templatecomments->getcomments($this->id);
  681. }
  682. }
  683. return $result;
  684. }
  685. public function gethascomm() {
  686. return ($this->data['comstatus'] != 'closed') && ((int) $this->data['commentscount'] > 0);
  687. }
  688. public function getexcerptcontent() {
  689. $posts = $this->factory->posts;
  690. if ($this->revision < $posts->revision) $this->update_revision($posts->revision);
  691. $result = $this->excerpt;
  692. $posts->beforeexcerpt($this, $result);
  693. $result = $this->replacemore($result, true);
  694. if (litepublisher::$options->parsepost) {
  695. $result = $this->theme->parse($result);
  696. }
  697. $posts->afterexcerpt($this, $result);
  698. return $result;
  699. }
  700. public function replacemore($content, $excerpt) {
  701. $more = $this->parsetml($excerpt ?
  702. 'content.excerpts.excerpt.morelink' :
  703. 'content.post.more');
  704. $tag = '<!--more-->';
  705. if ($i =strpos($content, $tag)) {
  706. return str_replace($tag, $more, $content);
  707. } else {
  708. return $excerpt ? $content : $more . $content;
  709. }
  710. }
  711. protected function getteaser() {
  712. $content = $this->filtered;
  713. $tag = '<!--more-->';
  714. if ($i =strpos($content, $tag)) {
  715. $content = substr($content, $i + strlen($tag));
  716. if (!strbegin($content, '<p>')) $content = '<p>' . $content;
  717. return $content;
  718. }
  719. return '';
  720. }
  721. protected function getcontentpage($page) {
  722. $result = '';
  723. if ($page == 1) {
  724. $result .= $this->filtered;
  725. $result = $this->replacemore($result, false);
  726. } elseif ($s = $this->getpage($page - 2)) {
  727. $result .= $s;
  728. } elseif ($page <= $this->commentpages) {
  729. } else {
  730. $result .= tlocal::i()->notfound;
  731. }
  732. return $result;
  733. }
  734. public function getcontent() {
  735. $result = '';
  736. $posts = $this->factory->posts;
  737. $posts->beforecontent($this, $result);
  738. if ($this->revision < $posts->revision) $this->update_revision($posts->revision);
  739. $result .= $this->getcontentpage(litepublisher::$urlmap->page);
  740. if (litepublisher::$options->parsepost) {
  741. $result = $this->theme->parse($result);
  742. }
  743. $posts->aftercontent($this, $result);
  744. return $result;
  745. }
  746. public function setcontent($s) {
  747. if (!is_string($s)) $this->error('Error! Post content must be string');
  748. $this->rawcontent = $s;
  749. tcontentfilter::i()->filterpost($this,$s);
  750. }
  751. public function update_revision($value) {
  752. if ($value != $this->revision) {
  753. $this->updatefiltered();
  754. $posts = $this->factory->posts;
  755. $this->revision = (int) $posts->revision;
  756. if ($this->id > 0) $this->save();
  757. }
  758. }
  759. public function updatefiltered() {
  760. tcontentfilter::i()->filterpost($this,$this->rawcontent);
  761. }
  762. public function getrawcontent() {
  763. if (($this->id > 0) && ($this->data['rawcontent'] === false)) {
  764. $this->data['rawcontent'] = $this->rawdb->getvalue($this->id, 'rawcontent');
  765. }
  766. return $this->data['rawcontent'];
  767. }
  768. protected function getrawdb() {
  769. return $this->getdb('rawposts');
  770. }
  771. public function getpage($i) {
  772. if ( isset($this->data['pages'][$i])) return $this->data['pages'][$i];
  773. if ($this->id > 0) {
  774. if ($r = $this->getdb('pages')->getassoc("(id = $this->id) and (page = $i) limit 1")) {
  775. $s = $r['content'];
  776. } else {
  777. $s = false;
  778. }
  779. $this->data['pages'][$i] = $s;
  780. return $s;
  781. }
  782. return false;
  783. }
  784. public function addpage($s) {
  785. $this->data['pages'][] = $s;
  786. $this->data['pagescount'] = count($this->data['pages']);
  787. if ($this->id > 0) {
  788. $this->getdb('pages')->insert(array(
  789. 'id' => $this->id,
  790. 'page' => $this->data['pagescount'] -1,
  791. 'content' => $s
  792. ));
  793. }
  794. }
  795. public function deletepages() {
  796. $this->data['pages'] = array();
  797. $this->data['pagescount'] = 0;
  798. if ($this->id > 0) $this->getdb('pages')->iddelete($this->id);
  799. }
  800. public function gethaspages() {
  801. return ($this->pagescount > 1) || ($this->commentpages > 1);
  802. }
  803. public function getpagescount() {
  804. return $this->data['pagescount'] + 1;
  805. }
  806. public function getcountpages() {
  807. return max($this->pagescount, $this->commentpages);
  808. }
  809. public function getcommentpages() {
  810. if (!litepublisher::$options->commentpages || ($this->commentscount <= litepublisher::$options->commentsperpage)) return 1;
  811. return ceil($this->commentscount / litepublisher::$options->commentsperpage);
  812. }
  813. public function getlastcommenturl() {
  814. $c = $this->commentpages;
  815. $url = $this->url;
  816. if (($c > 1) && !litepublisher::$options->comments_invert_order) $url = rtrim($url, '/') . "/page/$c/";
  817. return $url;
  818. }
  819. public function clearcache() {
  820. litepublisher::$urlmap->setexpired($this->idurl);
  821. }
  822. public function getschemalink() {
  823. return 'post';
  824. }
  825. //author
  826. protected function getauthorname() {
  827. return $this->getusername($this->author, false);
  828. }
  829. protected function getauthorlink() {
  830. return $this->getusername($this->author, true);
  831. }
  832. protected function getusername($id, $link) {
  833. if ($id <= 1) {
  834. if ($link) {
  835. return sprintf('<a href="%s/" rel="author" title="%2$s">%2$s</a>', litepublisher::$site->url, litepublisher::$site->author);
  836. } else {
  837. return litepublisher::$site->author;
  838. }
  839. } else {
  840. $users = tusers::i();
  841. if (!$users->itemexists($id)) return '';
  842. $item = $users->getitem($id);
  843. if (!$link || ($item['website'] == '')) return $item['name'];
  844. return sprintf('<a href="%s/users.htm%sid=%s">%s</a>',litepublisher::$site->url, litepublisher::$site->q, $id, $item['name']);
  845. }
  846. }
  847. public function getauthorpage() {
  848. $id = $this->author;
  849. if ($id <= 1) {
  850. return sprintf('<a href="%s/" rel="author" title="%2$s">%2$s</a>', litepublisher::$site->url, litepublisher::$site->author);
  851. } else {
  852. $pages = tuserpages::i();
  853. if (!$pages->itemexists($id)) return '';
  854. $pages->id = $id;
  855. if ($pages->url == '') return '';
  856. return sprintf('<a href="%s%s" title="%3$s" rel="author"><%3$s</a>', litepublisher::$site->url, $pages->url, $pages->name);
  857. }
  858. }
  859. }//class
  860. class tpostfactory extends tdata {
  861. public static function i() {
  862. return getinstance(__class__);
  863. }
  864. public function getposts() {
  865. return tposts::i();
  866. }
  867. public function getfiles() {
  868. return tfiles::i();
  869. }
  870. public function gettags() {
  871. return ttags::i();
  872. }
  873. public function getcats () {
  874. return tcategories::i();
  875. }
  876. public function getcategories() {
  877. return tcategories::i();
  878. }
  879. public function gettemplatecomments() {
  880. return ttemplatecomments::i();
  881. }
  882. public function getcomments($id) {
  883. return tcomments::i($id);
  884. }
  885. public function getpingbacks($id) {
  886. return tpingbacks::i($id);
  887. }
  888. public function getmeta($id) {
  889. return tmetapost::i($id);
  890. }
  891. public function gettransform(tpost $post) {
  892. return tposttransform::i($post);
  893. }
  894. public function add(tpost $post) {
  895. return tposttransform ::add($post);
  896. }
  897. }//class
  898. //posts.class.php
  899. class tposts extends titems {
  900. public $itemcoclasses;
  901. public $archives;
  902. public $rawtable;
  903. public $childtable;
  904. public static function i() {
  905. return getinstance(__class__);
  906. }
  907. public static function unsub($obj) {
  908. self::i()->unbind($obj);
  909. }
  910. protected function create() {
  911. $this->dbversion = true;
  912. parent::create();
  913. $this->table = 'posts';
  914. $this->childtable = '';
  915. $this->rawtable = 'rawposts';
  916. $this->basename = 'posts/index';
  917. $this->addevents('edited', 'changed', 'singlecron', 'beforecontent', 'aftercontent', 'beforeexcerpt', 'afterexcerpt',
  918. 'onselect', 'onhead', 'onanhead', 'ontags');
  919. $this->data['archivescount'] = 0;
  920. $this->data['revision'] = 0;
  921. $this->data['syncmeta'] = false;
  922. $this->addmap('itemcoclasses', array());
  923. }
  924. public function getitem($id) {
  925. if ($result = tpost::i($id)) return $result;
  926. $this->error("Item $id not found in class ". get_class($this));
  927. }
  928. public function finditems($where, $limit) {
  929. if (isset(titem::$instances['post']) && (count(titem::$instances['post']) > 0)) {
  930. $result = $this->db->idselect($where . ' '. $limit);
  931. $this->loaditems($result);
  932. return $result;
  933. } else {
  934. return $this->select($where, $limit);
  935. }
  936. }
  937. public function loaditems(array $items) {
  938. //exclude already loaded items
  939. if (!isset(titem::$instances['post'])) titem::$instances['post'] = array();
  940. $loaded = array_keys(titem::$instances['post']);
  941. $newitems = array_diff($items, $loaded);
  942. if (!count($newitems)) return $items;
  943. $newitems = $this->select(sprintf('%s.id in (%s)', $this->thistable, implode(',', $newitems)), '');
  944. return array_merge($newitems, array_intersect($loaded, $items));
  945. }
  946. public function setassoc(array $items) {
  947. if (count($items) == 0) return array();
  948. $result = array();
  949. $t = new tposttransform();
  950. $fileitems = array();
  951. foreach ($items as $a) {
  952. $t->post = tpost::newpost($a['class']);
  953. $t->setassoc($a);
  954. $result[] = $t->post->id;
  955. $f = $t->post->files;
  956. if (count($f) > 0) $fileitems = array_merge($fileitems, array_diff($f, $fileitems));
  957. }
  958. unset($t);
  959. if ($this->syncmeta) tmetapost::loaditems($result);
  960. if (count($fileitems) > 0) {
  961. $files = tfiles::i();
  962. $files->preload($fileitems);
  963. }
  964. $this->onselect($result);
  965. return $result;
  966. }
  967. public function select($where, $limit) {
  968. $db = litepublisher::$db;
  969. if ($this->childtable) {
  970. $childtable = $db->prefix . $this->childtable;
  971. return $this->setassoc($db->res2items($db->query("select $db->posts.*, $db->urlmap.url as url, $childtable.*
  972. from $db->posts, $db->urlmap, $childtable
  973. where $where and $db->posts.id = $childtable.id and $db->urlmap.id = $db->posts.idurl $limit")));
  974. }
  975. $items = $db->res2items($db->query("select $db->posts.*, $db->urlmap.url as url from $db->posts, $db->urlmap
  976. where $where and $db->urlmap.id = $db->posts.idurl $limit"));
  977. /*
  978. $items = $db->res2items($db->query(
  979. "select $db->posts.*, $db->urlmap.url as url from $db->posts
  980. left join $db->urlmap on $db->urlmap.id = $db->posts.idurl
  981. where $where $limit"));
  982. */
  983. if (count($items) == 0) return array();
  984. $subclasses = array();
  985. foreach ($items as &$item) {
  986. if (empty($item['class'])) $item['class'] = 'tpost';
  987. if ($item['class'] != 'tpost') {
  988. $subclasses[$item['class']][] = $item['id'];
  989. }
  990. }
  991. unset($item);
  992. foreach ($subclasses as $class => $list) {
  993. /*
  994. $childtable = $db->prefix .
  995. call_user_func_array(array($class, 'getchildtable'), array());
  996. $list = implode(',', $list);
  997. $subitems = $db->res2items($db->query("select $childtable.*
  998. from $childtable where id in ($list)"));
  999. */
  1000. $subitems = call_user_func_array(array($class, 'selectitems'), array($list));
  1001. foreach ($subitems as $id => $subitem) {
  1002. $items[$id] = array_merge($items[$id], $subitem);
  1003. }
  1004. }
  1005. return $this->setassoc($items);
  1006. }
  1007. public function getcount() {
  1008. return $this->db->getcount("status<> 'deleted'");
  1009. }
  1010. public function getchildscount($where) {
  1011. if ($this->childtable == '') return 0;
  1012. $db = litepublisher::$db;
  1013. $childtable = $db->prefix . $this->childtable;
  1014. if ($res = $db->query("SELECT COUNT($db->posts.id) as count FROM $db->posts, $childtable
  1015. where $db->posts.status <> 'deleted' and $childtable.id = $db->posts.id $where")) {
  1016. if ($r = $db->fetchassoc($res)) return $r['count'];
  1017. }
  1018. return 0;
  1019. }
  1020. public function getlinks($where, $tml) {
  1021. $db = $this->db;
  1022. $t = $this->thistable;
  1023. $items = $db->res2assoc($db->query(
  1024. "select $t.id, $t.title, $db->urlmap.url as url from $t, $db->urlmap
  1025. where $t.status = 'published' and $where and $db->urlmap.id = $t.idurl"));
  1026. if (count($items) == 0) return '';
  1027. $result = '';
  1028. $args = new targs();
  1029. $theme = ttheme::i();
  1030. foreach ($items as $item) {
  1031. $args->add($item);
  1032. $result .=$theme->parsearg($tml, $args);
  1033. }
  1034. return $result;
  1035. }
  1036. private function beforechange($post) {
  1037. $post->title = trim($post->title);
  1038. $post->modified = time();
  1039. $post->revision = $this->revision;
  1040. $post->class = get_class($post);
  1041. if (($post->status == 'published') && ($post->posted > time())) {
  1042. $post->status = 'future';
  1043. } elseif (($post->status == 'future') && ($post->posted <= time())) {
  1044. $post->status = 'published';
  1045. }
  1046. }
  1047. public function add(tpost $post) { if ($post->posted == 0) $post->posted = time();
  1048. $this->beforechange($post);
  1049. if ($post->posted == 0) $post->posted = time();
  1050. if ($post->posted <= time()) {
  1051. if ($post->status == 'future') $post->status = 'published';
  1052. } else {
  1053. if ($post->status =='published') $post->status = 'future';
  1054. }
  1055. if (($post->icon == 0) && !litepublisher::$options->icondisabled) {
  1056. $icons = ticons::i();
  1057. $post->icon = $icons->getid('post');
  1058. }
  1059. if ($post->idview == 1) {
  1060. $views = tviews::i();
  1061. if (isset($views->defaults['post'])) $post->id_view = $views->defaults['post'];
  1062. }
  1063. $linkgen = tlinkgenerator::i();
  1064. $post->url = $linkgen->addurl($post, $post->schemalink);
  1065. $urlmap = turlmap::i();
  1066. $id = $post->addtodb();
  1067. $post->idurl = $urlmap->add($post->url, get_class($post), (int) $post->id);
  1068. $post->db->setvalue($post->id, 'idurl', $post->idurl);
  1069. $post->onid();
  1070. $this->lock();
  1071. $this->updated($post);
  1072. $this->cointerface('add', $post);
  1073. $this->unlock();
  1074. $this->added($post->id);
  1075. $this->changed();
  1076. $urlmap->clearcache();
  1077. return $post->id;
  1078. }
  1079. public function edit(tpost $post) {
  1080. $this->beforechange($post);
  1081. $linkgen = tlinkgenerator::i();
  1082. $linkgen->editurl($post, $post->schemalink);
  1083. if ($post->posted <= time()) {
  1084. if ($post->status == 'future') $post->status = 'published';
  1085. } else {
  1086. if ($post->status =='published') $post->status = 'future';
  1087. }
  1088. $this->lock();
  1089. $post->save();
  1090. $this->updated($post);
  1091. $this->cointerface('edit', $post);
  1092. $this->unlock();
  1093. $this->edited($post->id);
  1094. $this->changed();
  1095. litepublisher::$urlmap->clearcache();
  1096. }
  1097. public function delete($id) {
  1098. if (!$this->itemexists($id)) return false;
  1099. $urlmap = turlmap::i();
  1100. $idurl = $this->db->getvalue($id, 'idurl');
  1101. $this->db->setvalue($id, 'status', 'deleted');
  1102. if ($this->childtable) {
  1103. $db = $this->getdb($this->childtable);
  1104. $db->delete("id = $id");
  1105. }
  1106. $this->lock();
  1107. $this->PublishFuture();
  1108. $this->UpdateArchives();
  1109. $this->cointerface('delete', $id);
  1110. $this->unlock();
  1111. $this->deleted($id);
  1112. $this->changed();
  1113. $urlmap->clearcache();
  1114. return true;
  1115. }
  1116. public function updated(tpost $post) {
  1117. $this->PublishFuture();
  1118. $this->UpdateArchives();
  1119. tcron::i()->add('single', get_class($this), 'dosinglecron', $post->id);
  1120. }
  1121. public function UpdateArchives() {
  1122. $this->archivescount = $this->db->getcount("status = 'published' and posted <= '" . sqldate() . "'");
  1123. }
  1124. public function dosinglecron($id) {
  1125. $this->PublishFuture();
  1126. ttheme::$vars['post'] = tpost::i($id);
  1127. $this->singlecron($id);
  1128. unset(ttheme::$vars['post']);
  1129. }
  1130. public function hourcron() {
  1131. $this->PublishFuture();
  1132. }
  1133. private function publish($id) {
  1134. $post = tpost::i($id);
  1135. $post->status = 'published';
  1136. $this->edit($post);
  1137. }
  1138. public function PublishFuture() {
  1139. if ($list = $this->db->idselect(sprintf('status = \'future\' and posted <= \'%s\' order by posted asc', sqldate()))) {
  1140. foreach( $list as $id) $this->publish($id);
  1141. }
  1142. }
  1143. public function getrecent($author, $count) {
  1144. $author = (int) $author;
  1145. $where = "status != 'deleted'";
  1146. if ($author > 1) $where .= " and author = $author";
  1147. return $this->finditems($where, ' order by posted desc limit ' . (int) $count);
  1148. }
  1149. public function getpage($author, $page, $perpage, $invertorder) {
  1150. $author = (int) $author;
  1151. $from = ($page - 1) * $perpage;
  1152. $t = $this->thistable;
  1153. $where = "$t.status = 'published'";
  1154. if ($author > 1) $where .= " and $t.author = $author";
  1155. $order = $invertorder ? 'asc' : 'desc';
  1156. return $this->finditems($where, " order by $t.posted $order limit $from, $perpage");
  1157. }
  1158. public function stripdrafts(array $items) {
  1159. if (count($items) == 0) return array();
  1160. $list = implode(', ', $items);
  1161. $t = $this->thistable;
  1162. return $this->db->idselect("$t.status = 'published' and $t.id in ($list)");
  1163. }
  1164. //coclasses
  1165. private function cointerface($method, $arg) {
  1166. foreach ($this->coinstances as $coinstance) {
  1167. if ($coinstance instanceof ipost) $coinstance->$method($arg);
  1168. }
  1169. }
  1170. public function addrevision() {
  1171. $this->data['revision']++;
  1172. $this->save();
  1173. litepublisher::$urlmap->clearcache();
  1174. }
  1175. public function getanhead(array $items) {
  1176. if (count($items) == 0) return '';
  1177. $this->loaditems($items);
  1178. $result = '';
  1179. foreach($items as $id) {
  1180. $result .= tpost::i($id)->anhead;
  1181. }
  1182. return $result;
  1183. }
  1184. //fix call reference
  1185. public function beforecontent($post, &$result) {
  1186. $this->callevent('beforecontent', array($post, &$result));
  1187. }
  1188. public function aftercontent($post, &$result) {
  1189. $this->callevent('aftercontent', array($post, &$result));
  1190. }
  1191. public function beforeexcerpt($post, &$result) {
  1192. $this->callevent('beforeexcerpt', array($post, &$result));
  1193. }
  1194. public function afterexcerpt($post, &$result) {
  1195. $this->callevent('afterexcerpt', array($post, &$result));
  1196. }
  1197. public function getsitemap($from, $count) {
  1198. return $this->externalfunc(__class__, 'Getsitemap', array($from, $count));
  1199. }
  1200. }//class
  1201. class tpostswidget extends twidget {
  1202. public static function i() {
  1203. return getinstance(__class__);
  1204. }
  1205. protected function create() {
  1206. parent::create();
  1207. $this->basename = 'widget.posts';
  1208. $this->template = 'posts';
  1209. $this->adminclass = 'tadminmaxcount';
  1210. $this->data['maxcount'] = 10;
  1211. }
  1212. public function getdeftitle() {
  1213. return tlocal::get('default', 'recentposts');
  1214. }
  1215. public function getcontent($id, $sidebar) {
  1216. $posts = tposts::i();
  1217. $list = $posts->getpage(0, 1, $this->maxcount, false);
  1218. $theme = ttheme::i();
  1219. return $theme->getpostswidgetcontent($list, $sidebar, '');
  1220. }
  1221. }//class
  1222. //post.transform.class.php
  1223. class tposttransform {
  1224. public $post;
  1225. public static $arrayprops= array('categories', 'tags', 'files');
  1226. public static $intprops= array('id', 'idurl', 'parent', 'author', 'revision', 'icon', 'commentscount', 'pingbackscount', 'pagescount', 'idview', 'idperm');
  1227. public static $boolprops= array('pingenabled');
  1228. public static $props = array('id', 'idurl', 'parent', 'author', 'revision', 'class',
  1229. //'created', 'modified',
  1230. 'posted',
  1231. 'title', 'title2', 'filtered', 'excerpt', 'rss', 'keywords', 'description', 'rawhead', 'moretitle',
  1232. 'categories', 'tags', 'files',
  1233. 'password', 'idview', 'idperm', 'icon',
  1234. 'status', 'comstatus', 'pingenabled',
  1235. 'commentscount', 'pingbackscount', 'pagescount',
  1236. );
  1237. public static function i(tpost $post) {
  1238. $self = getinstance(__class__);
  1239. $self->post = $post;
  1240. return $self;
  1241. }
  1242. public static function add(tpost $post) {
  1243. $self = self::i($post);
  1244. $values = array();
  1245. foreach (self::$props as $name) {
  1246. $values[$name] = $self->__get($name);
  1247. }
  1248. $db = $post->db;
  1249. $id = $db->add($values);
  1250. $post->rawdb->insert(array(
  1251. 'id' => $id,
  1252. 'created' => sqldate(),
  1253. 'modified' => sqldate(),
  1254. 'rawcontent' => $post->data['rawcontent']
  1255. ));
  1256. $db->table = 'pages';
  1257. foreach ($post->data['pages'] as $i => $content) {
  1258. $db->insert(array('id' => $id, 'page' => $i, 'content' => $content));
  1259. }
  1260. return $id;
  1261. }
  1262. public function save() {
  1263. $post = $this->post;
  1264. $db = $post->db;
  1265. $list = array();
  1266. foreach (self::$props As $name) {
  1267. if ($name == 'id') continue;
  1268. $list[] = "$name = " . $db->quote($this->__get($name));
  1269. }
  1270. $db->idupdate($post->id, implode(', ', $list));
  1271. $raw = array(
  1272. 'id' => $post->id,
  1273. 'modified' => sqldate()
  1274. );
  1275. if (false !== $post->data['rawcontent']) $raw['rawcontent'] = $post->data['rawcontent'];
  1276. $post->rawdb->updateassoc($raw);
  1277. /*
  1278. $db->table = 'pages';
  1279. $db->iddelete($post->id);
  1280. foreach ($post->data['pages'] as $i => $content) {
  1281. $db->insert(array('id' => $post->id, 'page' => $i, 'content' => $content));
  1282. }
  1283. */
  1284. }
  1285. public function setassoc(array $a) {
  1286. foreach ($a as $name => $value) {
  1287. $this->__set($name, $value);
  1288. }
  1289. }
  1290. public function __get($name) {
  1291. if ('pagescount' == $name) return $this->post->data[$name];
  1292. if (method_exists($this, $get = "get$name")) return $this->$get();
  1293. if (in_array($name, self::$arrayprops)) return implode(',', $this->post->$name);
  1294. if (in_array($name, self::$boolprops)) return $this->post->$name ? 1 : 0;
  1295. return $this->post->$name;
  1296. }
  1297. public function __set($name, $value) {
  1298. if (method_exists($this, $set = "set$name")) return $this->$set($value);
  1299. if (in_array($name, self::$arrayprops)) {
  1300. $this->post->data[$name] = tdatabase::str2array($value);
  1301. } elseif (in_array($name, self::$intprops)) {
  1302. $this->post->$name = (int) $value;
  1303. } elseif (in_array($name, self::$boolprops)) {
  1304. $this->post->data[$name] = $value == '1';
  1305. } else {
  1306. $this->post->$name = $value;
  1307. }
  1308. }
  1309. private function getposted() {
  1310. return sqldate($this->post->posted);
  1311. }
  1312. private function setposted($value) {
  1313. $this->post->posted = strtotime($value);
  1314. }
  1315. private function setrevision($value) {
  1316. $this->post->data['revision'] = $value;
  1317. }
  1318. }//class
  1319. //post.meta.class.php
  1320. class tmetapost extends titem {
  1321. public static function i($id = 0) {
  1322. return parent::iteminstance(__class__, (int) $id);
  1323. }
  1324. public static function getinstancename() {
  1325. return 'postmeta';
  1326. }
  1327. protected function create() {
  1328. $this->table = 'postsmeta';
  1329. }
  1330. public function getdbversion() {
  1331. return true;
  1332. }
  1333. public function __set($name, $value) {
  1334. if ($name == 'id') return $this->setid($value);
  1335. $exists = isset($this->data[$name]);
  1336. if ($exists && ($this->data[$name] == $value)) return true;
  1337. $this->data[$name] = $value;
  1338. $name = dbquote($name);
  1339. $value = dbquote($value);
  1340. if ($exists) {
  1341. $this->db->update("value = $value", "id = $this->id and name = $name");
  1342. } else {
  1343. $this->db->insertrow("(id, name, value) values ($this->id, $name, $value)");
  1344. }
  1345. }
  1346. //db
  1347. public function load() {
  1348. $this->LoadFromDB();
  1349. return true;
  1350. }
  1351. protected function LoadFromDB() {
  1352. $res = $this->db->select("id = $this->id");
  1353. while ($row = litepublisher::$db->fetchassoc($res)) {
  1354. $this->data[$row['name']] = $row['value'];
  1355. }
  1356. return true;
  1357. }
  1358. protected function SaveToDB() {
  1359. $db = $this->db;
  1360. $db->delete("id = $this->id");
  1361. foreach ($this->data as $name => $value) {
  1362. if ($name == 'id') continue;
  1363. $name = dbquote($name);
  1364. $value = dbquote($value);
  1365. $this->db->insertrow("(id, name, value) values ($this->id, $name, $value)");
  1366. }
  1367. }
  1368. public static function loaditems(array $items) {
  1369. if (!dbversion || count($items) == 0) return;
  1370. //exclude already loaded items
  1371. if (isset(self::$instances['postmeta'])) {
  1372. $items = array_diff($items, array_keys(self::$instances['postmeta']));
  1373. } else {
  1374. self::$instances['postmeta'] = array();
  1375. }
  1376. if (count($items) == 0) return;
  1377. $instances = &self::$instances['postmeta'];
  1378. $db = litepublisher::$db;
  1379. $db->table = 'postsmeta';
  1380. $res = $db->select(sprintf('id in (%s)', implode(',', $items)));
  1381. while ($row = $db->fetchassoc($res)) {
  1382. $id = (int) $row['id'];
  1383. if (!isset($instances[$id])) {
  1384. $instances[$id] = new self();
  1385. $instances[$id]->data['id'] = $id;
  1386. }
  1387. $instances[$id]->data[$row['name']] = $row['value'];
  1388. }
  1389. return true;
  1390. }
  1391. }//class
  1392. //tags.common.class.php
  1393. class tcommontags extends titems implements itemplate {
  1394. public $factory;
  1395. public $contents;
  1396. public $itemsposts;
  1397. public $PermalinkIndex;
  1398. public $postpropname;
  1399. public $id;
  1400. private $newtitle;
  1401. private $all_loaded;
  1402. private $_idposts;
  1403. protected function create() {
  1404. $this->dbversion = dbversion;
  1405. parent::create();
  1406. $this->addevents('changed', 'onbeforecontent', 'oncontent');
  1407. $this->factory = litepublisher::$classes->getfactory($this);
  1408. $this->data['lite'] = false;
  1409. $this->data['includechilds'] = false;
  1410. $this->data['includeparents'] = false;
  1411. $this->PermalinkIndex = 'category';
  1412. $this->postpropname = 'categories';
  1413. $this->contents = new ttagcontent($this);
  1414. if (!$this->dbversion) $this->data['itemsposts'] = array();
  1415. $this->itemsposts = new titemspostsowner ($this);
  1416. $this->all_loaded = false;
  1417. $this->_idposts = array();
  1418. }
  1419. public function loadall() {
  1420. //prevent double request
  1421. if ($this->all_loaded) return;
  1422. $this->all_loaded = true;
  1423. return parent::loadall();
  1424. }
  1425. public function select($where, $limit) {
  1426. if ($where != '') $where .= ' and ';
  1427. $db = litepublisher::$db;
  1428. $table = $this->thistable;
  1429. $res = $db->query("select $table.*, $db->urlmap.url from $table, $db->urlmap
  1430. where $where $table.idurl = $db->urlmap.id $limit");
  1431. return $this->res2items($res);
  1432. }
  1433. public function load() {
  1434. if (parent::load() && !$this->dbversion) {
  1435. $this->itemsposts->items = &$this->data['itemsposts'];
  1436. }
  1437. }
  1438. public function getsortedcontent(array $tml, $parent, $sortname, $count, $showcount) {
  1439. $sorted = $this->getsorted($parent, $sortname, $count);
  1440. if (count($sorted) == 0) return '';
  1441. $result = '';
  1442. $iconenabled = ! litepublisher::$options->icondisabled;
  1443. $theme = ttheme::i();
  1444. $args = new targs();
  1445. $args->rel = $this->PermalinkIndex;
  1446. $args->parent = $parent;
  1447. foreach($sorted as $id) {
  1448. $item = $this->getitem($id);
  1449. $args->add($item);
  1450. $args->icon = $iconenabled ? $this->geticonlink($id) : '';
  1451. $args->subcount =$showcount ? $theme->parsearg($tml['subcount'],$args) : '';
  1452. $args->subitems = $tml['subitems'] ? $this->getsortedcontent($tml, $id, $sortname, $count, $showcount) : '';
  1453. $result .= $theme->parsearg($tml['item'],$args);
  1454. }
  1455. if ($parent == 0) return $result;
  1456. $args->parent = $parent;
  1457. $args->item = $result;
  1458. return $theme->parsearg($tml['subitems'], $args);
  1459. }
  1460. public function geticonlink($id) {
  1461. $

Large files files are truncated, but you can click here to view the full file