<?
/*
* Libretto - Web download manager.
* Copyright (C) 2007 Jordi Pujol (jordi_pujolATtelefonicaDOTnet).
*
* 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 t($text) {
global $globalLanguage;
global $localization;
return (isset($localization[$text])) ? $localization[$text] :
( (isset($globalLanguage[$text])) ? $globalLanguage[$text] : "???" . $text );
}
function getHtmlContent($content) {
global $lang;
$file = (file_exists("content/".$content."_".$lang.".php")) ? "content/".
$content."_".$lang.".php" : "content/".$content.".php" ;
include $file;
}
function getLocalization() {
global $lang, $locales;
global $localization;
global $decimal_point, $thousands_sep;
//setlocale(LC_ALL, $lang);
setlocale(LC_ALL, $locales[$lang]);
$locale_info = localeconv();
$decimal_point = $locale_info['decimal_point'];
if ($decimal_point=='.')
$thousands_sep = ',';
else
$thousands_sep = '.';
if (file_exists("includes/lang-" . $lang . ".php")) {
include "includes/lang-" . $lang . ".php";
} else {
$localization = false;
}
}
function parseContent($content) {
$org = array("[title]", "[/title]", "[html]", "[/html]");
$dest = array("<h1>", "</h1>", "", "");
return str_replace($org, $dest, $content);
}
/**
* Displays for for language selection
*
* @access public
*/
function select_language() {
global $lang;
global $lang_codes;
$links = " ".t('language')." ".
"<select name=\"lang\" onchange=\"submit();\"".
" title=\"".t('language')."\">" . "\n";
foreach ($lang_codes AS $id) {
//Is current one active?
if ($lang == $id) {
$selected = ' selected="selected"';
} else {
$selected = '';
}
$links .= " ";
$links .= '<option value="' . $id . '" title="' . t('language-'.$id) . '"' . $selected . '>' . t($id) . '</option>' . "\n";
}
$links .= " </select>" . "\n";
$links .= '<input type="hidden" name="_submit_lang" value="1"/>' . "\n";
return $links;
} // End of function select_language
function my_file_put_contents($filename, $data, $append = false) {
$fp = fopen($filename, $append ? 'ab' : 'wb');
if ( !$append )
chmod($filename, 0600); // This file may contain passwords
fwrite($fp, print_r($data, true)."\n");
fclose($fp);
}
function get_available_port() {
include "includes/globals.php";
$address = '127.0.0.1';
$sock = socket_create (AF_INET, SOCK_STREAM, 0);
if ($sock < 0)
die ('Cannot create a socket');
for ( $port = $serverports[0]; $port <= $serverports[1]; $port++ )
if (@socket_bind ($sock, $address, $port)) {
socket_close($sock);
return $port;
}
return false;
}
/**
* Loads the configuration and sets the base URL correctly.
*/
function conf_init() {
include "includes/globals.php";
include_once 'settings.php';
if (!defined("CSORT_ASC"))
define("CSORT_ASC", 1);
if (!defined("CSORT_DESC"))
define("CSORT_DESC", -1);
if ( ! isset($_SESSION['sort']) )
$_SESSION['sort'] = false;
if ( ! isset($_SESSION['sortorder']) )
$_SESSION['sortorder'] = CSORT_ASC;
$user = '';
$userpass = '';
$rmask = (0777 & ~$createumask);
if (isset($authrequire))
if ($authrequire)
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW_Authenticate: Basic realm="Libretto Download Manager"');
header('HTTP/1_0 401 Unauthorized');
echo 'Sorry, contact your Web Site Administrator !';
exit;
} else {
$user = $_SERVER['PHP_AUTH_USER'];
$userpass = $_SERVER['PHP_AUTH_PW'];
if (!isset($users[$user])) {
header('WWW_Authenticate: Basic realm="Libretto Download Manager"');
header('HTTP/1_0 401 Unauthorized');
echo 'Sorry, contact your Web Site Administrator !';
exit;
}
}
_get_programs();
$storagedir = $destdir;
$username = '';
if ($user)
if ($users[$user]) {
if (isset($users[$user]['destdir']))
$storagedir = $users[$user]['destdir'];
if (isset($users[$user]['displayfolder']))
$displayfolder = $users[$user]['displayfolder'];
$username = $users[$user]['name'];
}
if (!file_exists($tmpdir))
@mkdir($tmpdir, 0711); // attempt to create; it may fail...
if (!file_exists($destdir))
@mkdir($destdir, 0711); // attempt to create; it may fail...
/* $stat = stat($tmpdir);
if ($stat['mode'] & 0006) {
if ($printwarnings)
print("WARNING: {$tmpdir} is publicly accessible! This is a security risk, as temporary files may contain passwords.<br>");
}*/
}
/**
* This function gets a list of programs that will be used to perform the
* download.
*
* set the downloaders array where the key is the downloader name
* and the value is the path to the program, or null if it couldn't be
* found.
*/
function _get_programs() {
include "includes/globals.php";
foreach ( $downloaders AS $downloadername => $downloader ) {
if ( isset($downloader['binary']) && ! isset($downloader['fullpath']) )
$downloaders[$downloadername]['fullpath'] = _search_path($downloader['binary']);
}
} // End of _get_programs()
/**
* Walk through the path and find the location of a specific file.
*
* @return mixed The full pathname to the file, or null on failure.
*/
function _search_path($file) {
$retval = false;
$path = getenv('PATH');
$path = explode(':', $path);
foreach ($path as $key => $value) {
$file2 = $value . '/' . $file;
if (is_file($file2)) {
$retval = $file2;
break;
}
}
return($retval);
} // End of _search_path()
function sort_by_mtime($file1,$file2) {
$time1 = filemtime($file1);
$time2 = filemtime($file2);
return ( $time1 == $time2 ) ?
0 :
( $time1 < $time2 ? 1 : -1 );
}
function csort_cmp(&$a, &$b) {
global $csort_cmp;
if ($a[$csort_cmp['key']] > $b[$csort_cmp['key']])
return $csort_cmp['direction'];
if ($a[$csort_cmp['key']] < $b[$csort_cmp['key']])
return -1 * $csort_cmp['direction'];
return 0;
}
function csort(&$a, $k, $sort_direction=CSORT_ASC) {
global $csort_cmp;
$csort_cmp = array(
'key' => $k,
'direction' => $sort_direction
);
usort($a, "csort_cmp");
unset($csort_cmp);
}
function formatTrimZeroes($p) {
include "includes/globals.php";
return rtrim(rtrim(number_format($p, 2,
$decimal_point, $thousands_sep),"0"),$decimal_point);
}
// Display the Percent Graphical Bar
function displayPercentBar($percent) {
$res = '<table width="100%" border="0">'."\n";
$res .= '<tr>'."\n";
$res .= '<td bgcolor="lime"'."\n";
$res .= 'width="'.$percent.'%">'."\n";
$res .= '<div class="tinypercent" align="center">'."\n";
$res .= formatTrimZeroes($percent).'%</div>'."\n";
$res .= '</td>'."\n";
if ( 100 > $percent ) {
$res .= '<td bgcolor="grey"'."\n";
$res .= 'width="'.(100 - $percent).'% ">'."\n";
$res .= '</td>'."\n";
}
$res .= '</tr>'."\n";
$res .= '</table>'."\n";
return $res;
}
function displayUserName() {
include "includes/globals.php";
if ($user) {
echo '<h4>'.t('user').': '.$user.', '.$username;
if ($authrequire && isset($users[$user]['administrator']) && $users[$user]['administrator'] )
echo ' : '.t('administrator');
echo '</h4>';
}
}
// ***************************************************************************
// ***************************************************************************
// Display the Drive Space Graphical Bar
function displayDriveSpaceBar() {
include "includes/globals.php";
$res = Array();
$res['total'] = disk_total_space($storagedir);
$res['free'] = disk_free_space($storagedir);
$res['freemb'] = @formatTrimZeroes($res['free'] / 1024 / 1024);
$res['used'] = $res['total'] - $res['free'];
$res['usedmb'] = @formatTrimZeroes($res['used'] / 1024 / 1024);
$res['percent'] = @formatTrimZeroes(100 * $res['used'] / $res['total']);
$rest = Array();
$rest['total'] = disk_total_space($tmpdir);
$rest['free'] = disk_free_space($tmpdir);
$rest['freemb'] = @formatTrimZeroes($rest['free'] / 1024 / 1024);
$rest['used'] = $rest['total'] - $rest['free'];
$rest['usedmb'] = formatTrimZeroes($rest['used'] / 1024 / 1024);
$rest['percent'] = formatTrimZeroes(100 * $rest['used'] / $rest['total']);
?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="15%" align="center"><?php echo t('storagedir').' '.$storagedir; ?></td>
<td>
<table width="100%" border="0">
<tr>
<td bgcolor="grey"
width="<?php echo $res['percent'] ?>%">
<div class="tinypercent" align="center"><font color="white">
<?php echo $res['percent']."% ".$res['usedmb'] . ' MB'; ?>
</font></div>
</td>
<td bgcolor="lime"
width="<?php echo (100 - $res['percent']) ?>% ">
<div class="tinypercent" align="center">
<?php echo $res['freemb'] . ' MB'; ?>
</div>
</td>
</tr>
</table>
</td>
<td width="15%" align="center"><?php echo t('tmpdir').' '.$tmpdir; ?></td>
<td>
<table width="100%" border="0">
<tr>
<td bgcolor="grey" width="<?php echo $rest['percent'] ?>%">
<div class="tinypercent" align="center"><font color="white">
<?php echo $rest['percent']."% ".$rest['usedmb'] . ' MB'; ?>
</font></div>
</td>
<td bgcolor="lime"
width="<?php echo (100 - $rest['percent']) ?>%">
<div class="tinypercent" align="center">
<?php echo $rest['freemb'] . ' MB'; ?>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
<?php
}
?>