organizr-functions.php 28 KB

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