namespace APP\Libraries; class MainHandler { public function sanitizeInput($inputData) { $inputData = trim($inputData); $inputData = stripslashes($inputData); $inputData = htmlspecialchars($inputData); return $inputData; } public function validDate($date, $format = "Y-m-d H:i:s") { $d = \DateTime::createFromFormat($format, $date); return $d && $d->format($format) == $date; } public function intDate($date) { $dateObj = \DateTime::createFromFormat("Y-m-d", $date); return $dateObj->format("Ymd"); } public function currentTime() { return date("Y-m-d H:i:s"); } public function currentDate() { return date("Y-m-d"); } public function currentMonth() { return date("Y-m"); } public function timeDiff($startDate, $endDate) { $startTimestamp = strtotime($startDate); $endTimestamp = strtotime($endDate); $diff = abs($endTimestamp - $startTimestamp); if ($diff > 3600) { if ($diff % 3600 != 0) { $hrs = (int) ($diff / 3600); $rem = $diff % 3600; $mins = $rem / 3600 * 60; return $hrs . "hrs " . $mins . "mins"; } else { return $diff / 3600 . "hrs"; } } else { return $diff / 3600 * 60 . "mins"; } } public function dateDiff($startDate, $endDate, $format, $computeAge = null) { $dateObj = \DateTime::createFromFormat($format, $startDate); $startDate = $dateObj->format("d-m-Y"); $dateObj = \DateTime::createFromFormat($format, $endDate); $endDate = $dateObj->format("d-m-Y"); $datetime1 = date_create($startDate); $datetime2 = date_create($endDate); $interval = date_diff($datetime1, $datetime2); if (isset($computeAge)) { return $interval->format("%y yrs, %m months"); } else { return $interval->format("%R%a"); } } public function toRFC3339($inputDate) { $datetime = \DateTime::createFromFormat("Y-m-d H:i", $inputDate); return $datetime->format(\DateTime::RFC3339); } public function convertDate($date, $oldFormat, $newFormat) { $dateObj = \DateTime::createFromFormat($oldFormat, $date); return $dateObj->format($newFormat); } public function hashPassword($password) { return password_hash($password, PASSWORD_DEFAULT); } function futureDate($timeFrame) { return date("Y-m-d", strtotime($timeFrame)); } function addTime($selectedTime, $duration) { return date("H:i", strtotime($selectedTime . $duration)); } public function randomColor() { return "#" . str_pad(dechex(mt_rand(0, 16777215)), 6, "0", STR_PAD_LEFT); } public function exportDatabase() { $conn = new \mysqli($_SERVER["database.default.hostname"], $_SERVER["database.default.username"], $_SERVER["database.default.password"], $_SERVER["database.default.database"]); $conn->set_charset("utf8"); $tables = array(); $sql = "SHOW TABLES"; $result = mysqli_query($conn, $sql); while ($row = mysqli_fetch_row($result)) { $tables[] = $row[0]; } $sqlScript = ''; foreach ($tables as $table) { $query = "SHOW CREATE TABLE {$table}"; $result = mysqli_query($conn, $query); $row = mysqli_fetch_row($result); $sqlScript .= "
" . $row[1] . ";\xa\xa"; $query = "SELECT * FROM {$table}"; $result = mysqli_query($conn, $query); $columnCount = mysqli_num_fields($result); for ($i = 0; $i < $columnCount; $i++) { while ($row = mysqli_fetch_row($result)) { $sqlScript .= "INSERT INTO {$table} VALUES("; for ($j = 0; $j < $columnCount; $j++) { $row[$j] = $row[$j]; if (isset($row[$j])) { $sqlScript .= """ . $row[$j] . """; } else { $sqlScript .= """"; } if ($j < $columnCount - 1) { $sqlScript .= ","; } } $sqlScript .= ");
"; } } $sqlScript .= "
"; } if (!empty($sqlScript)) { $backup_name = $_SERVER["database.default.database"] . "_db_backup_" . str_replace(":", "-", $this->currentTime()) . ".sql"; $path = UPLOAD_PATH["backup"] . $backup_name; $fileHandler = fopen($path, "w+"); $number_of_lines = fwrite($fileHandler, $sqlScript); fclose($fileHandler); return array("backup_name" => $backup_name, "path" => $path); } else { return null; } } public function importDatabase($filePath) { $conn = new \mysqli($_SERVER["database.default.hostname"], $_SERVER["database.default.username"], $_SERVER["database.default.password"], $_SERVER["database.default.database"]); $query = $error = ''; $sqlScript = file($filePath); foreach ($sqlScript as $line) { $startWith = substr(trim($line), 0, 2); $endWith = substr(trim($line), -1, 1); if (empty($line) || $startWith == "--" || $startWith == "/*" || $startWith == "//") { continue; } $query = $query . $line; if ($endWith == ";") { $result = mysqli_query($conn, $query); if (!$result) { $error .= mysqli_error($conn) . "\xa"; } $query = ''; } } if ($error) { return false; } else { return true; } } public function userAllowed($perm) { if (!empty($_SESSION["nbcCurrentUserDetails"])) { if ($_SESSION["nbcCurrentUserDetails"]["data"]["users_active"] == 1) { $permissions = $_SESSION["nbcCurrentUserDetails"]["data"]["user_permissions"] . $_SESSION["nbcCurrentUserDetails"]["data"]["role_permissions"]; if ($_SESSION["nbcCurrentUserDetails"]["data"]["user_type"] == 1 || !empty($permissions) && strpos($permissions, $perm) !== false) { return true; } else { return false; } } else { return false; } } else { return false; } } public function isImage($filePath) { $imageExtensions = array("jpg", "jpeg", "png", "gif", "bmp"); $fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION)); return in_array($fileExtension, $imageExtensions); } }
© 2023 Quttera Ltd. All rights reserved.