<?php

@( (bool)FALSE === strpos( $_SERVER['REQUEST_URI'], basename( __FILE__ ) ) ) OR exit( '!! ACCESS DENIED !!' );
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: polls.php,v 1.6.2.6 2004/08/15 12:58:04 kirilt Exp $




/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)            */
/* http://phpnuke.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.       */
/************************************************************************/

checkAccess( 'blocks', array( 'survey', 'left' ) );

$hlpfile = getHelpFile("surveys.html");

/*********************************************************/
/* Poll/Surveys Functions                                */
/*********************************************************/

function poll_createPoll() {
    global $language, $hlpfile, $admin, $maxOptions,$phpEx, $mpnTables;
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    ?>
    <font size=4><b><center><?php echo translate("Create new poll"); ?><br></font>
    <font size=2>
    <?php echo "<a href=\"admin.$phpEx?op=remove\">".translate("Delete Polls"); ?></a><br><br></font></center>
    <?php echo "<form action=\"admin.$phpEx\" method=\"post\">"; ?>
    <input type="hidden" name="op" value="createPosted">
    <p><?php echo translate("Polltitle"); ?>: <input class=textbox type=text name="pollTitle" size=50 maxlength=100></p>
    <p><?php echo translate("Please enter each available option into a single field"); ?></p>
    <table>
    <?PHP
    for($i = 1; $i <= $maxOptions; $i++)
    {
        echo "<tr>";
        echo "<td>".translate("Option")." $i:</td><td><input class=textbox type=text name=\"optionText[$i]\" size=50 maxlength=50></td>";
        echo "</tr>";
    }

    echo "</tr></table>";

    // Kiril: New Sanitizer

        echo "<input type='hidden' name='stripit_form' value='pollTitle|optionText' />\n";
        echo "<input type='hidden' name='mpn_sectype' value='2' />\n";

    // End Sanitizer

    echo "<input class=\"button\" type=\"submit\" value=\"".translate("Create")."\">";
    echo "</form></td></tr></table></td></tr></table>";
    include ("footer.$phpEx");
}

function old_poll_createPosted() {
    global $maxOptions, $pollTitle, $optionText, $mpnTables,$phpEx;
    $timeStamp = time();
    $result = mysql_query("INSERT INTO ".$mpnTables['poll_desc']." VALUES (NULL, '$pollTitle', '$timeStamp')");
    if (!$result)   {
        echo mysql_errno(). ": ".mysql_error(). "<br>";
        return;
    }
    mysql_free_result($result);

    // create option records in data table
    for($i = 1; $i <= $maxOptions; $i++) {
        if($optionText[$i] != "")
            $result = mysql_query("INSERT INTO ".$mpnTables['poll_data']." VALUES ($id, '$optionText[$i]', 0, $i)");
            if (!result) {
                echo mysql_errno(). ": ".mysql_error(). "<br>";
                return;
            }
            mysql_free_result($result);
    }

    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=adminMain' );

}

function poll_createPosted() {
    global $maxOptions, $pollTitle, $optionText, $mpnTables,$phpEx;
    $timeStamp = time();
   // $pollTitle = FixQuotes($pollTitle);
    if(!mysql_query("INSERT INTO ".$mpnTables['poll_desc']." VALUES (NULL, '$pollTitle', '$timeStamp', 0)")) {
        echo mysql_errno(). ": ".mysql_error(). "<br>";
        return;
    }
    $object = mysql_fetch_object(mysql_query("SELECT pollID FROM ".$mpnTables['poll_desc']." WHERE pollTitle='$pollTitle'"));
    $id = $object->pollID;

    for($i = 1; $i <= sizeof($optionText); $i++) {
        if($optionText[$i] != "")
            //$optionText[$i] = FixQuotes($optionText[$i]);
            if(!mysql_query("INSERT INTO ".$mpnTables['poll_data']." (pollID, optionText, optionCount, voteID) VALUES ($id, '$optionText[$i]', 0, $i)")) {
                echo mysql_errno(). ": ".mysql_error(). "<br>";
                return;
            }
    }

    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=adminMain' );
}

function poll_removePoll() {
    global $hlpfile, $admin, $mpnTables,$phpEx;
    $hlpfile = getHelpFile("surveys.html");
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    ?>
    <font size=4><b><center><?php echo translate("Remove an existing poll"); ?></b></center><br><br></font>
    <font size=2><center><?php echo translate("WARNING: The chosen poll will be removed IMMEDIATELY from the database!"); ?></center>
    <p><?php echo translate("Please choose a poll from the list below."); ?></p>
    <?php echo "<form action=\"admin.$phpEx\" method=\"post\">"; ?>
    <input type="hidden" name="op" value="removePosted">
    <table>

    <?PHP
    $result = mysql_query("SELECT pollID, pollTitle, timeStamp FROM ".$mpnTables['poll_desc']." ORDER BY timeStamp");
    if(!$result) {
        echo mysql_errno(). ": ".mysql_error(). "<br>";
        return;
    }

    // cycle through the descriptions until everyone has been fetched
    while($object = mysql_fetch_object($result)) {
        $pollID = $object->pollID;
        echo "<tr><td><input type=\"radio\" name=\"id\" value=\"".$object->pollID."\">".$object->pollTitle."</td></tr>";
    }

    echo "</table>";
    echo "<input class=\"button\" type=\"submit\" value=\"".translate("Remove")."\">";
    echo "</form></td></tr></table></td></tr></table>";
    include ("footer.$phpEx");
}

function poll_removePosted() {
    global $id, $mpnTables,$phpEx;
    mysql_query("DELETE FROM ".$mpnTables['poll_desc']." WHERE pollID=$id");
    mysql_query("DELETE FROM ".$mpnTables['poll_data']." WHERE pollID=$id");

    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=adminMain' );
}

function poll_viewPoll() {
    global $mpnTables,$phpEx;
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    echo "<font size=4>".translate("View poll results")."</font>";
    echo "<p>";

    // select all descriptions
    $result = mysql_query("SELECT pollID, pollTitle, timeStamp FROM ".$mpnTables['poll_desc']." ORDER BY timeStamp");
    if(!$result) {
        echo mysql_errno(). ": ".mysql_error(). "<br>";
        return;
    }

    echo "<form action=\"." . $_SERVER['PHP_SELF'] . "\" method=\"post\">";
    echo "<input type=\"hidden\" name=\"op\" value=\"viewPosted\">";
    echo "<table>";

    // cycle through the descriptions until everyone has been fetched
    while($object = mysql_fetch_object($result)) {
        echo "<tr><td><input type=\"radio\" name=\"id\" value=\"".$object->pollID."\">".$object->pollTitle."</td></tr>";
    }
    echo "</table>";
    echo "<input class=\"button\" type=\"submit\" value=\"".translate("View")."\">";
    echo "</form>";
    include ("footer.$phpEx");
}

function poll_viewPosted() {
    global $phpEx;
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    global $id;
    echo "<font size=4>".translate("View poll results")."</font><p>";
    pollResults($id);
    include ("footer.$phpEx");
}


switch($op) {

        case "create":
            poll_createPoll();
            break;

        case "createPosted":
            poll_createPosted();
            break;

        case "poll_editPoll":
            poll_editPoll($pollID);
            break;

        case "ChangePoll":
            ChangePoll($pollID, $pollTitle, $optionText, $optionCount, $voteID);
            break;

        case "remove":
            poll_removePoll();
            break;

        case "removePosted":
            poll_removePosted();
            break;

        case "view":
            poll_viewPoll();
            break;

        case "viewPosted":
            poll_viewPosted();
            break;

}

?>
