public function getProductAction($categoryid) { $catid = intval($categoryid); if ($catid == 0) { $products = Product::find(array('order' => 'productid asc')); } else { $products = Product::find(array('conditions' => 'catid = ?0', 'bind' => array(0 => $catid), 'order' => 'productid asc')); } $response = new \Phalcon\Http\Response(); $response->setHeader("Access-Control-Allow-Origin: *"); $response->setHeader("Content-Type", "application/json"); $response->setJsonContent($products->toArray()); $this->view->disable(); return $response; }
public static function responseJson(array $data = array(), $state = 200) { $datetime = gmdate("D, d M Y H:i:s") . ' GMT'; $response = new \Phalcon\Http\Response(); $response->setHeader('Pragma', 'no-cache'); $response->setHeader('Cache-Control', 'no-cache, private, no-store, must-revalidate, pre-check=0, post-check=0, max-age=0, max-stale=0'); $response->setHeader('Last-Modified', $datetime); $response->setHeader('X-Frame-Options', 'SAMEORIGIN'); $response->setContentType('application/json', 'UTF-8'); $response->setExpires(new \DateTime()); $response->setStatusCode($state); $response->setEtag(md5($datetime)); $response->setJsonContent($data, JSON_UNESCAPED_UNICODE); $response->send(); // Force quit the dispatcher loop. exit; }
public static function Json($json) { $response = new \Phalcon\Http\Response(); $response->setJsonContent($json); $contentLength = strlen($response->getContent()); $response->setHeader('ContentLength', $contentLength); $response->send(); die; }
private function resourceNotFound($resourceKey, View $view) { $view->setViewsDir(__DIR__ . '/../modules/Index/views/'); $view->setPartialsDir(''); $view->message = "Acl resource <b>{$resourceKey}</b> in <b>/app/config/acl.php</b> not exists"; $view->partial('error/error404'); $response = new \Phalcon\Http\Response(); $response->setHeader(404, 'Not Found'); $response->sendHeaders(); echo $response->getContent(); exit; }
private function resourceNotFound($resource) { $this->view->setViewsDir(APP_URL . 'modules/site/views/'); $this->view->setPartialsDir(''); $this->view->message = "Acl resource <b>{$resource}</b> in <b>/app/config/acl.php</b> not exists"; $this->view->partial('error/show404'); $response = new \Phalcon\Http\Response(); $response->setHeader(404, 'Not Found'); $response->sendHeaders(); echo $response->getContent(); exit; }
public static function p($status = 1, $msg = null, $data = [], $code = 200) { if (method_exists($msg, 'getMessage')) { $msg = $msg->getMessage(); } $response = new \Phalcon\Http\Response(); $response->setHeader("Content-Type", "application/json"); if ($status) { $response->setStatusCode(200); } else { $response->setStatusCode($code); } $response->setJsonContent(array('status' => $status == 1 ? 'success' : 'error', 'msg' => $msg, 'data' => $data)); $response->send(); }
/** * Create a Response object with related parameters * * @param string $content * @param int $status * @param string $contentType * @param null $message * @return \Phalcon\Http\Response */ protected function createResponse($content = self::NO_CONTENT, $status = self::STATUS_OK, $contentType = self::JSON, $message = null) { $response = new \Phalcon\Http\Response(); // Allow CORS $response->setHeader(self::HEADER_ALLOW_ORIGIN, "*"); // set new token to protected api if (!in_array($this->dispatcher->getActionName(), $this->publicAction) && !in_array($this->dispatcher->getActionName(), $this->authErrAction)) { $response->setHeader(self::HEADER_ACCESS_TOKEN, $this->session->get("loggedInUser")->access_token); } if ($message) { $response->setStatusCode($status, $message); } else { $response->setStatusCode($status, self::$MESSAGES[$status]); } if ($content !== self::NO_CONTENT) { $response->setHeader(self::HEADER_CONTENT_TYPE, $contentType); if ($contentType == BaseRestApiController::JSON) { $response->setJsonContent($content); } else { $response->setContent($content); } } return $response; }
/** * @param $data response to send back as JSON with Callback * @param bool|true $success * @param int $status_code of response default 200 * @param string $status_message of response default OK */ public function response($data, $success = true, $status_code = 200, $status_message = "OK") { //new response $response = new \Phalcon\Http\Response(); //header('Access-Control-Allow-Origin: *'); $response->setStatusCode($status_code, $status_message); $response->setContentType('application/json', 'utf-8'); $response->setHeader('Access-Control-Allow-Origin', '*'); //encode call $json = json_encode(array('success' => $success, 'data' => $data)); //set response to send back check for callback $response->setContent(isset($_GET['callback']) ? "{$_GET['callback']}({$json})" : $json); $response->send(); exit; //kill from other processing }
public function attachResponseService() { $this->_di->setShared('response', function () { $response = new \Phalcon\Http\Response(); $response->setHeader('X-Frame-Options', 'SAMEORIGIN'); $response->setHeader('X-Content-Type-Options', 'nosniff'); $response->setHeader('X-XSS-Protection', '1; mode=block'); $response->setHeader('X-Download-Options', 'noopen'); return $response; }); return $this; }
public function prepareTimePickerAction() { if ($this->request->isPost() && $this->request->isAjax()) { $date = $this->request->getPost('date'); $termin = strtotime($date) + 9 * 60 * 60; $termins = array(); for ($i = 0; $i < 20; $i++) { $apps = Application::find(array('conditions' => 'term = ?1', 'bind' => array(1 => date("Y.m.d H:i", $termin)))); if (count($apps) >= 4) { $termins[date("H:i", $termin)] = 1; } else { $termins[date("H:i", $termin)] = 0; } $termin += 30 * 60; } $view = clone $this->view; $view->start(); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->setParamToView('termins', $termins); $view->render('user', 'prepareTimePicker'); $view->finish(); $content = $view->getContent(); $this->view->disable(); $status = 200; $description = 'OK'; $headers = array(); $contentType = 'application/json'; $content = json_encode($content); $response = new \Phalcon\Http\Response(); $response->setStatusCode($status, $description); $response->setContentType($contentType, 'UTF-8'); $response->setContent($content); foreach ($headers as $key => $value) { $response->setHeader($key, $value); } return $response; } }
public function loadMoreAction() { if ($this->request->isPost() && $this->request->isAjax()) { $start_from = $this->request->getPost('start_from', 'int'); $type = $this->request->getPost('type', 'int'); $posts = Post::find(array('conditions' => 'is_service = ?1', 'bind' => array(1 => $type), 'order' => 'id DESC', 'limit' => 10, 'offset' => $start_from)); $tags = array(); foreach ($posts as $index => $post) { $pts = PostTag::find(array('conditions' => 'post_id = ?1', 'bind' => array(1 => $post->id))); $tags[$index] = array(); foreach ($pts as $pt) { $tags[$index][] = Tag::findFirst(array('conditions' => 'id = ?1', 'bind' => array(1 => $pt->tag_id))); } } if (count($posts) > 0) { $view = clone $this->view; $view->start(); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->setParamToView('posts', $posts); $view->setParamToView('tags', $tags); $view->render('index', 'loadMore'); $view->finish(); $content = $view->getContent(); $this->view->disable(); $status = 200; $description = 'OK'; $headers = array(); $contentType = 'application/json'; $content = json_encode($content); $response = new \Phalcon\Http\Response(); $response->setStatusCode($status, $description); $response->setContentType($contentType, 'UTF-8'); $response->setContent($content); foreach ($headers as $key => $value) { $response->setHeader($key, $value); } return $response; } else { return 0; } } }
private function sendContentAsJson($content) { $response = new Phalcon\Http\Response(json_encode($content)); $response->setHeader("Content-Type", "application/json"); return $response; }
// Check if the user has uploaded files if ($request->hasFiles() == true) { foreach ($request->getUploadedFiles() as $file) { echo $file->getName(), " ", $file->getSize(), "\n"; $file->moveTo('files/' . $file->getName()); } } // Headers $request->getHeader("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest"; $request->isAjax(); $request->isSecureRequest(); $request->getServerAddress(); $request->getClientAddress(); $request->getUserAgent(); $request->getAcceptableContent(); $request->getBestCharset(); $request->getBestLanguage(); // *** Response *** $response = new \Phalcon\Http\Response(); $response->setHeader("Content-Type", "application/pdf"); $response->setHeader("Content-Disposition", 'attachment; filename="downloaded.pdf"'); $response->setRawHeader("HTTP/1.1 200 OK"); $response->getHeaders()->get("Content-Type"); $response->setExpires($expireDate); $response->redirect(); $response->redirect("posts/index"); $response->redirect("http://en.wikipedia.org", true); $response->redirect("http://www.example.com/new-location", true, 301); $response->setStatusCode(404, "Not Found"); $response->setContent("Sorry, the page doesn't exist"); $response->send();
public function applicationsAction() { $date = new DateTime("now"); if ($this->request->isPost() and $this->request->isAjax()) { $start_from = $this->request->getPost('start_from', 'int'); $type = $this->request->getPost('type', 'int'); if ($type == 1) { $apps = Application::find(array('conditions' => 'confirmed = 0 and term >= ?1', 'order' => 'term DESC', 'limit' => '5', 'offset' => $start_from, 'bind' => array(1 => $date->format('Y.m.d')))); } else { if ($type == 2) { $apps = Application::find(array('conditions' => 'confirmed = 1 and term >= ?1', 'order' => 'term DESC', 'limit' => '5', 'offset' => $start_from, 'bind' => array(1 => $date->format('Y.m.d')))); } else { if ($type == 3) { $apps = Application::find(array('conditions' => 'confirmed = 1 and term < ?1', 'order' => 'term DESC', 'limit' => '5', 'offset' => $start_from, 'bind' => array(1 => $date->format('Y.m.d')))); } else { $apps = array(); } } } $services = array(); $users = array(); foreach ($apps as $key => $app) { $str = ""; $a_ss = ApplicationService::find(array('conditions' => 'application_id = ?1', 'bind' => array(1 => $app->id))); foreach ($a_ss as $a_s) { $service = Service::findFirst(array('conditions' => 'id = ?1', 'bind' => array(1 => $a_s->service_id))); $str .= $service->name . ', '; } $services[$key] = substr($str, 0, -2) . '.'; $car = Car::findFirst(array('conditions' => 'number = ?1', 'bind' => array(1 => $app->car))); $users[$key] = User::findFirst(array('conditions' => 'id = ?1', 'bind' => array(1 => $car->owner))); } if (count($apps) > 0) { $view = clone $this->view; $view->start(); $view->setRenderLevel(View::LEVEL_ACTION_VIEW); $view->setParamToView('apps', $apps); $view->setParamToView('type', $type); $view->setParamToView('services', $services); $view->setParamToView('users', $users); $view->render('admin', 'applications'); $view->finish(); $content = $view->getContent(); $this->view->disable(); $status = 200; $description = 'OK'; $headers = array(); $contentType = 'application/json'; $content = json_encode($content); $response = new \Phalcon\Http\Response(); $response->setStatusCode($status, $description); $response->setContentType($contentType, 'UTF-8'); $response->setContent($content); foreach ($headers as $key => $value) { $response->setHeader($key, $value); } return $response; } else { return 0; } } }
$totalsupplyinfo['ActiveMasternodesCount'] = $totalmninfo; $totalsupplyinfo['ActiveMasternodesUniqueIPs'] = $totaluniqueips; $totalsupplyinfo['MasternodeExpectedPayment'] = $totalsupplyinfo['TotalMNValue'] / $totalmninfo; //Change the HTTP status $response->setStatusCode(200, "OK"); $response->setJsonContent(array('status' => 'OK', 'data' => array('MasternodeStatsPerProtocolVersion' => $finalinfo, 'MasternodeStatsTotal' => $totalsupplyinfo))); } return $response; }); // Get table vars // Parameters: // none $app->get('/api/tablevars', function () use($app, &$mysqli) { //Create a response $response = new Phalcon\Http\Response(); $response->setHeader('Access-Control-Allow-Origin', '*'); $response->setHeader("Content-Type", "application/json"); $request = $app->request; $errmsg = array(); if (!array_key_exists('CONTENT_LENGTH', $_SERVER) || intval($_SERVER['CONTENT_LENGTH']) != 0) { $errmsg[] = "No CONTENT expected"; } if (count($errmsg) > 0) { //Change the HTTP status $response->setStatusCode(400, "Bad Request"); //Send errors to the client $response->setJsonContent(array('status' => 'ERROR', 'messages' => array('Payload (or CONTENT_LENGTH) is missing'))); } else { // Retrieve all known nodes for current hub $sql = "SELECT * FROM cmd_stats_values"; $numnodes = 0;