<?php
/*---------------------------------------------------+
| PHP-Fusion 6 Content Management System
+----------------------------------------------------+
| Copyright © 2002 - 2006 Nick Jones
| http://www.php-fusion.co.uk/
+----------------------------------------------------+
| Released under the terms & conditions of v2 of the
| GNU General Public License. For details refer to
| the included gpl.txt file or visit http://gnu.org
+----------------------------------------------------+
| Modified by PHP-Fusion SF Group
| Copyright © 2007 by SF
| http://www.rus-phpfusion.com/
+----------------------------------------------------*/

require_once "../maincore.php";
require_once BASEDIR."subheader.php";
require_once ADMIN."navigation.php";
include LOCALE.LOCALESET."admin/downloads.php";

if (!checkrights("D") || !defined("iAUTH") || $aid != iAUTH) fallback("../index.php");
if (isset($download_id) && !isNum($download_id)) fallback(FUSION_SELF.$aidlink);
if (!isset($step)) $step = "";

$result = dbquery("SELECT * FROM ".$db_prefix."download_cats");
if (dbrows($result) != 0) {
        if ($step == "delete") {
                $result = dbquery("DELETE FROM ".$db_prefix."downloads WHERE download_id='$download_id'");
                redirect(FUSION_SELF.$aidlink."&download_cat_id=$download_cat_id");
        }
        if (isset($_POST['save_download'])) {
                $download_title = stripinput($_POST['download_title']);
                $download_description = addslash($_POST['download_description']);
                $download_description_p = addslash($_POST['download_description_p']);
                
                if ($step !== "edit") {
                if ($_FILES['upload']['error']==0)
				{
				$upload_file = $_FILES['upload']['name'];
				$upload_dir = '../data/';
				$file_allow = array ("zip", "rar", "7z");
				$file_info = pathinfo($upload_file);
				if (!(in_array($file_info['extension'],$file_allow)))
				{
					echo "Разрешение ".$file_info['extension']." не является разрешенным";
					exit;
				}
				$tmp_path = $_FILES['upload']['tmp_name'];
				$full_path_copy = $upload_dir.$upload_file;
					if (move_uploaded_file($tmp_path,$full_path_copy))
				{
					
					$dir = 'data/';
					$download_url = $dir.$_FILES['upload']['name'];
                	$download_filesize = parsebytesize($_FILES['upload']['size']);
                	$uploaded = true;
                	
                }
                }
                }
                
                if ($step == "edit") {
                	if ($reedit == "1") {
						$download_url = stripinput($_POST['file_url']);
					 	$download_filesize = stripinput($_POST['file_sz']);
					 	$uploaded = true;
					} elseif ($reedit == "2"){
						$download_url = stripinput($_POST['file_url']);
					 	$download_filesize = stripinput($_POST['file_sz']);
						unlink ("../".$download_url);
						$result = dbquery("UPDATE ".$db_prefix."downloads SET download_url='$download_url', download_filesize='$download_filesize' WHERE download_id='$download_id'");
						$uploaded = true;
					} elseif ($reedit == "3"){
						$download_url = stripinput($_POST['file_url']);
					 	$download_filesize = stripinput($_POST['file_sz']);
						unlink ("../".$download_url);
						$result = dbquery("UPDATE ".$db_prefix."downloads SET download_url='$download_url', download_filesize='$download_filesize' WHERE download_id='$download_id'");
						unset($download_url);
						unset($download_filesize);
						if ($_FILES['upload']['error']==0)
				{
				$upload_file = $_FILES['upload']['name'];
				$upload_dir = '../data/';
				$file_allow = array ("zip", "rar", "7z");
				$file_info = pathinfo($upload_file);
				if (!(in_array($file_info['extension'],$file_allow)))
				{
					echo "Разрешение ".$file_info['extension']." не является разрешенным";
					exit;
				}
				$tmp_path = $_FILES['upload']['tmp_name'];
				$full_path_copy = $upload_dir.$upload_file;
					if (move_uploaded_file($tmp_path,$full_path_copy))
				{
					
					$dir = 'data/';
					$download_url = $dir.$_FILES['upload']['name'];
                	$download_filesize = parsebytesize($_FILES['upload']['size']);
                	$uploaded = true;
                	
                }
                }
					} elseif ($reedit == "4"){
					$download_url = stripinput($_POST['file_url']);
					 	$download_filesize = stripinput($_POST['file_sz']);
						unlink ("../".$download_url);
						$result = dbquery("UPDATE ".$db_prefix."downloads SET download_url='$download_url', download_filesize='$download_filesize' WHERE download_id='$download_id'");
						unset($download_url);
						unset($download_filesize);
					$download_url = stripinput($_POST['download_url_e']);
					$download_filesize = stripinput($_POST['download_filesize_e']);
					$uploaded = true;
					}
					
				else {
						if ($_FILES['upload']['error']==0)
				{
				$upload_file = $_FILES['upload']['name'];
				$upload_dir = '../data/';
				$file_allow = array ("zip", "rar", "7z");
				$file_info = pathinfo($upload_file);
				if (!(in_array($file_info['extension'],$file_allow)))
				{
					echo "Разрешение ".$file_info['extension']." не является разрешенным";
					exit;
				}
				$tmp_path = $_FILES['upload']['tmp_name'];
				$full_path_copy = $upload_dir.$upload_file;
					if (move_uploaded_file($tmp_path,$full_path_copy))
				{
					
					$dir = 'data/';
					$download_url = $dir.$_FILES['upload']['name'];
                	$download_filesize = parsebytesize($_FILES['upload']['size']);
                	$uploaded = true;
                	
                }
                }
					}
				
					
				}
                
                
                if (!isset($uploaded)){
					 $download_url = stripinput($_POST['download_url']);
					 $download_filesize = stripinput($_POST['download_filesize']);
			   	}
			   	$mirror_url = stripinput($_POST['mirror_url']);
				$download_license = stripinput($_POST['download_license']);
                $download_os = stripinput($_POST['download_os']);
                $download_version = stripinput($_POST['download_version']);
                
                $download_access = isNum($_POST['download_access']) ? $_POST['download_access'] : "0";
                $comments = isset($_POST['download_comments']) ? "1" : "0";
                $ratings = isset($_POST['download_ratings']) ? "1" : "0";
                if ($step == "edit") {
                        $download_datestamp = isset($_POST['update_datestamp']) ? ", download_datestamp='".time()."'" : "";
                        $result = dbquery("UPDATE ".$db_prefix."downloads SET download_title='$download_title', download_description='$download_description', download_description_p='$download_description_p', download_url='$download_url', download_cat='$download_cat', download_license='$download_license', download_os='$download_os', download_version='$download_version', download_filesize='$download_filesize'".$download_datestamp.", download_comments = $comments, download_ratings = $ratings, download_access = '$download_access', mirror_url='$mirror_url' WHERE download_id='$download_id'");
                        redirect(FUSION_SELF.$aidlink."&download_cat_id=$download_cat");
                } else {
                        $result = dbquery("INSERT INTO ".$db_prefix."downloads VALUES('', '".$userdata['user_id']."', '$download_title', '$download_description', '$download_description_p', '$download_url', '$download_cat', '$download_license', '$download_os', '$download_version', '$download_filesize', '".time()."', '0', '$comments', '$ratings', '$download_access', '$mirror_url')");
                        redirect(FUSION_SELF.$aidlink."&download_cat_id=$download_cat");
                }
        }
        if ($step == "edit") {
                $result = dbquery("SELECT * FROM ".$db_prefix."downloads WHERE download_id='$download_id'");
                $data = dbarray($result);
                $download_title = $data['download_title'];
                $download_description = stripslashes($data['download_description']);
                $download_description_p = stripslashes($data['download_description_p']);
                $download_url = $data['download_url'];
                $mirror_url = $data['mirror_url'];
                $download_license = $data['download_license'];
                $download_os = $data['download_os'];
                $download_version = $data['download_version'];
                $download_filesize = $data['download_filesize'];
                $download_access = $data['download_access'];
                $comments = ($data['download_comments'] == "1" ? " checked" : "");
                $ratings = ($data['download_ratings'] == "1" ? " checked" : "");
                $formaction = FUSION_SELF.$aidlink."&step=edit&amp;download_cat_id=$download_cat_id&amp;download_id=$download_id";
                opentable($locale['470']);
        } else {
                $download_title = "";
                $download_description = "";
                $download_description_p = "";
                $download_url = "";
                $mirror_url = "";
                $download_license = "";
                $download_os = "";
                $download_version = "";
                $download_filesize = "";
                $download_access = "";
                $comments = " checked";
                $ratings = " checked";
                $formaction = FUSION_SELF.$aidlink;
                opentable($locale['471']);
        }
        $editlist = ""; $sel = "";
$d_cat = @$data['download_cat'];
function catSelect($cats_id = 0, $chort = ''){
global $db_prefix, $editlist, $sel, $d_cat, $step ;
if ($cats_id!=0){$chort.='-';}
$result2 = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='".$cats_id."' ORDER BY download_cat_id ASC");
while ($data2 = dbarray($result2)) {
      if ($step == "edit") $sel = ($d_cat == $data2['download_cat_id'] ? " selected" : "");
      $editlist .= "<option value='".$data2['download_cat_id']."'$sel>$chort ".$data2['download_cat_name']."</option>\n";
      catSelect($data2['download_cat_id'], $chort);
}
}
$user_groups = getusergroups(); $access_opts = ""; $sel = "";
        while(list($key, $user_group) = each($user_groups)){
                $sel = ($download_access == $user_group['0'] ? " selected" : "");
                $access_opts .= "<option value='".$user_group['0']."'$sel>".$user_group['1']."</option>\n";
        }
catSelect();
        echo "<form name='inputform' method='post' action='$formaction' enctype='multipart/form-data'>
<table align='center' cellpadding='0' cellspacing='0' width='460'>
<tr>
<td width='80' class='tbl'>".$locale['480']."</td>
<td class='tbl'><input type='text' name='download_title' value='$download_title' class='textbox' style='width:380px;'></td>
</tr>
<tr>
<td valign='top' width='80' class='tbl'>".$locale['481']."</td>
<td class='tbl'><textarea name='download_description' rows='5' cols='70' class='textbox'>".$download_description."</textarea></td>
</tr>
<tr>
<td class='tbl'></td><td class='tbl'>
<input type='button' value='b' class='button' style='font-weight:bold;width:25px;' onClick=\"addText('download_description', '<b>', '</b>');\">
<input type='button' value='i' class='button' style='font-style:italic;width:25px;' onClick=\"addText('download_description', '<i>', '</i>');\">
<input type='button' value='u' class='button' style='text-decoration:underline;width:25px;' onClick=\"addText('download_description', '<u>', '</u>');\">
<input type='button' value='link' class='button' style='width:35px' onClick=\"addText('download_description', '<a href=\'', '\' target=\'_blank\'>Link</a>');\">
<input type='button' value='img' class='button' style='width:35px' onClick=\"addText('download_description', '<img src=\'', '\' style=\'margin:5px\' align=\'left\'>');\">
<input type='button' value='center' class='button' style='width:45px' onClick=\"addText('download_description', '<center>', '</center>');\">
<input type='button' value='small' class='button' style='width:40px' onClick=\"addText('download_description', '<span class=\'small\'>', '</span>');\">
<input type='button' value='small2' class='button' style='width:45px' onClick=\"addText('download_description', '<span class=\'small2\'>', '</span>');\">
<input type='button' value='alt' class='button' style='width:25px' onClick=\"addText('download_description', '<span class=\'alt\'>', '</span>');\"><br>
</td>
</tr>

<tr>
<td nowrap valign='top' width='80' class='tbl'>".$locale['005d']."</td>
<td class='tbl'><textarea name='download_description_p' rows='5' cols='70' class='textbox'>".$download_description_p."</textarea></td>
</tr>
<tr>
<tr>
<td class='tbl'></td><td class='tbl'>
<input type='button' value='b' class='button' style='font-weight:bold;width:25px;' onClick=\"addText('download_description_p', '<b>', '</b>');\">
<input type='button' value='i' class='button' style='font-style:italic;width:25px;' onClick=\"addText('download_description_p', '<i>', '</i>');\">
<input type='button' value='u' class='button' style='text-decoration:underline;width:25px;' onClick=\"addText('download_description_p', '<u>', '</u>');\">
<input type='button' value='link' class='button' style='width:35px' onClick=\"addText('download_description_p', '<a href=\'', '\' target=\'_blank\'>Link</a>');\">
<input type='button' value='img' class='button' style='width:35px' onClick=\"addText('download_description_p', '<img src=\'', '\' style=\'margin:5px\' align=\'left\'>');\">
<input type='button' value='center' class='button' style='width:45px' onClick=\"addText('download_description_p', '<center>', '</center>');\">
<input type='button' value='small' class='button' style='width:40px' onClick=\"addText('download_description_p', '<span class=\'small\'>', '</span>');\">
<input type='button' value='small2' class='button' style='width:45px' onClick=\"addText('download_description_p', '<span class=\'small2\'>', '</span>');\">
<input type='button' value='alt' class='button' style='width:25px' onClick=\"addText('download_description_p', '<span class=\'alt\'>', '</span>');\"><br>
</td>
</tr>";
if ($step !== "edit"){
echo "<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'>Загрузить с компьютера:</td>
</tr>
<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'><input type='file' class='textbox' style='width:380px;' name='upload' /></td>
</tr>
<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'>";
$post_max_size = (ini_get('file_uploads')==0) ? $locale['ic_006'] : @ini_get('post_max_size');
echo "Максимальный размер загрузки: ".$post_max_size;
echo "<br /><br /></td>
</tr>
<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'>или указать url и размер в ручную</td>
</tr><tr>
<td width='80' class='tbl'>".$locale['482']."</td>
<td class='tbl'><input type='text' name='download_url' value='$download_url' class='textbox' style='width:380px;'></td>
<tr>
<td width='80' class='tbl'>".$locale['487']."</td>
<td class='tbl'><input type='text' name='download_filesize' value='$download_filesize' class='textbox' style='width:150px;'></td>
</tr>";
} elseif ($step == "edit") {
echo "<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'>
<input type=hidden name='file_url' VALUE='$download_url'>
<input type=hidden name='file_sz' VALUE='$download_filesize'>
".@$report = empty($download_url) ? "Ссылка на файл не указана" : "Файл $download_url ($download_filesize)"."
$report</td>
</tr>";
	if ($download_url != "" & file_exists("../".$download_url)) {
		echo "<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'>
<input type=radio name=reedit VALUE='1' checked> Оставить без изменений<br />
<input type=radio name=reedit VALUE='2'> Удалить<br />
<input type=radio name=reedit VALUE='3'> Заменить<br />
<input type='file' class='textbox' style='width:380px;' name='upload' /><br />
<input type=radio name=reedit VALUE='4'> Заменить файлом с сети<br />
<input type='text' name='download_url_e' value='' class='textbox' style='width:380px;'><br />
Размер файла:<br />
<input type='text' name='download_filesize_e' value='' class='textbox' style='width:150px;'>
</td>
</tr>";
	} else {
echo "<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'>заменить фалом с компьютера:</td>
</tr>
<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'><input type='file' class='textbox' style='width:380px;' name='upload' /></td>
</tr>
<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'>";
$post_max_size = (ini_get('file_uploads')==0) ? $locale['ic_006'] : @ini_get('post_max_size');
echo "Максимальный размер загрузки: ".$post_max_size;
echo "<br /><br /></td>
</tr>
<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'>или указать url и размер в ручную</td>
</tr><tr>
<td width='80' class='tbl'>".$locale['482']."</td>
<td class='tbl'><input type='text' name='download_url' value='$download_url' class='textbox' style='width:380px;'></td>
<tr>
<td width='80' class='tbl'>".$locale['487']."</td>
<td class='tbl'><input type='text' name='download_filesize' value='$download_filesize' class='textbox' style='width:150px;'></td>
</tr>";
	}
	
}
echo "<tr>
<td width='80' class='tbl'>&nbsp;</td>
<td class='tbl'>дополнительная информация</td>
</tr>
<tr>
<td width='80' class='tbl'>".@$locale['482_1']."</td>
<td class='tbl'><input type='text' name='mirror_url' value='$mirror_url' class='textbox' style='width:380px;'></td>
<tr>
</tr>
<tr>
<td width='80' class='tbl'>".$locale['483']."</td>
<td class='tbl'><select name='download_cat' class='textbox' style='width:150px;'>
$editlist</select></td>
</tr>
<tr>
<td width='80' class='tbl' style='white-space:nowrap'>".$locale['433']."</td>
<td class='tbl'><select name='download_access' class='textbox' style='width:150px;'>
$access_opts</select></td>
</tr>
<tr>
<td width='80' class='tbl'>Официальный сайт:</td>
<td class='tbl'><input type='text' name='download_os' value='$download_os' class='textbox' style='width:150px;'></td>
</tr>
<tr>
<td colspan='2' class='tbl'>
<input type='checkbox' name='download_comments' value='yes'$comments> ".$locale['423D']."<br>
<input type='checkbox' name='download_ratings' value='yes'$ratings> ".$locale['424D']."</td>
</tr>
<tr>
<td align='center' colspan='2' class='tbl'>";
        if ($step == "edit") echo "<input type='checkbox' name='update_datestamp' value='1'> ".$locale['489']."<br><br>\n";
        echo "<input type='submit' name='save_download' value='".$locale['488']."' class='button'></td>
</tr>
</table>
</form>\n";
        closetable();
        tablebreak();
        opentable($locale['500']);
        echo "<table align='center' cellpadding='0' cellspacing='0' width='400'>\n";

$locale503 = $locale['503']; $locale504 = $locale['504'];
$locale505 = $locale['505'];

function files ($cats_id = 0, $chort = ''){
global $db_prefix, $download_cat_id, $locale503, $locale504, $locale505, $aidlink;
if ($cats_id!=0){$chort.='-';}
$result = dbquery("SELECT * FROM ".$db_prefix."download_cats WHERE download_cat_idc='".$cats_id."' ORDER BY download_cat_id ASC");
while ($data = dbarray($result)) {
                        if (!isset($download_cat_id)) $download_cat_id = "";
                        if ($data['download_cat_id'] == $download_cat_id) { $p_img = "off"; $div = ""; } else { $p_img = "on"; $div = "style='display:none'"; }
                        echo "<tr>
<td class='tbl2'>".$chort.$data['download_cat_name']."</td>
<td class='tbl2' align='right'><img onclick=\"javascript:flipBox('".$data['download_cat_id']."')\" src='".THEME."images/panel_".$p_img.".gif' name='b_".$data['download_cat_id']."' alt=''></td>
</tr>\n";
                        $result2 = dbquery("SELECT * FROM ".$db_prefix."downloads WHERE download_cat='".$data['download_cat_id']."' ORDER BY download_title");
                        if (dbrows($result2) != 0) {
                                echo "<tr>
<td colspan='2'>
<div id='box_".$data['download_cat_id']."'".$div.">
<table cellpadding='0' cellspacing='0' width='100%'>\n";
                                while ($data2 = dbarray($result2)) {
                                        if (!strstr($data2['download_url'],"http://") && !strstr($data2['download_url'],"../")) {
                                                $download_url = BASEDIR.$data2['download_url'];
                                        } else {
                                                $download_url = $data2['download_url'];
                                        }
                                        echo "<tr>\n<td class='tbl'><a href='$download_url' target='_blank'>".$data2['download_title']."</a></td>
<td align='right' width='100' class='tbl'><a href='".FUSION_SELF.$aidlink."&step=edit&amp;download_cat_id=".$data['download_cat_id']."&amp;download_id=".$data2['download_id']."'>".$locale503."</a> -
<a href='".FUSION_SELF.$aidlink."&step=delete&amp;download_cat_id=".$data['download_cat_id']."&amp;download_id=".$data2['download_id']."' onClick='return DeleteItem()'>".$locale504."</a></td>
</tr>\n";
                                }
                                echo "</table>
</div>
</td>
</tr>\n";
                        } else {
                                echo "<tr>
<td colspan='2'>
<div id='box_".$data['download_cat_id']."' style='display:none'>
<table cellpadding='0' cellspacing='0' width='100%'>
<tr>
<td class='tbl'>".$locale505."</td>
</tr>
</table>
</div>
</td>
</tr>\n";
                        }
files ($data['download_cat_id'], $chort);
                }
}

$result = dbquery("SELECT * FROM ".$db_prefix."download_cats ORDER BY download_cat_id");
if (dbrows($result) != 0) {
                echo "<tr>
<td class='tbl2'>".$locale['501']."</td>
<td align='right' class='tbl2'>".$locale['502']."</td>
</tr>
<tr>
<td colspan='2' height='1'></td>
</tr>\n";
files();
                echo "</table>\n";
                echo "<script type='text/javascript'>
function DeleteItem()
{
return confirm('".$locale['460']."');
}
</script>\n";
        } else {
                echo "<tr>
<td align='center'><br>
".$locale['506']."<br><br>
<a href='download_cats.php".$aidlink."'>".$locale['507']."<br><br></td>
</tr>
</table>\n";
        }
        closetable();
} else {
        opentable($locale['500']);
        echo "<center>".$locale['508']."<br>
".$locale['509']."<br><br>
<a href='download_cats.php".$aidlink."'>".$locale['510']."</a>".$locale['511']."</center>\n";
        closetable();
}

echo "</td>\n";
require_once BASEDIR."footer.php";
?>