khanat-opennel-code/code/web/private_php/ams/autoload/plugincache.php

275 lines
7.2 KiB
PHP
Raw Normal View History

<?php
/**
* API for loading and interacting with plugins
* contains getters and setters.
2014-09-02 16:34:09 +00:00
*
* @author shubham meena mentored by Matthew Lagoe
*/
class Plugincache {
private $id;
private $plugin_name;
private $plugin_type;
private $plugin_permission;
private $plugin_status;
private $plugin_info = array();
private $update_info = array();
2014-09-02 16:34:09 +00:00
/**
* A constructor.
* Empty constructor
*/
public function __construct() {
2014-09-02 16:34:09 +00:00
}
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 -> setUpdateInfo( json_decode( $values['UpdateInfo'] ) );
2014-09-02 16:34:09 +00:00
}
/**
* loads the object's attributes.
*/
2014-09-02 16:34:09 +00:00
public function load_With_SID() {
$dbl = new DBLayer( "lib" );
$statement = $dbl -> executeWithoutParams( "SELECT * FROM plugins" );
$row = $statement -> fetch();
$this -> set( $row );
2014-09-02 16:34:09 +00:00
}
/**
* get plugin id attribute of the object.
2014-09-02 16:34:09 +00:00
*
* @return integer id
*/
public function getId() {
return $this -> Id;
2014-09-02 16:34:09 +00:00
}
/**
* get plugin permission attribute of the object.
*/
public function getPluginPermission() {
return $this -> plugin_permission;
2014-09-02 16:34:09 +00:00
}
/**
* get plugin Type attribute of the object.
*/
public function getPluginType() {
return $this -> plugin_version;
2014-09-02 16:34:09 +00:00
}
/**
* get plugin status attribute of the object.
*/
public function getPluginStatus() {
return $this -> plugin_status;
2014-09-02 16:34:09 +00:00
}
/**
* get plugin name attribute of the object.
*/
public function getPluginName() {
return $this -> plugin_name;
2014-09-02 16:34:09 +00:00
}
/**
* get plugin info array attribute of the object.
*/
public function getPluginInfo() {
return $this -> plugin_info;
2014-09-02 16:34:09 +00:00
}
/**
* set plugin id attribute of the object.
2014-09-02 16:34:09 +00:00
*
* @param $s integer id
*/
public function setId( $s ) {
$this -> Id = $s;
2014-09-02 16:34:09 +00:00
}
/**
* set plugin permission attribute of the object.
2014-09-02 16:34:09 +00:00
*
* @param $t type of the query, set permission
*/
public function setPluginPermission( $t ) {
$this -> plugin_permission = $t;
2014-09-02 16:34:09 +00:00
}
/**
* set plugin version attribute of the object.
2014-09-02 16:34:09 +00:00
*
* @param $q string to set plugin version
*/
public function setPluginType( $q ) {
$this -> plugin_version = $q;
2014-09-02 16:34:09 +00:00
}
/**
* set plugin status attribute of the object.
2014-09-02 16:34:09 +00:00
*
* @param $d status code type int
*/
public function setPluginStatus( $d ) {
$this -> plugin_status = $d;
2014-09-02 16:34:09 +00:00
}
/**
* set plugin name attribute of the object.
2014-09-02 16:34:09 +00:00
*
* @param $p_n string to set plugin name.
*/
public function setPluginName( $p_n ) {
$this -> plugin_name = $p_n;
2014-09-02 16:34:09 +00:00
}
/**
* set plugin info attribute array of the object.
2014-09-02 16:34:09 +00:00
*
* @param $p_n array
*/
public function setPluginInfo( $p_n ) {
$this -> plugin_info = $p_n;
2014-09-02 16:34:09 +00:00
}
/**
* functionalities for plugin updates
*/
2014-09-02 16:34:09 +00:00
/**
* set update info attribute array of the object.
2014-09-02 16:34:09 +00:00
*
* @param $p_n array
*/
public function setUpdateInfo( $p_n ) {
$this -> update_info = $p_n;
2014-09-02 16:34:09 +00:00
}
/**
* get plugin info array attribute of the object.
*/
public function getUpdateInfo() {
return $this -> update_info;
2014-09-02 16:34:09 +00:00
}
/**
* some more plugin function that requires during plugin operations
*/
2014-09-02 16:34:09 +00:00
/**
* function to remove a non empty directory
2014-09-02 16:34:09 +00:00
*
* @param $dir directory address
2014-09-02 16:34:09 +00:00
* @return boolean
*/
public static function rrmdir( $dir ) {
$result = array_diff( scandir( $dir ), array( '.', '..' ) );
foreach( $result as $item )
{
if ( !@unlink( $dir . '/' . $item ) )
Plugincache :: rrmdir( $dir . '/' . $item );
2014-09-02 16:34:09 +00:00
}
return rmdir( $dir );
2014-09-02 16:34:09 +00:00
}
/**
* function to unzip the zipped files
2014-09-02 16:34:09 +00:00
*
* @param $target_path path to the target zipped file
* @param $destination path to the destination
2014-09-02 16:34:09 +00:00
* @return boolean
*/
public static function zipExtraction( $target_path, $destination )
{
$zip = new ZipArchive();
$x = $zip -> open( $target_path );
if ( $x === true ) {
if ( $zip -> extractTo( $destination ) )
{
$zip -> close();
return true;
2014-09-02 16:34:09 +00:00
}
else
{
$zip -> close();
return false;
2014-09-02 16:34:09 +00:00
}
}
}
/**
* Returns plugin information with respect to the id.
2014-09-02 16:34:09 +00:00
*
* @param $id plugin id.
* @param $fieldName string plugin field to return
2014-09-02 16:34:09 +00:00
*
* @return info field from the db.
*/
public static function pluginInfoUsingId( $id, $fieldName )
{
$db = new DBLayer( 'lib' );
$sth = $db -> selectWithParameter( $fieldName, 'plugins', array( 'id' => $id ), 'Id=:id' );
$row = $sth -> fetch();
return $row[$fieldName];
2014-09-02 16:34:09 +00:00
}
/**
* Function provides list of active plugins
2014-09-02 16:34:09 +00:00
*
* @return list of active plugins
*/
public static function activePlugins()
{
$db = new DBLayer( 'lib' );
$sth = $db -> selectWithParameter( 'Id', 'plugins', array( 'status' => 1 ), 'Status=:status' );
$row = $sth -> fetchAll();
return $row;
2014-09-02 16:34:09 +00:00
}
/**
* function to load hooks for the active plugins
* and return the contents get from them.
2014-09-02 16:34:09 +00:00
*
* -->Get the list of active plugins then call the global
* hooks exists in the plugins hook file ($pluginName.php).
2014-09-02 16:34:09 +00:00
* -->Collect the contents from the hooks and associate within
* array with key referenced plugin name.
* -->return the content to use with smarty template loader
*
* @return $content content get from hooks
*/
2014-09-02 16:34:09 +00:00
public static function loadHooks() {
$content = array();
2014-09-02 16:34:09 +00:00
$ac_arr = Plugincache::activePlugins();
foreach ($ac_arr as $key => $value) {
$plugin_path = Plugincache::pluginInfoUsingId($value['Id'], 'FileName');
$template_path = json_decode(Plugincache::pluginInfoUsingId($value['Id'], 'Info'))->TemplatePath;
$plugin_name = $plugin_path;
// calling hooks in the $pluginName.php
global $AMS_PLUGINS;
include $AMS_PLUGINS . '/' . $plugin_name . '/' . $plugin_name . '.php';
$arr = get_defined_functions();
foreach ($arr['user'] as $key => $value) {
if (stristr( $value, $plugin_name) == true) {
$content['hook_info'][$plugin_name] = call_user_func($value);
}
}
// path for the template
$content['hook_info'][$plugin_name]['TemplatePath'] = $template_path;
2014-09-02 16:34:09 +00:00
}
return $content;
}
2014-09-02 16:34:09 +00:00
}