Example #1
0
 /**
  * 
  * @param type $sPath
  * @return type
  */
 public function getFileContents($sPath, $aPost = array(), $sOrigPath = '')
 {
     if (strpos($sPath, 'http') === 0 || !empty($aPost)) {
         if (!$this->oHttpAdapter->available()) {
             throw new Exception(JchPlatformUtility::translate('No Http Adapter available'));
         }
         try {
             $response = $this->oHttpAdapter->request($sPath, $aPost);
             if ($response === FALSE) {
                 throw new RuntimeException(sprintf(JchPlatformUtility::translate('Failed getting file contents from %s'), $sPath));
             }
         } catch (RuntimeException $ex) {
             JchOptimizelogger::log($sPath . ': ' . $ex->getMessage(), JchPlatformPlugin::getPluginParams());
             $response['code'] = 404;
         } catch (BadFunctionCallException $ex) {
             throw new Exception($ex->getMessage());
         }
         if ($response['code'] >= 400) {
             $sPath = $sOrigPath == '' ? $sPath : $sOrigPath;
             $sContents = $this->notFound($sPath);
         } else {
             $sContents = $response['body'];
         }
     } else {
         if (file_exists($sPath)) {
             $sContents = @file_get_contents($sPath);
         } elseif ($this->oHttpAdapter->available()) {
             $sUriPath = JchPlatformPaths::path2Url($sPath);
             $sContents = $this->getFileContents($sUriPath, array(), $sPath);
         } else {
             $sContents = $this->notFound($sPath);
         }
     }
     return $sContents;
 }
 /**
  * 
  * @param type $sPath
  * @return type
  */
 public function getFileContents($sPath, $aPost = null, $aHeader = null, $sOrigPath = '')
 {
     $oHttpAdapter = $this->getHttpAdapter();
     if (strpos($sPath, 'http') === 0) {
         if (!$oHttpAdapter->available()) {
             throw new Exception('No Http Adapter available');
         }
         $this->response_code = 0;
         try {
             $sUserAgent = !empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
             $response = $oHttpAdapter->request($sPath, $aPost, $aHeader, $sUserAgent);
             $this->response_code = $response['code'];
             if (!isset($response) || $response === FALSE) {
                 throw new RuntimeException(sprintf('Failed getting file contents from %s', $sPath));
             }
         } catch (RuntimeException $ex) {
             JchOptimizelogger::log($sPath . ': ' . $ex->getMessage(), JchPlatformPlugin::getPluginParams());
         } catch (Exception $ex) {
             throw new Exception($sPath . ': ' . $ex->getMessage());
         }
         if ($this->response_code != 200 && !$this->allow_400) {
             $sPath = $sOrigPath == '' ? $sPath : $sOrigPath;
             $sContents = $this->notFound($sPath);
         } else {
             $sContents = $response['body'];
         }
     } else {
         if (file_exists($sPath)) {
             $sContents = @file_get_contents($sPath);
         } elseif ($oHttpAdapter->available()) {
             $sUriPath = JchPlatformPaths::path2Url($sPath);
             $sContents = $this->getFileContents($sUriPath, null, null, $sPath);
         } else {
             $sContents = $this->notFound($sPath);
         }
     }
     return $sContents;
 }