source: extensions/Comments_Access_Manager/include/functions.inc.php @ 11069

Last change on this file since 11069 was 11069, checked in by Eric, 13 years ago

Add new feature : Users in a specified group can post comments without admin validation when "Comments for all" is enabled and admin validation is enabled.
New version 2.2.1 hard coded for publication

  • Property svn:eol-style set to LF
File size: 6.4 KB
Line 
1<?php
2load_language('plugin.lang', CM_PATH);
3
4
5/**
6 * Triggered on get_admin_plugin_menu_links
7 *
8 * Plugin's administration menu
9 */
10function CM_admin_menu($menu)
11{
12// +-----------------------------------------------------------------------+
13// |                      Getting plugin name                              |
14// +-----------------------------------------------------------------------+
15  $plugin =  CM_Infos(CM_PATH);
16  $name = $plugin['name'];
17 
18  array_push($menu,
19    array(
20      'NAME' => $name,
21      'URL' => get_root_url().'admin.php?page=plugin-'.basename(CM_PATH)
22    )
23  );
24
25  return $menu;
26}
27
28
29/**
30 * checks if author is mandatory and set on comments post when comments for all is set
31 *
32 * cheks if author is in an allowed group to post comment when comments for all is not set
33 *
34 * @param : comment action, comment
35 *
36 * @return : comment action
37 *
38 */
39function CM_CheckCommentAuthor($comment_action, $comm)
40{
41  load_language('plugin.lang', CM_PATH);
42  global $infos, $conf, $user;
43
44  $conf_CM = unserialize($conf['CommentsManager']);
45
46// Does not allow empty author name on comments for all
47  if (isset($conf_CM[1]) and $conf_CM[1] == 'true' and $comm['author'] == 'guest' and $conf['comments_forall'])
48  {
49    $comment_action = 'reject';
50
51    array_push($infos, l10n('CM_Empty Author'));
52  }
53
54// Rules on comments NOT for all
55  if (!$conf['comments_forall'] and !is_admin())
56  {
57    if ((isset($conf_CM[2]) and $conf_CM[2] == 'true') and (isset($conf_CM[4]) and $conf_CM[4] == 'false') and !CM_CheckAuthor($comm['author'])) // Comments authorized group set - Auto validation group unset
58    {
59      $comment_action = 'reject'; // Comment rejected if author is not in the allowed group
60      array_push($infos, l10n('CM_Not_Allowed_Author'));
61    }
62    elseif ((isset($conf_CM[2]) and $conf_CM[2] == 'false') and (isset($conf_CM[4]) and $conf_CM[4] == 'true') and $conf['comments_validation']) // Comments authorized group unset - Auto validation group set
63    {
64      if (CM_CheckValidGroup($comm['author']))
65      {
66        $comment_action = 'validate'; // Comment is validated if author is not in the validated group
67      }
68      else
69      {
70        $comment_action = 'moderate'; // Comment needs moderation if author is not in the validated group
71      }
72    }
73    elseif ((isset($conf_CM[2]) and $conf_CM[2] == 'true') and (isset($conf_CM[4]) and $conf_CM[4] == 'true') and $conf['comments_validation']) // Comments authorized group set - Auto validation group set
74    {
75      if (!CM_CheckAuthor($comm['author']))
76      {
77        $comment_action = 'reject'; // Comment rejected if author is not in the allowed group
78        array_push($infos, l10n('CM_Not_Allowed_Author'));
79      }
80      elseif (CM_CheckValidGroup($comm['author']))
81      {
82        $comment_action = 'validate'; // Comment is validated if author is not in the validated group
83      }
84      else
85        $comment_action = 'moderate'; // Comment needs moderation if author is not in the validated group
86    }
87  }
88
89  return $comment_action;
90}
91
92
93/**
94 * Checks if comment's author name is in the allowed group
95 *
96 * @author   : author's name
97 *
98 * @returns  : Boolean (true is user is allowed to post / false if not allowed)
99 *
100 */
101function CM_CheckAuthor($author)
102{
103  global $conf;
104 
105        // Get CM configuration
106  $conf_CM = unserialize($conf['CommentsManager']);
107 
108  if (isset($conf_CM[3]) and $conf_CM[3] <> -1)
109  {
110    $query = '
111SELECT u.id,
112       u.username,
113       ug.user_id,
114       ug.group_id
115FROM '.USERS_TABLE.' AS u
116  INNER JOIN '.USER_GROUP_TABLE.' AS ug
117    ON u.id = ug.user_id
118WHERE u.username LIKE "'.$author.'"
119  AND ug.group_id = '.$conf_CM[3].'
120;';
121
122    $count = pwg_db_num_rows(pwg_query($query));
123
124    if (is_null($count) or $count == 0)
125    {
126      return false;
127    }
128    else
129      return true;
130  }
131}
132
133
134/**
135 * Checks if comment's author name is in the admin's pre-validated group
136 * avoid admins to validate comments for the members of this group
137 *
138 * @author   : author's name
139 *
140 * @returns  : Boolean (true if user's comment doesn't need validation / false if user's comment is moderated)
141 *
142 */
143function CM_CheckValidGroup($author)
144{
145  global $conf;
146 
147        // Get CM configuration
148  $conf_CM = unserialize($conf['CommentsManager']);
149 
150  if (isset($conf_CM[5]) and $conf_CM[5] <> -1)
151  {
152    $query = '
153SELECT u.id,
154       u.username,
155       ug.user_id,
156       ug.group_id
157FROM '.USERS_TABLE.' AS u
158  INNER JOIN '.USER_GROUP_TABLE.' AS ug
159    ON u.id = ug.user_id
160WHERE u.username LIKE "'.$author.'"
161  AND ug.group_id = '.$conf_CM[5].'
162;';
163
164    $count = pwg_db_num_rows(pwg_query($query));
165
166    if (is_null($count) or $count == 0)
167    {
168      return false;
169    }
170    else
171      return true;
172  }
173}
174
175
176/**
177 * Get the plugin version and name
178 *
179 * @param : plugin directory
180 *
181 * @return : plugin's version and name
182 *
183 */
184function CM_Infos($dir)
185{
186  $path = $dir;
187
188  $plg_data = implode( '', file($path.'main.inc.php') );
189  if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
190  {
191    $plugin['name'] = trim( $val[1] );
192  }
193  if (preg_match("|Version: (.*)|", $plg_data, $val))
194  {
195    $plugin['version'] = trim($val[1]);
196  }
197  if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
198  {
199    $plugin['uri'] = trim($val[1]);
200  }
201  if ($desc = load_language('description.txt', $path.'/', array('return' => true)))
202  {
203    $plugin['description'] = trim($desc);
204  }
205  elseif ( preg_match("|Description: (.*)|", $plg_data, $val) )
206  {
207    $plugin['description'] = trim($val[1]);
208  }
209  if ( preg_match("|Author: (.*)|", $plg_data, $val) )
210  {
211    $plugin['author'] = trim($val[1]);
212  }
213  if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
214  {
215    $plugin['author uri'] = trim($val[1]);
216  }
217  if (!empty($plugin['uri']) and strpos($plugin['uri'] , 'extension_view.php?eid='))
218  {
219    list( , $extension) = explode('extension_view.php?eid=', $plugin['uri']);
220    if (is_numeric($extension)) $plugin['extension'] = $extension;
221  }
222// IMPORTANT SECURITY !
223  $plugin = array_map('htmlspecialchars', $plugin);
224
225  return $plugin ;
226}
227
228
229/**
230 * Delete obsolete files on plugin upgrade
231 * Obsolete files are listed in file obsolete.list
232 *
233 */
234function CM_Obsolete_Files()
235{
236  if (file_exists(CM_PATH.'obsolete.list')
237    and $old_files = file(CM_PATH.'obsolete.list', FILE_IGNORE_NEW_LINES)
238    and !empty($old_files))
239  {
240    array_push($old_files, 'obsolete.list');
241    foreach($old_files as $old_file)
242    {
243      $path = CM_PATH.$old_file;
244      if (is_file($path))
245      {
246        @unlink($path);
247      }
248    }
249  }
250}
251?>
Note: See TracBrowser for help on using the repository browser.