<?php
/*
 * Copyright (c) 2004-2009 Luiz Otavio O Souza <loos.br@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* 
 * $Id: save_default_op.php 102 2009-02-05 18:37:40Z loos-br $
 */

    require('default.array.option.inc.php');
    require('default.array.cmd.inc.php');
    require('mysql.inc.php');

    // Date in the past
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    // always modified
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

    // HTTP/1.1
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);

    // HTTP/1.0
    header("Pragma: no-cache");

    header("Content-Type: text/html; charset=iso-8859-1");

    $checkbox = 0;
    $checkboxArray = '';
    $sql = "UPDATE defaults ";

    $found = 0;
    for($o = 0; ; $o++) {

        if (!$arrayOption[$o])
            break;

        if (strcasecmp($_REQUEST['name'], $arrayOption[$o]['name']) == 0) {
            $sql .= "SET commands = ";
            $checkbox = 1;
            $checkboxArray = $arrayOption[$o]['cmd'];
            $found = 1;
        }
    }

    switch ($_REQUEST['name']) {
	case "internal_host":
	    $sql .= "SET internal_host = ";
	    break;
	case "connect":
	    $sql .= "SET connect = ";
	    break;
	case "save_msg":
	    $sql .= "SET save_msg = ";
	    break;
	case "save_contacts":
	    $sql .= "SET save_contacts = ";
	    break;
	default:
	    if ($found == 1) break;
	    echo "fail";
	    exit;
    }

    if ($checkbox == 1) {
	if ($_REQUEST['value'] == "true")
	    $sql .= "commands | $arrayCMD[$checkboxArray]";
	else
	    $sql .= "commands & ~$arrayCMD[$checkboxArray]";
    } else
	$sql .= "'".$_REQUEST['value']."'";

    if (!mysql_query($sql, $mysql))
	echo "fail";
    echo "ok";
?>
