organizr-functions.php 37 KB

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