organizr-functions.php 40 KB

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