<?php require_once "inc/start.php";?>
    <?php
        if (isset($_SESSION['user']['hzu_userid']) !== false) {
            define("userFindSession", true);
        }else{
            define("userFindSession", false);
        }

        if (isset($_GET['q'])) {
            $do = g('q');
        }else{
            $do = '';
        }

        if (isset($_GET['s'])) {
            $s = g('s');
        }

        if (isset($_GET['id'])) {
            $id = g('id');
        }

        if (isset($_GET['p'])) {
            $p = g('p');
        }

        if (isset($_GET['text'])) {
            $t = g('text');
        }
       
        // HTTPS ve WWW zorunlu hale getirme + Büyük harfleri küçültme
        $parsed_url = parse_url($_SERVER['REQUEST_URI'])['path'];
        $query_url = parse_url($_SERVER['REQUEST_URI'])['query'];
        $request_uri = rawurldecode($parsed_url);  // URL'yi decode et
        $scheme = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http';
        $host = $_SERVER['HTTP_HOST'];

        // Eğer "www" yoksa ekleyelim
       

        $parsed_url_dev = rawurldecode(strtok($_SERVER['REQUEST_URI'], '?'));
        
        // Birden fazla "/" karakterini tek "/" haline getir
        $request_uri = preg_replace('/\/+/', '/', $parsed_url_dev);

        // URL'nin sonunda / varsa kaldıralım (ana dizin hariç)
        $trailing_slash = false;
        if (strlen($request_uri) > 1 && substr($request_uri, -1) === '/') {
            $request_uri = rtrim($request_uri, '/');
            $trailing_slash = true;
        }

        $redirectBool = false;
    
        // Eğer URL değişmişse yönlendirme yap
        if ($request_uri !== $parsed_url_dev) {
            $correct_url = "https://$host$request_uri";
    
            $query_url = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
            if ($query_url) {
                $correct_url .= '?' . $query_url;
            }
            $redirectBool = true;
            
        }
        
        // Büyük harfleri küçük harfe çevirelim
        $lowercase_uri = mb_strtolower($request_uri, 'UTF-8');
        $lowercase_uri = str_replace(['İ', 'İ', 'i̇'], 'i', $lowercase_uri);

        // Doğru olması gereken URL
        $correct_url = "https://$host$lowercase_uri";

        // Eğer mevcut URL yanlışsa veya sonunda / varsa yönlendir
        $current_url = "$scheme://$_SERVER[HTTP_HOST]". $request_uri;
        
        if (($current_url !== $correct_url) || $trailing_slash) {
            $redirectBool = true;
            if ($query_url) {
                $correct_url .= '?' . $query_url;
            }
        }

        // 301 yönlendirme için veritabanı kontrolü
        $url = $correct_url;
        $routes301Find = $db->prepare("SELECT * FROM routes_301 WHERE source_url = ? AND is_active = '1' AND status = '1' ORDER BY id DESC");
        $routes301Find->execute([$url]);
        $routes301Find = $routes301Find->fetch();
        if ($_SESSION['enveironment'] == 'development'){
           
        }
        if ($routes301Find) {
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: ' . $routes301Find->target_url);
            exit;
        }elseif ($redirectBool) {
            header('HTTP/1.1 301 Moved Permanently');
            header('Location: ' . $correct_url);
            exit;
        }
        
        if ($do && $s && !$id) {
            $servicesIndexAltCategoriesFind = $db->query("SELECT * FROM the_content WHERE tip = 'alt-services-categories' AND link = '$do' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
            $servicesIndexFind = $db->query("SELECT * FROM the_content WHERE tip = 'services' AND link = '$s' AND dil = '$selectLangCode' AND durum = '1' AND country = '$servicesIndexAltCategoriesFind->icerik_id' ")->fetch();
            if ($servicesIndexAltCategoriesFind && $servicesIndexFind) {
                $do = 'services-detail';
                $s = $servicesIndexAltCategoriesFind->link;
                $id = $servicesIndexFind->link;
            }
        }elseif ($do && !$s && !$id) {
            $servicesIndexAltCategoriesFind = $db->query("SELECT * FROM the_content WHERE tip = 'alt-services-categories' AND link = '$do' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
            if ($servicesIndexAltCategoriesFind) {
                $do = 'services-category';
                $s = $servicesIndexAltCategoriesFind->link;
            }else{
                $glassHouseLPFind = $db->query("SELECT * FROM the_content WHERE tip = 'glasshouse-lp' AND link = '$do' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
                if ($glassHouseLPFind) {
                    $do = 'glasshouse-lp';
                    $s = $glassHouseLPFind->link;
                    $id = $glassHouseLPFind->id;
                }
            }
        }
        $pachFind = pachFind($do);
        if ($pachFind == 'pages/services/service') {
            if ($id && $p) {
                $servicesIndexTopCategoriesFind = $db->query("SELECT * FROM the_content WHERE tip = 'services-categories' AND link = '$s' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
                $servicesIndexAltCategoriesFind = $db->query("SELECT * FROM the_content WHERE tip = 'alt-services-categories' AND link = '$id' AND dil = '$selectLangCode' AND durum = '1' AND kat_id = '$servicesIndexTopCategoriesFind->icerik_id' ")->fetch();
               
                $servicesIndexFind = $db->query("SELECT * FROM the_content WHERE tip = 'services' AND link = '$p' AND dil = '$selectLangCode' AND durum = '1' AND country = '$servicesIndexAltCategoriesFind->icerik_id' ")->fetch();
                if ($servicesIndexTopCategoriesFind && $servicesIndexAltCategoriesFind && $servicesIndexFind) {
                    $pachFind = 'pages/services/services-detail-new';
                }
            }elseif ($id) {
                $servicesIndexTopCategoriesFind = $db->query("SELECT * FROM the_content WHERE tip = 'services-categories' AND link = '$s' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
                $servicesIndexAltCategoriesFind = $db->query("SELECT * FROM the_content WHERE tip = 'alt-services-categories' AND link = '$id' AND dil = '$selectLangCode' AND durum = '1' AND kat_id = '$servicesIndexTopCategoriesFind->icerik_id' ")->fetch();
                if ($servicesIndexTopCategoriesFind && $servicesIndexAltCategoriesFind) {
                    $pachFind = 'pages/services/services-category-new';
                }
            }
        }
        $legalPageFind = $db->query("SELECT * FROM the_content WHERE tip = 'page' AND icerik_id = '20241b98c9' AND dil = '$selectLangCode' AND durum = '1' AND link = '$do' ")->fetch();
        if ($legalPageFind) {
            $pachFind = 'pages/page/legal-new';
            $do = 'legal';
            $s = $legalPageFind->link;
        }
        if ($pachFind == 'pages/page/404') {
            $do = $do . '/' . $s;
            $pachFind = pachFind($do);
        }
        if ($pachFind == 'pages/corporate/customer-experience-new' && $s && !$id) {
            $customerExperienceFind = $db->query("SELECT * FROM the_content WHERE tip = 'customer-experience' AND link = '$s' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
            if ($customerExperienceFind) {
                $pachFind = 'pages/corporate/customer-experience-detail-new';
            }else{
                header('HTTP/1.1 404 Not Found');
                $pachFind = 'pages/page/404';
            }
        }elseif ($pachFind == 'pages/corporate/customer-experience-new' && $s && $id) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/information-center/news-new' && $s && !$id) {
            $newsFind = $db->query("SELECT * FROM the_content WHERE tip = 'news' AND link = '$s' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
            if ($newsFind) {
                $pachFind = 'pages/information-center/news-detail-new';
            }else{
                header('HTTP/1.1 404 Not Found');
                $pachFind = 'pages/page/404';
            }
        }elseif ($pachFind == 'pages/information-center/news-new' && $s && $id) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/information-center/events-new' && $s && !$id) {
            $eventsFind = $db->query("SELECT * FROM the_content WHERE tip = 'events' AND link = '$s' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
            if ($eventsFind) {
                $pachFind = 'pages/information-center/events-detail-new';
            }else{
                header('HTTP/1.1 404 Not Found');
                $pachFind = 'pages/page/404';
            }
        }elseif ($pachFind == 'pages/information-center/events-new' && $s && $id) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/information-center/blog-new' && $s && !$id) {
            $blogCategories = $db->query("SELECT * FROM the_categories WHERE kat_tip = '26' AND dil = '$selectLangCode' AND link = '$s' AND durum = '1' ORDER BY sira ASC, id ASC")->fetch();
            if ($blogCategories) {
                $pachFind = 'pages/information-center/blog-new';
            }else{
                $blogFind = $db->query("SELECT * FROM the_content WHERE tip = 'blog' AND link = '$s' AND dil = '$selectLangCode' AND durum = '1' AND ust_by = '1' ")->fetch();
                if ($blogFind) {
                    $blogCategories = $db->query("SELECT * FROM the_categories WHERE kat_tip = '26' AND dil = '$selectLangCode' AND icerik_id = '$blogFind->kat_id' AND durum = '1' ORDER BY sira ASC, id ASC")->fetch();
                    if ($blogCategories) {
                        header('HTTP/1.1 301 Moved Permanently');
                        header('Location: ' . targetLinkFunc($languageArray['default']['code'], $systemLink[8][$selectLangCode],$selectLangCode,'',$blogCategories->link.'/'.$blogFind->link,'',''));
                        exit;
                    }else{
                        header('HTTP/1.1 404 Not Found');
                        $pachFind = 'pages/page/404'; 
                    }
                }else{
                    header('HTTP/1.1 404 Not Found');
                    $pachFind = 'pages/page/404';
                }
            }
        }elseif ($pachFind == 'pages/information-center/blog-new' && $s && $id) {
            $blogCategories = $db->query("SELECT * FROM the_categories WHERE kat_tip = '26' AND dil = '$selectLangCode' AND link = '$s' AND durum = '1' ORDER BY sira ASC, id ASC")->fetch();
            $blogFind = $db->query("SELECT * FROM the_content WHERE tip = 'blog' AND link = '$id' AND dil = '$selectLangCode' AND durum = '1' AND ust_by = '1' ")->fetch();
            if ($blogFind) {
                if (!$blogCategories) {
                    $realBlogCategoriesFind = $db->query("SELECT * FROM the_categories WHERE kat_tip = '26' AND dil = '$selectLangCode' AND icerik_id = '$blogFind->kat_id' AND durum = '1' ORDER BY sira ASC, id ASC")->fetch();
                    if ($realBlogCategoriesFind) {
                        $blogCategories = $realBlogCategoriesFind;
                    }
                    header('HTTP/1.1 301 Moved Permanently');
                    header('Location: ' . targetLinkFunc($languageArray['default']['code'], $systemLink[8][$selectLangCode],$selectLangCode,'',$blogCategories->link.'/'.$blogFind->link,'',''));
                    exit;
                }
                $pachFind = 'pages/information-center/blog-detail-new';
            }else{
                header('HTTP/1.1 404 Not Found');
                $pachFind = 'pages/page/404';
            }
        }elseif ($pachFind == 'pages/information-center/blog-new' && $s && $id && !$p) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/information-center/documents-new' && $s && !$id) {
            $documentsFind = $db->query("SELECT * FROM the_content WHERE tip = 'documents' AND link = '$s' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
            if ($documentsFind) {
                $pachFind = 'pages/information-center/documents-detail-new';
            }else{
                header('HTTP/1.1 404 Not Found');
                $pachFind = 'pages/page/404';
            }
        }elseif ($pachFind == 'pages/information-center/documents-new' && $s && $id) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/dictionary/dictionary' && $do && $s && !$id && !$p) {
            $turkishToLatin = [
                'Ç' => 'c', 'Ğ' => 'g', 'İ' => 'i', 'Ö' => 'o', 'Ş' => 's', 'Ü' => 'u',  'i̇' => 'i',
                'ç' => 'c', 'ğ' => 'g', 'ı' => 'i', 'ö' => 'o', 'ş' => 's', 'ü' => 'u',  'i̇' => 'i',
            ];
            $allAlphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'ı', 'ğ', 'ü', 'ö', 'ş', 'i̇'];
            $letter = mb_strtolower($s, 'UTF-8');
            $letter = strtr($letter, $turkishToLatin);
           
            if ($s !== mb_strtolower($s, 'UTF-8')) {
                if (mb_strtolower($s, 'UTF-8') != $letter) {
                    $s = $letter;
                }
                header('HTTP/1.1 301 Moved Permanently');
                if ($languageAll['default']['code'] == $selectLangCode) {
                    header('Location: ' . $site_url . $do . '/' . mb_strtolower($s, 'UTF-8') . '/' . $id);
                }else{
                    header('Location: ' . $site_url . $selectLangCode . '/' . $do . '/' . mb_strtolower($s, 'UTF-8') . '/' . $id);
                }
                exit;
            }elseif ($s != $letter) {
                $s = $letter;
                header('HTTP/1.1 301 Moved Permanently');
                if ($languageAll['default']['code'] == $selectLangCode) {
                    header('Location: ' . $site_url . $do . '/' . mb_strtolower($s, 'UTF-8') . '/' . $id);
                }else{
                    header('Location: ' . $site_url . $selectLangCode . '/' . $do . '/' . mb_strtolower($s, 'UTF-8') . '/' . $id);
                }
                exit;
            }elseif (!in_array($s, $allAlphabet)) {
                header('HTTP/1.1 404 Not Found');
                $pachFind = 'pages/page/404';
            }
        }elseif ($pachFind == 'pages/dictionary/dictionary-new' && $do && $s && $id && !$p) {
            $turkishToLatin = [
                'Ç' => 'c', 'Ğ' => 'g', 'İ' => 'i', 'Ö' => 'o', 'Ş' => 's', 'Ü' => 'u',  'i̇' => 'i',
                'ç' => 'c', 'ğ' => 'g', 'ı' => 'i', 'ö' => 'o', 'ş' => 's', 'ü' => 'u',  'i̇' => 'i',
            ];
            $allAlphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'ı', 'ğ', 'ü', 'ö', 'ş', 'i̇'];
            $letter = mb_strtolower($s, 'UTF-8');
            $letter = strtr($letter, $turkishToLatin);
        
            if ($s !== mb_strtolower($s, 'UTF-8')) {
                if (mb_strtolower($s, 'UTF-8') != $letter) {
                    $s = $letter;
                }
                header('HTTP/1.1 301 Moved Permanently');
                if ($languageAll['default']['code'] == $selectLangCode) {
                    header('Location: ' . $site_url . $do . '/' . mb_strtolower($s, 'UTF-8') . '/' . $id);
                }else{
                    header('Location: ' . $site_url . $selectLangCode . '/' . $do . '/' . mb_strtolower($s, 'UTF-8') . '/' . $id);
                }
                exit;
            }elseif ($s != $letter) {
                $s = $letter;
                header('HTTP/1.1 301 Moved Permanently');
                if ($languageAll['default']['code'] == $selectLangCode) {
                    header('Location: ' . $site_url . $do . '/' . mb_strtolower($s, 'UTF-8') . '/' . $id);
                }else{
                    header('Location: ' . $site_url . $selectLangCode . '/' . $do . '/' . mb_strtolower($s, 'UTF-8') . '/' . $id);
                }
                exit;
            }
            $dictionaryFind = $db->query("SELECT * FROM the_content WHERE tip = 'dictionary' AND link = '$id' AND dil = '$selectLangCode' AND durum = '1' ")->fetch();
            if ($dictionaryFind) {
                $pachFind = 'pages/dictionary/dictionary-detail-new';
            }else{
                header('HTTP/1.1 404 Not Found');
                $pachFind = 'pages/page/404';
            }
        }elseif ($pachFind == 'pages/dictionary/dictionary-new' && $id && $s && $id && $p) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/information-center/videos-new' && $s) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/page/contact-new' && $s) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/page/data-center-status-new' && $s) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/corporate/career-new' && $s) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/corporate/why-us-new' && $s) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/corporate/about-new' && $s) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }elseif ($pachFind == 'pages/page/sitemap-new' && $s) {
            header('HTTP/1.1 404 Not Found');
            $pachFind = 'pages/page/404';
        }
        
        $activePageArray = explode('/', $pachFind);
        if (file_exists("{$pachFind}.php")) {
            $activePachFindArray = explode('/', $pachFind);
            if ($pachFind == 'pages/page/404') {
                header('HTTP/1.1 404 Not Found');
                require("{$pachFind}.php");
            }else{
                require("{$pachFind}.php");
            }
        }else {
            $pachFind = pachFind($do);
            require("{$pachFind}.php");
        }

    ?>

<?php require_once "inc/body-end.php"; ob_end_flush(); ?>
