organizr-functions.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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-download',
  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-refresh',
  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-ticket',
  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-id-badge',
  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. 'Misc' => array(
  317. array(
  318. 'type' => 'password-alt',
  319. 'name' => 'registrationPassword',
  320. 'label' => 'Registration Password',
  321. 'value' => $GLOBALS['registrationPassword'],
  322. ),
  323. )
  324. );
  325. }
  326. function getSSO(){
  327. return array(
  328. 'Plex' => array(
  329. array(
  330. 'type' => 'password-alt',
  331. 'name' => 'plexToken',
  332. 'label' => 'Plex Token',
  333. 'value' => $GLOBALS['plexToken'],
  334. 'placeholder' => 'Use Get Token Button'
  335. ),
  336. array(
  337. 'type' => 'button',
  338. 'label' => 'Get Plex Token',
  339. 'class' => 'popup-with-form getPlexTokenSSO',
  340. 'icon' => 'fa fa-ticket',
  341. 'text' => 'Retrieve',
  342. 'href' => '#sso-plex-token-form',
  343. 'attr' => 'data-effect="mfp-3d-unfold"'
  344. ),
  345. array(
  346. 'type' => 'password-alt',
  347. 'name' => 'plexID',
  348. 'label' => 'Plex Machine',
  349. 'value' => $GLOBALS['plexID'],
  350. 'placeholder' => 'Use Get Plex Machine Button'
  351. ),
  352. array(
  353. 'type' => 'button',
  354. 'label' => 'Get Plex Machine',
  355. 'class' => 'popup-with-form getPlexMachineSSO',
  356. 'icon' => 'fa fa-id-badge',
  357. 'text' => 'Retrieve',
  358. 'href' => '#sso-plex-machine-form',
  359. 'attr' => 'data-effect="mfp-3d-unfold"'
  360. ),
  361. array(
  362. 'type' => 'input',
  363. 'name' => 'plexAdmin',
  364. 'label' => 'Admin Username',
  365. 'value' => $GLOBALS['plexAdmin'],
  366. 'placeholder' => 'Admin username for Plex'
  367. ),
  368. array(
  369. 'type' => 'blank',
  370. 'label' => ''
  371. ),
  372. array(
  373. 'type' => 'html',
  374. 'label' => 'Plex Note',
  375. 'html' => '<span lang="en">Please make sure both Token and Machine are filled in</span>'
  376. ),
  377. array(
  378. 'type' => 'switch',
  379. 'name' => 'ssoPlex',
  380. 'label' => 'Enable',
  381. 'value' => $GLOBALS['ssoPlex']
  382. )
  383. ),
  384. 'Ombi' => array(
  385. array(
  386. 'type' => 'input',
  387. 'name' => 'ombiURL',
  388. 'label' => 'Ombi URL',
  389. 'value' => $GLOBALS['ombiURL'],
  390. 'placeholder' => 'http(s)://hostname:port'
  391. ),
  392. array(
  393. 'type' => 'switch',
  394. 'name' => 'ssoOmbi',
  395. 'label' => 'Enable',
  396. 'value' => $GLOBALS['ssoOmbi']
  397. )
  398. ),
  399. 'Tautulli' => array(
  400. array(
  401. 'type' => 'input',
  402. 'name' => 'tautulliURL',
  403. 'label' => 'Tautulli URL',
  404. 'value' => $GLOBALS['tautulliURL'],
  405. 'placeholder' => 'http(s)://hostname:port'
  406. ),
  407. array(
  408. 'type' => 'switch',
  409. 'name' => 'ssoTautulli',
  410. 'label' => 'Enable',
  411. 'value' => $GLOBALS['ssoTautulli']
  412. )
  413. )
  414. );
  415. }
  416. function loadAppearance(){
  417. $appearance = array();
  418. $appearance['logo'] = $GLOBALS['logo'];
  419. $appearance['title'] = $GLOBALS['title'];
  420. $appearance['useLogo'] = $GLOBALS['useLogo'];
  421. $appearance['headerColor'] = $GLOBALS['headerColor'];
  422. $appearance['headerTextColor'] = $GLOBALS['headerTextColor'];
  423. $appearance['sidebarColor'] = $GLOBALS['sidebarColor'];
  424. $appearance['headerTextColor'] = $GLOBALS['headerTextColor'];
  425. $appearance['sidebarTextColor'] = $GLOBALS['sidebarTextColor'];
  426. $appearance['loginWallpaper'] = $GLOBALS['loginWallpaper'];
  427. $appearance['customCss'] = $GLOBALS['customCss'];
  428. return $appearance;
  429. }
  430. function getCustomizeAppearance(){
  431. if(file_exists(dirname(__DIR__,1).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php')){
  432. return array(
  433. 'Top Bar' => array(
  434. array(
  435. 'type' => 'input',
  436. 'name' => 'logo',
  437. 'label' => 'Logo',
  438. 'value' => $GLOBALS['logo']
  439. ),
  440. array(
  441. 'type' => 'input',
  442. 'name' => 'title',
  443. 'label' => 'Title',
  444. 'value' => $GLOBALS['title']
  445. ),
  446. array(
  447. 'type' => 'switch',
  448. 'name' => 'useLogo',
  449. 'label' => 'Use Logo instead of Title',
  450. 'value' => $GLOBALS['useLogo']
  451. )
  452. ),
  453. 'Login Page' => array(
  454. array(
  455. 'type' => 'input',
  456. 'name' => 'loginWallpaper',
  457. 'label' => 'Login Wallpaper',
  458. 'value' => $GLOBALS['loginWallpaper']
  459. )
  460. ),
  461. 'Colors & Themes' => array(
  462. array(
  463. 'type' => 'input',
  464. 'name' => 'headerColor',
  465. 'label' => 'Nav Bar Color',
  466. 'value' => $GLOBALS['headerColor'],
  467. 'class' => 'colorpicker',
  468. 'disabled' => true
  469. ),
  470. array(
  471. 'type' => 'input',
  472. 'name' => 'headerTextColor',
  473. 'label' => 'Nav Bar Text Color',
  474. 'value' => $GLOBALS['headerTextColor'],
  475. 'class' => 'colorpicker',
  476. 'disabled' => true
  477. ),
  478. array(
  479. 'type' => 'input',
  480. 'name' => 'sidebarColor',
  481. 'label' => 'Side Bar Color',
  482. 'value' => $GLOBALS['sidebarColor'],
  483. 'class' => 'colorpicker',
  484. 'disabled' => true
  485. ),
  486. array(
  487. 'type' => 'input',
  488. 'name' => 'sidebarTextColor',
  489. 'label' => 'Side Bar Text Color',
  490. 'value' => $GLOBALS['sidebarTextColor'],
  491. 'class' => 'colorpicker',
  492. 'disabled' => true
  493. ),
  494. array(
  495. 'type' => 'select',
  496. 'name' => 'theme',
  497. 'label' => 'Theme',
  498. 'class' => 'themeChanger',
  499. 'value' => $GLOBALS['theme'],
  500. 'options' => getThemes()
  501. ),
  502. array(
  503. 'type' => 'select',
  504. 'name' => 'style',
  505. 'label' => 'Style',
  506. 'class' => 'styleChanger',
  507. 'value' => $GLOBALS['style'],
  508. 'options' => array(
  509. array(
  510. 'name' => 'Light',
  511. 'value' => 'light'
  512. ),
  513. array(
  514. 'name' => 'Dark',
  515. 'value' => 'dark'
  516. ),
  517. array(
  518. 'name' => 'Horizontal',
  519. 'value' => 'horizontal'
  520. )
  521. )
  522. ),
  523. array(
  524. 'type' => 'textbox',
  525. 'name' => 'customCss',
  526. 'class' => 'hidden cssTextarea',
  527. 'label' => '',
  528. 'value' => $GLOBALS['customCss'],
  529. 'placeholder' => 'No <style> tags needed',
  530. 'attr' => 'rows="10"',
  531. ),
  532. array(
  533. 'type' => 'html',
  534. 'override' => 12,
  535. 'label' => 'Custom CSS [Can replace colors from above]',
  536. '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>'
  537. ),
  538. )
  539. );
  540. }
  541. }
  542. function editAppearance($array){
  543. switch ($array['data']['value']) {
  544. case 'true':
  545. $array['data']['value'] = (bool) true;
  546. break;
  547. case 'false':
  548. $array['data']['value'] = (bool) false;
  549. break;
  550. default:
  551. $array['data']['value'] = $array['data']['value'];
  552. }
  553. //return gettype($array['data']['value']).' - '.$array['data']['value'];
  554. switch ($array['data']['action']) {
  555. case 'editCustomizeAppearance':
  556. $newItem = array(
  557. $array['data']['name'] => $array['data']['value']
  558. );
  559. return (updateConfig($newItem)) ? true : false;
  560. break;
  561. default:
  562. # code...
  563. break;
  564. }
  565. }
  566. function updateConfigItem($array){
  567. switch ($array['data']['value']) {
  568. case 'true':
  569. $array['data']['value'] = (bool) true;
  570. break;
  571. case 'false':
  572. $array['data']['value'] = (bool) false;
  573. break;
  574. default:
  575. $array['data']['value'] = $array['data']['value'];
  576. }
  577. // Hash
  578. if($array['data']['type'] == 'password'){
  579. $array['data']['value'] = encrypt($array['data']['value']);
  580. }
  581. //return gettype($array['data']['value']).' - '.$array['data']['value'];
  582. $newItem = array(
  583. $array['data']['name'] => $array['data']['value']
  584. );
  585. return (updateConfig($newItem)) ? true : false;
  586. }
  587. function getPlugins(){
  588. if(file_exists(dirname(__DIR__,1).DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php')){
  589. $pluginList = array();
  590. foreach($GLOBALS['plugins'] as $plugin){
  591. foreach ($plugin as $key => $value) {
  592. $plugin[$key]['enabled'] = $GLOBALS[$value['configPrefix'].'-enabled'];
  593. }
  594. $pluginList = array_merge($pluginList, $plugin);
  595. }
  596. return $pluginList;
  597. }
  598. return false;
  599. }
  600. function editPlugins($array){
  601. switch ($array['data']['action']) {
  602. case 'enable':
  603. $newItem = array(
  604. $array['data']['configName'] => true
  605. );
  606. writeLog('success', 'Plugin Function - Enabled Plugin ['.$_POST['data']['name'].']', $GLOBALS['organizrUser']['username']);
  607. return (updateConfig($newItem)) ? true : false;
  608. break;
  609. case 'disable':
  610. $newItem = array(
  611. $array['data']['configName'] => false
  612. );
  613. writeLog('success', 'Plugin Function - Disabled Plugin ['.$_POST['data']['name'].']', $GLOBALS['organizrUser']['username']);
  614. return (updateConfig($newItem)) ? true : false;
  615. break;
  616. default:
  617. # code...
  618. break;
  619. }
  620. }
  621. function auth(){
  622. $debug = false; // CAREFUL WHEN SETTING TO TRUE AS THIS OPENS AUTH UP
  623. $ban = isset($_GET['ban']) ? strtoupper($_GET['ban']) : "";
  624. $whitelist = isset($_GET['whitelist']) ? $_GET['whitelist'] : false;
  625. $blacklist = isset($_GET['blacklist']) ? $_GET['blacklist'] : false;
  626. $group = isset($_GET['group']) ? (int)$_GET['group'] : (int)0;
  627. $currentIP = userIP();
  628. if(isset($GLOBALS['organizrUser'])){
  629. $currentUser = $GLOBALS['organizrUser']['username'];
  630. $currentGroup = $GLOBALS['organizrUser']['groupID'];
  631. }else{
  632. $currentUser = 'Guest';
  633. $currentGroup = getUserLevel();
  634. }
  635. $userInfo = "User: $currentUser | Group: $currentGroup | IP: $currentIP | Requesting Access to Group $group | Result: ";
  636. if ($whitelist) {
  637. if(in_array($currentIP, arrayIP($whitelist))) {
  638. !$debug ? exit(http_response_code(200)) : die("$userInfo Whitelist Authorized");
  639. }
  640. }
  641. if ($blacklist) {
  642. if(in_array($currentIP, arrayIP($blacklist))) {
  643. !$debug ? exit(http_response_code(401)) : die("$userInfo Blacklisted");
  644. }
  645. }
  646. if($group !== null){
  647. if(qualifyRequest($group)){
  648. !$debug ? exit(http_response_code(200)) : die("$userInfo Authorized");
  649. }else{
  650. !$debug ? exit(http_response_code(401)) : die("$userInfo Not Authorized");
  651. }
  652. }else{
  653. !$debug ? exit(http_response_code(401)) : die("Not Authorized Due To No Parameters Set");
  654. }
  655. }
  656. function logoOrText(){
  657. if($GLOBALS['useLogo'] == false){
  658. return '<h1>'.$GLOBALS['title'].'</h1>';
  659. }else{
  660. return '<img style="max-width: 350px;" src="'.$GLOBALS['logo'].'" alt="Home" />';
  661. }
  662. }
  663. function getImages(){
  664. $dirname = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  665. $path = 'plugins/images/tabs/';
  666. $images = scandir($dirname);
  667. $ignore = Array(".", "..", "._.DS_Store", ".DS_Store", ".pydio_id");
  668. $allIcons = array();
  669. foreach($images as $image){
  670. if(!in_array($image, $ignore)) {
  671. $allIcons[] = $path.$image;
  672. }
  673. }
  674. return $allIcons;
  675. }
  676. function editImages(){
  677. $array = array();
  678. $postCheck = array_filter($_POST);
  679. $filesCheck = array_filter($_FILES);
  680. if(!empty($postCheck)){
  681. if($_POST['data']['action'] == 'deleteImage'){
  682. if(file_exists(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])){
  683. writeLog('success', 'Image Manager Function - Deleted Image ['.$_POST['data']['imageName'].']', $GLOBALS['organizrUser']['username']);
  684. return (unlink(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])) ? true : false;
  685. }
  686. }
  687. }
  688. if(!empty($filesCheck)){
  689. ini_set('upload_max_filesize', '10M');
  690. ini_set('post_max_size', '10M');
  691. $tempFile = $_FILES['file']['tmp_name'];
  692. $targetPath = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  693. $targetFile = $targetPath. $_FILES['file']['name'];
  694. return (move_uploaded_file($tempFile,$targetFile)) ? true : false;
  695. }
  696. return false;
  697. }
  698. function getThemes(){
  699. $themes = array();
  700. foreach (glob(dirname(__DIR__,2).DIRECTORY_SEPARATOR.'css' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . "*.css") as $filename){
  701. $themes[] = array(
  702. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  703. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename))
  704. );
  705. }
  706. return $themes;
  707. }
  708. function getBranches(){
  709. return array(
  710. array(
  711. 'name' => 'Develop',
  712. 'value' => 'v2-develop'
  713. ),
  714. array(
  715. 'name' => 'Master',
  716. 'value' => 'v2-master'
  717. )
  718. );
  719. }
  720. function getAuthTypes(){
  721. return array(
  722. array(
  723. 'name' => 'Organizr DB',
  724. 'value' => 'internal'
  725. ),
  726. array(
  727. 'name' => 'Organizr DB + Backend',
  728. 'value' => 'both'
  729. ),
  730. array(
  731. 'name' => 'Backend Only',
  732. 'value' => 'external'
  733. )
  734. );
  735. }
  736. function getAuthBackends(){
  737. $backendOptions = array();
  738. $backendOptions[] = array(
  739. 'name' => 'Choose Backend',
  740. 'value' => false,
  741. 'disabled' => true
  742. );
  743. foreach (array_filter(get_defined_functions()['user'],function($v) { return strpos($v, 'plugin_auth_') === 0; }) as $value) {
  744. $name = str_replace('plugin_auth_','',$value);
  745. if (strpos($name, 'disabled') === false) {
  746. $backendOptions[] = array(
  747. 'name' => ucwords(str_replace('_',' ',$name)),
  748. 'value' => $name
  749. );
  750. } else {
  751. $backendOptions[] = array(
  752. 'name' => $value(),
  753. 'value' => 'none',
  754. 'disabled' => true,
  755. );
  756. }
  757. }
  758. ksort($backendOptions);
  759. return $backendOptions;
  760. }
  761. function wizardPath($array){
  762. $path = $array['data']['path'];
  763. if(file_exists($path)){
  764. if(is_writable($path)){
  765. return true;
  766. }
  767. }else{
  768. if(is_writable(dirname($path, 1))){
  769. if(mkdir($path, 0760, true)) {
  770. return true;
  771. }
  772. }
  773. }
  774. return 'permissions';
  775. }
  776. function groupSelect(){
  777. $groups = allGroups();
  778. $select = array();
  779. foreach ($groups as $key => $value) {
  780. $select[] = array(
  781. 'name' => $value['group'],
  782. 'value' => $value['group_id']
  783. );
  784. }
  785. return $select;
  786. }
  787. function getImage() {
  788. $refresh = false;
  789. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  790. if (!file_exists($cacheDirectory)) {
  791. mkdir($cacheDirectory, 0777, true);
  792. }
  793. @$image_url = $_GET['img'];
  794. @$key = $_GET['key'];
  795. @$image_height = $_GET['height'];
  796. @$image_width = $_GET['width'];
  797. @$source = $_GET['source'];
  798. @$itemType = $_GET['type'];
  799. if(strpos($key, '$') !== false){
  800. $key = explode('$', $key)[0];
  801. $refresh = true;
  802. }
  803. switch ($source) {
  804. case 'plex':
  805. $plexAddress = qualifyURL($GLOBALS['plexURL']);
  806. $image_src = $plexAddress . '/photo/:/transcode?height='.$image_height.'&width='.$image_width.'&upscale=1&url=' . $image_url . '&X-Plex-Token=' . $GLOBALS['plexToken'];
  807. break;
  808. case 'emby':
  809. $embyAddress = qualifyURL($GLOBALS['embyURL']);
  810. $imgParams = array();
  811. if (isset($_GET['height'])) { $imgParams['height'] = 'maxHeight='.$_GET['height']; }
  812. if (isset($_GET['width'])) { $imgParams['width'] = 'maxWidth='.$_GET['width']; }
  813. $image_src = $embyAddress . '/Items/'.$image_url.'/Images/'.$itemType.'?'.implode('&', $imgParams);
  814. break;
  815. default:
  816. # code...
  817. break;
  818. }
  819. if(isset($image_url) && isset($image_height) && isset($image_width) && isset($image_src)) {
  820. $cachefile = $cacheDirectory.$key.'.jpg';
  821. $cachetime = 604800;
  822. // Serve from the cache if it is younger than $cachetime
  823. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile) && $refresh == false) {
  824. header("Content-type: image/jpeg");
  825. //@readfile($cachefile);
  826. echo @curl('get',$cachefile)['content'];
  827. exit;
  828. }
  829. ob_start(); // Start the output buffer
  830. header('Content-type: image/jpeg');
  831. //@readfile($image_src);
  832. echo @curl('get',$image_src)['content'];
  833. // Cache the output to a file
  834. $fp = fopen($cachefile, 'wb');
  835. fwrite($fp, ob_get_contents());
  836. fclose($fp);
  837. ob_end_flush(); // Send the output to the browser
  838. die();
  839. } else {
  840. die("Invalid Request");
  841. }
  842. }
  843. function cacheImage($url,$name){
  844. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  845. if (!file_exists($cacheDirectory)) {
  846. mkdir($cacheDirectory, 0777, true);
  847. }
  848. $cachefile = $cacheDirectory.$name.'.jpg';
  849. copy($url, $cachefile);
  850. }
  851. function downloader($array){
  852. switch ($array['data']['source']) {
  853. case 'sabnzbd':
  854. switch ($array['data']['action']) {
  855. case 'resume':
  856. case 'pause':
  857. sabnzbdAction($array['data']['action'],$array['data']['target']);
  858. break;
  859. default:
  860. # code...
  861. break;
  862. }
  863. break;
  864. case 'nzbget':
  865. break;
  866. default:
  867. # code...
  868. break;
  869. }
  870. }
  871. function sabnzbdAction($action=null, $target=null) {
  872. if($GLOBALS['homepageSabnzbdEnabled'] && !empty($GLOBALS['sabnzbdURL']) && !empty($GLOBALS['sabnzbdToken']) && qualifyRequest($GLOBALS['homepageSabnzbdAuth'])){
  873. $url = qualifyURL($GLOBALS['sabnzbdURL']);
  874. switch ($action) {
  875. case 'pause':
  876. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value='.$target.'&' : 'mode=pause';
  877. $url = $url.'/api?'.$id.'&output=json&apikey='.$GLOBALS['sabnzbdToken'];
  878. break;
  879. case 'resume':
  880. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value='.$target.'&' : 'mode=resume';
  881. $url = $url.'/api?'.$id.'&output=json&apikey='.$GLOBALS['sabnzbdToken'];
  882. break;
  883. default:
  884. # code...
  885. break;
  886. }
  887. try{
  888. $options = (localURL($url)) ? array('verify' => false ) : array();
  889. $response = Requests::get($url, array(), $options);
  890. if($response->success){
  891. $api['content'] = json_decode($response->body, true);
  892. }
  893. }catch( Requests_Exception $e ) {
  894. writeLog('error', 'SabNZBd Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  895. };
  896. $api['content'] = isset($api['content']) ? $api['content'] : false;
  897. return $api;
  898. }
  899. }
  900. function getOrgUsers(){
  901. $result = allUsers();
  902. if (is_array($result) || is_object($result)){
  903. foreach($result['users'] as $k => $v){
  904. $return[$v['username']] = $v['email'];
  905. }
  906. return $return;
  907. }
  908. }
  909. function convertPlexName($user, $type){
  910. $array = libraryList('plex');
  911. switch ($type){
  912. case "username":
  913. case "u":
  914. $plexUser = array_search ($user, $array['users']);
  915. break;
  916. case "id":
  917. if (array_key_exists(strtolower($user), $array['users'])) {
  918. $plexUser = $array['users'][strtolower($user)];
  919. }
  920. break;
  921. default:
  922. $plexUser = false;
  923. }
  924. return (!empty($plexUser) ? $plexUser : null );
  925. }
  926. function libraryList($type=null){
  927. switch ($type) {
  928. case 'plex':
  929. if(!empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'])){
  930. $url = 'https://plex.tv/api/servers/'.$GLOBALS['plexID'].'/shared_servers';
  931. try{
  932. $headers = array(
  933. "Accept" => "application/json",
  934. "X-Plex-Token" => $GLOBALS['plexToken']
  935. );
  936. $response = Requests::get($url, $headers, array());
  937. libxml_use_internal_errors(true);
  938. if($response->success){
  939. $libraryList = array();
  940. $plex = simplexml_load_string($response->body);
  941. foreach($plex->SharedServer->Section AS $child) {
  942. $libraryList['libraries'][(string)$child['title']] = (string)$child['id'];
  943. }
  944. foreach($plex->SharedServer AS $child) {
  945. if(!empty($child['username'])){
  946. $username = (string)strtolower($child['username']);
  947. $email = (string)strtolower($child['email']);
  948. $libraryList['users'][$username] = (string)$child['id'];
  949. $libraryList['emails'][$email] = (string)$child['id'];
  950. $libraryList['both'][$username] = $email;
  951. }
  952. }
  953. $libraryList = array_change_key_case($libraryList,CASE_LOWER);
  954. return $libraryList;
  955. }
  956. }catch( Requests_Exception $e ) {
  957. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  958. };
  959. }
  960. break;
  961. default:
  962. # code...
  963. break;
  964. }
  965. return false;
  966. }
  967. function plexJoinAPI($array){
  968. return plexJoin($array['data']['username'],$array['data']['email'],$array['data']['password']);
  969. }
  970. function plexJoin($username, $email, $password){
  971. try{
  972. $url = 'https://plex.tv/users.json';
  973. $headers = array(
  974. 'Accept'=> 'application/json',
  975. 'Content-Type' => 'application/x-www-form-urlencoded',
  976. 'X-Plex-Product' => 'Organizr',
  977. 'X-Plex-Version' => '2.0',
  978. 'X-Plex-Client-Identifier' => '01010101-10101010',
  979. );
  980. $data = array(
  981. 'user[email]' => $email,
  982. 'user[username]' => $username,
  983. 'user[password]' => $password,
  984. );
  985. $response = Requests::post($url, $headers, $data, array());
  986. $json = json_decode($response->body, true);
  987. $errors = (!empty($json['errors']) ? true : false);
  988. $success = (!empty($json['user']) ? true : false);
  989. //Use This for later
  990. $usernameError = (!empty($json['errors']['username']) ? $json['errors']['username'][0] : false);
  991. $emailError = (!empty($json['errors']['email']) ? $json['errors']['email'][0] : false);
  992. $passwordError = (!empty($json['errors']['password']) ? $json['errors']['password'][0] : false);
  993. $errorMessage = "";
  994. if($errors){
  995. if($usernameError){ $errorMessage .= "[Username Error: ". $usernameError ."]"; }
  996. if($emailError){ $errorMessage .= "[Email Error: ". $emailError ."]"; }
  997. if($passwordError){ $errorMessage .= "[Password Error: ". $passwordError ."]"; }
  998. }
  999. return (!empty($success) && empty($errors) ? true : $errorMessage );
  1000. }catch( Requests_Exception $e ) {
  1001. writeLog('error', 'Plex.TV Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  1002. };
  1003. return false;
  1004. }