organizr-functions.php 40 KB

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