organizr-functions.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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 ($name == 'ldap') {
  349. if (!function_exists('ldap_connect')) {
  350. continue;
  351. }
  352. }
  353. if ($name == 'ldap_disabled') {
  354. if (function_exists('ldap_connect')) {
  355. continue;
  356. }
  357. }
  358. if (strpos($name, 'disabled') === false) {
  359. $backendOptions[] = array(
  360. 'name' => ucwords(str_replace('_', ' ', $name)),
  361. 'value' => $name
  362. );
  363. } else {
  364. $backendOptions[] = array(
  365. 'name' => $this->$value(),
  366. 'value' => 'none',
  367. 'disabled' => true,
  368. );
  369. }
  370. }
  371. ksort($backendOptions);
  372. return $backendOptions;
  373. }
  374. public function importUserButtons()
  375. {
  376. $emptyButtons = '
  377. <div class="col-md-12">
  378. <div class="white-box bg-org">
  379. <h3 class="box-title m-0" lang="en">Currently User import is available for Plex only.</h3> </div>
  380. </div>
  381. ';
  382. $buttons = '';
  383. if (!empty($this->config['plexToken'])) {
  384. $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>';
  385. }
  386. if (!empty($this->config['jellyfinURL']) && !empty($this->config['jellyfinToken'])) {
  387. $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>';
  388. }
  389. if (!empty($this->config['embyURL']) && !empty($this->config['embyToken'])) {
  390. $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>';
  391. }
  392. return ($buttons !== '') ? $buttons : $emptyButtons;
  393. }
  394. public function getHomepageMediaImage()
  395. {
  396. $refresh = false;
  397. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  398. if (!file_exists($cacheDirectory)) {
  399. mkdir($cacheDirectory, 0777, true);
  400. }
  401. @$image_url = $_GET['img'];
  402. @$key = $_GET['key'];
  403. @$image_height = $_GET['height'];
  404. @$image_width = $_GET['width'];
  405. @$source = $_GET['source'];
  406. @$itemType = $_GET['type'];
  407. if (strpos($key, '$') !== false) {
  408. $key = explode('$', $key)[0];
  409. $refresh = true;
  410. }
  411. switch ($source) {
  412. case 'plex':
  413. $plexAddress = $this->qualifyURL($this->config['plexURL']);
  414. $image_src = $plexAddress . '/photo/:/transcode?height=' . $image_height . '&width=' . $image_width . '&upscale=1&url=' . $image_url . '&X-Plex-Token=' . $this->config['plexToken'];
  415. break;
  416. case 'emby':
  417. $embyAddress = $this->qualifyURL($this->config['embyURL']);
  418. $imgParams = array();
  419. if (isset($_GET['height'])) {
  420. $imgParams['height'] = 'maxHeight=' . $_GET['height'];
  421. }
  422. if (isset($_GET['width'])) {
  423. $imgParams['width'] = 'maxWidth=' . $_GET['width'];
  424. }
  425. $image_src = $embyAddress . '/Items/' . $image_url . '/Images/' . $itemType . '?' . implode('&', $imgParams);
  426. break;
  427. case 'jellyfin':
  428. $jellyfinAddress = $this->qualifyURL($this->config['jellyfinURL']);
  429. $imgParams = array();
  430. if (isset($_GET['height'])) {
  431. $imgParams['height'] = 'maxHeight=' . $_GET['height'];
  432. }
  433. if (isset($_GET['width'])) {
  434. $imgParams['width'] = 'maxWidth=' . $_GET['width'];
  435. }
  436. $image_src = $jellyfinAddress . '/Items/' . $image_url . '/Images/' . $itemType . '?' . implode('&', $imgParams);
  437. break;
  438. default:
  439. # code...
  440. break;
  441. }
  442. if (isset($image_url) && isset($image_height) && isset($image_width) && isset($image_src)) {
  443. $cachefile = $cacheDirectory . $key . '.jpg';
  444. $cachetime = 604800;
  445. // Serve from the cache if it is younger than $cachetime
  446. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile) && $refresh == false) {
  447. header("Content-type: image/jpeg");
  448. @readfile($cachefile);
  449. exit;
  450. }
  451. ob_start(); // Start the output buffer
  452. header('Content-type: image/jpeg');
  453. $options = array('verify' => false);
  454. $response = Requests::get($image_src, array(), $options);
  455. if ($response->success) {
  456. echo $response->body;
  457. }
  458. // Cache the output to a file
  459. $fp = fopen($cachefile, 'wb');
  460. fwrite($fp, ob_get_contents());
  461. fclose($fp);
  462. ob_end_flush(); // Send the output to the browser
  463. die();
  464. } else {
  465. die("Invalid Request");
  466. }
  467. }
  468. public function cacheImage($url, $name, $extension = 'jpg')
  469. {
  470. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  471. if (!file_exists($cacheDirectory)) {
  472. mkdir($cacheDirectory, 0777, true);
  473. }
  474. $cacheFile = $cacheDirectory . $name . '.' . $extension;
  475. $cacheTime = 604800;
  476. if ((file_exists($cacheFile) && (time() - $cacheTime) > filemtime($cacheFile)) || !file_exists($cacheFile)) {
  477. @copy($url, $cacheFile);
  478. }
  479. }
  480. public function checkFrame($array, $url)
  481. {
  482. if (array_key_exists("x-frame-options", $array)) {
  483. if (gettype($array['x-frame-options']) == 'array') {
  484. $array['x-frame-options'] = $array['x-frame-options'][0];
  485. }
  486. $array['x-frame-options'] = strtolower($array['x-frame-options']);
  487. if ($array['x-frame-options'] == "deny") {
  488. return false;
  489. } elseif ($array['x-frame-options'] == "sameorgin") {
  490. $digest = parse_url($url);
  491. $host = ($digest['host'] ?? '');
  492. if ($this->getServer() == $host) {
  493. return true;
  494. } else {
  495. return false;
  496. }
  497. } elseif (strpos($array['x-frame-options'], 'allow-from') !== false) {
  498. $explodeServers = explode(' ', $array['x-frame-options']);
  499. $allowed = false;
  500. foreach ($explodeServers as $server) {
  501. $digest = parse_url($server);
  502. $host = ($digest['host'] ?? '');
  503. if ($this->getServer() == $host) {
  504. $allowed = true;
  505. }
  506. }
  507. return $allowed;
  508. } else {
  509. return false;
  510. }
  511. } else {
  512. if (!$array) {
  513. return false;
  514. }
  515. return true;
  516. }
  517. }
  518. public function frameTest($url)
  519. {
  520. if (!$url || $url == '') {
  521. $this->setAPIResponse('error', 'URL not supplied', 404);
  522. return false;
  523. }
  524. $array = array_change_key_case(get_headers($this->qualifyURL($url), 1));
  525. $url = $this->qualifyURL($url);
  526. if ($this->checkFrame($array, $url)) {
  527. $this->setAPIResponse('success', 'URL approved for iFrame', 200);
  528. return true;
  529. } else {
  530. $this->setAPIResponse('error', 'URL failed approval for iFrame', 409);
  531. return false;
  532. }
  533. }
  534. public function groupSelect()
  535. {
  536. $groups = $this->getAllGroups();
  537. $select = array();
  538. foreach ($groups as $key => $value) {
  539. $select[] = array(
  540. 'name' => $value['group'],
  541. 'value' => $value['group_id']
  542. );
  543. }
  544. return $select;
  545. }
  546. public function showLogin()
  547. {
  548. if ($this->config['hideRegistration'] == false) {
  549. 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>';
  550. }
  551. }
  552. public function checkoAuth()
  553. {
  554. return $this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] !== 'internal';
  555. }
  556. public function checkoAuthOnly()
  557. {
  558. return $this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] == 'external';
  559. }
  560. public function showoAuth()
  561. {
  562. $buttons = '';
  563. if ($this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] !== 'internal') {
  564. $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>';
  565. }
  566. return ($buttons) ? '
  567. <div class="panel">
  568. <div class="panel-heading bg-org" id="plex-login-heading" role="tab">
  569. <a class="panel-title" data-toggle="collapse" href="#plex-login-collapse" data-parent="#login-panels" aria-expanded="false" aria-controls="organizr-login-collapse">
  570. <img class="lazyload loginTitle" data-src="plugins/images/tabs/plex.png"> &nbsp;
  571. <span class="text-uppercase fw300" lang="en">Login with Plex</span>
  572. </a>
  573. </div>
  574. <div class="panel-collapse collapse in" id="plex-login-collapse" aria-labelledby="plex-login-heading" role="tabpanel">
  575. <div class="panel-body">
  576. <div class="row">
  577. <div class="col-xs-12 col-sm-12 col-md-12 text-center">
  578. <div class="social m-b-0">' . $buttons . '</div>
  579. </div>
  580. </div>
  581. </div>
  582. </div>
  583. </div>
  584. ' : '';
  585. }
  586. public function logoOrText()
  587. {
  588. if ($this->config['useLogoLogin'] == false) {
  589. return '<h1>' . $this->config['title'] . '</h1>';
  590. } else {
  591. return '<img class="loginLogo" src="' . $this->config['loginLogo'] . '" alt="Home" />';
  592. }
  593. }
  594. public function settingsDocker()
  595. {
  596. $type = ($this->docker) ? 'Official Docker' : 'Native';
  597. 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>';
  598. }
  599. public function settingsPathChecks()
  600. {
  601. $paths = $this->pathsWritable($this->paths);
  602. $items = '';
  603. $type = (array_search(false, $paths)) ? 'Not Writable' : 'Writable';
  604. $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>';
  605. foreach ($paths as $k => $v) {
  606. $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>';
  607. }
  608. return $result . $items;
  609. }
  610. public function pathsWritable($paths)
  611. {
  612. $results = array();
  613. foreach ($paths as $k => $v) {
  614. $results[$k] = [
  615. 'writable' => is_writable($v),
  616. 'path' => $v
  617. ];
  618. }
  619. return $results;
  620. }
  621. public function clearTautulliTokens()
  622. {
  623. foreach (array_keys($_COOKIE) as $k => $v) {
  624. if (strpos($v, 'tautulli') !== false) {
  625. $this->coookie('delete', $v);
  626. }
  627. }
  628. }
  629. public function clearJellyfinTokens()
  630. {
  631. foreach (array_keys($_COOKIE) as $k => $v) {
  632. if (strpos($v, 'user-') !== false) {
  633. $this->coookie('delete', $v);
  634. }
  635. }
  636. $this->coookie('delete', 'jellyfin_credentials');
  637. }
  638. public function analyzeIP($ip)
  639. {
  640. if (strpos($ip, '/') !== false) {
  641. $explodeIP = explode('/', $ip);
  642. $prefix = $explodeIP[1];
  643. $start_ip = $explodeIP[0];
  644. $ip_count = 1 << (32 - $prefix);
  645. $start_ip_long = ip2long($start_ip);
  646. $last_ip_long = ip2long($start_ip) + $ip_count - 1;
  647. } elseif (substr_count($ip, '.') == 3) {
  648. $start_ip_long = ip2long($ip);
  649. $last_ip_long = ip2long($ip);
  650. }
  651. return (isset($start_ip_long) && isset($last_ip_long)) ? array('from' => $start_ip_long, 'to' => $last_ip_long) : false;
  652. }
  653. public function authProxyRangeCheck($from, $to)
  654. {
  655. $approved = false;
  656. $userIP = ip2long($_SERVER['REMOTE_ADDR']);
  657. $low = $from;
  658. $high = $to;
  659. if ($userIP <= $high && $low <= $userIP) {
  660. $approved = true;
  661. }
  662. return $approved;
  663. }
  664. public function userDefinedIdReplacementLink($link, $variables)
  665. {
  666. return strtr($link, $variables);
  667. }
  668. public function requestOptions($url, $override = false, $timeout = null, $extras = null)
  669. {
  670. $options = [];
  671. if ($extras) {
  672. if (gettype($extras) == 'array') {
  673. $options = array_merge($options, $extras);
  674. }
  675. }
  676. if (is_numeric($timeout)) {
  677. $timeout = $timeout / 1000;
  678. $options = array_merge($options, array('timeout' => $timeout));
  679. }
  680. if ($this->localURL($url, $override)) {
  681. $options = array_merge($options, array('verify' => false));
  682. }
  683. return $options;
  684. }
  685. }