public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
Search Repo:
cmsmadesimple-2-0 / lib / misc.functions.php
100644 459 lines (408 sloc) 10.578 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.sf.net
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
 
/**
* Misc functions
*
* @package CMS
*/
 
/**
* Enter description here...
*
* @param unknown $val
* @param integer $quote_style
* @return unknown
*
* $quote_style may be one of:
* ENT_COMPAT : Will convert double-quotes and leave single-quotes alone.
* ENT_QUOTES : Will convert both double and single quotes.
* ENT_NOQUOTES : Will leave both double and single quotes unconverted.
*/
function cms_htmlentities($val, $param=ENT_QUOTES, $charset="UTF-8")
{
  if ($val == "")
  {
    return "";
  }
  $val = str_replace( "&#032;", " ", $val );
  $val = str_replace( "&" , "&amp;" , $val );
  $val = str_replace( "<!--" , "&#60;&#33;--" , $val );
  $val = str_replace( "-->" , "--&#62;" , $val );
  $val = preg_replace( "/<script/i" , "&#60;script" , $val );
  $val = str_replace( ">" , "&gt;" , $val );
  $val = str_replace( "<" , "&lt;" , $val );
  $val = str_replace( "\"" , "&quot;" , $val );
  $val = preg_replace( "/\\$/" , "&#036;" , $val );
  $val = str_replace( "!" , "&#33;" , $val );
  $val = str_replace( "'" , "&#39;" , $val );
  
  return $val;
}
 
/**
* Display $var nicely to the $gCms->errors array if $config['debug'] is set
*
* @param mixed $var
* @param string $title
*/
function debug_buffer($var, $title="")
{
  CmsProfiler::get_instance()->mark(print_r($var));
}
 
function filespec_is_excluded( $file, $excludes )
{
  // strip the path from the file
  foreach( $excludes as $excl )
  {
    if( @preg_match( "/".$excl."/i", basename($file) ) )
    {
      return true;
    }
  }
  return false;
}
 
/**
* Check the permissions of a directory recursively to make sure that
* we have write permission to all files
* &param path start path
*/
function is_directory_writable( $path )
{
  if ( substr ( $path , strlen ( $path ) - 1 ) != '/' ) { $path .= '/' ; }
  $result = true;
  if( $handle = @opendir( $path ) )
  {
    while( false !== ( $file = readdir( $handle ) ) )
    {
      if( $file == '.' || $file == '..' )
      {
        continue;
      }
 
      $p = $path.$file;
 
      if( !@is_writable( $p ) )
      {
        return false;
      }
 
      if( @is_dir( $p ) )
      {
        $result = is_directory_writable( $p );
        if( !$result )
        {
          return false;
        }
      }
    }
    @closedir( $handle );
  }
  else
  {
    return false;
  }
 
  return true;
}
 
/**
* Return an array containing a list of files in a directory
* performs a recursive serach
* @param path start path
* @param maxdepth how deep to browse (-1=unlimited)
* @param mode "FULL"|"DIRS"|"FILES"
* @param d for internal use only
**/
function get_recursive_file_list ( $path , $excludes, $maxdepth = -1 , $mode = "FULL" , $d = 0 )
{
  if ( substr ( $path , strlen ( $path ) - 1 ) != '/' ) { $path .= '/' ; }
  $dirlist = array () ;
  if ( $mode != "FILES" ) { $dirlist[] = $path ; }
  if ( $handle = opendir ( $path ) )
  {
    while ( false !== ( $file = readdir ( $handle ) ) )
    {
      $excluded = filespec_is_excluded( $file, $excludes );
      if ( $file != '.' && $file != '..' && $excluded == false )
      {
        $file = $path . $file ;
        if ( ! @is_dir ( $file ) ) { if ( $mode != "DIRS" ) { $dirlist[] = $file ; } }
        elseif ( $d >=0 && ($d < $maxdepth || $maxdepth < 0) )
        {
          $result = get_recursive_file_list ( $file . '/' , $excludes, $maxdepth , $mode , $d + 1 ) ;
          $dirlist = array_merge ( $dirlist , $result ) ;
        }
      }
    }
    closedir ( $handle ) ;
  }
  if ( $d == 0 ) { natcasesort ( $dirlist ) ; }
  return ( $dirlist ) ;
}
 
function recursive_delete( $dirname )
{
  // all subdirectories and contents:
  if(is_dir($dirname))$dir_handle=opendir($dirname);
  while($file=readdir($dir_handle))
  {
    if($file!="." && $file!="..")
    {
      if(!is_dir($dirname."/".$file))
      {
        if( !@unlink ($dirname."/".$file) )
        {
          closedir( $dir_handle );
          return false;
        }
      }
      else
      {
        recursive_delete($dirname."/".$file);
      }
    }
  }
  closedir($dir_handle);
  if( ! @rmdir($dirname) )
  {
    return false;
  }
  return true;
}
 
function chmod_r( $path, $mode )
{
  if( !is_dir( $path ) )
  return chmod( $path, $mode );
 
  $dh = @opendir( $path );
  if( !$dh ) return FALSE;
 
  while( $file = readdir( $dh ) )
  {
    if( $file == '.' || $file == '..' ) continue;
 
    $p = $path.DIRECTORY_SEPARATOR.$file;
    if( is_dir( $p ) )
    {
      if( !@chmod_r( $p, $mode ) )
      {
        closedir( $dh );
        return false;
      }
    }
    else if( !is_link( $p ) )
    {
      if( !@chmod( $p, $mode ) )
      {
        closedir( $dh );
        return false;
      }
    }
  }
  @closedir( $dh );
  return @chmod( $path, $mode );
}
 
function serialize_object(&$object)
{
  return base64_encode(serialize($object));
}
 
function unserialize_object(&$serialized)
{
  return unserialize(base64_decode($serialized));
}
 
function show_mem($string = '')
{
  var_dump($string . ' -- ' . memory_get_usage());
}
 
function showmem($string = '')
{
  return show_mem($string);
}
 
function munge_string_to_url($alias, $tolower = false)
{
  // replacement.php is encoded utf-8 and must be the first modification of alias
  //include(dirname(__FILE__) . '/replacement.php');
  $alias = str_replace($toreplace, $replacement, $alias);
  
  // lowercase only on empty aliases
  if ($tolower == true)
  {
    $alias = strtolower($alias);
  }
    
  $alias = preg_replace("/[^\w-]+/", "-", $alias);
  $alias = trim($alias, '-');
 
  return $alias;
}
 
/**
* Returns all parameters sent that are destined for the module with
* the given $id
*/
function get_module_parameters($id)
{
  $params = array();
  
  if ($id != '')
    {
      foreach ($_REQUEST as $key=>$value)
  {
   if (strpos($key, (string)$id) !== FALSE && strpos($key, (string)$id) == 0)
   {
   $key = str_replace($id, '', $key);
   if( $key == 'id' || $key == 'returnid' )
    {
     $value = (int)$value;
    }
   $params[$key] = $value;
   }
  }
    }
 
  return $params;
}
 
 
function can_users_upload()
{
  # first, check to see if safe mode is enabled
  # if it is, then check to see the owner of the index.php, moduleinterface.php
  # and the uploads and modules directory. if they all match, then we
  # can upload files.
  # if safe mode is off, then we just have to check the permissions.
  global $gCms;
  $file_index = $gCms->config['root_path'].DIRECTORY_SEPARATOR.'index.php';
  $dir_uploads = $gCms->config['uploads_path'];
 
  $stat_index = @stat($file_index);
  $stat_uploads = @stat($dir_uploads);
 
  if( $stat_index == FALSE || $stat_uploads == FALSE )
    {
      // couldn't get some necessary information.
      return false;
    }
 
  $safe_mode = ini_get_boolean('safe_mode');
  if( $safe_mode )
    {
      // we're in safe mode.
      if( $stat_index[4] != $stat_uploads[4] )
  {
   return FALSE;
  }
    }
 
  // now check to see if we can write to the directories
  if( !is_writable( $dir_modules ) )
    {
      return FALSE;
    }
  if( !is_writable( $dir_uploads ) )
    {
      return FALSE;
    }
  
  // It all worked.
  return TRUE;
}
 
function can_admin_upload()
{
  # first, check to see if safe mode is enabled
  # if it is, then check to see the owner of the index.php, moduleinterface.php
  # and the uploads and modules directory. if they all match, then we
  # can upload files.
  # if safe mode is off, then we just have to check the permissions.
  global $gCms;
  $file_index = $gCms->config['root_path'].DIRECTORY_SEPARATOR.'index.php';
  $file_moduleinterface = $gCms->config['root_path'].DIRECTORY_SEPARATOR.
    $gCms->config['admin_dir'].DIRECTORY_SEPARATOR.'moduleinterface.php';
  $dir_uploads = $gCms->config['uploads_path'];
  $dir_modules = $gCms->config['root_path'].DIRECTORY_SEPARATOR.'modules';
 
  $stat_index = @stat($file_index);
  $stat_moduleinterface = @stat($file_moduleinterface);
  $stat_uploads = @stat($dir_uploads);
  $stat_modules = @stat($dir_modules);
 
  $my_uid = @getmyuid();
 
  if( $my_uid === FALSE || $stat_index == FALSE ||
      $stat_moduleinterface == FALSE || $stat_uploads == FALSE ||
      $stat_modules == FALSE )
    {
      // couldn't get some necessary information.
      return FALSE;
    }
 
  $safe_mode = ini_get_boolean('safe_mode');
  if( $safe_mode )
    {
 
      // we're in safe mode.
      if( ($stat_moduleinterface[4] != $stat_modules[4]) ||
   ($stat_moduleinterface[4] != $stat_uploads[4]) ||
   ($my_uid != $stat_moduleinterface[4]) )
  {
   // owners don't match
   return FALSE;
  }
    }
 
  // now check to see if we can write to the directories
  if( !is_writable( $dir_modules ) )
    {
      return FALSE;
    }
  if( !is_writable( $dir_uploads ) )
    {
      return FALSE;
    }
  
  // It all worked.
  return TRUE;
}
 
function interpret_permissions($perms)
{
  $owner = array();
  $group = array();
  $other = array();
 
  if( $perms & 0400 )
    {
      $owner[] = lang('read');
    }
  if( $perms & 0200 )
    {
      $owner[] = lang('write');
    }
  if( $perms & 0100 )
    {
      $owner[] = lang('execute');
    }
 
  if( $perms & 0040 )
    {
      $group[] = lang('read');
    }
  if( $perms & 0020 )
    {
      $group[] = lang('write');
    }
  if( $perms & 0010 )
    {
      $group[] = lang('execute');
    }
 
  if( $perms & 0004 )
    {
      $other[] = lang('read');
    }
  if( $perms & 0002 )
    {
      $other[] = lang('write');
    }
  if( $perms & 0001 )
    {
      $other[] = lang('execute');
    }
 
  return array($owner,$group,$other);
}
 
function ini_get_boolean($str)
{
  $val1 = ini_get($str);
  $val2 = strtolower($val1);
 
  $ret = 0;
  if( $val2 == 1 || $val2 == '1' || $val2 == 'yes' || $val2 == 'true' || $val2 == 'on' )
     $ret = 1;
  return $ret;
}
 
function GetModuleParameters($id)
{
  return get_module_parameters($id);
}
 
# vim:ts=4 sw=4 noet
?>