source: trunk/content/plugins/comments/comments.php @ 1375

Revision 1375, 36.7 KB checked in by nick_ramsay, 3 years ago (diff)

[Trunk] Hotaru 1.1.3 [Run upgrade script]

Line 
1<?php
2/**
3 * name: Comments
4 * description: Enables logged-in users to comment on posts
5 * version: 1.8
6 * folder: comments
7 * class: Comments
8 * type: comments
9 * requires: sb_base 0.1, users 1.1
10 * hooks: install_plugin, theme_index_top, header_include, admin_header_include_raw, theme_index_main, sb_base_show_post_extra_fields, sb_base_post_show_post, admin_plugin_settings, admin_sidebar_plugin_settings, submit_2_fields, submit_edit_admin_fields, post_delete_post, profile_navigation, , admin_theme_main_stats, breadcrumbs, submit_functions_process_submitted, submit_2_process_submission
11 *
12 * PHP version 5
13 *
14 * LICENSE: Hotaru CMS is free software: you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation, either version 3 of
17 * the License, or (at your option) any later version.
18 *
19 * Hotaru CMS is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 * FITNESS FOR A PARTICULAR PURPOSE.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with Hotaru CMS. If not, see http://www.gnu.org/licenses/.
25 *
26 * @category  Content Management System
27 * @package   HotaruCMS
28 * @author    Nick Ramsay <admin@hotarucms.org>
29 * @copyright Copyright (c) 2009, Hotaru CMS
30 * @license   http://www.gnu.org/copyleft/gpl.html GNU General Public License
31 * @link      http://www.hotarucms.org/
32 */
33
34class Comments
35{
36    /**
37     * Install or Upgrade
38     */
39    public function install_plugin($h)
40    {
41        // ************
42        // PERMISSIONS
43        // ************
44       
45        $site_perms = $h->getDefaultPermissions('all');
46        if (!isset($site_perms['can_comment'])) {
47            $perms['options']['can_comment'] = array('yes', 'no', 'mod');
48            $perms['options']['can_edit_comments'] = array('yes', 'no', 'own');
49            $perms['options']['can_set_comments_pending'] = array('yes', 'no');
50            $perms['options']['can_delete_comments'] = array('yes', 'no');
51            $perms['options']['can_comment_manager_settings'] = array('yes', 'no');
52           
53            $perms['can_comment']['admin'] = 'yes';
54            $perms['can_comment']['supermod'] = 'yes';
55            $perms['can_comment']['moderator'] = 'yes';
56            $perms['can_comment']['member'] = 'yes';
57            $perms['can_comment']['undermod'] = 'mod';
58            $perms['can_comment']['default'] = 'no';
59           
60            $perms['can_edit_comments']['admin'] = 'yes';
61            $perms['can_edit_comments']['supermod'] = 'yes';
62            $perms['can_edit_comments']['moderator'] = 'yes';
63            $perms['can_edit_comments']['member'] = 'own';
64            $perms['can_edit_comments']['undermod'] = 'own';
65            $perms['can_edit_comments']['default'] = 'no';
66           
67            $perms['can_set_comments_pending']['admin'] = 'yes';
68            $perms['can_set_comments_pending']['supermod'] = 'yes';
69            $perms['can_set_comments_pending']['moderator'] = 'yes';
70            $perms['can_set_comments_pending']['default'] = 'no';
71           
72            $perms['can_delete_comments']['admin'] = 'yes';
73            $perms['can_delete_comments']['supermod'] = 'yes';
74            $perms['can_delete_comments']['default'] = 'no';
75           
76            $perms['can_comment_manager_settings']['admin'] = 'yes';
77            $perms['can_comment_manager_settings']['supermod'] = 'yes';
78            $perms['can_comment_manager_settings']['moderator'] = 'yes';
79            $perms['can_comment_manager_settings']['default'] = 'no';
80           
81            $h->updateDefaultPermissions($perms);
82        }
83
84
85        // ************
86        // SETTINGS
87        // ************
88       
89        // Get settings from database if they exist...
90        $comments_settings = $h->getSerializedSettings();
91       
92        // Default settings
93        if (!isset($comments_settings['comment_all_forms'])) { $comments_settings['comment_all_forms'] = "checked"; }
94        if (!isset($comments_settings['comment_voting'])) { $comments_settings['comment_voting'] = ""; }
95        if (!isset($comments_settings['comment_levels'])) { $comments_settings['comment_levels'] = 5; }
96        if (!isset($comments_settings['comment_email'])) { $comments_settings['comment_email'] = SITE_EMAIL; }
97        if (!isset($comments_settings['comment_allowable_tags'])) { $comments_settings['comment_allowable_tags'] = "<b><i><u><a><blockquote><del>"; }
98        if (!isset($comments_settings['comment_set_pending'])) { $comments_settings['comment_set_pending'] = ""; }
99        if (!isset($comments_settings['comment_order'])) { $comments_settings['comment_order'] = 'asc'; }
100        if (!isset($comments_settings['comment_pagination'])) { $comments_settings['comment_pagination'] = ''; }
101        if (!isset($comments_settings['comment_items_per_page'])) { $comments_settings['comment_items_per_page'] = 20; }
102        if (!isset($comments_settings['comment_x_comments'])) { $comments_settings['comment_x_comments'] = 1; }
103        if (!isset($comments_settings['comment_email_notify'])) { $comments_settings['comment_email_notify'] = ""; }
104        if (!isset($comments_settings['comment_email_notify_mods'])) { $comments_settings['comment_email_notify_mods'] = array(); }
105        if (!isset($comments_settings['comment_url_limit'])) { $comments_settings['comment_url_limit'] = 0; }
106        if (!isset($comments_settings['comment_daily_limit'])) { $comments_settings['comment_daily_limit'] = 0; }
107        if (!isset($comments_settings['comment_avatar_size'])) { $comments_settings['comment_avatar_size'] = "16"; }
108        if (!isset($comments_settings['comment_hide'])) { $comments_settings['comment_hide'] = "3"; }
109        if (!isset($comments_settings['comment_bury'])) { $comments_settings['comment_bury'] = "10"; }
110       
111        if ($h->isActive('avatar')) {
112            if (!isset($comments_settings['comment_avatars'])) { $comments_settings['comment_avatars'] = "checked"; }
113        } else {
114            if (!isset($comments_settings['comment_avatars'])) { $comments_settings['comment_avatars'] = ""; }
115        }
116       
117        $h->updateSetting('comments_settings', serialize($comments_settings));
118    }
119   
120   
121    /**
122     * Define table name, include language file and creat global Comments object
123     */
124    public function theme_index_top($h)
125    {
126        // Create a new global object called "comment".
127        require_once(LIBS . 'Comment.php');
128        $h->comment = new Comment();
129       
130        // Get settings from database if they exist...
131        $comments_settings = $h->getSerializedSettings();
132   
133        // Assign settings to class member
134        $h->comment->avatars = $comments_settings['comment_avatars'];
135        $h->comment->avatarSize = $comments_settings['comment_avatar_size'];
136        $h->comment->voting = $comments_settings['comment_voting'];
137        $h->comment->email = $comments_settings['comment_email'];
138        $h->comment->allowableTags = $comments_settings['comment_allowable_tags'];
139        $h->comment->levels = $comments_settings['comment_levels'];
140        $h->comment->setPending = $comments_settings['comment_set_pending'];
141        $h->comment->allForms = $comments_settings['comment_all_forms'];
142        $h->vars['comment_hide'] = $comments_settings['comment_hide'];
143       
144       
145        if ($h->pageName == 'rss_comments') {
146            $this->rssFeed($h);
147            return true;
148        }
149       
150        if ($h->pageName == 'comments')
151        {
152            // set current comment and responses to pending:
153            if ($h->cage->get->getAlpha('action') == 'setpending') {
154           
155                // before setting pending, we need to be certain this user has permission:
156                if ($h->currentUser->loggedIn && $h->currentUser->getPermission('can_set_comments_pending') == 'yes') {
157                    $cid = $h->cage->get->testInt('cid'); // comment id
158                    $comment = $h->comment->getComment($h, $cid);
159                    $h->comment->readComment($h, $comment); // read comment
160                    $h->comment->status = 'pending'; // set to pending
161                    $h->comment->editComment($h);  // update this comment
162
163                    $h->comment->postId = $h->cage->get->testInt('pid');  // post id
164                    $h->comment->setPendingCommentTree($h,$cid);   // set all responses to 'pending', too.
165                   
166                    // redirect back to thread:
167                    $h->post = new Post();
168                    $h->readPost($h->comment->postId);
169                    header("Location: " . $h->url(array('page'=>$h->post->id)));    // Go to the post
170                    die();
171                }
172            }
173           
174            // delete current comment and responses:
175            if ($h->cage->get->getAlpha('action') == 'delete') {
176           
177                // before deleting a comment, we need to be certain this user has permission:
178                if ($h->currentUser->loggedIn && $h->currentUser->getPermission('can_delete_comments') == 'yes') {
179                    $cid = $h->cage->get->testInt('cid'); // comment id
180                    $comment = $h->comment->getComment($h, $cid);
181                    $h->comment->readComment($h, $comment); // read comment
182                   
183                    $h->pluginHook('comments_delete_comment');
184                   
185                    $h->comment->deleteComment($h, $cid); // delete this comment
186                    $h->comment->deleteCommentTree($h, $cid);   // delete all responses, too.
187                   
188                    $h->clearCache('html_cache', false); // clear HTML cache to refresh Comments and Activity widgets
189                   
190                    $h->comment->postId = $h->cage->get->testInt('pid');  // post id
191                   
192                    // redirect back to thread:
193                    $h->readPost($h->comment->postId);
194                    header("Location: " . $h->url(array('page'=>$h->comment->postId)));    // Go to the post
195                    die();
196                }
197            }
198           
199            // FOR THE COMMENTS PAGE:
200            $h->pageTitle = $h->lang['comments'];
201            if ($h->cage->get->keyExists('user')) {
202                $h->pageTitle .= '[delimiter]' . $h->cage->get->testUsername('user');
203            }
204        }
205
206        // Is the comment form open on this thread?
207        $h->comment->thisForm = $h->comment->formStatus($h, 'select'); // returns 'open' or 'closed'
208
209        if (   ($h->pageType == 'post')
210            && ($h->comment->thisForm == 'open')
211            && ($h->comment->allForms == 'checked')) {
212           
213            if ($h->currentUser->loggedIn) {
214
215                if (($h->cage->post->getAlpha('comment_process') == 'newcomment') ||
216                    ($h->cage->post->getAlpha('comment_process') == 'editcomment'))
217                {
218       
219                    if ($h->cage->post->keyExists('comment_content')) {
220                        $h->comment->content = sanitize($h->cage->post->getHtmLawed('comment_content'), 'tags', $h->comment->allowableTags);
221                    }
222                   
223                    if ($h->cage->post->keyExists('comment_post_id')) {
224                        $h->comment->postId = $h->cage->post->testInt('comment_post_id');
225                    }
226
227                    if ($h->cage->post->keyExists('comment_user_id')) {
228                        $h->comment->author = $h->cage->post->testInt('comment_user_id');
229                    }
230               
231                    if ($h->cage->post->keyExists('comment_parent')) {
232                        $h->comment->parent = $h->cage->post->testInt('comment_parent');
233                        if ($h->cage->post->getAlpha('comment_process') == 'editcomment') {
234                            $h->comment->id = $h->cage->post->testInt('comment_parent');
235                        }
236                    }
237                   
238                    if ($h->cage->post->keyExists('comment_subscribe')) {
239                        $h->comment->subscribe = 1;
240                    } else {
241                        $h->comment->subscribe = 0;
242                        $h->comment->unsubscribe($h, $h->comment->postId);
243                    }
244                   
245                    if ($h->cage->post->getAlpha('comment_process') == 'newcomment')
246                    {
247                        // before posting, we need to be certain this user has permission:
248                        $safe = false;
249                        $can_comment = $h->currentUser->getPermission('can_comment');
250                        if ($can_comment == 'yes') { $safe = true; }
251                        if ($can_comment == 'mod') { $safe = true; $h->comment->status = 'pending'; }
252                       
253                        $result = array(); // holds results from addComment function
254                       
255                        // Okay, safe to add the comment...
256                        if ($safe) {
257                            // A user can unsubscribe by submitting an empty comment, so...
258                            if ($h->comment->content != '') {
259                                $result = $h->comment->addComment($h);
260
261                                // notify chosen mods of new comment by email if enabled and UserFunctions file exists
262                                if (($comments_settings['comment_email_notify']) && (file_exists(PLUGINS . 'users/libs/UserFunctions.php')))
263                                {
264                                    require_once(PLUGINS . 'users/libs/UserFunctions.php');
265                                    $uf = new UserFunctions();
266                                    $uf->notifyMods($h, 'comment', $h->comment->status, $h->comment->postId, $h->comment->id);
267                                }
268
269                                // email comment subscribers if this comment has 'approved' status:
270                                if ($h->comment->status == 'approved') {
271                                    $this->emailCommentSubscribers($h, $h->comment->postId);
272                                }
273                            } else {
274                                //comment empty so just check subscribe box:
275                                $h->comment->updateSubscribe($h, $h->comment->postId);
276                                $h->messages[$h->lang['comment_moderation_unsubscribed']] = 'green';
277                            }
278                        }
279                       
280                        if ($result['exceeded_daily_limit']) {
281                            $h->messages[$h->lang['comment_moderation_exceeded_daily_limit']] = 'green';
282                        } elseif ($result['exceeded_url_limit']) {
283                            $h->messages[$h->lang['comment_moderation_exceeded_url_limit']] = 'green';
284                        } elseif ($result['not_enough_comments']) {
285                            $h->messages[$h->lang['comment_moderation_not_enough_comments']] = 'green';
286                        }
287                    }
288                    elseif($h->cage->post->getAlpha('comment_process') == 'editcomment')
289                    {
290                        // before editing, we need to be certain this user has permission:
291                        $safe = false;
292                        $can_edit = $h->currentUser->getPermission('can_edit_comments');
293                        if ($can_edit == 'yes') { $safe = true; }
294                        if (($can_edit == 'own') && ($h->currentUser->id == $h->comment->author)) { $safe = true; }
295                        if ($safe) {
296                            $h->comment->editComment($h);
297                        }
298                    }
299                   
300                    if ($h->comment->status == 'pending') {
301                        return false;
302                    }
303                   
304                    header("Location: " . $h->url(array('page'=>$h->comment->postId)));    // Go to the post
305                    die();
306                   
307                }
308            }
309           
310        }
311   
312        return false;
313    }
314   
315   
316    /**
317     * Include css and JavaScript
318     */
319    public function header_include($h)
320    {
321        $h->includeCss('comments', 'comments');
322        $h->includeJs('comments', 'urldecode.min');
323        $h->includeJs('comments', 'comments');
324    }
325   
326   
327    /**
328     * Include jQuery for hiding and showing email options in plugin settings
329     */
330    public function admin_header_include_raw($h)
331    {
332        if ($h->isSettingsPage('comments')) {
333            echo "<script type='text/javascript'>\n";
334            echo "$(document).ready(function(){\n";
335                echo "$('#email_notify').click(function () {\n";
336                echo "$('#email_notify_options').slideToggle();\n";
337                echo "});\n";
338            echo "});\n";
339            echo "</script>\n";
340        }
341    }
342   
343   
344    /**
345     * Link to comments
346     */
347    public function sb_base_show_post_extra_fields($h)
348    {
349        echo '<li><a class="comment_link" href="' . $h->url(array('page'=>$h->post->id)) . '">' . $h->countComments() . '</a></li>' . "\n";
350    }
351   
352   
353    /**
354     * Prepare and display comments wrapper and form
355     */
356    public function sb_base_post_show_post($h)
357    {
358        // set default
359        $h->vars['subscribe_check'] = '';
360       
361        // Check if the currentUser is the post author
362        if ($h->post->author == $h->currentUser->id) {
363            // Check if the user subscribed to comments as a submitter
364            if ($h->post->subscribe == 1) {
365                $h->vars['subscribe_check'] = 'checked';
366            }
367        }
368       
369        // Check if the user subscribed to comments as a commenter
370        $sql = "SELECT COUNT(comment_subscribe) FROM " . TABLE_COMMENTS . " WHERE comment_post_id = %d AND comment_user_id = %d AND comment_subscribe = %d";
371        $subscribe_result = $h->db->get_var($h->db->prepare($sql, $h->post->id, $h->currentUser->id, 1));
372       
373        if ($subscribe_result > 0) {
374            $h->vars['subscribe_check'] = 'checked';
375        }
376
377        if (!$h->isPage('submit3')) {
378       
379            $comments_settings = $h->getSerializedSettings();
380            $h->comment->pagination = $comments_settings['comment_pagination'];
381            $h->comment->order = $comments_settings['comment_order'];
382            $h->comment->itemsPerPage = $comments_settings['comment_items_per_page'];
383           
384            // GET ALL PARENT COMMENTS
385            $parents = $h->comment->readAllParents($h, $h->post->id, $h->comment->order);
386                   
387            echo "<!--  START COMMENTS_WRAPPER -->\n";
388            echo "<div id='comments_wrapper'>\n";
389            echo "<h2>" . $h->countComments(false) . "</h2>\n";
390               
391            // IF PAGINATING COMMENTS:
392            if ($h->comment->pagination)
393            {
394                $pagedResults = $h->paginationFull($parents, $h->comment->itemsPerPage);
395
396                if (isset($pagedResults->items)) {
397                // cycle through the parents, and go get their children
398                    foreach($pagedResults->items as $parent) {
399       
400                            $this->displayComment($h, $parent);
401                            $this->commentTree($h, $parent->comment_id, 0);
402                            $h->comment->depth = 0;
403                    }
404                }
405            }
406            // IF NO PAGINATION:
407            else
408            {
409                if ($parents) {
410                    // cycle through the parents, and go get their children
411                    foreach ($parents as $parent) {
412                        $this->displayComment($h, $parent);
413                        $this->commentTree($h, $parent->comment_id, 0);
414                        $h->comment->depth = 0;
415                    }
416                }
417            }
418
419            echo "</div><!-- close comments_wrapper -->\n";
420            echo "<!--  END COMMENTS -->\n";
421        }
422       
423        if ($h->comment->pagination && $pagedResults) {
424            echo $h->pageBar($pagedResults);
425        }
426       
427        // determine where to return the user to after logging in:
428        if (!$h->cage->get->keyExists('return')) {
429            $host = $h->cage->server->sanitizeTags('HTTP_HOST');
430            $uri = $h->cage->server->sanitizeTags('REQUEST_URI');
431            $return = 'http://' . $host . $uri;
432            $return = urlencode(htmlentities($return,ENT_QUOTES,'UTF-8'));
433        } else {
434            $return = $h->cage->get->testUri('return'); // use existing return parameter
435        }
436               
437        if (!$h->currentUser->loggedIn) {
438            echo "<div class='comment_form_off'>";
439            echo "<a href='" . BASEURL . "index.php?page=login&amp;return=" . $return . "'>";
440            echo $h->lang['comments_please_login'] . "</a></div>";
441            return false;
442        }
443       
444        if ($h->currentUser->getPermission('can_comment') == 'no') {
445            echo "<div class='comment_form_off'>" . $h->lang['comments_no_permission'] . "</div>";
446            return false;
447        }
448       
449        if (!$h->isPage('submit3') && ($h->comment->thisForm == 'closed')
450            || ($h->comment->allForms != 'checked')) {
451            echo "<div class='comment_form_off'>" . $h->lang['comments_form_closed'] . "</div>";
452            return false;
453        }
454
455        if (!$h->isPage('submit3')) {
456            // force non-reply form to have parent "0" and depth "0"
457            $h->comment->id = 0;
458            $h->comment->depth = 0;
459            $h->vars['subscribe'] = ($h->comment->subscribe) ? 'checked' : '';
460            $h->displayTemplate('comment_form', 'comments', false);
461           
462            $h->pluginHook('comments_post_last_form');
463           
464            if ($h->currentUser->getPermission('can_comment_manager_settings') == 'yes') {
465                echo "<a id='comment_manager_link' href='" . $h->url(array('page'=>'plugin_settings', 'plugin'=>'comment_manager'), 'admin') . "'>";
466                echo $h->lang['comments_access_comment_manager'] . "</a>";
467            }
468        }
469    }
470   
471   
472    /**
473     * Recurse through comment tree
474     *
475     * @param int $item_id - id of current comment
476     * @param int $depth - for comment nesting
477     * @return bool
478     */
479    public function commentTree($h, $item_id, $depth)
480    {
481        while ($children = $h->comment->readAllChildren($h, $item_id)) {
482            foreach ($children as $child) {
483                $depth++;
484                if ($depth == $h->comment->levels) {
485                    // Prevent depth exceeding nesting levels
486                    // levels start at 0 so we're using -1.
487                    $depth = $h->comment->levels - 1;
488                }
489                $h->comment->depth = $depth;
490                $this->displayComment($h, $child);
491                if ($this->commentTree($h, $child->comment_id, $depth)) {
492                    return true;
493                } else {
494                    $depth--; // no more children for previous comment, come back up a level
495                }
496            }
497            return false;
498        }
499        return false;
500    }
501   
502   
503    /**
504     * Display a comment
505     *
506     * @param array $item - current comment
507     */
508    public function displayComment($h, $item, $all = false)
509    {
510        if ($h->isPage('submit2')) { return false; }
511       
512        $h->comment->readComment($h, $item);
513        if ($h->comment->status == 'approved') {
514            if ($all) {
515                $h->displayTemplate('all_comments', 'comments', false);
516            } else {
517                $h->displayTemplate('show_comments', 'comments', false);
518            }
519           
520            // don't show the reply form in these cases:
521            //if ($all) { return false; } // we're looking at the main comments page
522            if ($h->currentUser->getPermission('can_comment') == 'no') { return false; }
523            if (!$h->currentUser->loggedIn) { return false; }
524            if ($h->comment->thisForm == 'closed') { return false; }
525            if ($h->comment->allForms != 'checked') { return false; }
526   
527            // show the reply form:
528            $h->vars['subscribe'] = ($h->comment->subscribe) ? 'checked' : '';
529            $h->displayTemplate('comment_form', 'comments', false);
530        }
531    }
532   
533   
534    /**
535     * Show all comments list on a main "Comments" page
536     */
537    public function theme_index_main($h)
538    {
539        if (!$h->isPage('comments')) { return false; }
540       
541        if ($h->cage->get->keyExists('user')) {
542            $user = $h->cage->get->testUsername('user');
543            $userid = $h->getUserIdFromName($user);
544        } else {
545            $userid = 0;
546        }
547
548        $comments_settings = $h->getSerializedSettings();
549        $h->comment->itemsPerPage = $comments_settings['comment_items_per_page'];
550       
551        if ($userid) {
552            $comments_count = $h->comment->getAllCommentsCount($h, '', $userid);
553            $comments_query = $h->comment->getAllCommentsQuery($h, 'DESC', $userid);
554        } else {
555            $comments_count = $h->comment->getAllCommentsCount($h);
556            $comments_query = $h->comment->getAllCommentsQuery($h, 'DESC');
557        }
558       
559        if (!$comments_count) {
560            $h->showMessage($h->lang['comments_user_no_comments'], 'red');
561            return true;
562        }
563           
564        $pagedResults = $h->pagination($comments_query, $comments_count, $h->comment->itemsPerPage, 'comments');
565       
566        if (isset($pagedResults->items)) {
567            foreach ($pagedResults->items as $comment) {
568                $h->readPost($comment->comment_post_id);
569                // don't show this comment if its post is buried or pending:
570                if ($h->post->status == 'buried' || $h->post->status == 'pending') { continue; }
571               
572                $this->displayComment($h, $comment, true);
573            }
574           
575            echo $h->pageBar($pagedResults);
576        }
577        return true;
578    }
579   
580   
581    /**
582     * Add Comments RSS link to breadcrumbs
583     */
584    public function breadcrumbs($h)
585    {
586        if ($h->pageName != 'comments') { return false; }
587       
588        if ($h->subPage == 'user') {
589            $user = $h->cage->get->testUsername('user');
590            $userlink = "<a href='" . $h->url(array('user'=>$user)) . "'>";
591            $userlink .= $user . "</a> &raquo ";
592            $rss = "<a href='" . $h->url(array('page'=>'rss_comments', 'user'=>$h->cage->get->testUsername('user'))) . "'> ";
593            $crumbs = $userlink . $h->lang['comments'] . $rss;
594        } else {
595            $crumbs = $h->lang ['comments'] . "<a href='" . $h->url(array('page'=>'rss_comments')) . "'> ";
596        }
597        $crumbs .= " <img src='" . BASEURL . "content/themes/" . THEME . "images/rss_10.png' alt='" . $h->pageTitle . " RSS' /></a>\n ";
598       
599        return $crumbs;
600    }
601   
602   
603    /**
604     * Show post_subscribe option in Submit step 2 and Post Edit
605     */
606    public function submit_2_fields($h)
607    {
608        if ($h->post->subscribe) { $subscribe = 'checked'; } else { $subscribe = ''; }
609        echo "<tr><td colspan='3'>\n";
610        echo "<input id='post_subscribe' name='post_subscribe' type='checkbox' " . $subscribe . "> " . $h->lang['submit_subscribe'];
611        echo "</tr>";
612    }
613   
614   
615    /**
616     * Show Enable comment form option in Post Edit
617     */
618    public function submit_edit_admin_fields($h)
619    {
620        if ($h->post->comments == 'open') { $form_open = 'checked'; } else { $form_open = ''; }
621
622        echo "<tr><td colspan='3'>\n";
623        echo "<input id='enable_comments' name='enable_comments' type='checkbox' " . $form_open . "> " . $h->lang['submit_form_enable_comments'];
624        echo "</tr>";
625    }
626   
627   
628    /**
629     * Check and update post_submit in Submit step 2 and Post Edit pages
630     */
631    public function submit_functions_process_submitted($h)
632    {
633        if (($h->pageName != 'submit2') && ($h->pageName != 'edit_post')) { return false; }
634
635        // SUBSCRIBE TO COMMENTS
636       
637        if ($h->cage->post->keyExists('post_subscribe')) {
638            $h->post->subscribe = 1;
639            $subscribe = 'checked';
640        } else {
641            // use existing setting:
642            $subscribe = ($h->post->subscribe) ? 'checked' : '';
643        }
644
645        $h->vars['submitted_data']['submit_subscribe'] = $h->post->subscribe;
646       
647        // ENABLE / DISABLE COMMENT FORM
648       
649        // check on edit post
650        if ($h->pageName == 'edit_post') {
651            if ($h->cage->post->keyExists('enable_comments')) {
652                $h->post->comments = 'open';
653                $comments = 'open';
654            } else {
655                if ($h->currentUser->getPermission('can_edit_posts') == 'yes') {
656                    $h->post->comments = 'closed';
657                    $comments = 'closed';
658                } else {
659                    $comments = $h->post->comments; // keep existing setting
660                }
661            }
662        } else {
663            // open for submit 2
664            $h->post->comments = 'open';
665            $comments = 'open';
666        }
667       
668        $h->vars['submitted_data']['submit_comments'] = $h->post->comments;
669    }
670   
671   
672    /**
673     * Save subscribe to the database during post update
674     */
675    public function submit_2_process_submission($h)
676    {
677        $h->post->subscribe = $h->vars['submitted_data']['submit_subscribe'];
678        $h->post->comments = $h->vars['submitted_data']['submit_comments'];
679    }
680   
681   
682    /**
683     * Delete comments when post deleted
684     */
685    public function post_delete_post($h)
686    {
687        if (!$h->post->id) { return false; }
688       
689        $sql = "DELETE FROM " . TABLE_COMMENTS . " WHERE comment_post_id = %d";
690        $h->db->query($h->db->prepare($sql, $h->post->id));
691    }
692
693   
694    /**
695     * Profile navigation link
696     */
697    public function profile_navigation($h)
698    {
699        echo "<li><a href='" . $h->url(array('page'=>'comments', 'user'=>$h->vars['user']->name)) . "'>" . $h->lang['users_all_comments']  . "</a></li>\n";
700    }
701   
702   
703    /**
704     * Publish content as an RSS feed
705     * Uses the 3rd party RSS Writer class.
706     */   
707    public function rssFeed($h)
708    {
709        require_once(EXTENSIONS . 'RSSWriterClass/rsswriter.php');
710       
711        $select = '*';
712
713        $limit = $h->cage->get->getInt('limit');
714        $user = $h->cage->get->testUsername('user');
715
716        if (!$limit) { $limit = 10; }
717        if ($user) {
718            $userid = $h->getUserIdFromName($user);
719        } else {
720            $userid = 0;
721        }
722       
723        $h->pluginHook('comments_rss_feed');
724       
725        $feed           = new RSS();
726        $feed->title    = SITE_NAME;
727        $feed->link     = BASEURL;
728       
729        if ($user) {
730            $feed->description = $h->lang["comment_rss_comments_from_user"] . " " . $user;
731        } else {
732            $feed->description = $h->lang["comment_rss_latest_comments"] . SITE_NAME;
733        }
734       
735        // fetch comments from the database       
736        $comments = $h->comment->getAllComments($h, 0, "desc", $limit, $userid);
737       
738        if ($comments) {
739            foreach ($comments as $comment)
740            {
741                $h->readPost($comment->comment_post_id);
742               
743                $author = $h->getUserNameFromId($comment->comment_user_id);
744               
745                $item = new RSSItem();
746                if ($user) {
747                    $title = $h->lang["comment_rss_comment_on"] . html_entity_decode(urldecode($h->post->title), ENT_QUOTES,'UTF-8');
748                } else {
749                    $title = $author . $h->lang["comment_rss_commented_on"] . html_entity_decode(urldecode($h->post->title), ENT_QUOTES,'UTF-8');
750                }
751                $item->title = stripslashes($title);
752                $item->link  = $h->url(array('page'=>$comment->comment_post_id)) . "#c" . $comment->comment_id;
753                $item->setPubDate($comment->comment_date);
754                $item->description = "<![CDATA[ " . stripslashes(urldecode($comment->comment_content)) . " ]]>";
755                $feed->addItem($item);
756            }
757        }
758        echo $feed->serve();
759    }
760   
761   
762    /**
763     * Show stats on Admin home page
764     */
765    public function admin_theme_main_stats($h, $vars)
766    {
767        echo "<li>&nbsp;</li>";
768   
769        foreach ($vars as $stat_type) {
770            require_once(LIBS . 'Comment.php');
771            $c = new Comment();
772            $comments = $c->stats($h, $stat_type);
773            if (!$comments) { $comments = 0; }
774            $lang_name = 'comments_admin_stats_' . $stat_type;
775            echo "<li>" . $h->lang[$lang_name] . ": " . $comments . "</li>";
776        }
777    }
778   
779   
780    /**
781    * Send an email to thread subscribers
782    *
783    * @param int $post_id
784    */
785    function emailCommentSubscribers($h, $post_id)
786    {
787        $h->readPost($post_id);
788   
789        // build a list of subscribers
790        $subscriber_ids = array();
791       
792        // Get id of post author if subscribed
793        if ($h->post->subscribe == 1) {
794            array_push($subscriber_ids, $h->post->author);
795        }
796       
797        // Get ids of comment authors if subscribed
798        $sql = "SELECT comment_user_id FROM " . TABLE_COMMENTS . " WHERE comment_subscribe = %d AND comment_post_id = %d";
799        $comment_subscribers = $h->db->get_results($h->db->prepare($sql, 1, $h->post->id));
800        if ($comment_subscribers) {
801            foreach ($comment_subscribers as $comment_subscriber) {
802                array_push($subscriber_ids, $comment_subscriber->comment_user_id);
803            }
804        }
805       
806        // Use the ids to make an array of unique email addresses
807        $subscribers = array();
808        $subscriber_ids = array_unique($subscriber_ids);
809       
810        foreach ($subscriber_ids as $subscriber_id) {
811            // remove the current comment author so he/she doesn't get emailed his own comment
812            if ($subscriber_id != $h->comment->author) {
813                $email = $h->db->get_var($h->db->prepare("SELECT user_email FROM " . TABLE_USERS . " WHERE user_id = %d", $subscriber_id));
814                array_push($subscribers, $email);
815            }
816        }
817       
818        $send_to = trim(implode(",", $subscribers),",");
819       
820        $comment_author = $h->getUserNameFromId($h->comment->author);
821       
822        //clean up content:
823        $story_title = stripslashes(html_entity_decode(urldecode($h->post->title), ENT_QUOTES,'UTF-8'));
824        $comment_content = stripslashes($h->comment->content);
825       
826        $subject = $comment_author . ' ' . $h->lang["comment_email_subject"]  . ' ' . $story_title;
827       
828        $message =  $comment_author . $h->lang["comment_email_intro"] . SITE_NAME . ": \r\n\r\n";
829        $message .= $h->lang["comment_email_story_title"] . $story_title . "\r\n";
830        $message .= $h->lang["comment_email_story_link"] . $h->url(array('page'=>$h->post->id)) . "\r\n\r\n";
831        $message .= $h->lang["comment_email_comment"] . $comment_content . "\r\n\r\n";
832        $message .= "************************ \r\n";
833        $message .= $h->lang["comment_email_do_not_reply"] . " \r\n";
834        $message .= $h->lang["comment_email_unsubscribe"];
835       
836        if (!$h->comment->email) {
837            // Get settings from database if they exist...
838            $comments_settings = $h->getSerializedSettings('comments');
839            $h->comment->email = $comments_settings['comment_email'];
840        }
841       
842        $from = SITE_EMAIL;
843        $to = $h->comment->email;  // send email to address specified in Comment Settings;
844        if($send_to != "") {
845            $bcc = "\r\nBCC: " . $send_to;    // BCC individual addresses;
846        } else {
847            $bcc = "";
848        }
849
850        if (SMTP == 'true') {
851            $recipients['To'] = $to;
852            $recipients['Bcc'] = $send_to;
853            // no SMTP headers because they get overwritten in EmailFunctions anyway
854            $h->email($recipients, $subject, $message);
855        } else {
856            $recipients = $to;
857            $headers = "From: " . $from . $bcc . "\r\nReply-To: " . $from . "\r\nX-Priority: 3\r\n";
858            $h->email($recipients, $subject, $message, $headers);
859        }
860    }
861}
862
863?>
Note: See TracBrowser for help on using the repository browser.