// Check if the right prefix is queried
if ($CP_PREFIX != '10574') {
    REST::error(REST::HTTP_NOT_FOUND);
}
if ($_SERVER['REQUEST_METHOD'] === 'PUT' || $_SERVER['REQUEST_METHOD'] === 'POST') {
    // If it's a POST request, the PATH_INFO string contains a "template". We must
    // convert the template in a proper, unique Handle:
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        // A template is a Handle containing an asterisk '*' character.
        // The user can set eir own escape character:
        //    $escape = isset($_GET['escape']) ? $_GET['escape'] : '\\';
        //    if (strlen($escape) !== 1)
        //      REST::fatal(REST::HTTP_BAD_REQUEST, 'Invalid escape character');
        //    $escape = preg_quote($escape);
        // We use mysql's UUID function to create a unique string:
        $result = CP_MySQL::query('SELECT UUID()');
        $row = $result->fetch_row();
        // remove all non-hexadecimal characters (mysql adds dashes):
        $uuid = preg_replace('/[^\\da-f]/i', '', $row[0]);
        $result->free();
        // Parse the template and replace the asterisk with the new $uuid:
        if (!preg_match("/^((?:[^~]|~.)*)\\*((?:[^~]|~.)*)\$/s", $CP_SUFFIX, $matches)) {
            REST::fatal(REST::HTTP_BAD_REQUEST, 'Invalid Handle template');
        }
        $CP_SUFFIX = preg_replace("/~(.)/", '$1', $matches[1]) . $uuid . preg_replace("/~(.)/", '$1', $matches[2]);
    }
    // OK, let's parse the input. We accept form data...
    if ($_SERVER['CONTENT_TYPE'] === 'application/x-www-form-urlencoded') {
        if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
            $data = '';
            $input = REST::inputhandle();
    /**
     * @param $handle string
     * @return bool
     * @todo optimization by preparsed statements.
     */
    public function read()
    {
        $eschandle = CP_MySQL::escape_string($this->handle);
        $result = CP_MySQL::query(<<<EOS
SELECT `idx`, `type`, `data`, `ttl_type`, `ttl`, `timestamp`, `refs`,
       `admin_read`, `admin_write`, `pub_read`, `pub_write`
FROM `handles`
WHERE `handle` = {$eschandle}
ORDER BY `idx`;
EOS
);
        if (!$result->num_rows) {
            return false;
        }
        $this->type = array();
        while ($row = $result->fetch_row()) {
            $idx = $row[0];
            $this->type[$idx] = $row[1];
            $this->data[$idx] = $row[2];
            $this->ttl_type[$idx] = $row[3];
            $this->ttl[$idx] = $row[4];
            $this->timestamp[$idx] = $row[5];
            $this->refs[$idx] = $row[6];
            $this->admin_read[$idx] = $row[7];
            $this->admin_write[$idx] = $row[8];
            $this->pub_read[$idx] = $row[9];
            $this->pub_write[$idx] = $row[10];
        }
        $result->free();
        return true;
    }