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 1454 2011-06-06 09:56:04Z DarkViper $
|
14
|
* @filesource $HeadURL: svn://isteam.dynxs.de/wb-archiv/branches/2.8.x/wb/framework/functions.php $
|
15
|
* @lastmodified $Date: 2011-06-06 11:56:04 +0200 (Mon, 06 Jun 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
|
$string = str_replace($bad, '', $string);
|
687
|
// replace multiple dots in filename to single dot and (multiple) dots at the end of the filename to nothing
|
688
|
$string = preg_replace(array('/\.+/', '/\.+$/', '/\s/'), array('.', '', '_'), $string);
|
689
|
// Clean any page spacers at the end of string
|
690
|
$string = trim($string);
|
691
|
// Finally, return the cleaned string
|
692
|
return $string;
|
693
|
}
|
694
|
|
695
|
// Function to work out a page link
|
696
|
if(!function_exists('page_link'))
|
697
|
{
|
698
|
function page_link($link)
|
699
|
{
|
700
|
global $admin;
|
701
|
return $admin->page_link($link);
|
702
|
}
|
703
|
}
|
704
|
|
705
|
// Create a new file in the pages directory
|
706
|
function create_access_file($filename,$page_id,$level)
|
707
|
{
|
708
|
global $admin, $MESSAGE;
|
709
|
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/'))
|
710
|
{
|
711
|
$admin->print_error($MESSAGE['PAGES']['CANNOT_CREATE_ACCESS_FILE']);
|
712
|
}
|
713
|
else
|
714
|
{
|
715
|
// First make sure parent folder exists
|
716
|
$parent_folders = explode('/',str_replace(WB_PATH.PAGES_DIRECTORY, '', dirname($filename)));
|
717
|
$parents = '';
|
718
|
foreach($parent_folders AS $parent_folder)
|
719
|
{
|
720
|
if($parent_folder != '/' AND $parent_folder != '')
|
721
|
{
|
722
|
$parents .= '/'.$parent_folder;
|
723
|
if(!file_exists(WB_PATH.PAGES_DIRECTORY.$parents))
|
724
|
{
|
725
|
make_dir(WB_PATH.PAGES_DIRECTORY.$parents);
|
726
|
}
|
727
|
}
|
728
|
}
|
729
|
// The depth of the page directory in the directory hierarchy
|
730
|
// '/pages' is at depth 1
|
731
|
$pages_dir_depth=count(explode('/',PAGES_DIRECTORY))-1;
|
732
|
// Work-out how many ../'s we need to get to the index page
|
733
|
$index_location = '';
|
734
|
for($i = 0; $i < $level + $pages_dir_depth; $i++)
|
735
|
{
|
736
|
$index_location .= '../';
|
737
|
}
|
738
|
$content = ''.
|
739
|
'<?php
|
740
|
$page_id = '.$page_id.';
|
741
|
require("'.$index_location.'config.php");
|
742
|
require(WB_PATH."/index.php");
|
743
|
?>';
|
744
|
$handle = fopen($filename, 'w');
|
745
|
fwrite($handle, $content);
|
746
|
fclose($handle);
|
747
|
// Chmod the file
|
748
|
change_mode($filename);
|
749
|
}
|
750
|
}
|
751
|
|
752
|
// Function for working out a file mime type (if the in-built PHP one is not enabled)
|
753
|
if(!function_exists('mime_content_type'))
|
754
|
{
|
755
|
function mime_content_type($filename)
|
756
|
{
|
757
|
$mime_types = array(
|
758
|
'txt' => 'text/plain',
|
759
|
'htm' => 'text/html',
|
760
|
'html' => 'text/html',
|
761
|
'php' => 'text/html',
|
762
|
'css' => 'text/css',
|
763
|
'js' => 'application/javascript',
|
764
|
'json' => 'application/json',
|
765
|
'xml' => 'application/xml',
|
766
|
'swf' => 'application/x-shockwave-flash',
|
767
|
'flv' => 'video/x-flv',
|
768
|
|
769
|
// images
|
770
|
'png' => 'image/png',
|
771
|
'jpe' => 'image/jpeg',
|
772
|
'jpeg' => 'image/jpeg',
|
773
|
'jpg' => 'image/jpeg',
|
774
|
'gif' => 'image/gif',
|
775
|
'bmp' => 'image/bmp',
|
776
|
'ico' => 'image/vnd.microsoft.icon',
|
777
|
'tiff' => 'image/tiff',
|
778
|
'tif' => 'image/tiff',
|
779
|
'svg' => 'image/svg+xml',
|
780
|
'svgz' => 'image/svg+xml',
|
781
|
|
782
|
// archives
|
783
|
'zip' => 'application/zip',
|
784
|
'rar' => 'application/x-rar-compressed',
|
785
|
'exe' => 'application/x-msdownload',
|
786
|
'msi' => 'application/x-msdownload',
|
787
|
'cab' => 'application/vnd.ms-cab-compressed',
|
788
|
|
789
|
// audio/video
|
790
|
'mp3' => 'audio/mpeg',
|
791
|
'mp4' => 'audio/mpeg',
|
792
|
'qt' => 'video/quicktime',
|
793
|
'mov' => 'video/quicktime',
|
794
|
|
795
|
// adobe
|
796
|
'pdf' => 'application/pdf',
|
797
|
'psd' => 'image/vnd.adobe.photoshop',
|
798
|
'ai' => 'application/postscript',
|
799
|
'eps' => 'application/postscript',
|
800
|
'ps' => 'application/postscript',
|
801
|
|
802
|
// ms office
|
803
|
'doc' => 'application/msword',
|
804
|
'rtf' => 'application/rtf',
|
805
|
'xls' => 'application/vnd.ms-excel',
|
806
|
'ppt' => 'application/vnd.ms-powerpoint',
|
807
|
|
808
|
// open office
|
809
|
'odt' => 'application/vnd.oasis.opendocument.text',
|
810
|
'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
|
811
|
);
|
812
|
|
813
|
$temp = explode('.',$filename);
|
814
|
$ext = strtolower(array_pop($temp));
|
815
|
|
816
|
if (array_key_exists($ext, $mime_types))
|
817
|
{
|
818
|
return $mime_types[$ext];
|
819
|
}
|
820
|
elseif (function_exists('finfo_open'))
|
821
|
{
|
822
|
$finfo = finfo_open(FILEINFO_MIME);
|
823
|
$mimetype = finfo_file($finfo, $filename);
|
824
|
finfo_close($finfo);
|
825
|
return $mimetype;
|
826
|
}
|
827
|
else
|
828
|
{
|
829
|
return 'application/octet-stream';
|
830
|
}
|
831
|
}
|
832
|
}
|
833
|
|
834
|
// Generate a thumbnail from an image
|
835
|
function make_thumb($source, $destination, $size)
|
836
|
{
|
837
|
// Check if GD is installed
|
838
|
if(extension_loaded('gd') && function_exists('imageCreateFromJpeg'))
|
839
|
{
|
840
|
// First figure out the size of the thumbnail
|
841
|
list($original_x, $original_y) = getimagesize($source);
|
842
|
if ($original_x > $original_y)
|
843
|
{
|
844
|
$thumb_w = $size;
|
845
|
$thumb_h = $original_y*($size/$original_x);
|
846
|
}
|
847
|
if ($original_x < $original_y)
|
848
|
{
|
849
|
$thumb_w = $original_x*($size/$original_y);
|
850
|
$thumb_h = $size;
|
851
|
}
|
852
|
if ($original_x == $original_y)
|
853
|
{
|
854
|
$thumb_w = $size;
|
855
|
$thumb_h = $size;
|
856
|
}
|
857
|
// Now make the thumbnail
|
858
|
$source = imageCreateFromJpeg($source);
|
859
|
$dst_img = ImageCreateTrueColor($thumb_w, $thumb_h);
|
860
|
imagecopyresampled($dst_img,$source,0,0,0,0,$thumb_w,$thumb_h,$original_x,$original_y);
|
861
|
imagejpeg($dst_img, $destination);
|
862
|
// Clear memory
|
863
|
imagedestroy($dst_img);
|
864
|
imagedestroy($source);
|
865
|
// Return true
|
866
|
return true;
|
867
|
} else {
|
868
|
return false;
|
869
|
}
|
870
|
}
|
871
|
|
872
|
/*
|
873
|
* Function to work-out a single part of an octal permission value
|
874
|
*
|
875
|
* @param mixed $octal_value: an octal value as string (i.e. '0777') or real octal integer (i.e. 0777 | 777)
|
876
|
* @param string $who: char or string for whom the permission is asked( U[ser] / G[roup] / O[thers] )
|
877
|
* @param string $action: char or string with the requested action( r[ead..] / w[rite..] / e|x[ecute..] )
|
878
|
* @return boolean
|
879
|
*/
|
880
|
function extract_permission($octal_value, $who, $action)
|
881
|
{
|
882
|
// Make sure that all arguments are set and $octal_value is a real octal-integer
|
883
|
if( ($who == '') || ($action == '') || (preg_match( '/[^0-7]/', (string)$octal_value )) )
|
884
|
{
|
885
|
return false; // invalid argument, so return false
|
886
|
}
|
887
|
// convert $octal_value into a decimal-integer to be sure having a valid value
|
888
|
$right_mask = octdec($octal_value);
|
889
|
$action_mask = 0;
|
890
|
// set the $action related bit in $action_mask
|
891
|
switch($action[0]) // get action from first char of $action
|
892
|
{
|
893
|
case 'r':
|
894
|
case 'R':
|
895
|
$action_mask = 4; // set read-bit only (2^2)
|
896
|
break;
|
897
|
case 'w':
|
898
|
case 'W':
|
899
|
$action_mask = 2; // set write-bit only (2^1)
|
900
|
break;
|
901
|
case 'e':
|
902
|
case 'E':
|
903
|
case 'x':
|
904
|
case 'X':
|
905
|
$action_mask = 1; // set execute-bit only (2^0)
|
906
|
break;
|
907
|
default:
|
908
|
return false; // undefined action name, so return false
|
909
|
}
|
910
|
// shift action-mask into the right position
|
911
|
switch($who[0]) // get who from first char of $who
|
912
|
{
|
913
|
case 'u':
|
914
|
case 'U':
|
915
|
$action_mask <<= 3; // shift left 3 bits
|
916
|
case 'g':
|
917
|
case 'G':
|
918
|
$action_mask <<= 3; // shift left 3 bits
|
919
|
case 'o':
|
920
|
case 'O':
|
921
|
/* NOP */
|
922
|
break;
|
923
|
default:
|
924
|
return false; // undefined who, so return false
|
925
|
}
|
926
|
return( ($right_mask & $action_mask) != 0 ); // return result of binary-AND
|
927
|
}
|
928
|
|
929
|
// Function to delete a page
|
930
|
function delete_page($page_id)
|
931
|
{
|
932
|
global $admin, $database, $MESSAGE;
|
933
|
// Find out more about the page
|
934
|
$sql = 'SELECT `page_id`, `menu_title`, `page_title`, `level`, `link`, `parent`, `modified_by`, `modified_when` ';
|
935
|
$sql .= 'FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
|
936
|
$results = $database->query($sql);
|
937
|
if($database->is_error()) { $admin->print_error($database->get_error()); }
|
938
|
if($results->numRows() == 0) { $admin->print_error($MESSAGE['PAGES']['NOT_FOUND']); }
|
939
|
$results_array = $results->fetchRow();
|
940
|
$parent = $results_array['parent'];
|
941
|
$level = $results_array['level'];
|
942
|
$link = $results_array['link'];
|
943
|
$page_title = $results_array['page_title'];
|
944
|
$menu_title = $results_array['menu_title'];
|
945
|
|
946
|
// Get the sections that belong to the page
|
947
|
$sql = 'SELECT `section_id`, `module` FROM `'.TABLE_PREFIX.'sections` WHERE `page_id` = '.$page_id;
|
948
|
$query_sections = $database->query($sql);
|
949
|
if($query_sections->numRows() > 0)
|
950
|
{
|
951
|
while($section = $query_sections->fetchRow())
|
952
|
{
|
953
|
// Set section id
|
954
|
$section_id = $section['section_id'];
|
955
|
// Include the modules delete file if it exists
|
956
|
if(file_exists(WB_PATH.'/modules/'.$section['module'].'/delete.php'))
|
957
|
{
|
958
|
include(WB_PATH.'/modules/'.$section['module'].'/delete.php');
|
959
|
}
|
960
|
}
|
961
|
}
|
962
|
// Update the pages table
|
963
|
$sql = 'DELETE FROM `'.TABLE_PREFIX.'pages` WHERE `page_id` = '.$page_id;
|
964
|
$database->query($sql);
|
965
|
if($database->is_error())
|
966
|
{
|
967
|
$admin->print_error($database->get_error());
|
968
|
}
|
969
|
// Update the sections table
|
970
|
$sql = 'DELETE FROM `'.TABLE_PREFIX.'sections` WHERE `page_id` = '.$page_id;
|
971
|
$database->query($sql);
|
972
|
if($database->is_error()) {
|
973
|
$admin->print_error($database->get_error());
|
974
|
}
|
975
|
// Include the ordering class or clean-up ordering
|
976
|
include_once(WB_PATH.'/framework/class.order.php');
|
977
|
$order = new order(TABLE_PREFIX.'pages', 'position', 'page_id', 'parent');
|
978
|
$order->clean($parent);
|
979
|
// Unlink the page access file and directory
|
980
|
$directory = WB_PATH.PAGES_DIRECTORY.$link;
|
981
|
$filename = $directory.PAGE_EXTENSION;
|
982
|
$directory .= '/';
|
983
|
if(file_exists($filename))
|
984
|
{
|
985
|
if(!is_writable(WB_PATH.PAGES_DIRECTORY.'/'))
|
986
|
{
|
987
|
$admin->print_error($MESSAGE['PAGES']['CANNOT_DELETE_ACCESS_FILE']);
|
988
|
}
|
989
|
else
|
990
|
{
|
991
|
unlink($filename);
|
992
|
if( file_exists($directory) &&
|
993
|
(rtrim($directory,'/') != WB_PATH.PAGES_DIRECTORY) &&
|
994
|
(substr($link, 0, 1) != '.'))
|
995
|
{
|
996
|
rm_full_dir($directory);
|
997
|
}
|
998
|
}
|
999
|
}
|
1000
|
}
|
1001
|
|
1002
|
/*
|
1003
|
* @param string $file: name of the file to read
|
1004
|
* @param int $size: number of maximum bytes to read (0 = complete file)
|
1005
|
* @return string: the content as string, false on error
|
1006
|
*/
|
1007
|
function getFilePart($file, $size = 0)
|
1008
|
{
|
1009
|
$file_content = '';
|
1010
|
if( file_exists($file) && is_file($file) && is_readable($file))
|
1011
|
{
|
1012
|
if($size == 0)
|
1013
|
{
|
1014
|
$size = filesize($file);
|
1015
|
}
|
1016
|
if(($fh = fopen($file, 'rb')))
|
1017
|
{
|
1018
|
if( ($file_content = fread($fh, $size)) !== false )
|
1019
|
{
|
1020
|
return $file_content;
|
1021
|
}
|
1022
|
fclose($fh);
|
1023
|
}
|
1024
|
}
|
1025
|
return false;
|
1026
|
}
|
1027
|
|
1028
|
/**
|
1029
|
* replace varnames with values in a string
|
1030
|
*
|
1031
|
* @param string $subject: stringvariable with vars placeholder
|
1032
|
* @param array $replace: values to replace vars placeholder
|
1033
|
* @return string
|
1034
|
*/
|
1035
|
function replace_vars($subject = '', &$replace = null )
|
1036
|
{
|
1037
|
if(is_array($replace))
|
1038
|
{
|
1039
|
foreach ($replace as $key => $value)
|
1040
|
{
|
1041
|
$subject = str_replace("{{".$key."}}", $value, $subject);
|
1042
|
}
|
1043
|
}
|
1044
|
return $subject;
|
1045
|
}
|
1046
|
|
1047
|
// Load module into DB
|
1048
|
function load_module($directory, $install = false)
|
1049
|
{
|
1050
|
global $database,$admin,$MESSAGE;
|
1051
|
$retVal = false;
|
1052
|
if(is_dir($directory) && file_exists($directory.'/info.php'))
|
1053
|
{
|
1054
|
require($directory.'/info.php');
|
1055
|
if(isset($module_name))
|
1056
|
{
|
1057
|
if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
|
1058
|
if(!isset($module_platform) && isset($module_designed_for)) { $module_platform = $module_designed_for; }
|
1059
|
if(!isset($module_function) && isset($module_type)) { $module_function = $module_type; }
|
1060
|
$module_function = strtolower($module_function);
|
1061
|
// Check that it doesn't already exist
|
1062
|
$sqlwhere = 'WHERE `type` = \'module\' AND `directory` = \''.$module_directory.'\'';
|
1063
|
$sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'addons` '.$sqlwhere;
|
1064
|
if( $database->get_one($sql) )
|
1065
|
{
|
1066
|
$sql = 'UPDATE `'.TABLE_PREFIX.'addons` SET ';
|
1067
|
}else{
|
1068
|
// Load into DB
|
1069
|
$sql = 'INSERT INTO `'.TABLE_PREFIX.'addons` SET ';
|
1070
|
$sqlwhere = '';
|
1071
|
}
|
1072
|
$sql .= '`directory` = \''.$module_directory.'\', ';
|
1073
|
$sql .= '`name` = \''.$module_name.'\', ';
|
1074
|
$sql .= '`description`= \''.addslashes($module_description).'\', ';
|
1075
|
$sql .= '`type`= \'module\', ';
|
1076
|
$sql .= '`function` = \''.$module_function.'\', ';
|
1077
|
$sql .= '`version` = \''.$module_version.'\', ';
|
1078
|
$sql .= '`platform` = \''.$module_platform.'\', ';
|
1079
|
$sql .= '`author` = \''.addslashes($module_author).'\', ';
|
1080
|
$sql .= '`license` = \''.addslashes($module_license).'\'';
|
1081
|
$sql .= $sqlwhere;
|
1082
|
$retVal = $database->query($sql);
|
1083
|
// Run installation script
|
1084
|
if($install == true)
|
1085
|
{
|
1086
|
if(file_exists($directory.'/install.php'))
|
1087
|
{
|
1088
|
require($directory.'/install.php');
|
1089
|
}
|
1090
|
}
|
1091
|
}
|
1092
|
}
|
1093
|
}
|
1094
|
|
1095
|
// Load template into DB
|
1096
|
function load_template($directory)
|
1097
|
{
|
1098
|
global $database, $admin;
|
1099
|
$retVal = false;
|
1100
|
if(is_dir($directory) && file_exists($directory.'/info.php'))
|
1101
|
{
|
1102
|
require($directory.'/info.php');
|
1103
|
if(isset($template_name))
|
1104
|
{
|
1105
|
if(!isset($template_license))
|
1106
|
{
|
1107
|
$template_license = 'GNU General Public License';
|
1108
|
}
|
1109
|
if(!isset($template_platform) && isset($template_designed_for))
|
1110
|
{
|
1111
|
$template_platform = $template_designed_for;
|
1112
|
}
|
1113
|
if(!isset($template_function))
|
1114
|
{
|
1115
|
$template_function = 'template';
|
1116
|
}
|
1117
|
// Check that it doesn't already exist
|
1118
|
$sqlwhere = 'WHERE `type` = \'template\' AND `directory` = \''.$template_directory.'\'';
|
1119
|
$sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'addons` '.$sqlwhere;
|
1120
|
if( $database->get_one($sql) )
|
1121
|
{
|
1122
|
$sql = 'UPDATE `'.TABLE_PREFIX.'addons` SET ';
|
1123
|
}else{
|
1124
|
// Load into DB
|
1125
|
$sql = 'INSERT INTO `'.TABLE_PREFIX.'addons` SET ';
|
1126
|
$sqlwhere = '';
|
1127
|
}
|
1128
|
$sql .= '`directory` = \''.$template_directory.'\', ';
|
1129
|
$sql .= '`name` = \''.$template_name.'\', ';
|
1130
|
$sql .= '`description`= \''.addslashes($template_description).'\', ';
|
1131
|
$sql .= '`type`= \'template\', ';
|
1132
|
$sql .= '`function` = \''.$template_function.'\', ';
|
1133
|
$sql .= '`version` = \''.$template_version.'\', ';
|
1134
|
$sql .= '`platform` = \''.$template_platform.'\', ';
|
1135
|
$sql .= '`author` = \''.addslashes($template_author).'\', ';
|
1136
|
$sql .= '`license` = \''.addslashes($template_license).'\' ';
|
1137
|
$sql .= $sqlwhere;
|
1138
|
$retVal = $database->query($sql);
|
1139
|
}
|
1140
|
}
|
1141
|
return $retVal;
|
1142
|
}
|
1143
|
|
1144
|
// Load language into DB
|
1145
|
function load_language($file)
|
1146
|
{
|
1147
|
global $database,$admin;
|
1148
|
$retVal = false;
|
1149
|
if (file_exists($file) && preg_match('#^([A-Z]{2}.php)#', basename($file)))
|
1150
|
{
|
1151
|
// require($file); it's to large
|
1152
|
// read contents of the template language file into string
|
1153
|
$data = @file_get_contents(WB_PATH.'/languages/'.str_replace('.php','',basename($file)).'.php');
|
1154
|
// use regular expressions to fetch the content of the variable from the string
|
1155
|
$language_name = get_variable_content('language_name', $data, false, false);
|
1156
|
$language_code = get_variable_content('language_code', $data, false, false);
|
1157
|
$language_author = get_variable_content('language_author', $data, false, false);
|
1158
|
$language_version = get_variable_content('language_version', $data, false, false);
|
1159
|
$language_platform = get_variable_content('language_platform', $data, false, false);
|
1160
|
|
1161
|
if(isset($language_name))
|
1162
|
{
|
1163
|
if(!isset($language_license)) { $language_license = 'GNU General Public License'; }
|
1164
|
if(!isset($language_platform) && isset($language_designed_for)) { $language_platform = $language_designed_for; }
|
1165
|
// Check that it doesn't already exist
|
1166
|
$sqlwhere = 'WHERE `type` = \'language\' AND `directory` = \''.$language_code.'\'';
|
1167
|
$sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'addons` '.$sqlwhere;
|
1168
|
if( $database->get_one($sql) )
|
1169
|
{
|
1170
|
$sql = 'UPDATE `'.TABLE_PREFIX.'addons` SET ';
|
1171
|
}else{
|
1172
|
// Load into DB
|
1173
|
$sql = 'INSERT INTO `'.TABLE_PREFIX.'addons` SET ';
|
1174
|
$sqlwhere = '';
|
1175
|
}
|
1176
|
$sql .= '`directory` = \''.$language_code.'\', ';
|
1177
|
$sql .= '`name` = \''.$language_name.'\', ';
|
1178
|
$sql .= '`type`= \'language\', ';
|
1179
|
$sql .= '`version` = \''.$language_version.'\', ';
|
1180
|
$sql .= '`platform` = \''.$language_platform.'\', ';
|
1181
|
$sql .= '`author` = \''.addslashes($language_author).'\', ';
|
1182
|
$sql .= '`license` = \''.addslashes($language_license).'\' ';
|
1183
|
$sql .= $sqlwhere;
|
1184
|
$retVal = $database->query($sql);
|
1185
|
}
|
1186
|
}
|
1187
|
return $retVal;
|
1188
|
}
|
1189
|
|
1190
|
// Upgrade module info in DB, optionally start upgrade script
|
1191
|
function upgrade_module($directory, $upgrade = false)
|
1192
|
{
|
1193
|
global $database, $admin, $MESSAGE, $new_module_version;
|
1194
|
$mod_directory = WB_PATH.'/modules/'.$directory;
|
1195
|
if(file_exists($mod_directory.'/info.php'))
|
1196
|
{
|
1197
|
require($mod_directory.'/info.php');
|
1198
|
if(isset($module_name))
|
1199
|
{
|
1200
|
if(!isset($module_license)) { $module_license = 'GNU General Public License'; }
|
1201
|
if(!isset($module_platform) && isset($module_designed_for)) { $module_platform = $module_designed_for; }
|
1202
|
if(!isset($module_function) && isset($module_type)) { $module_function = $module_type; }
|
1203
|
$module_function = strtolower($module_function);
|
1204
|
// Check that it does already exist
|
1205
|
$sql = 'SELECT COUNT(*) FROM `'.TABLE_PREFIX.'addons` ';
|
1206
|
$sql .= 'WHERE `directory` = \''.$module_directory.'\'';
|
1207
|
if( $database->get_one($sql) )
|
1208
|
{
|
1209
|
// Update in DB
|
1210
|
$sql = 'UPDATE `'.TABLE_PREFIX.'addons` SET ';
|
1211
|
$sql .= '`version` = "'.$module_version.'", ';
|
1212
|
$sql .= '`description` = "'.addslashes($module_description).'", ';
|
1213
|
$sql .= '`platform` = \''.$module_platform.'\', ';
|
1214
|
$sql .= '`author` = \''.addslashes($module_author).'\', ';
|
1215
|
$sql .= '`license` = \''.addslashes($module_license).'\' ';
|
1216
|
$sql .= 'WHERE `directory` = \''.$module_directory.'\' ';
|
1217
|
$database->query($sql);
|
1218
|
if($database->is_error()) {
|
1219
|
$admin->print_error($database->get_error());
|
1220
|
}
|
1221
|
|
1222
|
// Run upgrade script
|
1223
|
if($upgrade == true)
|
1224
|
{
|
1225
|
if(file_exists($mod_directory.'/upgrade.php'))
|
1226
|
{
|
1227
|
require($mod_directory.'/upgrade.php');
|
1228
|
}
|
1229
|
}
|
1230
|
}
|
1231
|
}
|
1232
|
}
|
1233
|
}
|
1234
|
|
1235
|
// extracts the content of a string variable from a string (save alternative to including files)
|
1236
|
if(!function_exists('get_variable_content'))
|
1237
|
{
|
1238
|
function get_variable_content($search, $data, $striptags=true, $convert_to_entities=true)
|
1239
|
{
|
1240
|
$match = '';
|
1241
|
// search for $variable followed by 0-n whitespace then by = then by 0-n whitespace
|
1242
|
// then either " or ' then 0-n characters then either " or ' followed by 0-n whitespace and ;
|
1243
|
// the variable name is returned in $match[1], the content in $match[3]
|
1244
|
if (preg_match('/(\$' .$search .')\s*=\s*("|\')(.*)\2\s*;/', $data, $match))
|
1245
|
{
|
1246
|
if(strip_tags(trim($match[1])) == '$' .$search)
|
1247
|
{
|
1248
|
// variable name matches, return it's value
|
1249
|
$match[3] = ($striptags == true) ? strip_tags($match[3]) : $match[3];
|
1250
|
$match[3] = ($convert_to_entities == true) ? htmlentities($match[3]) : $match[3];
|
1251
|
return $match[3];
|
1252
|
}
|
1253
|
}
|
1254
|
return false;
|
1255
|
}
|
1256
|
}
|
1257
|
|
1258
|
/*
|
1259
|
* @param string $modulname: like saved in addons.directory
|
1260
|
* @param boolean $source: true reads from database, false from info.php
|
1261
|
* @return string: the version as string, if not found returns null
|
1262
|
*/
|
1263
|
|
1264
|
function get_modul_version($modulname, $source = true)
|
1265
|
{
|
1266
|
global $database;
|
1267
|
$version = null;
|
1268
|
if( $source != true )
|
1269
|
{
|
1270
|
$sql = 'SELECT `version` FROM `'.TABLE_PREFIX.'addons` WHERE `directory`=\''.$modulname.'\'';
|
1271
|
$version = $database->get_one($sql);
|
1272
|
} else {
|
1273
|
$info_file = WB_PATH.'/modules/'.$modulname.'/info.php';
|
1274
|
if(file_exists($info_file))
|
1275
|
{
|
1276
|
if(($info_file = file_get_contents($info_file)))
|
1277
|
{
|
1278
|
$version = get_variable_content('module_version', $info_file, false, false);
|
1279
|
$version = ($version !== false) ? $version : null;
|
1280
|
}
|
1281
|
}
|
1282
|
}
|
1283
|
return $version;
|
1284
|
}
|
1285
|
|
1286
|
/*
|
1287
|
* @param string $varlist: commaseperated list of varnames to move into global space
|
1288
|
* @return bool: false if one of the vars already exists in global space (error added to msgQueue)
|
1289
|
*/
|
1290
|
function vars2globals_wrapper($varlist)
|
1291
|
{
|
1292
|
$retval = true;
|
1293
|
if( $varlist != '')
|
1294
|
{
|
1295
|
$vars = explode(',', $varlist);
|
1296
|
foreach( $vars as $var)
|
1297
|
{
|
1298
|
if( isset($GLOBALS[$var]) )
|
1299
|
{
|
1300
|
ErrorLog::write( 'variabe $'.$var.' already defined in global space!!',__FILE__, __FUNCTION__, __LINE__);
|
1301
|
$retval = false;
|
1302
|
}else
|
1303
|
{
|
1304
|
global $$var;
|
1305
|
}
|
1306
|
}
|
1307
|
}
|
1308
|
return $retval;
|
1309
|
}
|
1310
|
|
1311
|
/*
|
1312
|
* filter directory traversal more thoroughly, thanks to hal 9000
|
1313
|
* @param string $dir: directory relative to MEDIA_DIRECTORY
|
1314
|
* @param bool $with_media_dir: true when to include MEDIA_DIRECTORY
|
1315
|
* @return: false if directory traversal detected, real path if not
|
1316
|
*/
|
1317
|
function check_media_path($directory, $with_media_dir = true)
|
1318
|
{
|
1319
|
$md = ($with_media_dir) ? MEDIA_DIRECTORY : '';
|
1320
|
$dir = realpath(WB_PATH . $md . '/' . utf8_decode($directory));
|
1321
|
$required = realpath(WB_PATH . MEDIA_DIRECTORY);
|
1322
|
if (strstr($dir, $required)) {
|
1323
|
return $dir;
|
1324
|
} else {
|
1325
|
return false;
|
1326
|
}
|
1327
|
}
|