USER.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. "U_WEB_DOMAINS" => $details['U_WEB_DOMAINS']
  72. );
  73. $reply[$user] = array_merge($user_details, $nses);
  74. }
  75. return $this->reply(TRUE, $reply);
  76. }
  77. /**
  78. * Add USER entry
  79. *
  80. * @param Request $request
  81. * @return string - Ajax Reply
  82. */
  83. public function addExecute(Request $request)
  84. {
  85. $spell = $request->getParameter('spell');
  86. $user = $this->getLoggedUser();
  87. $params = array(
  88. 'USER' => $spell['LOGIN_NAME'],
  89. 'PASSWORD' => $spell['PASSWORD'],
  90. 'EMAIL' => $spell['CONTACT'],
  91. 'ROLE' => $spell['ROLE'],
  92. 'OWNER' => $user['uid'],
  93. 'PACKAGE' => $spell['PACKAGE'],
  94. 'FNAME' => $spell['FNAME'],
  95. 'LNAME' => $spell['LNAME']
  96. );
  97. $result = Vesta::execute(Vesta::V_ADD_SYS_USER, $params);
  98. // Reports
  99. $enable_reports = Utils::getCheckboxBooleanValue($spell['REPORTS_ENABLED']);
  100. $reports_result = $this->setUserReports($spell['LOGIN_NAME'], $spell['REPORTS_ENABLED']);
  101. // NS
  102. //$ns_result = $this->setNSentries($spell['LOGIN_NAME'], $spell);
  103. // Set SHELL
  104. $this->setShell($spell['LOGIN_NAME'], $spell['SHELL']);
  105. if (!$result['status']) {
  106. $this->errors[] = array($result['error_code'] => $result['error_message']);
  107. }
  108. return $this->reply($result['status'], $result['data']);
  109. }
  110. /**
  111. * Delete USER entry
  112. *
  113. * @param Request $request
  114. * @return string - Ajax Reply
  115. */
  116. public function deleteExecute(Request $request)
  117. {
  118. $user = $this->getLoggedUser();
  119. $spell = $request->getParameter('spell');
  120. $params = array(
  121. 'USER' => $spell['LOGIN_NAME']
  122. );
  123. $result = Vesta::execute(Vesta::V_DEL_SYS_USER, $params);
  124. if (!$result['status']) {
  125. $this->errors[] = array($result['error_code'] => $result['error_message']);
  126. }
  127. return $this->reply($result['status'], $result['data']);
  128. }
  129. /**
  130. * Change USER entry
  131. *
  132. * @param Request $request
  133. * @return string - Ajax Reply
  134. */
  135. public function changeExecute(Request $request)
  136. {
  137. $_new = $request->getParameter('new');
  138. $_old = $request->getParameter('old');
  139. $_USER = $_old['LOGIN_NAME'];
  140. if (!empty($_new['PASSWORD']) && $_new['PASSWORD'] != Vesta::SAME_PASSWORD) {
  141. $result = array();
  142. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_PASSWORD, array('USER' => $_USER, 'PASSWORD' => $_new['PASSWORD']));
  143. if (!$result['status']) {
  144. $this->status = FALSE;
  145. $this->errors['PASSWORD'] = array($result['error_code'] => $result['error_message']);
  146. }
  147. }
  148. if ($_old['PACKAGE'] != $_new['PACKAGE']) {
  149. $result = array();
  150. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_PACKAGE, array('USER' => $_USER, 'PACKAGE' => $_new['PACKAGE']));
  151. if (!$result['status']) {
  152. $this->status = FALSE;
  153. $this->errors['PACKAGE'] = array($result['error_code'] => $result['error_message']);
  154. }
  155. }
  156. if ($_old['CONTACT'] != $_new['CONTACT']) {
  157. $result = array();
  158. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_CONTACT, array('USER' => $_USER, 'EMAIL' => $_new['CONTACT']));
  159. if (!$result['status']) {
  160. $this->status = FALSE;
  161. $this->errors['EMAIL'] = array($result['error_code'] => $result['error_message']);
  162. }
  163. }
  164. // Set SHELL
  165. $this->setShell($_USER, $_new['SHELL']);
  166. $this->setNSentries($_USER, $_new);
  167. $names = array(
  168. 'USER' => $_USER,
  169. 'FNAME' => $_new['FNAME'],
  170. 'LNAME' => $_new['LNAME']
  171. );
  172. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_NAME, $names);
  173. if (!$result['status']) {
  174. $this->status = FALSE;
  175. $this->errors['NAMES'] = array($result['error_code'] => $result['error_message']);
  176. }
  177. /*if ($_old['SHELL'] != $_new['SHELL']) {
  178. $result = array();
  179. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $_USER, 'SHELL' => $_new['SHELL']));
  180. if (!$result['status']) {
  181. $this->status = FALSE;
  182. $this->errors['SHELL'] = array($result['error_code'] => $result['error_message']);
  183. }
  184. }*/
  185. if (!$this->status) {
  186. Vesta::execute(Vesta::V_CHANGE_SYS_USER_PASSWORD, array('USER' => $_USER, 'PASSWORD' => $_old['PASSWORD']));
  187. Vesta::execute(Vesta::V_CHANGE_SYS_USER_PACKAGE, array('USER' => $_USER, 'PACKAGE' => $_old['PACKAGE']));
  188. Vesta::execute(Vesta::V_CHANGE_SYS_USER_CONTACT, array('USER' => $_USER, 'EMAIL' => $_old['EMAIL']));
  189. Vesta::execute(Vesta::V_CHANGE_SYS_USER_NS, array('USER' => $_USER, 'NS1' => $_old['NS1'], 'NS2' => $_old['NS2']));
  190. Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $_USER, 'SHELL' => $_old['SHELL']));
  191. }
  192. return $this->reply($this->status, '');
  193. }
  194. protected function setUserReports($user, $enabled)
  195. {
  196. if ($enabled === true) {
  197. $result = Vesta::execute(Vesta::V_ADD_SYS_USER_REPORTS, array('USER' => $user));
  198. }
  199. else {
  200. $result = Vesta::execute(Vesta::V_DEL_SYS_USER_REPORTS, array('USER' => $user));
  201. }
  202. return $result['status'];
  203. }
  204. protected function setNSentries($user, $data)
  205. {
  206. $ns = array();
  207. $ns['USER'] = $user;
  208. $ns['NS1'] = $data['NS1'];
  209. $ns['NS2'] = $data['NS2'];
  210. $ns['NS3'] = isset($data['NS3']) ? $data['NS3'] : '';
  211. $ns['NS4'] = isset($data['NS4']) ? $data['NS4'] : '';
  212. $ns['NS5'] = isset($data['NS5']) ? $data['NS5'] : '';
  213. $ns['NS6'] = isset($data['NS6']) ? $data['NS6'] : '';
  214. $ns['NS7'] = isset($data['NS7']) ? $data['NS7'] : '';
  215. $ns['NS8'] = isset($data['NS8']) ? $data['NS8'] : '';
  216. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_NS, $ns);
  217. return $result['status'];
  218. }
  219. protected function getNS($user, $data)
  220. {
  221. $result = array();
  222. $ns_str = $data['NS'];
  223. $ns_list = explode(',', $ns_str);
  224. foreach (range(0, 7) as $index) {
  225. $result['NS'.($index + 1)] = @trim(@$ns_list[$index]);
  226. }
  227. return $result;
  228. }
  229. /**
  230. * TODO: handle result set errors
  231. */
  232. protected function setShell($user, $shell)
  233. {
  234. $result = Vesta::execute(Vesta::V_CHANGE_SYS_USER_SHELL, array('USER' => $user, 'SHELL' => $shell));
  235. }
  236. }