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

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

-- Features refactory --
When "Comments for all" is disabled:

  • Users in a specified group can post comments without admin validation when admin validation is enabled.
  • Nickname is mandatory for guests comments

When "Comments for all" is enabled:

  • Users in a specified group can post comments without admin validation when admin validation is enabled.
  • Only a specified group can post comments

-- Admin panel refactory --
The admin panel displays option related with the state of "Comments for all" option.

New version 2.2.2 hard coded for publication

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