Skip to content

Instantly share code, notes, and snippets.

@monobogdan
Created December 10, 2019 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monobogdan/8eaeb6c2ea04f1449113dea398c40a6c to your computer and use it in GitHub Desktop.
Save monobogdan/8eaeb6c2ea04f1449113dea398c40a6c to your computer and use it in GitHub Desktop.
<?php
class Osnova
{
private $token;
public function __construct($token)
{
$this->token = $token;
$this->webHooks = array();
}
public function request($url, $data)
{
$c = curl_init($url);
curl_setopt($c, CURLOPT_HTTPHEADER, array(
"X-Device-Token: $this->token"
));
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLINFO_HEADER_OUT, 1);
curl_setopt($c, CURLOPT_USERAGENT, "Opera/9.80 (Windows NT 6.1; WOW64) Presto/2.12.388 Version/12.18");
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
$ret = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
return json_decode($ret);
}
public function utfGetToken($in, $from)
{
$ret = "";
for($i = $from; $i < strlen($in); $i++)
{
if($in[$i] == "u")
{
$ret .= "x";
continue;
}
if(in_array($in[$i], [" ", "\t", "\\", "\r", "\n"]))
break;
$ret .= $in[$i];
}
return $ret;
}
public function utfDecode($in)
{
$utfTok = "";
$ret = "";
for($i = 0; $i < strlen($in); $i++)
{
if($in[$i] == "\\" && $i < strlen($in) - 1)
{
$tok = $this->utfGetToken($in, $i + 1);
$i += strlen($tok);
$ret .= html_entity_decode("&#$tok;", ENT_COMPAT, "UTF-8");
continue;
}
$ret .= $in[$i];
}
return $ret;
}
public function sendComment($post, $replyTo, $comment, $attachments = array())
{
$this->request("https://api.dtf.ru/v1.8/comment/add", array(
"id" => $post,
"reply_to" => $replyTo,
"text" => $comment,
"attachments" => json_encode($attachments)
));
}
public function getAttachment($url)
{
return $this->request("https://api.dtf.ru/v1.8/uploader/extract", array(
"url" => $url
));
}
public function subscribe($event, $url, $method)
{
$this->webHooks[$event] = $method;
if(!$this->isWebHook())
{
$this->request("https://api.dtf.ru/v1.8/webhooks/add", array(
"url" => "$url?webhook",
"event" => $event
));
}
else
{
$in = file_get_contents("php://input");
$json = json_decode($in, true);
set_time_limit(0);
ignore_user_abort(true);
header("Connection: close");
header("Content-Length: 0");
flush();
if(isset($this->webHooks[$json["type"]]))
$this->webHooks[$json["type"]]($this, $json);
}
}
public function isWebHook()
{
return isset($_GET["webhook"]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment