", ""); 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; ?>
0) { ?>
URL:
Localpath:

0) { ?>
URL: URL of an external resource (like http://...). If you are uploading a file, just leave this one blank!
- or -
Upload file:
Description:
Page:
Group: or new:
Protected: > Select this if you want the download to be protected (registered users only)

0) && (strlen($urltest['host']) > 0)) { // Remote trailing slashes and add a single slash $url = rtrim($url, '/'); $url .= '/'; // ... the same for localpath. $localpath = rtrim($localpath, '/'); $localpath .= '/'; // Commit changes dl_mapping_commit_changes( $commit, $url, $localpath); // Indicate successful commitment $dl_commited = true; } else { // An error occured, nothing's be commited $dl_error = "Error inserting mapping! Please enter a valie URL!"; } } else { $dl_error = "Please specify URL and Localpath!"; } break; case 'commit': $commit = -1; $dl_commited = false; // Get the item id, if this is an update $commit = dl_get_int('dl_id'); if ($commit == NULL) { $commit = -1; } // At least these three parameters must be present if ($_REQUEST['dl_page_id']) { $myurl = $_REQUEST['dl_url']; if (!$myurl) { // URL unspecified - determine $myurl = dl_get1stmapping(); } // Get the category to which the item should be added if ((isset($_REQUEST['dl_category_new'])) && ($_REQUEST['dl_category_new'] != "")) { // If dl_category_new is available, a new category is created $category = $_REQUEST['dl_category_new']; } else { // Otherwise the dl_category variable must be present if (!isset($_REQUEST['dl_category'])) $category = "Varie"; else $category = $_REQUEST['dl_category']; } // Get the page on which these download should be shown $dl_page_id = dl_get_int('dl_page_id'); if ($dl_page_id == NULL) { // The parameter value is not an integer... abort $dl_page_id = -1; break; } //Get protection flag if ($_REQUEST['dl_protected']) { $protected = 1; } else { $protected = 0; } // Sanitize parameters $description = $_REQUEST['dl_description']; $description = wp_specialchars($description); $category = wp_specialchars($category); // Save the file if required $url = dl_save_file($myurl,$protected); if (!$description) $description = dl_getfilename($url); // Commit changes dl_commit_changes( $commit, $description, $url, $category, $protected, $dl_page_id); // Indicate successful commitment $dl_commited = true; } else { // An error occured, nothing's be commited $dl_error = "Please specify a page on which this download should be displayed!"; } break; case 'mapping_delete': $id = dl_get_int('dl_id'); if ($id != NULL) { // Remove the specified mapping dl_mapping_remove($id); } break; case 'delete': $id = dl_get_int('dl_id'); if ($id != NULL) { // Remove the specified download dl_remove($_REQUEST['dl_id']); } break; case 'mapping_edit': // Get the id for the mapping to edit $dl_mapping_edit = dl_get_int('dl_id'); break; case 'edit': // Get the id for the download to edit $dl_edit = dl_get_int('dl_id'); break; case 'move_up': // Move the download one position up $id = dl_get_int('dl_id'); if ($id != NULL) { dl_download_move_up($id); } break; case 'move_down': // Move the download one position down $id = dl_get_int('dl_id'); if ($id != NULL) { dl_download_move_down($id); } break; } } } /* * Form for options (not implemented yet) */ function dl_display_options_panel() { if (isset($_POST['info_update'])) { ?>

WP Downloads

Put some form input areas here.
Put some more form input areas here.
-1) { $row = $wpdb->get_row( "SELECT description, url, category, page, protected " . "FROM $wpdb->dl " . "WHERE id=$dl_edit"); dl_edit_form( $dl_edit, $row->description, $row->url, $row->category, $row->page, $row->protected); } ?>
Page:'); ?> Show downloads in category:'); ?>
 
-1) { $row = $wpdb->get_row( "SELECT * " . "FROM $wpdb->dlmap " . "WHERE id=$dl_mapping_edit"); dl_mapping_edit_form( $dl_mapping_edit, $row->url, $row->localpath); } ?>
A download mapping is used to map a local path (on the WP server) to a URL.
A mapping allows this plugin to discover the size of the files.
', ''); $result .= dl_loop_get_filename("
" . dl_loop_get_url(""), "
\n"); $result .= dl_loop_get_description("
", "
\n"); $result .= dl_loop_get_date("
", "
\n"); $result .= dl_loop_get_size("
", "
\n"); $result .= "
"; } return $result; } /* * Reset the output loop */ function dl_loop_reset() { global $dl_loop_entries_start; global $dl_loop_entries; global $dl_loop_first; global $dl_loop_category; $dl_loop_entries = $dl_loop_entries_start; $dl_loop_first = true; $dl_loop_category = ''; } /* * Initialize the output loop */ function dl_loop_load($pageid = -1, $category = '') { global $wpdb; global $dl_loop_entries_start; global $dl_loop_entries; global $dl_loop_first; global $dl_loop_category; $dl_loop_category = ''; $dl_loop_first = true; $filter = ""; // If a page id if given, modify the filter as required if ($pageid != -1) { $filter = "WHERE page=$pageid "; } // If a category name is given, modify the filter as required if ($category != "") { if ($filter != "") { $filter .= "AND "; } else { $filter = "WHERE "; } $filter .= "category='$category' "; } $dl_loop_entries = $wpdb->get_results( "SELECT * " . "FROM $wpdb->dl " . $filter . "ORDER BY category, position"); $dl_loop_entries_start = $dl_loop_entries; } /* * Continue with the next element in the output loop */ function dl_loop_next() { global $dl_loop_entries; global $dl_loop_first; if ($dl_loop_first) { $dl_loop_first = false; if ($dl_loop_entries) { return (current($dl_loop_entries) != false); } else { return false; } } return (next($dl_loop_entries) != false); } /* * Returns the current category. If this was already done an empty string * is retunred instead */ function dl_loop_get_category($before = '', $after = '') { global $dl_loop_entries; global $dl_loop_category; $dbo = current($dl_loop_entries); if ($dbo) { $category = $dbo->category; if ($category != $dl_loop_category) { $dl_loop_category = $category; return $before . ($category) . $after; } } return ''; } /* * Returns the current url */ function dl_loop_get_url($before = '', $after = '') { global $dl_loop_entries; $dbo = current($dl_loop_entries); if ($dbo->protected) return $before . "index.php?dl=" . dl_getfilename($dbo->url) . $after; else return $before . ($dbo->url) . $after; } /* * Returns the current description */ function dl_loop_get_description($before = '', $after = '') { global $dl_loop_entries; $dbo = current($dl_loop_entries); return $before . ($dbo->description) . $after; } /* * Returns the current filename */ function dl_loop_get_filename($before = '', $after = '') { global $dl_loop_entries; $dbo = current($dl_loop_entries); return $before . dl_get_filename($dbo->url) . $after; } /* * Returns the current date */ function dl_loop_get_date($before = '', $after = '', $fmt = 'd.m.Y') { global $dl_loop_entries; $dbo = current($dl_loop_entries); return $before . (date($fmt, strtotime($dbo->date))) . $after; } /* * Returns the current filesize */ function dl_loop_get_size($before = '', $after = '', $shorten = true, $remote_msg = 'remote') { global $dl_loop_entries; global $sizes; $dl = current($dl_loop_entries); if (dl_file_is_local($dl->url)) { $filesize = filesize(dl_get_local_path($dl->url)); if ($shorten) { // Determine correct size $i = 0; while ($filesize > 1024) { $filesize /= 1024; $i++; } $filesize = round($filesize) . " " . $sizes[$i]; } return $before . $filesize . $after; } return $before . $remote_msg . $after; } function dl_get1stmapping() { global $wpdb; $row = $wpdb->get_row( "SELECT * " . "FROM $wpdb->dlmap "); return $row->url; } function dl_get1stuploadfolder($protected = 0) { global $wpdb; $row = $wpdb->get_row( "SELECT * " . "FROM $wpdb->dlmap "); $folder = $row->localpath; if ($protected) $folder .= "protect/"; return $folder; } /* * gets the filename out of a specified URL */ function dl_getfilename($url = "untitled.txt") { $pos = strrpos($url,"/"); if ($pos > 0) { return substr($url,$pos+1); } else { return NULL; } } // Register hooks add_action('admin_menu', 'dl_register'); add_action('plugins_loaded', 'dl_initialize'); ?>