-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlexAuth.php
More file actions
executable file
·122 lines (106 loc) · 3.49 KB
/
Copy pathPlexAuth.php
File metadata and controls
executable file
·122 lines (106 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace XcVm\Module\Plex;
/**
* PlexAuth — plex auth
*
* @package XC_VM_Module_Plex
* @author Divarion_D <https://github.com/Divarion-D>
* @copyright 2025-2026 Vateron Media
* @link https://github.com/Vateron-Media/XC_VM
* @license AGPL-3.0 https://www.gnu.org/licenses/agpl-3.0.html
*/
class PlexAuth {
public static function getPlexToken($plexIP = null, $plexPort = null, $plexUsername = null, $plexPassword = null) {
$serverKey = self::getPlexServerCacheKey($plexIP, $plexPort, $plexUsername, $plexPassword);
$rToken = self::getCachedPlexToken($serverKey);
if ($rToken) {
$rToken = self::checkPlexToken($plexIP, $plexPort, $rToken);
}
if (!$rToken) {
$rData = self::getPlexLogin($plexUsername, $plexPassword);
if (isset($rData['user']['authToken'])) {
$rToken = self::checkPlexToken($plexIP, $plexPort, $rData['user']['authToken']);
if ($rToken) {
self::cachePlexToken($serverKey, $rToken);
}
} else {
$rToken = false;
}
}
return $rToken;
}
public static function getPlexServerCacheKey($ip, $port, $username = null, $password = null) {
if ($username && $password) {
return md5($ip . ':' . $port . ':' . $username . ':' . $password);
}
return md5($ip . ':' . $port);
}
public static function getCachedPlexToken($serverKey) {
$cacheFile = CONFIG_PATH . 'plex/plex_token_' . $serverKey . '.json';
if (!file_exists($cacheFile)) {
return null;
}
$data = json_decode(file_get_contents($cacheFile), true);
if (!$data || !isset($data['token']) || !isset($data['expires'])) {
return null;
}
if ($data['expires'] < time() + 86400) {
@unlink($cacheFile);
return null;
}
return $data['token'];
}
public static function getPlexLogin($rUsername, $rPassword) {
$headers = [
'Content-Type: application/xml; charset=utf-8',
'X-Plex-Client-Identifier: 526e163c-8dbd-11eb-8dcd-0242ac130003',
'X-Plex-Product: XC_VM',
'X-Plex-Version: v' . XC_VM_VERSION
];
$ch = curl_init('https://plex.tv/users/sign_in.json');
curl_setopt_array($ch, [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_HEADER => false,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_USERPWD => $rUsername . ':' . $rPassword,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
public static function checkPlexToken($rIP, $rPort, $rToken) {
$checkURL = 'http://' . $rIP . ':' . $rPort . '/myplex/account?X-Plex-Token=' . $rToken;
$ch = curl_init($checkURL);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_SSL_VERIFYPEER => false,
]);
$data = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($data);
if ($xml === false) {
return '';
}
$json = json_decode(json_encode($xml), true);
return (isset($json['@attributes']['signInState']) && $json['@attributes']['signInState'] === 'ok') ? $rToken : '';
}
public static function cachePlexToken($serverKey, $token) {
$cacheFile = CONFIG_PATH . 'plex/plex_token_' . $serverKey . '.json';
$data = [
'token' => $token,
'cached_at' => time(),
'expires' => time() + 30 * 86400
];
if (!is_dir(dirname($cacheFile))) {
mkdir(dirname($cacheFile), 0755, true);
}
file_put_contents($cacheFile, json_encode($data, JSON_PRETTY_PRINT));
@chmod($cacheFile, 0600);
}
}