core.class.php 10 KB

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