PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/jsrewrite/jfx-private/classes/JFX/Functions.php

http://jfxcms.googlecode.com/
PHP | 206 lines | 109 code | 33 blank | 64 comment | 39 complexity | 69161f1474677cad1a88a98192d68a9e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. ############### COPYLEFT GPLv3 LICENSE ###############
  3. ##
  4. ## JFX Version 0.2.7
  5. ## Website Management Software
  6. ## www.jfxcms.com
  7. ##
  8. ## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
  9. ##
  10. ## Anthony Gallon
  11. ## oi_antz@hotmail.com
  12. ##
  13. ## Permission is hereby granted to any person having a copy of this software
  14. ## to freely use and modify as required so long as the copyright notices
  15. ## and branding remain intact.
  16. ##
  17. ## Full license details available at http://www.jfxcms.com/license
  18. ##
  19. ############### COPYLEFT GPLv3 LICENSE ###############
  20. if(!function_exists('__autoload')){
  21. function __autoload($classname){
  22. global $CONFIG;
  23. if(class_exists($classname, false)) return;
  24. $filename = str_replace('_', '/', $classname).'.php';
  25. if(file_exists($CONFIG->classesDir.'/'.$filename)){
  26. require_once($CONFIG->classesDir.'/'.$filename);
  27. if(class_exists($classname, false)) return;
  28. }elseif(substr(strtolower($classname), 0, 11) == 'jfx_module_'){
  29. // try to locate the module
  30. $moduleKey = str_replace('jfx_module_', '', strtolower($classname));
  31. if(is_dir($CONFIG->modulesDir.'/'.$moduleKey) && is_readable($CONFIG->modulesDir.'/'.$moduleKey.'/'.$moduleKey.'.module.php')){
  32. require_once($CONFIG->modulesDir.'/'.$moduleKey.'/'.$moduleKey.'.module.php');
  33. if(class_exists($classname)) return true;
  34. else throw new exception('Class not found in file: '.
  35. $CONFIG->modulesDir.'/'.$moduleKey.'/'.$moduleKey.'.module.php
  36. '.$className);
  37. }
  38. }elseif(substr(strtolower($classname), 0, 10) == 'jfx_theme_'){
  39. $themeKey = str_replace('jfx_theme_', '', strtolower($classname));
  40. if(is_dir($CONFIG->themesDir.'/'.$themeKey) && is_readable($CONFIG->themesDir.'/'.$themeKey.'/'.$themeKey.'.theme.php')){
  41. require_once($CONFIG->themesDir.'/'.$themeKey.'/'.$themeKey.'.theme.php');
  42. if(class_exists($classname)) return true;
  43. else throw new exception('Class not found in file: '.
  44. $CONFIG->modulesDir.'/'.$moduleKey.'/'.$moduleKey.'.module.php
  45. '.$className);
  46. }
  47. }
  48. return false;
  49. }
  50. }
  51. /**
  52. * Regenerates keys so the first key is 0 and greatest key is (count($arr)-1),
  53. * maintaining natural order of values
  54. *
  55. * @param array $array
  56. * @return array $updatedArray
  57. */
  58. if(!function_exists('array_regenerate_keys')){
  59. function array_regenerate_keys($array)
  60. {
  61. $count = 0;
  62. $arr2 = array();
  63. foreach($array as $v){
  64. $arr2[$count] = $v;
  65. $count++;
  66. };
  67. unset($array, $count);
  68. return $arr2;
  69. }
  70. }
  71. /**
  72. * Array_Insert function to place a value into an array at a given position
  73. *
  74. * @param array $input
  75. * @param int $position
  76. * @param mixed $value
  77. * @return array $updatedInputArray
  78. */
  79. if(!function_exists('array_insert')){
  80. function array_insert ($input, $position, $value)
  81. {
  82. // we will definitely get the result back in an array, pos 1
  83. if (!is_array ($input))
  84. {
  85. $array = array ($input);
  86. $array[$position] = $value;
  87. return $array;
  88. }
  89. // move each key higher to make room for the new key=>value set
  90. for ($i = count($input); $i > $position; $i--)
  91. {
  92. $input[$i] = $input[$i - 1];
  93. }
  94. // insert the value to the position
  95. $input[$position] = $value;
  96. return $input;
  97. }
  98. }
  99. /**
  100. * Get the value from $_POST[$key] or $_FILES[$key] or return the alternative value.
  101. * Truncate to $strlen and sanitize with Antz_Input
  102. *
  103. * @param string $key
  104. * @param mixed $alt
  105. * @param int $strlen
  106. * @return mixed $value
  107. */
  108. if(!function_exists('post')){
  109. function post($key, $alt='', $strlen=255)
  110. {
  111. $input = JFX::registry('JFX_Input');
  112. $newval = $input->getPostValue($key, $strlen, false);
  113. if($newval !== false) return $newval;
  114. $newval = $input->getFilesValue($key, $strlen, false);
  115. if($newval !== false) return $newval;
  116. return $alt;
  117. }
  118. }
  119. /**
  120. * Get the value from $_GET[$key] or return the alternative value. Truncate to $strlen and sanitize
  121. * with Antz_Input
  122. *
  123. * @param string $key
  124. * @param mixed $alt
  125. * @param int $strlen
  126. * @return mixed $value
  127. */
  128. if(!function_exists('get')){
  129. function get($key, $alt='', $strlen=5000)
  130. {
  131. return (array_key_exists($key, $_GET))
  132. ? substr($_GET[$key], 0, $strlen)
  133. : $alt
  134. ;
  135. }
  136. }
  137. /**
  138. * Delete all files and folders recursively
  139. * @param string $dirname
  140. */
  141. if(!function_exists('unlink_directory')){
  142. function unlink_directory($dirname){
  143. if(!is_dir($dirname)){
  144. if(file_exists($dirname)) unlink($dirname);
  145. return;
  146. }
  147. $dh = opendir($dirname);
  148. while($file = readdir($dh)){
  149. if($file == '.' || $file == '..') continue;
  150. if(is_dir($dirname.'/'.$file)) unlink_directory($dirname.'/'.$file);
  151. else unlink($dirname.'/'.$file);
  152. }
  153. rmdir($dirname);
  154. }
  155. }
  156. /**
  157. * Copy a directory recursively
  158. * @param string $dirnameSource
  159. * @param string $dirnameDest
  160. */
  161. if(!function_exists('copy_directory')){
  162. function copy_directory($dirnameSource, $dirnameDest){
  163. if(!is_dir($dirnameDest)){
  164. mkdir($dirnameDest);
  165. }
  166. $dh = opendir($dirnameSource);
  167. while($file = readdir($dh)){
  168. if($file == '.' || $file == '..') continue;
  169. if(is_dir($dirnameSource.'/'.$file)) copy_directory($dirnameSource.'/'.$file, $dirnameDest.'/'.$file);
  170. else copy($dirnameSource.'/'.$file, $dirnameDest.'/'.$file);
  171. }
  172. }
  173. }