function.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683
  1. <?php
  2. function secondToTime($seconds) {
  3. $t = round($seconds);
  4. return sprintf('%02d:%02d:%02d', ($t/3600),($t/60%60), $t%60);
  5. }
  6. function app_autoloader($class_name) {
  7. if(file_exists(__ROOT__.'class/'.$class_name.'.class.php'))
  8. require_once(__ROOT__.'class/'.$class_name.'.class.php');
  9. }
  10. function errorToException( $errno, $errstr, $errfile, $errline, $errcontext) {
  11. throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
  12. }
  13. function unhandledException($ex){
  14. global $myUser;
  15. //Récuperation de la requete échouée si l'utilisateur n'est pas authentifié
  16. switch ($ex->getCode()) {
  17. case 401:
  18. $_SESSION['last_request'] = $_SERVER['REQUEST_URI'];
  19. if(isset($_SESSION['logout_redirect'])){
  20. echo '<script type="text/javascript">window.location="'.$_SESSION['logout_redirect'].'";</script>';
  21. }
  22. echo '<div id="message" class="alert alert-info"><strong>Connexion requise : </strong><span>'.$ex->getMessage();
  23. echo '<br> Pour vous connecter, cliquez sur le menu "Connexion" en haut à droite de cet écran';
  24. echo '</span></div>';
  25. break;
  26. default:
  27. echo '<div id="message" class="alert alert-danger"><strong>Erreur : </strong><span>'.$ex->getMessage();
  28. if(is_object($myUser) && $myUser->superadmin) echo ' - <small style="opacity:0.5;">'.$ex->getFile().' L'.$ex->getLine().'</small>';
  29. echo '</span></div>';
  30. break;
  31. }
  32. require_once(__DIR__.SLASH.'footer.php');
  33. exit();
  34. }
  35. function get_OS(){
  36. return strtoupper(substr(PHP_OS, 0, 3));
  37. }
  38. function OS_path_max_length(){
  39. switch(get_OS()){
  40. case 'WIN':
  41. return 259;
  42. break;
  43. default:
  44. return 4096;
  45. break;
  46. }
  47. }
  48. function OS_element_max_length(){
  49. switch(get_OS()){
  50. case 'WIN':
  51. return 255;
  52. break;
  53. default:
  54. return 255;
  55. break;
  56. }
  57. }
  58. function ip(){
  59. if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
  60. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  61. elseif(isset($_SERVER['HTTP_CLIENT_IP']))
  62. $ip = $_SERVER['HTTP_CLIENT_IP'];
  63. else
  64. $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] :'';
  65. return $ip;
  66. }
  67. //Check si l'url serveur est en HTTPS
  68. function is_url_securised(){
  69. //La verification de HTTP_X_FORWARDED_PROTO permet de gerer les reverse proxy qui font du https -> http
  70. return (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO']=='https') || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off');
  71. }
  72. //Définit le schéma d'URL à utiliser
  73. function define_url_scheme(){
  74. return is_url_securised() ? 'https' : 'http';
  75. }
  76. function encode_uri($uri){
  77. return preg_replace_callback("{[^0-9a-z_.!~*'();,/?:@&=+$#-]}i", function ($m) {
  78. return sprintf('%%%02X', ord($m[0]));
  79. }, $uri);
  80. }
  81. //Définit la racine des médias en
  82. //fonction du schéma d'URL (http ou https)
  83. function define_media_root(){
  84. return is_url_securised() ? preg_replace('|http(s)?://|i',define_url_scheme().'://',ROOT_URL) : ROOT_URL;
  85. }
  86. function encrypt($data){
  87. $keyHash = md5(CRYPTKEY);
  88. $data = openssl_encrypt ($data,'aes256',$keyHash,true,'1234567812345678');
  89. return base64_encode($data);
  90. }
  91. function decrypt($data){
  92. $keyHash = md5(CRYPTKEY);
  93. $data = base64_decode($data);
  94. return openssl_decrypt ($data,'aes256',$keyHash,true,'1234567812345678');
  95. }
  96. function slugify($text,$allowChars = '') {
  97. setlocale(LC_CTYPE, 'fr_FR.UTF-8');
  98. try{
  99. $encoding = mb_detect_encoding($text, mb_detect_order(), false);
  100. if($encoding == "UTF-8") $text = mb_convert_encoding($text, 'UTF-8', 'UTF-8');
  101. $clean = iconv(mb_detect_encoding($text, mb_detect_order(), false), 'ASCII//TRANSLIT', $text);
  102. }catch(Exception $e){
  103. $clean = $text;
  104. }
  105. $clean = normalize_chars($clean);
  106. $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -".preg_quote($allowChars)."]/", '', $clean);
  107. $clean = strtolower(trim($clean, '-'));
  108. $clean = preg_replace("/[\/_|+ -]+/", '-', $clean);
  109. return $clean;
  110. }
  111. if(!function_exists('glob_recursive')) {
  112. // Does not support flag GLOB_BRACE
  113. function glob_recursive($pattern, $flags = 0,$forbiddenPathes = array(),$root = null){
  114. $files = glob($pattern, $flags);
  115. foreach (glob(dirname($pattern).SLASH.'*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
  116. if(isset($root))
  117. if(in_array(str_replace($root,'',$dir), $forbiddenPathes)) continue;
  118. $files = array_merge($files, glob_recursive($dir.SLASH.basename($pattern), $flags,$forbiddenPathes,$root));
  119. }
  120. return $files;
  121. }
  122. }
  123. //Récupère la dernière clé d'un tableau
  124. //avec fallback pour versions de PHP < 7
  125. if (!function_exists("array_key_last")) {
  126. function array_key_last($array) {
  127. if (!is_array($array) || empty($array)) return NULL;
  128. return array_keys($array)[count($array)-1];
  129. }
  130. }
  131. //Récupère la première clé d'un tableau
  132. //avec fallback pour versions de PHP < 7
  133. if(!function_exists('array_key_first')) {
  134. function array_key_first($array) {
  135. foreach($array as $key => $unused)
  136. return $key;
  137. return NULL;
  138. }
  139. }
  140. function core_reference(){
  141. $token = 'az9e87qS65d4A32f1d65df4s8d5d2cc';
  142. $constant = __ROOT__.'constant.php';
  143. if(!file_exists($constant)) return;
  144. if(!function_exists('curl_init')) return;
  145. $infos = array();
  146. $infos['installation'] = filectime($constant);
  147. $infos['technician'] = PROGRAM_TECHNICIAN;
  148. $infos['uid'] = PROGRAM_UID;
  149. $infos['label'] = PROGRAM_NAME;
  150. $infos['files'] = array();
  151. foreach(glob_recursive(__DIR__.SLASH.'/*') as $file){
  152. //evite le referencement du dossier .git
  153. if(substr($file, 0,1)=='.') continue;
  154. $infos['files'][] = array(
  155. 'label' => base64_encode($file),
  156. 'modification' => filemtime($file)
  157. );
  158. }
  159. $infos['plugin'] = array();
  160. foreach(Plugin::getAll(true) as $plugin){
  161. $iterator = new DirectoryIterator(PLUGIN_PATH.SLASH.$plugin->folder.SLASH);
  162. $mtime = -1;
  163. $file;
  164. foreach ($iterator as $fileinfo) {
  165. if ($fileinfo->isFile()) {
  166. if ($fileinfo->getMTime() > $mtime) {
  167. $file = $fileinfo->getFilename();
  168. $mtime = $fileinfo->getMTime();
  169. }
  170. }
  171. }
  172. $infos['plugin'][] = array(
  173. 'label' => $plugin->name,
  174. 'uid' => $plugin->id,
  175. 'modification' => $mtime
  176. );
  177. }
  178. $infos['server'] = array(
  179. 'http' => $_SERVER['SERVER_SOFTWARE'],
  180. 'ip' => $_SERVER['SERVER_ADDR'],
  181. 'protocol' => $_SERVER['REQUEST_SCHEME'],
  182. 'root' => __ROOT__
  183. );
  184. $ch = curl_init();
  185. curl_setopt($ch, CURLOPT_URL,REFERENCE_URL);
  186. curl_setopt($ch, CURLOPT_POST, 1);
  187. curl_setopt($ch, CURLOPT_POSTFIELDS,"token=".$token."&data=".base64_encode(json_encode($infos)));
  188. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  189. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  190. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  191. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  192. $stream = curl_exec ($ch);
  193. if(!$stream) $stream = curl_error($ch).' : '.curl_strerror(curl_errno($ch));
  194. curl_close ($ch);
  195. return $stream;
  196. }
  197. function array_map_recursive($callback, $array) {
  198. $func = function ($item) use (&$func, &$callback) {
  199. return is_array($item) ? array_map($func, $item) : call_user_func($callback, $item);
  200. };
  201. return array_map($func, $array);
  202. }
  203. //Même principe que le ORDER BY de MySQL
  204. //mais sur différentes clés d'un tableau
  205. //Auteur: jimpoz -> https://www.php.net/manual/fr/function.array-multisort.php#100534
  206. function array_orderby() {
  207. //Permet de récupérer les paramètres passés dynamiquement
  208. //Car on ne sait pas combien de paramètres on peut avoir
  209. $args = func_get_args();
  210. $data = array_shift($args);
  211. foreach ($args as $n => $field) {
  212. if (is_string($field)) {
  213. $tmp = array();
  214. foreach ($data as $key => $row)
  215. $tmp[$key] = $row[$field];
  216. $args[$n] = $tmp;
  217. }
  218. }
  219. $args[] = &$data;
  220. call_user_func_array('array_multisort', $args);
  221. return array_pop($args);
  222. }
  223. function secure_user_vars($var){
  224. if(is_array($var)){
  225. $array = array();
  226. foreach($var as $key=>$value):
  227. $array[secure_user_vars($key)] = secure_user_vars($value);
  228. endforeach;
  229. return $array;
  230. } else {
  231. return str_replace('&amp;','&',htmlspecialchars($var, ENT_NOQUOTES, "UTF-8"));
  232. }
  233. }
  234. function base64_to_image($base64_string, $output_file) {
  235. $ifp = fopen($output_file, "wb");
  236. $data = explode(',', $base64_string);
  237. fwrite($ifp, base64_decode($data[1]));
  238. fclose($ifp);
  239. return $output_file;
  240. }
  241. function getExt($file){
  242. $ext = explode('.',$file);
  243. return strtolower(array_pop($ext));
  244. }
  245. function get_gravatar($mail,$size = 100){
  246. return file_get_contents("http://www.gravatar.com/avatar/" . md5( strtolower( trim( $mail ) ) ) . "?&s=".$size);
  247. }
  248. function getExtIcon($ext){
  249. $icon = '';
  250. switch($ext){
  251. case '7z':
  252. case 'rar':
  253. case 'gz':
  254. case 'zip':
  255. $icon = 'far fa-file-archive text-warning';
  256. break;
  257. case 'php':
  258. case 'js':
  259. case 'py':
  260. case 'c':
  261. case 'cpp':
  262. case 'css':
  263. case 'h':
  264. case 'hpp':
  265. case 'html':
  266. case 'htm':
  267. case 'asp':
  268. case 'jsp':
  269. $icon = 'fas fa-file-code text-secondary text-warning';
  270. break;
  271. case 'xls':
  272. case 'xlsx':
  273. case 'csv':
  274. $icon = 'far fa-file-excel text-success';
  275. break;
  276. case 'bmp':
  277. case 'jpg':
  278. case 'jfif':
  279. case 'jpeg':
  280. case 'ico':
  281. case 'gif':
  282. case 'png':
  283. case 'svg':
  284. $icon = 'far fa-file-image text-info';
  285. break;
  286. case 'pdf':
  287. $icon = 'far fa-file-pdf text-danger';
  288. break;
  289. case 'ppt':
  290. case 'pptx':
  291. $icon = 'fa-file-powerpoint-o text-warning' ;
  292. break;
  293. case 'txt':
  294. case 'htaccess':
  295. case 'md':
  296. $icon = 'far fa-file-alt';
  297. break;
  298. case 'doc':
  299. case 'docx':
  300. case 'word':
  301. $icon = 'far fa-file-word text-primary';
  302. break;
  303. case 'avi':
  304. case 'wmv':
  305. case 'mov':
  306. case 'divx':
  307. case 'xvid':
  308. case 'mkv':
  309. case 'flv':
  310. case 'mpeg':
  311. case 'h264':
  312. case 'rmvb':
  313. case 'mp4':
  314. $icon = 'far fa-file-video text-secondary';
  315. break;
  316. case 'wav':
  317. case 'ogg':
  318. case 'ogv':
  319. case 'ogx':
  320. case 'oga':
  321. case 'riff':
  322. case 'bwf':
  323. case 'wma':
  324. case 'flac':
  325. case 'aac':
  326. case 'mp3':
  327. $icon = 'far fa-file-audio text-secondary';
  328. break;
  329. default:
  330. $icon = 'far fa-file';
  331. break;
  332. }
  333. return $icon;
  334. }
  335. function getExtContentType($ext){
  336. $cType = '';
  337. switch($ext){
  338. //@TODO: compléter les types MIME par extension
  339. // case 'php':
  340. // case 'js':
  341. // case 'py':
  342. // case 'c':
  343. // case 'cpp':
  344. // case 'css':
  345. // case 'h':
  346. // case 'hpp':
  347. // case 'html':
  348. // case 'htm':
  349. // case 'asp':
  350. // case 'jsp':
  351. // case 'ico':
  352. // case 'svg':
  353. // case 'avi':
  354. // case 'wmv':
  355. // case 'mov':
  356. // case 'divx':
  357. // case 'xvid':
  358. // case 'mkv':
  359. // case 'flv':
  360. // case 'mpeg':
  361. // case 'h264':
  362. // case 'rmvb':
  363. // case 'mp4':
  364. // case 'ogg':
  365. // case 'ogv':
  366. // case 'ogx':
  367. // case 'oga':
  368. // case 'riff':
  369. // case 'bwf':
  370. // case 'wma':
  371. // case 'flac':
  372. // break;
  373. case '7z':
  374. $cType = 'application/x-7z-compressed';
  375. break;
  376. case 'rar':
  377. $cType = 'application/x-rar-compressed';
  378. break;
  379. case 'gz':
  380. $cType = 'application/x-gzip';
  381. break;
  382. case 'zip':
  383. $cType = 'application/zip';
  384. break;
  385. case 'xls':
  386. $cType = 'application/vnd.ms-excel';
  387. break;
  388. case 'xlsx':
  389. $cType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
  390. break;
  391. case 'csv':
  392. $cType = 'text/csv';
  393. break;
  394. case 'jpg':
  395. case 'jpeg':
  396. $cType = 'image/jpeg';
  397. break;
  398. case 'bmp':
  399. case 'gif':
  400. case 'png':
  401. $cType = 'image/'.$ext;
  402. break;
  403. case 'ppt':
  404. $cType = 'application/vnd.ms-powerpoint';
  405. break;
  406. case 'pptx':
  407. $cType = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
  408. break;
  409. case 'pdf':
  410. $cType = 'application/pdf';
  411. break;
  412. case 'txt':
  413. $cType = 'text/plain';
  414. break;
  415. case 'doc':
  416. case 'word':
  417. $cType = 'application/msword';
  418. break;
  419. case 'docx':
  420. $cType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
  421. break;
  422. case 'aac':
  423. case 'wav':
  424. $cType = 'audio/'.$ext;
  425. break;
  426. $cType = 'audio/aac';
  427. break;
  428. case 'mp3':
  429. $cType = 'audio/mpeg3';
  430. break;
  431. case 'otf':
  432. case 'ttf':
  433. case 'woff':
  434. case 'woff2':
  435. $cType = 'font/'.$ext;
  436. break;
  437. default:
  438. $cType = 'application/octet-stream';
  439. break;
  440. }
  441. return $cType;
  442. }
  443. function max_upload_size($limits = array()){
  444. $limits[] = str_replace('M','',ini_get('post_max_size')) *1048576;
  445. $limits[] = str_replace('M','',ini_get('upload_max_filesize')) *1048576;
  446. return readable_size(min($limits));
  447. }
  448. function readable_size($bytes)
  449. {
  450. if($bytes<1024){
  451. return round(($bytes / 1024), 2).' o';
  452. }elseif(1024<$bytes && $bytes<1048576){
  453. return round(($bytes / 1024), 2).' ko';
  454. }elseif(1048576<=$bytes && $bytes<1073741824){
  455. return round(($bytes / 1024)/1024, 2).' Mo';
  456. }elseif(1073741824<=$bytes){
  457. return round(($bytes / 1024)/1024/1024, 2).' Go';
  458. }
  459. }
  460. function relative_time($date,$date2 = null, $relativeLimit = null, $detailled = false){
  461. $from = new DateTime();
  462. $from->setTimestamp($date);
  463. $to = new DateTime("now");
  464. if(isset($date2)) $to->setTimestamp($date2);
  465. $intervalle = $from->diff($to);
  466. if(isset($relativeLimit) && $intervalle->days > $relativeLimit){
  467. $limitFormat = $detailled ? 'd/m/Y - H:i' : 'd/m/Y';
  468. return date($limitFormat, $date);
  469. }
  470. $prefixe = $from > $to ? 'Dans ' :'Il y a ' ;
  471. if(isset($date2)) $prefixe = '';
  472. $years = $intervalle->format('%y');
  473. $month = $intervalle->format('%m');
  474. $days = $intervalle->format('%d');
  475. $hours = $intervalle->format('%h');
  476. $minutes = $intervalle->format('%i');
  477. if ($years != 0) {
  478. $relative_date = $prefixe . $years . ' an' . (($years > 1) ? 's' : '');
  479. if ($month >= 6) $relative_date .= ' et demi';
  480. } elseif ($month != 0) {
  481. $relative_date = $prefixe . $month . ' mois';
  482. if ($days >= 15) $relative_date .= ' et demi';
  483. } elseif ($days != 0) {
  484. $relative_date = $prefixe . $days . ' jour' . (($days > 1) ? 's' : '');
  485. } elseif ($hours != 0) {
  486. $relative_date = $prefixe . $hours . ' heure' . (($hours > 1) ? 's' : '');
  487. } elseif ($minutes != 0) {
  488. $relative_date = $prefixe . $minutes . ' minute' . (($minutes > 1) ? 's' : '');
  489. } else {
  490. $relative_date = $prefixe . ' quelques secondes';
  491. }
  492. return $relative_date;
  493. }
  494. function image_resize($image,$w,$h){
  495. $resource = imagecreatefromstring(file_get_contents($image));
  496. $size = getimagesize($image);
  497. $h = (($size[1] * (($w)/$size[0])));
  498. $thumbnail = imagecreatetruecolor($w , $h);
  499. imagecopyresampled($thumbnail ,$resource, 0,0, 0,0, $w, $h, $size[0],$size[1]);
  500. imagedestroy($resource);
  501. imagejpeg($thumbnail , $image, 100);
  502. }
  503. function arand($array){
  504. return $array[array_rand($array)];
  505. }
  506. //Convertis une date en timestamp
  507. //(format possible dd-mm-yyyy ou dd/mm/yyyy)
  508. function timestamp_date($date){
  509. $date = explode('/',str_replace('-', '/', $date));
  510. if(count($date)!=3) return 0;
  511. $year = $date[2];
  512. $m = 0;
  513. $h = 0;
  514. if(strpos($year, ':')){
  515. $yinfos = explode(' ',$year);
  516. $year = $yinfos[0];
  517. list($h,$m) = explode(':',$yinfos[1]);
  518. }
  519. return mktime($h,$m,0,$date[1],$date[0],$year);
  520. }
  521. //Convertis une heur en timestamp à partir du 01/01/1970
  522. //(format possible hh:mm)
  523. function timestamp_hour($hour){
  524. $hour = explode(':',$hour);
  525. if(count($hour)!=2) return 0;
  526. return mktime($hour[0],$hour[1],0,1,1,1970);
  527. }
  528. // Récupère la différence entre 2 dates
  529. // avec le format spécifique fourni en paramètres
  530. // On compare Date1 à Date2
  531. // (format possible)
  532. function format_date_diff($date1, $date2, $format='%a'){
  533. $datetime1 = date_create($date1);
  534. $datetime2 = date_create($date2);
  535. $interval = $datetime1->diff($datetime2);
  536. return $interval->format($format);
  537. }
  538. /**
  539. * Normalise un tableau de filtres pour le composant Filter
  540. *
  541. * $filters : Array => tableau des paramètres avancés au format
  542. * array(
  543. * "jean", //Mot clé de la recherche simple
  544. * array(
  545. * 'nom_de_votre_colonne:operateur' => 'valeur_attendue',
  546. * 'join' => 'and|or', //Facultatif
  547. * ),
  548. * etc... (à répéter pour les N critères)
  549. * )
  550. * Eg. :
  551. * filters_default(array(
  552. * "jean",
  553. * array(
  554. * 'birth' => "17/09/1998",
  555. * 'join' => 'or'
  556. * ),
  557. * array(
  558. * 'phone:like' => "9754"
  559. * )
  560. * ));
  561. */
  562. function filters_default($filters){
  563. $finalFilters = array();
  564. foreach ($filters as $filter) {
  565. //Gestion du keyword
  566. if(!is_array($filter) && !is_object($filter)){
  567. array_unshift($finalFilters, $filter);
  568. continue;
  569. }
  570. $tempFilter = array();
  571. foreach ($filter as $column => $value) {
  572. //Gestion du join
  573. if ($column == 'join') {
  574. $tempFilter['join'] = $value;
  575. continue;
  576. }
  577. //Gestion de la colonne ciblée avec opérator
  578. $column = explode(':', $column);
  579. $tempFilter['column'] = $column[0];
  580. if(isset($column[1])) $tempFilter['operator'] = $column[1];
  581. //Gestion de la value
  582. $tempFilter['value'] = $value;
  583. }
  584. if(!empty($tempFilter)) $finalFilters[] = $tempFilter;
  585. }
  586. return filters_set($finalFilters);
  587. }
  588. /**
  589. * Normalise et contrôles sur un tableau
  590. * de filtres pour le composant Filter.
  591. *
  592. * $filters : Array => tableau des paramètres avancés au format
  593. * array(
  594. * "jean", //Mot clé de la recherche simple, peut aussi être nommée 'keyword' => "jean"
  595. * array(
  596. * 'column' => 'nom_de_votre_colonne]',
  597. * 'operator' => '=|!=|LIKE:<:>', //Facultatif => "=" par défaut
  598. * 'value' => 'valeur_attendue', //Peut être du type array si plusieurs valeurs attendues
  599. * 'join' => 'and|or' //Facultatif => "and" par défaut
  600. * ),
  601. * etc... (à répéter pour les N critères)
  602. * )
  603. * Eg. :
  604. * filters_set(array(
  605. * 'test',
  606. * array(
  607. * 'column' => 'login',
  608. * 'value' => 'admin'
  609. * )
  610. * ));
  611. */
  612. function filters_set($filters){
  613. $finalFilters = array(
  614. 'k' => '',
  615. 'a' => array()
  616. );
  617. foreach ($filters as $key => $filter) {
  618. if($key==="keyword" || (!is_array($filter)) ) {
  619. $finalFilters['k'] = $filter;
  620. continue;
  621. }
  622. if(!isset($filter['column']) || !isset($filter['value'])) return;
  623. $filter['operator'] = isset($filter['operator']) ? $filter['operator'] : '=';
  624. if(!in_array(strtolower($filter['operator']), array('<','>','=','!=','like','not like','between','is null','is not null'))) return;
  625. $tempFilter = array(
  626. "c" => $filter['column'],
  627. "o" => $filter['operator'],
  628. "v" => is_array($filter['value']) ? $filter['value'] : array($filter['value']),
  629. );
  630. if(isset($filter['join'])){
  631. if(!in_array(strtolower($filter['join']), array('and','or'))) return;
  632. $tempFilter['j'] = $filter['join'];
  633. }
  634. if(isset($filter['type'])) $tempFilter['t'] = $filter['type'];
  635. $finalFilters['a'][] = $tempFilter;
  636. }
  637. return $finalFilters;
  638. }
  639. // Construit une requete sécurisée pour le composant filtre
  640. function filter_secure_query($filters,$allowedColumns,&$query,&$data,$iteration = 0,$columnEscape="`"){
  641. if($iteration==0 && !empty($filters)) $query .= ' AND (';
  642. foreach ($filters as $i=>$filter) {
  643. if(isset($filter['group'])){
  644. $query .= ' ( ';
  645. filter_secure_query($filter['group'],$allowedColumns,$query,$data,$iteration+1);
  646. $query .= ' ) ';
  647. }else{
  648. $filter['operator'] = html_entity_decode($filter['operator']);
  649. if(!in_array($filter['column'], $allowedColumns)) throw new Exception("Colonne '".$filter['column']."' interdite", 400);
  650. if(!in_array(strtolower($filter['operator']), array('<','>','=','!=','like','not like','in','not in','between','is null','is not null'))) return;
  651. if(isset($filter['join']) && !in_array(strtolower($filter['join']), array('and','or'))) return;
  652. if(!preg_match("/.*\..*/i", $filter['column']))
  653. $filter['column'] = $columnEscape.$filter['column'].$columnEscape;
  654. if(strtolower($filter['type']) == 'date' && isset($filter['value'])){
  655. $filter['column'] = 'UNIX_TIMESTAMP(STR_TO_DATE(DATE_FORMAT(FROM_UNIXTIME('.$filter['column'].'),"%d/%m/%Y"), "%d/%m/%Y"))';
  656. if(is_array($filter['value'])){
  657. foreach($filter['value'] as $j=>$value){
  658. $filter['value'][$j] = timestamp_date($value);
  659. }
  660. } else {
  661. $filter['value'] = timestamp_date($filter['value']);
  662. }
  663. }
  664. switch(strtolower($filter['operator'])){
  665. case 'like':
  666. case 'not like':
  667. if(is_array($filter['value'])) $filter['value'] = array_pop($filter['value']);
  668. //$query .= $i==0 ? ' '.$filter['join'].' (' : ' '.$filter['join'].' ';
  669. $query .= ' '.$filter['column'].' '.$filter['operator'].' ?';
  670. $data[] = '%'.$filter['value'].'%' ;
  671. break;
  672. case 'is null':
  673. //$query .= $i==0 ? ' '.$filter['join'].' (' : ' '.$filter['join'].' ';
  674. $query .= ' ('.$filter['column'].' IS NULL OR '.$filter['column'].'="") ';
  675. break;
  676. case 'is not null':
  677. //$query .= $i==0 ? ' '.$filter['join'].' (' : ' '.$filter['join'].' ';
  678. $query .= ' ('.$filter['column'].' IS NOT NULL AND '.$filter['column'].'!="") ';
  679. break;
  680. case 'between':
  681. if(is_array($filter['value'])){
  682. //$query .= $i==0 ? ' '.$filter['join'].' (' : ' '.$filter['join'].' ';
  683. $query .= ' '.$filter['column'].' '.$filter['operator'].' ? AND ?';
  684. $data[] = $filter['value'][0];
  685. $data[] = $filter['value'][1];
  686. }
  687. break;
  688. case 'in':
  689. $query .= ' ('.$filter['column'].' IN (?) ) ';
  690. $data[] = implode(',',$filter['value']);
  691. break;
  692. default:
  693. if(is_array($filter['value']))
  694. $filter['value'] = array_pop($filter['value']);
  695. //$query .= $i==0 ? ' '.$filter['join'].' (' : ' '.$filter['join'].' ';
  696. $query .= ' '.$filter['column'].' '.$filter['operator'].' ?';
  697. $data[] = $filter['value'];
  698. break;
  699. }
  700. }
  701. if(isset($filter['join'])) $query .= ' '.$filter['join'].' ';
  702. }
  703. if($iteration==0 && !empty($filters)) $query .= ')';
  704. }
  705. function sort_secure_query($sort,$allowedColumns,&$query){
  706. if(!in_array($sort['sortable'], $allowedColumns)) return;
  707. if(!in_array(strtolower($sort['sort']), array('asc','desc',''))) return;
  708. $query .= ' ORDER BY '.$sort['sortable'].' '.$sort['sort'];
  709. }
  710. function check_mail($mail){
  711. return ($mail && !empty($mail) && filter_var($mail, FILTER_VALIDATE_EMAIL));
  712. }
  713. function truncate($text, $length = 100, $options = array()) {
  714. $default = array(
  715. 'ending' => '...',
  716. 'exact' => true,
  717. 'html' => false,
  718. 'keepTags' => true,
  719. );
  720. $options = array_merge($default, $options);
  721. extract($options);
  722. if ($html) {
  723. if (mb_strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
  724. return $text;
  725. }
  726. $totalLength = mb_strlen(strip_tags($ending));
  727. $openTags = array();
  728. $truncate = '';
  729. preg_match_all('/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER);
  730. foreach ($tags as $tag) {
  731. if (!preg_match('/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag[2])) {
  732. if (preg_match('/<[\w]+[^>]*>/s', $tag[0])) {
  733. array_unshift($openTags, $tag[2]);
  734. } else if (preg_match('/<\/([\w]+)[^>]*>/s', $tag[0], $closeTag)) {
  735. $pos = array_search($closeTag[1], $openTags);
  736. if ($pos !== false) {
  737. array_splice($openTags, $pos, 1);
  738. }
  739. }
  740. }
  741. $truncate .= $tag[1];
  742. $contentLength = mb_strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', ' ', $tag[3]));
  743. if ($contentLength + $totalLength > $length) {
  744. $left = $length - $totalLength;
  745. $entitiesLength = 0;
  746. if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|&#x[0-9a-f]{1,6};/i', $tag[3], $entities, PREG_OFFSET_CAPTURE)) {
  747. foreach ($entities[0] as $entity) {
  748. if ($entity[1] + 1 - $entitiesLength <= $left) {
  749. $left--;
  750. $entitiesLength += mb_strlen($entity[0]);
  751. } else {
  752. break;
  753. }
  754. }
  755. }
  756. $truncate .= mb_substr($tag[3], 0 , $left + $entitiesLength);
  757. break;
  758. } else {
  759. $truncate .= $tag[3];
  760. $totalLength += $contentLength;
  761. }
  762. if ($totalLength >= $length) {
  763. break;
  764. }
  765. }
  766. } else {
  767. if (mb_strlen($text) <= $length) {
  768. return $text;
  769. } else {
  770. $truncate = mb_substr($text, 0, $length - mb_strlen($ending));
  771. }
  772. }
  773. if (!$exact) {
  774. $spacepos = mb_strrpos($truncate, ' ');
  775. if (isset($spacepos)) {
  776. if ($html) {
  777. $bits = mb_substr($truncate, $spacepos);
  778. preg_match_all('/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER);
  779. if (!empty($droppedTags)) {
  780. foreach ($droppedTags as $closingTag) {
  781. if (!in_array($closingTag[1], $openTags)) {
  782. array_unshift($openTags, $closingTag[1]);
  783. }
  784. }
  785. }
  786. }
  787. $truncate = mb_substr($truncate, 0, $spacepos);
  788. }
  789. }
  790. $truncate .= $ending;
  791. if ($html) {
  792. foreach ($openTags as $tag) {
  793. $truncate .= '</'.$tag.'>';
  794. }
  795. }
  796. return (!$keepTags ? strip_tags($truncate) : $truncate);
  797. }
  798. /**
  799. * Permet de tronquer un texte en fonction d'un
  800. * nombre de caractères donné
  801. * @param string $content le texte à tronquer
  802. * @param int $limit le nombre de caractères qu'on garde
  803. * @param string $limiter le caractère de remplacement de fin de chaine
  804. * @return string le texte final, tronqué
  805. */
  806. function truncate_content($content,$limit,$limiter){
  807. if(strlen($content)>$limit) $content = mb_substr($content, 0,$limit).$limiter;
  808. return $content;
  809. }
  810. //Permet de décoder la chaîne d'entrée $string
  811. //en UTF-8 et de décoder les caractères spéciaux
  812. //pour l'affichage HTML
  813. function html_decode_utf8($string){
  814. return htmlspecialchars(html_entity_decode($string), ENT_QUOTES, 'UTF-8');
  815. }
  816. //Convertit la première lettre de la chaine $string
  817. //en majuscule et le reste de la chaine en minuscule UTF-8
  818. //pour l'affichage HTML
  819. function mb_ucfirst($string,$encoding = 'UTF-8'){
  820. $length = mb_strlen($string, $encoding);
  821. $firstChar = mb_substr($string, 0, 1, $encoding);
  822. $rest = mb_substr($string, 1, $length - 1, $encoding);
  823. return mb_strtoupper($firstChar, $encoding).$rest;
  824. }
  825. // Permet de mettre au bon format le n° de
  826. // téléphone fourni dans le formulaire
  827. function normalize_phone_number($number){
  828. $nb = str_replace(array(' ', '.', ',', '-'), '', $number);
  829. $nb = chunk_split($nb, 2, ' ');
  830. $nb = rtrim($nb);
  831. preg_match("/^[0-9]{2} [0-9]{2} [0-9]{2} [0-9]{2} [0-9]{2}/", $nb, $matches);
  832. return isset($matches[0]) ? $matches[0] : $nb;
  833. }
  834. // Permet de voir si le format du n° de téléphone
  835. // fourni correspond à un format correct
  836. function check_phone_number($number, $international=false){
  837. if(!$international && preg_match("/\+/i", $number)) return false;
  838. $nb = str_replace(array(' ', '.', ',', '-'), '', $number);
  839. if (!is_numeric($nb)) return false;
  840. return true;
  841. }
  842. //Retourne la valeur la plus proche d'un
  843. //nombre donné par rapport à un tableau de
  844. //nombres
  845. function get_closest_number($search, $arr){
  846. $closest = null;
  847. foreach ($arr as $item) {
  848. if ($closest === null || abs($search - $closest) > abs($item - $search)) {
  849. $closest = $item;
  850. }
  851. }
  852. return $closest;
  853. }
  854. // Permet de convertir string encodée en UTF-8
  855. // en ASCII pour ensuite appliquer une méthode
  856. // de Levenshtein sans avoir de divergences
  857. // trop importantes dues aux accents présents.
  858. function utf8_to_extended_ascii($str, &$map) {
  859. // find all multibyte characters (cf. utf-8 encoding specs)
  860. $matches = array();
  861. if (!preg_match_all('/[\xC0-\xF7][\x80-\xBF]+/', $str, $matches))
  862. return $str; // plain ascii string
  863. // update the encoding map with the characters not already met
  864. foreach ($matches[0] as $mbc)
  865. if (!isset($map[$mbc]))
  866. $map[$mbc] = chr(128 + count($map));
  867. // finally remap non-ascii characters
  868. return strtr($str, $map);
  869. }
  870. // Override de la méthode de Levenshtein pour
  871. // compérer 2 strings encondées en UTF-8
  872. function levenshtein_utf8($s1, $s2) {
  873. $charMap = array();
  874. $s1 = utf8_to_extended_ascii($s1, $charMap);
  875. $s2 = utf8_to_extended_ascii($s2, $charMap);
  876. return levenshtein($s1, $s2);
  877. }
  878. // Méthode qui retourne sous forme de tableau
  879. // les metaphones // des différents mots d'une
  880. // phrase.
  881. function get_metaphones($sentence) {
  882. $metaphones = array();
  883. $words = explode(' ',$sentence);
  884. foreach ($words as $word) {
  885. $metaphones[] = metaphone($word);
  886. }
  887. return $metaphones;
  888. }
  889. // Permet de trouver une chaîne de caractère s'approchant
  890. // le plus de l'entrée fournie en paramètres
  891. function find_best_match($words = array(), $input = '') {
  892. $closest = '';
  893. $foundBestMatch = -1;
  894. $tmpInput = implode(' ', get_metaphones($input));
  895. foreach($words as $word) {
  896. $tmpGauge = implode(' ', get_metaphones($word));
  897. $similarity = levenshtein_utf8($tmpInput, $tmpGauge);
  898. if ($similarity == 0) {
  899. $closest = $word;
  900. $foundBestMatch = 0;
  901. break;
  902. }
  903. if ($similarity <= $foundBestMatch || $foundBestMatch < 0) {
  904. $closest = $word;
  905. $foundBestMatch = $similarity;
  906. }
  907. }
  908. return $closest;
  909. }
  910. // Convertit nombres en lettres (utile pour Excel)
  911. function numbers_to_letters($num){
  912. $num = intval($num);
  913. $letter = '';
  914. if ($num <= 0) return $letter;
  915. while($num != 0){
  916. $p = ($num - 1) % 26;
  917. $num = intval(($num - $p) / 26);
  918. $letter = chr(65 + $p) . $letter;
  919. }
  920. return $letter;
  921. }
  922. //Convertit lettres en nombres (utile pour Excel)
  923. function letters_to_numbers($col){
  924. $col = str_pad($col,3,'0', STR_PAD_LEFT);
  925. $i = 0;
  926. if ($col[0] != '0') {
  927. $i = ((ord($col[0]) - 64) * 676) + 26;
  928. $i += ($col[1] == '0') ? 0 : (ord($col[1]) - 65) * 26;
  929. } else {
  930. $i += ($col[1] == '0') ? 0 : (ord($col[1]) - 64) * 26;
  931. }
  932. $i += ord($col[2]) - 64;
  933. return $i;
  934. }
  935. //Check si c'est un date bien formattée
  936. function is_date($date){
  937. $date = str_replace(array('-',' ','\\'),'/',trim($date));
  938. if(trim($date)=='') return false;
  939. if (count(explode('/',$date)) < 3) return false;
  940. list($d,$m,$y) = explode('/',$date);
  941. if( !is_numeric($d) || !is_numeric($m) || !is_numeric($y) ) return false;
  942. return checkdate ( $m , $d , $y );
  943. }
  944. //Cherche la position de $needles dans
  945. //$haystack, où $needles est un array
  946. //de string et $haystack est le string à
  947. //comparer
  948. function strpos_array($haystack, $needles=array(), $offset=0) {
  949. $chr = array();
  950. foreach($needles as $needle) {
  951. $res = strpos($haystack, $needle, $offset);
  952. if ($res !== false) $chr[$needle] = $res;
  953. }
  954. if(empty($chr)) return false;
  955. return min($chr);
  956. }
  957. //Supprime un dossier et son contenu
  958. //de manière récursive
  959. function delete_folder_tree($dir, $selfDestroy=false) {
  960. $files = array_diff(scandir($dir), array('.','..'));
  961. foreach ($files as $file) {
  962. (is_dir("$dir/$file")) ? delete_folder_tree("$dir/$file") : unlink("$dir/$file");
  963. }
  964. if($selfDestroy) return rmdir($dir);
  965. }
  966. //Normalise les caractères un peu spéciaux
  967. //d'une chaîne de caractère
  968. function normalize_chars($string, $mask=array()) {
  969. $normalizeChars = array(
  970. 'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
  971. 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
  972. 'Ï'=>'I', 'Ñ'=>'N', 'Ń'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U',
  973. 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a',
  974. 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i',
  975. 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ń'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o',
  976. 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f',
  977. 'ă'=>'a', 'î'=>'i', 'â'=>'a', 'ș'=>'s', 'ț'=>'t', 'Ă'=>'A', 'Î'=>'I', 'Â'=>'A', 'Ș'=>'S', 'Ț'=>'T',
  978. '’'=>'\'', '–'=>'-', '€'=>'&euro;', '&'=>'&amp;','œ'=>'oe', '•'=>'-'
  979. );
  980. return strtr($string, array_diff_key($normalizeChars, array_flip($mask)));
  981. }
  982. //Convertit un nombre en son équivalent écrit
  983. //e.g: 23 --> VINGT-TROIS
  984. function number_to_words($number, $feminine=false) {
  985. $hyphen = '-';
  986. $conjunction = ' et ';
  987. $separator = ', ';
  988. $negative = 'moins ';
  989. $decimal = ' virgule ';
  990. $dictionary = array(
  991. 0 => 'zero',
  992. 1 => !$feminine?'un':'une',
  993. 2 => 'deux',
  994. 3 => 'trois',
  995. 4 => 'quatre',
  996. 5 => 'cinq',
  997. 6 => 'six',
  998. 7 => 'sept',
  999. 8 => 'huit',
  1000. 9 => 'neuf',
  1001. 10 => 'dix',
  1002. 11 => 'onze',
  1003. 12 => 'douze',
  1004. 13 => 'treize',
  1005. 14 => 'quatorze',
  1006. 15 => 'quinze',
  1007. 16 => 'seize',
  1008. 17 => 'dix-sept',
  1009. 18 => 'dix-huit',
  1010. 19 => 'dix-neuf',
  1011. 20 => 'vingt',
  1012. 30 => 'trente',
  1013. 40 => 'quarante',
  1014. 50 => 'cinquante',
  1015. 60 => 'soixante',
  1016. 70 => 'soixante-dix',
  1017. 80 => 'quatre-vingt',
  1018. 90 => 'quatre-vingt dix',
  1019. 100 => 'cent',
  1020. 1000 => 'mille',
  1021. 1000000 => 'million',
  1022. 1000000000 => 'milliard',
  1023. 1000000000000 => 'trillion',
  1024. 1000000000000000 => 'quadrillion',
  1025. 1000000000000000000 => 'quintillion'
  1026. );
  1027. if (!is_numeric($number)) return false;
  1028. if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) throw new Exception('number_to_words accepte uniquement des nombres compris entre -'.PHP_INT_MAX.' et '.PHP_INT_MAX);
  1029. if ($number < 0) return $negative.number_to_words(abs($number));
  1030. $string = $fraction = null;
  1031. if (strpos($number, '.') !== false)
  1032. list($number, $fraction) = explode('.', $number);
  1033. switch (true) {
  1034. case $number < 21:
  1035. $string = $dictionary[$number];
  1036. break;
  1037. case $number == 21:
  1038. $string = $dictionary[20].$conjunction.$dictionary[1];
  1039. break;
  1040. case $number == 31:
  1041. $string = $dictionary[30].$conjunction.$dictionary[1];
  1042. break;
  1043. case $number == 41:
  1044. $string = $dictionary[40].$conjunction.$dictionary[1];
  1045. break;
  1046. case $number == 51:
  1047. $string = $dictionary[50].$conjunction.$dictionary[1];
  1048. break;
  1049. case $number == 61:
  1050. $string = $dictionary[60].$conjunction.$dictionary[1];
  1051. break;
  1052. case $number == 71:
  1053. $string = $dictionary[60].$conjunction.$dictionary[11];
  1054. break;
  1055. case $number == 81:
  1056. $string = $dictionary[80].$hyphen.$dictionary[1];
  1057. break;
  1058. case $number == 91:
  1059. $string = $dictionary[80].$hyphen.$dictionary[11];
  1060. break;
  1061. case $number < 100:
  1062. $tens = ((int) ($number / 10)) * 10;
  1063. $units = $number % 10;
  1064. $string = $dictionary[$tens];
  1065. if ($units) {
  1066. $string .= $hyphen . $dictionary[$units];
  1067. }
  1068. break;
  1069. case $number < 1000:
  1070. $hundreds = $number / 100;
  1071. $remainder = $number % 100;
  1072. $string = ((int)$hundreds==1 ? '' : $dictionary[$hundreds].' ') . $dictionary[100];
  1073. if ($remainder) {
  1074. $string .= ' ' . number_to_words($remainder);
  1075. }
  1076. break;
  1077. default:
  1078. $baseUnit = pow(1000, floor(log($number, 1000)));
  1079. $numBaseUnits = (int) ($number / $baseUnit);
  1080. $remainder = $number % $baseUnit;
  1081. $string = number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
  1082. if ($remainder) {
  1083. $string .= $remainder < 100 ? $conjunction : ' ';
  1084. $string .= number_to_words($remainder);
  1085. }
  1086. break;
  1087. }
  1088. if (null !== $fraction && is_numeric($fraction)) {
  1089. $string .= $decimal;
  1090. if(strlen($fraction) <= 3) {
  1091. $string .= number_to_words($fraction);
  1092. } else {
  1093. $words = array();
  1094. foreach (str_split((string) $fraction) as $number)
  1095. $words[] = $dictionary[$number];
  1096. $string .= implode(' ', $words);
  1097. }
  1098. }
  1099. return mb_strtoupper($string);
  1100. }
  1101. /**
  1102. * Retrourne le numéral ordinal compact
  1103. * d'un nombre passé en paramètre.
  1104. * eg:
  1105. * 1 --> 1er,
  1106. * 2 --> 2ème, etc..
  1107. * Les nombres ordinaux sont définis par un
  1108. * l'ensemble des entiers naturels non nuls
  1109. * représenté N*={1,2,3,...}
  1110. */
  1111. function number_to_ordinal($number, $feminine=false){
  1112. $number = (int) $number;
  1113. //Aucun ordinal pour le rang 0
  1114. if($number == 0) return;
  1115. if($number == 1)
  1116. return $number.($feminine?'ère':'er');
  1117. return $number.'ème';
  1118. }
  1119. /**
  1120. * Vérifie si un nombre est entre 2 bornes
  1121. * Possibilité d'inclure ou d'exclure les bornes avec $strict
  1122. */
  1123. function number_between($number, $low, $high, $strict=false) {
  1124. if(!$strict && ($number<$low || $number>$high)) return false;
  1125. if($strict && ($number<=$low || $number>=$high)) return false;
  1126. return true;
  1127. }
  1128. /*
  1129. * Fonction pour vérifier si un nombre
  1130. * est un nombre décimal
  1131. *
  1132. * Eg: - 95.00 --> false
  1133. * - 95.5 --> true
  1134. * Les nombres avec décimales à 0 sont donc exclus
  1135. */
  1136. function is_decimal($val) {
  1137. return is_numeric($val) && floor($val)!=$val;
  1138. }
  1139. /**
  1140. * Retourne les fonction interdites
  1141. * utilisées dans une fonction eval()
  1142. * @param string $source [le string qui va $etre eval()]
  1143. * @return Array [Tableau des méthodes utilisées interdites]
  1144. */
  1145. function forbidden_macro($source){
  1146. $tokens = token_get_all('<?php '.$source.' ?>');
  1147. $forbiddens = array();
  1148. $allowed_functions = array(
  1149. 'ucfirst',
  1150. 'strto.*',
  1151. 'str_.*',
  1152. 'substr',
  1153. 'password_encrypt',
  1154. 'strpos',
  1155. 'date',
  1156. '[im|ex]plode',
  1157. 'preg_*',
  1158. 'count',
  1159. 'time',
  1160. 'array_.*',
  1161. '.sort',
  1162. );
  1163. foreach($tokens as $token){
  1164. if(is_string($token)) continue;
  1165. list($id, $text,$line) = $token;
  1166. if(in_array($id, array(T_FUNCTION,T_FUNC_C,T_EVAL,T_STRING))){
  1167. $allowed = false;
  1168. foreach ($allowed_functions as $function) {
  1169. preg_match('/'.$function.'/i', $text, $matches);
  1170. if(count($matches)!=0){
  1171. $allowed = true;
  1172. break;
  1173. }
  1174. }
  1175. if(!$allowed) $forbiddens[] = $text.' L'.$line;
  1176. }
  1177. if(in_array($id, array(
  1178. T_INCLUDE,
  1179. T_EXTENDS,
  1180. T_CLONE,
  1181. T_EXIT,
  1182. T_GLOBAL,
  1183. T_HALT_COMPILER,
  1184. T_IMPLEMENTS,
  1185. T_INCLUDE_ONCE,
  1186. T_REQUIRE,
  1187. T_REQUIRE_ONCE,
  1188. T_IMPLEMENTS
  1189. ))){
  1190. $forbiddens[] = $text.' L'.$line;
  1191. }
  1192. }
  1193. return $forbiddens;
  1194. }
  1195. function template($stream,$data,$mustacheTemplate = false){
  1196. //loop
  1197. $stream = preg_replace_callback('/{{\:([^\/\:\?}]*)}}(.*?){{\/\:[^\/\:\?}]*}}/is',function($matches) use ($data) {
  1198. $tag = $matches[1];
  1199. $streamTpl = $matches[2];
  1200. $stream = '';
  1201. if(!isset($data[$tag])) return $stream;
  1202. $values = $data[$tag];
  1203. foreach($values as $join){
  1204. $occurence = $streamTpl;
  1205. foreach($join as $key=>$value){
  1206. if(is_array($value) || is_object($value)) continue;
  1207. $occurence = str_replace(array('{{'.$key.'}}'),array($value),$occurence);
  1208. }
  1209. $stream .= $occurence;
  1210. }
  1211. return $stream;
  1212. },$stream);
  1213. //conditions
  1214. $stream = preg_replace_callback('/{{\?([^\/\:\?}]*)}}(.*?){{\/\?[^\/\:\?}]*}}/is',function($matches) use ($data) {
  1215. $key = $matches[1];
  1216. $stream = $matches[2];
  1217. return !isset($data[$key]) || (is_array($data[$key]) && count($data[$key])==0) ?'':$stream;
  1218. },$stream);
  1219. //simple vars
  1220. $stream = preg_replace_callback('/{{([^\/\:\;\?}]*)}}/',function($matches) use ($data) {
  1221. $key = $matches[1];
  1222. if(isset($data[$key]) && (is_string($data[$key]) || is_numeric($data[$key])) ) return $data[$key];
  1223. $value = '';
  1224. $attributes = explode('.',$key);
  1225. $current = $data;
  1226. foreach ($attributes as $attribute) {
  1227. if ( !isset($current[$attribute]) ) return;
  1228. $current = $current[$attribute];
  1229. $value = $current;
  1230. }
  1231. return $value;
  1232. },$stream);
  1233. return $stream;
  1234. }
  1235. /**
  1236. * Auteur: Anonymous
  1237. * Lien: https://www.php.net/manual/en/features.file-upload.post-method.php#120686
  1238. *
  1239. * Normalise le tableau de fichier récupérés
  1240. * pour une utilisation plus efficace et intuitive
  1241. *
  1242. * Exemple de format initial pour 2 fichiers dans $_FILES :
  1243. * Array (
  1244. [name] => Array (
  1245. [0] => foo.txt
  1246. [1] => bar.txt
  1247. ),
  1248. [type] => Array(
  1249. [0] => text/plain
  1250. [1] => text/plain
  1251. ),
  1252. [tmp_name] => Array(
  1253. [0] => /tmp/phpYzdqkD
  1254. [1] => /tmp/phpeEwEWG
  1255. ),
  1256. [error] => Array(
  1257. [0] => 0
  1258. [1] => 0
  1259. ),
  1260. [size] => Array(
  1261. [0] => 123
  1262. [1] => 456
  1263. )
  1264. * )
  1265. *
  1266. * Exemple de format de retour pour 2 fichiers :
  1267. * Array(
  1268. [0] => Array(
  1269. [name] => foo.txt
  1270. [type] => text/plain
  1271. [tmp_name] => /tmp/phpYzdqkD
  1272. [error] => 0
  1273. [size] => 123
  1274. ),
  1275. [1] => Array(
  1276. [name] => bar.txt
  1277. [type] => text/plain
  1278. [tmp_name] => /tmp/phpeEwEWG
  1279. [error] => 0
  1280. [size] => 456
  1281. )
  1282. )
  1283. * @param Array &$file_post [tableau de fichiers $_FILES]
  1284. * @return Array [le tableau ré-arrangé]
  1285. */
  1286. function normalize_php_files() {
  1287. $function = function($files, $fixedFiles=array(), $path=array()) use (&$function) {
  1288. foreach ($files as $key => $value) {
  1289. $temp = $path;
  1290. $temp[] = $key;
  1291. if(is_array($value)) {
  1292. $fixedFiles = $function($value, $fixedFiles, $temp);
  1293. } else {
  1294. $next = array_splice($temp, 1, 1);
  1295. $temp = array_merge($temp, $next);
  1296. $new = &$fixedFiles;
  1297. foreach ($temp as $key)
  1298. $new = &$new[$key];
  1299. $new = $value;
  1300. }
  1301. }
  1302. return $fixedFiles;
  1303. };
  1304. return $function($_FILES);
  1305. }
  1306. /**
  1307. * Incrémente automatiquement le nom d'un
  1308. * fichier si celui-ci existe déjà
  1309. * Fonction récursive.
  1310. * @param String $extension [extension du fichier]
  1311. * @param String $folder [dossier où localiser le fichier]
  1312. * @param String $filename [nom du fichier]
  1313. * @return String [nom de fichier final]
  1314. */
  1315. function autoincrement_filename($extension, $folder, $filename){
  1316. static $counter = 0;
  1317. $fileNb = count(glob(FILE_PATH.$folder.$filename));
  1318. $filenameProps = explode('.', $filename);
  1319. unset($filenameProps[count($filenameProps)-1]);
  1320. $finalFilename = implode('.', $filenameProps);
  1321. if($fileNb>0) {
  1322. $counter+=1;
  1323. $filename = preg_match("/(^.*?\()(\d+)([^\\d]*\)\..*$)/", $filename, $matches) ? $matches[1].$counter.$matches[3] : $finalFilename.'('.$counter.')'.'.'.$extension;
  1324. $filename = autoincrement_filename($extension, $folder, $filename);
  1325. }
  1326. return $filename;
  1327. }
  1328. function relative_path($absolutePath,$reference = __ROOT__){
  1329. $absolutePath = str_replace(array('\\','/'),SLASH,$absolutePath);
  1330. $reference = str_replace(array('\\','/'),SLASH,$reference);
  1331. return str_replace($reference,'',$absolutePath);
  1332. }
  1333. //Définit si une couleur hexadecimale est claire
  1334. //ou sombre en fonction d'un seuil de luminosité
  1335. function get_light($hexcode,$treshold = 510.0) {
  1336. $rgb = to_rgb($hexcode);
  1337. return (max($rgb[0], $rgb[1], $rgb[2]) + min($rgb[0], $rgb[1], $rgb[2])) / $treshold;
  1338. }
  1339. //Génère une couleur hexadécimale aléatoire
  1340. function random_hex_color() {
  1341. return '#'.str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT);
  1342. }
  1343. //Génère une couleur pastel basé sur
  1344. //le hash md5 du mot passé en paramètre
  1345. function random_hex_pastel_color($name) {
  1346. $hash = md5($name);
  1347. $red = hexdec(substr($hash, 8, 2));
  1348. $green = hexdec(substr($hash, 4, 2));
  1349. $blue = hexdec(substr($hash, 0, 2));
  1350. if($red < 128) $red += 128;
  1351. if($green < 128) $green += 128;
  1352. if($blue < 128) $blue += 128;
  1353. return "#" . dechex($red) . dechex($green) . dechex($blue);
  1354. }
  1355. //Convertit un code hexadecimal en code RGB
  1356. function to_rgb($hexcode) {
  1357. $hexcode = substr($hexcode, 1);
  1358. return array(hexdec($hexcode[0] . $hexcode[1]),
  1359. hexdec($hexcode[2] . $hexcode[3]),
  1360. hexdec($hexcode[4] . $hexcode[5]));
  1361. }
  1362. //Retourne le nom complet d'un mois en fonction de son numéro
  1363. function month_name($month){
  1364. $translates = array('Janvier','Fevrier','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Décembre');
  1365. return $translates[$month-1];
  1366. }
  1367. //Retourne le nom complet d'un jour en fonction de son numéro (1 : lundi,..., 7 :dimanche)
  1368. function day_name($day){
  1369. $translates = array('Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi','Dimanche');
  1370. return isset($translates[$day-1])? $translates[$day-1]:$day;
  1371. }
  1372. //Convertit un timestamp dans un format
  1373. //agréable et complet : ex Mardi 18 Avril 2019
  1374. function complete_date($time=null){
  1375. if(!isset($time)) $time = time();
  1376. return day_name(date('N',$time)).' '.date('d',$time).' '.month_name(date('m',$time)).' '.date('Y',$time);
  1377. }
  1378. //Récuperation des jours féries
  1379. function get_not_workable($date=null){
  1380. if ($date === null)
  1381. $date = time();
  1382. $date = strtotime(date('m/d/Y',$date));
  1383. $year = date('Y',$date);
  1384. $easterDate = easter_date($year);
  1385. $easterDay = date('j', $easterDate);
  1386. $easterMonth = date('n', $easterDate);
  1387. $easterYear = date('Y', $easterDate);
  1388. $holidays = array(
  1389. // Dates fixes
  1390. mktime(0, 0, 0, 1, 1, $year), // 1er janvier
  1391. mktime(0, 0, 0, 5, 1, $year), // Fête du travail
  1392. mktime(0, 0, 0, 5, 8, $year), // Victoire des alliés
  1393. mktime(0, 0, 0, 7, 14, $year), // Fête nationale
  1394. mktime(0, 0, 0, 8, 15, $year), // Assomption
  1395. mktime(0, 0, 0, 11, 1, $year), // Toussaint
  1396. mktime(0, 0, 0, 11, 11, $year), // Armistice
  1397. mktime(0, 0, 0, 12, 25, $year), // Noel
  1398. // Dates variables
  1399. mktime(0, 0, 0, $easterMonth, $easterDay + 1, $easterYear),
  1400. mktime(0, 0, 0, $easterMonth, $easterDay + 39, $easterYear),
  1401. mktime(0, 0, 0, $easterMonth, $easterDay + 50, $easterYear),
  1402. );
  1403. return $holidays;
  1404. }
  1405. //Retourne le chemin web d'un fichier en fonction de son chemin
  1406. //physique (ex : /img/logo.png pour /var/www/erp-core/img/logo.png)
  1407. function webpath($path){
  1408. $url = ROOT_URL.str_replace(array(__DIR__.SLASH,"\\","/"),array('','/','/'),$path);
  1409. $url = preg_replace('/([^\:])(\/{2,})/i', '$1/', $url);
  1410. return $url;
  1411. }
  1412. //Retourne un tableau clé/valeur des valeurs
  1413. //existantes en doublons du tableau passé en paramètre
  1414. function array_not_unique($array=array()){
  1415. return array_diff_key($array, array_unique($array));
  1416. }
  1417. //Retourne true si aucune valeur du tableau passé
  1418. //en paramètre a un doublon, false sinon.
  1419. function is_array_unique($array=array()){
  1420. return empty(array_not_unique($array));
  1421. }
  1422. function make_cookie($nom, $valeur, $expire='',$ns ='/') {
  1423. if($expire == ''){
  1424. setcookie($nom, $valeur, mktime(0,0,0, date("d"), date("m"), (date("Y")+1)), $ns);
  1425. } else {
  1426. setcookie($nom, '', mktime(0,0,0, date("d"), date("m"), (date("Y")-1)), $ns);
  1427. }
  1428. }
  1429. //Permet de formatter les prix à afficher
  1430. //de la même manière partout sur l'ERP.
  1431. //Si jamais on veut changer de normalisation
  1432. //pour l'affichage des prix, il suffit de changer
  1433. //le fonctionnement ici uniquement.
  1434. function display_price($price){
  1435. return number_format($price, 2, ',', ' ');
  1436. }
  1437. //Permet de formatter les prix à enregistrer
  1438. //de la même manière partout sur l'ERP.
  1439. //Si jamais on veut changer de normalisation
  1440. //pour l'enregistrement des prix, il suffit de changer
  1441. //le fonctionnement ici uniquement.
  1442. function format_price($price){
  1443. return str_replace(',','.',$price);
  1444. }
  1445. //Permet de calculer un age en fonction de la date du jour
  1446. //Paramètre : la date de départ(format timestamp), l'unité de retour souhaitée(format 'd', 'm', 'Y'... voir fonction diff de php)
  1447. //Renvoie : l'age en entier
  1448. function age($from,$unit = 'y'){
  1449. if(!isset($from) || !is_numeric($from)) return 'N/A';
  1450. $from = new \DateTime(date('Y-m-d',$from));
  1451. $to = new \DateTime('today');
  1452. $age = $from->diff($to);
  1453. if(!$age || !array_key_exists($unit, $age))
  1454. return 'N/A';
  1455. return $age = $age->$unit;
  1456. }
  1457. //Permet d'échapper tous les caractères interdits dans une chaine json
  1458. function escape_json_string($value) { # list from www.json.org: (\b backspace, \f formfeed)
  1459. $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "\x08", "\x0c");
  1460. $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b");
  1461. $result = str_replace($escapers, $replacements, $value);
  1462. return $result;
  1463. }
  1464. if( !function_exists('apache_request_headers') ) {
  1465. ///
  1466. function apache_request_headers() {
  1467. $arh = array();
  1468. $rx_http = '/\AHTTP_/';
  1469. foreach($_SERVER as $key => $val) {
  1470. if( preg_match($rx_http, $key) ) {
  1471. $arh_key = preg_replace($rx_http, '', $key);
  1472. $rx_matches = array();
  1473. // do some nasty string manipulations to restore the original letter case
  1474. // this should work in most cases
  1475. $rx_matches = explode('_', $arh_key);
  1476. if( count($rx_matches) > 0 and strlen($arh_key) > 2 ) {
  1477. foreach($rx_matches as $ak_key => $ak_val) $rx_matches[$ak_key] = ucfirst($ak_val);
  1478. $arh_key = implode('-', $rx_matches);
  1479. }
  1480. $arh[$arh_key] = $val;
  1481. }
  1482. }
  1483. return( $arh );
  1484. }
  1485. }
  1486. if(!function_exists('apache_get_version')){
  1487. function apache_get_version(){
  1488. if(!isset($_SERVER['SERVER_SOFTWARE']) || strlen($_SERVER['SERVER_SOFTWARE']) == 0){
  1489. return false;
  1490. }
  1491. return $_SERVER["SERVER_SOFTWARE"];
  1492. }
  1493. }
  1494. //Effectue un basename en tenant compte des caractères
  1495. //utf8( non pris en compte par basename PHP sous linux)
  1496. function mt_basename($path){
  1497. $path = str_replace(array("\\","/"),SLASH, $path);
  1498. $nameSplit = explode(SLASH,$path);
  1499. return end($nameSplit);
  1500. }
  1501. //filtre les balises dangeureuses (script,link..) dans les contenus wysiwyg
  1502. function wysiwyg_filter($html){
  1503. $html = preg_replace('#<(script|link)(.*?)>(.*?)</(script|link)>#is', '', $html);
  1504. $html = preg_replace('#<(script|link)([^>]*?)>#is', '', $html);
  1505. return $html;
  1506. }
  1507. function display_format_price($price) {
  1508. return display_price(format_price($price));
  1509. }
  1510. ?>