organizr-functions.php 37 KB

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