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

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

Add maintain.inc.php file
Fix bug in ce_comment class

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