PageRenderTime 68ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/gui/public/admin/admin_add.php

https://bitbucket.org/droidzone/i-mscp
PHP | 293 lines | 214 code | 31 blank | 48 comment | 13 complexity | 80b94f3ed7f85e09149fbdf094cf9142 MD5 | raw file
  1. <?php
  2. /**
  3. * i-MSCP - internet Multi Server Control Panel
  4. *
  5. * The contents of this file are subject to the Mozilla Public License
  6. * Version 1.1 (the "License"); you may not use this file except in
  7. * compliance with the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS"
  11. * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  12. * License for the specific language governing rights and limitations
  13. * under the License.
  14. *
  15. * The Original Code is "VHCS - Virtual Hosting Control System".
  16. *
  17. * The Initial Developer of the Original Code is moleSoftware GmbH.
  18. * Portions created by Initial Developer are Copyright (C) 2001-2006
  19. * by moleSoftware GmbH. All Rights Reserved.
  20. *
  21. * Portions created by the ispCP Team are Copyright (C) 2006-2010 by
  22. * isp Control Panel. All Rights Reserved.
  23. *
  24. * Portions created by the i-MSCP Team are Copyright (C) 2010-2013 by
  25. * i-MSCP - internet Multi Server Control Panel. All Rights Reserved.
  26. *
  27. * @category i-MSCP
  28. * @package iMSCP_Core
  29. * @subpackage Admin
  30. * @copyright 2001-2006 by moleSoftware GmbH
  31. * @copyright 2006-2010 by ispCP | http://isp-control.net
  32. * @copyright 2010-2013 by i-MSCP | http://i-mscp.net
  33. * @author ispCP Team
  34. * @author i-MSCP Team
  35. * @link http://i-mscp.net
  36. */
  37. // Include core library
  38. require 'imscp-lib.php';
  39. iMSCP_Events_Manager::getInstance()->dispatch(iMSCP_Events::onAdminScriptStart);
  40. check_login('admin');
  41. /** @var $cfg iMSCP_Config_Handler_File */
  42. $cfg = iMSCP_Registry::get('config');
  43. $tpl = new iMSCP_pTemplate();
  44. $tpl->define_dynamic(
  45. array(
  46. 'layout' => 'shared/layouts/ui.tpl',
  47. 'page' => 'admin/admin_add.tpl',
  48. 'page_message' => 'layout'));
  49. $tpl->assign(
  50. array(
  51. 'TR_PAGE_TITLE' => tr('i-MSCP - Admin/Manage users/Add User'),
  52. 'THEME_CHARSET' => tr('encoding'),
  53. 'ISP_LOGO' => layout_getUserLogo()
  54. )
  55. );
  56. /**
  57. * @param $tpl iMSCP_pTemplate
  58. * @return void
  59. */
  60. function add_user($tpl)
  61. {
  62. /** @var $cfg iMSCP_Config_Handler_File */
  63. $cfg = iMSCP_Registry::get('config');
  64. // Dispatches the request
  65. if (is_xhr()) { // Passsword generation (AJAX request)
  66. header('Content-Type: text/plain; charset=utf-8');
  67. header('Cache-Control: no-cache, private');
  68. header('Pragma: no-cache');
  69. header("HTTP/1.0 200 Ok");
  70. echo passgen();
  71. exit;
  72. } elseif (isset($_POST['uaction']) && $_POST['uaction'] === 'add_user') {
  73. iMSCP_Events_Manager::getInstance()->dispatch(iMSCP_Events::onBeforeAddUser);
  74. if (check_user_data()) {
  75. $upass = cryptPasswordWithSalt(clean_input($_POST['password']));
  76. $user_id = $_SESSION['user_id'];
  77. $username = clean_input($_POST['username']);
  78. $fname = clean_input($_POST['fname']);
  79. $lname = clean_input($_POST['lname']);
  80. $gender = clean_input($_POST['gender']);
  81. $firm = clean_input($_POST['firm']);
  82. $zip = clean_input($_POST['zip']);
  83. $city = clean_input($_POST['city']);
  84. $state = clean_input($_POST['state']);
  85. $country = clean_input($_POST['country']);
  86. $email = clean_input($_POST['email']);
  87. $phone = clean_input($_POST['phone']);
  88. $fax = clean_input($_POST['fax']);
  89. $street1 = clean_input($_POST['street1']);
  90. $street2 = clean_input($_POST['street2']);
  91. if (get_gender_by_code($gender, true) === null) {
  92. $gender = '';
  93. }
  94. $query = "
  95. INSERT INTO
  96. `admin` (
  97. `admin_name`, `admin_pass`, `admin_type`, `domain_created`,
  98. `created_by`, `fname`, `lname`, `firm`, `zip`, `city`, `state`,
  99. `country`, `email`, `phone`, `fax`, `street1`, `street2`,
  100. `gender`
  101. ) VALUES (
  102. ?, ?, 'admin', unix_timestamp(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
  103. ?, ?, ?, ?
  104. )
  105. ";
  106. exec_query($query, array($username, $upass, $user_id, $fname, $lname,
  107. $firm, $zip, $city, $state, $country, $email,
  108. $phone, $fax, $street1, $street2, $gender));
  109. /** @var $db iMSCP_Database */
  110. $db = iMSCP_Registry::get('db');
  111. $new_admin_id = $db->insertId();
  112. $user_logged = $_SESSION['user_logged'];
  113. write_log("$user_logged: add admin: $username", E_USER_WARNING);
  114. $user_def_lang = $cfg->USER_INITIAL_LANG;
  115. $user_theme_color = $cfg->USER_INITIAL_THEME;
  116. $query = "
  117. REPLACE INTO
  118. `user_gui_props` (
  119. `user_id`, `lang`, `layout`
  120. ) VALUES (
  121. ?, ?, ?
  122. )
  123. ";
  124. exec_query($query, array($new_admin_id, $user_def_lang, $user_theme_color));
  125. iMSCP_Events_Manager::getInstance()->dispatch(iMSCP_Events::onAfterAddUser);
  126. send_add_user_auto_msg($user_id, clean_input($_POST['username']),
  127. clean_input($_POST['password']),
  128. clean_input($_POST['email']),
  129. clean_input($_POST['fname']),
  130. clean_input($_POST['lname']),
  131. tr('Administrator'),
  132. $gender);
  133. //$_SESSION['user_added'] = 1;
  134. set_page_message(tr('Admin account successfully created.'), 'success');
  135. redirectTo('manage_users.php');
  136. } else { // check user data
  137. $tpl->assign(
  138. array(
  139. 'EMAIL' => clean_input($_POST['email'], true),
  140. 'USERNAME' => clean_input($_POST['username'], true),
  141. 'FIRST_NAME' => clean_input($_POST['fname'], true),
  142. 'LAST_NAME' => clean_input($_POST['lname'], true),
  143. 'FIRM' => clean_input($_POST['firm'], true),
  144. 'ZIP' => clean_input($_POST['zip'], true),
  145. 'CITY' => clean_input($_POST['city'], true),
  146. 'STATE' => clean_input($_POST['state'], true),
  147. 'COUNTRY' => clean_input($_POST['country'], true),
  148. 'STREET_1' => clean_input($_POST['street1'], true),
  149. 'STREET_2' => clean_input($_POST['street2'], true),
  150. 'PHONE' => clean_input($_POST['phone'], true),
  151. 'FAX' => clean_input($_POST['fax'], true),
  152. 'VL_MALE' => (($_POST['gender'] == 'M')
  153. ? $cfg->HTML_SELECTED
  154. : ''),
  155. 'VL_FEMALE' => (($_POST['gender'] == 'F')
  156. ? $cfg->HTML_SELECTED
  157. : ''),
  158. 'VL_UNKNOWN' => ((($_POST['gender'] == 'U') || (empty($_POST['gender'])))
  159. ? $cfg->HTML_SELECTED : '')
  160. )
  161. );
  162. }
  163. } else {
  164. $tpl->assign(
  165. array(
  166. 'EMAIL' => '',
  167. 'USERNAME' => '',
  168. 'FIRST_NAME' => '',
  169. 'LAST_NAME' => '',
  170. 'FIRM' => '',
  171. 'ZIP' => '',
  172. 'CITY' => '',
  173. 'STATE' => '',
  174. 'COUNTRY' => '',
  175. 'STREET_1' => '',
  176. 'STREET_2' => '',
  177. 'PHONE' => '',
  178. 'FAX' => '',
  179. 'VL_MALE' => '',
  180. 'VL_FEMALE' => '',
  181. 'VL_UNKNOWN' => $cfg->HTML_SELECTED));
  182. }
  183. }
  184. /**
  185. * @return bool
  186. */
  187. function check_user_data()
  188. {
  189. if (!validates_username($_POST['username'])) {
  190. set_page_message(tr('Incorrect username length or syntax.'), 'error');
  191. return false;
  192. }
  193. if ($_POST['password'] != $_POST['password_confirmation']) {
  194. set_page_message(tr("Passwords doesn't match."), 'error');
  195. return false;
  196. }
  197. if (!checkPasswordSyntax($_POST['password'])) {
  198. return false;
  199. }
  200. if (!chk_email($_POST['email'])) {
  201. set_page_message(tr("Incorrect email length or syntax."), 'error');
  202. return false;
  203. }
  204. $query = "SELECT `admin_id` FROM `admin` WHERE `admin_name` = ?";
  205. $username = clean_input($_POST['username']);
  206. $rs = exec_query($query, $username);
  207. if ($rs->recordCount() != 0) {
  208. set_page_message(tr('This user name already exist.'), 'warning');
  209. return false;
  210. }
  211. return true;
  212. }
  213. generateNavigation($tpl);
  214. add_user($tpl);
  215. $tpl->assign(
  216. array(
  217. 'TR_EMPTY_OR_WORNG_DATA' => tr('Empty data or wrong field.'),
  218. 'TR_PASSWORD_NOT_MATCH' => tr("Passwords doesn't match."),
  219. 'TR_ADD_ADMIN' => tr('Add admin'),
  220. 'TR_CORE_DATA' => tr('Core data'),
  221. 'TR_USERNAME' => tr('Username'),
  222. 'TR_PASSWORD' => tr('Password'),
  223. 'TR_PASSWORD_REPEAT' => tr('Password confirmation'),
  224. 'TR_EMAIL' => tr('Email'),
  225. 'TR_ADDITIONAL_DATA' => tr('Additional data'),
  226. 'TR_FIRST_NAME' => tr('First name'),
  227. 'TR_LAST_NAME' => tr('Last name'),
  228. 'TR_GENDER' => tr('Gender'),
  229. 'TR_MALE' => tr('Male'),
  230. 'TR_FEMALE' => tr('Female'),
  231. 'TR_UNKNOWN' => tr('Unknown'),
  232. 'TR_COMPANY' => tr('Company'),
  233. 'TR_ZIP_POSTAL_CODE' => tr('Zip/Postal code'),
  234. 'TR_CITY' => tr('City'),
  235. 'TR_STATE' => tr('State/Province'),
  236. 'TR_COUNTRY' => tr('Country'),
  237. 'TR_STREET_1' => tr('Street 1'),
  238. 'TR_STREET_2' => tr('Street 2'),
  239. 'TR_PHONE' => tr('Phone'),
  240. 'TR_FAX' => tr('Fax'),
  241. 'TR_ADD' => tr('Add'),
  242. 'TR_GENERATE' => tr('Generate'),
  243. 'TR_SHOW' => tr('Show'),
  244. 'TR_PASSWORD_GENERATION_NEEDED' => tr('You must first generate a password'),
  245. 'TR_NEW_PASSWORD_IS' => tr('New password is'),
  246. 'TR_RESET' => tr('Reset')
  247. )
  248. );
  249. generatePageMessage($tpl);
  250. $tpl->parse('LAYOUT_CONTENT', 'page');
  251. iMSCP_Events_Manager::getInstance()->dispatch(iMSCP_Events::onAdminScriptEnd, array('templateEngine' => $tpl));
  252. $tpl->prnt();
  253. unsetMessages();