MAIN.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. protected $templates = null;
  19. protected $data = array();
  20. public function aboutExecute($request)
  21. {
  22. // defaults
  23. $about = array('version' => '0', 'company_email' => 'support@vestacp.com',
  24. 'version_name' => 'OGRE-23-1', 'company_name' => 'vestacp.com');
  25. // real data
  26. $config = Vesta::execute(Vesta::V_LIST_SYS_CONFIG, 'json');
  27. if (!empty($config['data']) && !empty($config['data']['config'])) {
  28. $config = $config['data']['config'];
  29. $about['version'] = $config['VERSION'];
  30. $about['version_name'] = $config['VERSION_NAME'];
  31. $about['company_email'] = $config['COMPANY_EMAIL'];
  32. $about['company_name'] = $config['COMPANY_NAME'];
  33. }
  34. return $this->reply(true, $about);
  35. }
  36. public function requestPasswordExecute($request)
  37. {
  38. if (empty($_SESSION['captcha_key'])
  39. || $_SESSION['captcha_key'] != $request->getParameter('captcha')) {
  40. return $this->reply(false, null, 'Captcha is invalid ');
  41. }
  42. $cmd = Config::get('sudo_path')." ".Config::get('vesta_functions_path').Vesta::V_LIST_SYS_USERS." 'json'";
  43. exec($cmd, $output, $return);
  44. $users = json_decode(implode('', $output), true);
  45. $login_matched_count = array();
  46. foreach ($users as $user => $data) {
  47. if ($user == trim($request->getParameter('login'))) {
  48. $login_matched_count[$user] = $data;
  49. }
  50. }
  51. if (empty($login_matched_count)) {
  52. return $this->reply(false, null, 'There is no such user.');
  53. }
  54. foreach ($login_matched_count as $reset_user => $data) {
  55. $secret_key = $data['RKEY'];
  56. $reset_link = 'https://'.$_SERVER['HTTP_HOST'].'/change_password.php?v='.$secret_key;
  57. $mail_body = <<<MAIL
  58. <div lang="en" style="background-color:#fff;color:#222">
  59. <a target="_blank" href="" style="color:#FFF">
  60. <img width="81" height="22" style="display:block;border:0" src="http://vestacp.com/i/logo.png" alt="Twitter">
  61. </a>
  62. <div style="font-family:'Helvetica Neue', Arial, Helvetica, sans-serif;font-size:13px;margin:14px">
  63. <h2 style="font-family:'Helvetica Neue', Arial, Helvetica, sans-serif;margin:0 0 16px;font-size:18px;font-weight:normal">
  64. Vesta received a request to reset the password for your account {$data['FNAME']} {$data['LNAME']}?
  65. </h2>
  66. <p>
  67. If you want to reset your password, click on the link below (or copy and paste the URL into your browser):<br>
  68. <a target="_blank" href="{$reset_link}">{$reset_link}</a>
  69. </p>
  70. <p>
  71. If you don't want to reset your password, please ignore this message.
  72. Your password will not be reset.
  73. If you have any concerns, please contact us at support@vestacp.com.
  74. </p>
  75. <p style="font-family:'Helvetica Neue', Arial, Helvetica, sans-serif;font-size:13px;line-height:18px;border-bottom:1px solid rgb(238, 238, 238);padding-bottom:10px;margin:0 0 10px">
  76. <span style="font:italic 13px Georgia,serif;color:rgb(102, 102, 102)">VestaCP</span>
  77. </p>
  78. <p style="font-family:'Helvetica Neue', Arial, Helvetica, sans-serif;margin-top:5px;font-size:10px;color:#888888">
  79. Please do not reply to this message; it was sent from an unmonitored email address.
  80. </p>
  81. </div>
  82. </div>
  83. MAIL;
  84. $headers = 'MIME-Version: 1.0' . "\n";
  85. $headers .= 'Content-type: text/html; charset=UTF-8' . "\n";
  86. $to = $data['CONTACT'];
  87. $subject = 'Reset your Vesta password';
  88. $message = $mail_body;
  89. mail($to, $subject, $message, $headers);
  90. }
  91. return $this->reply(true);
  92. }
  93. public function generateResetPasswordKey()
  94. {
  95. /*$key = sha1($_SERVER['HTTP_USER_AGENT'].$_SERVER['REMOTE_ADDR']);
  96. $key = substr($key, 0, 10) . $_SERVER['REQUEST_TIME'] . substr($key, 10, strlen($key));*/
  97. $user = $this->getLoggedUser();
  98. $rs = Vesta::execute('v_get_user_value', array('USER' => $user['uid'], 'VALUE' => 'RKEY'));
  99. return $rs[''];
  100. }
  101. public function signinExecute($request)
  102. {
  103. $login = $request->getParameter('login');
  104. $password = $request->getParameter('password');
  105. $ip = $request->getUserIP();
  106. $result = Vesta::execute('v_check_user_password', array('USER' => $login, 'PASSWORD' => $password, 'IP' => $ip), self::TEXT);
  107. if ($result['status'] == true) {
  108. return $this->reply(true, array('v_sd' => VestaSession::authorize($login)));
  109. }
  110. else {
  111. return $this->reply(false, array('error_msg' => 'Incorrect login or password'));
  112. }
  113. }
  114. public function logoffExecute($request)
  115. {
  116. VestaSession::logoff();
  117. return $this->reply(true);
  118. }
  119. public function getBackupsExecute(Request $request)
  120. {
  121. $user = VestaSession::getInstance()->getUser();
  122. $rs = Vesta::execute(Vesta::V_LIST_SYS_USER_BACKUPS, array('USER' => $user['uid'], 'RESPONSE' => 'json'));
  123. return $this->reply($rs['status'], @$rs['data']);
  124. }
  125. public function downloadBackupExecute(Request $request)
  126. {
  127. $user = VestaSession::getInstance()->getUser();
  128. header('Content-type: application/x-tar');
  129. header('Content-Disposition: attachment; filename="'.$user['uid'].'.'.$_REQUEST['key'].'.tar"');
  130. header('X-Accel-Redirect: /backup/'.$user['uid'].'.'.$_REQUEST['key'].'.tar');
  131. exit;
  132. }
  133. /**
  134. * Get Initial params.
  135. * Global constants / variables / configs
  136. *
  137. * @param Request $request
  138. * @return string - Ajax Reply
  139. */
  140. public function getInitialExecute(Request $request)
  141. {
  142. $user = VestaSession::getInstance()->getUser();
  143. $global_data = array();
  144. $totals = array(
  145. 'USER' => array('total' => 0, 'blocked' => 0),
  146. 'WEB_DOMAIN' => array('total' => 0, 'blocked' => 0),
  147. 'MAIL' => array('total' => 0),
  148. 'DB' => array('total' => 0, 'blocked' => 0),
  149. 'DNS' => array('total' => 0, 'blocked' => 0),
  150. 'IP' => array('total' => 0, 'blocked' => 0),
  151. 'CRON' => array('total' => 0, 'blocked' => 0)
  152. );
  153. $params = Vesta::execute(Vesta::V_LIST_SYS_USER, array('USER' => $user['uid']), self::JSON);
  154. $init = $params['data'][$user['uid']];
  155. $totals = array(
  156. 'USER' => array( 'total' => (int)$init['U_USERS'],
  157. 'blocked' => (int)$init['SUSPENDED_USERS']),
  158. 'WEB_DOMAIN' => array( 'total' => (int)$init['U_WEB_DOMAIN'],
  159. 'ssl' => (int)$init['U_WEB_SSL'],
  160. 'alias' => (int)$init['U_WEB_ALIASES'],
  161. 'blocked' => (int)$init['SUSPENDED_WEB']),
  162. 'MAIL' => array( 'total' => (int)$init['U_MAIL_DOMAINS'],
  163. 'accounts' => (int)$init['U_MAIL_ACCOUNTS'],
  164. 'blocked' => (int)$init['SUSPENDED_MAIL']),
  165. 'DB' => array( 'total' => (int)$init['U_DATABASES'],
  166. 'blocked' => (int)$init['SUSPENDED_DB']),
  167. 'DNS' => array( 'total' => (int)$init['U_DNS_DOMAINS'],
  168. 'records' => (int)$init['U_DNS_RECORDS'],
  169. 'blocked' => (int)$init['SUSPENDED_DNS']),
  170. 'IP' => array( 'total' => (int)$init['IP_AVAIL'],
  171. 'owned' => (int)$init['IP_OWNED']),
  172. 'CRON' => array( 'total' => (int)$init['U_CRON_JOBS'],
  173. 'blocked' => (int)$init['SUSPENDED_CRON'])
  174. );
  175. // users
  176. $rs = Vesta::execute(Vesta::V_LIST_SYS_USERS, null, self::JSON);
  177. $data_user = $rs['data'];
  178. $global_data['users'] = array();
  179. foreach ($data_user as $login_name => $usr) {
  180. // $totals['USER']['total'] += 1;
  181. if ($usr['SUSPENDED'] != 'yes') {
  182. $this->data['users'] = array($login_name => $login_name);
  183. }
  184. else {
  185. // $totals['USER']['blocked'] += 1;
  186. }
  187. }
  188. // ip
  189. $global_data['ips'] = array();
  190. $rs = Vesta::execute(Vesta::V_LIST_SYS_IPS, null, self::JSON);
  191. $data_ip = $rs['data'];
  192. foreach ($data_ip as $ip => $obj) {
  193. // $totals['IP']['total'] += 1;
  194. $this->data['ips'] = array($ip => $ip);
  195. }
  196. $reply = array(
  197. 'auth_user' => array('uid' => $user, 'admin' => !!VestaSession::getUserRole()),
  198. 'user_data' => array('BANDWIDTH' => (int)$init['BANDWIDTH'], 'DISK_QUOTA' => (int)$init['DISK_QUOTA']),
  199. 'WEB_DOMAIN' => $this->getWebDomainParams(),
  200. 'CRON' => $this->getCronParams(),
  201. 'IP' => $this->getIpParams(),
  202. 'DNS' => $this->getDnsParams(),
  203. 'DB' => $this->getDbParams(),
  204. 'USERS' => $this->getUsersParams(),
  205. 'totals' => $totals,
  206. 'PROFILE' => $user,
  207. 'real_user' => $_SESSION['real_user'] ? $_SESSION['real_user'] : NULL
  208. );
  209. return $this->reply(true, $reply);
  210. // web_domains
  211. $rs = Vesta::execute(Vesta::V_LIST_WEB_DOMAINS, array('USER' => $user['uid']), self::JSON);
  212. $data_web_domain = $rs['data'];
  213. foreach ($data_web_domain as $web) {
  214. $totals['WEB_DOMAIN']['total'] += 1;
  215. $web['SUSPEND'] == 'yes' ? $totals['WEB_DOMAIN']['blocked'] += 1 : false;
  216. }
  217. // db
  218. $rs = Vesta::execute(Vesta::V_LIST_DB_BASES, array('USER' => $user['uid']), self::JSON);
  219. $data_db = $rs['data'];
  220. foreach ($data_db as $db) {
  221. $totals['DB']['total'] += 1;
  222. $db['SUSPEND'] == 'yes' ? $totals['DB']['blocked'] += 1 : false;
  223. }
  224. // dns
  225. $rs = Vesta::execute(Vesta::V_LIST_DNS_DOMAINS, array('USER' => $user['uid']), self::JSON);
  226. $data_dns = $rs['data'];
  227. foreach ($data_dns as $dns) {
  228. $totals['DNS']['total'] += 1;
  229. $dns['SUSPEND'] == 'yes' ? $totals['DNS']['blocked'] += 1 : false;
  230. }
  231. // ip
  232. $global_data['ips'] = array();
  233. $rs = Vesta::execute(Vesta::V_LIST_SYS_IPS, null, self::JSON);
  234. $data_ip = $rs['data'];
  235. foreach ($data_ip as $ip => $obj) {
  236. $totals['IP']['total'] += 1;
  237. $global_data['ips'][$ip] = $ip;
  238. }
  239. // cron
  240. $rs = Vesta::execute(Vesta::V_LIST_CRON_JOBS, array('USER' => $user['uid']), self::JSON);
  241. $data_cron = $rs['data'];
  242. foreach ($data_cron as $cron) {
  243. $totals['CRON']['total'] += 1;
  244. $cron['SUSPEND'] == 'yes' ? $totals['CRON']['blocked'] += 1 : false;
  245. }
  246. $rs1 = Vesta::execute(Vesta::V_GET_SYS_USER_VALUE, array('USER' => $user['uid'], 'KEY' => 'BANDWIDTH'));
  247. $bandwidth = $rs1['data'];
  248. $rs = Vesta::execute(Vesta::V_GET_SYS_USER_VALUE, array('USER' => $user['uid'], 'KEY' => 'DISK_QUOTA'));
  249. $disk_quota = $rs['data'];
  250. $reply = array(
  251. 'auth_user' => array('uid' => $this->getLoggedUser(), 'admin' => !!VestaSession::getUserRole()),
  252. 'user_data' => array('BANDWIDTH' => (int)$bandwidth, 'DISK_QUOTA' => (int)$disk_quota),
  253. 'WEB_DOMAIN' => $this->getWebDomainParams($data_web_domain, $global_data),
  254. 'CRON' => $this->getCronParams(),
  255. 'IP' => $this->getIpParams(),
  256. 'DNS' => $this->getDnsParams(),
  257. 'DB' => $this->getDbParams(),
  258. 'USERS' => $this->getUsersParams($data_user),
  259. 'totals' => $totals,
  260. 'PROFILE' => $user,
  261. 'real_user' => $_SESSION['real_user'] ? $_SESSION['real_user'] : NULL
  262. );
  263. return $this->reply(true, $reply);
  264. }
  265. protected function getTemplates()
  266. {
  267. if (null != $this->templates) {
  268. return $this->templates;
  269. }
  270. else {
  271. $user = $this->getLoggedUser();
  272. $this->templates = array();
  273. $result = Vesta::execute(Vesta::V_LIST_WEB_TEMPLATES, array('USER' => $user['uid']), self::JSON);
  274. // TODO: handle errors!
  275. foreach ($result['data'] as $tpl => $description) {
  276. $this->templates[$tpl] = $description;
  277. }
  278. return $this->templates;
  279. }
  280. }
  281. /**
  282. * WEB DOMAIN initial params
  283. *
  284. * @params array $data
  285. * @return array
  286. */
  287. public function getWebDomainParams()
  288. {
  289. $user = $this->getLoggedUser();
  290. if (empty($this->data['ips'])) {
  291. $this->data['ips']['No available IP'] = 'No available IP';
  292. }
  293. return array(
  294. 'TPL' => $this->getTemplates(),
  295. 'ALIAS' => array(),
  296. 'STAT' => array(
  297. 'none' => 'none',
  298. 'webalizer' => 'webalizer',
  299. 'awstats' => 'awstats'
  300. ),
  301. 'IP' => $this->data['ips']
  302. );
  303. }
  304. /**
  305. * CRON initial params
  306. *
  307. * @params array $data
  308. * @return array
  309. */
  310. public function getCronParams($data = array())
  311. {
  312. return array();
  313. }
  314. /**
  315. * IP initial params
  316. *
  317. * @params array $data
  318. * @return array
  319. */
  320. public function getIpParams()
  321. {
  322. $ifaces = array();
  323. $result = Vesta::execute(Vesta::V_LIST_SYS_INTERFACES, array(Config::get('response_type')));
  324. foreach ($result['data'] as $iface) {
  325. $this->data['ifaces'] = array($iface => $iface);
  326. }
  327. return array(
  328. 'SYS_USERS' => $this->data['users'],
  329. 'STATUSES' => array(
  330. 'shared' => 'shared',
  331. 'exclusive' => 'exclusive'
  332. ),
  333. 'INTERFACES' => $this->data['ifaces'],
  334. 'OWNER' => $this->data['users'],
  335. 'MASK' => array(
  336. '255.255.255.0' => '255.255.255.0',
  337. '255.255.255.128' => '255.255.255.128',
  338. '255.255.255.192' => '255.255.255.192',
  339. '255.255.255.224' => '255.255.255.224',
  340. '255.255.255.240' => '255.255.255.240',
  341. '255.255.255.248' => '255.255.255.248',
  342. '255.255.255.252' => '255.255.255.252',
  343. '255.255.255.255' => '255.255.255.255'
  344. )
  345. );
  346. }
  347. /**
  348. * DNS initial params
  349. *
  350. * @params array $data
  351. * @return array
  352. */
  353. public function getDnsParams($data = array())
  354. {
  355. $dns_templates = array();
  356. $user = $this->getLoggedUser();
  357. $this->templates = array();
  358. $result = Vesta::execute(Vesta::V_LIST_DNS_TEMPLATES, null, self::JSON);
  359. // TODO: handle errors!
  360. foreach ($result['data'] as $tpl => $description) {
  361. $this->data['dns_templates'] = array($tpl => $description);
  362. }
  363. return array(
  364. 'IP' => $this->data['ips'],
  365. 'TPL' => $this->data['dns_templates'],
  366. 'EXP' => array(),
  367. 'SOA' => array(),
  368. 'TTL' => array(),
  369. 'record' => array(
  370. 'RECORD' => array(),
  371. 'RECORD_TYPE' => array('A' => 'A', 'NS' => 'NS', 'MX' => 'MX', 'TXT' => 'TXT', 'MAIL' => 'MAIL'),
  372. 'RECORD_VALUE' => array()
  373. )
  374. );
  375. }
  376. /**
  377. * DB initial params
  378. *
  379. * @params array $data
  380. * @return array
  381. */
  382. public function getDbParams()
  383. {
  384. $db_types = $this->getDBTypes();
  385. $db_hosts = $this->getDBHosts();
  386. return array(
  387. 'TYPE' => $db_types,
  388. 'HOST' => $db_hosts,
  389. 'CHARSET' => array(
  390. 'utf8' => 'utf8', 'latin1' => 'latin1', 'cp1251' => 'cp1251'
  391. /*
  392. '' => '',
  393. 'big5' => 'Big5 — Traditional Chinese ',
  394. 'dec8' => 'dec8 — DEC West European ',
  395. 'cp850' => 'cp850 — DOS West European',
  396. 'hp8' => 'hp8 — HP West European',
  397. 'koi8r' => 'koi8r — KOI8-R Relcom Russian',
  398. 'latin1' => 'latin1 — cp1252 West European',
  399. 'latin2' => 'latin2 — ISO 8859-2 Central European',
  400. 'swe7' => 'swe7 — 7bit Swedish',
  401. 'ascii' => 'ascii — US ASCII',
  402. 'ujis' => 'ujis — EUC-JP Japanese',
  403. 'sjis' => 'sjis — Shift-JIS Japanese',
  404. 'hebrew' => 'hebrew — ISO 8859-8 Hebrew',
  405. 'tis620' => 'tis620 — TIS620 Thai',
  406. 'euckr' => 'euckr — EUC-KR Korean',
  407. 'koi8u' => 'koi8u — KOI8-U Ukrainian',
  408. 'gb2312' => 'gb2312 — GB2312 Simplified Chinese',
  409. 'greek' => 'greek — ISO 8859-7 Greek',
  410. 'cp1250' => 'cp1250 — Windows Central European',
  411. 'gbk' => 'gbk — GBK Simplified Chinese',
  412. 'latin5' => 'latin5 — ISO 8859-9 Turkish',
  413. 'armscii8' => 'armscii8— ARMSCII-8 Armenian',
  414. 'utf8' => 'utf8 — UTF-8 Unicode',
  415. 'ucs2' => 'ucs2 — UCS-2 Unicode',
  416. 'cp866' => 'cp866 — DOS Russian',
  417. 'keybcs2' => 'keybcs2 — DOS Kamenicky Czech-Slovak',
  418. 'macce' => 'macce — Mac Central European',
  419. 'macroman' => 'macroman— Mac West European',
  420. 'cp853' => 'cp852 — DOS Central European',
  421. 'latin7' => 'latin7 — ISO 8859-13 Baltic',
  422. 'cp1251' => 'cp1251 — Windows Cyrillic',
  423. 'cp1256' => 'cp1256 — Windows Arabic',
  424. 'cp1257' => 'cp1257 — Windows Baltic',
  425. 'binary' => 'binary — Binary pseudo charset',
  426. 'geostd8' => 'geostd8 — GEOSTD8 Georgian',
  427. 'cp932' => 'cp932 — SJIS for Windows Japanese',
  428. 'eucjpms' => 'eucjpms — UJIS for Windows Japanese'
  429. */
  430. )
  431. );
  432. }
  433. public function getDBTypes()
  434. {
  435. return array('mysql' => 'MySQL', 'pgsql' => 'PostgreSQL');
  436. }
  437. public function getDBHosts()
  438. {
  439. return array('localhost' => 'localhost');
  440. foreach($this->getDBTypes() as $type => $type_name){
  441. $result = Vesta::execute(Vesta::V_LIST_DB_HOSTS, $type, self::JSON);
  442. foreach ($result['data'] as $host_name => $host_data) {
  443. if (Utils::getCheckboxBooleanValue($host_data['ACTIVE'])) {
  444. $hosts[$host_name] = $type_name .' – '. $host_name;
  445. }
  446. }
  447. }
  448. return $hosts;
  449. }
  450. /**
  451. * Users initial params
  452. *
  453. * @params array $data
  454. * @return array
  455. */
  456. public function getUsersParams()
  457. {
  458. $result = Vesta::execute(Vesta::V_LIST_USER_PACKAGES, null, self::JSON);
  459. foreach ($result['data'] as $pckg_name => $pckg_data) {
  460. $this->data['user_packages'] = array($pckg_name => $pckg_name);
  461. }
  462. return array(
  463. 'PACKAGE' => $this->data['user_packages'],
  464. 'SHELL' => array(
  465. 'sh' => 'sh',
  466. 'bash' => 'bash',
  467. 'nologin' => 'nologin',
  468. 'tcsh' => 'tcsh',
  469. 'csh' => 'csh')
  470. );
  471. }
  472. }