MAIN.class.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. }
  164. // db
  165. $rs = Vesta::execute(Vesta::V_LIST_DB_BASES, array('USER' => $user['uid']), self::JSON);
  166. $data_db = $rs['data'];
  167. foreach ($data_db as $db) {
  168. $totals['DB']['total'] += 1;
  169. }
  170. // dns
  171. $rs = Vesta::execute(Vesta::V_LIST_DNS_DOMAINS, array('USER' => $user['uid']), self::JSON);
  172. $data_dns = $rs['data'];
  173. foreach ($data_dns as $dns) {
  174. $totals['DNS']['total'] += 1;
  175. }
  176. // ip
  177. $global_data['ips'] = array();
  178. $rs = Vesta::execute(Vesta::V_LIST_SYS_IPS, null, self::JSON);
  179. $data_ip = $rs['data'];
  180. foreach ($data_ip as $ip => $obj) {
  181. $totals['IP']['total'] += 1;
  182. $global_data['ips'][$ip] = $ip;
  183. }
  184. // cron
  185. $rs = Vesta::execute(Vesta::V_LIST_CRON_JOBS, array('USER' => $user['uid']), self::JSON);
  186. $data_cron = $rs['data'];
  187. foreach ($data_cron as $cron) {
  188. $totals['CRON']['total'] += 1;
  189. $cron['SUSPEND'] == 'yes' ? $totals['CRON']['blocked'] += 1 : false;
  190. }
  191. $rs1 = Vesta::execute(Vesta::V_GET_SYS_USER_VALUE, array('USER' => $user['uid'], 'KEY' => 'BANDWIDTH'));
  192. $bandwidth = $rs1['data'];
  193. $rs = Vesta::execute(Vesta::V_GET_SYS_USER_VALUE, array('USER' => $user['uid'], 'KEY' => 'DISK_QUOTA'));
  194. $disk_quota = $rs['data'];
  195. $reply = array(
  196. 'auth_user' => array('uid' => $this->getLoggedUser(), 'admin' => !!VestaSession::getUserRole()),
  197. 'user_data' => array('BANDWIDTH' => (int)$bandwidth, 'DISK_QUOTA' => (int)$disk_quota),
  198. 'WEB_DOMAIN' => $this->getWebDomainParams($data_web_domain, $global_data),
  199. 'CRON' => $this->getCronParams(),
  200. 'IP' => $this->getIpParams($data_ip, $global_data),
  201. 'DNS' => $this->getDnsParams(),
  202. 'DB' => $this->getDbParams($data_db),
  203. 'USERS' => $this->getUsersParams($data_user),
  204. 'totals' => $totals,
  205. 'PROFILE' => $user,
  206. 'real_user' => $_SESSION['real_user'] ? $_SESSION['real_user'] : NULL
  207. );
  208. return $this->reply(true, $reply);
  209. }
  210. protected function getTemplates()
  211. {
  212. if (null != $this->templates) {
  213. return $this->templates;
  214. }
  215. else {
  216. $user = $this->getLoggedUser();
  217. $this->templates = array();
  218. $result = Vesta::execute(Vesta::V_LIST_WEB_TEMPLATES, array('USER' => $user['uid']), self::JSON);
  219. // TODO: handle errors!
  220. foreach ($result['data'] as $tpl => $description) {
  221. $this->templates[$tpl] = $description;
  222. }
  223. return $this->templates;
  224. }
  225. }
  226. /**
  227. * WEB DOMAIN initial params
  228. *
  229. * @params array $data
  230. * @return array
  231. */
  232. public function getWebDomainParams($data, $global_data)
  233. {
  234. $user = $this->getLoggedUser();
  235. $ips = array();
  236. $result = Vesta::execute(Vesta::V_LIST_USER_IPS, array('USER' => $user['uid']), self::JSON);
  237. foreach ($result['data'] as $sys_ip => $ip_data) {
  238. $ips[$sys_ip] = $sys_ip;
  239. }
  240. if (empty($ips)) {
  241. $ips['No available IP'] = 'No available IP';
  242. }
  243. return array(
  244. 'TPL' => $this->getTemplates(),
  245. 'ALIAS' => array(),
  246. 'STAT' => array(
  247. 'none' => 'none',
  248. 'webalizer' => 'webalizer',
  249. 'awstats' => 'awstats'
  250. ),
  251. 'IP' => $ips
  252. );
  253. }
  254. /**
  255. * CRON initial params
  256. *
  257. * @params array $data
  258. * @return array
  259. */
  260. public function getCronParams($data = array())
  261. {
  262. return array();
  263. }
  264. /**
  265. * IP initial params
  266. *
  267. * @params array $data
  268. * @return array
  269. */
  270. public function getIpParams($data = array(), $global_data = array())
  271. {
  272. $ifaces = array();
  273. $result = Vesta::execute(Vesta::V_LIST_SYS_INTERFACES, array(Config::get('response_type')));
  274. foreach ($result['data'] as $iface) {
  275. $ifaces[$iface] = $iface;
  276. }
  277. return array(
  278. 'SYS_USERS' => $global_data['users'],
  279. 'STATUSES' => array(
  280. 'shared' => 'shared',
  281. 'exclusive' => 'exclusive'
  282. ),
  283. 'INTERFACES' => $ifaces,
  284. 'OWNER' => $global_data['users'],
  285. 'MASK' => array(
  286. '255.255.255.0' => '255.255.255.0',
  287. '255.255.255.128' => '255.255.255.128',
  288. '255.255.255.192' => '255.255.255.192',
  289. '255.255.255.224' => '255.255.255.224',
  290. '255.255.255.240' => '255.255.255.240',
  291. '255.255.255.248' => '255.255.255.248',
  292. '255.255.255.252' => '255.255.255.252',
  293. '255.255.255.255' => '255.255.255.255'
  294. )
  295. );
  296. }
  297. /**
  298. * DNS initial params
  299. *
  300. * @params array $data
  301. * @return array
  302. */
  303. public function getDnsParams($data = array())
  304. {
  305. $dns_templates = array();
  306. $user = $this->getLoggedUser();
  307. $this->templates = array();
  308. $result = Vesta::execute(Vesta::V_LIST_DNS_TEMPLATES, null, self::JSON);
  309. // TODO: handle errors!
  310. foreach ($result['data'] as $tpl => $description) {
  311. $dns_templates[$tpl] = $description;
  312. }
  313. return array(
  314. 'IP' => @$data['ips'],
  315. 'TPL' => $dns_templates,
  316. 'EXP' => array(),
  317. 'SOA' => array(),
  318. 'TTL' => array(),
  319. 'record' => array(
  320. 'RECORD' => array(),
  321. 'RECORD_TYPE' => array('A' => 'A', 'NS' => 'NS', 'MX' => 'MX', 'TXT' => 'TXT'),
  322. 'RECORD_VALUE' => array()
  323. )
  324. );
  325. }
  326. /**
  327. * DB initial params
  328. *
  329. * @params array $data
  330. * @return array
  331. */
  332. public function getDbParams($data = array())
  333. {
  334. $db_types = $this->getDBTypes();
  335. return array(
  336. 'TYPE' => $db_types,
  337. 'HOST' => array('vestacp.com' => 'vestacp.com', 'askcow.org' => 'askcow.org'),
  338. 'ENCODING' => array(
  339. 'utf8' => 'utf8', 'latin1' => 'latin1', 'cp1251' => 'cp1251',
  340. '' => '',
  341. 'big5' => 'Big5 — Traditional Chinese ',
  342. 'dec8' => 'dec8 — DEC West European ',
  343. 'cp850' => 'cp850 — DOS West European',
  344. 'hp8' => 'hp8 — HP West European',
  345. 'koi8r' => 'koi8r — KOI8-R Relcom Russian',
  346. 'latin1' => 'latin1 — cp1252 West European',
  347. 'latin2' => 'latin2 — ISO 8859-2 Central European',
  348. 'swe7' => 'swe7 — 7bit Swedish',
  349. 'ascii' => 'ascii — US ASCII',
  350. 'ujis' => 'ujis — EUC-JP Japanese',
  351. 'sjis' => 'sjis — Shift-JIS Japanese',
  352. 'hebrew' => 'hebrew — ISO 8859-8 Hebrew',
  353. 'tis620' => 'tis620 — TIS620 Thai',
  354. 'euckr' => 'euckr — EUC-KR Korean',
  355. 'koi8u' => 'koi8u — KOI8-U Ukrainian',
  356. 'gb2312' => 'gb2312 — GB2312 Simplified Chinese',
  357. 'greek' => 'greek — ISO 8859-7 Greek',
  358. 'cp1250' => 'cp1250 — Windows Central European',
  359. 'gbk' => 'gbk — GBK Simplified Chinese',
  360. 'latin5' => 'latin5 — ISO 8859-9 Turkish',
  361. 'armscii8' => 'armscii8— ARMSCII-8 Armenian',
  362. 'utf8' => 'utf8 — UTF-8 Unicode',
  363. 'ucs2' => 'ucs2 — UCS-2 Unicode',
  364. 'cp866' => 'cp866 — DOS Russian',
  365. 'keybcs2' => 'keybcs2 — DOS Kamenicky Czech-Slovak',
  366. 'macce' => 'macce — Mac Central European',
  367. 'macroman' => 'macroman— Mac West European',
  368. 'cp853' => 'cp852 — DOS Central European',
  369. 'latin7' => 'latin7 — ISO 8859-13 Baltic',
  370. 'cp1251' => 'cp1251 — Windows Cyrillic',
  371. 'cp1256' => 'cp1256 — Windows Arabic',
  372. 'cp1257' => 'cp1257 — Windows Baltic',
  373. 'binary' => 'binary — Binary pseudo charset',
  374. 'geostd8' => 'geostd8 — GEOSTD8 Georgian',
  375. 'cp932' => 'cp932 — SJIS for Windows Japanese',
  376. 'eucjpms' => 'eucjpms — UJIS for Windows Japanese'
  377. )
  378. );
  379. }
  380. public function getDBTypes()
  381. {
  382. return array('mysql' => 'mysql', 'postgre' => 'postgre');
  383. }
  384. /**
  385. * Users initial params
  386. *
  387. * @params array $data
  388. * @return array
  389. */
  390. public function getUsersParams($data = array(), $global_data = array())
  391. {
  392. $pckg = array();
  393. // json
  394. $result = Vesta::execute(Vesta::V_LIST_USER_PACKAGES, null, self::JSON);
  395. foreach ($result['data'] as $pckg_name => $pckg_data) {
  396. $pckg[$pckg_name] = $pckg_name;
  397. }
  398. return array(
  399. 'PACKAGE' => $pckg,
  400. 'SHELL' => array(
  401. 'sh' => 'sh',
  402. 'bash' => 'bash',
  403. 'nologin' => 'nologin',
  404. 'tcsh' => 'tcsh',
  405. 'csh' => 'csh')
  406. );
  407. }
  408. }