1
|
<?php
|
2
|
/**
|
3
|
*
|
4
|
* @category frontend
|
5
|
* @package framework
|
6
|
* @author WebsiteBaker Project
|
7
|
* @copyright 2004-2009, Ryan Djurovich
|
8
|
* @copyright 2009-2011, Website Baker Org. e.V.
|
9
|
* @link http://www.websitebaker2.org/
|
10
|
* @license http://www.gnu.org/licenses/gpl.html
|
11
|
* @platform WebsiteBaker 2.8.x
|
12
|
* @requirements PHP 5.2.2 and higher
|
13
|
* @version $Id: functions.php 1435 2011-03-16 23:39:07Z Luisehahne $
|
14
|
* @filesource $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/functions.php $
|
15
|
* @lastmodified $Date: 2011-03-17 00:39:07 +0100 (Thu, 17 Mar 2011) $
|
16
|
*
|
17
|
*/
|
18
|
|
19
|
// Must include code to stop this file being access directly
|
20
|
if(defined('WB_PATH') == false) { die("Cannot access this file directly"); }
|
21
|
|
22
|
// Define that this file has been loaded
|
23
|
define('FUNCTIONS_FILE_LOADED', true);
|
24
|
|
25
|
/**
|
26
|
* @description: recursively delete a non empty directory
|
27
|
* @param string $directory :
|
28
|
* @param bool $empty : true if you want the folder just emptied, but not deleted
|
29
|
* false, or just simply leave it out, the given directory will be deleted, as well
|
30
|
* @return boolean: list of ro-dirs
|
31
|
* @from http://www.php.net/manual/de/function.rmdir.php#98499
|
32
|
*/
|
33
|
function rm_full_dir($directory, $empty = false) {
|
34
|
|
35
|
if(substr($directory,-1) == "/")
|
36
|
{
|
37
|
$directory = substr($directory,0,-1);
|
38
|
}
|
39
|
|
40
|
// If suplied dirname is a file then unlink it
|
41
|
if (is_file( $directory ))
|
42
|
{
|
43
|
return unlink($directory);
|
44
|
}
|
45
|
|
46
|
if(!file_exists($directory) || !is_dir($directory))
|
47
|
{
|
48
|
return false;
|
49
|
} elseif(!is_readable($directory))
|
50
|
{
|
51
|
return false;
|
52
|
} else {
|
53
|
$directoryHandle = opendir($directory);
|
54
|
|
55
|
while ($contents = readdir($directoryHandle))
|
56
|
{
|
57
|
if($contents != '.' && $contents != '..')
|
58
|
{
|
59
|
$path = $directory . "/" . $contents;
|
60
|
|
61
|
if(is_dir($path))
|
62
|
{
|
63
|
rm_full_dir($path);
|
64
|
} else {
|
65
|
unlink($path);
|
66
|
}
|
67
|
}
|
68
|
}
|
69
|
|
70
|
closedir($directoryHandle);
|
71
|
|
72
|
if($empty == false)
|
73
|
{
|
74
|
if(!rmdir($directory))
|
75
|
{
|
76
|
return false;
|
77
|
}
|
78
|
}
|
79
|
|
80
|
return true;
|
81
|
}
|
82
|
}
|
83
|
|
84
|
/*
|
85
|
* returns a recursive list of all subdirectories from a given directory
|
86
|
* @access public
|
87
|
* @param string $directory: from this dir the recursion will start
|
88
|
* @param bool $show_hidden: if set to TRUE also hidden dirs (.dir) will be shown
|
89
|
* @return array
|
90
|
* example:
|
91
|
* /srv/www/httpdocs/wb/media/a/b/c/
|
92
|
* /srv/www/httpdocs/wb/media/a/b/d/
|
93
|
* directory_list('/srv/www/httpdocs/wb/media/') will return:
|
94
|
* /a
|
95
|
* /a/b
|
96
|
* /a/b/c
|
97
|
* /a/b/d
|
98
|
*/
|
99
|
function directory_list($directory, $show_hidden = false)
|
100
|
{
|
101
|
$result_list = array();
|
102
|
if (is_dir($directory))
|
103
|
{
|
104
|
$dir = dir($directory); // Open the directory
|
105
|
while (false !== $entry = $dir->read()) // loop through the directory
|
106
|
{
|
107
|
if($entry == '.' || $entry == '..') { continue; } // Skip pointers
|
108
|
if($entry[0] == '.' && $show_hidden == false) { continue; } // Skip hidden files
|
109
|
if (is_dir("$directory/$entry")) // Add dir and contents to list
|
110
|
{
|
111
|
$result_list = array_merge($result_list, directory_list("$directory/$entry"));
|
112
|
$result_list[] = "$directory/$entry";
|
113
|
}
|
114
|
}
|
115
|
$dir->close();
|
116
|
}
|
117
|
|
118
|
// sorting
|
119
|
if(natcasesort($result_list))
|
120
|
{
|
121
|
// new indexing
|
122
|
$result_list = array_merge($result_list);
|
123
|
}
|
124
|
return $result_list; // Now return the list
|
125
|
}
|
126
|
|
127
|
// Function to open a directory and add to a dir list
|
128
|
function chmod_directory_contents($directory, $file_mode)
|
129
|
{
|
130
|
if (is_dir($directory))
|
131
|
{
|
132
|
// Set the umask to 0
|
133
|
$umask = umask(0);
|
134
|
// Open the directory then loop through its contents
|
135
|
$dir = dir($directory);
|
136
|
while (false !== $entry = $dir->read())
|
137
|
{
|
138
|
// Skip pointers
|
139
|
if($entry[0] == '.') { continue; }
|
140
|
// Chmod the sub-dirs contents
|
141
|
if(is_dir("$directory/$entry"))
|
142
|
{
|
143
|
chmod_directory_contents($directory.'/'.$entry, $file_mode);
|
144
|
}
|
145
|
change_mode($directory.'/'.$entry);
|
146
|
}
|
147
|
$dir->close();
|
148
|
// Restore the umask
|
149
|
umask($umask);
|
150
|
}
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Scan a given directory for dirs and files.
|
155
|
*
|
156
|
* usage: scan_current_dir ($root = '' )
|
157
|
*
|
158
|
* @param $root set a absolute rootpath as string. if root is empty the current path will be scan
|
159
|
* @param $search set a search pattern for files, empty search brings all files
|
160
|
* @access public
|
161
|
* @return array returns a natsort array with keys 'path' and 'filename'
|
162
|
*
|
163
|
*/
|
164
|
if(!function_exists('scan_current_dir'))
|
165
|
{
|
166
|
function scan_current_dir($root = '', $search = '/.*/')
|
167
|
{
|
168
|
$FILE = array();
|
169
|
$array = array();
|
170
|
clearstatcache();
|
171
|
$root = empty ($root) ? getcwd() : $root;
|
172
|
if (($handle = opendir($root)))
|
173
|
{
|
174
|
// Loop through the files and dirs an add to list DIRECTORY_SEPARATOR
|
175
|
while (false !== ($file = readdir($handle)))
|
176
|
{
|
177
|
if (substr($file, 0, 1) != '.' && $file != 'index.php')
|
178
|
{
|
179
|
if (is_dir($root.'/'.$file))
|
180
|
{
|
181
|
$FILE['path'][] = $file;
|
182
|
} elseif (preg_match($search, $file, $array) )
|
183
|
{
|
184
|
$FILE['filename'][] = $array[0];
|
185
|
}
|
186
|
}
|
187
|
}
|
188
|
$close_verz = closedir($handle);
|
189
|
}
|
190
|
|
191
|
// sorting
|
192
|
if (isset ($FILE['path']) && natcasesort($FILE['path']))
|
193
|
{
|
194
|
// new indexing
|
195
|
$FILE['path'] = array_merge($FILE['path']);
|
196
|
}
|
197
|
// sorting
|
198
|
if (isset ($FILE['filename']) && natcasesort($FILE['filename']))
|
199
|
{
|
200
|
// new indexing
|
201
|
$FILE['filename'] = array_merge($FILE['filename']);
|
202
|
}
|
203
|
return $FILE;
|
204
|
}
|
205
|
}
|
206
|
|
207
|
// Function to open a directory and add to a file list
|
208
|
function file_list($directory, $skip = array(), $show_hidden = false)
|
209
|
{
|
210
|
$result_list = array();
|
211
|
if (is_dir($directory))
|
212
|
{
|
213
|
$dir = dir($directory); // Open the directory
|
214
|
while (false !== ($entry = $dir->read())) // loop through the directory
|
215
|
{
|
216
|
if($entry == '.' || $entry == '..') { continue; } // Skip pointers
|
217
|
if($entry[0] == '.' && $show_hidden == false) { continue; } // Skip hidden files
|
218
|
if( sizeof($skip) > 0 && in_array($entry, $skip) ) { continue; } // Check if we to skip anything else
|
219
|
if(is_file( $directory.'/'.$entry)) // Add files to list
|
220
|
{
|
221
|
$result_list[] = $directory.'/'.$entry;
|
222
|
}
|
223
|
}
|
224
|
$dir->close(); // Now close the folder object
|
225
|
}
|
226
|
|
227
|
// make the list nice. Not all OS do this itself
|
228
|
if(natcasesort($result_list))
|
229
|
{
|
230
|
$result_list = array_merge($result_list);
|
231
|
}
|
232
|
|
233
|
return $result_list;
|
234
|
}
|
235
|
|
236
|
// Function to get a list of home folders not to show
|
237
|
function get_home_folders()
|
238
|
{
|
239
|
global $database, $admin;
|
240
|
$home_folders = array();
|
241
|
// Only return home folders is this feature is enabled
|
242
|
// and user is not admin
|
243
|
// if(HOME_FOLDERS AND ($_SESSION['GROUP_ID']!='1')) {
|
244
|
if(HOME_FOLDERS AND (!in_array('1',explode(',', $_SESSION['GROUPS_ID']))))
|
245
|
{
|
246
|
$sql = 'SELECT `home_folder` FROM `'.TABLE_PREFIX.'users` WHERE `home_folder` != "'.$admin->get_home_folder().'"';
|
247
|
$query_home_folders = $database->query($sql);
|
248
|
if($query_home_folders->numRows() > 0)
|
249
|
{
|
250
|
while($folder = $query_home_folders->fetchRow())
|
251
|
{
|
252
|
$home_folders[$folder['home_folder']] = $folder['home_folder'];
|
253
|
}
|
254
|
}
|
255
|
function remove_home_subs($directory = '/', $home_folders = '')
|
256
|
{
|
257
|
if( ($handle = opendir(WB_PATH.MEDIA_DIRECTORY.$directory)) )
|
258
|
{
|
259
|
// Loop through the dirs to check the home folders sub-dirs are not shown
|
260
|
while(false !== ($file = readdir($handle)))
|
261
|
{
|
262
|
if($file[0] != '.' && $file != 'index.php')
|
263
|
{
|
264
|
if(is_dir(WB_PATH.MEDIA_DIRECTORY.$directory.'/'.$file))
|
265
|
{
|
266
|
if($directory != '/')
|
267
|
{
|
268
|
$file = $directory.'/'.$file;
|
269
|
}
|
270
|
else
|
271
|
{
|
272
|
$file = '/'.$file;
|
273
|
}
|
274
|
foreach($home_folders AS $hf)
|
275
|
{
|
276
|
$hf_length = strlen($hf);
|
277
|
if($hf_length > 0)
|
278
|
{
|
279
|
if(substr($file, 0, $hf_length+1) == $hf)
|
280
|
{
|
281
|
$home_folders[$file] = $file;
|
282
|
}
|
283
|
}
|
284
|
}
|
285
|
$home_folders = remove_home_subs($file, $home_folders);
|
286
|
}
|
287
|
}
|
288
|
}
|
289
|
}
|
290
|
return $home_folders;
|
291
|
}
|
292
|
$home_folders = remove_home_subs('/', $home_folders);
|
293
|
}
|
294
|
return $home_folders;
|
295
|
}
|
296
|
|
297
|
/*
|
298
|
* @param object &$wb: $wb from frontend or $admin from backend
|
299
|
* @return array: list of new entries
|
300
|
* @description: callback remove path in files/dirs stored in array
|
301
|
* @example: array_walk($array,'remove_path',PATH);
|
302
|
*/
|
303
|
//
|
304
|
function remove_path(&$path, $key, $vars = '')
|
305
|
{
|
306
|
$path = str_replace($vars, '', $path);
|
307
|
}
|
308
|
|
309
|
/*
|
310
|
* @param object &$wb: $wb from frontend or $admin from backend
|
311
|
* @return array: list of ro-dirs
|
312
|
* @description: returns a list of directories beyound /wb/media which are ReadOnly for current user
|
313
|
*/
|
314
|
function media_dirs_ro( &$wb )
|
315
|
{
|
316
|
global $database;
|
317
|
// if user is admin or home-folders not activated then there are no restrictions
|
318
|
$allow_list = array();
|
319
|
if( $wb->get_user_id() == 1 || !HOME_FOLDERS )
|
320
|
{
|
321
|
return array();
|
322
|
}
|
323
|
// at first read any dir and subdir from /media
|
324
|
$full_list = directory_list( WB_PATH.MEDIA_DIRECTORY );
|
325
|
// add own home_folder to allow-list
|
326
|
if( $wb->get_home_folder() )
|
327
|
{
|
328
|
// old: $allow_list[] = get_home_folder();
|
329
|
$allow_list[] = $wb->get_home_folder();
|
330
|
}
|
331
|
// get groups of current user
|
332
|
$curr_groups = $wb->get_groups_id();
|
333
|
// if current user is in admin-group
|
334
|
if( ($admin_key = array_search('1', $curr_groups)) !== false)
|
335
|
{
|
336
|
// remove admin-group from list
|
337
|
unset($curr_groups[$admin_key]);
|
338
|
// search for all users where the current user is admin from
|
339
|
foreach( $curr_groups as $group)
|
340
|
{
|
341
|
$sql = 'SELECT `home_folder` FROM `'.TABLE_PREFIX.'users` ';
|
342
|
$sql .= 'WHERE (FIND_IN_SET(\''.$group.'\', `groups_id`) > 0) AND `home_folder` <> \'\' AND `user_id` <> '.$wb->get_user_id();
|
343
|
if( ($res_hf = $database->query($sql)) != null )
|
344
|
{
|
345
|
while( $rec_hf = $res_hf->fetchrow() )
|
346
|
{
|
347
|
$allow_list[] = $rec_hf['home_folder'];
|
348
|
}
|
349
|
}
|
350
|
}
|
351
|
}
|
352
|
$tmp_array = $full_list;
|
353
|
// create a list for readonly dir
|
354
|
$array = array();
|
355
|
while( sizeof($tmp_array) > 0)
|
356
|
{
|
357
|
$tmp = array_shift($tmp_array);
|
358
|
$x = 0;
|
359
|
while($x < sizeof($allow_list))
|
360
|
{
|
361
|
if(strpos ($tmp,$allow_list[$x])) {
|
362
|
$array[] = $tmp;
|
363
|
}
|
364
|
$x++;
|
365
|
}
|
366
|
}
|
367
|
|
368
|
$full_list = array_diff( $full_list, $array );
|
369
|
$tmp = array();
|
370
|
$full_list = array_merge($tmp,$full_list);
|
371
|
|
372
|
return $full_list;
|
373
|
}
|
374
|
|
375
|
/*
|
376
|
* @param object &$wb: $wb from frontend or $admin from backend
|
377
|
* @return array: list of rw-dirs
|
378
|
* @description: returns a list of directories beyound /wb/media which are ReadWrite for current user
|
379
|
*/
|
380
|
function media_dirs_rw ( &$wb )
|
381
|
{
|
382
|
global $database;
|
383
|
// if user is admin or home-folders not activated then there are no restrictions
|
384
|
// at first read any dir and subdir from /media
|
385
|
$full_list = directory_list( WB_PATH.MEDIA_DIRECTORY );
|
386
|
$array = array();
|
387
|
$allow_list = array();
|
388
|
if( ($wb->ami_group_member('1')) && !HOME_FOLDERS )
|
389
|
{
|
390
|
return $full_list;
|
391
|
}
|
392
|
// add own home_folder to allow-list
|
393
|
if( $wb->get_home_folder() )
|
394
|
{
|
395
|
$allow_list[] = $wb->get_home_folder();
|
396
|
} else {
|
397
|
$array = $full_list;
|
398
|
}
|
399
|
// get groups of current user
|
400
|
$curr_groups = $wb->get_groups_id();
|
401
|
// if current user is in admin-group
|
402
|
if( ($admin_key = array_search('1', $curr_groups)) == true)
|
403
|
{
|
404
|
// remove admin-group from list
|
405
|
// unset($curr_groups[$admin_key]);
|
406
|
// search for all users where the current user is admin from
|
407
|
foreach( $curr_groups as $group)
|
408
|
{
|
409
|
$sql = 'SELECT `home_folder` FROM `'.TABLE_PREFIX.'users` ';
|
410
|
$sql .= 'WHERE (FIND_IN_SET(\''.$group.'\', `groups_id`) > 0) AND `home_folder` <> \'\' AND `user_id` <> '.$wb->get_user_id();
|
411
|
if( ($res_hf = $database->query($sql)) != null )
|
412
|
{
|
413
|
while( $rec_hf = $res_hf->fetchrow() )
|
414
|
{
|
415
|
$allow_list[] = $rec_hf['home_folder'];
|
416
|
}
|
417
|
}
|
418
|
}
|
419
|
}
|
420
|
|
421
|
$tmp_array = $full_list;
|
422
|
// create a list for readwrite dir
|
423
|
while( sizeof($tmp_array) > 0)
|
424
|
{
|
425
|
$tmp = array_shift($tmp_array);
|
426
|
$x = 0;
|
427
|
while($x < sizeof($allow_list))
|
428
|
{
|
429
|
if(strpos ($tmp,$allow_list[$x])) {
|
430
|
$array[] = $tmp;
|
431
|
}
|
432
|
$x++;
|
433
|
}
|
434
|
}
|
435
|
|
436
|
$tmp = array();
|
437
|
$array = array_unique($array);
|
438
|
$full_list = array_merge($tmp,$array);
|
439
|
unset($array);
|
440
|
unset($allow_list);
|
441
|
|
442
|
return $full_list;
|
443
|
}
|
444
|
|
445
|
// Function to create directories
|
446
|
function make_dir($dir_name, $dir_mode = OCTAL_DIR_MODE)
|
447
|
{
|
448
|
if(!is_dir($dir_name))
|
449
|
{
|
450
|
$umask = umask(0);
|
451
|
mkdir($dir_name, $dir_mode);
|
452
|
umask($umask);
|
453
|
return true;
|
454
|
} else {
|
455
|
return false;
|
456
|
}
|
457
|
}
|
458
|
|
459
|
// Function to chmod files and directories
|
460
|
function change_mode($name)
|
461
|
{
|
462
|
if(OPERATING_SYSTEM != 'windows')
|
463
|
{
|
464
|
// Only chmod if os is not windows
|
465
|
if(is_dir($name))
|
466
|
{
|
467
|
$mode = OCTAL_DIR_MODE;
|
468
|
}
|
469
|
else
|
470
|
{
|
471
|
$mode = OCTAL_FILE_MODE;
|
472
|
}
|
473
|
|
474
|
if(file_exists($name))
|
475
|
{
|
476
|
$umask = umask(0);
|
477
|
chmod($name, $mode);
|
478
|
umask($umask);
|
479
|
return true;
|
480
|
}
|
481
|
else
|
482
|
{
|
483
|
return false;
|
484
|
}
|
485
|
}
|
486
|
else
|
487
|
{
|
488
|
return true;
|
489
|
}
|
490
|
}
|
491
|
|
492
|
// Function to figure out if a parent exists
|
493
|
function is_parent($page_id)
|
494
|
{
|
495
|
global $database;
|
496
|
// Get parent
|
497
|
$sql = 'SELECT `parent` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
|
498
|
$parent = $database->get_one($sql);
|
499
|
// If parent isnt 0 return its ID
|
500
|
if(is_null($parent))
|
501
|
{
|
502
|
return false;
|
503
|
}
|
504
|
else
|
505
|
{
|
506
|
return $parent;
|
507
|
}
|
508
|
}
|
509
|
|
510
|
// Function to work out level
|
511
|
function level_count($page_id)
|
512
|
{
|
513
|
global $database;
|
514
|
// Get page parent
|
515
|
$sql = 'SELECT `parent` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
|
516
|
$parent = $database->get_one($sql);
|
517
|
if($parent > 0)
|
518
|
{ // Get the level of the parent
|
519
|
$sql = 'SELECT `level` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$parent;
|
520
|
$level = $database->get_one($sql);
|
521
|
return $level+1;
|
522
|
}
|
523
|
else
|
524
|
{
|
525
|
return 0;
|
526
|
}
|
527
|
}
|
528
|
|
529
|
// Function to work out root parent
|
530
|
function root_parent($page_id)
|
531
|
{
|
532
|
global $database;
|
533
|
// Get page details
|
534
|
$sql = 'SELECT `parent`, `level` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
|
535
|
$query_page = $database->query($sql);
|
536
|
$fetch_page = $query_page->fetchRow();
|
537
|
$parent = $fetch_page['parent'];
|
538
|
$level = $fetch_page['level'];
|
539
|
if($level == 1)
|
540
|
{
|
541
|
return $parent;
|
542
|
}
|
543
|
elseif($parent == 0)
|
544
|
{
|
545
|
return $page_id;
|
546
|
}
|
547
|
else
|
548
|
{ // Figure out what the root parents id is
|
549
|
$parent_ids = array_reverse(get_parent_ids($page_id));
|
550
|
return $parent_ids[0];
|
551
|
}
|
552
|
}
|
553
|
|
554
|
// Function to get page title
|
555
|
function get_page_title($id)
|
556
|
{
|
557
|
global $database;
|
558
|
// Get title
|
559
|
$sql = 'SELECT `page_title` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$id;
|
560
|
$page_title = $database->get_one($sql);
|
561
|
return $page_title;
|
562
|
}
|
563
|
|
564
|
// Function to get a pages menu title
|
565
|
function get_menu_title($id)
|
566
|
{
|
567
|
global $database;
|
568
|
// Get title
|
569
|
$sql = 'SELECT `menu_title` FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$id;
|
570
|
$menu_title = $database->get_one($sql);
|
571
|
return $menu_title;
|
572
|
}
|
573
|
|
574
|
// Function to get all parent page titles
|
575
|
function get_parent_titles($parent_id)
|
576
|
{
|
577
|
$titles[] = get_menu_title($parent_id);
|
578
|
if(is_parent($parent_id) != false)
|
579
|
{
|
580
|
$parent_titles = get_parent_titles(is_parent($parent_id));
|
581
|
$titles = array_merge($titles, $parent_titles);
|
582
|
}
|
583
|
return $titles;
|
584
|
}
|
585
|
|
586
|
// Function to get all parent page id's
|
587
|
function get_parent_ids($parent_id)
|
588
|
{
|
589
|
$ids[] = $parent_id;
|
590
|
if(is_parent($parent_id) != false)
|
591
|
{
|
592
|
$parent_ids = get_parent_ids(is_parent($parent_id));
|
593
|
$ids = array_merge($ids, $parent_ids);
|
594
|
}
|
595
|
return $ids;
|
596
|
}
|
597
|
|
598
|
// Function to genereate page trail
|
599
|
function get_page_trail($page_id) {
|
600
|
return implode(',', array_reverse(get_parent_ids($page_id)));
|
601
|
}
|
602
|
|
603
|
// Function to get all sub pages id's
|
604
|
function get_subs($parent, $subs)
|
605
|
{
|
606
|
// Connect to the database
|
607
|
global $database;
|
608
|
// Get id's
|
609
|
$sql = 'SELECT `page_id` FROM `'.TABLE_PREFIX.'pages` WHERE `parent` = '.$parent;
|
610
|
$query = $database->query($sql);
|
611
|
if($query->numRows() > 0)
|
612
|
{
|
613
|
while($fetch = $query->fetchRow())
|
614
|
{
|
615
|
$subs[] = $fetch['page_id'];
|
616
|
// Get subs of this sub
|
617
|
$subs = get_subs($fetch['page_id'], $subs);
|
618
|
}
|
619
|
}
|
620
|
// Return subs array
|
621
|
return $subs;
|
622
|
}
|
623
|
|
624
|
// Function as replacement for php's htmlspecialchars()
|
625
|
// Will not mangle HTML-entities
|
626
|
function my_htmlspecialchars($string)
|
627
|
{
|
628
|
$string = preg_replace('/&(?=[#a-z0-9]+;)/i', '__amp;_', $string);
|
629
|
$string = strtr($string, array('<'=>'<', '>'=>'>', '&'=>'&', '"'=>'"', '\''=>'''));
|
630
|
$string = preg_replace('/__amp;_(?=[#a-z0-9]+;)/i', '&', $string);
|
631
|
return($string);
|
632
|
}
|
633
|
|
634
|
// Convert a string from mixed html-entities/umlauts to pure $charset_out-umlauts
|
635
|
// Will replace all numeric and named entities except > < ' " '
|
636
|
// In case of error the returned string is unchanged, and a message is emitted.
|
637
|
function entities_to_umlauts($string, $charset_out=DEFAULT_CHARSET)
|
638
|
{
|
639
|
require_once(WB_PATH.'/framework/functions-utf8.php');
|
640
|
return entities_to_umlauts2($string, $charset_out);
|
641
|
}
|
642
|
|
643
|
// Will convert a string in $charset_in encoding to a pure ASCII string with HTML-entities.
|
644
|
// In case of error the returned string is unchanged, and a message is emitted.
|
645
|
function umlauts_to_entities($string, $charset_in=DEFAULT_CHARSET)
|
646
|
{
|
647
|
require_once(WB_PATH.'/framework/functions-utf8.php');
|
648
|
return umlauts_to_entities2($string, $charset_in);
|
649
|
}
|
650
|
|
651
|
// Function to convert a page title to a page filename
|
652
|
function page_filename($string)
|
653
|
{
|
654
|
require_once(WB_PATH.'/framework/functions-utf8.php');
|
655
|
$string = entities_to_7bit($string);
|
656
|
// Now remove all bad characters
|
657
|
$bad = array(
|
658
|
'\'', /* / */ '"', /* " */ '<', /* < */ '>', /* > */
|
659
|
'{', /* { */ '}', /* } */ '[', /* [ */ ']', /* ] */ '`', /* ` */
|
660
|
'!', /* ! */ '@', /* @ */ '#', /* # */ '$', /* $ */ '%', /* % */
|
661
|
'^', /* ^ */ '&', /* & */ '*', /* * */ '(', /* ( */ ')', /* ) */
|
662
|
'=', /* = */ '+', /* + */ '|', /* | */ '/', /* / */ '\\', /* \ */
|
663
|
';', /* ; */ ':', /* : */ ',', /* , */ '?' /* ? */
|
664
|
);
|
665
|
$string = str_replace($bad, '', $string);
|
666
|
// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
|
667
|
$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
|
668
|
// Now replace spaces with page spcacer
|
669
|
$string = trim($string);
|
670
|
$string = preg_replace('/(\s)+/', PAGE_SPACER, $string);
|
671
|
// Now convert to lower-case
|
672
|
$string = strtolower($string);
|
673
|
// If there are any weird language characters, this will protect us against possible problems they could cause
|
674
|
$string = str_replace(array('%2F', '%'), array('/', ''), urlencode($string));
|
675
|
// Finally, return the cleaned string
|
676
|
return $string;
|
677
|
}
|
678
|
|
679
|
// Function to convert a desired media filename to a clean filename
|
680
|
function media_filename($string)
|
681
|
{
|
682
|
require_once(WB_PATH.'/framework/functions-utf8.php');
|
683
|
$string = entities_to_7bit($string);
|
684
|
// Now remove all bad characters
|
685
|
$bad = array(
|
686
|
'\'', // '
|
687
|
'"', // "
|
688
|
'`', // `
|
689
|
'!', // !
|
690
|
'@', // @
|
691
|
'#', // #
|
692
|
'$', // $
|
693
|
'%', // %
|
694
|
'^', // ^
|
695
|
'&', // &
|
696
|
'*', // *
|
697
|
'=', // =
|
698
|
'+', // +
|
699
|
'|', // |
|
700
|
'/', // /
|
701
|
'\\', // \
|
702
|
';', // ;
|
703
|
':', // :
|
704
|
',', // ,
|
705
|
'?' // ?
|
706
|
);
|
707
|
$string = str_replace($bad, '', $string);
|
708
|
// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
|
709
|
$string = preg_replace(array('/\.+/', '/\.+$/'), array('.', ''), $string);
|
710
|
// Clean any page spacers at the end of string
|
711
|
$string = trim($string);
|
712
|
// Finally, return the cleaned string
|
713
|
return $string;
|
714
|
}
|
715
|
|
716
|
// Function to work out a page link
|
717
|
if(!function_exists('page_link'))
|
718
|
{
|
719
|
function page_link($link)
|
720
|
{
|
721
|
global $admin;
|
722
|
return $admin->page_link($link);
|
723
|
}
|
724
|
}
|
725
|
|
726
|
// Create a new file in the pages directory
|
727
|
function create_access_file($filename,$page_id,$level)
|
728
|
{
|
729
|
global $admin, $MESSAGE;
|
730
|
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/'))
|
731
|
{
|
732
|
$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
|
733
|
}
|
734
|
else
|
735
|
{
|
736
|
// First make sure parent folder exists
|
737
|
$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
|
738
|
$parents = '';
|
739
|
foreach($parent_folders AS $parent_folder)
|
740
|
{
|
741
|
if($parent_folder != '/' AND $parent_folder != '')
|
742
|
{
|
743
|
$parents .= '/'.$parent_folder;
|
744
|
if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents))
|
745
|
{
|
746
|
make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
|
747
|
}
|
748
|
}
|
749
|
}
|
750
|
// The depth of the page directory in the directory hierarchy
|
751
|
// '/pages' is at depth 1
|
752
|
$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
|
753
|
// Work-out how many ../'s we need to get to the index page
|
754
|
$index_location = '';
|
755
|
for($i = 0; $i < $level + $pages_dir_depth; $i++)
|
756
|
{
|
757
|
$index_location .= '../';
|
758
|
}
|
759
|
$content = ''.
|
760
|
'<?php
|
761
|
$page_id = '.$page_id.';
|
762
|
require("'.$index_location.'config.php");
|
763
|
require(WB_PATH."/index.php");
|
764
|
?>';
|
765
|
$handle = fopen($filename, 'w');
|
766
|
fwrite($handle, $content);
|
767
|
fclose($handle);
|
768
|
// Chmod the file
|
769
|
change_mode($filename);
|
770
|
}
|
771
|
}
|
772
|
|
773
|
// Function for working out a file mime type (if the in-built PHP one is not enabled)
|
774
|
if(!function_exists('mime_content_type'))
|
775
|
{
|
776
|
function mime_content_type($filename)
|
777
|
{
|
778
|
$mime_types = array(
|
779
|
'txt' => 'text/plain',
|
780
|
'htm' => 'text/html',
|
781
|
'html' => 'text/html',
|
782
|
'php' => 'text/html',
|
783
|
'css' => 'text/css',
|
784
|
'js' => 'application/javascript',
|
785
|
'json' => 'application/json',
|
786
|
'xml' => 'application/xml',
|
787
|
'swf' => 'application/x-shockwave-flash',
|
788
|
'flv' => 'video/x-flv',
|
789
|
|
790
|
// images
|
791
|
'png' => 'image/png',
|
792
|
'jpe' => 'image/jpeg',
|
793
|
'jpeg' => 'image/jpeg',
|
794
|
'jpg' => 'image/jpeg',
|
795
|
'gif' => 'image/gif',
|
796
|
'bmp' => 'image/bmp',
|
797
|
'ico' => 'image/vnd.microsoft.icon',
|
798
|
'tiff' => 'image/tiff',
|
799
|
'tif' => 'image/tiff',
|
800
|
'svg' => 'image/svg+xml',
|
801
|
'svgz' => 'image/svg+xml',
|
802
|
|
803
|
// archives
|
804
|
'zip' => 'application/zip',
|
805
|
'rar' => 'application/x-rar-compressed',
|
806
|
'exe' => 'application/x-msdownload',
|
807
|
'msi' => 'application/x-msdownload',
|
808
|
'cab' => 'application/vnd.ms-cab-compressed',
|
809
|
|
810
|
// audio/video
|
811
|
'mp3' => 'audio/mpeg',
|
812
|
'mp4' => 'audio/mpeg',
|
813
|
'qt' => 'video/quicktime',
|
814
|
'mov' => 'video/quicktime',
|
815
|
|
816
|
// adobe
|
817
|
'pdf' => 'application/pdf',
|
818
|
'psd' => 'image/vnd.adobe.photoshop',
|
819
|
'ai' => 'application/postscript',
|
820
|
'eps' => 'application/postscript',
|
821
|
'ps' => 'application/postscript',
|
822
|
|
823
|
// ms office
|
824
|
'doc' => 'application/msword',
|
825
|
'rtf' => 'application/rtf',
|
826
|
'xls' => 'application/vnd.ms-excel',
|
827
|
'ppt' => 'application/vnd.ms-powerpoint',
|
828
|
|
829
|
// open office
|
830
|
'odt' => 'application/vnd.oasis.opendocument.text',
|
831
|
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
|
832
|
);
|
833
|
|
834
|
$temp = explode('.',$filename);
|
835
|
$ext = strtolower(array_pop($temp));
|
836
|
|
837
|
if (array_key_exists($ext, $mime_types))
|
838
|
{
|
839
|
return $mime_types[$ext];
|
840
|
}
|
841
|
elseif (function_exists('finfo_open'))
|
842
|
{
|
843
|
$finfo = finfo_open(FILEINFO_MIME);
|
844
|
$mimetype = finfo_file($finfo, $filename);
|
845
|
finfo_close($finfo);
|
846
|
return $mimetype;
|
847
|
}
|
848
|
else
|
849
|
{
|
850
|
return 'application/octet-stream';
|
851
|
}
|
852
|
}
|
853
|
}
|
854
|
|
855
|
// Generate a thumbnail from an image
|
856
|
function make_thumb($source, $destination, $size)
|
857
|
{
|
858
|
// Check if GD is installed
|
859
|
if(extension_loaded('gd') && function_exists('imageCreateFromJpeg'))
|
860
|
{
|
861
|
// First figure out the size of the thumbnail
|
862
|
list($original_x, $original_y) = getimagesize($source);
|
863
|
if ($original_x > $original_y)
|
864
|
{
|
865
|
$thumb_w = $size;
|
866
|
$thumb_h = $original_y*($size/$original_x);
|
867
|
}
|
868
|
if ($original_x < $original_y)
|
869
|
{
|
870
|
$thumb_w = $original_x*($size/$original_y);
|
871
|
$thumb_h = $size;
|
872
|
}
|
873
|
if ($original_x == $original_y)
|
874
|
{
|
875
|
$thumb_w = $size;
|
876
|
$thumb_h = $size;
|
877
|
}
|
878
|
// Now make the thumbnail
|
879
|
$source = imageCreateFromJpeg($source);
|
880
|
$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
|
881
|
imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
|
882
|
imagejpeg($dst_img, $destination);
|
883
|
// Clear memory
|
884
|
imagedestroy($dst_img);
|
885
|
imagedestroy($source);
|
886
|
// Return true
|
887
|
return true;
|
888
|
} else {
|
889
|
return false;
|
890
|
}
|
891
|
}
|
892
|
|
893
|
/*
|
894
|
* Function to work-out a single part of an octal permission value
|
895
|
*
|
896
|
* @param mixed $octal_value: an octal value as string (i.e. '0777') or real octal integer (i.e. 0777 | 777)
|
897
|
* @param string $who: char or string for whom the permission is asked( U[ser] / G[roup] / O[thers] )
|
898
|
* @param string $action: char or string with the requested action( r[ead..] / w[rite..] / e|x[ecute..] )
|
899
|
* @return boolean
|
900
|
*/
|
901
|
function extract_permission($octal_value, $who, $action)
|
902
|
{
|
903
|
// Make sure that all arguments are set and $octal_value is a real octal-integer
|
904
|
if( ($who == '') || ($action == '') || (preg_match( '/[^0-7]/', (string)$octal_value )) )
|
905
|
{
|
906
|
return false; // invalid argument, so return false
|
907
|
}
|
908
|
// convert $octal_value into a decimal-integer to be sure having a valid value
|
909
|
$right_mask = octdec($octal_value);
|
910
|
$action_mask = 0;
|
911
|
// set the $action related bit in $action_mask
|
912
|
switch($action[0]) // get action from first char of $action
|
913
|
{
|
914
|
case 'r':
|
915
|
case 'R':
|
916
|
$action_mask = 4; // set read-bit only (2^2)
|
917
|
break;
|
918
|
case 'w':
|
919
|
case 'W':
|
920
|
$action_mask = 2; // set write-bit only (2^1)
|
921
|
break;
|
922
|
case 'e':
|
923
|
case 'E':
|
924
|
case 'x':
|
925
|
case 'X':
|
926
|
$action_mask = 1; // set execute-bit only (2^0)
|
927
|
break;
|
928
|
default:
|
929
|
return false; // undefined action name, so return false
|
930
|
}
|
931
|
// shift action-mask into the right position
|
932
|
switch($who[0]) // get who from first char of $who
|
933
|
{
|
934
|
case 'u':
|
935
|
case 'U':
|
936
|
$action_mask <<= 3; // shift left 3 bits
|
937
|
case 'g':
|
938
|
case 'G':
|
939
|
$action_mask <<= 3; // shift left 3 bits
|
940
|
case 'o':
|
941
|
case 'O':
|
942
|
/* NOP */
|
943
|
break;
|
944
|
default:
|
945
|
return false; // undefined who, so return false
|
946
|
}
|
947
|
return( ($right_mask & $action_mask) != 0 ); // return result of binary-AND
|
948
|
}
|
949
|
|
950
|
// Function to delete a page
|
951
|
function delete_page($page_id)
|
952
|
{
|
953
|
global $admin, $database, $MESSAGE;
|
954
|
// Find out more about the page
|
955
|
$sql = 'SELECT `page_id`, `menu_title`, `page_title`, `level`, `link`, `parent`, `modified_by`, `modified_when` ';
|
956
|
$sql .= 'FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
|
957
|
$results = $database->query($sql);
|
958
|
if($database->is_error()) { $admin->print_error($database->get_error()); }
|
959
|
if($results->numRows() == 0) { $admin->print_error($MESSAGE['PAGES']['NOT_FOUND']); }
|
960
|
$results_array = $results->fetchRow();
|
961
|
$parent = $results_array['parent'];
|
962
|
$level = $results_array['level'];
|
963
|
$link = $results_array['link'];
|
964
|
$page_title = $results_array['page_title'];
|
965
|
$menu_title = $results_array['menu_title'];
|
966
|
|
967
|
// Get the sections that belong to the page
|
968
|
$sql = 'SELECT `section_id`, `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id` = '.$page_id;
|
969
|
$query_sections = $database->query($sql);
|
970
|
if($query_sections->numRows() > 0)
|
971
|
{
|
972
|
while($section = $query_sections->fetchRow())
|
973
|
{
|
974
|
// Set section id
|
975
|
$section_id = $section['section_id'];
|
976
|
// Include the modules delete file if it exists
|
977
|
if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php'))
|
978
|
{
|
979
|
include(WB_PATH.'/modules/'.$section['module'].'/delete.php');
|
980
|
}
|
981
|
}
|
982
|
}
|
983
|
// Update the pages table
|
984
|
$sql = 'DELETE FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
|
985
|
$database->query($sql);
|
986
|
if($database->is_error())
|
987
|
{
|
988
|
$admin->print_error($database->get_error());
|
989
|
}
|
990
|
// Update the sections table
|
991
|
$sql = 'DELETE FROM `'.TABLE_PREFIX.'sections` WHERE `page_id` = '.$page_id;
|
992
|
$database->query($sql);
|
993
|
if($database->is_error()) {
|
994
|
$admin->print_error($database->get_error());
|
995
|
}
|
996
|
// Include the ordering class or clean-up ordering
|
997
|
include_once(WB_PATH.'/framework/class.order.php');
|
998
|
$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
|
999
|
$order->clean($parent);
|
1000
|
// Unlink the page access file and directory
|
1001
|
$directory = WB_PATH.PAGES_DIRECTORY.$link;
|
1002
|
$filename = $directory.PAGE_EXTENSION;
|
1003
|
$directory .= '/';
|
1004
|
if(file_exists($filename))
|
1005
|
{
|
1006
|
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/'))
|
1007
|
{
|
1008
|
$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
|
1009
|
}
|
1010
|
else
|
1011
|
{
|
1012
|
unlink($filename);
|
1013
|
if( file_exists($directory) &&
|
1014
|
(rtrim($directory,'/') != WB_PATH.PAGES_DIRECTORY) &&
|
1015
|
(substr($link, 0, 1) != '.'))
|
1016
|
{
|
1017
|
rm_full_dir($directory);
|
1018
|
}
|
1019
|
}
|
1020
|
}
|
1021
|
}
|
1022
|
|
1023
|
/*
|
1024
|
* @param string $file: name of the file to read
|
1025
|
* @param int $size: number of maximum bytes to read (0 = complete file)
|
1026
|
* @return string: the content as string, false on error
|
1027
|
*/
|
1028
|
function getFilePart($file, $size = 0)
|
1029
|
{
|
1030
|
$file_content = '';
|
1031
|
if( file_exists($file) && is_file($file) && is_readable($file))
|
1032
|
{
|
1033
|
if($size == 0)
|
1034
|
{
|
1035
|
$size = filesize($file);
|
1036
|
}
|
1037
|
if(($fh = fopen($file, 'rb')))
|
1038
|
{
|
1039
|
if( ($file_content = fread($fh, $size)) !== false )
|
1040
|
{
|
1041
|
return $file_content;
|
1042
|
}
|
1043
|
fclose($fh);
|
1044
|
}
|
1045
|
}
|
1046
|
return false;
|
1047
|
}
|
1048
|
|
1049
|
/**
|
1050
|
* replace varnames with values in a string
|
1051
|
*
|
1052
|
* @param string $subject: stringvariable with vars placeholder
|
1053
|
* @param array $replace: values to replace vars placeholder
|
1054
|
* @return string
|
1055
|
*/
|
1056
|
function replace_vars($subject = '', &$replace = null )
|
1057
|
{
|
1058
|
if(is_array($replace))
|
1059
|
{
|
1060
|
foreach ($replace as $key => $value)
|
1061
|
{
|
1062
|
$subject = str_replace("{{".$key."}}", $value, $subject);
|
1063
|
}
|
1064
|
}
|
1065
|
return $subject;
|
1066
|
}
|
1067
|
|
1068
|
// Load module into DB
|
1069
|
function load_module($directory, $install = false)
|
1070
|
{
|
1071
|
global $database,$admin,$MESSAGE;
|
1072
|
$retVal = false;
|
1073
|
if(is_dir($directory) && file_exists($directory.'/info.php'))
|
1074
|
{
|
1075
|
require($directory.'/info.php');
|
1076
|
if(isset($module_name))
|
1077
|
{
|
1078
|
if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
|
1079
|
if(!isset($module_platform) && isset($module_designed_for)) { $module_platform = $module_designed_for; }
|
1080
|
if(!isset($module_function) && isset($module_type)) { $module_function = $module_type; }
|
1081
|
$module_function = strtolower($module_function);
|
1082
|
// Check that it doesn't already exist
|
1083
|
$sqlwhere = 'WHERE `type` = \'module\' AND `directory` = \''.$module_directory.'\'';
|
1084
|
$sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'addons` '.$sqlwhere;
|
1085
|
if( $database->get_one($sql) )
|
1086
|
{
|
1087
|
$sql = 'UPDATE `'.TABLE_PREFIX.'addons` SET ';
|
1088
|
}else{
|
1089
|
// Load into DB
|
1090
|
$sql = 'INSERT INTO `'.TABLE_PREFIX.'addons` SET ';
|
1091
|
$sqlwhere = '';
|
1092
|
}
|
1093
|
$sql .= '`directory` = \''.$module_directory.'\', ';
|
1094
|
$sql .= '`name` = \''.$module_name.'\', ';
|
1095
|
$sql .= '`description`= \''.addslashes($module_description).'\', ';
|
1096
|
$sql .= '`type`= \'module\', ';
|
1097
|
$sql .= '`function` = \''.$module_function.'\', ';
|
1098
|
$sql .= '`version` = \''.$module_version.'\', ';
|
1099
|
$sql .= '`platform` = \''.$module_platform.'\', ';
|
1100
|
$sql .= '`author` = \''.addslashes($module_author).'\', ';
|
1101
|
$sql .= '`license` = \''.addslashes($module_license).'\'';
|
1102
|
$sql .= $sqlwhere;
|
1103
|
$retVal = $database->query($sql);
|
1104
|
// Run installation script
|
1105
|
if($install == true)
|
1106
|
{
|
1107
|
if(file_exists($directory.'/install.php'))
|
1108
|
{
|
1109
|
require($directory.'/install.php');
|
1110
|
}
|
1111
|
}
|
1112
|
}
|
1113
|
}
|
1114
|
}
|
1115
|
|
1116
|
// Load template into DB
|
1117
|
function load_template($directory)
|
1118
|
{
|
1119
|
global $database, $admin;
|
1120
|
$retVal = false;
|
1121
|
if(is_dir($directory) && file_exists($directory.'/info.php'))
|
1122
|
{
|
1123
|
require($directory.'/info.php');
|
1124
|
if(isset($template_name))
|
1125
|
{
|
1126
|
if(!isset($template_license))
|
1127
|
{
|
1128
|
$template_license = 'GNU General Public License';
|
1129
|
}
|
1130
|
if(!isset($template_platform) && isset($template_designed_for))
|
1131
|
{
|
1132
|
$template_platform = $template_designed_for;
|
1133
|
}
|
1134
|
if(!isset($template_function))
|
1135
|
{
|
1136
|
$template_function = 'template';
|
1137
|
}
|
1138
|
// Check that it doesn't already exist
|
1139
|
$sqlwhere = 'WHERE `type` = \'template\' AND `directory` = \''.$template_directory.'\'';
|
1140
|
$sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'addons` '.$sqlwhere;
|
1141
|
if( $database->get_one($sql) )
|
1142
|
{
|
1143
|
$sql = 'UPDATE `'.TABLE_PREFIX.'addons` SET ';
|
1144
|
}else{
|
1145
|
// Load into DB
|
1146
|
$sql = 'INSERT INTO `'.TABLE_PREFIX.'addons` SET ';
|
1147
|
$sqlwhere = '';
|
1148
|
}
|
1149
|
$sql .= '`directory` = \''.$template_directory.'\', ';
|
1150
|
$sql .= '`name` = \''.$template_name.'\', ';
|
1151
|
$sql .= '`description`= \''.addslashes($template_description).'\', ';
|
1152
|
$sql .= '`type`= \'template\', ';
|
1153
|
$sql .= '`function` = \''.$template_function.'\', ';
|
1154
|
$sql .= '`version` = \''.$template_version.'\', ';
|
1155
|
$sql .= '`platform` = \''.$template_platform.'\', ';
|
1156
|
$sql .= '`author` = \''.addslashes($template_author).'\', ';
|
1157
|
$sql .= '`license` = \''.addslashes($template_license).'\' ';
|
1158
|
$sql .= $sqlwhere;
|
1159
|
$retVal = $database->query($sql);
|
1160
|
}
|
1161
|
}
|
1162
|
return $retVal;
|
1163
|
}
|
1164
|
|
1165
|
// Load language into DB
|
1166
|
function load_language($file)
|
1167
|
{
|
1168
|
global $database,$admin;
|
1169
|
$retVal = false;
|
1170
|
if (file_exists($file) && preg_match('#^([A-Z]{2}.php)#', basename($file)))
|
1171
|
{
|
1172
|
// require($file); it's to large
|
1173
|
// read contents of the template language file into string
|
1174
|
$data = @file_get_contents(WB_PATH.'/languages/'.str_replace('.php','',basename($file)).'.php');
|
1175
|
// use regular expressions to fetch the content of the variable from the string
|
1176
|
$language_name = get_variable_content('language_name', $data, false, false);
|
1177
|
$language_code = get_variable_content('language_code', $data, false, false);
|
1178
|
$language_author = get_variable_content('language_author', $data, false, false);
|
1179
|
$language_version = get_variable_content('language_version', $data, false, false);
|
1180
|
$language_platform = get_variable_content('language_platform', $data, false, false);
|
1181
|
|
1182
|
if(isset($language_name))
|
1183
|
{
|
1184
|
if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
|
1185
|
if(!isset($language_platform) && isset($language_designed_for)) { $language_platform = $language_designed_for; }
|
1186
|
// Check that it doesn't already exist
|
1187
|
$sqlwhere = 'WHERE `type` = \'language\' AND `directory` = \''.$language_code.'\'';
|
1188
|
$sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'addons` '.$sqlwhere;
|
1189
|
if( $database->get_one($sql) )
|
1190
|
{
|
1191
|
$sql = 'UPDATE `'.TABLE_PREFIX.'addons` SET ';
|
1192
|
}else{
|
1193
|
// Load into DB
|
1194
|
$sql = 'INSERT INTO `'.TABLE_PREFIX.'addons` SET ';
|
1195
|
$sqlwhere = '';
|
1196
|
}
|
1197
|
$sql .= '`directory` = \''.$language_code.'\', ';
|
1198
|
$sql .= '`name` = \''.$language_name.'\', ';
|
1199
|
$sql .= '`type`= \'language\', ';
|
1200
|
$sql .= '`version` = \''.$language_version.'\', ';
|
1201
|
$sql .= '`platform` = \''.$language_platform.'\', ';
|
1202
|
$sql .= '`author` = \''.addslashes($language_author).'\', ';
|
1203
|
$sql .= '`license` = \''.addslashes($language_license).'\' ';
|
1204
|
$sql .= $sqlwhere;
|
1205
|
$retVal = $database->query($sql);
|
1206
|
}
|
1207
|
}
|
1208
|
return $retVal;
|
1209
|
}
|
1210
|
|
1211
|
// Upgrade module info in DB, optionally start upgrade script
|
1212
|
function upgrade_module($directory, $upgrade = false)
|
1213
|
{
|
1214
|
global $database, $admin, $MESSAGE, $new_module_version;
|
1215
|
$mod_directory = WB_PATH.'/modules/'.$directory;
|
1216
|
if(file_exists($mod_directory.'/info.php'))
|
1217
|
{
|
1218
|
require($mod_directory.'/info.php');
|
1219
|
if(isset($module_name))
|
1220
|
{
|
1221
|
if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
|
1222
|
if(!isset($module_platform) && isset($module_designed_for)) { $module_platform = $module_designed_for; }
|
1223
|
if(!isset($module_function) && isset($module_type)) { $module_function = $module_type; }
|
1224
|
$module_function = strtolower($module_function);
|
1225
|
// Check that it does already exist
|
1226
|
$sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'addons` ';
|
1227
|
$sql .= 'WHERE `directory` = \''.$module_directory.'\'';
|
1228
|
if( $database->get_one($sql) )
|
1229
|
{
|
1230
|
// Update in DB
|
1231
|
$sql = 'UPDATE `'.TABLE_PREFIX.'addons` SET ';
|
1232
|
$sql .= '`version` = "'.$module_version.'", ';
|
1233
|
$sql .= '`description` = "'.addslashes($module_description).'", ';
|
1234
|
$sql .= '`platform` = \''.$module_platform.'\', ';
|
1235
|
$sql .= '`author` = \''.addslashes($module_author).'\', ';
|
1236
|
$sql .= '`license` = \''.addslashes($module_license).'\' ';
|
1237
|
$sql .= 'WHERE `directory` = \''.$module_directory.'\' ';
|
1238
|
$database->query($sql);
|
1239
|
if($database->is_error()) {
|
1240
|
$admin->print_error($database->get_error());
|
1241
|
}
|
1242
|
|
1243
|
// Run upgrade script
|
1244
|
if($upgrade == true)
|
1245
|
{
|
1246
|
if(file_exists($mod_directory.'/upgrade.php'))
|
1247
|
{
|
1248
|
require($mod_directory.'/upgrade.php');
|
1249
|
}
|
1250
|
}
|
1251
|
}
|
1252
|
}
|
1253
|
}
|
1254
|
}
|
1255
|
|
1256
|
// extracts the content of a string variable from a string (save alternative to including files)
|
1257
|
if(!function_exists('get_variable_content'))
|
1258
|
{
|
1259
|
function get_variable_content($search, $data, $striptags=true, $convert_to_entities=true)
|
1260
|
{
|
1261
|
$match = '';
|
1262
|
// search for $variable followed by 0-n whitespace then by = then by 0-n whitespace
|
1263
|
// then either " or ' then 0-n characters then either " or ' followed by 0-n whitespace and ;
|
1264
|
// the variable name is returned in $match[1], the content in $match[3]
|
1265
|
if (preg_match('/(\$' .$search .')\s*=\s*("|\')(.*)\2\s*;/', $data, $match))
|
1266
|
{
|
1267
|
if(strip_tags(trim($match[1])) == '$' .$search)
|
1268
|
{
|
1269
|
// variable name matches, return it's value
|
1270
|
$match[3] = ($striptags == true) ? strip_tags($match[3]) : $match[3];
|
1271
|
$match[3] = ($convert_to_entities == true) ? htmlentities($match[3]) : $match[3];
|
1272
|
return $match[3];
|
1273
|
}
|
1274
|
}
|
1275
|
return false;
|
1276
|
}
|
1277
|
}
|
1278
|
|
1279
|
/*
|
1280
|
* @param string $modulname: like saved in addons.directory
|
1281
|
* @param boolean $source: true reads from database, false from info.php
|
1282
|
* @return string: the version as string, if not found returns null
|
1283
|
*/
|
1284
|
|
1285
|
function get_modul_version($modulname, $source = true)
|
1286
|
{
|
1287
|
global $database;
|
1288
|
$version = null;
|
1289
|
if( $source != true )
|
1290
|
{
|
1291
|
$sql = 'SELECT `version` FROM `'.TABLE_PREFIX.'addons` WHERE `directory`=\''.$modulname.'\'';
|
1292
|
$version = $database->get_one($sql);
|
1293
|
} else {
|
1294
|
$info_file = WB_PATH.'/modules/'.$modulname.'/info.php';
|
1295
|
if(file_exists($info_file))
|
1296
|
{
|
1297
|
if(($info_file = file_get_contents($info_file)))
|
1298
|
{
|
1299
|
$version = get_variable_content('module_version', $info_file, false, false);
|
1300
|
$version = ($version !== false) ? $version : null;
|
1301
|
}
|
1302
|
}
|
1303
|
}
|
1304
|
return $version;
|
1305
|
}
|
1306
|
|
1307
|
/*
|
1308
|
* @param string $varlist: commaseperated list of varnames to move into global space
|
1309
|
* @return bool: false if one of the vars already exists in global space (error added to msgQueue)
|
1310
|
*/
|
1311
|
function vars2globals_wrapper($varlist)
|
1312
|
{
|
1313
|
$retval = true;
|
1314
|
if( $varlist != '')
|
1315
|
{
|
1316
|
$vars = explode(',', $varlist);
|
1317
|
foreach( $vars as $var)
|
1318
|
{
|
1319
|
if( isset($GLOBALS[$var]) )
|
1320
|
{
|
1321
|
ErrorLog::write( 'variabe $'.$var.' already defined in global space!!',__FILE__, __FUNCTION__, __LINE__);
|
1322
|
$retval = false;
|
1323
|
}else
|
1324
|
{
|
1325
|
global $$var;
|
1326
|
}
|
1327
|
}
|
1328
|
}
|
1329
|
return $retval;
|
1330
|
}
|
1331
|
|
1332
|
/*
|
1333
|
* filter directory traversal more thoroughly, thanks to hal 9000
|
1334
|
* @param string $dir: directory relative to MEDIA_DIRECTORY
|
1335
|
* @param bool $with_media_dir: true when to include MEDIA_DIRECTORY
|
1336
|
* @return: false if directory traversal detected, real path if not
|
1337
|
*/
|
1338
|
function check_media_path($directory, $with_media_dir = true)
|
1339
|
{
|
1340
|
$md = ($with_media_dir) ? MEDIA_DIRECTORY : '';
|
1341
|
$dir = realpath(WB_PATH . $md . '/' . utf8_decode($directory));
|
1342
|
$required = realpath(WB_PATH . MEDIA_DIRECTORY);
|
1343
|
if (strstr($dir, $required)) {
|
1344
|
return $dir;
|
1345
|
} else {
|
1346
|
return false;
|
1347
|
}
|
1348
|
}
|