<?php

/**
 * Created by PhpStorm.
 * User: ValekS
 * Date: 16.02.14
 * Time: 18:10
 */

define('_IN_JOHNCMS', 1);

require('../incfiles/core.php');

// подключение языкового файла
$lng_yt = core::load_lng('you_tube');
// вывод титлов страницы
$textl = ($act == 's' ? $lng_yt['dl_video'] : $lng_yt['search_v']);

require('../incfiles/head.php');

// Удаление старых запросов
$sec = 24 * 60 * 60;
$sec_del = time() - $sec;
$count = mysql_result(mysql_query("SELECT COUNT(*) FROM `ytdown`"), 0);

if ($count > 20)
	mysql_query("DELETE FROM `ytdown` WHERE `time` < ".$sec_del);

echo '<div class="phdr">'.$lng_yt['search_v'].'</div>';


if ($act != 'search' && $act != 'save') {
	
	echo '<div class="gmenu"><form action="index.php?act='.($act == 's' ? 'save' : 'search').'" method="post">';
        
	echo '<input type="text" value="" name="search" />' .
	'<input type="submit" value="'.($act == 's' ? $lng_yt['downl'] : $lng_yt['search']).'" name="submit" /><br/>';
		
	if ($act != 's')
		echo '<input name="anonym" type="checkbox" value="0" />&#160;'.$lng_yt['anonym'];
		
	echo '</form></div>';

	echo '<div class="list2">'.($act == 's' ? $lng_yt['about_downl'] : $lng_yt['about_search']).'<br/>'.
    ($act == 's' ? '<a href="index">'.$lng_yt['s_video'].'</a></div>' : '<a href="index?act=s">'.$lng_yt['dl_video'].'</a></div>');
	
	echo '<div class="list1"><b>'.$lng_yt['last'].'</b><br/>';

	$req = mysql_query("SELECT * FROM `ytdown` ORDER BY `time` DESC LIMIT 10");
		
	for ($i = 0; $res = mysql_fetch_assoc($req); ++$i) {
		
		echo functions::display_date($res['time']) . ' - <a href="results?s='. str_replace(' ', '+', $res['text']).'">'. functions::checkout($res['text']).'</a><br/>';
		
	}
		
	echo '</div>';

} elseif ($act == 'search') {

	$search = isset($_POST['search']) ? trim($_POST['search']) : false;
	$s = str_replace(' ', '+', $search);
	$anonym = isset($_POST['anonym']);

	$error = array();

	if ($user_id) {
		// Антифлуд для зарегистрированных пользователей
		$flood = FALSE;
		$flood = functions::antiflood();
	} 

	if (empty($search))
		$error[] = $lng_yt['error_search_empty'];

	if ($flood)
		$error = $lng_yt['error_search_flood'] . $flood . '&#160;' . $lng['seconds'].'!';

	if (!$error) {

		if($user_id) {
			$searchbd = mysql_fetch_array(mysql_query("SELECT * FROM `ytdown` WHERE `uid` = '".$user_id."' AND `text` = '".$search."'"));
		} else {
			$searchbd = NULL;
		}

		if (!$searchbd && $anonym == 0) {
			// Вставляем сообщение в базу
			mysql_query("INSERT INTO `ytdown` SET
			`time` = '" . time() . "',
			`uid` = '" . ($user_id ? $user_id : 0) . "',
			`text` = '" . mysql_real_escape_string($search) . "'
			");
		}

		// антиспам
		if ($user_id) {
			mysql_query("UPDATE `users` SET `lastpost` = '" . time() . "' WHERE `id` = ".$user_id);
		}

		header('location: results?s='.$s.'');
   
	} else {
		echo functions::display_error($error, '<a href="index">' . $lng['back'] . '</a>');
		require('../incfiles/end.php');
		exit;
	}
		
} elseif ($act == 'save') {
	
	$save = isset($_POST['search']) ? trim($_POST['search']) : false;

	$error = array();

	if ($user_id) {
		// Антифлуд для зарегистрированных пользователей
		$flood = FALSE;
		$flood = functions::antiflood();
	} 

	if (empty($save))
		$error[] = $lng_yt['error_dl_empty'];

	preg_match('#youtube\.com(\|/|/embed/|/v/|/watch\?v=|&gt;|&lt;|&quot;)([A-Za-z0-9_-]{5,11})#i', $save, $youtube);
    
	if (isset($youtube['2']) && $youtube['2'] != '') {
		$v = $youtube['2'];
	} else {
		$v = $save;
	}

	if ($flood)
		$error = $lng_yt['error_dl_flood'] . $flood . '&#160;' . $lng['seconds'].'!';

	if (!$error) {

		// антиспам
		if ($user_id) {
			mysql_query("UPDATE `users` SET `lastpost` = '" . time() . "' WHERE `id` = ".$user_id);
		}

		header('location: watch?v='.$v.'');
	} else {
		echo functions::display_error($error, '<a href="index">' . $lng['back'] . '</a>');
		require('../incfiles/end.php');
		exit;
	}

}

require('../incfiles/end.php');