<?php 
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING ^ E_DEPRECATED ^ E_STRICT); 
$rootPath = is_file("../../../../library/document.php") ? "../../../../" : "../../";
define("CMS", "korCMS");
include($rootPath."config/odyssey.ini.php");
include($rootPath."library/db.php");
include($rootPath."library/system.php");
include($rootPath."library/messages.php");
include($rootPath."library/variables.php");
include($rootPath."library/language.php");
include($rootPath."library/parameters.php");
include($rootPath."library/content.php");
include($rootPath."library/admin_pagination.php");
include($rootPath."library/fileSystem.php");
include($rootPath."library/imageResize.php");
include($rootPath."library/authentication.php");
include($rootPath."library/sef.php");
$sef = new sef();
$db = new db();
$system = new system();
$messages = new messages();
$variables = new variables();
$language = new language();
$parameters = new parameters();
$content = new content();
$content = new admin_pagination();
$fileSystem = new fileSystem();
$imageResize = new imageResize();
$authentication = new authentication(false, true);
$db->query("SET NAMES 'utf8mb4'");

function getFPath(){
	$parts = explode("/", $_SERVER["PHP_SELF"]);
	array_shift($parts); // the first element will be empty to we get rid of it
	array_pop($parts); // get rid of script name
	$fPath =implode("/", $parts);
	if(substr($fPath, -1) != "/") $fPath.="/";
	$fPath = ($fPath == "/") ? null : $fPath;	
	return $fPath;	
}

$langID = ($_GET['langID']) ? (int)$_GET['langID'] : $language->getDefaultLanguage();
$Lsufix = ((int)$langID > 1) ? "_translated" : NULL;
$variables->getSafeVars(array_keys($_REQUEST));

$token1 = "38629649";
$token2 = "73972658";
list($stage1, $token2b) = explode($token2, $_REQUEST['u']);
list($token1b, $userID) = explode($token1, $stage1);
if(empty($userID)) die("Access denied");

// lets check if the section's record belongs to this user
$section = $system->getTableSectionName($sectionID);
$uid = $db->getRecord("select userID from $section where id='$linkID'")->userID;
if($uid <> $userID) die("Access denied");


$parameters->importParameters();
$isMultilingualSite = $language->isMultilingualSite();
if($action=="add" && $youtubeCode && $linkID && $sectionID && $writedata){ 
	if(preg_match('/v=/',$youtubeCode)){
		preg_match('/v=(.{11})/', $youtubeCode, $matches);
		$youtubeCode = (mb_strlen($matches[1], 'utf-8') == 11) ? $matches[1] : $youtubeCode;
	}
	if(preg_match('/youtu.be/',$youtubeCode)){
		preg_match('/youtu.be\/(.{11})/', $youtubeCode, $matches);
		$youtubeCode = (mb_strlen($matches[1], 'utf-8') == 11) ? $matches[1] : $youtubeCode;
	}	

	if(mb_strlen($youtubeCode, 'utf-8') <> 11){ $messages->addError(t("Δεν ορίσατε αποδεκτό YouTube κωδικό ή URL"));}

	if(!$messages->hasErrors){
		$title_gr = (empty($title_gr)) ? $youtubeCode : $title_gr;
		$alias = $variables->toLatin($title_gr);
		$sort = 0;
		$db->query("INSERT INTO youtube VALUES('','1','0','$alias','$sort','$youtubeCode','$linkID', '$sectionID')");
		$mainID=$db->lastID();
		if((int)$mainID){
			$Lresult = $db->query("SELECT * FROM languages WHERE active=1 ");
			$LRecords = $db->getRows($Lresult);
			foreach($LRecords as $Lrow) {
				$extension = ($Lrow->extension) ? $Lrow->extension : '_gr';
				$languageID = $Lrow->id;
				$title = str_replace('"', '&#034;', str_replace("'", "&#039;", ${'title'.$extension}));
				$title = (empty($title)) ? $title_gr : $title;
				$keys="id,mainID,langID,title";
				$values="'','$mainID','$languageID','$title'";
				if($title){
					$query = "INSERT INTO content_youtube (".$keys.") VALUES(".$values.")";
					$db->query($query);
				}			
			}
		}
		
echo '		<script type="text/javascript">
		parent.$("#youtube_instructions").css("display","block");
		parent.youtubeVideos.location.reload();
		parent.$.fancybox.close();
		</script>'; 
		exit;
	}
}

function t($key, $langID=NULL, $context=NULL){ // shortcut of $language->getText($key, $langID=NULL, $context=NULL)
	global $language;
	global $langID;
	return $language->getText($key, $langID, $context);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="<?php echo $rootPath;?>admin/css/admin.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="<?php echo $rootPath;?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo $rootPath;?>admin/js/admin.js"></script>
<script>
jQuery(function($){
	$('#loading').css("display", "none");
});
</script>
<style  type="text/css">
<!--
body {
	margin:0;
	overflow: auto;
	background: #ededed;	
	font-family: Verdana, sans-serif, Arial;
	font-size:11px;	
}
.section_title{
	font-family: 'Roboto Condensed', sans-serif;
	font-size:1.4em;	
	margin: 0;
}
-->
</style>
</head>
<body>

<table width="100%" cellpadding="0" cellspacing="0" class="normal">
<tr>
<td valign="top"><?php 
if(!$_REQUEST['action'] || !$_REQUEST['linkID'] || !$_REQUEST['sectionID']){
$messages->addError(t("Μη εξουσιοδοτημένη πρόσβαση")."!");
}
if($messages->hasErrors){
$messages->printSystemMessages();
}else{
if($_REQUEST['action']=="add" && !$_REQUEST['youtubeCode']){ // select file to attach
$header=t("ΠΡΟΣΘΗΚΗ ΣΤΟΙΧΕΙΩΝ ΝΕΟΥ VIDEO");
?>
<div class="section_title"><?php echo $header;?></div>
<form id="editFile" name="editFile" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="100%" border="0" cellpadding="3" class="normal" style="margin-top:30px">
<tr>
  <td align="right"><?php echo t("YouTube Κωδικός ή URL");?>:</td>
  <td><input name="youtubeCode" type="text" class="listbox" id="youtubeCode" size="40"></td>
</tr>
<?php
	$Lresult =$db->query("SELECT * FROM languages WHERE active=1 ");
	$LRecords = $db->getRows($Lresult);
	foreach($LRecords as $Lrow) {
		if((int)$langID > 1) $Lrow->title = $Lrow->title_translated;
		$extension = ($Lrow->extension) ? $Lrow->extension : '_gr';
		?>
		<tr>
		
		<td align="right"><?php echo t("Τίτλος του Video");?> <?php  if($isMultilingualSite){echo '<span style="font-size:9px"><i>('.t('στα').' '.$Lrow->title.')</i></span>';}?>: 
		<?php if($isMultilingualSite){ ?>
		<img  style="padding:1px; border:1px solid black; margin-bottom:-7px;" src="<?php echo $rootPath;?>mediabank/flags/flag<?php  echo $extension;?>.png">
		<?php 
} ?>
		</td>
		<td>
		<input name="title<?php  echo $extension;?>" type="text" class="listbox" id="title<?php  echo $extension;?>" size="40"></td>
		</tr>
		<?php 
}
?>





<tr>
	<td align="right">
	<input name="action" type="hidden" id="action" value="<?php echo $action;?>">
	<input name="u" type="hidden" value="<?php echo $u;?>">
	<input name="linkID" type="hidden" id="linkID" value="<?php echo $linkID;?>">
	<input name="sectionID" type="hidden" id="sectionID" value="<?php echo $sectionID;?>">
	</td>
	<td>
	<input name="writedata" type="submit" class="btn btn-success" id="writedata" value="<?php echo t("Προσθήκη");?>">
	</td>
</tr>
</table>
</form>
<?php 
}
}
?>
</td>
</tr>
</table>
</body>
</html>
