Пример #1
0
function currency_main($use_headers = True, $return_output = FALSE)
{
    ob_start();
    try {
        $page = array();
        $currency = new currency($GLOBALS['currency_db_base'], $GLOBALS['currency_db_user'], $GLOBALS['currency_db_password']);
        if (!isset($_GET['command'])) {
            $command = 'get_note';
        } else {
            $command = $_GET['command'];
        }
        if (!isset($_GET['id'])) {
            $id = 0;
        } else {
            $id = $_GET['id'];
        }
        if ($command == 'get_note') {
            if (isset($_GET['count'])) {
                $count = $_GET['count'];
            } else {
                $count = 1;
            }
            $autocommit = FALSE;
            if (isset($_GET['autocommit'])) {
                $autocommit = $_GET['autocommit'];
            }
            for ($i = 0; $i < $count; $i++) {
                if ($autocommit == 'yes') {
                    $note = $currency->_get_note($_GET['value'], 'y', $id);
                } else {
                    $note = $currency->get_note($_GET['value'], $id);
                }
                array_push($page, join("\t", $note));
            }
        } elseif ($command == 'merge_notes') {
            if (isset($_GET['values'])) {
                if ($_GET['values'] == '') {
                    $_GET['values'] = array();
                }
            } else {
                $_GET['values'] = FALSE;
            }
            $notes = $currency->merge_notes_columns($_GET['serial'], $_GET['name'], $_GET['value'], $id, $_GET['values']);
            foreach ($notes as $note) {
                array_push($page, join("\t", $note));
            }
        } elseif ($command == 'break_note') {
            if (isset($_GET['values'])) {
                if ($_GET['values'] == '') {
                    $_GET['values'] = array();
                }
            } else {
                $_GET['values'] = FALSE;
            }
            $notes = $currency->break_note($_GET['serial'], $_GET['name'], $_GET['value'], $id, $_GET['values']);
            foreach ($notes as $note) {
                array_push($page, join("\t", $note));
            }
        } elseif ($command == 'change_note') {
            $note = $currency->change_note($_GET['serial'], $_GET['name'], $_GET['value'], $id);
            array_push($page, join("\t", $note));
        } elseif ($command == 'check_note') {
            $note = $currency->check_note($_GET['serial'], $_GET['name'], $_GET['value'], $id);
            array_push($page, join("\t", $note));
        } elseif ($command == 'commit') {
            array_push($page, $currency->commit($_GET['transaction_id']));
        } elseif ($command == 'put_note') {
            $currency->put_note($_GET['serial'], $_GET['name'], $_GET['value']);
            array_push($page, "OK");
        } else {
            throw new Exception("unknown command " . $command);
        }
        print join("\n", $page);
        $status = true;
    } catch (Exception $error) {
        print $error->getMessage();
        $status = FALSE;
    }
    if ($use_headers) {
        if ($status) {
            header('Content-type: text/plain');
        } else {
            header('HTTP/1.0 500 Internal Server Error');
            error_log(ob_get_contents());
        }
    }
    if ($return_output) {
        $status = ob_get_contents();
        error_log($status);
        ob_end_clean();
    } else {
        ob_end_flush();
    }
    return $status;
}