core.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. <?php
  2. class core
  3. {
  4. public static function ifGet(string $_string = NULL)
  5. {
  6. if($_string == NULL){
  7. return (empty($_GET)) ? FALSE : TRUE;
  8. } else {
  9. if (isset($_GET[$_string])) {
  10. return TRUE;
  11. } else {
  12. return FALSE;
  13. }
  14. }
  15. }
  16. public static function ifPost(string $_string = NULL)
  17. {
  18. if($_string == NULL){
  19. return (empty($_POST)) ? FALSE : TRUE;
  20. } else {
  21. if (isset($_POST[$_string])) {
  22. return TRUE;
  23. } else {
  24. return FALSE;
  25. }
  26. }
  27. }
  28. public static function ifFiles(string $_string = NULL)
  29. {
  30. if ($_string == NULL) {
  31. return (empty($_FILES)) ? FALSE : TRUE;
  32. } else {
  33. if (isset($_FILES[$_string]) AND $_FILES[$_string]["size"] > 0) {
  34. return TRUE;
  35. } else {
  36. return FALSE;
  37. }
  38. }
  39. }
  40. public static function getGet(string $_string = NULL)
  41. {
  42. if ($_string == NULL) {
  43. return $_GET;
  44. } else {
  45. if (isset($_GET[$_string])) {
  46. return $_GET[$_string];
  47. } else {
  48. return NULL;
  49. }
  50. }
  51. }
  52. public static function getPost(string $_string = NULL)
  53. {
  54. if ($_string == NULL) {
  55. return $_POST;
  56. } else {
  57. if (isset($_POST[$_string])) {
  58. return $_POST[$_string];
  59. } else {
  60. return NULL;
  61. }
  62. }
  63. }
  64. public static function getFiles(string $_string = NULL)
  65. {
  66. if ($_string == NULL) {
  67. return $_FILES;
  68. } else {
  69. if (isset($_FILES[$_string])) {
  70. return $_FILES[$_string];
  71. } else {
  72. return NULL;
  73. }
  74. }
  75. }
  76. public static function isInArrayString(array $_array, string $_string, int $_exact = NULL)
  77. {
  78. foreach ($_array as $value) {
  79. if (strripos($_string, $value) !== FALSE and $_exact == NULL) {
  80. return TRUE;
  81. } elseif ($_string == $value and $_exact == 1) {
  82. return TRUE;
  83. }
  84. }
  85. return FALSE;
  86. }
  87. public static function checkboxSelecter(bool $_val, $_echo = 1)
  88. {
  89. $tmp = ($_val == TRUE) ? "checked" : "";
  90. if($_echo == 1){
  91. echo $tmp;
  92. } else {
  93. return $tmp;
  94. }
  95. }
  96. public static function getAllConfig()
  97. {
  98. db::query("SELECT "
  99. . "" . DB_T_CONFIG . ".name, "
  100. . "" . DB_T_CONFIG . ".value "
  101. . "FROM " . DB_T_CONFIG);
  102. return db::resultset();
  103. }
  104. public static function getConfig(string $_name)
  105. {
  106. db::query("SELECT value FROM " . DB_T_CONFIG . " WHERE name = :name");
  107. db::bind(':name', $_name);
  108. return db::single()["value"];
  109. }
  110. public static function updateConfig(string $_name, string $_value)
  111. {
  112. db::query("UPDATE " . DB_T_CONFIG . " SET "
  113. . "value = :value "
  114. . "WHERE name = :name");
  115. db::bind(':value', $_value);
  116. db::bind(':name', $_name);
  117. try {
  118. db::execute();
  119. return TRUE;
  120. } catch (Exception $ex) {
  121. return FALSE;
  122. }
  123. }
  124. public static function cleanAccent(string $_data)
  125. {
  126. $search = array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ç', 'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'à', 'á', 'â', 'ã', 'ä', 'å', 'ç', 'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï', 'ð', 'ò', 'ó', 'ô', 'õ', 'ö', 'ù', 'ú', 'û', 'ü', 'ý', 'ÿ');
  127. $replace = array('A', 'A', 'A', 'A', 'A', 'A', 'C', 'E', 'E', 'E', 'E', 'I', 'I', 'I', 'I', 'O', 'O', 'O', 'O', 'O', 'U', 'U', 'U', 'U', 'Y', 'a', 'a', 'a', 'a', 'a', 'a', 'c', 'e', 'e', 'e', 'e', 'i', 'i', 'i', 'i', 'o', 'o', 'o', 'o', 'o', 'o', 'u', 'u', 'u', 'u', 'y', 'y');
  128. $return = str_replace($search, $replace, $_data);
  129. return strtoupper($return);
  130. }
  131. public static function convertDate(string $_datetime, bool $_hour = TRUE)
  132. {
  133. $pieces = explode(" ", $_datetime);
  134. if ($_hour == TRUE) {
  135. $pieces3 = explode(":", $pieces[1]);
  136. }
  137. $pieces2 = explode("-", $pieces[0]);
  138. if ($_hour == TRUE) {
  139. return $pieces2[2] . "/" . $pieces2[1] . "/" . $pieces2[0] . " à " . $pieces3[0] . ":" . $pieces3[1];
  140. } else {
  141. return $pieces2[2] . "/" . $pieces2[1] . "/" . $pieces2[0];
  142. }
  143. }
  144. public static function dateFr(string $_timestampMysql = NULL)
  145. {
  146. if ($_timestampMysql == NULL) {
  147. $Now = new DateTime('now', new DateTimeZone(TIME_ZONE));
  148. return $Now->format("d/m/Y H:i:s");
  149. } else {
  150. return DateTime::createFromFormat("d/m/Y H:i:s", $_timestampMysql);
  151. }
  152. }
  153. public static function dateFromTimestamp(int $_timestamp = NULL)
  154. {
  155. if ($_timestamp == NULL) {
  156. return NULL;
  157. } else {
  158. return date("Y-m-d H:i:s", $_timestamp);
  159. }
  160. }
  161. public static function dateWhithoutHours(string $_datetime)
  162. {
  163. return explode(" ", $_datetime)[0];
  164. }
  165. public static function formatFileSize(float $_size, int $_decimalplaces = 0)
  166. {
  167. $sizes = array('O', 'Ko', 'Mo', 'Go', 'To');
  168. for ($i = 0; $_size > 1024 && $i < count($sizes) - 1; $i++) {
  169. $_size /= 1024;
  170. }
  171. return round($_size, $_decimalplaces) . ' ' . $sizes[$i];
  172. }
  173. public static function checkStringOnly(string $_string)
  174. {
  175. if (!ctype_alpha($_string)) {
  176. return TRUE;
  177. } else {
  178. return FALSE;
  179. }
  180. }
  181. public static function print_r(array $_array, int $_exit = NULL)
  182. {
  183. echo "<div>".debug::getTraces() . "</div>";
  184. echo "<pre>";
  185. print_r($_array);
  186. echo "</pre>";
  187. ($_exit != NULL) ? exit() : NULL;
  188. }
  189. public static function elementMenu(string $_id, string $_href, string $_titre, string $_style = NULL)
  190. {
  191. if (access::ifAccesss($_id)) {
  192. ($_style != NULL) ? $_style = ' style="' . $_style . '"' : NULL;
  193. echo '<li class="nav-item"><a class="nav-link' . get::currentPage($_id) . '" aria-current="page" href="' . $_href . '"' . $_style . '>';
  194. echo icon::getFont(["type" => $_id, "size" => "18px"]);
  195. echo ' ' . $_titre . '</a></li>';
  196. }
  197. }
  198. public static function elementMenuLink(string $_id, string $_href, string $_titre, string $_style = NULL, string $_target = "_blank")
  199. {
  200. if (access::ifAccesss($_id)) {
  201. ($_style != NULL) ? $_style = ' style="' . $_style . '"' : NULL;
  202. echo '<li class="nav-item"><a class="nav-link" target="' . $_target . '" href="' . $_href . '"' . $_style . '>';
  203. echo icon::getFont(["type" => $_id, "size" => "18px"]);
  204. echo ' ' . $_titre . '</a></li>';
  205. }
  206. }
  207. public static function elementMenuH6(string $_id, string $_titre, string $_style = NULL, string $_collapse = NULL)
  208. {
  209. if (access::ifAccesss($_id)) {
  210. ($_style != NULL) ? $_style = $_style : NULL;
  211. echo '<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
  212. <a style="text-decoration: none; ' . $_style . '" href="#' . $_collapse . '" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle text-dark">' . $_titre . '</a>
  213. </h6>';
  214. }
  215. }
  216. public static function filAriane(array $_arbo)
  217. {
  218. $return = '<nav aria-label="breadcrumb bg-300">';
  219. $return .= '<ol class="breadcrumb" style="padding:5px 10px; border-bottom: 1px solid #e9ecef;">';
  220. foreach ($_arbo["arbo"] as $label => $lien) {
  221. if ($_arbo["current"] == $label) {
  222. $return .= '<li class="breadcrumb-item active" aria-current="page">' . $label . '</li>';
  223. } elseif ($lien == NULL) {
  224. $return .= '<li class="breadcrumb-item">' . $label . '</li>';
  225. } else {
  226. $return .= '<li class="breadcrumb-item"><a href="' . $lien . '" title="' . $label . '">' . $label . '</a></li>';
  227. }
  228. }
  229. $return .= '</ol>';
  230. $return .= '</nav>';
  231. return $return;
  232. }
  233. public static function caculPourcentage(?int $_nombre, ?int $_total, int $_pourcentage = 100)
  234. {
  235. if ($_nombre == NULL) return 0;
  236. $resultat = ($_nombre / $_total) * $_pourcentage;
  237. return round($resultat);
  238. }
  239. public static function encodeUTF8(string $_data)
  240. {
  241. return (mb_detect_encoding($_data) != "UTF-8") ? mb_convert_encoding($_data, 'UTF-8', mb_list_encodings()) : $_data;
  242. }
  243. public static function testConnexionInternet()
  244. {
  245. $hosts = ['1.1.1.1', '1.0.0.1', '8.8.8.8', '8.8.4.4'];
  246. foreach ($hosts as $host) {
  247. if ($connected = @fsockopen($host, 443)) {
  248. fclose($connected);
  249. return TRUE;
  250. }
  251. }
  252. return FALSE;
  253. }
  254. public static function printDateTxt()
  255. {
  256. $date = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
  257. return $date->format(time()) . " à " . date("H:i:s");
  258. }
  259. public static function addFileMaintenance()
  260. {
  261. $myfile = fopen(DOCUMENT_ROOT . FILE_MAINTENANCE, "w");
  262. fclose($myfile);
  263. }
  264. public static function removeFileMaintenance()
  265. {
  266. unlink(DOCUMENT_ROOT . FILE_MAINTENANCE);
  267. }
  268. public static function isMaintenance()
  269. {
  270. return (file_exists(DOCUMENT_ROOT . FILE_MAINTENANCE)) ? TRUE : FALSE;
  271. }
  272. public static function addFileDebug()
  273. {
  274. $myfile = fopen(DOCUMENT_ROOT . FILE_DEBUG, "w");
  275. fclose($myfile);
  276. }
  277. public static function removeFileDebug()
  278. {
  279. unlink(DOCUMENT_ROOT . FILE_DEBUG);
  280. }
  281. public static function isDebug()
  282. {
  283. return (file_exists(DOCUMENT_ROOT . FILE_DEBUG)) ? TRUE : FALSE;
  284. }
  285. public static function resetDatas()
  286. {
  287. db::query("TRUNCATE " . DB_T_TEMP_SALARIES);
  288. db::execute();
  289. json::delete("tmp_salaries");
  290. db::query("TRUNCATE " . DB_T_SALARIES);
  291. db::execute();
  292. json::delete("salaries");
  293. db::query("TRUNCATE " . DB_T_FILES);
  294. db::execute();
  295. file::cleanAllFiles(DIR_DATAS_FILES);
  296. db::query("TRUNCATE " . DB_T_EXCEL);
  297. db::execute();
  298. json::delete("excel");
  299. db::query("TRUNCATE " . DB_T_SALARIES_PROWEB);
  300. db::execute();
  301. json::delete("salaries-proweb");
  302. db::query("TRUNCATE " . DB_T_EVENTS_INSCRITS);
  303. db::execute();
  304. db::query("TRUNCATE " . DB_T_EVENTS);
  305. db::execute();
  306. json::delete("events");
  307. db::query("TRUNCATE " . DB_T_EXCEL_PROWEB);
  308. db::execute();
  309. json::delete("excel-proweb");
  310. file::cleanAllFiles(SFTP_LOCAL);
  311. }
  312. public static function base64_url_encode(string $val)
  313. {
  314. return strtr(base64_encode($val), '+/=', '-_,');
  315. }
  316. public static function base64_url_decode(string $val)
  317. {
  318. return base64_decode(strtr($val, '-_,', '+/='));
  319. }
  320. public static function convertirEnUtf8(string $_texte)
  321. {
  322. if (!mb_detect_encoding($_texte, 'UTF-8', TRUE)) {
  323. return mb_convert_encoding($_texte, 'UTF-8', 'auto');
  324. } else {
  325. return $_texte;
  326. }
  327. }
  328. public static function printFormSelectOption(string $_string = NULL, $_value)
  329. {
  330. if ($_string != NULL and $_string == $_value) {
  331. echo " selected";
  332. }
  333. }
  334. public static function getFormValue(string $_string = NULL)
  335. {
  336. if ($_string != NULL) {
  337. return $_string;
  338. }
  339. }
  340. public static function printFormValue(string $_string = NULL)
  341. {
  342. if ($_string != NULL) {
  343. echo $_string;
  344. }
  345. }
  346. public static function convertBytes(float $val, string $type_val = "o", string $type_wanted = "Mo", bool $_float = FALSE)
  347. {
  348. $tab_val = array("o", "ko", "Mo", "Go", "To", "Po", "Eo");
  349. if (!(in_array($type_val, $tab_val) && in_array($type_wanted, $tab_val)))
  350. return 0;
  351. $tab = array_flip($tab_val);
  352. $diff = $tab[$type_val] - $tab[$type_wanted];
  353. $type_wanted_print = $_float == FALSE ? $type_wanted : NULL;
  354. if ($diff > 0)
  355. return round(($val * pow(1024, $diff)), 2) . $type_wanted_print;
  356. if ($diff < 0)
  357. return round(($val / pow(1024, -$diff)), 2) . $type_wanted_print;
  358. return round(($val), 2) . $type_wanted_print;
  359. }
  360. public static function getSizeDataBase(){
  361. db::query("SELECT
  362. table_schema AS nameDB,
  363. ROUND(SUM( data_length + index_length ) / 1024 / 1024, 2) AS moDB
  364. FROM information_schema.TABLES
  365. WHERE TABLE_SCHEMA = '".DB_NAME."'");
  366. return db::single();
  367. }
  368. public static function progressBarWarning(float $_num, float $_max, string $_label, string $_icon){
  369. $changeUnit = 1073741824;
  370. $valueUnitNum = $_num >= $changeUnit ? "Go" : "Mo";
  371. $valueUnitMax = $_max >= $changeUnit ? "Go" : "Mo";
  372. $pourcentage = number_format(($_num / $_max) * 100, 2);
  373. if($pourcentage < 50){
  374. $infos = ["color" => "bg-success"];
  375. } elseif($pourcentage < 75){
  376. $infos = ["color" => "bg-warning"];
  377. } else {
  378. $infos = ["color" => "bg-danger"];
  379. }
  380. echo ' <div class="mb-3" style="margin:10px 0;">
  381. <label class="form-label" style=""><i class="'.$_icon.'" style="font-size:18px; margin:4px;"></i> <span style="font-weight: bold;">'.$_label.'</span> [' . core::convertBytes($_num, "o", $valueUnitNum) . ' / ' . core::convertBytes($_max, "o", $valueUnitMax) . ']</label>
  382. <div class="progress" role="progressbar" aria-label="Success example" aria-valuenow="'.$pourcentage.'" aria-valuemin="0" aria-valuemax="100">
  383. <div class="progress-bar '.$infos["color"].'" style="width: '.$pourcentage.'%">'.$pourcentage.'%</div>
  384. </div>
  385. </div>';
  386. }
  387. }