Example #1
0
 public function run()
 {
     //set_error_handler(array('\Slim\Slim', 'handleErrors'));
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', Config::get('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body, but only if it isn't a HEAD request
     if (!$this->request->isHead()) {
         echo $body;
     }
 }
Example #2
0
 /**
  * Return a string representation of the complete http response.
  *
  * @return string
  */
 public function __toString()
 {
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->finalize();
     Util::serializeCookies($headers, $this->cookies, ['cookies.encrypt' => false]);
     $version = '1.1';
     $text = self::getMessageForCode($status);
     return sprintf("HTTP/%s %s\n", $version, $text) . $this->stringifyHeaders($headers) . "\n" . $body;
 }
Example #3
0
 /**
  * Redefine the run method
  *
  * We no need to use a Slim handler
  */
 public function run()
 {
     //set_error_handler(array('\Slim\Slim', 'handleErrors')); //Espo: no needs to use this handler
     //Apply final outer middleware layers
     if ($this->config('debug')) {
         //Apply pretty exceptions only in debug to avoid accidental information leakage in production
         //$this->add(new \Slim\Middleware\PrettyExceptions()); //Espo: no needs to use this handler
     }
     //Invoke middleware and application stack
     $this->middleware[0]->call();
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', $this->config('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body, but only if it isn't a HEAD request
     if (!$this->request->isHead()) {
         echo $body;
     }
     //restore_error_handler(); //Espo: no needs to use this handler
 }
Example #4
0
 /**
  * Constructor
  * @param \Slim\Environment $env
  */
 public function __construct(\Slim\Environment $env)
 {
     $this->env = $env;
     $this->headers = new \Slim\Http\Headers(\Slim\Http\Headers::extract($env));
     $this->cookies = new \Slim\Helper\Set(\Slim\Http\Util::parseCookieHeader($env['HTTP_COOKIE']));
 }
Example #5
0
 public function testPrefersLeftmostCookieWhenManyCookiesWithSameName()
 {
     $header = 'foo=bar; foo=beer';
     $result = \Slim\Http\Util::parseCookieHeader($header);
     $this->assertEquals('bar', $result['foo']);
 }
Example #6
0
 /**
  * Get value of encrypted HTTP cookie
  *
  * Return the value of an encrypted cookie from the current HTTP request,
  * or return NULL if cookie does not exist. Encrypted cookies created during
  * the current request will not be available until the next request.
  *
  * @param  string       $name
  * @return string|false
  */
 public function getEncryptedCookie($name, $deleteIfInvalid = true)
 {
     $value = \Slim\Http\Util::decodeSecureCookie($this->request->cookies($name), $this->config('cookies.secret_key'), $this->config('cookies.cipher'), $this->config('cookies.cipher_mode'));
     if ($value === false && $deleteIfInvalid) {
         $this->deleteCookie($name);
     }
     return $value;
 }
Example #7
0
 /**
  * Run
  *
  * This method invokes the middleware stack, including the core Slim application;
  * the result is an array of HTTP status, header, and body. These three items
  * are returned to the HTTP client.
  */
 public function run()
 {
     /**
      * set time zone for log and message queue message body
      * @author Mustafa Zeynel Dağlı
      */
     date_default_timezone_set($this->container['settings']['time.zone']);
     /**
      * if rest service entry logging conf. true, publish to message queue
      * @author Mustafa Zeynel Dağlı
      */
     if ($this->container['settings']['restEntry.rabbitMQ'] == true) {
         $this->publishMessage();
     }
     set_error_handler(array('\\Slim\\Slim', 'handleErrors'));
     //Apply final outer middleware layers
     if ($this->config('debug')) {
         //Apply pretty exceptions only in debug to avoid accidental information leakage in production
         $this->add(new \Slim\Middleware\PrettyExceptions());
     }
     /**
      * zeynel dağlı
      */
     if ($this->container['settings']['log.level'] <= \Slim\Log::ERROR) {
         //print_r('--slim run kontrolor--');
         $this->add(new \Slim\Middleware\PrettyExceptions());
     }
     //Invoke middleware and application stack
     $this->middleware[0]->call();
     //print_r('--slim run kontrolor2--');
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', $this->config('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body, but only if it isn't a HEAD request
     if (!$this->request->isHead()) {
         echo $body;
     }
     $this->applyHook('slim.after');
     restore_error_handler();
 }
Example #8
0
 /**
  * Run
  *
  * This method invokes the middleware stack, including the core Slim application;
  * the result is an array of HTTP status, header, and body. These three items
  * are returned to the HTTP client.
  */
 public function run()
 {
     set_error_handler(array('\\Slim\\Slim', 'handleErrors'));
     //Apply final outer middleware layers
     $this->add(new \Slim\Middleware\PrettyExceptions());
     //Invoke middleware and application stack
     $this->middleware[0]->call();
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', $this->config('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body
     echo $body;
     restore_error_handler();
 }
 /**
  * Run
  *
  * This method invokes the middleware stack, including the core Slim application;
  * the result is an array of HTTP status, header, and body. These three items
  * are returned to the HTTP client.
  */
 public function run()
 {
     /**
      * set time zone for log and message queue message body
      * @author Mustafa Zeynel Dağlı
      */
     date_default_timezone_set($this->container['settings']['time.zone']);
     /**
      * MQMAnager middle ware katmanından önce inject
      * ediliyor/ test amaçlı değiştirilecek
      */
     $MQManagerConfigObject = new \Utill\MQ\MQManagerConfig();
     $managerConfig = new \Zend\ServiceManager\Config($MQManagerConfigObject->getConfig());
     $MQManager = new \Utill\MQ\MQManager($managerConfig);
     $MQManager->setService('slimApp', $this);
     $this->setMQManager($MQManager);
     set_error_handler(array('\\Slim\\Slim', 'handleErrors'));
     /**
      * MQmanager exceptions has been tested  by changing error handler function
      * @author Zeynel Dağlı
      * @todo first tests did not work, after further tests if not work
      * this code can be removed
      */
     //set_error_handler(array($this, 'handleErrorsCustom'));
     //Apply final outer middleware layers
     if ($this->config('debug')) {
         //Apply pretty exceptions only in debug to avoid accidental information leakage in production
         $this->add(new \Slim\Middleware\PrettyExceptions());
     }
     /**
      * zeynel dağlı
      */
     if ($this->container['settings']['log.level'] <= \Slim\Log::ERROR) {
         //print_r('--slim run kontrolor--');
         $this->add(new \Slim\Middleware\PrettyExceptions());
     }
     //Invoke middleware and application stack
     $this->middleware[0]->call();
     //print_r('--slim run kontrolor2--');
     /**
      * if rest service entry logging conf. true, publish to message queue
      * @since 07/12/2015 this functionality is being called from MQ manager
      * @author Mustafa Zeynel Dağlı
      */
     //if($this->container['settings']['restEntry.rabbitMQ'] == true) $this->publishMessage();
     if ($this->container['settings']['restEntry.rabbitMQ'] == true) {
         $this->getMQManager()->get('MQRestCallLog');
     }
     //Fetch status, header, and body
     list($status, $headers, $body) = $this->response->finalize();
     // Serialize cookies (with optional encryption)
     \Slim\Http\Util::serializeCookies($headers, $this->response->cookies, $this->settings);
     //Send headers
     if (headers_sent() === false) {
         //Send status
         if (strpos(PHP_SAPI, 'cgi') === 0) {
             header(sprintf('Status: %s', \Slim\Http\Response::getMessageForCode($status)));
         } else {
             header(sprintf('HTTP/%s %s', $this->config('http.version'), \Slim\Http\Response::getMessageForCode($status)));
         }
         //Send headers
         foreach ($headers as $name => $value) {
             $hValues = explode("\n", $value);
             foreach ($hValues as $hVal) {
                 header("{$name}: {$hVal}", false);
             }
         }
     }
     //Send body, but only if it isn't a HEAD request
     if (!$this->request->isHead()) {
         echo $body;
     }
     $this->applyHook('slim.after');
     restore_error_handler();
 }
Example #10
0
 /**
  * Save session
  */
 protected function saveSession()
 {
     $value = \Slim\Http\Util::encodeSecureCookie(serialize($_SESSION), $this->settings['expires'], $this->settings['secret'], $this->settings['cipher'], $this->settings['cipher_mode']);
     if (strlen($value) > 4096) {
         $this->app->getLog()->error('WARNING! Slim\\Middleware\\SessionCookie data size is larger than 4KB. Content save failed.');
     } else {
         $this->app->response()->setCookie($this->settings['name'], array('value' => $value, 'domain' => $this->settings['domain'], 'path' => $this->settings['path'], 'expires' => $this->settings['expires'], 'secure' => $this->settings['secure'], 'httponly' => $this->settings['httponly']));
     }
     session_destroy();
 }
Example #11
0
 public function childServer($addr)
 {
     $output = $this->output;
     $bindTo = json_decode($addr, true);
     $this->childHost = $bindTo['host'];
     $this->port = $bindTo['port'];
     cli_set_process_title("mpcmf/console server:run/child -b {$this->childHost} -p {$this->port}");
     posix_setgid(99);
     posix_setuid(99);
     posix_seteuid(99);
     posix_setegid(99);
     $loop = Factory::create();
     $socket = new reactSocketServer($loop);
     $http = new reactHttpServer($socket);
     $http->on('request', function (reactRequest $request, reactResponse $response) use($output) {
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> New connection");
         //MPCMF_DEBUG && $clientName = $request->getRemoteAddress() . '#' . spl_object_hash($request);
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Client connected");
         profiler::resetStack();
         if (!$this->prepare($request, $response, $output)) {
             return;
         }
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Starting application</info>");
         try {
             $app = $this->app();
             $slim = $app->slim();
             $originApplication = $this->applicationInstance->getCurrentApplication();
             $this->applicationInstance->setApplication($app);
             $slim->call();
         } catch (\Exception $e) {
             $response->writeHead(500);
             $response->end("Exception: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n{$e->getTraceAsString()}");
             return;
         }
         /** @var int[]|Headers[]|string[] $content */
         $content = $slim->response->finalize();
         Util::serializeCookies($content[1], $slim->response->cookies, $slim->settings);
         $content[1] = $content[1]->all();
         $this->applicationInstance->setApplication($originApplication);
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Ending application</info>");
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> Writing data and closing connection");
         static $serverSoftware;
         if ($serverSoftware === null) {
             $serverSoftware = 'MPCMF Async PHP ' . phpversion();
         }
         if (array_key_exists('HTTP_ACCEPT_ENCODING', $_SERVER) && strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip') !== false) {
             $content[1]['Content-Encoding'] = 'gzip';
             $content[2] = gzencode($content[2], 9);
         }
         $content[1]['X-PHP-Server'] = $serverSoftware;
         $content[1]['X-PHP-Server-Addr'] = "{$this->childHost}:{$this->port}";
         $response->writeHead($content[0], $content[1]);
         $response->end($content[2]);
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> Connection closed");
     });
     $output->writeln("<error>[CHILD]</error> Starting child server on {$this->childHost}:{$this->port}");
     $socket->listen($this->port, $this->childHost);
     $loop->run();
 }