2
0

core.class.php 13 KB

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