Skip to content

Instantly share code, notes, and snippets.

@Izumi-kun
Last active February 14, 2016 20:32
Show Gist options
  • Save Izumi-kun/9a76ce9d7c9c44aad044 to your computer and use it in GitHub Desktop.
Save Izumi-kun/9a76ce9d7c9c44aad044 to your computer and use it in GitHub Desktop.
Полезные функццции
<?php
/**
* Парсинг заголовков
* @param $rawHeaders
* @return array
*/
public function httpParseHeaders($rawHeaders)
{
$headers = [];
$key = '';
foreach (explode("\n", $rawHeaders) as $h) {
$h = explode(':', $h, 2);
if (isset($h[1])) {
if (!isset($headers[$h[0]])) {
$headers[$h[0]] = trim($h[1]);
} elseif (is_array($headers[$h[0]])) {
$headers[$h[0]] = array_merge($headers[$h[0]], [trim($h[1])]);
} else {
$headers[$h[0]] = array_merge([$headers[$h[0]]], [trim($h[1])]);
}
$key = $h[0];
} else {
if (substr($h[0], 0, 1) == "\t") {
$headers[$key] .= "\r\n\t" . trim($h[0]);
} elseif (!$key) {
$headers[0] = trim($h[0]);
}
trim($h[0]);
}
}
return $headers;
}
/**
* Запрос
* @param $url
* @param $fields
* @param string $cookie
* @param bool $nobody
* @param bool $useProxy
* @return array [header, body, errno]
*/
public function httpReq($url, $fields, $cookie = '', $nobody = true, $useProxy = false)
{
$ret = ['header' => '', 'body' => '', 'errno' => 0];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
curl_setopt($ch, CURLOPT_NOBODY, $nobody);
if ($fields) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
}
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if ($useProxy) {
curl_setopt($ch, CURLOPT_PROXY, 'insert url');
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:pass');
curl_setopt($ch, CURLOPT_PROXYPORT, 'port');
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
}
$data = curl_exec($ch);
$hSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$ret['errno'] = curl_errno($ch);
if (!$ret['errno']) {
$ret['header'] = $this->httpParseHeaders(substr($data, 0, $hSize));
if (!$nobody) {
$ret['body'] = substr($data, $hSize);
}
}
curl_close($ch);
return $ret;
}
/**
* Релиз на Тотошку
* @param $hash
* @param $apikey
* @return bool
*/
public function processTokyoTosho($hash, $apikey)
{
$info = $this->tracker->getTorrentInfo($hash);
if (!$info) {
return false;
}
$fields = [
'type' => '7', // RAW
'url' => "http://leopard-raws.org/download.php?hash=$hash",
'comment' => $info['comment'],
'apikey' => $apikey,
'website' => 'http://leopard-raws.org/',
'send' => 'Submit New Torrent'
];
$res = $this->httpReq('http://tokyotosho.info/new.php', $fields);
if (!$res['errno']) {
return true;
}
return false;
}
/**
* Загружает торрент на няя
* @param $hash
* @param string $id
* @param string $pw
* @return bool
*/
public function processNyaa($hash, $id = '', $pw = '')
{
$info = $this->tracker->getTorrentInfo($hash);
$file = $this->pathToTorrents . '/' . $hash . '.torrent';
if (!$info || !file_exists($file)) {
return false;
}
$fields = [
'torrent' => '@' . realpath($file) . ';filename=' . $info['name'] . ';type=application/x-bittorrent',
'torrenturl' => '',
'catid' => '1_11', // RAW
'info' => 'http://leopard-raws.org/',
'remake' => '0',
'anonymous' => '0',
'hidden' => '0',
'description' => $info['comment'],
'rules' => '1',
'submit' => 'Upload'
];
$cookies = ($id && $pw) ? "id=$id; pw=$pw" : '';
$res = $this->httpReq('http://www.nyaa.se/?page=upload', $fields, $cookies, true, true);
if (substr($res['header'][0], 9) == "200 OK") {
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment