core.class.php 12 KB

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