<?php
/*---------------------------------------------------+
| PHP-Fusion 7 Content Management System
+----------------------------------------------------+
| Copyright © 2002 - 2008 Nick Jones
| http://www.php-fu...
+----------------------------------------------------+
| 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
+----------------------------------------------------+
| Download System mSF For PHP-Fusion 7.xx v.4.8
| Copyright © 2008-2009 Powered by .:MeF:.
| URL: http://x-iweb.ru/
+----------------------------------------------------*/

require_once "maincore.php";
require_once THEMES."templates/header.php";
include LOCALE.LOCALESET."downloads.php";
include LOCALE.LOCALESET."admin/downloads.php";

    if (iMEMBER) {

    if (isset($_POST['save_download'])) {
   
   require_once INCLUDES."download_system/function.php";
   
       $result = dbquery("SELECT * FROM ".DB_PREFIX."download_settings");
      $data = dbarray($result);
      
      $upload_file = $_FILES['upload']['name'];
   if ($upload_file != '') {
      $tmp_path = $_FILES['upload']['tmp_name'];
       $filext = strtolower(strrchr($upload_file,"."));
        $downtypes = explode(",", $data['downtypes']);      
         if (in_array($filext, $downtypes)) {   
      $upload_dir = BASEDIR."data/";
      $upload_file_name = str_replace(" ", "_", str_replace("%20", "_", $upload_file));      
        if (file_exists($upload_dir.$upload_file_name)) {
        $upload_name = universal_detect_exists($upload_dir, $upload_file_name);
        } else { $upload_name = $upload_file_name; }
                   if (move_uploaded_file($tmp_path, $upload_dir.$upload_name)) {
                  $download_url = "data/".$upload_name;   
                   $size = array_reduce (array (" $B", " $KB", " $MB", " $GB", " $TB"),
         create_function ('$a,$b', 'return is_numeric($a)?($a>=1024?$a/1024:number_format($a,2).$b):$a;'),            filesize ($upload_dir.$upload_name));
                        }
   
      } else { Header("Location: ".FUSION_SELF."?error=upload"); exit; }
      
    } else { $download_url = stripinput($_POST['download_url']); }
   $download_title = stripinput($_POST['download_title']);
      if ($_POST['download_filesize'] != '') {
    $download_filesize = stripinput($_POST['download_filesize']);
       } else { $download_filesize = $size; }
    $download_description = stripinput(addslash(censorwords($_POST['download_description'])));         
    $mirror_url = $data['allow_mirror'] == '1' ? stripinput($_POST['mirror_url']) : "";
   $download_lang = $data['allow_lang'] == '1' ? stripinput($_POST['download_lang']) : "";
   $download_rus = $data['allow_rus'] == '1' ? stripinput($_POST['download_rus']) : "";
   $download_os = $data['allow_os'] == '1' ? stripinput($_POST['download_os']) : "";
   $download_version = $data['allow_platform'] == '1' ? stripinput($_POST['download_version']) : "";
   $download_button = $data['allow_button'] == '1' ? stripinput($_POST['download_button']) : "";
   $download_burl = $data['allow_button'] == '1' ? stripinput($_POST['download_burl']) : "";
   $download_license = $data['allow_license'] == '1' ? stripinput($_POST['download_license']) : "";
      $download_description_p = "";
      $download_background = "images/downloads/down.png";                  
        $download_access = "0";
        $comments = "1";
        $ratings = "1";
      $recommends = "0";
      $download_datestamp = time();
      if (iSUPERADMIN) { $download_submit = "0"; } else { $download_submit = "1"; }
      $download_user = $userdata['user_id'];
      $download_forum = "";
      $download_cat = stripinput($_POST['download_cat']);
                  
      if ($data['allow_screen'] == '1') {
      $upload_screen = $_FILES['filename']['name'];
            if ($upload_screen != '') {
      $screenext = strtolower(strrchr($upload_screen,"."));
        $screentypes = explode(",", $data['downtypes']);      
             if (in_array($screenext, $screentypes)) {   
          $upload_dir_im = BASEDIR."images/screens/";
          $tmp_path = $_FILES['filename']['tmp_name'];
          $file_name = str_replace(" ", "_", str_replace("%20", "_", $upload_screen));      
                    if (file_exists($upload_dir_im.$file_name)) {
               $screen = universal_detect_exists($upload_dir_im, $file_name);
                    } else { $screen = $file_name; }
                  if(copy($tmp_path, $upload_dir_im.$screen));
                   $download_screen = $screen;
             } else { redirect(FUSION_SELF."?error=upload"); exit; }
         }
      } else { $download_screen = ""; }
            
        $result = dbquery("INSERT INTO ".DB_DOWNLOADS." (download_user, download_title, download_description, download_description_p, download_url, download_cat, download_license, download_os, download_version, download_filesize, download_datestamp, download_count, download_allow_comments, download_allow_ratings, download_allow_recommends, download_access, mirror_url, download_lang, download_rus, download_forum, download_screen, download_button, download_burl, download_background, download_submit) VALUES ('$download_user', '$download_title', '$download_description', '$download_description_p', '$download_url', '$download_cat', '$download_license', '$download_os', '$download_version', '$download_filesize', '$download_datestamp', '0', '$comments', '$ratings', '$recommends', '$download_access', '$mirror_url', '$download_lang', '$download_rus', '$download_forum', '$download_screen', '$download_button', '$download_burl', '$download_background', '$download_submit')");
      
          redirect(FUSION_SELF."?status=ok");
            
        } else {
                                 
                opentable($locale['SA100']);
                  
   echo "<div>".$locale['SA101']."</div></br>";   
   
           if (isset($_GET['status']) == "ok") {
          $message = "".$locale['SA104']."";
          if ($message) {   echo "<div class='admin-message'>".$message."</div>\n"; }
          } elseif (isset($_GET['error']) == "upload") {
          $message = $locale['SA106'];
         if ($message) {   echo "<div class='admin-message'>".$message."</div>\n"; }
          }
      
    function catSelect($cats_id = '0', $chort = '', $sel = ''){
    global $editlist, $sel;
            if ($cats_id != 0) { $chort .= '-'; }
        $result2 = dbquery("SELECT * FROM ".DB_DOWNLOAD_CATS." WHERE download_cat_idc='".$cats_id."' ORDER BY download_cat_id ASC");
            while ($data2 = dbarray($result2)) {
        $editlist .= "<option value='".$data2['download_cat_id']."'$sel>$chort ".$data2['download_cat_name']."</option>\n";
    catSelect($data2['download_cat_id'], $chort);
            }
    }

    catSelect();

    echo "<form name='inputform' method='post' step='".FUSION_SELF."' enctype='multipart/form-data'>";
    echo "<table align='center' cellpadding='0' cellspacing='0' width='460'>";
    echo "<tr>";
    echo "<td class='tbl2' align='center' colspan='2'><b>".$locale['SA102']."</b></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['480D']."</td>";
    echo "<td class='tbl'><input type='text' name='download_title' class='textbox' style='width:380px;'></td>";
    echo "</tr>";

        $result = dbquery("SELECT * FROM ".DB_PREFIX."download_settings");
      $data = dbarray($result);

            if ($data['allow_url'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['482D']."</td>";
    echo "<td class='tbl'><input type='text' name='download_url' class='textbox' style='width:380px;'></td>";
    echo "</tr>";
            } if ($data['allow_filesize'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['487D']."</td>";
    echo "<td class='tbl'><input type='text' name='download_filesize' class='textbox' style='width:150px;'></td>";
    echo "</tr>";
            } if ($data['allow_upload'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['S102']."</td>";
    echo "<td class='tbl'><input type='file' name='upload' class='textbox' style='width:380px;' /></td>";
    echo "</tr>";
            }
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['483D']."</td>";
    echo "<td class='tbl'><select name='download_cat' class='textbox' style='width:150px;'>
$editlist</select></td>";
    echo "</tr>";

          if ($data['allow_license'] != '0' || $data['allow_lang'] != '0' || $data['allow_platform'] != '0' || $data['allow_os'] != '0' || $data['allow_mirror'] != '0' || $data['allow_rus'] != '0' || $data['allow_screen'] != '0' || $data['allow_button'] != '0') {
         
    echo "<tr>";
    echo "<td class='tbl2' align='center' colspan='2'><b>".$locale['417D']."</b></td>";
    echo "</tr>";
            }

            if ($data['allow_license'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['484D']."</td>";
    echo "<td class='tbl'><input type='text' name='download_license' class='textbox' style='width:150px;'></td>";
    echo "</tr>";
            } if ($data['allow_lang'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['445D']."</td>";
    echo "<td class='tbl'><input type='text' name='download_lang' class='textbox' style='width:150px;'></td>";
    echo "</tr>";
            } if ($data['allow_platform'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['486D']."</td>";
    echo "<td class='tbl'><input type='text' name='download_version' class='textbox' style='width:150px;'></td>";
    echo "</tr>";
            } if ($data['allow_os'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['448D'].":</td>";
    echo "<td class='tbl'><input type='text' name='download_os' class='textbox' style='width:380px;'></td>";
    echo "</tr>";
            } if ($data['allow_mirror'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['482_1D']."</td>";
    echo "<td class='tbl'><input type='text' name='mirror_url' class='textbox' style='width:380px;'></td></tr>";
    echo "</tr>";
            } if ($data['allow_rus'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['446D']."</td>";
    echo "<td class='tbl'><input type='text' name='download_rus' class='textbox' style='width:380px;'></td>";
    echo "</tr>";
            } if ($data['allow_screen'] == '1') {
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['442D']."</td>";
    echo "<td class='tbl'><input type='file' name='filename' class='textbox' style='width:380px;'></td>";
    echo "</tr>";
            } if ($data['allow_button'] == '1') {
    echo "<tr>";
    echo "<td class='tbl2' align='center' colspan='2'><b>".$locale['SA103']."</b></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['428D']."</td>";
    echo "<td class='tbl'><input type='text' name='download_button' class='textbox' style='width:380px;'></td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td width='80' class='tbl'>".$locale['429D']."</td>";
    echo "<td class='tbl'><input type='text' name='download_burl' class='textbox' style='width:380px;'></td>";
    echo "</tr>";
         }
   echo "<tr><td align='center' colspan='2' class='tbl'><br />";
    echo "<input type='submit' name='save_download' value='".$locale['488D']."' class='button'></td>";
    echo "</tr>";
    echo "</table>";
    echo "</form>\n";

        closetable();
      
        }
      
    } else { opentable($locale['S100']); echo "<div align='center'>".$locale['S101']."</div>\n";  closetable(); }

require_once THEMES."templates/footer.php";
?>
