organizr-functions.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. <?php
  2. function organizrSpecialSettings()
  3. {
  4. $refreshSearch = "Refresh";
  5. $tautulliSearch = "tautulli_token";
  6. $tautulli = array_filter($_COOKIE, function ($k) use ($tautulliSearch) {
  7. return stripos($k, $tautulliSearch) !== false;
  8. }, ARRAY_FILTER_USE_KEY);
  9. return array(
  10. 'homepage' => array(
  11. 'refresh' => array_filter($GLOBALS, function ($k) use ($refreshSearch) {
  12. return stripos($k, $refreshSearch) !== false;
  13. }, ARRAY_FILTER_USE_KEY),
  14. 'search' => array(
  15. 'enabled' => (qualifyRequest($GLOBALS['mediaSearchAuth']) && $GLOBALS['mediaSearch'] == true && $GLOBALS['plexToken']) ? true : false,
  16. 'type' => $GLOBALS['mediaSearchType'],
  17. ),
  18. 'ombi' => array(
  19. 'enabled' => (qualifyRequest($GLOBALS['homepageOmbiAuth']) && qualifyRequest($GLOBALS['homepageOmbiRequestAuth']) && $GLOBALS['homepageOmbiEnabled'] == true && $GLOBALS['ssoOmbi'] && isset($_COOKIE['Auth'])) ? true : false,
  20. 'authView' => (qualifyRequest($GLOBALS['homepageOmbiAuth'])) ? true : false,
  21. 'authRequest' => (qualifyRequest($GLOBALS['homepageOmbiRequestAuth'])) ? true : false,
  22. 'sso' => ($GLOBALS['ssoOmbi']) ? true : false,
  23. 'cookie' => (isset($_COOKIE['Auth'])) ? true : false,
  24. ),
  25. 'options' => array(
  26. 'alternateHomepageHeaders' => $GLOBALS['alternateHomepageHeaders'],
  27. )
  28. ),
  29. 'sso' => array(
  30. 'plex' => array(
  31. 'enabled' => ($GLOBALS['ssoPlex']) ? true : false,
  32. 'cookie' => isset($_COOKIE['mpt']) ? true : false,
  33. ),
  34. 'ombi' => array(
  35. 'enabled' => ($GLOBALS['ssoOmbi']) ? true : false,
  36. 'cookie' => isset($_COOKIE['Auth']) ? true : false,
  37. ),
  38. 'tautulli' => array(
  39. 'enabled' => ($GLOBALS['ssoTautulli']) ? true : false,
  40. 'cookie' => !empty($tautulli) ? true : false,
  41. ),
  42. )
  43. );
  44. }
  45. function wizardConfig($array)
  46. {
  47. foreach ($array['data'] as $items) {
  48. foreach ($items as $key => $value) {
  49. if ($key == 'name') {
  50. $newKey = $value;
  51. }
  52. if ($key == 'value') {
  53. $newValue = $value;
  54. }
  55. if (isset($newKey) && isset($newValue)) {
  56. $$newKey = $newValue;
  57. }
  58. }
  59. }
  60. $location = cleanDirectory($location);
  61. $dbName = $dbName . '.db';
  62. $configVersion = $GLOBALS['installedVersion'];
  63. $configArray = array(
  64. 'dbName' => $dbName,
  65. 'dbLocation' => $location,
  66. 'license' => $license,
  67. 'organizrHash' => $hashKey,
  68. 'organizrAPI' => $api,
  69. 'registrationPassword' => $registrationPassword,
  70. );
  71. // Create Config
  72. $GLOBALS['dbLocation'] = $location;
  73. $GLOBALS['dbName'] = $dbName;
  74. if (createConfig($configArray)) {
  75. // Call DB Create
  76. if (createDB($location, $dbName)) {
  77. // Add in first user
  78. if (createFirstAdmin($location, $dbName, $username, $password, $email)) {
  79. if (createToken($username, $email, gravatar($email), 'Admin', 0, $hashKey, 1)) {
  80. return true;
  81. } else {
  82. return 'token';
  83. }
  84. } else {
  85. return 'admin';
  86. }
  87. } else {
  88. return 'db';
  89. }
  90. } else {
  91. return 'config';
  92. }
  93. return false;
  94. }
  95. function register($array)
  96. {
  97. // Grab username and password from login form
  98. foreach ($array['data'] as $items) {
  99. foreach ($items as $key => $value) {
  100. if ($key == 'name') {
  101. $newKey = $value;
  102. }
  103. if ($key == 'value') {
  104. $newValue = $value;
  105. }
  106. if (isset($newKey) && isset($newValue)) {
  107. $$newKey = $newValue;
  108. }
  109. }
  110. }
  111. if ($registrationPassword == $GLOBALS['registrationPassword']) {
  112. $defaults = defaultUserGroup();
  113. writeLog('success', 'Registration Function - Registration Password Verified', $username);
  114. if (createUser($username, $password, $defaults, $email)) {
  115. writeLog('success', 'Registration Function - A User has registered', $username);
  116. if (createToken($username, $email, gravatar($email), $defaults['group'], $defaults['group_id'], $GLOBALS['organizrHash'], 1)) {
  117. writeLoginLog($username, 'success');
  118. writeLog('success', 'Login Function - A User has logged in', $username);
  119. return true;
  120. }
  121. } else {
  122. writeLog('error', 'Registration Function - An error occured', $username);
  123. return 'username taken';
  124. }
  125. } else {
  126. writeLog('warning', 'Registration Function - Wrong Password', $username);
  127. return 'mismatch';
  128. }
  129. }
  130. function removeFile($array)
  131. {
  132. $filePath = $array['data']['path'];
  133. $fileName = $array['data']['name'];
  134. if (file_exists($filePath)) {
  135. if (unlink($filePath)) {
  136. writeLog('success', 'Log Management Function - Log: ' . $fileName . ' has been purged/deleted', 'SYSTEM');
  137. return true;
  138. } else {
  139. writeLog('error', 'Log Management Function - Log: ' . $fileName . ' - Error Occured', 'SYSTEM');
  140. return false;
  141. }
  142. } else {
  143. writeLog('error', 'Log Management Function - Log: ' . $fileName . ' does not exist', 'SYSTEM');
  144. return false;
  145. }
  146. }
  147. function recover($array)
  148. {
  149. $email = $array['data']['email'];
  150. $newPassword = randString(10);
  151. try {
  152. $connect = new Dibi\Connection([
  153. 'driver' => 'sqlite3',
  154. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  155. ]);
  156. $isUser = $connect->fetch('SELECT * FROM users WHERE email = ? COLLATE NOCASE', $email);
  157. if ($isUser) {
  158. $connect->query('
  159. UPDATE users SET', [
  160. 'password' => password_hash($newPassword, PASSWORD_BCRYPT)
  161. ], '
  162. WHERE email=? COLLATE NOCASE', $email);
  163. if ($GLOBALS['PHPMAILER-enabled']) {
  164. $emailTemplate = array(
  165. 'type' => 'reset',
  166. 'body' => $GLOBALS['PHPMAILER-emailTemplateResetPassword'],
  167. 'subject' => $GLOBALS['PHPMAILER-emailTemplateResetPasswordSubject'],
  168. 'user' => $isUser['username'],
  169. 'password' => $newPassword,
  170. 'inviteCode' => null,
  171. );
  172. $emailTemplate = phpmEmailTemplate($emailTemplate);
  173. $sendEmail = array(
  174. 'to' => $email,
  175. 'user' => $isUser['username'],
  176. 'subject' => $emailTemplate['subject'],
  177. 'body' => phpmBuildEmail($emailTemplate),
  178. );
  179. phpmSendEmail($sendEmail);
  180. }
  181. writeLog('success', 'User Management Function - User: ' . $isUser['username'] . '\'s password was reset', $isUser['username']);
  182. return true;
  183. } else {
  184. writeLog('error', 'User Management Function - Error - User: ' . $email . ' An error Occured', $email);
  185. return 'an error occured';
  186. }
  187. } catch (Dibi\Exception $e) {
  188. writeLog('error', 'User Management Function - Error - User: ' . $email . ' An error Occured', $email);
  189. return 'an error occured';
  190. }
  191. }
  192. function editUser($array)
  193. {
  194. if ($array['data']['username'] == '' && $array['data']['username'] == '') {
  195. return 'Username/email not set';
  196. }
  197. try {
  198. $connect = new Dibi\Connection([
  199. 'driver' => 'sqlite3',
  200. 'database' => $GLOBALS['dbLocation'] . $GLOBALS['dbName'],
  201. ]);
  202. if (!usernameTakenExcept($array['data']['username'], $array['data']['email'], $GLOBALS['organizrUser']['userID'])) {
  203. $connect->query('
  204. UPDATE users SET', [
  205. 'username' => $array['data']['username'],
  206. 'email' => $array['data']['email'],
  207. ], '
  208. WHERE id=?', $GLOBALS['organizrUser']['userID']);
  209. if (!empty($array['data']['password'])) {
  210. $connect->query('
  211. UPDATE users SET', [
  212. 'password' => password_hash($array['data']['password'], PASSWORD_BCRYPT)
  213. ], '
  214. WHERE id=?', $GLOBALS['organizrUser']['userID']);
  215. }
  216. writeLog('success', 'User Management Function - User: ' . $array['data']['username'] . '\'s info was changed', $GLOBALS['organizrUser']['username']);
  217. return true;
  218. } else {
  219. return 'Username/Email Already Taken';
  220. }
  221. } catch (Dibi\Exception $e) {
  222. writeLog('error', 'User Management Function - Error - User: ' . $array['data']['username'] . ' An error Occured', $GLOBALS['organizrUser']['username']);
  223. return 'an error occured';
  224. }
  225. }
  226. function logout()
  227. {
  228. coookie('delete', 'organizrToken');
  229. coookie('delete', 'mpt');
  230. coookie('delete', 'Auth');
  231. $GLOBALS['organizrUser'] = false;
  232. return true;
  233. }
  234. function qualifyRequest($accessLevelNeeded)
  235. {
  236. if (getUserLevel() <= $accessLevelNeeded) {
  237. return true;
  238. } else {
  239. return false;
  240. }
  241. }
  242. function getUserLevel()
  243. {
  244. $requesterToken = isset(getallheaders()['Token']) ? getallheaders()['Token'] : false;
  245. // Check token or API key
  246. // If API key, return 0 for admin
  247. if (strlen($requesterToken) == 20 && $requesterToken == $GLOBALS['organizrAPI']) {
  248. //DO API CHECK
  249. return 0;
  250. } elseif (isset($GLOBALS['organizrUser'])) {
  251. return $GLOBALS['organizrUser']['groupID'];
  252. }
  253. // All else fails? return guest id
  254. return 999;
  255. }
  256. function organizrStatus()
  257. {
  258. $status = array();
  259. $dependenciesActive = array();
  260. $dependenciesInactive = array();
  261. $extensions = array("PDO_SQLITE", "PDO", "SQLITE3", "zip", "cURL", "openssl", "simplexml", "json", "session");
  262. $functions = array("hash", "fopen", "fsockopen", "fwrite", "fclose", "readfile");
  263. foreach ($extensions as $check) {
  264. if (extension_loaded($check)) {
  265. array_push($dependenciesActive, $check);
  266. } else {
  267. array_push($dependenciesInactive, $check);
  268. }
  269. }
  270. foreach ($functions as $check) {
  271. if (function_exists($check)) {
  272. array_push($dependenciesActive, $check);
  273. } else {
  274. array_push($dependenciesInactive, $check);
  275. }
  276. }
  277. if (!file_exists('config' . DIRECTORY_SEPARATOR . 'config.php')) {
  278. $status['status'] = "wizard";//wizard - ok for test
  279. }
  280. if (count($dependenciesInactive) > 0 || !is_writable(dirname(__DIR__, 2)) || !(version_compare(PHP_VERSION, '7.0.0') >= 0)) {
  281. $status['status'] = "dependencies";
  282. }
  283. $status['status'] = (!empty($status['status'])) ? $status['status'] : $status['status'] = "ok";
  284. $status['writable'] = is_writable(dirname(__DIR__, 2)) ? 'yes' : 'no';
  285. $status['minVersion'] = (version_compare(PHP_VERSION, '7.0.0') >= 0) ? 'yes' : 'no';
  286. $status['dependenciesActive'] = $dependenciesActive;
  287. $status['dependenciesInactive'] = $dependenciesInactive;
  288. $status['version'] = $GLOBALS['installedVersion'];
  289. $status['os'] = getOS();
  290. $status['php'] = phpversion();
  291. return $status;
  292. }
  293. function getSettingsMain()
  294. {
  295. return array(
  296. 'Github' => array(
  297. array(
  298. 'type' => 'select',
  299. 'name' => 'branch',
  300. 'label' => 'Branch',
  301. 'value' => $GLOBALS['branch'],
  302. 'options' => getBranches()
  303. ),
  304. array(
  305. 'type' => 'button',
  306. 'label' => 'Force Install Branch',
  307. 'class' => 'updateNow',
  308. 'icon' => 'fa fa-download',
  309. 'text' => 'Retrieve'
  310. )
  311. ),
  312. 'API' => array(
  313. array(
  314. 'type' => 'password-alt',
  315. 'name' => 'organizrAPI',
  316. 'label' => 'Organizr API',
  317. 'value' => $GLOBALS['organizrAPI']
  318. ),
  319. array(
  320. 'type' => 'button',
  321. 'label' => 'Generate New API Key',
  322. 'class' => 'newAPIKey',
  323. 'icon' => 'fa fa-refresh',
  324. 'text' => 'Generate'
  325. )
  326. ),
  327. 'Authentication' => array(
  328. array(
  329. 'type' => 'select',
  330. 'name' => 'authType',
  331. 'id' => 'authSelect',
  332. 'label' => 'Authentication Type',
  333. 'value' => $GLOBALS['authType'],
  334. 'options' => getAuthTypes()
  335. ),
  336. array(
  337. 'type' => 'select',
  338. 'name' => 'authBackend',
  339. 'id' => 'authBackendSelect',
  340. 'label' => 'Authentication Backend',
  341. 'class' => 'backendAuth switchAuth',
  342. 'value' => $GLOBALS['authBackend'],
  343. 'options' => getAuthBackends()
  344. ),
  345. array(
  346. 'type' => 'password-alt',
  347. 'name' => 'plexToken',
  348. 'class' => 'plexAuth switchAuth',
  349. 'label' => 'Plex Token',
  350. 'value' => $GLOBALS['plexToken'],
  351. 'placeholder' => 'Use Get Token Button'
  352. ),
  353. array(
  354. 'type' => 'button',
  355. 'label' => 'Get Plex Token',
  356. 'class' => 'popup-with-form getPlexTokenAuth plexAuth switchAuth',
  357. 'icon' => 'fa fa-ticket',
  358. 'text' => 'Retrieve',
  359. 'href' => '#auth-plex-token-form',
  360. 'attr' => 'data-effect="mfp-3d-unfold"'
  361. ),
  362. array(
  363. 'type' => 'password-alt',
  364. 'name' => 'plexID',
  365. 'class' => 'plexAuth switchAuth',
  366. 'label' => 'Plex Machine',
  367. 'value' => $GLOBALS['plexID'],
  368. 'placeholder' => 'Use Get Plex Machine Button'
  369. ),
  370. array(
  371. 'type' => 'button',
  372. 'label' => 'Get Plex Machine',
  373. 'class' => 'popup-with-form getPlexMachineAuth plexAuth switchAuth',
  374. 'icon' => 'fa fa-id-badge',
  375. 'text' => 'Retrieve',
  376. 'href' => '#auth-plex-machine-form',
  377. 'attr' => 'data-effect="mfp-3d-unfold"'
  378. ),
  379. array(
  380. 'type' => 'input',
  381. 'name' => 'authBackendHost',
  382. 'class' => 'ldapAuth ftpAuth switchAuth',
  383. 'label' => 'Host Address',
  384. 'value' => $GLOBALS['authBackendHost'],
  385. 'placeholder' => 'http{s) | ftp(s) | ldap(s)://hostname:port'
  386. ),
  387. array(
  388. 'type' => 'input',
  389. 'name' => 'authBaseDN',
  390. 'class' => 'ldapAuth switchAuth',
  391. 'label' => 'Host Base DN',
  392. 'value' => $GLOBALS['authBaseDN'],
  393. 'placeholder' => 'cn=%s,dc=sub,dc=domain,dc=com'
  394. ),
  395. array(
  396. 'type' => 'input',
  397. 'name' => 'embyURL',
  398. 'class' => 'embyAuth switchAuth',
  399. 'label' => 'Emby URL',
  400. 'value' => $GLOBALS['embyURL'],
  401. 'placeholder' => 'http(s)://hostname:port'
  402. ),
  403. array(
  404. 'type' => 'password-alt',
  405. 'name' => 'embyToken',
  406. 'class' => 'embyAuth switchAuth',
  407. 'label' => 'Emby Token',
  408. 'value' => $GLOBALS['embyToken'],
  409. 'placeholder' => ''
  410. )
  411. /*array(
  412. 'type' => 'button',
  413. 'label' => 'Send Test',
  414. 'class' => 'phpmSendTestEmail',
  415. 'icon' => 'fa fa-paper-plane',
  416. 'text' => 'Send'
  417. )*/
  418. ),
  419. 'Misc' => array(
  420. array(
  421. 'type' => 'password-alt',
  422. 'name' => 'registrationPassword',
  423. 'label' => 'Registration Password',
  424. 'value' => $GLOBALS['registrationPassword'],
  425. ),
  426. array(
  427. 'type' => 'switch',
  428. 'name' => 'hideRegistration',
  429. 'label' => 'Hide Registration',
  430. 'value' => $GLOBALS['hideRegistration']
  431. )
  432. )
  433. );
  434. }
  435. function getSSO()
  436. {
  437. return array(
  438. 'Plex' => array(
  439. array(
  440. 'type' => 'password-alt',
  441. 'name' => 'plexToken',
  442. 'label' => 'Plex Token',
  443. 'value' => $GLOBALS['plexToken'],
  444. 'placeholder' => 'Use Get Token Button'
  445. ),
  446. array(
  447. 'type' => 'button',
  448. 'label' => 'Get Plex Token',
  449. 'class' => 'popup-with-form getPlexTokenSSO',
  450. 'icon' => 'fa fa-ticket',
  451. 'text' => 'Retrieve',
  452. 'href' => '#sso-plex-token-form',
  453. 'attr' => 'data-effect="mfp-3d-unfold"'
  454. ),
  455. array(
  456. 'type' => 'password-alt',
  457. 'name' => 'plexID',
  458. 'label' => 'Plex Machine',
  459. 'value' => $GLOBALS['plexID'],
  460. 'placeholder' => 'Use Get Plex Machine Button'
  461. ),
  462. array(
  463. 'type' => 'button',
  464. 'label' => 'Get Plex Machine',
  465. 'class' => 'popup-with-form getPlexMachineSSO',
  466. 'icon' => 'fa fa-id-badge',
  467. 'text' => 'Retrieve',
  468. 'href' => '#sso-plex-machine-form',
  469. 'attr' => 'data-effect="mfp-3d-unfold"'
  470. ),
  471. array(
  472. 'type' => 'input',
  473. 'name' => 'plexAdmin',
  474. 'label' => 'Admin Username',
  475. 'value' => $GLOBALS['plexAdmin'],
  476. 'placeholder' => 'Admin username for Plex'
  477. ),
  478. array(
  479. 'type' => 'blank',
  480. 'label' => ''
  481. ),
  482. array(
  483. 'type' => 'html',
  484. 'label' => 'Plex Note',
  485. 'html' => '<span lang="en">Please make sure both Token and Machine are filled in</span>'
  486. ),
  487. array(
  488. 'type' => 'switch',
  489. 'name' => 'ssoPlex',
  490. 'label' => 'Enable',
  491. 'value' => $GLOBALS['ssoPlex']
  492. )
  493. ),
  494. 'Ombi' => array(
  495. array(
  496. 'type' => 'input',
  497. 'name' => 'ombiURL',
  498. 'label' => 'Ombi URL',
  499. 'value' => $GLOBALS['ombiURL'],
  500. 'placeholder' => 'http(s)://hostname:port'
  501. ),
  502. array(
  503. 'type' => 'switch',
  504. 'name' => 'ssoOmbi',
  505. 'label' => 'Enable',
  506. 'value' => $GLOBALS['ssoOmbi']
  507. )
  508. ),
  509. 'Tautulli' => array(
  510. array(
  511. 'type' => 'input',
  512. 'name' => 'tautulliURL',
  513. 'label' => 'Tautulli URL',
  514. 'value' => $GLOBALS['tautulliURL'],
  515. 'placeholder' => 'http(s)://hostname:port'
  516. ),
  517. array(
  518. 'type' => 'switch',
  519. 'name' => 'ssoTautulli',
  520. 'label' => 'Enable',
  521. 'value' => $GLOBALS['ssoTautulli']
  522. )
  523. )
  524. );
  525. }
  526. function loadAppearance()
  527. {
  528. $appearance = array();
  529. $appearance['logo'] = $GLOBALS['logo'];
  530. $appearance['title'] = $GLOBALS['title'];
  531. $appearance['useLogo'] = $GLOBALS['useLogo'];
  532. $appearance['headerColor'] = $GLOBALS['headerColor'];
  533. $appearance['headerTextColor'] = $GLOBALS['headerTextColor'];
  534. $appearance['sidebarColor'] = $GLOBALS['sidebarColor'];
  535. $appearance['headerTextColor'] = $GLOBALS['headerTextColor'];
  536. $appearance['sidebarTextColor'] = $GLOBALS['sidebarTextColor'];
  537. $appearance['accentColor'] = $GLOBALS['accentColor'];
  538. $appearance['accentTextColor'] = $GLOBALS['accentTextColor'];
  539. $appearance['buttonColor'] = $GLOBALS['buttonColor'];
  540. $appearance['buttonTextColor'] = $GLOBALS['buttonTextColor'];
  541. $appearance['buttonTextHoverColor'] = $GLOBALS['buttonTextHoverColor'];
  542. $appearance['buttonHoverColor'] = $GLOBALS['buttonHoverColor'];
  543. $appearance['loginWallpaper'] = $GLOBALS['loginWallpaper'];
  544. $appearance['customCss'] = $GLOBALS['customCss'];
  545. return $appearance;
  546. }
  547. function getCustomizeAppearance()
  548. {
  549. if (file_exists(dirname(__DIR__, 1) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php')) {
  550. return array(
  551. 'Top Bar' => array(
  552. array(
  553. 'type' => 'input',
  554. 'name' => 'logo',
  555. 'label' => 'Logo',
  556. 'value' => $GLOBALS['logo']
  557. ),
  558. array(
  559. 'type' => 'input',
  560. 'name' => 'title',
  561. 'label' => 'Title',
  562. 'value' => $GLOBALS['title']
  563. ),
  564. array(
  565. 'type' => 'switch',
  566. 'name' => 'useLogo',
  567. 'label' => 'Use Logo instead of Title',
  568. 'value' => $GLOBALS['useLogo']
  569. )
  570. ),
  571. 'Login Page' => array(
  572. array(
  573. 'type' => 'input',
  574. 'name' => 'loginWallpaper',
  575. 'label' => 'Login Wallpaper',
  576. 'value' => $GLOBALS['loginWallpaper']
  577. )
  578. ),
  579. 'Options' => array(
  580. array(
  581. 'type' => 'switch',
  582. 'name' => 'alternateHomepageHeaders',
  583. 'label' => 'Alternate Homepage Titles',
  584. 'value' => $GLOBALS['alternateHomepageHeaders']
  585. )
  586. ),
  587. 'Colors & Themes' => array(
  588. array(
  589. 'type' => 'html',
  590. 'override' => 12,
  591. 'label' => 'Custom CSS [Can replace colors from above]',
  592. 'html' => '
  593. <div class="row">
  594. <div class="col-lg-12">
  595. <div class="panel panel-info">
  596. <div class="panel-heading">
  597. <span lang="en">Notice</span>
  598. </div>
  599. <div class="panel-wrapper collapse in" aria-expanded="true">
  600. <div class="panel-body">
  601. <span lang="en">The value of #987654 is just a placeholder, you can change to any value you like.</span>
  602. </div>
  603. </div>
  604. </div>
  605. </div>
  606. </div>
  607. ',
  608. ),
  609. array(
  610. 'type' => 'blank',
  611. 'label' => ''
  612. ),
  613. array(
  614. 'type' => 'input',
  615. 'name' => 'headerColor',
  616. 'label' => 'Nav Bar Color',
  617. 'value' => $GLOBALS['headerColor'],
  618. 'class' => 'pick-a-color',
  619. 'attr' => 'data-original="' . $GLOBALS['headerColor'] . '"'
  620. ),
  621. array(
  622. 'type' => 'input',
  623. 'name' => 'headerTextColor',
  624. 'label' => 'Nav Bar Text Color',
  625. 'value' => $GLOBALS['headerTextColor'],
  626. 'class' => 'pick-a-color',
  627. 'attr' => 'data-original="' . $GLOBALS['headerTextColor'] . '"'
  628. ),
  629. array(
  630. 'type' => 'input',
  631. 'name' => 'sidebarColor',
  632. 'label' => 'Side Bar Color',
  633. 'value' => $GLOBALS['sidebarColor'],
  634. 'class' => 'pick-a-color',
  635. 'attr' => 'data-original="' . $GLOBALS['sidebarColor'] . '"'
  636. ),
  637. array(
  638. 'type' => 'input',
  639. 'name' => 'sidebarTextColor',
  640. 'label' => 'Side Bar Text Color',
  641. 'value' => $GLOBALS['sidebarTextColor'],
  642. 'class' => 'pick-a-color',
  643. 'attr' => 'data-original="' . $GLOBALS['sidebarTextColor'] . '"'
  644. ),
  645. array(
  646. 'type' => 'input',
  647. 'name' => 'accentColor',
  648. 'label' => 'Accent Color',
  649. 'value' => $GLOBALS['accentColor'],
  650. 'class' => 'pick-a-color',
  651. 'attr' => 'data-original="' . $GLOBALS['accentColor'] . '"'
  652. ),
  653. array(
  654. 'type' => 'input',
  655. 'name' => 'accentTextColor',
  656. 'label' => 'Accent Text Color',
  657. 'value' => $GLOBALS['accentTextColor'],
  658. 'class' => 'pick-a-color',
  659. 'attr' => 'data-original="' . $GLOBALS['accentTextColor'] . '"'
  660. ),
  661. array(
  662. 'type' => 'input',
  663. 'name' => 'buttonColor',
  664. 'label' => 'Button Color',
  665. 'value' => $GLOBALS['buttonColor'],
  666. 'class' => 'pick-a-color',
  667. 'attr' => 'data-original="' . $GLOBALS['buttonColor'] . '"'
  668. ),
  669. array(
  670. 'type' => 'input',
  671. 'name' => 'buttonTextColor',
  672. 'label' => 'Button Text Color',
  673. 'value' => $GLOBALS['buttonTextColor'],
  674. 'class' => 'pick-a-color',
  675. 'attr' => 'data-original="' . $GLOBALS['buttonTextColor'] . '"'
  676. ),/*
  677. array(
  678. 'type' => 'input',
  679. 'name' => 'buttonHoverColor',
  680. 'label' => 'Button Hover Color',
  681. 'value' => $GLOBALS['buttonHoverColor'],
  682. 'class' => 'pick-a-color',
  683. 'disabled' => true
  684. ),
  685. array(
  686. 'type' => 'input',
  687. 'name' => 'buttonTextHoverColor',
  688. 'label' => 'Button Hover Text Color',
  689. 'value' => $GLOBALS['buttonTextHoverColor'],
  690. 'class' => 'pick-a-color',
  691. 'disabled' => true
  692. ),*/
  693. array(
  694. 'type' => 'select',
  695. 'name' => 'theme',
  696. 'label' => 'Theme',
  697. 'class' => 'themeChanger',
  698. 'value' => $GLOBALS['theme'],
  699. 'options' => getThemes()
  700. ),
  701. array(
  702. 'type' => 'select',
  703. 'name' => 'style',
  704. 'label' => 'Style',
  705. 'class' => 'styleChanger',
  706. 'value' => $GLOBALS['style'],
  707. 'options' => array(
  708. array(
  709. 'name' => 'Light',
  710. 'value' => 'light'
  711. ),
  712. array(
  713. 'name' => 'Dark',
  714. 'value' => 'dark'
  715. ),
  716. array(
  717. 'name' => 'Horizontal',
  718. 'value' => 'horizontal'
  719. )
  720. )
  721. )
  722. ),
  723. 'Custom CSS' => array(
  724. array(
  725. 'type' => 'html',
  726. 'override' => 12,
  727. 'label' => 'Custom CSS [Can replace colors from above]',
  728. 'html' => '<button type="button" class="hidden saveCss btn btn-info btn-circle pull-right m-r-5 m-l-10"><i class="fa fa-save"></i> </button><div id="customCSSEditor" style="height:300px">' . $GLOBALS['customCss'] . '</div>'
  729. ),
  730. array(
  731. 'type' => 'textbox',
  732. 'name' => 'customCss',
  733. 'class' => 'hidden cssTextarea',
  734. 'label' => '',
  735. 'value' => $GLOBALS['customCss'],
  736. 'placeholder' => 'No <style> tags needed',
  737. 'attr' => 'rows="10"',
  738. ),
  739. )
  740. );
  741. }
  742. }
  743. function editAppearance($array)
  744. {
  745. switch ($array['data']['value']) {
  746. case 'true':
  747. $array['data']['value'] = (bool)true;
  748. break;
  749. case 'false':
  750. $array['data']['value'] = (bool)false;
  751. break;
  752. default:
  753. $array['data']['value'] = $array['data']['value'];
  754. }
  755. //return gettype($array['data']['value']).' - '.$array['data']['value'];
  756. switch ($array['data']['action']) {
  757. case 'editCustomizeAppearance':
  758. $newItem = array(
  759. $array['data']['name'] => $array['data']['value']
  760. );
  761. return (updateConfig($newItem)) ? true : false;
  762. break;
  763. default:
  764. # code...
  765. break;
  766. }
  767. }
  768. function updateConfigMultiple($array)
  769. {
  770. return (updateConfig($array['data']['payload'])) ? true : false;
  771. }
  772. function updateConfigMultipleForm($array)
  773. {
  774. $newItem = array();
  775. foreach ($array['data']['payload'] as $k => $v) {
  776. switch ($v['value']) {
  777. case 'true':
  778. $v['value'] = (bool)true;
  779. break;
  780. case 'false':
  781. $v['value'] = (bool)false;
  782. break;
  783. default:
  784. $v['value'] = $v['value'];
  785. }
  786. // Hash
  787. if ($v['type'] == 'password') {
  788. if (strpos($v['value'], '==') !== false) {
  789. $v['value'] = $v['value'];
  790. } else {
  791. $v['value'] = encrypt($v['value']);
  792. }
  793. }
  794. $newItem[$v['name']] = $v['value'];
  795. }
  796. //return $newItem;
  797. return (updateConfig($newItem)) ? true : false;
  798. }
  799. function updateConfigItem($array)
  800. {
  801. switch ($array['data']['value']) {
  802. case 'true':
  803. $array['data']['value'] = (bool)true;
  804. break;
  805. case 'false':
  806. $array['data']['value'] = (bool)false;
  807. break;
  808. default:
  809. $array['data']['value'] = $array['data']['value'];
  810. }
  811. // Hash
  812. if ($array['data']['type'] == 'password') {
  813. $array['data']['value'] = encrypt($array['data']['value']);
  814. }
  815. $newItem = array(
  816. $array['data']['name'] => $array['data']['value']
  817. );
  818. return (updateConfig($newItem)) ? true : false;
  819. }
  820. function getPlugins()
  821. {
  822. if (file_exists(dirname(__DIR__, 1) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'config.php')) {
  823. $pluginList = [];
  824. foreach ($GLOBALS['plugins'] as $plugin) {
  825. foreach ($plugin as $key => $value) {
  826. if (strpos($value['license'], $GLOBALS['license']) !== false) {
  827. $plugin[$key]['enabled'] = $GLOBALS[$value['configPrefix'] . '-enabled'];
  828. $pluginList[$key] = $plugin[$key];
  829. }
  830. }
  831. }
  832. return $pluginList;
  833. }
  834. return false;
  835. }
  836. function editPlugins($array)
  837. {
  838. switch ($array['data']['action']) {
  839. case 'enable':
  840. $newItem = array(
  841. $array['data']['configName'] => true
  842. );
  843. writeLog('success', 'Plugin Function - Enabled Plugin [' . $_POST['data']['name'] . ']', $GLOBALS['organizrUser']['username']);
  844. return (updateConfig($newItem)) ? true : false;
  845. break;
  846. case 'disable':
  847. $newItem = array(
  848. $array['data']['configName'] => false
  849. );
  850. writeLog('success', 'Plugin Function - Disabled Plugin [' . $_POST['data']['name'] . ']', $GLOBALS['organizrUser']['username']);
  851. return (updateConfig($newItem)) ? true : false;
  852. break;
  853. default:
  854. # code...
  855. break;
  856. }
  857. }
  858. function auth()
  859. {
  860. $debug = false; // CAREFUL WHEN SETTING TO TRUE AS THIS OPENS AUTH UP
  861. $ban = isset($_GET['ban']) ? strtoupper($_GET['ban']) : "";
  862. $whitelist = isset($_GET['whitelist']) ? $_GET['whitelist'] : false;
  863. $blacklist = isset($_GET['blacklist']) ? $_GET['blacklist'] : false;
  864. $group = isset($_GET['group']) ? (int)$_GET['group'] : (int)0;
  865. $currentIP = userIP();
  866. if (isset($GLOBALS['organizrUser'])) {
  867. $currentUser = $GLOBALS['organizrUser']['username'];
  868. $currentGroup = $GLOBALS['organizrUser']['groupID'];
  869. } else {
  870. $currentUser = 'Guest';
  871. $currentGroup = getUserLevel();
  872. }
  873. $userInfo = "User: $currentUser | Group: $currentGroup | IP: $currentIP | Requesting Access to Group $group | Result: ";
  874. if ($whitelist) {
  875. if (in_array($currentIP, arrayIP($whitelist))) {
  876. !$debug ? exit(http_response_code(200)) : die("$userInfo Whitelist Authorized");
  877. }
  878. }
  879. if ($blacklist) {
  880. if (in_array($currentIP, arrayIP($blacklist))) {
  881. !$debug ? exit(http_response_code(401)) : die("$userInfo Blacklisted");
  882. }
  883. }
  884. if ($group !== null) {
  885. if (qualifyRequest($group)) {
  886. !$debug ? exit(http_response_code(200)) : die("$userInfo Authorized");
  887. } else {
  888. !$debug ? exit(http_response_code(401)) : die("$userInfo Not Authorized");
  889. }
  890. } else {
  891. !$debug ? exit(http_response_code(401)) : die("Not Authorized Due To No Parameters Set");
  892. }
  893. }
  894. function logoOrText()
  895. {
  896. if ($GLOBALS['useLogo'] == false) {
  897. return '<h1>' . $GLOBALS['title'] . '</h1>';
  898. } else {
  899. return '<img class="loginLogo" src="' . $GLOBALS['logo'] . '" alt="Home" />';
  900. }
  901. }
  902. function showLogin()
  903. {
  904. if ($GLOBALS['hideRegistration'] == false) {
  905. 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>';
  906. }
  907. }
  908. function getImages()
  909. {
  910. $dirname = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'tabs' . DIRECTORY_SEPARATOR;
  911. $path = 'plugins/images/tabs/';
  912. $images = scandir($dirname);
  913. $ignore = array(".", "..", "._.DS_Store", ".DS_Store", ".pydio_id");
  914. $allIcons = array();
  915. foreach ($images as $image) {
  916. if (!in_array($image, $ignore)) {
  917. $allIcons[] = $path . $image;
  918. }
  919. }
  920. return $allIcons;
  921. }
  922. function editImages()
  923. {
  924. $array = array();
  925. $postCheck = array_filter($_POST);
  926. $filesCheck = array_filter($_FILES);
  927. if (!empty($postCheck)) {
  928. if ($_POST['data']['action'] == 'deleteImage') {
  929. if (file_exists(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . $_POST['data']['imagePath'])) {
  930. writeLog('success', 'Image Manager Function - Deleted Image [' . $_POST['data']['imageName'] . ']', $GLOBALS['organizrUser']['username']);
  931. return (unlink(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . $_POST['data']['imagePath'])) ? true : false;
  932. }
  933. }
  934. }
  935. if (!empty($filesCheck)) {
  936. ini_set('upload_max_filesize', '10M');
  937. ini_set('post_max_size', '10M');
  938. $tempFile = $_FILES['file']['tmp_name'];
  939. $targetPath = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'tabs' . DIRECTORY_SEPARATOR;
  940. $targetFile = $targetPath . $_FILES['file']['name'];
  941. return (move_uploaded_file($tempFile, $targetFile)) ? true : false;
  942. }
  943. return false;
  944. }
  945. function getThemes()
  946. {
  947. $themes = array();
  948. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . "*.css") as $filename) {
  949. $themes[] = array(
  950. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  951. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename))
  952. );
  953. }
  954. return $themes;
  955. }
  956. function getBranches()
  957. {
  958. return array(
  959. array(
  960. 'name' => 'Develop',
  961. 'value' => 'v2-develop'
  962. ),
  963. array(
  964. 'name' => 'Master',
  965. 'value' => 'v2-master'
  966. )
  967. );
  968. }
  969. function getAuthTypes()
  970. {
  971. return array(
  972. array(
  973. 'name' => 'Organizr DB',
  974. 'value' => 'internal'
  975. ),
  976. array(
  977. 'name' => 'Organizr DB + Backend',
  978. 'value' => 'both'
  979. ),
  980. array(
  981. 'name' => 'Backend Only',
  982. 'value' => 'external'
  983. )
  984. );
  985. }
  986. function getAuthBackends()
  987. {
  988. $backendOptions = array();
  989. $backendOptions[] = array(
  990. 'name' => 'Choose Backend',
  991. 'value' => false,
  992. 'disabled' => true
  993. );
  994. foreach (array_filter(get_defined_functions()['user'], function ($v) {
  995. return strpos($v, 'plugin_auth_') === 0;
  996. }) as $value) {
  997. $name = str_replace('plugin_auth_', '', $value);
  998. if (strpos($name, 'disabled') === false) {
  999. $backendOptions[] = array(
  1000. 'name' => ucwords(str_replace('_', ' ', $name)),
  1001. 'value' => $name
  1002. );
  1003. } else {
  1004. $backendOptions[] = array(
  1005. 'name' => $value(),
  1006. 'value' => 'none',
  1007. 'disabled' => true,
  1008. );
  1009. }
  1010. }
  1011. ksort($backendOptions);
  1012. return $backendOptions;
  1013. }
  1014. function wizardPath($array)
  1015. {
  1016. $path = $array['data']['path'];
  1017. if (file_exists($path)) {
  1018. if (is_writable($path)) {
  1019. return true;
  1020. }
  1021. } else {
  1022. if (is_writable(dirname($path, 1))) {
  1023. if (mkdir($path, 0760, true)) {
  1024. return true;
  1025. }
  1026. }
  1027. }
  1028. return 'permissions';
  1029. }
  1030. function groupSelect()
  1031. {
  1032. $groups = allGroups();
  1033. $select = array();
  1034. foreach ($groups as $key => $value) {
  1035. $select[] = array(
  1036. 'name' => $value['group'],
  1037. 'value' => $value['group_id']
  1038. );
  1039. }
  1040. return $select;
  1041. }
  1042. function getImage()
  1043. {
  1044. $refresh = false;
  1045. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  1046. if (!file_exists($cacheDirectory)) {
  1047. mkdir($cacheDirectory, 0777, true);
  1048. }
  1049. @$image_url = $_GET['img'];
  1050. @$key = $_GET['key'];
  1051. @$image_height = $_GET['height'];
  1052. @$image_width = $_GET['width'];
  1053. @$source = $_GET['source'];
  1054. @$itemType = $_GET['type'];
  1055. if (strpos($key, '$') !== false) {
  1056. $key = explode('$', $key)[0];
  1057. $refresh = true;
  1058. }
  1059. switch ($source) {
  1060. case 'plex':
  1061. $plexAddress = qualifyURL($GLOBALS['plexURL']);
  1062. $image_src = $plexAddress . '/photo/:/transcode?height=' . $image_height . '&width=' . $image_width . '&upscale=1&url=' . $image_url . '&X-Plex-Token=' . $GLOBALS['plexToken'];
  1063. break;
  1064. case 'emby':
  1065. $embyAddress = qualifyURL($GLOBALS['embyURL']);
  1066. $imgParams = array();
  1067. if (isset($_GET['height'])) {
  1068. $imgParams['height'] = 'maxHeight=' . $_GET['height'];
  1069. }
  1070. if (isset($_GET['width'])) {
  1071. $imgParams['width'] = 'maxWidth=' . $_GET['width'];
  1072. }
  1073. $image_src = $embyAddress . '/Items/' . $image_url . '/Images/' . $itemType . '?' . implode('&', $imgParams);
  1074. break;
  1075. default:
  1076. # code...
  1077. break;
  1078. }
  1079. if (isset($image_url) && isset($image_height) && isset($image_width) && isset($image_src)) {
  1080. $cachefile = $cacheDirectory . $key . '.jpg';
  1081. $cachetime = 604800;
  1082. // Serve from the cache if it is younger than $cachetime
  1083. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile) && $refresh == false) {
  1084. header("Content-type: image/jpeg");
  1085. //@readfile($cachefile);
  1086. echo @curl('get', $cachefile)['content'];
  1087. exit;
  1088. }
  1089. ob_start(); // Start the output buffer
  1090. header('Content-type: image/jpeg');
  1091. //@readfile($image_src);
  1092. echo @curl('get', $image_src)['content'];
  1093. // Cache the output to a file
  1094. $fp = fopen($cachefile, 'wb');
  1095. fwrite($fp, ob_get_contents());
  1096. fclose($fp);
  1097. ob_end_flush(); // Send the output to the browser
  1098. die();
  1099. } else {
  1100. die("Invalid Request");
  1101. }
  1102. }
  1103. function cacheImage($url, $name)
  1104. {
  1105. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  1106. if (!file_exists($cacheDirectory)) {
  1107. mkdir($cacheDirectory, 0777, true);
  1108. }
  1109. $cachefile = $cacheDirectory . $name . '.jpg';
  1110. copy($url, $cachefile);
  1111. }
  1112. function downloader($array)
  1113. {
  1114. switch ($array['data']['source']) {
  1115. case 'sabnzbd':
  1116. switch ($array['data']['action']) {
  1117. case 'resume':
  1118. case 'pause':
  1119. sabnzbdAction($array['data']['action'], $array['data']['target']);
  1120. break;
  1121. default:
  1122. # code...
  1123. break;
  1124. }
  1125. break;
  1126. case 'nzbget':
  1127. break;
  1128. default:
  1129. # code...
  1130. break;
  1131. }
  1132. }
  1133. function sabnzbdAction($action = null, $target = null)
  1134. {
  1135. if ($GLOBALS['homepageSabnzbdEnabled'] && !empty($GLOBALS['sabnzbdURL']) && !empty($GLOBALS['sabnzbdToken']) && qualifyRequest($GLOBALS['homepageSabnzbdAuth'])) {
  1136. $url = qualifyURL($GLOBALS['sabnzbdURL']);
  1137. switch ($action) {
  1138. case 'pause':
  1139. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value=' . $target . '&' : 'mode=pause';
  1140. $url = $url . '/api?' . $id . '&output=json&apikey=' . $GLOBALS['sabnzbdToken'];
  1141. break;
  1142. case 'resume':
  1143. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value=' . $target . '&' : 'mode=resume';
  1144. $url = $url . '/api?' . $id . '&output=json&apikey=' . $GLOBALS['sabnzbdToken'];
  1145. break;
  1146. default:
  1147. # code...
  1148. break;
  1149. }
  1150. try {
  1151. $options = (localURL($url)) ? array('verify' => false) : array();
  1152. $response = Requests::get($url, array(), $options);
  1153. if ($response->success) {
  1154. $api['content'] = json_decode($response->body, true);
  1155. }
  1156. } catch (Requests_Exception $e) {
  1157. writeLog('error', 'SabNZBd Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  1158. };
  1159. $api['content'] = isset($api['content']) ? $api['content'] : false;
  1160. return $api;
  1161. }
  1162. }
  1163. // Deluge API isn't working ATM - will get with dev.
  1164. function delugeAction($action = null, $target = null)
  1165. {
  1166. if ($GLOBALS['homepageDelugeEnabled'] && !empty($GLOBALS['delugeURL']) && !empty($GLOBALS['delugePassword']) && qualifyRequest($GLOBALS['homepageDelugeAuth'])) {
  1167. $url = qualifyURL($GLOBALS['delugeURL']);
  1168. try {
  1169. $deluge = new deluge($GLOBALS['delugeURL'], decrypt($GLOBALS['delugePassword']));
  1170. switch ($action) {
  1171. case 'pause':
  1172. $torrents = $deluge->pauseTorrent($target);
  1173. break;
  1174. case 'pauseAll':
  1175. $torrents = $deluge->pauseAllTorrents();
  1176. break;
  1177. case 'resume':
  1178. $torrents = $deluge->resumeTorrent($target);
  1179. break;
  1180. case 'resumeAll':
  1181. $torrents = $deluge->resumeAllTorrents();
  1182. break;
  1183. default:
  1184. # code...
  1185. break;
  1186. }
  1187. $api['content'] = $torrents;
  1188. } catch (Excecption $e) {
  1189. writeLog('error', 'Deluge Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  1190. }
  1191. $api['content'] = isset($api['content']) ? $api['content'] : false;
  1192. return $api;
  1193. }
  1194. return false;
  1195. }
  1196. function getOrgUsers()
  1197. {
  1198. $result = allUsers();
  1199. if (is_array($result) || is_object($result)) {
  1200. foreach ($result['users'] as $k => $v) {
  1201. $return[$v['username']] = $v['email'];
  1202. }
  1203. return $return;
  1204. }
  1205. }
  1206. function convertPlexName($user, $type)
  1207. {
  1208. $array = libraryList('plex');
  1209. switch ($type) {
  1210. case "username":
  1211. case "u":
  1212. $plexUser = array_search($user, $array['users']);
  1213. break;
  1214. case "id":
  1215. if (array_key_exists(strtolower($user), $array['users'])) {
  1216. $plexUser = $array['users'][strtolower($user)];
  1217. }
  1218. break;
  1219. default:
  1220. $plexUser = false;
  1221. }
  1222. return (!empty($plexUser) ? $plexUser : null);
  1223. }
  1224. function libraryList($type = null)
  1225. {
  1226. switch ($type) {
  1227. case 'plex':
  1228. if (!empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'])) {
  1229. $url = 'https://plex.tv/api/servers/' . $GLOBALS['plexID'] . '/shared_servers';
  1230. try {
  1231. $headers = array(
  1232. "Accept" => "application/json",
  1233. "X-Plex-Token" => $GLOBALS['plexToken']
  1234. );
  1235. $response = Requests::get($url, $headers, array());
  1236. libxml_use_internal_errors(true);
  1237. if ($response->success) {
  1238. $libraryList = array();
  1239. $plex = simplexml_load_string($response->body);
  1240. foreach ($plex->SharedServer->Section as $child) {
  1241. $libraryList['libraries'][(string)$child['title']] = (string)$child['id'];
  1242. }
  1243. foreach ($plex->SharedServer as $child) {
  1244. if (!empty($child['username'])) {
  1245. $username = (string)strtolower($child['username']);
  1246. $email = (string)strtolower($child['email']);
  1247. $libraryList['users'][$username] = (string)$child['id'];
  1248. $libraryList['emails'][$email] = (string)$child['id'];
  1249. $libraryList['both'][$username] = $email;
  1250. }
  1251. }
  1252. $libraryList = array_change_key_case($libraryList, CASE_LOWER);
  1253. return $libraryList;
  1254. }
  1255. } catch (Requests_Exception $e) {
  1256. writeLog('error', 'Plex Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  1257. };
  1258. }
  1259. break;
  1260. default:
  1261. # code...
  1262. break;
  1263. }
  1264. return false;
  1265. }
  1266. function plexJoinAPI($array)
  1267. {
  1268. return plexJoin($array['data']['username'], $array['data']['email'], $array['data']['password']);
  1269. }
  1270. function plexJoin($username, $email, $password)
  1271. {
  1272. try {
  1273. $url = 'https://plex.tv/users.json';
  1274. $headers = array(
  1275. 'Accept' => 'application/json',
  1276. 'Content-Type' => 'application/x-www-form-urlencoded',
  1277. 'X-Plex-Product' => 'Organizr',
  1278. 'X-Plex-Version' => '2.0',
  1279. 'X-Plex-Client-Identifier' => '01010101-10101010',
  1280. );
  1281. $data = array(
  1282. 'user[email]' => $email,
  1283. 'user[username]' => $username,
  1284. 'user[password]' => $password,
  1285. );
  1286. $response = Requests::post($url, $headers, $data, array());
  1287. $json = json_decode($response->body, true);
  1288. $errors = (!empty($json['errors']) ? true : false);
  1289. $success = (!empty($json['user']) ? true : false);
  1290. //Use This for later
  1291. $usernameError = (!empty($json['errors']['username']) ? $json['errors']['username'][0] : false);
  1292. $emailError = (!empty($json['errors']['email']) ? $json['errors']['email'][0] : false);
  1293. $passwordError = (!empty($json['errors']['password']) ? $json['errors']['password'][0] : false);
  1294. $errorMessage = "";
  1295. if ($errors) {
  1296. if ($usernameError) {
  1297. $errorMessage .= "[Username Error: " . $usernameError . "]";
  1298. }
  1299. if ($emailError) {
  1300. $errorMessage .= "[Email Error: " . $emailError . "]";
  1301. }
  1302. if ($passwordError) {
  1303. $errorMessage .= "[Password Error: " . $passwordError . "]";
  1304. }
  1305. }
  1306. return (!empty($success) && empty($errors) ? true : $errorMessage);
  1307. } catch (Requests_Exception $e) {
  1308. writeLog('error', 'Plex.TV Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  1309. };
  1310. return false;
  1311. }
  1312. function checkFrame($array, $url)
  1313. {
  1314. if (array_key_exists("x-frame-options", $array)) {
  1315. if ($array['x-frame-options'] == "deny") {
  1316. return false;
  1317. } elseif ($array['x-frame-options'] == "sameorgin") {
  1318. $digest = parse_url($url);
  1319. $host = (isset($digest['host']) ? $digest['host'] : '');
  1320. if (getServer() == $host) {
  1321. return true;
  1322. } else {
  1323. return false;
  1324. }
  1325. }
  1326. } else {
  1327. if (!$array) {
  1328. return false;
  1329. }
  1330. return true;
  1331. }
  1332. }
  1333. function frameTest($url)
  1334. {
  1335. $array = array_change_key_case(get_headers(qualifyURL($url), 1));
  1336. $url = qualifyURL($url);
  1337. if (checkFrame($array, $url)) {
  1338. return true;
  1339. } else {
  1340. return false;
  1341. }
  1342. }