Robin Thoni 10 лет назад
Сommit
ebf0b530bc
10 измененных файлов: 344 добавлений и 0 удалений
  1. 1
    0
      .gitignore
  2. 14
    0
      .htaccess
  3. 27
    0
      gates.php
  4. 0
    0
      index.php
  5. 54
    0
      messages.php
  6. 29
    0
      received.php
  7. 48
    0
      register.php
  8. 104
    0
      send.php
  9. 51
    0
      sent.php
  10. 16
    0
      sql.php

+ 1
- 0
.gitignore Просмотреть файл

@@ -0,0 +1 @@
1
+.htpasswd

+ 14
- 0
.htaccess Просмотреть файл

@@ -0,0 +1,14 @@
1
+AuthUserFile /var/sms/.htpasswd
2
+AuthType Basic
3
+AuthName "SMS Service"
4
+
5
+Require valid-user
6
+<Files register.php>
7
+	Require user __gates__
8
+</Files>
9
+<Files sent.php>
10
+	Require user __gates__
11
+</Files>
12
+<Files received.php>
13
+	Require user __gates__
14
+</Files>

+ 27
- 0
gates.php Просмотреть файл

@@ -0,0 +1,27 @@
1
+<?php
2
+require_once "sql.php";
3
+
4
+$where = "";
5
+$array = array();
6
+if(isset($_GET['id']))
7
+{
8
+	$id = intval($_GET['id']);
9
+	if($id <= 0)
10
+		error("Invalid id");
11
+
12
+	$where = "WHERE id=:id";
13
+	$array = array(":id" => $id);
14
+}
15
+
16
+$select = $db->prepare("SELECT * FROM gates ".$where);
17
+if(!$select->execute($array))
18
+	error("Unable to read gates");
19
+$gates = array();
20
+while($gate = $select->fetch())
21
+	$gates[] = array("id" => intval($gate['id']), "name" => $gate['name'],
22
+		"number" => $gate['number'], "address" => $gate['address'],
23
+		"port" => intval($gate['port']), "lastSeen" =>
24
+		strtotime($gate['lastSeen']));
25
+
26
+echo json_encode(array("timestamp" => time(), "gateTimeout" => $gateTimeout, "gates" => $gates));
27
+?>


+ 54
- 0
messages.php Просмотреть файл

@@ -0,0 +1,54 @@
1
+<?php
2
+require_once "sql.php";
3
+
4
+$where = "";
5
+$array = array();
6
+
7
+if(isset($_GET['ids']))
8
+{
9
+	$ids = explode(",", $_GET['ids']);
10
+	$where = "WHERE id=0";
11
+	foreach($ids as $id)
12
+	{
13
+		$id = trim($id);
14
+		if($id != "")
15
+		{
16
+			$id = intval($id);
17
+			if($id <= 0)
18
+				error("Invalid ids");
19
+			$array[] = $id;
20
+			$where = $where." OR id=?";
21
+		}
22
+	}
23
+	if(count($array) == 0)
24
+		$where = "";
25
+}
26
+else if(isset($_GET['lastMessages']))
27
+{
28
+	$l = intval($_GET['lastMessages']);
29
+	if($l <= 0)
30
+		error("Invalid last messages");
31
+	$where = "ORDER BY id DESC LIMIT $l OFFSET 0";
32
+}
33
+else if(isset($_GET['lastMessagesTime']))
34
+{
35
+	$l = intval($_GET['lastMessagesTime']);
36
+	if($l <= 0)
37
+		error("Invalid last time messages");
38
+	$where = "WHERE time>=:time";
39
+	$array = array(":time" => date("Y-m-d H:i:s", time() - $l));
40
+}
41
+
42
+$select = $db->prepare("SELECT * FROM sent ".$where);
43
+
44
+if(!$select->execute($array))
45
+	error("Unable to read messages");
46
+$messages = array();
47
+while($msg = $select->fetch())
48
+	$messages[] = array("id" => intval($msg['id']), "recipient" => $msg['recipient'],
49
+		"text" => $msg['text'], "time" => strtotime($msg['time']),
50
+		"status" => intval($msg['status']), "gate" => intval($msg['gate']),
51
+		"timeStatus" => $msg['timeStatus'] ? strtotime($msg['timeStatus']) : 0);
52
+
53
+echo json_encode(array("timestamp" => time(), "messages" => $messages));
54
+?>

+ 29
- 0
received.php Просмотреть файл

@@ -0,0 +1,29 @@
1
+<?php
2
+require_once "sql.php";
3
+if(isset($_GET['sender']))
4
+	$sender = $_GET['sender'];
5
+else
6
+	$sender = "";
7
+if(isset($_GET['text']))
8
+	$text = $_GET['text'];
9
+else
10
+	$text = "";
11
+
12
+if($sender == "")
13
+	error("Empty sender");
14
+
15
+$select = $db->prepare("SELECT id FROM gates WHERE address=:address");
16
+if(!$select->execute(array(":address" => $_SERVER['REMOTE_ADDR'])))
17
+	error("Unable to retrieve gate details");
18
+
19
+if($g = $select->fetch())
20
+{
21
+	$insert = $db->prepare("INSERT INTO `received` (`sender`, `text`, `gate`) VALUES (:sender, :text, :gate)");
22
+	if(!$insert->execute(array(":sender" => $sender, ":text" => $text, ":gate" => $g['id'])))
23
+		error("Failed to update database: ".$insert->errorInfo()[2]);
24
+	else
25
+		echo json_encode(array("id" => $db->lastInsertId()));
26
+}
27
+else
28
+	error("You're not allowed to do this");
29
+?>

+ 48
- 0
register.php Просмотреть файл

@@ -0,0 +1,48 @@
1
+<?php
2
+require_once "sql.php";
3
+if(isset($_GET['name']))
4
+	$name = $_GET['name'];
5
+else
6
+	$name = "";
7
+if(isset($_GET['number']))
8
+	$number = $_GET['number'];
9
+else
10
+	$number = "";
11
+if(isset($_GET['port']))
12
+	$port = $_GET['port'];
13
+else
14
+	$port = "";
15
+
16
+if($name == "")
17
+	error("Empty name");
18
+if($number == "")
19
+	error("Empty number");
20
+if($port == "")
21
+	error("Empty port");
22
+$port = intval($port);
23
+if($port < 1 || $port > 65535)
24
+	error("Invalid port (1 <= port <= 65535)");
25
+
26
+$select = $db->prepare("SELECT id FROM gates WHERE number=:number");
27
+if($select->execute(array(":number" => $number)))
28
+{
29
+	if($g = $select->fetch())
30
+	{
31
+		$update = $db->prepare("UPDATE gates SET name=:name, address=:address, port=:port, lastSeen=NULL WHERE id=:id");
32
+		if($update->execute(array(":name" => $name, ":address" => $_SERVER['REMOTE_ADDR'], ":port" => $port, ":id" => $g['id'])))
33
+			echo json_encode(array("id" => intval($g['id']), "gateTimeout" => $gateTimeout));
34
+		else
35
+			error("Failed to update database");
36
+	}
37
+	else
38
+	{
39
+		$insert = $db->prepare("INSERT INTO gates (name, number, address, port) VALUES (:name, :number, :address, :port) ON DUPLICATE KEY UPDATE lastSeen=NULL");
40
+		if($insert->execute(array(":name" => $name, ":number" => $number, ":address" => $_SERVER['REMOTE_ADDR'], ":port" => $port)))
41
+			echo json_encode(array("id" => intval($db->lastInsertId()), "gateTimeout" => $gateTimeout));
42
+		else
43
+			error("Failed to insert into database");
44
+	}
45
+}
46
+else
47
+	error("Failed to query gates");
48
+?>

+ 104
- 0
send.php Просмотреть файл

@@ -0,0 +1,104 @@
1
+<?php
2
+/*
3
+Status codes:
4
+0: sending
5
+1: sent
6
+2: failed to contact gate
7
+3: generic error
8
+4: no service
9
+5: no pdu
10
+6: radio off
11
+7: unknown error
12
+*/
13
+require_once "sql.php";
14
+if(isset($_GET['to']))
15
+	$to = $_GET['to'];
16
+else
17
+	$to = "";
18
+if(isset($_GET['gate']))
19
+	$gate = $_GET['gate'];
20
+else
21
+	$gate = "";
22
+if(isset($_GET['text']))
23
+	$text = $_GET['text'];
24
+else
25
+	$text = "";
26
+if(isset($_GET['maxWait']))
27
+	$maxWait = $_GET['maxWait'];
28
+else
29
+	$maxWait = "";
30
+
31
+if($to == "")
32
+	error("Empty recipient");
33
+if($text == "")
34
+	error("Empty text");
35
+if($gate == "")
36
+	error("No gate provided");
37
+if($maxWait == "")
38
+	error("Empty max wait");
39
+
40
+$gate = intval($gate);
41
+if($gate <= 0)
42
+	error("Invalid gate");
43
+$maxWait = intval($maxWait);
44
+if($maxWait < 0 || !is_numeric($_GET['maxWait']))
45
+	error("Invalid max wait");
46
+
47
+$select = $db->prepare("SELECT address, port, lastSeen FROM gates WHERE id=:gate");
48
+if(!$select->execute(array(":gate" => $gate)))
49
+	error("Unable to retrieve gate details");
50
+
51
+if($g = $select->fetch())
52
+{
53
+	if(intval(strtotime($g['lastSeen'])) < time() - $gateTimeout)
54
+		error("Gate is down");
55
+
56
+	$insert = $db->prepare("INSERT INTO `sent` (`recipient`, `text`, `status`, `gate`) VALUES (:recipient, :text, :status, :gate)");
57
+	if(!$insert->execute(array(":recipient" => $to, ":text" => $text, ":status" => 0, ":gate" => $gate)))
58
+		error("Failed to save message");
59
+	else
60
+	{
61
+		$id = intval($db->lastInsertId());
62
+		$out = http_get("http:/"."/".$g['address'].":".$g['port']."/send.php?to=".urlencode($to)."&text=".urlencode($text)."&id=".$id,
63
+				array("timeout" => 10, "connecttimeout" => 10));
64
+		if($out == "")
65
+		{
66
+			$update = $db->prepare("UPDATE sent SET `status`=2, `timeStatus`=NOW() WHERE `id`=:id");
67
+			if(!$update->execute(array(":id" => $id)))
68
+				error("Failed to update database after failed to contact gate (general failure)");
69
+			else
70
+				echo json_encode(array("id" => $id, "status" => 2));
71
+		}
72
+		else
73
+		{
74
+			$out = json_decode(http_parse_message($out)->body);
75
+			if(isset($out->error))
76
+			{
77
+				$update = $db->prepare("UPDATE sent SET `status`=7, `timeStatus`=NOW() WHERE `id`=:id");
78
+				if(!$update->execute(array(":id" => $id)))
79
+					error("Failed to update database after failed to contact gate (general failure)");
80
+				else
81
+					echo json_encode(array("id" => $id, "status" => 7, "error" => $out->error));
82
+			}
83
+			else
84
+			{
85
+				$status = 0;
86
+				$maxWait *= 1000;
87
+				do
88
+				{
89
+					$select = $db->prepare("SELECT status FROM sent WHERE id=:id");
90
+					if($select->execute(array(":id" => $id)))
91
+						if($f = $select->fetch())
92
+							$status = $f['status'];
93
+					$maxWait -= 100;
94
+					if($maxWait > 0)
95
+						usleep(100000);
96
+				} while($maxWait > 0 && $status == 0);
97
+				echo json_encode(array("id" => $id, "status" => $status));
98
+			}
99
+		}
100
+	}
101
+}
102
+else
103
+	error("Gate not found");
104
+?>

+ 51
- 0
sent.php Просмотреть файл

@@ -0,0 +1,51 @@
1
+<?php
2
+/*
3
+Status codes:
4
+0: sending
5
+1: sent
6
+2: failed to contact gate
7
+3: generic error
8
+4: no service
9
+5: no pdu
10
+6: radio off
11
+7: unknown error
12
+*/
13
+require_once "sql.php";
14
+if(isset($_GET['id']))
15
+	$id = $_GET['id'];
16
+else
17
+	$id = "";
18
+if(isset($_GET['status']))
19
+	$status = $_GET['status'];
20
+else
21
+	$status = "";
22
+
23
+if($id == "")
24
+	error("Empty id");
25
+if($status == "")
26
+	error("Empty status");
27
+
28
+$id = intval($id);
29
+if($id <= 0)
30
+	error("Invalid id");
31
+$status = intval($status);
32
+if($status < 1 || $status > 7)
33
+	error("Invalid status");
34
+
35
+$select = $db->prepare("SELECT timeStatus, status FROM sent WHERE id=:id AND gate=(SELECT id FROM gates WHERE address=:address)");
36
+if(!$select->execute(array(":address" => $_SERVER['REMOTE_ADDR'], ":id" => $id)))
37
+	error("Unable to retrieve gate details");
38
+
39
+if($g = $select->fetch())
40
+{
41
+	if($g['timeStatus'] || $g['status'] != 0)
42
+		error("This status has already been set: ".$g['status']);
43
+	$update = $db->prepare("UPDATE sent SET `status`=:status, `timeStatus`=NOW() WHERE `id`=:id");
44
+	if(!$update->execute(array(":id" => $id, ":status" => $status)))
45
+		error("Failed to update database");
46
+	else
47
+		echo json_encode(array("id" => $id, "status" => $status));
48
+}
49
+else
50
+	error("You're not allowed to do this");
51
+?>

+ 16
- 0
sql.php Просмотреть файл

@@ -0,0 +1,16 @@
1
+<?php
2
+function error($str)
3
+{
4
+	die(json_encode(array("error" => $str)));
5
+}
6
+
7
+try
8
+{
9
+	$db = new PDO('mysql:host=localhost;dbname=sms', "sms", "cWrzmRByJtjZ2ax4");
10
+}
11
+catch(Exception $e)
12
+{
13
+	error("Unable to connect to the database");
14
+}
15
+$gateTimeout = 5 * 60;
16
+?>

Загрузка…
Отмена
Сохранить