organizr-functions.php 34 KB

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