1
0
mirror of https://github.com/monstra-cms/monstra.git synced 2025-08-03 11:47:51 +02:00

Uploading new plugins via the admin panel #215

This commit is contained in:
metal_gvc
2014-02-09 21:09:40 +02:00
parent 9a6329a6cd
commit bcac217db3
3 changed files with 119 additions and 0 deletions

View File

@@ -203,4 +203,26 @@ class Dir
return $total_size;
}
/**
* Copy directory.
* <code>
* Dir::copy('source_folder_path', 'destination_folder_path);
* </code>
* @param $src
* @param $dst
*/
public static function copy($src, $dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src .'/'. $file) ) {
self::copy($src .'/'. $file, $dst .'/'. $file);
} else {
copy($src .'/'. $file,$dst .'/'. $file);
}
}
}
closedir($dir);
}
}

View File

@@ -1,5 +1,8 @@
<?php
Stylesheet::add('plugins/box/filesmanager/css/style.css', 'backend', 11);
Javascript::add('plugins/box/filesmanager/js/fileuploader.js', 'backend', 11);
// Add plugin navigation link
Navigation::add(__('Plugins', 'plugins'), 'extends', 'plugins', 1);
@@ -110,6 +113,65 @@ class PluginsAdmin extends Backend
}
// Upload & extract plugin archive
// -------------------------------------
if (Request::post('upload_file')) {
if (Security::check(Request::post('csrf'))) {
if ($_FILES['file']) {
if (in_array(File::ext($_FILES['file']['name']), array('zip'))) {
$tmp_dir = sys_get_temp_dir() . uniqid('monstra_');
$error = 'Plugin was not uploaded';
if (Dir::create($tmp_dir)) {
$file_locations = Zip::factory()->extract($_FILES['file']['tmp_name'], $tmp_dir);
if (!empty($file_locations)) {
$manifest = '';
foreach ($file_locations as $filepath) {
if (substr($filepath, -strlen('.manifest.xml')) === '.manifest.xml') {
$manifest = $filepath;
break;
}
}
if (!empty($manifest) && basename(dirname($manifest)) === 'install') {
$manifest_file = pathinfo($manifest, PATHINFO_BASENAME);
$plugin_name = str_replace('.manifest.xml', '', $manifest_file);
if (Dir::create(PLUGINS . DS . $plugin_name)) {
$tmp_plugin_dir = dirname(dirname($manifest));
Dir::copy($tmp_plugin_dir, PLUGINS . DS . $plugin_name);
Notification::set('success', __('Plugin was uploaded', 'plugins'));
$error = false;
}
}
}
} else {
$error = 'System error';
}
} else {
$error = 'Forbidden plugin file type';
}
} else {
$error = 'Plugin was not uploaded';
}
if ($error) {
Notification::set('error', __($error, 'plugins'));
}
if (Request::post('dragndrop')) {
Request::shutdown();
} else {
Request::redirect($site_url.'/admin/index.php?id=plugins');
}
} else { die('Request was denied because it contained an invalid security token. Please refresh the page and try again.'); }
}
// Installed plugins
$plugins_installed = array();
@@ -149,6 +211,11 @@ class PluginsAdmin extends Backend
->assign('installed_plugins', $installed_plugins)
->assign('plugins_to_intall', $plugins_to_intall)
->assign('_users_plugins', $_users_plugins)
->assign('fileuploader', array(
'uploadUrl' => $site_url.'/admin/index.php?id=plugins',
'csrf' => Security::token(),
'errorMsg' => __('Upload server error', 'filesmanager')
))
->display();
}
}

View File

@@ -1,6 +1,8 @@
<h2><?php echo __('Plugins', 'plugins'); ?></h2>
<br>
<input type="hidden" id="fUploaderInit" value='<?php echo json_encode($fileuploader); ?>' />
<div class="tabbable">
<!-- Plugins_tabs -->
@@ -93,6 +95,34 @@
<?php } ?>
</tbody>
</table>
<div class="row">
<div class="col-md-12">
<?php
echo (
Form::open(null, array('enctype' => 'multipart/form-data', 'class' => 'form-inline')).
Form::hidden('csrf', Security::token())
);
?>
<div class="fileupload fileupload-new fileupload-controls" data-provides="fileupload">
<button class="btn btn-default btn-file"><span class="fileupload-new"><?php echo __('Select file', 'plugins'); ?></span><span class="fileupload-exists"><?php echo __('Change', 'plugins'); ?></span><input type="file" name="file" /></button>
<?php
echo (
Form::submit('upload_file', __('Upload', 'plugins'), array('class' => 'btn btn-primary')).
Form::close()
);
?>
<span class="fileupload-preview"></span>
</div>
<div id="uploadArea" class="upload-area">
<div id="fuProgress" class="upload-progress"></div>
<div id="fuPlaceholder" class="upload-file-pholder"><?php echo __('Drop File Here', 'plugins'); ?></div>
</div>
<div id="fileInfo" class="upload-file-info"></div>
</div>
</div>
</div>
<!-- /Plugins_to_install_list -->