PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/library/utils.php

http://github.com/vxtindia/Generatrix
PHP | 311 lines | 250 code | 31 blank | 30 comment | 104 complexity | 3a2146b33d60d79f62047baa743a42c9 MD5 | raw file
  1. <?php
  2. function createHash($string, $length = 6) {
  3. return substr(md5($string), 0, $length);
  4. }
  5. function multi_str_replace($replace, $haystack) {
  6. $content = $haystack;
  7. foreach($replace as $original => $changed) {
  8. $content = str_replace($original, $changed, $content);
  9. }
  10. return $content;
  11. }
  12. function sanitize($term, $separator = '_') {
  13. return preg_replace('/-+/', $separator, trim(preg_replace('/[^a-zA-Z0-9]/', $separator, trim(strtolower(str_replace($separator, ' ', $term))) ) ) );
  14. }
  15. // Creates a link based on the APPLICATION PATH defined in /app/settings/config.json
  16. // USAGE : href('/app/views/homeView.php');
  17. function chref($path) {
  18. if(true == USE_CDN) {
  19. return CDN_URI . href($path);
  20. } else {
  21. return href($path);
  22. }
  23. }
  24. function href($path) {
  25. $relative_root = '';
  26. $slashes = explode('/', APPLICATION_ROOT);
  27. for($i = 0; $i < count($slashes); $i++) {
  28. if(($i > 2) && (isset($slashes[$i])) && ($slashes[$i] != '')) {
  29. $relative_root .= ( '/' . $slashes[$i]);
  30. }
  31. }
  32. return $relative_root . $path;
  33. }
  34. // Calculate time in readable format
  35. function getReadableTime($seconds) {
  36. $mult = 1;
  37. if($seconds < 0) {
  38. $mult = -1;
  39. $seconds = $seconds * (-1);
  40. }
  41. if($seconds < 60) {
  42. if($seconds == 1)
  43. return ($seconds * $mult) . " second";
  44. return ($seconds * $mult) . " seconds";
  45. }
  46. $minutes = round($seconds / 60);
  47. if($minutes < 60) {
  48. if($minutes == 1)
  49. return ($minutes * $mult) . " minute";
  50. return ($minutes * $mult) . " minutes";
  51. }
  52. $hours = round($minutes / 60);
  53. if($hours < 24) {
  54. if($hours == 1)
  55. return ($hours * $mult) . " hour";
  56. return ($hours * $mult) . " hours";
  57. }
  58. $days = round($hours / 24);
  59. if($days < 30) {
  60. if($days == 1)
  61. return ($days * $mult) . " day";
  62. return ($days * $mult) . " days";
  63. }
  64. $months = round($days / 30);
  65. if($months < 12) {
  66. if($months == 1)
  67. return ($months * $mult) . " month";
  68. return ($months * $mult) . " months";
  69. }
  70. $years = round($months / 12);
  71. if($years == 1)
  72. return ($years * $mult) . " year";
  73. return ($years * $mult) . " years";
  74. }
  75. // Use timthumb to create smaller iamges
  76. function cimage($path, $width = '', $height = '') {
  77. if(true == USE_CDN) {
  78. return CDN_URI . image($path, $width, $height);
  79. } else {
  80. return image($path, $width, $height);
  81. }
  82. }
  83. function image($path, $width = '', $height = '') {
  84. if(check($path)) {
  85. return href('/public/scripts/timthumb.phpx?src=' . href($path) . '&w=' . $width . '&h=' . $height);
  86. } else {
  87. display_error('The image ' . href($path) . ' was not found');
  88. }
  89. }
  90. function fileExists($path) {
  91. if(file_exists(path($path)) && !is_dir(path($path))) {
  92. return true;
  93. }
  94. return false;
  95. }
  96. function dirExists($path) {
  97. if(file_exists(path($path)) && is_dir(path($path))) {
  98. return true;
  99. }
  100. return false;
  101. }
  102. // Checks the file permission for a file inside generatrix
  103. function perms($path) {
  104. if(file_exists(path($path))) {
  105. return substr(sprintf('%o', fileperms(path($path))), -3);
  106. } else {
  107. display_error('You are trying to edit the permissions, but the <strong>file ' . path($path) . ' does not exist</strong>');
  108. return false;
  109. }
  110. }
  111. // Returns the full path of the file (use the property defined in /app/settings/config.json
  112. // USAGE : path('/app/views/homeView.php');
  113. function path($path) {
  114. $relative_root = substr(DISK_ROOT, 0, strlen(DISK_ROOT) - 1);
  115. return $relative_root . $path;
  116. }
  117. // check if a value has been set and is not null
  118. function check($value) {
  119. if(isset($value) && ($value != ''))
  120. return true;
  121. return false;
  122. }
  123. function _c($array, $value1 = false, $value2 = false, $value3 = false, $value4 = false, $value5 = false, $value6 = false, $value7 = false, $value8 = false) {
  124. if($value8 !== false) {
  125. return isset($array[$value1][$value2][$value3][$value4][$value5][$value6][$value7][$value8]) ? true : false;
  126. } else if($value7 !== false) {
  127. return isset($array[$value1][$value2][$value3][$value4][$value5][$value6][$value7]) ? true : false;
  128. } else if($value6 !== false) {
  129. return isset($array[$value1][$value2][$value3][$value4][$value5][$value6]) ? true : false;
  130. } else if($value5 !== false) {
  131. return isset($array[$value1][$value2][$value3][$value4][$value5]) ? true : false;
  132. } else if($value4 !== false) {
  133. return isset($array[$value1][$value2][$value3][$value4]) ? true : false;
  134. } else if($value3 !== false) {
  135. return isset($array[$value1][$value2][$value3]) ? true : false;
  136. } else if($value2 !== false) {
  137. return isset($array[$value1][$value2]) ? true : false;
  138. } else if($value1 !== false) {
  139. return isset($array[$value1]) ? true : false;
  140. } else {
  141. return isset($array) ? true : false;
  142. }
  143. }
  144. function _g($array, $value1 = false, $value2 = false, $value3 = false, $value4 = false, $value5 = false, $value6 = false, $value7 = false, $value8 = false) {
  145. if($value8 !== false) {
  146. return isset($array[$value1][$value2][$value3][$value4][$value5][$value6][$value7][$value8]) ? $array[$value1][$value2][$value3][$value4][$value5][$value6][$value7][$value8] : false;
  147. } else if($value7 !== false) {
  148. return isset($array[$value1][$value2][$value3][$value4][$value5][$value6][$value7]) ? $array[$value1][$value2][$value3][$value4][$value5][$value6][$value7] : false;
  149. } else if($value6 !== false) {
  150. return isset($array[$value1][$value2][$value3][$value4][$value5][$value6]) ? $array[$value1][$value2][$value3][$value4][$value5][$value6] : false;
  151. } else if($value5 !== false) {
  152. return isset($array[$value1][$value2][$value3][$value4][$value5]) ? $array[$value1][$value2][$value3][$value4][$value5] : false;
  153. } else if($value4 !== false) {
  154. return isset($array[$value1][$value2][$value3][$value4]) ? $array[$value1][$value2][$value3][$value4] : false;
  155. } else if($value3 !== false) {
  156. return isset($array[$value1][$value2][$value3]) ? $array[$value1][$value2][$value3] : false;
  157. } else if($value2 !== false) {
  158. return isset($array[$value1][$value2]) ? $array[$value1][$value2] : false;
  159. } else if($value1 !== false) {
  160. return isset($array[$value1]) ? $array[$value1] : false;
  161. } else {
  162. return isset($array) ? $array : false;
  163. }
  164. }
  165. function _h($array, $value1 = false, $value2 = false, $value3 = false, $value4 = false, $value5 = false, $value6 = false, $value7 = false, $value8 = false) {
  166. return htmlentities( _g($array, $value1, $value2, $value3, $value4, $value5, $value6, $value7, $value8) );
  167. }
  168. // Check if a value inside an array isset and is not null
  169. function checkArray($array, $value) {
  170. if(is_array($array) && isset($array[$value]) && ($array[$value] != ''))
  171. return true;
  172. return false;
  173. }
  174. // Create json object
  175. function json($data) {
  176. header('Content-Type: application/json');
  177. return json_encode($data);
  178. }
  179. // Redirect to a particular path
  180. // USAGE : location('/user/forgotpass');
  181. function location($path) {
  182. $file_name = '';
  183. $line_number = '';
  184. if(!headers_sent($file_name, $line_number)) {
  185. header("Location: " . href($path));
  186. exit();
  187. } else {
  188. display_system('Cannot redirect the page to <strong>' . href($path) . '</strong> because headers have already been sent. The headers were started by <strong>' . $file_name. ' [ Line Number : '. $line_number . ']</strong>');
  189. }
  190. }
  191. function bt() {
  192. $bt = debug_backtrace();
  193. $output = array(
  194. 'file' => $bt[0]['file'],
  195. 'line' => $bt[0]['line']
  196. );
  197. return $output;
  198. }
  199. function ut($variable) {
  200. return urlencode(trim($variable));
  201. }
  202. function facebook_like() {
  203. $url = APPLICATION_ROOT . href('/' . $_GET['url']);
  204. return '<iframe src="http://www.facebook.com/plugins/like.php?href=' . urlencode($url) . '&amp;layout=button_count&amp;show_faces=true&amp;width=150&amp;action=like&amp;font&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:150px; height:21px;" allowTransparency="true"></iframe>';
  205. }
  206. function prepare($text) {
  207. return stripslashes(html_entity_decode($text));
  208. }
  209. function prepareExcerpt($text) {
  210. return prepare(str_replace('&lt;br&gt;', ' ', $text));
  211. }
  212. function requirePackage($package, $file) {
  213. if(fileExists('/app/packages/' . $package . '/' . $file)) {
  214. require_once(path('/app/packages/' . $package . '/' . $file));
  215. } else {
  216. display_error('The file ' . path('/app/packages/' . $package . '/' . $file) . ' does not exist');
  217. }
  218. }
  219. function chopToWord($string, $length, $ellipsis = true) {
  220. if(strlen($string) <= $length) {
  221. return $string;
  222. } else {
  223. return substr($string, 0, strrpos(substr($string, 0, $length), ' ')) . ($ellipsis ? '&hellip;' : '');
  224. }
  225. }
  226. function br2nl($string) {
  227. return str_replace(array('<br>', '<br />', '<br/>'), PHP_EOL, $string);
  228. }
  229. function timeDiffLog($running, $timer1, $timer2) {
  230. display_system('[' . ($timer2 - $timer1) * 1000 . ' s] ' . $running);
  231. }
  232. /**
  233. Validate an email address.
  234. Provide email address (raw input)
  235. Returns true if the email address has the email
  236. address format and the domain exists.
  237. **/
  238. function validEmail($email) {
  239. $isValid = true;
  240. $atIndex = strrpos($email, "@");
  241. if (is_bool($atIndex) && !$atIndex) {
  242. $isValid = false;
  243. } else {
  244. $domain = substr($email, $atIndex+1);
  245. $local = substr($email, 0, $atIndex);
  246. $localLen = strlen($local);
  247. $domainLen = strlen($domain);
  248. if ($localLen < 1 || $localLen > 64) {
  249. // local part length exceeded
  250. $isValid = false;
  251. } else if ($domainLen < 1 || $domainLen > 255) {
  252. // domain part length exceeded
  253. $isValid = false;
  254. } else if ($local[0] == '.' || $local[$localLen-1] == '.') {
  255. // local part starts or ends with '.'
  256. $isValid = false;
  257. } else if (preg_match('/\\.\\./', $local)) {
  258. // local part has two consecutive dots
  259. $isValid = false;
  260. } else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
  261. // character not valid in domain part
  262. $isValid = false;
  263. } else if (preg_match('/\\.\\./', $domain)) {
  264. // domain part has two consecutive dots
  265. $isValid = false;
  266. } else if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local))) {
  267. // character not valid in local part unless
  268. // local part is quoted
  269. if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\","",$local))) {
  270. $isValid = false;
  271. }
  272. }
  273. //if ($isValid && !(checkdnsrr($domain,"MX") || ↪checkdnsrr($domain,"A"))) {
  274. // // domain not found in DNS
  275. // $isValid = false;
  276. //}
  277. }
  278. return $isValid;
  279. }
  280. ?>