2
0

core.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 "<pre>";
  167. print_r($_array);
  168. echo "</pre>";
  169. ($_exit != NULL) ? exit() : NULL;
  170. }
  171. public static function elementMenu(string $_id, string $_href, string $_titre, string $_feather, string $_style = NULL)
  172. {
  173. if (access::ifAccesss($_id)) {
  174. ($_style != NULL) ? $_style = ' style="' . $_style . '"' : NULL;
  175. echo '<li class="nav-item">
  176. <a class="nav-link' . get::currentPage($_id) . '" aria-current="page" href="' . $_href . '"' . $_style . '>
  177. <span data-feather="' . $_feather . '"' . $_style . '></span>
  178. ' . $_titre . '
  179. </a>
  180. </li>';
  181. }
  182. }
  183. public static function elementMenuLink(string $_id, string $_href, string $_titre, string $_feather, string $_style = NULL, string $_target = "_blank")
  184. {
  185. if (access::ifAccesss($_id)) {
  186. ($_style != NULL) ? $_style = ' style="' . $_style . '"' : NULL;
  187. echo '<li class="nav-item">
  188. <a class="nav-link" target="' . $_target . '" href="' . $_href . '"' . $_style . '>
  189. <span data-feather="' . $_feather . '"' . $_style . '></span>
  190. ' . $_titre . '
  191. </a>
  192. </li>';
  193. }
  194. }
  195. public static function elementMenuH6(string $_id, string $_titre, string $_style = NULL, string $_collapse = NULL)
  196. {
  197. if (access::ifAccesss($_id)) {
  198. ($_style != NULL) ? $_style = $_style : NULL;
  199. echo '<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
  200. <a style="text-decoration: none; ' . $_style . '" href="#' . $_collapse . '" data-toggle="collapse" aria-expanded="false" class="dropdown-toggle text-dark">' . $_titre . '</a>
  201. </h6>';
  202. }
  203. }
  204. public static function filAriane(array $_arbo)
  205. {
  206. $return = '<nav aria-label="breadcrumb bg-300">';
  207. $return .= '<ol class="breadcrumb" style="padding:5px 10px; border-bottom: 1px solid #e9ecef;">';
  208. foreach ($_arbo["arbo"] as $label => $lien) {
  209. if ($_arbo["current"] == $label) {
  210. $return .= '<li class="breadcrumb-item active" aria-current="page">' . $label . '</li>';
  211. } elseif ($lien == NULL) {
  212. $return .= '<li class="breadcrumb-item">' . $label . '</li>';
  213. } else {
  214. $return .= '<li class="breadcrumb-item"><a href="' . $lien . '" title="' . $label . '">' . $label . '</a></li>';
  215. }
  216. }
  217. $return .= '</ol>';
  218. $return .= '</nav>';
  219. return $return;
  220. }
  221. public static function caculPourcentage(?int $_nombre, ?int $_total, int $_pourcentage = 100)
  222. {
  223. if ($_nombre == NULL) return 0;
  224. $resultat = ($_nombre / $_total) * $_pourcentage;
  225. return round($resultat);
  226. }
  227. public static function encodeUTF8(string $_data)
  228. {
  229. return (mb_detect_encoding($_data) != "UTF-8") ? mb_convert_encoding($_data, 'UTF-8', mb_list_encodings()) : $_data;
  230. }
  231. public static function testConnexionInternet()
  232. {
  233. $hosts = ['1.1.1.1', '1.0.0.1', '8.8.8.8', '8.8.4.4'];
  234. foreach ($hosts as $host) {
  235. if ($connected = @fsockopen($host, 443)) {
  236. fclose($connected);
  237. return TRUE;
  238. }
  239. }
  240. return FALSE;
  241. }
  242. public static function printDateTxt()
  243. {
  244. $date = new IntlDateFormatter('fr_FR', IntlDateFormatter::LONG, IntlDateFormatter::NONE);
  245. return $date->format(time()) . " à " . date("H:i:s");
  246. }
  247. public static function addFileMaintenance()
  248. {
  249. $myfile = fopen(DOCUMENT_ROOT . FILE_MAINTENANCE, "w");
  250. fclose($myfile);
  251. }
  252. public static function removeFileMaintenance()
  253. {
  254. unlink(DOCUMENT_ROOT . FILE_MAINTENANCE);
  255. }
  256. public static function isMaintenance()
  257. {
  258. return (file_exists(DOCUMENT_ROOT . FILE_MAINTENANCE)) ? TRUE : FALSE;
  259. }
  260. public static function addFileDebug()
  261. {
  262. $myfile = fopen(DOCUMENT_ROOT . FILE_DEBUG, "w");
  263. fclose($myfile);
  264. }
  265. public static function removeFileDebug()
  266. {
  267. unlink(DOCUMENT_ROOT . FILE_DEBUG);
  268. }
  269. public static function isDebug()
  270. {
  271. return (file_exists(DOCUMENT_ROOT . FILE_DEBUG)) ? TRUE : FALSE;
  272. }
  273. public static function resetDatas()
  274. {
  275. db::query("TRUNCATE " . DB_T_TEMP_SALARIES);
  276. db::execute();
  277. json::delete("tmp_salaries");
  278. db::query("TRUNCATE " . DB_T_SALARIES);
  279. db::execute();
  280. json::delete("salaries");
  281. db::query("TRUNCATE " . DB_T_FILES);
  282. db::execute();
  283. file::cleanAllFiles(DIR_DATAS_FILES);
  284. db::query("TRUNCATE " . DB_T_EXCEL);
  285. db::execute();
  286. json::delete("excel");
  287. db::query("TRUNCATE " . DB_T_SALARIES_PROWEB);
  288. db::execute();
  289. json::delete("salaries-proweb");
  290. db::query("TRUNCATE " . DB_T_EVENTS_INSCRITS);
  291. db::execute();
  292. db::query("TRUNCATE " . DB_T_EVENTS);
  293. db::execute();
  294. json::delete("events");
  295. db::query("TRUNCATE " . DB_T_EXCEL_PROWEB);
  296. db::execute();
  297. json::delete("excel-proweb");
  298. file::cleanAllFiles(SFTP_LOCAL);
  299. }
  300. public static function base64_url_encode(string $val)
  301. {
  302. return strtr(base64_encode($val), '+/=', '-_,');
  303. }
  304. public static function base64_url_decode(string $val)
  305. {
  306. return base64_decode(strtr($val, '-_,', '+/='));
  307. }
  308. public static function convertirEnUtf8(string $_texte)
  309. {
  310. if (!mb_detect_encoding($_texte, 'UTF-8', TRUE)) {
  311. return mb_convert_encoding($_texte, 'UTF-8', 'auto');
  312. } else {
  313. return $_texte;
  314. }
  315. }
  316. public static function printFormSelectOption(string $_string = NULL, $_value)
  317. {
  318. if ($_string != NULL and $_string == $_value) {
  319. echo " selected";
  320. }
  321. }
  322. public static function printFormValue(string $_string = NULL)
  323. {
  324. if ($_string != NULL) {
  325. echo $_string;
  326. }
  327. }
  328. public static function convertBytes($val, $type_val = "o", $type_wanted = "Mo")
  329. {
  330. $tab_val = array("o", "ko", "Mo", "Go", "To", "Po", "Eo");
  331. if (!(in_array($type_val, $tab_val) && in_array($type_wanted, $tab_val)))
  332. return 0;
  333. $tab = array_flip($tab_val);
  334. $diff = $tab[$type_val] - $tab[$type_wanted];
  335. if ($diff > 0)
  336. return round(($val * pow(1024, $diff)), 2) . $type_wanted;
  337. if ($diff < 0)
  338. return round(($val / pow(1024, -$diff)), 2) . $type_wanted;
  339. return round(($val), 2) . $type_wanted;
  340. }
  341. public static function getSizeDataBase(){
  342. db::query("SELECT
  343. table_schema AS nameDB,
  344. ROUND(SUM( data_length + index_length ) / 1024 / 1024, 2) AS moDB
  345. FROM information_schema.TABLES
  346. WHERE TABLE_SCHEMA = '".DB_NAME."'");
  347. return db::single();
  348. }
  349. }