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

270 lines
7.1 KiB
PHP
Raw Normal View History

<?php
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* API for loading and interacting with plugins
* contains getters and setters
2014-06-11 06:22:57 +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();
2014-06-19 11:41:10 +00:00
private $update_info = array();
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* A constructor.
* Empty constructor
*/
public function __construct() {
}
public function set( $values ) {
$this -> setId( $values['Id'] );
$this -> setPluginName( $values['Name'] );
2014-06-19 11:41:10 +00:00
$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-06-11 06:22:57 +00:00
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* loads the object's attributes.
*/
2014-06-19 11:41:10 +00:00
2014-06-11 06:22:57 +00:00
public function load_With_SID() {
$dbl = new DBLayer( "lib" );
$statement = $dbl -> executeWithoutParams( "SELECT * FROM plugins" );
$row = $statement -> fetch();
$this -> set( $row );
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* get plugin id attribute of the object.
*
* @return integer id
*/
public function getId() {
return $this -> Id;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* get plugin permission attribute of the object.
*/
public function getPluginPermission() {
return $this -> plugin_permission;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* get plugin Type attribute of the object.
*/
public function getPluginType() {
return $this -> plugin_version;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* get plugin status attribute of the object.
*/
public function getPluginStatus() {
return $this -> plugin_status;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* get plugin name attribute of the object.
*/
public function getPluginName() {
return $this -> plugin_name;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* get plugin info array attribute of the object.
*/
public function getPluginInfo() {
return $this -> plugin_info;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* set plugin id attribute of the object.
*
* @param $s integer id
*/
public function setId( $s ) {
$this -> Id = $s;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* set plugin permission attribute of the object.
*
* @param $t type of the query, set permission
*/
public function setPluginPermission( $t ) {
$this -> plugin_permission = $t;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* set plugin version attribute of the object.
*
* @param $q string to set plugin version
*/
public function setPluginType( $q ) {
$this -> plugin_version = $q;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* set plugin status attribute of the object.
*
* @param $d status code type int
*/
public function setPluginStatus( $d ) {
$this -> plugin_status = $d;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* set plugin name attribute of the object.
*
* @param $p_n string to set plugin name.
*/
public function setPluginName( $p_n ) {
$this -> plugin_name = $p_n;
}
2014-06-01 19:35:37 +00:00
/**
2014-06-11 06:22:57 +00:00
* set plugin info attribute array of the object.
*
* @param $p_n array
*/
public function setPluginInfo( $p_n ) {
$this -> plugin_info = $p_n;
}
2014-06-01 19:35:37 +00:00
2014-06-19 11:41:10 +00:00
/**
* functionalities for plugin updates
*/
/**
2014-06-19 11:41:10 +00:00
* set update info attribute array of the object.
*
2014-06-19 11:41:10 +00:00
* @param $p_n array
*/
public function setUpdateInfo( $p_n ) {
$this -> update_info = $p_n;
}
/**
2014-07-03 11:14:37 +00:00
* get plugin info array attribute of the object.
2014-06-19 11:41:10 +00:00
*/
public function getUpdateInfo() {
return $this -> update_info;
}
/**
* some more plugin function that requires during plugin operations
*/
2014-06-19 11:41:10 +00:00
/**
* function to remove a non empty directory
*
* @param $dir directory address
* @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 );
}
return rmdir( $dir );
}
2014-07-03 11:14:37 +00:00
/**
* function to unzip the zipped files
*
* @param $target_path path to the target zipped file
* @param $destination path to the destination
* @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;
}
else
{
$zip -> close();
return false;
}
}
}
/**
* returns plugin information with respect to the id
*
* @param id $ plugin id
* @return field info for the plugin
*/
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];
}
/**
* function provides list of active plugins
*
* @return $ac_plugins 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-06-01 19:35:37 +00:00
/**
2014-07-03 11:14:37 +00:00
* function to load hooks for the active plugins
* and return the contents in the hooks in an array
*
* @return $content content available in hooks
*/
public static function loadHooks()
{
$content = array();
$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 = explode( '/', $plugin_path )[4];
2014-07-03 11:14:37 +00:00
// calling hooks in the $pluginName.php
include $plugin_path . '/' . $plugin_name . '.php';
2014-07-03 11:14:37 +00:00
$arr = get_defined_functions();
foreach( $arr['user'] as $key => $value )
{
if ( stristr( $value, $plugin_name ) == true )
2014-07-03 11:14:37 +00:00
{
$content['hook_info'][$plugin_name] = call_user_func( $value );
2014-07-03 11:14:37 +00:00
}
}
// path for the template
$content['hook_info'][$plugin_name]['TemplatePath'] = $template_path;
}
2014-07-03 11:14:37 +00:00
return $content;
}
}