Example #1
0
 public function send(ResponseBuilder &$res)
 {
     if ('CLI' != $_SERVER['REQUEST_METHOD']) {
         if (isset($res->file)) {
             if (isset($res->file->path)) {
                 if (false == is_readable($res->file->path)) {
                     throw new AppException('Can\' access file ' . $res->file->path);
                 }
                 if (false == is_file($res->file->path)) {
                     throw new AppException('File ' . $res->file->path . ' not found');
                 }
                 $filename = isset($res->file->name) ? $res->file->name : sprintf('"%s"', addcslashes(basename($res->file->path), '"\\'));
                 $size = filesize($res->file->path);
             } else {
                 if (isset($res->file->contents)) {
                     $filename = isset($res->file->name) ? $res->file->name : 'Untitled';
                     $size = strlen($res->file->contents);
                 }
             }
             if (false == isset($res->file->header) or false == is_array($res->file->header)) {
                 $res->file->header = array();
             }
             $res->header = array_merge($res->header, array('Content-Description' => 'File Transfer', 'Content-Type' => 'application/octet-stream', 'Content-Disposition' => 'attachment; filename=' . $filename, 'Content-Transfer-Encoding' => 'binary', 'Connection' => 'Keep-Alive', 'Expires' => '0', 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', 'Pragma' => 'public', 'Content-Length' => $size), $res->file->header);
         }
         if (false == isset($res->status)) {
             $res->status = 200;
         }
         if (isset($res->redirect)) {
             $res->header['Location'] = $res->redirect;
             if (false == isset($res->status)) {
                 $res->status = 303;
             }
         }
         if (isset($res->refresh)) {
             if (false === isset($res->refresh->timeout)) {
                 $res->refresh->timeout = 0;
             }
             $res->header['Refresh'] = $res->refresh->timeout . ' ;url=' . rawurlencode($res->refresh->url);
         }
         if (false == isset($res->statusText)) {
             $res->statusText = Utils::getStatusText($res->status);
         }
         if (is_array($res->body) or is_object($res->body)) {
             $res->header['Content-Type'] = 'application/json; charset=utf-8';
         }
         header($_SERVER['SERVER_PROTOCOL'] . ' ' . $res->status . ' ' . $res->statusText);
         foreach ($res->header as $key => $value) {
             if (null === $value) {
                 header_remove($key);
             } else {
                 header($key . ': ' . $value);
             }
         }
         $res->cookies->write();
     }
     if (isset($res->file)) {
         if (isset($res->file->path)) {
             while (@ob_end_flush()) {
             }
             readfile($res->file->path);
         } else {
             if (isset($res->file->contents)) {
                 $this->write($res->file->contents);
             }
         }
     } else {
         if (isset($res->body)) {
             if (is_scalar($res->body)) {
                 $this->write($res->body);
             } else {
                 if (is_array($res->body) or is_object($res->body)) {
                     $this->write(JSON::encode($res->body));
                 }
             }
         }
     }
 }
Example #2
0
 public function __construct($url, $options = array())
 {
     $this->url = $url;
     if (false === isset($options['protocol'])) {
         $options['protocol'] = 'http';
     }
     if (false == isset($options['method'])) {
         $options['method'] = 'GET';
     } else {
         $options['method'] = strtoupper($options['method']);
     }
     if (false == isset($options['content'])) {
         $options['content'] = array();
     }
     if (false == isset($options['content-type'])) {
         $options['content-type'] = 'urlencode';
     }
     $boundary = "---------------------" . substr(md5(rand(0, 32000)), 0, 10);
     switch ($options['content-type']) {
         case 'urlencode':
             if ($options['method'] == 'GET' or $options['method'] == 'DELETE') {
                 if ($options['content']) {
                     $this->url .= '?' . rawurldecode(http_build_query($options['content']));
                 }
                 $options['content'] = '';
             } else {
                 $options['content'] = http_build_query($options['content']);
             }
             $options['header']['Content-Type'] = 'application/x-www-form-urlencoded';
             break;
         case 'json':
             $options['content'] = JSON::encode($options['content']);
             $options['header']['Content-Type'] = 'application/json';
             break;
         case 'form':
             if ($options['content']) {
                 $vars = explode('&', http_build_query($options['content']));
                 $options['content'] = "--{$boundary}\n";
                 if ($vars) {
                     foreach ($vars as $v) {
                         list($key, $value) = explode('=', $v, 2);
                         $key = rawurldecode($key);
                         $options['content'] .= "Content-Disposition: form-data; name=\"{$key}\"\n\n{$value}\n";
                         $options['content'] .= "--{$boundary}\n";
                     }
                 }
             } else {
                 $options['content'] = "--{$boundary}\n";
             }
             $options['header']['Content-Type'] = 'multipart/form-data; boundary=' . $boundary;
             break;
     }
     if (isset($options['files'])) {
         foreach ($options['files'] as $file) {
             if (false == is_readable($file['path'])) {
                 throw new AppException('Can\' access file ' . $file['path']);
             }
             $options['content'] .= "Content-Disposition: form-data; name=\"{$file['name']}\"; filename=\"" . basename($file['path']) . "\"\n";
             if (isset($file['type'])) {
                 $options['content'] .= "Content-Type: {$file['type']}\n";
             }
             $options['content'] .= "Content-Transfer-Encoding: binary\n\n";
             $options['content'] .= file_get_contents($file['path']) . "\n";
             $options['content'] .= "--{$boundary}\n";
         }
     }
     $options['Content-Length'] = strlen($options['content']);
     if (isset($options['header'])) {
         $options['header'] = implode('', array_map(function ($k, $v) {
             return $k . ': ' . $v . "\r\n";
         }, array_keys($options['header']), array_values($options['header'])));
     }
     if (false === isset($options['timeout'])) {
         $options['timeout'] = 25;
     }
     if (false === isset($options['ignore_errors'])) {
         $options['ignore_errors'] = true;
     }
     $protocol = $options['protocol'];
     unset($options['protocol']);
     unset($options['content-type']);
     $this->context = stream_context_create(array($protocol => $options));
 }