MAIN.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <?php
  2. /**
  3. * Main entity class
  4. * Provides usefull methods (utils), shared for sub entities (DNS, IP etc)
  5. * Subentities should be extended from MAIN class
  6. *
  7. * Details:
  8. * - methods, used for ajax executions must be postfixed with execute keyword
  9. * Eg.: getDnsInformationExecute()
  10. *
  11. * @author vesta, http://vestacp.com/
  12. * @author Dmitry Malishev <dima.malishev@gmail.com>
  13. * @author Dmitry Naumov-Socolov <naumov.socolov@gmail.com>
  14. * @copyright vesta 2010-2011
  15. */
  16. class MAIN extends AjaxHandler
  17. {
  18. /**
  19. * Get Version
  20. *
  21. * @param Request $request
  22. * @return string - Ajax Reply
  23. */
  24. public function versionExecute(Request $request)
  25. {
  26. $result = array(
  27. 'version' => '1.0',
  28. 'author' => 'http://vestacp.com/',
  29. 'docs' => 'http://vestacp.com/docs'
  30. );
  31. return $this->reply(true, $result);
  32. }
  33. /**
  34. * Get Initial params.
  35. * Global constants / variables / configs
  36. *
  37. * @param Request $request
  38. * @return string - Ajax Reply
  39. */
  40. public function getInitialExecute(Request $request)
  41. {
  42. /*require_once V_ROOT_DIR . 'api/IP.class.php';
  43. require_once V_ROOT_DIR . 'api/USER.class.php';
  44. // IP
  45. $ip_obj = new IP();
  46. $user_ips = json_decode($ip_obj->getListUserIpsExecute($request), TRUE);
  47. foreach ($user_ips['data'] as $ip) {
  48. $ips[$ip['IP_ADDRESS']] = $ip['IP_ADDRESS'];
  49. }
  50. // USER
  51. $user_obj = new USER();
  52. $users = json_decode($user_obj->getListExecute($request), TRUE);
  53. $user_names = array_keys($users['data']);
  54. $interfaces_arr = json_decode($ip_obj->getSysInterfacesExecute($request), TRUE);
  55. $interfaces = $interfaces_arr['data'];
  56. $data_web_domain = array('ips' => $ips);
  57. $data_ip = array('user_names' => $user_names, 'interfaces' => $interfaces);
  58. $data_dns = array('ips' => $ips);
  59. $data_db = array('db_types' => $this->getDBTypes());
  60. $data_users = array('user_names' => $user_names);*/
  61. $user = VestaSession::getInstance()->getUser();
  62. $global_data = array();
  63. $totals = array(
  64. 'USER' => array('total' => 0, 'blocked' => 0),
  65. 'WEB_DOMAIN' => array('total' => 0, 'blocked' => 0),
  66. 'MAIL' => array('total' => 0),
  67. 'DB' => array('total' => 0, 'blocked' => 0),
  68. 'DNS' => array('total' => 0, 'blocked' => 0),
  69. 'IP' => array('total' => 0, 'blocked' => 0),
  70. 'CRON' => array('total' => 0, 'blocked' => 0)
  71. );
  72. // users
  73. $rs = Vesta::execute(Vesta::V_LIST_SYS_USERS, null, self::JSON);
  74. $data_user = $rs['data'];
  75. $global_data['users'] = array();
  76. foreach ($data_user as $login_name => $usr) {
  77. $totals['USER']['total'] += 1;
  78. if ($usr['SUSPENDED'] != 'yes') {
  79. $global_data['users'][$login_name] = $login_name;
  80. }
  81. else {
  82. $totals['USER']['blocked'] += 1;
  83. }
  84. }
  85. // web_domains
  86. $rs = Vesta::execute(Vesta::V_LIST_WEB_DOMAINS, array('USER' => $user['uid']), self::JSON);
  87. $data_web_domain = $rs['data'];
  88. foreach ($data_web_domain as $web) {
  89. $totals['WEB_DOMAIN']['total'] += 1;
  90. }
  91. // db
  92. $rs = Vesta::execute(Vesta::V_LIST_DB_BASES, array('USER' => $user['uid']), self::JSON);
  93. $data_db = $rs['data'];
  94. foreach ($data_db as $db) {
  95. $totals['DB']['total'] += 1;
  96. //$db['SUSPENDED'] == 'yes' ? $totals['DB']['blocked'] += 1 : false;
  97. }
  98. // dns
  99. $rs = Vesta::execute(Vesta::V_LIST_DNS_DOMAINS, array('USER' => $user['uid']), self::JSON);
  100. $data_dns = $rs['data'];
  101. foreach ($data_dns as $dns) {
  102. $totals['DNS']['total'] += 1;
  103. }
  104. // ip
  105. $global_data['ips'] = array();
  106. $rs = Vesta::execute(Vesta::V_LIST_SYS_IPS, null, self::JSON);
  107. $data_ip = $rs['data'];
  108. foreach ($data_ip as $ip => $obj) {
  109. $totals['IP']['total'] += 1;
  110. $global_data['ips'][$ip] = $ip;
  111. }
  112. // cron
  113. $rs = Vesta::execute(Vesta::V_LIST_CRON_JOBS, array('USER' => $user['uid']), self::JSON);
  114. $data_cron = $rs['data'];
  115. foreach ($data_cron as $cron) {
  116. $totals['CRON']['total'] += 1;
  117. $cron['SUSPEND'] == 'yes' ? $totals['CRON']['blocked'] += 1 : false;
  118. }
  119. $reply = array(
  120. 'WEB_DOMAIN' => $this->getWebDomainParams($data_web_domin, $global_data),
  121. 'CRON' => $this->getCronParams(),
  122. 'IP' => $this->getIpParams($data_ip, $global_data),
  123. 'DNS' => $this->getDnsParams(),
  124. 'DB' => $this->getDbParams($data_db),
  125. 'USERS' => $this->getUsersParams($data_user),
  126. 'totals' => $totals
  127. );
  128. return $this->reply(true, $reply);
  129. }
  130. /**
  131. * WEB DOMAIN initial params
  132. *
  133. * @params array $data
  134. * @return array
  135. */
  136. public function getWebDomainParams($data, $global_data)
  137. {
  138. $user = $this->getLoggedUser();
  139. $ips = array();
  140. //v_list_sys_user_ips vesta
  141. $result = Vesta::execute(Vesta::V_LIST_SYS_USER_IPS, array('USER' => $user['uid']), self::JSON);
  142. foreach ($result['data'] as $sys_ip => $ip_data) {
  143. $ips[$sys_ip] = $sys_ip;
  144. }
  145. if (empty($ips)) {
  146. $ips['No available IP'] = 'No available IP';
  147. }
  148. return array(
  149. 'TPL' => array('default' => 'default'),
  150. 'ALIAS' => array(),
  151. 'STAT' => array(
  152. 'webalizer' => 'webalizer',
  153. 'awstats' => 'awstats'
  154. ),
  155. 'IP' => $ips
  156. );
  157. }
  158. /**
  159. * CRON initial params
  160. *
  161. * @params array $data
  162. * @return array
  163. */
  164. public function getCronParams($data = array())
  165. {
  166. return array();
  167. }
  168. /**
  169. * IP initial params
  170. *
  171. * @params array $data
  172. * @return array
  173. */
  174. public function getIpParams($data = array(), $global_data = array())
  175. {
  176. $ifaces = array();
  177. $result = Vesta::execute(Vesta::V_LIST_SYS_INTERFACES, array(Config::get('response_type')));
  178. foreach ($result['data'] as $iface) {
  179. $ifaces[$iface] = $iface;
  180. }
  181. return array(
  182. 'SYS_USERS' => $users,
  183. 'STATUSES' => array(
  184. 'shared' => 'shared',
  185. 'exclusive' => 'exclusive'
  186. ),
  187. 'INTERFACES' => $ifaces,
  188. 'OWNER' => $global_data['users'],
  189. 'MASK' => array(
  190. '255.255.255.0' => '255.255.255.0',
  191. '255.255.255.128' => '255.255.255.128',
  192. '255.255.255.192' => '255.255.255.192',
  193. '255.255.255.224' => '255.255.255.224',
  194. '255.255.255.240' => '255.255.255.240',
  195. '255.255.255.248' => '255.255.255.248',
  196. '255.255.255.252' => '255.255.255.252',
  197. '255.255.255.255' => '255.255.255.255'
  198. )
  199. );
  200. }
  201. /**
  202. * DNS initial params
  203. *
  204. * @params array $data
  205. * @return array
  206. */
  207. public function getDnsParams($data = array())
  208. {
  209. return array(
  210. 'IP' => @$data['ips'],
  211. 'TPL' => array('default' => 'default'),
  212. 'EXP' => array(),
  213. 'SOA' => array(),
  214. 'TTL' => array(),
  215. 'record' => array(
  216. 'RECORD' => array(),
  217. 'RECORD_TYPE' => array('A' => 'A', 'NS' => 'NS', 'MX' => 'MX', 'TXT' => 'TXT'),
  218. 'RECORD_VALUE' => array()
  219. )
  220. );
  221. }
  222. /**
  223. * DB initial params
  224. *
  225. * @params array $data
  226. * @return array
  227. */
  228. public function getDbParams($data = array())
  229. {
  230. $db_types = $this->getDBTypes();
  231. return array(
  232. 'TYPE' => $db_types,
  233. 'HOST' => array('vestacp.com' => 'vestacp.com', 'askcow.org' => 'askcow.org')
  234. );
  235. }
  236. public function getDBTypes()
  237. {
  238. return array('mysql' => 'mysql', 'postgre' => 'postgre');
  239. }
  240. /**
  241. * Users initial params
  242. *
  243. * @params array $data
  244. * @return array
  245. */
  246. public function getUsersParams($data = array())
  247. {
  248. $pckg = array();
  249. // json
  250. $result = Vesta::execute('v_list_sys_user_packages', null, self::JSON);
  251. foreach ($result['data'] as $pckg_name => $pckg_data) {
  252. $pckg[$pckg_name] = $pckg_name;
  253. }
  254. return array(
  255. 'ROLE' => array('user' => 'user'),
  256. 'OWNER' => $data['user_names'],
  257. 'PACKAGE' => $pckg,
  258. 'SHELL' => array(
  259. 'sh' => 'sh',
  260. 'bash' => 'bash',
  261. 'nologin' => 'nologin',
  262. 'tcsh' => 'tcsh',
  263. 'csh' => 'csh')
  264. );
  265. }
  266. }