4
0

organizr-functions.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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, $type = 'image')
  174. {
  175. $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
  176. if ($type == 'image') {
  177. switch ($ext) {
  178. case 'gif':
  179. case 'png':
  180. case 'jpeg':
  181. case 'jpg':
  182. case 'svg':
  183. return true;
  184. default:
  185. return false;
  186. }
  187. } elseif ($type == 'cert') {
  188. switch ($ext) {
  189. case 'pem':
  190. return true;
  191. default:
  192. return false;
  193. }
  194. }
  195. }
  196. public function getImages()
  197. {
  198. $allIconsPrep = array();
  199. $allIcons = array();
  200. $ignore = array(".", "..", "._.DS_Store", ".DS_Store", ".pydio_id", "index.html");
  201. $dirname = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'tabs' . DIRECTORY_SEPARATOR;
  202. $path = 'plugins/images/tabs/';
  203. $images = scandir($dirname);
  204. foreach ($images as $image) {
  205. if (!in_array($image, $ignore)) {
  206. $allIconsPrep[$image] = array(
  207. 'path' => $path,
  208. 'name' => $image
  209. );
  210. }
  211. }
  212. $dirname = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'userTabs' . DIRECTORY_SEPARATOR;
  213. $path = 'plugins/images/userTabs/';
  214. $images = scandir($dirname);
  215. foreach ($images as $image) {
  216. if (!in_array($image, $ignore)) {
  217. $allIconsPrep[$image] = array(
  218. 'path' => $path,
  219. 'name' => $image
  220. );
  221. }
  222. }
  223. ksort($allIconsPrep);
  224. foreach ($allIconsPrep as $item) {
  225. $allIcons[] = $item['path'] . $item['name'];
  226. }
  227. return $allIcons;
  228. }
  229. public function imageSelect($form)
  230. {
  231. $i = 1;
  232. $images = $this->getImages();
  233. $return = '<select class="form-control tabIconImageList" id="' . $form . '-chooseImage" name="chooseImage"><option lang="en">Select or type Icon</option>';
  234. foreach ($images as $image) {
  235. $i++;
  236. $return .= '<option value="' . $image . '">' . basename($image) . '</option>';
  237. }
  238. return $return . '</select>';
  239. }
  240. public function getThemes()
  241. {
  242. $themes = array();
  243. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . "*.css") as $filename) {
  244. $themes[] = array(
  245. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  246. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename))
  247. );
  248. }
  249. return $themes;
  250. }
  251. public function getSounds()
  252. {
  253. $sounds = array();
  254. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'sounds' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . "*.mp3") as $filename) {
  255. $sounds[] = array(
  256. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  257. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', 'plugins/sounds/default/' . basename($filename) . '.mp3')
  258. );
  259. }
  260. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'sounds' . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . "*.mp3") as $filename) {
  261. $sounds[] = array(
  262. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  263. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', 'plugins/sounds/custom/' . basename($filename) . '.mp3')
  264. );
  265. }
  266. return $sounds;
  267. }
  268. public function getBranches()
  269. {
  270. return array(
  271. array(
  272. 'name' => 'Develop',
  273. 'value' => 'v2-develop'
  274. ),
  275. array(
  276. 'name' => 'Master',
  277. 'value' => 'v2-master'
  278. )
  279. );
  280. }
  281. public function getSettingsTabs()
  282. {
  283. return array(
  284. array(
  285. 'name' => 'Tab Editor',
  286. 'value' => '0'
  287. ),
  288. array(
  289. 'name' => 'Customize',
  290. 'value' => '1'
  291. ),
  292. array(
  293. 'name' => 'User Management',
  294. 'value' => '2'
  295. ),
  296. array(
  297. 'name' => 'Image Manager',
  298. 'value' => '3'
  299. ),
  300. array(
  301. 'name' => 'Plugins',
  302. 'value' => '4'
  303. ),
  304. array(
  305. 'name' => 'System Settings',
  306. 'value' => '5'
  307. )
  308. );
  309. }
  310. public function getAuthTypes()
  311. {
  312. return array(
  313. array(
  314. 'name' => 'Organizr DB',
  315. 'value' => 'internal'
  316. ),
  317. array(
  318. 'name' => 'Organizr DB + Backend',
  319. 'value' => 'both'
  320. ),
  321. array(
  322. 'name' => 'Backend Only',
  323. 'value' => 'external'
  324. )
  325. );
  326. }
  327. public function getLDAPOptions()
  328. {
  329. return array(
  330. array(
  331. 'name' => 'Active Directory',
  332. 'value' => '1'
  333. ),
  334. array(
  335. 'name' => 'OpenLDAP',
  336. 'value' => '2'
  337. ),
  338. array(
  339. 'name' => 'Free IPA',
  340. 'value' => '3'
  341. ),
  342. );
  343. }
  344. public function getAuthBackends()
  345. {
  346. $backendOptions = array();
  347. $backendOptions[] = array(
  348. 'name' => 'Choose Backend',
  349. 'value' => false,
  350. 'disabled' => true
  351. );
  352. foreach (array_filter(get_class_methods('Organizr'), function ($v) {
  353. return strpos($v, 'plugin_auth_') === 0;
  354. }) as $value) {
  355. $name = str_replace('plugin_auth_', '', $value);
  356. if ($name == 'ldap') {
  357. if (!function_exists('ldap_connect')) {
  358. continue;
  359. }
  360. }
  361. if ($name == 'ldap_disabled') {
  362. if (function_exists('ldap_connect')) {
  363. continue;
  364. }
  365. }
  366. if (strpos($name, 'disabled') === false) {
  367. $backendOptions[] = array(
  368. 'name' => ucwords(str_replace('_', ' ', $name)),
  369. 'value' => $name
  370. );
  371. } else {
  372. $backendOptions[] = array(
  373. 'name' => $this->$value(),
  374. 'value' => 'none',
  375. 'disabled' => true,
  376. );
  377. }
  378. }
  379. ksort($backendOptions);
  380. return $backendOptions;
  381. }
  382. public function importUserButtons()
  383. {
  384. $emptyButtons = '
  385. <div class="col-md-12">
  386. <div class="white-box bg-org">
  387. <h3 class="box-title m-0" lang="en">Currently User import is available for Plex only.</h3> </div>
  388. </div>
  389. ';
  390. $buttons = '';
  391. if (!empty($this->config['plexToken'])) {
  392. $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>';
  393. }
  394. if (!empty($this->config['jellyfinURL']) && !empty($this->config['jellyfinToken'])) {
  395. $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>';
  396. }
  397. if (!empty($this->config['embyURL']) && !empty($this->config['embyToken'])) {
  398. $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>';
  399. }
  400. return ($buttons !== '') ? $buttons : $emptyButtons;
  401. }
  402. public function getHomepageMediaImage()
  403. {
  404. $refresh = false;
  405. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  406. if (!file_exists($cacheDirectory)) {
  407. mkdir($cacheDirectory, 0777, true);
  408. }
  409. @$image_url = $_GET['img'];
  410. @$key = $_GET['key'];
  411. @$image_height = $_GET['height'];
  412. @$image_width = $_GET['width'];
  413. @$source = $_GET['source'];
  414. @$itemType = $_GET['type'];
  415. if (strpos($key, '$') !== false) {
  416. $key = explode('$', $key)[0];
  417. $refresh = true;
  418. }
  419. switch ($source) {
  420. case 'plex':
  421. $plexAddress = $this->qualifyURL($this->config['plexURL']);
  422. $image_src = $plexAddress . '/photo/:/transcode?height=' . $image_height . '&width=' . $image_width . '&upscale=1&url=' . $image_url . '&X-Plex-Token=' . $this->config['plexToken'];
  423. break;
  424. case 'emby':
  425. $embyAddress = $this->qualifyURL($this->config['embyURL']);
  426. $imgParams = array();
  427. if (isset($_GET['height'])) {
  428. $imgParams['height'] = 'maxHeight=' . $_GET['height'];
  429. }
  430. if (isset($_GET['width'])) {
  431. $imgParams['width'] = 'maxWidth=' . $_GET['width'];
  432. }
  433. $image_src = $embyAddress . '/Items/' . $image_url . '/Images/' . $itemType . '?' . implode('&', $imgParams);
  434. break;
  435. case 'jellyfin':
  436. $jellyfinAddress = $this->qualifyURL($this->config['jellyfinURL']);
  437. $imgParams = array();
  438. if (isset($_GET['height'])) {
  439. $imgParams['height'] = 'maxHeight=' . $_GET['height'];
  440. }
  441. if (isset($_GET['width'])) {
  442. $imgParams['width'] = 'maxWidth=' . $_GET['width'];
  443. }
  444. $image_src = $jellyfinAddress . '/Items/' . $image_url . '/Images/' . $itemType . '?' . implode('&', $imgParams);
  445. break;
  446. default:
  447. # code...
  448. break;
  449. }
  450. if (isset($image_url) && isset($image_height) && isset($image_width) && isset($image_src)) {
  451. $cachefile = $cacheDirectory . $key . '.jpg';
  452. $cachetime = 604800;
  453. // Serve from the cache if it is younger than $cachetime
  454. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile) && $refresh == false) {
  455. header("Content-type: image/jpeg");
  456. @readfile($cachefile);
  457. exit;
  458. }
  459. ob_start(); // Start the output buffer
  460. header('Content-type: image/jpeg');
  461. $options = array('verify' => false);
  462. $response = Requests::get($image_src, array(), $options);
  463. if ($response->success) {
  464. echo $response->body;
  465. }
  466. // Cache the output to a file
  467. $fp = fopen($cachefile, 'wb');
  468. fwrite($fp, ob_get_contents());
  469. fclose($fp);
  470. ob_end_flush(); // Send the output to the browser
  471. die();
  472. } else {
  473. die("Invalid Request");
  474. }
  475. }
  476. public function cacheImage($url, $name, $extension = 'jpg')
  477. {
  478. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  479. if (!file_exists($cacheDirectory)) {
  480. mkdir($cacheDirectory, 0777, true);
  481. }
  482. $cacheFile = $cacheDirectory . $name . '.' . $extension;
  483. $cacheTime = 604800;
  484. if ((file_exists($cacheFile) && (time() - $cacheTime) > filemtime($cacheFile)) || !file_exists($cacheFile)) {
  485. @copy($url, $cacheFile);
  486. }
  487. }
  488. public function checkFrame($array, $url)
  489. {
  490. if (array_key_exists("x-frame-options", $array)) {
  491. if (gettype($array['x-frame-options']) == 'array') {
  492. $array['x-frame-options'] = $array['x-frame-options'][0];
  493. }
  494. $array['x-frame-options'] = strtolower($array['x-frame-options']);
  495. if ($array['x-frame-options'] == "deny") {
  496. return false;
  497. } elseif ($array['x-frame-options'] == "sameorgin") {
  498. $digest = parse_url($url);
  499. $host = ($digest['host'] ?? '');
  500. if ($this->getServer() == $host) {
  501. return true;
  502. } else {
  503. return false;
  504. }
  505. } elseif (strpos($array['x-frame-options'], 'allow-from') !== false) {
  506. $explodeServers = explode(' ', $array['x-frame-options']);
  507. $allowed = false;
  508. foreach ($explodeServers as $server) {
  509. $digest = parse_url($server);
  510. $host = ($digest['host'] ?? '');
  511. if ($this->getServer() == $host) {
  512. $allowed = true;
  513. }
  514. }
  515. return $allowed;
  516. } else {
  517. return false;
  518. }
  519. } else {
  520. if (!$array) {
  521. return false;
  522. }
  523. return true;
  524. }
  525. }
  526. public function frameTest($url)
  527. {
  528. if (!$url || $url == '') {
  529. $this->setAPIResponse('error', 'URL not supplied', 404);
  530. return false;
  531. }
  532. $array = array_change_key_case(get_headers($this->qualifyURL($url), 1));
  533. $url = $this->qualifyURL($url);
  534. if ($this->checkFrame($array, $url)) {
  535. $this->setAPIResponse('success', 'URL approved for iFrame', 200);
  536. return true;
  537. } else {
  538. $this->setAPIResponse('error', 'URL failed approval for iFrame', 409);
  539. return false;
  540. }
  541. }
  542. public function groupSelect()
  543. {
  544. $groups = $this->getAllGroups();
  545. $select = array();
  546. foreach ($groups as $key => $value) {
  547. $select[] = array(
  548. 'name' => $value['group'],
  549. 'value' => $value['group_id']
  550. );
  551. }
  552. return $select;
  553. }
  554. public function showLogin()
  555. {
  556. if ($this->config['hideRegistration'] == false) {
  557. 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>';
  558. }
  559. }
  560. public function checkoAuth()
  561. {
  562. return $this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] !== 'internal';
  563. }
  564. public function checkoAuthOnly()
  565. {
  566. return $this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] == 'external';
  567. }
  568. public function showoAuth()
  569. {
  570. $buttons = '';
  571. if ($this->config['plexoAuth'] && $this->config['authBackend'] == 'plex' && $this->config['authType'] !== 'internal') {
  572. $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>';
  573. }
  574. return ($buttons) ? '
  575. <div class="panel">
  576. <div class="panel-heading bg-org" id="plex-login-heading" role="tab">
  577. <a class="panel-title" data-toggle="collapse" href="#plex-login-collapse" data-parent="#login-panels" aria-expanded="false" aria-controls="organizr-login-collapse">
  578. <img class="lazyload loginTitle" data-src="plugins/images/tabs/plex.png"> &nbsp;
  579. <span class="text-uppercase fw300" lang="en">Login with Plex</span>
  580. </a>
  581. </div>
  582. <div class="panel-collapse collapse in" id="plex-login-collapse" aria-labelledby="plex-login-heading" role="tabpanel">
  583. <div class="panel-body">
  584. <div class="row">
  585. <div class="col-xs-12 col-sm-12 col-md-12 text-center">
  586. <div class="social m-b-0">' . $buttons . '</div>
  587. </div>
  588. </div>
  589. </div>
  590. </div>
  591. </div>
  592. ' : '';
  593. }
  594. public function logoOrText()
  595. {
  596. if ($this->config['useLogoLogin'] == false) {
  597. return '<h1>' . $this->config['title'] . '</h1>';
  598. } else {
  599. return '<img class="loginLogo" src="' . $this->config['loginLogo'] . '" alt="Home" />';
  600. }
  601. }
  602. public function settingsDocker()
  603. {
  604. $type = ($this->docker) ? 'Official Docker' : 'Native';
  605. 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>';
  606. }
  607. public function settingsPathChecks()
  608. {
  609. $paths = $this->pathsWritable($this->paths);
  610. $items = '';
  611. $type = (array_search(false, $paths)) ? 'Not Writable' : 'Writable';
  612. $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>';
  613. foreach ($paths as $k => $v) {
  614. $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>';
  615. }
  616. return $result . $items;
  617. }
  618. public function pathsWritable($paths)
  619. {
  620. $results = array();
  621. foreach ($paths as $k => $v) {
  622. $results[$k] = [
  623. 'writable' => is_writable($v),
  624. 'path' => $v
  625. ];
  626. }
  627. return $results;
  628. }
  629. public function clearTautulliTokens()
  630. {
  631. foreach (array_keys($_COOKIE) as $k => $v) {
  632. if (strpos($v, 'tautulli') !== false) {
  633. $this->coookie('delete', $v);
  634. }
  635. }
  636. }
  637. public function clearJellyfinTokens()
  638. {
  639. foreach (array_keys($_COOKIE) as $k => $v) {
  640. if (strpos($v, 'user-') !== false) {
  641. $this->coookie('delete', $v);
  642. }
  643. }
  644. $this->coookie('delete', 'jellyfin_credentials');
  645. }
  646. public function analyzeIP($ip)
  647. {
  648. if (strpos($ip, '/') !== false) {
  649. $explodeIP = explode('/', $ip);
  650. $prefix = $explodeIP[1];
  651. $start_ip = $explodeIP[0];
  652. $ip_count = 1 << (32 - $prefix);
  653. $start_ip_long = ip2long($start_ip);
  654. $last_ip_long = ip2long($start_ip) + $ip_count - 1;
  655. } elseif (substr_count($ip, '.') == 3) {
  656. $start_ip_long = ip2long($ip);
  657. $last_ip_long = ip2long($ip);
  658. }
  659. return (isset($start_ip_long) && isset($last_ip_long)) ? array('from' => $start_ip_long, 'to' => $last_ip_long) : false;
  660. }
  661. public function authProxyRangeCheck($from, $to)
  662. {
  663. $approved = false;
  664. $userIP = ip2long($_SERVER['REMOTE_ADDR']);
  665. $low = $from;
  666. $high = $to;
  667. if ($userIP <= $high && $low <= $userIP) {
  668. $approved = true;
  669. }
  670. return $approved;
  671. }
  672. public function userDefinedIdReplacementLink($link, $variables)
  673. {
  674. return strtr($link, $variables);
  675. }
  676. public function requestOptions($url, $timeout = null, $override = false, $customCertificate = false, $extras = null)
  677. {
  678. $options = [];
  679. if (is_numeric($timeout)) {
  680. if ($timeout >= 1000) {
  681. $timeout = $timeout / 1000;
  682. }
  683. $options = array_merge($options, array('timeout' => $timeout));
  684. }
  685. if ($customCertificate) {
  686. if ($this->hasCustomCert()) {
  687. $options = array_merge($options, array('verify' => $this->getCustomCert(), 'verifyname' => false));
  688. }
  689. }
  690. if ($this->localURL($url, $override)) {
  691. $options = array_merge($options, array('verify' => false, 'verifyname' => false));
  692. }
  693. if ($extras) {
  694. if (gettype($extras) == 'array') {
  695. $options = array_merge($options, $extras);
  696. }
  697. }
  698. return $options;
  699. }
  700. }