<?php

	if(route(1)){

		$url = route(1);
		$page = $db->from('pages')->where('page_url', $url)->first();

		if($page){

			if($page['page_visibility'] == 1){

				$seo = json_decode($page['page_seo'], true);

				$meta = [
					"title" => $seo['title'],
					"desc" 	=> $seo['desc'],
					"keyw" 	=> $seo['keyw']
				];

				$title = $page['page_title'];

				require view('page');

			}elseif($page['page_visibility'] == 2){

				if(session('user_id')){

					$seo = json_decode($page['page_seo'], true);

					$meta = [
						"title" => $seo['title'],
						"desc" 	=> $seo['desc'],
						"keyw" 	=> $seo['keyw']
					];

					$title = $page['page_title'];

					require view('page');

				}else{

					header('Location:' . site());

				}

			}elseif($page['page_visibility'] == 3){

				header('Location:' . site());

			}

			

		}else{

			header('Location:'.site());

		}

	}else{

		header('Location:'.site());
	
	}
	$extra_sql = "";
	$integration = "";
	foreach($plugins as $plugin){
		if ($plugin['app_id'] !== 1 && $plugin['app_id'] !== 2 && $plugin['app_id'] !== 3 && $plugin['app_id'] !== 4 && $plugin['app_id'] !== 5) {
			@$data = json_decode($plugin['app_data'], true);
			if(@$data['visibility'] == 0){ // Tüm Sayfalar
				@$integration .= $data['code'];
			}elseif(@$data['visibility'] == 1){ // Giriş Yapanlar
				@$integration .= $data['code'];
			}elseif(@$data['visibility'] == 2){
				@$integration .= $data['code'];
			}
		}
	}
	$output = ob_get_clean();
	$extra = [
		"{INTEGRATIONS}" => $integration,
		"{PAGE_CONTENT}" => htmlspecialchars_decode($page['page_content']),
		"{PAGE_TITLE}" => $page['page_title']
	];
	echo replace($output, $extra);