USER.class.php 12 KB

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