@ -3,11 +3,11 @@
/**
/**
* This module contains the function to install plugins
* This module contains the function to install plugins
* or check if the uploaded file is an update for a plugin.
* or check if the uploaded file is an update for a plugin.
*
*
* When user uploads a file with .zip extension(neccessary requirement)
* When user uploads a file with .zip extension(neccessary requirement)
* steps that should perform:
* steps that should perform:
* --> Check if the file type is .zip.
* --> Check if the file type is .zip.
* --> Extract it to a temp folder.
* --> Extract it to a temp folder.
* --> Check for the .info file. If not exists throw error
* --> Check for the .info file. If not exists throw error
* --> Extract the information from the .info file.
* --> Extract the information from the .info file.
* --> Check for the plugin name already exists or not.
* --> Check for the plugin name already exists or not.
@ -16,35 +16,35 @@
* --> if UpdateInfo not found throw error.
* --> if UpdateInfo not found throw error.
* --> if UpdateInfo found add the update to the ryzom_ams_lib.updates table.
* --> if UpdateInfo found add the update to the ryzom_ams_lib.updates table.
* --> if it's not an update and plugin with same name already exists throw error.
* --> if it's not an update and plugin with same name already exists throw error.
* --> if plugin with same name not present provide option to install plugin
* --> if plugin with same name not present provide option to install plugin
*
*
* @author Shubham Meena, mentored by Matthew Lagoe
* @author Shubham Meena, mentored by Matthew Lagoe
*
*
*/
*/
/**
/**
* This function is used in installing plugins or adding updates
* This function is used in installing plugins or adding updates
* for previously installed plugins.
* for previously installed plugins.
*
*
*/
*/
function install_plugin() {
function install_plugin() {
$result = array();
$result = array();
// if logged in
// if logged in
if ( WebUsers :: isLoggedIn() ) {
if ( WebUsers :: isLoggedIn() ) {
// path of temporary folder for storing files
// path of temporary folder for storing files
$temp_path = "../../ams_lib/temp";
$temp_path = "../../ams_lib/temp";
// create a temp directory if not exist
// create a temp directory if not exist
// temp folder where we first store all uploaded plugins before install
// temp folder where we first store all uploaded plugins before install
if ( !file_exists( "$temp_path" ) )
if ( !file_exists( "$temp_path" ) )
{
{
mkdir( $temp_path );
mkdir( $temp_path );
}
}
// checking the server if file is uploaded or not
// checking the server if file is uploaded or not
if ( ( isset( $_FILES["file"] ) ) & & ( $_FILES["file"]["size"] > 0 ) )
if ( ( isset( $_FILES["file"] ) ) & & ( $_FILES["file"]["size"] > 0 ) )
{
{
@ -53,26 +53,26 @@ function install_plugin() {
$dir = trim( $_FILES["file"]["name"], ".zip" );
$dir = trim( $_FILES["file"]["name"], ".zip" );
$target_path = "../../ams_lib/plugins/$dir"; //path in which the zip extraction is to be done
$target_path = "../../ams_lib/plugins/$dir"; //path in which the zip extraction is to be done
$destination = "../../ams_lib/plugins/";
$destination = "../../ams_lib/plugins/";
// scanning plugin folder if plugin with same name is already exists or not
// scanning plugin folder if plugin with same name is already exists or not
$x = checkForUpdate( $dir, $destination, $fileTmpLoc, $temp_path );
$x = checkForUpdate( $dir, $destination, $fileTmpLoc, $temp_path );
if ( $x == '1' )
if ( $x == '1' )
{
{
echo "update found";
echo "update found";
exit();
throw n ew SystemE xit();
}
}
else if ( $x == '2' )
else if ( $x == '2' )
{
{
echo "Plugin already exists with same name .";
echo "Plugin already exists with same name .";
exit();
throw n ew SystemE xit();
}
}
else if ( $x == '3' )
else if ( $x == '3' )
{
{
echo "Update info is not present in the update";
echo "Update info is not present in the update";
exit();
throw n ew SystemE xit();
}
}
// checking for the command to install plugin is given or not
// checking for the command to install plugin is given or not
if ( !isset( $_POST['install_plugin'] ) )
if ( !isset( $_POST['install_plugin'] ) )
{
{
@ -80,30 +80,30 @@ function install_plugin() {
{
{
if ( move_uploaded_file( $fileTmpLoc, $temp_path . "/" . $fileName ) ) {
if ( move_uploaded_file( $fileTmpLoc, $temp_path . "/" . $fileName ) ) {
echo "$fileName upload is complete.< / br > " . "< button type = 'submit' class = 'btn btn-primary' style = 'margin-left:5px; margin-top:10px;' name = 'install_plugin' > Install Plugin< / button > < / br > ";
echo "$fileName upload is complete.< / br > " . "< button type = 'submit' class = 'btn btn-primary' style = 'margin-left:5px; margin-top:10px;' name = 'install_plugin' > Install Plugin< / button > < / br > ";
exit();
throw n ew SystemE xit();
}
}
else
else
{
{
echo "Error in uploading file.";
echo "Error in uploading file.";
exit();
throw n ew SystemE xit();
}
}
}
}
else
else
{
{
echo "Please select a file with .zip extension to upload.";
echo "Please select a file with .zip extension to upload.";
exit();
throw n ew SystemE xit();
}
}
}
}
else
else
{
{
// calling function to unzip archives
// calling function to unzip archives
if ( zipExtraction( $temp_path . "/" . $fileName , $destination ) )
if ( zipExtraction( $temp_path . "/" . $fileName , $destination ) )
{
{
if ( file_exists( $target_path . "/.info" ) )
if ( file_exists( $target_path . "/.info" ) )
{
{
$result = readPluginFile( ".info", $target_path );
$result = readPluginFile( ".info", $target_path );
// sending all info to the database
// sending all info to the database
$install_result = array();
$install_result = array();
$install_result['FileName'] = $target_path;
$install_result['FileName'] = $target_path;
@ -112,52 +112,55 @@ function install_plugin() {
if ( Ticket_User :: isMod( unserialize( $_SESSION['ticket_user'] ) ) )
if ( Ticket_User :: isMod( unserialize( $_SESSION['ticket_user'] ) ) )
{
{
$install_result['Permission'] = 'admin';
$install_result['Permission'] = 'admin';
}
}
else
else
{
{
$install_result['Permission'] = 'user';
$install_result['Permission'] = 'user';
}
}
$install_result['Info'] = json_encode( $result );
$install_result['Info'] = json_encode( $result );
// connection with the database
// connection with the database
$dbr = new DBLayer( "lib" );
$dbr = new DBLayer( "lib" );
$dbr -> insert( "plugins", $install_result );
$dbr -> insert( "plugins", $install_result );
// if everything is successfull redirecting to the plugin template
// if everything is successfull redirecting to the plugin template
header("Cache-Control: max-age=1");
header( "Location: index.php?page=plugins& result=1" );
header( "Location: index.php?page=plugins& result=1" );
exit;
throw n ew SystemE xit() ;
}
}
else
else
{
{
// file .info not exists
// file .info not exists
rmdir( $target_path );
rmdir( $target_path );
header("Cache-Control: max-age=1");
header( "Location: index.php?page=install_plugin& result=2" );
header( "Location: index.php?page=install_plugin& result=2" );
exit;
throw n ew SystemE xit() ;
}
}
} else
} else
{
{
// extraction failed
// extraction failed
header("Cache-Control: max-age=1");
header( "Location: index.php?page=install_plugin& result=0" );
header( "Location: index.php?page=install_plugin& result=0" );
exit;
throw n ew SystemE xit() ;
}
}
}
}
}
}
else
else
{
{
echo "Please Browse for a file before clicking the upload button";
echo "Please Browse for a file before clicking the upload button";
exit();
throw n ew SystemE xit();
}
}
}
}
}
}
/**
/**
* function to unzip the zipped files
* function to unzip the zipped files
*
*
* @param $target_path path to the target zipped file
* @param $target_path path to the target zipped file
* @param $destination path to the destination
* @param $destination path to the destination
* @return boolean
* @return boolean
*/
*/
function zipExtraction( $target_path, $destination )
function zipExtraction( $target_path, $destination )
{
{
@ -168,31 +171,31 @@ function zipExtraction( $target_path, $destination )
{
{
$zip -> close();
$zip -> close();
return true;
return true;
}
}
else
else
{
{
$zip -> close();
$zip -> close();
return false;
return false;
}
}
}
}
}
}
/**
/**
* function to read text files and extract
* function to read text files and extract
* the information into an array
* the information into an array
*
*
* -----------------------------------------------------------
* -----------------------------------------------------------
* format:
* format:
* -----------------------------------------------------------
* -----------------------------------------------------------
* PluginName = Name of the plugin
* PluginName = Name of the plugin
* Version = version of the plugin
* Version = version of the plugin
* Type = type of the plugin
* Type = type of the plugin
* TemplatePath = path to the template
* TemplatePath = path to the template
* Description = Description of the plugin ,it's functionality
* Description = Description of the plugin ,it's functionality
* -----------------------------------------------------------
* -----------------------------------------------------------
*
*
* reads only files with name .info
* reads only files with name .info
*
*
* @param $fileName file to read
* @param $fileName file to read
* @param $target_path path to the folder containing .info file
* @param $target_path path to the folder containing .info file
* @return array containing above information in array(value => key)
* @return array containing above information in array(value => key)
@ -205,17 +208,17 @@ function readPluginFile( $fileName, $target_path )
$line_of_text = fgets( $file_handle );
$line_of_text = fgets( $file_handle );
$parts = array_map( 'trim', explode( '=', $line_of_text, 2 ) );
$parts = array_map( 'trim', explode( '=', $line_of_text, 2 ) );
@$result[$parts[0]] = $parts[1];
@$result[$parts[0]] = $parts[1];
}
}
fclose( $file_handle );
fclose( $file_handle );
return $result;
return $result;
}
}
/**
/**
* function to check for updates or
* function to check for updates or
* if the same plugin already exists
* if the same plugin already exists
* also, if the update founds ,check for the UpdateInfo in the .info file.
* also, if the update founds ,check for the UpdateInfo in the .info file.
* Update is saved in the temp directory with pluginName_version.zip
* Update is saved in the temp directory with pluginName_version.zip
*
*
* @param $fileName file which is uploaded in .zip extension
* @param $fileName file which is uploaded in .zip extension
* @param $findPath where we have to look for the installed plugins
* @param $findPath where we have to look for the installed plugins
* @param $tempFile path for the temporary file
* @param $tempFile path for the temporary file
@ -234,23 +237,23 @@ function checkForUpdate( $fileName, $findPath, $tempFile, $tempPath )
if ( !file_exists( $tempPath . "/test" ) )
if ( !file_exists( $tempPath . "/test" ) )
{
{
mkdir( $tempPath . "/test" );
mkdir( $tempPath . "/test" );
}
}
// extracting the update
// extracting the update
if ( zipExtraction( $tempFile, $tempPath . "/test/" ) )
if ( zipExtraction( $tempFile, $tempPath . "/test/" ) )
{
{
$result = readPluginFile( ".info", $tempPath . "/test/" . $fileName );
$result = readPluginFile( ".info", $tempPath . "/test/" . $fileName );
// check for the version for the plugin
// check for the version for the plugin
$db = new DBLayer( "lib" );
$db = new DBLayer( "lib" );
$sth = $db -> select( "plugins", array( 'Name' => $result['PluginName'] ), "Name = :Name" );
$sth = $db -> select( "plugins", array( 'Name' => $result['PluginName'] ), "Name = :Name" );
$info = $sth -> fetch();
$info = $sth -> fetch();
$info['Info'] = json_decode( $info['Info'] );
$info['Info'] = json_decode( $info['Info'] );
// the two versions from main plugin and the updated part
// the two versions from main plugin and the updated part
$new_version = explode( '.', $result['Version'] );
$new_version = explode( '.', $result['Version'] );
$pre_version = explode( '.', $info['Info'] -> Version );
$pre_version = explode( '.', $info['Info'] -> Version );
// For all plugins we have used semantic versioning
// For all plugins we have used semantic versioning
// Format: X.Y.Z ,X->Major, Y->Minor, Z->Patch
// Format: X.Y.Z ,X->Major, Y->Minor, Z->Patch
// change in the X Y & Z values refer the type of change in the plugin.
// change in the X Y & Z values refer the type of change in the plugin.
@ -265,22 +268,22 @@ function checkForUpdate( $fileName, $findPath, $tempFile, $tempPath )
// removing the uploaded file
// removing the uploaded file
Plugincache :: rrmdir( $tempPath . "/test/" . $fileName );
Plugincache :: rrmdir( $tempPath . "/test/" . $fileName );
return '2'; //plugin already exists
return '2'; //plugin already exists
}
}
else
else
{
{
// check for update info if exists
// check for update info if exists
if ( !array_key_exists( 'UpdateInfo', $result ) )
if ( !array_key_exists( 'UpdateInfo', $result ) )
{
{
return '3'; //update info tag not found
return '3'; //update info tag not found
}
}
else
else
{
{
// check if update already exists
// check if update already exists
if ( pluginUpdateExists( $info['Id'], $tempPath . "/" . trim( $fileName, ".zip" ) . "_" . $result['Version'] . ".zip" ) )
if ( pluginUpdateExists( $info['Id'], $tempPath . "/" . trim( $fileName, ".zip" ) . "_" . $result['Version'] . ".zip" ) )
{
{
echo "Update already exists";
echo "Update already exists";
exit;
throw n ew SystemE xit() ;
}
}
else {
else {
// removing the preivous update
// removing the preivous update
$dbr = new DBLayer( "lib" );
$dbr = new DBLayer( "lib" );
@ -293,24 +296,25 @@ function checkForUpdate( $fileName, $findPath, $tempFile, $tempPath )
$update['UpdatePath'] = $tempPath . "/" . trim( $fileName, ".zip" ) . "_" . $result['Version'] . ".zip";
$update['UpdatePath'] = $tempPath . "/" . trim( $fileName, ".zip" ) . "_" . $result['Version'] . ".zip";
$update['UpdateInfo'] = json_encode( $result );
$update['UpdateInfo'] = json_encode( $result );
$dbr -> insert( "updates", $update );
$dbr -> insert( "updates", $update );
header("Cache-Control: max-age=1");
header( "Location: index.php?page=plugins& result=7" );
header( "Location: index.php?page=plugins& result=7" );
exit;
throw n ew SystemE xit() ;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
/**
/**
* Function to check for the update of a plugin already exists
* Function to check for the update of a plugin already exists
*
*
* @param $pluginId id of the plugin for which update is available
* @param $pluginId id of the plugin for which update is available
* @param $updatePath path of the new update
* @param $updatePath path of the new update
* @return boolean True if update already exists else False
* @return boolean True if update already exists else False
*
*
*/
*/
function PluginUpdateExists( $pluginId, $updatePath )
function PluginUpdateExists( $pluginId, $updatePath )
{
{
@ -320,10 +324,10 @@ function PluginUpdateExists( $pluginId, $updatePath )
if ( $updatePath == $row['UpdatePath'] )
if ( $updatePath == $row['UpdatePath'] )
{
{
return true;
return true;
}
}
else
else
{
{
rmdir( $row['UpdatePath'] );
rmdir( $row['UpdatePath'] );
return false;
return false;
}
}
}
}