PageRenderTime 89ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/users/admin/edit.php

http://viet-group.googlecode.com/
PHP | 352 lines | 302 code | 44 blank | 6 comment | 44 complexity | ff665d2cac5d59d878299f9b19802c1f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @Project NUKEVIET CMS 3.0
  4. * @Author VINADES (contact@vinades.vn)
  5. * @Copyright ? 2010 VINADES. All rights reserved
  6. * @Createdate 04/05/2010
  7. */
  8. if ( ! defined( 'NV_IS_FILE_ADMIN' ) ) die( 'Stop!!!' );
  9. $page_title = $lang_module['edit_title'];
  10. $userid = $nv_Request->get_int( 'userid', 'get', 0 );
  11. if ( empty( $userid ) )
  12. {
  13. Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name );
  14. die();
  15. }
  16. $sql = "SELECT * FROM `" . NV_USERS_GLOBALTABLE . "` WHERE `userid`=" . $userid;
  17. $result = $db->sql_query( $sql );
  18. $numrows = $db->sql_numrows( $result );
  19. if ( $numrows != 1 )
  20. {
  21. Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name );
  22. die();
  23. }
  24. $row = $db->sql_fetchrow( $result );
  25. $array_old_groups = ( ! empty( $row['in_groups'] ) ) ? explode( ',', $row['in_groups'] ) : array();
  26. $allow = false;
  27. $sql = "SELECT `lev` FROM `" . NV_AUTHORS_GLOBALTABLE . "` WHERE `admin_id`=" . $userid;
  28. $query = $db->sql_query( $sql );
  29. $numrows = $db->sql_numrows( $query );
  30. if ( ! $numrows )
  31. {
  32. $allow = true;
  33. }
  34. else
  35. {
  36. list( $level ) = $db->sql_fetchrow( $query );
  37. if ( $admin_info['admin_id'] == $userid or $admin_info['level'] < $level )
  38. {
  39. $allow = true;
  40. }
  41. }
  42. if ( ! $allow )
  43. {
  44. Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name );
  45. die();
  46. }
  47. $_user = array();
  48. $groups_list = nv_groups_list();
  49. $error = "";
  50. if ( $nv_Request->isset_request( 'confirm', 'post' ) )
  51. {
  52. nv_insert_logs( NV_LANG_DATA, $module_name, 'log_edit_user', "userid " . $userid, $admin_info['userid'] );
  53. $_user['username'] = filter_text_input( 'username', 'post', '', 1, NV_UNICKMAX );
  54. $_user['email'] = filter_text_input( 'email', 'post', '', 1, 100 );
  55. $_user['password1'] = filter_text_input( 'password1', 'post', '', 0, NV_UPASSMAX );
  56. $_user['password2'] = filter_text_input( 'password2', 'post', '', 0, NV_UPASSMAX );
  57. $_user['question'] = filter_text_input( 'question', 'post', '', 1, 255 );
  58. $_user['answer'] = filter_text_input( 'answer', 'post', '', 1, 255 );
  59. $_user['full_name'] = filter_text_input( 'full_name', 'post', '', 1, 255 );
  60. $_user['gender'] = filter_text_input( 'gender', 'post', '', 1, 1 );
  61. $_user['website'] = filter_text_input( 'website', 'post', '' );
  62. $_user['location'] = filter_text_input( 'location', 'post', '', 1 );
  63. $_user['yim'] = filter_text_input( 'yim', 'post', '', 1, 100 );
  64. $_user['telephone'] = filter_text_input( 'telephone', 'post', '', 1, 100 );
  65. $_user['fax'] = filter_text_input( 'fax', 'post', '', 1, 100 );
  66. $_user['mobile'] = filter_text_input( 'mobile', 'post', '', 1, 100 );
  67. $_user['view_mail'] = $nv_Request->get_int( 'view_mail', 'post', 0 );
  68. $_user['sig'] = filter_text_textarea( 'sig', '', NV_ALLOWED_HTML_TAGS );
  69. $_user['birthday'] = filter_text_input( 'birthday', 'post', '', 1, 10 );
  70. $_user['in_groups'] = $nv_Request->get_typed_array( 'group', 'post', 'int' );
  71. $_user['delpic'] = $nv_Request->get_int( 'delpic', 'post', 0 );
  72. if ( ! empty( $_user['website'] ) )
  73. {
  74. if ( ! preg_match( "#^(http|https|ftp|gopher)\:\/\/#", $_user['website'] ) )
  75. {
  76. $_user['website'] = "http://" . $_user['website'];
  77. }
  78. if ( ! nv_is_url( $_user['website'] ) )
  79. {
  80. $_user['website'] = "";
  81. }
  82. }
  83. if ( ( $error_username = nv_check_valid_login( $_user['username'], NV_UNICKMAX, NV_UNICKMIN ) ) != "" )
  84. {
  85. $error = $error_username;
  86. }
  87. elseif ( $_user['username'] != $db->fixdb( $_user['username'] ) )
  88. {
  89. $error = sprintf( $lang_module['account_deny_name'], '<strong>' . $_user['username'] . '</strong>' );
  90. }
  91. elseif ( ( $error_xemail = nv_check_valid_email( $_user['email'] ) ) != "" )
  92. {
  93. $error = $error_xemail;
  94. }
  95. elseif ( $db->sql_numrows( $db->sql_query( "SELECT `userid` FROM `" . NV_USERS_GLOBALTABLE . "` WHERE `userid`!=" . $userid . " AND `md5username`=" . $db->dbescape( md5( $_user['username'] ) ) ) ) != 0 )
  96. {
  97. $error = $lang_module['edit_error_username_exist'];
  98. }
  99. elseif ( $db->sql_numrows( $db->sql_query( "SELECT `userid` FROM `" . NV_USERS_GLOBALTABLE . "` WHERE `userid`!=" . $userid . " AND `email`=" . $db->dbescape( $_user['email'] ) ) ) != 0 )
  100. {
  101. $error = $lang_module['edit_error_email_exist'];
  102. }
  103. elseif ( $db->sql_numrows( $db->sql_query( "SELECT `userid` FROM `" . NV_USERS_GLOBALTABLE . "_reg` WHERE `email`=" . $db->dbescape( $_user['email'] ) ) ) != 0 )
  104. {
  105. $error = $lang_module['edit_error_email_exist'];
  106. }
  107. elseif ( $db->sql_numrows( $db->sql_query( "SELECT `userid` FROM `" . NV_USERS_GLOBALTABLE . "_openid` WHERE `userid`!=" . $userid . " AND `email`=" . $db->dbescape( $_user['email'] ) ) ) != 0 )
  108. {
  109. $error = $lang_module['edit_error_email_exist'];
  110. }
  111. elseif ( ! empty( $_user['password1'] ) and ( $check_pass = nv_check_valid_pass( $_user['password1'], NV_UPASSMAX, NV_UPASSMIN ) ) != "" )
  112. {
  113. $error = $check_pass;
  114. }
  115. elseif ( ! empty( $_user['password1'] ) and $_user['password1'] != $_user['password2'] )
  116. {
  117. $error = $lang_module['edit_error_password'];
  118. }
  119. elseif ( empty( $_user['question'] ) )
  120. {
  121. $error = $lang_module['edit_error_question'];
  122. }
  123. elseif ( empty( $_user['answer'] ) )
  124. {
  125. $error = $lang_module['edit_error_answer'];
  126. }
  127. else
  128. {
  129. $_user['sig'] = nv_nl2br( $_user['sig'], "<br />" );
  130. if ( $_user['gender'] != "M" and $_user['gender'] != "F" )
  131. {
  132. $_user['gender'] = "";
  133. }
  134. unset( $m );
  135. if ( preg_match( "/^([0-9]{1,2})\.([0-9]{1,2})\.([0-9]{4})$/", $_user['birthday'], $m ) )
  136. {
  137. $_user['birthday'] = mktime( 0, 0, 0, $m[2], $m[1], $m[3] );
  138. }
  139. else
  140. {
  141. $_user['birthday'] = 0;
  142. }
  143. $array_in_groups = array_values( $_user['in_groups'] );
  144. $array_all_groups = array_merge( $array_old_groups, $array_in_groups );
  145. $_user['in_groups'] = array();
  146. if ( ! empty( $array_all_groups ) )
  147. {
  148. foreach ( $array_all_groups as $group_id_i )
  149. {
  150. $query = "SELECT `users` FROM `" . NV_GROUPS_GLOBALTABLE . "` WHERE `group_id`=" . $group_id_i;
  151. $result = $db->sql_query( $query );
  152. $numrows = $db->sql_numrows( $result );
  153. if ( $numrows )
  154. {
  155. $row_users = $db->sql_fetchrow( $result );
  156. $users = trim( $row_users['users'] );
  157. $users = ! empty( $users ) ? explode( ",", $users ) : array();
  158. if ( in_array( $group_id_i, $array_in_groups ) )
  159. {
  160. $users = array_merge( $users, array( $userid ) );
  161. $_user['in_groups'][] = $group_id_i;
  162. }
  163. else
  164. {
  165. $users = array_diff( $users, array( $userid ) );
  166. }
  167. $users = array_unique( $users );
  168. sort( $users );
  169. $users = array_values( $users );
  170. $users = ! empty( $users ) ? implode( ",", $users ) : "";
  171. $sql = "UPDATE `" . NV_GROUPS_GLOBALTABLE . "` SET `users`=" . $db->dbescape_string( $users ) . " WHERE `group_id`=" . $group_id_i;
  172. $db->sql_query( $sql );
  173. }
  174. }
  175. }
  176. $_user['in_groups'] = ( ! empty( $_user['in_groups'] ) ) ? implode( ',', $_user['in_groups'] ) : '';
  177. $password = ! empty( $_user['password1'] ) ? $crypt->hash( $_user['password1'] ) : $row['password'];
  178. $photo = $row['photo'];
  179. if ( $_user['delpic'] )
  180. {
  181. if ( ! empty( $photo ) and is_file( NV_ROOTDIR . '/' . $photo ) )
  182. {
  183. if ( nv_deletefile( NV_ROOTDIR . '/' . $photo ) )
  184. {
  185. $photo = "";
  186. }
  187. }
  188. }
  189. $db->sql_query( "UPDATE `" . NV_USERS_GLOBALTABLE . "` SET
  190. `username`=" . $db->dbescape( $_user['username'] ) . ",
  191. `md5username`=" . $db->dbescape( md5( $_user['username'] ) ) . ",
  192. `password`=" . $db->dbescape( $password ) . ",
  193. `email`=" . $db->dbescape( $_user['email'] ) . ",
  194. `full_name`=" . $db->dbescape( $_user['full_name'] ) . ",
  195. `gender`=" . $db->dbescape( $_user['gender'] ) . ",
  196. `photo`=" . $db->dbescape( $photo ) . ",
  197. `birthday`=" . $_user['birthday'] . ",
  198. `sig`=" . $db->dbescape( $_user['sig'] ) . ",
  199. `website`=" . $db->dbescape( $_user['website'] ) . ",
  200. `location`=" . $db->dbescape( $_user['location'] ) . ",
  201. `yim`=" . $db->dbescape( $_user['yim'] ) . ",
  202. `telephone`=" . $db->dbescape( $_user['telephone'] ) . ",
  203. `fax`=" . $db->dbescape( $_user['fax'] ) . ",
  204. `mobile`=" . $db->dbescape( $_user['mobile'] ) . ",
  205. `question`=" . $db->dbescape( $_user['question'] ) . ",
  206. `answer`=" . $db->dbescape( $_user['answer'] ) . ",
  207. `view_mail`=" . $_user['view_mail'] . ",
  208. `in_groups`=" . $db->dbescape_string( $_user['in_groups'] ) . "
  209. WHERE `userid`=" . $userid );
  210. if ( isset( $_FILES['photo'] ) and is_uploaded_file( $_FILES['photo']['tmp_name'] ) )
  211. {
  212. @require_once ( NV_ROOTDIR . "/includes/class/upload.class.php" );
  213. $upload = new upload( array( 'images' ), $global_config['forbid_extensions'], $global_config['forbid_mimes'], NV_UPLOAD_MAX_FILESIZE, NV_MAX_WIDTH, NV_MAX_HEIGHT );
  214. $upload_info = $upload->save_file( $_FILES['photo'], NV_UPLOADS_REAL_DIR . '/' . $module_name, false );
  215. @unlink( $_FILES['photo']['tmp_name'] );
  216. if ( empty( $upload_info['error'] ) )
  217. {
  218. @chmod( $upload_info['name'], 0644 );
  219. if ( ! empty( $photo ) and is_file( NV_ROOTDIR . '/' . $photo ) )
  220. {
  221. @nv_deletefile( NV_ROOTDIR . '/' . $photo );
  222. }
  223. $file_name = str_replace( NV_ROOTDIR . "/", "", $upload_info['name'] );
  224. $sql = "UPDATE `" . NV_USERS_GLOBALTABLE . "` SET `photo`=" . $db->dbescape( $file_name ) . " WHERE `userid`=" . $userid;
  225. $db->sql_query( $sql );
  226. }
  227. }
  228. Header( "Location: " . NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name );
  229. exit();
  230. }
  231. }
  232. else
  233. {
  234. $_user = $row;
  235. $_user['password1'] = $_user['password2'] = "";
  236. $_user['birthday'] = ! empty( $_user['birthday'] ) ? date( "d.m.Y", $_user['birthday'] ) : "";
  237. $_user['in_groups'] = ! empty( $_user['in_groups'] ) ? explode( ",", $_user['in_groups'] ) : array();
  238. if ( ! empty( $_user['sig'] ) ) $_user['sig'] = nv_br2nl( $_user['sig'] );
  239. }
  240. $genders = array( //
  241. 'N' => array( 'key' => 'N', 'title' => $lang_module['NA'], 'selected' => '' ), //
  242. 'M' => array( 'key' => 'M', 'title' => $lang_module['male'], 'selected' => $_user['gender'] == "M" ? " selected=\"selected\"" : "" ), //
  243. 'F' => array( 'key' => 'F', 'title' => $lang_module['female'], 'selected' => $_user['gender'] == "F" ? " selected=\"selected\"" : "" ) );//
  244. $_user['view_mail'] = $_user['view_mail'] ? " checked=\"checked\"" : "";
  245. if ( ! empty( $_user['sig'] ) ) $_user['sig'] = nv_htmlspecialchars( $_user['sig'] );
  246. $groups = array();
  247. if ( ! empty( $groups_list ) )
  248. {
  249. foreach ( $groups_list as $group_id => $grtl )
  250. {
  251. $groups[] = array( 'id' => $group_id, 'title' => $grtl, 'checked' => ( ! empty( $_user['in_groups'] ) and in_array( $group_id, $_user['in_groups'] ) ) ? " checked=\"checked\"" : "" );
  252. }
  253. }
  254. $xtpl = new XTemplate( "user_edit.tpl", NV_ROOTDIR . "/themes/" . $global_config['module_theme'] . "/modules/" . $module_file );
  255. $xtpl->assign( 'LANG', $lang_module );
  256. $xtpl->assign( 'DATA', $_user );
  257. $xtpl->assign( 'FORM_ACTION', NV_BASE_ADMINURL . "index.php?" . NV_NAME_VARIABLE . "=" . $module_name . "&amp;" . NV_OP_VARIABLE . "=edit&amp;userid=" . $userid );
  258. $xtpl->assign( 'NV_BASE_SITEURL', NV_BASE_SITEURL );
  259. if ( ! empty( $error ) )
  260. {
  261. $xtpl->assign( 'ERROR', $error );
  262. $xtpl->parse( 'main.error' );
  263. }
  264. if ( defined( 'NV_IS_USER_FORUM' ) )
  265. {
  266. $xtpl->parse( 'main.is_forum' );
  267. }
  268. else
  269. {
  270. foreach ( $genders as $gender )
  271. {
  272. $xtpl->assign( 'GENDER', $gender );
  273. $xtpl->parse( 'main.edit_user.gender' );
  274. }
  275. if ( ! empty( $row['photo'] ) )
  276. {
  277. $size = @getimagesize( NV_ROOTDIR . '/' . $row['photo'] );
  278. $img = array( //
  279. 'href' => $row['photo'], //
  280. 'height' => $size[1], //
  281. 'width' => $size[0] );//
  282. $xtpl->assign( 'IMG', $img );
  283. $xtpl->parse( 'main.edit_user.photo' );
  284. }
  285. if ( ! empty( $groups ) )
  286. {
  287. foreach ( $groups as $group )
  288. {
  289. $xtpl->assign( 'GROUP', $group );
  290. $xtpl->parse( 'main.edit_user.group.list' );
  291. }
  292. $xtpl->parse( 'main.edit_user.group' );
  293. }
  294. $xtpl->parse( 'main.edit_user' );
  295. }
  296. $xtpl->parse( 'main' );
  297. $contents = $xtpl->text( 'main' );
  298. $my_head = "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/popcalendar/popcalendar.js\"></script>\n";
  299. $my_head .= "<script type=\"text/javascript\" src=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.js\"></script>\n";
  300. $my_head .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . NV_BASE_SITEURL . "js/shadowbox/shadowbox.css\" />\n";
  301. $my_head .= "<script type=\"text/javascript\">\n";
  302. $my_head .= "Shadowbox.init({\n";
  303. $my_head .= "});\n";
  304. $my_head .= "</script>\n";
  305. include ( NV_ROOTDIR . "/includes/header.php" );
  306. echo nv_admin_theme( $contents );
  307. include ( NV_ROOTDIR . "/includes/footer.php" );
  308. ?>