function output_array($array, $prefix = "")
{
    $out = "";
    while (list($key, $val) = @each($array)) {
        $out .= $prefix . "[{$key}] = ";
        if (is_array($val)) {
            $out .= "array{\n" . output_array($val, $prefix . "[{$key}]") . "\n}\n";
        } else {
            $out .= $val . "\n";
        }
    }
    return $out;
}
function calculation_array($array)
{
    if ($_POST['task_type'] == '1') {
        echo "<h2>Task type: 1</h2>";
        echo '<div class="col-sm-6"><h3>Created array:</h3>';
        echo output_array($array) . '</div>';
        $result = calculation_type_1($array);
        echo '<div class="col-sm-6"><h3>After calculation:</h3>';
        echo '<p>Number of negative elements: ' . $result['negative_elements_counter'] . '</p><p>Multiplied segment: ' . $result['multiplied_segment'] . '</p>';
        echo '</div>';
        return;
    } elseif ($_POST['task_type'] == '2') {
        echo "<h2>Task type: 2</h2>";
        echo '<div class="col-sm-6"><h3>Before calculation:</h3>';
        echo output_array($array) . '</div>';
        echo '<div class="col-sm-6"><h3>After calculation:</h3>';
        echo output_array(calculation_type_2($array)) . '</div>';
        return;
    } else {
        return "<h1>Error! Task type undefined!<h1>";
    }
}
function sql_error($sql)
{
    global $session;
    return output_array($session) . "SQL = <pre>{$sql}</pre>" . db_error(LINK);
}
             require_once "lib/sanitize.php";
             $name = color_sanitize($session['user']['name']);
             $url = getsetting("serverurl", "http://" . $_SERVER['SERVER_NAME'] . ($_SERVER['SERVER_PORT'] == 80 ? "" : ":" . $_SERVER['SERVER_PORT']) . dirname($_SERVER['REQUEST_URI']));
             if (!preg_match("/\\/\$/", $url)) {
                 $url = $url . "/";
                 savesetting("serverurl", $url);
             }
             $tl_server = translate_inline("Server");
             $tl_author = translate_inline("Author");
             $tl_date = translate_inline("Date");
             $tl_body = translate_inline("Body");
             $tl_subject = sprintf_translate("New LoGD Petition at %s", $url);
             $msg = "{$tl_server}: {$url}\n";
             $msg .= "{$tl_author}: {$name}\n";
             $msg .= "{$tl_date} : {$date}\n";
             $msg .= "{$tl_body} :\n" . output_array($post) . "\n";
             mail(getsetting("gameadminemail", "*****@*****.**"), $tl_subject, $msg);
         }
         $session['user']['password'] = $p;
         output("Your petition has been sent to the server admin.");
         output("Please be patient, most server admins have jobs and obligations beyond their game, so sometimes responses will take a while to be received.");
     } else {
         output("`\$There was a problem with your petition!`n");
         output("`@Please read the information below carefully; there was a problem with your petition, and it was not submitted.\n");
         rawoutput("<blockquote>");
         output($post['cancelreason']);
         rawoutput("</blockquote>");
     }
 } else {
     output("`\$`bError:`b There have already been %s petitions filed from your network in the last day; to prevent abuse of the petition system, you must wait until there have been 5 or fewer within the last 24 hours.", $row['c']);
     output("If you have multiple issues to bring up with the staff of this server, you might think about consolidating those issues to reduce the overall number of petitions you file.");
		unset($session[user][password]);
		/*
		mail(getsetting("gameadminemail","postmaster@localhost"),"LoGD Petition",output_array($_POST,"POST:").output_array($session,"Session:"));
		$sql = "SELECT acctid FROM accounts WHERE emailaddress='".getsetting("gameadminemail","postmaster@localhost")."'";
		//output($sql);
		$result = db_query($sql);
		if (db_num_rows($result)==0){
			$sql = "SELECT acctid FROM accounts WHERE superuser>=3";
			$result = db_query($sql);
		}
		for ($i=0;$i<db_num_rows($result);$i++){
			$row = db_fetch_assoc($result);
			systemmail($row[acctid],"Petition",output_array($_POST),(int)$session[user][acctid]);
		}
		*/
		$sql = "INSERT INTO petitions (author,date,body,pageinfo) VALUES (".(int)$session[user][acctid].",now(),\"".addslashes(output_array($_POST))."\",\"".addslashes(output_array($session,"Session:"))."\")";
		db_query($sql);
		$session[user][password]=$p;
		output("Your petition has been sent to the server admin.  Please be patient, most server admins
		have jobs and obligations beyond their game, so sometimes responses will take a while to be received.");
		
	}else{
		output("<form action='petition.php?op=submit' method='POST'>
		Your Character's Name: <input name='charname'>`n
		Your email address: <input name='email'>`n
		Description of the problem:`n
		<textarea name='description' cols='30' rows='5' class='input'></textarea>`n
		<input type='submit' class='button' value='Submit'>`n
		Please be as descriptive as possible in your petition.  If you have questions about how the game works,
		please check out the <a href='petition.php?op=faq'>FAQ</a>.  Petitions about game mechanics will more than
		likely not be answered unless they have something to do with a bug.