Exemplo n.º 1
0
 function add_icon_filenames()
 {
     require_once JPATH_COMPONENT_SITE . DS . 'file_types.php';
     // Get all the attachment IDs
     $db =& JFactory::getDBO();
     $query = "SELECT id, filename, file_type, icon_filename FROM #__attachments WHERE file_type IS NULL";
     $db->setQuery($query);
     $rows = $db->loadObjectList();
     if (count($rows) == 0) {
         return 'No file_type fields need updating';
     }
     $IDs = array();
     foreach ($rows as $row) {
         $IDs[] = $row->id;
     }
     // Update the icon file_types all the attachments (that do not have one already)
     $row =& JTable::getInstance('Attachments', 'Table');
     $numUpdated = 0;
     foreach ($IDs as $id) {
         $row->load($id);
         // Only update those attachment records that don't already have an icon_filename
         if (strlen($row->icon_filename) == 0) {
             $new_icon_filename = AttachmentsFileTypes::icon_filename($row->filename, $row->file_type);
             if (strlen($new_icon_filename) > 0) {
                 $row->icon_filename = $new_icon_filename;
                 if (!$row->store()) {
                     JError::raiseError(500, $row->getError());
                 }
                 $numUpdated++;
             }
         }
     }
     return JText::_('ADDED ICON FILENAMES TO') . " {$numUpdated} " . JText::_('ATTACHMENT S');
 }
 /**
  * Test various file type and mime type conversions to icon filenames
  *
  * @dataProvider provider
  *
  * @param string $filename the filename to test
  * @param string $icon_filename the expected icon_filename
  * @param string $mime_type the mime type to test (if the filename is empty)
  */
 public function testConversions($filename, $icon_filename, $mime_type)
 {
     $this->assertEquals($icon_filename, AttachmentsFileTypes::icon_filename($filename, $mime_type));
     if ($filename) {
         $this->assertEquals($mime_type, AttachmentsFileTypes::mime_type($filename));
     }
 }
Exemplo n.º 3
0
 function unique_icon_filenames()
 {
     global $attachments_icon_from_file_extension;
     if (!isset($attachments_icon_from_file_extension)) {
         AttachmentsFileTypes::_init();
     }
     $vals = array_unique(array_values($attachments_icon_from_file_extension));
     sort($vals);
     return $vals;
 }
 /**
  * Test the round-trip conversions form icon-filename to mime-type and back
  */
 public function testRoundTrip()
 {
     $icon_filenames = AttachmentsFileTypes::unique_icon_filenames();
     $mime_from_icon = array_flip(AttachmentsFileTypes::$attachments_icon_from_mime_type);
     foreach ($icon_filenames as $icon) {
         if (array_key_exists($icon, $mime_from_icon)) {
             $mime_type = $mime_from_icon[$icon];
             $this->assertEquals($icon, AttachmentsFileTypes::icon_filename('', $mime_type));
         }
     }
 }
Exemplo n.º 5
0
 /**
  * Display the edit view
  */
 public function display($tpl = null)
 {
     // For convenience
     $attachment = $this->attachment;
     // Prevent unallowed editing
     if (!$this->attachment->parent->userMayEditAttachment($attachment)) {
         $errmsg = JText::_('ATTACH_ERROR_NO_PERMISSION_TO_EDIT');
         return JError::raiseError(403, $errmsg . ' (ERR 178)');
     }
     // Construct derived data
     $attachment->parent_entity_name = JText::_('ATTACH_' . $attachment->parent_entity);
     if (!isset($attachment->modifier_name)) {
         AttachmentsHelper::addAttachmentUserNames($attachment);
     }
     // Compute the attachment size in kB
     $attachment->size_kb = (int) (10 * $attachment->file_size / 1024.0) / 10.0;
     // set up lists for form controls
     $this->lists = array();
     $this->lists['published'] = JHtml::_('select.booleanlist', 'state', 'class="inputbox"', $attachment->state);
     $this->lists['url_valid'] = JHtml::_('select.booleanlist', 'url_valid', 'class="inputbox" title="' . JText::_('ATTACH_URL_IS_VALID_TOOLTIP') . '"', $attachment->url_valid);
     // Construct the drop-down list for legal icon filenames
     $icon_filenames = array();
     require_once JPATH_COMPONENT_SITE . '/file_types.php';
     foreach (AttachmentsFileTypes::unique_icon_filenames() as $ifname) {
         $icon_filenames[] = JHtml::_('select.option', $ifname);
     }
     $this->lists['icon_filenames'] = JHtml::_('select.genericlist', $icon_filenames, 'icon_filename', 'class="inputbox" size="1"', 'value', 'text', $attachment->icon_filename);
     // If switching from article to URL default url_verify to true
     if ($attachment->uri_type == 'file' and $this->update == 'url') {
         $attachment->url_verify = true;
     }
     // Set up for checkboxes
     $this->relative_url_checked = $attachment->url_relative ? 'checked="yes"' : '';
     $this->verify_url_checked = $attachment->url_verify ? 'checked="yes"' : '';
     // Set up some tooltips
     $this->enter_url_tooltip = JText::_('ATTACH_ENTER_URL') . '::' . JText::_('ATTACH_ENTER_URL_TOOLTIP');
     $this->display_filename_tooltip = JText::_('ATTACH_DISPLAY_FILENAME') . '::' . JText::_('ATTACH_DISPLAY_FILENAME_TOOLTIP');
     $this->display_url_tooltip = JText::_('ATTACH_DISPLAY_URL') . '::' . JText::_('ATTACH_DISPLAY_URL_TOOLTIP');
     $this->download_count_tooltip = JText::_('ATTACH_NUMBER_OF_DOWNLOADS') . '::' . JText::_('ATTACH_NUMBER_OF_DOWNLOADS_TOOLTIP');
     // Set up mootools/modal
     AttachmentsJavascript::setupModalJavascript();
     // Add the style sheets
     JHtml::stylesheet('com_attachments/attachments_admin_form.css', array(), true);
     $lang = JFactory::getLanguage();
     if ($lang->isRTL()) {
         JHtml::stylesheet('com_attachments/attachments_admin_form_rtl.css', array(), true);
     }
     // Set the toolbar
     $this->addToolBar();
     // Display the form
     parent::display($tpl);
 }
Exemplo n.º 6
0
 function edit()
 {
     global $option, $mainframe;
     $db =& JFactory::getDBO();
     $row =& JTable::getInstance('Attachments', 'Table');
     $cid = JRequest::getVar('cid', array(0), '', 'array');
     $change_article = JRequest::getVar('change', False) == 'article';
     $update_file = JRequest::getVar('change', False) == 'file';
     $id = $cid[0];
     $row->load($id);
     $lists = array();
     $lists['published'] = JHTML::_('select.booleanlist', 'published', 'class="inputbox"', $row->published);
     // Construct the drop-down list for legal icon filenames
     $icon_filenames = array();
     require_once JPATH_COMPONENT_SITE . DS . 'file_types.php';
     foreach (AttachmentsFileTypes::unique_icon_filenames() as $ifname) {
         $icon_filenames[] = JHTML::_('select.option', $ifname);
     }
     $lists['icon_filenames'] = JHTML::_('select.genericlist', $icon_filenames, 'icon_filename', 'class="inputbox" size="1"', 'value', 'text', $row->icon_filename);
     // Get the uploaders name
     $query = "SELECT name FROM #__users WHERE id='" . $row->uploader_id . "' LIMIT 1";
     $db->setQuery($query);
     $row->uploader_name = $db->loadResult();
     // Massage the data
     $row->size = intval($row->file_size / 1024.0);
     $row->url = $mainframe->getSiteURL() . $row->url;
     // Get the article name
     $article_id = $row->article_id;
     $query = "SELECT * FROM #__content WHERE id='{$article_id}' LIMIT 1";
     $db->setQuery($query);
     $arows = $db->loadObjectList();
     if (count($arows) != 1) {
         $errmsg = JText::_('ERROR INVALID ARTICLE ID') . " ({$article_id})";
         JError::raiseError(500, $errmsg);
     }
     $row->article_title = $arows[0]->title;
     if ($change_article) {
         $document =& JFactory::getDocument();
         $document->addScript(JURI::root(true) . '/media/system/js/modal.js');
         $js = "\r\n            function jSelectArticle(id, title) {\r\n                    document.getElementById('article_id').value = id;\r\n                    document.getElementById('article_title').value = title;\r\n                    document.getElementById('sbox-window').close();\r\n                    }";
         $document->addScriptDeclaration($js);
         JHTML::_('behavior.modal', 'a.modal-button');
     }
     JRequest::setVar('hidemainmenu', 1);
     HTML_attachments::editAttachment($row, $lists, $option, $change_article, $update_file);
 }
Exemplo n.º 7
0
 /**
  * Add icon filenames for all attachments missing an icon
  */
 public static function add_icon_filenames()
 {
     require_once JPATH_COMPONENT_SITE . '/file_types.php';
     // Get all the attachment IDs
     $db = JFactory::getDBO();
     $query = $db->getQuery(true);
     $query->select('id, filename, file_type, icon_filename')->from('#__attachments');
     $query->where('file_type IS NULL');
     $db->setQuery($query);
     $attachments = $db->loadObjectList();
     if ($db->getErrorNum()) {
         $errmsg = $db->stderr() . ' (ERR 68)';
         JError::raiseError(500, $errmsg);
     }
     if (count($attachments) == 0) {
         return JText::_('ATTACH_NO_FILE_TYPE_FIELDS_NEED_UPDATING');
     }
     $IDs = array();
     foreach ($attachments as $attachment) {
         $IDs[] = $attachment->id;
     }
     // Update the icon file_types all the attachments (that do not have one already)
     JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_attachments/tables');
     $attachment = JTable::getInstance('Attachment', 'AttachmentsTable');
     $numUpdated = 0;
     foreach ($IDs as $id) {
         $attachment->load($id);
         // Only update those attachment records that don't already have an icon_filename
         if (JString::strlen($attachment->icon_filename) == 0) {
             $new_icon_filename = AttachmentsFileTypes::icon_filename($attachment->filename, $attachment->file_type);
             if (JString::strlen($new_icon_filename) > 0) {
                 $attachment->icon_filename = $new_icon_filename;
                 if (!$attachment->store()) {
                     $errmsg = JText::sprintf('ATTACH_ERROR_ADDING_ICON_FILENAME_FOR_ATTACHMENT_S', $attachment->filename) . ' ' . $attachment->getError() . ' (ERR 69)';
                     JError::raiseError(500, $errsmg);
                 }
                 $numUpdated++;
             }
         }
     }
     return JText::sprintf('ATTACH_ADDED_ICON_FILENAMES_TO_N_ATTACHMENTS', $numUpdated);
 }
Exemplo n.º 8
0
 /**
  * Method to get the field input markup.
  *
  * @return	string	The field input markup.
  * @since	1.6
  */
 public function getInput()
 {
     // Initialize variables.
     $html = array();
     // Construct the list of legal icon filenames
     $icon_filenames = array();
     require_once JPATH_COMPONENT_SITE . '/file_types.php';
     foreach (AttachmentsFileTypes::unique_icon_filenames() as $ifname) {
         $icon_filenames[] = JHtml::_('select.option', $ifname);
     }
     $icon_list = JHtml::_('select.genericlist', $icon_filenames, 'jform[icon_filename]', 'class="inputbox" size="1"', 'value', 'text', $this->value, 'jform_icon_filename');
     // Is it readonly?
     if ((string) $this->element['readonly'] == 'true') {
         // Create a read-only list (no name) with a hidden input to store the value.
         $html[] = $icon_list;
         $html[] = '<input type="hidden" name="' . $this->name . '" value="' . $this->value . '"/>';
     } else {
         // Create a regular list.
         $html[] = $icon_list;
     }
     return implode($html);
 }
Exemplo n.º 9
0
 function upload_file(&$row, $article_id, $update = false, $attachment_id = false)
 {
     global $mainframe;
     // Get the component parameters
     jimport('joomla.application.component.helper');
     $params = JComponentHelper::getParams('com_attachments');
     // Get the auto-publish setting
     $auto_publish = $params->get('publish_default', false);
     // Make sure the attachments directory exists
     $upload_subdir = $params->get('attachments_subdir', 'attachments');
     if ($upload_subdir == '') {
         $upload_subdir = 'attachments';
     }
     $upload_dir = JPATH_SITE . DS . $upload_subdir;
     $secure = $params->get('secure', false);
     if (!AttachmentsHelper::setup_upload_directory($upload_dir, $secure)) {
         $errmsg = JText::_('ERROR UNABLE TO SETUP UPLOAD DIR');
         JError::raiseError(500, "<p>" . $errmsg . " ({$upload_dir})</p>");
     }
     // If we are updating, note the name of the old filename
     $old_filename = null;
     $old_filename_sys = null;
     if ($update) {
         $old_filename = $row->filename;
         $old_filename_sys = $row->filename_sys;
     }
     // Get the new filename
     $filename = $_FILES['upload']['name'];
     $ftype = $_FILES['upload']['type'];
     // Make sure a file was successfully uploaded
     if ($update) {
         $update_file = JRequest::getVar('change', true);
     } else {
         $update_file = true;
     }
     if ($update_file && $_FILES['upload']['size'] == 0 && $_FILES['upload']['tmp_name'] == '') {
         // Guess the type of error
         if ($filename == '') {
             $error = 'no_file';
             $error_msg = JText::_('ERROR UPLOADING FILE') . ' ' . $filename;
             $error_msg .= ' (' . JText::_('YOU MUST SELECT A FILE TO UPLOAD') . ')';
             if ($mainframe->isAdmin()) {
                 return $error_msg;
             }
         } else {
             $error = 'file_too_big';
             $error_msg = JText::_('ERROR UPLOADING FILE') . ' ' . $filename;
             $error_msg .= ' <br>(' . JText::_('ERROR MAY BE LARGER THAN LIMIT') . ' ';
             $error_msg .= get_cfg_var('upload_max_filesize') . ')';
             if ($mainframe->isAdmin()) {
                 return $error_msg;
             }
         }
         // Set up the view to redisplay the form with warnings
         if ($update) {
             require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'update' . DS . 'view.php';
             $view = new AttachmentsViewUpdate();
             $view->assign('update_file', $update_file);
             $view->assign('attachment_id', $attachment_id);
         } else {
             require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'upload' . DS . 'view.php';
             $view = new AttachmentsViewUpload();
         }
         $view->assign('save_url', JRoute::_("index.php?option=com_attachments&task=save&tmpl=component"));
         $view->assign('filename', $filename);
         $view->assign('article_id', $article_id);
         $view->assign('article_title', AttachmentsHelper::get_article_title($article_id));
         $view->assign('description', $row->description);
         $view->assign('display_filename', $row->display_filename);
         $view->assign('user_field_1', $row->user_field_1);
         $view->assign('user_field_2', $row->user_field_2);
         $view->assign('user_field_3', $row->user_field_3);
         $view->assign('from', JRequest::getVar('from', ''));
         $view->assign('Itemid', JRequest::getVar('Itemid', 1));
         $view->assignRef('params', $params);
         $view->display(null, $error, $error_msg, true);
         exit;
     }
     // Make sure the file type is okay (respect restrictions imposed by media manager)
     jimport('joomla.filesystem.file');
     $cmparams =& JComponentHelper::getParams('com_media');
     // First check to make sure the extension is allowed
     $allowable = explode(',', $cmparams->get('upload_extensions'));
     $ignored = explode(',', $cmparams->get('ignore_extensions'));
     $format = strtolower(JFile::getExt($filename));
     $error = false;
     $error_msg = false;
     if (!in_array($format, $allowable) && !in_array($format, $ignored)) {
         $error = 'illegal_file_extension';
         $error_msg = JText::_('ERROR UPLOADING FILE') . ': ' . $filename;
         $error_msg .= "<br>" . JText::_('ERROR ILLEGAL FILE EXTENSION') . " {$format}";
         $error_msg .= "<br>" . JText::_('ERROR CHANGE IN MEDIA MANAGER');
     }
     // Check to make sure the mime type is okay
     if ($cmparams->get('restrict_uploads', true)) {
         if ($cmparams->get('check_mime', true)) {
             $allowed_mime = explode(',', $cmparams->get('upload_mime'));
             $illegal_mime = explode(',', $cmparams->get('upload_mime_illegal'));
             if (strlen($ftype) && !in_array($ftype, $allowed_mime) && in_array($ftype, $illegal_mime)) {
                 $error = 'illegal_mime_type';
                 $error_msg = JText::_('ERROR UPLOADING FILE') . ' ' . $filename;
                 $error_msg .= ', ' . JText::_('ERROR ILLEGAL FILE MIME TYPE') . " {$ftype}";
                 $error_msg .= "  <br>" . JText::_('ERROR CHANGE IN MEDIA MANAGER');
             }
         }
     }
     // If there was an error, refresh the form with a warning
     if ($error) {
         if ($mainframe->isAdmin()) {
             return $error_msg;
         }
         // Set up the view to redisplay the form with warnings
         if ($update) {
             require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'update' . DS . 'view.php';
             $view = new AttachmentsViewUpdate();
             $view->assign('update_file', JRequest::getVar('change', false));
             $view->assign('attachment_id', $attachment_id);
         } else {
             require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'upload' . DS . 'view.php';
             $view = new AttachmentsViewUpload();
         }
         $view->assign('save_url', JRoute::_("index.php?option=com_attachments&task=save&tmpl=component"));
         $view->assign('filename', $filename);
         $view->assign('article_id', $article_id);
         $view->assign('article_title', AttachmentsHelper::get_article_title($article_id));
         $view->assign('description', $row->description);
         $view->assign('display_filename', $row->display_filename);
         $view->assign('user_field_1', $row->user_field_1);
         $view->assign('user_field_2', $row->user_field_2);
         $view->assign('user_field_3', $row->user_field_3);
         $view->assign('from', JRequest::getVar('from', ''));
         $view->assign('Itemid', JRequest::getVar('Itemid', 1));
         $view->assignRef('params', $params);
         $view->display(null, $error, $error_msg, true);
         exit;
     }
     // Define where the attachments go
     $upload_url = $params->get('attachments_subdir', 'attachments');
     $upload_dir = JPATH_SITE . DS . $upload_url;
     // Figure out the system filename
     $filename_sys = null;
     $url = null;
     $prepend = $params->get('prepend', 'article_id');
     switch ($prepend) {
         case 'article_id':
             $prefix = sprintf("%03d_", $article_id);
             $filename_sys = $upload_dir . DS . $prefix . $filename;
             $url = $upload_url . "/" . $prefix . $filename;
             break;
             // NOTE: for attachment_id, save normally and make a second pass
             //       to rename the file after we know the attachment ID
         // NOTE: for attachment_id, save normally and make a second pass
         //       to rename the file after we know the attachment ID
         default:
             $filename_sys = $upload_dir . DS . $filename;
             $url = $upload_url . "/" . $filename;
     }
     // If not updating, make sure the system filename doesn't already exist
     $error = false;
     if (!$update && JFile::exists($filename_sys) && $prepend != 'attachment_id') {
         $error = 'file_not_on_server';
         $error_msg = JText::_('ERROR FILE ALREADY ON SERVER');
         $error_msg .= "   ({$filename})";
         if ($mainframe->isAdmin()) {
             return $error_msg;
         }
         // Set up the view to redisplay the form with warnings
         require_once JPATH_COMPONENT_SITE . DS . 'views' . DS . 'upload' . DS . 'view.php';
         $view = new AttachmentsViewUpload();
         $view->assign('filename', $filename);
         $view->assign('save_url', JRoute::_("index.php?option=com_attachments&task=save&tmpl=component"));
         $view->assign('article_id', $article_id);
         $view->assign('article_title', AttachmentsHelper::get_article_title($article_id));
         $view->assign('description', $row->description);
         $view->assign('display_filename', $row->display_filename);
         $view->assign('user_field_1', $row->user_field_1);
         $view->assign('user_field_2', $row->user_field_2);
         $view->assign('user_field_3', $row->user_field_3);
         $view->assign('from', JRequest::getVar('from', ''));
         $view->assign('Itemid', JRequest::getVar('Itemid', 1));
         $view->assignRef('params', $params);
         $view->display(null, $error, $error_msg, true);
         exit;
     }
     // Get the maximum allowed filename length (for the filename display)
     $max_filename_length = $params->get('max_filename_length', 0);
     if (is_numeric($max_filename_length)) {
         $max_filename_length = intval($max_filename_length);
     } else {
         $max_filename_length = 0;
     }
     // Create a display filename, if needed (for long filenames)
     if ($max_filename_length > 0 and strlen($row->display_filename) == 0 and strlen($filename) > $max_filename_length) {
         $row->display_filename = AttachmentsHelper::truncate_filename($filename, $max_filename_length);
     }
     // Copy the info about the uploaded file into the new record
     $row->filename = $filename;
     $row->filename_sys = $filename_sys;
     $row->url = $url;
     $row->file_type = $ftype;
     $row->file_size = $_FILES['upload']['size'];
     $row->published = $auto_publish;
     // Set the create/modify dates
     jimport('joomla.utilities.date');
     $now = new JDate();
     $row->create_date = $now->toMySQL();
     $row->modification_date = $row->create_date;
     // Add the icon file type
     require_once JPATH_COMPONENT_SITE . DS . 'file_types.php';
     $row->icon_filename = AttachmentsFileTypes::icon_filename($filename, $ftype);
     // Save the updated attachment
     if (!$row->store()) {
         JError::raiseError(500, $row->getError());
     }
     // Get the upload id
     $db =& JFactory::getDBO();
     $attachment_id = $db->insertid();
     // If we're prepending attachment IDs, fix the system filename and URL and
     // update the attachment record (now that we know the attachment ID)
     if ($prepend == 'attachment_id') {
         $prefix = sprintf("%03d_", $attachment_id);
         $filename_sys = $upload_dir . DS . $prefix . $filename;
         $url = $upload_url . "/" . $prefix . $filename;
         $row->id = $attachment_id;
         $row->filename_sys = $filename_sys;
         $row->url = $url;
         $row->store();
     }
     // Move the file
     $msg = "";
     if (JFile::upload($_FILES['upload']['tmp_name'], $filename_sys)) {
         $size = intval($row->file_size / 1024.0);
         chmod($filename_sys, 0644);
         if ($update) {
             $msg = JText::_('UPDATED ATTACHMENT') . ' ' . $filename . " (" . $size . " Kb)!";
         } else {
             $msg = JText::_('UPLOADED ATTACHMENT') . ' ' . $filename . " (" . $size . " Kb)!";
         }
     } else {
         $query = "DELETE FROM #__attachments WHERE id={$attachment_id}";
         $db->setQuery($query);
         $result = $db->query();
         $msg = JText::_('ERROR MOVING FILE') . " {$_FILES['upload']['tmp_name']} -> {$filename_sys})";
     }
     // If we are updating, we may need to delete the old file
     if ($update) {
         if ($filename != $old_filename) {
             JFile::delete($old_filename_sys);
         }
     }
     return $msg;
 }
Exemplo n.º 10
0
 /**
  * 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;
 }