<div id="center-column">
<div class="top-bar">
<h1>Cpanel - Upload Games</h1>
<div class="breadcrumbs"><a href="index.php?act=addmedia" title="Ad Media">Add Media</a> / <a href="index.php?act=addcode" title="Ad Code">Add Code</a> / <a href="index.php?act=managegames" title="Manage Games">Manage Games</a> / <a href="index.php?act=brokenfiles" title="Broken Files">Broken Files</a></div>
</div><br />
<div class="select-bar">
<label>
<h3>Add Game</h3>
</label>
</div>
<?php

function get_file_extension($filepath) {
    preg_match('/[^?]*/', $filepath, $matches);
    $string = $matches[0];
    $pattern = preg_split('/\./', $string, -1, PREG_SPLIT_OFFSET_CAPTURE);
    // check if there is any extension
    if(count($pattern) == 1) {
        return false;
    }
       
    if(count($pattern) > 1) {
        $filenamepart = $pattern[count($pattern)-1][0];
        preg_match('/[^?]*/', $filenamepart, $matches);
        return strtolower($matches[0]);
    }
}
$num = rand(0, pow(10, 5)) . '-'; // 5 digit random number to prefix game name
$thumb_types=array(
    'gif',
    'jpeg',
    'jpg',
	'png'
);
$file_types=array(
	'swf',
	'dcr',
	'mov',
	'mpg',
	'avi',
	'flv',
	'wmv'
);
if (isset($_POST['add'])) {
	include_once ("../includes/db_functions.inc.php");
	if (!isset($_FILES['file']['name']) || !isset($_FILES['thumbnail']['name'])) {
		echo '<center>No file or thumbnail detected</center>';
		exit();
	}
	$thumb_ext = get_file_extension($_FILES["thumbnail"]["name"]) or die("<center>You must include a game pic.</center>");
	$file_ext = get_file_extension($_FILES["file"]["name"]) or die("<center>You must include a game file.</center>");
	if (in_array($thumb_ext, $thumb_types) || in_array($file_ext, $file_types)) {
		if ($_FILES["file"]["error"] > 0) {
			echo "<center>Return Code: " . $_FILES["thumbnail"]["error"] . "</center>";
		}
		else {
			move_uploaded_file($_FILES["thumbnail"]["tmp_name"],
			$setting['sitepath']."/img/" . $num . $_FILES["thumbnail"]["name"]);
			move_uploaded_file($_FILES["file"]["tmp_name"],
			$setting['sitepath']."/swf/" . $num . $_FILES["file"]["name"]);
			$img = yasDB_clean("img/" .  $num . $_FILES['thumbnail']['name']);
			$file = yasDB_clean("swf/" .  $num . $_FILES['file']['name']);
			$desc = yasDB_clean($_POST['description']);
			$title = yasDB_clean($_POST['title']);
			$height = yasDB_clean($_POST['height']);
			$width = yasDB_clean($_POST['width']); 
			if ($_GET['height'] && $_POST['width'] > 0 ) {
				$query = yasDB_insert("INSERT INTO games (title, description, category, thumbnail, file, height, width, type) VALUES ('$title', '$desc', '{$_POST['category']}', '$img', '$file', '$height', '$width'', '{$_POST['type']}')",false);
				if ($query) echo '<center><p align="center"><b>Game successfully added!</b></p></center>';
			}
			else {
				list($width, $height, $type, $attributes) = getimagesize($setting['sitepath'].'/'.$file);
				$query = yasDB_insert("INSERT INTO games (title, description, category, thumbnail, file, height, width, type) VALUES ('$title', '$desc', '{$_POST['category']}', '$img', '$file', '$height', '$width', '{$_POST['type']}')",false);
				if ($query) echo '<center><p align="center"><b>Game successfully added!</b></p></center>';
			}		   
		}
	}
	else {
		echo '<center>File or thumbnail type not supported</center>';
		exit();
	}
}
 ?>
 <div class="table">
		<img src="img/bg-th-left.gif" width="8" height="7" alt="" class="left" />
	    <img src="img/bg-th-right.gif" width="7" height="7" alt="" class="right" />
        <form enctype="multipart/form-data" action="index.php?act=uploadgames" method="post">
		<input type="hidden" name="act" value="uploadgames"/>
		<table class="listing form" cellpadding="0" cellspacing="0">
		<tr>
	    <th class="full" colspan="2">Upload A Game</th>
	    </tr>
		<tr>
		<td class="first" width="172"><strong>Title</strong></td>
		<td class="last"><input type="text" name="title" /></td>
		</tr>
		<tr class="bg">
		<td class="first"><strong>Description</strong></td>
		<td class="last"><textarea name="description"></textarea></td>
		</tr>
		<tr>
		<td class="first" width="172"><strong>Category</strong></td>
		<td class="last"><select name="category">
	    <?php
	    $query = yasDB_select("SELECT * FROM categories",false);
	    while($row = $query->fetch_array(MYSQLI_ASSOC)) {
		echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
	    }
	    $query->close();
	    ?>
	    </select></td>
		</tr>
		<tr class="bg">
		<td class="first"><strong>Game File Type</strong></td>
		<td class="last"><select name="type">
		<option value="SWF">swf</option>
		<option value="DCR">dcr</option>
	    </select></td>
		</tr>
		<tr>
		<td class="first" width="172"><strong>Game File</strong></td>
		<td class="last"><input type="file" name="file" /> (swf/dcr)</td>
		</tr>
		<tr class="bg">
		<td class="first"><strong>Thumbnail</strong></td>
		<td class="last"><input type="file" name="thumbnail"> (gif/jpg/png)</td>
		</tr>
		<tr>
		<td class="first" width="172"><strong>Height:*</strong></td>
		<td class="last"><input type="text" name="height" /></td>
		</tr>
		<tr class="bg">
		<td class="first"><strong>Width:*</strong></td>
		<td class="last"><input type="text" name="width" /></td>
		</tr>
		<tr>
		<td class="first" width="172"><strong>Submit</strong></td>
		<td class="last"><input type="submit" class="button" name="add" value="Add Game!" /></td>
		</tr>
		</tr>
		</table>
		</div>
		</form>
</div>           