2018-01-01 17:01:56 +01:00
|
|
|
//
|
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2017
|
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
2018-03-03 16:09:03 +01:00
|
|
|
#include <td/telegram/td_json_client.h>
|
2018-01-01 17:01:56 +01:00
|
|
|
#include <td/telegram/Log.h>
|
|
|
|
|
2018-02-15 22:33:15 +01:00
|
|
|
#include <phpcpp.h>
|
|
|
|
|
2018-01-01 17:01:56 +01:00
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
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-03 16:09:03 +01:00
|
|
|
void __construct(Php::Parameters ¶ms) {
|
|
|
|
Php::Value self(this);
|
|
|
|
self["tdlibParameters"] = params[0];
|
2018-02-17 12:02:43 +01:00
|
|
|
initTdlib();
|
|
|
|
}
|
|
|
|
void __wakeup() {
|
|
|
|
initTdlib();
|
2018-03-01 01:49:03 +01:00
|
|
|
}
|
|
|
|
void __destruct() {
|
|
|
|
deinitTdlib();
|
2018-02-17 12:02:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void initTdlib() {
|
2018-03-03 16:09:03 +01:00
|
|
|
client = td_json_client_create();
|
|
|
|
Php::Value self(this);
|
|
|
|
Php::Array init;
|
|
|
|
init["@type"] = "setTdlibParameters";
|
|
|
|
init["tdlibParameters"] = self["tdlibParameters"];
|
|
|
|
std::string initJson = Php::call("json_encode", init);
|
|
|
|
|
|
|
|
td_json_client_send(client, initJson.c_str());
|
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-03 16:09:03 +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") {
|
|
|
|
Php::Value self(this);
|
|
|
|
self["tdlibParameters"] = value;
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-03 16:09:03 +01:00
|
|
|
std::string valueJson = Php::call("json_encode", value);
|
|
|
|
td_json_client_send(client, valueJson.c_str());
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-01 01:49:03 +01:00
|
|
|
|
|
|
|
Php::Value receive(Php::Parameters ¶ms) {
|
2018-03-03 16:09:03 +01:00
|
|
|
return Php::call("json_decode", td_json_client_receive(client, params[0]), true);
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-01 01:49:03 +01:00
|
|
|
|
|
|
|
Php::Value execute(Php::Parameters ¶ms) {
|
|
|
|
Php::Value value = params[0];
|
|
|
|
if (value.get("@type") == "setTdlibParameters") {
|
|
|
|
Php::Value self(this);
|
|
|
|
self["tdlibParameters"] = value;
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-03 16:09:03 +01:00
|
|
|
std::string valueJson = Php::call("json_encode", value);
|
|
|
|
return Php::call("json_decode", td_json_client_execute(client, valueJson.c_str()), true);
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-01 01:49:03 +01:00
|
|
|
|
|
|
|
|
2018-01-01 17:01:56 +01:00
|
|
|
|
2018-03-01 01:49:03 +01:00
|
|
|
private:
|
2018-03-03 16:09:03 +01:00
|
|
|
void *client;
|
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) {
|
|
|
|
return td::Log::set_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) {
|
|
|
|
td::Log::set_max_file_size(params[0]);
|
2018-01-01 17:01:56 +01:00
|
|
|
}
|
2018-03-01 01:49:03 +01:00
|
|
|
static void set_verbosity_level(Php::Parameters ¶ms) {
|
|
|
|
td::Log::set_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-03 16:09:03 +01:00
|
|
|
api.method<&API::__construct>("__construct", Php::Public | Php::Final, {Php::ByVal("tdlibParameters", Php::Type::Array)});
|
2018-03-01 01:49:03 +01:00
|
|
|
api.method<&API::__wakeup>("__wakeup", Php::Public | Php::Final, {});
|
|
|
|
api.method<&API::__destruct>("__destruct", Php::Public | Php::Final, {});
|
|
|
|
api.method<&API::send>("send", Php::Public | Php::Final);
|
2018-03-03 16:09:03 +01:00
|
|
|
api.method<&API::receive>("receive", Php::Public | Php::Final, {Php::ByVal("timeout", Php::Type::Float)});
|
2018-03-01 01:49:03 +01:00
|
|
|
api.method<&API::execute>("execute", Php::Public | Php::Final);
|
2018-02-15 22:33:15 +01:00
|
|
|
|
2018-03-03 16:09:03 +01:00
|
|
|
api.property("tdlibParameters", nullptr, Php::Private);
|
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
|
|
|
|
|
|
|
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-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;
|
|
|
|
}
|
|
|
|
}
|