PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/comments.class.files.php

http://litepublisher.googlecode.com/
PHP | 464 lines | 355 code | 71 blank | 38 comment | 59 complexity | 031ffcfb0b097c2379119ced17db9765 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-3.0
  1. <?php
  2. /**
  3. * Lite Publisher
  4. * Copyright (C) 2010, 2012 Vladimir Yushko http://litepublisher.com/
  5. * Dual licensed under the MIT (mit.txt)
  6. * and GPL (gpl.txt) licenses.
  7. **/
  8. class tcomments extends titems {
  9. public $pid;
  10. private $rawitems;
  11. private $holditems;
  12. private static $instances;
  13. public static function i($pid = 0) {
  14. $pid = (int) $pid;
  15. if (!isset(self::$instances)) self::$instances = array();
  16. if (isset(self::$instances[$pid])) return self::$instances[$pid];
  17. $self = litepublisher::$classes->newinstance(__class__);
  18. self::$instances[$pid] = $self;
  19. $self->pid = $pid;
  20. $self->load();
  21. return $self;
  22. }
  23. protected function create() {
  24. parent::create();
  25. $this->addevents('edited');
  26. }
  27. public function getcomment($id) {
  28. $result = new tcomment($this);
  29. $result->id = $id;
  30. if (!isset($this->items[$id]) && isset($this->hold->items[$id])) $result->owner = $this->hold;
  31. return $result;
  32. }
  33. public function getbasename() {
  34. return 'posts'. DIRECTORY_SEPARATOR . $this->pid . DIRECTORY_SEPARATOR . 'comments';
  35. }
  36. public function getraw() {
  37. if (!isset($this->rawitems)) {
  38. $this->rawitems = new trawcomments($this);
  39. }
  40. return $this->rawitems;
  41. }
  42. public function gethold() {
  43. if (!isset($this->holditems)) {
  44. $this->holditems = new tholdcomments($this);
  45. }
  46. return $this->holditems;
  47. }
  48. public function getapprovedcount() {
  49. return count($this->items);
  50. }
  51. public function add($author, $content, $status, $ip) {
  52. $filter = tcontentfilter::i();
  53. $item = array(
  54. 'author' => $author,
  55. 'posted' => time(),
  56. 'content' => $filter->filtercomment($content)
  57. );
  58. if ($status == 'approved') {
  59. $this->items[++$this->autoid] = $item;
  60. } else {
  61. $this->hold->items[++$this->autoid] = $item;
  62. $this->hold->save();
  63. }
  64. $this->save();
  65. $this->raw->add($this->autoid, $content, $ip);
  66. $this->added($this->autoid);
  67. return $this->autoid;
  68. }
  69. public function edit($id, $author, $content) {
  70. if (isset($this->items[$id])) {
  71. $item = &$this->items[$id];
  72. $approved = true;
  73. } elseif (isset($this->hold->items[$id])) {
  74. $item = &$this->hold->items[$id];
  75. $approved = false;
  76. } else {
  77. return false;
  78. }
  79. $filter = tcontentfilter::i();
  80. $item['author'] = $author;
  81. $item['content'] = $filter->filtercomment($content);
  82. if ($approved) {
  83. $this->save();
  84. } else {
  85. $this->hold->save();
  86. }
  87. $this->raw->items[$id]['content'] = $content;
  88. $this->raw->save();
  89. $this->edited($id);
  90. return true;
  91. }
  92. public function delete($id) {
  93. if (isset($this->items[$id])) {
  94. $author = $this->items[$id]['author'];
  95. unset($this->items[$id]);
  96. $this->save();
  97. } else {
  98. if (!isset($this->hold->items[$id])) return false;
  99. $author = $this->hold->items[$id]['author'];
  100. unset($this->hold->items[$id]);
  101. $this->hold->save();
  102. }
  103. $this->raw->delete($id);
  104. $this->deleteauthor($author);
  105. $this->deleted($id);
  106. return true;
  107. }
  108. public function deleteauthor($author) {
  109. foreach ($this->items as $id => $item) {
  110. if ($author == $item['author']) return;
  111. }
  112. foreach ($this->hold->items as $id => $item) {
  113. if ($author == $item['author']) return;
  114. }
  115. //?????? ?? ?????
  116. $comusers = tcomusers::i($this->pid);
  117. $comusers->delete($author);
  118. }
  119. public function setstatus($id, $status) {
  120. if ($status == 'approved') {
  121. return $this->approve($id);
  122. } else {
  123. return $this->sethold($id);
  124. }
  125. }
  126. public function sethold($id) {
  127. if (!isset($this->items[$id])) return false;
  128. $item = $this->items[$id];
  129. unset($this->items[$id]);
  130. $this->save();
  131. $this->hold->items[$id] = $item;
  132. $this->hold->save();
  133. return true;
  134. }
  135. public function approve($id) {
  136. if (!isset($this->hold->items[$id])) return false;
  137. $this->items[$id] = $this->hold->items[$id];
  138. $this->save();
  139. unset($this->hold->items[$id]);
  140. $this->hold->save();
  141. return true;
  142. }
  143. /*
  144. public function sort() {
  145. $Result[$id] = $item['posted'];
  146. asort($Result);
  147. return array_keys($Result);
  148. }
  149. public function insert($author, $content, $ip, $posted, $status) {
  150. $filter = tcontentfilter::i();
  151. $item = array(
  152. 'author' => $author,
  153. 'posted' => $posted,
  154. 'content' => $filter->filtercomment($content)
  155. );
  156. if ($status == 'approved') {
  157. $this->items[++$this->autoid] = $item;
  158. } else {
  159. $this->hold->items[++$this->autoid] = $item;
  160. $this->hold->save();
  161. }
  162. $this->save();
  163. $this->raw->add($this->autoid, $content, $ip);
  164. return $this->autoid;
  165. }
  166. */
  167. public function getholdcontent($idauthor) {
  168. if (litepublisher::$options->admincookie) return '';
  169. return $this->hold->dogetcontent(true, $idauthor);
  170. }
  171. public function getmoderator() {
  172. if (!litepublisher::$options->admincookie) return false;
  173. if (litepublisher::$options->group == 'admin') return true;
  174. $groups = tusergroups::i();
  175. return $groups->hasrigt(litepublisher::$options->group, 'moderator');
  176. }
  177. public function getcontent() {
  178. $result = $this->dogetcontent(false, 0);
  179. if (!$this->moderator) return $result;
  180. $theme = ttheme::i();
  181. $lang = tlocal::admin('comment');
  182. $post = tpost::i($this->pid);
  183. if ($post->commentpages == litepublisher::$urlmap->page) {
  184. $result .= $this->hold->dogetcontent(true, 0);
  185. } else {
  186. //add empty list of hold comments
  187. $args = targs::i();
  188. $args->comment = '';
  189. $result .= $theme->parsearg($theme->templates['content.post.templatecomments.holdcomments'], $args);
  190. }
  191. $args = targs::i();
  192. $args->comments = $result;
  193. return $theme->parsearg($theme->templates['content.post.templatecomments.moderateform'], $args);
  194. }
  195. public function dogetcontent($hold, $idauthor) {
  196. $result = '';
  197. $post = tpost::i($this->pid);
  198. $from = 0;
  199. $items = array_keys($this->items);
  200. if (!$hold) {
  201. if (litepublisher::$options->commentpages ) {
  202. $page = min(litepublisher::$urlmap->page, $post->commentpages );
  203. if (litepublisher::$options->comments_invert_order) $page = max(0, $post->commentpages - $page) + 1;
  204. $from = ($page - 1) * litepublisher::$options->commentsperpage;
  205. $items = array_slice($items, $from, litepublisher::$options->commentsperpage, true);
  206. }
  207. }
  208. $ismoder = $this->moderator;
  209. $theme = ttheme::i();
  210. $args = targs::i();
  211. if (count($items) > 0) {
  212. $args->from = $from;
  213. $comment = new tcomment($this);
  214. ttheme::$vars['comment'] = $comment;
  215. if ($hold) $comment->status = 'hold';
  216. $lang = tlocal::i('comment');
  217. if ($ismoder) {
  218. tlocal::usefile('admin');
  219. $moderate =$theme->templates['content.post.templatecomments.comments.comment.moderate'];
  220. } else {
  221. $moderate = '';
  222. }
  223. $tmlcomment= $theme->gettag('content.post.templatecomments.comments.comment');;
  224. $tml = strtr((string) $tmlcomment, array(
  225. '$moderate' => $moderate,
  226. '$quotebuttons' => $post->commentsenabled ? $tmlcomment->quotebuttons : ''
  227. ));
  228. $index = $from;
  229. $class1 = $tmlcomment->class1;
  230. $class2 = $tmlcomment->class2;
  231. foreach ($items as $id) {
  232. //one method for approved and hold comments
  233. if (!litepublisher::$options->admincookie && $hold) {
  234. if ($idauthor != $this->items[$id]['author']) continue;
  235. }
  236. $comment->id = $id;
  237. $args->index = ++$index;
  238. $args->class = ($index % 2) == 0 ? $class1 : $class2;
  239. $result .= $theme->parsearg($tml, $args);
  240. }
  241. }//if count
  242. unset(ttheme::$vars['comment']);
  243. if (!$ismoder) {
  244. if ($result == '') return '';
  245. }
  246. if ($hold) {
  247. $tml = $theme->templates['content.post.templatecomments.holdcomments'];
  248. } else {
  249. $tml = $theme->templates['content.post.templatecomments.comments'];
  250. }
  251. $args->from = $from + 1;
  252. $args->comment = $result;
  253. return $theme->parsearg($tml, $args);
  254. }
  255. }//class
  256. class tholdcomments extends tcomments {
  257. public $owner;
  258. public $idauthor;
  259. public static function i($pid = 0) {
  260. $owner = tcomments::i($pid);
  261. return $owner->hold;
  262. }
  263. public function __construct($owner) {
  264. $this->owner = $owner;
  265. parent::__construct();
  266. $this->pid = $owner->pid;
  267. }
  268. public function getbasename() {
  269. return $this->owner->getbasename() . '.hold';
  270. }
  271. public function delete($id) {
  272. if (!isset($this->items[$id])) return false;
  273. $author= $this->items[$id]['author'];
  274. unset($this->items[$id]);
  275. $this->save();
  276. $this->owner->raw->delete($id);
  277. $this->owner->deleteauthor($author);
  278. $this->deleted($id);
  279. }
  280. }//class
  281. class trawcomments extends titems {
  282. public $owner;
  283. public function getbasename() {
  284. return 'posts'. DIRECTORY_SEPARATOR . $this->owner->pid . DIRECTORY_SEPARATOR . 'comments.raw';
  285. }
  286. public function __construct($owner) {
  287. $this->owner = $owner;
  288. parent::__construct();
  289. }
  290. public function add($id, $content, $ip) {
  291. $this->items[$id] = array(
  292. 'content' => $content,
  293. 'ip' => $ip
  294. );
  295. $this->save();
  296. }
  297. }//class
  298. //wrapper for simple acces to single comment
  299. class TComment {
  300. public $id;
  301. public $owner;
  302. public $status;
  303. public function __construct($owner = null) {
  304. $this->owner = $owner;
  305. $this->status = 'approved';
  306. }
  307. public function __get($name) {
  308. if (method_exists($this,$get = "get$name")) {
  309. return $this->$get();
  310. }
  311. return $this->owner->items[$this->id][$name];
  312. }
  313. public function __set($name, $value) {
  314. if ($name == 'content') {
  315. $this->setcontent($value);
  316. } else {
  317. $this->owner->items[$this->id][$name] = $value;
  318. }
  319. }
  320. public function save() {
  321. $this->owner->save();
  322. }
  323. private function setcontent($value) {
  324. $filter = tcontentfilter::i();
  325. $this->owner->items[$this->id]['content'] = $filter->filtercomment($value);
  326. $this->save();
  327. $this->owner->raw->items[$this->id]['content'] = $value;
  328. $this->owner->raw->save();
  329. }
  330. private function getauthoritem() {
  331. $comusers = tcomusers::i($this->owner->pid);
  332. return $comusers->getitem($this->author);
  333. }
  334. public function getname() {
  335. return $this->authoritem['name'];
  336. }
  337. public function getemail() {
  338. return $this->authoritem['email'];
  339. }
  340. public function getmd5email() {
  341. return md5($this->authoritem['url']);
  342. }
  343. public function getgravatar() {
  344. return sprintf('<img class="avatar photo" src="http://www.gravatar.com/avatar/%s?s=32&amp;r=g&amp;d=wavatar" title="%2$s" alt="%2$s"/>', $this->getmd5email(), $this->name);
  345. }
  346. public function getwebsite() {
  347. return $this->authoritem['url'];
  348. }
  349. public function getauthorlink() {
  350. $idpost = $this->owner->pid;
  351. $comusers = tcomusers::i($idpost);
  352. $item = $comusers->getitem($this->author);
  353. $name = $item['name'];
  354. if (empty($url)) return $name;
  355. $manager = tcommentmanager::i();
  356. if ($manager->hidelink) return $name;
  357. $rel = $manager->nofollow ? 'rel="nofollow"' : '';
  358. if ($manager->redir) {
  359. return "<a $rel href=\"" . litepublisher::$site->url . "/comusers.htm" . litepublisher::$site->q . "id=$this->author&post=$idpost\">$name</a>";
  360. } else {
  361. return "<a $rel href=\"$url\">$name</a>";
  362. }
  363. }
  364. public function getdate() {
  365. $theme = ttheme::i();
  366. return tlocal::date($this->posted, $theme->templates['content.post.templatecomments.comments.comment.date']);
  367. }
  368. public function getlocalstatus() {
  369. return tlocal::get('commentstatus', $this->status);
  370. }
  371. public function gettime() {
  372. return date('H:i', $this->posted);
  373. }
  374. public function geturl() {
  375. $post = tpost::i($this->owner->pid);
  376. return "$post->link#comment-$this->id";
  377. }
  378. public function getposttitle() {
  379. $post = tpost::i($this->owner->pid);
  380. return $post->title;
  381. }
  382. public function getrawcontent() {
  383. return $this->owner->raw->items[$this->id]['content'];
  384. }
  385. public function getip() {
  386. return $this->owner->raw->items[$this->id]['ip'];
  387. }
  388. }//class
  389. ?>