organizr-functions.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. <?php
  2. trait OrganizrFunctions
  3. {
  4. public function checkHostPrefix($s)
  5. {
  6. if (empty($s)) {
  7. return $s;
  8. }
  9. return (substr($s, -1, 1) == '\\') ? $s : $s . '\\';
  10. }
  11. public function approvedFileExtension($filename)
  12. {
  13. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  14. switch ($ext) {
  15. case 'gif':
  16. case 'png':
  17. case 'jpeg':
  18. case 'jpg':
  19. case 'svg':
  20. return true;
  21. break;
  22. default:
  23. return false;
  24. }
  25. }
  26. public function getImages()
  27. {
  28. $allIconsPrep = array();
  29. $allIcons = array();
  30. $ignore = array(".", "..", "._.DS_Store", ".DS_Store", ".pydio_id", "index.html");
  31. $dirname = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'tabs' . DIRECTORY_SEPARATOR;
  32. $path = 'plugins/images/tabs/';
  33. $images = scandir($dirname);
  34. foreach ($images as $image) {
  35. if (!in_array($image, $ignore)) {
  36. $allIconsPrep[$image] = array(
  37. 'path' => $path,
  38. 'name' => $image
  39. );
  40. }
  41. }
  42. $dirname = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'userTabs' . DIRECTORY_SEPARATOR;
  43. $path = 'plugins/images/userTabs/';
  44. $images = scandir($dirname);
  45. foreach ($images as $image) {
  46. if (!in_array($image, $ignore)) {
  47. $allIconsPrep[$image] = array(
  48. 'path' => $path,
  49. 'name' => $image
  50. );
  51. }
  52. }
  53. ksort($allIconsPrep);
  54. foreach ($allIconsPrep as $item) {
  55. $allIcons[] = $item['path'] . $item['name'];
  56. }
  57. return $allIcons;
  58. }
  59. public function imageSelect($form)
  60. {
  61. $i = 1;
  62. $images = $this->getImages();
  63. $return = '<select class="form-control tabIconImageList" id="' . $form . '-chooseImage" name="chooseImage"><option lang="en">Select or type Icon</option>';
  64. foreach ($images as $image) {
  65. $i++;
  66. $return .= '<option value="' . $image . '">' . basename($image) . '</option>';
  67. }
  68. return $return . '</select>';
  69. }
  70. public function getThemes()
  71. {
  72. $themes = array();
  73. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . "*.css") as $filename) {
  74. $themes[] = array(
  75. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  76. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename))
  77. );
  78. }
  79. return $themes;
  80. }
  81. public function getSounds()
  82. {
  83. $sounds = array();
  84. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'sounds' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . "*.mp3") as $filename) {
  85. $sounds[] = array(
  86. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  87. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', 'plugins/sounds/default/' . basename($filename) . '.mp3')
  88. );
  89. }
  90. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'sounds' . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . "*.mp3") as $filename) {
  91. $sounds[] = array(
  92. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  93. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', 'plugins/sounds/custom/' . basename($filename) . '.mp3')
  94. );
  95. }
  96. return $sounds;
  97. }
  98. public function getBranches()
  99. {
  100. return array(
  101. array(
  102. 'name' => 'Develop',
  103. 'value' => 'v2-develop'
  104. ),
  105. array(
  106. 'name' => 'Master',
  107. 'value' => 'v2-master'
  108. )
  109. );
  110. }
  111. public function getAuthTypes()
  112. {
  113. return array(
  114. array(
  115. 'name' => 'Organizr DB',
  116. 'value' => 'internal'
  117. ),
  118. array(
  119. 'name' => 'Organizr DB + Backend',
  120. 'value' => 'both'
  121. ),
  122. array(
  123. 'name' => 'Backend Only',
  124. 'value' => 'external'
  125. )
  126. );
  127. }
  128. public function getLDAPOptions()
  129. {
  130. return array(
  131. array(
  132. 'name' => 'Active Directory',
  133. 'value' => '1'
  134. ),
  135. array(
  136. 'name' => 'OpenLDAP',
  137. 'value' => '2'
  138. ),
  139. array(
  140. 'name' => 'First IPA',
  141. 'value' => '3'
  142. ),
  143. );
  144. }
  145. public function getAuthBackends()
  146. {
  147. $backendOptions = array();
  148. $backendOptions[] = array(
  149. 'name' => 'Choose Backend',
  150. 'value' => false,
  151. 'disabled' => true
  152. );
  153. foreach (array_filter(get_class_methods('Organizr'), function ($v) {
  154. return strpos($v, 'plugin_auth_') === 0;
  155. }) as $value) {
  156. $name = str_replace('plugin_auth_', '', $value);
  157. if (strpos($name, 'disabled') === false) {
  158. $backendOptions[] = array(
  159. 'name' => ucwords(str_replace('_', ' ', $name)),
  160. 'value' => $name
  161. );
  162. } else {
  163. $backendOptions[] = array(
  164. 'name' => $this->$value(),
  165. 'value' => 'none',
  166. 'disabled' => true,
  167. );
  168. }
  169. }
  170. ksort($backendOptions);
  171. return $backendOptions;
  172. }
  173. public function importUserButtons()
  174. {
  175. $emptyButtons = '
  176. <div class="col-md-12">
  177. <div class="white-box bg-org">
  178. <h3 class="box-title m-0" lang="en">Currently User import is available for Plex only.</h3> </div>
  179. </div>
  180. ';
  181. $buttons = '';
  182. if (!empty($this->config['plexToken'])) {
  183. $buttons .= '<button class="btn m-b-20 m-r-20 bg-plex text-muted waves-effect waves-light importUsersButton" onclick="importUsers(\'plex\')" type="button"><span class="btn-label"><i class="mdi mdi-plex"></i></span><span lang="en">Import Plex Users</span></button>';
  184. }
  185. if (!empty($this->config['embyURL']) && !empty($this->config['embyToken']) && ($this->config['homepageJellyfinInstead'])) {
  186. $buttons .= '<button class="btn m-b-20 m-r-20 bg-primary text-muted waves-effect waves-light importUsersButton" onclick="importUsers(\'jellyfin\')" type="button"><span class="btn-label"><i class="mdi mdi-fish"></i></span><span lang="en">Import Jellyfin Users</span></button>';
  187. }
  188. if (!empty($this->config['embyURL']) && !empty($this->config['embyToken']) && (!$this->config['homepageJellyfinInstead'])) {
  189. $buttons .= '<button class="btn m-b-20 m-r-20 bg-emby text-muted waves-effect waves-light importUsersButton" onclick="importUsers(\'emby\')" type="button"><span class="btn-label"><i class="mdi mdi-emby"></i></span><span lang="en">Import Jellyfin Users</span></button>';
  190. }
  191. return ($buttons !== '') ? $buttons : $emptyButtons;
  192. }
  193. public function getHomepageMediaImage()
  194. {
  195. $refresh = false;
  196. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  197. if (!file_exists($cacheDirectory)) {
  198. mkdir($cacheDirectory, 0777, true);
  199. }
  200. @$image_url = $_GET['img'];
  201. @$key = $_GET['key'];
  202. @$image_height = $_GET['height'];
  203. @$image_width = $_GET['width'];
  204. @$source = $_GET['source'];
  205. @$itemType = $_GET['type'];
  206. if (strpos($key, '$') !== false) {
  207. $key = explode('$', $key)[0];
  208. $refresh = true;
  209. }
  210. switch ($source) {
  211. case 'plex':
  212. $plexAddress = $this->qualifyURL($this->config['plexURL']);
  213. $image_src = $plexAddress . '/photo/:/transcode?height=' . $image_height . '&width=' . $image_width . '&upscale=1&url=' . $image_url . '&X-Plex-Token=' . $this->config['plexToken'];
  214. break;
  215. case 'emby':
  216. $embyAddress = $this->qualifyURL($this->config['embyURL']);
  217. $imgParams = array();
  218. if (isset($_GET['height'])) {
  219. $imgParams['height'] = 'maxHeight=' . $_GET['height'];
  220. }
  221. if (isset($_GET['width'])) {
  222. $imgParams['width'] = 'maxWidth=' . $_GET['width'];
  223. }
  224. $image_src = $embyAddress . '/Items/' . $image_url . '/Images/' . $itemType . '?' . implode('&', $imgParams);
  225. break;
  226. default:
  227. # code...
  228. break;
  229. }
  230. if (isset($image_url) && isset($image_height) && isset($image_width) && isset($image_src)) {
  231. $cachefile = $cacheDirectory . $key . '.jpg';
  232. $cachetime = 604800;
  233. // Serve from the cache if it is younger than $cachetime
  234. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile) && $refresh == false) {
  235. header("Content-type: image/jpeg");
  236. @readfile($cachefile);
  237. exit;
  238. }
  239. ob_start(); // Start the output buffer
  240. header('Content-type: image/jpeg');
  241. $options = array('verify' => false);
  242. $response = Requests::get($image_src, array(), $options);
  243. if ($response->success) {
  244. echo $response->body;
  245. }
  246. // Cache the output to a file
  247. $fp = fopen($cachefile, 'wb');
  248. fwrite($fp, ob_get_contents());
  249. fclose($fp);
  250. ob_end_flush(); // Send the output to the browser
  251. die();
  252. } else {
  253. die("Invalid Request");
  254. }
  255. }
  256. public function cacheImage($url, $name, $extension = 'jpg')
  257. {
  258. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  259. if (!file_exists($cacheDirectory)) {
  260. mkdir($cacheDirectory, 0777, true);
  261. }
  262. $cacheFile = $cacheDirectory . $name . '.' . $extension;
  263. $cacheTime = 604800;
  264. if ((file_exists($cacheFile) && time() - $cacheTime < filemtime($cacheFile)) || !file_exists($cacheFile)) {
  265. @copy($url, $cacheFile);
  266. }
  267. }
  268. public function checkFrame($array, $url)
  269. {
  270. if (array_key_exists("x-frame-options", $array)) {
  271. if ($array['x-frame-options'] == "deny") {
  272. return false;
  273. } elseif ($array['x-frame-options'] == "sameorgin") {
  274. $digest = parse_url($url);
  275. $host = (isset($digest['host']) ? $digest['host'] : '');
  276. if ($this->getServer() == $host) {
  277. return true;
  278. } else {
  279. return false;
  280. }
  281. }
  282. } else {
  283. if (!$array) {
  284. return false;
  285. }
  286. return true;
  287. }
  288. }
  289. public function frameTest($url)
  290. {
  291. if (!$url || $url == '') {
  292. $this->setAPIResponse('error', 'URL not supplied', 404);
  293. return false;
  294. }
  295. $array = array_change_key_case(get_headers($this->qualifyURL($url), 1));
  296. $url = $this->qualifyURL($url);
  297. if ($this->checkFrame($array, $url)) {
  298. $this->setAPIResponse('success', 'URL approved for iFrame', 200);
  299. return true;
  300. } else {
  301. $this->setAPIResponse('error', 'URL failed approval for iFrame', 409);
  302. return false;
  303. }
  304. }
  305. public function groupSelect()
  306. {
  307. $groups = $this->getAllGroups();
  308. $select = array();
  309. foreach ($groups as $key => $value) {
  310. $select[] = array(
  311. 'name' => $value['group'],
  312. 'value' => $value['group_id']
  313. );
  314. }
  315. return $select;
  316. }
  317. public function showLogin()
  318. {
  319. if ($this->config['hideRegistration'] == false) {
  320. return '<p><span lang="en">Don\'t have an account?</span><a href="#" class="text-primary m-l-5 to-register"><b lang="en">Sign Up</b></a></p>';
  321. }
  322. }
  323. public function checkoAuth()
  324. {
  325. return $this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] !== 'internal';
  326. }
  327. public function checkoAuthOnly()
  328. {
  329. return $this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] == 'external';
  330. }
  331. public function showoAuth()
  332. {
  333. $buttons = '';
  334. if ($this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] !== 'internal') {
  335. $buttons .= '<a href="javascript:void(0)" onclick="oAuthStart(\'plex\')" class="btn btn-lg btn-block text-uppercase waves-effect waves-light bg-plex text-muted" data-toggle="tooltip" title="" data-original-title="Login with Plex"> <span>Login</span><i aria-hidden="true" class="mdi mdi-plex m-l-5"></i> </a>';
  336. }
  337. return ($buttons) ? '
  338. <div class="panel">
  339. <div class="panel-heading bg-org" id="plex-login-heading" role="tab">
  340. <a class="panel-title" data-toggle="collapse" href="#plex-login-collapse" data-parent="#login-panels" aria-expanded="false" aria-controls="organizr-login-collapse">
  341. <img class="lazyload loginTitle" data-src="plugins/images/tabs/plex.png"> &nbsp;
  342. <span class="text-uppercase fw300" lang="en">Login with Plex</span>
  343. </a>
  344. </div>
  345. <div class="panel-collapse collapse in" id="plex-login-collapse" aria-labelledby="plex-login-heading" role="tabpanel">
  346. <div class="panel-body">
  347. <div class="row">
  348. <div class="col-xs-12 col-sm-12 col-md-12 text-center">
  349. <div class="social m-b-0">' . $buttons . '</div>
  350. </div>
  351. </div>
  352. </div>
  353. </div>
  354. </div>
  355. ' : '';
  356. }
  357. public function logoOrText()
  358. {
  359. if ($this->config['useLogoLogin'] == false) {
  360. return '<h1>' . $this->config['title'] . '</h1>';
  361. } else {
  362. return '<img class="loginLogo" src="' . $this->config['loginLogo'] . '" alt="Home" />';
  363. }
  364. }
  365. public function settingsDocker()
  366. {
  367. $type = ($this->docker) ? 'Official Docker' : 'Native';
  368. return '<li><div class="bg-info"><i class="mdi mdi-flag mdi-24px text-white"></i></div><span class="text-muted hidden-xs m-t-10" lang="en">Install Type</span> ' . $type . '</li>';
  369. }
  370. public function settingsPathChecks()
  371. {
  372. $items = '';
  373. $type = (array_search(false, $this->pathsWritable($this->paths))) ? 'Not Writable' : 'Writable';
  374. $result = '<li class="mouse" onclick="toggleWritableFolders();"><div class="bg-info"><i class="mdi mdi-folder mdi-24px text-white"></i></div><span class="text-muted hidden-xs m-t-10" lang="en">Organizr Paths</span> ' . $type . '</li>';
  375. foreach ($this->pathsWritable($this->paths) as $k => $v) {
  376. $items .= '<li class="folders-writable hidden"><div class="bg-info"><i class="mdi mdi-folder mdi-24px text-white"></i></div><span class="text-muted hidden-xs m-t-10" lang="en">' . $k . '</span> ' . (($v) ? 'Writable' : 'Not Writable') . '</li>';
  377. }
  378. return $result . $items;
  379. }
  380. public function pathsWritable($paths)
  381. {
  382. $results = array();
  383. foreach ($paths as $k => $v) {
  384. $results[$k] = is_writable($v);
  385. }
  386. return $results;
  387. }
  388. public function clearTautulliTokens()
  389. {
  390. foreach (array_keys($_COOKIE) as $k => $v) {
  391. if (strpos($v, 'tautulli') !== false) {
  392. $this->coookie('delete', $v);
  393. }
  394. }
  395. }
  396. public function analyzeIP($ip)
  397. {
  398. if (strpos($ip, '/') !== false) {
  399. $explodeIP = explode('/', $ip);
  400. $prefix = $explodeIP[1];
  401. $start_ip = $explodeIP[0];
  402. $ip_count = 1 << (32 - $prefix);
  403. $start_ip_long = ip2long($start_ip);
  404. $last_ip_long = ip2long($start_ip) + $ip_count - 1;
  405. } elseif (substr_count($ip, '.') == 3) {
  406. $start_ip_long = ip2long($ip);
  407. $last_ip_long = ip2long($ip);
  408. }
  409. return (isset($start_ip_long) && isset($last_ip_long)) ? array('from' => $start_ip_long, 'to' => $last_ip_long) : false;
  410. }
  411. public function authProxyRangeCheck($from, $to)
  412. {
  413. $approved = false;
  414. $userIP = ip2long($_SERVER['REMOTE_ADDR']);
  415. $low = $from;
  416. $high = $to;
  417. if ($userIP <= $high && $low <= $userIP) {
  418. $approved = true;
  419. }
  420. return $approved;
  421. }
  422. }
  423. function embyJoinAPI($array)
  424. {
  425. $username = ($array['username']) ?? null;
  426. $email = ($array['email']) ?? null;
  427. $password = ($array['password']) ?? null;
  428. if (!$username) {
  429. $GLOBALS['api']['response']['result'] = 'error';
  430. $GLOBALS['api']['response']['message'] = 'Username not supplied';
  431. return false;
  432. }
  433. if (!$email) {
  434. $GLOBALS['api']['response']['result'] = 'error';
  435. $GLOBALS['api']['response']['message'] = 'Email not supplied';
  436. return false;
  437. }
  438. if (!$password) {
  439. $GLOBALS['api']['response']['result'] = 'error';
  440. $GLOBALS['api']['response']['message'] = 'Password not supplied';
  441. return false;
  442. }
  443. return embyJoin($username, $email, $password);
  444. }
  445. function embyJoin($username, $email, $password)
  446. {
  447. try {
  448. #create user in emby.
  449. $headers = array(
  450. "Accept" => "application/json"
  451. );
  452. $data = array();
  453. $url = $GLOBALS['embyURL'] . '/emby/Users/New?name=' . $username . '&api_key=' . $GLOBALS['embyToken'];
  454. $response = Requests::Post($url, $headers, json_encode($data), array());
  455. $response = $response->body;
  456. //return($response);
  457. $response = json_decode($response, true);
  458. //return($response);
  459. $userID = $response["Id"];
  460. //return($userID);
  461. #authenticate as user to update password.
  462. //randomizer four digits of DeviceId
  463. // I dont think ther would be security problems with hardcoding deviceID but randomizing it would mitigate any issue.
  464. $deviceIdSeceret = rand(0, 9) . "" . rand(0, 9) . "" . rand(0, 9) . "" . rand(0, 9);
  465. //hardcoded device id with the first three digits random 0-9,0-9,0-9,0-9
  466. $embyAuthHeader = 'MediaBrowser Client="Emby Mobile", Device="Firefox", DeviceId="' . $deviceIdSeceret . 'aWxssS81LgAggFdpbmRvd3MgTlQgMTAuMDsgV2luNjxx7IHf2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzcyLjAuMzYyNi4xMTkgU2FmYXJpLzUzNy4zNnwxNTUxNTczMTAyNDI4", Version="4.0.2.0"';
  467. $headers = array(
  468. "Accept" => "application/json",
  469. "Content-Type" => "application/json",
  470. "X-Emby-Authorization" => $embyAuthHeader
  471. );
  472. $data = array(
  473. "Pw" => "",
  474. "Username" => $username
  475. );
  476. $url = $GLOBALS['embyURL'] . '/emby/Users/AuthenticateByName';
  477. $response = Requests::Post($url, $headers, json_encode($data), array());
  478. $response = $response->body;
  479. $response = json_decode($response, true);
  480. $userToken = $response["AccessToken"];
  481. #update password
  482. $embyAuthHeader = 'MediaBrowser Client="Emby Mobile", Device="Firefox", Token="' . $userToken . '", DeviceId="' . $deviceIdSeceret . 'aWxssS81LgAggFdpbmRvd3MgTlQgMTAuMDsgV2luNjxx7IHf2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzcyLjAuMzYyNi4xMTkgU2FmYXJpLzUzNy4zNnwxNTUxNTczMTAyNDI4", Version="4.0.2.0"';
  483. $headers = array(
  484. "Accept" => "application/json",
  485. "Content-Type" => "application/json",
  486. "X-Emby-Authorization" => $embyAuthHeader
  487. );
  488. $data = array(
  489. "CurrentPw" => "",
  490. "NewPw" => $password,
  491. "Id" => $userID
  492. );
  493. $url = $GLOBALS['embyURL'] . '/emby/Users/' . $userID . '/Password';
  494. Requests::Post($url, $headers, json_encode($data), array());
  495. #update config
  496. $headers = array(
  497. "Accept" => "application/json",
  498. "Content-Type" => "application/json"
  499. );
  500. $url = $GLOBALS['embyURL'] . '/emby/Users/' . $userID . '/Policy?api_key=' . $GLOBALS['embyToken'];
  501. $response = Requests::Post($url, $headers, getEmbyTemplateUserJson(), array());
  502. #add emby.media
  503. try {
  504. #seperate because this is not required
  505. $headers = array(
  506. "Accept" => "application/json",
  507. "X-Emby-Authorization" => $embyAuthHeader
  508. );
  509. $data = array(
  510. "ConnectUsername " => $email
  511. );
  512. $url = $GLOBALS['embyURL'] . '/emby/Users/' . $userID . '/Connect/Link';
  513. Requests::Post($url, $headers, json_encode($data), array());
  514. } catch (Requests_Exception $e) {
  515. writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  516. $GLOBALS['api']['response']['message'] = $e->getMessage();
  517. $GLOBALS['api']['response']['result'] = 'error';
  518. return false;
  519. }
  520. $GLOBALS['api']['response']['message'] = 'User has joined Emby';
  521. return (true);
  522. //return( "USERID:".$userID);
  523. } catch (Requests_Exception $e) {
  524. writeLog('error', 'Emby create Function - Error: ' . $e->getMessage(), 'SYSTEM');
  525. $GLOBALS['api']['response']['message'] = $e->getMessage();
  526. $GLOBALS['api']['response']['result'] = 'error';
  527. return false;
  528. };
  529. return false;
  530. }
  531. /*loads users from emby and returns a correctly formated policy for a new user.
  532. */
  533. function getEmbyTemplateUserJson()
  534. {
  535. $headers = array(
  536. "Accept" => "application/json"
  537. );
  538. $data = array();
  539. $url = $GLOBALS['embyURL'] . '/emby/Users?api_key=' . $GLOBALS['embyToken'];
  540. $response = Requests::Get($url, $headers, array());
  541. $response = $response->body;
  542. $response = json_decode($response, true);
  543. //error_Log("response ".json_encode($response));
  544. writeLog('error', 'userList:' . json_encode($response), 'SYSTEM');
  545. //$correct stores the template users object
  546. $correct = null;
  547. foreach ($response as $element) {
  548. if ($element['Name'] == $GLOBALS['INVITES-EmbyTemplate']) {
  549. $correct = $element;
  550. }
  551. }
  552. writeLog('error', 'Correct user:' . json_encode($correct), 'SYSTEM');
  553. if ($correct == null) {
  554. //return empty JSON if user incorectly configured template
  555. return "{}";
  556. }
  557. //select policy section and remove possibly dangeours rows.
  558. $policy = $correct['Policy'];
  559. //writeLog('error', 'policy update'.$policy, 'SYSTEM');
  560. unset($policy['AuthenticationProviderId']);
  561. unset($policy['InvalidLoginAttemptCount']);
  562. unset($policy['DisablePremiumFeatures']);
  563. unset($policy['DisablePremiumFeatures']);
  564. return (json_encode($policy));
  565. }
  566. function checkHostPrefix($s)
  567. {
  568. if (empty($s)) {
  569. return $s;
  570. }
  571. return (substr($s, -1, 1) == '\\') ? $s : $s . '\\';
  572. }