Skip to content

Commit 7996102

Browse files
committed
proxy implemented.
1 parent a7c75e1 commit 7996102

26 files changed

+738
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
include_once("Proxy.php");
10+
class Client
11+
{
12+
private $proxy;
13+
private $un;
14+
private $pw;
15+
public function __construct()
16+
{
17+
$this->tableMaster="proxy_log";
18+
$this->hookup=UniversalConnect::doConnect();
19+
$this->un=$this->hookup->real_escape_string(trim($_POST['uname']));
20+
$this->pw=$this->hookup->real_escape_string(trim($_POST['pw']));
21+
22+
$this->getIface($this->proxy=new Proxy());
23+
}
24+
private function getIface(ISubject $proxy)
25+
{
26+
$proxy->login($this->un,$this->pw);
27+
}
28+
}
29+
$worker=new Client();
30+
?>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
include_once('UniversalConnect.php');
10+
class ConnectClient
11+
{
12+
private $hookup;
13+
public function __construct()
14+
{
15+
//One line for entire connection operation
16+
$this->hookup=UniversalConnect::doConnect();
17+
}
18+
}
19+
$worker=new ConnectClient();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
include_once('UniversalConnect.php');
10+
class CreateTable
11+
{
12+
private $tableMaster;
13+
private $hookup;
14+
public function __construct()
15+
{
16+
$this->tableMaster="proxyLog";
17+
$this->hookup=UniversalConnect::doConnect();
18+
$drop = "DROP TABLE IF EXISTS $this->tableMaster";
19+
if($this->hookup->query($drop) === true)
20+
{
21+
printf("Old table %s has been dropped.<br/>",$this->tableMaster);
22+
}
23+
echo $sql = "CREATE TABLE $this->tableMaster (uname NVARCHAR(15),
24+
pw NVARCHAR(120)"; die;
25+
print_r($this->hookup->query($sql)); die;
26+
if($this->hookup->query($sql) === true)
27+
{
28+
echo "Table $this->tableMaster has been created successfully.<br/>";
29+
}
30+
$this->hookup->close();
31+
}
32+
}
33+
$worker=new CreateTable();
34+
35+
36+
/*
37+
* use proxy_practise;
38+
CREATE TABLE IF NOT EXISTS `proxy_log` (
39+
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT,
40+
`uname` varchar(11) NOT NULL,
41+
`pw` varchar(11) NOT NULL,
42+
PRIMARY KEY (`id`)
43+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=1;
44+
*/
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
include_once('UniversalConnect.php');
10+
class HashRegister
11+
{
12+
public function __construct()
13+
{
14+
$this->tableMaster="proxy_log";
15+
$this->hookup=UniversalConnect::doConnect();
16+
$username=$this->hookup->real_escape_string(trim($_POST['uname']));
17+
$pwNow=$this->hookup->real_escape_string(trim($_POST['pw']));
18+
$sql = "INSERT INTO $this->tableMaster (uname,pw) VALUES ('$username',
19+
md5('$pwNow'))";
20+
if($this->hookup->query($sql))
21+
{
22+
echo "Registration completed:";
23+
}
24+
elseif ( ($result = $this->hookup->query($sql))===false )
25+
{
26+
printf("Invalid query: %s <br/> Whole query: %s <br/>",
27+
$this->hookup->error, $sql);
28+
exit();
29+
}
30+
$this->hookup->close();
31+
}
32+
}
33+
$worker=new HashRegister();
34+
?>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
interface IConnectInfo
10+
{
11+
const HOST ="localhost";
12+
const UNAME ="root";
13+
const PW ="anit";
14+
const DBNAME = "proxy_practise";
15+
16+
public function doConnect();
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
interface ISubject
10+
{
11+
function request();
12+
}
13+
?>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
include_once("ISubject.php");
10+
include_once('RealSubject.php');
11+
include_once('UniversalConnect.php');
12+
13+
class Proxy implements ISubject
14+
{
15+
private $tableMaster;
16+
private $hookup;
17+
private $logGood;
18+
private $realSubject;
19+
20+
public function login($uNow,$pNow)
21+
{
22+
//Filtered from Client; hash the password
23+
$uname=$uNow;
24+
$pw=md5($pNow);
25+
$this->logGood=false;
26+
//Choose table and connect
27+
$this->tableMaster="proxy_log";
28+
$this->hookup=UniversalConnect::doConnect();
29+
//Create MySQL statement
30+
$sql = "SELECT pw FROM $this->tableMaster WHERE uname='$uname'";
31+
if($result=$this->hookup->query($sql))
32+
{
33+
$row=$result->fetch_array(MYSQLI_ASSOC);
34+
35+
if($row['pw']== substr($pw, 0, 11))
36+
{
37+
$this->logGood=true;
38+
}
39+
$result->close();
40+
}
41+
elseif ( ($result = $this->hookup->query($sql))===false )
42+
{
43+
printf("Failed: %s <br/>", $this->hookup->error);
44+
exit();
45+
}
46+
$this->hookup->close();
47+
if($this->logGood)
48+
{
49+
$this->request();
50+
}
51+
else{
52+
echo "Username and/or Password not on record.";
53+
}
54+
}
55+
public function request()
56+
{
57+
$this->realSubject=new RealSubject();
58+
$this->realSubject->request();
59+
}
60+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
include_once('ISubject.php');
10+
class RealSubject implements ISubject
11+
{
12+
public function request()
13+
{
14+
$practice=<<<REQUEST
15+
<!DOCTYPE html>
16+
<html>
17+
<head>
18+
<meta charset="UTF-8">
19+
<link rel='stylesheet' type='text/css' href='proxy.css' />
20+
</head>
21+
<body>
22+
<header>PHP Tip Sheet:<br>
23+
<span class='subhead'>For OOP Developers</span></header>
24+
<ol>
25+
<li>Program to the interface and not the implementation.</li>
26+
<li>Encapsulate your objects.</li>
27+
<li>Favor composition over class inheritance.</li>
28+
<li>A class should only have a single responsibility.</li>
29+
</ol>
30+
</body>
31+
</html>
32+
REQUEST;
33+
echo $practice;
34+
}
35+
}
36+
?>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
include_once('IConnectInfo.php');
10+
11+
class UniversalConnect implements IConnectInfo {
12+
13+
private static $server=IConnectInfo::HOST;
14+
private static $currentDB= IConnectInfo::DBNAME;
15+
private static $user= IConnectInfo::UNAME;
16+
private static $pass= IConnectInfo::PW;
17+
private static $hookup;
18+
19+
public function doConnect() {
20+
self::$hookup=mysqli_connect(self::$server, self::$user, self::$pass,
21+
self::$currentDB);
22+
23+
if(self::$hookup)
24+
{
25+
//Remove slashes in following line for debugging
26+
//echo "Successful connection to MySQL:";
27+
}
28+
elseif (mysqli_connect_error(self::$hookup))
29+
{
30+
echo('Here is why it failed: ' . mysqli_connect_error());
31+
}
32+
return self::$hookup;
33+
}
34+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Old table proxyLog has been dropped.<br/>CREATE TABLE proxyLog (uname NVARCHAR(15),
2+
pw NVARCHAR(120)

0 commit comments

Comments
 (0)