This repository was archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmdocs-batch-upload.php
More file actions
160 lines (160 loc) · 6.55 KB
/
mdocs-batch-upload.php
File metadata and controls
160 lines (160 loc) · 6.55 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
function mdocs_batch_upload($current_cat) {
// INPUT SANITIZATION
$post_page = sanitize_text_field($_REQUEST['page']);
$post_cat = sanitize_text_field($_REQUEST['cat']);
$do_zip = false;
//$mdocs = get_option('mdocs-list');
$cats = get_option('mdocs-cats');
//var_dump($cats);
$do_complte = false;
/*
if(isset($_FILES['mdocs-batch']) && strpos($_FILES['mdocs-batch']['type'],'zip') == false) {
?>
<div class="error">
<p><?php _e('Please upload a zip file.','mdocs'); ?></p>
</div>
<?php
*/
if(isset($_FILES['mdocs-batch']) && strpos($_FILES['mdocs-batch']['type'],'zip') >= 0) {
if(!file_exists('/tmp/')) mkdir('/tmp/');
if(!file_exists('/tmp/mdocs/')) mkdir('/tmp/mdocs/');
$zip_result = mdocs_unzip($_FILES['mdocs-batch']['tmp_name'], '/tmp');
$do_zip = true;
} elseif (isset($_POST['mdocs-batch-complete'])) {
$do_complte = true;
}
?>
<h1><?php _e('Batch Library Upload','mdocs'); ?></h1>
<div class="error">
<h3>Warning</h3>
<p><?php _e('Batch Upload is still in beta. Be sure to backup your library before running this process. If anything should go wrong, just import the backup using the"Overwrite Saved Variables" option, and then run the "File System Cleanup" process to revert to the original state.','mdocs'); ?></p></div>
<div class="updated">
<p><?php _e('Create a zip file of all the documents you want to upload. You may name it whatever you want, naming doesn\'t matter. Once you have created the file, simply upload it, then use the quick select form to place the files in the proper directory. Once satisfied press the \'Complete\' button to finsh the process.','mdocs'); ?></p>
</div>
<?php if($do_zip == false && $do_complte == false) { ?>
<form class="mdocs-uploader-form" enctype="multipart/form-data" action="<?php echo get_site_url().'/wp-admin/admin.php?page='.$post_page.'&cat='.$post_cat; ?>" method="POST">
<input type="file" name="mdocs-batch" /><br/>
<input type="submit" class="button button-primary" value="<?php _e('Upload Zip File','mdocs') ?>" /><br/>
</form>
<?php } elseif($do_zip) {
$cats = get_option('mdocs-cats');
$current_cat = key($cats);
?>
<form class="mdocs-uploader-form" enctype="multipart/form-data" action="<?php echo get_site_url().'/wp-admin/admin.php?page='.$post_page.'&cat='.$post_cat; ?>" method="POST">
<input type="hidden" name="mdocs-batch-complete" value="1" />
<input type="hidden" name="mdocs-type" value="mdocs-add" />
<?php
if($zip_result) {
foreach($zip_result['file'] as $index => $zip_file) {
$file = explode('/',$zip_file);
if(count($file) == 1) $file = explode('\\',$zip_file);
$file = $file[count($file)-1];
$file = preg_replace('/[^A-Za-z0-9\-._]/', '', $file);
$file = str_replace(' ','-', $file);
$filename = $file;
$ext = strrchr($file,'.');
$file = str_replace($ext, '', $file);
?>
<div class="mdocs-batch-container">
<input type="hidden" name="mdocs[filename][<?php echo $index; ?>]" value="<?php echo $filename; ?>" />
<input type="hidden" name="mdocs[tmp-file][<?php echo $index; ?>]" value="<?php echo $zip_file; ?>" />
<label><?php _e('File Name','mdocs'); ?>:
<input type="text" name="mdocs[name][<?php echo $index; ?>]" value="<?php echo $file; ?>"/>
</label>
<label><?php _e('Category','mdocs'); ?>:
<select name="mdocs[cat][<?php echo $index; ?>]">
<?php mdocs_get_cats($cats, $current_cat); ?>
</select>
</label>
<label>
<?php _e('Version','mdocs'); ?>:
<input type="text" name="mdocs[version][<?php echo $index; ?>]" value="1.0" />
</label>
</div>
<?php
}?>
<br>
<input type="submit" class="button button-primary" value="<?php _e('Complete','mdocs') ?>" />
<br/>
<?php } ?>
</form>
<?php
} elseif ($_POST['mdocs-batch-complete'] ) {
$file = array();
$current_user = wp_get_current_user();
$batch_log = '';
foreach($_POST['mdocs']['tmp-file'] as $index => $tmp) {
$valid_mime_type = false;
$file['name'] = $_POST['mdocs']['filename'][$index];
$result = wp_check_filetype($tmp);
$file['tmp_name'] = $tmp;
$file['error'] = 0;
if(file_exists($tmp)) $file['size'] = filesize($tmp);
$file['post_status'] = 'publish';
$file['post-status'] = 'publish';
//MDOCS FILE TYPE VERIFICATION
$mimes = get_allowed_mime_types();
foreach ($mimes as $type => $mime) {
if ($mime === $result['type']) {
$valid_mime_type = true;
break;
}
}
$batch_log .= _('Processed File => ').$file['name']."<br>";
if($valid_mime_type) {
$upload = mdocs_process_file($file);
$mdocs = get_option('mdocs-list');
array_push($mdocs, array(
'id'=>(string)$upload['attachment_id'],
'parent'=>(string)$upload['parent_id'],
'filename'=>$upload['filename'],
'name'=>$_POST['mdocs']['name'][$index],
'desc'=>$upload['desc'],
'type'=>$result['type'],
'cat'=>$_POST['mdocs']['cat'][$index],
'owner'=>$current_user->user_login,
'size'=>(string)$file['size'],
'modified'=>(string)time(),
'version'=>(string)$_POST['mdocs']['version'][$index],
'show_social'=>(string)'on',
'non_members'=> (string)'on',
'file_status'=>(string)'public',
'post_status'=> (string)'publish',
'post_status_sys'=> (string)'publish',
'doc_preview'=>(string)'',
'downloads'=>(string)0,
'archived'=>array(),
'ratings'=>array(),
'rating'=>0
));
$mdocs = mdocs_array_sort($mdocs, 'name', SORT_ASC);
mdocs_save_list($mdocs);
$batch_log .= _('Mime Type Allowed => ').$result['type']."<br>";
$batch_log .= _('File Uploaded with No Errors.')."<br><br>";
} else {
$batch_log .= __("Invalid Mime Type => ",'mdocs').$result['type']._(" Unable to process file.")."<br>";
$batch_log .= _('File Was Not Uploaded because an Error occured.')."<br><br>";
}
$file = array();
}
$batch_log .= _("Cleaning up tmp folder and files")."<br><br>";
$files = glob('/tmp/mdocs/*');
foreach($files as $file) if(is_file($file)) unlink($file);
$files = glob('/tmp/mdocs/.*');
foreach($files as $file) if(is_file($file)) unlink($file);
if(file_exists('/tmp/mdocs')) @rmdir('/tmp/mdocs');
$batch_log .= _("Batch Process Complete.");
?>
<div class="updated">
<p><?php _e('The batch process has completed, below is a log of results:','mdocs'); ?></p>
<p><?php echo $batch_log; ?></p>
</div>
<form class="mdocs-uploader-form" enctype="multipart/form-data" action="<?php echo get_site_url().'/wp-admin/admin.php?page='.$post_page.'&cat='.$post_cat; ?>" method="POST">
<input type="file" name="mdocs-batch" /><br/>
<input type="submit" class="button button-primary" value="<?php _e('Upload Zip File','mdocs') ?>" /><br/>
</form>
<?php
}
}
?>