organizr-functions.php 26 KB

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