PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/index.php

http://github.com/shupp/VegaDNS
PHP | 263 lines | 159 code | 62 blank | 42 comment | 72 complexity | 797259903aba854a970aed40c7ad7d05 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /*
  3. *
  4. * VegaDNS - DNS Administration Tool for use with djbdns
  5. *
  6. * CREDITS:
  7. * Written by Bill Shupp
  8. * <hostmaster@shupp.org>
  9. *
  10. * LICENSE:
  11. * This software is distributed under the GNU General Public License
  12. * Copyright 2003-2016, Bill Shupp
  13. * see COPYING for details
  14. *
  15. */
  16. // PHP INIT/SECURITY STUFF
  17. ini_set('display_errors', 0);
  18. ini_set('display_startup_errors', 0);
  19. ini_set('log_errors', 1);
  20. ini_set('allow_url_fopen', 0);
  21. ini_set('session.use_cookies',0);
  22. ini_set('session.use_only_cookies', 0);
  23. ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
  24. // Smarty
  25. define('SMARTY_DIR', 'smarty/');
  26. require(SMARTY_DIR.'/Smarty.class.php');
  27. $smarty = new Smarty;
  28. $smarty->assign('php_self', $_SERVER['PHP_SELF']);
  29. // Get configuration settings
  30. require('src/config.php');
  31. // Set version
  32. $smarty->assign('version', $version);
  33. // Get functions
  34. require('src/functions.php');
  35. // Get IPv6 Functions
  36. require_once 'src/Net/IPv6.php';
  37. // Get PDO wrapper and connect
  38. require_once 'src/VDB.php';
  39. try {
  40. $pdo = VDB::singleton();
  41. } catch (Exception $e) {
  42. echo "Error connecting to database: " . $e->getMessage();
  43. exit;
  44. }
  45. // Make sure the private_dirs exist and are writable
  46. if(!is_writable($session_dir)) die("Error: $session_dir is not writabale. Please read INSTALL");
  47. if(!is_writable("$private_dirs/templates_c")) die("Error: $private_dirs/templates_c is not writabale. Please read INSTALL");
  48. if(!is_writable("$private_dirs/configs")) die("Error: $private_dirs/configs is not writabale. Please read INSTALL");
  49. if(!is_writable("$private_dirs/cache")) die("Error: $private_dirs/cache is not writabale. Please read INSTALL");
  50. if(isset($_REQUEST['state']) && $_REQUEST['state'] == 'get_data') {
  51. // Check trusted hosts
  52. $array = explode(',',$trusted_hosts);
  53. $remote_addr = ip2long($_SERVER['REMOTE_ADDR']);
  54. while((list($key,$value) = each($array)) && $trusted == 0) {
  55. $cidr = explode("/", trim($value), 2);
  56. $addr = ip2long($cidr[0]);
  57. $len = (count($cidr) == 2) ? intval($cidr[1]) : 32;
  58. $shift = 32 - $len;
  59. if (($remote_addr >> $shift) == ($addr >> $shift)) {
  60. $trusted = 1;
  61. break;
  62. }
  63. }
  64. if($trusted == 1) {
  65. // EXPORT DATA
  66. header("Content-type: text/plain");
  67. require('src/data.php');
  68. } else {
  69. header($_SERVER['SERVER_PROTOCOL'] . " 403 Forbidden");
  70. echo "Error: Host ".$_SERVER['REMOTE_ADDR']." is not authorized to access this page";
  71. }
  72. exit;
  73. }
  74. // Setup session
  75. session_name('VDNSSessid');
  76. if (isset($use_mysql_sessions) && $use_mysql_sessions == true) {
  77. require_once 'src/MySQLSessions.php';
  78. $mysql_sessions = MysqlSessions::singleton();
  79. } else {
  80. session_save_path($session_dir);
  81. session_start();
  82. }
  83. $smarty->assign('session_name', session_name());
  84. $smarty->assign('session_id', session_id());
  85. if(!isset($_REQUEST['state'])) {
  86. if(check_first_use() == 1) {
  87. // Add tables
  88. require('src/create_tables.php');
  89. set_msg("Welcome to VegaDNS!<br>Please edit your account settings for the initial 'senior_admin'");
  90. header("Location: ".$_SERVER['PHP_SELF']."?".SID."&state=logged_in&mode=users&user_mode=edit_account&cid=1");
  91. exit;
  92. }
  93. // MAIN
  94. $smarty->display('header.tpl');
  95. $smarty->display('login_screen.tpl');
  96. $smarty->display('footer.tpl');
  97. exit;
  98. } else if($_REQUEST['state'] == "end") {
  99. // CANCEL
  100. // End session
  101. $q = "delete from active_sessions where sid='".session_id()."'";
  102. $result = $pdo->query($q) or die(print_r($pdo->errorInfo()));
  103. session_unset();
  104. session_destroy();
  105. header("Location: index.php");
  106. exit;
  107. } else if($_REQUEST['state'] == "login_screen") {
  108. // LOGIN SCREEN
  109. $smarty->display('header.tpl');
  110. require('src/login_screen.php');
  111. $smarty->display('footer.tpl');
  112. exit;
  113. } else if($_REQUEST['state'] == "login") {
  114. // LOGIN
  115. if(!isset($_REQUEST['mode'])) {
  116. $auth = "FALSE";
  117. if(isset($_REQUEST['email']) && isset($_REQUEST['password'])) {
  118. $auth = authenticate_user($_REQUEST['email'], $_REQUEST['password']);
  119. } else {
  120. set_msg_err("Error: You must supply a username and password");
  121. header("Location: ".$_SERVER['PHP_SELF']."?".SID);
  122. exit;
  123. }
  124. if($auth == "TRUE") {
  125. header("Location: ".$_SERVER['PHP_SELF']."?".SID."&state=logged_in");
  126. exit;
  127. } else {
  128. set_msg_err("Error signing on: incorrect email address or password<p><a href=".$_SERVER['PHP_SELF']."?".SID."&state=help>forgot your password?</a>");
  129. header("Location: ".$_SERVER['PHP_SELF']."?".SID);
  130. exit;
  131. }
  132. } else {
  133. // Make sure they are logged in
  134. $email = verify_session();
  135. if($email == "") {
  136. set_msg_err("Error: you do not appear to be logged in");
  137. header("Location: ".$_SERVER['PHP_SELF']."?".SID);
  138. exit;
  139. } else {
  140. header("Location: ".$_SERVER['PHP_SELF']."?".SID."&state=logged_in");
  141. exit;
  142. }
  143. }
  144. } else if($_REQUEST['state'] == "logged_in") {
  145. // SHOW MAIN SCREEN
  146. // First make sure they are really logged in!
  147. $email = verify_session();
  148. if($email == "") {
  149. set_msg_err("Error: you do not appear to be logged in.");
  150. header("Location: ".$_SERVER['PHP_SELF']."?".SID);
  151. exit;
  152. } else {
  153. // Set base url for convenience
  154. $base_url = $_SERVER['PHP_SELF']."?".SID."&state=logged_in";
  155. // Get current account settings
  156. $result = $pdo->query("select * from accounts where Email='$email'")
  157. or die(print_r($pdo->errorInfo()));
  158. $user_info = $result->fetchAll();
  159. $user_info = $user_info[0];
  160. // Setup smarty stuff
  161. $smarty->assign('email', $email);
  162. $smarty->assign('state', $_REQUEST['state']);
  163. if(isset($_REQUEST['mode']))
  164. $smarty->assign('mode', $_REQUEST['mode']);
  165. $smarty->assign('base_url', $base_url);
  166. $smarty->assign('logout_url', $_SERVER['PHP_SELF'].'?'.SID.'&state=end');
  167. $smarty->assign('account_type', $user_info['Account_Type']);
  168. $smarty->assign('cid', $user_info['cid']);
  169. if(!isset($_REQUEST['mode']) || $_REQUEST['mode'] == "main_menu") {
  170. $smarty->display('header.tpl');
  171. $smarty->display('footer.tpl');
  172. exit;
  173. } else if($_REQUEST['mode'] == "domains") {
  174. // LIST DOMAINS
  175. require('src/domains.php');
  176. exit;
  177. } else if($_REQUEST['mode'] == "users") {
  178. // USERS
  179. require('src/users.php');
  180. exit;
  181. } else if($_REQUEST['mode'] == "records") {
  182. // LIST RECORDS FOR DOMAIN
  183. require('src/records.php');
  184. exit;
  185. } else if($_REQUEST['mode'] == "default_records") {
  186. // LIST DEFAULT RECORDS FOR NEW DOMAINS
  187. require('src/default_records.php');
  188. exit;
  189. } else if($_REQUEST['mode'] == "dnsquery") {
  190. // LIST DEFAULT RECORDS FOR NEW DOMAINS
  191. require('src/dnsquery.php');
  192. exit;
  193. } else {
  194. die("Error: illegal mode\n");
  195. }
  196. }
  197. } else if($_REQUEST['state'] == "help") {
  198. require('src/help.php');
  199. exit;
  200. }
  201. ?>