public static function POST($params) { //Define model $model = ['title' => ['type' => Validate::TYPE_TEXT, 'max' => 12, 'min' => 3, Validate::REQUIRED], 'content' => ['type' => Validate::TYPE_TEXT, 'max' => 4096, 'min' => 12, Validate::REQUIRED]]; //Require and Validate model Validate::model($params, $model); //Declare them as variables $title = $params['title']; $content = $params['content']; $post = ['title' => $title, 'content' => $content, 'timestamp' => time()]; $post = \Phramework\Models\Filter::castEntry($post, ['timestamp' => Validate::TYPE_UNIX_TIMESTAMP]); //Store ($title, $content) somehow and get the id $id = rand(0, 100); $post['id'] = $id; \Phramework\Models\Response::created('http://localhost/post/' . $id . '/'); //Sample output Phramework::view(['post' => $post], 'post', 'Blog post'); }
/** * Prepare log object * @param integer $flags * @param object $settings * @param object $params * @param string $HTTPMethod * @param array $headers * @param object|null $additionalParameters * @return object */ private static function prepareObject($flags, $settings, $params, $HTTPMethod, $headers, $additionalParameters) { list($URI) = \Phramework\URIStrategy\URITemplate::URI(); $object = (object) ['request_id' => Phramework::getRequestUUID(), 'URI' => $URI, 'method' => $HTTPMethod, 'user_id' => null, 'ip_address' => \Phramework\Models\Util::getIPAddress(), 'request_headers' => null, 'request_params' => null, 'request_body_raw' => null, 'request_timestamp' => $_SERVER['REQUEST_TIME'], 'response_timestamp' => time(), 'response_headers' => null, 'response_body' => null, 'response_status_code' => http_response_code(), 'exception' => null, 'exception_class' => null, 'errors' => null, 'call_trace' => null, 'flags' => $flags, 'additional_parameters' => $additionalParameters]; if (($flags & self::LOG_USER_ID) !== 0) { $user = Phramework::getUser(); $object->user_id = $user ? $user->id : false; } /* Request flags */ if (($flags & self::LOG_REQUEST_HEADERS) !== 0) { //Asterisk authorization header value except schema if (isset($headers['Authorization'])) { list($authorizationSchema) = sscanf($headers['Authorization'], '%s %s'); $headers['Authorization'] = $authorizationSchema . ' ***'; } $object->request_headers = $headers; } else { $request_headers = []; if (($flags & self::LOG_REQUEST_HEADER_CONTENT_TYPE) !== 0) { //Write content type $request_headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] = isset($headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE]) ? $headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] : null; } if (($flags & self::LOG_REQUEST_HEADER_AGENT) !== 0) { $request_headers['User-Agent'] = isset($headers['User-Agent']) ? $headers['User-Agent'] : null; } if (($flags & self::LOG_REQUEST_HEADER_REFERER) !== 0) { $request_headers['Referer'] = isset($headers['Referer']) ? $headers['Referer'] : null; } if (($flags & self::LOG_REQUEST_HEADER_ACCEPT) !== 0) { $request_headers['Accept'] = isset($headers['Accept']) ? $headers['Accept'] : null; } if (!empty($request_headers)) { $object->request_headers = $request_headers; } } if (($flags & self::LOG_REQUEST_PARAMS) !== 0) { $object->request_params = $params; } if (($flags & self::LOG_REQUEST_BODY_RAW) !== 0) { $bodyRaw = file_get_contents('php://input'); if (strlen($bodyRaw) > $settings->body_raw_limit) { $bodyRaw = 'TRIMMED' . PHP_EOL . substr($bodyRaw, 0, $settings->body_raw_limit); } //Apply FILTER_SANITIZE_STRING $object->request_body_raw = \Phramework\Models\Filter::string($bodyRaw); //include content type headers if disabled if (!empty($bodyRaw) && ($flags & self::LOG_REQUEST_HEADERS) === 0 && ($flags & self::LOG_REQUEST_HEADER_CONTENT_TYPE) === 0) { $contentType = isset($headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE]) ? $headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] : null; if (empty($object->request_headers)) { //make sure it's array $object->request_headers = []; } $object->request_headers[\Phramework\Models\Request::HEADER_CONTENT_TYPE] = $contentType; } } $responseHeaders = new \stdClass(); foreach (headers_list() as $header) { list($key, $value) = explode(': ', $header); $responseHeaders->{$key} = $value; } /* Response flags */ if (($flags & self::LOG_RESPONSE_HEADER) !== 0) { $object->response_headers = $responseHeaders; } if (($flags & self::LOG_RESPONSE_BODY) !== 0) { $object->response_body = ob_get_contents(); if (($flags & self::LOG_RESPONSE_HEADER) === 0) { //show content type if headers are disabled $object->response_headers = (object) [\Phramework\Models\Request::HEADER_CONTENT_TYPE => isset($responseHeaders->{\Phramework\Models\Request::HEADER_CONTENT_TYPE}) ? $responseHeaders->{\Phramework\Models\Request::HEADER_CONTENT_TYPE} : null]; } } return $object; }
/** * Type cast entry's attributes based on the provided model * * If any TYPE_UNIX_TIMESTAMP are present an additional attribute will * be included with the suffix _formatted, the format of the string can be * changed from timestamp_format setting. * @param array $entry * @param array $model * @return array Returns the typecasted entry * @deprecated since 1.1.0 */ public static function castEntry($entry, $model) { if (!$entry) { return $entry; } $timestamp_format = \Phramework\Phramework::getSetting('timestamp_format', null, 'Y-m-d\\TH:i:s\\Z'); //Repeat for each model's attribute of the entry. //$k holds the key of the attribute and $v the type foreach ($model as $k => $v) { if (!isset($entry[$k])) { continue; } //Typecast Filter::typecast($entry[$k], $v); //if type is a Validate::TYPE_UNIX_TIMESTAMP //then inject a string version of the timestamp to this entry if ($v === Validate::TYPE_UNIX_TIMESTAMP) { //offset included! $converted = gmdate($timestamp_format, $entry[$k]); //inject the string version of the timestamp $entry[$k . '_formatted'] = $converted; } } return $entry; }