MAIN.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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()),
  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. );
  207. return $this->reply(true, $reply);
  208. }
  209. protected function getTemplates()
  210. {
  211. if (null != $this->templates) {
  212. return $this->templates;
  213. }
  214. else {
  215. $user = $this->getLoggedUser();
  216. $this->templates = array();
  217. $result = Vesta::execute(Vesta::V_LIST_WEB_TEMPLATES, array('USER' => $user['uid']), self::JSON);
  218. // TODO: handle errors!
  219. foreach ($result['data'] as $tpl => $description) {
  220. $this->templates[$tpl] = $description;
  221. }
  222. return $this->templates;
  223. }
  224. }
  225. /**
  226. * WEB DOMAIN initial params
  227. *
  228. * @params array $data
  229. * @return array
  230. */
  231. public function getWebDomainParams($data, $global_data)
  232. {
  233. $user = $this->getLoggedUser();
  234. $ips = array();
  235. $result = Vesta::execute(Vesta::V_LIST_USER_IPS, array('USER' => $user['uid']), self::JSON);
  236. foreach ($result['data'] as $sys_ip => $ip_data) {
  237. $ips[$sys_ip] = $sys_ip;
  238. }
  239. if (empty($ips)) {
  240. $ips['No available IP'] = 'No available IP';
  241. }
  242. return array(
  243. 'TPL' => $this->getTemplates(),
  244. 'ALIAS' => array(),
  245. 'STAT' => array(
  246. 'none' => 'none',
  247. 'webalizer' => 'webalizer',
  248. 'awstats' => 'awstats'
  249. ),
  250. 'IP' => $ips
  251. );
  252. }
  253. /**
  254. * CRON initial params
  255. *
  256. * @params array $data
  257. * @return array
  258. */
  259. public function getCronParams($data = array())
  260. {
  261. return array();
  262. }
  263. /**
  264. * IP initial params
  265. *
  266. * @params array $data
  267. * @return array
  268. */
  269. public function getIpParams($data = array(), $global_data = array())
  270. {
  271. $ifaces = array();
  272. $result = Vesta::execute(Vesta::V_LIST_SYS_INTERFACES, array(Config::get('response_type')));
  273. foreach ($result['data'] as $iface) {
  274. $ifaces[$iface] = $iface;
  275. }
  276. return array(
  277. 'SYS_USERS' => $global_data['users'],
  278. 'STATUSES' => array(
  279. 'shared' => 'shared',
  280. 'exclusive' => 'exclusive'
  281. ),
  282. 'INTERFACES' => $ifaces,
  283. 'OWNER' => $global_data['users'],
  284. 'MASK' => array(
  285. '255.255.255.0' => '255.255.255.0',
  286. '255.255.255.128' => '255.255.255.128',
  287. '255.255.255.192' => '255.255.255.192',
  288. '255.255.255.224' => '255.255.255.224',
  289. '255.255.255.240' => '255.255.255.240',
  290. '255.255.255.248' => '255.255.255.248',
  291. '255.255.255.252' => '255.255.255.252',
  292. '255.255.255.255' => '255.255.255.255'
  293. )
  294. );
  295. }
  296. /**
  297. * DNS initial params
  298. *
  299. * @params array $data
  300. * @return array
  301. */
  302. public function getDnsParams($data = array())
  303. {
  304. $dns_templates = array();
  305. $user = $this->getLoggedUser();
  306. $this->templates = array();
  307. $result = Vesta::execute(Vesta::V_LIST_DNS_TEMPLATES, null, self::JSON);
  308. // TODO: handle errors!
  309. foreach ($result['data'] as $tpl => $description) {
  310. $dns_templates[$tpl] = $description;
  311. }
  312. return array(
  313. 'IP' => @$data['ips'],
  314. 'TPL' => $dns_templates,
  315. 'EXP' => array(),
  316. 'SOA' => array(),
  317. 'TTL' => array(),
  318. 'record' => array(
  319. 'RECORD' => array(),
  320. 'RECORD_TYPE' => array('A' => 'A', 'NS' => 'NS', 'MX' => 'MX', 'TXT' => 'TXT'),
  321. 'RECORD_VALUE' => array()
  322. )
  323. );
  324. }
  325. /**
  326. * DB initial params
  327. *
  328. * @params array $data
  329. * @return array
  330. */
  331. public function getDbParams($data = array())
  332. {
  333. $db_types = $this->getDBTypes();
  334. return array(
  335. 'TYPE' => $db_types,
  336. 'HOST' => array('vestacp.com' => 'vestacp.com', 'askcow.org' => 'askcow.org')
  337. );
  338. }
  339. public function getDBTypes()
  340. {
  341. return array('mysql' => 'mysql', 'postgre' => 'postgre');
  342. }
  343. /**
  344. * Users initial params
  345. *
  346. * @params array $data
  347. * @return array
  348. */
  349. public function getUsersParams($data = array(), $global_data = array())
  350. {
  351. $pckg = array();
  352. // json
  353. $result = Vesta::execute(V_LIST_USER_PACKAGES, null, self::JSON);
  354. foreach ($result['data'] as $pckg_name => $pckg_data) {
  355. $pckg[$pckg_name] = $pckg_name;
  356. }
  357. return array(
  358. 'PACKAGE' => $pckg,
  359. 'SHELL' => array(
  360. 'sh' => 'sh',
  361. 'bash' => 'bash',
  362. 'nologin' => 'nologin',
  363. 'tcsh' => 'tcsh',
  364. 'csh' => 'csh')
  365. );
  366. }
  367. }