This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
forked from devfarm-it/wp-gpx-maps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-gpx-maps_admin_tracks.php
206 lines (171 loc) · 5.27 KB
/
wp-gpx-maps_admin_tracks.php
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
if ( !(is_admin()) )
return;
$is_admin = current_user_can( 'manage_options' );
if ( $is_admin != 1 )
return;
$gpxRegEx = '/.gpx$/i';
if ( isset($_POST['clearcache']) )
{
if ( isset($_GET['_wpnonce'])
&&
wp_verify_nonce( $_GET['_wpnonce'], 'wpgpx_clearcache_nonce' . $entry )
)
{
echo "Cache is now empty!";
wpgpxmaps_recursive_remove_directory($cacheGpxPath, true);
}
}
if ( is_writable ( $realGpxPath ) ){
?>
<div class="tablenav top">
<form enctype="multipart/form-data" method="POST" style="float:left; margin:5px 20px 0 0" action="/wp-admin/options-general.php?page=WP-GPX-Maps">
Choose a file to upload: <input name="uploadedfile[]" type="file" onchange="submitgpx(this);" multiple />
<?php
if ( isset($_FILES['uploadedfile']) )
{
$total = count($_FILES['uploadedfile']['name']);
for($i=0; $i<$total; $i++) {
$uploadingFileName = basename( $_FILES['uploadedfile']['name'][$i]);
$target_path = $realGpxPath ."/". $uploadingFileName;
if (preg_match($gpxRegEx, $target_path))
{
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$i], $target_path)) {
echo "<br />File <b>". $uploadingFileName . "</b> has been uploaded";
} else{
echo "<br />There was an error uploading the file, please try again!";
}
}
else
{
echo "file not supported!";
}
}
}
?>
</form>
<form method="POST" style="float:left; margin:5px 20px 0 0" action="/wp-admin/options-general.php?page=WP-GPX-Maps&_wpnonce=<?php echo wp_create_nonce( 'wpgpx_clearcache_nonce' ) ?>" >
<input type="submit" name="clearcache" value="Clear Cache" />
</form>
</div>
<?php
}
else
{
?>
<br />
<br />
<p style='font-size:2em;'>please make <b><?php echo $realGpxPath ?></b> folder writable. </p>
<br />
<br />
<?php
}
$myGpxFileNames = array();
if ( is_readable ( $realGpxPath ) && $handle = opendir($realGpxPath)) {
while (false !== ($entry = readdir($handle))) {
if (preg_match($gpxRegEx, $entry ))
{
if ( isset($_GET['_wpnonce'])
&&
wp_verify_nonce( $_GET['_wpnonce'], 'wpgpx_deletefile_nonce_' . $entry )
) {
if ( file_exists($realGpxPath ."/". $entry) )
{
unlink($realGpxPath ."/". $entry);
echo "<br/><b>$entry has been deleted.</b>";
}
else {
echo "<br/><b>Can't delete $entry.</b>";
}
}
else
{
$myFile = $realGpxPath . "/" . $entry;
$myGpxFileNames[] = array(
'name' => $entry,
'size' => filesize( $myFile ),
'lastedit' => filemtime( $myFile ),
'nonce' => wp_create_nonce( 'wpgpx_deletefile_nonce_' . $entry ),
);
}
}
}
closedir($handle);
}
if ( is_readable ( $realGpxPath ) && $handle = opendir($realGpxPath)) {
while (false !== ($entry = readdir($handle))) {
if (preg_match($gpxRegEx,$entry ))
{
$filenames[] = $realGpxPath . "/" . $entry;
}
}
closedir($handle);
}
$wpgpxmaps_gpxRelativePath = get_site_url(null, '/wp-content/uploads/gpx/');
?>
<table id="table" class="wp-list-table widefat plugins"></table>
<script type="text/javascript">
function submitgpx(el)
{
var newEl = document.createElement('span');
newEl.innerHTML = 'Uploading file...';
el.parentNode.insertBefore(newEl,el.nextSibling);
el.parentNode.submit()
}
jQuery('#table').bootstrapTable({
columns: [{
field: 'name',
title: 'File',
sortable: true,
formatter: function(value, row, index) {
return [
'<b>' + row.name + '</b><br />',
'<a class="delete_gpx_row" href="/wp-admin/options-general.php?page=WP-GPX-Maps&_wpnonce=' + row.nonce + '" >Delete</a>',
' | ',
'<a href="<?php echo $wpgpxmaps_gpxRelativePath ?>' + row.name + '">Download</a>',
' | ',
'Shortcode: [sgpx gpx="<?php echo $relativeGpxPath ?>' + row.name + '"]',
].join('')
}
}, {
field: 'lastedit',
title: 'Last modified',
sortable: true,
formatter: function(value, row, index) {
var d = new Date(value*1000);
return d.toLocaleDateString() + " " + d.toLocaleTimeString();
}
}, {
field: 'size',
title: 'File size',
sortable: true,
formatter: function(value, row, index) { return humanFileSize(value); }
}],
sortName : 'lastedit',
sortOrder : 'desc',
data: <?php echo json_encode( $myGpxFileNames ) ?>
});
jQuery('.delete_gpx_row').click(function(){
return confirm("Are you sure you want to delete?");
})
function humanFileSize(bytes, si) {
var thresh = si ? 1000 : 1024;
if(Math.abs(bytes) < thresh) {
return bytes + ' B';
}
var units = si
? ['kB','MB','GB','TB','PB','EB','ZB','YB']
: ['KiB','MiB','GiB','TiB','PiB','EiB','ZiB','YiB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while(Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1)+' '+units[u];
}
</script>
<style>
#table tr:hover {
background:#eeeeee;
}
</style>