MAIN.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. $user = $this->getLoggedUser();
  38. $rs = $config = Vesta::execute('v_get_sys_user_value', array($user['uid'], 'RKEY'));
  39. if (empty($_SESSION['captcha_key'])
  40. || $_SESSION['captcha_key'] != $request->getParameter('captcha')) {
  41. return $this->reply(false, null, 'Captcha is invalid ');
  42. }
  43. $users = Vesta::execute(Vesta::V_LIST_SYS_USERS, 'json');
  44. $email_matched_count = array();
  45. if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$request->getParameter('email'))) {
  46. return $this->reply(false, null, 'Email is invalid');
  47. }
  48. foreach ($users['data'] as $user) {
  49. if ($user['CONTACT'] == trim($request->getParameter('email'))) {
  50. $email_matched_count[] = $user;
  51. }
  52. }
  53. if (empty($email_matched_count)) {
  54. return $this->reply(false, null, 'There is no such user.');
  55. }
  56. $secret_key = $this->generateResetPasswordKey();
  57. $reset_link = 'https://'.$_SERVER['HTTP_HOST'].'/change_password.php?v='.$secret_key;
  58. $mail_body = <<<MAIL
  59. <div lang="en" style="background-color:#fff;color:#222">
  60. <a target="_blank" href="" style="color:#FFF">
  61. <img width="81" height="22" style="display:block;border:0" src="http://vestacp.com/i/logo.png" alt="Twitter">
  62. </a>
  63. <div style="font-family:'Helvetica Neue', Arial, Helvetica, sans-serif;font-size:13px;margin:14px">
  64. <h2 style="font-family:'Helvetica Neue', Arial, Helvetica, sans-serif;margin:0 0 16px;font-size:18px;font-weight:normal">
  65. Vesta received a request to reset the password for your account {$user['FNAME']} {$user['LNAME']}?
  66. </h2>
  67. <p>
  68. If you want to reset your password, click on the link below (or copy and paste the URL into your browser):<br>
  69. <a target="_blank" href="{$reset_link}">{$reset_link}</a>
  70. </p>
  71. <p>
  72. If you don't want to reset your password, please ignore this message.
  73. Your password will not be reset.
  74. If you have any concerns, please contact us at support@vestacp.com.
  75. </p>
  76. <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">
  77. <span style="font:italic 13px Georgia,serif;color:rgb(102, 102, 102)">VestaCP</span>
  78. </p>
  79. <p style="font-family:'Helvetica Neue', Arial, Helvetica, sans-serif;margin-top:5px;font-size:10px;color:#888888">
  80. Please do not reply to this message; it was sent from an unmonitored email address.
  81. </p>
  82. </div>
  83. </div>
  84. MAIL;
  85. $headers = 'MIME-Version: 1.0' . "\n";
  86. $headers .= 'Content-type: text/html; charset=UTF-8' . "\n";
  87. $to = $request->getParameter('email');
  88. $subject = 'Reset your Vesta password';
  89. $message = $mail_body;
  90. mail($to, $subject, $message, $headers);
  91. return $this->reply(true, array('key_code' => substr($secret_key, 0, 5) . $_SERVER['REQUEST_TIME'] . substr($secret_key, -5)));
  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_sys_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. $result = Vesta::execute('v_check_sys_user_password', array('USER' => $login, 'PASSWORD' => $password), self::TEXT);
  106. if ($result['status'] == true) {
  107. return $this->reply(true, array('v_sd' => VestaSession::authorize($login)));
  108. }
  109. else {
  110. return $this->reply(false, array('error_msg' => 'Incorrect login or password'));
  111. }
  112. }
  113. public function logoffExecute($request)
  114. {
  115. VestaSession::logoff();
  116. return $this->reply(true);
  117. }
  118. public function getBackupsExecute(Request $request)
  119. {
  120. $user = VestaSession::getInstance()->getUser();
  121. $rs = Vesta::execute(Vesta::V_LIST_SYS_USER_BACKUPS, array('USER' => $user['uid'], 'RESPONSE' => 'json'));
  122. return $this->reply($rs['status'], @$rs['data']);
  123. }
  124. /**
  125. * Get Initial params.
  126. * Global constants / variables / configs
  127. *
  128. * @param Request $request
  129. * @return string - Ajax Reply
  130. */
  131. public function getInitialExecute(Request $request)
  132. {
  133. $user = VestaSession::getInstance()->getUser();
  134. $global_data = array();
  135. $totals = array(
  136. 'USER' => array('total' => 0, 'blocked' => 0),
  137. 'WEB_DOMAIN' => array('total' => 0, 'blocked' => 0),
  138. 'MAIL' => array('total' => 0),
  139. 'DB' => array('total' => 0, 'blocked' => 0),
  140. 'DNS' => array('total' => 0, 'blocked' => 0),
  141. 'IP' => array('total' => 0, 'blocked' => 0),
  142. 'CRON' => array('total' => 0, 'blocked' => 0)
  143. );
  144. // users
  145. $rs = Vesta::execute(Vesta::V_LIST_SYS_USERS, null, self::JSON);
  146. $data_user = $rs['data'];
  147. $global_data['users'] = array();
  148. foreach ($data_user as $login_name => $usr) {
  149. $totals['USER']['total'] += 1;
  150. if ($usr['SUSPENDED'] != 'yes') {
  151. $global_data['users'][$login_name] = $login_name;
  152. }
  153. else {
  154. $totals['USER']['blocked'] += 1;
  155. }
  156. }
  157. // web_domains
  158. $rs = Vesta::execute(Vesta::V_LIST_WEB_DOMAINS, array('USER' => $user['uid']), self::JSON);
  159. $data_web_domain = $rs['data'];
  160. foreach ($data_web_domain as $web) {
  161. $totals['WEB_DOMAIN']['total'] += 1;
  162. }
  163. // db
  164. $rs = Vesta::execute(Vesta::V_LIST_DB_BASES, array('USER' => $user['uid']), self::JSON);
  165. $data_db = $rs['data'];
  166. foreach ($data_db as $db) {
  167. $totals['DB']['total'] += 1;
  168. }
  169. // dns
  170. $rs = Vesta::execute(Vesta::V_LIST_DNS_DOMAINS, array('USER' => $user['uid']), self::JSON);
  171. $data_dns = $rs['data'];
  172. foreach ($data_dns as $dns) {
  173. $totals['DNS']['total'] += 1;
  174. }
  175. // ip
  176. $global_data['ips'] = array();
  177. $rs = Vesta::execute(Vesta::V_LIST_SYS_IPS, null, self::JSON);
  178. $data_ip = $rs['data'];
  179. foreach ($data_ip as $ip => $obj) {
  180. $totals['IP']['total'] += 1;
  181. $global_data['ips'][$ip] = $ip;
  182. }
  183. // cron
  184. $rs = Vesta::execute(Vesta::V_LIST_CRON_JOBS, array('USER' => $user['uid']), self::JSON);
  185. $data_cron = $rs['data'];
  186. foreach ($data_cron as $cron) {
  187. $totals['CRON']['total'] += 1;
  188. $cron['SUSPEND'] == 'yes' ? $totals['CRON']['blocked'] += 1 : false;
  189. }
  190. $reply = array(
  191. 'auth_user' => array('uid' => $this->getLoggedUser()),
  192. 'WEB_DOMAIN' => $this->getWebDomainParams($data_web_domain, $global_data),
  193. 'CRON' => $this->getCronParams(),
  194. 'IP' => $this->getIpParams($data_ip, $global_data),
  195. 'DNS' => $this->getDnsParams(),
  196. 'DB' => $this->getDbParams($data_db),
  197. 'USERS' => $this->getUsersParams($data_user),
  198. 'totals' => $totals,
  199. 'PROFILE' => $user
  200. );
  201. return $this->reply(true, $reply);
  202. }
  203. protected function getTemplates()
  204. {
  205. if (null != $this->templates) {
  206. return $this->templates;
  207. }
  208. else {
  209. $user = $this->getLoggedUser();
  210. $this->templates = array();
  211. $result = Vesta::execute(Vesta::V_LIST_WEB_TEMPLATES, array('USER' => $user['uid']), self::JSON);
  212. // TODO: handle errors!
  213. foreach ($result['data'] as $tpl => $description) {
  214. $this->templates[$tpl] = $description;
  215. }
  216. return $this->templates;
  217. }
  218. }
  219. /**
  220. * WEB DOMAIN initial params
  221. *
  222. * @params array $data
  223. * @return array
  224. */
  225. public function getWebDomainParams($data, $global_data)
  226. {
  227. $user = $this->getLoggedUser();
  228. $ips = array();
  229. $result = Vesta::execute(Vesta::V_LIST_SYS_USER_IPS, array('USER' => $user['uid']), self::JSON);
  230. foreach ($result['data'] as $sys_ip => $ip_data) {
  231. $ips[$sys_ip] = $sys_ip;
  232. }
  233. if (empty($ips)) {
  234. $ips['No available IP'] = 'No available IP';
  235. }
  236. return array(
  237. 'TPL' => $this->getTemplates(),
  238. 'ALIAS' => array(),
  239. 'STAT' => array(
  240. 'none' => 'none',
  241. 'webalizer' => 'webalizer',
  242. 'awstats' => 'awstats'
  243. ),
  244. 'IP' => $ips
  245. );
  246. }
  247. /**
  248. * CRON initial params
  249. *
  250. * @params array $data
  251. * @return array
  252. */
  253. public function getCronParams($data = array())
  254. {
  255. return array();
  256. }
  257. /**
  258. * IP initial params
  259. *
  260. * @params array $data
  261. * @return array
  262. */
  263. public function getIpParams($data = array(), $global_data = array())
  264. {
  265. $ifaces = array();
  266. $result = Vesta::execute(Vesta::V_LIST_SYS_INTERFACES, array(Config::get('response_type')));
  267. foreach ($result['data'] as $iface) {
  268. $ifaces[$iface] = $iface;
  269. }
  270. return array(
  271. 'SYS_USERS' => $global_data['users'],
  272. 'STATUSES' => array(
  273. 'shared' => 'shared',
  274. 'exclusive' => 'exclusive'
  275. ),
  276. 'INTERFACES' => $ifaces,
  277. 'OWNER' => $global_data['users'],
  278. 'MASK' => array(
  279. '255.255.255.0' => '255.255.255.0',
  280. '255.255.255.128' => '255.255.255.128',
  281. '255.255.255.192' => '255.255.255.192',
  282. '255.255.255.224' => '255.255.255.224',
  283. '255.255.255.240' => '255.255.255.240',
  284. '255.255.255.248' => '255.255.255.248',
  285. '255.255.255.252' => '255.255.255.252',
  286. '255.255.255.255' => '255.255.255.255'
  287. )
  288. );
  289. }
  290. /**
  291. * DNS initial params
  292. *
  293. * @params array $data
  294. * @return array
  295. */
  296. public function getDnsParams($data = array())
  297. {
  298. $dns_templates = array();
  299. $user = $this->getLoggedUser();
  300. $this->templates = array();
  301. $result = Vesta::execute(Vesta::V_LIST_DNS_TEMPLATES, null, self::JSON);
  302. // TODO: handle errors!
  303. foreach ($result['data'] as $tpl => $description) {
  304. $dns_templates[$tpl] = $description;
  305. }
  306. return array(
  307. 'IP' => @$data['ips'],
  308. 'TPL' => $dns_templates,
  309. 'EXP' => array(),
  310. 'SOA' => array(),
  311. 'TTL' => array(),
  312. 'record' => array(
  313. 'RECORD' => array(),
  314. 'RECORD_TYPE' => array('A' => 'A', 'NS' => 'NS', 'MX' => 'MX', 'TXT' => 'TXT'),
  315. 'RECORD_VALUE' => array()
  316. )
  317. );
  318. }
  319. /**
  320. * DB initial params
  321. *
  322. * @params array $data
  323. * @return array
  324. */
  325. public function getDbParams($data = array())
  326. {
  327. $db_types = $this->getDBTypes();
  328. return array(
  329. 'TYPE' => $db_types,
  330. 'HOST' => array('vestacp.com' => 'vestacp.com', 'askcow.org' => 'askcow.org')
  331. );
  332. }
  333. public function getDBTypes()
  334. {
  335. return array('mysql' => 'mysql', 'postgre' => 'postgre');
  336. }
  337. /**
  338. * Users initial params
  339. *
  340. * @params array $data
  341. * @return array
  342. */
  343. public function getUsersParams($data = array(), $global_data = array())
  344. {
  345. $pckg = array();
  346. // json
  347. $result = Vesta::execute('v_list_sys_user_packages', null, self::JSON);
  348. foreach ($result['data'] as $pckg_name => $pckg_data) {
  349. $pckg[$pckg_name] = $pckg_name;
  350. }
  351. return array(
  352. 'PACKAGE' => $pckg,
  353. 'SHELL' => array(
  354. 'sh' => 'sh',
  355. 'bash' => 'bash',
  356. 'nologin' => 'nologin',
  357. 'tcsh' => 'tcsh',
  358. 'csh' => 'csh')
  359. );
  360. }
  361. }