Example #1
0
 public function getHttpClient()
 {
     static $_instance = null;
     if (is_null($_instance)) {
         // get an http client
         require_once sh404SEF_ADMIN_ABS_PATH . 'lib/Zend/Http/Client.php';
         $_instance = new Sh_Zend_Http_Client();
         // set params
         $_instance->setConfig(array('maxredirects' => 0, 'timeout' => 10));
     }
     return $_instance;
 }
Example #2
0
 public function _doRemoteConfig()
 {
     // if not set to auto check and not forced to do so
     // when user click on "check updates" button
     // we don't actually try to get updates info
     $sefConfig =& Sh404sefFactory::getConfig();
     // prepare a default response object
     $response = new stdClass();
     $response->status = true;
     $response->config = array();
     // check if allowed to read central configuration file
     if (!$sefConfig->canReadRemoteConfig) {
         return $response;
     }
     // get an http client
     require_once JPATH_ROOT . DS . 'administrator' . DS . 'components' . DS . 'com_sh404sef' . DS . 'lib' . DS . 'Zend' . DS . 'Http' . DS . 'Client.php';
     $hClient = new Sh_Zend_Http_Client();
     // request file content
     $adapters = array('Sh_Zend_Http_Client_Adapter_Curl', 'Sh_Zend_Http_Client_Adapter_Socket');
     $rawResponse = null;
     foreach ($adapters as $adapter) {
         try {
             // set params
             //$hClient->setUri( self::$_endPoint . '/' . self::$_devConfig);
             $hClient->setUri(self::$_endPoint . '/' . self::$_config);
             $hClient->setConfig(array('maxredirects' => 0, 'timeout' => 10));
             $hClient->setAdapter($adapter);
             $rawResponse = $hClient->request();
             break;
         } catch (Exception $e) {
             // need that to be Exception, so as to catch Sh_Zend_Exceptions.. as well
             // we failed, let's try another method
         }
     }
     // return if error
     if (empty($rawResponse)) {
         $response->status = false;
         return $response;
     }
     if (!is_object($rawResponse) || $rawResponse->isError()) {
         $response->status = false;
         return $response;
     }
     // check the file
     $type = $rawResponse->getHeader('Content-type');
     if (strtolower($type) != 'text/xml' && strtolower($type) != 'application/xml') {
         $response->status = false;
         return $response;
     }
     // should be OK then
     $response->status = true;
     // get an xml object and parse the response
     $rawConfig = simplexml_load_string($rawResponse->getBody());
     // into our response object
     $response->config = array();
     if (!empty($rawConfig)) {
         foreach ($rawConfig->config as $configName => $configElement) {
             foreach ($configElement as $configKey => $values) {
                 if (is_string($values)) {
                     $response->config[$configKey] = $values;
                 } else {
                     $counter = 0;
                     foreach ($values as $arrayName => $arrayValues) {
                         $children = $arrayValues->children();
                         $count = count($children);
                         if ($count > 0) {
                             foreach ($arrayValues as $key => $value) {
                                 $response->config[$configKey][$counter][$key] = (string) $value;
                             }
                         } else {
                             $response->config[$configKey][] = (string) $arrayValues;
                         }
                         $counter++;
                     }
                 }
             }
         }
     }
     // check if this is a valid information file
     return $response;
 }
Example #3
0
 /**
  * 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;
 }