USER.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * USERS
  4. *
  5. * @author vesta, http://vestacp.com/
  6. * @author Dmitry Malishev <dima.malishev@gmail.com>
  7. * @author Dmitry Naumov-Socolov <naumov.socolov@gmail.com>
  8. * @copyright vesta 2010-2011
  9. */
  10. class USER extends AjaxHandler
  11. {
  12. /**
  13. * Get USER entries
  14. *
  15. * @param Request $request
  16. * @return string - Ajax Reply
  17. */
  18. public function getListExecute(Request $request)
  19. {
  20. $reply = array();
  21. $result = Vesta::execute(Vesta::V_LIST_SYS_USERS, array(Config::get('response_type')));
  22. foreach ($result['data'] as $user => $details) {
  23. $nses = $this->getNS($user, $details);
  24. $user_details = array(
  25. "FNAME" => $details['FNAME'],
  26. "LNAME" => $details['LNAME'],
  27. "LOGIN_NAME" => $user,
  28. "FULLNAME" => $details['FNAME'].' '.$details['LNAME'],
  29. "PACKAGE" => $details['PACKAGE'],
  30. "WEB_DOMAINS" => $details['WEB_DOMAINS'],
  31. "WEB_SSL" => $details['WEB_SSL'],
  32. "WEB_ALIASES" => $details['WEB_ALIASES'],
  33. "DATABASES" => $details['DATABASES'],
  34. "MAIL_DOMAINS" => $details['MAIL_DOMAINS'],
  35. "MAIL_BOXES" => $details['MAIL_BOXES'],
  36. "MAIL_FORWARDERS" => $details['MAIL_FORWARDERS'],
  37. "DNS_DOMAINS" => $details['DNS_DOMAINS'],
  38. "DISK_QUOTA" => $details['DISK_QUOTA'],
  39. "BANDWIDTH" => $details['BANDWIDTH'],
  40. // "NS" => $details['NS'],
  41. "SHELL" => $details['SHELL'],
  42. "BACKUPS" => $details['BACKUPS'],
  43. "WEB_TPL" => $details['WEB_TPL'],
  44. "SUSPEND" => $details['SUSPENDED'],
  45. "CONTACT" => $details['CONTACT'],
  46. "REPORTS" => $details['REPORTS'],
  47. "IP_OWNED" => $details['IP_OWNED'],
  48. "U_DIR_DISK" => $details['U_DIR_DISK'],
  49. "U_DISK" => $details['U_DISK'],
  50. "U_BANDWIDTH" => $details['U_BANDWIDTH'],
  51. "U_WEB_DOMAINS" => $details['U_WEB_DOMAINS'],
  52. "U_WEB_SSL" => $details['U_WEB_SSL'],
  53. "U_DNS_DOMAINS" => $details['U_DNS_DOMAINS'],
  54. "U_DATABASES" => $details['U_DATABASES'],
  55. "U_MAIL_DOMAINS" => $details['U_MAIL_DOMAINS'],
  56. "DATE" => $details['DATE'],
  57. "U_MAIL_FORWARDERS" => '0',
  58. "U_MAIL_BOXES" => '0'
  59. );
  60. $reply[$user] = array_merge($user_details, $nses);
  61. // $reply[$user] = $user_details;
  62. }
  63. return $this->reply(TRUE, $reply);
  64. }
  65. public function getUserExecute(Request $request)
  66. {
  67. $user = $this->getLoggedUser();
  68. $reply = array();
  69. $result = Vesta::execute(Vesta::V_LIST_SYS_USER, array($user['uid'], Config::get('response_type')));
  70. foreach ($result['data'] as $user => $details) {
  71. $user_details = array(
  72. "FNAME" => $details['FNAME'],
  73. "LNAME" => $details['LNAME'],
  74. "LOGIN_NAME" => $user,
  75. "FULLNAME" => $details['FNAME'].' '.$details['LNAME'],
  76. "PACKAGE" => $details['PACKAGE'],
  77. "WEB_DOMAINS" => $details['WEB_DOMAINS'],
  78. "WEB_SSL" => $details['WEB_SSL'],
  79. "WEB_ALIASES" => $details['WEB_ALIASES'],
  80. "DATABASES" => $details['DATABASES'],
  81. "MAIL_DOMAINS" => $details['MAIL_DOMAINS'],
  82. "MAIL_BOXES" => $details['MAIL_BOXES'],
  83. "MAIL_FORWARDERS" => $details['MAIL_FORWARDERS'],
  84. "DNS_DOMAINS" => $details['DNS_DOMAINS'],
  85. "DISK_QUOTA" => $details['DISK_QUOTA'],
  86. "BANDWIDTH" => $details['BANDWIDTH'],
  87. "NS" => $details['NS'],
  88. "SHELL" => $details['SHELL'],
  89. "BACKUPS" => $details['BACKUPS'],
  90. "WEB_TPL" => $details['WEB_TPL'],
  91. "SUSPEND" => $details['SUSPENDED'],
  92. "CONTACT" => $details['CONTACT'],
  93. "REPORTS" => $details['REPORTS'],
  94. "IP_OWNED" => $details['IP_OWNED'],
  95. "U_DIR_DISK" => $details['U_DIR_DISK'],
  96. "U_DISK" => $details['U_DISK'],
  97. "U_BANDWIDTH" => $details['U_BANDWIDTH'],
  98. "U_WEB_DOMAINS" => $details['U_WEB_DOMAINS'],
  99. "U_WEB_SSL" => $details['U_WEB_SSL'],
  100. "U_DNS_DOMAINS" => $details['U_DNS_DOMAINS'],
  101. "U_DATABASES" => $details['U_DATABASES'],
  102. "U_MAIL_DOMAINS" => $details['U_MAIL_DOMAINS'],
  103. "DATE" => $details['DATE']
  104. );
  105. $reply[$user] = $user_details;
  106. }
  107. return $this->reply(TRUE, $reply);
  108. }
  109. /**
  110. * Add USER entry
  111. *
  112. * @param Request $request
  113. * @return string - Ajax Reply
  114. */
  115. public function addExecute(Request $request)
  116. {
  117. $spell = $request->getParameter('spell');
  118. $user = $this->getLoggedUser();
  119. $params = array(
  120. 'USER' => $spell['LOGIN_NAME'],
  121. 'PASSWORD' => $spell['PASSWORD'],
  122. 'EMAIL' => $spell['CONTACT'],
  123. 'PACKAGE' => $spell['PACKAGE'],
  124. 'FNAME' => $spell['FNAME'],
  125. 'LNAME' => $spell['LNAME']
  126. );
  127. $result = Vesta::execute(Vesta::V_ADD_SYS_USER, $params);
  128. // Reports
  129. $enable_reports = Utils::getCheckboxBooleanValue($spell['REPORTS_ENABLED']);
  130. $reports_result = $this->setUserReports($spell['LOGIN_NAME'], $spell['REPORTS_ENABLED']);
  131. // Set SHELL
  132. $this->setShell($spell['LOGIN_NAME'], $spell['SHELL']);
  133. if (!$result['status']) {
  134. $this->errors[] = array($result['error_code'] => $result['error_message']);
  135. }
  136. if ($_s['SUSPEND'] == 'on') {
  137. if($result['status']){
  138. $result = array();
  139. $result = Vesta::execute(Vesta::V_SUSPEND_SYS_USER, array('USER' => $user['uid'], 'USER' => $spell['LOGIN_NAME']));
  140. if (!$result['status']) {
  141. $this->status = FALSE;
  142. $this->errors['SUSPEND'] = array($result['error_code'] => $result['error_message']);
  143. }
  144. }
  145. }
  146. return $this->reply($result['status'], $result['data']);
  147. }
  148. /**
  149. * Delete USER entry
  150. *
  151. * @param Request $request
  152. * @return string - Ajax Reply
  153. */
  154. public function deleteExecute(Request $request)
  155. {
  156. $user = $this->getLoggedUser();
  157. $spell = $request->getParameter('spell');
  158. $params = array(
  159. 'USER' => $spell['LOGIN_NAME']
  160. );
  161. $result = Vesta::execute(Vesta::V_DEL_SYS_USER, $params);
  162. if (!$result['status']) {
  163. $this->errors[] = array($result['error_code'] => $result['error_message']);
  164. }
  165. return $this->reply($result['status'], $result['data']);
  166. }
  167. /**
  168. * Change USER entry
  169. *
  170. * @param Request $request
  171. * @return string - Ajax Reply
  172. */
  173. public function changeExecute(Request $request)
  174. {
  175. $_new = $request->getParameter('new');
  176. $_old = $request->getParameter('old');
  177. $_USER = $_old['LOGIN_NAME'];
  178. $reports_result = $this->setUserReports($_USER, $_new['REPORTS_ENABLED']);
  179. if (!empty($_new['PASSWORD']) && $_new['PASSWORD'] != Vesta::SAME_PASSWORD) {
  180. $result = array();
  181. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_PASSWORD, array('USER' => $_USER, 'PASSWORD' => $_new['PASSWORD']));
  182. if (!$result['status']) {
  183. $this->status = FALSE;
  184. $this->errors['PASSWORD'] = array($result['error_code'] => $result['error_message']);
  185. }
  186. }
  187. if ($_old['PACKAGE'] != $_new['PACKAGE']) {
  188. $result = array();
  189. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_PACKAGE, array('USER' => $_USER, 'PACKAGE' => $_new['PACKAGE']));
  190. if (!$result['status']) {
  191. $this->status = FALSE;
  192. $this->errors['PACKAGE'] = array($result['error_code'] => $result['error_message']);
  193. }
  194. }
  195. if ($_old['CONTACT'] != $_new['CONTACT']) {
  196. $result = array();
  197. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_CONTACT, array('USER' => $_USER, 'EMAIL' => $_new['CONTACT']));
  198. if (!$result['status']) {
  199. $this->status = FALSE;
  200. $this->errors['EMAIL'] = array($result['error_code'] => $result['error_message']);
  201. }
  202. }
  203. // Set SHELL
  204. $this->setShell($_USER, $_new['SHELL']);
  205. $this->setNSentries($_USER, $_new);
  206. $names = array(
  207. 'USER' => $_USER,
  208. 'FNAME' => $_new['FNAME'],
  209. 'LNAME' => $_new['LNAME']
  210. );
  211. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_NAME, $names);
  212. if (!$result['status']) {
  213. $this->status = FALSE;
  214. $this->errors['NAMES'] = array($result['error_code'] => $result['error_message']);
  215. }
  216. if ($_old['SUSPEND'] != $_new['SUSPEND']) {
  217. $result = array();
  218. if($_new['SUSPEND'] == 'on'){
  219. $result = Vesta::execute(Vesta::V_SUSPEND_SYS_USER, array('USER' => $_USER));
  220. }
  221. else{
  222. $result = Vesta::execute(Vesta::V_UNSUSPEND_SYS_USER, array('USER' => $_USER));
  223. }
  224. if (!$result['status']) {
  225. $this->status = FALSE;
  226. $this->errors['SUSPEND'] = array($result['error_code'] => $result['error_message']);
  227. }
  228. }
  229. if (!$this->status) {
  230. Vesta::execute(Vesta::V_CHANGE_SYS_USER_PASSWORD, array('USER' => $_USER, 'PASSWORD' => $_old['PASSWORD']));
  231. Vesta::execute(Vesta::V_CHANGE_SYS_USER_PACKAGE, array('USER' => $_USER, 'PACKAGE' => $_old['PACKAGE']));
  232. Vesta::execute(Vesta::V_CHANGE_SYS_USER_CONTACT, array('USER' => $_USER, 'EMAIL' => $_old['EMAIL']));
  233. Vesta::execute(Vesta::V_CHANGE_SYS_USER_NS, array('USER' => $_USER, 'NS1' => $_old['NS1'], 'NS2' => $_old['NS2']));
  234. Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $_USER, 'SHELL' => $_old['SHELL']));
  235. if($_old['SUSPEND'] == 'on'){
  236. $result = Vesta::execute(Vesta::V_SUSPEND_SYS_USER, array('USER' => $_USER));
  237. }
  238. else{
  239. $result = Vesta::execute(Vesta::V_UNSUSPEND_SYS_USER, array('USER' => $_USER));
  240. }
  241. }
  242. return $this->reply($this->status, '');
  243. }
  244. protected function setUserReports($user, $enabled)
  245. {
  246. if ($enabled == 'off') {
  247. $result = Vesta::execute(Vesta::V_DEL_SYS_USER_REPORTS, array('USER' => $user));
  248. }
  249. else {
  250. $result = Vesta::execute(Vesta::V_ADD_SYS_USER_REPORTS, array('USER' => $user));
  251. }
  252. return $result['status'];
  253. }
  254. protected function setNSentries($user, $data)
  255. {
  256. $ns = array();
  257. $ns['USER'] = $user;
  258. $ns['NS1'] = $data['NS1'];
  259. $ns['NS2'] = $data['NS2'];
  260. $ns['NS3'] = isset($data['NS3']) ? $data['NS3'] : '';
  261. $ns['NS4'] = isset($data['NS4']) ? $data['NS4'] : '';
  262. $ns['NS5'] = isset($data['NS5']) ? $data['NS5'] : '';
  263. $ns['NS6'] = isset($data['NS6']) ? $data['NS6'] : '';
  264. $ns['NS7'] = isset($data['NS7']) ? $data['NS7'] : '';
  265. $ns['NS8'] = isset($data['NS8']) ? $data['NS8'] : '';
  266. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_NS, $ns);
  267. return $result['status'];
  268. }
  269. protected function getNS($user, $data)
  270. {
  271. $result = array();
  272. $ns_str = $data['NS'];
  273. $ns_list = explode(',', $ns_str);
  274. foreach (range(0, 7) as $index) {
  275. $result['NS'.($index + 1)] = @trim(@$ns_list[$index]);
  276. }
  277. return $result;
  278. }
  279. /**
  280. * TODO: handle result set errors
  281. */
  282. protected function setShell($user, $shell)
  283. {
  284. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $user, 'SHELL' => $shell));
  285. }
  286. public function massiveSuspendExecute(Request $request)
  287. {
  288. $_entities = $request->getParameter('entities');
  289. foreach($_entities as $entity){
  290. $result = Vesta::execute(Vesta::V_SUSPEND_SYS_USER, array('USER' => $entity['LOGIN_NAME']));
  291. }
  292. return $this->reply($result['status'], $result['data']);
  293. }
  294. public function massiveUnsuspendExecute(Request $request)
  295. {
  296. $_entities = $request->getParameter('entities');
  297. foreach($_entities as $entity){
  298. $result = Vesta::execute(Vesta::V_UNSUSPEND_SYS_USER, array('USER' => $entity['LOGIN_NAME']));
  299. }
  300. return $this->reply($result['status'], $result['data']);
  301. }
  302. public function massiveDeleteExecute(Request $request)
  303. {
  304. $_entities = $request->getParameter('entities');
  305. foreach($_entities as $entity){
  306. $result = Vesta::execute(Vesta::V_DEL_SYS_USER, array('USER' => $entity['LOGIN_NAME']));
  307. }
  308. return $this->reply($result['status'], $result['data']);
  309. }
  310. public function loginAsExecute(Request $request)
  311. {
  312. $_user = $request->getParameter('user');
  313. if(Vesta::hasRights(VestaSession::getInstance()->getUserRole(), 'login_as'))
  314. {
  315. VestaSession::loginAs($_user);
  316. return $this->reply(TRUE, '');
  317. }
  318. return $this->reply(FALSE, '');
  319. }
  320. public function logoutAsExecute(Request $request)
  321. {
  322. VestaSession::logoutAs();
  323. return $this->reply(TRUE, '');
  324. }
  325. }