", "");
echo dl_loop_get_url('" . dl_loop_get_filename('', '') . '') . "
";
echo ("..." . dl_loop_get_description("
", "
"));
echo dl_loop_get_date() . ", " . dl_loop_get_size();
}
This outputs a list with all downloads for the current page.
ChangeLog:
+ 06.06.2005, 0.1
Initial release
+ 07.06.2005, 0.1a
Fixed some bugs (array handling, ...)
Added preliminary documentation :)
+ 14.06.2005, 0.1b
_Hack_ for PHP < 5.x:
PHP < 5.x is apparently unable to resolve object member refereces such
as 'current($list)->entry'. This causes PHP to die with a parse error.
(Thanks to Krischan)
+ 30.6.2005 0.2
Correct working and support for filtering (protected files)
Released under the GPL license
http://www.gnu.org/licenses/gpl.txt
This file is part of WordPress.
WordPress 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
*/
define("DL_FORM", "Downloads");
define("DL_MAPPING_FORM", "DownloadsMapping");
$sizes = array('bytes', 'kbytes', 'mbytes', 'gbytes', 'tbytes');
$datefmt = "d.m.Y";//, H:i:s";
/*$dl_new = -1;
$dl_edit = -1;
$dl_commit = -1;
$dl_mapping_new = -1;
$dl_mapping_edit = -1;
$dl_mapping_commit = -1;
$dl_page_id = -1;
$dl_show_category = '';
$dl_error = '';
$dl_commited = false;
*/
/*
* Create tables required by the plugin
*/
function dl_create_tables() {
global $wpdb;
// main table
$wpdb->query(
"CREATE TABLE IF NOT EXISTS $wpdb->dl (
id INTEGER UNSIGNED NOT NULL auto_increment,
description VARCHAR(255) NOT NULL default '',
category VARCHAR(64) NOT NULL default '',
url VARCHAR(255) NOT NULL default '',
owner INTEGER NOT NULL default 1,
page INTEGER NOT NULL default 0,
position INTEGER NOT NULL default 0,
date DATETIME NOT NULL default '0000-00-00 00:00:00',
creation_date DATETIME NOT NULL default '0000-00-00 00:00:00',
protected INTEGER UNSIGNED NOT NULL default 0,
PRIMARY KEY(id))");
// mapping table
$wpdb->query(
"CREATE TABLE IF NOT EXISTS $wpdb->dlmap (
id INTEGER UNSIGNED NOT NULL auto_increment,
url VARCHAR(255) NOT NULL default '',
localpath VARCHAR(255) NOT NULL default '',
PRIMARY KEY(id))");
}
/*
* Loads url-localdir-mappings from the database
*/
function dl_load_mappings() {
global $wpdb;
global $mappings;
$results = $wpdb->get_results(
"SELECT * FROM $wpdb->dlmap");
if ($results) {
foreach ($results as $result) {
$mappings[$result->url] = $result->localpath;
}
}
}
/*
* Initialize Downloads
* - Setup database
* - Load url-localdir-mappings
*/
function dl_initialize() {
global $table_prefix;
global $wpdb;
// Register database names
$wpdb->dl = $table_prefix . 'dl';
$wpdb->dlmap = $table_prefix . 'dlmap';
// Create tables in database if required
dl_create_tables();
// Load mappings from database
dl_load_mappings();
}
/*
* Returns the value of $name if it is an integer
* $default otherwise.
*/
function dl_get_int($name, $default = NULL) {
$result = $default;
// Get the value if it is an integer
if (isset($_REQUEST[$name])) {
if (is_numeric($_REQUEST[$name])) {
$result = $_REQUEST[$name];
}
}
return $result;
}
/*
* Returns the url base for the given url
* - proto://user:pass@host:port/path -> proto://user:pass@host:port/
*/
function dl_get_urlbase($url) {
$parsed = parse_url($url);
$urlbase = $parsed['scheme'] ? $parsed['scheme'] . '://' : '';
$urlbase .= $parsed['user'] ? $parsed['user'] . ($parsed['pass'] ? $parsed['pass'] : '') . '@' : '';
$urlbase .= $parsed['host'] ? $parsed['host'] : '';
$urlbase .= $parsed['port'] ? ':' . $parsed['port'] : '';
$urlbase .= '/';
return $urlbase;
}
/*
* Returns the path of a given url
* - proto://user:pass@host:port/path -> path
*/
function dl_get_urlpath($url) {
$parsed = parse_url($url);
return ltrim($parsed['path'], '/');
}
/*
* Returns the file of a given url
* - proto://user:pass@host:port/dir/file -> file
*/
function dl_get_urlfile($url) {
$path = dl_get_urlpath($url);
$last_slash = strrpos($path, '/');
return substr($path, $last_slash + 1, strlen($path) - $last_slash - 1);
}
/*
* Returns the directory of a given url
* - proto://user:pass@host:port/dir/file -> dir/
*/
function dl_get_urldir($url) {
$path = dl_get_urlpath($url);
$last_slash = strrpos($path, '/');
return substr($path, 0, $last_slash);
}
/*
* Check if the file specifed by the url is hosted locally
*/
function dl_file_is_local($url) {
$pos = strpos($url,dl_get1stmapping());
return ($pos == 0);
}
/*
* Returns the local path for the given url
* URL MUST correspond to a locally hosted file
*/
function dl_get_local_path($url) {
$mapping = dl_get1stmapping();
$folder = dl_get1stuploadfolder();
$pos = strpos($url,$mapping);
if ($pos != 0) return NULL;
$rest = substr($url,strlen($mapping));
return $folder . $rest;
}
/*
* Returns the directory part of a path
*/
function dl_get_dirname($path) {
$pos = strrpos($path, '/');
if ($pos > -1) {
return substr($path, 0, $pos + 1);
}
return NULL;
}
/*
* Returns the filename part of a path
*/
function dl_get_filename($path) {
$pos = strrpos($path, '/');
if ($pos > -1) {
return substr($path, $pos + 1, strlen($path) - $pos - 1);
}
return $path;
}
/*
* Save a file that was passed via HTTP POST
* Returns the final URL where the file resides. When no file
* was uploaded, dl_url is returned. In case of an error, NULL is returned.
*
* Input: full URL of an external resource
* or
* URL of the folder on the current server where the file will go
*/
function dl_save_file($url,$protected) {
global $wpdb;
global $dl_error;
// Get the url and the filename
// changed - now we are passing it $url = $_REQUEST['dl_url'];
$file = $_FILES['dl_userfile'];
// If no file was passed, assume it is an external resource - URL remains untouched
if ($file['name'] == "") {
return $url;
}
// If no error occured proceed
if ($file['error'] == 0) {
// Get the local path for the file
$localpath = dl_get1stuploadfolder($protected);
if ($localpath != NULL) {
// check if the directory exists, otherwise create it
if ((file_exists(dl_get_dirname($localpath))) ||
(mkdir(dl_get_dirname($localpath), 0755, true)))
{
$localpath .= $file['name'];
if ($protected) {
$url .= "protect/" . $file['name'];
} else {
$url .= $file['name'];
}
// Move the uploaded file
if (!move_uploaded_file($file['tmp_name'], $localpath)) {
// Unable to move the file? Emit an error message.
$dl_error = "Error moving file " . $file['name'] . " to " . $localpath . "!";
}
return $url;
} else {
// Unable to create the path
$dl_error = "Error creating " . $localpath . "!";
}
} else {
// No mapping exists for the specified url
$dl_error = "No mapping available for " . $url . "!";
}
} else {
// Unable to save the file
$dl_error = "Error saving file (" . $file['error'] . ")";
}
return NULL;
}
/*
* Echoes out a list (option fields for select) of categories for the passed page id.
* If the page id is -1, all categories are returned
*/
function dl_get_category_list($page = -1) {
global $wpdb;
$filter = "";
if ($page > -1) {
$filter = "WHERE page=$page ";
}
$results = $wpdb->get_results(
"SELECT DISTINCT category " .
"FROM $wpdb->dl " .
$filter .
"ORDER BY category");
foreach ($results as $row) {
?>
query(
"DELETE FROM $wpdb->dlmap WHERE id=$id");
}
/*
* Remove a download
*/
function dl_remove($id) {
global $wpdb;
// Remove file
$results = $wpdb->get_results(
"SELECT * " .
"FROM $wpdb->dl " .
"WHERE id=$id");
if ($results) {
foreach ($results as $row) {
$url = $row->url;
$localpath = dl_get_local_path($row->url);
//echo ("Cancello: $url - " . $localpath);
unlink($localpath);
$wpdb->query(
"DELETE FROM $wpdb->dl WHERE id=$id");
}
} else {
echo ("Unexistent> $id");
}
return;
}
/*
* Get the next free position withing a category in a page
*/
function dl_download_next_free_position($page_id, $category) {
global $wpdb;
$result = $wpdb->get_var(
"SELECT MAX(position) " .
"FROM $wpdb->dl ".
"WHERE page=$page_id AND category='$category'");
return (isset($result) ? $result + 1 : 0);
}
/*
* Move a download entry within a category in a page one position up
*/
function dl_download_move_up($id) {
global $wpdb;
$row = $wpdb->get_row(
"SELECT id, page, category, position " .
"FROM $wpdb->dl WHERE id=$id");
// Check if there is such a row
if ($row) {
$position = $row->position;
// Move element up iff position is greater than 0
if ($position > 0) {
$position--;
$prevrow = $wpdb->get_row(
"SELECT id, position " .
"FROM $wpdb->dl " .
"WHERE page=$row->page AND category='$row->category' AND position<=$position " .
"ORDER BY position DESC");
// If there is a previous element, move it one position down
if ($prevrow) {
$position = $prevrow->position;
$prevrow_position = $position + 1;
$wpdb->query(
"UPDATE $wpdb->dl " .
"SET position=$prevrow_position WHERE id=$prevrow->id");
}
// Move the current element one to the new position
$wpdb->query(
"UPDATE $wpdb->dl " .
"SET position=$position " .
"WHERE id=$id");
}
}
}
/*
* Move a download entry within a category in a page one position down
*/
function dl_download_move_down($id) {
global $wpdb;
$row = $wpdb->get_row(
"SELECT id, position, page, category " .
"FROM $wpdb->dl WHERE id=$id");
// Check if there if such a row
if ($row) {
$position = $row->position;
// Get the last download id
$next_free = dl_download_next_free_position(
$row->page,
$row->category) - 1;
// Move element down iff we're not at the end of the list
if ($next_free > $row->position) {
$position++;
$nextrow = $wpdb->get_row(
"SELECT id, position " .
"FROM $wpdb->dl " .
"WHERE page=$row->page AND category='$row->category' AND position>=$position " .
"ORDER BY position ASC");
// If there is a previous element, move it one position up
if ($nextrow) {
$position = $nextrow->position;
$nextrow_position = $position - 1;
$wpdb->query(
"UPDATE $wpdb->dl " .
"SET position=$nextrow_position " .
"WHERE id=$nextrow->id");
}
// Move the current element one to the new position
$wpdb->query(
"UPDATE $wpdb->dl " .
"SET position=$position " .
"WHERE id=$id");
}
}
}
/*
* Create or commit changes for new/existent mapping
*/
function dl_mapping_commit_changes($id, $url, $localpath) {
global $wpdb;
if ($id > -1) {
// If an id is given, try to update the entriy
$query =
"UPDATE $wpdb->dlmap " .
"SET url='$url', localpath='$localpath' " .
"WHERE id=$id";
} else {
// Otherwise insert a new one
$query =
"INSERT INTO $wpdb->dlmap(url, localpath) " .
"VALUES(" .
"'$url', " .
"'$localpath')";
}
// Fire query :)
$wpdb->query($query);
}
/*
* Create or commit changes for new/existent download
*/
function dl_commit_changes($id, $description, $url, $category, $protected, $page) {
global $wpdb;
if ($id > -1) {
// If an id is given, try to update the entry
$query =
"UPDATE $wpdb->dl " .
"SET description='$description', url='$url', category='$category', page=$page, date='" . current_time('mysql') . "', " .
"protected=$protected " .
"WHERE id=$id";
} else {
// Otherwise insert a new entry after the last one
$query =
"INSERT INTO $wpdb->dl(description, url, category, page, position, date, creation_date, protected)" .
"VALUES(" .
"'$description', " .
"'$url', " .
"'$category', " .
$page . ", " .
dl_download_next_free_position($page, $category) . ", " .
"'" . current_time('mysql') . "', " .
"'" . current_time('mysql') . "', " .
$protected . ")";
}
// Fire query :)
$wpdb->query($query);
}
/*
* Form for creating a mapping
*/
function dl_mapping_edit_form($id = 0, $url = '', $localpath = '') {
global $wpdb;
?>