Plugin installation first upload t{block name=content}

This commit is contained in:
shubham_meena 2014-06-13 16:40:27 +05:30
parent 60d9b16a4a
commit dd11b78476
5 changed files with 189 additions and 53 deletions

View file

@ -61,16 +61,23 @@ plugin_name = "Name"
plugin_version = "Version"
plugin_description = "Description"
plugin_type = "Type"
plugin_permission = "Access Permission"
plugin_permission = "Access</br> Permission"
plugin_status = "Status"
ip_success = "Plugin added succesfully."
ip_success = "Plugin added succesfuly."
plugin_actions = "Actions"
dp_success = "Plugin deleted successfuly"
dp_error = "Error in deleting plugin.Please try again later"
ac_success = "Plugin Activated successfuly"
ac_error = "Plugin facing some error in activating. Please try again later"
dc_success = "Plugin de-Activated successfuly"
dc_error = "Plugin facing some error in de-activating. Please try again later"
[install_plugin]
ip_title = "Install a new Plugin"
ip_message = "For example: name.zip from your local computer"
ip_support = "Upload the plugin archieve to install.</br>The following file extension is supported: zip."
ip_error = "Please select the format with zip extension"
ip_info_nfound = "Info file not found in the Plugin.Please recheck"
ip_file_nfnd="Please upload a plugin before clicking on install button"
[show_ticket]
t_title = "Ticket"

View file

@ -11,7 +11,18 @@ function install_plugin() {
// if logged in
if ( WebUsers :: isLoggedIn() ) {
if ( ( isset( $_FILES["file"] ) ) && ( $_FILES["file"]["size"] > 0 ) && ( $_FILES["file"]["type"] == 'application/zip' ) )
// path of temporary folder for storing files
$temp_path = "../../ams_lib/temp";
// create a temp directory if not exist
// temp folder where we first store all uploaded plugins before install
if ( !file_exists( "$temp_path" ) )
{
mkdir( $temp_path );
}
// checking the server if file is uploaded or not
if ( ( isset( $_FILES["file"] ) ) && ( $_FILES["file"]["size"] > 0 ) )
{
$fileName = $_FILES["file"]["name"]; //the files name takes from the HTML form
$fileTmpLoc = $_FILES["file"]["tmp_name"]; //file in the PHP tmp folder
@ -19,40 +30,44 @@ function install_plugin() {
$target_path = "../../ams_lib/plugins/$dir"; //path in which the zip extraction is to be done
$destination = "../../ams_lib/plugins/";
if ( move_uploaded_file( $fileTmpLoc, $destination . $fileName ) ) {
// zip object to handle zip archieves
$zip = new ZipArchive();
$x = $zip -> open( $destination . $fileName );
if ( $x === true ) {
$zip -> extractTo( $destination ); // change this to the correct site path
$zip -> close();
// checking for the command to install plugin is given or not
if ( !isset( $_POST['install_plugin'] ) )
{
if ( ( $_FILES["file"]["type"] == 'application/zip' ) )
{
if ( move_uploaded_file( $fileTmpLoc, $temp_path . "/" . $fileName ) ) {
echo "$fileName upload is complete.";
exit();
}
else
{
echo "Error in uploading file.";
exit();
}
}
else
{
echo "Please select a file with .zip extension to upload.";
exit();
}
}
else
{
// removing the uploaded zip file
unlink( $destination . $fileName );
// check for the info file
// calling function to unzip archives
if ( zipExtraction( $temp_path . "/" . $fileName , $destination ) )
{
if ( file_exists( $target_path . "/.info" ) )
{
// read the details of the plugin through the info file
$file_handle = fopen( $target_path . "/.info", "r" );
$result = array();
while ( !feof( $file_handle ) ) {
$line_of_text = fgets( $file_handle );
$parts = array_map( 'trim', explode( '=', $line_of_text, 2 ) );
@$result[$parts[0]] = $parts[1];
}
fclose( $file_handle );
$result = array();
$result = readPluginFile( ".info", $target_path );
// sending all info to the database
$install_result = array();
$install_result['FileName'] = $target_path;
$install_result['Name'] = $result['PluginName'];
$install_result['Type'] = $result['type'];
if ( Ticket_User :: isMod( unserialize( $_SESSION['ticket_user'] ) ) )
// $install_result['Type'] = $result['type'];
if ( Ticket_User :: isMod( unserialize( $_SESSION['ticket_user'] ) ) )
{
$install_result['Permission'] = 'admin';
}
@ -67,28 +82,87 @@ function install_plugin() {
$dbr = new DBLayer( "lib" );
$dbr -> insert( "plugins", $install_result );
header( "Location: index.php?page=plugins&result=1" );
// if everything is successfull redirecting to the plugin template
header( "Location: index.php?page=plugins&result=1" );
exit;
}
else
{
// file .info not exists
rmdir( $target_path );
header( "Location: index.php?page=install_plugin&result=2" );
exit;
}
}
} else
{
// extraction failed
header( "Location: index.php?page=install_plugin&result=0" );
exit;
}
}
header( "Location: index.php?page=plugins" );
exit;
}
}
else
{
header( "Location: index.php?page=install_plugin&result=1" );
exit;
echo "Please Browse for a file before clicking the upload button";
exit();
}
}
}
/**
* function to unzip the zipped files
*
* @param $target_path path to the target zipped file
* @param $destination path to the destination
* @return boolean
*/
function zipExtraction( $target_path, $destination )
{
$zip = new ZipArchive();
$x = $zip -> open( $target_path );
if ( $x === true ) {
if ( $zip -> extractTo( $destination ) )
{
$zip -> close();
return true;
}
else
{
$zip -> close();
return false;
}
}
}
/**
* function to read text files and extract
* the information into an array
*
* -----------------------------------------------------------
* format:
* -----------------------------------------------------------
* PluginName = Name of the plugin
* Version = version of the plugin
* Type = type of the plugin
* Description = Description of the plugin ,it's functionality
* -----------------------------------------------------------
*
* reads only files with name .info
*
* @param $fileName file to read
* @param $targetPath path to the folder containing .info file
* @return array containing above information in array(value => key)
*/
function readPluginFile( $fileName, $target_path )
{
$file_handle = fopen( $target_path . "/" . $fileName, "r" );
$result = array();
while ( !feof( $file_handle ) ) {
$line_of_text = fgets( $file_handle );
$parts = array_map( 'trim', explode( '=', $line_of_text, 2 ) );
@$result[$parts[0]] = $parts[1];
}
fclose( $file_handle );
return $result;
}

View file

@ -15,12 +15,15 @@
<center>
<p>{$ip_support}</p>
<div class="alert alert-error">
<form enctype="multipart/form-data" method="post" action="index.php?page=plugin&action=install_plugin" >
<form enctype="multipart/form-data" method="post" action="index.php?page=plugin&action=install_plugin" id="upload_plugin" >
<label for="file">Filename:</label>&nbsp;&nbsp;
<input type="file" name="file" id="file"></br>
{if isset($smarty.get.result) and $smarty.get.result eq "1"}<p>{$ip_error}</p>{/if}
<progress id="progressBar" value="0" max="100" style="width:300px;"></progress></br>
<input type="button" value="Upload" onclick="uploadPlugin()"></br>
<h3 id="status"></h3>
{if isset($smarty.get.result) and $smarty.get.result eq "0"}<p>{$ip_file_nfnd}</p>{/if}
{if isset($smarty.get.result) and $smarty.get.result eq "2"}<p>{$ip_info_nfound}</p>{/if}
<button type="submit" class="btn btn-primary" style="margin-left:5px; margin-top:10px;">Install Plugin</button></br>
<button type="submit" class="btn btn-primary" style="margin-left:5px; margin-top:10px;" name="install_plugin">Install Plugin</button></br>
</div>
{$ip_message}
</center>

View file

@ -192,6 +192,50 @@
}
</script>
<!-- script for file uploading-->
<script>
function _(e1)
{
return document.getElementById(e1);
}
function uploadPlugin()
{
var fileObject = _("file").files[0];
var formdata = new FormData();
formdata.append("file",fileObject);
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", progressHandler, false);
ajax.addEventListener("load", completeHandler, false);
ajax.addEventListener("error", errorHandler, false);
ajax.addEventListener("abort", abortHandler, false);
ajax.open("POST", "index.php?page=plugin&action=install_plugin");
ajax.send(formdata);
}
function progressHandler(event)
{
var percent = (event.loaded/event.total)*100;
_("progressBar").value = Math.round(percent);
}
function completeHandler(event)
{
_("status").innerHTML = event.target.responseText;
_("progressBar").value = 0;
}
function errorHandler(event)
{
_("status").innerHTML = "upload Failed";
}
function abortHandler(event)
{
_("status").innerHTML = "upload Aborted";
}
</script>
<!-- jQuery -->
<script src="js/jquery-1.7.2.min.js"></script>
<!-- jQuery UI -->

View file

@ -10,23 +10,28 @@
</div>
</div>
{if isset($smarty.get.result) and $smarty.get.result eq "1"}<div class="alert alert-error"><p>{$ip_success}</p></div>{/if}
{if isset($smarty.get.result) and $smarty.get.result eq "0"}<div class="alert alert-error"><p>{$dp_error}</p></div>{/if}
{if isset($smarty.get.result) and $smarty.get.result eq "2"}<div class="alert alert-error"><p>{$dp_success}</p></div>{/if}
{if isset($smarty.get.result) and $smarty.get.result eq "3"}<div class="alert alert-error"><p>{$ac_success}</p></div>{/if}
{if isset($smarty.get.result) and $smarty.get.result eq "4"}<div class="alert alert-error"><p>{$ac_error}</p></div>{/if}
{if isset($smarty.get.result) and $smarty.get.result eq "5"}<div class="alert alert-error"><p>{$dc_success}</p></div>{/if}
{if isset($smarty.get.result) and $smarty.get.result eq "6"}<div class="alert alert-error"><p>{$dc_error}</p></div>{/if}
<div class="box-content">
<center><p>{$plugin_info}</p></center>
<center><a href="index.php?page=plugins&action=deletePlugins"><button class="btn btn-primary btn-large">Delete</button></a>
<a href="index.php?page=plugins&action=activatePlugins"><button class="btn btn-primary btn-large dropdown-toggle">Activate</button></a>
<a href="index.php?page=plugins&action=deactivatePlugins"><button class="btn btn-primary btn-large dropdown-toggle">Deactivate</button></a>
<a href="index.php?page=install_plugin"><button class="btn btn-primary btn-large dropdown-toggle">Add</button></a>
<a href="index.php?page=plugins&action=updatePlugins"><button class="btn btn-primary btn-large dropdown-toggle">Check for updates</button></a>
</center>
<center>
<a href="index.php?page=install_plugin"><button class="btn btn-primary btn-large dropdown-toggle">Install New Plugin</button></a>
<a href="index.php?page=plugins_update"><button class="btn btn-primary btn-large dropdown-toggle">Check for updates</button></a>
</center>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>{$plugin_status}</th>
<th width="150">{$plugin_name}</th>
<th width="100">{$plugin_name}</th>
<th>{$plugin_version}</th>
<th width="400">{$plugin_description}</th>
<th>{$plugin_type}</th>
<th width="350">{$plugin_description}</th>
<th width="80">{$plugin_type}</th>
<th>{$plugin_permission}</th>
<th>{$plugin_actions}</th>
</tr>
</thead>
<tbody>
@ -38,6 +43,9 @@
<td class="center">{$element.plugin_info->Description}</td>
<td class="center">{$element.plugin_type}</td>
<td class="center">{$element.plugin_permission}</td>
<td><a href="index.php?page=plugins&action=delete_plugin&id={$element.id}"><button class="btn btn-primary btn-large">Delete</button></a>
{if ($element.plugin_status) eq "0"}<a href="index.php?page=plugins&action=activate_plugin&id={$element.id}"><button class="btn btn-primary btn-large dropdown-toggle">Activate</button></a>{/if}
{if ($element.plugin_status) eq "1"}<a href="index.php?page=plugins&action=deactivate_plugin&id={$element.id}"><button class="btn btn-primary btn-large dropdown-toggle">Deactivate</button></a>{/if}</td>
</tr>
{/foreach}