organizr-functions.php 37 KB

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