2018-03-05 20:22:25 +01:00
|
|
|
/*
|
|
|
|
Copyright 2018 Daniil Gentili
|
|
|
|
(https://daniil.it)
|
|
|
|
This file is part of pif-tdpony.
|
|
|
|
pif-tdpony is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
The PWRTelegram API is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along with pif-tdpony.
|
|
|
|
If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-03-03 16:09:03 +01:00
|
|
|
#include <td/telegram/td_json_client.h>
|
2018-03-07 13:45:23 +01:00
|
|
|
#include <td/telegram/td_log.h>
|
2018-01-01 17:01:56 +01:00
|
|
|
|
2018-02-15 22:33:15 +01:00
|
|
|
#include <phpcpp.h>
|
|
|
|
|
2018-01-01 17:01:56 +01:00
|
|
|
#include <string>
|
2018-03-07 13:45:23 +01:00
|
|
|
#include <queue>
|
2018-01-01 17:01:56 +01:00
|
|
|
|
2018-02-17 12:02:43 +01:00
|
|
|
class API : public Php::Base {
|
2018-01-01 17:01:56 +01:00
|
|
|
public:
|
2018-03-01 01:49:03 +01:00
|
|
|
API() = default;
|
|
|
|
virtual ~API() = default;
|
2018-03-07 13:45:23 +01:00
|
|
|
|
2018-03-03 16:09:03 +01:00
|
|
|
void __construct(Php::Parameters ¶ms) {
|
2018-03-07 13:45:23 +01:00
|
|
|
self = Php::Value(this);
|
|
|
|
if (params.size() != 1) {
|
|
|
|
throw Php::Exception("Invalid parameter count");
|
|
|
|
}
|
|
|
|
if (params[0].isString()) {
|
|
|
|
if (!Php::call("file_exists", params[0])) {
|
|
|
|
throw Php::Exception("Provided file does not exist!");
|
|
|
|
}
|
|
|
|
Php::Value unserialized = Php::call("unserialize", Php::call("file_get_contents", params[0]));
|
|
|
|
if (!unserialized.instanceOf("\\danog\\MadelineProto\\X\\TD")) {
|
|
|
|
throw Php::Exception("An error occurred during deserialization!");
|
|
|
|
}
|
|
|
|
self["API"] = unserialized;
|
|
|
|
self["session"] = params[0];
|
|
|
|
} else {
|
|
|
|
self["API"] = Php::Object("\\danog\\MadelineProto\\X\\TD", params[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void serialize(Php::Parameters ¶ms) {
|
|
|
|
Php::Value path;
|
|
|
|
if (params.size() == 1 && params[0].isString()) {
|
|
|
|
path = params[0];
|
|
|
|
} else if (self.contains("session") && self.get("session").isString()) {
|
|
|
|
path = self.get("session");
|
|
|
|
} else {
|
|
|
|
throw Php::Exception("No valid path was provided");
|
|
|
|
}
|
|
|
|
last_serialized = Php::call("time");
|
|
|
|
Php::call("file_put_contents", path, Php::call("serialize", self.get("API")));
|
|
|
|
}
|
|
|
|
void __destruct() {
|
|
|
|
if (self.contains("session") && self.get("session").isString()) {
|
|
|
|
last_serialized = Php::call("time");
|
|
|
|
Php::call("file_put_contents", self.get("session"), Php::call("serialize", self.get("API")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Php::Value __actualcall(Php::Parameters ¶ms) {
|
|
|
|
if (last_serialized < Php::call("time") - 30 && self.contains("session") && self.get("session").isString()) {
|
|
|
|
last_serialized = Php::call("time");
|
|
|
|
Php::call("file_put_contents", self.get("session"), Php::call("serialize", self.get("API")));
|
|
|
|
}
|
|
|
|
if (params[1].size() == 1) {
|
|
|
|
return self.get("API").call(params[0], params[1].get(0));
|
|
|
|
}
|
|
|
|
return self.get("API").call(params[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Php::Value self;
|
|
|
|
Php::Value last_serialized = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class TD : public Php::Base {
|
|
|
|
public:
|
|
|
|
TD() = default;
|
|
|
|
virtual ~TD() = default;
|
|
|
|
|
|
|
|
void __construct(Php::Parameters ¶ms) {
|
|
|
|
self = Php::Value(this);
|
2018-03-06 15:29:54 +01:00
|
|
|
self["tdlibParameters"]["@type"] = "setTdlibParameters";
|
|
|
|
self["tdlibParameters"]["parameters"] = params[0];
|
2018-02-17 12:02:43 +01:00
|
|
|
initTdlib();
|
|
|
|
}
|
|
|
|
void __wakeup() {
|
2018-03-07 13:45:23 +01:00
|
|
|
self = Php::Value(this);
|
2018-02-17 12:02:43 +01:00
|
|
|
initTdlib();
|
2018-03-01 01:49:03 +01:00
|
|
|
}
|
|
|
|
void __destruct() {
|
|
|
|
deinitTdlib();
|
2018-02-17 12:02:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-07 13:45:23 +01:00
|
|
|
Php::Value __sleep() {
|
|
|
|
Php::Array buffer;
|
|
|
|
while (updates.size()) {
|
|
|
|
buffer[buffer.size()] = updates.front();
|
|
|
|
updates.pop();
|
|
|
|
}
|
|
|
|
for (auto &item : buffer) {
|
|
|
|
updates.push(item.second);
|
|
|
|
}
|
|
|
|
self["updatesBuffer"] = buffer;
|
|
|
|
Php::Array result({"updatesBuffer", "tdlibParameters"});
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-02-17 12:02:43 +01:00
|
|
|
void initTdlib() {
|
2018-03-03 16:09:03 +01:00
|
|
|
client = td_json_client_create();
|
2018-03-07 13:45:23 +01:00
|
|
|
if (self["updatesBuffer"]) {
|
|
|
|
Php::Value buffer = self["updatesBuffer"];
|
|
|
|
for (auto &item : buffer) {
|
|
|
|
updates.push(item.second);
|
|
|
|
}
|
|
|
|
Php::Array empty;
|
|
|
|
self["updatesBuffer"] = empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
Php::Value update;
|
|
|
|
while (true) {
|
|
|
|
update = json_decode(td_json_client_receive(client, 0));
|
|
|
|
if (update) {
|
|
|
|
if (update.get("@type") == "updateAuthorizationState" && update.get("authorization_state").get("@type") == "authorizationStateWaitTdlibParameters") {
|
|
|
|
execute(self["tdlibParameters"]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
updates.push(update);
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-02-17 12:02:43 +01:00
|
|
|
void deinitTdlib() {
|
2018-03-03 16:09:03 +01:00
|
|
|
td_json_client_destroy(client);
|
2018-02-17 12:02:43 +01:00
|
|
|
}
|
2018-03-07 13:45:23 +01:00
|
|
|
|
|
|
|
|
2018-03-01 01:49:03 +01:00
|
|
|
void send(Php::Parameters ¶ms) {
|
|
|
|
Php::Value value = params[0];
|
|
|
|
if (value.get("@type") == "setTdlibParameters") {
|
|
|
|
self["tdlibParameters"] = value;
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-03 19:38:45 +01:00
|
|
|
td_json_client_send(client, json_encode(value));
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-07 13:45:23 +01:00
|
|
|
|
2018-03-01 01:49:03 +01:00
|
|
|
Php::Value receive(Php::Parameters ¶ms) {
|
2018-03-07 13:45:23 +01:00
|
|
|
if (updates.size()) {
|
|
|
|
Php::Value res = updates.front();
|
|
|
|
updates.pop();
|
|
|
|
return res;
|
|
|
|
}
|
2018-03-03 19:38:45 +01:00
|
|
|
return json_decode(td_json_client_receive(client, params[0]));
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-07 13:45:23 +01:00
|
|
|
|
2018-03-01 01:49:03 +01:00
|
|
|
Php::Value execute(Php::Parameters ¶ms) {
|
2018-03-07 13:45:23 +01:00
|
|
|
return execute(params[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
Php::Value execute(Php::Value value) {
|
|
|
|
if (value["@type"] && value.get("@type") == "setTdlibParameters") {
|
2018-03-01 01:49:03 +01:00
|
|
|
self["tdlibParameters"] = value;
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-07 13:45:23 +01:00
|
|
|
std::int64_t query_id = next_query_id();
|
|
|
|
value["@extra"] = query_id;
|
|
|
|
|
|
|
|
td_json_client_send(client, json_encode(value));
|
|
|
|
|
|
|
|
Php::Value update;
|
|
|
|
while (true) {
|
|
|
|
update = json_decode(td_json_client_receive(client, 0));
|
|
|
|
if (update) {
|
|
|
|
if (update["@extra"] == query_id) {
|
|
|
|
return update;
|
|
|
|
}
|
2018-01-01 17:01:56 +01:00
|
|
|
|
2018-03-07 13:45:23 +01:00
|
|
|
updates.push(update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Php::Value __call(const char *name, Php::Parameters ¶ms) {
|
|
|
|
Php::Array value;
|
|
|
|
if (params.size() == 1) {
|
|
|
|
value = params[0];
|
|
|
|
}
|
|
|
|
value["@type"] = name;
|
|
|
|
return execute(value);
|
|
|
|
}
|
2018-03-01 01:49:03 +01:00
|
|
|
private:
|
2018-03-03 16:09:03 +01:00
|
|
|
void *client;
|
2018-03-07 13:45:23 +01:00
|
|
|
std::queue<Php::Value> updates;
|
|
|
|
Php::Value self;
|
|
|
|
|
|
|
|
std::int64_t query_id = 0;
|
|
|
|
std::int64_t next_query_id() {
|
|
|
|
return ++query_id;
|
|
|
|
}
|
2018-03-03 19:38:45 +01:00
|
|
|
const char *json_encode(Php::Value value) {
|
|
|
|
return strdup(Php::call("json_encode", value));
|
|
|
|
}
|
2018-03-07 13:45:23 +01:00
|
|
|
Php::Value json_decode(const char *value) {
|
|
|
|
Php::Value result;
|
|
|
|
if (value) {
|
|
|
|
result = Php::call("json_decode", value, true);
|
|
|
|
}
|
|
|
|
return result;
|
2018-03-03 19:38:45 +01:00
|
|
|
}
|
2018-03-01 01:49:03 +01:00
|
|
|
};
|
2018-01-01 17:01:56 +01:00
|
|
|
|
|
|
|
|
2018-03-03 16:09:03 +01:00
|
|
|
class Logger : public Php::Base
|
2018-03-01 01:49:03 +01:00
|
|
|
{
|
|
|
|
public:
|
2018-03-03 16:09:03 +01:00
|
|
|
Logger() = default;
|
|
|
|
virtual ~Logger() = default;
|
2018-03-01 01:49:03 +01:00
|
|
|
|
|
|
|
static Php::Value set_file_path(Php::Parameters ¶ms) {
|
2018-03-07 13:45:23 +01:00
|
|
|
return td_set_log_file_path(params[0]);
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-01 01:49:03 +01:00
|
|
|
static void set_max_file_size(Php::Parameters ¶ms) {
|
2018-03-07 13:45:23 +01:00
|
|
|
std::int64_t size = params[0];
|
|
|
|
td_set_log_max_file_size(size);
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-01 01:49:03 +01:00
|
|
|
static void set_verbosity_level(Php::Parameters ¶ms) {
|
2018-03-07 13:45:23 +01:00
|
|
|
td_set_log_verbosity_level(params[0]);
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-02-15 22:33:15 +01:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function that is called by PHP right after the PHP process
|
|
|
|
* has started, and that returns an address of an internal PHP
|
|
|
|
* strucure with all the details and features of your extension
|
|
|
|
*
|
|
|
|
* @return void* a pointer to an address that is understood by PHP
|
|
|
|
*/
|
|
|
|
PHPCPP_EXPORT void *get_module()
|
|
|
|
{
|
|
|
|
// static(!) Php::Extension object that should stay in memory
|
|
|
|
// for the entire duration of the process (that's why it's static)
|
|
|
|
static Php::Extension extension("pif-tdpony", "1.0");
|
|
|
|
|
|
|
|
// description of the class so that PHP knows which methods are accessible
|
2018-02-17 12:02:43 +01:00
|
|
|
Php::Class<API> api("API");
|
2018-02-15 22:33:15 +01:00
|
|
|
|
2018-03-07 13:45:23 +01:00
|
|
|
api.method<&API::__construct>("__construct", Php::Public | Php::Final);
|
2018-03-01 01:49:03 +01:00
|
|
|
api.method<&API::__destruct>("__destruct", Php::Public | Php::Final, {});
|
2018-03-07 13:45:23 +01:00
|
|
|
api.method<&API::__actualcall>("__call", Php::Public | Php::Final, {Php::ByVal("method", Php::Type::String), Php::ByVal("args", Php::Type::Array)});
|
|
|
|
api.method<&API::serialize>("serialize", Php::Public | Php::Final, {Php::ByVal("path", Php::Type::String)});
|
2018-02-15 22:33:15 +01:00
|
|
|
|
2018-02-17 12:02:43 +01:00
|
|
|
api.constant("PIF_TDPONY_VERSION", "1.0");
|
2018-02-15 22:33:15 +01:00
|
|
|
|
2018-03-07 13:45:23 +01:00
|
|
|
|
|
|
|
Php::Class<TD> td("TD");
|
|
|
|
td.method<&TD::__construct>("__construct", Php::Public | Php::Final);
|
|
|
|
td.method<&TD::__destruct>("__destruct", Php::Public | Php::Final);
|
|
|
|
td.method<&TD::__wakeup>("__wakeup", Php::Public | Php::Final);
|
|
|
|
td.method<&TD::__sleep>("__sleep", Php::Public | Php::Final);
|
|
|
|
td.method<&TD::send>("send", Php::Public | Php::Final);
|
|
|
|
td.method<&TD::receive>("receive", Php::Public | Php::Final, {Php::ByVal("timeout", Php::Type::Float)});
|
|
|
|
td.method<&TD::execute>("execute", Php::Public | Php::Final);
|
|
|
|
|
|
|
|
td.property("tdlibParameters", nullptr, Php::Private);
|
|
|
|
td.property("updatesBuffer", nullptr, Php::Private);
|
|
|
|
|
|
|
|
|
2018-02-15 22:33:15 +01:00
|
|
|
Php::Namespace danog("danog");
|
|
|
|
Php::Namespace MadelineProto("MadelineProto");
|
2018-02-17 12:02:43 +01:00
|
|
|
Php::Namespace X("X");
|
2018-02-15 22:33:15 +01:00
|
|
|
|
2018-03-03 16:09:03 +01:00
|
|
|
Php::Class<Logger> logger("Logger");
|
2018-03-01 01:49:03 +01:00
|
|
|
|
2018-03-03 16:09:03 +01:00
|
|
|
logger.method<&Logger::set_file_path>("set_file_path", Php::Public, {Php::ByVal("file_path", Php::Type::String)});
|
|
|
|
logger.method<&Logger::set_max_file_size>("set_max_file_size", Php::Public, {Php::ByVal("set_max_file_size", Php::Type::Numeric)});
|
|
|
|
logger.method<&Logger::set_verbosity_level>("set_verbosity_level", Php::Public, {Php::ByVal("verbosity_level", Php::Type::Numeric)});
|
2018-02-15 22:33:15 +01:00
|
|
|
|
2018-02-17 12:02:43 +01:00
|
|
|
X.add(std::move(api));
|
2018-03-07 13:45:23 +01:00
|
|
|
X.add(std::move(td));
|
2018-03-03 16:09:03 +01:00
|
|
|
X.add(std::move(logger));
|
2018-02-17 12:02:43 +01:00
|
|
|
MadelineProto.add(std::move(X));
|
2018-02-15 22:33:15 +01:00
|
|
|
danog.add(std::move(MadelineProto));
|
|
|
|
extension.add(std::move(danog));
|
|
|
|
|
|
|
|
return extension;
|
|
|
|
}
|
|
|
|
}
|