additional_plugins/serendipity_event_downloadmanager/serendipity_event_downloadmanager.php

2697 lines
116 KiB
PHP
Raw Permalink Normal View History

<?php
2011-12-13 12:29:05 +01:00
if (IN_serendipity !== true) {
die ("Don't hack!");
}
// Load possible language files.
@serendipity_plugin_api::load_language(dirname(__FILE__));
2011-12-13 12:29:05 +01:00
// Set global plugin path setting, to avoid different pluginpath '/plugins/' as plugins serendipity vars
if (!isset($serendipity['dlm']['pluginpath'])) {
2011-12-13 12:29:05 +01:00
$pluginpath = pathinfo(dirname(__FILE__));
$serendipity['dlm']['pluginpath'] = basename(rtrim($pluginpath['dirname'], '/')) . '/serendipity_event_downloadmanager/';
}
class serendipity_event_downloadmanager extends serendipity_event
{
2011-12-13 12:29:05 +01:00
var $debug;
var $MIME;
var $globs = array();
var $isWIN = null;
2011-12-13 12:29:05 +01:00
/**
* introspect API
*/
function introspect(&$propbag)
{
global $serendipity;
2011-12-13 12:29:05 +01:00
$this->pluginglobs();
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_TITLE);
$propbag->add('description', PLUGIN_DOWNLOADMANAGER_DESC);
$propbag->add('requirements', array(
'serendipity' => '1.6',
2011-12-13 12:29:05 +01:00
'smarty' => '2.6.7',
'php' => '5.3.0'
2011-12-13 12:29:05 +01:00
));
$propbag->add('version', '0.38');
$propbag->add('author', 'Alexander \'dma147\' Mieland, Grischa Brockhaus, Ian');
2011-12-13 12:29:05 +01:00
$propbag->add('stackable', false);
$propbag->add('event_hooks', array(
'entries_header' => true,
'entry_display' => true,
'genpage' => true,
2011-12-13 12:29:05 +01:00
'external_plugin' => true,
'css' => true,
'css_backend' => true,
'backend_sidebar_entries' => true,
'backend_sidebar_admin_appearance' => true,
2011-12-13 12:29:05 +01:00
'backend_sidebar_entries_event_display_downloadmanager' => true
)
);
$propbag->add('configuration', array(
'pagetitle',
'headline',
'intro',
'pageurl',
'permalink',
'separator',
2011-12-13 12:29:05 +01:00
'absincomingpath',
'absdownloadspath',
'httppath',
'add_existing_file',
'iconwidth',
'iconheight',
'dateformat',
'showhidden_registered',
'registered_only',
'showfilename',
'showdownloads',
'showfilesize',
'showdate',
'showdesc_inlist',
'directdl_inlist',
'filename_field',
'filesize_field',
'filedate_field',
'dls_field'
)
);
$propbag->add('groups', array('FRONTEND_FULL_MODS'));
$this->dependencies = array('serendipity_event_entryproperties' => 'keep');
}
/**
* introspect_config_item API
*/
2011-12-13 12:29:05 +01:00
function introspect_config_item($name, &$propbag)
{
global $serendipity;
switch($name) {
case 'pagetitle' :
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_PAGETITLE);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_PAGETITLE_BLAHBLAH);
$propbag->add('default', 'My downloads');
2011-12-13 12:29:05 +01:00
break;
case 'headline' :
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_HEADLINE);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_HEADLINE_BLAHBLAH);
$propbag->add('default', 'Here you can find some useful downloads');
2011-12-13 12:29:05 +01:00
break;
case 'intro':
$propbag->add('type', ($serendipity['wysiwyg'] === true ? 'html' : 'text'));
$propbag->add('rows', 3);
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_INTRO);
$propbag->add('description','');
$propbag->add('default', '');
2011-12-13 12:29:05 +01:00
break;
case 'pageurl' :
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_PAGEURL);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_PAGEURL_BLAHBLAH);
$propbag->add('default', 'downloadmanager');
2011-12-13 12:29:05 +01:00
break;
case 'permalink':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_PERMALINK);
$propbag->add('description',sprintf(PLUGIN_DOWNLOADMANAGER_PERMALINK_BLAHBLAH, $serendipity['serendipityHTTPPath'] . 'downloads.html'));
$propbag->add('default', $serendipity['rewrite'] != 'none'
? $serendipity['serendipityHTTPPath'] . 'downloads.html'
: $serendipity['serendipityHTTPPath'] . $serendipity['indexFile'] . '?/downloads.html');
2011-12-13 12:29:05 +01:00
break;
case 'separator':
$propbag->add('type', 'separator');
break;
2011-12-13 12:29:05 +01:00
// Remember, the chg2archivespath variable was here to indicate the upgrade to DLM Version 0.24+ had run through
// and files got merged to the new location, which should not be configurable any more, so be a 'content' information!
case 'absincomingpath':
$propbag->add('type', 'content');
$propbag->add('default', '<strong>'.PLUGIN_DOWNLOADMANAGER_ABSINCOMINGPATH . '</strong>: <em>' . $serendipity['serendipityPath'] . 'archives/.dlm/ftpin' . '</em><br>' . PLUGIN_DOWNLOADMANAGER_ABSINCOMINGPATH_BLAHBLAH);
2011-12-13 12:29:05 +01:00
break;
case 'absdownloadspath':
$propbag->add('type', 'content');
$propbag->add('default', '<strong>'.PLUGIN_DOWNLOADMANAGER_ABSDOWNLOADPATH . '</strong>: <em>' . $serendipity['serendipityPath'] . 'archives/.dlm/files' . '</em><br>' . PLUGIN_DOWNLOADMANAGER_ABSDOWNLOADPATH_BLAHBLAH);
2011-12-13 12:29:05 +01:00
break;
case 'httppath':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_HTTPPATH);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_HTTPPATH_BLAHBLAH);
$propbag->add('default', $serendipity['serendipityHTTPPath'] . 'plugins/serendipity_event_downloadmanager/');
2011-12-13 12:29:05 +01:00
break;
case 'iconwidth':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_ICONWIDTH);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_ICONWIDTHBLAH);
$propbag->add('default', '18');
2011-12-13 12:29:05 +01:00
break;
case 'iconheight':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_ICONHEIGHT);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_ICONHEIGHT_BLAHBLAH);
$propbag->add('default', '20');
2011-12-13 12:29:05 +01:00
break;
case 'dateformat':
$propbag->add('type', 'string');
$propbag->add('name', GENERAL_PLUGIN_DATEFORMAT);
$propbag->add('description',sprintf(PLUGIN_DOWNLOADMANAGER_DATEFORMAT, 'Y/m/d, h:ia'));
$propbag->add('default', 'Y/m/d, h:ia');
2011-12-13 12:29:05 +01:00
break;
case 'showhidden_registered':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_SHOWHIDDEN_REGISTERED);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_SHOWHIDDEN_REGISTERED_BLAHBLAH);
$propbag->add('default', 'false');
2011-12-13 12:29:05 +01:00
break;
case 'registered_only':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_REGISTERED_ONLY);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_REGISTERED_ONLY_BLAHBLAH);
$propbag->add('default', 'false');
2011-12-13 12:29:05 +01:00
break;
case 'showfilename':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_SHOWFILENAME);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_SHOWFILENAME_BLAHBLAH);
$propbag->add('default', 'true');
2011-12-13 12:29:05 +01:00
break;
case 'showdownloads':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_SHOWDOWNLOADS);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_SHOWDOWNLOADS_BLAHBLAH);
$propbag->add('default', 'true');
2011-12-13 12:29:05 +01:00
break;
case 'showfilesize':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_SHOWFILESIZE);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_SHOWFILESIZE_BLAHBLAH);
$propbag->add('default', 'true');
2011-12-13 12:29:05 +01:00
break;
case 'showdate':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_SHOWFILEDATE);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_SHOWFILEDATE_BLAHBLAH);
$propbag->add('default', 'false');
2011-12-13 12:29:05 +01:00
break;
case 'showdesc_inlist':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_SHOWDESC_INLIST);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_SHOWDESC_INLIST_DESC);
$propbag->add('default', 'false');
2011-12-13 12:29:05 +01:00
break;
case 'directdl_inlist':
$listdl_types = array(
'no' => PLUGIN_DOWNLOADMANAGER_DOWNLOAD_INLIST_NO,
'icon' => PLUGIN_DOWNLOADMANAGER_DOWNLOAD_INLIST_ICON,
'name' => PLUGIN_DOWNLOADMANAGER_DOWNLOAD_INLIST_NAME,
'both' => PLUGIN_DOWNLOADMANAGER_DOWNLOAD_INLIST_BOTH,
);
$propbag->add('type', 'select');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_DOWNLOAD_INLIST);
$propbag->add('description', PLUGIN_DOWNLOADMANAGER_DOWNLOAD_INLIST_DESC);
$propbag->add('default', 'no');
$propbag->add('select_values', $listdl_types);
2011-12-13 12:29:05 +01:00
break;
case 'add_existing_file':
$existing_types = array(
'insert' => PLUGIN_DOWNLOADMANAGER_ADD_EXISTING_INSERT,
'update' => PLUGIN_DOWNLOADMANAGER_ADD_EXISTING_UPDATE,
);
$propbag->add('type', 'select');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_ADD_EXISTING);
$propbag->add('description', PLUGIN_DOWNLOADMANAGER_ADD_EXISTING_DESC);
$propbag->add('default', 'insert');
$propbag->add('select_values', $existing_types);
2011-12-13 12:29:05 +01:00
break;
case 'filename_field' :
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_FILENAME_FIELD);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_FILENAME_FIELD_BLAHBLAH);
$propbag->add('default', PLUGIN_DOWNLOADMANAGER_FILENAME);
2011-12-13 12:29:05 +01:00
break;
case 'filesize_field' :
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_FILESIZE_FIELD);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_FILESIZE_FIELD_BLAHBLAH);
$propbag->add('default', PLUGIN_DOWNLOADMANAGER_FILESIZE);
2011-12-13 12:29:05 +01:00
break;
case 'filedate_field' :
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_FILEDATE_FIELD);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_FILEDATE_FIELD_BLAHBLAH);
$propbag->add('default', PLUGIN_DOWNLOADMANAGER_FILEDATE);
2011-12-13 12:29:05 +01:00
break;
case 'dls_field' :
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_DOWNLOADMANAGER_DLS_FIELD);
$propbag->add('description',PLUGIN_DOWNLOADMANAGER_DLS_FIELD_BLAHBLAH);
$propbag->add('default', PLUGIN_DOWNLOADMANAGER_NUM_DOWNLOADS);
2011-12-13 12:29:05 +01:00
break;
default:
return false;
}
return true;
}
/**
* show
*/
function show()
{
2011-12-13 12:29:05 +01:00
global $serendipity;
if ($this->selected()) {
if (!headers_sent()) {
header('HTTP/1.0 200');
header('Status: 200 OK');
}
if (!is_object($serendipity['smarty'])) {
serendipity_smarty_init();
}
$_ENV['staticpage_pagetitle'] = preg_replace('@[^a-z0-9]@i', '_',$this->get_config('pagetitle'));
$serendipity['smarty']->assign('staticpage_pagetitle', $_ENV['staticpage_pagetitle']);
2011-12-13 12:29:05 +01:00
$this->showShoutPage();
}
}
/**
* This method is a plugin API standard starter
*/
function selected()
{
2011-12-13 12:29:05 +01:00
global $serendipity;
if (!empty($serendipity['POST']['subpage'])) {
$serendipity['GET']['subpage'] = $serendipity['POST']['subpage'];
}
2011-12-13 12:29:05 +01:00
if ($serendipity['GET']['subpage'] == $this->get_config('pageurl') ||
preg_match('@^' . preg_quote($this->get_config('permalink')) . '@i', $serendipity['GET']['subpage'])) {
return true;
}
return false;
}
/**
* setup database
*/
function setupDB()
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$built = $this->get_config('dl_db_built', null);
if (empty($built) && !defined('DLMANAGER_UPGRADE_DONE')) {
$q = "CREATE TABLE {$serendipity['dbPrefix']}dma_downloadmanager_files (
id {AUTOINCREMENT} {PRIMARY},
2011-12-13 12:29:05 +01:00
catid int(10) NOT NULL default '0',
timestamp int(10) NOT NULL default '0',
2011-12-13 12:29:05 +01:00
systemfilename varchar(32),
realfilename varchar(150),
description text,
filesize int(10) NOT NULL default '0',
dlcount int(10) NOT NULL default '0'
)";
$sql = serendipity_db_schema_import($q);
$q = "CREATE TABLE {$serendipity['dbPrefix']}dma_downloadmanager_categories (
node_id {AUTOINCREMENT} {PRIMARY},
root_id int(10) NOT NULL default '0',
payload varchar(64),
lft int(10) NOT NULL default '0',
rgt int(10) NOT NULL default '0'
)";
$sql = serendipity_db_schema_import($q);
$sql = "SELECT *
FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories
WHERE payload = 'root'";
2011-12-13 12:29:05 +01:00
$root = serendipity_db_query($sql);
if (intval($root[0]['node_id']) != 1) {
2011-12-13 12:29:05 +01:00
serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}dma_downloadmanager_categories (payload, lft, rgt )
VALUES ('root', 1, 2 )");
2011-12-13 12:29:05 +01:00
$last_insert_id = serendipity_db_insert_id('dma_downloadmanager_categories', 'node_id');
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_categories
SET root_id = $last_insert_id
WHERE node_id = $last_insert_id");
2011-12-13 12:29:05 +01:00
}
$this->set_config('dl_db_built', '1');
@define('DLMANAGER_UPGRADE_DONE', true); // No further static pages may be called!
}
switch($built){
case 1:
$q = "CREATE TABLE {$serendipity['dbPrefix']}dma_downloadmanager_categories_tmp (
node_id {AUTOINCREMENT} {PRIMARY},
root_id int(10) NOT NULL default '0',
payload varchar(64),
lft int(10) NOT NULL default '0',
rgt int(10) NOT NULL default '0'
)";
$sql = serendipity_db_schema_import($q);
$q = "INSERT INTO {$serendipity['dbPrefix']}dma_downloadmanager_categories_tmp
(node_id, root_id, payload, lft, rgt)
SELECT node_id, root_id, payload, lft, rgt FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories;";
$sql = serendipity_db_schema_import($q);
$q = "DROP TABLE {$serendipity['dbPrefix']}dma_downloadmanager_categories;";
$sql = serendipity_db_schema_import($q);
$q = "CREATE TABLE {$serendipity['dbPrefix']}dma_downloadmanager_categories (
node_id {AUTOINCREMENT} {PRIMARY},
root_id int(10) NOT NULL default '0',
payload varchar(64),
lft int(10) NOT NULL default '0',
rgt int(10) NOT NULL default '0',
hidden int(2) NOT NULL default '0'
)";
$sql = serendipity_db_schema_import($q);
$q = "INSERT INTO {$serendipity['dbPrefix']}dma_downloadmanager_categories
(node_id, root_id, payload, lft, rgt)
SELECT node_id, root_id, payload, lft, rgt FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories_tmp;";
$sql = serendipity_db_schema_import($q);
$q = "DROP TABLE {$serendipity['dbPrefix']}dma_downloadmanager_categories_tmp;";
$sql = serendipity_db_schema_import($q);
$this->set_config('dl_db_built', '2');
break;
}
}
/**
* uninstall API
*/
function uninstall(&$propbag)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
serendipity_db_query("DROP TABLE {$serendipity['dbPrefix']}dma_downloadmanager_files");
serendipity_db_query("DROP TABLE {$serendipity['dbPrefix']}dma_downloadmanager_categories");
}
/**
* dlm sql db function set
*
* @param type string and db vars
* @return result
*
* serendipity_db_query [ sql, single(true, false), arrtype(both, assoc, num), dberror(true, false), string(array key name), string(array field name) ... ]
*/
function dlm_sql_db($type=NULL, $whe=NULL)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
switch($type) {
2011-12-13 12:29:05 +01:00
case 'DLM_BE_UPDATE':
// approve events
$sql = "UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_files SET $whe";
$result = serendipity_db_query($sql, true, 'both', true);
break;
case 'DLM_BE_DELETE_FILE':
// delete events
$sql = "DELETE FROM {$serendipity['dbPrefix']}dma_downloadmanager_files WHERE $whe";
$result = serendipity_db_query($sql, true, 'both', true);
break;
case 'DLM_BE_DELETE_CAT':
// delete events
$sql = "DELETE FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories WHERE $whe";
$result = serendipity_db_query($sql, true, 'both', true);
break;
case 'DLM_SUBCATS':
// mysql_fetch_array :: result is a single array select (TRUE)
$sql = "SELECT node1.*, round((node1.rgt-node1.lft-1)/2,0) AS subcats
FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories AS node1
WHERE $whe";
$result = serendipity_db_query($sql, true, 'assoc', true);
break;
case 'DLM_COUNT':
// count mysql_num_rows :: result is a single array select (TRUE)
$sql = "SELECT COUNT(*) AS num FROM {$serendipity['dbPrefix']}dma_downloadmanager_files WHERE $whe";
$result = serendipity_db_query($sql, true, 'assoc', false);
break;
case 'DLM_SELECT':
// mysql_fetch_array :: result is a single array select (TRUE)
$sql = "SELECT * FROM {$serendipity['dbPrefix']}dma_downloadmanager_files WHERE $whe";
$result = serendipity_db_query($sql, true, 'assoc', true);
break;
case 'DLM_SELECT_ARRAY':
// mysql_fetch_array :: result is a multiple array select (FALSE)
$sql = "SELECT * FROM {$serendipity['dbPrefix']}dma_downloadmanager_files WHERE $whe";
$result = serendipity_db_query($sql, false, 'assoc', true);
break;
}
2011-12-13 12:29:05 +01:00
return $result;
}
/**
* create plugins global array
*
2011-12-13 12:29:05 +01:00
* @access private
*/
function pluginglobs()
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$this->globs = array(
'dateformat' => $this->get_config('dateformat', 'Y-m-d H:i'),
'ftppath' => (!$this->get_config('absincomingpath')
2011-12-13 12:29:05 +01:00
? $serendipity['serendipityPath'] . 'archives/.dlm/ftpin'
: $this->get_config('absincomingpath')),
'dlmpath' => (!$this->get_config('absdownloadspath')
2011-12-13 12:29:05 +01:00
? $serendipity['serendipityPath'] . 'archives/.dlm/files'
: $this->get_config('absdownloadspath')),
'attention' => (($serendipity['version'][0] < 2)
? '<img class="dlm_backend_attention" src="' . $serendipity['serendipityHTTPPath'] . 'templates/default/admin/img/admin_msg_note.png" alt="" />'
: '<span class="msg_error"><span class="icon-attention-circled" aria-hidden="true"></span> ')
2011-12-13 12:29:05 +01:00
);
}
/**
* Avoids "bug" on 64-bit PHP systems cutting off utf8 encoded first chars
* @see https://bugs.php.net/bug.php?id=62119
* @see https://evertpot.com/271/
* Thanks to: http://php.net/manual/de/function.basename.php#85369
*/
function mb_basename($file)
{
return end(explode('/',$file));
}
/**
* get relative path
*/
function getRelPath()
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$c_path = dirname(__FILE__);
$b_path = $serendipity['serendipityPath'];
if ($b_path[(strlen($b_path)-1)] == '/') {
2011-12-13 12:29:05 +01:00
$b_path = substr($b_path, 0, strlen($b_path)-1);
}
$r_path = '.' . str_replace($b_path, '', $c_path);
2011-12-13 12:29:05 +01:00
return $r_path;
}
/**
* error message redirector and Smarty assigning
*/
function ERRMSG($msg, $type='error')
{
2011-12-13 12:29:05 +01:00
global $serendipity;
if (!is_object($serendipity['smarty'])) {
serendipity_smarty_init();
}
if ($serendipity['version'][0] < 2) {
$serendipity['smarty']->assign(array('div' => 'div', 'tag' => 'p'));
}
if ($type == 'error') {
// assign files of category to smarty
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign('dlmerr', array('thiserror' => true, 'errormsg' => $msg));
} elseif ( $type == 'success') {
// assign files of category to smarty
$serendipity['smarty']->assign('dlmerr', array('thiserror' => true, 'successmsg' => $msg));
} elseif ( $type == 'status') {
// assign files of category to smarty
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign('dlmerr', array('thiserror' => true, 'statusmsg' => $msg));
}
}
/**
* Fetch all Categories in Frontend and Backend inclusive root level
*
2011-12-13 12:29:05 +01:00
* @param Boolean BackEndCall (default false)
* @return array
**/
function GetAllCats($bec=false)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$sql = "
SELECT node1.node_id AS node_id,
node1.root_id AS root_id,
node1.payload AS payload,
node1.lft AS lft,
node1.rgt AS rgt,
node1.hidden AS hidden,
round((node1.rgt-node1.lft-1)/2,0) AS subcats,
((min(node2.rgt)-node1.rgt-(node1.lft>1))/2) > 0 AS lower,
(( (node1.lft-max(node2.lft)>1) )) AS upper,
COUNT(*) AS level
FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories AS node1,
{$serendipity['dbPrefix']}dma_downloadmanager_categories AS node2
WHERE node1.lft BETWEEN node2.lft AND node2.rgt
AND (node2.root_id = node1.root_id)
AND (node2.node_id != node1.node_id OR node1.lft = 1)
AND (node2.root_id = 1)";
$sql .= ($bec === false) ? ((serendipity_db_bool($this->get_config('showhidden_registered', 'false')) && serendipity_userLoggedIn()) ? '' : " AND (node1.hidden != 1) AND (node2.hidden != 1) ") : '';
2011-12-13 12:29:05 +01:00
$sql .= " GROUP BY node1.lft, node1.rgt, node1.node_id, node1.root_id, node1.payload";
$cats = serendipity_db_query($sql, false, 'assoc', true);
2011-12-13 12:29:05 +01:00
if (!is_array($cats)) {
$cats = array();
}
2011-12-13 12:29:05 +01:00
return $cats;
}
/**
* get sub categories
*/
function GetSubCats($node_id, $user=0)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$node_id = intval($node_id);
$sql = "SELECT node1.node_id AS node_id,
2011-12-13 12:29:05 +01:00
node1.payload AS payload,
node1.lft AS lft,
node1.rgt AS rgt,
node1.hidden AS hidden,
round((node1.rgt-node1.lft-1)/2,0) AS subcats,
COUNT(*) AS level
FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories AS node1,
{$serendipity['dbPrefix']}dma_downloadmanager_categories AS node2,
{$serendipity['dbPrefix']}dma_downloadmanager_categories AS node3
2011-12-13 12:29:05 +01:00
WHERE node1.lft BETWEEN node2.lft AND node2.rgt
AND node1.lft BETWEEN node3.lft AND node3.rgt
AND node2.root_id = 1
AND node3.node_id = $node_id";
if ($user == 1) {
if (serendipity_db_bool($this->get_config('showhidden_registered', 'false')) && serendipity_userLoggedIn())
2011-12-13 12:29:05 +01:00
$sql .= "";
else
$sql .= " AND (node1.hidden != 1) ";
}
$sql .= " GROUP BY node1.lft, node1.rgt, node1.node_id, node1.root_id, node1.payload";
$cats = serendipity_db_query($sql);
return $cats;
}
/**
* calculate file size
*/
function calcFilesize($filesize)
{
2011-12-13 12:29:05 +01:00
$array = array(
'YB' => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
'ZB' => 1024 * 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
'EB' => 1024 * 1024 * 1024 * 1024 * 1024 * 1024,
'PB' => 1024 * 1024 * 1024 * 1024 * 1024,
'TB' => 1024 * 1024 * 1024 * 1024,
'GB' => 1024 * 1024 * 1024,
'MB' => 1024 * 1024,
'KB' => 1024,
);
if ($filesize <= 1024) {
2011-12-13 12:29:05 +01:00
$filesize = $filesize . ' Bytes';
}
foreach($array AS $name => $size) {
if ($filesize > $size || $filesize == $size) {
2011-12-13 12:29:05 +01:00
$filesize = round((round($filesize / $size * 100) / 100), 2) . ' ' . $name;
}
}
return $filesize;
}
/**
* get mime application
*/
function getMime($filename)
{
2011-12-13 12:29:05 +01:00
static $mimetypes = array(
"ez" => "application/andrew-inset",
"hqx" => "application/mac-binhex40",
"cpt" => "application/mac-compactpro",
"doc" => "application/msword",
"bin" => "application/octet-stream",
"dms" => "application/octet-stream",
"lha" => "application/octet-stream",
"lzh" => "application/octet-stream",
"exe" => "application/octet-stream",
"class" => "application/octet-stream",
"so" => "application/octet-stream",
"dll" => "application/octet-stream",
"oda" => "application/oda",
"pdf" => "application/pdf",
"ai" => "application/postscript",
"eps" => "application/postscript",
"ps" => "application/postscript",
"smi" => "application/smil",
"smil" => "application/smil",
"wbxml" => "application/vnd.wap.wbxml",
"wmlc" => "application/vnd.wap.wmlc",
"wmlsc" => "application/vnd.wap.wmlscriptc",
"bcpio" => "application/x-bcpio",
"vcd" => "application/x-cdlink",
"pgn" => "application/x-chess-pgn",
"cpio" => "application/x-cpio",
"csh" => "application/x-csh",
"dcr" => "application/x-director",
"dir" => "application/x-director",
"dxr" => "application/x-director",
"dvi" => "application/x-dvi",
"spl" => "application/x-futuresplash",
"gtar" => "application/x-gtar",
"hdf" => "application/x-hdf",
"js" => "application/x-javascript",
"skp" => "application/x-koan",
"skd" => "application/x-koan",
"skt" => "application/x-koan",
"skm" => "application/x-koan",
"latex" => "application/x-latex",
"nc" => "application/x-netcdf",
"cdf" => "application/x-netcdf",
"sh" => "application/x-sh",
"shar" => "application/x-shar",
"swf" => "application/x-shockwave-flash",
"sit" => "application/x-stuffit",
"sv4cpio" => "application/x-sv4cpio",
"sv4crc" => "application/x-sv4crc",
"tar" => "application/x-tar",
"tcl" => "application/x-tcl",
"tex" => "application/x-tex",
"texinfo" => "application/x-texinfo",
"texi" => "application/x-texinfo",
"t" => "application/x-troff",
"tr" => "application/x-troff",
"roff" => "application/x-troff",
"man" => "application/x-troff-man",
"me" => "application/x-troff-me",
"ms" => "application/x-troff-ms",
"ustar" => "application/x-ustar",
"src" => "application/x-wais-source",
"xhtml" => "application/xhtml+xml",
"xht" => "application/xhtml+xml",
"zip" => "application/zip",
"au" => "audio/basic",
"snd" => "audio/basic",
"mid" => "audio/midi",
"midi" => "audio/midi",
"kar" => "audio/midi",
"mpga" => "audio/mpeg",
"mp2" => "audio/mpeg",
"mp3" => "audio/mpeg",
"aif" => "audio/x-aiff",
"aiff" => "audio/x-aiff",
"aifc" => "audio/x-aiff",
"m3u" => "audio/x-mpegurl",
"ram" => "audio/x-pn-realaudio",
"rm" => "audio/x-pn-realaudio",
"rpm" => "audio/x-pn-realaudio-plugin",
"ra" => "audio/x-realaudio",
"wav" => "audio/x-wav",
"pdb" => "chemical/x-pdb",
"xyz" => "chemical/x-xyz",
"bmp" => "image/bmp",
"gif" => "image/gif",
"ief" => "image/ief",
"jpeg" => "image/jpeg",
"jpg" => "image/jpeg",
"jpe" => "image/jpeg",
"png" => "image/png",
"tiff" => "image/tiff",
"tif" => "image/tif",
"djvu" => "image/vnd.djvu",
"djv" => "image/vnd.djvu",
"wbmp" => "image/vnd.wap.wbmp",
"ras" => "image/x-cmu-raster",
"pnm" => "image/x-portable-anymap",
"pbm" => "image/x-portable-bitmap",
"pgm" => "image/x-portable-graymap",
"ppm" => "image/x-portable-pixmap",
"prc" => "application/x-pilot",
"pdb" => " application/x-pilot-pdb",
"rgb" => "image/x-rgb",
"xbm" => "image/x-xbitmap",
"xpm" => "image/x-xpixmap",
"xwd" => "image/x-windowdump",
"igs" => "model/iges",
"iges" => "model/iges",
"msh" => "model/mesh",
"mesh" => "model/mesh",
"silo" => "model/mesh",
"wrl" => "model/vrml",
"vrml" => "model/vrml",
"css" => "text/css",
"html" => "text/html",
"htm" => "text/html",
"asc" => "text/plain",
"txt" => "text/plain",
"rtx" => "text/richtext",
"rtf" => "text/rtf",
"sgml" => "text/sgml",
"sgm" => "text/sgml",
"tsv" => "text/tab-seperated-values",
"wml" => "text/vnd.wap.wml",
"wmls" => "text/vnd.wap.wmlscript",
"etx" => "text/x-setext",
"xml" => "text/xml",
"xsl" => "text/xml",
"mpeg" => "video/mpeg",
"mpg" => "video/mpeg",
"mpe" => "video/mpeg",
"qt" => "video/quicktime",
"mov" => "video/quicktime",
"mxu" => "video/vnd.mpegurl",
"avi" => "video/x-msvideo",
"wmv" => "video/x-msvideo",
"movie" => "video/x-sgi-movie",
"ice" => "x-conference-xcooltalk",
"ics" => "text/calendar"
);
$MIMETYPE = array();
$filename = $this->mb_basename($filename);
2011-12-13 12:29:05 +01:00
$fileparts = explode(".", $filename);
$EXTENSION = $fileparts[(count($fileparts) - 1)];
if (file_exists(dirname(__FILE__) . "/img/dlicons/".$EXTENSION.".png")) {
2011-12-13 12:29:05 +01:00
$MIMETYPE['ICON'] = $this->get_config('httppath')."img/dlicons/".$EXTENSION.".png";
} else {
2011-12-13 12:29:05 +01:00
$MIMETYPE['ICON'] = $this->get_config('httppath')."img/dlicons/unknown_small.png";
}
if (!empty($mimetypes[$EXTENSION]) && trim($mimetypes[$EXTENSION]) != '') {
2011-12-13 12:29:05 +01:00
$MIMETYPE['TYPE'] = $mimetypes[$EXTENSION];
} else {
2011-12-13 12:29:05 +01:00
$MIMETYPE['TYPE'] = "application/octet-stream";
}
2011-12-13 12:29:05 +01:00
return $MIMETYPE;
}
/**
* showShoutPage
*/
function showShoutPage()
{
2011-12-13 12:29:05 +01:00
global $serendipity;
if (!headers_sent()) {
2011-12-13 12:29:05 +01:00
header('HTTP/1.0 200');
header('Status: 200 OK');
}
2011-12-13 12:29:05 +01:00
if (!is_object($serendipity['smarty'])) {
2011-12-13 12:29:05 +01:00
serendipity_smarty_init();
}
2011-12-13 12:29:05 +01:00
if (!serendipity_db_bool($this->get_config('unhideroot'))) {
// with DLM version 0.25 we set payload.root to be unhidden.0, to get the expected results in frontend
2011-12-13 12:29:05 +01:00
$result = $this->dlm_sql_db('DLM_UPDATE', "hidden = '0' WHERE payload = 'root' AND node_id = '1'");
if ($result) $this->set_config('unhideroot', 'true');
2011-12-13 12:29:05 +01:00
}
// assign to smarty and all 3 frontend pages
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign(
array(
'httppath' => $this->get_config('httppath'),
'pagetitle' => $this->get_config('pagetitle'),
'headline' => $this->get_config('headline'),
'dlm_intro' => $this->get_config('intro'),
'dlm_is_registered' => serendipity_db_bool($this->get_config('registered_only', 'false'))
)
2011-12-13 12:29:05 +01:00
);
if (isset($_GET['file']) && intval($_GET['file']) >= 1) {
// FRONTEND PAGE 3: FILEDETAILS SINGLE FILE
if (empty($filename) || $filename == 'none.html') {
$filename = 'dlmanager.filedetails.tpl';
}
$id = intval($_GET['file']);
$catid = intval($_GET['thiscat']);
$sqlfe = (serendipity_db_bool($this->get_config('showhidden_registered', 'false')) && serendipity_userLoggedIn()) ? '' : " AND hidden != 1 ";
2011-12-13 12:29:05 +01:00
$cat = $this->dlm_sql_db('DLM_SUBCATS', "node_id = $catid" . $sqlfe);
if (is_array($cat) && !empty($cat)) {
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign('showfile', true);
// get subcats of cat
$ret1 = $this->dlm_sql_db('DLM_COUNT', "catid = $catid");
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign(
array(
'catname' => $cat['payload'],
'pageurl' => $this->get_config('pageurl'),
'catid' => $catid,
'num_subcats' => intval($cat['subcats']),
'num_files' => intval($ret1['num']),
'basepage' => $serendipity['serendipityHTTPPath'] . $serendipity['indexFile']
)
);
// get single file content as a simple linear array, smarty tpl needs no loop, but has no proper $var|print_r output
// else take DLM_SELECT_ARRAY and add $file[0] here - remember smarty switches arrays back -1 level in templates!
$file = $this->dlm_sql_db('DLM_SELECT', "id = $id");
$mime = $this->getMime($file['realfilename']);
2011-12-13 12:29:05 +01:00
if (is_array($file) && !empty($file)) {
$temp_array = array('comment' => stripslashes($file['description']));
serendipity_plugin_api::hook_event('frontend_display', $temp_array);
// push the file array to hold everything needed
$file['id'] = ''; // obfuscated
$file['systemfilename'] = ''; // obfuscated
2011-12-13 12:29:05 +01:00
$file['iconfile'] = $mime['ICON'];
$file['icontype'] = $mime['TYPE'];
$file['iconwidth'] = $this->get_config('iconwidth');
$file['iconheight'] = $this->get_config('iconheight');
$file['filesize_field'] = $this->get_config('filesize_field');
$file['filedate_field'] = $this->get_config('filedate_field');
$file['filename'] = stripslashes($file['realfilename']);
$file['dlcount'] = intval($file['dlcount']);
$file['filesize'] = $this->calcFilesize($file['filesize']);
$file['filedate'] = date($this->globs['dateformat'], $file['timestamp']);
$file['description'] = $temp_array['comment'];
$file['dlurl'] = $serendipity['baseURL'] . ($serendipity['rewrite'] == "none" ? $serendipity['indexFile'] . '?/' : '') . 'plugin/dlfile_'.$id;
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign('thisfile', $file);
}
unset($cat);
unset($file);
}
}
elseif (isset($_GET['thiscat']) && intval($_GET['thiscat']) > 0) {
// FRONTEND PAGE 2: FILELIST OF CATEGORY
if (empty($filename) || $filename == 'none.html')
$filename = 'dlmanager.filelist.tpl';
2011-12-13 12:29:05 +01:00
$id = intval($_GET['thiscat']);
$level = intval($_GET['level']);
$sqlfe = (serendipity_db_bool($this->get_config('showhidden_registered', 'false')) && serendipity_userLoggedIn()) ? '' : " AND hidden != 1 ";
2011-12-13 12:29:05 +01:00
$cat = $this->dlm_sql_db('DLM_SUBCATS', "node_id = $id" . $sqlfe);
if (is_array($cat)) {
2011-12-13 12:29:05 +01:00
$ret1 = $this->dlm_sql_db('DLM_COUNT', "catid = $id");
2011-12-13 12:29:05 +01:00
$num_dls = intval($ret1['num']);
$serendipity['smarty']->assign(
array(
'catname' => $cat['payload'],
'numsubcats' => $cat['subcats'],
'pageurl' => $this->get_config('pageurl'),
'numdls' => intval($num_dls),
'basepage' => $serendipity['serendipityHTTPPath'] . $serendipity['indexFile']
2011-12-13 12:29:05 +01:00
)
);
$files = $this->dlm_sql_db('DLM_SELECT_ARRAY', "catid = $id ORDER BY timestamp DESC");
$subcats = $this->GetSubCats($id, 1);
if (is_array($subcats) && count($subcats) >= 2) {
$serendipity['smarty']->assign('has_subcats', true);
foreach($subcats AS $subcat) {
2011-12-13 12:29:05 +01:00
if ($level == 1) { $sublvl = 2; } else { $sublvl = 2; }
if ($subcat['level'] == ($level + $sublvl)) {
$ret = $this->dlm_sql_db('DLM_COUNT', "catid = ".$subcat['node_id']);
2011-12-13 12:29:05 +01:00
$num_dls = intval($ret['num']);
$nodetb = array('f' => $this->get_config('httppath').'img/f.gif',
'e' => $this->get_config('httppath').'img/e.gif',
2011-12-13 12:29:05 +01:00
'path' => $serendipity['serendipityHTTPPath'] . $serendipity['indexFile'] . "?serendipity[subpage]=".$this->get_config('pageurl')."&amp;level=".($subcat['level']-1)."&amp;thiscat=".$subcat['node_id']
);
// construct the smarty template array
$sctable[] = array('subcat' => $subcat, 'node' => $nodetb, 'num' => $num_dls);
}
}
// assign subcategeories to smarty
$serendipity['smarty']->assign('sclist', $sctable);
}
if (is_array($files)) {
if (serendipity_db_bool($this->get_config('showfilename', 'true')))
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign('filename_field', $this->get_config('filename_field'));
if (serendipity_db_bool($this->get_config('showdownloads', 'true')))
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign('dls_field', $this->get_config('dls_field'));
if (serendipity_db_bool($this->get_config('showfilesize', 'true')))
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign('filesize_field', $this->get_config('filesize_field'));
if (serendipity_db_bool($this->get_config('showdate', 'false')))
2011-12-13 12:29:05 +01:00
$serendipity['smarty']->assign('filedate_field', $this->get_config('filedate_field'));
2011-12-13 12:29:05 +01:00
$nis = array();
$colspan = 0;
if (serendipity_db_bool($this->get_config('showfilename', 'true'))) {
2011-12-13 12:29:05 +01:00
++$colspan;
$nis['showfilename'] = true;
}
if (serendipity_db_bool($this->get_config('showdownloads', 'true'))) {
2011-12-13 12:29:05 +01:00
++$colspan;
$nis['showdownloads'] = true;
}
if (serendipity_db_bool($this->get_config('showfilesize', 'true'))) {
2011-12-13 12:29:05 +01:00
++$colspan;
$nis['showfilesize'] = true;
}
if (serendipity_db_bool($this->get_config('showdate', 'false'))) {
2011-12-13 12:29:05 +01:00
++$colspan;
$nis['showdate'] = true;
}
if (serendipity_db_bool($this->get_config('showdesc_inlist', 'false'))) {
2011-12-13 12:29:05 +01:00
$nis['showdesc_inlist'] = true;
}
foreach($files AS $file) {
2011-12-13 12:29:05 +01:00
$infourl = $serendipity['serendipityHTTPPath'] . $serendipity['indexFile'] . "?serendipity[subpage]=".$this->get_config('pageurl')."&amp;thiscat=".$id."&amp;file=".$file['id'];
$dlurl = $serendipity['baseURL'] . ($serendipity['rewrite'] == 'none' ? $serendipity['indexFile'] . '?/' : '') . 'plugin/dlfile_' . $file['id'];
2011-12-13 12:29:05 +01:00
$mime = $this->getMime($file['realfilename']);
2011-12-13 12:29:05 +01:00
$fileinfo = array('file_desc' => str_replace(array("\r\n","\n","\r"),array("<br />","<br />","<br />"), $file['description']),
'filedate' => date($this->globs['dateformat'], $file['timestamp']),
'filesize' => $this->calcFilesize($file['filesize']),
'iconfile' => $mime['ICON'],
'iconwidth' => $this->get_config('iconwidth'),
'iconheight' => $this->get_config('iconheight'),
'icontype' => $mime['TYPE'],
'iconurl' => ($this->get_config('directdl_inlist','no') == 'icon' || $this->get_config('directdl_inlist','no') == 'both') ? $dlurl : $infourl,
'nameurl' => ($this->get_config('directdl_inlist','no') == 'name' || $this->get_config('directdl_inlist','no') == 'both') ? $dlurl : $infourl
);
// construct the smarty template array
$fltable[] = array('file' => $file, 'info' => $fileinfo, 'is' => $nis, 'col' => $colspan);
}
// assign files of categeory to smarty
$serendipity['smarty']->assign('fltable', $fltable);
}
}
unset($cat);
unset($files);
} else {
// FRONTEND PAGE 1: SUBCATLIST OF ROOT CATEGORY
if (empty($filename) || $filename == 'none.html')
$filename = 'dlmanager.catlist.tpl';
2011-12-13 12:29:05 +01:00
// build the frontend category array list (in backend = false and as <select> call list = false)
$catlist = array();
$catlist = $this->buildCategoriesList();
2011-12-13 12:29:05 +01:00
if (is_array($catlist) && sizeof($catlist) >= 1) {
$serendipity['smarty']->assign(
array(
'categories_found' => true,
'catlist' => $catlist
)
);
2011-12-13 12:29:05 +01:00
} else {
$serendipity['smarty']->assign('categories_found', false);
}
unset($catlist);
unset($cats);
}
/* get the frontend dlm template file */
echo $this->parseTemplate($filename);
2011-12-13 12:29:05 +01:00
}
/**
* Generate cryptographically secure random strings.
* Based on Kohana's Text::random() method and this answer:http://stackoverflow.com/a/13733588/179104
* Thanks to https://gist.github.com/raveren/5555297
*/
function random_text( $type = 'alnum', $length = 8 )
{
switch ( $type ) {
case 'alnum':
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'alpha':
$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case 'hexdec':
$pool = '0123456789abcdef';
break;
case 'numeric':
$pool = '0123456789';
break;
case 'nozero':
$pool = '123456789';
break;
case 'distinct':
$pool = '2345679ACDEFHJKLMNPRSTUVWXYZ';
break;
default:
$pool = (string) $type;
break;
}
2011-12-13 12:29:05 +01:00
$crypto_rand_secure = function ( $min, $max ) {
$range = $max - $min;
if ( $range < 0 ) return $min; // not so random...
$log = log( $range, 2 );
$bytes = (int) ( $log / 8 ) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) ( 1 << $bits ) - 1; // set all lower bits to 1
do {
$rnd = hexdec( bin2hex( openssl_random_pseudo_bytes( $bytes ) ) );
$rnd = $rnd & $filter; // discard irrelevant bits
} while ( $rnd >= $range );
return $min + $rnd;
};
$token = '';
$max = strlen( $pool );
for ( $i = 0; $i < $length; $i++ ) {
$token .= $pool[$crypto_rand_secure( 0, $max )];
2011-12-13 12:29:05 +01:00
}
return $token;
2011-12-13 12:29:05 +01:00
}
/**
* add category
*/
function addCat($node_id, $catname)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$sql = "SELECT root_id, lft, rgt
FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories
WHERE node_id = $node_id";
$node = serendipity_db_query($sql);
$root_id = $node[0]['root_id'];
$lft = $node[0]['lft'];
$rgt = $node[0]['rgt'];
2011-12-13 12:29:05 +01:00
@serendipity_db_query("LOCK TABLES {$serendipity['dbPrefix']}dma_downloadmanager_categories WRITE");
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_categories
SET lft = lft + 2
WHERE root_id = $root_id
AND lft > $rgt
AND rgt >= $rgt");
2011-12-13 12:29:05 +01:00
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_categories
SET rgt = rgt + 2
WHERE root_id = $root_id
AND rgt >= $rgt");
2011-12-13 12:29:05 +01:00
serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}dma_downloadmanager_categories
( root_id, payload, lft, rgt )
VALUES
2011-12-13 12:29:05 +01:00
( $root_id, '".serendipity_db_escape_string($catname)."', $rgt, $rgt + 1 )");
@serendipity_db_query("UNLOCK TABLES");
return true;
}
/**
* build the full categories list array
*
2011-12-13 12:29:05 +01:00
* @param boolean catlist call backend (default: false)
* @param boolean <select> call array (default: false)
* @return array
*/
function buildCategoriesList($admin=false, $seca=false)
{
2011-12-13 12:29:05 +01:00
$cats = array();
$cats = $this->GetAllCats($admin);
foreach($cats AS $cat) {
2011-12-13 12:29:05 +01:00
if (($cat['level'] == 1)) {
$parent = array();
}
if ( ($cat['level'] >= $last['level']))
$parent[$last['level']] = $last;
if ($seca === false) {
2011-12-13 12:29:05 +01:00
// the frontend and backend category call
if ($cat['payload'] != 'root') {
2011-12-13 12:29:05 +01:00
$clt[] = $this->buildCat($cat, $parent);
}
} else {
// the backend <select> call
$clt[] = $this->buildCat($cat, $parent, true);
}
$last = $cat;
}
2011-12-13 12:29:05 +01:00
unset($last);
unset($cat);
unset($parent);
unset($cats);
2011-12-13 12:29:05 +01:00
return $clt;
}
/**
* build the category array
*
2011-12-13 12:29:05 +01:00
* @param array A referenced array of this category
* @param array A referenced array of the interating last cat array
* @param boolean is backend <select> call (default: false)
* @return array
*/
function buildCat($cat, $parent, $seca=false)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
if ($seca === false) {
2011-12-13 12:29:05 +01:00
$ret = $this->dlm_sql_db('DLM_COUNT', "catid = ".$cat['node_id']);
$fnum = intval($ret['num']);
} else $fnum = NULL; // backend <select> has no need of filenum in cat
2011-12-13 12:29:05 +01:00
$tl = array(); // build tab list
$cl = array(); // build cat list
// build the category level (image/select) array
for ($i=2; $i<$cat['level']; ++$i) {
if ($parent[$i]['lower']) {
2011-12-13 12:29:05 +01:00
$tl[] = 'l';
} else {
2011-12-13 12:29:05 +01:00
$tl[] = 'e';
}
}
if ( ($cat['level'] > 1) ) {
if ($cat['lower']) {
2011-12-13 12:29:05 +01:00
$tl[] = 'b';
} else {
2011-12-13 12:29:05 +01:00
$tl[] = 'nb';
}
}
$path = ($_GET['serendipity']['adminAction'] == 'downloadmanager')
? ''
: $serendipity['serendipityHTTPPath'] . $serendipity['indexFile'] . '?serendipity[subpage]='.$this->get_config('pageurl').'&amp;level='.$cat['level'].'&amp;thiscat='.$cat['node_id'];
2011-12-13 12:29:05 +01:00
// construct the smarty template array (no need of catname, while sending full cat array, used here, as long as defined in other function)
$cl = array( 'cat' => $cat,
'catname' => $cat['payload'],
'imgname' => $tl,
'filenum' => $fnum,
2011-12-13 12:29:05 +01:00
'path' => $path
);
2011-12-13 12:29:05 +01:00
return $cl;
}
/**
* delete categoey
*/
function delCat($node_id)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$id = intval($node_id);
$sql = "SELECT node1.root_id, node1.lft, node1.rgt,
round((node1.rgt-node1.lft-1)/2,0) AS subcats
FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories AS node1
WHERE node_id = $id";
2011-12-13 12:29:05 +01:00
$node = serendipity_db_query($sql);
if (is_array($node)) {
2011-12-13 12:29:05 +01:00
if ($node[0]['subcats']>=1) {
2011-12-13 12:29:05 +01:00
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_DELETE_NOT_ALLOWED);
} else {
$files_deleted = 0;
$sql = "SELECT systemfilename
FROM {$serendipity['dbPrefix']}dma_downloadmanager_files
WHERE catid = $id";
2011-12-13 12:29:05 +01:00
$files = serendipity_db_query($sql);
2011-12-13 12:29:05 +01:00
if (is_array($files)) {
foreach($files AS $file) {
2011-12-13 12:29:05 +01:00
if (file_exists($this->globs['dlmpath']."/".$file['systemfilename']) && !@unlink($this->globs['dlmpath']."/".$file['systemfilename'])) {
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_DELETE_IN_DOWNLOADDIR_NOT_ALLOWED);
$files_deleted = 0;
} else {
$files_deleted = 1;
}
}
} else { $files_deleted = 1; }
2011-12-13 12:29:05 +01:00
if ($files_deleted == 1) {
@serendipity_db_query("LOCK TABLES {$serendipity['dbPrefix']}dma_downloadmanager_categories WRITE");
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_categories
SET lft=lft-2
WHERE lft > ".$node[0]['lft']."
AND root_id = ".$node[0]['root_id']);
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_categories
SET rgt=rgt-2
WHERE rgt > ".$node[0]['rgt']."
AND root_id = ".$node[0]['root_id']);
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories
WHERE node_id = ".$id);
2011-12-13 12:29:05 +01:00
@serendipity_db_query("UNLOCK TABLES");
}
}
}
return true;
}
/**
* hide category
*/
function hideCat($catid, $hide)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$sql = "SELECT node1.root_id, node1.lft, node1.rgt,
round((node1.rgt-node1.lft-1)/2,0) AS subcats
FROM {$serendipity['dbPrefix']}dma_downloadmanager_categories AS node1
WHERE node_id = $catid";
2011-12-13 12:29:05 +01:00
$node = serendipity_db_query($sql);
if (is_array($node)) {
2011-12-13 12:29:05 +01:00
@serendipity_db_query("LOCK TABLES {$serendipity['dbPrefix']}dma_downloadmanager_categories WRITE");
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_categories
SET hidden = ".$hide."
WHERE node_id = ".$catid);
2011-12-13 12:29:05 +01:00
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_categories
SET hidden = ".$hide."
WHERE lft BETWEEN ".$node[0]['lft']." AND ".$node[0]['rgt']);
2011-12-13 12:29:05 +01:00
@serendipity_db_query("UNLOCK TABLES");
}
}
/**
* rename categories
*/
function renameCats($catnames)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
2011-12-13 12:29:05 +01:00
if (!is_array($catnames)) {
return false;
}
@serendipity_db_query("LOCK TABLES {$serendipity['dbPrefix']}dma_downloadmanager_categories WRITE");
foreach ($catnames AS $id => $name) {
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_categories
SET payload = '".serendipity_db_escape_string($name)."'
WHERE node_id = ".intval($id));
2011-12-13 12:29:05 +01:00
}
@serendipity_db_query("UNLOCK TABLES");
}
/**
* delete possible empty ftp directories
*/
function delEmptyDir()
{
$_dir = $this->globs['ftppath'];
try {
$_ftpDirs = new RecursiveDirectoryIterator($_dir);
// NOTE: UnexpectedValueException thrown for PHP >= 5.3
}
catch (Exception $e) {
return 0;
}
$_files = new RecursiveIteratorIterator($_ftpDirs, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($_files AS $_file) {
if ($_file->isDir()) {
if (!$_files->isDot()) {
// delete folder if empty, non-empty are skipped
@rmdir($_file->getPathname());
}
}
}
}
2011-12-13 12:29:05 +01:00
/**
* delete file
*/
function delFile($file_id)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$id = intval($file_id);
$ret = serendipity_db_query("SELECT systemfilename
FROM {$serendipity['dbPrefix']}dma_downloadmanager_files
WHERE id = $id");
2011-12-13 12:29:05 +01:00
if (file_exists($this->globs['dlmpath']."/".$ret[0]['systemfilename']) && !@unlink($this->globs['dlmpath']."/".$ret[0]['systemfilename'])) {
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_DELETE_IN_DOWNLOADDIR_NOT_ALLOWED);
} else {
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}dma_downloadmanager_files WHERE id = $id");
}
return true;
}
/**
* delete incoming file
*/
function delIncomingFile($file)
{
// removed $this->globs['ftppath']."/".$file to $file, since new iterator fetch is using fullpath already, put to delinfile
if (file_exists($file) && !@unlink($file)) {
2011-12-13 12:29:05 +01:00
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_DELETE_IN_INCOMING_NOT_ALLOWED);
} else {
2011-12-13 12:29:05 +01:00
return true;
}
}
/**
* WIN OS filename encodings for read, handle, write in case of LANG_CHARSET == UTF-8
* - decodes to ISO-8859-1 for file properties stats reading checks
* - converts to UTF-8 in case of POSTing arrays, sending single filenames via GET and storing to the database tables
*
* @param string
* @return string
*/
function encodeToUTF($name, $reverse=false)
{
if ($this->isWIN === null) {
// Windows does not have UTF-8 locales.
$this->isWIN = (LANG_CHARSET == 'UTF-8' && (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? true : false));
}
if ($this->debug) echo "<b>NAME</b> ".$this->mb_basename($name)." is: <b>".mb_detect_encoding($name, 'UTF-8, ISO-8859-1', true)."</b><br>\n";
// if WIN decode for stats
if ($this->isWIN) {
if (!$reverse) {
$name = utf8_decode($name);
if ($this->debug) echo '<b>NAME</b> return for file props internally UTF-8 <b>de</b>coded: <em>'.$this->mb_basename($name)."</em><br>\n";
if ($this->debug) echo "<b>NAME</b> detected as: <b>".mb_detect_encoding($name, 'UTF-8, ISO-8859-1', true)."</b><br><br>\n";
} else {
// ASCII filenames only!
if (!function_exists('mb_convert_encoding')) {
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_PHPMB_ERROR, 'status');
}
if (mb_detect_encoding($name, 'UTF-8', false)) {
if ($this->debug) echo "<b>NAME</b> return mb_convert_encoding back to UTF-8 for file prop stats reading: ";
$name = mb_convert_encoding($name, 'UTF-8', 'ISO-8859-1');
if ($this->debug) echo "<b>".mb_detect_encoding($name, 'UTF-8, ISO-8859-1', true)."</b><br><br>\n";
}
}
}
return $name;
}
2011-12-13 12:29:05 +01:00
/**
* import file
*/
function importFile($filedir, $catid)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$catid = intval($catid);
$filedir = $this->encodeToUTF($filedir); // OK for copy
// Avoid basename cutting Umlaut UTF-8 Chars on 1st char, eg <20>rgerlich.pdf uploaded as rgerlich.pdf
// this may be some kind of PHP bug (https://bugs.php.net/bug.php?id=62119) or is locale-aware, or be, while encoded chars start with a slash;
// Anyway we just check if we are inside a dir with a DS, which avoids this - (false === (strpos($filedir, '/'))) ? $filedir : basename($filedir);
$file = $this->mb_basename($filedir); // end(explode...) is doing the same... though
$file = $this->encodeToUTF($file, true); // to UTF-8 while searched in DB where we have utf8
2011-12-13 12:29:05 +01:00
// Check if file is already existing in category:
$file_update = false;
if ($this->get_config('add_existing_file', 'insert') == 'update') {
$sql = "SELECT systemfilename
FROM {$serendipity['dbPrefix']}dma_downloadmanager_files
WHERE realfilename = '".serendipity_db_escape_string($file)."'
AND catid = $catid
ORDER BY timestamp DESC";
$dbfilelist = serendipity_db_query($sql);
$file_update = is_array($dbfilelist);
2011-12-13 12:29:05 +01:00
}
$sysfilename = $file_update ? $dbfilelist[0]['systemfilename'] : $this->random_text('alnum', 32);
if (@copy($this->globs['ftppath']."/".$filedir, $this->globs['dlmpath']."/".$sysfilename)) {
$timestamp = @filemtime($this->globs['dlmpath']."/".$sysfilename);
$filesize = @filesize($this->globs['dlmpath']."/".$sysfilename);
2011-12-13 12:29:05 +01:00
if ($file_update) {
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_files
SET timestamp = '$timestamp',
filesize = '$filesize'
WHERE
catid = '$catid' AND
realfilename = '".serendipity_db_escape_string($file)."'");
2011-12-13 12:29:05 +01:00
} else {
serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}dma_downloadmanager_files
(
catid,
timestamp,
systemfilename,
realfilename,
filesize
) VALUES (
'".$catid."',
'".$timestamp."',
'".$sysfilename."',
'".serendipity_db_escape_string($file)."',
'".$filesize."'
)");
}
@chmod($this->globs['dlmpath']."/".$sysfilename, 0666);
if (file_exists($this->globs['ftppath']."/".$filedir) && !@unlink($this->globs['ftppath']."/".$filedir)) {
2011-12-13 12:29:05 +01:00
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_DELETE_IN_INCOMING_NOT_ALLOWED);
}
2011-12-13 12:29:05 +01:00
} else {
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_COPY_NOT_ALLOWED);
}
}
/**
* import MediaLibrary file
*/
function importMLFile($file, $path, $catid)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
2011-12-13 12:29:05 +01:00
$catid = intval($catid);
$sysfilename = $this->random_text('alnum', 32);
2011-12-13 12:29:05 +01:00
if (isset($path) && trim($path) != '' && !preg_match("@\.\./@", $path)) {
2011-12-13 12:29:05 +01:00
$extrapath = trim($path);
} else {
$extrapath = '';
2011-12-13 12:29:05 +01:00
}
$uploadPath = $serendipity['serendipityPath'].$serendipity['uploadPath'];
if ($uploadPath[(strlen($uploadPath)-1)] == '/') {
2011-12-13 12:29:05 +01:00
$uploadPath = substr($uploadPath, 0, (strlen($uploadPath)-1));
}
2011-12-13 12:29:05 +01:00
$uploadPath .= $extrapath;
if (@copy($uploadPath."/".$file, $this->globs['dlmpath']."/".$sysfilename)) {
$timestamp = filemtime($this->globs['dlmpath']."/".$sysfilename);
$filesize = filesize($this->globs['dlmpath']."/".$sysfilename);
serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}dma_downloadmanager_files
(
catid,
timestamp,
systemfilename,
realfilename,
filesize
) VALUES (
'".$catid."',
'".$timestamp."',
'".$sysfilename."',
'".serendipity_db_escape_string($file)."',
'".$filesize."'
)");
@chmod($this->globs['dlmpath']."/".$sysfilename, 0666);
} else {
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_COPY_NOT_ALLOWED);
}
}
/**
* edit file
*/
function editFile($fileid, $oldcatid, $newcatid, $rename, $description)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_files
SET catid='".$newcatid."',
realfilename='".serendipity_db_escape_string($rename)."',
description='".serendipity_db_escape_string($description)."'
WHERE id = ".$fileid);
2011-12-13 12:29:05 +01:00
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_FILE_EDITED, 'success');
2011-12-13 12:29:05 +01:00
$_GET['thiscat'] = $newcatid;
$_GET['editfile'] = $fileid;
}
/**
* upload files
*/
function uploadFiles()
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$upload_max_filesize = ini_get('upload_max_filesize');
$upload_max_filesize = preg_replace('/M/', '000000', $upload_max_filesize);
$MAX_FILE_SIZE = intval($upload_max_filesize);
$SUCCESS = 0;
$countfile = 0;
if (isset($serendipity['POST']['uploaded']) && intval($serendipity['POST']['uploaded']) >= 1) {
$catid = intval($serendipity['POST']['catid']);
for ($ulnum=0;$ulnum<count($_FILES['file']['tmp_name']);++$ulnum) {
if (trim($_FILES['file']['tmp_name'][$ulnum]) != "none" AND $_FILES['file']['size'][$ulnum] >= 5 AND is_uploaded_file($_FILES['file']['tmp_name'][$ulnum])) {
$FILESIZE = $_FILES['file']['size'][$ulnum];
if ($FILESIZE > ($MAX_FILE_SIZE)) {
$TOOBIG[] = $_FILES['file']['name'][$ulnum];
} else {
$fname = serendipity_db_escape_string($_FILES['file']['name'][$ulnum]);
// Check, if file already exists in database
$file_update = false;
if ($this->get_config('add_existing_file', 'insert') == 'update') {
$sql = "SELECT systemfilename
FROM {$serendipity['dbPrefix']}dma_downloadmanager_files
WHERE realfilename = $fname
AND catid = $catid
ORDER BY timestamp DESC";
2011-12-13 12:29:05 +01:00
$dbfilelist = serendipity_db_query($sql);
$file_update = is_array($dbfilelist);
2011-12-13 12:29:05 +01:00
}
if ($file_update) {
$SERVERFILENAME = $dbfilelist[0]['systemfilename'];
} else {
$SERVERFILENAME = $this->random_text('alnum', 32);
2011-12-13 12:29:05 +01:00
}
2011-12-13 12:29:05 +01:00
if (!move_uploaded_file($_FILES['file']['tmp_name'][$ulnum], $this->globs['dlmpath']."/".$SERVERFILENAME)) {
$NOTCOPIED[] = $_FILES['file']['name'][$ulnum];
} else {
$fdesc = serendipity_db_escape_string($serendipity['POST']['desc'][$ulnum]);
if ($file_update) {
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_files
SET timestamp = '".time()."',
filesize = '$FILESIZE' ".
(!empty($fdesc) ? ",description='$fdesc'" : '').
" WHERE
2011-12-13 12:29:05 +01:00
catid = '$catid' AND
realfilename = '".$fname."'");
} else {
serendipity_db_query("INSERT INTO {$serendipity['dbPrefix']}dma_downloadmanager_files
(
catid,
timestamp,
systemfilename,
realfilename,
description,
filesize
) VALUES (
'".$catid."',
'".time()."',
'".$SERVERFILENAME."',
'".$fname."',
'".$fdesc."',
'".$FILESIZE."'
)");
}
@chmod($this->globs['dlmpath']."/".$SERVERFILENAME, 0666);
$SUCCESS = 1;
++$countfile;
}
}
@unlink($_FILES['file']['tmp_name'][$ulnum]);
}
}
}
$this->UPLOAD_SUCCESS =& $SUCCESS;
$this->UPLOAD_COUNT =& $countfile;
$this->UPLOAD_TOOBIG =& $TOOBIG;
$this->UPLOAD_NOTCOPIED =& $NOTCOPIED;
$_GET['thiscat'] = intval($serendipity['POST']['catid']);
}
/**
* show downloads
*/
function ADMIN_showDownloads()
{
2011-12-13 12:29:05 +01:00
global $serendipity;
2011-12-13 12:29:05 +01:00
// forbid entry if not admin
if (!serendipity_userLoggedIn() && $_SESSION['serendipityAuthedUser'] !== true && $_SESSION['serendipityUserlevel'] != '255') {
return false;
2011-12-13 12:29:05 +01:00
}
$ddiv = false;
2011-12-13 12:29:05 +01:00
if (!is_dir($this->globs['ftppath'])) {
@mkdir($this->globs['ftppath'], 0777, true);
}
2011-12-13 12:29:05 +01:00
if (!is_dir($this->globs['dlmpath'])) {
@mkdir($this->globs['dlmpath'], 0777, true);
}
if (!empty($serendipity['POST']['dlmanAction']) && $serendipity['POST']['childof'] >= 1) {
$this->addCat(intval($serendipity['POST']['childof']), $serendipity['POST']['catname']);
}
elseif (!empty($serendipity['POST']['dlmanAction']) && $serendipity['POST']['edited'] >= 1) {
$this->editFile(intval($serendipity['POST']['fileid']), intval($serendipity['POST']['catid']), intval($serendipity['POST']['moveto']), $serendipity['POST']['realfilename'], $serendipity['POST']['description']);
}
elseif (!empty($serendipity['POST']['dlmanAction']) && $serendipity['POST']['uploaded'] >= 1) {
$this->uploadFiles();
}
if (!empty($serendipity['POST']['catnamAction'])) {
$this->renameCats($_POST['catname']);
}
if (!empty($_GET['delcat']) && intval($_GET['delcat']) != 1) {
$this->delCat(intval($_GET['delcat']));
}
if (!empty($_GET['hidecat']) && intval($_GET['catid']) >= 1) {
$this->hideCat(intval($_GET['catid']), intval($_GET['hide']));
}
if (!empty($_GET['delfile']) && intval($_GET['delfile']) != 0) {
$this->delFile(intval($_GET['delfile']));
}
if (!empty($_GET['delinfile']) && trim($_GET['delinfile']) != '') {
if ($this->delIncomingFile($this->globs['ftppath']."/".$_GET['delinfile'])) {
$this->delEmptyDir();
}
2011-12-13 12:29:05 +01:00
}
if (!empty($_GET['importfile']) && trim($_GET['importfile']) != '') {
2011-12-13 12:29:05 +01:00
$this->importFile($_GET['importfile'], intval($_GET['thiscat']));
}
if (!empty($_GET['ifile']) && intval($_GET['medialib']) == 1) {
$this->importMLFile($_GET['ifile'], $_GET['smlpath'], intval($_GET['thiscat']));
}
$page = 1;
$divnum = 3;
if ($_GET['editfile']) { $page = 3; }
if ($_GET['thiscat']) { $page = 2; $divnum = 4; }
// assign some global backend vars as 'dlmgbl' array to smarty backend index template
2011-12-13 12:29:05 +01:00
// strip last character / in string 'thispath' is $string = substr($string, 0, -1); else in smarty as modifier $string|substr:0:-1
$serendipity['smarty']->assign('dlmgbl',
array(
2011-12-13 12:29:05 +01:00
'httppath' => $this->get_config('httppath'),
'filename_field' => $this->get_config('filename_field'),
'filenums_field' => $this->get_config('dls_field'),
'filesize_field' => $this->get_config('filesize_field'),
'filedate_field' => $this->get_config('filedate_field'),
'thisversion' => $serendipity['plugin_dlm_version'],
'thispath' => $serendipity['serendipityPath'] . substr($serendipity['dlm']['pluginpath'], 0, -1),
'thiscat' => intval($_GET['thiscat']) ? intval($_GET['thiscat']) : 1,
'thispage' => $page
)
);
2011-12-13 12:29:05 +01:00
if (!empty($_GET['thiscat']) && intval($_GET['thiscat']) >= 1) { //!= 1, this didn't get root level files to move etc, but now the back button of page 3 (file details) returns two root levels, one like a sublevel and one as the dlm startpage which isn't too confusing
/* BACKEND PAGE 2 SECTION
- edit (single file move and file description),
- upload form,
- files in selected category,
- ftp/trash folderfiles,
- Serendipities media library files with subcats,
- subcategories of root,
2011-12-13 12:29:05 +01:00
- the appendix (including the helptip and the clear trash button) */
2011-12-13 12:29:05 +01:00
$id = intval($_GET['thiscat']);
$cat = $this->dlm_sql_db('DLM_SUBCATS', "node_id = $id");
if (is_array($cat)) {
2011-12-13 12:29:05 +01:00
$ret1 = $this->dlm_sql_db('DLM_COUNT', "catid = $id");
2011-12-13 12:29:05 +01:00
$num_dls = intval($ret1['num']);
$cat['num'] = $num_dls;
// append to backend global array in index headers and page 2 subpages the selected cat array
$serendipity['smarty']->append('dlmgbl', array('cat' => $cat));
if (!empty($_GET['editfile']) && intval($_GET['editfile']) >= 1) {
// generate the single file edit page (id = catid)
$this->backend_dlm_edit_file($cat, intval($_GET['editfile']), $id);
} else {
if (!empty($_GET['upload']) && intval($_GET['upload']) >= 1) {
2011-12-13 12:29:05 +01:00
// generate the upload form - PAGE 2
$this->backend_dlm_build_uploadform($id);
2011-12-13 12:29:05 +01:00
} else {
if (count($this->UPLOAD_TOOBIG) >= 1 || count($this->UPLOAD_NOTCOPIED) >= 1) {
$ERRMSG = PLUGIN_DOWNLOADMANAGER_ERRORS_OCCOURED."<br />";
if (count($this->UPLOAD_TOOBIG) >= 1) {
$ERRMSG .= "<br />".PLUGIN_DOWNLOADMANAGER_ERRORS_TOOBIG."<br />";
for ($a=0; $a<count($this->UPLOAD_TOOBIG); ++$a) {
2011-12-13 12:29:05 +01:00
$ERRMSG .= $this->UPLOAD_TOOBIG[$a]."<br />";
}
}
if (count($this->UPLOAD_NOTCOPIED) >= 1) {
$ERRMSG .= "<br />".PLUGIN_DOWNLOADMANAGER_ERRORS_NOTCOPIED."<br />";
for ($a=0;$a<count($this->UPLOAD_NOTCOPIED);++$a) {
$ERRMSG .= $this->UPLOAD_NOTCOPIED[$a]."<br />";
}
}
$this->ERRMSG($ERRMSG);
}
$ct = (isset($_GET['dlmftpdir']) && (intval($_GET['dlmftpdir']) == 1) ? true : false);
2011-12-13 12:29:05 +01:00
$files = $this->dlm_sql_db('DLM_SELECT_ARRAY', "catid = ".$id." ORDER BY timestamp DESC");
$ddiv = ($_GET['thiscat'] && $num_dls > 0) ? true : false;
2011-12-13 12:29:05 +01:00
// generate file table content here - DIV 1 @ PAGE 2
$this->backend_dlm_build_filetable($files, (($ct) ? false : $ddiv), $this->globs['ftppath'], $this->globs['dlmpath'], $this->globs['dateformat'], $num_dls, intval($_GET['thiscat']), 1, 2);
// generate ftp/trash folder table content here - DIV 2 @ PAGE 2
$this->backend_dlm_build_ftptable($ct, $this->globs['ftppath'], intval($_GET['thiscat']), 2, 2);
// generate media library folder table content here - DIV 3 @ PAGE 2
2011-12-13 12:29:05 +01:00
$this->backend_dlm_build_s9ml_table($this->globs['dateformat'], $serendipity['serendipityPath'] . $serendipity['uploadPath'], 3, 2);
if (!is_array($cats)) {
2011-12-13 12:29:05 +01:00
$cats = array();
$cats = $this->GetAllCats(true);
2011-12-13 12:29:05 +01:00
if (is_array($cats) && sizeof($cats) >= 1) {
unset($cat);
}
}
// new: generate categories table - DIV 4 (permanently open = true) @ PAGE 2
$this->backend_dlm_build_categories($cats, (($ct) ? false : true), $this->globs['ftppath'], intval($_GET['thiscat']), 4, 2);
2011-12-13 12:29:05 +01:00
// generate appendix trash & helptip
$this->backend_dlm_appendix($this->globs['ftppath'], intval($_GET['thiscat']), 5, 2);
2011-12-13 12:29:05 +01:00
}
}
} else {
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_CAT_NOT_FOUND);
}
} else {
/* BACKEND PAGE 1 ROOT SECTION
2011-12-13 12:29:05 +01:00
- add subcategories to root or selected category
- files in selected category,
- subcategories of root,
2011-12-13 12:29:05 +01:00
- the appendix (including the helptip and the clear trash button) */
if (!$_GET['thiscat'] || empty($_GET['thiscat'])) $_GET['thiscat'] = 1;
2011-12-13 12:29:05 +01:00
// generate categories add table - DIV 1 @PAGE 1 == startpage
$this->backend_dlm_add_categories($cats, 1, 1);
$files = $this->dlm_sql_db('DLM_SELECT_ARRAY', "catid = 1 ORDER BY timestamp DESC");
2011-12-13 12:29:05 +01:00
$fnum = (is_array($files) ? count($files) : 0);
2011-12-13 12:29:05 +01:00
// new: generate file table content here - DIV 2 @ PAGE 1 == startpage
$this->backend_dlm_build_filetable($files, $ddiv, $this->globs['ftppath'], $this->globs['dlmpath'], $this->globs['dateformat'], $fnum, intval($_GET['thiscat']), 2, 1);
if (!is_array($cats)) {
2011-12-13 12:29:05 +01:00
$cats = array();
$cats = $this->GetAllCats(true);
2011-12-13 12:29:05 +01:00
if (is_array($cats) && sizeof($cats) >= 1) {
unset($files);
}
}
2011-12-13 12:29:05 +01:00
// generate categories table - DIV 3 (permanently open = true) @ PAGE 1 == startpage
$this->backend_dlm_build_categories($cats, true, $this->globs['ftppath'], intval($_GET['thiscat']), 3, 1);
2011-12-13 12:29:05 +01:00
// generate appendix trash & helptip
$this->backend_dlm_appendix($this->globs['ftppath'], intval($_GET['thiscat']), 4, 1);
2011-12-13 12:29:05 +01:00
}
2011-12-13 12:29:05 +01:00
/* get the backend dlm index template file */
echo $this->parseTemplate('backend.dlm.index.tpl');
2011-12-13 12:29:05 +01:00
}
2011-12-13 12:29:05 +01:00
/**
* generate content API
*/
function generate_content(&$title)
{
2011-12-13 12:29:05 +01:00
$title = PLUGIN_DOWNLOADMANAGER_TITLE.' ('.$this->get_config('pageurl').')';
}
/**
* install API
*/
function install()
{
2011-12-13 12:29:05 +01:00
$this->setupDB();
}
/**
* event_ hook API
*/
function event_hook($event, &$bag, &$eventData, $addData = null)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
$hooks = &$bag->get('event_hooks');
2011-12-13 12:29:05 +01:00
$serendipity['plugin_dlm_version'] = &$bag->get('version');
2011-12-13 12:29:05 +01:00
if (isset($hooks[$event])) {
2011-12-13 12:29:05 +01:00
switch($event) {
case 'genpage':
if ($serendipity['rewrite'] != 'none') {
$nice_url = $serendipity['serendipityHTTPPath'] . $addData['uriargs'];
} else {
$nice_url = $serendipity['serendipityHTTPPath'] . $serendipity['indexFile'] . '?/' . $addData['uriargs'];
}
$oldsubpage = $serendipity['GET']['subpage'];
if (empty($serendipity['GET']['subpage'])) {
$serendipity['GET']['subpage'] = $nice_url;
}
if ($this->selected()) {
$serendipity['head_title'] = $this->get_config('pagetitle');
$serendipity['head_subtitle'] = (function_exists('serendipity_specialchars') ? serendipity_specialchars($serendipity['blogTitle']) : htmlspecialchars($serendipity['blogTitle'], ENT_COMPAT, LANG_CHARSET));
} else {
// Put subpage back so staticpage plugin will work
$serendipity['GET']['subpage'] = $oldsubpage;
}
break;
2011-12-13 12:29:05 +01:00
case 'entry_display' :
if ($this->selected()) {
// This is important to not display an entry list!
2011-12-13 12:29:05 +01:00
if (is_array($eventData)) {
$eventData['clean_page'] = true;
} else {
$eventData = array('clean_page' => true);
2011-12-13 12:29:05 +01:00
}
}
break;
case 'backend_sidebar_entries':
$this->setupDB();
// forbid entry if not admin
if (!serendipity_userLoggedIn() && $_SESSION['serendipityAuthedUser'] !== true && $_SESSION['serendipityUserlevel'] != '255') {
break;
}
if ($serendipity['version'][0] < 2) {
echo "\n".' <li class="serendipitySideBarMenuLink serendipitySideBarMenuEntryLinks"><a href="?serendipity[adminModule]=event_display&amp;serendipity[adminAction]=downloadmanager">' . PLUGIN_DOWNLOADMANAGER_TITLE . '</a></li>'."\n";
2011-12-13 12:29:05 +01:00
}
break;
case 'backend_sidebar_admin_appearance':
$this->setupDB();
// forbid entry if not admin
if (!serendipity_userLoggedIn() && $_SESSION['serendipityAuthedUser'] !== true && $_SESSION['serendipityUserlevel'] != '255') {
break;
}
if ($serendipity['version'][0] > 1) {
echo "\n".' <li><a href="?serendipity[adminModule]=event_display&amp;serendipity[adminAction]=downloadmanager">' . PLUGIN_DOWNLOADMANAGER_TITLE . '</a></li>'."\n";
2011-12-13 12:29:05 +01:00
}
break;
case 'backend_sidebar_entries_event_display_downloadmanager':
2011-12-13 12:29:05 +01:00
if (!is_object($serendipity['smarty'])) {
serendipity_smarty_init();
2011-12-13 12:29:05 +01:00
}
$this->ADMIN_showDownloads();
2011-12-13 12:29:05 +01:00
break;
case 'external_plugin':
$uri_parts = explode('?', str_replace('&amp;', '&', $eventData));
// Try to get request parameters from eventData name
if (!empty($uri_parts[1])) {
$reqs = explode('&', $uri_parts[1]);
foreach($reqs AS $id => $req) {
$val = explode('=', $req);
if (empty($_REQUEST[$val[0]])) {
$_REQUEST[$val[0]] = $val[1];
}
}
}
$parts = explode('_', $uri_parts[0]);
2011-12-13 12:29:05 +01:00
if (!empty($parts[1])) {
$param = (int) $parts[1];
2011-12-13 12:29:05 +01:00
} else {
$param = null;
2011-12-13 12:29:05 +01:00
}
switch($parts[0]) {
case 'dlfile':
$fileid = intval($parts[1]);
$q = "UPDATE {$serendipity['dbPrefix']}dma_downloadmanager_files
SET dlcount = dlcount+1
WHERE id = $fileid";
serendipity_db_query($q);
2011-12-13 12:29:05 +01:00
$sql = "SELECT *
FROM {$serendipity['dbPrefix']}dma_downloadmanager_files
WHERE id = $fileid";
2011-12-13 12:29:05 +01:00
$file = serendipity_db_query($sql);
$mime = $this->getMime($file[0]['realfilename']);
$contenttype = $mime['TYPE'];
$filename = $file[0]['realfilename'];
$filename = str_replace(' ', '_', $filename);
$path = $this->globs['dlmpath'];
$sysname = $file[0]['systemfilename'];
$filesize = $file[0]['filesize'];
if (function_exists("getallheaders")) {
$headers = getallheaders();
}
if (substr($headers["Range"], 0, 6) == "bytes=") {
header("HTTP/1.1 206 Partial Content");
header("Content-Type: $contenttype");
header("Content-Disposition: attachment; filename=".$filename);
header("Accept-Ranges: bytes");
header("Connection: close");
$bytes = explode("=",$headers["Range"]);
$bytes = $bytes[1];
if (preg_match("@^-([0-9]+)@", $bytes, $bytes_len)) {
$bytes_len = $bytes_len[1];
$bytes_start = $filesize - $bytes_len;
$bytes_end = $filesize - 1;
header("Content-Length: ".$bytes_len);
} elseif (preg_match("@([0-9]+)-$@", $bytes, $bytes_start)) {
$bytes_start = $bytes_start[1];
$bytes_end = $filesize - 1;
$bytes_len = $filesize - $bytes_start;
header("Content-Length: $bytes_len");
} elseif (preg_match("@^([0-9]+)-([0-9]+)$@", $bytes, $bytes_pos))
{
$bytes_start = $bytes_pos[0];
$bytes_end = $bytes_pos[1];
if ($bytes_start < 0 || $bytes_start > ($filesize - 1)) {
$bytes_start = 0;
}
if ($bytes_end < $bytes_start || $bytes_end > ($filesize - 1)) {
$bytes_end = $filesize - 1;
}
$bytes_len = $bytes_end - $bytes_start + 1;
header("Content-Length: $bytes_len");
} else {
$bytes_start = 0;
$bytes_end = $filesize - 1;
$bytes_len = $bytes_end - $bytes_start + 1;
header("Content-Length: $bytes_len");
}
header("Content-Range: bytes $bytes_start-$bytes_end/".$filesize);
$fp = fopen($path."/".$sysname,"rb");
fseek($fp, $bytes_start);
$contents = fread ($fp, $bytes_len );
fclose($fp);
echo $contents;
} else {
$fp = fopen($path."/".$sysname,"rb");
$contents = fread ($fp, $filesize);
fclose($fp);
header("Content-Type: $contenttype");
header("Content-Disposition: attachment; filename=".$filename);
header("Accept-Ranges: bytes");
header("Content-Length: " . strlen($contents));
header("Connection: close");
echo $contents;
}
break;
}
break;
/* put here all your css stuff you need for the downloadmanagers plugin frontend output */
case 'css':
if (stristr($eventData, '#downloadmanager')) {
2011-12-13 12:29:05 +01:00
// class exists in CSS, so a user has customized it and we don't need default
return true;
}
$dlm_css = '';
2011-12-13 12:29:05 +01:00
$tfile = serendipity_getTemplateFile('style_dlmanager_frontend.css', 'serendipityPath');
if ($tfile) {
$dlm_css = @file_get_contents($tfile);
}
if (!$tfile || $tfile == 'style_dlmanager_frontend.css') {
2011-12-13 12:29:05 +01:00
$tfile = dirname(__FILE__) . '/style_dlmanager_frontend.css';
$dlm_css = @file_get_contents($tfile);
2011-12-13 12:29:05 +01:00
}
$eventData .= $dlm_css; // append CSS
2011-12-13 12:29:05 +01:00
break;
2011-12-13 12:29:05 +01:00
/* put here all you css stuff you need for the backend of dlm */
case 'css_backend':
if (stristr($eventData, '#backend_downloadmanager')) {
2011-12-13 12:29:05 +01:00
// class exists in CSS, so a user has customized it and we don't need default
return true;
}
$tfile = serendipity_getTemplateFile('style_dlmanager_backend.css', 'serendipityPath');
if ($tfile) {
2011-12-13 12:29:05 +01:00
$tfilecontent = @file_get_contents($tfile);
}
if ( (!$tfile || $tfile == 'style_dlmanager_backend.css') && !$tfilecontent ) {
2011-12-13 12:29:05 +01:00
$tfile = dirname(__FILE__) . '/style_dlmanager_backend.css';
$tfilecontent = @file_get_contents($tfile);
}
if (!empty($tfilecontent) && $serendipity['version'][0] < 2) {
$tfilecontent .= '
#dlm_messages {
margin: 16px 0;
padding: 4px;
text-align: center;
}
';
}
2011-12-13 12:29:05 +01:00
// add replaced css content to the end of serendipity_admin.css
if (!empty($tfilecontent)) $this->backend_dlm_css($eventData, $tfilecontent);
2011-12-13 12:29:05 +01:00
break;
case 'entries_header' :
//this shows our page and not an empty one
$this->show();
break;
default:
return false;
}
return true;
} else {
return false;
}
2011-12-13 12:29:05 +01:00
}
/***************************************************
* Backend administration functions
**************************************************/
/* add backend css to serendipity_admin.css */
function backend_dlm_css(&$eventData, &$becss)
{
2011-12-13 12:29:05 +01:00
$eventData .= $becss;
}
/**
* helptip array
*
* HELPTIP_CF = (sub)category folder;
* HELPTIP_FF = file folder;
* HELPTIP_IF = incoming ftp/trash folder;
2011-12-13 12:29:05 +01:00
* HELPTIP_MF = s9y media library folder;
*/
function backend_dlm_helptip()
{
2011-12-13 12:29:05 +01:00
return array(
'hlp[0]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_CF_START,
'hlp[1]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_CF_CHANGE,
2011-12-13 12:29:05 +01:00
'hlp[2]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_FF_CHANGE,
'hlp[3]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_IF_VIEW,
'hlp[4]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_IF_ERASE,
'hlp[5]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_IF_TRASH,
'hlp[6]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_FF_MULTI,
'hlp[7]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_FF_SINGLE,
'hlp[8]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_FF_KEEP,
'hlp[9]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_IF_MOVE,
'hlp[10]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_IF_LFTP,
'hlp[11]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_DESC
/*
'hlp[3]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_IF_S9ML,
2011-12-13 12:29:05 +01:00
'hlp[]' => PLUGIN_DOWNLOADMANAGER_HELPTIP_,
*/
);
}
2011-12-13 12:29:05 +01:00
/**
* refresh a page to show correct values directly after move, erase, clean etc (mostly done on page 2)
* order by header(), Javascript, HTML (meta refresh)
*
2011-12-13 12:29:05 +01:00
* @param string request url
2011-12-13 12:29:05 +01:00
* @return false exit page
*/
function backend_dlm_refresh($url)
{
if ($url && !headers_sent()) {
if (header('Location: http://' . $_SERVER['HTTP_HOST'] . $url)) exit;
} else {
2011-12-13 12:29:05 +01:00
echo '<script type="text/javascript">';
echo ' window.location.href="' . $url . '&viajs=1"';
echo '</script>';
echo '<noscript>';
echo ' <meta http-equiv="refresh" content="0;url=' . $url . '&viameta=1" />';
echo '</noscript>';
exit;
}
}
2011-12-13 12:29:05 +01:00
/**
* fetch dlm backend pathfiles table content
*
* @param string The path to iterate
* @param boolean If path is Serendipity MediaLibrary (default false)
2011-12-13 12:29:05 +01:00
*
* @return array
*/
function backend_dlm_fetch_pathfiles($path, $ml=false)
{
$fa = array();
$fa['d_arr'] = array();
$fa['f_arr'] = array();
$d = 0;
$f = 0;
if (!is_dir($path)) return;
try {
$_dir = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
// NOTE: UnexpectedValueException thrown for PHP >= 5.3
} catch (Exception $e) {
echo "Error: " . $e->getMessage();#return;
}
if ($ml) {
$iterator = new RecursiveIteratorIterator($_dir, RecursiveIteratorIterator::SELF_FIRST);
} else {
$iterator = new RecursiveIteratorIterator($_dir, RecursiveIteratorIterator::CHILD_FIRST);
}
foreach ($iterator AS $file) {
$_bfname = $this->mb_basename($file);
$_mdepth = $ml ? $iterator->getDepth() : 0; // allows to avoid recursive file iteration in ML since we have the dir structure to navigate
if ($file->isFile() && $_mdepth == 0 && $_bfname != '.empty' && (false === (strpos($_bfname, '.serendipityThumb')))) {
$filename = $this->encodeToUTF($file->getPathname(), true); // OK
$fa['f_arr'][++$f] = str_replace("\\", "/", $filename);
} else {
// do we need to do this encoding stuff for dir path too?
if ($file->isDir()) {
$fa['d_arr'][++$d] = str_replace("\\", "/", $file->__toString());
2011-12-13 12:29:05 +01:00
}
}
}
#echo '<pre>';debug_print_backtrace();echo '</pre>';
#echo '<pre>' . print_r($fa, true) . '</pre>';
2011-12-13 12:29:05 +01:00
return $fa;
}
2011-12-13 12:29:05 +01:00
/**
* recursive str_replaces in files array, happens to special keys only, used by smarty files array to set filesize, mime array and filedate
*
2011-12-13 12:29:05 +01:00
* @param array the array data
* @param string/boolean use with date() replacement funktion too
* @param string/boolean search in specific key only = string (optional)
* @param string/boolean new keyname, use with different replacement funktion only (optional)
*
2011-12-13 12:29:05 +01:00
* @return array
**/
function backend_str_replace_recursive(&$data, $p=false, $skey=false, $nkey=false)
{
if (is_array($data)) {
foreach($data AS $key => $value) {
if (is_array($value) ) {
2011-12-13 12:29:05 +01:00
$this->backend_str_replace_recursive($data[$key], $p, $skey, $nkey);
} else {
if ($key == $skey) {
if (!$nkey) $data[$key] = ($p ? date($p, $value) : $this->calcFilesize($value));
2011-12-13 12:29:05 +01:00
elseif ($p === false) $data[$nkey] = $this->getMime($value);
else $data[$nkey] = ($p ? date($p, $value) : $this->calcFilesize($value));
}
}
}
}
2011-12-13 12:29:05 +01:00
return isset($data) ? $data : false;
}
/* see function backend_str_replace_recursive() - this one is for categories only */
function backend_str_replace_recursive_cat(&$data, $p=false, $skey=false, $nkey=false)
{
if (is_array($data)) {
foreach($data AS $key => $value) {
if (is_array($value) ) {
2011-12-13 12:29:05 +01:00
$this->backend_str_replace_recursive_cat($data[$key], $p, $skey, $nkey);
} else {
if ($key === $skey) $data[$nkey] = (550 - (20 * $value));
2011-12-13 12:29:05 +01:00
}
}
}
2011-12-13 12:29:05 +01:00
return isset($data) ? $data : false;
}
/**
* generate and assign backend appendix trash & helptip
2011-12-13 12:29:05 +01:00
*
* @param string The configs absincomepath
* @param int The category id number
2011-12-13 12:29:05 +01:00
* @param int The div number (standard=3)
* @param int The page number
*
* @return string
*/
function backend_dlm_appendix($absinth='', $cn=1, $dn=0, $pn=0)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
// fetch all physically files in incoming ftp or trash table
$ifiles = $this->backend_dlm_fetch_pathfiles($absinth); // is 3rd and last run in workflow
2011-12-13 12:29:05 +01:00
$ifn = count($ifiles['f_arr']);
unset($ifiles);
// assign the backend appendix vars to smarty template page section 'appendix'
$serendipity['smarty']->assign('dlmapx',
2011-12-13 12:29:05 +01:00
array(
'appendix' => true,
'cleanme' => ($ifn >= 1) ? true : false,
'helplist' => $this->backend_dlm_helptip()
)
);
// view all smarty template vars
#echo '<pre>' . print_r( $serendipity['smarty']->get_template_vars(), true ) . '</pre>';
2011-12-13 12:29:05 +01:00
return;
}
/**
2011-12-13 12:29:05 +01:00
* build the dlm backend add categories table
*
* @param array A referenced array of categories
* @param int The div number (standard=3)
* @param int The page number
*
* @return string
*/
function backend_dlm_add_categories($cats, $dn=0, $pn=0)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
// build the category list in backend => true and as <select> call list => true
$catlist = $this->buildCategoriesList(true, true);
2011-12-13 12:29:05 +01:00
// assign the backend addcat vars to smarty template page section 'addcat'
$serendipity['smarty']->assign('dlmact',
2011-12-13 12:29:05 +01:00
array(
'addcat' => true,
'selcatlist' => $catlist
)
);
unset($catlist);
return;
}
/**
2011-12-13 12:29:05 +01:00
* build the dlm backend categories table content
*
* @param array A referenced array of categories
* @param boolean Default value foldable divs (permanently open = true)
2011-12-13 12:29:05 +01:00
* @param string The configs absincomepath
* @param int The category id number
2011-12-13 12:29:05 +01:00
* @param int The div number (standard=3)
* @param int The page number
*
* @return string
*/
function backend_dlm_build_categories($cats, $ddiv=false, $absinth='', $cn=1, $dn=0, $pn=0)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
2011-12-13 12:29:05 +01:00
if (is_array($cats) && sizeof($cats) >= 1) {
$cc = is_array($cats) ? count($cats)-1 : 0; // else root level would be counted too
// build the category list in backend => true and as <select> call list => false
$catlist = $this->buildCategoriesList(true, false);
2011-12-13 12:29:05 +01:00
// set value of key filesize to something readable
$catlist = $this->backend_str_replace_recursive_cat($catlist, false, 'level', 'inputsize');
2011-12-13 12:29:05 +01:00
// assign the backend category vars to smarty template page section 'hascats'
$serendipity['smarty']->assign('dlmhcs',
2011-12-13 12:29:05 +01:00
array(
'hascats' => true,
'ddiv' => $ddiv,
'catlist' => $catlist,
'catsinccat' => $cc,
'cn' => $cn
)
);
}
2011-12-13 12:29:05 +01:00
unset($cats);
unset($catlist);
return;
}
/**
2011-12-13 12:29:05 +01:00
* build dlm backend file table content
*
* @param array A referenced array of files
* @param boolean Default value foldable divs (permanently open = true)
2011-12-13 12:29:05 +01:00
* @param string The configs absincomepath
* @param string The configs absdownloadspath
* @param string The configs Dateformat
* @param int This dirs total file count number
* @param int The category id number
2011-12-13 12:29:05 +01:00
* @param int The div number
* @param int The page number
*
* @return string
*/
function backend_dlm_build_filetable($files, $ddiv=false, $absinth='', $absdoth='', $dateformat='Y-m-d H:i', $fn=0, $catid=1, $dn=0, $pn=0)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
2011-12-13 12:29:05 +01:00
$moved = false;
/* reject multiple files being marked to erase */
if (isset($_POST['Reject_Selected']) || isset($_POST['Reject_Selected_x']) || isset($_POST['Reject_Selected_y'])) {
if (is_array($_POST['dlm']['files'])) {
2011-12-13 12:29:05 +01:00
// build new array - fetch file names by id
$dfile = array();
foreach ($_POST['dlm']['files'] AS $k => $v) {
foreach($files AS $file) {
if ($file['id'] == $v) {
$file['realfilename'] = $this->encodeToUTF($file['realfilename']); // OK
// build the correct path from($this->globs['dlmpath']) -> to($this->globs['ftppath']) by new array
$dfile[] = array( 'id' => $file['id'],
'cat' => $file['catid'],
'sfn' => $absdoth.'/'.$file['systemfilename'],
2011-12-13 12:29:05 +01:00
'rfn' => $absinth.'/'.$file['realfilename']
);
}
}
}
if ( is_array($dfile) && !empty($dfile) ) {
2011-12-13 12:29:05 +01:00
$realcatid = $dfile[0]['cat'];
foreach($dfile AS $movit) {
2011-12-13 12:29:05 +01:00
// This does not rename the file, as you might assume, instead, it moves the file physically!
if (!@rename ($movit['sfn'], $movit['rfn'])) {
2011-12-13 12:29:05 +01:00
$this->ERRMSG(PLUGIN_DOWNLOADMANAGER_DELETE_IN_DOWNLOADDIR_NOT_ALLOWED);
} else {
2011-12-13 12:29:05 +01:00
$result = $this->dlm_sql_db('DLM_BE_DELETE_FILE', "id = ".$movit['id']);
}
}
if ($result) {
2011-12-13 12:29:05 +01:00
unset($movit);
$moved = true;
}
}
}
}
if ($moved === true) {
2011-12-13 12:29:05 +01:00
$url = $_SERVER['PHP_SELF'] . '?serendipity[adminModule]=event_display&serendipity[adminAction]=downloadmanager&thiscat=' . ($realcatid ? $realcatid : $catid) . '&dlmftpdir=1';
if (is_array($dfile)) unset($dfile);
if (is_array($_POST)) unset($_POST);
2011-12-13 12:29:05 +01:00
$this->backend_dlm_refresh($url);
}
if (is_array($files) && !empty($files)) {
2011-12-13 12:29:05 +01:00
// set value of key filesize to something readable
$files = $this->backend_str_replace_recursive($files, false, 'filesize');
2011-12-13 12:29:05 +01:00
// set value of key timestamp as new key/value pair to something readable defined by our config setting $this->get_config('dateformat')
$files = $this->backend_str_replace_recursive($files, $dateformat, 'timestamp', 'filedate');
2011-12-13 12:29:05 +01:00
// set value of key realfilename as new key/value pair to collect mime values as array by file
$files = $this->backend_str_replace_recursive($files, false, 'realfilename', 'mime');
2011-12-13 12:29:05 +01:00
}
// assign the backend filetable vars to smarty template page section 'catfiles'
$serendipity['smarty']->assign('dlmcfs',
2011-12-13 12:29:05 +01:00
array(
'catfiles' => true,
'ddiv' => $ddiv,
'filelist' => $files,
'downloadpath' => $serendipity['baseURL'] . ($serendipity['rewrite'] == 'none' ? $serendipity['indexFile'] . '?/' : '') . 'plugin/dlfile_'
2011-12-13 12:29:05 +01:00
)
);
return;
}
/**
2011-12-13 12:29:05 +01:00
* build dlm backend s9y media gallery file table content
*
* @param string The configs Dateformat
* @param string The path to the s9y media library
* @param int The div number
* @param int The page number
*
* @return string
*/
function backend_dlm_build_s9ml_table($dateformat='Y-m-d H:i', $path='', $dn=0, $pn=0)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
2011-12-13 12:29:05 +01:00
if (isset($_GET['smlpath']) && trim($_GET['smlpath']) != '' && !preg_match("@\.\./@", $_GET['smlpath'])) {
$extrapath = trim($_GET['smlpath']);
} else {
$extrapath = '';
}
if ($path[(strlen($path)-1)] == "/") {
2011-12-13 12:29:05 +01:00
$path = substr($path, 0, (strlen($path)-1));
}
$path .= str_replace("\\", "/", $extrapath);
2011-12-13 12:29:05 +01:00
$files = array();
// fetch all allowed physically files and dirs in current category
$files = $this->backend_dlm_fetch_pathfiles($path, true); // only fetch files by directory - is 2cd in workflow
2011-12-13 12:29:05 +01:00
if (count($files['d_arr']) <= 0 && count($files['f_arr']) <= 0) {
$sml_arr = false;
} else {
$sml_arr = true;
if (!empty($extrapath)) {
$backpath = preg_replace("`\/[^\/]*$`i", '', $extrapath);
2011-12-13 12:29:05 +01:00
}
foreach($files['d_arr'] AS $key => $val) {
2011-12-13 12:29:05 +01:00
$smldirs[] = array(
'filename' => str_replace($serendipity['serendipityPath'] . $serendipity['uploadPath'], '', $val),
2011-12-13 12:29:05 +01:00
'expath' => $extrapath
);
}
foreach($files['f_arr'] AS $key => $val) {
$mime = $this->getMime($val);
$filedate = date($dateformat, filemtime($val));
$filesize = filesize($val);
$filesize = $this->calcFilesize($filesize);
$smlfiles[] = array(
'filename' => str_replace($serendipity['serendipityPath'] . $serendipity['uploadPath'], '', $val),
'filesize' => $filesize,
'filedate' => $filedate,
'filemime' => $mime,
'expath' => $extrapath
);
2011-12-13 12:29:05 +01:00
}
}
// assign as array('sml') the S9y media library files to smarty tpl page 2 'thissml' section
$serendipity['smarty']->assign('dlmtsl',
2011-12-13 12:29:05 +01:00
array(
'thissml' => true,
'ddiv' => (isset($_GET['smlpath']) && !empty($_GET['smlpath'])) ? true : false,
'smlpath' => !empty($path) ? $path : $extrapath,
'smlfiles' => $smlfiles,
'issmlarr' => $sml_arr,
'smldirs' => $smldirs,
'extrapath' => $extrapath,
'backpath' => $backpath
)
);
return;
}
2011-12-13 12:29:05 +01:00
/**
* generate page 2 incoming folder table content here - DLM 2 @ PAGE 2
*
* @param boolean Default value foldable divs (permanently open = true)
2011-12-13 12:29:05 +01:00
* @param string The full path incoming FTP folder
* @param int The category id number
* @param int The div number
* @param int The page number
*
* @return string
**/
function backend_dlm_build_ftptable($ddiv=false, $absinth='', $catid=1, $dn=0, $pn=0)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
// fetch all physically files in incoming ftp or trash table
$files = $this->backend_dlm_fetch_pathfiles($absinth); // is first run in workflow
2011-12-13 12:29:05 +01:00
$fn = count($files['f_arr']);
$ct = ($fn >= 1) ? true : false;
/* clean trash = incoming folder by blue trash box */
if ($ct && (intval($_GET['cleantrash']) == 1 && intval($_POST['dlm']['cleartrash']) == 1)
&& !(isset($_POST['Move_Selected']) || isset($_POST['Move_Selected_x']) || isset($_POST['Move_Selected_y'])) ) {
foreach($files['f_arr'] AS $file) {
$ctfile = $this->delIncomingFile($file); // already as fullpath
#$ctfile = true;
2011-12-13 12:29:05 +01:00
}
if ($ctfile === true) {
if (is_array($_POST)) unset($_POST);
if (is_array($files)) unset($files);
2011-12-13 12:29:05 +01:00
$url = $_SERVER['PHP_SELF'] . '?serendipity[adminModule]=event_display&serendipity[adminAction]=downloadmanager&thiscat=' . $catid;
$this->backend_dlm_refresh($url);
}
}
if ($ctfile === true && is_array($files)) unset($files); // when does this ever happen?
2011-12-13 12:29:05 +01:00
/* move multiple files being marked to move to a new directory */
if ( isset($_POST['Move_Selected']) || isset($_POST['Move_Selected_x']) || isset($_POST['Move_Selected_y']) ) {
if (is_array($_POST['dlm']['ifiles']) && !empty($_POST['dlm']['ifiles'])) {
foreach ($_POST['dlm']['ifiles'] AS $ifile) {
2011-12-13 12:29:05 +01:00
$this->importFile($ifile, $catid);
}
// remove possible empty directories
$this->delEmptyDir();
2011-12-13 12:29:05 +01:00
}
if (is_array($_POST)) unset($_POST);
2011-12-13 12:29:05 +01:00
$url = $_SERVER['PHP_SELF'] . '?serendipity[adminModule]=event_display&serendipity[adminAction]=downloadmanager&thiscat=' . $catid;
$this->backend_dlm_refresh($url);
}
foreach($files['f_arr'] AS $key => $val) {
$mime = $this->getMime($val);
$_val = $this->encodeToUTF($val); // OK win decode for stats
// otherwise thrown Warning: filemtime(): and filesize(): stat failed warning on LINUX
// are thrown for the lowered permissions - which is actually want we want in ftpdir
$filedate = date($this->globs['dateformat'], @filemtime($_val));
$filesize = @filesize($_val);
$filesize = $this->calcFilesize($filesize);
#$val = serendipity_specialchars($val);
$files[] = array(
'filename' => str_replace($absinth.'/', '', $val),
'filesize' => $filesize,
'filedate' => $filedate,
'filemime' => $mime
);
2011-12-13 12:29:05 +01:00
}
2011-12-13 12:29:05 +01:00
// assign the backend ftp/trash vars to smarty template page section 'thisftp'
$serendipity['smarty']->assign('dlmtfp',
2011-12-13 12:29:05 +01:00
array(
'thisftp' => true,
'ddiv' => $ddiv,
'ftpfiles' => $files,
'ct' => $ct,
'movedtoftp' => $ddiv,
'ftppath' => $absinth
)
);
return;
}
2011-12-13 12:29:05 +01:00
/**
* build upload form
*/
function backend_dlm_build_uploadform($id)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
2011-12-13 12:29:05 +01:00
$upload_max_filesize = ini_get('upload_max_filesize');
$upload_max_filesize = preg_replace('/M/', '000000', $upload_max_filesize);
$MAX_FILE_SIZE = intval($upload_max_filesize);
$MAX_SIZE_PER_FILE = ($MAX_FILE_SIZE / 1000000)." MB";
// assign the backend uploadform vars to smarty template page section 'uploadform'
$serendipity['smarty']->assign('dlmulf',
2011-12-13 12:29:05 +01:00
array(
'thistype' => 'uploadform',
'file_uploads' => ini_get('file_uploads'),
'MAX_FILE_SIZE' => $MAX_FILE_SIZE,
'MAX_SIZE_PER_FILE' => ($MAX_FILE_SIZE / 1000000)." MB"
)
);
return;
}
/**
2011-12-13 12:29:05 +01:00
* build dlm backend file table content
*
* @param array A referenced array of this category
* @param int The single file id number
* @param int The single file catid number
*
* @return string
*/
function backend_dlm_edit_file($cat, $id, $catid)
{
2011-12-13 12:29:05 +01:00
global $serendipity;
2011-12-13 12:29:05 +01:00
// get all specific information about file
$file = $this->dlm_sql_db('DLM_SELECT', "id = $id");
$mime = $this->getMime($file['realfilename']);
2011-12-13 12:29:05 +01:00
// build the category list in backend => true and as <select> call list => true
$catlist = $this->buildCategoriesList(true, true);
2011-12-13 12:29:05 +01:00
// assign the backend editfile vars to smarty template page section 'editfile'
$serendipity['smarty']->assign('dlmefe',
2011-12-13 12:29:05 +01:00
array(
'thistype' => 'editfile',
'description' => $file['description'],
'realfilename' => $file['realfilename'],
'fileid' => $id,
'mime' => $mime,
'catid' => $catid,
'selcatlist' => $catlist
)
);
unset($catlist);
return;
}
2011-12-13 12:29:05 +01:00
}
/* vim: set sts=4 ts=4 expandtab : */
?>