Update lua versions
[ryzomcore.git] / web / private_php / ams / autoload / helpers.php
blob6838699b5d80bd3a9e80324c14dc9b656976d82d
1 <?php
2 /**
3 * Helper class for more site specific functions.
5 * @author Daan Janssens, mentored by Matthew Lagoe
6 */
7 class Helpers {
9 /**
10 * workhorse of the website, it loads the template and shows it or returns th html.
11 * it uses smarty to load the $template, but before displaying the template it will pass the $vars to smarty. Also based on your language settings a matching
12 * array of words & sentences for that page will be loaded. In case the $returnHTML parameter is set to true, it will return the html instead of displaying the template.
14 * @param $template the name of the template(page) that we want to load.
15 * @param $vars an array of variables that should be loaded by smarty before displaying or returning the html.
16 * @param $returnHTML (default=false) if set to true, the html that should have been displayed, will be returned.
17 * @return in case $returnHTML=true, it returns the html of the template being loaded.
19 public static function loadTemplate( $template, $vars = array (), $returnHTML = false )
21 //error_log(print_r($_GET,true));
22 //error_log(print_r($_POST,true));
23 global $AMS_LIB;
24 global $SITEBASE;
25 global $AMS_TRANS;
26 global $INGAME_LAYOUT;
27 global $AMS_CACHEDIR;
28 global $AMS_PLUGINS;
30 // define('SMARTY_SPL_AUTOLOAD',1);
31 require_once $AMS_LIB . '/smarty/libs/Smarty.class.php';
33 $smarty = new Smarty;
34 $smarty -> setCompileDir( $SITEBASE . '/templates_c/' );
35 $smarty -> setCacheDir( $AMS_CACHEDIR );
36 $smarty -> setConfigDir( $SITEBASE . '/configs/' );
37 // turn smarty debugging on/off
38 $smarty -> debugging = false;
39 // caching must be disabled for multi-language support
40 $smarty -> caching = false;
41 $smarty -> cache_lifetime = 300;
43 $smarty->addPluginsDir($AMS_PLUGINS);
45 if (function_exists('apc_cache_info')) {
46 // production
47 //$smarty->caching = true;
48 //$smarty->setCachingType("apc");
49 //$smarty->compile_check = false;
52 // needed by smarty.
53 helpers :: create_folders ();
54 global $FORCE_INGAME;
56 // if ingame, then use the ingame templates
57 if ( helpers :: check_if_game_client() or $FORCE_INGAME ) {
58 $smarty -> template_dir = $AMS_LIB . '/ingame_templates/';
59 $smarty -> setConfigDir( $AMS_LIB . '/configs' );
60 $variables = parse_ini_file( $AMS_LIB . '/configs/ingame_layout.ini', true );
61 foreach ( $variables[$INGAME_LAYOUT] as $key => $value ) {
62 $smarty -> assign( $key, $value );
64 } else {
65 $smarty -> template_dir = $SITEBASE . '/templates/';
66 $smarty -> setConfigDir( $SITEBASE . '/configs' );
69 foreach ( $vars as $key => $value ) {
70 $smarty -> assign( $key, $value );
73 // load page specific variables that are language dependent
74 $variables = Helpers :: handle_language();
75 if ( $template != 'layout_plugin' )
77 foreach ( $variables[$template] as $key => $value ) {
78 $smarty -> assign( $key, $value );
81 // load ams content variables that are language dependent
82 foreach ( $variables['ams_content'] as $key => $value ) {
83 $smarty -> assign( $key, $value );
86 //load ams content variables that are language dependent
87 foreach ( $variables['ams_content'] as $key => $value){
88 $smarty -> assign( $key, $value);
91 $id = session_id();
92 $smarty -> assign( "sessionid", $id );
94 $dbl = new DBLayer("lib");
95 $statement = $dbl->executeWithoutParams("SELECT * FROM settings");
96 $rows = $statement->fetchAll();
98 foreach ($rows as &$value) {
99 $smarty -> assign( $value['Setting'], $value['Value'] );
102 // smarty inheritance for loading the matching wrapper layout (with the matching menu bar)
103 if ( isset( $vars['permission'] ) && $vars['permission'] == 3 ) {
104 $inherited = "extends:layout_admin.tpl|";
105 } else if ( isset( $vars['permission'] ) && $vars['permission'] == 2 ) {
106 $inherited = "extends:layout_mod.tpl|";
107 } else if ( isset( $vars['permission'] ) && $vars['permission'] == 1 ) {
108 $inherited = "extends:layout_user.tpl|";
109 } else {
110 $inherited = "";
113 // if $returnHTML is set to true, return the html by fetching the template else display the template.
114 if ( $returnHTML == true ) {
115 return $smarty -> fetch( $inherited . $template . '.tpl' );
116 } else {
117 $smarty -> display( $inherited . $template . '.tpl' );
123 * creates the folders that are needed for smarty.
125 * @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.
127 static public function create_folders() {
128 global $AMS_LIB;
129 global $SITEBASE;
130 $arr = array( $AMS_LIB . '/ingame_templates/',
131 $AMS_LIB . '/configs',
132 // $AMS_LIB . '/cache',
133 $SITEBASE . '/cache/',
134 $SITEBASE . '/templates/',
135 $SITEBASE . '/templates_c/',
136 $SITEBASE . '/configs'
138 foreach ( $arr as &$value ) {
140 if ( !file_exists( $value ) ) {
141 print( $value );
142 mkdir( $value );
150 * check if the http request is sent ingame or not.
152 * @return returns true in case it's sent ingame, else false is returned.
154 static public function check_if_game_client()
156 // if HTTP_USER_AGENT is not set then its ryzom core
157 global $FORCE_INGAME;
158 if ( ( isset( $_SERVER['HTTP_USER_AGENT'] ) && ( strpos( $_SERVER['HTTP_USER_AGENT'], "Ryzom" ) === 0 ) ) || $FORCE_INGAME || ! isset( $_SERVER['HTTP_USER_AGENT'] ) ) {
159 return true;
160 } else {
161 return false;
167 * Handles the language specific aspect.
168 * 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.
169 * Cookies are also being used in case the user isn't logged in.
171 * @return returns the parsed content of the language .ini file related to the users language setting.
173 static public function handle_language() {
174 global $DEFAULT_LANGUAGE;
175 global $AMS_TRANS;
177 // if user wants to change the language
178 if ( isset( $_GET['Language'] ) && isset( $_GET['setLang'] ) ) {
179 // The ingame client sometimes sends full words, derive those!
180 switch ( $_GET['Language'] ) {
182 case "English":
183 $lang = "en";
184 break;
186 case "French":
187 $lang = "fr";
188 break;
190 default:
191 $lang = $_GET['Language'];
193 // if the file exists en the setLang = true
194 if ( file_exists( $AMS_TRANS . '/' . $lang . '.ini' ) && $_GET['setLang'] == "true" ) {
195 // set a cookie & session var and incase logged in write it to the db!
196 setcookie( 'Language', $lang , time() + 60 * 60 * 24 * 30 );
197 $_SESSION['Language'] = $lang;
198 if ( WebUsers :: isLoggedIn() ) {
199 WebUsers :: setLanguage( $_SESSION['id'], $lang );
201 } else {
202 $_SESSION['Language'] = $DEFAULT_LANGUAGE;
204 } else {
205 // if the session var is not set yet
206 if ( !isset( $_SESSION['Language'] ) ) {
207 // check if a cookie already exists for it
208 if ( isset( $_COOKIE['Language'] ) ) {
209 $_SESSION['Language'] = $_COOKIE['Language'];
210 // else use the default language
211 } else {
212 $_SESSION['Language'] = $DEFAULT_LANGUAGE;
217 if ( $_SESSION['Language'] == "" ) {
218 $_SESSION['Language'] = $DEFAULT_LANGUAGE;
220 return parse_ini_file( $AMS_TRANS . '/' . $_SESSION['Language'] . '.ini', true );
226 * Time output function for handling the time display.
228 * @return returns the time in the format specified in the $TIME_FORMAT global variable.
230 static public function outputTime($time, $str = 1) {
231 global $TIME_FORMAT;
232 if ($str) {
233 return date($TIME_FORMAT, strtotime($time));
234 } else {
235 return date($TIME_FORMAT, $time);
240 * Auto login function for ingame use.
241 * 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.
242 * 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.
244 * @return returns "FALSE" if the cookies didn't match, else it returns an array with the user's id and name.
246 static public function check_login_ingame() {
247 return NULL;
249 // FIXME
251 if ( helpers :: check_if_game_client () or $forcelibrender = false ) {
252 $dbr = new DBLayer( "ring" );
253 if ( isset( $_GET['UserId'] ) && isset( $_COOKIE['ryzomId'] ) ) {
254 $id = $_GET['UserId'];
256 $statement = $dbr -> select( "ring_users", array( 'id' => $id, 'cookie' => $_COOKIE['ryzomId'] ), "user_id=:id AND cookie =:cookie" );
258 // $statement = $dbr->execute("SELECT * FROM ring_users WHERE user_id=:id AND cookie =:cookie", array('id' => $id, 'cookie' => $_COOKIE['ryzomId']));
260 if ( $statement -> rowCount() ) {
261 $entry = $statement -> fetch();
262 // print_r($entry);
263 return array( 'id' => $entry['user_id'], 'name' => $entry['user_name'] );
264 } else {
265 return "FALSE";
267 } else {
268 return "FALSE";
270 } else {
271 return "FALSE";