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
cmsmadesimple-2-0 / lib / cmsms.api.php
100644 404 lines (361 sloc) 9.516 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
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; -*-
# CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
# 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$
 
/**
* Functions that need to be in a global scope. Mostly for utility and
* shortcuts.
*
* @since 1.1
*/
 
//Defines
define("ROOT_DIR", dirname(dirname(__FILE__)));
define("DS", DIRECTORY_SEPARATOR);
 
//Load file location defines
require_once(ROOT_DIR.DS.'fileloc.php');
 
//So we can use them in __autoload
require_once(ROOT_DIR.DS.'lib'.DS.'classes'.DS.'class.cms_object.php');
require_once(ROOT_DIR.DS.'lib'.DS.'classes'.DS.'class.cms_config.php');
require_once(ROOT_DIR.DS.'lib'.DS.'classes'.DS.'class.cms_cache.php');
 
//set_error_handler('cms_warning_handler', E_WARNING | E_USER_WARNING);
 
/**
* The one and only autoload function for the system. This basically allows us
* to remove a lot of the require_once BS and keep the file loading to as much
* of a minimum as possible.
*/
function cms_autoload($class_name)
{
  //$files = scan_classes();
  $files = CmsCache::get_instance()->call('scan_classes');
  
  //Fix references to older classes
  if ($class_name == 'CMSModule')
    $class_name = 'CmsModule';
    
  if (array_key_exists('class.' . underscore($class_name) . '.php', $files))
  {
    require($files['class.' . underscore($class_name) . '.php']);
  }
  else if (array_key_exists('class.' . underscore($class_name) . '.inc.php', $files))
  {
    require($files['class.' . underscore($class_name) . '.inc.php']);
  }
  else if (array_key_exists('class.' . strtolower($class_name) . '.php', $files))
  {
    require($files['class.' . strtolower($class_name) . '.php']);
  }
  else if (array_key_exists('class.' . strtolower($class_name) . '.inc.php', $files))
  {
    require($files['class.' . strtolower($class_name) . '.inc.php']);
  }
  else if (CmsContentOperations::load_content_type($class_name))
  {
  }
}
 
spl_autoload_register('cms_autoload');
 
function scan_classes()
{
  $dir = cms_join_path(ROOT_DIR,'lib','classes');
  if (!isset($GLOBALS['dirscan']))
  {
    $files = array();
    /*
    $time1 = microtime(true);
    */
    scan_classes_recursive($dir, $files);
    /*
    $time2 = microtime(true);
    var_dump($time2 - $time1);
    */
    $GLOBALS['dirscan'] = $files;
    return $files;
  }
  else
  {
    return $GLOBALS['dirscan'];
  }
}
 
function scan_classes_recursive($dir = '.', &$files)
{
  foreach(new DirectoryIterator($dir) as $file)
  {
    if (!$file->isDot() && $file->getFilename() != '.svn')
    {
      if ($file->isDir())
      {
        $newdir = $file->getPathname();
        scan_classes_recursive($newdir, $files);
      }
      else
      {
        if (starts_with(basename($file->getPathname()), 'class.'))
          $files[basename($file->getPathname())] = $file->getPathname();
      }
    }
  }
  
  return $files;
}
 
/**
* Returns the global CmsApplication singleton. This is the equivalent of
* global $gCms from days gone by.
*/
function cmsms()
{
  return CmsApplication::get_instance();
}
 
/**
* Returns the global CmsApplication singleton. This is the equivalent of
* global $gCms from days gone by.
*/
function cms_global()
{
  return CmsApplication::get_instance();
}
 
/**
* Returns a reference to the adodb(lite) connection singleton object.
* Replaces the global $gCms; $db =& $gCms->GetDb(); routine.
*/
function cms_db()
{
  return CmsDatabase::get_instance();
}
 
function cms_orm($class = '')
{
  if ($class == '')
    return CmsObjectRelationalManager::get_instance();
  else
    return CmsObjectRelationalManager::get_instance()->$class;
}
 
/**
* Returns a reference to the config object (used to be a hash). Replaces
* the global $gCms; $config =& $gCms->GetConfig() routine.
*/
function cms_config()
{
  return CmsConfig::get_instance();
}
 
/**
* Returns a reference to the global smarty object. Replaces
* the global $gCms; $config =& $gCms->GetSmarty() routine.
*/
function cms_smarty()
{
  return CmsSmarty::get_instance();
}
 
/**
* Joins a path together using proper directory separators
* Taken from: http://www.php.net/manual/en/ref.dir.php
*
* @since 1.0
*/
function cms_join_path()
{
   $num_args = func_num_args();
  $args = func_get_args();
  $path = $args[0];
 
  if( $num_args > 1 )
  {
    for ($i = 1; $i < $num_args; $i++)
    {
      $path .= DS.$args[$i];
    }
  }
 
  return $path;
}
 
function starts_with($str, $sub)
{
  return ( substr( $str, 0, strlen( $sub ) ) == $sub );
}
 
function startswith( $str, $sub )
{
  return starts_with($str, $sub);
}
 
function ends_with( $str, $sub )
{
  return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub );
}
 
function endswith( $str, $sub )
{
  return ends_with($str, $sub);
}
 
/**
* Returns given $lower_case_and_underscored_word as a camelCased word.
* Take from cakephp (http://cakephp.org)
* Licensed under the MIT License
*
* @param string $lower_case_and_underscored_word Word to camelize
* @return string Camelized word. likeThis.
*/
function camelize($lowerCaseAndUnderscoredWord)
{
  $replace = str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
  return $replace;
}
 
/**
* Returns an underscore-syntaxed ($like_this_dear_reader) version of the $camel_cased_word.
* Take from cakephp (http://cakephp.org)
* Licensed under the MIT License
*
* @param string $camel_cased_word Camel-cased word to be "underscorized"
* @return string Underscore-syntaxed version of the $camel_cased_word
*/
function underscore($camelCasedWord)
{
  $replace = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
  return $replace;
}
 
/**
* Returns a human-readable string from $lower_case_and_underscored_word,
* by replacing underscores with a space, and by upper-casing the initial characters.
* Take from cakephp (http://cakephp.org)
* Licensed under the MIT License
*
* @param string $lower_case_and_underscored_word String to be made more readable
* @return string Human-readable string
*/
function humanize($lowerCaseAndUnderscoredWord)
{
  $replace = ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord));
  return $replace;
}
 
/**
* Looks through the hash given. If a key named val1 exists, then it's value is
* returned. If not, then val2 is returned. Furthermore, passing one of the php
* filter ids (http://www.php.net/manual/en/ref.filter.php) will filter the
* returned value.
*
* @param array The has to parse through
* @param string The key to look for
* @param mixed The value to return if the key isn't found
* @param integer An optional filter id to pass the returned value through
* @param array Optional parameters for the filter_var call
* @return mixed The result of the coalesce
* @author Ted Kulp
* @since 1.1
**/
function coalesce_key($array, $val1, $val2, $filter = -1, $filter_options = array())
{
  if (isset($array[$val1]))
  {
    if ($filter > -1)
      return filter_var($array[$val1], $filter, $filter_options);
    else
      return $array[$val1];
  }
  return $val2;
}
 
/**
* Finds all keys_to_remove in the hash, removes them,
* and returns the new hash.
*
* @param array The original hash
* @param array The names of the keys to remove
* @return array The result of the key removal
* @author Ted Kulp
* @since 1.1
*/
function remove_keys($array, $keys_to_remove)
{
  if (is_array($array))
  {
    foreach ($keys_to_remove as $k)
    {
      if (array_key_exists($k, $array))
      {
        unset($array[$k]);
      }
    }
  }
  
  return $array;
}
 
/**
* Global wrapper for CmsResponse::redirect()
*
* @param $to The url to redirect to
*
* @return void
* @author Ted Kulp
**/
function redirect($to)
{
  CmsResponse::redirect($to);
}
 
function lang()
{
  $ary = func_get_args();
  return call_user_func_array(array('CmsLanguage', 'translate'), $ary);
}
 
/**
* Returns the currently configured database prefix.
*
* @since 0.4
*/
function cms_db_prefix()
{
  return CmsDatabase::get_prefix();
}
 
/**
* Returns whether or not we're in debug mode currently.
*
* @return boolean
* @author Ted Kulp
*/
function in_debug()
{
  return CmsConfig::get("debug") == true;
}
 
/**
* Error handler so that we can swallow warning messages unless the
* debug flag is on.
*
* @author Ted Kulp
*/
function cms_warning_handler($errno, $errstr, $errfile = '', $errline = -1, $errcontext = array())
{
  if (in_debug())
  {
    trigger_error($errstr, E_USER_WARNING);
  }
}
  
function substr_match($str1, $str2, $reverse = false)
{
  $len = strlen($str1) <= strlen($str2) ? strlen($str1) : strlen($str2);
  $i = 0;
  
  $cmpstr1 = $str1;
  $cmpstr2 = $str2;
  
  if ($reverse)
  {
    $cmpstr1 = strrev($str1);
    $cmpstr2 = strrev($str2);
  }
  
  for (; $i < $len; $i++)
  {
    if (!isset($cmpstr1[$i]) || !isset($cmpstr2[$i]) || $cmpstr1[$i] != $cmpstr2[$i])
    {
      break;
    }
  }
  
  if ($reverse)
  {
    $i = strlen($str1) - $i;
    return substr($str1, $i);
  }
  else
  {
    return substr($str1, 0, $i);
  }
  
}
 
?>