organizr-functions.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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['jellyfinURL']) && !empty($this->config['jellyfinToken'])) {
  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'])) {
  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. case 'jellyfin':
  227. $jellyfinAddress = $this->qualifyURL($this->config['jellyfinURL']);
  228. $imgParams = array();
  229. if (isset($_GET['height'])) {
  230. $imgParams['height'] = 'maxHeight=' . $_GET['height'];
  231. }
  232. if (isset($_GET['width'])) {
  233. $imgParams['width'] = 'maxWidth=' . $_GET['width'];
  234. }
  235. $image_src = $jellyfinAddress . '/Items/' . $image_url . '/Images/' . $itemType . '?' . implode('&', $imgParams);
  236. break;
  237. default:
  238. # code...
  239. break;
  240. }
  241. if (isset($image_url) && isset($image_height) && isset($image_width) && isset($image_src)) {
  242. $cachefile = $cacheDirectory . $key . '.jpg';
  243. $cachetime = 604800;
  244. // Serve from the cache if it is younger than $cachetime
  245. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile) && $refresh == false) {
  246. header("Content-type: image/jpeg");
  247. @readfile($cachefile);
  248. exit;
  249. }
  250. ob_start(); // Start the output buffer
  251. header('Content-type: image/jpeg');
  252. $options = array('verify' => false);
  253. $response = Requests::get($image_src, array(), $options);
  254. if ($response->success) {
  255. echo $response->body;
  256. }
  257. // Cache the output to a file
  258. $fp = fopen($cachefile, 'wb');
  259. fwrite($fp, ob_get_contents());
  260. fclose($fp);
  261. ob_end_flush(); // Send the output to the browser
  262. die();
  263. } else {
  264. die("Invalid Request");
  265. }
  266. }
  267. public function cacheImage($url, $name, $extension = 'jpg')
  268. {
  269. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  270. if (!file_exists($cacheDirectory)) {
  271. mkdir($cacheDirectory, 0777, true);
  272. }
  273. $cacheFile = $cacheDirectory . $name . '.' . $extension;
  274. $cacheTime = 604800;
  275. if ((file_exists($cacheFile) && time() - $cacheTime < filemtime($cacheFile)) || !file_exists($cacheFile)) {
  276. @copy($url, $cacheFile);
  277. }
  278. }
  279. public function checkFrame($array, $url)
  280. {
  281. if (array_key_exists("x-frame-options", $array)) {
  282. if ($array['x-frame-options'] == "deny") {
  283. return false;
  284. } elseif ($array['x-frame-options'] == "sameorgin") {
  285. $digest = parse_url($url);
  286. $host = (isset($digest['host']) ? $digest['host'] : '');
  287. if ($this->getServer() == $host) {
  288. return true;
  289. } else {
  290. return false;
  291. }
  292. }
  293. } else {
  294. if (!$array) {
  295. return false;
  296. }
  297. return true;
  298. }
  299. }
  300. public function frameTest($url)
  301. {
  302. if (!$url || $url == '') {
  303. $this->setAPIResponse('error', 'URL not supplied', 404);
  304. return false;
  305. }
  306. $array = array_change_key_case(get_headers($this->qualifyURL($url), 1));
  307. $url = $this->qualifyURL($url);
  308. if ($this->checkFrame($array, $url)) {
  309. $this->setAPIResponse('success', 'URL approved for iFrame', 200);
  310. return true;
  311. } else {
  312. $this->setAPIResponse('error', 'URL failed approval for iFrame', 409);
  313. return false;
  314. }
  315. }
  316. public function groupSelect()
  317. {
  318. $groups = $this->getAllGroups();
  319. $select = array();
  320. foreach ($groups as $key => $value) {
  321. $select[] = array(
  322. 'name' => $value['group'],
  323. 'value' => $value['group_id']
  324. );
  325. }
  326. return $select;
  327. }
  328. public function showLogin()
  329. {
  330. if ($this->config['hideRegistration'] == false) {
  331. 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>';
  332. }
  333. }
  334. public function checkoAuth()
  335. {
  336. return $this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] !== 'internal';
  337. }
  338. public function checkoAuthOnly()
  339. {
  340. return $this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] == 'external';
  341. }
  342. public function showoAuth()
  343. {
  344. $buttons = '';
  345. if ($this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] !== 'internal') {
  346. $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>';
  347. }
  348. return ($buttons) ? '
  349. <div class="panel">
  350. <div class="panel-heading bg-org" id="plex-login-heading" role="tab">
  351. <a class="panel-title" data-toggle="collapse" href="#plex-login-collapse" data-parent="#login-panels" aria-expanded="false" aria-controls="organizr-login-collapse">
  352. <img class="lazyload loginTitle" data-src="plugins/images/tabs/plex.png"> &nbsp;
  353. <span class="text-uppercase fw300" lang="en">Login with Plex</span>
  354. </a>
  355. </div>
  356. <div class="panel-collapse collapse in" id="plex-login-collapse" aria-labelledby="plex-login-heading" role="tabpanel">
  357. <div class="panel-body">
  358. <div class="row">
  359. <div class="col-xs-12 col-sm-12 col-md-12 text-center">
  360. <div class="social m-b-0">' . $buttons . '</div>
  361. </div>
  362. </div>
  363. </div>
  364. </div>
  365. </div>
  366. ' : '';
  367. }
  368. public function logoOrText()
  369. {
  370. if ($this->config['useLogoLogin'] == false) {
  371. return '<h1>' . $this->config['title'] . '</h1>';
  372. } else {
  373. return '<img class="loginLogo" src="' . $this->config['loginLogo'] . '" alt="Home" />';
  374. }
  375. }
  376. public function settingsDocker()
  377. {
  378. $type = ($this->docker) ? 'Official Docker' : 'Native';
  379. 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>';
  380. }
  381. public function settingsPathChecks()
  382. {
  383. $paths = $this->pathsWritable($this->paths);
  384. $items = '';
  385. $type = (array_search(false, $paths)) ? 'Not Writable' : 'Writable';
  386. $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>';
  387. foreach ($paths as $k => $v) {
  388. $items .= '<li class="folders-writable hidden"><div class="bg-primary"><i class="mdi mdi-folder mdi-24px text-white"></i></div><a tabindex="0" type="button" class="btn btn-default btn-outline popover-info pull-right clipboard" lang="en" data-container="body" title="" data-toggle="popover" data-placement="left" data-content="' . $v['path'] . '" data-original-title="File Path" data-clipboard-text="' . $v['path'] . '">' . $k . '</a> ' . (($v['writable']) ? 'Writable' : 'Not Writable') . '</li>';
  389. }
  390. return $result . $items;
  391. }
  392. public function pathsWritable($paths)
  393. {
  394. $results = array();
  395. foreach ($paths as $k => $v) {
  396. $results[$k] = [
  397. 'writable' => is_writable($v),
  398. 'path' => $v
  399. ];
  400. }
  401. return $results;
  402. }
  403. public function clearTautulliTokens()
  404. {
  405. foreach (array_keys($_COOKIE) as $k => $v) {
  406. if (strpos($v, 'tautulli') !== false) {
  407. $this->coookie('delete', $v);
  408. }
  409. }
  410. }
  411. public function analyzeIP($ip)
  412. {
  413. if (strpos($ip, '/') !== false) {
  414. $explodeIP = explode('/', $ip);
  415. $prefix = $explodeIP[1];
  416. $start_ip = $explodeIP[0];
  417. $ip_count = 1 << (32 - $prefix);
  418. $start_ip_long = ip2long($start_ip);
  419. $last_ip_long = ip2long($start_ip) + $ip_count - 1;
  420. } elseif (substr_count($ip, '.') == 3) {
  421. $start_ip_long = ip2long($ip);
  422. $last_ip_long = ip2long($ip);
  423. }
  424. return (isset($start_ip_long) && isset($last_ip_long)) ? array('from' => $start_ip_long, 'to' => $last_ip_long) : false;
  425. }
  426. public function authProxyRangeCheck($from, $to)
  427. {
  428. $approved = false;
  429. $userIP = ip2long($_SERVER['REMOTE_ADDR']);
  430. $low = $from;
  431. $high = $to;
  432. if ($userIP <= $high && $low <= $userIP) {
  433. $approved = true;
  434. }
  435. return $approved;
  436. }
  437. }
  438. function embyJoinAPI($array)
  439. {
  440. $username = ($array['username']) ?? null;
  441. $email = ($array['email']) ?? null;
  442. $password = ($array['password']) ?? null;
  443. if (!$username) {
  444. $GLOBALS['api']['response']['result'] = 'error';
  445. $GLOBALS['api']['response']['message'] = 'Username not supplied';
  446. return false;
  447. }
  448. if (!$email) {
  449. $GLOBALS['api']['response']['result'] = 'error';
  450. $GLOBALS['api']['response']['message'] = 'Email not supplied';
  451. return false;
  452. }
  453. if (!$password) {
  454. $GLOBALS['api']['response']['result'] = 'error';
  455. $GLOBALS['api']['response']['message'] = 'Password not supplied';
  456. return false;
  457. }
  458. return embyJoin($username, $email, $password);
  459. }
  460. function embyJoin($username, $email, $password)
  461. {
  462. try {
  463. #create user in emby.
  464. $headers = array(
  465. "Accept" => "application/json"
  466. );
  467. $data = array();
  468. $url = $GLOBALS['embyURL'] . '/emby/Users/New?name=' . $username . '&api_key=' . $GLOBALS['embyToken'];
  469. $response = Requests::Post($url, $headers, json_encode($data), array());
  470. $response = $response->body;
  471. //return($response);
  472. $response = json_decode($response, true);
  473. //return($response);
  474. $userID = $response["Id"];
  475. //return($userID);
  476. #authenticate as user to update password.
  477. //randomizer four digits of DeviceId
  478. // I dont think ther would be security problems with hardcoding deviceID but randomizing it would mitigate any issue.
  479. $deviceIdSeceret = rand(0, 9) . "" . rand(0, 9) . "" . rand(0, 9) . "" . rand(0, 9);
  480. //hardcoded device id with the first three digits random 0-9,0-9,0-9,0-9
  481. $embyAuthHeader = 'MediaBrowser Client="Emby Mobile", Device="Firefox", DeviceId="' . $deviceIdSeceret . 'aWxssS81LgAggFdpbmRvd3MgTlQgMTAuMDsgV2luNjxx7IHf2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzcyLjAuMzYyNi4xMTkgU2FmYXJpLzUzNy4zNnwxNTUxNTczMTAyNDI4", Version="4.0.2.0"';
  482. $headers = array(
  483. "Accept" => "application/json",
  484. "Content-Type" => "application/json",
  485. "X-Emby-Authorization" => $embyAuthHeader
  486. );
  487. $data = array(
  488. "Pw" => "",
  489. "Username" => $username
  490. );
  491. $url = $GLOBALS['embyURL'] . '/emby/Users/AuthenticateByName';
  492. $response = Requests::Post($url, $headers, json_encode($data), array());
  493. $response = $response->body;
  494. $response = json_decode($response, true);
  495. $userToken = $response["AccessToken"];
  496. #update password
  497. $embyAuthHeader = 'MediaBrowser Client="Emby Mobile", Device="Firefox", Token="' . $userToken . '", DeviceId="' . $deviceIdSeceret . 'aWxssS81LgAggFdpbmRvd3MgTlQgMTAuMDsgV2luNjxx7IHf2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzcyLjAuMzYyNi4xMTkgU2FmYXJpLzUzNy4zNnwxNTUxNTczMTAyNDI4", Version="4.0.2.0"';
  498. $headers = array(
  499. "Accept" => "application/json",
  500. "Content-Type" => "application/json",
  501. "X-Emby-Authorization" => $embyAuthHeader
  502. );
  503. $data = array(
  504. "CurrentPw" => "",
  505. "NewPw" => $password,
  506. "Id" => $userID
  507. );
  508. $url = $GLOBALS['embyURL'] . '/emby/Users/' . $userID . '/Password';
  509. Requests::Post($url, $headers, json_encode($data), array());
  510. #update config
  511. $headers = array(
  512. "Accept" => "application/json",
  513. "Content-Type" => "application/json"
  514. );
  515. $url = $GLOBALS['embyURL'] . '/emby/Users/' . $userID . '/Policy?api_key=' . $GLOBALS['embyToken'];
  516. $response = Requests::Post($url, $headers, getEmbyTemplateUserJson(), array());
  517. #add emby.media
  518. try {
  519. #seperate because this is not required
  520. $headers = array(
  521. "Accept" => "application/json",
  522. "X-Emby-Authorization" => $embyAuthHeader
  523. );
  524. $data = array(
  525. "ConnectUsername " => $email
  526. );
  527. $url = $GLOBALS['embyURL'] . '/emby/Users/' . $userID . '/Connect/Link';
  528. Requests::Post($url, $headers, json_encode($data), array());
  529. } catch (Requests_Exception $e) {
  530. writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  531. $GLOBALS['api']['response']['message'] = $e->getMessage();
  532. $GLOBALS['api']['response']['result'] = 'error';
  533. return false;
  534. }
  535. $GLOBALS['api']['response']['message'] = 'User has joined Emby';
  536. return (true);
  537. //return( "USERID:".$userID);
  538. } catch (Requests_Exception $e) {
  539. writeLog('error', 'Emby create Function - Error: ' . $e->getMessage(), 'SYSTEM');
  540. $GLOBALS['api']['response']['message'] = $e->getMessage();
  541. $GLOBALS['api']['response']['result'] = 'error';
  542. return false;
  543. };
  544. return false;
  545. }
  546. /*loads users from emby and returns a correctly formated policy for a new user.
  547. */
  548. function getEmbyTemplateUserJson()
  549. {
  550. $headers = array(
  551. "Accept" => "application/json"
  552. );
  553. $data = array();
  554. $url = $GLOBALS['embyURL'] . '/emby/Users?api_key=' . $GLOBALS['embyToken'];
  555. $response = Requests::Get($url, $headers, array());
  556. $response = $response->body;
  557. $response = json_decode($response, true);
  558. //error_Log("response ".json_encode($response));
  559. writeLog('error', 'userList:' . json_encode($response), 'SYSTEM');
  560. //$correct stores the template users object
  561. $correct = null;
  562. foreach ($response as $element) {
  563. if ($element['Name'] == $GLOBALS['INVITES-EmbyTemplate']) {
  564. $correct = $element;
  565. }
  566. }
  567. writeLog('error', 'Correct user:' . json_encode($correct), 'SYSTEM');
  568. if ($correct == null) {
  569. //return empty JSON if user incorectly configured template
  570. return "{}";
  571. }
  572. //select policy section and remove possibly dangeours rows.
  573. $policy = $correct['Policy'];
  574. //writeLog('error', 'policy update'.$policy, 'SYSTEM');
  575. unset($policy['AuthenticationProviderId']);
  576. unset($policy['InvalidLoginAttemptCount']);
  577. unset($policy['DisablePremiumFeatures']);
  578. unset($policy['DisablePremiumFeatures']);
  579. return (json_encode($policy));
  580. }
  581. function checkHostPrefix($s)
  582. {
  583. if (empty($s)) {
  584. return $s;
  585. }
  586. return (substr($s, -1, 1) == '\\') ? $s : $s . '\\';
  587. }