コード例 #1
0
ファイル: api.php プロジェクト: carriercomm/coebot-www
function apiVars($query)
{
    $channel = channelOrDie($query);
    $varName = $query[3];
    if (!preg_match('/^[A-Z0-9\\-]{1,64}$/i', $varName)) {
        tellError("bad varname", 400);
    }
    if ($query[2] == 'set') {
        authOrDie($query, AUTH_SHARED_SECRET);
        $json = json_decode(file_get_contents("php://input"));
        if ($json == false || !property_exists($json, "value")) {
            tellBadParam('json');
        }
        $value = $json->value;
        if (strlen($value) > 255) {
            tellBadParam('value');
        }
        // if (property_exists($json, "description")) {
        //     $description = $json->description;
        //     if (strlen($description) > 255) {
        //         tellBadParam('description');
        //     }
        //     $success = dbSetVar($channel, $varName, $value, $description);
        // } else {
        $success = dbSetVar($channel, $varName, $value);
        // }
        if ($success) {
            tellSuccess();
        } else {
            tellError("database error", 500);
        }
    } else {
        if ($query[2] == 'increment') {
            authOrDie($query, AUTH_SHARED_SECRET);
            $json = json_decode(file_get_contents("php://input"));
            if ($json == false || !property_exists($json, "incAmount")) {
                tellBadParam('json');
            }
            $incAmount = $json->incAmount;
            if (!is_int($incAmount) && !is_float($incAmount)) {
                tellBadParam('incAmount');
            }
            $result = dbIncrementVar($channel, $varName, $incAmount);
            if ($result !== false) {
                $response = array("newValue" => $result);
                tellSuccess($response);
            } else {
                tellError("database error", 500);
            }
        } else {
            if ($query[2] == 'unset') {
                authOrDie($query, AUTH_SHARED_SECRET);
                $success = dbDeleteVar($channel, $varName);
                if ($success === false) {
                    tellError("database error", 500);
                } else {
                    if ($success === NULL) {
                        tellError("var not found", 404);
                    }
                }
                tellSuccess();
            } else {
                if ($query[2] == 'get') {
                    $row = dbGetVar($channel, $varName);
                    if ($row === false) {
                        tellError("database error", 500);
                    } else {
                        if ($row === NULL) {
                            tellError("var not found", 404);
                        }
                    }
                    tellSuccess($row);
                } else {
                    tellBadMethod();
                }
            }
        }
    }
}
コード例 #2
0
ファイル: common.php プロジェクト: carriercomm/coebot-www
/**
 * Increments the value of a var, or creates a new row for that var in the database
 *
 * Returns new value if successful, or false if an error occurred (should use === when checking return value)
 */
function dbIncrementVar($channel, $varName, $incAmount = 0)
{
    global $mysqli;
    initMysqli();
    $row = dbGetVar($channel, $varName);
    if ($row === false || $row === NULL) {
        $dbAction = dbSetVar($channel, $varName, $incAmount);
        if ($dbAction === false) {
            return $dbAction;
        }
        return strval($incAmount);
    } else {
        $oldValue = $row['value'];
        if (is_numeric($oldValue)) {
            if (intval($oldValue) == floatval($oldValue)) {
                $newValue = intval($oldValue) + $incAmount;
            } else {
                $newValue = floatval($oldValue) + $incAmount;
            }
            $newValueStr = strval($newValue);
            $dbAction = dbSetVar($channel, $varName, $newValueStr);
            if ($dbAction === false) {
                return $dbAction;
            }
            return $newValueStr;
        } else {
            return $oldValue;
        }
    }
}