organizr-functions.php 34 KB

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