<?
/*
 // +--------------------------------------------------------------------------+
 // | Project:    NVTracker - NetVision BitTorrent Tracker                     |
 // +--------------------------------------------------------------------------+
 // | This file is part of NVTracker. NVTracker is based on BTSource,          |
 // | originally by RedBeard of TorrentBits, extensively modified by           |
 // | Gartenzwerg.                                                             |
 // |                                                                          |
 // | NVTracker 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, or        |
 // | (at your option) any later version.                                      |
 // |                                                                          |
 // | NVTracker 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 NVTracker; if not, write to the Free Software Foundation,     |
 // | Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            |
 // +--------------------------------------------------------------------------+
 // | Obige Zeilen dürfen nicht entfernt werden!    Do not remove above lines! |
 // +--------------------------------------------------------------------------+
 */

require "include/bittorrent.php";

function mkbytes($amount, $unit)
{
    switch($unit)
    {
        case "GB":
            return 1024 * 1024 * 1024 * $amount;
            break;
        case "MB":
            return 1024 * 1024 * $amount;
            break;
        default:
            return $amount;
    }
}

dbconn();
loggedinorreturn();

if (get_user_class() < UC_SYSOP) stderr("Fehler!", "Zugriff Verweigert!");

if ($_POST['message'] != "" && $_POST["subject"] != "")
{
    // get the checkbox group
    $class= $_POST["class"];
    
    // check for empty choice
    if (!isset($class)) stderr("Fehler!", "Bitte mindestens einen Benutzerrang als Empfänger auswählen!");
    
    // check for empty or negative amount
    if ($_POST["amount"] <= 0) stderr("Fehler!", "Der Betrag muss positiv und größer als 0 sein!");
    
    $num=0;
    foreach($class as $x)
    {
        if (substr($x,0,3) == "UC_")
        {
            if ($num < count($class) - 1) $querystring .= " class = " . constant($x) . " OR";
            else $querystring .= " class = " . constant($x);
            $classnames .= get_user_class_name(constant($x)).", ";
            $num++;
        }
    }
    
    if ($num == intval($_POST["numclasses"]))
    {
        // update upload
        $res = mysql_query("UPDATE `users` SET `uploaded` = `uploaded` + ". mkbytes($_POST["amount"], $_POST["unit"])) or sqlerr(__FILE__, __LINE__);
        // select users for pm
        $res = mysql_query("SELECT id FROM users");
        // message text
        $msg = $_POST['message']  . "\n\nNOTIZ: Dies ist eine Upload Gutschrift über ".$_POST["amount"]." ".$_POST["unit"]." für alle Benutzer";
    }
    else
    {
        // update upload
        $res = mysql_query("UPDATE `users` SET `uploaded` = `uploaded` + ". mkbytes($_POST["amount"], $_POST["unit"])." where".$querystring) or sqlerr(__FILE__, __LINE__);
        // select users for pm
        $res = mysql_query("SELECT id FROM users where".$querystring) or sqlerr(__FILE__, __LINE__);
        // message text
        $msg = $_POST['message']  . "\n\nNOTIZ: Dies ist eine Upload Gutschrift über ".$_POST["amount"]." ".$_POST["unit"]." für folgende Benutzerränge: " . substr($classnames,0,(strlen($classnames)-2));
    }
    
    $subject = $_POST['subject'];
    
    if ($_POST["fromsystem"] == "yes") $sender_id="0";
    else $sender_id = $CURUSER["id"];
    
    while($arr = mysql_fetch_row($res))
    {
        sendPersonalMessage($sender_id, $arr[0], $subject, $msg, PM_FOLDERID_INBOX, 0);
    }
    
    stderr("Erfolg!", "Massen Upload Gutschrift wurde erfolgreich gutgeschrieben an folgende Benutzerränge:<br>" . substr($classnames,0,(strlen($classnames)-2)) . "<br><a href=\"massgift.php\">Zurück</a>");
}else if ($HTTP_SERVER_VARS["REQUEST_METHOD"] == "POST") stderr("Fehler!", "Bitte die Felder Betreff und Nachricht ausfüllen!");

stdhead("Massen Upload Gutschrift");

?>
<form method=post action="massgift.php" name="massgift">
<table cellpadding="4" cellspacing="1" border="0" style="width:<?=$GLOBALS["WIDTH"]?>" class="tableinborder">
    <tr class="tabletitle" style="width:100%">
        <td colspan="10" width="100%" style="text-align: center"><b>Upload Gutschrift - Massen System</b></td>
    </tr>
    <tr>
        <td width="100%" class="tablea"><br>
            <table width="<?=$GLOBALS["WIDTH"]?>" border="0" cellspacing="1" cellpadding="4" align="center" class="tableinborder">
                <tr align="center">
                    <td colspan=2 class="tabletitle"><b>Upload Gutschrift an ausgew&auml;hlte R&auml;nge</b></td>
                </tr>
                <tr>
                    <td class=tablea colspan=2>
                        <table class="tablea" style="width:100%">
                        <?
                            // determine user classes
                            $numclasses=0;
                            $constants = get_defined_constants();
                            foreach(array_keys($constants) as $x)
                            {
                                if (substr($x,0,3) == "UC_")
                                {
                                    echo "<tr><td><input name=\"class[]\" value=\"".$x."\" type=\"checkbox\">".get_user_class_name($constants[$x])."<br></td></tr>\n";
                                    $numclasses++;
                                }
                            }
                        ?>
                            <tr>
                                <td><input type="button" value="Alle ausw&auml;hlen" name="select" onclick="this.value=check(document.massgift['class[]'])"></td>
                            </tr>
                        </table>
                        <input type="hidden" name="numclasses" value="<? echo $numclasses; ?>" />
                    </td>
                </tr>
                <tr>
                    <td class="tablea">Betreff</td>
                    <td class="tablea"><input type=text size=100 name="subject" /></td>
                </tr>
                <tr>
                    <td class="tablea">Upload Menge</td>
                    <td class="tablea"><input type=text size=10 name="amount" /> <select name="unit"><option>MB</option><option>GB</option></select></td>
                </tr>
                <tr>
                    <td class="tablea">Nachricht</td>
                    <td class="tablea"><textarea cols=96 rows=10 name="message"></textarea></td>
                </tr>
                <tr>
                    <td class="tablea">Als System</td>
                    <td class="tablea"><input type="checkbox" name="fromsystem" value="yes" />Den Versender als System angeben statt des Namen</td>
                </tr>
                <tr>
                    <td class=tabletitle align="center" colspan=2><input type="submit" value="Abschicken" class="btn" /></td>
                </tr>
            </table><br>
        </td>
    </tr>
</table>
</form>
<?

stdfoot();

?> 