Example #1
0
 public function parseFrom($string)
 {
     $fields = explode("\r\n", Preg::replace($string, '/\\x0D\\x0A[\\x09\\x20]+/', ' '));
     $fields = explode("\r\n", $string);
     //Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
     //HTTP-Version   = "HTTP" "/" 1*DIGIT "." 1*DIGIT
     if (!Preg::match($statusLine = array_shift($fields), '|HTTP/([0-9]+\\.[0-9]+)\\s+([0-5][0-9]+)\\s+(.*)|', $match)) {
         throw new \Psc\Exception('Kann Header Status-Line nicht parsen: "' . $statusLine . '"');
     }
     list($NULL, $this->version, $this->code, $this->reason) = $match;
     /*
        message-header = field-name ":" [ field-value ]
        field-name     = token
        field-value    = *( field-content | LWS )
        field-content  = <the OCTETs making up the field-value
                         and consisting of either *TEXT or combinations
                         of token, separators, and quoted-string>
     */
     foreach ($fields as $field) {
         try {
             if (Preg::match($field, '/([^:]+): (.+)/m', $match)) {
                 list($NULL, $name, $value) = $match;
                 $name = Preg::replace_callback(mb_strtolower(trim($name)), '/(?<=^|[\\x09\\x20\\x2D])./', function ($m) {
                     return mb_strtoupper($m[0]);
                 });
                 if (isset($this->values[$name])) {
                     if (is_array($this->values[$name])) {
                         $this->values[$name][] = $value;
                     } else {
                         $this->values[$name] = array($this->values[$name], $value);
                     }
                 } else {
                     $this->values[$name] = trim($value);
                 }
             }
         } catch (\Psc\Exception $e) {
             if (mb_strpos($e->getMessage(), 'Bad UTF8') === FALSE) {
                 throw $e;
             }
         }
     }
 }
Example #2
0
 /**
  * Erstellt einen Request aus den Umgebungsvariablen
  * 
  * wird eine Umgebungsvariable mit NULL übergeben (oder ausgelassen), wird die global Umgebungsvariable genommen
  * infer() ist also äquivalent mit:
  * infer($_GET, $_POST, $_COOKIE, $_SERVER)
  * 
  * ist $_GET['mod_rewrite_request'] gesetzt wird dies als resource genommen
  * 
  * @TODO Symfony hierfür nehmen (am besten ganz ersetzen)
  * 
  * @deprecated das übergeben von Variablen ist strongly discouraged!
  */
 public static function infer(SfRequest $sfRequest = NULL)
 {
     if (!isset($sfRequest)) {
         $sfRequest = SfRequest::createFromGlobals();
     }
     // alternativ könnten wir den code aus sf kopieren oder sf mal patchen..
     $method = NULL;
     switch ($sfRequest->getMethod()) {
         // wertet schon X-HTTP-METHOD-OVERRIDE aus
         case 'POST':
             $method = Request::POST;
             break;
         case 'PUT':
             $method = Request::PUT;
             break;
         case 'DELETE':
             $method = Request::DELETE;
             break;
         case 'PATCH':
             $method = Request::PATCH;
             break;
         case 'GET':
         default:
             $method = Request::GET;
             break;
     }
     $request = new Request($method, rawurldecode($sfRequest->getPathInfo()));
     $request->setQuery($sfRequest->query->all());
     $request->setReferer($sfRequest->server->get('HTTP_REFERER'));
     $request->setUserAgent($sfRequest->server->get('HTTP_USER_AGENT'));
     $request->setPreferredLanguages($sfRequest->getLanguages());
     $header = $request->getHeader();
     foreach ($sfRequest->headers->all() as $key => $value) {
         // wir verschönern hier z.B. X_REQUESTED_WITH zu X-Requested-With
         $key = mb_strtolower($key);
         $key = \Psc\Preg::replace_callback($key, '/(^|-)([a-z]{1})/', function ($m) {
             return ($m[1] === '' ? NULL : '-') . mb_strtoupper($m[2]);
         });
         // das ist voll doof, aber aus legacy gründen müssen wir das machen
         // schöner wäre auch die sf Requests / Header zu benutzen, dann wären wir durch
         if (count($value) === 1) {
             // unwrap arrays mit nur einem eintrag
             $value = current($value);
         }
         $header->setField($key, $value);
     }
     /* Body */
     if (mb_strpos($request->getHeaderField('Content-Type'), 'application/x-www-form-urlencoded') === 0) {
         $request->setBody($sfRequest->request->all());
     } elseif (mb_strpos($request->getHeaderField('Content-Type'), 'multipart/form-data') === 0) {
         $request->setBody($sfRequest->request->all());
         $files = array();
         foreach ($sfRequest->files->all() as $key => $sfFile) {
             if ($sfFile instanceof \Symfony\Component\HttpFoundation\File\UploadedFile) {
                 if (!$sfFile->isValid()) {
                     throw new \Psc\Exception('Cannot Upload File: ' . $sfFile->getClientOriginalName() . ' Error Code: ' . $sfFile->getErorr() . ' size: ' . $sfFile->getClientSize());
                 } else {
                     $files[$key] = $f = new \Psc\System\UploadedFile($sfFile->getPathName());
                     $f->setOriginalName($sfFile->getClientOriginalName());
                 }
                 $request->setFiles($files);
             }
             // FIXME else: kann auch ein array von files sein oder ein array von array ...
             // aber wie machen wir das in den files array rein?
         }
     } else {
         $request->setBody($sfRequest->getContent());
         // really raw
     }
     return $request;
 }
Example #3
0
 public function parseFrom($string)
 {
     $fields = explode("\r\n", Preg::replace($string, '/\\x0D\\x0A[\\x09\\x20]+/', ' '));
     array_shift($fields);
     // status line wegnehmen, denn das wird in den ableitenden geparsed
     /*
        message-header = field-name ":" [ field-value ]
        field-name     = token
        field-value    = *( field-content | LWS )
        field-content  = <the OCTETs making up the field-value
                         and consisting of either *TEXT or combinations
                         of token, separators, and quoted-string>
     */
     foreach ($fields as $field) {
         if (Preg::match($field, '/([^:]+): (.+)/m', $match)) {
             list($NULL, $name, $value) = $match;
             $name = Preg::replace_callback(mb_strtolower(trim($name)), '/(?<=^|[\\x09\\x20\\x2D])./', function ($m) {
                 return mb_strtoupper($m[0]);
             });
             $this->setField($name, $value);
         }
     }
 }
Example #4
0
 public static function replaceLinksMarkup($text)
 {
     $link = function ($url, $label) {
         // label is already escaped
         if (Preg::match($url, '~^([a-zA-Z0-9]+://|www\\.)~')) {
             return HTML::tag('a', $label, array('href' => $url, 'class' => 'external', 'target' => '_blank'));
         } else {
             return HTML::tag('a', $label, array('href' => $url, 'class' => 'internal'));
         }
     };
     $openLink = preg_quote('[[');
     $closeLink = preg_quote(']]');
     $sepLink = preg_quote('|');
     // [[http://www.google.com|This Link points to google]]
     $text = \Psc\Preg::replace_callback($text, '/' . $openLink . '(.*?)' . $sepLink . '(.*?)' . $closeLink . '/', function ($match) use($link) {
         return $link($match[1], $match[2]);
     });
     // [[http://www.google.com]]
     $text = \Psc\Preg::replace_callback($text, '/' . $openLink . '(.*?)' . $closeLink . '/', function ($match) use($link) {
         return $link($match[1], $match[1]);
     });
     return $text;
 }