organizr-functions.php 21 KB

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