organizr-functions.php 22 KB

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