/**
  *	Function to get the contents of the URL. It will get the contents using Gzip compression if possible in
  *	order to save bandwidth. It uses the HTTP Client class from Simon Willison to do the dirty work.
  *
  *	More information about the HTTP client class can be found on: http://scripts.incutio.com/httpclient/
  *
  *	If it fails to retrieve the data, it will raise a fatal error.
  *
  *	By default, it will cache the downloaded data based on the etag and last-modified headers. The cache files
  *	are stored in the temp directory of the Yellow Duck framework and have the extension "wch". You can delete
  *	these automatically as they will be recreated on the fly if needed.
  *
  *	For configuring the cache, there are two configuration variables you can redefine if needed:
  *	YD_HTTP_CACHE_TIMEOUT: the lifetime of the cache in seconds (default: 3600).
  *	YD_HTTP_CACHE_USEHEAD: if a HEAD HTTP request should be used to verify the cache validity (default: 1).
  *
  *	@param $cache	(optional) Indicate if the web content should be cached or not. By default, caching is
  *					turned on.
  *  @param $fail    (optional) Whether to fail or not if the contents cannot be downloaded. Defaults to true.
  *
  *	@returns	Returns the contents of the URL.
  */
 function getContents($cache = true, $fail = true)
 {
     // Check if caching is enabled
     $cacheFName = null;
     // Check the cache
     if ($cache == true) {
         // Include the filesystem library
         include_once YD_DIR_HOME_CLS . '/YDFileSystem.php';
         // Check if we need to use the HTTP HEAD function
         if (YDConfig::get('YD_HTTP_CACHE_USEHEAD') == 1) {
             // Get the headers
             $headers = $this->getHeaders();
             // Check if we have etag or last modified
             if (isset($headers['etag']) || isset($headers['last-modified'])) {
                 $cacheFName = $this->getUrl();
                 if (isset($headers['etag'])) {
                     $cacheFName .= $headers['etag'];
                 }
                 if (isset($headers['last-modified'])) {
                     $cacheFName .= $headers['last-modified'];
                 }
                 if (isset($headers['content-length'])) {
                     $cacheFName .= $headers['content-length'];
                 }
                 $cacheFName = YD_TMP_PRE . 'W_' . md5($cacheFName) . '.wch';
                 $cacheFName = YD_DIR_TEMP . '/' . $cacheFName;
             }
         }
         // If the cache filename is null, use the default one
         if ($cacheFName == null) {
             $cacheFName = YD_DIR_TEMP . '/' . YD_TMP_PRE . 'W_' . md5($this->getUrl()) . '.wch';
         }
         // Use the cache file if any
         if (is_file($cacheFName)) {
             $file = new YDFSFile($cacheFName);
             $cacheValidTime = $file->getLastModified() + YDConfig::get('YD_HTTP_CACHE_TIMEOUT');
             if (time() < $cacheValidTime) {
                 return $file->getContents();
             }
         }
     }
     // Create a new HTTP client
     $client = $this->_getHttpClient();
     // Now send the request
     $result = @$client->doRequest();
     // Check if there was a result
     if ($result == false) {
         if ($fail) {
             trigger_error('Failed to retrieve the data from the url "' . $this->getUrl() . '". ' . $client->getError(), YD_ERROR);
         } else {
             return false;
         }
     } else {
         $data = @$client->getContent();
     }
     // Check if caching is enabled
     if ($cache == true) {
         // Save the cached data
         if ($cacheFName != null) {
             $dir = new YDFSDirectory(YD_DIR_TEMP);
             $dir->createFile($cacheFName, $data);
         }
     }
     // Return the data
     return $data;
 }