PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/core/functions.php

https://bitbucket.org/vaneves/repono
PHP | 277 lines | 177 code | 21 blank | 79 comment | 32 complexity | 3142b8177e5ecf575273c77a9f1cbee9 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2011-2012, Valdirene da Cruz Neves Júnior <linkinsystem666@gmail.com>
  4. * All rights reserved.
  5. */
  6. /**
  7. * Define algumas funções que serão utilizadas pelo framework
  8. */
  9. if(!function_exists('e'))
  10. {
  11. /**
  12. * Imprime um conteúdo
  13. * @param string $string valor a ser impresso
  14. */
  15. function e($string)
  16. {
  17. echo $string;
  18. }
  19. }
  20. /**
  21. * Executa a função print_r com a tag <pre>
  22. * @param mixed $struct estrutura a ser impressa
  23. * @return void
  24. */
  25. function pr($struct)
  26. {
  27. echo '<pre>';
  28. print_r($struct);
  29. echo '</pre>';
  30. }
  31. /**
  32. * Executa a função print_r com a tag <pre>
  33. * @param mixed $struct estrutura a ser impressa
  34. * @return void
  35. */
  36. function pre($struct)
  37. {
  38. echo '<pre>';
  39. print_r($struct);
  40. echo '</pre>';
  41. }
  42. /**
  43. * Cria e retorna o caractere de tabulação
  44. * @param int $n quantidade de vezes que desejar dar tabulação
  45. * @return string retorna a tabulação
  46. */
  47. function tab($n = 1)
  48. {
  49. return str_repeat("\t", $n);
  50. }
  51. /**
  52. * Cria e retorna espeçacos em branco
  53. * @param int $n quantidade de espaços que deseja criar
  54. * @return string retorna os espaços
  55. */
  56. function t($n = 1)
  57. {
  58. return str_repeat('&nbsp;', ($n * 5));
  59. }
  60. /**
  61. * Cria uma instância de stdClass com a propriedade 'd', que recebe o valor informado no parâmetro
  62. * @param object $object objeto que será valor da propridade 'd'
  63. * @return stdClass retorna uma instância de stdClass
  64. */
  65. function d($object)
  66. {
  67. $d = new stdClass;
  68. $d->d = $object;
  69. return $d;
  70. }
  71. /**
  72. * Converte um objeto ou um array em uma string XML
  73. * @param mixed $data dados a serem convertidos em XML
  74. * @return string retorna uma string XML
  75. */
  76. function xml_encode($data)
  77. {
  78. if (!is_array($data) && !is_object($data))
  79. return $data;
  80. $encoded = "\n";
  81. foreach($data as $k => $d)
  82. {
  83. $e = is_string($k) ? $k : 'n';
  84. $encoded .= "\t<". $e .">". xml_encode($d) ."</". $e .">\n";
  85. }
  86. return $encoded . "";
  87. }
  88. /**
  89. * Codifica os valores de um array ou um objeto em UTF-8
  90. * @param mixed $data dados a serem convertidos
  91. * @return mixed retorna o array ou objeto convertido
  92. */
  93. function utf8encode($data)
  94. {
  95. if(is_string($data))
  96. return utf8_encode($data);
  97. if (is_array($data))
  98. {
  99. $encoded = array();
  100. foreach($data as $k => $d)
  101. $encoded[$k] = utf8encode($d);
  102. return $encoded;
  103. }
  104. if (is_object($data))
  105. {
  106. $encoded = new stdClass;
  107. foreach($data as $k => $d)
  108. $encoded->{$k} = utf8encode($d);
  109. return $encoded;
  110. }
  111. return $data;
  112. }
  113. /**
  114. * Decodifica os valores de um array ou objeto de UTF-8
  115. * @param mixed $data dados a serem decodificados
  116. * @return mixed retorna um objeto ou array sem a codificação UTF-8
  117. */
  118. function utf8decode($data)
  119. {
  120. if(is_string($data))
  121. return utf8_decode($data);
  122. if (is_array($data))
  123. {
  124. $encoded = array();
  125. foreach($data as $k => $d)
  126. $encoded[$k] = utf8decode($d);
  127. return $encoded;
  128. }
  129. if(is_object($data))
  130. {
  131. $encoded = new stdClass;
  132. foreach($data as $k => $d)
  133. $encoded->{$k} = utf8decode($d);
  134. return $encoded;
  135. }
  136. return $data;
  137. }
  138. /**
  139. * Une dois ou mais array
  140. * @param array $array1 primeiro array
  141. * @param array $array2 segundo array
  142. * @param array $arrayN enéssimo array
  143. * @return array retorna um array com união dos demais
  144. */
  145. function array_union()
  146. {
  147. $args = func_get_args();
  148. $new_array = array();
  149. foreach($args as $array)
  150. {
  151. foreach($array as $element)
  152. $new_array[] = $element;
  153. }
  154. return $new_array;
  155. }
  156. /**
  157. * Cria um indentificado único
  158. * @return string retorna o GUID gerado
  159. */
  160. function guid()
  161. {
  162. if (function_exists('com_create_guid') === true)
  163. return trim(com_create_guid(), '{}');
  164. return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
  165. }
  166. /**
  167. * Gera uma senha
  168. * @param int $length tamanho da senha
  169. * @param int $strength nível se segurança da senha, os valores podem ser 1, 2, 4 e 8, quanto maior, mais segura
  170. * @return string retorna a senha gerada
  171. */
  172. function new_passwd($length = 8, $strength = 0)
  173. {
  174. $vowels = 'aeiou';
  175. $consonants = 'bcdfghjklmnpqrstvwxyz';
  176. if ($strength & 1)
  177. $consonants .= 'BCDFGHJKLMNPQRSTVWXYZ';
  178. if ($strength & 2)
  179. $vowels .= 'AEIOU';
  180. if ($strength & 4)
  181. $consonants .= '123456789';
  182. if ($strength & 8)
  183. $consonants .= '@#$%';
  184. $password = '';
  185. $alt = time() % 2;
  186. for ($i = 0; $i < $length; $i++)
  187. {
  188. if ($alt == 1)
  189. {
  190. $password .= $consonants[(rand() % strlen($consonants))];
  191. $alt = 0;
  192. }
  193. else
  194. {
  195. $password .= $vowels[(rand() % strlen($vowels))];
  196. $alt = 1;
  197. }
  198. }
  199. return $password;
  200. }
  201. if (!function_exists('get_called_class'))
  202. {
  203. /**
  204. * http://djomla.blog.com/2011/02/16/php-versions-5-2-and-5-3-get_called_class/
  205. */
  206. function get_called_class($bt = false, $l = 1)
  207. {
  208. if (!$bt)
  209. $bt = debug_backtrace();
  210. if (!isset($bt[$l]))
  211. throw new Exception("Cannot find called class -> stack level too deep.");
  212. if (!isset($bt[$l]['type']))
  213. {
  214. throw new Exception('type not set');
  215. }
  216. else
  217. {
  218. if($bt[$l]['type'] == '::')
  219. {
  220. $lines = file($bt[$l]['file']);
  221. $i = 0;
  222. $callerLine = '';
  223. do
  224. {
  225. $i++;
  226. $callerLine = $lines[$bt[$l]['line'] - $i] . $callerLine;
  227. } while (stripos($callerLine, $bt[$l]['function']) === false);
  228. preg_match('/([a-zA-Z0-9\_]+)::' . $bt[$l]['function'] . '/', $callerLine, $matches);
  229. if (!isset($matches[1])) // must be an edge case.
  230. throw new Exception("Could not find caller class: originating method call is obscured.");
  231. if($matches[1] == 'self' || $matches[1] == 'parent' )
  232. return get_called_class($bt, $l + 1);
  233. else
  234. return $matches[1];
  235. }
  236. elseif($bt[$l]['type'] == '->') // won't get here.
  237. {
  238. //if($bt[$l]['function'] == '__get')
  239. //{
  240. // edge case -> get class of calling object
  241. if (!is_object($bt[$l]['object']))
  242. return $bt[$l]['class'];
  243. return get_class($bt[$l]['object']);
  244. /*}
  245. else
  246. {
  247. return $bt[$l]['class'];
  248. }*/
  249. }
  250. else
  251. {
  252. throw new Exception("Unknown backtrace method type");
  253. }
  254. }
  255. }
  256. }