CMS MADE SIMPLE FORGE

Secure file download

 

[#3775] Added global options and direct option.

avatar
Created By: Artūras Šlajus (arturaz)
Date Submitted: Fri Jul 24 11:12:52 -0400 2009

Assigned To: fitzelputzel (tkeil69575)
Version: v1.1
CMSMS Version: None
Severity: Minor
Resolution: None
State: Open
Summary:
Added global options and direct option.
Detailed Description:
<?php
#CMS - CMS Made Simple
#(c)2004 by Ted Kulp (wishy@users.sf.net)
#This project's homepage is: http://cmsmadesimple.sf.net
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function format_filesize ($int,$bytesstyle = false) {
	if ($bytesstyle != false) {$bytes = $int;}
	$str = " bytes";
	if ($int > 1024+256){$int /= 1024; $str = " KB";}
	if ($int > 1024+256){$int /= 1024; $str = " MB";}
	if ($int > 1024+256){$int /= 1024; $str = " GB";}
	if ($int > 1024+256){$int /= 1024; $str = " TB";}
	$int2 = round(number_format($int,2),2);
	if ($str != " bytes" AND $bytesstyle == 1){$str .= " (".$bytes." bytes)";}
	return $int2.$str;
} 
	
function smarty_cms_function_securefile($params, &$smarty) {

	//get and set the parameters
	$sizeoffile = $show_title = "";
	$path = isset($params['path']) ? $params['path']:false;
	$title = isset($params['title']) ? $params['title']:false;

  $smartyparams = array(
    'showsize' => $smarty->get_template_vars('securefile_showsize'),
    'direct' => $smarty->get_template_vars('securefile_direct')
  );
	$showsize = isset($params['showsize']) ? $params['showsize'] : (
    isset($smartyparams['showsize']) ? $smartyparams['showsize'] : 0
  );
	$direct = isset($params['direct']) ? $params['direct'] : (
    isset($smartyparams['direct']) ? $smartyparams['direct'] : 0
  );

  $extension = strtoupper(substr($path, (strrpos($path,".")+1)));
  $name = isset($params['name']) ? $params['name']:"[$extension]";	
  $encoded_link = base64_encode("$path");
  if ($title!=false) {$show_title = " title=\"$title\"";}
  if ($showsize==1 and file_exists("$path")) {
    $sizeoffile = " ~".format_filesize(filesize("$path"));
  }

  if ($direct) {
    echo "<a href=\"$path\"{$show_title}>$name</a>{$sizeoffile}";
  }
  else {
echo "<a
href=\"download.php?id=$encoded_link\"{$show_title}>$name</a>{$sizeoffile}";
  }

}

function smarty_cms_help_function_securefile() {
	?>

	<h3>What does this tag do?</h3>
	<ul>
<li>Makes obfuscated link to a downloadable file in htaccess protected
folder.</li>
<li><font color="red">Important:</font> Dependencies for this function is the
file download.php<br />
You must configure the settings in this file before using and upload it to your
root folder.</li>
	</ul>
	<h3>How do I use this tag?</h3>
<blockquote><b>Just insert the tag into your template/page like in one of the
examples here: </b><br />
  <code>1. {securefile path='uploads/secure/securedoc.pdf'}</code><code><br>
2. {securefile path='uploads/secure/securedoc.pdf' name='My File' title='My
Title' showsize=1}</code><code><br>
3. {securefile path='uploads/secure/securedoc.pdf' name='&lt;img
src="image.jpg" /&gt;' title='My Title'}</code>
  </blockquote>
	<h3>What parameters does it take?</h3>
	<ul>
<li><em>(optional)</em> <code>name</code> - Show your link name, instead of
standard link, e.g. name='My secure document' (can be an image too), instead of
[PDF] or whatever extension your file has</li>
<li><em>(optional)</em> <code>title</code> - Add title to download link, e.g.
title='Click here to download'</li>
<li><em>(optional)</em> <code>showsize</code> - Show file size behind download
link, showsize=1, will show e.g. ~345.23 KB </li>
<li><em>(optional)</em> <code>direct</code> - just generate direct &lt;a&gt;
link instead of using download.php</li>
	</ul>
  <h3>Global options</h3>
<p>Both showsize and direct options can be toggled using global variables
prefixed with "securefile_". In example:</p>
  <blockquote>
<code>{assign var='securefile_showsize' value=1}</code> <br />
<code>{securefile path='uploads/secure/securedoc.pdf'}</code> (this will show
size) <br />
<code>{securefile path='uploads/secure/securedoc.pdf' showsize=0}</code> (this
will not show size)<br />
  </blockquote>
	<?php
}

function smarty_cms_about_function_securefile() {
	?>
	<p>Author: Tina Keil, Artūras Šlajus &lt;x11@arturaz.net&gt;<br />
    Version: 1.1, 24.07.2009</p>
  <ul>
    <li>24.07.2009 - Added global options and direct option.</li>
  </ul>
	<?php
}
?>


History

Updates

Updated: 2011-12-09 12:08
description: resolution_id: => 5
cmsms_version_id: => -1