organizr-functions.php 30 KB

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