USER.class.php 17 KB

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