PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/blog/core/packages/quip-2.3.2-pl/modCategory/5196d190c20ee6f49f6a0930f91425b4/0/quip/model/quip/quipthread.class.php

https://bitbucket.org/orchdork10159/dnsman.ly
PHP | 346 lines | 210 code | 32 blank | 104 comment | 49 complexity | 68cd687a33598e24d1d2cce4d6d31e4e MD5 | raw file
  1. <?php
  2. /**
  3. * Quip
  4. *
  5. * Copyright 2010-11 by Shaun McCormick <shaun@modx.com>
  6. *
  7. * This file is part of Quip, a simple commenting component for MODx Revolution.
  8. *
  9. * Quip is free software; you can redistribute it and/or modify it under the
  10. * terms of the GNU General Public License as published by the Free Software
  11. * Foundation; either version 2 of the License, or (at your option) any later
  12. * version.
  13. *
  14. * Quip is distributed in the hope that it will be useful, but WITHOUT ANY
  15. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  16. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * Quip; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
  20. * Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * @package quip
  23. */
  24. /**
  25. * @package quip
  26. */
  27. class quipThread extends xPDOObject {
  28. /** @var modX $xpdo */
  29. public $xpdo;
  30. /**
  31. * Checks whether or not the user has access to the specified permission.
  32. *
  33. * @param string $permission
  34. * @return boolean True if user has permission
  35. */
  36. public function checkPolicy($permission) {
  37. $access = true;
  38. /* first check moderator access */
  39. if ($this->get('moderated')) {
  40. $moderatorGroups = $this->trimArray($this->get('moderator_group'));
  41. $moderators = $this->trimArray($this->get('moderators'));
  42. $inModeratorGroup = !empty($moderatorGroups) && !empty($this->xpdo->user) ? $this->xpdo->user->isMember($moderatorGroups) : false;
  43. $access = $inModeratorGroup || in_array($this->xpdo->user->get('username'),$moderators);
  44. } else {
  45. $access = $this->xpdo->user->isMember('Administrator');
  46. }
  47. /* now check global access */
  48. switch ($permission) {
  49. case 'view':
  50. $access = $this->xpdo->hasPermission('quip.thread_view');
  51. break;
  52. case 'truncate':
  53. $access = $this->xpdo->hasPermission('quip.thread_truncate');
  54. break;
  55. case 'remove':
  56. $access = $this->xpdo->hasPermission('quip.thread_remove');
  57. break;
  58. case 'comment_approve':
  59. $access = $this->xpdo->hasPermission('quip.comment_approve');
  60. break;
  61. case 'comment_remove':
  62. $access = $this->xpdo->hasPermission('quip.comment_approve');
  63. break;
  64. case 'comment_update':
  65. $access = $this->xpdo->hasPermission('quip.comment_approve');
  66. break;
  67. }
  68. return $access;
  69. }
  70. /**
  71. * Trims an array's values to remove whitespaces. If value passed is a string, explodes it first.
  72. *
  73. * @param array $array
  74. * @param string $delimiter
  75. * @return string
  76. */
  77. protected function trimArray($array,$delimiter = ',') {
  78. if (!is_array($array)) {
  79. $array = explode($delimiter,$array);
  80. }
  81. $ret = array();
  82. foreach ($array as $i) {
  83. $ret[] = trim($i);
  84. }
  85. return $ret;
  86. }
  87. /**
  88. * Make the URL of the Quip thread for easy reference
  89. *
  90. * @param int $resource The ID of the resource to make from
  91. * @param array $params Any params to add to the URL
  92. * @param array $options An array of options for URL building
  93. * @return string The created URL
  94. */
  95. public function makeUrl($resource = 0,$params = array(),array $options = array()) {
  96. if (empty($resource)) $resource = $this->get('resource');
  97. if (empty($params)) $params = $this->get('existing_params');
  98. if (empty($params)) $params = array();
  99. if (empty($options['context_key'])) {
  100. $options['context_key'] = $this->get('context_key');
  101. if (empty($options['context_key'])) {
  102. $options['context_key'] = $this->xpdo->context->get('key');
  103. }
  104. }
  105. $scheme= $this->xpdo->context->getOption('scheme','',$options);
  106. return $this->xpdo->makeUrl($resource,$options['context_key'],$params,$scheme);
  107. }
  108. /**
  109. * Sync the thread object
  110. *
  111. * @param array $scriptProperties
  112. * @return bool True if changed
  113. */
  114. public function sync(array $scriptProperties = array()) {
  115. $changed = false;
  116. $scriptProperties['idPrefix'] = $this->xpdo->getOption('idPrefix',$scriptProperties,'qcom');
  117. /* change idPrefix if set */
  118. if (!empty($scriptProperties['idPrefix']) && $this->get('idprefix') != $scriptProperties['idPrefix']) {
  119. $this->set('idprefix',$scriptProperties['idPrefix']);
  120. $changed = true;
  121. }
  122. /* change moderate if diff */
  123. if (isset($scriptProperties['moderate']) && $this->get('moderated') != $scriptProperties['moderate']) {
  124. $this->set('moderated',$scriptProperties['moderate']);
  125. $changed = true;
  126. }
  127. /* change moderators if diff */
  128. if (!empty($scriptProperties['moderators']) && $this->get('moderators') != $scriptProperties['moderators']) {
  129. $this->set('moderators',$scriptProperties['moderators']);
  130. $changed = true;
  131. }
  132. /* change moderatorGroup if diff */
  133. if (!empty($scriptProperties['moderatorGroup']) && $this->get('moderator_group') != $scriptProperties['moderatorGroup']) {
  134. $this->set('moderator_group',$scriptProperties['moderatorGroup']);
  135. $changed = true;
  136. }
  137. /* change notify_emails if diff */
  138. if (!empty($scriptProperties['notifyEmails']) && $this->get('notify_emails') != $scriptProperties['notifyEmails']) {
  139. $this->set('notify_emails',$scriptProperties['notifyEmails']);
  140. $changed = true;
  141. }
  142. if ($changed) {
  143. $this->save();
  144. }
  145. return $changed;
  146. }
  147. /**
  148. * Ensure that the Lexicon is loaded for the modX instance
  149. *
  150. * @return bool True if loaded
  151. */
  152. protected function _loadLexicon() {
  153. if (!$this->xpdo->lexicon) {
  154. $this->xpdo->lexicon = $this->xpdo->getService('lexicon','modLexicon');
  155. if (empty($this->xpdo->lexicon)) {
  156. $this->xpdo->log(xPDO::LOG_LEVEL_ERROR,'[Quip] Could not load MODx lexicon.');
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. /**
  163. * Gets an array of emails for all moderators of this thread
  164. *
  165. * @return array
  166. */
  167. public function getModeratorEmails() {
  168. $moderatorNames = $this->get('moderators');
  169. $moderatorNames = explode(',',$moderatorNames);
  170. $moderators = array();
  171. foreach ($moderatorNames as $name) {
  172. $c = $this->xpdo->newQuery('modUser');
  173. $c->innerJoin('modUserProfile','Profile');
  174. $c->select(array('modUser.id','Profile.email'));
  175. $c->where(array('username' => $name));
  176. $user = $this->xpdo->getObject('modUser',$c);
  177. if ($user) {
  178. $moderators[] = $user->get('email');
  179. }
  180. }
  181. /* now get usergroup moderators */
  182. $moderatorGroup = $this->get('moderator_group');
  183. $c = $this->xpdo->newQuery('modUserProfile');
  184. $c->innerJoin('modUser','User');
  185. $c->innerJoin('modUserGroupMember','UserGroupMembers','User.id = UserGroupMembers.member');
  186. $c->innerJoin('modUserGroup','UserGroup','UserGroup.id = UserGroupMembers.user_group');
  187. $c->where(array(
  188. 'UserGroup.name' => $moderatorGroup,
  189. ));
  190. $members = $this->xpdo->getCollection('modUserProfile',$c);
  191. foreach ($members as $member) {
  192. $email = $member->get('email');
  193. if (!empty($email)) array_push($moderators,$email);
  194. }
  195. $moderators = array_unique($moderators);
  196. return $moderators;
  197. }
  198. /**
  199. * Sends notification to all watchers of this thread saying a new post has been made.
  200. *
  201. * @param quipComment $comment A reference to the actual comment
  202. * @return boolean True if successful
  203. */
  204. public function notify(quipComment &$comment) {
  205. if (!$this->_loadLexicon()) return false;
  206. $this->xpdo->lexicon->load('quip:emails');
  207. /* get the poster's email address */
  208. $posterEmail = false;
  209. $user = $comment->getOne('Author');
  210. if ($user) {
  211. $profile = $user->getOne('Profile');
  212. if ($profile) {
  213. $posterEmail = $profile->get('email');
  214. }
  215. }
  216. /* get email body/subject */
  217. $properties = $comment->toArray();
  218. $properties['url'] = $comment->makeUrl('',array(),array('scheme' => 'full'));
  219. $body = $this->xpdo->lexicon('quip.email_notify',$properties);
  220. $subject = $this->xpdo->lexicon('quip.email_notify_subject');
  221. /* send notifications */
  222. $success = true;
  223. $notifyEmails = $this->get('notify_emails');
  224. $emails = explode(',',$notifyEmails);
  225. /* send notifications to notify_emails subjects */
  226. if (!empty($emails)) {
  227. $this->sendEmail($subject,$body,$emails);
  228. }
  229. /* now send to notified users */
  230. $notifiees = $this->getMany('Notifications');
  231. /** @var quipCommentNotify $notification */
  232. foreach ($notifiees as $notification) {
  233. $email = $notification->get('email');
  234. /* remove invalid emails */
  235. if (empty($email) || strpos($email,'@') == false) {
  236. $notification->remove();
  237. continue;
  238. }
  239. /* don't notify the poster, since they posted the comment. */
  240. if ($posterEmail == $email) {
  241. continue;
  242. }
  243. $notification->send($comment,$properties);
  244. }
  245. return $success;
  246. }
  247. /**
  248. * Sends an email for this thread
  249. * @param string $subject
  250. * @param string $body
  251. * @param string $to
  252. * @return bool
  253. */
  254. protected function sendEmail($subject,$body,$to) {
  255. if (!$this->_loadLexicon()) return false;
  256. $this->xpdo->lexicon->load('quip:emails');
  257. $this->xpdo->getService('mail', 'mail.modPHPMailer');
  258. if (!$this->xpdo->mail) return false;
  259. $emailFrom = $this->xpdo->context->getOption('quip.emailsFrom',$this->xpdo->context->getOption('emailsender'));
  260. $emailReplyTo = $this->xpdo->context->getOption('quip.emailsReplyTo',$this->xpdo->context->getOption('emailsender'));
  261. /* allow multiple to addresses */
  262. if (!is_array($to)) {
  263. $to = explode(',',$to);
  264. }
  265. $success = false;
  266. foreach ($to as $emailAddress) {
  267. if (empty($emailAddress) || strpos($emailAddress,'@') == false) continue;
  268. $this->xpdo->mail->set(modMail::MAIL_BODY,$body);
  269. $this->xpdo->mail->set(modMail::MAIL_FROM,$emailFrom);
  270. $this->xpdo->mail->set(modMail::MAIL_FROM_NAME,$this->xpdo->context->getOption('quip.emails_from_name','Quip'));
  271. $this->xpdo->mail->set(modMail::MAIL_SENDER,$emailFrom);
  272. $this->xpdo->mail->set(modMail::MAIL_SUBJECT,$subject);
  273. $this->xpdo->mail->address('to',$emailAddress);
  274. $this->xpdo->mail->address('reply-to',$emailReplyTo);
  275. $this->xpdo->mail->setHTML(true);
  276. $success = $this->xpdo->mail->send();
  277. $this->xpdo->mail->reset();
  278. }
  279. return $success;
  280. }
  281. /**
  282. * Checks to see if the thread has been auto-closed after a specified number of days.
  283. *
  284. * @param integer $closeAfter The number of days to close the thread after.
  285. * @return boolean True if still open
  286. */
  287. public function checkIfStillOpen($closeAfter = 14) {
  288. if (empty($closeAfter)) return true;
  289. return ((time() - strtotime($this->get('createdon'))) / 60 / 60 / 24) <= $closeAfter;
  290. }
  291. /**
  292. * Truncates a thread.
  293. * @return boolean
  294. */
  295. public function truncate() {
  296. if (!$this->checkPolicy('truncate')) return false;
  297. $c = $this->xpdo->newQuery('quipComment');
  298. $c->where(array(
  299. 'thread' => $this->get('name'),
  300. ));
  301. $comments = $this->xpdo->getCollection('quipComment',$c);
  302. $truncated = true;
  303. /** @var quipComment $comment */
  304. foreach ($comments as $comment) {
  305. $comment->set('deleted',true);
  306. $comment->set('deletedon',strftime('%Y-%m-%d %H:%M:%S'));
  307. if ($this->xpdo instanceof modX) {
  308. $comment->set('deletedby',$this->xpdo->user->get('id'));
  309. }
  310. $truncated = $comment->save();
  311. }
  312. return $truncated;
  313. }
  314. }