Browse Source

Initial release - 0.1.0 .

themage
theMage [php] 10 years ago
parent
commit
4c79efb91f

+ 12 - 0
php/admin/delete.php

@ -0,0 +1,12 @@
1
<?php
2
if (!function_exists('check_login')) { include("include.php"); }
3
4
$fwdid = $_GET['id'];
5
if (!preg_match('#^\d+$#', $fwdid)) { get_error(); }
6
7
$query = "DELETE FROM redirects
8
		WHERE id=$fwdid";
9
$res = $dbconnect->query($query);
10
is_error($res);
11
12
?>

+ 18 - 0
php/admin/edit.php

@ -0,0 +1,18 @@
1
<?php
2
if (!function_exists('check_login')) { include("include.php"); }
3
4
$fwdid = $_GET['id'];
5
if (!preg_match('#^\d+$#', $fwdid)) { get_error(); }
6
7
$res = $dbconnect->query("SELECT *,flags&1 active FROM redirects WHERE id = $fwdid");
8
is_error($res);
9
if ($res->numRows() != 0) {
10
	$smarty->assign('forward', $res->fetchRow(DB_FETCHMODE_ASSOC));
11
	$smarty->assign('pagetitle', 'Edit Redirect');
12
	$smarty->assign('template', 'edit.tpl');
13
	$smarty->display('main.tpl');
14
} else {
15
	get_error(); //invalid redirect, parameter error!
16
}
17
18
?>

+ 94 - 0
php/admin/include.php

@ -0,0 +1,94 @@
1
<?php
2
include('../include.php');
3
4
include("Smarty.class.php");
5
$smarty = new Smarty;
6
$smarty->template_dir = "../../templates";
7
$smarty->compile_dir = "../../templates_c";
8
9
check_login();
10
11
function check_login() {
12
	global $dbconnect;
13
	if (isset($_POST['username']) && isset($_POST['password'])) {
14
		if (!preg_match('#^\w+$#', $_POST['username'])) {
15
			die("Invalid username");
16
		}
17
		$_SESSION['username'] = $_POST['username'];
18
		$_SESSION['password'] = md5($_POST['password']);
19
	}
20
	if(isset($_SESSION['username']) && isset($_SESSION['password'])) {
21
		$res = $dbconnect->query(
22
			"SELECT id FROM users
23
				WHERE username = '".$_SESSION['username']
24
				. "' AND password = '".$_SESSION['password']."'"
25
		);
26
		if ($res->numRows() == 0) {
27
			login_page();
28
		}
29
	} else {
30
		login_page();
31
	}
32
}
33
34
function login_page() {
35
	global $smarty;
36
37
	$smarty->assign('pagetitle','Login');
38
	$smarty->assign('template','login.tpl');
39
	$smarty->display('main.tpl');
40
	die();
41
}
42
43
function limit() {
44
	global $_CONF; global $smarty;
45
	if (!isset($_GET['page'])) {
46
		$_GET['page'] = 1;
47
	}
48
	$smarty->assign('current_page', $_GET['page']);
49
50
	if ($_CONF['range'] > 0) {
51
		if ($_CONF['db_type'] == 'mysql') {
52
			$limit = 'LIMIT '
53
				. (($_GET['page'] * $_CONF['range']) - $_CONF['range'])
54
				. ', '
55
				. $_CONF['range'];
56
		} elseif ($_CONF['db_type'] == 'psql') {
57
			$limit = 'OFFSET '
58
				. (($_GET['page'] * $_CONF['range']) - $_CONF['range'])
59
				. ' LIMIT '
60
				. $_CONF['range'];
61
		} else {
62
			$limit = '';
63
		}
64
	} else {
65
		$limit = '';
66
	}
67
68
	return $limit;
69
}
70
71
function pages($sql) {
72
	global $_CONF; global $smarty;
73
	global $dbconnect;
74
75
	if (!($_CONF['range'] > 0 ) ) {
76
		return;
77
	}
78
79
	$pages = array();
80
	$count = $dbconnect->query($sql)->numRows();
81
	if ($count > $_CONF['range'] ) {
82
		for ($i = 1; $i <= ceil($count/$_CONF['range']); $i++ ){
83
			array_push($pages, $i);
84
		}
85
	}
86
	$smarty->assign('pages', $pages);
87
	$smarty->assign('pagecount', $pages[count($pages)-1]);
88
}
89
90
function get_error() {
91
	die( 'There is an error on the parameters' );
92
}
93
94
?>

+ 11 - 0
php/admin/index.php

@ -0,0 +1,11 @@
1
<?php
2
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
3
	include('save.php');
4
}
5
if (isset($_GET['delete']) && $_GET['delete'] == 'y') {
6
	include('delete.php');
7
}
8
9
include "list.php";
10
11
?>

+ 17 - 0
php/admin/list.php

@ -0,0 +1,17 @@
1
<?php
2
if(!function_exists('check_login')) { include("include.php"); }
3
4
$smarty->assign("forwardlist",
5
		sql_query("SELECT id, hostname, forward, flags&1 active
6
			FROM redirects
7
			ORDER BY hostname ".
8
			limit()
9
		)
10
	);
11
pages("SELECT id FROM redirects");
12
13
$smarty->assign('pagetitle', 'List');
14
$smarty->assign('template', 'list.tpl');
15
$smarty->assign('pageroot', './index.php');
16
$smarty->display('main.tpl');
17
?>

+ 9 - 0
php/admin/new.php

@ -0,0 +1,9 @@
1
<?php
2
if (!function_exists('check_login')) { include("include.php"); }
3
4
$smarty->assign('forward', array('forward'=>'http://'));
5
$smarty->assign('pagetitle', 'New Redirect');
6
$smarty->assign('template', 'edit.tpl');
7
$smarty->display('main.tpl');
8
9
?>

+ 27 - 0
php/admin/save.php

@ -0,0 +1,27 @@
1
<?php
2
if (!function_exists('check_login')) { include("include.php"); }
3
4
$fwdid = $_POST['id'];
5
6
$host = mysql_real_escape_string( $_POST['hostname'] );
7
$fwd	= mysql_real_escape_string( $_POST['forward'] );
8
$active = $_POST['active'] ? 1 : 0;
9
10
if (!$host || !$fwd) { get_error(); }
11
12
if ($fwdid) {
13
	$query = "UPDATE redirects
14
			SET hostname='$host',
15
					forward='$fwd',
16
					flags=flags-(flags&1)+$active
17
			WHERE id=$fwdid";
18
} else {
19
	$query = "INSERT INTO redirects
20
			SET hostname='$host',
21
					forward='$fwd',
22
					flags=$active";
23
}
24
$res = $dbconnect->query($query);
25
is_error($res);
26
27
?>

+ 5 - 0
php/config.php

@ -1,5 +1,7 @@
1 1
<?php
2
2 3
// Include paths.
4
$_CONF['smarty_path']   = "/usr/share/php/smarty/libs";
3 5
$_CONF['peardb_path']   = "/usr/share/php";
4 6
5 7
// Database DSN.
@ -9,5 +11,8 @@ $_CONF['db_pass']       = "redirect";
9 11
$_CONF['db_host']       = "localhost";
10 12
$_CONF['db_db']         = "webforward";
11 13
14
$_CONF['default_forward'] = 'http://www.yahoo.com/';
15
16
$_CONF['range']					= 50;
12 17
13 18
?>

+ 14 - 4
php/include.php

@ -2,7 +2,10 @@
2 2
session_start();
3 3
4 4
include("config.php");
5
set_include_path(get_include_path().':'.$_CONF['peardb_path']);
5
set_include_path(get_include_path()
6
	.':'.$_CONF['peardb_path']
7
	.':'.$_CONF['smarty_path']
8
);
6 9
7 10
include('DB.php');
8 11
$dsn = $_CONF['db_type'] . '://' . $_CONF['db_user'] . ':'
@ -15,6 +18,8 @@ if (DB::isError($dbconnect)) {
15 18
}
16 19
17 20
function get_forward( $host ) {
21
	global $_CONF;
22
18 23
	$host = mysql_real_escape_string( $host );
19 24
20 25
	$res = sql_query(
@ -24,15 +29,20 @@ function get_forward( $host ) {
24 29
				AND flags&1"
25 30
	);
26 31
27
	$fwd = @$res[0]['forward']
28
		or $fwd = @$_CONF['default_forward']
29
		or $fwd = 'http://www.google.com/';
32
	$fwd = $res[0]['forward'];
33
	if (!$fwd) {
34
		$fwd = $_CONF['default_forward'];
35
	}
36
	if (!$fwd) {
37
		$fwd = 'http://www.google.com/';
38
	}
30 39
31 40
	return $fwd;
32 41
}
33 42
34 43
function sql_query($query) {
35 44
	global $dbconnect;
45
36 46
	$return_array = array();
37 47
	$res = $dbconnect->query($query);
38 48
	is_error($res);

+ 1 - 1
php/index.php

@ -1,5 +1,5 @@
1 1
<?php
2
if(!function_exists("is_admin")) { include("include.php"); }
2
if(!function_exists("get_forward")) { include("include.php"); }
3 3
4 4
$fwd = get_forward( $_SERVER['HTTP_HOST'] );
5 5

+ 51 - 0
php/style.css

@ -0,0 +1,51 @@
1
body {
2
	background-color: #dfeffc;
3
}
4
a {
5
	color: #2e6e9e;
6
	text-decoration: none;
7
}
8
a:hover, a:active {
9
	text-decoration: underline;
10
}
11
h1 {
12
	font-size: 1.3em;
13
	color: #e17009;
14
}
15
#wrapper {
16
	text-align: center;
17
}
18
#wrapper table {
19
	margin: auto;
20
	text-align:left;
21
	border: 1px solid #c5dbec;
22
	background-color: #fbec88;
23
	border-collapse:collapse;
24
	padding:2px;
25
}
26
27
#wrapper tr.odd {
28
	background-color: #d0e5f5;
29
	border: 1px solid #79b7e7;
30
	color: #1d5987;
31
}
32
#wrapper tr.odd a {
33
	color: #1d5987;
34
}
35
#wrapper tr.even {
36
	background-color: #f5f8f9;
37
	border: 1px solid #79b7e7;
38
	color: #2e6e9e;
39
}
40
#wrapper tr.even a {
41
	color: #2e6e9e;
42
}
43
44
#wrapper td {
45
	margin:0;
46
	padding: 2px 5px 2px 5px;
47
}
48
#footer {
49
	text-align: center;
50
	border-top: 1px solid #e17009;
51
}

+ 16 - 0
templates/edit.tpl

@ -0,0 +1,16 @@
1
<div id="edit">
2
	<form name="editfwd" method="post" action="./">
3
		<input type="hidden" name="id" value="{$forward.id}" />
4
		<table>
5
			<tr><th>Hostname:</th><td><input name="hostname" value="{$forward.hostname}"></td></tr>
6
			<tr><th>Forward:</th><td><input name="forward" value="{$forward.forward}"></td></tr>
7
			<tr><th>Active:</th><td>
8
				<select name="active">
9
					<option value="0" {if $forward.active == 0}selected="selected"{/if}>No</option>
10
					<option value="1" {if $forward.active == 1}selected="selected"{/if}>Yes</option>
11
				</select>
12
			</td></tr>
13
			<tr><th colspan="2"><input type="submit" Value="Save" /></th></tr>
14
		</table>
15
	</form>
16
</div>

+ 8 - 0
templates/footer.tpl

@ -0,0 +1,8 @@
1
2
		</div>
3
	</div>
4
	<div id="footer">
5
		Powered by <b>WebForward</b>
6
	</div>
7
</body>
8
</html>

+ 11 - 0
templates/header.tpl

@ -0,0 +1,11 @@
1
<!DOCTYPE HTML>
2
<html>
3
<head>
4
	<title>webforward - {$pagetitle}</title>
5
	<link rel="stylesheet" type="text/css" href="/style.css" />
6
</head>
7
<body>
8
	<div id="wrapper">
9
		<h1>WebForward</h1>
10
		<div id="content">
11

+ 29 - 0
templates/list.tpl

@ -0,0 +1,29 @@
1
<div id="list">
2
3
<a href="./new.php">New Forward</a>
4
{include file=pages.tpl}
5
<table class="data">
6
	<tr><th>Hostname</th><th>Forward to</th>
7
		<th>Edit</th><th>Delete</th>
8
	</tr>
9
{section name=i loop=$forwardlist}
10
	<tr class="{cycle values="odd,even"}">
11
		<td><a class="class" href="./edit.php?id={$forwardlist[i].id}">
12
			{$forwardlist[i].hostname}
13
		</a></td>
14
		<td><a href="{$forwardlist[i].forward}">
15
			{$forwardlist[i].forward}
16
		</a></td>
17
		<td><a class="class" href="./edit.php?id={$forwardlist[i].id}">
18
			Edit
19
		</a></td>
20
		<td><a class="class" href="./?id={$forwardlist[i].id}&delete=y">
21
			Delete
22
		</a></td>
23
	</tr>
24
{/section}
25
</table>
26
{include file=pages.tpl}
27
<a href="./new.php">New Forward</a>
28
29
</div>

+ 9 - 0
templates/login.tpl

@ -0,0 +1,9 @@
1
<div id="login">
2
	<form method="post" name="login">
3
		<table align="center">
4
			<tr><th>Username:</th><td><input type="text" name="username"></td></tr>
5
			<tr><th>Password:</th><td><input type="password" name="password"></td></tr>
6
			<tr><th colspan="2"><input type="submit" value="Login"></th></tr>
7
		</table>
8
	</form>
9
</div>

+ 5 - 0
templates/main.tpl

@ -0,0 +1,5 @@
1
{include file=header.tpl}
2
3
{include file=$template}
4
5
{include file=footer.tpl}

+ 19 - 0
templates/pages.tpl

@ -0,0 +1,19 @@
1
{if $pages}
2
<div class="paging">
3
{if $current_page != 1}
4
	<a class="class" href="{$page_root}page=1">&lt;&lt;First</a>
5
	<a class="class" href="{$page_root}page={math equation="$current_page - 1"}">&lt;Previous</a>
6
{/if}
7
{foreach name=pages from=$pages item=page}
8
	{if $current_page == $page}
9
		{$page}
10
	{else}
11
		<a class="class" href="{$page_root}page={$page}">{$page}</a>
12
	{/if}
13
{/foreach}
14
{if $current_page != $pagecount}
15
	<a class="class" href="{$page_root}page={math equation="$current_page + 1"}">Next&gt;</a>
16
	<a class="class" href="{$page_root}page={$pagecount}">Last&gt;&gt;</a>
17
{/if}
18
</div>
19
{/if}

+ 3 - 0
templates_c/readme

@ -0,0 +1,3 @@
1
this file is here for git. you can delete it.
2
3
themage

+ 9 - 0
webforward.mysql

@ -5,3 +5,12 @@ CREATE TABLE redirects (
5 5
	updated timestamp,
6 6
	active int
7 7
);
8
9
CREATE TABLE users (
10
	id int auto_increment primary key,
11
	username varchar(50),
12
	password varchar(50)
13
);
14
15
INSERT INTO users VALUES (1, 'admin', MD5('redirectamos'));
16