Example #1
0
 public function __construct($charset = null)
 {
     parent::__construct();
     if (is_null($charset)) {
         $charset = self::DEFAULT_CHARSET;
     }
     parent::setContentType('text/plain', $charset);
 }
Example #2
0
 /**
  * Launches the MVC framework
  */
 public static function go()
 {
     Logging::log('Dispatching');
     try {
         if ($route = self::getRouting()->getRouteFromUrl(self::getRequest()->getParameter('url', null, false))) {
             if (self::$_redirect_login) {
                 Logging::log('An error occurred setting up the user object, redirecting to login', 'main', Logging::LEVEL_NOTICE);
                 self::setMessage('login_message_err', self::geti18n()->__('Please log in'));
                 self::getResponse()->headerRedirect(self::getRouting()->generate('login_page'), 403);
             }
             if (self::performAction($route['module'], $route['action'])) {
                 return true;
             }
         } else {
             self::performAction('main', 'notFound');
         }
     } catch (TemplateNotFoundException $e) {
         header("HTTP/1.0 404 Not Found", true, 404);
         throw $e;
     } catch (ActionNotFoundException $e) {
         header("HTTP/1.0 404 Not Found", true, 404);
         throw $e;
         //('Module action "' . $route['action'] . '" does not exist for module "' . $route['module'] . '"', $e);
     } catch (CSRFFailureException $e) {
         self::$_response->setHttpStatus(301);
         $message = $e->getMessage();
         if (self::getRequest()->getRequestedFormat() == 'json') {
             self::$_response->setContentType('application/json');
             $message = json_encode(array('message' => $message));
         }
         self::$_response->renderHeaders();
         echo $message;
     } catch (\Exception $e) {
         header("HTTP/1.0 500 Not Found", true, 404);
         throw $e;
     }
 }
Example #3
0
 /**
  * @test
  * @param $contentType
  * @param $charset
  * @param $expected
  * @dataProvider contentTypeDataProvider
  */
 public function testIfContentType($contentType, $charset, $expected)
 {
     $response = new Response();
     $response->setContentType($contentType, $charset);
     $this->assertEquals($expected, $response->headers->get('Content-Type'));
 }
Example #4
0
 public static function run()
 {
     $dotenv = new \Dotenv\Dotenv(TXTROOT);
     $dotenv->load();
     if (isset($_SERVER['HTTP_USER_AGENT']) && stripos($_SERVER['HTTP_USER_AGENT'], 'Slackbot-LinkExpanding') !== false) {
         Response::sendResponse(Response::HTTP_403, ['error' => "No slackbots allowed"]);
         exit;
     }
     if (!getenv('REDIS_URL')) {
         Response::sendResponse(Response::HTTP_500, ['error' => "REDIS_URL environment variable required"]);
         exit;
     }
     if (!Request::isGet() && !Request::isPost()) {
         Response::sendResponse(Response::HTTP_405, ['error' => "Please use a GET or POST"]);
         exit;
     }
     if (getenv('AUTH') && (!isset($_POST['auth']) || !static::compareStrings(getenv('AUTH'), $_POST['auth']))) {
         Response::sendResponse(Response::HTTP_401, ['error' => "'auth' parameter is missing or invalid"]);
         exit;
     }
     //    header('Access-Control-Allow-Origin: ' . $_SERVER['ORIGIN']);
     //    header('Access-Control-Allow-Credentials: true');
     //    Access-Control-Allow-Methods: GET, POST
     // x-frame-options
     $redis = Redis::getRedis(getenv('REDIS_URL'));
     $hash = ltrim(Request::getPath(), '/');
     if ($hash) {
         if ($hash == 'robots.txt') {
             Response::setStatus(Response::HTTP_200);
             Response::setContentType(Response::TEXT);
             Response::setContent("User-agent: *\nDisallow: /");
             Response::send();
             exit;
         }
         if (Request::isPost()) {
             Response::sendResponse(Response::HTTP_405, ['error' => "Cannot post to a hash"]);
             exit;
         }
         if (strlen($hash) > Redis::MAX_KEY_LENGTH || !preg_match('/^[A-Za-z0-9]+$/', $hash)) {
             Response::sendResponse(Response::HTTP_404, ['error' => "Invalid hash"]);
             exit;
         }
         $data = $redis->hGetAll(Redis::PREFIX . $hash);
         if (!$data) {
             Response::sendResponse(Response::HTTP_404, ['error' => "Hash not found"]);
             exit;
         }
         $datum = Datum::createFromArray($data);
         if ($datum->once) {
             $redis->del(Redis::PREFIX . $hash);
         }
         // set proper cache header, esp for read-once
         // actually, PROBABLY NOT A GOOD IDEA, esp for things that are meant to expire. we should do the opposite - dont cache
         // Response::setCacheForeverHeaders();
         Response::sendResponse('datum', ['datum' => $datum]);
         exit;
     }
     if (Request::isGet()) {
         Response::sendResponse('home', ['domain' => 'http' . (Request::isSSL() ? 's' : '') . '://' . Request::getHost()]);
         exit;
     } else {
         $data = isset($_POST['data']) ? $_POST['data'] : file_get_contents("php://input");
         if (!$data) {
             Response::sendResponse(Response::HTTP_400, ['error' => 'No data submitted']);
             exit;
         }
         $datum = new Datum(trim($data), Datum::T_TEXT, Request::isFlagOn('once'));
         $key = substr(static::randId(), 0, Redis::MAX_KEY_LENGTH);
         $ttl = isset($_POST['ttl']) ? max(1, min((int) $_POST['ttl'], Redis::MAX_TTL)) : Redis::MAX_TTL;
         $redis->hMSet(Redis::PREFIX . $key, $datum->toArray());
         $redis->expire(Redis::PREFIX . $key, $ttl);
         $url = 'http' . (Request::isSSL() ? 's' : '') . '://' . Request::getHost() . '/' . $key;
         Response::sendResponse(Response::HTTP_201, ['url' => $url, 'ttl' => $ttl, '_textKey' => 'url']);
         exit;
     }
 }
Example #5
0
 protected function render_json($text, $status = NULL)
 {
     $this->response->setContentType('application/json');
     $text = JSON::encode($text);
     $this->response->setHeader('X-JSON', '(' . $text . ')');
     $this->render_text($text, $status);
 }
 public function setContent($responseContent)
 {
     parent::setContentType("application/json" . (isset($this->charset) ? "; charset={$this->charset}" : ""));
     $jsonContent = json_encode($responseContent);
     parent::setContent($jsonContent);
 }