Downloads". If you are upgrading Download Monitor it is a good idea to back-up your database just in case.
Version: 2.2.2
Author: Mike Jolley
Author URI: http://blue-anvil.com
*/
/* 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
*/
################################################################################
// Vars and version
################################################################################
$dlm_build="B20090107";
$wp_dlm_root = get_bloginfo('wpurl')."/wp-content/plugins/download-monitor/";
add_option('max_upload_size','10485760','no'); //10mb
$max_upload_size = get_option('max_upload_size');
global $table_prefix;
$wp_dlm_db = $table_prefix."DLM_DOWNLOADS";
$wp_dlm_db_cats = $table_prefix."DLM_CATS";
$wp_dlm_db_formats = $table_prefix."DLM_FORMATS";
// Get extensions
$allowed_e = get_option('wp_dlm_extensions');
if (empty( $allowed_e )) {
wp_dlm_init();
$allowed_e = get_option('wp_dlm_extensions');
}
$allowed_extentions = explode(",",$allowed_e);
include_once('classes/upload.class.php');
load_plugin_textdomain('wp-download_monitor', 'wp-content/plugins/download-monitor/');
################################################################################
// ADD MEDIA BUTTONS AND FORMS
################################################################################
function wp_dlm_add_media_button() {
echo '';
}
add_action('media_buttons', 'wp_dlm_add_media_button', 20);
################################################################################
// 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_reinstall();
// 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.
if ($wp_db_version <= 6124) {
// Version 2.3.3 and below
echo '';
//wp_enqueue_script('jquery');
// 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 '';
}
} elseif ($wp_db_version > 6124 && $wp_db_version < 9872) {
// 2.5 + 2.6 with new interface
echo '';
} else {
// 2.7
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_type', 'ID', 'wp_dlm_type', 'no');
add_option('wp_dlm_default_format', '0', 'wp_dlm_default_format', 'no');
add_option('wp_dlm_does_not_exist','','no');
add_option('wp_dlm_image_url',get_bloginfo('wpurl')."/wp-content/plugins/download-monitor/img/download.gif",'no');
add_option('wp_dlm_extensions', '.zip,.pdf,.mp3,.rar', '', 'no');
global $wp_dlm_db,$wp_dlm_db_cats,$wp_dlm_db_formats,$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 ,
`file_description` LONGTEXT NULL ,
`dlversion` VARCHAR (200) NOT NULL ,
`postDate` DATETIME NOT NULL ,
`hits` INT (12) UNSIGNED NOT NULL ,
`user` VARCHAR (200) NOT NULL ,
`category_id` INT (12) NULL,
`members` INT (1) NULL,
`mirrors` LONGTEXT 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);
$sql = "CREATE TABLE IF NOT EXISTS ".$wp_dlm_db_formats." (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR (250) NOT NULL ,
`format` LONGTEXT NOT NULL,
PRIMARY KEY ( `id` )
)";
$result = $wpdb->query($sql);
$q = $wpdb->get_results("select * from $wp_dlm_db;");
if ( empty( $q ) ) {
$wpdb->query("TRUNCATE table $wp_dlm_db");
}
return;
}
function wp_dlm_reinstall() {
global $wpdb, $wp_dlm_db, $wp_dlm_db_cats;
// GET OLD DATA
$wpdb->show_errors;
$query = sprintf("SELECT * from %s;",
$wpdb->escape( $wp_dlm_db ));
$result_d = $wpdb->get_results($query);
if($result_d && $wpdb->num_rows>0) {
$values="";
foreach($result_d as $d) {
$id=$d->id;
$title=$d->title;
$filename=$d->filename;
$dlversion=$d->dlversion;
$postDate=$d->postDate;
$hits=$d->hits;
$user=$d->user;
$members=$d->members;
$category_id=$d->category_id;
$mirrors=$d->mirrors;
$file_description=$d->file_description;
$values.='("'.$id.'","'.$title.'","'.$filename.'","'.$dlversion.'","'.$postDate.'","'.$hits.'","'.$user.'","'.$members.'","'.$category_id.'","'.$mirrors.'","'.$file_description.'"),';
}
$values = substr_replace($values,"",-1);
}
$query = sprintf("SELECT * from %s;",
$wpdb->escape( $wp_dlm_db_cats ));
$result_cats = $wpdb->get_results($query);
if($result_cats && $wpdb->num_rows>0) {
$values2="";
foreach($result_cats as $d) {
$id=$d->id;
$name=$d->name;
$parent=$d->parent;
$values2.='("'.$id.'","'.$name.'","'.$parent.'"),';
}
$values2 = substr_replace($values2,"",-1);
}
// DROP TABLES
$sql = 'DROP TABLE IF EXISTS `'.$wp_dlm_db.'`';
$wpdb->query($sql);
$sql = 'DROP TABLE IF EXISTS `'.$wp_dlm_db_cats.'`';
$wpdb->query($sql);
wp_dlm_init();
// ADD OLD DATA
if (!empty($values)) {
$query_ins = sprintf("INSERT INTO %s (id, title, filename, dlversion, postDate, hits, user, members, category_id, mirrors, file_description) VALUES %s;",
$wpdb->escape( $wp_dlm_db ),
$values);
$wpdb->query($query_ins);
}
if (!empty($values2)) {
$query_ins = sprintf("INSERT INTO %s (id, name, parent) VALUES %s;",
$wpdb->escape( $wp_dlm_db_cats ),
$values2);
$wpdb->query($query_ins);
}
}
################################################################################
// 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() {
function stripit($in) {
if (!is_array($in)) $out = stripslashes($in); else $out = $in;
return $out;
}
if (get_magic_quotes_gpc()){
$_GET = array_map('stripit', $_GET);
$_POST = array_map('stripit', $_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 .= '';
}
?>
escape($wp_dlm_db_formats),
$wpdb->escape($id));
$format = $wpdb->get_row($query_select);
return $format->format;
}
################################################################################
// INSERT LINK INTO POSTS
################################################################################
function wp_dlm_parse_downloads($data) {
if (substr_count($data,"[download#")) {
global $wpdb,$wp_dlm_root,$allowed_extentions,$max_upload_size,$wp_dlm_db,$wp_dlm_db_formats,$wp_dlm_db_cats;
$wp_dlm_db_exists = false;
$def_format = get_option('wp_dlm_default_format');
// 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) {
//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');
// Handle Custom Formats (format=...)
if (substr_count($data,"#format=")) {
preg_match_all("/\[download#([0-9]+)#format=([0-9]+)\]/", $data, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
// Get format
$format = wp_dlm_get_custom_format($val[2]);
if ($format) {
// Get download info + insert
$query_select = sprintf("SELECT * FROM %s WHERE id = '%s';",
$wpdb->escape($wp_dlm_db),
$wpdb->escape($val[1]));
$d = $wpdb->get_row($query_select);
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;
}
$fpatts = array();
$fsubs = array();
$fpatts[] = '{url}';
$fsubs[] = $downloadurl.$downloadlink;
$fpatts[] = '{version}';
$fsubs[] = $d->dlversion;
$fpatts[] = '{title}';
$fsubs[] = $d->title;
$fpatts[] = '{size}';
$fsubs[] = wp_dlm_get_size($d->filename);
$fpatts[] = '{hits}';
$fsubs[] = $d->hits;
$fpatts[] = '{image_url}';
$fsubs[] = get_option('wp_dlm_image_url');
if ($d->category_id>0) {
$fpatts[] = '{category}';
$c = $wpdb->get_row("SELECT name FROM $wp_dlm_db_cats where id=".$d->category_id." LIMIT 1;");
$fsubs[] = $c->name;
preg_match("/{category,([^,{}]*),([^,{}]*)}/", $format, $match);
$fpatts[] = $match[0];
$fsubs[] = $match[1].$c->name.$match[2];
} else {
$fpatts[] = '{category}';
$fsubs[] = "";
preg_match("/{category,([^,{}]*),([^,{}]*)}/", $format, $match);
$fpatts[] = $match[0];
$fsubs[] = "";
}
$fpatts[] = '{description}';
$fsubs[] = $d->file_description;
$fpatts[] = '{description-autop}';
$fsubs[] = wpautop($d->file_description);
preg_match("/{description,([^,{}]*),([^,{}]*)}/", $format, $match);
$fpatts[] = $match[0];
if ($d->file_description) $fsubs[] = $match[1].$d->file_description.$match[2]; else $fsubs[] = "";
preg_match("/{description-autop,([^,{}]*),([^,{}]*)}/", $format, $match);
$fpatts[] = $match[0];
if ($d->file_description) $fsubs[] = $match[1].wpautop($d->file_description).$match[2]; else $fsubs[] = "";
$code = str_replace($fpatts, $fsubs, $format);
$data = str_replace($val[0],$code,$data);
}
}
}
// 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) {
// If download is present in post
if ( strstr($data, "[download#".$d->id ) ) {
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;
}
################################################################################
// Define Patterns
################################################################################
// Regular download link - NOW USES DEFAULT FORMAT
if ($def_format==0) {
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.')';
} else {
// Get Custom formatted version
$format = wp_dlm_get_custom_format($def_format);
$fpatts = array();
$fsubs = array();
$fpatts[] = '{url}';
$fsubs[] = $downloadurl.$downloadlink;
$fpatts[] = '{version}';
$fsubs[] = $d->dlversion;
$fpatts[] = '{title}';
$fsubs[] = $d->title;
$fpatts[] = '{size}';
$fsubs[] = wp_dlm_get_size($d->filename);
$fpatts[] = '{hits}';
$fsubs[] = $d->hits;
$fpatts[] = '{image_url}';
$fsubs[] = get_option('wp_dlm_image_url');
if ($d->category_id>0) {
$fpatts[] = '{category}';
$c = $wpdb->get_row("SELECT name FROM $wp_dlm_db_cats where id=".$d->category_id." LIMIT 1;");
$fsubs[] = $c->name;
preg_match("/{category,([^,{}]*),([^,{}]*)}/", $format, $match);
$fpatts[] = $match[0];
$fsubs[] = $match[1].$c->name.$match[2];
} else {
$fpatts[] = '{category}';
$fsubs[] = "";
preg_match("/{category,([^,{}]*),([^,{}]*)}/", $format, $match);
$fpatts[] = $match[0];
$fsubs[] = "";
}
$fpatts[] = '{description}';
$fsubs[] = $d->file_description;
$fpatts[] = '{description-autop}';
$fsubs[] = wpautop($d->file_description);
preg_match("/{description,([^,{}]*),([^,{}]*)}/", $format, $match);
$fpatts[] = $match[0];
if ($d->file_description) $fsubs[] = $match[1].$d->file_description.$match[2]; else $fsubs[] = "";
preg_match("/{description-autop,([^,{}]*),([^,{}]*)}/", $format, $match);
$fpatts[] = $match[0];
if ($d->file_description) $fsubs[] = $match[1].wpautop($d->file_description).$match[2]; else $fsubs[] = "";
$link = str_replace($fpatts, $fsubs, $format);
}
$patts[] = "[download#" . $d->id . "]";
$subs[] = $link;
// 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;
// URL only
$link = $downloadurl.$downloadlink;
$patts[] = "[download#" . $d->id . "#url]";
$subs[] = $link;
// Description only
$link = $d->file_description;
$patts[] = "[download#" . $d->id . "#description]";
$subs[] = $link;
// Description (autop) only
$link = wpautop($d->file_description);
$patts[] = "[download#" . $d->id . "#description_autop]";
$subs[] = $link;
// Hits only
$link = $d->hits;
$patts[] = "[download#" . $d->id . "#hits]";
$subs[] = $link;
// 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;
// Regular download link WITH filesize
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;
// 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;
} // DOwnload present
} return str_replace($patts, $subs, $data);
} else return $data;
} else return $data;
} else return $data;
}
add_filter('the_content', 'wp_dlm_parse_downloads',1,1);
add_filter('the_excerpt', 'wp_dlm_parse_downloads',1,1);
add_filter('the_meta_key', 'wp_dlm_parse_downloads',1,1);
add_filter('widget_text', 'wp_dlm_parse_downloads',1,1);
add_filter('widget_title', 'wp_dlm_parse_downloads',1,1);
################################################################################
// CATEGORIES - INSERT LINK INTO POSTS
################################################################################
function wp_dlm_parse_downloads_cats($data) {
if (substr_count($data,"[download_cat#")) {
global $wpdb,$wp_dlm_root,$allowed_extentions,$max_upload_size,$wp_dlm_db,$wp_dlm_db_cats;
// Get cats and dig for sub cats
function get_sub_cats($the_cats) {
global $wpdb, $wp_dlm_db_cats;
$a=sizeof($the_cats);
$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);
if ($res) {
foreach($res as $r) {
if (!in_array($r->id, $the_cats)) $the_cats[]=$r->id;
}
}
$b=sizeof($the_cats);
if ($a!=$b) {
$sub_cats = get_sub_cats($the_cats);
$the_cats = $the_cats + $sub_cats;
}
return $the_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 = "SELECT * FROM ".$wpdb->escape($wp_dlm_db_cats)." ORDER BY id;";
$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 IF WE FIND IT IN THE DATA
if ( strstr($data, "[download_cat#".$c->id."]" ) ) {
// GENERATE LIST
$links = '
';
// Get list of cats and sub cats
$the_cats = array();
$the_cats[] = $c->id;
// Run it beatch
$the_cats = get_sub_cats($the_cats);
// We can query the downloads now
$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_parse_downloads_cats',1,1);
add_filter('the_excerpt', 'wp_dlm_parse_downloads_cats',1,1);
add_filter('the_meta_key', 'wp_dlm_parse_downloads_cats',1,1);
add_filter('widget_text', 'wp_dlm_parse_downloads_cats',1,1);
add_filter('widget_title', 'wp_dlm_parse_downloads_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 $wpdb,$wp_dlm_root,$allowed_extentions,$max_upload_size,$wp_dlm_db,$wp_dlm_db_cats,$wp_dlm_db_formats;
// 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'];
$mirrors = htmlspecialchars(trim($_POST['mirrors']));
$file_description = trim($_POST['file_description']);
//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 '
You can categorise downloads using these categories. You can then show groups of downloads using the category tags or a dedicated download page (see documentation). Please note, deleting a category also deletes it\'s child categories.',"wp-download_monitor"); ?>
This allows you to define formats in which to output your downloads however you want.',"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"); ?>
This will delete the old download monitor tables and recreate them. You should only do this as a last resort if experiencing database errors after updating the plugin. Download monitor will attempt to re-add any downloads currently in the database.',"wp-download_monitor"); ?>
WARNING: THIS MAY DELETE DOWNLOAD DATA IN THE DATABASE; BACKUP YOUR DATABASE FIRST!',"wp-download_monitor"); ?>
Need help? FAQ, Usage instructions and other notes can be found on the plugin page here.',"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, sp please don\'t forget to donate if you found this plugin useful to ensure continued development.',"wp-download_monitor"); ?>
escape( $wp_dlm_db ),
$wpdb->escape( $no ));
break;
case (2) :
$query = sprintf("SELECT * FROM %s ORDER BY postDate DESC 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($format = 0) {
global $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 .= '