Ejemplo n.º 1
0
            break;
        case 'POST':
            REST::created(REST::urlbase() . CP::PORTAL_URL . urlencode($CP_PREFIX) . '/' . urlencode($CP_SUFFIX));
            break;
        default:
            // this shouldn't happen
            REST::fatal(REST::HTTP_INTERNAL_SERVER_ERROR);
    }
} elseif ($_SERVER['REQUEST_METHOD'] === 'DELETE') {
    $handle = new CP_Handle("{$CP_PREFIX}/{$CP_SUFFIX}");
    REST::fatal($handle->delete() ? REST::HTTP_OK : REST::HTTP_NOT_FOUND);
} else {
    // If we're here, the request method should be GET or HEAD. Otherwise, quit:
    REST::require_method('GET', 'HEAD');
    // Create a CP_Handle object...
    $handle = new CP_Handle("{$CP_PREFIX}/{$CP_SUFFIX}");
    // and read its contents from the database. If it's not there...
    if (!$handle->read()) {
        // return a 404 Not Found to the client.
        REST::fatal(REST::HTTP_NOT_FOUND);
    }
    // The simplest response is the HTTP/1.1 307 Moved Temporarily.
    // The client can suppress this behaviour by sending a redirect=no query
    // parameter:
    if (!isset($_GET['redirect']) || !in_array(strtolower($_GET['redirect']), array('', 'no', 'false', '0'))) {
        // The client MAY specify an index=n query parameter, to select a specific
        // URL:
        $index = isset($_GET['index']) ? (int) $_GET['index'] : null;
        // If the client didn't specify an index, find the URL with the lowest index:
        if ($index === null) {
            foreach ($handle->type as $idx => $type) {
    /**
     * @param $handle string
     * @return bool
     * @todo Allow large fields by using mysqli_stmt_send_long_data().
     */
    public function create()
    {
        $eschandle = CP_MySQL::escape_string($this->handle);
        CP_MySQL::real_query("LOCK TABLES `handles` LOW_PRIORITY WRITE;");
        try {
            // Check if the handle already exists:
            $result = CP_MySQL::query("SELECT COUNT(*) FROM `handles` WHERE `handle` = {$eschandle};");
            $row = $result->fetch_row();
            if ($row[0]) {
                return false;
            }
            // Check if a prepared statement already exists:
            if (!self::$create_stmt) {
                self::$create_stmt = CP_MySQL::mysql()->prepare(<<<EOS
INSERT INTO `handles` (
  handle, idx, type, data, ttl_type, ttl, timestamp, refs,
  admin_read, admin_write, pub_read, pub_write
)
VALUES (
  ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
);
EOS
);
            }
            $p_idx = $p_type = $p_data = $p_ttl_type = $p_ttl = $p_timestamp = $p_refs = $p_admin_read = $p_admin_write = $p_pub_read = $p_pub_write = null;
            self::$create_stmt->bind_param('sissiiisiiii', $this->handle, $p_idx, $p_type, $p_data, $p_ttl_type, $p_ttl, $p_timestamp, $p_refs, $p_admin_read, $p_admin_write, $p_pub_read, $p_pub_write);
            $this->force_hs_admin();
            foreach ($this->type as $p_idx => $p_type) {
                $p_data = (string) @$this->data[$p_idx];
                if (!isset($this->ttl_type[$p_idx])) {
                    $this->ttl_type[$p_idx] = 0;
                }
                $p_ttl_type = $this->ttl_type[$p_idx];
                if (!isset($this->ttl[$p_idx])) {
                    $this->ttl[$p_idx] = 86400;
                }
                $p_ttl = $this->ttl[$p_idx];
                if (!isset($this->timestamp[$p_idx])) {
                    $this->timestamp[$p_idx] = time();
                }
                $p_timestamp = $this->timestamp[$p_idx];
                if (!isset($this->refs[$p_idx])) {
                    $this->refs[$p_idx] = '';
                }
                $p_refs = $this->refs[$p_idx];
                if (!isset($this->admin_read[$p_idx])) {
                    $this->admin_read[$p_idx] = 1;
                }
                $p_admin_read = $this->admin_read[$p_idx];
                if (!isset($this->admin_write[$p_idx])) {
                    $this->admin_write[$p_idx] = 1;
                }
                $p_admin_write = $this->admin_write[$p_idx];
                if (!isset($this->pub_read[$p_idx])) {
                    $this->pub_read[$p_idx] = 1;
                }
                $p_pub_read = $this->pub_read[$p_idx];
                if (!isset($this->pub_write[$p_idx])) {
                    $this->pub_write[$p_idx] = 0;
                }
                $p_pub_write = $this->pub_write[$p_idx];
                if (!self::$create_stmt->execute()) {
                    throw new CP_MySQL_Exception(CP_MySQL::mysql()->error, CP_MySQL::mysql()->errno);
                }
            }
            CP_MySQL::real_query('UNLOCK TABLES;');
        } catch (Exception $e) {
            self::delete($this->handle);
            CP_MySQL::real_query('UNLOCK TABLES;');
            throw $e;
        }
        return true;
    }