From eadc19a6d8b5396b010d8ca26adf7bf0450631f8 Mon Sep 17 00:00:00 2001 From: CupIvan Date: Wed, 9 May 2018 17:26:46 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BC=D0=B5=D1=80=20=D1=80?= =?UTF-8?q?=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BC?= =?UTF-8?q?=D0=B5=D1=81=D1=81=D0=B5=D0=BD=D0=B4=D0=B6=D0=B5=D1=80=D0=B0=20?= =?UTF-8?q?=D0=BD=D0=B0=20PHP?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- php/.gitignore | 2 ++ php/config.php | 4 +++ php/index.php | 5 +++ php/init.php | 10 ++++++ php/m/api.php | 23 ++++++++++++ php/m/db.php | 63 ++++++++++++++++++++++++++++++++ php/m/functions.php | 13 +++++++ php/m/send.php | 14 ++++++++ php/post.php | 80 +++++++++++++++++++++++++++++++++++++++++ php/server.conf.example | 1 + php/server.sh | 4 +++ php/tpl/chat.tpl | 10 ++++++ php/tpl/contact.tpl | 20 +++++++++++ php/tpl/contacts.tpl | 29 +++++++++++++++ php/tpl/dns.tpl | 16 +++++++++ php/tpl/dns_edit.tpl | 19 ++++++++++ php/tpl/index.tpl | 21 +++++++++++ php/tpl/messages.tpl | 18 ++++++++++ 18 files changed, 352 insertions(+) create mode 100644 php/.gitignore create mode 100644 php/config.php create mode 100644 php/index.php create mode 100644 php/init.php create mode 100644 php/m/api.php create mode 100644 php/m/db.php create mode 100644 php/m/functions.php create mode 100644 php/m/send.php create mode 100644 php/post.php create mode 100644 php/server.conf.example create mode 100755 php/server.sh create mode 100644 php/tpl/chat.tpl create mode 100644 php/tpl/contact.tpl create mode 100644 php/tpl/contacts.tpl create mode 100644 php/tpl/dns.tpl create mode 100644 php/tpl/dns_edit.tpl create mode 100644 php/tpl/index.tpl create mode 100644 php/tpl/messages.tpl diff --git a/php/.gitignore b/php/.gitignore new file mode 100644 index 0000000..3797243 --- /dev/null +++ b/php/.gitignore @@ -0,0 +1,2 @@ +db +server.conf diff --git a/php/config.php b/php/config.php new file mode 100644 index 0000000..b2a7257 --- /dev/null +++ b/php/config.php @@ -0,0 +1,4 @@ + $v) if (is_string($v)) $m[$k] = urldecode($v); + if (is_callable($handler)) $handler($m); + else require_once "tpl/$handler"; + } +} diff --git a/php/m/db.php b/php/m/db.php new file mode 100644 index 0000000..e0ef54f --- /dev/null +++ b/php/m/db.php @@ -0,0 +1,63 @@ + $_) + $data[$id] = $a + $data[$id]; + db_save($db, $data); +} + +function db_delete($db, $filter) +{ + $data = db_get($db); + foreach (db_search($db, $filter) as $id => $_) + unset($data[$id]); + db_save($db, $data); +} + +function db_search($db, $filter = []) +{ + $res = []; + foreach (db_get($db) as $id => $a) + { + foreach ($filter as $k => $v) + if ($a[$k] != $v) continue 2; + $res[$id] = $a; + } + return $res; +} + +function db_search_one($db, $filter) +{ + $res = db_search($db, $filter); + foreach ($res as $a) return $a; + return []; +} + +function db_get($db) +{ + $fname = "./db/$db.json"; + if (!file_exists($fname)) return []; + $db = @json_decode(@file_get_contents($fname), true); + return $db ?: []; +} + +function db_save($db, $data) +{ + $fname = "./db/$db.json"; + if (empty($data)) unlink($fname); + else + { + $st = json_encode($data, JSON_UNESCAPED_UNICODE); + if (!file_exists($x = dirname($fname))) mkdir($x, 0777, true); + if ($st) file_put_contents($fname, $st); + } +} diff --git a/php/m/functions.php b/php/m/functions.php new file mode 100644 index 0000000..705ea44 --- /dev/null +++ b/php/m/functions.php @@ -0,0 +1,13 @@ + ['method'=>'POST','header'=>"Connection: close\r\nContent-Length: $data_len\r\n", 'content'=>$data_url] + ]); + + $res = json_decode(@file_get_contents('http://'.$data['to'].'/messages/', false, $context), true); + return !empty($res); +} diff --git a/php/post.php b/php/post.php new file mode 100644 index 0000000..065ab23 --- /dev/null +++ b/php/post.php @@ -0,0 +1,80 @@ +time(), 'addr'=>$_SERVER['HTTP_HOST']]; + echo json_encode($a); + exit; +}); + +api_get('/contacts/ping', function(){ + foreach (db_search('contacts') as $a) + if ($a['addr'] != $_SERVER['HTTP_HOST']) // COMMENT: сами себе не отправляем + { + if (time() - @$a['time_ping'] > TIME_PING) + { + $st = @file_get_contents('http://'.$a['addr'].'/ping'); + $json = json_decode($st, true); + $x = $json ? ['time_online' => time()] : []; + db_update('contacts', ['time_ping' => time()] + $x, ['addr'=>$a['addr']]); + } + // если на связи - пытаемся дотправить сообщения + if (time() - $a['time_online'] < TIME_ONLINE) + { + $addr = $a['addr']; + foreach (db_search("undelivered/$addr") as $a) + if (send($a)) + { + db_insert("messages/$addr", $a + ['received'=>time()]); + db_delete("undelivered/$addr", ['timestamp'=>$a['timestamp']]); + } else break; + } + if (!empty($data)) $a['messages'] = $data; + } + exit; +}); + +api_get('/messages/(?[^/]+)', 'messages.tpl'); + +if ($_SERVER['REQUEST_METHOD'] != 'POST') return; + +api_post('/contacts/(?[^/]+)', function($m){ + if (empty($_REQUEST['addr'])) die('Не указан адрес!'); + $addr = $m['addr']; + $x = db_search('contacts', $addr == 'new' ? request('addr') : ['addr'=>$addr]); + if ($addr == 'new') + { + if (!empty($x)) die('Такой адрес уже есть в базе!'); + db_insert('contacts', request('addr,nickname,email')); + redirect('/contacts/'); + } else { + if (request('delete')) + { + db_delete('contacts', ['addr'=>$addr]); + redirect('/contacts/'); + } + else + db_update('contacts', request('addr,nickname,email'), ['addr'=>$addr]); + } +}); + +api_post('/messages/(?[^/]+)', function($m){ + $a = request('text') + ['from'=>$_SERVER['HTTP_HOST'], 'to'=>$m['addr'], 'timestamp'=>time()]; + if (send($a)) + db_insert('messages/'.$m['addr'], $a + ['received'=>time()]); + else + db_insert('undelivered/'.$m['addr'], $a); + redirect($_SERVER['HTTP_REFERER']); +}); + +api_post('/messages/', function($m){ + $a = request('from,to,text,timestamp'); + if ($a['to'] == $_SERVER['HTTP_HOST']) + { + db_insert('messages/'.$a['from'], $a + ['received'=>time()]); + $res = ['received'=>time()]; + echo json_encode($res); + } + exit; +}); + +redirect(); diff --git a/php/server.conf.example b/php/server.conf.example new file mode 100644 index 0000000..2bcad11 --- /dev/null +++ b/php/server.conf.example @@ -0,0 +1 @@ +IP=127.0.0.1 diff --git a/php/server.sh b/php/server.sh new file mode 100755 index 0000000..e7d8326 --- /dev/null +++ b/php/server.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +source ./server.conf +php -S $IP:8000 index.php diff --git a/php/tpl/chat.tpl b/php/tpl/chat.tpl new file mode 100644 index 0000000..4e75b88 --- /dev/null +++ b/php/tpl/chat.tpl @@ -0,0 +1,10 @@ +

Сообщения

+ + + +
+ + +
+ + diff --git a/php/tpl/contact.tpl b/php/tpl/contact.tpl new file mode 100644 index 0000000..832572c --- /dev/null +++ b/php/tpl/contact.tpl @@ -0,0 +1,20 @@ +$m['addr']]); + } +?> + + +

контакт

+
+ Адрес: + Ник: + email: + +
diff --git a/php/tpl/contacts.tpl b/php/tpl/contacts.tpl new file mode 100644 index 0000000..e9ab65c --- /dev/null +++ b/php/tpl/contacts.tpl @@ -0,0 +1,29 @@ +

Контакты

+ + + +Добавить + + + + + + + + + + + + + + + + + + +
АдресНикemailonline
TIME_ONLINE)?'нет':'да'?>сообщения
diff --git a/php/tpl/dns.tpl b/php/tpl/dns.tpl new file mode 100644 index 0000000..25d618d --- /dev/null +++ b/php/tpl/dns.tpl @@ -0,0 +1,16 @@ +

DNS записи

+ +Добавить + + + + + + + + + + + + +
АдресДомен
diff --git a/php/tpl/dns_edit.tpl b/php/tpl/dns_edit.tpl new file mode 100644 index 0000000..04ea92d --- /dev/null +++ b/php/tpl/dns_edit.tpl @@ -0,0 +1,19 @@ +$m['addr']]); + } +?> + + +

запись

+
+ Адрес: + Домен: + +
diff --git a/php/tpl/index.tpl b/php/tpl/index.tpl new file mode 100644 index 0000000..da6e3da --- /dev/null +++ b/php/tpl/index.tpl @@ -0,0 +1,21 @@ + + + + + + +

Ваш IP:

+ + +[^/]+)', 'contact.tpl'); + api_get('/contacts/(?[^/]+)/messages', 'chat.tpl'); +?> + + + + + diff --git a/php/tpl/messages.tpl b/php/tpl/messages.tpl new file mode 100644 index 0000000..bfc61f8 --- /dev/null +++ b/php/tpl/messages.tpl @@ -0,0 +1,18 @@ + + + + +

Список сообщений пуст

+ + : +(не доставлено)';?> +
+ + +