function.php 59 KB

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