106 行
3.4 KiB
PHP
106 行
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Peertube;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Http\Request;
|
|
use App\Http\Controllers\Engine;
|
|
// use Illuminate\Support\Facades\Log;
|
|
|
|
class Common extends Engine {
|
|
public $user = [];
|
|
private $engine;
|
|
private $access_token;
|
|
private $refresh_token;
|
|
|
|
public function __construct () {
|
|
$this->access_token = isset($_COOKIE['access_token']) ? $_COOKIE['access_token'] : null;
|
|
$this->refresh_token = isset($_COOKIE['refresh_token']) ? $_COOKIE['refresh_token'] : null;
|
|
$this->user['local'] = $this->getLocal();
|
|
$this->user['me'] = $this->getMe($this->user['local']);
|
|
$this->user['config'] = $this->getConfig();
|
|
$this->user['notify'] = $this->getNotify();
|
|
$this->engine = new Engine;
|
|
}
|
|
|
|
public function getLocal () {
|
|
return $this->ptapi('/api/v1/oauth-clients/local');
|
|
}
|
|
|
|
public function getMe ($param) {
|
|
if (!isset($_COOKIE['access_token']) && isset($_COOKIE['refresh_token'])) {
|
|
$r = new Request;
|
|
$login = new \App\Http\Controllers\Peertube\Login;
|
|
$r->client_id = $param->client_id;
|
|
$r->client_secret = $param->client_secret;
|
|
$r->refresh_token = $this->refresh_token;
|
|
$this->login($r);
|
|
$this->access_token = $_COOKIE['access_token'];
|
|
$this->refresh_token = $_COOKIE['refresh_token'];
|
|
}
|
|
|
|
if (isset($_COOKIE['access_token']) && isset($_COOKIE['refresh_token'])) return $this->ptapi('/api/v1/users/me');
|
|
return null;
|
|
}
|
|
|
|
public function getConfig () {
|
|
return $this->ptapi('/api/v1/config/');
|
|
}
|
|
|
|
public function getNotify () {
|
|
if (isset($_COOKIE['access_token']) && isset($_COOKIE['refresh_token'])) return $this->ptapi('/api/v1/users/me/notifications?start=0&count=0&unread=true');
|
|
return null;
|
|
}
|
|
|
|
public function ptapi ($url, $param='', $method='get', $contenttype='json') {
|
|
set_time_limit(0);
|
|
$this->access_token = isset($_COOKIE['access_token']) ? $_COOKIE['access_token'] : null;
|
|
$this->refresh_token = isset($_COOKIE['refresh_token']) ? $_COOKIE['refresh_token'] : null;
|
|
$header = ['Content-Type: application/'.$contenttype, 'Host: '.str_replace('https://', '', env('PEER_URI'))];
|
|
|
|
if (!is_null($this->access_token)) {
|
|
$header[] = 'Authorization: Bearer '.$this->access_token;
|
|
}
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, env('PEER_URI').$url);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
|
|
if ($method == 'post') {
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
if ($param != '') curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
|
|
}
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
$get = curl_exec($ch);
|
|
$err = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if (!$get) return $err;
|
|
$get = json_decode($get);
|
|
if (isset($get->status) && $get->status == 404) return [];
|
|
|
|
return $get;
|
|
}
|
|
|
|
public function vidlist ($get) {
|
|
$res = [
|
|
'today' => [],
|
|
'week' => [],
|
|
'month' => [],
|
|
'lastmonth' => [],
|
|
'moreearly' => [],
|
|
];
|
|
|
|
foreach ($get->data as $g) {
|
|
$ud = strtotime($g->createdAt);
|
|
|
|
if ($ud > time() - 86400 && $ud < time() + 86400) $res['today'][] = $g;
|
|
else if ($ud > time() - 604800 && $ud < time() + 604800) $res['week'][] = $g;
|
|
else if ($ud > time() - 2629800 && $ud < time() + 2629800) $res['month'][] = $g;
|
|
else if ($ud > time() - 5259600 && $ud < time() + 5259600) $res['lastmonth'][] = $g;
|
|
else $res['moreearly'][] = $g;
|
|
}
|
|
|
|
return $res;
|
|
}
|
|
}
|