1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
<?php
/**
* @version $Revision$
* @package liberty
* @subpackage plugins_storage
*/
namespace Bitweaver\Liberty;
use Bitweaver\BitBase;
use Bitweaver\KernelTools;
global $gLibertySystem, $gBitSystem, $gBitSmarty, $gBitThemes;
/**
* definitions
*/
define( 'PLUGIN_GUID_BIT_FILES', 'bitfile' );
$pluginParams = [
'store_function' => '\Bitweaver\Liberty\bit_files_store',
'verify_function' => '\Bitweaver\Liberty\bit_files_verify',
'load_function' => '\Bitweaver\Liberty\bit_files_load',
'expunge_function' => '\Bitweaver\Liberty\bit_files_expunge',
'description' => 'Always load by default, disable to prevent Direct File Upload To Server',
'plugin_type' => STORAGE_PLUGIN,
'auto_activate' => false,
'edit_label' => 'Upload File',
'primary_edit_field' => '<input type="file" name="primary_attachment" size="40" />',
'edit_field' => '<input type="file" name="upload" size="40" />',
'edit_help' => 'After selecting the file you want to upload, please return to the edit area and click the save button.',
];
if( isset( $gBitSystem )) {
if ($gBitSystem->getConfig("liberty_attachment_style") == "multiple") {
$pluginParams['edit_label'] = 'Upload File(s)';
$pluginParams['edit_help'] = 'The file(s) will be uploaded to your personal storage area.<br />After selecting the file(s) you want to upload, please return to the edit area and click the save button.';
$pluginParams['edit_field'] = '<div id="upload_div"></div><input type="file" name="upload" size="40" id="uploads" />'.
'<!-- Multiselect javascript. -->'.
'<script src="'.UTIL_PKG_URL.'javascript/multifile.js"></script>'.
'<script>'.
'var upload_files = document.getElementById( \'upload_div\' );'.
'var upload_element = document.getElementById( \'uploads\' );'.
'var multi_selector = new MultiSelector( upload_files, '.$gBitSystem->getConfig('liberty_max_multiple_attachments', 10).' );'.
'multi_selector.addNamedElement( upload_element , \'uploads\');'.
'</script>';
$gBitSmarty->assign( 'loadMultiFile', true );
} elseif( $gBitSystem->getConfig( 'liberty_attachment_style' ) == "ajax" ) {
$pluginParams['edit_help_new'] = $pluginParams['edit_help'];
$pluginParams['edit_field_new'] = $pluginParams['edit_field'];
$pluginParams['edit_help'] = 'The file(s) will be uploaded to your personal storage area.<br />After selecting the file you want to upload an attachment ID will be displayed for you to use in your content.';
$pluginParams['edit_field'] = '<input type="file" name="upload" size="40" id="upload" onchange="javascript:LibertyAttachment.uploader(this, \'{$smarty.const.LIBERTY_PKG_URL}attachment_uploader.php\',\'Please wait for the current upload to finish.\', \'liberty_upload_frame\', \'editpageform\');" />'.
'{include file="bitpackage:liberty/attachment_uploader_inc.tpl"}';
}
}
//$gLibertySystem->registerPlugin( STORAGE_TYPE_BIT_FILES, $pluginParams );
$gLibertySystem->registerPlugin( PLUGIN_GUID_BIT_FILES, $pluginParams );
function bit_files_verify( &$pStoreRow ) {
$pStoreRow['plugin_guid'] = PLUGIN_GUID_BIT_FILES;
$pStoreRow['foreign_id'] = null;
if( strlen( $pStoreRow['upload']['name'] ) > 200 ) {
$pStoreRow['upload']['name'] = substr( $pStoreRow['upload']['name'], 0, 195 ).'.'.substr( $pStoreRow['upload']['name'], strrpos( $pStoreRow['upload']['name'], '.' ) );
}
$pStoreRow['dest_base_name'] = substr( $pStoreRow['upload']['name'], 0, strrpos( $pStoreRow['upload']['name'], '.' ) );
if( function_exists( 'transliterate' ) ) {
// $pStoreRow['dest_base_name'] = transliterate( $pStoreRow['dest_base_name'], array('han_transliterate', 'diacritical_remove'), 'utf-8', 'utf-8' );
}
$pStoreRow['source_file'] = $pStoreRow['upload']['tmp_name'];
return true;
}
function bit_files_store( &$pStoreRow ) {
global $gBitSystem, $gBitUser;
$ret = null;
// we have been given an attachment_id but no foreign_id. we will make a last attempt to see if this is an update or an insert
if( BitBase::verifyId( $pStoreRow['attachment_id'] ) && !BitBase::verifyId( $pStoreRow['foreign_id'] )) {
$pStoreRow['foreign_id'] = $gBitSystem->mDb->getOne( "SELECT `foreign_id` FROM `".BIT_DB_PREFIX."liberty_attachments` WHERE `attachment_id` = ?", [ $pStoreRow['attachment_id'] ] );
}
if( BitBase::verifyId( $pStoreRow['foreign_id'] ) ) {
$sql = "UPDATE `".BIT_DB_PREFIX."liberty_files` SET `file_name`=?, `mime_type`=?, `file_size`=? WHERE `file_id` = ?";
$gBitSystem->mDb->query( $sql, [ $pStoreRow['upload']['name'], $pStoreRow['upload']['type'], $pStoreRow['upload']['size'], $pStoreRow['foreign_id'] ] );
} else {
$pStoreRow['foreign_id'] = $gBitSystem->mDb->GenID( 'liberty_files_id_seq' );
$sql = "INSERT INTO `".BIT_DB_PREFIX."liberty_files` ( `file_name`, `file_id`, `mime_type`, `file_size`, `user_id` ) VALUES ( ?, ?, ?, ?, ? )";
$userId = !empty( $pStoreRow['upload']['user_id'] ) ? $pStoreRow['upload']['user_id'] : $gBitUser->mUserId;
$gBitSystem->mDb->query($sql, [ $pStoreRow['upload']['name'], $pStoreRow['foreign_id'], $pStoreRow['upload']['type'], $pStoreRow['upload']['size'], $userId ] );
}
return $ret;
}
function bit_files_load( $pRow ) {
// this fuction broken, will fix soon - spiderr
// I think its fixed now - no promises though! - drewslater
global $gBitSystem, $gLibertySystem;
$ret = [];
if( !empty( $pRow['foreign_id'] ) && is_numeric( $pRow['foreign_id'] )) {
$query = "
SELECT *
FROM `".BIT_DB_PREFIX."liberty_attachments` la
INNER JOIN `".BIT_DB_PREFIX."liberty_files` lf ON (lf.`file_id` = la.`foreign_id`)
WHERE la.`foreign_id` = ? AND `attachment_plugin_guid` = ?";
if( $ret = $gBitSystem->mDb->getRow( $query, [ $pRow['foreign_id'], PLUGIN_GUID_BIT_FILES ] ) ) {
$ret['file_name'] = basename( $ret['file_name'] );
$ret['source_file'] = liberty_mime_get_source_file( $ret );
if( ($canThumbFunc = liberty_get_function( 'can_thumbnail' )) && $canThumbFunc( $ret['mime_type'] ) ) {
$thumbHash['default_image'] = LIBERTY_PKG_URL.'icons/generating_thumbnails.png';
}
$ret['source_url'] = liberty_mime_get_source_url( $ret );
$ret['thumbnail_url'] = liberty_fetch_thumbnails( $ret );
$ret['wiki_plugin_link'] = "{attachment id=".$ret['attachment_id']."}";
}
}
return $ret;
}
function bit_files_expunge( $pStorageId ) {
global $gBitUser, $gBitSystem;
$ret = false;
if (is_numeric($pStorageId)) {
$sql = "SELECT * FROM `".BIT_DB_PREFIX."liberty_attachments` la
INNER JOIN `".BIT_DB_PREFIX."liberty_files` lf ON (lf.`file_id`=la.`foreign_id`)
WHERE la.`attachment_id` = ?";
if( $row = $gBitSystem->mDb->getRow( $sql, [ $pStorageId ])) {
$sourceFile = liberty_mime_get_source_file( $row );
if( $gBitUser->isAdmin() || $gBitUser->mUserId == $row['user_id'] ) {
if( file_exists( $sourceFile )) {
// make sure this is a valid storage directory before removing it
if( preg_match( '!/users/\d+/\d+/\w+/\d+/.+!', $sourceFile )) {
\Bitweaver\KernelTools::unlink_r( dirname( $sourceFile ));
} else {
unlink( $sourceFile );
}
}
$query = "DELETE FROM `".BIT_DB_PREFIX."liberty_files` WHERE `file_id` = ?";
$gBitSystem->mDb->query($query, [$row['foreign_id']] );
$ret = true;
}
}
}
return $ret;
}
|