PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/core/functions.php

http://github.com/caferrari/vorticephp
PHP | 201 lines | 159 code | 11 blank | 31 comment | 9 complexity | 3cf5751ba576e3f750e888b424126191 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /*
  3. * Copyright (c) 2008, Carlos André Ferrari <[carlos@]ferrari.eti.br>; Luan Almeida <[luan@]luan.eti.br>
  4. * All rights reserved.
  5. */
  6. /**
  7. * Framework functions
  8. * @package Framework
  9. */
  10. /**
  11. * auto load classes
  12. * @param $class classname
  13. * @return void
  14. */
  15. function __autoload($class)
  16. {
  17. if (class_exists($class, false) || interface_exists($class, false)) return;
  18. $folders = array(
  19. "core/classes/",
  20. "core/classes/exceptions",
  21. "app/classes/",
  22. "app/model/",
  23. "app/facade/",
  24. "app/controller/",
  25. "app/helper/",
  26. "core/helper/"
  27. );
  28. if (defined("module")){
  29. $m = module;
  30. array_unshift($folders, "app/modules/$m/model/", "app/modules/$m/controller/");
  31. }
  32. foreach ($folders as $f)
  33. if (file_exists(root . "{$f}/{$class}.php")) { include_once(root . "{$f}/{$class}.php"); return; }
  34. //throw (new Exception("Class not found: $class"));
  35. }
  36. /**
  37. * Check if the request is from a mobile phone
  38. * @return boolean
  39. */
  40. function check_lib($filename, $base){
  41. $path = root . "core/lib/$filename";
  42. if (!file_exists($path)) return false;
  43. $base = strtolower($base);
  44. $found = false;
  45. $file = fopen("$path","r");
  46. $l = fgets($file, 128);
  47. if (!trim($l)) return false;
  48. do {
  49. $found = (strpos($base, $l) !== false);
  50. $l = trim(fgets($file, 128));
  51. } while (!$found && !feof($file));
  52. return $found ? true : false;
  53. }
  54. /**
  55. * Check if the request is from a mobile phone
  56. * @return boolean
  57. */
  58. function is_mobile(){
  59. $mobile = isset($_SESSION["vortice-mobile"]) ? $_SESSION["vortice-mobile"] : "";
  60. if ($mobile === ""){
  61. $base = isset($_SERVER['HTTP_X_OPERAMINI_PHONE']) ? $_SERVER['HTTP_X_OPERAMINI_PHONE'] :
  62. isset($_SERVER['X-OperaMini-Phone-UA']) ? $_SERVER['X-OperaMini-Phone-UA'] :
  63. isset($_SERVER['X-OperaMini-Phone']) ? $_SERVER['X-OperaMini-Phone'] : '';
  64. $base .= isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
  65. $base .= isset($_SERVER['HTTP_ACCEPT']) ? strtolower($_SERVER['HTTP_ACCEPT']) : '';
  66. $mobile = check_lib("mobile-strings", $base);
  67. $_SESSION["vortice-mobile"] = $mobile;
  68. }
  69. return $mobile;
  70. }
  71. /**
  72. * Check if is a Search Engine bot request
  73. * @return boolean
  74. */
  75. function is_bot(){
  76. $tmp = '66.249.65.39' . isset($_SERVER['HTTP_USER_AGENT']) ? strtolower($_SERVER['HTTP_USER_AGENT']) : '';
  77. return check_lib("bot-strings", $tmp);
  78. }
  79. /**
  80. * get a posted value
  81. * @param $v variable name
  82. * @return string
  83. */
  84. function p($v){
  85. global $_POST, $_PAR;
  86. return str_replace(
  87. array("\\\"", "\\'"),
  88. array("\"", "'"),
  89. isset($_POST[$v]) ? $_POST[$v] : (isset($_PAR[$v]) ? $_PAR[$v] : ''));
  90. }
  91. /**
  92. * initalize and get URL parts
  93. * @param $i url part index
  94. * @return string
  95. */
  96. function u($i){
  97. if (!defined("uri_parts")){
  98. preg_match_all("@([^/]+):([^/]+)|([^/]+)@", uri, $mat, PREG_SET_ORDER);
  99. $u = array();
  100. foreach ($mat as $k => $v){
  101. $u[$k] = $v[0];
  102. if (count($v) == 3) $u[$v[1]] = $v[2];
  103. }
  104. define ("uri_parts", serialize($u));
  105. }else
  106. $u = unserialize(uri_parts);
  107. return isset($u[$i]) ? $u[$i] : '';
  108. }
  109. /**
  110. * Redirect the response
  111. * @param $destino Destination url encoded with Link class
  112. * @param $delay Delay
  113. * @return void
  114. */
  115. function redirect($destino="", $delay=0){
  116. if (ajax){
  117. $json = Json::getInstance();
  118. $json->addPackage("redirect", urlencode($destino));
  119. exit($json->render());
  120. }else
  121. exit("<html><head><meta http-equiv=\"refresh\" content=\"$delay;URL=$destino\"></head><body></body></html>");
  122. }
  123. /**
  124. * Reset a vector keys
  125. * @param $arr Array
  126. * @return array
  127. */
  128. function reset_keys(&$arr){
  129. $new = array();
  130. foreach ($arr as $i) $new[] = $i;
  131. return $new;
  132. }
  133. /**
  134. * Convert sala-de-imprensa to SalaDeImprensa
  135. * @param $str string
  136. * @return string
  137. */
  138. function camelize($str='') {
  139. return str_replace(' ', '', ucwords(str_replace(array('_', '-'), ' ', $str)));
  140. }
  141. /**
  142. * Convert SalaDeImprensa to sala_de_imprensa
  143. * @param $str string
  144. * @return string
  145. */
  146. function uncamelize($str=''){
  147. return preg_replace('@^_+|_+$@', '', strtolower(preg_replace("/([A-Z])/", "_$1", $str)));
  148. }
  149. /**
  150. * Convert the first char to lower case
  151. * @param $str string
  152. * @return string
  153. */
  154. if (!function_exists("lcfirst")){
  155. function lcfirst($str="") {
  156. if ($str=='') return '';
  157. $str{0} = strtolower($str{0});
  158. return $str;
  159. }
  160. }
  161. /**
  162. * Get current microtime
  163. * @return float
  164. */
  165. function microtime_float()
  166. {
  167. list($usec, $sec) = explode(" ", microtime());
  168. return ((float)$usec + (float)$sec);
  169. }
  170. function d($v){
  171. die(print_r($v));
  172. }
  173. /**
  174. * Translate a phrase using I18n class
  175. * @param mixed
  176. * @return string
  177. */
  178. function e(){
  179. return I18n::e(func_get_args());
  180. }