USER.class.php 16 KB

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