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