function choose_step()
{
    global $_GET;
    extract($_GET);
    if (isset($id)) {
        require_lib("validate");
        $v =& new Validate();
        if (!$v->isOk($id, "num", 1, 9, "")) {
            return "<li class=err>Invalid site entry id</li>";
        }
    }
    $step = 1;
    switch ($step) {
        case "0":
            if (!isset($msg)) {
                $msg = "";
            }
            $OUTPUT = "{$msg}";
            break;
        case "1":
            $OUTPUT = "<script>document.location.href='" . urler(PURCHASE_URL . "?" . sendhash()) . "';</script>";
            break;
    }
    return $OUTPUT;
}
function choose_step()
{
    global $_GET;
    extract($_GET);
    if (isset($id)) {
        require_lib("validate");
        $v =& new Validate();
        if (!$v->isOk($id, "num", 1, 9, "")) {
            return "<li class=err>Invalid site entry id</li>";
        }
    }
    $step = 1;
    switch ($step) {
        case "0":
            if (!isset($msg)) {
                $msg = "";
            }
            $OUTPUT = "{$msg}";
            break;
        case "1":
            $request = @file(urler(REPORTS_URL . "?" . sendhash()));
            if ($request == false) {
                $site_msg = "<li class=err>Connection to server failed. Check you internet connection and try again.</li>";
                return $site_msg;
            }
            $OUTPUT = implode("", $request);
            break;
    }
    return $OUTPUT;
}
function choose_step()
{
    global $_GET;
    extract($_GET);
    switch ($step) {
        case "0":
            if (!isset($msg)) {
                $msg = "";
            }
            $OUTPUT = "{$msg}";
            break;
        case "1":
            $OUTPUT = "\r\n\t\t<h3>General Message</h3>\r\n\t\t<form method=get action='" . SELF . "'>\r\n\t\t<input type=hidden name=step value='2'>\r\n\t\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t\t<tr>\r\n\t\t\t<th colspan=2>Message Details</th>\r\n\t\t</tr>\r\n\t\t<tr class='bg-odd'>\r\n\t\t\t<td>Cell Number:</td>\r\n\t\t\t<td><input name=cellnum type=text></td>\r\n\t\t</tr>\r\n\t\t<tr class='bg-even'>\r\n\t\t\t<td>Message</td>\r\n\t\t\t<td><textarea cols=25 rows=4 name=message></textarea></td>\r\n\t\t</tr>\r\n\t\t<tr>\r\n\t\t\t<td colspan=2 align=center><input type=submit value='Send'></td>\r\n\t\t</tr>\r\n\t\t</table>\r\n\t\t</form>";
            break;
        case "2":
            $message = str_replace("=", "|", base64_encode($message));
            $request = @file(urler(GENERALMSG_URL . "?cellnum={$cellnum}&message={$message}&" . sendhash()));
            if ($request == false) {
                return "<li class=err>Connection failed. Check your internet connection and try again.</li>";
            }
            $OUTPUT = implode("", $request);
            break;
    }
    return $OUTPUT;
}
function choose_step()
{
    $request = @file(urler(READCREDITS_URL . "?" . sendhash()));
    if ($request == false) {
        return "<li class=err>Connection failed. Check your internet connection and try again.</li>";
    }
    $OUTPUT = implode("", $request);
    return $OUTPUT;
}
function search()
{
    global $_GET;
    extract($_GET);
    require_lib("validate");
    $v =& new Validate();
    $v->isOk($idnum, "string", 0, 100, "Invalid id / registration number.");
    if ($v->isError()) {
        $err = "";
        foreach ($v->getErrors() as $key => $value) {
            $err .= "<li class=err>{$value['msg']}</li>";
        }
        return enter($err);
    }
    // post the search request
    $search_request = @file(urler(BLACKLIST_SEARCH_URL . "?idnum={$idnum}&" . sendhash()));
    if ($search_request == false) {
        $site_msg = "<li class=err>Connection to server failed. Check you internet connection and try again.</li>";
    } else {
        $site_msg = "";
        $status = 0;
        // 0 = none, 1 = read message
        foreach ($search_request as $value) {
            $value = str_replace("\n", "", $value);
            switch ($value) {
                case "<DR_E>":
                    break;
                case "<DR_M>":
                    $status = 1;
                    break;
                case "</DR_M>":
                    $status = 0;
                    break;
                case "</DR_E>":
                    $status = 0;
                    break;
                default:
                    if ($status == 1) {
                        $site_msg .= "{$value}\n";
                    }
            }
        }
    }
    $OUTPUT = "\r\n\t<h3>Search Black / White List Registry</h3>";
    foreach ($_GET as $key => $value) {
        if ($key != "key") {
            $OUTPUT .= "<input type=hidden name='{$key}' value='{$value}'>";
        }
    }
    $OUTPUT .= "\r\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t<tr>\r\n\t\t<th>Data Returned</th>\r\n\t</tr>\r\n\t<tr class='bg-odd'>\r\n\t\t<td valign=top>{$site_msg}</td>\r\n\t</tr>\r\n\t</table>";
    return $OUTPUT;
}
#Developers, Software Vendors, Support, Accountants, Users
#
#
#The full software license can be found here:
#http://www.accounting-123.com/a.php?a=153/GPLv3
#
#
#
#
#
#
#
#
#
#
#
require "settings.php";
require "https_urlsettings.php";
db_conn_maint("cubit");
$sql = "SELECT * FROM version";
$rslt = db_exec($sql) or errDie("Error fetching version info.");
$version = pg_fetch_result($rslt, 0, 0);
// post the search request
$update_request = @file(urler(UPDATE_URL . "?version={$version}&" . sendhash()));
if ($update_request == false) {
    $site_msg = "<li class=err>Connection to server failed. Check you internet connection and try again.</li>";
} else {
    $site_msg = implode("<br>", $update_request);
}
$OUTPUT = "\r\n<h3>Cubit Update Download</h3>\r\n{$site_msg}";
require "template.php";
function submit()
{
    global $_GET;
    extract($_GET);
    require_lib("validate");
    $v =& new Validate();
    $v->isOk($idnum, "string", 0, 100, "Invalid id / registration number.");
    $v->isOk($name, "string", 0, 100, "Invalid tenant full name.");
    $v->isOk($surname, "string", 0, 100, "Invalid tenant surname.");
    $v->isOk($comment, "string", 0, 100, "Invalid tenant comment.");
    $v->isOk($personname, "string", 0, 100, "Invalid listing person name.");
    $v->isOk($persontel, "string", 0, 100, "Invalid listing person telephone number.");
    if (isset($paying)) {
        $v->isOk($paying, "string", 0, 100, "Invalid Paying quality selection.");
    }
    if (isset($person)) {
        $v->isOk($person, "string", 0, 100, "Invalid Person quality selection.");
    }
    if (isset($dobusiness)) {
        $v->isOk($dobusiness, "string", 0, 100, "Invalid 'Do Business' value.");
    }
    if (!isset($paying)) {
        $v->addError("", "Invalid Paying quality selection.");
    }
    if (!isset($person)) {
        $v->addError("", "Invalid Person quality selection.");
    }
    if (!isset($dobusiness)) {
        $v->addError("", "Invalid 'Do Business' value.");
    }
    if ($v->isError()) {
        $err = "";
        foreach ($v->getErrors() as $key => $value) {
            $err .= "<li class=err>{$value['msg']}</li>";
        }
        return enter($err);
    }
    $comment = str_replace("=", "|", base64_encode($comment));
    // post the search request
    $search_request = @file(urler(BLACKLIST_SUBMIT_URL . "?idnum={$idnum}&name={$name}&surname={$surname}&comment={$comment}&personname={$personname}&persontel={$persontel}&paying={$paying}&person={$person}&dobusiness={$dobusiness}&" . sendhash()));
    if ($search_request == false) {
        $site_msg = "<li class=err>Connection to server failed. Check you internet connection and try again.</li>";
    } else {
        $site_msg = "";
        $status = 0;
        // 0 = none, 1 = read message
        foreach ($search_request as $value) {
            $value = str_replace("\n", "", $value);
            switch (trim($value)) {
                case "<DR_E>":
                    break;
                case "<DR_M>":
                    $status = 1;
                    break;
                case "</DR_M>":
                    $status = 0;
                    break;
                case "</DR_E>":
                    $status = 0;
                    break;
                default:
                    if ($status == 1) {
                        $site_msg .= "{$value}\n";
                    }
            }
        }
        $site_msg = nl2br($site_msg);
    }
    $OUTPUT = "\r\n\t<h3>Create Black / White List Registry Entry</h3>";
    foreach ($_GET as $key => $value) {
        if ($key != "key") {
            $OUTPUT .= "<input type=hidden name='{$key}' value='{$value}'>";
        }
    }
    $OUTPUT .= "\r\n\t<table cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t<tr>\r\n\t\t<th>Data Returned</th>\r\n\t</tr>\r\n\t<tr class='bg-odd'>\r\n\t\t<td>{$site_msg}</td>\r\n\t</tr>\r\n\t</table>";
    return $OUTPUT;
}
function choose_step()
{
    global $_GET;
    extract($_GET);
    require_lib("validate");
    $v =& new Validate();
    switch ($step) {
        case "0":
            if (!isset($msg)) {
                $msg = "";
            }
            $OUTPUT = "{$msg}";
            break;
        case "1":
            $OUTPUT = enter("");
            break;
        case "2":
            $contact_form = "";
            $custs_list = "";
            // create the list of customers
            if (!isset($send_all) && isset($custs) && is_array($custs)) {
                foreach ($custs as $key => $value) {
                    if (!$v->isOk($value, "num", 1, 9, "")) {
                        continue;
                    }
                    db_conn("cubit");
                    $rslt = db_exec("SELECT * FROM customers WHERE cusnum = '{$value}'") or errDie("Error reading buyer.");
                    if (pg_num_rows($rslt) < 1) {
                        continue;
                    }
                    $row = pg_fetch_array($rslt);
                    $contact_form .= "<input type=hidden name='custs[{$key}]' value='{$value}'>";
                    $custs_list .= "{$row['cusname']} {$row['surname']}<br>";
                }
            }
            if (isset($send_all)) {
                $contact_form = "<input type=hidden name=send_all value=true>";
                $custs_list = "All Customers";
            }
            if (empty($contact_form)) {
                $OUTPUT = enter("<li class=err>Please select a valid customer from the list.</li>");
                return $OUTPUT;
            }
            $OUTPUT = "<h3>General Message</h3>\r\n\t\t\t<form name=msgform method=post action='" . SELF . "'>\r\n\t\t\t<input type=hidden name=msg value='{$msg}'>\r\n\t\t\t<input type=hidden name=step value=3>\r\n\t\t\t{$contact_form}\r\n\t\t\t<table width=700 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t\t\t<tr class='bg-odd'>\r\n\t\t\t\t<td valign=top>\r\n\t\t\t\t{$msg}\r\n\t\t\t\t</td>\r\n\t\t\t\t<td valign=top>\r\n\t\t\t\t\t<center><h3>Format Characters</h3></center>\r\n\t\t\t\t\t@name - Customer name<br>\r\n\t\t\t\t\t@surname - Customer surname (Blank with Buyers/Possible Tenants)<br>\r\n\t\t\t\t\t@balance - Customer balance (Blank with Possible Tenants)<br>\r\n\t\t\t\t\t<br>&nbsp;\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr class='bg-even'>\r\n\t\t\t\t<td colspan=2>\r\n\t\t\t\t<table width=100%>\r\n\t\t\t\t<tr>\r\n\t\t\t\t\t<td><b>Customers to send message to:</b></td>\r\n\t\t\t\t</tr>\r\n\t\t\t\t<tr>\r\n\t\t\t\t\t<td nowrap>\r\n\t\t\t\t\t{$custs_list}\r\n\t\t\t\t\t</td>\r\n\t\t\t\t</tr>\r\n\t\t\t\t</table>\r\n\t\t\t\t</td>\r\n\t\t\t</tr>\r\n\t\t\t<tr>\r\n\t\t\t\t<td colspan=2 align=center><input type=submit value='Send'></td>\r\n\t\t\t</tr>\r\n\t\t\t</table>\r\n\t\t\t</form>";
            break;
        case "3":
            $OUTPUT = "\r\n\t\t<form method=post name=dataform action='" . BULKMSGS_URL . "?" . sendhash() . "'>";
            $i = 0;
            if (isset($send_all)) {
                db_conn("cubit");
                $sql = "SELECT * FROM customers";
                $rslt = db_exec($sql) or errDie("Error reading customers list.");
                $custs = array();
                while ($row = pg_fetch_array($rslt)) {
                    $custs[] = "{$row['cusnum']}";
                }
            } else {
                if (!isset($custs) || !is_array($custs)) {
                    $custs = array();
                }
            }
            // buyers
            foreach ($custs as $key => $value) {
                db_conn("cubit");
                $sql = "SELECT * FROM customers WHERE cusnum='{$value}'";
                $rslt = db_exec($sql) or errDie("Error reading customers list.");
                while ($row = pg_fetch_array($rslt)) {
                    if (!empty($row["cellno"])) {
                        $cusbalance = "R " . sprint($row["balance"]);
                        $smsg = $msg;
                        $smsg = str_replace("@name", $row["cusname"], $smsg);
                        $smsg = str_replace("@surname", $row["surname"], $smsg);
                        $smsg = str_replace("@balance", "{$cusbalance}", $smsg);
                        $smsg = str_replace("=", "|", base64_encode($smsg));
                        $OUTPUT .= "\r\n\t\t\t\t\t\t<input type=hidden name='cust[{$i}]' value='{$row['cellno']}'>\r\n\t\t\t\t\t\t<input type=hidden name='msg[{$i}]' value='{$smsg}'>";
                        $i++;
                    }
                }
            }
            $OUTPUT .= "</form>\r\n\t\t<script>document.dataform.submit();</script>";
            break;
    }
    return $OUTPUT;
}
function send($_POST)
{
    extract($_POST);
    # validate input
    require_lib("validate");
    $v = new validate();
    $v->isOk($id, "num", 1, 9, "Invalid Query ID.");
    $v->isOk($to, "num", 1, 20, "Invalid cell no.");
    $v->isOk($text, "string", 1, 150, "Invalid sms text.");
    # display errors, if any
    if ($v->isError()) {
        $confirm = "";
        $errors = $v->getErrors();
        foreach ($errors as $e) {
            $confirm .= "<li class=err>" . $e["msg"];
        }
        return error($_POST, $confirm . "</li>");
    }
    db_conn('crm');
    $Sl = "SELECT * FROM tokens WHERE id='{$id}'";
    $Ry = db_exec($Sl) or errDie("Unable to get query data from db.");
    if (pg_numrows($Ry) < 1) {
        return "Invalid query.";
    }
    $time = date("H:i:s");
    $date = date("Y-m-d");
    $text_enc = base64_encode($text);
    $request = @file(urler(GENERALMSG_URL . "?cellnum={$to}&message={$text_enc}&" . sendhash()));
    if ($request == false) {
        return "<li class=err>Connection failed. Check your internet connection and try again.</li>";
    }
    db_conn('crm');
    $Sl = "INSERT INTO token_actions(token,action,donedate,donetime,doneby,donebyid)\r\n\tVALUES ('{$id}','Sent SMS','{$date}','{$time}','" . USER_NAME . "','" . USER_ID . "')";
    $Ry = db_exec($Sl) or errDie("Unable to insert record.");
    $OUTPUT .= "<script> window.opener.parent.mainframe.location.reload(); window.close(); </script>";
    return $OUTPUT;
    $out = "<table border=0 cellpadding='" . TMPL_tblCellPadding . "' cellspacing='" . TMPL_tblCellSpacing . "'>\r\n\t<tr><th>SMS Sent</th></tr>\r\n\t<tr class='bg-odd'><td>" . implode("", $request) . "</td></tr>\r\n\t</table>";
    return $out;
}