source: extensions/CommentEditor/classes/ce_plugin.class.php @ 3431

Last change on this file since 3431 was 3431, checked in by Criss, 15 years ago

Fix plugin URI to be available in plugin update check page.
Reduce code line size.

  • Property svn:eol-style set to LF
File size: 9.0 KB
Line 
1<?php
2/* $Id: ce_plugin.class.php,v 1.3 2009/06/17 19:08:25 Criss Exp $ */
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5ce_require_class("CE_Comment");
6
7/**
8 * CE_Plugin class
9 */
10class CE_Plugin {
11
12    private static $url_prefix = null;
13
14
15    function CE_Plugin() {
16        if (null == self::$url_prefix) {
17            self::$url_prefix = get_root_url() . 'index.php?' . CE_ACTION . '=';
18        }
19    }
20
21    /* ************************ */
22    /* ** Trigger management ** */
23    /* ************************ */
24
25    /**
26     * Check whether the page contains comments.
27     * Add an edit link if user is allowed to edit comment
28     */
29    function loc_begin_page_header() {
30        global $template;
31        $all_comments = &$template->get_template_vars('comments');
32        for ($i=0; $i < count($all_comments) ; $i++) {
33            $comment = &$all_comments[$i];
34            if (isset($comment['U_DELETE'])) {
35                $comment['U_ID'] =  $this->getCommentId($comment['U_DELETE']);
36
37                $comment['DATE'] .= ' - <a href="' . $this->getEditUrlPrefix();
38                $comment['DATE'] .= $comment['U_ID'] . '">';
39                $comment['DATE'] .= 'Edit';
40                $comment['DATE'] .= '</a>';
41            }
42        }
43    }
44
45    /**
46     * Check whether a comment edit or update is requested.
47     */
48    function loc_end_index() {
49        $comment_id = "";
50        $action = $this->isCommentEdit($_SERVER['QUERY_STRING'], $comment_id);
51        switch ($action) {
52            case CE_ACTION_EDIT:
53                // Edit
54                $this->editComment($comment_id);
55                break;
56            case CE_ACTION_UPDATE:
57                $this->updateComment();
58                break;
59            case CE_ACTION_NONE:
60                // Not an edit mode
61                break;
62        }
63    }
64
65    /* ************************ */
66    /* ** Private functions  ** */
67    /* ************************ */
68
69    function getTemplate($p_template = 'edit.tpl') {
70        return realpath(CE_TEMPLATE . $p_template);
71    }
72
73    function isCommentEdit($p_query_string, &$p_comment_id) {
74        $params = split('\&', $p_query_string);
75        $action = CE_ACTION_NONE;
76        $comment_id = "";
77        foreach ($params as $current_param) {
78            $key_value = split('=', $current_param);
79            if (isset($key_value[1])) {
80                if (CE_ACTION == $key_value[0] && CE_ACTION_NONE == $action) {
81                    //CE_ACTION_EDIT == $lKeyVal[1]
82                    switch ($key_value[1]) {
83                        case CE_ACTION_EDIT:
84                        case CE_ACTION_UPDATE:
85                            $action = $key_value[1];
86                            break;
87                    }
88                }
89                if (CE_ID == $key_value[0]) {
90                    $comment_id = $key_value[1];
91                }
92            }
93        }
94        if (CE_ACTION_EDIT == $action) {
95            $p_comment_id = $comment_id;
96        }
97        return $action;
98    }
99
100    function isEditAllowed($p_comment) {
101        if (is_admin()) {
102            return true;
103        }
104        global $user;
105        return $p_comment->isAuthor($user['username']);
106    }
107
108    function editComment($p_comment_id) {
109        //  Include language advices
110        load_language('plugin.lang', CE_PATH);
111
112        $comment = new CE_Comment($p_comment_id);
113        $infos = array();
114        if (!$this->isEditAllowed($comment)) {
115            // Not allowed
116            array_push($infos, l10n('comment_edit_forbidden'));
117            $this->displayError($infos);
118            return;
119        }
120        $this->displayComment($comment);
121    }
122
123    function updateComment() {
124        //  Include language advices
125        load_language('plugin.lang', CE_PATH);
126
127        $infos = array();
128        if (!isset($_POST['commentid'])) {
129            array_push($infos, l10n('comment_access_invalid'));
130            $this->displayError($infos);
131            return;
132        }
133        $comment = new CE_Comment($_POST['commentid']);
134
135        if (!$this->isEditAllowed($comment)) {
136            array_push($infos, l10n('comment_edit_forbidden'));
137            $this->displayError($infos);
138            return;
139        }
140        if (isset($_POST['author'])) {
141            $comment->setInfo('author', $_POST['author']);
142        } else if (isset($_POST['freeauthorck'])) {
143            // Free author
144            $new_author = trim(stripslashes($_POST['freeauthor']));
145            $new_author = $comment->validateAuthor($new_author);
146            $comment->setInfo('author', $new_author);
147        }
148
149        $comment_array = array(
150            'author'        => trim( stripslashes($comment->getInfo('author')) ),
151            'content'       => trim( stripslashes($_POST['content']) ),
152            'image_id'      => $_POST['imageid'],
153            'comment_id'    => $_POST['commentid'],
154        );
155        $comment_action = update_user_comment($comment_array, @$_POST['key'], $infos );
156
157        switch ($comment_action) {
158            case 'moderate':
159              array_push( $infos, l10n('comment_to_validate') );
160            case 'validate':
161              array_push( $infos, l10n('comment_added'));
162              break;
163            case 'reject':
164              set_status_header(403);
165              array_push($infos, l10n('comment_not_added') );
166              break;
167            default:
168              trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING);
169        }
170        // allow plugins to notify what's going on
171        trigger_action( 'user_comment_insertion',
172                        array_merge($comment_array, array('action'=>$comment_action)));
173        if ($comment_action=='reject') {
174            $this->displayError($infos);
175        } else {
176            $this->displayInfo($infos);
177        }
178    }
179
180    function getCommentId($p_string_delete) {
181        $comment_id = "";
182        $elements = split('\?', $p_string_delete);
183        if (count($elements) > 1) {
184            $elem_var = split('\&amp;', $elements[1]);
185            foreach ($elem_var as $var) {
186                $key_value = split('=', $var);
187                if (count($key_value) > 1) {
188                    switch ($key_value[0]) {
189                        case 'comment_to_delete':
190                        case 'delete':
191                            $comment_id = $key_value[1];
192                            break;
193                        default:
194                            // No ID
195                    }
196                }
197            }
198        }
199        return $comment_id;
200    }
201
202    function displayError($p_error) {
203        $this->displayMessage($p_error, CE_TYPE_ERROR, CE_ERRORS);
204    }
205
206    function displayInfo($p_info) {
207        $this->displayMessage($p_info, CE_TYPE_INFO, CE_INFOS);
208    }
209
210    function displayMessage($p_message, $p_type, $p_image) {
211        //  Include language advices
212        load_language('plugin.lang', CE_PATH);
213
214        $messages = array();
215        foreach ($p_message as $message) {
216            array_push($messages, $message);
217        }
218        $message_array = array(
219            'TYPE'  => $p_type,
220            'IMG'   => $p_image,
221            'TITLE' => l10n($p_type),
222            'MSG'   => $messages
223        );
224
225        global $template;
226        $template->set_filenames(array('ce_message'=>$this->getTemplate('message.tpl')));
227        $template->block_html_head( '',
228                                    '<link rel="stylesheet" type="text/css" href="'.CE_STYLE.'">',
229                                    $smarty, $repeat);
230        $begin = 'PLUGIN_INDEX_CONTENT_BEFORE';
231        $template->assign('message', $message_array);
232        $old_begin = $template->get_template_vars($begin);
233        $template->assign($begin, $template->parse('ce_message', true));
234        $template->concat($begin, $old_begin);
235    }
236
237    function displayComment($p_comment) {
238        global $template;
239
240        $template->set_filenames(array('ce_edit'=>$this->getTemplate()));
241        $template->block_html_head( '',
242                                    '<link rel="stylesheet" type="text/css" href="'.CE_STYLE.'">',
243                                    $smarty, $repeat);
244        $begin = 'PLUGIN_INDEX_CONTENT_BEFORE';
245        $end = 'PLUGIN_INDEX_CONTENT_AFTER';
246        $template->assign($begin, null);
247        $template->assign($end, null);
248        $tplComment = array(
249            'CONTENT'   => $p_comment->getInfo('content'),
250            'KEY'       => $p_comment->getInfo('comment_id'),
251            'AUTHOR'    => $p_comment->getInfo('author'),
252            'IMAGE'     => $p_comment->getInfo('image_id'),
253            'DISABLED'  => !is_admin(),
254            'USERS'     => CE_Comment::getUsers(),
255            'F_ACTION'  => $this->getUpdateUrlPrefix(),
256            'FREEAUTHOR'=> ($p_comment->isKnownAuthor())?'':'checked="checked"'
257        );
258        $template->assign('comment', $tplComment);
259        $template->concat($begin, $template->parse('ce_edit', true));
260    }
261
262    function getEditUrlPrefix() {
263        return self::$url_prefix . CE_ACTION_EDIT . '&' . CE_ID . '=';
264    }
265
266    function getUpdateUrlPrefix() {
267        return self::$url_prefix . CE_ACTION_UPDATE;
268    }
269
270}
271?>
Note: See TracBrowser for help on using the repository browser.