예제 #1
0
파일: Client.php 프로젝트: jasmun/Noco100
 /**
  * Attempt to detect the MIME type of a file using available extensions
  *
  * This method will try to detect the MIME type of a file. If the fileinfo
  * extension is available, it will be used. If not, the mime_magic
  * extension which is deprected but is still available in many PHP setups
  * will be tried.
  *
  * If neither extension is available, the default application/octet-stream
  * MIME type will be returned
  *
  * @param  string $file File path
  * @return string       MIME type
  */
 protected function _detectFileMimeType($file)
 {
     $type = null;
     // First try with fileinfo functions
     if (function_exists('finfo_open')) {
         if (self::$_fileInfoDb === null) {
             self::$_fileInfoDb = @finfo_open(FILEINFO_MIME);
         }
         if (self::$_fileInfoDb) {
             $type = finfo_file(self::$_fileInfoDb, $file);
         }
     } elseif (function_exists('mime_content_type')) {
         $type = mime_content_type($file);
     }
     // Fallback to the default application/octet-stream
     if (!$type) {
         $type = 'application/octet-stream';
     }
     return $type;
 }
예제 #2
0
파일: Proxy.php 프로젝트: jasmun/Noco100
 /**
  * Send request to the proxy server
  *
  * @param string        $method
  * @param IfwPsn_Vendor_Zend_Uri_Http $uri
  * @param string        $http_ver
  * @param array         $headers
  * @param string        $body
  * @return string Request as string
  * @throws IfwPsn_Vendor_Zend_Http_Client_Adapter_Exception
  */
 public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
 {
     // If no proxy is set, fall back to default Socket adapter
     if (!$this->config['proxy_host']) {
         return parent::write($method, $uri, $http_ver, $headers, $body);
     }
     // Make sure we're properly connected
     if (!$this->socket) {
         require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Http/Client/Adapter/Exception.php';
         throw new IfwPsn_Vendor_Zend_Http_Client_Adapter_Exception('Trying to write but we are not connected');
     }
     $host = $this->config['proxy_host'];
     $port = $this->config['proxy_port'];
     if ($this->connected_to[0] != "tcp://{$host}" || $this->connected_to[1] != $port) {
         require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Http/Client/Adapter/Exception.php';
         throw new IfwPsn_Vendor_Zend_Http_Client_Adapter_Exception('Trying to write but we are connected to the wrong proxy server');
     }
     // Add Proxy-Authorization header
     if ($this->config['proxy_user']) {
         // Check to see if one already exists
         $hasProxyAuthHeader = false;
         foreach ($headers as $k => $v) {
             if ((string) $k == 'proxy-authorization' || preg_match("/^proxy-authorization:/i", $v)) {
                 $hasProxyAuthHeader = true;
                 break;
             }
         }
         if (!$hasProxyAuthHeader) {
             $headers[] = 'Proxy-authorization: ' . IfwPsn_Vendor_Zend_Http_Client::encodeAuthHeader($this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']);
         }
     }
     // if we are proxying HTTPS, preform CONNECT handshake with the proxy
     if ($uri->getScheme() == 'https' && !$this->negotiated) {
         $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
         $this->negotiated = true;
     }
     // Save request method for later
     $this->method = $method;
     // Build request headers
     if ($this->negotiated) {
         $path = $uri->getPath();
         if ($uri->getQuery()) {
             $path .= '?' . $uri->getQuery();
         }
         $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
     } else {
         $request = "{$method} {$uri} HTTP/{$http_ver}\r\n";
     }
     // Add all headers to the request string
     foreach ($headers as $k => $v) {
         if (is_string($k)) {
             $v = "{$k}: {$v}";
         }
         $request .= "{$v}\r\n";
     }
     if (is_resource($body)) {
         $request .= "\r\n";
     } else {
         // Add the request body
         $request .= "\r\n" . $body;
     }
     // Send the request
     if (!@fwrite($this->socket, $request)) {
         require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Http/Client/Adapter/Exception.php';
         throw new IfwPsn_Vendor_Zend_Http_Client_Adapter_Exception('Error writing request to proxy server');
     }
     if (is_resource($body)) {
         if (stream_copy_to_stream($body, $this->socket) == 0) {
             require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Http/Client/Adapter/Exception.php';
             throw new IfwPsn_Vendor_Zend_Http_Client_Adapter_Exception('Error writing request to server');
         }
     }
     return $request;
 }