From cf6c9b66839dddba4257eb78cbf96a1fa579de92 Mon Sep 17 00:00:00 2001 From: shubham_meena Date: Thu, 3 Jul 2014 16:44:37 +0530 Subject: [PATCH] hook functionality --- .../ryzom_ams/ams_lib/autoload/dblayer.php | 2 +- .../ryzom_ams/ams_lib/autoload/helpers.php | 407 +++++++++--------- .../ams_lib/autoload/plugincache.php | 89 +++- .../ryzom_ams/www/html/func/update_plugin.php | 1 - .../tools/server/ryzom_ams/www/html/index.php | 16 +- .../www/html/templates/layout_admin.tpl | 2 + .../www/html/templates/layout_plugin.tpl | 12 + 7 files changed, 322 insertions(+), 207 deletions(-) create mode 100644 code/ryzom/tools/server/ryzom_ams/www/html/templates/layout_plugin.tpl diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php index 63b6bfc81..43282789e 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/dblayer.php @@ -237,7 +237,7 @@ class DBLayer { } catch ( Exception $e ) { - $this -> rollBack(); + $this -> PDO -> rollBack(); throw new Exception( "error in deleting" ); } diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/helpers.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/helpers.php index 2d26f3c21..3410cf602 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/helpers.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/helpers.php @@ -1,224 +1,241 @@ setCompileDir($SITEBASE.'/templates_c/'); - $smarty->setCacheDir($SITEBASE.'/cache/'); - $smarty -> setConfigDir($SITEBASE . '/configs/' ); + $smarty -> setCompileDir( $SITEBASE . '/templates_c/' ); + $smarty -> setCacheDir( $SITEBASE . '/cache/' ); + $smarty -> setConfigDir( $SITEBASE . '/configs/' ); // turn smarty debugging on/off - $smarty -> debugging = false; + $smarty -> debugging = false; // caching must be disabled for multi-language support - $smarty -> caching = false; + $smarty -> caching = false; $smarty -> cache_lifetime = 5; - - //needed by smarty. - helpers :: create_folders (); - global $FORCE_INGAME; - - //if ingame, then use the ingame templates - if ( helpers::check_if_game_client() or $FORCE_INGAME ){ - $smarty -> template_dir = $AMS_LIB . '/ingame_templates/'; + + // needed by smarty. + helpers :: create_folders (); + global $FORCE_INGAME; + + // if ingame, then use the ingame templates + if ( helpers :: check_if_game_client() or $FORCE_INGAME ) { + $smarty -> template_dir = $AMS_LIB . '/ingame_templates/'; $smarty -> setConfigDir( $AMS_LIB . '/configs' ); $variables = parse_ini_file( $AMS_LIB . '/configs/ingame_layout.ini', true ); - foreach ( $variables[$INGAME_LAYOUT] as $key => $value ){ - $smarty -> assign( $key, $value ); - } - }else{ - $smarty -> template_dir = $SITEBASE . '/templates/'; + foreach ( $variables[$INGAME_LAYOUT] as $key => $value ) { + $smarty -> assign( $key, $value ); + } + } else { + $smarty -> template_dir = $SITEBASE . '/templates/'; $smarty -> setConfigDir( $SITEBASE . '/configs' ); - } - - foreach ( $vars as $key => $value ){ - $smarty -> assign( $key, $value ); - } - - //load page specific variables that are language dependent - $variables = Helpers::handle_language(); - foreach ( $variables[$template] as $key => $value ){ - $smarty -> assign( $key, $value ); - } - - //smarty inheritance for loading the matching wrapper layout (with the matching menu bar) - if( isset($vars['permission']) && $vars['permission'] == 3 ){ - $inherited = "extends:layout_admin.tpl|"; - }else if( isset($vars['permission']) && $vars['permission'] == 2){ - $inherited = "extends:layout_mod.tpl|"; - }else if( isset($vars['permission']) && $vars['permission'] == 1){ - $inherited = "extends:layout_user.tpl|"; - }else{ - $inherited =""; - } - - //if $returnHTML is set to true, return the html by fetching the template else display the template. - if($returnHTML == true){ - return $smarty ->fetch($inherited . $template . '.tpl' ); - }else{ - $smarty -> display( $inherited . $template . '.tpl' ); - } - } - - - /** - * creates the folders that are needed for smarty. - * @todo for the drupal module it might be possible that drupal_mkdir needs to be used instead of mkdir, also this should be in the install.php instead. - */ - static public function create_folders(){ - global $AMS_LIB; + } + + foreach ( $vars as $key => $value ) { + $smarty -> assign( $key, $value ); + } + + // load page specific variables that are language dependent + $variables = Helpers :: handle_language(); + if ( $template != 'layout_plugin' ) + { + foreach ( $variables[$template] as $key => $value ) { + $smarty -> assign( $key, $value ); + } + } + // load ams content variables that are language dependent + foreach ( $variables['ams_content'] as $key => $value ) { + $smarty -> assign( $key, $value ); + } + + // smarty inheritance for loading the matching wrapper layout (with the matching menu bar) + if ( isset( $vars['permission'] ) && $vars['permission'] == 3 ) { + $inherited = "extends:layout_admin.tpl|"; + } else if ( isset( $vars['permission'] ) && $vars['permission'] == 2 ) { + $inherited = "extends:layout_mod.tpl|"; + } else if ( isset( $vars['permission'] ) && $vars['permission'] == 1 ) { + $inherited = "extends:layout_user.tpl|"; + } else { + $inherited = ""; + } + + // if $returnHTML is set to true, return the html by fetching the template else display the template. + if ( $returnHTML == true ) { + return $smarty -> fetch( $inherited . $template . '.tpl' ); + } else { + $smarty -> display( $inherited . $template . '.tpl' ); + } + } + + + /** + * creates the folders that are needed for smarty. + * + * @todo for the drupal module it might be possible that drupal_mkdir needs to be used instead of mkdir, also this should be in the install.php instead. + */ + static public function create_folders() { + global $AMS_LIB; global $SITEBASE; $arr = array( $AMS_LIB . '/ingame_templates/', $AMS_LIB . '/configs', - //$AMS_LIB . '/cache', - $SITEBASE . '/cache/', + // $AMS_LIB . '/cache', + $SITEBASE . '/cache/', $SITEBASE . '/templates/', $SITEBASE . '/templates_c/', $SITEBASE . '/configs' ); - foreach ( $arr as & $value ){ - - if ( !file_exists( $value ) ){ - print($value); - mkdir($value); - } - } - - } - - - /** + foreach ( $arr as &$value ) { + + if ( !file_exists( $value ) ) { + print( $value ); + mkdir( $value ); + } + } + + } + + + /** * check if the http request is sent ingame or not. + * * @return returns true in case it's sent ingame, else false is returned. */ - static public function check_if_game_client() - { - // if HTTP_USER_AGENT is not set then its ryzom core - global $FORCE_INGAME; - if ( ( isset($_SERVER['HTTP_USER_AGENT']) && (strpos($_SERVER['HTTP_USER_AGENT'],"Ryzom") === 0)) || $FORCE_INGAME || ! isset($_SERVER['HTTP_USER_AGENT']) ){ - return true; - }else{ - return false; - } - } - - - /** + static public function check_if_game_client() + { + // if HTTP_USER_AGENT is not set then its ryzom core + global $FORCE_INGAME; + if ( ( isset( $_SERVER['HTTP_USER_AGENT'] ) && ( strpos( $_SERVER['HTTP_USER_AGENT'], "Ryzom" ) === 0 ) ) || $FORCE_INGAME || ! isset( $_SERVER['HTTP_USER_AGENT'] ) ) { + return true; + } else { + return false; + } + } + + + /** * Handles the language specific aspect. * The language can be changed by setting the $_GET['Language'] & $_GET['setLang'] together. This will also change the language entry of the user in the db. * Cookies are also being used in case the user isn't logged in. + * * @return returns the parsed content of the language .ini file related to the users language setting. */ - static public function handle_language(){ - global $DEFAULT_LANGUAGE; - global $AMS_TRANS; - - //if user wants to change the language - if(isset($_GET['Language']) && isset($_GET['setLang'])){ - //The ingame client sometimes sends full words, derive those! - switch($_GET['Language']){ - - case "English": - $lang = "en"; - break; - - case "French": - $lang = "fr"; - break; - - default: - $lang = $_GET['Language']; - } - //if the file exists en the setLang = true - if( file_exists( $AMS_TRANS . '/' . $lang . '.ini' ) && $_GET['setLang'] == "true"){ - //set a cookie & session var and incase logged in write it to the db! - setcookie( 'Language', $lang , time() + 60*60*24*30 ); - $_SESSION['Language'] = $lang; - if(WebUsers::isLoggedIn()){ - WebUsers::setLanguage($_SESSION['id'],$lang); - } - }else{ - $_SESSION['Language'] = $DEFAULT_LANGUAGE; - } - }else{ - //if the session var is not set yet - if(!isset($_SESSION['Language'])){ - //check if a cookie already exists for it - if ( isset( $_COOKIE['Language'] ) ) { - $_SESSION['Language'] = $_COOKIE['Language']; - //else use the default language - }else{ - $_SESSION['Language'] = $DEFAULT_LANGUAGE; - } - } - } - - if ($_SESSION['Language'] == ""){ - $_SESSION['Language'] = $DEFAULT_LANGUAGE; - } - return parse_ini_file( $AMS_TRANS . '/' . $_SESSION['Language'] . '.ini', true ); - - } - + static public function handle_language() { + global $DEFAULT_LANGUAGE; + global $AMS_TRANS; + + // if user wants to change the language + if ( isset( $_GET['Language'] ) && isset( $_GET['setLang'] ) ) { + // The ingame client sometimes sends full words, derive those! + switch ( $_GET['Language'] ) { + + case "English": + $lang = "en"; + break; + + case "French": + $lang = "fr"; + break; + + default: + $lang = $_GET['Language']; + } + // if the file exists en the setLang = true + if ( file_exists( $AMS_TRANS . '/' . $lang . '.ini' ) && $_GET['setLang'] == "true" ) { + // set a cookie & session var and incase logged in write it to the db! + setcookie( 'Language', $lang , time() + 60 * 60 * 24 * 30 ); + $_SESSION['Language'] = $lang; + if ( WebUsers :: isLoggedIn() ) { + WebUsers :: setLanguage( $_SESSION['id'], $lang ); + } + } else { + $_SESSION['Language'] = $DEFAULT_LANGUAGE; + } + } else { + // if the session var is not set yet + if ( !isset( $_SESSION['Language'] ) ) { + // check if a cookie already exists for it + if ( isset( $_COOKIE['Language'] ) ) { + $_SESSION['Language'] = $_COOKIE['Language']; + // else use the default language + } else { + $_SESSION['Language'] = $DEFAULT_LANGUAGE; + } + } + } - /** - * Time output function for handling the time display. - * @return returns the time in the format specified in the $TIME_FORMAT global variable. - */ - static public function outputTime($time, $str = 1){ - global $TIME_FORMAT; - if($str){ - return date($TIME_FORMAT,strtotime($time)); - }else{ - return date($TIME_FORMAT,$time); - } - } - - /** - * Auto login function for ingame use. - * This function will allow users who access the website ingame, to log in without entering the username and password. It uses the COOKIE entry in the open_ring db. - * it checks if the cookie sent by the http request matches the one in the db. This cookie in the db is changed everytime the user relogs. - * @return returns "FALSE" if the cookies didn't match, else it returns an array with the user's id and name. - */ - static public function check_login_ingame(){ - if ( helpers :: check_if_game_client () or $forcelibrender = false ){ - $dbr = new DBLayer("ring"); - if (isset($_GET['UserId']) && isset($_COOKIE['ryzomId'])){ - $id = $_GET['UserId']; - $statement = $dbr->select("ring_users", array('id' => $id, 'cookie' => $_COOKIE['ryzomId']), "user_id=:id AND cookie =:cookie"); - if ($statement->rowCount() ){ - $entry = $statement->fetch(); - //print_r($entry); - return array('id' => $entry['user_id'], 'name' => $entry['user_name']); - }else{ - return "FALSE"; - } - }else{ - return "FALSE"; - } - }else{ - return "FALSE"; - } - } +if ( $_SESSION['Language'] == "" ) { + $_SESSION['Language'] = $DEFAULT_LANGUAGE; + } +return parse_ini_file( $AMS_TRANS . '/' . $_SESSION['Language'] . '.ini', true ); + + } + + +/** + * Time output function for handling the time display. + * + * @return returns the time in the format specified in the $TIME_FORMAT global variable. + */ +static public function outputTime( $time, $str = 1 ) { +global $TIME_FORMAT; + if ( $str ) { + return date( $TIME_FORMAT, strtotime( $time ) ); + } else { + return date( $TIME_FORMAT, $time ); + } +} + +/** + * Auto login function for ingame use. + * This function will allow users who access the website ingame, to log in without entering the username and password. It uses the COOKIE entry in the open_ring db. + * it checks if the cookie sent by the http request matches the one in the db. This cookie in the db is changed everytime the user relogs. + * + * @return returns "FALSE" if the cookies didn't match, else it returns an array with the user's id and name. + */ +static public function check_login_ingame() { +if ( helpers :: check_if_game_client () or $forcelibrender = false ) { + $dbr = new DBLayer( "ring" ); + if ( isset( $_GET['UserId'] ) && isset( $_COOKIE['ryzomId'] ) ) { + $id = $_GET['UserId']; + + $statement = $dbr -> select( "ring_users", array( 'id' => $id, 'cookie' => $_COOKIE['ryzomId'] ), "user_id=:id AND cookie =:cookie" ); + + // $statement = $dbr->execute("SELECT * FROM ring_users WHERE user_id=:id AND cookie =:cookie", array('id' => $id, 'cookie' => $_COOKIE['ryzomId'])); + + if ( $statement -> rowCount() ) { + $entry = $statement -> fetch(); + // print_r($entry); + return array( 'id' => $entry['user_id'], 'name' => $entry['user_name'] ); + } else { + return "FALSE"; + } + } else { + return "FALSE"; + } + } else { + return "FALSE"; + } +} } diff --git a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/plugincache.php b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/plugincache.php index 09bd942b3..42bdda045 100644 --- a/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/plugincache.php +++ b/code/ryzom/tools/server/ryzom_ams/ams_lib/autoload/plugincache.php @@ -156,18 +156,17 @@ class Plugincache { } /** - * get update info array attribute of the object. + * get plugin info array attribute of the object. */ public function getUpdateInfo() { return $this -> update_info; } - /** * some more plugin function that requires during plugin operations * */ - + /** * function to remove a non empty directory * @@ -187,4 +186,86 @@ class Plugincache { return rmdir( $dir ); } } - } + + /** + * 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; + } + + /** + * 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' ); + $pluginName = Plugincache :: pluginInfoUsingId( $value['Id'], 'Name' ); + + // calling hooks in the $pluginName.php + include $plugin_path . '/' . strtolower( $pluginName ) . '.php'; + $arr = get_defined_functions(); + + foreach( $arr['user'] as $key => $value ) + { + if ( stristr( $value, strtolower( $pluginName ) ) == true ) + { + $content['hook_info'][$pluginName] = call_user_func( $value ); + } + } + } + return $content; + } + } diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/func/update_plugin.php b/code/ryzom/tools/server/ryzom_ams/www/html/func/update_plugin.php index ddf4b0abf..1420572b1 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/func/update_plugin.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/func/update_plugin.php @@ -16,7 +16,6 @@ function update_plugin() { $db = new DBLayer( 'lib' ); $sth = $db -> executeWithoutParams( "SELECT * FROM plugins INNER JOIN updates ON plugins.Id=updates.PluginId Where plugins.Id=$id" ); $row = $sth -> fetch(); - print_r( $row ); // replacing update in the database Plugincache :: rrmdir( $row['FileName'] ); diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/index.php b/code/ryzom/tools/server/ryzom_ams/www/html/index.php index f01eab3e4..e87c5dcd5 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/index.php +++ b/code/ryzom/tools/server/ryzom_ams/www/html/index.php @@ -14,7 +14,6 @@ // load required pages and turn error reporting on/off error_reporting( E_ALL ); ini_set( 'display_errors', 'on' ); -require_once( '../../ams_lib/libinclude.php' ); if ( !file_exists( '../is_installed' ) ) { // if is_installed doesnt exist run setup require( 'installer/libsetup.php' ); @@ -25,9 +24,10 @@ if ( !file_exists( '../is_installed' ) ) { // if config exists then include it require( '../config.php' ); } +require_once( $AMS_LIB . '/libinclude.php' ); session_start(); -// Running Cron? +// Running Cron if ( isset( $_GET["cron"] ) ) { if ( $_GET["cron"] == "true" ) { Sync :: syncdata( false ); @@ -39,6 +39,7 @@ Sync :: syncdata( false ); // Decide what page to load if ( ! isset( $_GET["page"] ) ) { + if ( isset( $_SESSION['user'] ) ) { if ( Ticket_User :: isMod( unserialize( $_SESSION['ticket_user'] ) ) ) { $page = 'dashboard'; @@ -101,9 +102,6 @@ if ( isset( $_SESSION['user'] ) ) { $return['username'] = $_SESSION['user']; } - - - // Set permission if ( isset( $_SESSION['ticket_user'] ) ) { $return['permission'] = unserialize( $_SESSION['ticket_user'] ) -> getPermission(); @@ -112,7 +110,6 @@ if ( isset( $_SESSION['ticket_user'] ) ) { $return['permission'] = 0; } - // hide sidebar + topbar in case of login/register if ( $page == 'login' || $page == 'register' || $page == 'logout' || $page == 'forgot_password' || $page == 'reset_password' ) { $return['no_visible_elements'] = 'TRUE'; @@ -126,5 +123,12 @@ if ( $page == 'error' ) { $return['no_visible_elements'] = 'FALSE'; } +// call to load hooks for the active plugins +$hook_content = Plugincache :: loadHooks(); +foreach( $hook_content as $key => $value ) + { + $return[$key] = $value; + } + // load the template with the variables in the $return array helpers :: loadTemplate( $page , $return ); diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout_admin.tpl b/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout_admin.tpl index c50c1c20d..a0d1e49ef 100644 --- a/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout_admin.tpl +++ b/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout_admin.tpl @@ -9,6 +9,8 @@
  • Queues
  • Support Groups
  • +
  • Plugins
  • + {if isset($hook_info)} {foreach from=$hook_info item=element}
  • {$element.menu_display}
  • {/foreach}{/if}
  • Syncing
  • Logout
  • {/block} diff --git a/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout_plugin.tpl b/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout_plugin.tpl new file mode 100644 index 000000000..5bc5938c4 --- /dev/null +++ b/code/ryzom/tools/server/ryzom_ams/www/html/templates/layout_plugin.tpl @@ -0,0 +1,12 @@ +{block name=content} +
    +{if isset($hook_info)} +{foreach from=$hook_info item=element} +{if $element.menu_display eq $smarty.get.name} +{include file=$element.template_path} +{/if} +{/foreach} +{/if} +
    +{/block} +