USER.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. // get reports attribute
  24. $result_report = Vesta::execute(Vesta::V_GET_SYS_USER_VALUE, array('USER' => $user, 'VALUE' => 'reports'), self::TEXT);
  25. if ($result_report['status'] != true) {
  26. $report = null;
  27. }
  28. else {
  29. $report = $result_report['data'];
  30. }
  31. $fullname_id = rand(0, count($users)-1);
  32. $fullname = implode('', array($details['FNAME'], ' ', $details['LNAME']));
  33. $nses = $this->getNS($user, $details);
  34. $user_details = array(
  35. "FNAME" => $details['FNAME'],
  36. "LNAME" => $details['LNAME'],
  37. "LOGIN_NAME" => $user,
  38. "FULLNAME" => $fullname,
  39. "PACKAGE" => $details['PACKAGE'],
  40. "WEB_DOMAINS" => $details['WEB_DOMAINS'],
  41. "WEB_SSL" => $details['WEB_SSL'],
  42. "WEB_ALIASES" => $details['WEB_ALIASES'],
  43. "DATABASES" => $details['DATABASES'],
  44. "MAIL_DOMAINS" => $details['MAIL_DOMAINS'],
  45. "MAIL_BOXES" => $details['MAIL_BOXES'],
  46. "MAIL_FORWARDERS" => $details['MAIL_FORWARDERS'],
  47. "DNS_DOMAINS" => $details['DNS_DOMAINS'],
  48. "DISK_QUOTA" => $details['DISK_QUOTA'],
  49. "BANDWIDTH" => $details['BANDWIDTH'],
  50. "SHELL" => $details['SHELL'],
  51. "BACKUPS" => $details['BACKUPS'],
  52. "WEB_TPL" => $details['WEB_TPL'],
  53. "MAX_CHILDS" => $details['MAX_CHILDS'],
  54. "SUSPENDED" => $details['SUSPENDED'],
  55. "OWNER" => $details['OWNER'],
  56. "ROLE" => $details['ROLE'],
  57. "IP_OWNED" => $details['IP_OWNED'],
  58. "U_CHILDS" => $details['U_CHILDS'],
  59. "U_DISK" => $details['U_DISK'],//$u_disk,
  60. "U_BANDWIDTH" => $details['U_BANDWIDTH'],//$u_bandwidth,
  61. "U_WEB_DOMAINS" => $details['U_WEB_DOMAINS'],
  62. "U_WEB_SSL" => $details['U_WEB_SSL'],
  63. "U_DNS_DOMAINS" => $details['U_DNS_DOMAINS'],
  64. "U_DATABASES" => $details['U_DATABASES'],
  65. "U_MAIL_DOMAINS" => $details['U_MAIL_DOMAINS'],
  66. "CONTACT" => $details['CONTACT'],
  67. "DATE" => $details['DATE'],
  68. "U_MAIL_BOXES" => rand(1, 10), // TODO: skid
  69. "U_MAIL_FORWARDERS" => rand(1, 10), // TODO: skid
  70. "REPORTS_ENABLED" => $report
  71. );
  72. $reply[$user] = array_merge($user_details, $nses);
  73. }
  74. return $this->reply(TRUE, $reply);
  75. }
  76. /**
  77. * Add USER entry
  78. *
  79. * @param Request $request
  80. * @return string - Ajax Reply
  81. */
  82. public function addExecute(Request $request)
  83. {
  84. $spell = $request->getParameter('spell');
  85. $user = $this->getLoggedUser();
  86. $params = array(
  87. 'USER' => $spell['LOGIN_NAME'],
  88. 'PASSWORD' => $spell['PASSWORD'],
  89. 'EMAIL' => $spell['CONTACT'],
  90. 'ROLE' => $spell['ROLE'],
  91. 'OWNER' => $user['uid'],
  92. 'PACKAGE' => $spell['PACKAGE'],
  93. 'FNAME' => $spell['FNAME'],
  94. 'LNAME' => $spell['LNAME']
  95. );
  96. $result = Vesta::execute(Vesta::V_ADD_SYS_USER, $params);
  97. // Reports
  98. $enable_reports = Utils::getCheckboxBooleanValue($spell['REPORTS_ENABLED']);
  99. $reports_result = $this->setUserReports($spell['LOGIN_NAME'], $spell['REPORTS_ENABLED']);
  100. // NS
  101. $ns_result = $this->setNSentries($spell['LOGIN_NAME'], $spell);
  102. // Set SHELL
  103. $this->setShell($spell['LOGIN_NAME'], $spell['SHELL']);
  104. if (!$result['status']) {
  105. $this->errors[] = array($result['error_code'] => $result['error_message']);
  106. }
  107. return $this->reply($result['status'], $result['data']);
  108. }
  109. /**
  110. * Delete USER entry
  111. *
  112. * @param Request $request
  113. * @return string - Ajax Reply
  114. */
  115. public function deleteExecute(Request $request)
  116. {
  117. $user = $this->getLoggedUser();
  118. $spell = $request->getParameter('spell');
  119. $params = array(
  120. 'USER' => $spell['LOGIN_NAME']
  121. );
  122. $result = Vesta::execute(Vesta::V_DEL_SYS_USER, $params);
  123. if (!$result['status']) {
  124. $this->errors[] = array($result['error_code'] => $result['error_message']);
  125. }
  126. return $this->reply($result['status'], $result['data']);
  127. }
  128. /**
  129. * Change USER entry
  130. *
  131. * @param Request $request
  132. * @return string - Ajax Reply
  133. */
  134. public function changeExecute(Request $request)
  135. {
  136. $_new = $request->getParameter('new');
  137. $_old = $request->getParameter('old');
  138. $_USER = $_old['LOGIN_NAME'];
  139. if (!empty($_new['PASSWORD']) && $_new['PASSWORD'] != Vesta::SAME_PASSWORD) {
  140. $result = array();
  141. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_PASSWORD, array('USER' => $_USER, 'PASSWORD' => $_new['PASSWORD']));
  142. if (!$result['status']) {
  143. $this->status = FALSE;
  144. $this->errors['PASSWORD'] = array($result['error_code'] => $result['error_message']);
  145. }
  146. }
  147. if ($_old['PACKAGE'] != $_new['PACKAGE']) {
  148. $result = array();
  149. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_PACKAGE, array('USER' => $_USER, 'PACKAGE' => $_new['PACKAGE']));
  150. if (!$result['status']) {
  151. $this->status = FALSE;
  152. $this->errors['PACKAGE'] = array($result['error_code'] => $result['error_message']);
  153. }
  154. }
  155. if ($_old['CONTACT'] != $_new['CONTACT']) {
  156. $result = array();
  157. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_CONTACT, array('USER' => $_USER, 'EMAIL' => $_new['CONTACT']));
  158. if (!$result['status']) {
  159. $this->status = FALSE;
  160. $this->errors['EMAIL'] = array($result['error_code'] => $result['error_message']);
  161. }
  162. }
  163. // Set SHELL
  164. $this->setShell($_USER, $_new['SHELL']);
  165. $this->setNSentries($_USER, $_new);
  166. $names = array(
  167. 'USER' => $_USER,
  168. 'FNAME' => $_new['FNAME'],
  169. 'LNAME' => $_new['LNAME']
  170. );
  171. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_NAME, $names);
  172. if (!$result['status']) {
  173. $this->status = FALSE;
  174. $this->errors['NAMES'] = array($result['error_code'] => $result['error_message']);
  175. }
  176. /*if ($_old['SHELL'] != $_new['SHELL']) {
  177. $result = array();
  178. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $_USER, 'SHELL' => $_new['SHELL']));
  179. if (!$result['status']) {
  180. $this->status = FALSE;
  181. $this->errors['SHELL'] = array($result['error_code'] => $result['error_message']);
  182. }
  183. }*/
  184. if (!$this->status) {
  185. Vesta::execute(Vesta::V_CHANGE_SYS_USER_PASSWORD, array('USER' => $_USER, 'PASSWORD' => $_old['PASSWORD']));
  186. Vesta::execute(Vesta::V_CHANGE_SYS_USER_PACKAGE, array('USER' => $_USER, 'PACKAGE' => $_old['PACKAGE']));
  187. Vesta::execute(Vesta::V_CHANGE_SYS_USER_CONTACT, array('USER' => $_USER, 'EMAIL' => $_old['EMAIL']));
  188. Vesta::execute(Vesta::V_CHANGE_SYS_USER_NS, array('USER' => $_USER, 'NS1' => $_old['NS1'], 'NS2' => $_old['NS2']));
  189. Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $_USER, 'SHELL' => $_old['SHELL']));
  190. }
  191. return $this->reply($this->status, '');
  192. }
  193. protected function setUserReports($user, $enabled)
  194. {
  195. if ($enabled === true) {
  196. $result = Vesta::execute(Vesta::V_ADD_SYS_USER_REPORTS, array('USER' => $user));
  197. }
  198. else {
  199. $result = Vesta::execute(Vesta::V_DEL_SYS_USER_REPORTS, array('USER' => $user));
  200. }
  201. return $result['status'];
  202. }
  203. protected function setNSentries($user, $data)
  204. {
  205. $ns = array();
  206. $ns['USER'] = $user;
  207. $ns['NS1'] = $data['NS1'];
  208. $ns['NS2'] = $data['NS2'];
  209. $ns['NS3'] = isset($data['NS3']) ? $data['NS3'] : '';
  210. $ns['NS4'] = isset($data['NS4']) ? $data['NS4'] : '';
  211. $ns['NS5'] = isset($data['NS5']) ? $data['NS5'] : '';
  212. $ns['NS6'] = isset($data['NS6']) ? $data['NS6'] : '';
  213. $ns['NS7'] = isset($data['NS7']) ? $data['NS7'] : '';
  214. $ns['NS8'] = isset($data['NS8']) ? $data['NS8'] : '';
  215. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_NS, $ns);
  216. return $result['status'];
  217. }
  218. protected function getNS($user, $data)
  219. {
  220. $result = array();
  221. $ns_str = $data['NS'];
  222. $ns_list = explode(',', $ns_str);
  223. foreach (range(0, 7) as $index) {
  224. $result['NS'.($index + 1)] = @trim(@$ns_list[$index]);
  225. }
  226. return $result;
  227. }
  228. /**
  229. * TODO: handle result set errors
  230. */
  231. protected function setShell($user, $shell)
  232. {
  233. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $user, 'SHELL' => $shell));
  234. }
  235. }