organizr-functions.php 41 KB

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