# PLUGIN PREVIEW BY TEXTPATTERN.INFO
/*
adi_file_tab - Enhanced file tab
Written by Adi Gilbert
Released under the GNU Lesser General Public License
Version history:
0.3 - updated for TXP 4.6 (again)
- enhancement: author edit (4.5.7+ only)
0.2 - fixes for 4.6 (thanks jpdupont)
- enhancement: plugin privs
0.1 - initial release
*/
/* TODO
- strip out TXP 4.5 code
- tidy up styles
*/
if (@txpinterface == 'admin') {
global $event,$step,$adi_file_tab_debug,$adi_file_tab_message,$filename;
$adi_file_tab_debug = 0;
// bored with anything pre-4.5
if (!version_compare(txp_version,'4.5.0','>=')) return;
adi_file_tab_init($event,$step);
}
function adi_file_tab_init($event,$step) {
global $textarray,$step,$prefs,$adi_file_tab_debug,$adi_file_tab_url,$adi_file_tab_prefs,$adi_file_tab_outlaws,$adi_file_tab_admin_outlaws,$adi_file_tab_message,$adi_file_tab_txp460,$adi_file_tab_plugin_status;
// holy crap TXP 4.6!
$adi_file_tab_txp460 = (version_compare(txp_version,'4.6-dev','>='));
// preferences & defaults
$adi_file_tab_prefs = array(
'adi_file_tab_privs' => array('value' => '1,2,3,4,5,6', 'blank' => 1, 'input' => 'custom', 'label' => 'adi_plugin_privs', 'tab' => 'options'), // allowed to use plugin
'adi_file_tab_replace_default' => array('value' => '1', 'blank' => 0, 'input' => 'yesnoradio', 'label' => 'adi_file_tab_replace_default', 'tab' => 'options'),
'adi_file_tab_exclude_filter' => array('value' => '.sql', 'blank' => 1, 'input' => 'text_input', 'label' => 'adi_file_tab_exclude_filter', 'tab' => 'options'),
);
// privs to be excluded (list of group ids)
$adi_file_tab_outlaws = array(0);
$adi_file_tab_admin_outlaws = array(0);
// Textpack
$adi_file_tab_url = array(
'textpack' => 'http://www.greatoceanmedia.com.au/files/adi_textpack.txt',
'textpack_download' => 'http://www.greatoceanmedia.com.au/textpack/download',
'textpack_feedback' => 'http://www.greatoceanmedia.com.au/textpack/?plugin=adi_file_tab',
);
if (strpos($prefs['plugin_cache_dir'],'adi') !== FALSE) // use Adi's local version
$adi_file_tab_url['textpack'] = $prefs['plugin_cache_dir'].'/adi_textpack.txt';
# --- BEGIN PLUGIN TEXTPACK ---
$adi_file_tab_gtxt = array(
'adi_auto_update_filename' => 'Automatically update filename',
'adi_copy_filename_title' => 'Copy from filename',
'adi_copy_title_description' => 'Copy from title',
'adi_copy_update_filename_uploaded' => 'Update filename to match uploaded file?', // typo in string_name!
'adi_delete_file' => 'Delete file',
'adi_file_details' => 'File details',
'adi_file_exists_dir' => 'File <strong>{name}</strong> already exists in {dir}',
'adi_file_renamed' => 'File renamed',
'adi_file_rename_fail' => 'File rename failed',
'adi_file_replaced' => 'File replaced',
'adi_file_tab_confirm_update_filename' => 'Update filename?',
'adi_file_tab_exclude_filter' => 'Exclude filename filter',
'adi_file_tab_replace_default' => 'Filename update selected by default?',
'adi_filtered' => 'Filtered',
'adi_name_is_dir' => '<strong>{name}</strong> is a directory',
'adi_plugin_privs' => 'Plugin privileges:',
'adi_rename_file' => 'Rename file',
'adi_rename' => 'Rename',
'adi_replace_from_existing' => "Replace from existing file",
'adi_reset_downloads' => 'Reset download count',
'adi_textpack_fail' => 'Textpack installation failed',
'adi_textpack_feedback' => 'Textpack feedback',
'adi_textpack_online' => 'Textpack also available online',
'adi_timestamp_invalid' => 'Invalid timestamp',
'adi_unfiltered' => 'Unfiltered',
'adi_update_filename' => 'Update filename',
'adi_update_prefs' => 'Update preferences',
);
# --- END PLUGIN TEXTPACK ---
// update $textarray
$textarray += $adi_file_tab_gtxt;
// plugin lifecycle
register_callback('adi_file_tab_lifecycle','plugin_lifecycle.adi_file_tab');
// who gets to play?
add_privs('adi_file_tab',adi_file_tab_pref('adi_file_tab_privs'));
if (has_privs('adi_file_tab')) {
// lethal injection - TXP 4.5
if (($event == 'file') && !$adi_file_tab_txp460)
ob_start('adi_file_tab_inject_details');
// callbacks for modern times
register_callback('adi_file_tab','admin_side','body_end');
register_callback('adi_file_tab_extend_detail','file_ui','extend_detail_form');
// style
if (($event == 'file') && ($step)) // not for file list tab
register_callback('adi_file_tab_style','admin_side','head_end');
// scripts
if ($event == 'file')
register_callback('adi_file_tab_general_script','admin_side','body_end');
if (($event == 'file') && (($step == 'file_edit') || ($step == 'file_insert') || ($step == 'file_replace')))
register_callback('adi_file_tab_edit_script','admin_side','body_end');
// get file ID (irrelevant in file list tab)
if ($event == 'file')
$id = adi_file_tab_id();
// new operations
if (($event == 'file') && ($step == 'file_save')) {
// modify file downloads count
if (gps('adi_file_tab_downloads') != '')
adi_file_tab_update_downloads(adi_file_tab_id(),gps('adi_file_tab_downloads'));
// modify author (THIS DOESN'T WORK IN 4.5.5 COZ TXP FILE SAVE OVERWRITES FILE AUTHOR WITH LOGGED IN USER - FIXED IN 4.5.7)
if (gps('adi_file_tab_author') != '')
adi_file_tab_update_author(adi_file_tab_id(),gps('adi_file_tab_author'));
}
// update filename - interfere with standard file replace operation
if (gps('adi_file_tab_update_filename')) { // update filename option selected
if ($_FILES) // there's been a file upload
if ($_FILES['thefile']['error'])
$adi_file_tab_message = array();
else { // automatic filename update during standard file replace
// uploaded file
$new_filename = '';
$new_filename = $_FILES['thefile']['name'];
$adi_file_tab_message = adi_file_tab_rename_file($id,$new_filename);
}
else { // last chance filename update
$new_filename = gps('adi_file_tab_new_filename');
$adi_file_tab_message = adi_file_tab_rename_file($id,$new_filename);
}
if (count($adi_file_tab_message) > 1) // there's a problem, so return user to file edit page
$step = 'file_edit';
}
// rename file - new operation
if (gps('adi_file_tab_rename_file')) { // update filename option selected
$adi_file_tab_new_filename = gps('adi_file_tab_new_filename');
$adi_file_tab_message = adi_file_tab_rename_file($id,$adi_file_tab_new_filename);
}
// replace from existing file - new operation
if (gps('adi_file_tab_replace_from_existing')) { // update filename option selected
$adi_file_tab_new_filename = gps('filename');
$adi_file_tab_message = adi_file_tab_replace_file($id,$adi_file_tab_new_filename);
}
}
// plugin options
$adi_file_tab_plugin_status = fetch('status','txp_plugin','name','adi_file_tab',$adi_file_tab_debug);
if ($adi_file_tab_plugin_status) { // proper install - options under Plugins tab
add_privs('plugin_prefs.adi_file_tab','1,2,6');
register_callback('adi_file_tab_options','plugin_prefs.adi_file_tab');
}
else { // txpdev - options under Extensions tab
add_privs('adi_file_tab_options','1,2,6');
register_tab('extensions','adi_file_tab_options','adi_file_tab options');
register_callback('adi_file_tab_options','adi_file_tab_options');
}
}
function adi_file_tab_style() {
// some style action
global $prefs;
echo
'<style type="text/css">
/* adi_file_tab - general style */
/* new file heading */
.adi_file_tab_heading { margin:0; font-weight:bold }
.adi_file_tab_details { margin-top:0; padding-bottom:0.2em; border-bottom:0.1em solid #111 }
.adi_file_tab_details span { text-transform:lowercase }
/* replace file section (& upload file if MISSING) */
.replace-file, .upload-file { padding-bottom:1em; border-bottom:0.1em solid #ccc }
.replace-file h3 { margin-bottom:0.5em }
.replace-file a.pophelp { display:none } /* lose misleading popup help button */
.edit-file-replace { margin:0 }
/* auto update filename checkbox */
input#adi_file_tab_update_filename { margin-left:0 }
/* last-chance filename update button */
.adi_file_tab_update_filename_button { clear:both; overflow:hidden }
.adi_file_tab_update_filename_button p { float:left; margin-bottom:0 }
.adi_file_tab_update_filename_button form { float:right; margin-top:0 }
/* replace from existing file */
#adi_file_tab_replace_from_existing { overflow:hidden; padding-bottom:1em; margin-top:1em; border-bottom:0.1em solid #ccc }
#adi_file_tab_replace_from_existing h3 { margin:0 }
#adi_file_tab_replace_from_existing p.existing-file { float:left; margin-top:0 }
#adi_file_tab_replace_from_existing p.existing-file + p { margin-bottom:0 }
#adi_file_tab_replace_from_existing select { margin:0 0 0 1em }
#adi_file_tab_replace_from_existing input.smallerbox { float:right }
/* rename file section */
.summary-details { margin-bottom:1em }
#adi_file_tab_rename_file { overflow:hidden; padding-bottom:1em; border-bottom:0.1em solid #ccc }
#adi_file_tab_rename_file span.edit-value { float:left; width:auto }
#adi_file_tab_rename_file span.edit-value input { margin:0 0 0 1em; width:26em }
#adi_file_tab_rename_file input.smallerbox { float:right }
#adi_file_tab_rename_file p { margin-bottom:0 }
/* delete file section */
#adi_file_tab_delete_file { margin:1em 0; border-bottom:0.1em solid #ccc }
#adi_file_tab_delete_file h3 { float:left; margin:0 }
#adi_file_tab_delete_file p { text-align:right }
/* file details area */
div.adi_file_tab_details_heading { border-bottom:0 }
p.edit-file-description { margin-bottom:0 }
/* copy filename to title checkbox */
p.adi_file_tab_copy_file_title { float:right; margin:0.5em 0 0; text-align:right }
/* copy title to description checkbox */
label.adi_file_tab_copy_title_desc { float:right; margin-top:0.3em }
/* file created heading */
h3#file_created-label { margin-top:0 }
/* rearrange date/time inputs (TXP 4.5 only) */
p.edit-file-publish-now input { margin-left:0 }
p.edit-file-published { float:left; margin-top:0 }
p.edit-file-created { float:left; margin-top:0; margin-left:1em }
section#file_created { overflow:hidden } /* TXP 4.6 */
/* downloads input */
p.adi_file_tab_downloads { margin-bottom:0 }
p.adi_file_tab_downloads input { width:4em }
p.adi_file_tab_downloads span.edit-label { width:auto; margin:1em 0 }
p.adi_file_tab_downloads span.edit-value { float:left; margin:1em 0 1em 1em; width:auto }
/* author input */
p.adi_file_tab_author select { margin:0 0 0 1em }
/* TXP 4.6 */
div.file-uploader { margin-bottom:0 } /* auto update filename checkbox */
div.file-uploader label { font-size:1.15em; font-weight:bold } /* make label look like other h3 */
div.file-uploader input[type=submit] { float:right } /* line up standard upload button with others */
form.file-detail p.adi_file_tab_copy_file_title { float:none; margin:0 }
form.file-detail div.edit-file-title { margin-bottom:0 }
div.edit-file-description { overflow:hidden }
p.adi_file_tab_file_info { margin:0 }
.file-uploader .adi_file_tab_update_filename { outline:1px solid red;}
';
if ($prefs['theme_name'] == 'hive')
echo '
/* adi_file_tab - Hive style */
#adi_file_tab_delete_file h3 { float:none }
#adi_file_tab_rename_file span.edit-value { margin-left:1em }
#adi_file_tab_rename_file input.smallerbox { margin-right:1em }
';
echo '</style>'.n;
}
function adi_file_tab_lifecycle($event,$step) {
// a matter of life & death
// $event: "plugin_lifecycle.adi_file_tab"
// $step: "installed", "enabled", disabled", "deleted"
// install/reinstall: "installed"; make active: "enabled"; make inactive: "disabled"; delete: "disabled" then "deleted"
// (as of TXP 4.5, reinstall only triggers "installed" event)
// adi_lifecycle: "installed" -> install() regardless, upgrade() if required; "deleted" -> uninstall(); "enabled" & disabled" -> do nothing
global $adi_file_tab_debug;
$result = $upgrade_required = '?';
if ($step == 'installed') {
// $result = adi_file_tab_install();
$result = TRUE;
$upgrade_required = adi_file_tab_upgrade();
if ($upgrade_required)
$result &= adi_file_tab_upgrade(TRUE);
}
if ($step == 'deleted')
$result = adi_file_tab_uninstall();
if ($adi_file_tab_debug)
echo "event=$event,step=$step,result=$result,upgrade_required=$upgrade_required";
}
function adi_file_tab_upgrade($do_upgrade=FALSE) {
// check/perform upgrade
global $adi_file_tab_debug;
// these prefs should be non-user (i.e. not private)
$pref1_found = safe_row('*','txp_prefs',"name ='adi_file_tab_replace_default' AND user_name != ''",$adi_file_tab_debug);
$pref2_found = safe_row('*','txp_prefs',"name ='adi_file_tab_exclude_filter' AND user_name != ''",$adi_file_tab_debug);
$upgrade_required = $pref1_found || $pref2_found;
if ($do_upgrade) {
if ($upgrade_required) {
$res = TRUE;
// repair prefs (lose this code if prefs get privatised in future)
if ($pref1_found) { // "user" pref found
$value = adi_file_tab_pref('adi_file_tab_replace_default'); // get current value (i.e. current user or current global or default - in that order)
$res &= safe_delete('txp_prefs',"name = 'adi_file_tab_replace_default'",$adi_file_tab_debug); // delete pref
$res &= adi_file_tab_pref('adi_file_tab_replace_default',$value); // re-save pref as non-user
}
if ($pref2_found) {
$value = adi_file_tab_pref('adi_file_tab_exclude_filter');
$res &= safe_delete('txp_prefs',"name = 'adi_file_tab_exclude_filter'",$adi_file_tab_debug);
$res &= adi_file_tab_pref('adi_file_tab_exclude_filter',$value);
}
return $res;
}
else
return TRUE;
}
else
return $upgrade_required;
}
function adi_file_tab_uninstall() {
// uninstall adi_file_tab
global $adi_file_tab_debug;
// delete preferences
$res = safe_delete('txp_prefs',"name LIKE 'adi_file_tab_%'",$adi_file_tab_debug);
return $res;
}
function adi_file_tab_pref($name,$value=NULL,$private=FALSE) {
// read or set pref
global $prefs,$adi_file_tab_prefs;
if ($value === NULL)
return get_pref($name,$adi_file_tab_prefs[$name]['value']);
else {
if (array_key_exists($name,$adi_file_tab_prefs))
$html = $adi_file_tab_prefs[$name]['input'];
else
$html = 'text_input';
$res = set_pref($name,$value,'adi_file_tab_admin',2,$html,0,$private);
$prefs[$name] = get_pref($name,$adi_file_tab_prefs[$name]['value'],TRUE);
return $res;
}
}
function adi_file_tab_id() {
// return file id
global $adi_file_tab_debug;
$id = gps('id'); // existing file
if (empty($id)) // newly added file
$id = safe_field('id','txp_file',"1=1 ORDER BY id DESC LIMIT 1",$adi_file_tab_debug); // get largest ID (i.e. last one added)
return $id;
}
function adi_file_tab_inject_details($buffer) {
// time for your injection Mister 4.5
global $DB;
if (!isset($DB))
$DB = new db;
$pattern = '#<p class="edit-condition">.*<div class="summary-details#sU';
$insert = 'adi_file_tab_details';
$buffer = preg_replace_callback($pattern, $insert, $buffer);
return $buffer;
}
function adi_file_tab_details() {
// markup for file details - TXP 4.5
global $event,$step,$adi_file_tab_debug,$file_base_path,$adi_file_tab_message;
$id = adi_file_tab_id();
$out = '';
$message = '';
$rs = safe_row("*","txp_file","id = $id");
if ($rs) {
extract($rs);
if ($message) { // set up message within File Edit pane
$message = strong($adi_file_tab_message[0]);
$class = "success";
if (isset($adi_file_tab_message[1])) {
// translate E_ERROR, E_WARNING into class
if ($adi_file_tab_message[1] == 1)
$class = "error";
if ($adi_file_tab_message[1] == 2)
$class = "warning";
}
$message = graf($message,' class="'.$class.'"');
}
// new file details display
$filename = sanitizeForFile($filename);
$file_exists = file_exists(build_file_path($file_base_path,$filename));
$condition =
'<span class="'
.(($file_exists) ? 'success' : 'error')
.'">'
.(($file_exists) ? gTxt('file_status_ok') : gTxt('file_status_missing'))
.'</span>';
$out .= gTxt('file').' #'.$id.' — '.$filename;
$out = tag($out,'h3',' class="adi_file_tab_heading" title="'.$filename.'"');
$out .=
graf(
'('
.adi_file_tab_bytesize($size)
.', '
.tag(gTxt('last_modified'),'span')
.' '
.$modified
.', '
.$condition
.') '
.span('['.make_download_link($id,gTxt('download'),$filename).']',' class="files_detail"')
,' class="adi_file_tab_details"'
);
}
return $out.$message.'<div class="summary-details';
}
function adi_file_tab_file_info($id) {
// TXP 4.6 replacement for adi_file_tab_details()
global $adi_file_tab_debug,$file_base_path,$adi_file_tab_message,$adi_file_tab_txp460;
$out = '';
$message = '';
$rs = safe_row("*","txp_file","id = $id");
if ($rs) {
extract($rs);
if ($adi_file_tab_debug) {
echo __FUNCTION__.'():'.br;
dmp($rs);
}
// file details markup
$filename = sanitizeForFile($filename);
$file_exists = file_exists(build_file_path($file_base_path,$filename));
$out =
graf(
tag(
adi_file_tab_bytesize($size)
.', '
.gTxt('last_modified')
.' '
.$modified
,'em'
)
,' class="adi_file_tab_file_info"'
);
}
echo $out.$message;
}
function adi_file_tab_title_checkbox() {
// markup for "copy from filename"
return
graf(
tag(
checkbox('adi_file_tab_copy_file_title','1','0','', 'adi_file_tab_copy_file_title').sp.gTxt('adi_copy_filename_title')
,'label'
)
,' class="adi_file_tab_checkbox adi_file_tab_copy_file_title"'
);
}
function adi_file_tab_desc_checkbox() {
// markup for "copy from description"
return
tag(
checkbox('adi_file_tab_copy_title_desc','1','0','', 'adi_file_tab_copy_title_desc').sp.gTxt('adi_copy_title_description')
,'label'
,' class="adi_file_tab_checkbox adi_file_tab_copy_title_desc"'
);
}
function adi_file_tab($event,$step) {
// display file info & links
// e.g. $event = article_ui, $step = file_edit
global $prefs,$adi_file_tab_debug,$adi_file_tab_message;
if ($adi_file_tab_debug) {
echo __FUNCTION__.'():'.br;
echo '$adi_file_tab_message';
dmp($adi_file_tab_message);
echo 'lastmod: '.$prefs['lastmod'];
}
// homegrown messages
adi_file_tab_messagepane();
}
function adi_file_tab_extend_detail($event,$step,$default,$rs) {
// add extras to bottom of file edit form
global $adi_file_tab_txp460;
$out = '';
if ($rs) {
extract($rs);
$out .= adi_file_tab_update_filename_checkbox(); // shifted up into standard file replace form (or upload form if file missing)
$out .= adi_file_tab_update_filename_button($rs); // shift "last chance" update filename button to after file replace area
adi_file_tab_rename($rs); // shifted after standard file replace <form> (removed if file missing)
$out .= adi_file_tab_replace_from_existing($rs); // shifted to after standard file replace form (or upload form if file missing)
$out .= adi_file_tab_delete($rs); // shifted to after replace from existing form
if ((version_compare(txp_version,'4.5','>=')) && (version_compare(txp_version,'4.5.7','<='))) $out .= adi_file_tab_details_heading(); // jQuery shifted up to top of file details area
if ($adi_file_tab_txp460) $out .= adi_file_tab_file_info($id); // shifted to file id area
$out .= adi_file_tab_downloads($downloads); // TXP 4.5 - shifted to after description, TXP 4.6 replaces download count (non-input) text
$out .= adi_file_tab_title_checkbox(); // shifted to after title input field
$out .= adi_file_tab_desc_checkbox(); // shifted to after description textarea
if (version_compare(txp_version,'4.5.7','>=')) $out .= adi_file_tab_author($id); // (author update won't work in pre 4.5.7 due to bug)
}
return $out;
}
function adi_file_tab_rename($rs) {
// rename option
global $adi_file_tab_debug,$adi_file_tab_txp460;
// if ($adi_file_tab_txp460)
// $out = '<section id="adi_file_tab_rename_file" class="txp-details">';
// else
$out = '<div id="adi_file_tab_rename_file" class="summary-details">'; // class="summary-details" added for 4.5 Hive
extract($rs);
$out .=
tag(gTxt('adi_rename_file'),'h3')
.form(
graf(
tag(
tag(gTxt('filename'),'label',' for="adi_file_tab_new_filename"')
.fInput('text','adi_file_tab_new_filename',$filename,'','','','','','adi_file_tab_new_filename')
,'span'
,' class="edit-value"'
)
.fInput(
'submit'
,'do_something'
,gTxt('adi_rename')
,'smallerbox'
)
)
.eInput('file')
.sInput('file_edit') // file_edit $step returns user to file edit panel
.hInput('adi_file_tab_rename_file','1')
.hInput('id',$id)
.hInput('adi_file_tab_old_filename',$filename)
,''
,''
,'post'
,'adi_file_tab_rename_form'
);
// if ($adi_file_tab_txp460)
// $out .= '</section>';
// else
$out .= '</div>';
echo $out; // don't use return (otherwise forms get nested & growser interferes with markup, making jQuery shift fail)
}
function adi_file_tab_update_filename_button($rs) {
// button to update filename - another chance after a "auto-update-filename-unchecked" replace file operation
global $adi_file_tab_debug,$file_base_path,$adi_file_tab_message;
if (gps('adi_file_tab_update_filename')) // don't want button if filename auto update has just been performed
return;
$debug = '';
$out = '';
if (isset($_FILES['thefile'])) {
$id = adi_file_tab_id();
$rs = safe_row("*","txp_file","id = $id",$adi_file_tab_debug);
extract($rs);
// new file
$new_filename = $_FILES['thefile']['name'];
$new_path = build_file_path($file_base_path,$new_filename);
$debug .= 'NEW FILENAME: '.$new_filename.br.'NEW PATH: '.$new_path.br;
// original file
$original_path = build_file_path($file_base_path,$filename);
$debug .= 'ORIGINAL FILENAME: '.$filename.br.'ORIGINAL PATH: '.$original_path.br;
// create button, but only if a differently named file has been uploaded
if ($filename != $new_filename) {
$out .= '<div class="adi_file_tab_update_filename_button warning">';
$out .= graf(strong(gTxt('adi_copy_update_filename_uploaded')).br.'('.$new_filename.')');
$out .=
form(
graf(
fInput(
'submit'
,'do_something'
,gTxt('adi_update_filename')
,'smallerbox'
)
.eInput('file')
.sInput('file_edit') // file_edit $step returns user to file edit panel
.hInput('adi_file_tab_update_filename','1')
.hInput('id',$id)
.hInput('adi_file_tab_old_filename',$filename)
.hInput('adi_file_tab_new_filename',$new_filename)
)
,''
,''
,'post'
,'adi_file_tab_update_button'
);
$out .= '</div>';
}
}
if ($adi_file_tab_debug)
echo $debug;
echo $out; // don't use return (otherwise forms get nested & growser interferes with markup, making jQuery shift fail)
}
function adi_file_tab_update_filename_checkbox() {
// select whether to update filename as part of replace file function or not
return
tag(
checkbox('adi_file_tab_update_filename','1',adi_file_tab_pref('adi_file_tab_replace_default'),'', 'adi_file_tab_update_filename')
.sp
.gTxt('adi_auto_update_filename')
,'label'
,' class="adi_file_tab_update_filename"'
);
}
function adi_file_tab_rename_file($id,$new_filename) {
// rename file
global $adi_file_tab_debug,$file_base_path,$adi_file_tab_message,$txp_user;
$debug = __FUNCTION__.'():'.br;
$id = assert_int($id);
$out = '';
$message = array();
$new_filename = trim($new_filename);
$rs = safe_row("*","txp_file","id = $id",$adi_file_tab_debug);
extract($rs);
$debug .= "FILE ID = $id".br;
$original_path = build_file_path($file_base_path,$filename);
$debug .= 'ORIGINAL FILENAME: '.$filename.br.'ORIGINAL PATH: '.$original_path.br;
$new_path = build_file_path($file_base_path,$new_filename);
$debug .= 'NEW FILENAME: '.$new_filename.br.'NEW PATH: '.$new_path.br;
$debug .= "TXP USER = $txp_user".br;
$debug .= "FILE AUTHOR = $author".br;
if (!has_privs('file.edit') && !($author == $txp_user && has_privs('file.edit.own')))
return gTxt('restricted_area');
// make up error message (acts as a flag to do nuffin later)
if ((strpbrk($new_filename,"\\/?%*:|\"<>") === FALSE) && ($new_filename != '')) { // no dodgy characters & not blank
if (is_file($new_path)) { // filename already exists so check if it's a different file
$rs = safe_row("id as other_id","txp_file","filename = '".doSlash($new_filename)."'",$adi_file_tab_debug);
extract($rs);
if ($rs) {
$debug .= "OTHER FILE ID = $other_id".br;
if ($id != $other_id) { // new filename already in use in database
$message = array(gTxt('adi_file_rename_fail').' — '.gTxt('file_already_exists',array('{name}'=>$new_filename)),E_ERROR);
$debug .= 'NEW FILENAME ALREADY EXISTS IN DB'.br;
}
}
else { // found in files directory (but not in database)
$message = array(gTxt('adi_file_rename_fail').' — '.gTxt('adi_file_exists_dir',array('{name}'=>$new_filename,'{dir}'=>$file_base_path)),E_ERROR);
$debug .= "NEW FILENAME ALREADY EXISTS IN DIR (BUT NOT IN DB)".br;
}
}
else if (is_dir($new_path)) { // new filename is a directory
$message = array(gTxt('adi_file_rename_fail').' — '.gTxt('adi_name_is_dir',array('{name}'=>$new_filename)),E_ERROR);
$debug .= 'NEW FILENAME IS A DIRECTORY'.br;
}
}
else
$message = array(gTxt('invalid_filename'),E_ERROR);
// do the doing
if (!$message) { // so far so good, so do rename/replace
$rename_ok = rename($original_path,$new_path);
if ($rename_ok) {
// update database accordingly
safe_update('txp_file',"filename = '".doSlash($new_filename)."', modified = now()","id = $id",$adi_file_tab_debug);
update_lastmod();
// get new filename from database (as a double check)
$rs = safe_row("filename","txp_file","id = $id",$adi_file_tab_debug);
extract($rs);
$message = array(gTxt('adi_file_renamed'));
}
else {
$message = array(gTxt('adi_file_rename_fail'),E_ERROR);
$debug .= 'rename() FAILED';
}
}
if ($adi_file_tab_debug) echo $debug;
return $message;
}
function adi_file_tab_filter_list($filename) {
// return TRUE if filename doesn't contain filter
return strpos($filename,adi_file_tab_pref('adi_file_tab_exclude_filter')) === FALSE ;
}
function adi_file_tab_replace_from_existing($rs) {
// replace file markup
global $adi_file_tab_debug,$adi_file_tab_txp460;
$existing_files = get_filenames();
if (count($existing_files) > 0) {
// if ($adi_file_tab_txp460)
// $out = '<section id="adi_file_tab_replace_from_existing" class="txp-details">';
// else
$out = '<div id="adi_file_tab_replace_from_existing" class="summary-details">';
extract($rs);
$out .=
tag(gTxt('adi_replace_from_existing'),'h3')
.'<div role="group">'
.form(
graf(
'<label for="file-existing">'.gTxt('existing_file').'</label>'
.selectInput('filename', $existing_files, '', 1, '', 'file-existing')
, ' class="existing-file"'
)
.graf(
fInput(
'submit'
,'do_something'
,gTxt('replace_file')
,'smallerbox'
)
)
.eInput('file')
.sInput('file_edit') // file_edit $step returns user to file edit panel
.hInput('adi_file_tab_replace_from_existing','1')
.hInput('id',$id)
.hInput('adi_file_tab_old_filename',$filename)
)
.'</div>';
// if ($adi_file_tab_txp460)
// $out .= '</section>';
// else
$out .= '</div>';
echo $out; // don't use return (otherwise forms get nested & growser interferes with markup, making jQuery shift fail)
}
}
function adi_file_tab_replace_file($id,$new_filename) {
// replace file
global $adi_file_tab_debug,$file_base_path;
$message = array();
$debug = __FUNCTION__.'():'.br;
$id = assert_int($id);
$new_path = build_file_path($file_base_path,$new_filename);
$rs = safe_row("*","txp_file","id = $id",$adi_file_tab_debug);
extract($rs);
$debug .= "FILE ID = $id".br;
$original_path = build_file_path($file_base_path,$filename);
$debug .= 'ORIGINAL FILENAME: '.$filename.br.'ORIGINAL PATH: '.$original_path.br;
$new_path = build_file_path($file_base_path,$new_filename);
$debug .= 'NEW FILENAME: '.$new_filename.br.'NEW PATH: '.$new_path.br;
if (trim($new_filename) == '')
// new filename blank
$message = array(gTxt('invalid_filename'),E_ERROR);
else {
// replace file
@chmod($new_path,0644); // taken from file_set_perm() in txp_file.php
$size = filesize($new_path);
// update database accordingly
safe_update('txp_file',"filename = '".doSlash($new_filename)."', modified = now()".", size = $size","id = $id",$adi_file_tab_debug);
update_lastmod();
$message = array(gTxt('adi_file_replaced'));
}
if ($adi_file_tab_debug) echo $debug;
return $message;
}
function adi_file_tab_delete($rs) {
// delete file markup - actual delete done by standard functionality
global $txp_user,$adi_file_tab_debug,$adi_file_tab_txp460;
$debug = __FUNCTION__.'():'.br;
$author = '';
extract($rs);
$debug .= "TXP USER = $txp_user".br;
$debug .= "FILE AUTHOR = $author".br;
$can_delete = has_privs('file.delete') || ($author == $txp_user && has_privs('file.delete.own'));
$out = '';
if ($can_delete) {
$debug .= "CAN DELETE FILE".br;
// if ($adi_file_tab_txp460)
// $out = '<section id="adi_file_tab_delete_file" class="txp-details">';
// else
$out = '<div id="adi_file_tab_delete_file" class="summary-details">';
$out .=
tag(gTxt('adi_delete_file'),'h3')
.'<div role="group">'
.form(
graf(
fInput('submit','do_something',gTxt('delete'),'smallerbox',"",'return verify(\''.gTxt('confirm_delete_popup').'\')')
.eInput('file')
.sInput('file_multi_edit')
.hInput('edit_method','delete')
.hInput('selected[]',$id)
)
)
.'</div>';
// if ($adi_file_tab_txp460)
// $out .= '</section>';
// else
$out .= '</div>';
}
else
$debug .= "CAN'T DELETE FILE".br;
if ($adi_file_tab_debug) echo $debug;
echo $out; // don't use return (otherwise forms get nested & growser interferes with markup, making jQuery shift fail)
}
function adi_file_tab_details_heading() {
// file details heading markup - 4.5 only
return '<div class="adi_file_tab_details_heading txp-details">'.tag(gTxt('adi_file_details'),'h3',' class="adi_file_tab_details_heading"').'</div>';
}
function adi_file_tab_author($id) {
// markup for author select
global $adi_file_tab_debug,$adi_file_tab_txp460;
$id = assert_int($id);
$rs = safe_row("author","txp_file","id = $id",$adi_file_tab_debug);
if ($rs) {
extract($rs);
if ($author) {
$rs = safe_column('name','txp_users','privs > 0',$adi_file_tab_debug);
if ($rs) {
if ($adi_file_tab_txp460) {
echo
inputLabel(
'adi_file_tab_author',
selectInput('adi_file_tab_author',$rs,$author,'','','adi_file_tab_author'),
'author', '', array('class' => 'txp-form-field adi_file_tab_author')
);
}
else
echo
graf(
tag(gTxt('author'),'label',' for="adi_file_tab_author"')
.selectInput('adi_file_tab_author',$rs,$author,'','','adi_file_tab_author')
,' class="adi_file_tab_author"'
);
}
}
}
}
function adi_file_tab_update_author($id=NULL,$author='') {
// update author
global $adi_file_tab_debug;
$id = assert_int($id);
update_lastmod();
return safe_update('txp_file',"author = '$author'","id = $id",$adi_file_tab_debug);
}
function adi_file_tab_downloads($downloads) {
// input field for downloads count
global $adi_file_tab_txp460;
if ($adi_file_tab_txp460) {
return
inputLabel(
'adi_file_tab_downloads',
fInput('text', 'adi_file_tab_downloads', $downloads, '', '', '', INPUT_REGULAR, '', 'adi_file_tab_downloads'),
'download_count', '', array('class' => 'txp-form-field adi_file_tab_edit_download_count')
);
}
else {
$out =
tag(
tag(
gTxt('downloads')
,'label'
,' for="adi_file_tab_downloads"'
)
,'span'
,' class="edit-label"'
)
.tag(
fInput('text','adi_file_tab_downloads',$downloads,'','','','','','adi_file_tab_downloads')
,'span'
,' class="edit-value"'
);
return graf($out,' class="adi_file_tab_downloads"');
}
}
function adi_file_tab_update_downloads($id=NULL,$downloads='') {
// update downloads count
global $adi_file_tab_debug;
$id = assert_int($id);
$downloads = trim($downloads);
if (($id != NULL) && ctype_digit($downloads)) {
update_lastmod();
return safe_update('txp_file',"downloads='$downloads'",'id='.$id,$adi_file_tab_debug);
}
}
function adi_file_tab_messagepane() {
// generate messagepane & message
global $adi_file_tab_message;
if ($adi_file_tab_message) {
$class = 'success';
if (is_array($adi_file_tab_message)) {
$message = $adi_file_tab_message[0];
if (isset($adi_file_tab_message[1])) {
// translate E_ERROR, E_WARNING into class
if ($adi_file_tab_message[1] == 1)
$class = "error";
if ($adi_file_tab_message[1] == 2)
$class = "warning";
}
}
else { // message from adi_file_tab_rename_file
$message = strip_tags($adi_file_tab_message);
}
$html = '<span id="message" class="messageflash '.$class.'">'.$message.' <a href="#close" class="close">×</a></span>';
$message_script = <<<END_SCRIPT
<script type="text/javascript">
<!--
$('#messagepane').append('$html');
$("#messagepane").css({
left: ($(window).width() - $("#messagepane").outerWidth()) / 2
});
$(window).resize();
// -->
</script>
END_SCRIPT;
echo $message_script;
}
}
function adi_file_tab_bytesize($bytes) {
// transform bytes into killer-bytes ...
if ($bytes < 1024) {
$size = $bytes.' bytes';
}
else if ($bytes < pow(1024,2)) {
$size = round($bytes / 1024, 2);
$size .= ' KB';
}
else if ($bytes < pow(1024,3)) {
$size = round($bytes / pow(1024,2), 2);
$size .= ' MB';
}
else {
$size = round($bytes / pow(1024,3), 2);
$size .= ' GB';
}
return $size;
}
function adi_file_tab_edit_script() {
// jQuery magic for file edit tab
global $adi_file_tab_debug,$adi_file_tab_message;
$id = adi_file_tab_id();
if (!empty($id)) {
$rs = safe_row("filename","txp_file","id = $id",$adi_file_tab_debug);
if ($rs) {
extract($rs);
// filename is POSTed by standard Save button, so if there's a failure (in an adi_file_tab function), have to make sure the filename from database is used
$save_button_tweak = '// do nothing';
if (count($adi_file_tab_message) > 1)
$save_button_tweak = '$("input[name=filename]").val("'.$filename.'");';
if ($adi_file_tab_debug) {
$debug = __FUNCTION__.'():'.br;
$debug .= "FILENAME = $filename".br;
$debug .= "save_button_tweak = $save_button_tweak".br;
echo $debug;
}
echo <<<END_SCRIPT
<script type="text/javascript">
$().ready(function() {
// copy filename to title
var old_title = $("input[name=title]").val();
$("#adi_file_tab_copy_file_title").click(function(){
// console.log("Old title = " + old_title);
// TXP 4.5 - filename recorded in title attribute (added by plugin)
var filename = $("h3.adi_file_tab_heading").attr("title");
if (filename == null) { // TXP 4.6
var filename = $("div.edit-file-name div.txp-form-field-value a").html();
}
// console.log("Filename = " + filename);
if ($(this).prop("checked")) {
$("input[name=title]").val(filename);
}
else {
$("input[name=title]").val(old_title);
}
});
// copy title to description
var old_desc = $("textarea").val();
$("#adi_file_tab_copy_title_desc").click(function(){
// console.log("Old desc = " + old_desc);
var title = $("input[name=title]").val();
// console.log("Title = " + title);
if ($(this).prop("checked")) {
$("textarea").val(title);
}
else {
$("textarea").val(old_desc);
}
});
// tweak Save button filename value?
$save_button_tweak
// do stuff depending on whether file ok or missing (with TXP 4.5 & 4.6 variants)
if ($('.edit-file-title').length) { // FILE OK (using file title field to indicate whether file ok)
if ($('div.txp-body').length) { // TXP 4.5
$("label.adi_file_tab_update_filename").appendTo("div.replace-file form");
$("#adi_file_tab_rename_file").insertAfter("div.replace-file");
$("#adi_file_tab_replace_from_existing").prependTo("div.file-detail");
$("#adi_file_tab_delete_file").insertAfter("adi_file_tab_replace_from_existing");
$("div.adi_file_tab_details_heading").prependTo("form#file_details");
$("p.adi_file_tab_copy_file_title").appendTo("p.edit-file-title");
$("label.adi_file_tab_copy_title_desc").appendTo("p.edit-file-description");
$("p.adi_file_tab_downloads").appendTo("p.edit-file-description");
$("p.adi_file_tab_author").insertAfter("p.adi_file_tab_downloads");
}
else { // TXP 4.6
$("label.adi_file_tab_update_filename").appendTo("form.replace-file");
$("#adi_file_tab_rename_file").insertAfter("form.replace-file");
$("#adi_file_tab_replace_from_existing").insertAfter("form.replace-file");
$("#adi_file_tab_delete_file").insertAfter("#adi_file_tab_replace_from_existing");
$("p.adi_file_tab_copy_file_title").insertAfter("div.edit-file-title");
$("label.adi_file_tab_copy_title_desc").appendTo("div.edit-file-description");
$("div.edit-file-download-count").remove();
$("div.adi_file_tab_edit_download_count").prependTo("form#file_details");
}
$("div.adi_file_tab_author").prependTo("form#file_details");
}
else { // FILE MISSING
if ($('div.txp-body').length) { // TXP 4.5
$("label.adi_file_tab_update_filename").appendTo("div.upload-file form");
$("#adi_file_tab_replace_from_existing").insertAfter("div.upload-file");
$("#adi_file_tab_delete_file").insertAfter("adi_file_tab_replace_from_existing");
$("div.adi_file_tab_details_heading").appendTo("div.not-exists");
$("p.adi_file_tab_downloads").appendTo("div.adi_file_tab_details_heading"); // put the downloads field somewhere else (TXP 4.5)
$("p.adi_file_tab_author").insertAfter("div.adi_file_tab_details_heading");
}
else { // TXP 4.6
$("label.adi_file_tab_update_filename").appendTo(".upload-file");
$("#adi_file_tab_replace_from_existing").insertAfter(".upload-file");
$("#adi_file_tab_delete_file").insertAfter("#adi_file_tab_replace_from_existing");
$("div.adi_file_tab_author").insertAfter(".edit-file-name");
}
$("#adi_file_tab_rename_file").remove();
$("p.adi_file_tab_copy_file_title").remove();
$("label.adi_file_tab_copy_title_desc").remove();
$("form#assign_file").remove(); // lose standard "replace from existing" list (in favour of adi_file_tab's version)
}
// do regardless of ok or missing
if ($('div.txp-body').length) { // TXP 4.5
$("div.adi_file_tab_update_filename_button").appendTo("div.replace-file");
}
else { // TXP 4.6
$("p.adi_file_tab_file_info").appendTo("div.edit-file-id .txp-form-field-value");
$("div.adi_file_tab_update_filename_button").insertAfter("form.replace-file");
}
});
</script>
END_SCRIPT;
}
}
}
function adi_file_tab_general_script() {
// jQuery magic for all file tabs
global $adi_file_tab_debug,$adi_file_tab_plugin_status;
// filtration
$filter = adi_file_tab_pref('adi_file_tab_exclude_filter');
if ($filter == '') {
$link_text = gTxt('adi_unfiltered');
$link_title = 'Add filter';
}
else {
$link_text = gTxt('adi_filtered');
$link_title = 'Edit filter';
}
if ($adi_file_tab_plugin_status) // proper install (options under Plugins tab)
$event = 'plugin_prefs.adi_file_tab';
else // txpdev (options under Extensions tab)
$event = 'adi_file_tab_options';
$calling_card = eLink($event,'','','',$link_text,'','',$link_title);
if ($filter != '') {
echo <<<END_SCRIPT
<script type="text/javascript">
$().ready(function() {
// remove items according to filter
$("#file-existing option[value*='$filter']").remove();
});
</script>
END_SCRIPT;
}
echo <<<END_SCRIPT
<script type="text/javascript">
$().ready(function() {
// leave calling card
$("#file-existing").after(' ($calling_card)');
});
</script>
END_SCRIPT;
}
function adi_file_tab_pref_inputs($event,$tab=NULL) {
// output preference input fields accoring to options/admin setting
global $txp_groups,$adi_file_tab_prefs,$adi_file_tab_outlaws,$adi_file_tab_admin_outlaws;
if ($tab === NULL) return;
$out = '';
foreach ($adi_file_tab_prefs as $pref_name => $pref_info) {
if ($pref_info['tab'] == $tab) {
switch ($pref_info['input']) {
case 'yesnoradio':
$out .=
graf(
tag(gTxt($pref_info['label']),'label')
.sp.sp
.tag(radio($pref_name,'1',(adi_file_tab_pref($pref_name) == '1')).sp.gTxt('yes'),'label')
.sp.sp
.tag(radio($pref_name,'0',(adi_file_tab_pref($pref_name) == '0')).sp.gTxt('no'),'label')
);
break;
case 'custom':
// admin privs
if ($pref_name == 'adi_file_tab_admin_privs') {
$priv_out = gTxt($pref_info['label']);
$admin_privs = do_list(adi_file_tab_pref('adi_file_tab_admin_privs'));
foreach ($txp_groups as $index => $group)
if (array_search($index,$adi_file_tab_admin_outlaws) === FALSE) // ignore the outlaws
$priv_out .= tag(sp.sp.checkbox2("adi_file_tab_admin_privs[$index]",(array_search($index,$admin_privs) !== FALSE),'','adi_file_tab_admin_privs').sp.gTxt($group),'label');
$out .= graf($priv_out);
}
// plugin privs
if ($pref_name == 'adi_file_tab_privs') {
$priv_out = gTxt($pref_info['label']);
$plugin_privs = do_list(adi_file_tab_pref('adi_file_tab_privs'));
foreach ($txp_groups as $index => $group)
if (array_search($index,$adi_file_tab_outlaws) === FALSE) // ignore the outlaws
$priv_out .= tag(sp.sp.checkbox2("adi_file_tab_privs[$index]",(array_search($index,$plugin_privs) !== FALSE),'','adi_file_tab_privs').sp.gTxt($group),'label');
$out .= graf($priv_out);
}
break;
default: // text_input
$out .=
graf(
tag(gTxt($pref_info['label']),'label')
.sp.sp
.finput('text',$pref_name,stripslashes(adi_file_tab_pref($pref_name)),$pref_name)
);
break;
}
}
}
if ($out)
return
form(
tag(
tag(gTxt('edit_preferences'),'h2')
.$out
.fInput('submit','do_something',gTxt('adi_update_prefs'),'smallerbox')
.eInput($event)
.sInput('update_prefs')
,'div'
,' style="text-align:center"'
)
);
}
function adi_file_tab_update_prefs($tab=NULL) {
// update prefs
global $txp_user,$adi_file_tab_prefs;
if ($tab === NULL)
return FALSE;
else {
$result = TRUE;
// run through all prefs according to tab
foreach ($adi_file_tab_prefs as $pref_name => $pref_info) {
if ($pref_info['tab'] == $tab) {
if (array_key_exists($pref_name,$_POST))
$value = $_POST[$pref_name];
else if ($pref_info['input'] == 'yesnoradio')
$value = '0';
else
$value = $pref_info['value'];
if (is_array($value)) { // string-ify arrays
$values = array_keys($value); // need index values not 1s
if ($pref_name == 'adi_file_tab_admin_privs') {
$user_priv = safe_field('privs','txp_users',"name='".doSlash($txp_user)."'");
$values = array_unique(array_merge($values,array(1,$user_priv))); // can't kill off Publisher or self
}
$value = implode(',',$values);
}
// some values not allowed to be blank, reset to default
if ((!$pref_info['blank']) && (trim($value) == ''))
$value = $adi_file_tab_prefs[$pref_name]['value'];
$result &= adi_file_tab_pref($pref_name,$value);
}
}
return $result;
}
}
function adi_file_tab_options($event,$step) {
// plugin options page
global $adi_file_tab_debug,$adi_file_tab_url,$textarray;
$message = '';
if (adi_file_tab_upgrade())
$step = 'upgrade';
// steppers
if ($step == 'textpack') {
if (function_exists('install_textpack')) {
$adi_textpack = file_get_contents($adi_file_tab_url['textpack']);
if ($adi_textpack) {
$result = install_textpack($adi_textpack);
$message = gTxt('textpack_strings_installed', array('{count}' => $result));
$textarray = load_lang(LANG); // load in new strings
}
else
$message = array(adi_menu_gtxt('adi_textpack_fail'),E_ERROR);
}
}
else if ($step == 'update_prefs') {
$result = adi_file_tab_update_prefs('options');
$result ? $message = gTxt('preferences_saved') : $message = array(gTxt('adi_pref_update_fail'),E_ERROR);
}
else if ($step == 'upgrade') {
$result = adi_file_tab_upgrade(TRUE);
$result ? $message = gTxt('adi_upgraded') : $message = array(gTxt('adi_upgrade_fail'),E_ERROR);
}
// generate page
pagetop('adi_file_tab '.gTxt('plugin_prefs'),$message);
// options
echo tag(
tag('adi_file_tab '.gTxt('plugin_prefs'),'h2')
// link to file list tab
.graf(href(gTxt('tab_file'),'?event=file'))
// prefs
.adi_file_tab_pref_inputs($event,'options')
// textpack links
.graf(href(gTxt('install_textpack'),'?event='.$event.a.'step=textpack'),' style="margin-top:3em"')
.graf(href(gTxt('adi_textpack_online'),$adi_file_tab_url['textpack_download']))
.graf(href(gTxt('adi_textpack_feedback'),$adi_file_tab_url['textpack_feedback']))
,'div'
,' style="text-align:center; margin-bottom:3em"'
);
}