Skip to content
Newer
Older
100755 359 lines (312 sloc) 11.9 KB
25e8713 @chrisgraham first commit
authored Aug 15, 2011
1 <?php /*
2
3 ocPortal
9014b71 @chrisgraham Updated copyright dates
authored Jan 9, 2012
4 Copyright (c) ocProducts, 2004-2012
25e8713 @chrisgraham first commit
authored Aug 15, 2011
5
6 See text/EN/licence.txt for full licencing information.
7
8
9 NOTE TO PROGRAMMERS:
10 Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
11 **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****
12
13 */
14
15 /**
16 * @license http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
17 * @copyright ocProducts Ltd
18 * @package core_ocf
19 */
20
21 /**
22 * Standard code module initialisation function.
23 */
24 function init__ocf_groups()
25 {
26 global $USER_GROUPS_CACHED;
27 $USER_GROUPS_CACHED=array();
28
29 global $GROUP_MEMBERS_CACHE;
30 $GROUP_MEMBERS_CACHE=array();
c6716e0 @chrisgraham Removed tabs off end of file lines
authored Jun 4, 2012
31
25e8713 @chrisgraham first commit
authored Aug 15, 2011
32 global $PROBATION_GROUP;
33 $PROBATION_GROUP=NULL;
34
35 global $ALL_DEFAULT_GROUPS;
36 $ALL_DEFAULT_GROUPS=array();
37 }
38
39 /**
b64ef37 Lots of minor fixes
Chris Graham authored Jul 10, 2012
40 * Get a nice list for selection from the usergroups. Suitable for admin use only (does not check hidden status).
25e8713 @chrisgraham first commit
authored Aug 15, 2011
41 *
42 * @param ?AUTO_LINK Usergroup selected by default (NULL: no specific default).
43 * @return tempcode The list.
44 */
45 function ocf_nice_get_usergroups($it=NULL)
46 {
47 $group_count=$GLOBALS['FORUM_DB']->query_value('f_groups','COUNT(*)');
b64ef37 Lots of minor fixes
Chris Graham authored Jul 10, 2012
48 $_m=$GLOBALS['FORUM_DB']->query_select('f_groups',array('id','g_name'),($group_count>200)?array('g_is_private_club'=>0):NULL,'ORDER BY g_order');
25e8713 @chrisgraham first commit
authored Aug 15, 2011
49 $entries=new ocp_tempcode();
50 foreach ($_m as $m)
51 {
52 $entries->attach(form_input_list_entry(strval($m['id']),$it===$m['id'],get_translated_text($m['g_name'],$GLOBALS['FORUM_DB'])));
53 }
54
55 return $entries;
56 }
57
58 /**
59 * Find the first default group.
60 *
61 * @return GROUP The first default group.
62 */
63 function get_first_default_group()
64 {
65 $default_groups=ocf_get_all_default_groups(true);
66 return array_pop($default_groups);
67 }
68
69 /**
70 * Get a list of the default usergroups (the usergroups a member is put in when they join).
71 *
72 * @param boolean Whether to include the default primary.
73 * @return array The list of default IDs.
74 */
75 function ocf_get_all_default_groups($include_primary=false)
76 {
77 global $ALL_DEFAULT_GROUPS;
78 if (array_key_exists($include_primary?1:0,$ALL_DEFAULT_GROUPS)) return $ALL_DEFAULT_GROUPS[$include_primary?1:0];
c6716e0 @chrisgraham Removed tabs off end of file lines
authored Jun 4, 2012
79
96cd8db @chrisgraham Fixed various signup bugs
authored Dec 31, 2011
80 $rows=$GLOBALS['FORUM_DB']->query_select('f_groups',array('id'),array('g_is_default'=>1,'g_is_presented_at_install'=>0),'ORDER BY g_order');
25e8713 @chrisgraham first commit
authored Aug 15, 2011
81 $groups=collapse_1d_complexity('id',$rows);
82 if ($include_primary)
83 {
84 $test=$GLOBALS['FORUM_DB']->query_value_null_ok('f_groups','id',array('id'=>db_get_first_id()+8));
85 if (!is_null($test))
86 $groups[]=db_get_first_id()+8;
87 }
88 $ALL_DEFAULT_GROUPS[$include_primary?1:0]=$groups;
89 return $groups;
90 }
91
92 /**
93 * Ensure a list of usergroups are cached in memory.
94 *
95 * @param mixed The list of usergroups (array) or '*'.
96 */
97 function ocf_ensure_groups_cached($groups)
98 {
99 global $USER_GROUPS_CACHED;
100
101 if ($groups==='*')
102 {
103 $group_count=$GLOBALS['FORUM_DB']->query_value('f_groups','COUNT(*)');
104 $rows=$GLOBALS['FORUM_DB']->query_select('f_groups',array('*'),($group_count>200)?array('g_is_private_club'=>0):NULL);
105 foreach ($rows as $row)
106 {
107 $row['g__name']=get_translated_text($row['g_name'],$GLOBALS['FORUM_DB']);
108 $row['g__title']=get_translated_text($row['g_title'],$GLOBALS['FORUM_DB']);
109 $USER_GROUPS_CACHED[$row['id']]=$row;
110 }
111 return;
112 }
113
114 $groups_to_load='';
115 $counter=0;
116 foreach ($groups as $group)
117 {
118 if (!array_key_exists($group,$USER_GROUPS_CACHED))
119 {
120 if ($groups_to_load!='') $groups_to_load.=' OR ';
121 $groups_to_load.='g.id='.strval($group);
122 $counter++;
123 }
124 }
125 if ($counter==0) return;
126 $extra_groups=$GLOBALS['FORUM_DB']->query('SELECT g.* FROM '.$GLOBALS['FORUM_DB']->get_table_prefix().'f_groups g WHERE '.$groups_to_load,NULL,NULL,false,false,array('g_name','g_title'));
127
128 if (count($extra_groups)!=$counter) warn_exit(do_lang_tempcode('MISSING_RESOURCE'));
129
130 //require_code('lang');
131 foreach ($extra_groups as $extra_group)
132 {
133 if (function_exists('get_translated_text'))
134 {
135 $extra_group['g__name']=get_translated_text($extra_group['g_name'],$GLOBALS['FORUM_DB']);
136 $extra_group['g__title']=get_translated_text($extra_group['g_title'],$GLOBALS['FORUM_DB']);
137 }
138
139 $USER_GROUPS_CACHED[$extra_group['id']]=$extra_group;
140 }
141 }
142
143 /**
144 * Get a rendered link to a usergroup.
145 *
146 * @param GROUP The ID of the group.
147 * @return tempcode The link.
148 */
149 function ocf_get_group_link($id)
150 {
151 $_row=$GLOBALS['FORUM_DB']->query_select('f_groups',array('*'),array('id'=>$id),'',1);
2c42f79 @chrisgraham misc fixes
authored Sep 3, 2011
152 if (!array_key_exists(0,$_row)) return make_string_tempcode(do_lang('UNKNOWN'));
25e8713 @chrisgraham first commit
authored Aug 15, 2011
153 $row=$_row[0];
c6716e0 @chrisgraham Removed tabs off end of file lines
authored Jun 4, 2012
154
25e8713 @chrisgraham first commit
authored Aug 15, 2011
155 if ($row['id']==db_get_first_id()) return make_string_tempcode(escape_html(get_translated_text($row['g_name'],$GLOBALS['FORUM_DB'])));
c6716e0 @chrisgraham Removed tabs off end of file lines
authored Jun 4, 2012
156
fdee17c @chrisgraham Full Code Quality check (including on addons, fixing most issues)
authored Nov 22, 2011
157 $name=ocf_get_group_name($row['id']);
25e8713 @chrisgraham first commit
authored Aug 15, 2011
158
159 $see_hidden=has_specific_permission(get_member(),'see_hidden_groups');
160 if ((!$see_hidden) && ($row['g_hidden']==1))
161 {
162 return make_string_tempcode(escape_html($name));
163 }
164
165 return hyperlink(build_url(array('page'=>'groups','type'=>'view','id'=>$row['id']),get_module_zone('groups')),$name,false,true);
166 }
167
168 /**
169 * Get a usergroup name.
170 *
171 * @param GROUP The ID of the group.
172 * @return string The usergroup name.
173 */
174 function ocf_get_group_name($group)
175 {
176 $name=ocf_get_group_property($group,'name');
177 if (is_string($name)) return $name;
178 return get_translated_text($name,$GLOBALS['FORUM_DB']);
179 }
180
181 /**
182 * Get a certain property of a certain.
183 *
184 * @param GROUP The ID of the group.
185 * @param ID_TEXT The identifier of the property.
186 * @return mixed The property value.
187 */
188 function ocf_get_group_property($group,$property)
189 {
190 ocf_ensure_groups_cached(array($group));
191 global $USER_GROUPS_CACHED;
c6716e0 @chrisgraham Removed tabs off end of file lines
authored Jun 4, 2012
192
25e8713 @chrisgraham first commit
authored Aug 15, 2011
193 if (($property=='name') && ($USER_GROUPS_CACHED[$group]['g_hidden']==1) && (!has_specific_permission(get_member(),'see_hidden_groups')))
194 {
195 return do_lang('UNKNOWN');
196 }
c6716e0 @chrisgraham Removed tabs off end of file lines
authored Jun 4, 2012
197
25e8713 @chrisgraham first commit
authored Aug 15, 2011
198 return $USER_GROUPS_CACHED[$group]['g_'.$property];
199 }
200
201 /**
202 * Get the best value of all values of a property for a member (due to members being in multiple usergroups).
203 *
204 * @param MEMBER The ID of the member.
205 * @param ID_TEXT The identifier of the property.
206 * @return mixed The property value.
207 */
208 function ocf_get_member_best_group_property($member_id,$property)
209 {
210 return ocf_get_best_group_property($GLOBALS['OCF_DRIVER']->get_members_groups($member_id,false,true),$property);
211 }
212
213 /**
214 * Get the best value of all values of a property for a list of usergroups.
215 *
216 * @param array The list of usergroups.
217 * @param ID_TEXT The identifier of the property.
218 * @return mixed The best property value ('best' is dependant on the property we are looking at).
219 */
220 function ocf_get_best_group_property($groups,$property)
221 {
222 $big_is_better=array('gift_points_per_day','gift_points_base','enquire_on_new_ips','is_super_admin','is_super_moderator','max_daily_upload_mb','max_attachments_per_post','max_avatar_width','max_avatar_height','max_post_length_comcode','max_sig_length_comcode');
223 //$small_and_perfectly_formed=array('flood_control_submit_secs','flood_control_access_secs'); Not needed by elimination, but nice to have here as a note
224
225 $go_super_size=in_array($property,$big_is_better);
226
227 global $USER_GROUPS_CACHED;
228 ocf_ensure_groups_cached($groups);
229 $best_value_so_far=0; // Initialise type to integer
230 $best_value_so_far=NULL;
231 foreach ($groups as $group)
232 {
233 $this_value=$USER_GROUPS_CACHED[$group]['g_'.$property];
234 if ((is_null($best_value_so_far)) ||
235 (($best_value_so_far<$this_value) && ($go_super_size)) ||
236 (($best_value_so_far>$this_value) && (!$go_super_size)))
237 $best_value_so_far=$this_value;
238 }
239 return $best_value_so_far;
240 }
241
242 /**
243 * Get a list of the usergroups a member is in (keys say the usergroups, values are irrelevant).
244 *
245 * @param ?MEMBER The member to find the usergroups of (NULL: current member).
246 * @param boolean Whether to skip looking at secret usergroups.
247 * @param boolean Whether to take probation into account
248 * @return array Reverse list (e.g. array(1=>1,2=>1,3=>1) for someone in (1,2,3)).
249 */
250 function ocf_get_members_groups($member_id=NULL,$skip_secret=false,$handle_probation=true)
251 {
252 if (is_guest($member_id))
253 {
254 $ret=array();
255 $ret[db_get_first_id()]=1;
256 return $ret;
257 }
f3c32ce @chrisgraham misc fixes
authored May 1, 2012
258
25e8713 @chrisgraham first commit
authored Aug 15, 2011
259 if (is_null($member_id)) $member_id=get_member();
260
177d9f9 Lots of fixes
Chris Graham authored Nov 8, 2011
261 if ($handle_probation)
25e8713 @chrisgraham first commit
authored Aug 15, 2011
262 {
177d9f9 Lots of fixes
Chris Graham authored Nov 8, 2011
263 $opt=$GLOBALS['FORUM_DRIVER']->get_member_row_field($member_id,'m_on_probation_until');
264 if ((!is_null($opt)) && ($opt>time()))
25e8713 @chrisgraham first commit
authored Aug 15, 2011
265 {
177d9f9 Lots of fixes
Chris Graham authored Nov 8, 2011
266 global $PROBATION_GROUP;
267 if (is_null($PROBATION_GROUP))
268 {
269 $probation_group=get_option('probation_usergroup');
270 $PROBATION_GROUP=$GLOBALS['FORUM_DB']->query_value_null_ok('f_groups g LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'translate t ON t.id=g.g_name','g.id',array('text_original'=>$probation_group));
271 if (is_null($PROBATION_GROUP)) $PROBATION_GROUP=false;
272 }
273 if ($PROBATION_GROUP!==false) return array($PROBATION_GROUP=>1);
25e8713 @chrisgraham first commit
authored Aug 15, 2011
274 }
275 }
276
277 global $GROUP_MEMBERS_CACHE;
278 if (isset($GROUP_MEMBERS_CACHE[$member_id][$skip_secret][$handle_probation])) return $GROUP_MEMBERS_CACHE[$member_id][$skip_secret][$handle_probation];
279
280 $groups=array();
281
282 // Now implicit usergroup hooks
283 $hooks=find_all_hooks('systems','ocf_implicit_usergroups');
284 foreach (array_keys($hooks) as $hook)
285 {
286 require_code('hooks/systems/ocf_implicit_usergroups/'.$hook);
287 $ob=object_factory('Hook_implicit_usergroups_'.$hook);
288 if ($ob->is_member_within($member_id)) $groups[$ob->get_bound_group_id()]=1;
289 }
290
291 require_code('ocf_members');
292 if ((!function_exists('ocf_is_ldap_member')/*can happen if said in safe mode and detecting safe mode when choosing whether to avoid a custom file via admin permission which requires this function to run*/) || (!ocf_is_ldap_member($member_id)))
293 {
d2af880 @chrisgraham misc fixes
authored May 6, 2012
294 $no_hidden=(($skip_secret) && ((/*For installer*/!function_exists('get_member')) || ($member_id!=get_member())) && ((!function_exists('has_specific_permission')) || (!has_specific_permission(get_member(),'see_hidden_groups'))));
25e8713 @chrisgraham first commit
authored Aug 15, 2011
295 $_groups=$GLOBALS['FORUM_DB']->query_select('f_group_members m LEFT JOIN '.$GLOBALS['FORUM_DB']->get_table_prefix().'f_groups g ON g.id=m.gm_group_id',array('gm_group_id','g_hidden'),array('gm_member_id'=>$member_id,'gm_validated'=>1),'ORDER BY g.g_order');
296 foreach ($_groups as $group)
297 $groups[$group['gm_group_id']]=1;
298 if (!isset($GLOBALS['OCF_DRIVER'])) // We didn't init fully (MICRO_BOOTUP), but now we dug a hole - get out of it
299 {
300 if (method_exists($GLOBALS['FORUM_DRIVER'],'forum_layer_initialise')) $GLOBALS['FORUM_DRIVER']->forum_layer_initialise();
301 }
302 $primary_group=$GLOBALS['OCF_DRIVER']->get_member_row_field($member_id,'m_primary_group');
303 if (is_null($primary_group)) $primary_group=db_get_first_id();
304 $groups[$primary_group]=1;
305 foreach (array_keys($groups) as $group_id)
306 {
307 $groups[$group_id]=1;
308 }
309
310 $GROUP_MEMBERS_CACHE[$member_id][false][$handle_probation]=$groups;
311 $groups2=$groups;
312 foreach ($_groups as $group)
313 {
314 if ($group['g_hidden']==1)
315 unset($groups2[$group['gm_group_id']]);
316 }
317 $GROUP_MEMBERS_CACHE[$member_id][true][$handle_probation]=$groups2;
318 if ($no_hidden) $groups=$groups2;
319 } else
320 {
321 $groups=ocf_get_members_groups_ldap($member_id);
322 $GROUP_MEMBERS_CACHE[$member_id][false][$handle_probation]=$groups;
323 $GROUP_MEMBERS_CACHE[$member_id][true][$handle_probation]=$groups;
c6716e0 @chrisgraham Removed tabs off end of file lines
authored Jun 4, 2012
324
25e8713 @chrisgraham first commit
authored Aug 15, 2011
325 // Mirror to f_group_members table, so direct queries will also get it (we need to do listings of group members, for instance)
326 $GLOBALS['FORUM_DB']->query_delete('f_group_members',array('gm_member_id'=>$member_id));
327 foreach (array_keys($groups) as $group_id)
328 {
329 $GLOBALS['FORUM_DB']->query_insert('f_group_members',array(
330 'gm_group_id'=>$group_id,
331 'gm_member_id'=>$member_id,
332 'gm_validated'=>1
333 ));
334 }
335 }
336
337 return $groups;
338 }
339
0c4a19e @chrisgraham Lots of little fixes
authored Feb 6, 2012
340 /**
341 * Get the ID for a usergroup if we only know the title. Warning: Only use this with custom code, never core code! It assumes a single language and that usergroups aren't renamed.
342 *
343 * @param SHORT_TEXT The title.
344 * @return ?AUTO_LINK The ID (NULL: could not find).
345 */
346 function find_usergroup_id($title)
347 {
348 $usergroups=$GLOBALS['FORUM_DRIVER']->get_usergroup_list();
349 foreach ($usergroups as $id=>$usergroup)
350 {
351 if ($usergroup==$title)
352 {
353 return $id;
354 }
355 }
356 return NULL;
357 }
25e8713 @chrisgraham first commit
authored Aug 15, 2011
358
Something went wrong with that request. Please try again.