organizr-functions.php 36 KB

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