Downloads".
*/
/* Copyright 2006 Michael Jolley (email : jolley.small.at.googlemail.com)
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
*/
$dlm_build="B20080619";
$wp_dlm_root = get_bloginfo('wpurl')."/wp-content/plugins/download-monitor/"; //FIXED: 2 - get_settings depreciated
$max_upload_size = 10485760; //10mb
// Get extensions
$allowed_e = get_option('wp_dlm_extensions');
if (empty( $allowed_e )) {
wp_dlm_init();
$allowed_e = get_option('wp_dlm_extensions');
}
// Put into array
$allowed_extentions = explode(",",$allowed_e);
$wp_dlm_db = $table_prefix."DLM_DOWNLOADS"; //FIXED: 2 - Defining db table
$wp_dlm_db_cats = $table_prefix."DLM_CATS";
load_plugin_textdomain('wp-download_monitor', 'wp-content/plugins/download-monitor/');
################################################################################
// HANDLE UPDATES
################################################################################
function wp_dlm_update() {
global $dlm_build;
add_option('wp_dlm_build', $dlm_build, 'Version of DLM plugin', 'no');
if ( get_option('wp_dlm_build') != $dlm_build ) {
// Init again
wp_dlm_init();
// Show update message
echo '
';
_e('
The plugin has recently been updated - You may need to re-save your permalinks settings (Options/settings -> Permalinks) for the changes to occur in your blog.
',"wp-download_monitor");
// Update the build
update_option('wp_dlm_build', $dlm_build);
}
}
################################################################################
// Set up menus within the wordpress admin sections
################################################################################
function wp_dlm_menu() {
// Add submenus to the manage menu:
add_management_page(__('Downloads','wp-download_monitor'), __('Downloads','wp-download_monitor'), 6,'Downloads', 'wp_dlm_admin');
}
add_action('admin_menu', 'wp_dlm_menu');
################################################################################
// ADMIN HEADER
################################################################################
function wp_dlm_head() {
global $wp_db_version;
// Provide css based on wordpress version. Version 2.3.3 and below:
if ($wp_db_version <= 6124) {
echo '';
// Include JQUERY where needed
if( strpos($_SERVER['REQUEST_URI'], 'post.php')
|| strstr($_SERVER['PHP_SELF'], 'page-new.php')
|| $_GET['page']=="Downloads"
|| strstr($_SERVER['PHP_SELF'], 'post-new.php')
|| strstr($_SERVER['PHP_SELF'], 'page.php') )
{
echo '';
}
} else {
// 2.5 + with new interface
echo '';
}
if ($_GET['activate'] && $_GET['activate']==true) {
wp_dlm_init();
}
}
add_action('admin_head', 'wp_dlm_head');
################################################################################
// Set up database
################################################################################
function wp_dlm_init() {
add_option('wp_dlm_url', '', 'URL for download', 'no');
add_option('wp_dlm_url', 'ID', 'wp_dlm_type', 'no');
// Added options for extensions
add_option('wp_dlm_extensions', '.zip,.pdf,.mp3,.rar', '', 'no');
global $wp_dlm_db,$wp_dlm_db_cats,$wpdb;
$sql = "CREATE TABLE IF NOT EXISTS ".$wp_dlm_db." (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR (200) NOT NULL ,
`filename` LONGTEXT NOT NULL ,
`dlversion` VARCHAR (200) NOT NULL ,
`postDate` DATETIME NOT NULL ,
`hits` INT (12) UNSIGNED NOT NULL ,
`user` VARCHAR (200) NOT NULL ,
PRIMARY KEY ( `id` )
)";
$result = $wpdb->query($sql);
$sql = "CREATE TABLE IF NOT EXISTS ".$wp_dlm_db_cats." (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` LONGTEXT NOT NULL ,
`parent` INT (12) UNSIGNED NOT NULL,
PRIMARY KEY ( `id` )
)";
$result = $wpdb->query($sql);
// ADD ROW FOR MEMBER ONLY DOWNLOADS
$sql = "ALTER TABLE ".$wp_dlm_db." ADD `members` INT (1) NULL;";
$result = @$wpdb->query($sql);
// ADD ROW FOR CATEGORY
$sql = "ALTER TABLE ".$wp_dlm_db." ADD `category_id` INT (12) NULL;";
$result = @$wpdb->query($sql);
$q=$wpdb->get_results("select * from $wp_dlm_db;");
if ( empty( $q ) ) {
$wpdb->query("TRUNCATE table $wp_dlm_db");
} else {
// Check for old plugin url's and convert to new path
foreach($q as $download) {
$thisfile = $download->filename;
$newpath = str_replace("/wp-downloadMonitor/","/download-monitor/",$thisfile);
if ($newpath!=$thisfile) {
// update download
$query_update = sprintf("UPDATE %s SET filename='%s' WHERE id=%s;",
$wpdb->escape( $wp_dlm_db ),
$wpdb->escape( $newpath ),
$wpdb->escape( $download->id ));
$d = $wpdb->get_row($query_update);
}
}
// do the same for new dir
foreach($q as $download) {
$thisfile = $download->filename;
$newpath = str_replace("plugins/wp-download_monitor/user_uploads/","uploads/",$thisfile);
$newpath2 = str_replace("plugins/download-monitor/user_uploads/","uploads/",$thisfile);
if ($newpath!=$thisfile) {
// update download
$query_update = sprintf("UPDATE %s SET filename='%s' WHERE id=%s;",
$wpdb->escape( $wp_dlm_db ),
$wpdb->escape( $newpath ),
$wpdb->escape( $download->id ));
$d = $wpdb->get_row($query_update);
}
if ($newpath2!=$thisfile) {
// update download
$query_update = sprintf("UPDATE %s SET filename='%s' WHERE id=%s;",
$wpdb->escape( $wp_dlm_db ),
$wpdb->escape( $newpath2 ),
$wpdb->escape( $download->id ));
$d = $wpdb->get_row($query_update);
}
}
}
return;
}
################################################################################
// MAGIC QUOTES - checks if magic quotes enabled, disables the add_slashes on
// inputs, so ensure add_slashes before interacting with the database
################################################################################
function wp_dlm_magic() {
if (get_magic_quotes_gpc()){
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
}
return;
}
################################################################################
// INSERT BUTTON ON POST SCREEN
################################################################################
function wp_dlm_ins_button() {
//set globals
global $table_prefix,$wpdb,$wp_dlm_db,$wp_dlm_db_cats;
if( strpos($_SERVER['REQUEST_URI'], 'post.php')
|| strstr($_SERVER['PHP_SELF'], 'page-new.php')
|| strstr($_SERVER['PHP_SELF'], 'page.php')
|| strstr($_SERVER['PHP_SELF'], 'post-new.php') )
{
$wp_dlm_db_exists = false;
// Check table exists
$tables = $wpdb->get_results("show tables;");
foreach ( $tables as $table )
{
foreach ( $table as $value )
{
if ( strtolower($value) == strtolower($wp_dlm_db) ) $wp_dlm_db_exists = true;
}
}
if ($wp_dlm_db_exists==true) {
// select all downloads
$query_select = sprintf("SELECT * FROM %s ORDER BY id;",
$wpdb->escape($wp_dlm_db));
$downloads = $wpdb->get_results($query_select);
$js .= '';
if (!empty($downloads)) {
$js .= '';
// select all cats
$query_select_cats = sprintf("SELECT * FROM %s WHERE parent=0 ORDER BY id;",
$wpdb->escape( $wp_dlm_db_cats ));
$cats = $wpdb->get_results($query_select_cats);
if (!empty($cats)) {
$js .= '';
}
?>
get_results("show tables;");
foreach ( $tables as $table )
{
foreach ( $table as $value )
{
if ( strtolower($value) == strtolower($wp_dlm_db) ) $wp_dlm_db_exists = true;
}
}
if ($wp_dlm_db_exists==true) {
//echo "-Table exists-";
$url = get_option('wp_dlm_url');
$downloadurl = get_bloginfo('wpurl').'/'.$url;
if (empty($url)) $downloadurl = $wp_dlm_root.'download.php?id=';
$downloadtype = get_option('wp_dlm_type');
// select all downloads
$query_select = sprintf("SELECT * FROM %s ORDER BY id;",
$wpdb->escape($wp_dlm_db));
$downloads = $wpdb->get_results($query_select);
if (!empty($downloads)) {
//echo "-Downloads found-";
$patts = array();
$subs = array();
foreach($downloads as $d) {
switch ($downloadtype) {
case ("Title") :
$downloadlink = urlencode($d->title);
break;
case ("Filename") :
$downloadlink = $d->filename;
$links = explode("/",$downloadlink);
$downloadlink = end($links);
break;
default :
$downloadlink = $d->id;
break;
}
for ( $i = 1; $i <= 7; $i += 1) {
switch ($i) {
case (1) :
// Regular download link
if (!empty($d->dlversion))
$link = 'dlversion.' '.__("downloaded","wp-download_monitor").' '.$d->hits.' '.__("times","wp-download_monitor").'" >'.$d->title.' ('.$d->hits.')';
else $link = 'hits.' '.__("times","wp-download_monitor").'" >'.$d->title.' ('.$d->hits.')';
$patts[] = "[download#" . $d->id . "]";
$subs[] = $link;
break;
case (2) :
// No hit counter
if (!empty($d->dlversion))
$link = 'dlversion.' '.__("downloaded","wp-download_monitor").' '.$d->hits.' '.__("times","wp-download_monitor").'" >'.$d->title.'';
else $link = 'hits.' '.__("times","wp-download_monitor").'" >'.$d->title.'';
$patts[] = "[download#" . $d->id . "#nohits]";
$subs[] = $link;
break;
case (3) :
// URL only
$link = $downloadurl.$downloadlink;
$patts[] = "[download#" . $d->id . "#url]";
$subs[] = $link;
break;
case (4) :
// Hits only
$link = $d->hits;
$patts[] = "[download#" . $d->id . "#hits]";
$subs[] = $link;
break;
case (5) :
// Image link
if (!empty($d->dlversion))
$link = 'title.' '.__("Version","wp-download_monitor").' '.$d->dlversion.'">title.' '.__("Version","wp-download_monitor").' '.$d->dlversion.'" />
'.__("Downloaded a total of","wp-download_monitor").' '.$d->hits.' '.__("times","wp-download_monitor").'
'.__("Downloaded a total of","wp-download_monitor").' '.$d->hits.' '.__("times","wp-download_monitor").'
';
$patts[] = "[download#" . $d->id . "#image]";
$subs[] = $link;
break;
case (6) :
// Regular download link WITH filesize
//echo "-Link output-";
if (!empty($d->dlversion))
$link = 'dlversion.' '.__("downloaded","wp-download_monitor").' '.$d->hits.' '.__("times","wp-download_monitor").'" >'.$d->title.' ('.$d->hits.') - '.wp_dlm_get_size($d->filename).'';
else $link = 'hits.' '.__("times","wp-download_monitor").'" >'.$d->title.' ('.$d->hits.') - '.wp_dlm_get_size($d->filename).'';
$patts[] = "[download#" . $d->id . "#size]";
$subs[] = $link;
break;
case (7) :
// No hit counter + filesize
if (!empty($d->dlversion))
$link = 'dlversion.' '.__("downloaded","wp-download_monitor").' '.$d->hits.' '.__("times","wp-download_monitor").'" >'.$d->title.' ('.wp_dlm_get_size($d->filename).')';
else $link = 'hits.' '.__("times","wp-download_monitor").'" >'.$d->title.' ('.wp_dlm_get_size($d->filename).')';
$patts[] = "[download#" . $d->id . "#size#nohits]";
$subs[] = $link;
break;
}
}
} return str_replace($patts, $subs, $data);
}else return $data;
} else return $data;
} else return $data;
}
add_filter('the_content', 'wp_dlm_ins',1,1);
add_filter('the_excerpt', 'wp_dlm_ins',1,1);
################################################################################
// CATEGORIES - INSERT LINK INTO POSTS
################################################################################
function wp_dlm_ins_cats($data) {
if (substr_count($data,"[download_cat#")) {
global $table_prefix,$wpdb,$wp_dlm_root,$allowed_extentions,$max_upload_size,$wp_dlm_db,$wp_dlm_db_cats;
$wp_dlm_db_exists = false;
// Check table exists
$tables = $wpdb->get_results("show tables;");
foreach ( $tables as $table )
{
foreach ( $table as $value )
{
if ( strtolower($value) == strtolower($wp_dlm_db) ) $wp_dlm_db_exists = true;
}
}
if ($wp_dlm_db_exists==true) {
$url = get_option('wp_dlm_url');
$downloadurl = get_bloginfo('wpurl').'/'.$url;
if (empty($url)) $downloadurl = $wp_dlm_root.'download.php?id=';
$downloadtype = get_option('wp_dlm_type');
// select all cats
$query_select = sprintf("SELECT * FROM %s ORDER BY id;",
$wpdb->escape($wp_dlm_db_cats));
$cats = $wpdb->get_results($query_select);
if (!empty($cats)) {
$patts = array();
$subs = array();
foreach($cats as $c) {
// Get downloads for cat and put in ul
$links = '
';
// Get list of cats and sub cats
$the_cats = array();
$the_cats[] = $c->id;
$query = sprintf("SELECT id from %s WHERE parent IN (%s);",
$wpdb->escape( $wp_dlm_db_cats ),
$wpdb->escape( implode(",",$the_cats) ));
$res = $wpdb->get_results($query);
$b=sizeof($the_cats);
if ($res) {
foreach($res as $r) {
if (!in_array($r->id,$the_cats)) $the_cats[]=$r->id;
}
}
$a=sizeof($the_cats);
while ($b!=$a) {
$query = sprintf("SELECT id from %s WHERE parent IN (%s);",
$wpdb->escape( $wp_dlm_db_cats ),
$wpdb->escape( implode(",",$the_cats) ));
$res = $wpdb->get_results($query);
$b=sizeof($the_cats);
if ($res) {
foreach($res as $r) {
if (!in_array($r->id,$the_cats)) $the_cats[]=$r->id;
}
}
$a=sizeof($the_cats);
}
$query = sprintf("SELECT * FROM %s WHERE `category_id` IN (%s) ORDER BY `title`;",
$wpdb->escape( $wp_dlm_db ),
$wpdb->escape( implode(",",$the_cats) ));
// Now grab downloads
$downloads = $wpdb->get_results($query);
if (!empty($downloads)) {
foreach($downloads as $d) {
switch ($downloadtype) {
case ("Title") :
$downloadlink = urlencode($d->title);
break;
case ("Filename") :
$downloadlink = $d->filename;
$link = explode("/",$downloadlink);
$downloadlink = end($link);
break;
default :
$downloadlink = $d->id;
break;
}
if (!empty($d->dlversion))
$links.= '
';
$patts[] = "[download_cat#" . $c->id . "]";
$subs[] = $links;
} return str_replace($patts, $subs, $data);
}else return $data;
} else return $data;
} else return $data;
}
add_filter('the_content', 'wp_dlm_ins_cats',1,1);
add_filter('the_excerpt', 'wp_dlm_ins_cats',1,1);
// Formats file size
function wp_dlm_get_size($path) {
$path = str_replace(get_bloginfo('wpurl'),"./",$path);
if (file_exists($path)) {
$size = filesize($path);
if ($size) {
$bytes = array('bytes','KB','MB','GB','TB');
foreach($bytes as $val) {
if($size > 1024){
$size = $size / 1024;
}else{
break;
}
}
return round($size, 2)." ".$val;
}
}
}
// Function used later to output categories
function get_option_children_cats($parent,$chain,$current,$showid=1) {
global $wp_dlm_db_cats,$wpdb;
$sql = sprintf("SELECT * FROM %s WHERE parent=%s ORDER BY id;",
$wpdb->escape( $wp_dlm_db_cats ),
$wpdb->escape( $parent ));
$scats = $wpdb->get_results($sql);
if (!empty($scats)) {
foreach ( $scats as $c ) {
$string.= '';
$string.= get_option_children_cats($c->id, "$chain$c->name — ",$current);
}
}
return $string;
}
################################################################################
// ADMIN PAGE
################################################################################
function wp_dlm_admin()
{
//set globals
global $table_prefix,$wpdb,$wp_dlm_root,$allowed_extentions,$max_upload_size,$wp_dlm_db,$wp_dlm_db_cats;
// turn off magic quotes
wp_dlm_magic();
wp_dlm_update();
// DEFINE QUERIES
// select all downloads
if (empty( $_POST['dlhits'] )) $_POST['dlhits'] = 0;
// select a downloads
$query_select_1 = sprintf("SELECT * FROM %s WHERE id=%s;",
$wpdb->escape( $wp_dlm_db ),
$wpdb->escape( $_GET['id'] ));
$action = $_GET['action'];
if (!empty($action)) {
switch ($action) {
case "add" :
$method = $_REQUEST['method'];
if (!empty($method))
{
//SAVE
if ( $_POST['sub'] ) {
//get postdata
$title = htmlspecialchars(trim($_POST['title']));
$filename = htmlspecialchars(trim($_POST['filename']));
$dlversion = htmlspecialchars(trim($_POST['dlversion']));
$dlhits = htmlspecialchars(trim($_POST['dlhits']));
$postDate = $_POST['postDate'];
$user = $_POST['user'];
$members = (isset($_POST['memberonly'])) ? 1 : 0;
$download_cat = $_POST['download_cat'];
//validate fields
if (empty( $_POST['title'] )) $errors.=__('
Required field: Title omitted
',"wp-download_monitor");
if (empty( $_POST['dlhits'] )) $_POST['dlhits'] = 0;
if (!is_numeric($_POST['dlhits'] )) $errors.=__('
Invalid hits entered
',"wp-download_monitor");
if ($method=="upload") {
//attempt to upload file
if ( empty($errors ) ) {
global $max_upload_size;
$max_size = $max_upload_size; // the max. size for uploading
$my_upload = new wp_dlm_file_upload;
$my_upload->upload_dir = "../wp-content/uploads/"; // the folder for the uploaded files (you may have to create this folder)
$my_upload->extensions = $allowed_extentions; // specify the allowed extensions here
$my_upload->max_length_filename = 100; // change this value to fit your field length in your database (standard 100)
$my_upload->rename_file = false;
//upload it
$my_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$my_upload->the_file = $_FILES['upload']['name'];
$my_upload->http_error = $_FILES['upload']['error'];
$my_upload->replace = (isset($_POST['replace'])) ? $_POST['replace'] : "n";
$my_upload->do_filename_check = "n";
if ($my_upload->upload()) {
$full_path = $my_upload->upload_dir.$my_upload->file_copy;
$info = $my_upload->show_error_string();
}
else $errors = '
';
$members = (isset($_POST['memberonly'])) ? 1 : 0;
if (empty($errors)) {
if (!empty($_FILES['upload']['tmp_name'])) {
//user is replacing the file
global $max_upload_size;
$max_size = $max_upload_size; // the max. size for uploading
$my_upload = new wp_dlm_file_upload;
$my_upload->upload_dir = "../wp-content/uploads/"; // "files" is the folder for the uploaded files (you have to create this folder)
$my_upload->extensions = $allowed_extentions; // specify the allowed extensions here
$my_upload->max_length_filename = 100; // change this value to fit your field length in your database (standard 100)
$my_upload->rename_file = false;
//upload it
$my_upload->the_temp_file = $_FILES['upload']['tmp_name'];
$my_upload->the_file = $_FILES['upload']['name'];
$my_upload->http_error = $_FILES['upload']['error'];
$my_upload->replace = "y";
$my_upload->do_filename_check = "n";
if ($my_upload->upload()) {
$full_path = $my_upload->upload_dir.$my_upload->file_copy;
$info = $my_upload->show_error_string();
}
else $errors.= '
';
// Truncate table if empty
global $wp_dlm_db;
$q=$wpdb->get_results("select * from $wp_dlm_db;");
if ( empty( $q ) ) {
$wpdb->query("TRUNCATE table $wp_dlm_db");
}
$show=true;
break;
case "cancelled" :
$show=true;
break;
case "saveurl" :
$url = $_POST['url'];
update_option('wp_dlm_url', trim($url));
update_option('wp_dlm_type', $_POST['type']);
if (!empty($url)) {
echo '
';
_e('
Download URL updated - You need to re-save your permalinks settings (Options/settings -> Permalinks) for
the changes to occur in your blog.
If your .htaccess file cannot be written to by WordPress, add the following to your
.htaccess file above the "# BEGIN WordPress" line:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^download/(.*) wp-content/plugins/download-monitor/download.php?id=$1 [L]
replacing "download/" with your custom url.
',"wp-download_monitor");
echo '
';
} else {
echo '
';
_e('
Download URL updated - You need to re-save your permalinks settings (Options/settings -> Permalinks) for
the changes to occur in your blog.
If your .htaccess file cannot be written to by WordPress, remove the following from your
.htaccess file if it exists above the "# BEGIN WordPress" line:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^download/(.*) wp-content/plugins/download-monitor/download.php?id=$1 [L]
replacing "download/" with your previous custom url.
escape($wp_dlm_db));
// Figure out the limit for the query based on the current page number.
$from = (($page * 10) - 10);
$paged_select = sprintf("SELECT * FROM %s ORDER BY %s LIMIT %s,10;",
$wpdb->escape( $wp_dlm_db ),
$wpdb->escape( $sort ),
$wpdb->escape( $from ));
$download = $wpdb->get_results($paged_select);
$total = $wpdb->get_var($total_results);
// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total / 10);
if (!empty($download)) {
echo '';
foreach ( $download as $d ) {
$date = date("jS M Y", strtotime($d->postDate));
$path = get_bloginfo('wpurl')."/wp-content/uploads/";
$file = str_replace($path, "", $d->filename);
$links = explode("/",$file);
$file = end($links);
echo ('
');
echo '
'.$d->id.'
'.$d->title.'
'.$file.'
';
if ($d->category_id=="" || $d->category_id==0) echo "N/A"; else {
$c = $wpdb->get_row("SELECT * FROM $wp_dlm_db_cats where id=".$d->category_id." LIMIT 1;");
$chain = $c->name;
while ($c->parent>0) {
$c = $wpdb->get_row("SELECT * FROM $wp_dlm_db_cats where id=".$c->parent." LIMIT 1;");
$chain = $c->name.' — '.$chain;
}
echo $d->category_id." - ".$chain;
}
echo '
'.$d->dlversion.'
';
if ($d->members) echo __('Yes',"wp-download_monitor"); else echo __('No',"wp-download_monitor");
echo '
You can categorise downloads using these categories. You can then show groups of downloads using the category tags or a dedicated download page (see instructions box below). Please note, deleting a category also deletes it\'s child categories.',"wp-download_monitor"); ?>
You can set the allowed uploaded file extensions here by entering a list of extensions separated by commas, e.g. .zip,.rar',"wp-download_monitor"); ?>
Set the url of the downloads, e.g. download/. In this example a download link would look like this:
http://yoursite.com/download/2.
Leave this option blank to use the default download path (wp-content/plugins/download-monitor/download.php?id=)
You can also choose how to link to the download in it\'s url, e.g. selecting "filename" would make the link appear as http://yoursite.com/download/filename.zip.
',"wp-download_monitor"); ?>
Add a download by clicking the \'Add new Download\' or \'Add Existing Download\' button. You will then be shown the \'Add Download\' form. On this form:
If using the \'add new download\' form, choose to upload your file from your hard disk by using the \'Select a file\' input
or if using the \'add existing download\' form enter the url of an already existing file in the \'URL\' input (e.g http://www.blue-anvil.com/download.zip).
Add a Title for your download, and optionally a version. This name will be used when displaying the download link.
',"wp-download_monitor"); ?>
This plugin has special tags for use in Wordpress posts to display download links. If using the rich text editor you must enter these
manually, however, if using the normal plain editor there is a dropdown menu to add them quicker. Replace \'id\' with the downloads id.
',"wp-download_monitor"); ?>
There are a few other template tags to use in your wordpress templates. Replace \'$no\' with the amount of downloads to show.
Most downloaded - <?php wp_dlm_show_downloads(1,$no); ?>
Most recent - <?php wp_dlm_show_downloads(2,$no); ?>
Random - <?php wp_dlm_show_downloads(3,$no); ?>
',"wp-download_monitor"); ?>
Simply add the tag [#show_downloads] to a page.',"wp-download_monitor"); ?>
Add the tag [#advanced_downloads] to a page.',"wp-download_monitor"); ?>
Use [download_cat#id] replacing id with the id of the category.',"wp-download_monitor"); ?>
The Wordpress Download monitor plugin was created by Mike Jolley. The development
of this plugin took a lot of time and effort, therefore donations are very welcome, and encouraged
(I have living costs you know) :)',"wp-download_monitor"); ?>
escape( $wp_dlm_db ),
$wpdb->escape( $no ));
break;
case (2) :
$query = sprintf("SELECT * FROM %s ORDER BY postDate LIMIT %s;",
$wpdb->escape( $wp_dlm_db ),
$wpdb->escape( $no ));
break;
case (3) :
$query = sprintf("SELECT * FROM %s ORDER BY rand() LIMIT %s;",
$wpdb->escape( $wp_dlm_db ),
$wpdb->escape( $no ));
break;
}
if (!empty($query)) {
$url = get_option('wp_dlm_url');
$downloadurl = get_bloginfo('wpurl').'/'.$url;
if (empty($url)) $downloadurl = $wp_dlm_root.'download.php?id=';
$dl = $wpdb->get_results($query);
$downloadtype = get_option('wp_dlm_type');
if (!empty($dl)) {
echo '
';
foreach($dl as $d) {
$date = date("jS M Y", strtotime($d->postDate)); // FIXED: 1.6 - Capital D modded
switch ($downloadtype) {
case ("Title") :
$downloadlink = $d->title;
break;
case ("Filename") :
$downloadlink = $d->filename;
$links = explode("/",$downloadlink);
$downloadlink = end($links);
break;
default :
$downloadlink = $d->id;
break;
}
switch ($mode) {
case (1) :
echo '
';
}
}
return $retval;
}
// Shows Top downloads by default
// Dropdown to select a category of downloads or view all
function wp_dlm_advanced() {
global $table_prefix,$wpdb,$wp_dlm_root,$allowed_extentions,$max_upload_size,$wp_dlm_db,$wp_dlm_db_cats;
// Get post data
$showing = (int) $_POST['show_downloads'];
if ($showing==0 || $showing=="") {
// Most popular by default
$query = sprintf("SELECT * FROM %s ORDER BY hits DESC LIMIT 10;",
$wpdb->escape( $wp_dlm_db ));
} else {
// Get list of cats and sub cats
$the_cats = array();
$the_cats[] = $showing;
$query = sprintf("SELECT id from %s WHERE parent IN (%s);",
$wpdb->escape( $wp_dlm_db_cats ),
$wpdb->escape( implode(",",$the_cats) ));
$res = $wpdb->get_results($query);
$b=sizeof($the_cats);
if ($res) {
foreach($res as $r) {
if (!in_array($r->id,$the_cats)) $the_cats[]=$r->id;
}
}
$a=sizeof($the_cats);
while ($b!=$a) {
$query = sprintf("SELECT id from %s WHERE parent IN (%s);",
$wpdb->escape( $wp_dlm_db_cats ),
$wpdb->escape( implode(",",$the_cats) ));
$res = $wpdb->get_results($query);
$b=sizeof($the_cats);
if ($res) {
foreach($res as $r) {
if (!in_array($r->id,$the_cats)) $the_cats[]=$r->id;
}
}
$a=sizeof($the_cats);
}
$query = sprintf("SELECT * FROM %s WHERE `category_id` IN (%s) ORDER BY `title`;",
$wpdb->escape( $wp_dlm_db ),
$wpdb->escape( implode(",",$the_cats) ));
}
// Output selector box
$retval = '
';
if (!empty($query)) {
$url = get_option('wp_dlm_url');
$downloadurl = get_bloginfo('wpurl').'/'.$url;
if (empty($url)) $downloadurl = $wp_dlm_root.'download.php?id=';
$dl = $wpdb->get_results($query);
$downloadtype = get_option('wp_dlm_type');
if (!empty($dl)) {
$retval .= '
";
return $retval;
}
################################################################################
// SHOW ALL DOWNLOADS TAG
################################################################################
function wp_dlm_ins_all($data) {
if (substr_count($data,"[#show_downloads]")) {
$data = str_replace("[#show_downloads]",wp_dlm_all(), $data);
}
if (substr_count($data,"[#advanced_downloads]")) {
$data = str_replace("[#advanced_downloads]",wp_dlm_advanced(), $data);
}
return $data;
}
add_filter('the_content', 'wp_dlm_ins_all',1,1);
/*
Easy PHP Upload - version 2.31
A easy to use class for your (multiple) file uploads
Copyright (c) 2004 - 2006, Olaf Lederer
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the finalwebsites.com nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
______________________________________________________________________
available at http://www.finalwebsites.com/snippets.php
Comments & suggestions: http://www.webdigity.com/index.php/board,73.0.html,ref.olaf
*/
class wp_dlm_file_upload {
var $the_file;
var $the_temp_file;
var $upload_dir;
var $replace;
var $do_filename_check;
var $max_length_filename = 100;
var $extensions;
var $ext_string;
var $http_error;
var $rename_file; // if this var is true the file copy get a new name
var $file_copy; // the new name
var $message = array();
var $create_directory = true;
function wp_dlm_file_upload() {
$this->rename_file = false;
$this->ext_string = "";
}
function show_error_string() {
$msg_string = "";
foreach ($this->message as $value) {
$msg_string .= $value." \n";
}
return $msg_string;
}
function set_file_name($new_name = "") { // this "conversion" is used for unique/new filenames
if ($this->rename_file) {
if ($this->the_file == "") return;
$name = ($new_name == "") ? strtotime("now") : $new_name;
sleep(3);
$name = $name.$this->get_extension($this->the_file);
} else {
$name = str_replace(" ", "_", $this->the_file); // space will result in problems on linux systems
}
return $name;
}
function upload($to_name = "") {
$new_name = $this->set_file_name($to_name);
if ($this->check_file_name($new_name)) {
if ($this->validateExtension()) {
if (is_uploaded_file($this->the_temp_file)) {
$this->file_copy = $new_name;
if ($this->move_upload($this->the_temp_file, $this->file_copy)) {
$this->message[] = $this->error_text($this->http_error);
if ($this->rename_file) $this->message[] = $this->error_text(16);
return true;
}
} else {
$this->message[] = $this->error_text($this->http_error);
return false;
}
} else {
$this->show_extensions();
$this->message[] = $this->error_text(11);
return false;
}
} else {
return false;
}
}
function check_file_name($the_name) {
if ($the_name != "") {
if (strlen($the_name) > $this->max_length_filename) {
$this->message[] = $this->error_text(13);
return false;
} else {
if ($this->do_filename_check == "y") {
if (preg_match("/^[a-z0-9_]*\.(.){1,5}$/i", $the_name)) {
return true;
} else {
$this->message[] = $this->error_text(12);
return false;
}
} else {
return true;
}
}
} else {
$this->message[] = $this->error_text(10);
return false;
}
}
function get_extension($from_file) {
$ext = strtolower(strrchr($from_file,"."));
return $ext;
}
function validateExtension() {
$extension = $this->get_extension($this->the_file);
$ext_array = $this->extensions;
if (in_array($extension, $ext_array)) {
// check mime type hier too against allowed/restricted mime types (boolean check mimetype)
return true;
} else {
return false;
}
}
// this method is only used for detailed error reporting
function show_extensions() {
$this->ext_string = implode(" ", $this->extensions);
}
function move_upload($tmp_file, $new_file) {
if ($this->existing_file($new_file)) {
$newfile = $this->upload_dir.$new_file;
if ($this->check_dir($this->upload_dir)) {
if (move_uploaded_file($tmp_file, $newfile)) {
umask(0);
chmod($newfile , 0644);
return true;
} else {
return false;
}
} else {
$this->message[] = $this->error_text(14);
return false;
}
} else {
$this->message[] = $this->error_text(15);
return false;
}
}
function check_dir($directory) {
if (!is_dir($directory)) {
if ($this->create_directory) {
umask(0);
mkdir($directory, 0777);
return true;
} else {
return false;
}
} else {
return true;
}
}
function existing_file($file_name) {
if ($this->replace == "y") {
return true;
} else {
if (file_exists($this->upload_dir.$file_name)) {
return false;
} else {
return true;
}
}
}
function get_uploaded_file_info($name) {
$str = "File name: ".basename($name)."\n";
$str .= "File size: ".filesize($name)." bytes\n";
if (function_exists("mime_content_type")) {
$str .= "Mime type: ".mime_content_type($name)."\n";
}
if ($img_dim = getimagesize($name)) {
$str .= "Image dimensions: x = ".$img_dim[0]."px, y = ".$img_dim[1]."px\n";
}
return $str;
}
// this method was first located inside the foto_upload extension
function del_temp_file($file) {
$delete = @unlink($file);
clearstatcache();
if (@file_exists($file)) {
$filesys = eregi_replace("/","\\",$file);
$delete = @system("del $filesys");
clearstatcache();
if (@file_exists($file)) {
$delete = @chmod ($file, 0644);
$delete = @unlink($file);
$delete = @system("del $filesys");
}
}
}
// this function creates a file field and if $show_alternate is true it will show a text field if the given file already exists
// there is also a submit button to remove the text field value
function create_file_field($element, $label = "", $length = 25, $show_replace = true, $replace_label = "Replace old file?", $file_path = "", $file_name = "", $show_alternate = false, $alt_length = 30, $alt_btn_label = "Delete image") {
$field = ($label != "") ? "\n" : "";
$file_field = "\n";
$file_field .= ($show_replace) ? "".$replace_label."" : "";
if ($file_name != "" && $show_alternate) {
$field .= "error_text(17), $file_name)."\" />\n" : " />\n";
$field .= "".$alt_btn_label."\n";
} else {
$field .= $file_field;
}
return $field;
}
// some error (HTTP)reporting, change the messages or remove options if you like.
function error_text($err_num) {
// start http errors
$error[0] = __('File',"wp-download_monitor").": ".$this->the_file." ".__('successfully uploaded!',"wp-download_monitor");
$error[1] = __("The uploaded file exceeds the max. upload filesize directive in the server configuration.","wp-download_monitor");
$error[2] = __("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form.","wp-download_monitor");
$error[3] = __("The uploaded file was only partially uploaded","wp-download_monitor");
$error[4] = __("No file was uploaded","wp-download_monitor");
// end http errors
$error[10] = __("Please select a file for upload.","wp-download_monitor");
$error[11] = __("Only files with the following extensions are allowed:","wp-download_monitor")." ".$this->ext_string."";
$error[12] = __("Sorry, the filename contains invalid characters. Use only alphanumerical chars and separate parts of the name (if needed) with an underscore. A valid filename ends with one dot followed by the extension.","wp-download_monitor");
$error[13] = __("The filename exceeds the maximum length of ","wp-download_monitor").$this->max_length_filename.__("characters.","wp-download_monitor");
$error[14] = __("Sorry, the upload directory doesn't exist!","wp-download_monitor");
$error[15] = __("Uploading","wp-download_monitor")." ".$this->the_file."...Error! ".__("Sorry, a file with this name already exitst.","wp-download_monitor");
$error[16] = __("The uploaded file is renamed to","wp-download_monitor")." ".$this->file_copy."";
$error[17] = __("The file %s does not exist.","wp-download_monitor");
return $error[$err_num];
}
}
################################################################################
// Dashboard widget - Based on "Dashboard: Draft Posts" by http://www.viper007bond.com/
################################################################################
// Only for wordpress 2.5 and above!
if ($wp_db_version > 6124) {
class wp_dlm_dash {
// Class initialization
function wp_dlm_dash() {
// Add to dashboard
add_action( 'wp_dashboard_setup', array(&$this, 'register_widget') );
add_filter( 'wp_dashboard_widgets', array(&$this, 'add_widget') );
}
// Register the widget for dashboard use
function register_widget() {
wp_register_sidebar_widget( 'download_monitor_dash', __( 'Downloads', 'wp-download_monitor' ), array(&$this, 'widget'), array( 'all_link' => 'edit.php?page=Downloads' ) );
}
// Insert into dashboard
function add_widget( $widgets ) {
global $wp_registered_widgets;
if ( !isset($wp_registered_widgets['download_monitor_dash']) ) return $widgets;
array_splice( $widgets, 2, 0, 'download_monitor_dash' );
return $widgets;
}
// Output the widget
function widget( $args ) {
extract( $args, EXTR_SKIP );
echo $before_widget;
echo $before_title;
echo $widget_name;
echo $after_title;
global $wp_dlm_db,$wpdb;
echo "
".__('Most Recent',"wp-download_monitor")."
";
$query = sprintf("SELECT * FROM %s ORDER BY postDate DESC LIMIT 3;",
$wpdb->escape( $wp_dlm_db ));
if (!empty($query)) {
$dl = $wpdb->get_results($query);
echo '
';
if (!empty($dl)){
foreach($dl as $d) {
$date = date("jS M Y", strtotime($d->postDate));
echo '