core.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 formatEuro($_amount) {
  174. $amount = (float)$_amount;
  175. $formattedAmount = number_format($amount, 2, ',', ' ');
  176. return $formattedAmount . ' €';
  177. }
  178. public static function checkStringOnly(string $_string)
  179. {
  180. if (!ctype_alpha($_string)) {
  181. return TRUE;
  182. } else {
  183. return FALSE;
  184. }
  185. }
  186. public static function elementMenu(string $_id, string $_href, string $_titre, string $_style = NULL)
  187. {
  188. if (access::ifAccesss($_id)) {
  189. ($_style != NULL) ? $_style = ' style="' . $_style . '"' : NULL;
  190. echo '<li class="nav-item"><a class="nav-link' . get::currentPage($_id) . '" aria-current="page" href="' . $_href . '"' . $_style . '>';
  191. echo icon::getFont(["type" => $_id, "size" => "18px"]);
  192. echo ' ' . $_titre . '</a></li>';
  193. }
  194. }
  195. public static function elementMenuLink(string $_id, string $_href, string $_titre, string $_style = NULL, string $_target = "_blank")
  196. {
  197. if (access::ifAccesss($_id)) {
  198. ($_style != NULL) ? $_style = ' style="' . $_style . '"' : NULL;
  199. echo '<li class="nav-item"><a class="nav-link" target="' . $_target . '" href="' . $_href . '"' . $_style . '>';
  200. echo icon::getFont(["type" => $_id, "size" => "18px"]);
  201. echo ' ' . $_titre . '</a></li>';
  202. }
  203. }
  204. public static function elementMenuH6(string $_id, string $_titre, string $_style = NULL, string $_collapse = NULL)
  205. {
  206. if (access::ifAccesss($_id)) {
  207. ($_style != NULL) ? $_style = $_style : NULL;
  208. echo '<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
  209. <a style="text-decoration: none; ' . $_style . '" href="#' . $_collapse . '" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle text-dark">' . $_titre . '</a>
  210. </h6>';
  211. }
  212. }
  213. public static function filAriane(array $_arbo)
  214. {
  215. $return = '<nav aria-label="breadcrumb bg-300">';
  216. $return .= '<ol class="breadcrumb" style="padding:5px 10px; border-bottom: 1px solid #e9ecef;">';
  217. foreach ($_arbo["arbo"] as $label => $lien) {
  218. if ($_arbo["current"] == $label) {
  219. $return .= '<li class="breadcrumb-item active" aria-current="page">' . $label . '</li>';
  220. } elseif ($lien == NULL) {
  221. $return .= '<li class="breadcrumb-item">' . $label . '</li>';
  222. } else {
  223. $return .= '<li class="breadcrumb-item"><a href="' . $lien . '" title="' . $label . '">' . $label . '</a></li>';
  224. }
  225. }
  226. $return .= '</ol>';
  227. $return .= '</nav>';
  228. return $return;
  229. }
  230. public static function caculPourcentage(?int $_nombre, ?int $_total, int $_pourcentage = 100)
  231. {
  232. if ($_nombre == NULL) return 0;
  233. $resultat = ($_nombre / $_total) * $_pourcentage;
  234. return round($resultat);
  235. }
  236. public static function encodeUTF8(string $_data)
  237. {
  238. return (mb_detect_encoding($_data) != "UTF-8") ? mb_convert_encoding($_data, 'UTF-8', mb_list_encodings()) : $_data;
  239. }
  240. public static function testConnexionInternet()
  241. {
  242. $hosts = ['1.1.1.1', '1.0.0.1', '8.8.8.8', '8.8.4.4'];
  243. foreach ($hosts as $host) {
  244. if ($connected = @fsockopen($host, 443)) {
  245. fclose($connected);
  246. return TRUE;
  247. }
  248. }
  249. return FALSE;
  250. }
  251. public static function printDateTxt()
  252. {
  253. $date = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
  254. return $date->format(time()) . " à " . date("H:i:s");
  255. }
  256. public static function resetDatas()
  257. {
  258. db::query("TRUNCATE " . DB_T_TEMP_SALARIES);
  259. db::execute();
  260. json::delete("tmp_salaries");
  261. db::query("TRUNCATE " . DB_T_SALARIES);
  262. db::execute();
  263. json::delete("salaries");
  264. db::query("TRUNCATE " . DB_T_FILES);
  265. db::execute();
  266. file::cleanAllFiles(DIR_DATAS_FILES);
  267. db::query("TRUNCATE " . DB_T_EXCEL);
  268. db::execute();
  269. json::delete("excel");
  270. db::query("TRUNCATE " . DB_T_SALARIES_PROWEB);
  271. db::execute();
  272. json::delete("salaries-proweb");
  273. db::query("TRUNCATE " . DB_T_EVENTS_INSCRITS);
  274. db::execute();
  275. db::query("TRUNCATE " . DB_T_EVENTS);
  276. db::execute();
  277. json::delete("events");
  278. db::query("TRUNCATE " . DB_T_EXCEL_PROWEB);
  279. db::execute();
  280. json::delete("excel-proweb");
  281. file::cleanAllFiles(SFTP_LOCAL);
  282. }
  283. public static function base64_url_encode(string $val)
  284. {
  285. return strtr(base64_encode($val), '+/=', '-_,');
  286. }
  287. public static function base64_url_decode(string $val)
  288. {
  289. return base64_decode(strtr($val, '-_,', '+/='));
  290. }
  291. public static function convertirEnUtf8(string $_texte)
  292. {
  293. if (!mb_detect_encoding($_texte, 'UTF-8', TRUE)) {
  294. return mb_convert_encoding($_texte, 'UTF-8', 'auto');
  295. } else {
  296. return $_texte;
  297. }
  298. }
  299. public static function printFormSelectOption(string $_string = NULL, $_value)
  300. {
  301. if ($_string != NULL and $_string == $_value) {
  302. echo " selected";
  303. }
  304. }
  305. public static function getFormValue(string $_string = NULL)
  306. {
  307. if ($_string != NULL) {
  308. return $_string;
  309. }
  310. }
  311. public static function printFormValue(string $_string = NULL)
  312. {
  313. if ($_string != NULL) {
  314. echo $_string;
  315. }
  316. }
  317. public static function convertBytes(float $val, string $type_val = "o", string $type_wanted = "Mo", bool $_float = FALSE)
  318. {
  319. $tab_val = array("o", "ko", "Mo", "Go", "To", "Po", "Eo");
  320. if (!(in_array($type_val, $tab_val) && in_array($type_wanted, $tab_val)))
  321. return 0;
  322. $tab = array_flip($tab_val);
  323. $diff = $tab[$type_val] - $tab[$type_wanted];
  324. $type_wanted_print = $_float == FALSE ? $type_wanted : NULL;
  325. if ($diff > 0)
  326. return round(($val * pow(1024, $diff)), 2) . $type_wanted_print;
  327. if ($diff < 0)
  328. return round(($val / pow(1024, -$diff)), 2) . $type_wanted_print;
  329. return round(($val), 2) . $type_wanted_print;
  330. }
  331. public static function getSizeDataBase(){
  332. db::query("SELECT
  333. table_schema AS nameDB,
  334. ROUND(SUM( data_length + index_length ) / 1024 / 1024, 2) AS moDB
  335. FROM information_schema.TABLES
  336. WHERE TABLE_SCHEMA = '".DB_NAME."'");
  337. return db::single();
  338. }
  339. public static function progressBarWarning(float $_num, float $_max, string $_label, string $_icon){
  340. $changeUnit = 1073741824;
  341. $valueUnitNum = $_num >= $changeUnit ? "Go" : "Mo";
  342. $valueUnitMax = $_max >= $changeUnit ? "Go" : "Mo";
  343. $pourcentage = number_format(($_num / $_max) * 100, 2);
  344. if($pourcentage < 50){
  345. $infos = ["color" => "bg-success"];
  346. } elseif($pourcentage < 75){
  347. $infos = ["color" => "bg-warning"];
  348. } else {
  349. $infos = ["color" => "bg-danger"];
  350. }
  351. echo ' <div class="mb-3" style="margin:10px 0;">
  352. <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>
  353. <div class="progress" role="progressbar" aria-label="Success example" aria-valuenow="'.$pourcentage.'" aria-valuemin="0" aria-valuemax="100">
  354. <div class="progress-bar '.$infos["color"].'" style="width: '.$pourcentage.'%">'.$pourcentage.'%</div>
  355. </div>
  356. </div>';
  357. }
  358. static public function printBadgeGeneral(bool $_alerte){
  359. return $_alerte == TRUE ? '<span class="position-absolute start-100 translate-middle p-1 bg-danger border border-light rounded-circle"></span>' : NULL;
  360. }
  361. static public function ifbadge()
  362. {
  363. $return = [];
  364. $return["DOC"] = document::badgeAlert();
  365. if ($return["DOC"] != NULL) {
  366. $return["ALERTE"] = TRUE;
  367. } else {
  368. $return["ALERTE"] = FALSE;
  369. }
  370. return $return;
  371. }
  372. static public function menu(array $_navInfos){
  373. $badge = self::ifbadge();
  374. echo '<a href="/" style="box-shadow: none; background-color:'. $_navInfos["color"] .';" class="navbar-brand">' . $_navInfos["title"] . '</a>' . debug::getBadges();
  375. echo '<div id="navbarCollapse" class="collapse navbar-collapse p-0">';
  376. echo '<ul class="nav navbar-nav ms-auto">';
  377. echo '<li class="nav-item dropdown">';
  378. echo '<a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown">' . session::getName() . self::printBadgeGeneral($badge["ALERTE"]) . '</a>';
  379. echo '<div class="dropdown-menu dropdown-menu-end">';
  380. echo '<a href="/user.html" class="dropdown-item">Mon profil</a>';
  381. if((access::ifAccesss("documents") AND session::getType() == 5) OR session::getType() == 1){ // Membre du bureau ou Admin
  382. $nb = $badge["DOC"] > 0 ? '<span class="position-absolute badge rounded-pill bg-danger" style="right:-10px; margin-top:-10px;">' . $badge["DOC"] . '</span>' : NULL;
  383. echo '<a href="/documents-my-assign.html" class="dropdown-item">Vos assignations' . $nb . '</a>';
  384. echo '<a href="/alertes-emails.html" class="dropdown-item">Vos règles d\'alertes</a>';
  385. }
  386. echo '<div class="dropdown-divider"></div>';
  387. echo '<a href="/submit.php?from=logout" class="dropdown-item">Se déconnecter</a>';
  388. echo '</li>';
  389. echo '</div>';
  390. echo '</ul>';
  391. echo '</div>';
  392. }
  393. static public function getUserIP() {
  394. $ip = 'Inconnue';
  395. if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  396. // IP partagée par un proxy
  397. $ip = $_SERVER['HTTP_CLIENT_IP'];
  398. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  399. // IP du client derrière un proxy
  400. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  401. } else {
  402. // IP du client directement connectée au serveur
  403. $ip = $_SERVER['REMOTE_ADDR'];
  404. }
  405. // Nettoyage des IPs multiples dans le cas de 'HTTP_X_FORWARDED_FOR'
  406. if (strpos($ip, ',') !== false) {
  407. $ip = explode(',', $ip)[0];
  408. }
  409. return $ip;
  410. }
  411. }