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

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

Display edit block on picture page if request comes from this page
Add simple administration configuration management
Add and remove default configuration from databaseon plugin install / uninstall

  • Property svn:eol-style set to LF
File size: 12.0 KB
Line 
1<?php
2/* $Id: ce_plugin.class.php,v 1.11 2009/06/26 09:17:01 Criss Exp $ */
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5/**
6 * CE_Plugin class
7 */
8class CE_Plugin {
9
10  var $comments_ids;
11  var $config;
12  var $plugin_id;
13
14  /* ************************ */
15  /* ** Constructor        ** */
16  /* ************************ */
17
18  function CE_Plugin($plugin_id, $config = null) {
19    $this->plugin_id = $plugin_id;
20    $this->config = $config;
21    if (null != $this->config) {
22      $this->config->loadConfig();
23    }
24  }
25
26  function getConfig() {
27    return $this->config;
28  }
29
30  /* ************************ */
31  /* ** Trigger management ** */
32  /* ************************ */
33
34  function get_admin_plugin_menu_links($menu) {
35    array_push(
36        $menu,
37        array(
38            'NAME' => $this->plugin_id,
39            'URL' => $this->get_plugin_admin_url()
40        )
41    );
42    return $menu;
43  }
44
45  /**
46   * Update comment ID array
47   */
48  function render_comment_content($content) {
49    $author = null;
50    $id = null;
51    switch (script_basename()) {
52      case "comments":
53        global $comment;
54        $author = $comment['author'];
55        $id = $comment['comment_id'];
56        break;
57      case "picture":
58        global $row;
59        $author = $row['author'];
60        $id = $row['id'];
61        break;
62      default:
63        // Not in right script
64        return $content;
65    }
66    global $template, $conf, $user;
67    if ((!is_a_guest() and ($user[$conf['user_fields']['username']] == $author))
68        or is_admin()) {
69      $key = count($template->get_template_vars('comments'));
70      $this->comments_ids[$key] = $id;
71    }
72    return $content;
73  }
74
75  /**
76   * Check whether the page contains comments.
77   * Add an edit link if user is allowed to edit comment
78   */
79  function loc_begin_page_header() {
80    global $template;
81    $comments = $template->get_template_vars('comments');
82    if (!isset($comments) or (null == $comments)) {
83      // No comment...
84      return;
85    }
86    // Include language advices
87    load_language('plugin.lang', CE_PATH);
88    foreach ($comments as $key => $value) {
89      if (isset($this->comments_ids[$key])) {
90        // User allowed to have edit link
91        $current_id = $this->comments_ids[$key];
92        $comments[$key]['U_ID'] =  $current_id;
93        $comments[$key]['DATE'] .= ' - <a href="';
94        $comments[$key]['DATE'] .= $this->getEditUrl($current_id);
95        $comments[$key]['DATE'] .= '">';
96        $comments[$key]['DATE'] .= l10n('comment_edit_tool');
97        $comments[$key]['DATE'] .= '</a>';
98      }
99    }
100    $template->assign('comments', $comments);
101  }
102
103  /**
104   * Check whether a comment edit or update is requested.
105   */
106  function loc_begin_index() {
107    $this->doAction();
108  }
109
110  function loc_begin_picture() {
111    $this->doAction();
112  }
113
114  /* ************************ */
115  /* ** Private functions  ** */
116  /* ************************ */
117
118  function doAction() {
119    $comment_id = "";
120    $action = $this->isCommentEdit($_SERVER['QUERY_STRING'], $comment_id);
121    switch ($action) {
122      case CE_ACTION_EDIT:
123        // Edit
124        $this->editComment($comment_id);
125        break;
126      case CE_ACTION_UPDATE:
127        $this->updateComment();
128        break;
129      case CE_ACTION_ERROR:
130        $infos = array('comment_edit_forbidden');
131        $this->displayError($infos);
132        break;
133      case CE_ACTION_NONE:
134        // Not an edit mode
135        break;
136    }
137  }
138
139  function getTemplate($p_template = 'edit.tpl') {
140    return realpath(CE_TEMPLATE . $p_template);
141  }
142
143  function isCommentEdit($p_query_string, &$p_comment_id) {
144    $params = split('\&', $p_query_string);
145    $action = CE_ACTION_NONE;
146    $comment_id = "";
147    foreach ($params as $current_param) {
148      $key_value = split('=', $current_param);
149      if (isset($key_value[1])) {
150        if (CE_ACTION == $key_value[0] && CE_ACTION_NONE == $action) {
151          switch ($key_value[1]) {
152            case CE_ACTION_EDIT:
153            case CE_ACTION_UPDATE:
154              $action = $key_value[1];
155              break;
156            default:
157          }
158        }
159        if (CE_ID == $key_value[0]) {
160          $comment_id = $key_value[1];
161        }
162      }
163    }
164    if (CE_ACTION_EDIT == $action) {
165      if (is_numeric($comment_id)) {
166        $p_comment_id = intval($comment_id);
167      } else {
168        $action = CE_ACTION_ERROR;
169      }
170    }
171    return $action;
172  }
173
174  function isEditAllowed($p_comment) {
175    if ((FALSE == $p_comment->getInfo()) or
176        is_a_guest()) {
177      return false;
178    }
179    if (is_admin() and !is_adviser()) {
180      return true;
181    }
182    global $conf, $user;
183    return $p_comment->isAuthor($user[$conf['user_fields']['username']]);
184  }
185
186  function editComment($p_comment_id) {
187    $comment = new CE_Comment($p_comment_id);
188    $infos = array();
189    if (!$this->isEditAllowed($comment)) {
190      // Not allowed
191      array_push($infos, 'comment_edit_forbidden');
192      $this->displayError($infos);
193      return;
194    }
195    $this->displayComment($comment);
196  }
197
198  function updateComment() {
199    $infos = array();
200    if ((!isset($_POST['ce_commentid'])) or
201        (!isset($_POST['ce_imageid']))) {
202      array_push($infos, 'comment_access_invalid');
203      $this->displayError($infos);
204      return;
205    }
206    if (!is_numeric($_POST['ce_imageid'])) {
207      array_push($infos, 'comment_access_invalid');
208      $this->displayError($infos);
209      return;
210    }
211    $comment = new CE_Comment($_POST['ce_commentid']);
212
213    if (!$this->isEditAllowed($comment)) {
214      array_push($infos, 'comment_edit_forbidden');
215      $this->displayError($infos);
216      return;
217    }
218    $image_id = $comment->getInfo('image_id');
219    if (intval($_POST['ce_imageid']) != intval($image_id)) {
220      array_push($infos, 'comment_access_invalid');
221      $this->displayError($infos);
222      return;
223    }
224    if (isset($_POST['ce_author'])) {
225      $comment->setInfo('author', $_POST['ce_author']);
226    } else if ((isset($_POST['ce_freeauthorck'])) and
227               (isset($_POST['ce_freeauthor']))) {
228      // Free author
229      $new_author = trim(stripslashes($_POST['ce_freeauthor']));
230      $new_author = $comment->validateAuthor($new_author);
231      $comment->setInfo('author', $new_author);
232    }
233
234    $comment_array = array(
235      'author'      => trim( stripslashes($comment->getInfo('author')) ),
236      'content'     => trim( stripslashes($_POST['ce_content']) ),
237      'image_id'    => $image_id,
238      'comment_id'  => $comment->getInfo('comment_id'),
239    );
240
241    // Deactivate anti-flood system
242    global $conf;
243    $old_anti_flood = $conf['anti-flood_time'];
244    $conf['anti-flood_time'] = 0;
245
246    $comment_action = update_user_comment($comment_array, @$_POST['key'], $infos );
247
248    // Reactivate anti-flood system
249    $conf['anti-flood_time'] = $old_anti_flood;
250
251    switch ($comment_action) {
252      case 'moderate':
253        array_push( $infos, 'comment_to_validate' );
254      case 'validate':
255        array_push( $infos, 'comment_added');
256        break;
257      case 'reject':
258        set_status_header(403);
259        array_push($infos, 'comment_not_added' );
260        break;
261      default:
262        trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING);
263    }
264
265    // allow plugins to notify what's going on
266    trigger_action( 'user_comment_insertion',
267                    array_merge($comment_array, array('action'=>$comment_action)));
268    if ($comment_action=='reject') {
269      $this->displayError($infos);
270    } else {
271      $this->displayInfo($infos);
272    }
273  }
274
275  function getCommentId($p_string_delete) {
276    $comment_id = "";
277    $elements = split('\?', $p_string_delete);
278    if (count($elements) > 1) {
279      $elem_var = split('\&amp;', $elements[1]);
280      foreach ($elem_var as $var) {
281        $key_value = split('=', $var);
282        if (count($key_value) > 1) {
283          switch ($key_value[0]) {
284            case 'comment_to_delete':
285            case 'delete':
286              $comment_id = $key_value[1];
287              break;
288            default:
289              // No ID
290          }
291        }
292      }
293    }
294    return $comment_id;
295  }
296
297  function displayError($p_error) {
298    if ($this->config->getValue(CE_CFG_DISPLAY_ERROR)) {
299        $this->displayMessage($p_error, CE_TYPE_ERROR, CE_ERRORS);
300    }
301  }
302
303  function displayInfo($p_info) {
304    if ($this->config->getValue(CE_CFG_DISPLAY_INFO)) {
305      $this->displayMessage($p_info, CE_TYPE_INFO, CE_INFOS);
306    }
307  }
308
309  function displayMessage($p_message, $p_type, $p_image) {
310    // Include language advices
311    load_language('plugin.lang', CE_PATH);
312
313    $messages = array();
314    foreach ($p_message as $message) {
315      array_push($messages, l10n($message));
316    }
317    $message_array = array(
318      'TYPE'  => $p_type,
319      'IMG'   => $p_image,
320      'TITLE' => l10n($p_type),
321      'MSG'   => $messages
322    );
323
324    global $template;
325    if ("" == $template->get_template_vars('U_HOME')) {
326      $template->assign('U_HOME', make_index_url());
327    }
328    $template->set_filenames(array('ce_message'=>$this->getTemplate('message.tpl')));
329    $template->block_html_head( '',
330                  '<link rel="stylesheet" type="text/css" href="'
331                  .CE_INCLUDE . $this->getScriptName() . '.css'
332                  .'">',
333    $smarty, $repeat);
334    if (!$this->setBeginEndFields($begin, $end)) {
335      return;
336    }
337
338    $template->assign('message', $message_array);
339    $old_begin = $template->get_template_vars($begin);
340    $template->assign($begin, $template->parse('ce_message', true));
341    $template->concat($begin, $old_begin);
342  }
343
344  function displayComment($p_comment) {
345    // Include language advices
346    load_language('plugin.lang', CE_PATH);
347
348    global $template;
349    $template->set_filenames(array('ce_edit'=>$this->getTemplate()));
350    $template->block_html_head( '',
351                  '<link rel="stylesheet" type="text/css" href="'
352                  .CE_INCLUDE . $this->getScriptName() . '.css'
353                  .'">',
354    $smarty, $repeat);
355
356    if (!$this->setBeginEndFields($begin, $end)) {
357      return;
358    }
359
360    $tpl_comment = array(
361      'CONTENT'   => $p_comment->getInfo('content'),
362      'KEY'       => $p_comment->getInfo('comment_id'),
363      'AUTHOR'    => $p_comment->getInfo('author'),
364      'IMAGE'     => $p_comment->getInfo('image_id'),
365      'DISABLED'  => !is_admin(),
366      'USERS'     => CE_Comment::getUsers(),
367      'F_ACTION'  => $this->getUpdateUrl(),
368      'FREEAUTHOR'=> ($p_comment->isKnownAuthor())?'':'checked="checked"'
369      );
370      if ("" == $template->get_template_vars('U_HOME')) {
371        $template->assign('U_HOME', make_index_url());
372      }
373      $template->assign('comment', $tpl_comment);
374      $old_begin = $template->get_template_vars($begin);
375      $template->assign($begin, $template->parse('ce_edit', true));
376      $template->concat($begin, $old_begin);
377  }
378
379  function setBeginEndFields(&$begin, &$end) {
380    switch (script_basename()) {
381      case 'index':
382        $begin = 'PLUGIN_INDEX_CONTENT_BEFORE';
383        $end = 'PLUGIN_INDEX_CONTENT_AFTER';
384        break;
385      case 'picture':
386        $begin = 'PLUGIN_PICTURE_BEFORE';
387        $end = 'PLUGIN_PICTURE_AFTER';
388        break;
389      default:
390        return FALSE;
391    }
392    return TRUE;
393  }
394
395  function getScriptName() {
396    switch (script_basename()) {
397      case 'index':
398      case 'picture':
399        return script_basename();
400      default:
401        return 'index';
402    }
403  }
404
405  function getEditUrl($comment_id) {
406    $url  = get_root_url() . $this->getScriptName() . '.php';
407    $url .= get_query_string_diff(array(CE_ACTION, CE_ID));
408    $url = add_url_params($url,
409                          array(
410                            CE_ACTION => CE_ACTION_EDIT,
411                            CE_ID=>$comment_id
412                          )
413            );
414    return $url;
415  }
416
417  function getUpdateUrl() {
418    $url  = get_root_url() . $this->getScriptName() .'.php';
419    $url .= get_query_string_diff(array(CE_ACTION, CE_ID));
420    $url = add_url_params($url,
421                          array(
422                            CE_ACTION => CE_ACTION_UPDATE
423                          )
424            );
425    return $url;
426  }
427
428  function get_plugin_admin_url() {
429    return get_admin_plugin_menu_link(CE_PATH . 'administration.php');
430  }
431
432}
433?>
Note: See TracBrowser for help on using the repository browser.