PageRenderTime 237ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/index.php

http://freshdns.googlecode.com/
PHP | 371 lines | 318 code | 49 blank | 4 comment | 5 complexity | 3693e06c6ef6be904e61eb50eb004e5e MD5 | raw file
Possible License(s): GPL-2.0
  1. <?
  2. // ALL BROWSER CACHE MUST DIE!
  3. header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
  4. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
  5. include_once('./config.inc.php');
  6. $login = new login($config['database']);
  7. $json = new Services_JSON();
  8. $manager = new manager($config['database']);
  9. try {
  10. $login->isLoggedIn();
  11. }catch(Exception $ex)
  12. {
  13. $login->logout();
  14. header("Location: index.php");
  15. exit;
  16. }
  17. if(!$login->isLoggedIn())
  18. // THE USER IS NOT LOGGED IN
  19. {
  20. switch($_GET['p'])
  21. {
  22. default:
  23. include('./templates/header.tpl.php');
  24. echo '<div id="body">
  25. <div id="info"><table>
  26. <tr>
  27. <td rowspan="2" width="70"><img id="infoimg" src="./images/info.png" alt="Welcome!" /></td>
  28. <td><b>Welcome to FreshDNS<span id="infoHead"></span></b></td>
  29. </tr>
  30. <tr>
  31. <td rowspan="2">FreshDNS is a webbased, PHP and AJAX powered DNS-administration tool for powerDNS</td>
  32. </tr>
  33. </table></div>
  34. <div class="leeg">&nbsp;</div>
  35. <div id="login"></div></div>';
  36. echo '<script type="text/javascript" language="JavaScript1.2">loginForm();</script>';
  37. include('./templates/footer.tpl.php');
  38. break;
  39. case "doLogin":
  40. try
  41. {
  42. $login->login($_POST['username'], $_POST['password']);
  43. $return = array("status" => "success", "text" => "Welcome, you have been logged in.", "reload" => "yes");
  44. echo $json->encode($return);
  45. }catch(Exception $ex)
  46. {
  47. $return = array("status" => "failed", "text" => $ex->getMessage());
  48. echo $json->encode($return);
  49. }
  50. break;
  51. case "phpinfo":
  52. phpinfo();
  53. break;
  54. }
  55. }else
  56. {
  57. switch ($_GET['p'])
  58. {
  59. default:
  60. case "frontpage":
  61. include('./templates/header.tpl.php');
  62. //echo '<div id="list"></div>';
  63. if(!isset($_GET['q']))
  64. {
  65. echo '<script type="text/javascript" language="JavaScript1.2">list(\'A\');</script>';
  66. }else
  67. {
  68. echo '<script type="text/javascript" language="JavaScript1.2">liveSearchStart();</script>';
  69. }
  70. include('./templates/footer.tpl.php');
  71. break;
  72. case "livesearch":
  73. $records = $manager->searchDomains($_GET['q']);
  74. echo $json->encode($records);
  75. break;
  76. case "letterlist":
  77. $records = $manager->getListByLetter(strtolower($_GET['letter']));
  78. echo $json->encode($records);
  79. break;
  80. case "ownerslist":
  81. $records = $manager->getListByOwner($_GET['userId']);
  82. echo $json->encode($records);
  83. break;
  84. case "deleteZone":
  85. if(!$_GET['domainId'])
  86. {
  87. $return = array("status" => "failed", "text" => "There was no domainId recieved");
  88. echo $json->encode($return);
  89. }else
  90. {
  91. try
  92. {
  93. $manager->removeAllRecords($_GET['domainId']);
  94. $manager->removeZoneByDomainId($_GET['domainId']);
  95. $manager->removeDomain($_GET['domainId']);
  96. $return = array("status" => "success", "text" => "The zone has been deleted.");
  97. echo $json->encode($return);
  98. }catch(Exception $ex)
  99. {
  100. $return = array("status" => "failed", "text" => $ex->getMessage());
  101. echo $json->encode($return);
  102. }
  103. }
  104. break;
  105. case "getDomainInfo":
  106. if(!$_GET['domainId'])
  107. {
  108. $return = array("status" => "failed", "text" => "There was no domainId recieved");
  109. echo $json->encode($return);
  110. exit;
  111. }
  112. $domain = $manager->getDomain($_GET['domainId']);
  113. $records = $manager->getAllRecords($_GET['domainId']);
  114. $return = array('domain' => $domain, 'records' => $records);
  115. echo $json->encode($return);
  116. unset($domain, $records);
  117. break;
  118. case "saveRecord":
  119. try
  120. {
  121. $manager->updateRecord($_POST['recordId'], $_POST['recordId'], $_POST['domainId'], $_POST['name'], $_POST['type'], $_POST['content'], $_POST['ttl'], $_POST['prio'], time());
  122. $return = array("status" => "success", "text" => "The record has been updated.");
  123. echo $json->encode($return);
  124. }catch (Exception $ex)
  125. {
  126. $return = array("status" => "failed", "text" => $ex->getMessage());
  127. echo $json->encode($return);
  128. }
  129. break;
  130. case "saveAllRecords":
  131. try
  132. {
  133. foreach($_POST['id'] AS $rowId => $recordId)
  134. {
  135. $manager->updateRecord ($recordId, $recordId, $_POST['domainId'], $_POST['name'][$rowId], $_POST['type'][$rowId], $_POST['content'][$rowId], $_POST['ttl'][$rowId], $_POST['prio'][$rowId], time(), false);
  136. }
  137. $manager->updateSoaSerial($_POST['domainId']);
  138. $return = array("status" => "success", "text" => "All records have been updated.");
  139. echo $json->encode($return);
  140. }catch (Exception $ex)
  141. {
  142. $return = array("status" => "failed", "text" => $ex->getMessage());
  143. echo $json->encode($return);
  144. }
  145. break;
  146. case "removeRecord":
  147. try
  148. {
  149. $manager->removeRecord($_GET['recordId'], $_GET['domainId']);
  150. $return = array("status" => "success", "text" => "The record has been deleted.");
  151. echo $json->encode($return);
  152. }catch (Exception $ex)
  153. {
  154. $return = array("status" => "failed", "text" => $ex->getMessage());
  155. echo $json->encode($return);
  156. }
  157. break;
  158. case "newRecord":
  159. try
  160. {
  161. $manager->addRecord ($_POST['domainId'], $_POST['name'], $_POST['type'], $_POST['content'], $_POST['ttl'], $_POST['prio'], $_POST['changeDate']);
  162. $return = array("status" => "success", "text" => "The record has been added.");
  163. echo $json->encode($return);
  164. }catch (Exception $ex)
  165. {
  166. $return = array("status" => "failed", "text" => $ex->getMessage());
  167. echo $json->encode($return);
  168. }
  169. break;
  170. case "transferDomain":
  171. try
  172. {
  173. $manager->transferDomain($_POST['domainId'], $_POST['owner']);
  174. $return = array("status" => "success", "text" => "The domain has been transfered.");
  175. echo $json->encode($return);
  176. }catch (Exception $ex)
  177. {
  178. $return = array("status" => "failed", "text" => $ex->getMessage());
  179. echo $json->encode($return);
  180. }
  181. break;
  182. case "getOwners":
  183. try
  184. {
  185. $return = $manager->getAllOwners();
  186. echo $json->encode($return);
  187. }catch (Exception $ex)
  188. {
  189. $return = array("status" => "failed", "text" => $ex->getMessage());
  190. echo $json->encode($return);
  191. }
  192. break;
  193. case "getTemplates":
  194. $return = array();
  195. foreach($config['DNS']['templates'] AS $name => $values)
  196. {
  197. $return[] = $name;
  198. }
  199. echo $json->encode($return);
  200. break;
  201. case "newDomain":
  202. try
  203. {
  204. $domainId = $manager->addDomain(trim($_POST['domain']), $_POST['master'], $_POST['lastCheck'], $_POST['type'], $_POST['notifiedSerial'], $_POST['account']);
  205. $manager->addZone($domainId, $_POST['owner'], "");
  206. foreach($config['DNS']['templates'][$_POST['template']] AS $record)
  207. {
  208. $record['name'] = str_replace("{#DOMAIN#}", $_POST['domain'], $record['name']);
  209. $record['content'] = str_replace("{#DOMAIN#}", $_POST['domain'], $record['content']);
  210. $record['content'] = str_replace("{#NS0#}", $config['DNS']['ns0'], $record['content']);
  211. $record['content'] = str_replace("{#NS1#}", $config['DNS']['ns1'], $record['content']);
  212. $record['content'] = str_replace("{#NS2#}", $config['DNS']['ns2'], $record['content']);
  213. $record['content'] = str_replace("{#WEBIP#}", $_POST['webIP'], $record['content']);
  214. $record['content'] = str_replace("{#MAILIP#}", $_POST['mailIP'], $record['content']);
  215. $record['content'] = str_replace("{#HOSTMASTER#}", $config['DNS']['hostmaster'], $record['content']);
  216. $record['content'] = str_replace("{#SOACODE#}", $manager->createNewSoaSerial(), $record['content']);
  217. $manager->addRecord ($domainId, $record['name'], $record['type'], $record['content'], $record['ttl'], $record['prio'], $record['changeDate']);
  218. }
  219. // IT'S A NEW DOMAIN, RESET THE SOA TO 00
  220. $manager->setSoaSerial ($domainId, $config['DNS']['ns0'], $config['DNS']['hostmaster'], $manager->createNewSoaSerial());
  221. $return = array("status" => "success", "text" => "The domain has been added.");
  222. echo $json->encode($return);
  223. }catch (Exception $ex)
  224. {
  225. $return = array("status" => "failed", "text" => $ex->getMessage());
  226. echo $json->encode($return);
  227. }
  228. break;
  229. case "newDomains":
  230. $domains = explode("\n", $_POST['domains']);
  231. $succes = array();
  232. $failed = array();
  233. foreach($domains AS $domain)
  234. {
  235. try
  236. {
  237. $domainId = $manager->addDomain(trim($domain), $_POST['master'], $_POST['lastCheck'], $_POST['type'], $_POST['notifiedSerial'], $_POST['account']);
  238. $manager->addZone($domainId, $_POST['owner'], "");
  239. foreach($config['DNS']['templates'][$_POST['template']] AS $record)
  240. {
  241. $record['name'] = str_replace("{#DOMAIN#}", $domain, $record['name']);
  242. $record['content'] = str_replace("{#DOMAIN#}", $domain, $record['content']);
  243. $record['content'] = str_replace("{#NS0#}", $config['DNS']['ns0'], $record['content']);
  244. $record['content'] = str_replace("{#NS1#}", $config['DNS']['ns1'], $record['content']);
  245. $record['content'] = str_replace("{#NS2#}", $config['DNS']['ns2'], $record['content']);
  246. $record['content'] = str_replace("{#WEBIP#}", $_POST['webIP'], $record['content']);
  247. $record['content'] = str_replace("{#MAILIP#}", $_POST['mailIP'], $record['content']);
  248. $record['content'] = str_replace("{#HOSTMASTER#}", $config['DNS']['hostmaster'], $record['content']);
  249. $record['content'] = str_replace("{#SOACODE#}", $manager->createNewSoaSerial(), $record['content']);
  250. $manager->addRecord ($domainId, $record['name'], $record['type'], $record['content'], $record['ttl'], $record['prio'], $record['changeDate']);
  251. }
  252. $succes[] = "The domain ".$domain." has been added.";
  253. }catch (Exception $ex)
  254. {
  255. $failed[] = "Failed: The domain ".$domain." hasn't been added: ".$ex->getMessage()."\n";
  256. }
  257. }
  258. $return = array("status" => "success", "text" => implode("\n", $succes)."\n".implode("\n", $failed));
  259. echo $json->encode($return);
  260. break;
  261. case "deleteUser":
  262. try
  263. {
  264. $manager->removeUserData($_POST['userId']);
  265. $manager->removeUser($_POST['userId']);
  266. $return = array("status" => "success", "text" => "All user data has been deleted.");
  267. echo $json->encode($return);
  268. }catch (Exception $ex)
  269. {
  270. $return = array("status" => "failed", "text" => $ex->getMessage());
  271. echo $json->encode($return);
  272. }
  273. break;
  274. case "getUser":
  275. echo $json->encode($manager->getUser($_POST['userId']));
  276. break;
  277. case "editUser":
  278. try
  279. {
  280. $manager->updateUser($_POST['userId'], $_POST['userId'], $_POST['username'], $_POST['password'], $_POST['fullname'], $_POST['email'], $_POST['description'], $_POST['level'], $_POST['active'], $_POST['maxdomains']);
  281. $return = array("status" => "success", "text" => "The user has been editted.");
  282. echo $json->encode($return);
  283. }catch (Exception $ex)
  284. {
  285. $return = array("status" => "failed", "text" => $ex->getMessage());
  286. echo $json->encode($return);
  287. }
  288. break;
  289. case "addUser":
  290. try
  291. {
  292. $manager->addUser($_POST['username'], md5($_POST['password']), $_POST['fullname'], $_POST['email'], $_POST['description'], $_POST['level'], $_POST['active'], $_POST['maxdomains']);
  293. $return = array("status" => "success", "text" => "The user has been added.");
  294. echo $json->encode($return);
  295. }catch (Exception $ex)
  296. {
  297. $return = array("status" => "failed", "text" => $ex->getMessage());
  298. echo $json->encode($return);
  299. }
  300. break;
  301. case "phpinfo":
  302. phpinfo();
  303. break;
  304. case "logout":
  305. $login->logout();
  306. echo '<script>window.location.href="index.php";</script>';
  307. break;
  308. }
  309. }
  310. unset($json, $manager);
  311. ?>