<?php 
/*    
 *    Copyright (c) 2010 VidiScript
 *
 *    This file is part of VidiScript.
 *
 *    VidiScript is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VidiScript is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VidiScript.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    File Name: new_uploads.inc
 *    Description: Admin options for reviewing new uploads
 *    $Date: 2010-02-21 23:16:57 +0000 (Sun, 21 Feb 2010) $
 *    $Revision: 12 $
 */
global $sitepath,$db ;
$path = getSetting("sitefolder", $db) ;
$sitefolder = "http://".$_SERVER['SERVER_NAME'].$path ;
$newUploads = "" ;
if (isset($_POST['submit'])) {
	$db->query("SELECT * FROM media WHERE status = 'false'") ;
	$resSF = $db->fetchAll() ;
	foreach ($resSF as $resCheck) {
		if ((isset($_POST['vid'.$resCheck['id']]) && $_POST['func'] == "Approve Selected") || $_POST['func'] == "Approve All") {
			if ($resCheck['status'] != 'true') {
				$db->query("UPDATE `media` SET `status` = 'true' WHERE id = '".$resCheck['id']."'") ;
				$db->query("SELECT * FROM media WHERE id = '".$resCheck['id']."'") ;
				$media = $db->fetch() ;
				$db->query("SELECT name FROM category WHERE id = ".$media['category']) ;
				$resCat = $db->fetch() ;
				$Cat = url_encode($resCat['name']) ;
				$title = url_encode($media['title']) ;
				if (getUserID($media['poster']) != 0) {
					$db->query("SELECT userid FROM subscription WHERE subscribedtoid = ".getUserID($media['poster'])) ;
					$resUsers = $db->fetchAll() ;
					if ($db->numRows() > 0) {
						foreach ($resUsers as $row) {
							$db->query("SELECT * FROM member WHERE id = ".$row['userid']) ;
							$subUser = $db->fetch() ;
							$messageBody = getEmail("email_newmedia") ;
							$messageBody = str_replace("[MEDIALINK]", $sitename."/".$Cat."/".$title, $messageBody) ;
							$messageBody = str_replace("[POSTER]", $media['poster'], $messageBody) ;
							$messageBody = str_replace("[SITENAME]", getSetting("sitename", $db), $messageBody) ;
							$messageBody = str_replace("[UNSUBLINK]", $sitename."?id=54&del=".getUserID($media['poster']), $messageBody) ;
							$headers = "From: \"Subscription\" <".getSetting('contact_email', $db).">>\r\n"."Reply-To: ".getSetting('contact_email', $db)."\r\n"."X-Mailer: PHP/".phpversion() ;
							mail($subUser['email'], 'New media by '.$media['poster'], $messageBody, $headers) ;
						}
					}
				}
			}
		}
		if (($_POST['func'] == "Delete Selected" && isset($_POST['vid'.$resCheck['id']])) || $_POST['func'] == "Delete All") {
			if ($resCheck['thumb'] != 'usr_upload.jpg') {
				if (file_exists('uploads/thumbs/'.$resCheck['thumb'].'.small.jpg') && file_exists('uploads/thumbs/'.$resCheck['thumb'].'.large.jpg')) {
					@unlink('uploads/thumbs/'.$resCheck['thumb'].'.small.jpg') ;
					@unlink('uploads/thumbs/'.$resCheck['thumb'].'.large.jpg') ;
				}
			}
			if ($resCheck['mediatype'] != 'embed') {
				@unlink('uploads/'.$resCheck['mediaurl']) ;
			}
			$db->query("DELETE FROM `media` WHERE id = '".$resCheck['id']."'") ;
		}
	} //FE
}
$db->query("SELECT * FROM media WHERE status = 'false'") ;
if ($db->numRows() != 0) {
	$sql = "SELECT * FROM `media` WHERE `status` = 'false' AND `extern` != 'locked' ORDER BY `id` DESC" ;
	$result = mysql_query($sql) or die('Error: '.mysql_error()) ;
	$counte = mysql_num_rows($result) ;
	if ($counte > 0) {
		if ($result) {
			$count = 1 ;
			while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
				$db->query("SELECT * FROM `category` WHERE id = '".$row['category']."'") ;
				$_cat = $db->fetch() ;
				$newUploads .= "<tr><th>$count</th><td><a href='".$sitefolder."play/".url_encode($_cat['name'])."/".url_encode($row['title'])."' target=\"_blank\" alt=\"Play\">".$row['title']."</a></td><td>".$_cat['name']."</td><td>".date("d.m.Y", $row['added'])."</td><td class='td_select'><input class='checkbox' name='vid".$row['id']."' type='checkbox' /></td></tr>" ;
				$count++ ;
			}
		}
	}
	else {
		$newUploads = "<tr><th>-</th><td>-</td><td>-</td><td>-</td><td>-</td></tr>" ;
	}
	$db->query("SELECT * FROM media WHERE extern = 'locked' AND status = 'false'") ;
	$res = $db->fetchAll() ;
	if ($db->numRows() != 0) {
		$count = 1 ;
		foreach ($res as $row) {
			$db->query("SELECT * FROM `category` WHERE id = '".$row['category']."'") ;
			$_cat = $db->fetch() ;
			$lockedUploads .= "<tr><th>$count</th><td><a href='".$sitefolder."play/".url_encode($_cat['name'])."/".url_encode($row['title'])."' target=\"_blank\" alt=\"Play\">".$row['title']."</a></td><td>".$_cat['name']."</td><td>".date("d.m.Y", $row['added'])."</td><td class='td_select'><input class='checkbox' name='vid".$row['id']."' type='checkbox' /></td><td><a target='_blank' href='".$sitefolder."uploads/".$row['mediaurl']."'>linky</a></td></tr>" ;
			$count++ ;
		}
	}
	else {
		$lockedUploads = "<tr><th>-</th><td>-</td><td>-</td><td>-</td><td>-</td></tr>" ;
	}
}
else {
	$message = "There are no new uploads." ;
}
?>