root/include/functions_admin.php

Revision 300, 19.2 kB (checked in by Nafania, 2 years ago)

--

Line 
1 <?php
2 /***************************************************************************</span>
3 <span class="code-comment"> *                            functions_admin.php
4  *                            -------------------
5  *   begin                : Saturday, Feb 13, 2001
6  *   copyright            : (C) 2001 The phpBB Group
7  *   email                : support@phpbb.com
8  *
9  *   $Id: functions_admin.php,v 1.5.2.5 2005/09/14 19:16:21 acydburn Exp $
10  *
11  *
12  ***************************************************************************/
13
14 /***************************************************************************
15  *
16  *   This program is free software; you can redistribute it and/or modify
17  *   it under the terms of the GNU General Public License as published by
18  *   the Free Software Foundation; either version 2 of the License, or
19  *   (at your option) any later version.
20  *
21  *
22  ***************************************************************************/
23
24 //
25 // Simple version of jumpbox, just lists authed forums
26 //
27 function make_forum_select($box_name, $ignore_forum = false, $select_forum = '')</span>
28 <span class="code-keyword">{
29     global $db, $userdata;
30
31     $is_auth_ary = auth(AUTH_READ, AUTH_LIST_ALL, $userdata);
32
33     $sql = 'SELECT f.forum_id, f.forum_name, f.forum_parent
34         FROM ' . FORUM_CATEGORIES_TABLE . ' c, ' . FORUMS_TABLE . ' f
35         WHERE f.cat_id = c.cat_id
36         ORDER BY c.cat_order, f.forum_order';
37     $result = $db->sql_query($sql);
38
39     // Begin Simple Subforums MOD
40     $list = array();
41     // End Simple Subforums MOD
42     while( $row = $db->sql_fetchrow($result) )
43     {
44             // Begin Simple Subforums MOD
45         $list[] = $row;
46     }
47     $forum_list = '';
48     for( $i = 0; $i < count($list); $i++ )
49     {
50         if( !$list[$i]['forum_parent'] )
51         {
52             $row = $list[$i];
53             $parent_hidden = true;
54     // End Simple Subforums MOD
55         if ( $is_auth_ary[$row['forum_id']]['auth_read'] && $ignore_forum != $row['forum_id'] )
56         {
57             $selected = ( $select_forum == $row['forum_id'] ) ? ' selected="selected"' : '';
58             $forum_list .= '<option value="' . $row['forum_id'] . '"' . $selected .'>' . $row['forum_name'] . '</option>';
59             // Begin Simple Subforums MOD
60                 $parent_hidden = false;
61             }
62             if ( $is_auth_ary[$row['forum_id']]['auth_read'] )
63             {
64                 $parent_id = $row['forum_id'];
65                 for($j=0; $j<count($list); $j++)
66                 {
67                     $row = $list[$j];
68                     if( $row['forum_parent'] == $parent_id && $is_auth_ary[$row['forum_id']]['auth_read'] && $ignore_forum != $row['forum_id'] )
69                     {
70                         if( $parent_hidden )
71                         {
72                             $forum_list .= '<option value="" disabled="disabled">' . $list[$i]['forum_name'] . '</option>';
73                             $parent_hidden = false;
74                         }
75                         $selected = ( $select_forum == $row['forum_id'] ) ? ' selected="selected"' : '';
76                         $forum_list .= '<option value="' . $row['forum_id'] . '"' . $selected .'>-- ' . $row['forum_name'] . '</option>';
77                     }
78                 }
79             }
80             // End Simple Subforums MOD
81         }
82     }
83
84     $forum_list = ( $forum_list == '' ) ? '<option value="-1">-- ! No Forums ! --</option>' : '<select name="' . $box_name . '">' . $forum_list . '</select>';
85
86     return $forum_list;
87 }
88
89 //</span>
90 <span class="code-comment">// Synchronise functions for forums/topics
91 //
92 function sync($type, $id = false)</span>
93 <span class="code-keyword">{
94     global $db;
95
96     switch($type)
97     {
98         case 'all forums':
99             $sql = "SELECT forum_id
100                 FROM " . FORUMS_TABLE;
101             $result = $db->sql_query($sql);
102
103             while( $row = $db->sql_fetchrow($result) )
104             {
105                 sync('forum', $row['forum_id']);
106             }
107                break;
108
109         case 'all topics':
110             $sql = "SELECT topic_id
111                 FROM " . TOPICS_TABLE;
112             $result = $db->sql_query($sql);
113
114             while( $row = $db->sql_fetchrow($result) )
115             {
116                 sync('topic', $row['topic_id']);
117             }
118             break;
119
120           case 'forum':
121             $sql = "SELECT MAX(post_id) AS last_post, COUNT(post_id) AS total
122                 FROM " . POSTS_TABLE . "
123                 WHERE forum_id = $id";
124             $result = $db->sql_query($sql);
125
126             if ( $row = $db->sql_fetchrow($result) )
127             {
128                 $last_post = ( $row['last_post'] ) ? $row['last_post'] : 0;
129                 $total_posts = ($row['total']) ? $row['total'] : 0;
130             }
131             else
132             {
133                 $last_post = 0;
134                 $total_posts = 0;
135             }
136
137             $sql = "SELECT COUNT(topic_id) AS total
138                 FROM " . TOPICS_TABLE . "
139                 WHERE forum_id = $id";
140             $result = $db->sql_query($sql);
141
142             $total_topics = ( $row = $db->sql_fetchrow($result) ) ? ( ( $row['total'] ) ? $row['total'] : 0 ) : 0;
143
144             $sql = 'SELECT post_time FROM ' . POSTS_TABLE . ' WHERE post_id = ' . $last_post;
145             $result = $db->sql_query($sql);
146             $last_post_time = ( $subrow = $db->sql_fetchrow($result) ) ? $subrow['post_time'] : 0;
147
148             $sql = "UPDATE " . FORUMS_TABLE . "
149                 SET forum_last_post_id = $last_post, forum_posts = $total_posts, forum_topics = $total_topics, forum_last_post_time = $last_post_time
150                 WHERE forum_id = $id";
151             $db->sql_query($sql);
152             break;
153
154         case 'topic':
155             $sql = "SELECT MAX(p.post_id) AS last_post, MIN(p.post_id) AS first_post, COUNT(p.post_id) AS total_posts
156                 FROM " . POSTS_TABLE . " p
157                 WHERE p.topic_id = $id";
158             $result = $db->sql_query($sql);
159
160             if ( $row = $db->sql_fetchrow($result) )
161             {
162
163                 $sql = 'SELECT post_time FROM ' . POSTS_TABLE . ' WHERE post_id = ' . $row['last_post'];
164                 $result = $db->sql_query($sql);
165                 $last_post_time = ( $subrow = $db->sql_fetchrow($result) ) ? $subrow['post_time'] : 0;
166
167                 if ($row['total_posts'])
168                 {
169                     // Correct the details of this topic
170                     $sql = 'UPDATE ' . TOPICS_TABLE . '
171                         SET topic_replies = ' . ($row['total_posts'] - 1) . ',
172                         topic_first_post_id = ' . $row['first_post'] . ',
173                         topic_last_post_id = ' . $row['last_post'] . ",
174                         topic_last_post_time = " . $last_post_time . "
175                         WHERE topic_id = $id";
176
177                     $db->sql_query($sql);
178                 }
179                 else
180                 {
181                     // There are no replies to this topic
182                     // Check if it is a move stub
183                     $sql = 'SELECT topic_moved_id
184                         FROM ' . TOPICS_TABLE . "
185                         WHERE topic_id = $id";
186
187                     $result = $db->sql_query($sql);
188
189                     if ($row = $db->sql_fetchrow($result))
190                     {
191                         if (!$row['topic_moved_id'])
192                         {
193                             $sql = 'DELETE FROM ' . TOPICS_TABLE . " WHERE topic_id = $id";
194
195                             $db->sql_query($sql);
196                         }
197                     }
198
199                     $db->sql_freeresult($result);
200                 }
201             }
202             break;
203     }
204
205     return true;
206 }
207
208 /**</span>
209 <span class="code-comment">* Build select field options in acp pages
210 */
211 function build_select($option_ary, $option_default = false)</span>
212 <span class="code-keyword">{
213     global $user;
214
215     $html = '';
216     foreach ($option_ary as $value => $title)
217     {
218         $selected = ($option_default !== false && $value == $option_default) ? ' selected="selected"' : '';
219         $html .= '<option value="' . $value . '"' . $selected . '>' . $lang[$title] . '</option>';
220     }
221
222     return $html;
223 }
224
225 /**</span>
226 <span class="code-comment">* Build radio fields in acp pages
227 */
228 function h_radio($name, &$input_ary, $input_default = false, $id = false, $key = false)</span>
229 <span class="code-keyword">{
230     global $lang;
231
232     $html = '';
233     $id_assigned = false;
234     foreach ($input_ary as $value => $title)
235     {
236         $selected = ($input_default !== false && $value == $input_default) ? ' checked="checked"' : '';
237         $html .= '<label><input type="radio" name="' . $name . '"' . (($id && !$id_assigned) ? ' id="' . $id . '"' : '') . ' value="' . $value . '"' . $selected . (($key) ? ' accesskey="' . $key . '"' : '') . ' class="radio" /> ' . $lang[$title] . '</label>';
238         $id_assigned = true;
239     }
240
241     return $html;
242 }
243
244 /**</span>
245 <span class="code-comment">* Build configuration template for acp configuration pages
246 */
247 function build_cfg_template($tpl_type, $key, &$new, $config_key, $vars)</span>
248 <span class="code-keyword">{
249     global $lang;
250
251     $tpl = '';
252     $name = 'config[' . $config_key . ']';
253
254     switch ($tpl_type[0])
255     {
256         case 'text':
257         case 'password':
258             $size = (int) $tpl_type[1];
259             $maxlength = (int) $tpl_type[2];
260
261             $tpl = '<input id="' . $key . '" type="' . $tpl_type[0] . '"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="' . $name . '" value="' . $new[$config_key] . '" />';
262         break;
263
264         case 'text_ary':
265             $size = (int) $tpl_type[1];
266             $maxlength = (int) $tpl_type[2];
267             preg_match('/\[(.*?)\]/', $config_key, $matches);
268             $name = 'config[' . preg_replace('/\[(.*?)\]/', '', $config_key) . '][' . $matches[1] . ']';
269             $new[$config_key] = ( isset($new[$config_key]) ? $new[$config_key] : '' );
270
271             $tpl = '<input id="' . $key . '" type="text"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="' . $name . '" value="' . $new[$config_key] . '" />';
272         break;
273
274         case 'dimension':
275             $size = (int) $tpl_type[1];
276             $maxlength = (int) $tpl_type[2];
277
278             $tpl = '<input id="' . $key . '" type="text"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="config[' . $config_key . '_width]" value="' . $new[$config_key . '_width'] . '" /> x <input type="text"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="config[' . $config_key . '_height]" value="' . $new[$config_key . '_height'] . '" />';
279         break;
280
281         case 'textarea':
282             $rows = (int) $tpl_type[1];
283             $cols = (int) $tpl_type[2];
284
285             $tpl = '<textarea id="' . $key . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' . $new[$config_key] . '</textarea>';
286         break;
287
288         case 'radio':
289             $key_yes    = ( $new[$config_key]) ? ' checked="checked"' : '';
290             $key_no    = ( !$new[$config_key]) ? ' checked="checked"' : '';
291
292             $tpl_type_cond = explode('_', $tpl_type[1]);
293             $type_no = ($tpl_type_cond[0] == 'disabled' || $tpl_type_cond[0] == 'enabled') ? false : true;
294
295             $tpl_no = '<label><input type="radio" name="' . $name . '" value="0"' . $key_no . ' class="radio" /> ' . (($type_no) ? $lang['no'] : $lang['disabled']) . '</label>';
296             $tpl_yes = '<label><input type="radio" id="' . $key . '" name="' . $name . '" value="1"' . $key_yes . ' class="radio" /> ' . (($type_no) ? $lang['yes'] : $lang['enabled']) . '</label>';
297
298             $tpl = ($tpl_type_cond[0] == 'yes' || $tpl_type_cond[0] == 'enabled') ? $tpl_yes . $tpl_no : $tpl_no . $tpl_yes;
299         break;
300
301         case 'radio_ary':
302             $key_yes    = ( isset($new[$config_key]) && $new[$config_key]) ? ' checked="checked"' : '';
303             $key_no    = ( isset($new[$config_key]) && !$new[$config_key]) ? ' checked="checked"' : '';
304
305             preg_match('/\[(.*?)\]/', $config_key, $matches);
306             $name = 'config[' . preg_replace('/\[(.*?)\]/', '', $config_key) . '][' . $matches[1] . ']';
307
308             $tpl_type_cond = explode('_', $tpl_type[1]);
309             $type_no = ($tpl_type_cond[0] == 'disabled' || $tpl_type_cond[0] == 'enabled') ? false : true;
310
311             $tpl_no = '<label><input type="radio" name="' . $name . '" value="0"' . $key_no . ' class="radio" /> ' . (($type_no) ? $lang['no'] : $lang['disabled']) . '</label>';
312             $tpl_yes = '<label><input type="radio" id="' . $key . '" name="' . $name . '" value="1"' . $key_yes . ' class="radio" /> ' . (($type_no) ? $lang['yes'] : $lang['enabled']) . '</label>';
313
314             $tpl = ($tpl_type_cond[0] == 'yes' || $tpl_type_cond[0] == 'enabled') ? $tpl_yes . $tpl_no : $tpl_no . $tpl_yes;
315         break;
316
317         case 'select':
318         case 'custom':
319
320             $return = '';
321
322             if (isset($vars['method']))
323             {
324                 $call = array($vars['method']);
325             }
326             else if (isset($vars['function']))
327             {
328                 $call = $vars['function'];
329             }
330             else
331             {
332                 break;
333             }
334
335             if (isset($vars['params']))
336             {
337                 $args = array();
338                 foreach ($vars['params'] as $value)
339                 {
340                     switch ($value)
341                     {
342                         case '{CONFIG_VALUE}':
343                             $value = $new[$config_key];
344                         break;
345
346                         case '{KEY}':
347                             $value = $key;
348                         break;
349                     }
350
351                     $args[] = $value;
352                 }
353             }
354             else
355             {
356                 $args = array(@$new[$config_key], $key);
357             }
358
359             $return = call_user_func_array($call, $args);
360
361             if ($tpl_type[0] == 'select')
362             {
363                 $tpl = $return;
364             }
365             else
366             {
367                 $tpl = $return;
368             }
369
370         break;
371
372         default:
373         break;
374     }
375
376     if (isset($vars['append']))
377     {
378         $tpl .= $vars['append'];
379     }
380
381     return $tpl;
382 }
383
384 /**</span>
385 <span class="code-comment">* Going through a config array and validate values, writing errors to $error. The validation method  accepts parameters separated by ':' for string and int.
386 * The first parameter defines the type to be used, the second the lower bound and the third the upper bound. Only the type is required.
387 */
388 function validate_config_vars($config_vars, &$cfg_array, &$error)</span>
389 <span class="code-keyword">{
390     global $root_path, $lang;
391     $type    = 0;
392     $min    = 1;
393     $max    = 2;
394
395     foreach ($config_vars as $config_name => $config_definition)
396     {
397         if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
398         {
399             continue;
400         }
401
402         if (!isset($config_definition['validate']))
403         {
404             continue;
405         }
406
407         $validator = explode(':', $config_definition['validate']);
408
409         // Validate a bit. ;) (0 = type, 1 = min, 2= max)
410         switch ($validator[$type])
411         {
412             case 'string':
413                 $length = strlen($cfg_array[$config_name]);
414
415                 // the column is a VARCHAR
416                 $validator[$max] = (isset($validator[$max])) ? min(255, $validator[$max]) : 255;
417
418                 if (isset($validator[$min]) && $length < $validator[$min])
419                 {
420                     $error[] = sprintf($lang['setting_too_short'], $lang[$config_definition['lang']], $validator[$min]);
421                 }
422                 else if (isset($validator[$max]) && $length > $validator[2])
423                 {
424                     $error[] = sprintf($lang['setting_too_long'], $lang[$config_definition['lang']], $validator[$max]);
425                 }
426             break;
427
428             case 'bool':
429                 $cfg_array[$config_name] = ($cfg_array[$config_name]) ? 1 : 0;
430             break;
431
432             case 'int':
433                 $cfg_array[$config_name] = (int) $cfg_array[$config_name];
434
435                 if (isset($validator[$min]) && $cfg_array[$config_name] < $validator[$min])
436                 {
437                     $error[] = sprintf($lang['setting_too_low'], $lang[$config_definition['lang']], $validator[$min]);
438                 }
439                 else if (isset($validator[$max]) && $cfg_array[$config_name] > $validator[$max])
440                 {
441                     $error[] = sprintf($lang['setting_too_big'], $lang[$config_definition['lang']], $validator[$max]);
442                 }
443             break;
444
445             case 'float':
446                 $cfg_array[$config_name] = str_replace(',', '.', $cfg_array[$config_name]);
447                 $cfg_array[$config_name] = (float) $cfg_array[$config_name];
448
449                 if (isset($validator[$min]) && $cfg_array[$config_name] < $validator[$min])
450                 {
451                     $error[] = sprintf($lang['setting_too_low'], $lang[$config_definition['lang']], $validator[$min]);
452                 }
453                 else if (isset($validator[$max]) && $cfg_array[$config_name] > $validator[$max])
454                 {
455                     $error[] = sprintf($lang['setting_too_big'], $lang[$config_definition['lang']], $validator[$max]);
456                 }
457             break;
458
459             // Absolute path
460             case 'script_path':
461                 if (!$cfg_array[$config_name])
462                 {
463                     break;
464                 }
465
466                 $destination = str_replace('\\', '/', $cfg_array[$config_name]);
467
468                 if ($destination !== '/')
469                 {
470                     // Adjust destination path (no trailing slash)
471                     if (substr($destination, -1, 1) == '/')
472                     {
473                         $destination = substr($destination, 0, -1);
474                     }
475
476                     $destination = str_replace(array('../', './'), '', $destination);
477
478                     if ($destination[0] != '/')
479                     {
480                         $destination = '/' . $destination;
481                     }
482                 }
483
484                 $cfg_array[$config_name] = trim($destination);
485
486             break;
487
488             case 'lang':
489                 if (!$cfg_array[$config_name])
490                 {
491                     break;
492                 }
493
494                 $cfg_array[$config_name] = basename($cfg_array[$config_name]);
495
496                 if ( !check_language($cfg_array[$config_name]) )
497                 {
498                     $error[] = $lang['wrong_data_lang'];
499                 }
500             break;
501
502             case 'style':
503                 if (!$cfg_array[$config_name])
504                 {
505                     break;
506                 }
507
508                 if ( !check_style_id($cfg_array[$config_name]) )
509                 {
510                     $error[] = $lang['wrong_data_style'];
511                 }
512             break;
513
514             case 'class':
515                 if ( $cfg_array[$config_name] === '' )
516                 {
517                     break;
518                 }
519
520                 if ( !check_user_class($cfg_array[$config_name]) )
521                 {
522                     $error[] = $lang['wrong_data_class'];
523                 }
524             break;
525
526             // Relative path (appended $root_path)
527             case 'rpath':
528             case 'rwpath':
529                 if (!$cfg_array[$config_name])
530                 {
531                     break;
532                 }
533
534                 $destination = $cfg_array[$config_name];
535
536                 // Adjust destination path (no trailing slash)
537                 if (substr($destination, -1, 1) == '/' || substr($destination, -1, 1) == '\\')
538                 {
539                     $destination = substr($destination, 0, -1);
540                 }
541
542                 $destination = str_replace(array('../', '..\\', './', '.\\'), '', $destination);
543                 if ($destination && ($destination[0] == '/' || $destination[0] == "\\"))
544                 {
545                     $destination = '';
546                 }
547
548                 $cfg_array[$config_name] = trim($destination);
549
550             // Path being relative (still prefixed by tracker_root_path), but with the ability to escape the root dir...
551             case 'path':
552             case 'wpath':
553
554                 if (!$cfg_array[$config_name])
555                 {
556                     break;
557                 }
558
559                 $cfg_array[$config_name] = trim($cfg_array[$config_name]);
560
561                 // Make sure no NUL byte is present...
562                 if (strpos($cfg_array[$config_name], "\0") !== false || strpos($cfg_array[$config_name], '%00') !== false)
563                 {
564                     $cfg_array[$config_name] = '';
565                     break;
566                 }
567
568                 if (!file_exists($root_path . $cfg_array[$config_name]))
569                 {
570                     $error[] = sprintf($lang['directory_doesnt_exists'], $cfg_array[$config_name]);
571                 }
572
573                 if (file_exists($root_path . $cfg_array[$config_name]) && !is_dir($root_path . $cfg_array[$config_name]))
574                 {
575                     $error[] = sprintf($lang['directory_not_dir'], $cfg_array[$config_name]);
576                 }
577
578                 // Check if the path is writable
579                 if ($config_definition['validate'] == 'wpath' || $config_definition['validate'] == 'rwpath')
580                 {
581                     if (file_exists($root_path . $cfg_array[$config_name]) && !@is_writable($root_path . $cfg_array[$config_name]))
582                     {
583                         $error[] = sprintf($lang['directory_not_writable'], $cfg_array[$config_name]);
584                     }
585                 }
586
587             break;
588         }
589     }
590
591     return;
592 }
593
594 /**</span>
595 <span class="code-comment">* Checks whatever or not a variable is OK for use in the Database
596 * param mixed $value_ary An array of the form array(array('lang' => ..., 'value' => ..., 'column_type' =>))'
597 * param mixed $error The error array
598 */
599 function validate_range($value_ary, &$error)</span>
600 <span class="code-keyword">{
601     global $lang;
602
603     $column_types = array(
604         'BOOL'    => array('php_type' => 'int',         'min' => 0,                 'max' => 1),
605         'USINT'    => array('php_type' => 'int',        'min' => 0,                 'max' => 65535),
606         'UINT'    => array('php_type' => 'int',         'min' => 0,                 'max' => (int) 0x7fffffff),
607         'INT'    => array('php_type' => 'int',         'min' => (int) 0x80000000,     'max' => (int) 0x7fffffff),
608         'TINT'    => array('php_type' => 'int',        'min' => -128,                'max' => 127),
609
610         'VCHAR'    => array('php_type' => 'string',     'min' => 0,                 'max' => 255),
611     );
612     foreach ($value_ary as $value)
613     {
614         $column = explode(':', $value['column_type']);
615         $max = $min = 0;
616         $type = 0;
617         if (!isset($column_types[$column[0]]))
618         {
619             continue;
620         }
621         else
622         {
623             $type = $column_types[$column[0]];
624         }
625
626         switch ($type['php_type'])
627         {
628             case 'string' :
629                 $max = (isset($column[1])) ? min($column[1],$type['max']) : $type['max'];
630                 if (strlen($value['value']) > $max)
631                 {
632                     $error[] = sprintf($lang['SETTING_TOO_LONG'], $lang[$value['lang']], $max);
633                 }
634             break;
635
636             case 'int':
637                 $min = (isset($column[1])) ? max($column[1],$type['min']) : $type['min'];
638                 $max = (isset($column[2])) ? min($column[2],$type['max']) : $type['max'];
639                 if ($value['value'] < $min)
640                 {
641                     $error[] = sprintf($lang['SETTING_TOO_LOW'], $lang[$value['lang']], $min);
642                 }
643                 else if ($value['value'] > $max)
644                 {
645                     $error[] = sprintf($lang['SETTING_TOO_BIG'], $lang[$value['lang']], $max);
646                 }
647             break;
648         }
649     }
650 }
651
652 ?>
Note: See TracBrowser for help on using the browser.