Skip to content

Commit

Permalink
Added checks if file URLs are accessible (considered broken for HTTP …
Browse files Browse the repository at this point in the history
…status code less than 200 OR higher equal to 400)
  • Loading branch information
ggppdk committed Sep 14, 2017
1 parent b58fd15 commit d471a11
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 49 deletions.
57 changes: 35 additions & 22 deletions admin/controllers/filemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,28 @@ function save()
else
{
// Validate file URL
$data['filename_original'] = flexicontent_html::dataFilter($data['filename_original'], 4000, 'URL', 0); // Clean bad text/html
$data['filename'] = $data['filename_original'];
$url = flexicontent_html::dataFilter($data['filename_original'], 4000, 'URL', 0); // Clean bad text/html
$data['filename'] = $data['filename_original'] = $url;

if ( empty($data['size']) )
// Get file size from submitted field (file URL), set to zero if no size unit specified
if ( !empty($data['size']) )
{
$data['size'] = $model->get_file_size_from_url($data['filename_original']);
if ($data['size'] < 0 || empty($data['size']))
{
$data['size'] = 0;
}
}
else
{
// Get file size from submitted field (file URL), set to zero if no size unit specified
$arr_sizes = array('KBs'=>1024, 'MBs'=>(1024*1024), 'GBs'=>(1024*1024*1024));
$size_unit = (int) @ $arr_sizes[$data['size_unit']];
$data['size'] = ((int)$data['size']) * $size_unit;
}

else
{
$data['size'] = $model->get_file_size_from_url($url);

if ($data['size'] === -999)
{
$app->enqueueMessage($url . ' -- ' . $model->getError(), 'warning');
}

$data['size'] = $data['size'] < 0 ? 0 : $data['size'];
}
}

// Validate access level exists (set to public otherwise)
Expand Down Expand Up @@ -699,8 +703,8 @@ function addurl($Fobj = null, & $exitMessages = null)
$this->runMode = $Fobj ? 'interactive' : $this->runMode;
$file_id = 0;

$filename = $this->input->get('file-url-data', null, 'string');
$filename = flexicontent_html::dataFilter($filename, 4000, 'URL', 0); // Validate file URL
$url = $this->input->get('file-url-data', null, 'string');
$url = flexicontent_html::dataFilter($url, 4000, 'URL', 0); // Validate file URL
$altname = $this->input->get('file-url-title', null, 'string');

$filedesc = flexicontent_html::dataFilter($this->input->get('file-url-desc', '', 'string'), 32000, 'STRING', 0); // Limit number of characters
Expand All @@ -715,7 +719,7 @@ function addurl($Fobj = null, & $exitMessages = null)
jimport('joomla.utilities.date');

// check if the form fields are not empty
if (!$filename || !$altname)
if (!$url || !$altname)
{
$this->exitHttpHead = array( 0 => array('status' => '400 Bad Request') );
$this->exitMessages = array( 0 => array('error' => 'FLEXI_WARNFILEURLFORM') );
Expand All @@ -727,8 +731,14 @@ function addurl($Fobj = null, & $exitMessages = null)

if (empty($filesize))
{
$filesize = $model->get_file_size_from_url($filename);
if ($filesize < 0) $filesize = 0;
$filesize = $model->get_file_size_from_url($url);

if ($filesize === -999)
{
$app->enqueueMessage($url . ' -- ' . $model->getError(), 'warning');
}

$filesize = $filesize < 0 ? 0 : $filesize;
}

else
Expand All @@ -742,14 +752,17 @@ function addurl($Fobj = null, & $exitMessages = null)
}

// we verifiy the url prefix and add http if any
if (!preg_match("#^http|^https|^ftp#i", $filename)) { $filename = 'http://'.$filename; }
if (!preg_match("#^http|^https|^ftp#i", $url))
{
$url = 'http://'.$url;
}

$db = JFactory::getDbo();
$user = JFactory::getUser();

$obj = new stdClass();
$obj->filename = $filename;
$obj->filename_original = $filename;
$obj->filename = $url;
$obj->filename_original = $url;
$obj->altname = $altname;

$obj->url = 1;
Expand Down Expand Up @@ -778,9 +791,9 @@ function addurl($Fobj = null, & $exitMessages = null)

$session_files = $session->get($upload_context, array());
$session_files['ids'][] = $file_id;
$session_files['names'][] = $filename;
$session_files['names'][] = $url;
$session_files['ids_pending'][] = $file_id;
$session_files['names_pending'][] = $filename;
$session_files['names_pending'][] = $url;
$session->set($upload_context, $session_files);
}

Expand Down
14 changes: 8 additions & 6 deletions admin/controllers/filemanager.raw.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,6 @@ function index()
$file->total_usage = 0;
$path = $file->secure ? COM_FLEXICONTENT_FILEPATH : COM_FLEXICONTENT_MEDIAPATH; // JPATH_ROOT . DS . <media_path | file_path>
$file_path = $path . DS . $file->filename;
$error_msg = null;

if (!$file->url)
{
Expand All @@ -249,11 +248,14 @@ function index()
$url = $file->filename_original ?: $file->filename;
if ($url)
{
$file->size = $file_model->get_file_size_from_url($url, $error_msg);
}
if ($error_msg)
{
$errors[] = $file->filename_original . ' -- ' . $error_msg['message'];
$filesize = $file_model->get_file_size_from_url($url);

if ($filesize === -999)
{
$errors[] = $url . ' -- ' . $file_model->getError();
}

$file->size = $filesize < 0 ? 0 : $filesize;
}
}

Expand Down
1 change: 1 addition & 0 deletions admin/language/en-GB/en-GB.com_flexicontent.ini
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ FLEXI_YOUR_ACCOUNT_CANNOT_UPLOAD="Your account does not have privilege to upload
FLEXI_USAGE_IN="Usage in"
FLEXI_REAL_PATH="Real path"
FLEXI_FILES_MBS="Files (MBs)"
FLEXI_REAL_SIZE="Real size"

FLEXI_FILEMAN_LIST="File list"
FLEXI_FILEMAN_INFO="Info"
Expand Down
45 changes: 37 additions & 8 deletions admin/models/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,30 +305,59 @@ protected function _afterLoad($record)
* or -1 if the size could not be determined
* or -999 if there was an error
*/
function get_file_size_from_url($url, & $error_msg = null)
function get_file_size_from_url($url)
{
// clear last error
$ignore_last_error = error_get_last();

try {
$headers = @ get_headers($url, 1);
$error_msg = error_get_last();
$headers = array('Location' => $url);

// Follow the Location headers until the actual file URL is known
while (isset($headers['Location']))
{
$url = is_array($headers['Location'])
? end($headers['Location'])
: $headers['Location'];
$headers = get_headers($url, 1);

$headers = @ get_headers($url, 1);

// Check for get headers failing to execute
if ($headers === false)
{
$error = error_get_last();

$error_message = is_array($error) && isset($error['message'])
? $error['message']
: 'Error retrieving headers of URL';
$this->setError($error_message);

return -999;
}

// Check for bad response from server, e.g. not found 404 , or 403 no access
$n = 0;
while(isset($headers[$n]))
{
$code = (int) substr($headers[$n], 9, 3);
if ($code < 200 || $code >= 400 )
{
$this->setError($headers[$n]);
return -999;
}
$n++;
}
}
}

catch (RuntimeException $e) {
$this->setError($e->getMessage());
return -999; // indicate a fatal error
}

// Get file size
$filesize = isset($headers["Content-Length"]) ? $headers["Content-Length"] : -1; // indicate that the size could not be determined
return $filesize;

// Get file size, -1 indicates that the size could not be determined
return isset($headers["Content-Length"])
? $headers["Content-Length"]
: -1;
}
}
15 changes: 4 additions & 11 deletions admin/views/file/tmpl/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,6 @@ function submitbutton(pressbutton)
$tip_class = FLEXI_J30GE ? ' hasTooltip' : ' hasTip';
$btn_class = FLEXI_J30GE ? 'btn' : 'fc_button fcsimple';
$disabled = $this->row->url ? '' : ' disabled="disabled"';

if (!$this->row->url)
{
$path = $this->row->secure ? COM_FLEXICONTENT_FILEPATH : COM_FLEXICONTENT_MEDIAPATH; // JPATH_ROOT . DS . <media_path | file_path>
$file_path = $path . DS . $this->row->filename;

$file_size = file_exists($file_path) ? filesize($file_path) : 0;
$file_size_str = $file_size < 1024 * 1024 ?
number_format(filesize($file_path) / (1024), 2) .' KBs' :
number_format(filesize($file_path) / (1024 * 1024), 2) .' MBs';
}
?>


Expand Down Expand Up @@ -837,6 +826,10 @@ function submitbutton(pressbutton)
<option value="GBs">GBs</option>
</select>
<span class="hasTooltip" title="<?php echo flexicontent_html::getToolTip('FLEXI_SIZE', 'FLEXI_SIZE_IN_FORM', 1, 1); ?>"><i class="icon-info"></i></span>

<?php if ($this->row->calculated_size !== $this->row->size) : ?>
<span class="fc-mssg fc-mssg-inline fc-nobgimage fc-warning"><?php echo JText::_('FLEXI_REAL_SIZE') . ' : ' . $this->row->calculated_size; ?></span>
<?php endif; ?>
</td>
</tr>

Expand Down
37 changes: 35 additions & 2 deletions admin/views/file/view.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ function display($tpl = null)

// Add css to document
!JFactory::getLanguage()->isRtl()
? $document->addStyleSheetVersion(JUri::base(true).'/components/com_flexicontent/assets/css/flexicontentbackend.css', FLEXI_VHASH)
: $document->addStyleSheetVersion(JUri::base(true).'/components/com_flexicontent/assets/css/flexicontentbackend_rtl.css', FLEXI_VHASH);
? $document->addStyleSheetVersion(JUri::base(true).'/components/com_flexicontent/assets/css/flexicontentbackend.css', FLEXI_VHASH)
: $document->addStyleSheetVersion(JUri::base(true).'/components/com_flexicontent/assets/css/flexicontentbackend_rtl.css', FLEXI_VHASH);
!JFactory::getLanguage()->isRtl()
? $document->addStyleSheetVersion(JUri::base(true).'/components/com_flexicontent/assets/css/j3x.css', FLEXI_VHASH)
: $document->addStyleSheetVersion(JUri::base(true).'/components/com_flexicontent/assets/css/j3x_rtl.css', FLEXI_VHASH);
Expand Down Expand Up @@ -214,6 +214,39 @@ function display($tpl = null)
}


// ***
// *** Get real file size (currently)
// ***

if (!$row->url)
{
$path = $this->row->secure ? COM_FLEXICONTENT_FILEPATH : COM_FLEXICONTENT_MEDIAPATH; // JPATH_ROOT . DS . <media_path | file_path>
$file_path = $path . DS . $this->row->filename;

$file_size = file_exists($file_path) ? filesize($file_path) : 0;
$file_size_str = $file_size < 1024 * 1024 ?
number_format(filesize($file_path) / (1024), 2) .' KBs' :
number_format(filesize($file_path) / (1024 * 1024), 2) .' MBs';
}

else
{
$url = $row->filename_original ?: $row->filename;
$filesize = $model->get_file_size_from_url($url);

if ($filesize === -999)
{
$app->enqueueMessage($model->getError(), 'warning');
}

if (empty($row->size))
{
$row->size = $filesize < 0 ? 0 : $filesize;
}

$row->calculated_size = $filesize < 0 ? 0 : $filesize;
}


//***
//*** Build access level list
Expand Down

0 comments on commit d471a11

Please sign in to comment.