Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Utility.class.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. class Utility {
  3. private $user;
  4. function __construct() {
  5. global $user;
  6. $this->user = $user;
  7. }
  8. public function setHash($destination='') {
  9. return '#/'.$destination;
  10. }
  11. public function validateEmail($email='') {
  12. return filter_var($email, FILTER_VALIDATE_EMAIL);
  13. }
  14. public function setDateToIsoFormat($string='', $separator='/') {
  15. $iso = null;
  16. if (strpos($string, $separator) !== false) {
  17. $tmp = explode($separator, $string);
  18. if (is_array($tmp) && !empty($tmp)) {
  19. $y = trim($tmp[2]);
  20. $m = trim($tmp[1]);
  21. $d = trim($tmp[0]);
  22. if (checkdate((int)$m, (int)$d, (int)$y)) {
  23. $iso = $y.'-'.$m.'-'.$d;
  24. }
  25. }
  26. }
  27. return $iso;
  28. }
  29. public function setDateToItalianFormat($string='', $separator='-') {
  30. $date = null;
  31. if (strpos($string, $separator) !== false) {
  32. $tmp = explode($separator, $string);
  33. if (is_array($tmp) && !empty($tmp)) {
  34. $y = trim($tmp[0]);
  35. $m = trim($tmp[1]);
  36. $d = trim($tmp[2]);
  37. if (checkdate((int)$m, (int)$d, (int)$y)) {
  38. $date = $d.'/'.$m.'/'.$y;
  39. }
  40. }
  41. }
  42. return $date;
  43. }
  44. public function isValidDate($date='', $format='Y-m-d') {
  45. $d = DateTime::createFromFormat($format, $date);
  46. return $d && $d->format($format) === $date;
  47. }
  48. public function formatNumber($float=0, $decimals=0) {
  49. $lang = $this->user->getUserLang();
  50. if ($lang == 'en') {
  51. $format = number_format($float, $decimals, '.', ',');
  52. } else {
  53. $format = number_format($float, $decimals, ',', '.');
  54. }
  55. return $format;
  56. }
  57. public function isDateInThePast($iso_date) {
  58. $date = new DateTime($iso_date);
  59. $now = new DateTime();
  60. if ($date < $now) {
  61. return true;
  62. }
  63. return false;
  64. }
  65. public function getAge($date='') {
  66. $from = new DateTime($date);
  67. $to = new DateTime('today');
  68. return $from->diff($to)->y;
  69. }
  70. public function orderArray($array=[], $key='', $dir='asc', $type='regular') {
  71. $tmp = [];
  72. $ordered = [];
  73. $returnArray = [];
  74. $sortType = SORT_REGULAR;
  75. if (is_array($array) && !empty($array)) {
  76. foreach($array as $item) {
  77. $ordered[$item[$key]] = $item;
  78. }
  79. switch($type) {
  80. case 'numeric':
  81. $sortType = SORT_NUMERIC;
  82. break;
  83. case 'string':
  84. $sortType = SORT_STRING;
  85. break;
  86. }
  87. if ($dir == 'asc') {
  88. ksort($ordered, $sortType);
  89. } else {
  90. krsort($ordered, $sortType);
  91. }
  92. }
  93. if (!empty($ordered)) {
  94. foreach($ordered as $item) {
  95. $returnArray[] = $item;
  96. }
  97. }
  98. return $returnArray;
  99. }
  100. public function slugify($text='') {
  101. // replace non letter or digits by -
  102. $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  103. // transliterate
  104. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  105. // remove unwanted characters
  106. $text = preg_replace('~[^-\w]+~', '', $text);
  107. // trim
  108. $text = trim($text, '-');
  109. // remove duplicate -
  110. $text = preg_replace('~-+~', '-', $text);
  111. // lowercase
  112. //$text = strtolower($text);
  113. if (empty($text)) {
  114. return 'n-a';
  115. }
  116. return $text;
  117. }
  118. public function deepTrim($element){
  119. if(!is_array($element)){
  120. if(!is_bool($element)){
  121. if(is_null($element)) return $element;
  122. if(is_int($element)) return (int)trim($element);
  123. if(is_float($element)) return (float)trim($element);
  124. return trim($element);
  125. } else { // is_bool
  126. // no-op
  127. }
  128. } else { // is_array
  129. foreach($element as $key => $value){
  130. $element[$key] = $this->deepTrim($value);
  131. }
  132. }
  133. return $element;
  134. }
  135. public function sureHtml($str, $flag=ENT_QUOTES, $encoding='UTF-8', $allowed_tags=null){
  136. return strip_tags(htmlspecialchars($str, $flag, $encoding), $allowed_tags);
  137. }
  138. public function mime2ext($mime) {
  139. $mime_map = [
  140. 'video/3gpp2' => '3g2',
  141. 'video/3gp' => '3gp',
  142. 'video/3gpp' => '3gp',
  143. 'application/x-compressed' => '7zip',
  144. 'audio/x-acc' => 'aac',
  145. 'audio/ac3' => 'ac3',
  146. 'application/postscript' => 'ai',
  147. 'audio/x-aiff' => 'aif',
  148. 'audio/aiff' => 'aif',
  149. 'audio/x-au' => 'au',
  150. 'video/x-msvideo' => 'avi',
  151. 'video/msvideo' => 'avi',
  152. 'video/avi' => 'avi',
  153. 'application/x-troff-msvideo' => 'avi',
  154. 'application/macbinary' => 'bin',
  155. 'application/mac-binary' => 'bin',
  156. 'application/x-binary' => 'bin',
  157. 'application/x-macbinary' => 'bin',
  158. 'image/bmp' => 'bmp',
  159. 'image/x-bmp' => 'bmp',
  160. 'image/x-bitmap' => 'bmp',
  161. 'image/x-xbitmap' => 'bmp',
  162. 'image/x-win-bitmap' => 'bmp',
  163. 'image/x-windows-bmp' => 'bmp',
  164. 'image/ms-bmp' => 'bmp',
  165. 'image/x-ms-bmp' => 'bmp',
  166. 'application/bmp' => 'bmp',
  167. 'application/x-bmp' => 'bmp',
  168. 'application/x-win-bitmap' => 'bmp',
  169. 'application/cdr' => 'cdr',
  170. 'application/coreldraw' => 'cdr',
  171. 'application/x-cdr' => 'cdr',
  172. 'application/x-coreldraw' => 'cdr',
  173. 'image/cdr' => 'cdr',
  174. 'image/x-cdr' => 'cdr',
  175. 'zz-application/zz-winassoc-cdr' => 'cdr',
  176. 'application/mac-compactpro' => 'cpt',
  177. 'application/pkix-crl' => 'crl',
  178. 'application/pkcs-crl' => 'crl',
  179. 'application/x-x509-ca-cert' => 'crt',
  180. 'application/pkix-cert' => 'crt',
  181. 'text/css' => 'css',
  182. 'text/x-comma-separated-values' => 'csv',
  183. 'text/comma-separated-values' => 'csv',
  184. 'application/vnd.msexcel' => 'csv',
  185. 'application/x-director' => 'dcr',
  186. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
  187. 'application/x-dvi' => 'dvi',
  188. 'message/rfc822' => 'eml',
  189. 'application/x-msdownload' => 'exe',
  190. 'video/x-f4v' => 'f4v',
  191. 'audio/x-flac' => 'flac',
  192. 'video/x-flv' => 'flv',
  193. 'image/gif' => 'gif',
  194. 'application/gpg-keys' => 'gpg',
  195. 'application/x-gtar' => 'gtar',
  196. 'application/x-gzip' => 'gzip',
  197. 'application/mac-binhex40' => 'hqx',
  198. 'application/mac-binhex' => 'hqx',
  199. 'application/x-binhex40' => 'hqx',
  200. 'application/x-mac-binhex40' => 'hqx',
  201. 'text/html' => 'html',
  202. 'image/x-icon' => 'ico',
  203. 'image/x-ico' => 'ico',
  204. 'image/vnd.microsoft.icon' => 'ico',
  205. 'text/calendar' => 'ics',
  206. 'application/java-archive' => 'jar',
  207. 'application/x-java-application' => 'jar',
  208. 'application/x-jar' => 'jar',
  209. 'image/jp2' => 'jp2',
  210. 'video/mj2' => 'jp2',
  211. 'image/jpx' => 'jp2',
  212. 'image/jpm' => 'jp2',
  213. 'image/jpeg' => 'jpeg',
  214. 'image/pjpeg' => 'jpeg',
  215. 'application/x-javascript' => 'js',
  216. 'application/json' => 'json',
  217. 'text/json' => 'json',
  218. 'application/vnd.google-earth.kml+xml' => 'kml',
  219. 'application/vnd.google-earth.kmz' => 'kmz',
  220. 'text/x-log' => 'log',
  221. 'audio/x-m4a' => 'm4a',
  222. 'application/vnd.mpegurl' => 'm4u',
  223. 'audio/midi' => 'mid',
  224. 'application/vnd.mif' => 'mif',
  225. 'video/quicktime' => 'mov',
  226. 'video/x-sgi-movie' => 'movie',
  227. 'audio/mpeg' => 'mp3',
  228. 'audio/mpg' => 'mp3',
  229. 'audio/mpeg3' => 'mp3',
  230. 'audio/mp3' => 'mp3',
  231. 'video/mp4' => 'mp4',
  232. 'video/mpeg' => 'mpeg',
  233. 'application/oda' => 'oda',
  234. 'audio/ogg' => 'ogg',
  235. 'video/ogg' => 'ogg',
  236. 'application/ogg' => 'ogg',
  237. 'application/x-pkcs10' => 'p10',
  238. 'application/pkcs10' => 'p10',
  239. 'application/x-pkcs12' => 'p12',
  240. 'application/x-pkcs7-signature' => 'p7a',
  241. 'application/pkcs7-mime' => 'p7c',
  242. 'application/x-pkcs7-mime' => 'p7c',
  243. 'application/x-pkcs7-certreqresp' => 'p7r',
  244. 'application/pkcs7-signature' => 'p7s',
  245. 'application/pdf' => 'pdf',
  246. 'application/octet-stream' => 'pdf',
  247. 'application/x-x509-user-cert' => 'pem',
  248. 'application/x-pem-file' => 'pem',
  249. 'application/pgp' => 'pgp',
  250. 'application/x-httpd-php' => 'php',
  251. 'application/php' => 'php',
  252. 'application/x-php' => 'php',
  253. 'text/php' => 'php',
  254. 'text/x-php' => 'php',
  255. 'application/x-httpd-php-source' => 'php',
  256. 'image/png' => 'png',
  257. 'image/x-png' => 'png',
  258. 'application/powerpoint' => 'ppt',
  259. 'application/vnd.ms-powerpoint' => 'ppt',
  260. 'application/vnd.ms-office' => 'ppt',
  261. 'application/msword' => 'doc',
  262. 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
  263. 'application/x-photoshop' => 'psd',
  264. 'image/vnd.adobe.photoshop' => 'psd',
  265. 'audio/x-realaudio' => 'ra',
  266. 'audio/x-pn-realaudio' => 'ram',
  267. 'application/x-rar' => 'rar',
  268. 'application/rar' => 'rar',
  269. 'application/x-rar-compressed' => 'rar',
  270. 'audio/x-pn-realaudio-plugin' => 'rpm',
  271. 'application/x-pkcs7' => 'rsa',
  272. 'text/rtf' => 'rtf',
  273. 'text/richtext' => 'rtx',
  274. 'video/vnd.rn-realvideo' => 'rv',
  275. 'application/x-stuffit' => 'sit',
  276. 'application/smil' => 'smil',
  277. 'text/srt' => 'srt',
  278. 'image/svg+xml' => 'svg',
  279. 'application/x-shockwave-flash' => 'swf',
  280. 'application/x-tar' => 'tar',
  281. 'application/x-gzip-compressed' => 'tgz',
  282. 'image/tiff' => 'tiff',
  283. 'text/plain' => 'txt',
  284. 'text/x-vcard' => 'vcf',
  285. 'application/videolan' => 'vlc',
  286. 'text/vtt' => 'vtt',
  287. 'audio/x-wav' => 'wav',
  288. 'audio/wave' => 'wav',
  289. 'audio/wav' => 'wav',
  290. 'application/wbxml' => 'wbxml',
  291. 'video/webm' => 'webm',
  292. 'audio/x-ms-wma' => 'wma',
  293. 'application/wmlc' => 'wmlc',
  294. 'video/x-ms-wmv' => 'wmv',
  295. 'video/x-ms-asf' => 'wmv',
  296. 'application/xhtml+xml' => 'xhtml',
  297. 'application/excel' => 'xl',
  298. 'application/msexcel' => 'xls',
  299. 'application/x-msexcel' => 'xls',
  300. 'application/x-ms-excel' => 'xls',
  301. 'application/x-excel' => 'xls',
  302. 'application/x-dos_ms_excel' => 'xls',
  303. 'application/xls' => 'xls',
  304. 'application/x-xls' => 'xls',
  305. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
  306. 'application/vnd.ms-excel' => 'xlsx',
  307. 'application/xml' => 'xml',
  308. 'text/xml' => 'xml',
  309. 'text/xsl' => 'xsl',
  310. 'application/xspf+xml' => 'xspf',
  311. 'application/x-compress' => 'z',
  312. 'application/x-zip' => 'zip',
  313. 'application/zip' => 'zip',
  314. 'application/x-zip-compressed' => 'zip',
  315. 'application/s-compressed' => 'zip',
  316. 'multipart/x-zip' => 'zip',
  317. 'text/x-scriptzsh' => 'zsh',
  318. ];
  319. return isset($mime_map[$mime]) === true ? $mime_map[$mime] : false;
  320. }
  321. public function splitWords($string, $limit=2) {
  322. $tmp = explode(' ', $string);
  323. $words = [];
  324. if (is_array($tmp)) {
  325. foreach($tmp as $word) {
  326. $word = trim($word);
  327. if (strlen($word) > $limit) {
  328. if (strpos($word, "'") !== false) {
  329. $pos = strpos($word, "'");
  330. $word = substr($word, $pos+1);
  331. }
  332. if (strlen($word) > $limit)
  333. $words[] = trim($word);
  334. }
  335. }
  336. }
  337. return $words;
  338. }
  339. }