MAIN.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. public function aboutExecute($request)
  20. {
  21. // defaults
  22. $about = array('version' => '0', 'company_email' => 'support@vestacp.com',
  23. 'version_name' => 'OGRE-23-1', 'company_name' => 'vestacp.com');
  24. // real data
  25. $config = Vesta::execute(Vesta::V_LIST_SYS_CONFIG, 'json');
  26. if (!empty($config['data']) && !empty($config['data']['config'])) {
  27. $config = $config['data']['config'];
  28. $about['version'] = $config['VERSION'];
  29. $about['version_name'] = $config['VERSION_NAME'];
  30. $about['company_email'] = $config['COMPANY_EMAIL'];
  31. $about['company_name'] = $config['COMPANY_NAME'];
  32. }
  33. return $this->reply(true, $about);
  34. }
  35. public function requestPasswordExecute($request)
  36. {
  37. if (empty($_SESSION['captcha_key'])
  38. || $_SESSION['captcha_key'] != $request->getParameter('captcha')) {
  39. return $this->reply(false, null, 'Captcha is invalid ');
  40. }
  41. $users = Vesta::execute(Vesta::V_LIST_SYS_USERS, 'json');
  42. $email_matched_count = array();
  43. if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$request->getParameter('email'))) {
  44. return $this->reply(false, null, 'Email is invalid');
  45. }
  46. foreach ($users['data'] as $user) {
  47. if ($user['CONTACT'] == trim($request->getParameter('email'))) {
  48. $email_matched_count[] = $user;
  49. }
  50. }
  51. if (empty($email_matched_count)) {
  52. return $this->reply(false, null, 'There is no such user.');
  53. }
  54. foreach ($email_matched_count as $reset_user) {
  55. $secret_key = $reset_user['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 {$reset_user['FNAME']} {$reset_user['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 = $request->getParameter('email');
  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. /**
  126. * Get Initial params.
  127. * Global constants / variables / configs
  128. *
  129. * @param Request $request
  130. * @return string - Ajax Reply
  131. */
  132. public function getInitialExecute(Request $request)
  133. {
  134. $user = VestaSession::getInstance()->getUser();
  135. $global_data = array();
  136. $totals = array(
  137. 'USER' => array('total' => 0, 'blocked' => 0),
  138. 'WEB_DOMAIN' => array('total' => 0, 'blocked' => 0),
  139. 'MAIL' => array('total' => 0),
  140. 'DB' => array('total' => 0, 'blocked' => 0),
  141. 'DNS' => array('total' => 0, 'blocked' => 0),
  142. 'IP' => array('total' => 0, 'blocked' => 0),
  143. 'CRON' => array('total' => 0, 'blocked' => 0)
  144. );
  145. // users
  146. $rs = Vesta::execute(Vesta::V_LIST_SYS_USERS, null, self::JSON);
  147. $data_user = $rs['data'];
  148. $global_data['users'] = array();
  149. foreach ($data_user as $login_name => $usr) {
  150. $totals['USER']['total'] += 1;
  151. if ($usr['SUSPENDED'] != 'yes') {
  152. $global_data['users'][$login_name] = $login_name;
  153. }
  154. else {
  155. $totals['USER']['blocked'] += 1;
  156. }
  157. }
  158. // web_domains
  159. $rs = Vesta::execute(Vesta::V_LIST_WEB_DOMAINS, array('USER' => $user['uid']), self::JSON);
  160. $data_web_domain = $rs['data'];
  161. foreach ($data_web_domain as $web) {
  162. $totals['WEB_DOMAIN']['total'] += 1;
  163. $web['SUSPEND'] == 'yes' ? $totals['WEB_DOMAIN']['blocked'] += 1 : false;
  164. }
  165. // db
  166. $rs = Vesta::execute(Vesta::V_LIST_DB_BASES, array('USER' => $user['uid']), self::JSON);
  167. $data_db = $rs['data'];
  168. foreach ($data_db as $db) {
  169. $totals['DB']['total'] += 1;
  170. $db['SUSPEND'] == 'yes' ? $totals['DB']['blocked'] += 1 : false;
  171. }
  172. // dns
  173. $rs = Vesta::execute(Vesta::V_LIST_DNS_DOMAINS, array('USER' => $user['uid']), self::JSON);
  174. $data_dns = $rs['data'];
  175. foreach ($data_dns as $dns) {
  176. $totals['DNS']['total'] += 1;
  177. $dns['SUSPEND'] == 'yes' ? $totals['DNS']['blocked'] += 1 : false;
  178. }
  179. // ip
  180. $global_data['ips'] = array();
  181. $rs = Vesta::execute(Vesta::V_LIST_SYS_IPS, null, self::JSON);
  182. $data_ip = $rs['data'];
  183. foreach ($data_ip as $ip => $obj) {
  184. $totals['IP']['total'] += 1;
  185. $global_data['ips'][$ip] = $ip;
  186. }
  187. // cron
  188. $rs = Vesta::execute(Vesta::V_LIST_CRON_JOBS, array('USER' => $user['uid']), self::JSON);
  189. $data_cron = $rs['data'];
  190. foreach ($data_cron as $cron) {
  191. $totals['CRON']['total'] += 1;
  192. $cron['SUSPEND'] == 'yes' ? $totals['CRON']['blocked'] += 1 : false;
  193. }
  194. $rs1 = Vesta::execute(Vesta::V_GET_SYS_USER_VALUE, array('USER' => $user['uid'], 'KEY' => 'BANDWIDTH'));
  195. $bandwidth = $rs1['data'];
  196. $rs = Vesta::execute(Vesta::V_GET_SYS_USER_VALUE, array('USER' => $user['uid'], 'KEY' => 'DISK_QUOTA'));
  197. $disk_quota = $rs['data'];
  198. $reply = array(
  199. 'auth_user' => array('uid' => $this->getLoggedUser(), 'admin' => !!VestaSession::getUserRole()),
  200. 'user_data' => array('BANDWIDTH' => (int)$bandwidth, 'DISK_QUOTA' => (int)$disk_quota),
  201. 'WEB_DOMAIN' => $this->getWebDomainParams($data_web_domain, $global_data),
  202. 'CRON' => $this->getCronParams(),
  203. 'IP' => $this->getIpParams($data_ip, $global_data),
  204. 'DNS' => $this->getDnsParams(),
  205. 'DB' => $this->getDbParams($data_db),
  206. 'USERS' => $this->getUsersParams($data_user),
  207. 'totals' => $totals,
  208. 'PROFILE' => $user,
  209. 'real_user' => $_SESSION['real_user'] ? $_SESSION['real_user'] : NULL
  210. );
  211. return $this->reply(true, $reply);
  212. }
  213. protected function getTemplates()
  214. {
  215. if (null != $this->templates) {
  216. return $this->templates;
  217. }
  218. else {
  219. $user = $this->getLoggedUser();
  220. $this->templates = array();
  221. $result = Vesta::execute(Vesta::V_LIST_WEB_TEMPLATES, array('USER' => $user['uid']), self::JSON);
  222. // TODO: handle errors!
  223. foreach ($result['data'] as $tpl => $description) {
  224. $this->templates[$tpl] = $description;
  225. }
  226. return $this->templates;
  227. }
  228. }
  229. /**
  230. * WEB DOMAIN initial params
  231. *
  232. * @params array $data
  233. * @return array
  234. */
  235. public function getWebDomainParams($data, $global_data)
  236. {
  237. $user = $this->getLoggedUser();
  238. $ips = array();
  239. $result = Vesta::execute(Vesta::V_LIST_USER_IPS, array('USER' => $user['uid']), self::JSON);
  240. foreach ($result['data'] as $sys_ip => $ip_data) {
  241. $ips[$sys_ip] = $sys_ip;
  242. }
  243. if (empty($ips)) {
  244. $ips['No available IP'] = 'No available IP';
  245. }
  246. return array(
  247. 'TPL' => $this->getTemplates(),
  248. 'ALIAS' => array(),
  249. 'STAT' => array(
  250. 'none' => 'none',
  251. 'webalizer' => 'webalizer',
  252. 'awstats' => 'awstats'
  253. ),
  254. 'IP' => $ips
  255. );
  256. }
  257. /**
  258. * CRON initial params
  259. *
  260. * @params array $data
  261. * @return array
  262. */
  263. public function getCronParams($data = array())
  264. {
  265. return array();
  266. }
  267. /**
  268. * IP initial params
  269. *
  270. * @params array $data
  271. * @return array
  272. */
  273. public function getIpParams($data = array(), $global_data = array())
  274. {
  275. $ifaces = array();
  276. $result = Vesta::execute(Vesta::V_LIST_SYS_INTERFACES, array(Config::get('response_type')));
  277. foreach ($result['data'] as $iface) {
  278. $ifaces[$iface] = $iface;
  279. }
  280. return array(
  281. 'SYS_USERS' => $global_data['users'],
  282. 'STATUSES' => array(
  283. 'shared' => 'shared',
  284. 'exclusive' => 'exclusive'
  285. ),
  286. 'INTERFACES' => $ifaces,
  287. 'OWNER' => $global_data['users'],
  288. 'MASK' => array(
  289. '255.255.255.0' => '255.255.255.0',
  290. '255.255.255.128' => '255.255.255.128',
  291. '255.255.255.192' => '255.255.255.192',
  292. '255.255.255.224' => '255.255.255.224',
  293. '255.255.255.240' => '255.255.255.240',
  294. '255.255.255.248' => '255.255.255.248',
  295. '255.255.255.252' => '255.255.255.252',
  296. '255.255.255.255' => '255.255.255.255'
  297. )
  298. );
  299. }
  300. /**
  301. * DNS initial params
  302. *
  303. * @params array $data
  304. * @return array
  305. */
  306. public function getDnsParams($data = array())
  307. {
  308. $dns_templates = array();
  309. $user = $this->getLoggedUser();
  310. $this->templates = array();
  311. $result = Vesta::execute(Vesta::V_LIST_DNS_TEMPLATES, null, self::JSON);
  312. // TODO: handle errors!
  313. foreach ($result['data'] as $tpl => $description) {
  314. $dns_templates[$tpl] = $description;
  315. }
  316. return array(
  317. 'IP' => @$data['ips'],
  318. 'TPL' => $dns_templates,
  319. 'EXP' => array(),
  320. 'SOA' => array(),
  321. 'TTL' => array(),
  322. 'record' => array(
  323. 'RECORD' => array(),
  324. 'RECORD_TYPE' => array('A' => 'A', 'NS' => 'NS', 'MX' => 'MX', 'TXT' => 'TXT'),
  325. 'RECORD_VALUE' => array()
  326. )
  327. );
  328. }
  329. /**
  330. * DB initial params
  331. *
  332. * @params array $data
  333. * @return array
  334. */
  335. public function getDbParams($data = array())
  336. {
  337. $db_types = $this->getDBTypes();
  338. $db_hosts = $this->getDBHosts();
  339. $result = Vesta::execute(Vesta::V_LIST_DNS_TEMPLATES, null, self::JSON);
  340. return array(
  341. 'TYPE' => $db_types,
  342. 'HOST' => $db_hosts,
  343. 'CHARSET' => array(
  344. 'utf8' => 'utf8', 'latin1' => 'latin1', 'cp1251' => 'cp1251'
  345. /*
  346. '' => '',
  347. 'big5' => 'Big5 — Traditional Chinese ',
  348. 'dec8' => 'dec8 — DEC West European ',
  349. 'cp850' => 'cp850 — DOS West European',
  350. 'hp8' => 'hp8 — HP West European',
  351. 'koi8r' => 'koi8r — KOI8-R Relcom Russian',
  352. 'latin1' => 'latin1 — cp1252 West European',
  353. 'latin2' => 'latin2 — ISO 8859-2 Central European',
  354. 'swe7' => 'swe7 — 7bit Swedish',
  355. 'ascii' => 'ascii — US ASCII',
  356. 'ujis' => 'ujis — EUC-JP Japanese',
  357. 'sjis' => 'sjis — Shift-JIS Japanese',
  358. 'hebrew' => 'hebrew — ISO 8859-8 Hebrew',
  359. 'tis620' => 'tis620 — TIS620 Thai',
  360. 'euckr' => 'euckr — EUC-KR Korean',
  361. 'koi8u' => 'koi8u — KOI8-U Ukrainian',
  362. 'gb2312' => 'gb2312 — GB2312 Simplified Chinese',
  363. 'greek' => 'greek — ISO 8859-7 Greek',
  364. 'cp1250' => 'cp1250 — Windows Central European',
  365. 'gbk' => 'gbk — GBK Simplified Chinese',
  366. 'latin5' => 'latin5 — ISO 8859-9 Turkish',
  367. 'armscii8' => 'armscii8— ARMSCII-8 Armenian',
  368. 'utf8' => 'utf8 — UTF-8 Unicode',
  369. 'ucs2' => 'ucs2 — UCS-2 Unicode',
  370. 'cp866' => 'cp866 — DOS Russian',
  371. 'keybcs2' => 'keybcs2 — DOS Kamenicky Czech-Slovak',
  372. 'macce' => 'macce — Mac Central European',
  373. 'macroman' => 'macroman— Mac West European',
  374. 'cp853' => 'cp852 — DOS Central European',
  375. 'latin7' => 'latin7 — ISO 8859-13 Baltic',
  376. 'cp1251' => 'cp1251 — Windows Cyrillic',
  377. 'cp1256' => 'cp1256 — Windows Arabic',
  378. 'cp1257' => 'cp1257 — Windows Baltic',
  379. 'binary' => 'binary — Binary pseudo charset',
  380. 'geostd8' => 'geostd8 — GEOSTD8 Georgian',
  381. 'cp932' => 'cp932 — SJIS for Windows Japanese',
  382. 'eucjpms' => 'eucjpms — UJIS for Windows Japanese'
  383. */
  384. )
  385. );
  386. }
  387. public function getDBTypes()
  388. {
  389. return array('mysql' => 'MySQL', 'pgsql' => 'PostgreSQL');
  390. }
  391. public function getDBHosts()
  392. {
  393. return array('localhost' => 'localhost');
  394. foreach($this->getDBTypes() as $type => $type_name){
  395. $result = Vesta::execute(Vesta::V_LIST_DB_HOSTS, $type, self::JSON);
  396. foreach ($result['data'] as $host_name => $host_data) {
  397. if (Utils::getCheckboxBooleanValue($host_data['ACTIVE'])) {
  398. $hosts[$host_name] = $type_name .' – '. $host_name;
  399. }
  400. }
  401. }
  402. return $hosts;
  403. }
  404. /**
  405. * Users initial params
  406. *
  407. * @params array $data
  408. * @return array
  409. */
  410. public function getUsersParams($data = array(), $global_data = array())
  411. {
  412. $pckg = array();
  413. // json
  414. $result = Vesta::execute(Vesta::V_LIST_USER_PACKAGES, null, self::JSON);
  415. foreach ($result['data'] as $pckg_name => $pckg_data) {
  416. $pckg[$pckg_name] = $pckg_name;
  417. }
  418. return array(
  419. 'PACKAGE' => $pckg,
  420. 'SHELL' => array(
  421. 'sh' => 'sh',
  422. 'bash' => 'bash',
  423. 'nologin' => 'nologin',
  424. 'tcsh' => 'tcsh',
  425. 'csh' => 'csh')
  426. );
  427. }
  428. }