<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program 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 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/********************************************************/
/* NSN Supporters(TM) Universal                         */
/* By: NukeScripts Network (webmaster@nukescripts.net)  */
/* http://www.nukescripts.net                           */
/* Copyright  2000-2005 by NukeScripts Network         */
/********************************************************/
defined('MAXIMUS') or Header("Location: ../../index.php");

$module_name = "Supporters";
get_lang($module_name);
$sqlsub = "SELECT * from `".TABLE_NSNSP_CONFIG."`";
if ( !($resultsub = $db->sql_query($sqlsub)) )
{ query_die(GENERAL_ERROR, 'Could not obtain nsnsp_config modules', '', __LINE__, __FILE__, $sqlsub);}
$supportersub = $db->sql_fetchrow($resultsub);

if ($supportersub['require_user'] == 0 || is_user($user)) {
switch ($op) {

    default:
    $pagetitle = _ADDSUPPORTER;
    $comefrom = $_SERVER['HTTP_REFERER'];
    $sip = $_SERVER['REMOTE_ADDR'];
    if(!is_array($user)) {
        $member = base64_decode($user);
        $member = explode(":", $member);
        $memname = "$member[1]";
    } else {
        $memname = "$user[1]";
    }
    $sql = "select `user_id`, `username`, `user_email`, `user_website` from `".TABLE_USERS."` where `username`='$memname'";
    if ( !($result = $db->sql_query($sql)) )
{ query_die(GENERAL_ERROR, 'Could not obtain users modules', '', __LINE__, __FILE__, $sql);}
    $row    = $db->sql_fetchrow($result);
    $suid   = $db->XMFnum($row['user_id']);
    $sname  = stripslashes($row['username']);
    $semail = stripslashes($row['user_email']);
    $surl   = stripslashes($row['user_website']);
    incoh();
    title(_ADDSUPPORTER);
    Max_OpenTable();
    echo "<center><b>"._ADDSUPPORTER."</b><br />"._ALLREQ."<br />\n";
    if (is_admin($admin)) {
        echo "<center>[ <a href='modules.php?name=$module_name&amp;file=admin'>"._GOTOADMIN."</a> ]\n";
    }
    echo "<table border='0'>\n";
    if ($supportersub['image_type']==0) { $enctype = ""; } else { $enctype = " enctype='multipart/form-data'"; }
    echo "<form action='modules.php?name=$module_name' method='post'$enctype>\n";
    echo "<input type='hidden' name='file' value='submit' />\n";
    echo "<input type='hidden' name='op' value='add_save' />\n";
    echo "<input type='hidden' name='user_id' value='$suid' />\n";
    echo "<tr><td><b>"._SITENAME.":</b></td><td><input type='text' name='site_name' size='50' /></td></tr>\n";
    echo "<tr><td><b>"._SITEURL.":</b></td><td><input type='text' name='site_url' size='50' value='$surl' /></td></tr>\n";
    if ($supportersub['image_type']==0) { $type = "type='input'"; } else { $type = "type='file'"; }
    echo "<tr><td valign='top'><b>"._SITEIMAGE.":</b></td><td><input $type name='site_image' size='50' />";
    echo "<br />"._MUSTBE;
    if ($supportersub['image_type']==0) { echo "<br />"._IMAGETYPE0; } else { echo "<br />"._IMAGETYPE1; }
    echo "</td></tr>\n";
    echo "<tr><td valign='top'><b>"._SITEDESCRIPTION.":</b></td><td><textarea rows='5' cols='50' name='site_description'></textarea></td></tr>\n";
    echo "<tr><td><b>"._SUPPORTERNAME.":</b></td><td><input type='test' name='user_name' value='$sname' size='40' /></td></tr>\n";
    echo "<tr><td><b>"._SUPPORTEREMAIL.":</b></td><td><input type='text' name='user_email' value='$semail' size='40' /></td></tr>\n";
    echo "<tr><td><b>"._SUPPORTERIP.":</b></td><td>$sip</td></tr>\n";
    echo "<input type='hidden' name='user_ip' value='$sip' />\n";
    echo "<input type='hidden' name='comefrom' value='$comefrom' />\n";
    echo "<tr><td align='center' colspan='2'><input type='submit' value='"._SADDSITE."' /></td></tr>\n";
    echo "</form></table></center>\n";
    Max_CloseTable();
    incof();
    break;

    case "add_save":
        if (($site_name=="")OR($site_url=="")OR($site_description=="")) {
            incoh();
            Max_OpenTable();
            echo "<center><b>"._MISSINGDATA."</b></center><br />\n";
            echo "<center>"._GOBACK."</center>\n";
            Max_CloseTable();
            incof();
            die();
        }
        if ($supportersub['image_type']==0) {
            $imgurl = $site_image;
        } else {
            list($newest_oid) = $db->sql_fetchrow($db->sql_query("SELECT max(site_id) AS newest_oid FROM ".TABLE_NSNSP_SITES.""));
            if ($newest_oid == "-1") { $new_oid = 1; } else { $new_oid = $newest_oid+1; }
            $oid = str_pad($new_oid, 6, "0", STR_PAD_LEFT);
            $imageurl_name = $_FILES['site_image']['name'];
            $imageurl_temp = $_FILES['site_image']['tmp_name'];
            $ext = substr($imageurl_name, strrpos($imageurl_name,'.'), 5);
            if (move_uploaded_file($imageurl_temp, "modules/$module_name/images/supporters/$oid$ext")) {
                chmod ("modules/$module_name/images/supporters/$oid$ext", 0777);
                $imgurl = "modules/$module_name/images/supporters/$oid$ext";
            } else {
                incoh();
                title(_CONFBANN);
                Max_OpenTable();
                echo "<center><b>"._NOUPLOAD."</b></center><br />\n";
                echo "<center>"._GOBACK."</center>\n";
                Max_CloseTable();
                incof();
                die();
            }
        }
        $result = $db->sql_query("INSERT INTO ".TABLE_NSNSP_SITES." values (NULL, '$site_name', '$site_url', '$imgurl', '0', '0', now(), '$site_description', '$user_id', '$user_name', '$user_email', '$user_ip')");
        if (!$result) {
            incoh();
            Max_OpenTable();
            echo "<center><b>"._DBERROR1."</b></center><br />\n";
            echo "<center>"._GOBACK."</center>\n";
            Max_CloseTable();
            incof();
            die();
        } else {
            $msg = "$sitename "._SUPPORTERDATE."\n\n";
            $msg .= _SITENAME.": $site_name\n";
            $msg .= _SITEURL.": $site_url\n";
            $msg .= _SITEIMAGE.": $imgurl\n";
            $msg .= _SITEDESCRIPTION.": $site_description\n";
            $msg .= _SUPPORTERID2.": $user_id\n";
            $msg .= _SUPPORTERNAME2.": $user_name\n";
            $msg .= _SUPPORTEREMAIL2.": $user_email\n";
            $msg .= _SUPPORTERIP2.": $user_ip\n";
            $to = $adminmail;
            $subject = "$sitename - "._SUPPORTERDATE;
            $mailheaders = "From: $adminmail\r\n";
            $mailheaders .= "Reply-To: $adminmail\r\n";
            $mailheaders .= "Return-Path: $adminmail\r\n";
            mail($to, $subject, $msg, $mailheaders);
        }
        redirect_url("$comefrom");
    break;

}
} else {
    redirect_url("index.php");
}