organizr-functions.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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 = [];
  653. foreach($GLOBALS['plugins'] as $plugin){
  654. foreach ($plugin as $key => $value) {
  655. if(strpos($value['license'], $GLOBALS['license']) !== false){
  656. $plugin[$key]['enabled'] = $GLOBALS[$value['configPrefix'].'-enabled'];
  657. $pluginList[$key] = $plugin[$key];
  658. }
  659. }
  660. }
  661. return $pluginList;
  662. }
  663. return false;
  664. }
  665. function editPlugins($array){
  666. switch ($array['data']['action']) {
  667. case 'enable':
  668. $newItem = array(
  669. $array['data']['configName'] => true
  670. );
  671. writeLog('success', 'Plugin Function - Enabled Plugin ['.$_POST['data']['name'].']', $GLOBALS['organizrUser']['username']);
  672. return (updateConfig($newItem)) ? true : false;
  673. break;
  674. case 'disable':
  675. $newItem = array(
  676. $array['data']['configName'] => false
  677. );
  678. writeLog('success', 'Plugin Function - Disabled Plugin ['.$_POST['data']['name'].']', $GLOBALS['organizrUser']['username']);
  679. return (updateConfig($newItem)) ? true : false;
  680. break;
  681. default:
  682. # code...
  683. break;
  684. }
  685. }
  686. function auth(){
  687. $debug = false; // CAREFUL WHEN SETTING TO TRUE AS THIS OPENS AUTH UP
  688. $ban = isset($_GET['ban']) ? strtoupper($_GET['ban']) : "";
  689. $whitelist = isset($_GET['whitelist']) ? $_GET['whitelist'] : false;
  690. $blacklist = isset($_GET['blacklist']) ? $_GET['blacklist'] : false;
  691. $group = isset($_GET['group']) ? (int)$_GET['group'] : (int)0;
  692. $currentIP = userIP();
  693. if(isset($GLOBALS['organizrUser'])){
  694. $currentUser = $GLOBALS['organizrUser']['username'];
  695. $currentGroup = $GLOBALS['organizrUser']['groupID'];
  696. }else{
  697. $currentUser = 'Guest';
  698. $currentGroup = getUserLevel();
  699. }
  700. $userInfo = "User: $currentUser | Group: $currentGroup | IP: $currentIP | Requesting Access to Group $group | Result: ";
  701. if ($whitelist) {
  702. if(in_array($currentIP, arrayIP($whitelist))) {
  703. !$debug ? exit(http_response_code(200)) : die("$userInfo Whitelist Authorized");
  704. }
  705. }
  706. if ($blacklist) {
  707. if(in_array($currentIP, arrayIP($blacklist))) {
  708. !$debug ? exit(http_response_code(401)) : die("$userInfo Blacklisted");
  709. }
  710. }
  711. if($group !== null){
  712. if(qualifyRequest($group)){
  713. !$debug ? exit(http_response_code(200)) : die("$userInfo Authorized");
  714. }else{
  715. !$debug ? exit(http_response_code(401)) : die("$userInfo Not Authorized");
  716. }
  717. }else{
  718. !$debug ? exit(http_response_code(401)) : die("Not Authorized Due To No Parameters Set");
  719. }
  720. }
  721. function logoOrText(){
  722. if($GLOBALS['useLogo'] == false){
  723. return '<h1>'.$GLOBALS['title'].'</h1>';
  724. }else{
  725. return '<img style="max-width: 350px;" src="'.$GLOBALS['logo'].'" alt="Home" />';
  726. }
  727. }
  728. function getImages(){
  729. $dirname = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  730. $path = 'plugins/images/tabs/';
  731. $images = scandir($dirname);
  732. $ignore = Array(".", "..", "._.DS_Store", ".DS_Store", ".pydio_id");
  733. $allIcons = array();
  734. foreach($images as $image){
  735. if(!in_array($image, $ignore)) {
  736. $allIcons[] = $path.$image;
  737. }
  738. }
  739. return $allIcons;
  740. }
  741. function editImages(){
  742. $array = array();
  743. $postCheck = array_filter($_POST);
  744. $filesCheck = array_filter($_FILES);
  745. if(!empty($postCheck)){
  746. if($_POST['data']['action'] == 'deleteImage'){
  747. if(file_exists(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])){
  748. writeLog('success', 'Image Manager Function - Deleted Image ['.$_POST['data']['imageName'].']', $GLOBALS['organizrUser']['username']);
  749. return (unlink(dirname(__DIR__,2).DIRECTORY_SEPARATOR.$_POST['data']['imagePath'])) ? true : false;
  750. }
  751. }
  752. }
  753. if(!empty($filesCheck)){
  754. ini_set('upload_max_filesize', '10M');
  755. ini_set('post_max_size', '10M');
  756. $tempFile = $_FILES['file']['tmp_name'];
  757. $targetPath = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'tabs'.DIRECTORY_SEPARATOR;
  758. $targetFile = $targetPath. $_FILES['file']['name'];
  759. return (move_uploaded_file($tempFile,$targetFile)) ? true : false;
  760. }
  761. return false;
  762. }
  763. function getThemes(){
  764. $themes = array();
  765. foreach (glob(dirname(__DIR__,2).DIRECTORY_SEPARATOR.'css' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . "*.css") as $filename){
  766. $themes[] = array(
  767. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  768. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename))
  769. );
  770. }
  771. return $themes;
  772. }
  773. function getBranches(){
  774. return array(
  775. array(
  776. 'name' => 'Develop',
  777. 'value' => 'v2-develop'
  778. ),
  779. array(
  780. 'name' => 'Master',
  781. 'value' => 'v2-master'
  782. )
  783. );
  784. }
  785. function getAuthTypes(){
  786. return array(
  787. array(
  788. 'name' => 'Organizr DB',
  789. 'value' => 'internal'
  790. ),
  791. array(
  792. 'name' => 'Organizr DB + Backend',
  793. 'value' => 'both'
  794. ),
  795. array(
  796. 'name' => 'Backend Only',
  797. 'value' => 'external'
  798. )
  799. );
  800. }
  801. function getAuthBackends(){
  802. $backendOptions = array();
  803. $backendOptions[] = array(
  804. 'name' => 'Choose Backend',
  805. 'value' => false,
  806. 'disabled' => true
  807. );
  808. foreach (array_filter(get_defined_functions()['user'],function($v) { return strpos($v, 'plugin_auth_') === 0; }) as $value) {
  809. $name = str_replace('plugin_auth_','',$value);
  810. if (strpos($name, 'disabled') === false) {
  811. $backendOptions[] = array(
  812. 'name' => ucwords(str_replace('_',' ',$name)),
  813. 'value' => $name
  814. );
  815. } else {
  816. $backendOptions[] = array(
  817. 'name' => $value(),
  818. 'value' => 'none',
  819. 'disabled' => true,
  820. );
  821. }
  822. }
  823. ksort($backendOptions);
  824. return $backendOptions;
  825. }
  826. function wizardPath($array){
  827. $path = $array['data']['path'];
  828. if(file_exists($path)){
  829. if(is_writable($path)){
  830. return true;
  831. }
  832. }else{
  833. if(is_writable(dirname($path, 1))){
  834. if(mkdir($path, 0760, true)) {
  835. return true;
  836. }
  837. }
  838. }
  839. return 'permissions';
  840. }
  841. function groupSelect(){
  842. $groups = allGroups();
  843. $select = array();
  844. foreach ($groups as $key => $value) {
  845. $select[] = array(
  846. 'name' => $value['group'],
  847. 'value' => $value['group_id']
  848. );
  849. }
  850. return $select;
  851. }
  852. function getImage() {
  853. $refresh = false;
  854. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  855. if (!file_exists($cacheDirectory)) {
  856. mkdir($cacheDirectory, 0777, true);
  857. }
  858. @$image_url = $_GET['img'];
  859. @$key = $_GET['key'];
  860. @$image_height = $_GET['height'];
  861. @$image_width = $_GET['width'];
  862. @$source = $_GET['source'];
  863. @$itemType = $_GET['type'];
  864. if(strpos($key, '$') !== false){
  865. $key = explode('$', $key)[0];
  866. $refresh = true;
  867. }
  868. switch ($source) {
  869. case 'plex':
  870. $plexAddress = qualifyURL($GLOBALS['plexURL']);
  871. $image_src = $plexAddress . '/photo/:/transcode?height='.$image_height.'&width='.$image_width.'&upscale=1&url=' . $image_url . '&X-Plex-Token=' . $GLOBALS['plexToken'];
  872. break;
  873. case 'emby':
  874. $embyAddress = qualifyURL($GLOBALS['embyURL']);
  875. $imgParams = array();
  876. if (isset($_GET['height'])) { $imgParams['height'] = 'maxHeight='.$_GET['height']; }
  877. if (isset($_GET['width'])) { $imgParams['width'] = 'maxWidth='.$_GET['width']; }
  878. $image_src = $embyAddress . '/Items/'.$image_url.'/Images/'.$itemType.'?'.implode('&', $imgParams);
  879. break;
  880. default:
  881. # code...
  882. break;
  883. }
  884. if(isset($image_url) && isset($image_height) && isset($image_width) && isset($image_src)) {
  885. $cachefile = $cacheDirectory.$key.'.jpg';
  886. $cachetime = 604800;
  887. // Serve from the cache if it is younger than $cachetime
  888. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile) && $refresh == false) {
  889. header("Content-type: image/jpeg");
  890. //@readfile($cachefile);
  891. echo @curl('get',$cachefile)['content'];
  892. exit;
  893. }
  894. ob_start(); // Start the output buffer
  895. header('Content-type: image/jpeg');
  896. //@readfile($image_src);
  897. echo @curl('get',$image_src)['content'];
  898. // Cache the output to a file
  899. $fp = fopen($cachefile, 'wb');
  900. fwrite($fp, ob_get_contents());
  901. fclose($fp);
  902. ob_end_flush(); // Send the output to the browser
  903. die();
  904. } else {
  905. die("Invalid Request");
  906. }
  907. }
  908. function cacheImage($url,$name){
  909. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  910. if (!file_exists($cacheDirectory)) {
  911. mkdir($cacheDirectory, 0777, true);
  912. }
  913. $cachefile = $cacheDirectory.$name.'.jpg';
  914. copy($url, $cachefile);
  915. }
  916. function downloader($array){
  917. switch ($array['data']['source']) {
  918. case 'sabnzbd':
  919. switch ($array['data']['action']) {
  920. case 'resume':
  921. case 'pause':
  922. sabnzbdAction($array['data']['action'],$array['data']['target']);
  923. break;
  924. default:
  925. # code...
  926. break;
  927. }
  928. break;
  929. case 'nzbget':
  930. break;
  931. default:
  932. # code...
  933. break;
  934. }
  935. }
  936. function sabnzbdAction($action=null, $target=null) {
  937. if($GLOBALS['homepageSabnzbdEnabled'] && !empty($GLOBALS['sabnzbdURL']) && !empty($GLOBALS['sabnzbdToken']) && qualifyRequest($GLOBALS['homepageSabnzbdAuth'])){
  938. $url = qualifyURL($GLOBALS['sabnzbdURL']);
  939. switch ($action) {
  940. case 'pause':
  941. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=pause&value='.$target.'&' : 'mode=pause';
  942. $url = $url.'/api?'.$id.'&output=json&apikey='.$GLOBALS['sabnzbdToken'];
  943. break;
  944. case 'resume':
  945. $id = ($target !== '' && $target !== 'main' && isset($target)) ? 'mode=queue&name=resume&value='.$target.'&' : 'mode=resume';
  946. $url = $url.'/api?'.$id.'&output=json&apikey='.$GLOBALS['sabnzbdToken'];
  947. break;
  948. default:
  949. # code...
  950. break;
  951. }
  952. try{
  953. $options = (localURL($url)) ? array('verify' => false ) : array();
  954. $response = Requests::get($url, array(), $options);
  955. if($response->success){
  956. $api['content'] = json_decode($response->body, true);
  957. }
  958. }catch( Requests_Exception $e ) {
  959. writeLog('error', 'SabNZBd Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  960. };
  961. $api['content'] = isset($api['content']) ? $api['content'] : false;
  962. return $api;
  963. }
  964. }
  965. function getOrgUsers(){
  966. $result = allUsers();
  967. if (is_array($result) || is_object($result)){
  968. foreach($result['users'] as $k => $v){
  969. $return[$v['username']] = $v['email'];
  970. }
  971. return $return;
  972. }
  973. }
  974. function convertPlexName($user, $type){
  975. $array = libraryList('plex');
  976. switch ($type){
  977. case "username":
  978. case "u":
  979. $plexUser = array_search ($user, $array['users']);
  980. break;
  981. case "id":
  982. if (array_key_exists(strtolower($user), $array['users'])) {
  983. $plexUser = $array['users'][strtolower($user)];
  984. }
  985. break;
  986. default:
  987. $plexUser = false;
  988. }
  989. return (!empty($plexUser) ? $plexUser : null );
  990. }
  991. function libraryList($type=null){
  992. switch ($type) {
  993. case 'plex':
  994. if(!empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'])){
  995. $url = 'https://plex.tv/api/servers/'.$GLOBALS['plexID'].'/shared_servers';
  996. try{
  997. $headers = array(
  998. "Accept" => "application/json",
  999. "X-Plex-Token" => $GLOBALS['plexToken']
  1000. );
  1001. $response = Requests::get($url, $headers, array());
  1002. libxml_use_internal_errors(true);
  1003. if($response->success){
  1004. $libraryList = array();
  1005. $plex = simplexml_load_string($response->body);
  1006. foreach($plex->SharedServer->Section AS $child) {
  1007. $libraryList['libraries'][(string)$child['title']] = (string)$child['id'];
  1008. }
  1009. foreach($plex->SharedServer AS $child) {
  1010. if(!empty($child['username'])){
  1011. $username = (string)strtolower($child['username']);
  1012. $email = (string)strtolower($child['email']);
  1013. $libraryList['users'][$username] = (string)$child['id'];
  1014. $libraryList['emails'][$email] = (string)$child['id'];
  1015. $libraryList['both'][$username] = $email;
  1016. }
  1017. }
  1018. $libraryList = array_change_key_case($libraryList,CASE_LOWER);
  1019. return $libraryList;
  1020. }
  1021. }catch( Requests_Exception $e ) {
  1022. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  1023. };
  1024. }
  1025. break;
  1026. default:
  1027. # code...
  1028. break;
  1029. }
  1030. return false;
  1031. }
  1032. function plexJoinAPI($array){
  1033. return plexJoin($array['data']['username'],$array['data']['email'],$array['data']['password']);
  1034. }
  1035. function plexJoin($username, $email, $password){
  1036. try{
  1037. $url = 'https://plex.tv/users.json';
  1038. $headers = array(
  1039. 'Accept'=> 'application/json',
  1040. 'Content-Type' => 'application/x-www-form-urlencoded',
  1041. 'X-Plex-Product' => 'Organizr',
  1042. 'X-Plex-Version' => '2.0',
  1043. 'X-Plex-Client-Identifier' => '01010101-10101010',
  1044. );
  1045. $data = array(
  1046. 'user[email]' => $email,
  1047. 'user[username]' => $username,
  1048. 'user[password]' => $password,
  1049. );
  1050. $response = Requests::post($url, $headers, $data, array());
  1051. $json = json_decode($response->body, true);
  1052. $errors = (!empty($json['errors']) ? true : false);
  1053. $success = (!empty($json['user']) ? true : false);
  1054. //Use This for later
  1055. $usernameError = (!empty($json['errors']['username']) ? $json['errors']['username'][0] : false);
  1056. $emailError = (!empty($json['errors']['email']) ? $json['errors']['email'][0] : false);
  1057. $passwordError = (!empty($json['errors']['password']) ? $json['errors']['password'][0] : false);
  1058. $errorMessage = "";
  1059. if($errors){
  1060. if($usernameError){ $errorMessage .= "[Username Error: ". $usernameError ."]"; }
  1061. if($emailError){ $errorMessage .= "[Email Error: ". $emailError ."]"; }
  1062. if($passwordError){ $errorMessage .= "[Password Error: ". $passwordError ."]"; }
  1063. }
  1064. return (!empty($success) && empty($errors) ? true : $errorMessage );
  1065. }catch( Requests_Exception $e ) {
  1066. writeLog('error', 'Plex.TV Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  1067. };
  1068. return false;
  1069. }