edit_user.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /*
  3. *
  4. * OGP - Open Game Panel
  5. * Copyright (C) 2008 - 2018 The OGP Development Team
  6. *
  7. * http://www.opengamepanel.org/
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. function exec_ogp_module() {
  25. global $db, $settings;
  26. // Check if the user_id is set in the uri first. Prevents notices if it's been removed.
  27. if(!isset($_REQUEST['user_id']) === true){
  28. print_failure(get_lang('valid_user'));
  29. return;
  30. }
  31. $my_user_id = $_SESSION['user_id']; #who we're logged in as
  32. $user_id = $_REQUEST['user_id'];
  33. $isAdmin = $db->isAdmin($my_user_id);
  34. $mySubUsers = $db->getUsersSubUsersIds($_SESSION['user_id']);
  35. // Check that the user_id parameter corresponds to a valid user.
  36. if(($userInfo = $db->getUserById($user_id)) === null)
  37. {
  38. print_failure(get_lang('valid_user'));
  39. return;
  40. }
  41. // Allow user to modify owned subuser account information
  42. else if ( ! $isAdmin && $my_user_id !== $user_id && @!in_array($user_id, $mySubUsers))
  43. {
  44. print_failure(get_lang('no_rights'));
  45. return;
  46. }
  47. if ( $isAdmin )
  48. {
  49. $users = $db->getUserList();
  50. foreach ( $users as $user )
  51. {
  52. if ( $db->isAdmin($user['user_id']) )
  53. {
  54. $first_admin_id = $user['user_id'];
  55. break;
  56. }
  57. }
  58. if( $db->isAdmin($user_id) and ( $first_admin_id != $my_user_id ) and ( $user_id != $my_user_id ) )
  59. {
  60. print_failure(get_lang('no_rights'));
  61. return;
  62. }
  63. }
  64. echo "<h2>".($my_user_id !== $user_id ? get_lang_f('editing_profile', htmlentities($userInfo['users_login'])) : get_lang('your_profile'))."</h2>";
  65. echo "<div align='center'>";
  66. require_once("includes/form_table_class.php");
  67. if ( ( isset($_POST['new_password']) || isset($_POST['retype_new_password']) ) &&
  68. $_POST['new_password'] !== $_POST['retype_new_password'] )
  69. {
  70. print_failure(get_lang('password_mismatch'));
  71. }
  72. # If we are editing our own profile we need to enter our current password as well
  73. elseif (isset($_POST['edit_user']) &&
  74. ($my_user_id === $user_id) &&
  75. !$db->is_valid_login($my_user_id,$_REQUEST['current_password']))
  76. {
  77. print_failure(get_lang('current_password_mismatch'));
  78. }
  79. else if (isset($_POST['edit_user']))
  80. {
  81. $user_id = sanitizeInputStr($_POST['user_id']);
  82. $newlang = sanitizeInputStr($_POST['newlang']);
  83. $login = sanitizeInputStr($_POST['login']);
  84. $firstname = sanitizeInputStr($_POST['first_name']);
  85. $lastname = sanitizeInputStr($_POST['last_name']);
  86. $email = sanitizeInputStr($_POST['email_address']);
  87. $city = sanitizeInputStr($_POST['city']);
  88. $province = sanitizeInputStr($_POST['province']);
  89. $country = sanitizeInputStr($_POST['country']);
  90. $phone = sanitizeInputStr($_POST['phone_number']);
  91. $phone = preg_replace("/[^0-9]/", "", $phone);
  92. $theme = sanitizeInputStr($_POST['theme']);
  93. $page_limit = sanitizeInputStr($_POST['page_limit']);
  94. // OGP needs to set the new theme and language in the current session, only if I'm modifying my own user profile.
  95. if ( $my_user_id == $user_id )
  96. {
  97. $_SESSION['users_theme'] = $theme;
  98. $_SESSION['users_lang'] = $newlang;
  99. }
  100. $fields['users_lang'] = $newlang;
  101. $fields['users_fname'] = $firstname;
  102. $fields['users_lname'] = $lastname;
  103. $fields['users_phone'] = $phone;
  104. $fields['users_city'] = $city;
  105. $fields['users_province'] = $province;
  106. $fields['users_country'] = $country;
  107. if( isset($settings['editable_email']) )
  108. {
  109. if( $settings['editable_email'] == "1" OR ( $settings['editable_email'] == "0" and $isAdmin ) )
  110. $fields['users_email'] = $email;
  111. }
  112. elseif( !isset( $settings['editable_email'] ) )
  113. {
  114. $fields['users_email'] = $email;
  115. }
  116. if ( $isAdmin )
  117. {
  118. $mins = sanitizeInputStr($_POST['minutes']);
  119. $hours = sanitizeInputStr($_POST['hours']);
  120. $months = sanitizeInputStr($_POST['month']);
  121. $days = sanitizeInputStr($_POST['days']);
  122. $years = sanitizeInputStr($_POST['years']);
  123. if($months == 'X' || $days == 'X' || $years == 'X' || $hours == 'X' || $mins == 'X')
  124. $expire_timestamp = "X";
  125. else
  126. $expire_timestamp = mktime( $hours, $mins, 0, $months, $days, $years);
  127. $fields['users_role'] = sanitizeInputStr($_POST['newrole']);
  128. $fields['users_comment'] = sanitizeInputStr($_POST['comment']);
  129. $fields['user_expires'] = $expire_timestamp;
  130. $fields['users_login'] = $login;
  131. // Handle email preference
  132. if(isset($_POST['user_receives_emails']) && is_numeric($_POST['user_receives_emails'])){
  133. $fields['user_receives_emails'] = sanitizeInputStr($_POST['user_receives_emails']);
  134. }
  135. }
  136. if ( empty($theme) )
  137. $fields['users_theme'] = NULL;
  138. else
  139. $fields['users_theme'] = $theme;
  140. if (empty($page_limit) || !is_numeric($page_limit) || $page_limit < 10){
  141. $fields['users_page_limit'] = 25;
  142. }else{
  143. if($page_limit > 9999){
  144. $page_limit = 9999;
  145. }
  146. $fields['users_page_limit'] = $page_limit;
  147. }
  148. if ( isset($_POST['new_password']) && !empty($_POST['new_password']) )
  149. $fields['users_passwd'] = md5($_POST['new_password']);
  150. if ( !$db->editUser($fields,$user_id) )
  151. {
  152. print_failure(get_lang_f('failed_to_update_user_profile_error', $db->getError()));
  153. }
  154. else
  155. {
  156. print_success(get_lang_f('profile_of_user_modified_successfully',$login));
  157. $db->logger(get_lang_f('profile_of_user_modified_successfully',$login));
  158. }
  159. global $view;
  160. if ( $isAdmin )
  161. {
  162. $view->refresh("?m=user_admin");
  163. }
  164. else
  165. {
  166. if(isset($_SESSION['REFER']))
  167. $view->refresh($_SESSION['REFER']);
  168. else
  169. $view->refresh("?m=user_admin&amp;p=edit_user&user_id=".$_SESSION['user_id']);
  170. }
  171. return;
  172. }
  173. $ft = new FormTable();
  174. $ft->start_form('?m=user_admin&amp;p=edit_user');
  175. $ft->add_field_hidden('user_id',$user_id);
  176. $ft->start_table();
  177. $login_option = ( !$isAdmin ) ? 'readonly="readonly"' : "";
  178. $ft->add_field('string','login',$userInfo['users_login'],64,$login_option);
  179. if ( $my_user_id === $user_id )
  180. {
  181. $ft->add_field('password','current_password','', 64);
  182. }
  183. $ft->add_field('password','new_password','', 64);
  184. $ft->add_field('password','retype_new_password','', 64);
  185. $locale_files = makefilelist("lang/", ".|..|.svn", true, "folders");
  186. array_push($locale_files,"-");
  187. sort($locale_files);
  188. $ft->add_custom_field('language',
  189. create_drop_box_from_array($locale_files,"newlang",@$userInfo['users_lang']));
  190. require_once('modules/settings/functions.php');
  191. $theme = "";
  192. $add_empty = FALSE;
  193. if ( isset($userInfo['users_theme']) )
  194. {
  195. $theme = $userInfo['users_theme'];
  196. $add_empty = TRUE;
  197. }
  198. $ft->add_custom_field('theme', get_theme_html_str($theme, $add_empty));
  199. $ft->add_field('string','page_limit',$userInfo['users_page_limit'], 64);
  200. $ft->add_field('string','first_name',$userInfo['users_fname'], 64);
  201. $ft->add_field('string','last_name',$userInfo['users_lname'], 64);
  202. $ft->add_field('string','phone_number',$userInfo['users_phone'], 64);
  203. $email_option = ( !$isAdmin and isset( $settings['editable_email'] ) and $settings['editable_email'] == "0" ) ? 'readonly="readonly"' : "";
  204. $ft->add_field('string','email_address',$userInfo['users_email'],64,$email_option);
  205. $ft->add_field('string','city',$userInfo['users_city'], 64);
  206. $ft->add_field('string','province',$userInfo['users_province'], 64);
  207. $ft->add_field('string','country',$userInfo['users_country'], 64);
  208. $ft->add_field('string','api_token',$db->getApiToken($userInfo['user_id']), 64, "readonly");
  209. // Receives email notifications (for admins only --- really)
  210. if ( $isAdmin ) {
  211. $ft->add_field('on_off','user_receives_emails',$userInfo['user_receives_emails']);
  212. }
  213. if ( $isAdmin && $userInfo['users_role'] != "subuser" ) {
  214. $ft->add_custom_field('user_role',
  215. create_drop_box_from_array(array('user', 'admin'),"newrole", $userInfo['users_role']));
  216. $ft->add_field('text','comment',$userInfo['users_comment'], 48);
  217. ?>
  218. <tr>
  219. <td align='right'><?php print_lang('expires'); ?>:</td>
  220. <?php
  221. $timediff = $userInfo["user_expires"];
  222. //echo "Timediff is $timediff<br>";
  223. if(read_expire($timediff) !== 'X')
  224. {
  225. $exday = date("j", $timediff);
  226. $exyear = date("Y", $timediff);
  227. $exmonth = date("m", $timediff);
  228. $exhour = date("H", $timediff);
  229. $exmin = date("i", $timediff);
  230. }
  231. else
  232. {
  233. $exday = "X";
  234. $exyear = "X";
  235. $exmonth = "X";
  236. $exhour = "X";
  237. $exmin = "X";
  238. }
  239. $minutes = range(0,59);
  240. $pad_length = 2;
  241. foreach ($minutes as &$minute)
  242. {
  243. $minute = str_pad($minute, $pad_length, "0", STR_PAD_LEFT);
  244. }
  245. $months = array('X' => 'X', '1' => 'Jan', '2' => 'Feb', '3' => 'Mar', '4' => 'Apr', '5' => 'May', '6' => 'Jun', '7' => 'July',
  246. '8' => 'Aug', '9' => 'Sep', '10' => 'Oct', '11' => 'Nov', '12' => 'Dec');
  247. #The ugliness below is to populate the expiration fields with what is in the db
  248. #Looks bad, but it works well
  249. echo "<td align='left'>";
  250. $x_array = array('X');
  251. echo create_drop_box_from_array(array_merge($x_array,range(1,31)),"days",$exday,true);
  252. echo create_drop_box_from_array($months,"month",$exmonth,false);
  253. echo create_drop_box_from_array(array_merge($x_array,range(date('Y')-1,date('Y')+10)),
  254. "years",$exyear,true);
  255. echo " - ";
  256. echo create_drop_box_from_array(array_merge($x_array,range(0,23)),"hours",$exhour,true);
  257. echo ":";
  258. echo create_drop_box_from_array(array_merge($x_array,$minutes),"minutes",$exmin,true);
  259. echo "<tr><td colspan='2' class='info'>".get_lang('expires_info')."</td></tr>";
  260. }
  261. $ft->end_table();
  262. $ft->add_button("submit","edit_user",get_lang('save_profile'));
  263. $ft->end_form();
  264. echo "</div>";
  265. }
  266. ?>