MAIN.class.php 15 KB

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