/** * Get the info about this URL * * @param string $raw_url the raw url to parse * @param &object &$attachment the attachment object * @param bool $verify whether the existance of the URL should be checked * @param bool $relative_url allow relative URLs * * @return true if the URL is okay, or an error object if not */ public static function get_url_info($raw_url, &$attachment, $verify, $relative_url) { // Check the URL for existence // * Get 'size' (null if the there were errors accessing the link, // or 0 if the URL loaded but had None/Null/0 for length // * Get 'file_type' // * Get 'filename' (for display) // // * Rename all occurances of 'display_name' to 'display_name' $u = AttachmentsHelper::parse_url($raw_url, $relative_url); // Deal with parsing errors if ($u->error) { return $u; } // Set up defaults for what we want to know $filename = basename($u->path); $file_size = 0; $mime_type = ''; $found = false; // Set the defaults $attachment->filename = JString::trim($filename); $attachment->file_size = $file_size; $attachment->url_valid = false; // Get parameters jimport('joomla.application.component.helper'); $params = JComponentHelper::getParams('com_attachments'); $overlay = $params->get('superimpose_url_link_icons', true); // Get the timeout $timeout = $params->get('link_check_timeout', 10); if (is_numeric($timeout)) { $timeout = (int) $timeout; } else { $timeout = 10; } // Check the URL to see if it is valid $errstr = null; $fp = false; $app = JFactory::getApplication(); if ($timeout > 0) { // Set up error handler in case it times out or some other error occurs set_error_handler(create_function('$a, $b, $c, $d', 'throw new Exception("fsockopen error");'), E_ALL); try { $fp = fsockopen($u->domain, $u->port, $errno, $errstr, $timeout); restore_error_handler(); } catch (Exception $e) { restore_error_handler(); if ($verify) { $u->error = true; $u->error_code = 'url_check_exception'; $u->error_msg = $e->getMessage(); } } if ($u->error) { $error_msg = JText::sprintf('ATTACH_ERROR_CHECKING_URL_S', $raw_url); if ($app->isAdmin()) { $result = new JObject(); $result->error = true; $result->error_msg = $error_msg; return $result; } $u->error_msg = $error_msg; return $u; } } // Check the URL to get the size, etc if ($fp) { $request = "HEAD {$u->url} HTTP/1.1\nHOST: {$u->domain}\nConnection: close\n\n"; fputs($fp, $request); while (!feof($fp)) { $http_response = fgets($fp, 128); // Check to see if it was found if (preg_match("|^HTTP/1\\.\\d [0-9]+ ([^\$]+)\$|m", $http_response, $match)) { if (trim($match[1]) == 'OK') { $found = true; } } // Check for length if (preg_match("/Content\\-Length: (\\d+)/i", $http_response, $match)) { $file_size = (int) $match[1]; } // Check for content type if (preg_match("/Content\\-Type: ([^;\$]+)/i", $http_response, $match)) { $mime_type = trim($match[1]); } } fclose($fp); // Return error if it was not found (timed out, etc) if (!$found && $verify) { $u->error = true; $u->error_code = 'url_not_found'; $u->error_msg = JText::sprintf('ATTACH_ERROR_COULD_NOT_ACCESS_URL_S', $raw_url); return $u; } } else { if ($verify && $timeout > 0) { // Error connecting $u->error = true; $u->error_code = 'url_error_connecting'; $error_msg = JText::sprintf('ATTACH_ERROR_CONNECTING_TO_URL_S', $raw_url) . "<br /> (" . $errstr . ")"; $u->error_msg = $error_msg; return $u; } if ($timeout == 0) { // Pretend it was found $found = true; if ($overlay) { $mime_type = 'link/generic'; } else { $mime_type = 'link/unknown'; } } } // Update the record $attachment->filename = JString::trim($filename); $attachment->file_size = $file_size; $attachment->url_valid = $found; // Deal with the file type if (!$mime_type) { require_once JPATH_COMPONENT_SITE . '/file_types.php'; $mime_type = AttachmentsFileTypes::mime_type($filename); } if ($mime_type) { $attachment->file_type = JString::trim($mime_type); } else { if ($overlay) { $mime_type = 'link/generic'; $attachment->file_type = 'link/generic'; } else { $mime_type = 'link/unknown'; $attachment->file_type = 'link/unknown'; } } // See if we can figure out the icon require_once JPATH_COMPONENT_SITE . '/file_types.php'; $icon_filename = AttachmentsFileTypes::icon_filename($filename, $mime_type); if ($icon_filename) { $attachment->icon_filename = AttachmentsFileTypes::icon_filename($filename, $mime_type); } else { if ($mime_type == 'link/unknown') { $attachment->icon_filename = 'link.gif'; } elseif ($mime_type == 'link/broken') { $attachment->icon_filename = 'link_broken.gif'; } else { $attachment->icon_filename = 'link.gif'; } } return true; }