PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/core/packages/quip-2.3.1-pl/modCategory/c5d21c5476da1575228a61f46e2e9263/0/quip/elements/snippets/snippet.quipreply.php

https://bitbucket.org/orchdork10159/dnsman.ly
PHP | 233 lines | 160 code | 20 blank | 53 comment | 54 complexity | ba4df24e4b87fb3d6d4d9eb3b795b4d3 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. * Displays a reply form for a thread
  26. *
  27. * @var modX $modx
  28. * @var array $scriptProperties
  29. * @var Quip $quip
  30. *
  31. * @name QuipReply
  32. * @author Shaun McCormick <shaun@modx.com>
  33. * @package quip
  34. */
  35. $quip = $modx->getService('quip','Quip',$modx->getOption('quip.core_path',null,$modx->getOption('core_path').'components/quip/').'model/quip/',$scriptProperties);
  36. if (!($quip instanceof Quip)) return '';
  37. $quip->initialize($modx->context->get('key'));
  38. $controller = $quip->loadController('ThreadReply');
  39. $output = $controller->run($scriptProperties);
  40. return $output;
  41. /* get thread */
  42. $thread = $modx->getOption('quip_thread',$_REQUEST,$modx->getOption('thread',$scriptProperties,''));
  43. if (empty($thread)) return '';
  44. $thread = $modx->getObject('quipThread',array('name' => $thread));
  45. if (!$thread) return '';
  46. /* sync properties with thread row values */
  47. $thread->sync($scriptProperties);
  48. $ps = $thread->get('quipreply_call_params');
  49. if (!empty($ps)) {
  50. $diff = array_diff_assoc($ps,$scriptProperties);
  51. if (empty($diff)) $diff = array_diff_assoc($scriptProperties,$ps);
  52. }
  53. if (empty($_REQUEST['quip_thread']) && (!empty($diff) || empty($ps))) { /* only sync call params if not on threaded reply page */
  54. $thread->set('quipreply_call_params',$scriptProperties);
  55. $thread->save();
  56. }
  57. /* if in threaded reply page, get the original passing values to QuipReply in the thread's main page and use those */
  58. if (!empty($_REQUEST['quip_thread']) && is_array($ps) && !empty($ps)) $scriptProperties = array_merge($scriptProperties,$ps);
  59. unset($ps,$diff);
  60. /* setup default properties */
  61. $requireAuth = $modx->getOption('requireAuth',$scriptProperties,false);
  62. $requireUsergroups = $modx->getOption('requireUsergroups',$scriptProperties,false);
  63. $addCommentTpl = $modx->getOption('tplAddComment',$scriptProperties,'quipAddComment');
  64. $loginToCommentTpl = $modx->getOption('tplLoginToComment',$scriptProperties,'quipLoginToComment');
  65. $previewTpl = $modx->getOption('tplPreview',$scriptProperties,'quipPreviewComment');
  66. $closeAfter = $modx->getOption('closeAfter',$scriptProperties,14);
  67. $requirePreview = $modx->getOption('requirePreview',$scriptProperties,false);
  68. $previewAction = $modx->getOption('previewAction',$scriptProperties,'quip-preview');
  69. $postAction = $modx->getOption('postAction',$scriptProperties,'quip-post');
  70. $allowedTags = $modx->getOption('quip.allowed_tags',$scriptProperties,'<br><b><i>');
  71. $preHooks = $modx->getOption('preHooks',$scriptProperties,'');
  72. $postHooks = $modx->getOption('postHooks',$scriptProperties,'');
  73. $unsubscribeAction = $modx->getOption('unsubscribeAction',$scriptProperties,'quip_unsubscribe');
  74. /* get parent and auth */
  75. $parent = $modx->getOption('quip_parent',$_REQUEST,$modx->getOption('parent',$scriptProperties,0));
  76. $hasAuth = $modx->user->hasSessionContext($modx->context->get('key')) || $modx->getOption('debug',$scriptProperties,false) || empty($requireAuth);
  77. if (!empty($requireUsergroups)) {
  78. $requireUsergroups = explode(',',$requireUsergroups);
  79. $hasAuth = $modx->user->isMember($requireUsergroups);
  80. }
  81. /* setup default placeholders */
  82. $placeholders = array();
  83. $p = $modx->request->getParameters();
  84. unset($p['reported'],$p['quip_approved']);
  85. $placeholders['parent'] = $parent;
  86. $placeholders['thread'] = $thread->get('name');
  87. $placeholders['url'] = $modx->makeUrl($modx->resource->get('id'),'',$p);
  88. $placeholders['idprefix'] = $thread->get('idprefix');
  89. /* handle POST */
  90. $fields = array();
  91. $hasPreview = false;
  92. if (!empty($_POST)) {
  93. foreach ($_POST as $k => $v) {
  94. $fields[$k] = str_replace(array('[',']'),array('&#91;','&#93;'),$v);
  95. }
  96. $fields['name'] = strip_tags($fields['name']);
  97. $fields['email'] = strip_tags($fields['email']);
  98. $fields['website'] = strip_tags($fields['website']);
  99. /* verify a message was posted */
  100. if (empty($fields['comment'])) $errors['comment'] = $modx->lexicon('quip.message_err_ns');
  101. if (empty($fields['name'])) $errors['name'] = $modx->lexicon('quip.name_err_ns');
  102. if (empty($fields['email'])) $errors['email'] = $modx->lexicon('quip.email_err_ns');
  103. if (!empty($_POST[$postAction]) && empty($errors)) {
  104. $comment = include_once $quip->config['processorsPath'].'web/comment/create.php';
  105. if (is_object($comment) && $comment instanceof quipComment) {
  106. $params = $modx->request->getParameters();
  107. unset($params[$postAction],$params['quip_parent'],$params['quip_thread']);
  108. $params['quip_approved'] = $comment->get('approved') ? 1 : 0;
  109. $url = $comment->makeUrl('',$params);
  110. /* if not approved, remove # and replace with success message #
  111. * since comment is not yet visible
  112. */
  113. if (!$comment->get('approved')) {
  114. $url = str_replace('#'.$thread->get('idprefix').$comment->get('id'),'#quip-success-'.$thread->get('idprefix'),$url);
  115. }
  116. $modx->sendRedirect($url);
  117. } else if (is_array($comment)) {
  118. $errors = array_merge($errors,$comment);
  119. }
  120. $fields[$previewAction] = true;
  121. }
  122. /* handle preview */
  123. else if (!empty($fields[$previewAction]) && empty($errors)) {
  124. $errors = include_once $quip->config['processorsPath'].'web/comment/preview.php';
  125. }
  126. if (!empty($errors)) {
  127. $placeholders['error'] = implode("<br />\n",$errors);
  128. foreach ($errors as $k => $v) {
  129. $placeholders['error.'.$k] = $v;
  130. }
  131. $placeholders = array_merge($placeholders,$fields);
  132. }
  133. }
  134. /* display moderated success message */
  135. if (isset($_GET['quip_approved']) && $_GET['quip_approved'] == 0) {
  136. $placeholders['successMsg'] = $modx->lexicon('quip.comment_will_be_moderated');
  137. }
  138. /* handle unsubscribing from thread */
  139. if (!empty($_GET[$unsubscribeAction]) && $modx->user->hasSessionContext($modx->context->get('key'))) {
  140. $profile = $modx->user->getOne('Profile');
  141. if ($profile) {
  142. $notify = $modx->getObject('quipCommentNotify',array(
  143. 'email' => $profile->get('email'),
  144. 'thread' => $thread,
  145. ));
  146. if ($notify && $notify->remove()) {
  147. $placeholders['successMsg'] = $modx->lexicon('quip.unsubscribed');
  148. }
  149. }
  150. }
  151. /* if using recaptcha, load recaptcha html if user is not logged in */
  152. $disableRecaptchaWhenLoggedIn = (boolean)$modx->getOption('disableRecaptchaWhenLoggedIn',$scriptProperties,true);
  153. $useRecaptcha = (boolean)$modx->getOption('recaptcha',$scriptProperties,false);
  154. if ($useRecaptcha && !($disableRecaptchaWhenLoggedIn && $hasAuth) && !$hasPreview) {
  155. $recaptcha = $modx->getService('recaptcha','reCaptcha',$quip->config['modelPath'].'recaptcha/');
  156. if ($recaptcha instanceof reCaptcha) {
  157. $recaptchaTheme = $modx->getOption('recaptchaTheme',$scriptProperties,'clean');
  158. $html = $recaptcha->getHtml($recaptchaTheme);
  159. $modx->setPlaceholder('quip.recaptcha_html',$html);
  160. } else {
  161. return $modx->lexicon('quip.recaptcha_err_load');
  162. }
  163. }
  164. /* build reply form */
  165. $replyForm = '';
  166. $stillOpen = $thread->checkIfStillOpen($closeAfter) && !$modx->getOption('closed',$scriptProperties,false);
  167. if ($hasAuth && $stillOpen) {
  168. $phs = array_merge($placeholders,array(
  169. 'username' => $modx->user->get('username'),
  170. ));
  171. $phs['unsubscribe'] = '';
  172. /* prefill fields */
  173. $profile = $modx->user->getOne('Profile');
  174. if ($profile) {
  175. $phs['name'] = !empty($fields['name']) ? $fields['name'] : $profile->get('fullname');
  176. $phs['email'] = !empty($fields['email']) ? $fields['email'] : $profile->get('email');
  177. $phs['website'] = !empty($fields['website']) ? $fields['website'] : $profile->get('website');
  178. /* allow for unsubscribing for logged-in users */
  179. if ($modx->user->hasSessionContext($modx->context->get('key'))) {
  180. $notify = $modx->getObject('quipCommentNotify',array(
  181. 'email' => $profile->get('email'),
  182. 'thread' => $thread,
  183. ));
  184. if ($notify) {
  185. $phs['notifyId'] = $notify->get('id');
  186. $phs['unsubscribe'] = $quip->getChunk('quipUnsubscribe',$phs);
  187. $params = $modx->request->getParameters();
  188. $params[$unsubscribeAction] = 1;
  189. $phs['unsubscribeUrl'] = $modx->makeUrl($modx->resource->get('id'),'',$params);
  190. }
  191. }
  192. }
  193. /* if requirePreview == false, auto-can post */
  194. if (!$requirePreview) {
  195. $phs['can_post'] = true;
  196. }
  197. $phs['post_action'] = $postAction;
  198. $phs['preview_action'] = $previewAction;
  199. $phs['allowed_tags'] = $allowedTags;
  200. $phs['notifyChecked'] = !empty($fields['notify']) ? ' checked="checked"' : '';
  201. $replyForm = $quip->getChunk($addCommentTpl,$phs);
  202. } else if (!$stillOpen) {
  203. $replyForm = $modx->lexicon('quip.thread_autoclosed');
  204. } else {
  205. $replyForm = $quip->getChunk($loginToCommentTpl,$placeholders);
  206. }
  207. /* output or set to placeholder */
  208. $toPlaceholder = $modx->getOption('toPlaceholder',$scriptProperties,false);
  209. if ($toPlaceholder) {
  210. $modx->setPlaceholder($toPlaceholder,$replyForm);
  211. return '';
  212. }
  213. return $replyForm;