/*==================================================== CREATE SEO FRIENDLY SLUG Supports English + Hindi + Unicode ====================================================*/ function createSlug($text) { $text = trim($text); // Remove HTML $text = strip_tags($text); // Convert multiple spaces to single $text = preg_replace('/\s+/', ' ', $text); // Replace spaces with hyphen $text = str_replace(' ', '-', $text); // Remove unwanted characters but keep Unicode (Hindi) $text = preg_replace('/[^\p{L}\p{N}\-]+/u', '', $text); // Remove duplicate hyphens $text = preg_replace('/-+/', '-', $text); // Trim hyphens $text = trim($text, '-'); // Lowercase English only if (function_exists('mb_strtolower')) { $text = mb_strtolower($text, 'UTF-8'); } else { $text = strtolower($text); } return $text; } /*==================================================== GENERATE UNIQUE SLUG ====================================================*/ function generateUniqueSlug($conn, $table, $slug, $ignoreId = 0) { $originalSlug = createSlug($slug); if ($originalSlug == "") { $originalSlug = "page"; } $newSlug = $originalSlug; $counter = 1; while (true) { if ($ignoreId > 0) { $stmt = $conn->prepare(" SELECT id FROM {$table} WHERE slug=? AND id!=? LIMIT 1 "); $stmt->bind_param("si", $newSlug, $ignoreId); } else { $stmt = $conn->prepare(" SELECT id FROM {$table} WHERE slug=? LIMIT 1 "); $stmt->bind_param("s", $newSlug); } $stmt->execute(); $result = $stmt->get_result(); if ($result->num_rows == 0) { break; } $newSlug = $originalSlug . "-" . $counter; $counter++; } return $newSlug; } /*==================================================== UPLOAD IMAGE ====================================================*/ function uploadImage($file, $uploadDir = "uploads/") { if (!isset($file) || $file['error'] != UPLOAD_ERR_OK) { return false; } // Auto Create Upload Folder if (!is_dir($uploadDir)) { mkdir($uploadDir, 0777, true); } $allowedExt = ['jpg', 'jpeg', 'png', 'webp']; $allowedMime = [ 'image/jpeg', 'image/png', 'image/webp' ]; $extension = strtolower( pathinfo($file['name'], PATHINFO_EXTENSION) ); if (!in_array($extension, $allowedExt)) { return false; } $mime = mime_content_type($file['tmp_name']); if (!in_array($mime, $allowedMime)) { return false; } // Max Size = 2MB if ($file['size'] > 2 * 1024 * 1024) { return false; } // Secure Random Name $newName = date("YmdHis") . "_" . bin2hex(random_bytes(4)) . "." . $extension; $destination = rtrim($uploadDir, "/") . "/" . $newName; if (move_uploaded_file($file['tmp_name'], $destination)) { return $newName; } return false; } /*==================================================== DELETE IMAGE ====================================================*/ function deleteImage($imageName, $uploadDir = "uploads/") { if (empty($imageName)) { return; } $file = rtrim($uploadDir, "/") . "/" . $imageName; if (file_exists($file)) { unlink($file); } } /*==================================================== IMAGE URL ====================================================*/ function imageUrl($imageName, $uploadDir = "uploads/") { if (empty($imageName)) { return $uploadDir . "no-image.png"; } return rtrim($uploadDir, "/") . "/" . $imageName; } /*==================================================== GET CATEGORY NAME ====================================================*/ function getCategoryName($conn, $categoryId) { $stmt = $conn->prepare(" SELECT name FROM categories WHERE id=? LIMIT 1 "); $stmt->bind_param("i", $categoryId); $stmt->execute(); $result = $stmt->get_result(); if($result->num_rows){ return $result->fetch_assoc()['name']; } return ""; } /*==================================================== GET CATEGORY ====================================================*/ function getCategory($conn, $categoryId) { $stmt = $conn->prepare(" SELECT * FROM categories WHERE id=? LIMIT 1 "); $stmt->bind_param("i",$categoryId); $stmt->execute(); return $stmt->get_result()->fetch_assoc(); } /*==================================================== GET PAGE BY SLUG ====================================================*/ function getPageBySlug($conn,$slug) { $stmt = $conn->prepare(" SELECT * FROM pages WHERE slug=? AND status='Publish' LIMIT 1 "); $stmt->bind_param("s",$slug); $stmt->execute(); return $stmt->get_result()->fetch_assoc(); } /*==================================================== GET PAGE BY ID ====================================================*/ function getPageById($conn,$id) { $stmt = $conn->prepare(" SELECT * FROM pages WHERE id=? LIMIT 1 "); $stmt->bind_param("i",$id); $stmt->execute(); return $stmt->get_result()->fetch_assoc(); } /*==================================================== GET RECENT PAGES ====================================================*/ function getRecentPages($conn,$limit=5) { $stmt = $conn->prepare(" SELECT * FROM pages WHERE status='Publish' ORDER BY id DESC LIMIT ? "); $stmt->bind_param("i",$limit); $stmt->execute(); return $stmt->get_result(); } /*==================================================== GET POPULAR PAGES ====================================================*/ function getPopularPages($conn,$limit=5) { $stmt = $conn->prepare(" SELECT * FROM pages WHERE status='Publish' ORDER BY views DESC LIMIT ? "); $stmt->bind_param("i",$limit); $stmt->execute(); return $stmt->get_result(); } /*==================================================== GET RELATED PAGES ====================================================*/ function getRelatedPages($conn,$categoryId,$pageId,$limit=6) { $stmt = $conn->prepare(" SELECT * FROM pages WHERE category_id=? AND id!=? AND status='Publish' ORDER BY RAND() LIMIT ? "); $stmt->bind_param( "iii", $categoryId, $pageId, $limit ); $stmt->execute(); return $stmt->get_result(); } /*==================================================== GET WEBSITE SETTING ====================================================*/ function getSetting($conn, $key) { $stmt = $conn->prepare(" SELECT setting_value FROM settings WHERE setting_key=? LIMIT 1 "); $stmt->bind_param("s", $key); $stmt->execute(); $result = $stmt->get_result(); if($result->num_rows){ return $result->fetch_assoc()['setting_value']; } return ""; } /*==================================================== WEBSITE NAME ====================================================*/ function siteName($conn) { return getSetting($conn,"site_name"); } /*==================================================== WEBSITE LOGO ====================================================*/ function siteLogo($conn) { return getSetting($conn,"site_logo"); } /*==================================================== WEBSITE EMAIL ====================================================*/ function siteEmail($conn) { return getSetting($conn,"site_email"); } /*==================================================== WEBSITE PHONE ====================================================*/ function sitePhone($conn) { return getSetting($conn,"site_phone"); } /*==================================================== FORMAT DATE ====================================================*/ function formatDate($date,$format="d M Y") { if(empty($date)){ return ""; } return date($format,strtotime($date)); } /*==================================================== SHORT EXCERPT ====================================================*/ function excerpt($text,$length=150) { $text = strip_tags($text); if(strlen($text)<=$length){ return $text; } return substr($text,0,$length)."..."; } /*==================================================== READING TIME ====================================================*/ function readingTime($content) { $words = str_word_count(strip_tags($content)); $minutes = ceil($words/200); return $minutes." min read"; } /*==================================================== PAGE VIEW COUNTER ====================================================*/ function updatePageViews($conn,$pageId) { $stmt = $conn->prepare(" UPDATE pages SET views=views+1 WHERE id=? "); $stmt->bind_param("i",$pageId); $stmt->execute(); } /*==================================================== GET TOTAL VIEWS ====================================================*/ function totalViews($page) { return number_format($page['views']); } /*==================================================== ACTIVE MENU ====================================================*/ function activeMenu($page, $currentPage) { return ($page == $currentPage) ? "active" : ""; } /*==================================================== STATUS BADGE ====================================================*/ function statusBadge($status) { if($status=="Publish"){ return 'Publish'; } return 'Draft'; } /*==================================================== CONVERT YOUTUBE URL TO EMBED ====================================================*/ function youtubeEmbed($url) { if(empty($url)){ return ""; } if(strpos($url,"watch?v=")!==false){ return str_replace( "watch?v=", "embed/", $url ); } if(strpos($url,"youtu.be/")!==false){ $id = basename($url); return "https://www.youtube.com/embed/".$id; } return $url; } /*==================================================== PAGE URL ====================================================*/ function pageUrl($slug) { return "page.php?slug=".urlencode($slug); } /*==================================================== CATEGORY URL ====================================================*/ function categoryUrl($id) { return "category.php?id=".$id; } /*==================================================== ASSET URL ====================================================*/ function asset($path) { return $path; } /*==================================================== ADMIN URL ====================================================*/ function adminUrl($path="") { return "admin/".$path; } /*==================================================== FRONTEND URL ====================================================*/ function frontendUrl($path="") { return $path; } /*==================================================== IMAGE URL ====================================================*/ function imagePath($image) { if(empty($image)){ return "uploads/no-image.png"; } return "uploads/".$image; } /*==================================================== CHECK LOGIN ====================================================*/ function isLoggedIn() { return isset($_SESSION['admin_id']); } /*==================================================== SAFE NUMBER FORMAT ====================================================*/ function numberFormat($number) { return number_format((int)$number); } /*==================================================== LIMIT TITLE ====================================================*/ function shortTitle($title,$length=50) { if(mb_strlen($title)<= $length){ return $title; } return mb_substr($title,0,$length)."..."; }