updating functionality with some error fixes

This commit is contained in:
shubham_meena 2014-06-18 15:10:31 +05:30
parent 146ef5e4e0
commit 2998ce870f
6 changed files with 369 additions and 216 deletions

View file

@ -1,232 +1,245 @@
<?php
/**
* Handles the database connections. It uses PDO to connect to the different databases. It will use the argument of the constructor to setup a connection to the database
* with the matching entry in the $cfg global variable.
* @author Daan Janssens, mentored by Matthew Lagoe
*
*/
class DBLayer{
* Handles the database connections. It uses PDO to connect to the different databases. It will use the argument of the constructor to setup a connection to the database
* with the matching entry in the $cfg global variable.
*
* @author Daan Janssens, mentored by Matthew Lagoe
*/
class DBLayer {
private $PDO; /**< The PDO object, instantiated by the constructor */
private $PDO;
/**
* *< The PDO object, instantiated by the constructor
*/
/**
* The constructor.
* Instantiates the PDO object attribute by connecting to the arguments matching database(the db info is stored in the $cfg global var)
* @param $db String, the name of the databases entry in the $cfg global var.
*/
function __construct($db, $dbn = null)
{
if ($db != "install"){
global $cfg;
$dsn = "mysql:";
$dsn .= "host=". $cfg['db'][$db]['host'].";";
$dsn .= "dbname=". $cfg['db'][$db]['name'].";";
$dsn .= "port=". $cfg['db'][$db]['port'].";";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$this->PDO = new PDO($dsn,$cfg['db'][$db]['user'],$cfg['db'][$db]['pass'], $opt);
} else {
global $cfg;
$dsn = "mysql:";
$dsn .= "host=". $cfg['db'][$dbn]['host'].";";
$dsn .= "port=". $cfg['db'][$dbn]['port'].";";
$opt = array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$this->PDO = new PDO($dsn,$_POST['Username'],$_POST['Password'], $opt);
}
}
/**
* execute a query that doesn't have any parameters
* @param $query the mysql query
* @return returns a PDOStatement object
*/
public function executeWithoutParams($query){
$statement = $this->PDO->prepare($query);
$statement->execute();
return $statement;
}
/**
* execute a query that has parameters
* @param $query the mysql query
* @param $params the parameters that are being used by the query
* @return returns a PDOStatement object
*/
public function execute($query,$params){
$statement = $this->PDO->prepare($query);
$statement->execute($params);
return $statement;
}
/**
* execute a query (an insertion query) that has parameters and return the id of it's insertion
* @param $query the mysql query
* @param $params the parameters that are being used by the query
* @return returns the id of the last inserted element.
*/
public function executeReturnId($tb_name,$data){
$field_values =':'. implode(',:', array_keys($data));
$field_options = implode(',', array_keys($data));
try{
$sth = $this->PDO->prepare("INSERT INTO $tb_name ($field_options) VALUE ($field_values)");
foreach ($data as $key => $value )
{
$sth->bindValue(":$key", $value);
}
$this->PDO->beginTransaction();
//execution
$sth->execute();
$lastId =$this->PDO->lastInsertId();
$this->PDO->commit();
}catch (Exception $e)
{
//for rolling back the changes during transaction
$this->PDO->rollBack();
throw new Exception("error in inseting");
}
return $lastId;
}
/**
* The constructor.
* Instantiates the PDO object attribute by connecting to the arguments matching database(the db info is stored in the $cfg global var)
*
* @param $db String, the name of the databases entry in the $cfg global var.
*/
function __construct( $db, $dbn = null )
{
if ( $db != "install" ) {
global $cfg;
$dsn = "mysql:";
$dsn .= "host=" . $cfg['db'][$db]['host'] . ";";
$dsn .= "dbname=" . $cfg['db'][$db]['name'] . ";";
$dsn .= "port=" . $cfg['db'][$db]['port'] . ";";
$opt = array(
PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION,
PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC
);
$this -> PDO = new PDO( $dsn, $cfg['db'][$db]['user'], $cfg['db'][$db]['pass'], $opt );
} else {
global $cfg;
$dsn = "mysql:";
$dsn .= "host=" . $cfg['db'][$dbn]['host'] . ";";
$dsn .= "port=" . $cfg['db'][$dbn]['port'] . ";";
$opt = array(
PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION,
PDO :: ATTR_DEFAULT_FETCH_MODE => PDO :: FETCH_ASSOC
);
$this -> PDO = new PDO( $dsn, $_POST['Username'], $_POST['Password'], $opt );
}
}
/**
* execute a query that doesn't have any parameters
*
* @param $query the mysql query
* @return returns a PDOStatement object
*/
public function executeWithoutParams( $query ) {
$statement = $this -> PDO -> prepare( $query );
$statement -> execute();
return $statement;
}
/**
* execute a query that has parameters
*
* @param $query the mysql query
* @param $params the parameters that are being used by the query
* @return returns a PDOStatement object
*/
public function execute( $query, $params ) {
$statement = $this -> PDO -> prepare( $query );
$statement -> execute( $params );
return $statement;
}
/**
* execute a query (an insertion query) that has parameters and return the id of it's insertion
*
* @param $query the mysql query
* @param $params the parameters that are being used by the query
* @return returns the id of the last inserted element.
*/
public function executeReturnId( $tb_name, $data ) {
$field_values = ':' . implode( ',:', array_keys( $data ) );
$field_options = implode( ',', array_keys( $data ) );
try {
$sth = $this -> PDO -> prepare( "INSERT INTO $tb_name ($field_options) VALUE ($field_values)" );
foreach ( $data as $key => $value )
{
$sth -> bindValue( ":$key", $value );
}
$this -> PDO -> beginTransaction();
$sth -> execute();
$lastId = $this -> PDO -> lastInsertId();
$this -> PDO -> commit();
}
catch ( Exception $e )
{
// for rolling back the changes during transaction
$this -> PDO -> rollBack();
throw new Exception( "error in inseting" );
}
return $lastId;
}
/**
* Select function using prepared statement
*
* @param string $tb_name Table Name to Select
* @param array $data Associative array
* @param string $where where to select
* @return statement object
*/
public function selectWithParameter($param, $tb_name, $data, $where)
{
try{
$sth = $this->PDO->prepare("SELECT $param FROM $tb_name WHERE $where");
$this->PDO->beginTransaction();
$sth->execute($data);
$this->PDO->commit();
}catch(Exception $e)
{
$this->PDO->rollBack();
throw new Exception("error selection");
return false;
}
public function selectWithParameter( $param, $tb_name, $data, $where )
{
try {
$sth = $this -> PDO -> prepare( "SELECT $param FROM $tb_name WHERE $where" );
$this -> PDO -> beginTransaction();
$sth -> execute( $data );
$this -> PDO -> commit();
}
catch( Exception $e )
{
$this -> PDO -> rollBack();
throw new Exception( "error selection" );
return false;
}
return $sth;
}
}
/**
*
* Select function using prepared statement
*
* @param string $tb_name Table Name to Select
* @param array $data Associative array
* @param string $where where to select
* @return statement object
*/
public function select($tb_name, $data ,$where)
{
try{
$sth = $this->PDO->prepare("SELECT * FROM $tb_name WHERE $where");
$this->PDO->beginTransaction();
$sth->execute($data);
$this->PDO->commit();
}catch(Exception $e)
{
$this->PDO->rollBack();
throw new Exception("error selection");
return false;
}
public function select( $tb_name, $data , $where )
{
try {
$sth = $this -> PDO -> prepare( "SELECT * FROM $tb_name WHERE $where" );
$this -> PDO -> beginTransaction();
$sth -> execute( $data );
$this -> PDO -> commit();
}
catch( Exception $e )
{
$this -> PDO -> rollBack();
throw new Exception( "error selection" );
return false;
}
return $sth;
}
}
/**
*
* Update function with prepared statement
*
* @param string $tb_name name of the table
* @param array $data associative array with values
* @param string $where where part
* @throws Exception error in updating
*/
public function update($tb_name, $data, $where)
{
$field_option_values=null;
foreach ($data as $key => $value)
{
$field_option_values.=",$key".'=:'.$value;
}
$field_option_values = ltrim($field_option_values,',');
try {
$sth = $this->PDO->prepare("UPDATE $tb_name SET $field_option_values WHERE $where ");
foreach ($data as $key => $value)
{
$sth->bindValue(":$key", $value);
}
$this->PDO->beginTransaction();
$sth->execute();
$this->PDO->commit();
}catch (Exception $e)
{
$this->PDO->rollBack();
throw new Exception('error in updating');
}
}
public function update( $tb_name, $data, $where )
{
$field_option_values = null;
foreach ( $data as $key => $value )
{
$field_option_values .= ",$key" . '=:' . $key;
}
$field_option_values = ltrim( $field_option_values, ',' );
try {
$sth = $this -> PDO -> prepare( "UPDATE $tb_name SET $field_option_values WHERE $where " );
foreach ( $data as $key => $value )
{
$sth -> bindValue( ":$key", $value );
}
$this -> PDO -> beginTransaction();
$sth -> execute();
$this -> PDO -> commit();
}
catch ( Exception $e )
{
$this -> PDO -> rollBack();
throw new Exception( 'error in updating' );
return false;
}
return true;
}
/**
*
* insert function using prepared statements
*
* @param string $tb_name Name of the table to insert in
* @param array $data Associative array of data to insert
*/
public function insert($tb_name, $data)
{
$field_values =':'. implode(',:', array_keys($data));
$field_options = implode(',', array_keys($data));
try{
$sth = $this->PDO->prepare("INSERT INTO $tb_name ($field_options) VALUE ($field_values)");
foreach ($data as $key => $value )
{
$sth->bindValue(":$key", $value);
}
$this->PDO->beginTransaction();
//execution
$sth->execute();
$this->PDO->commit();
}catch (Exception $e)
{
//for rolling back the changes during transaction
$this->PDO->rollBack();
throw new Exception("error in inseting");
}
}
public function insert( $tb_name, $data )
{
$field_values = ':' . implode( ',:', array_keys( $data ) );
$field_options = implode( ',', array_keys( $data ) );
try {
$sth = $this -> PDO -> prepare( "INSERT INTO $tb_name ($field_options) VALUE ($field_values)" );
foreach ( $data as $key => $value )
{
$sth -> bindValue( ":$key", $value );
}
$this -> PDO -> beginTransaction();
// execution
$sth -> execute();
$this -> PDO -> commit();
}
catch ( Exception $e )
{
// for rolling back the changes during transaction
$this -> PDO -> rollBack();
throw new Exception( "error in inseting" );
}
}
/**
*
* Delete database entery using prepared statement
* @param string $tb_name
* @param string $where
*
* @param string $tb_name
* @param string $where
* @throws error in deleting
*/
public function delete($tb_name, $where)
{
public function delete( $tb_name, $data, $where )
{
try {
$sth = $this->prepare("DELETE FROM $tb_name WHERE $where");
$this->PDO->beginTransaction();
$sth->execute();
$this->PDO->commit();
}
catch (Exception $e)
{
$this->rollBack();
throw new Exception("error in deleting");
}
}
}
$sth = $this -> PDO -> prepare( "DELETE FROM $tb_name WHERE $where" );
$this -> PDO -> beginTransaction();
$sth -> execute( $data );
$this -> PDO -> commit();
}
catch ( Exception $e )
{
$this -> rollBack();
throw new Exception( "error in deleting" );
}
}
}

View file

@ -26,10 +26,10 @@ class Plugincache {
public function set( $values ) {
$this -> setId( $values['Id'] );
$this -> setPluginName( $values['Name'] );
$this -> setPluginType( $values['Type'] );
$this -> setPluginPermission( $values['Permission'] );
$this -> setPluginStatus( $values['Status'] );
$this -> setPluginInfo( json_decode( $values['Info'] ) );
$this -> setPluginType( $values['Type'] );
$this -> setPluginPermission( $values['Permission'] );
$this -> setPluginStatus( $values['Status'] );
$this -> setPluginInfo( json_decode( $values['Info'] ) );
}
/**
@ -140,4 +140,30 @@ class Plugincache {
$this -> plugin_info = $p_n;
}
/**
* some more plugin function that requires during plugin operations
*
* /
*
*
* /**
* function to remove a non empty directory
*
* @param $dir directory address
* @return boolean
*/
public static function rrmdir( $dir ) {
if ( is_dir( $dir ) ) {
$objects = scandir( $dir );
foreach ( $objects as $object ) {
if ( $object != "." && $object != ".." ) {
if ( filetype( $dir . "/" . $object ) == "dir" ) rmdir( $dir . "/" . $object );
else unlink( $dir . "/" . $object );
}
}
reset( $objects );
return rmdir( $dir );
}
}
}

View file

@ -5,6 +5,9 @@
login_info = "Please enter your MySQL Username and Password to install the database.<br>This is being loaded because the is_installed file is missing.<br>This process will take about 30 seconds."
login_here = "here"
[ams_content]
ams_title="Ryzom Account Mangement System"
[dashboard]
home_title = "Introduction"
home_info = "Welcome to the Ryzom Core - Account Management System"
@ -66,11 +69,12 @@ plugin_status = "Status"
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"
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."
up_success = "Update added successfully. Go to Updates page for installing updates."
[install_plugin]
ip_title = "Install a new Plugin"
@ -79,6 +83,15 @@ ip_support = "Upload the plugin archieve to install.</br>The following file exte
ip_info_nfound = "Info file not found in the Plugin.Please recheck"
ip_file_nfnd="Please upload a plugin before clicking on install button"
[plugins_update]
up_title = "Updates for Plugins"
up_info = "Here you can see the entire list of available updates for plugins."
up_serial = "Serial No."
plugin_name = "Name"
plugin_version = "Version"
up_updated_version = "New Version"
up_action = "Actions"
[show_ticket]
t_title = "Ticket"
title = "Title"
@ -152,8 +165,8 @@ go_home = "Go Home"
userlist_info = "welcome to the userlist"
[login]
login_info = "Please login with your Username and Password."
login_error_message = "The username/password were not correct!"
login_info = "Please login with your Email/Username and Password."
login_error_message = "The Email/username/password were not correct!"
login_register_message ="<strong>Register</strong> If you don't have an account yet, create one"
login_here = "here"
login_forgot_password_message = "In case you forgot your password, click"

View file

@ -8,7 +8,9 @@
*/
function install_plugin() {
// if logged in
$result = array();
// if logged in
if ( WebUsers :: isLoggedIn() ) {
// path of temporary folder for storing files
@ -30,13 +32,32 @@ function install_plugin() {
$target_path = "../../ams_lib/plugins/$dir"; //path in which the zip extraction is to be done
$destination = "../../ams_lib/plugins/";
// checking for the command to install plugin is given or not
// scanning plugin folder if plugin with same name is already exists or not
$x = checkForUpdate( $dir, $destination, $fileTmpLoc, $temp_path );
if ( $x == '1' )
{
echo "update found";
exit();
}
else if ( $x == '2' )
{
echo "Plugin already exists with same name .";
exit();
}
else if ( $x == '3' )
{
echo "Update info is not present in the update";
exit();
}
// 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.";
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();
}
else
@ -59,15 +80,14 @@ function install_plugin() {
{
if ( file_exists( $target_path . "/.info" ) )
{
$result = array();
$result = readPluginFile( ".info", $target_path );
$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';
}
@ -165,4 +185,86 @@ function readPluginFile( $fileName, $target_path )
}
fclose( $file_handle );
return $result;
}
}
/**
* function to check for updates or
* if the same plugin already exists
* also, if the update founds ,check for the update info in the .info file.
* Update is saved in the temp direcotry with pluginName_version.zip
*
* @param $fileName file which is uploaded in .zip extension
* @param $findPath where we have to look for the installed plugins
* @param $tempFile path for the temporary file
* @param $tempPath path where we have to store the update
* @return 2 if plugin already exists and update not found
* @return 3 if update info tag not found in .info file
*/
function checkForUpdate( $fileName, $findPath, $tempFile, $tempPath )
{
// check for plugin if exists
$file = scandir( $findPath );
foreach( $file as $key => $value )
{
if ( strcmp( $value, $fileName ) == 0 )
{
if ( !file_exists( $tempPath . "/test" ) )
{
mkdir( $tempPath . "/test" );
}
if ( zipExtraction( $tempFile, $tempPath . "/test/" ) )
{
$result = readPluginFile( ".info", $tempPath . "/test/" . $fileName );
// check for the version for the plugin
$db = new DBLayer( "lib" );
$sth = $db -> select( "plugins", array( ':name' => $result['PluginName'] ), "Name = :name" );
$info = $sth -> fetch();
$info['Info'] = json_decode( $info['Info'] );
// the two versions from main plugin and the updated part
$new_version = explode( '.', $result['Version'] );
$pre_version = explode( '.', $info['Info'] -> Version );
// For all plugins we have used semantic versioning
// 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.
// for initial development only Minor an Patch MUST be 0.
// if there is bug fix then there MUST be an increment in the Z value.
// if there is change in the functionality or addition of new functionality
// then there MUST be an increment in the Y value.
// When there is increment in the X value , Y and Z MUST be 0.
// comparing if there is some change
if ( !array_intersect( $new_version , $pre_version ) )
{
// removing the uploaded file
Plugincache :: rrmdir( $tempPath . "/test/" . $fileName );
return '2';
}
else
{
// check for update info if exists
if ( !array_key_exists( 'UpdateInfo', $result ) )
{
return '3'; //update info tag not found
}
else
{
// storing update in the temp directory
// format of update save
if ( move_uploaded_file( $tempFile, $tempPath . "/" . trim( $fileName, ".zip" ) . "_" . $result['Version'] . ".zip" ) ) {
// setting update information in the database
$dbr = new DBLayer( "lib" );
$update['PluginId'] = $info['Id'];
$update['UpdatePath'] = $tempPath . "/" . trim( $fileName, ".zip" ) . "_" . $result['Version'] . ".zip";
$update['UpdateInfo'] = json_encode( $result );
$dbr -> insert( "updates", $update );
header( "Location: index.php?page=plugins&result=7" );
exit;
}
}
}
}
}
}
}

View file

@ -22,8 +22,7 @@
<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;" name="install_plugin">Install Plugin</button></br>
{if isset($smarty.get.result) and $smarty.get.result eq "2"}<p>{$ip_info_nfound}</p>{/if}
</div>
{$ip_message}
</center>

View file

@ -16,6 +16,7 @@
{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}
{if isset($smarty.get.result) and $smarty.get.result eq "7"}<div class="alert alert-error"><p>{$up_success}</p></div>{/if}
<div class="box-content">
<center><p>{$plugin_info}</p></center>
<center>
@ -65,4 +66,3 @@
</div><!--/span-->
</div><!--/row-->
{/block}