PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/comments.manager.class.php

http://litepublisher.googlecode.com/
PHP | 139 lines | 111 code | 20 blank | 8 comment | 21 complexity | 5bf7de871bcedd06ef292db3b1df36ea 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 tcommentmanager extends tevents_storage {
  9. public static function i() {
  10. return getinstance(__class__);
  11. }
  12. protected function create() {
  13. parent::create();
  14. $this->basename = 'commentmanager';
  15. $this->addevents('onchanged', 'approved', 'comuseradded', 'is_spamer', 'oncreatestatus');
  16. }
  17. public function getcount() {
  18. litepublisher::$db->table = 'comments';
  19. return litepublisher::$db->getcount();
  20. }
  21. public function addcomuser($name, $email, $website, $ip) {
  22. $users = tusers::i();
  23. $id = $users->add(array(
  24. 'email' => strtolower(trim($email)),
  25. 'name' => $name,
  26. 'website' => tcontentfilter::clean_website($website),
  27. 'status' => 'comuser',
  28. 'idgroups' => 'commentator'
  29. ));
  30. if ($id) {
  31. $this->comuseradded($id);
  32. }
  33. return $id;
  34. }
  35. public function add($idpost, $idauthor, $content, $ip) {
  36. $status = $this->createstatus($idpost, $idauthor, $content, $ip);
  37. if (!$status) return false;
  38. $comments = tcomments::i();
  39. return $comments->add($idpost, $idauthor, $content, $status, $ip);
  40. }
  41. public function reply($idparent, $content) {
  42. $idauthor = 1; //admin
  43. $comments = tcomments::i();
  44. $idpost = $comments->getvalue($idparent, 'post');
  45. $id = $comments->add($idpost, $idauthor, $content, 'approved', '');
  46. $comments->setvalue($id, 'parent', $idparent);
  47. return $id;
  48. }
  49. public function changed($id) {
  50. $comments = tcomments::i();
  51. $idpost = $comments->getvalue($id, 'post');
  52. $count = $comments->db->getcount("post = $idpost and status = 'approved'");
  53. $comments->getdb('posts')->setvalue($idpost, 'commentscount', $count);
  54. if (litepublisher::$options->commentspull) tcommentspull::i()->set($idpost, $count);
  55. //update trust
  56. try {
  57. $idauthor = $comments->getvalue($id, 'author');
  58. $users = tusers::i();
  59. if ($this->trustlevel > intval($users->getvalue($idauthor, 'trust'))) {
  60. $trust = $comments->db->getcount("author = $idauthor and status = 'approved' limit " . ($this->trustlevel + 1));
  61. $users->setvalue($idauthor, 'trust', $trust);
  62. }
  63. } catch (Exception $e) {
  64. }
  65. $this->onchanged($id);
  66. }
  67. public function sendmail($id) {
  68. if ($this->sendnotification) {
  69. litepublisher::$urlmap->onclose($this, 'send_mail', $id);
  70. }
  71. }
  72. public function send_mail($id) {
  73. $comments = tcomments::i();
  74. $comment = $comments->getcomment($id);
  75. //ignore admin comments
  76. if ($comment->author == 1) return;
  77. ttheme::$vars['comment'] = $comment;
  78. $args = new targs();
  79. $adminurl = litepublisher::$site->url . '/admin/comments/'. litepublisher::$site->q . "id=$id";
  80. $ref = md5(litepublisher::$secret . $adminurl);
  81. $adminurl .= "&ref=$ref&action";
  82. $args->adminurl = $adminurl;
  83. tlocal::usefile('mail');
  84. $lang = tlocal::i('mailcomments');
  85. $theme = ttheme::i();
  86. $subject = $theme->parsearg($lang->subject, $args);
  87. $body = $theme->parsearg($lang->body, $args);
  88. return tmailer::sendtoadmin($subject, $body, false);
  89. }
  90. public function createstatus($idpost, $idauthor, $content, $ip) {
  91. $status = $this->oncreatestatus ($idpost, $idauthor, $content, $ip);
  92. if (false === $status) return false;
  93. if ($status == 'spam') return false;
  94. if (($status == 'hold') || ($status == 'approved')) return $status;
  95. if (!$this->filterstatus) return $this->defstatus;
  96. if ($this->defstatus == 'approved') return 'approved';
  97. if ($this->trusted($idauthor)) return 'approved';
  98. return 'hold';
  99. }
  100. public function canadd($idauthor) {
  101. return !$this->is_spamer($idauthor);
  102. }
  103. public function is_duplicate($idpost, $content) {
  104. $comments = tcomments::i($idpost);
  105. $content = trim($content);
  106. $hash = basemd5($content);
  107. return $comments->raw->findid("hash = '$hash'");
  108. }
  109. public function request($arg) {
  110. $id = isset($_GET['id']) ? (int) $_GET['id'] : 1;
  111. $users = tusers::i();
  112. if (!$users->itemexists($id)) return "<?php litepublisher::$urlmap->redir('/');";
  113. $item = $users->getitem($id);
  114. $url = $item['website'];
  115. if (!strpos($url, '.')) $url = litepublisher::$site->url . '/';
  116. if (!strbegin($url, 'http://')) $url = 'http://' . $url;
  117. return "<?php litepublisher::$urlmap->redir('$url');";
  118. }
  119. }//class