ajax.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. // Include functions and user
  3. require_once('functions.php');
  4. require_once("user.php");
  5. $GLOBALS['USER'] = new User('registration_callback');
  6. // Upgrade environment
  7. upgradeCheck();
  8. // Lazyload settings
  9. $databaseConfig = configLazy('config/config.php');
  10. // Get Action
  11. if (isset($_POST['submit'])) { $action = $_POST['submit']; }
  12. if (isset($_POST['action'])) { $action = $_POST['action']; }
  13. if (isset($_GET['action'])) { $action = $_GET['action']; }
  14. if (isset($_GET['a'])) { $action = $_GET['a']; }
  15. unset($_POST['action']);
  16. // No Action
  17. if (!isset($action)) {
  18. sendNotification(false, 'No Action Specified!');
  19. }
  20. // Process Request
  21. $response = array();
  22. switch ($_SERVER['REQUEST_METHOD']) {
  23. case 'GET':
  24. switch ($action) {
  25. case 'get-backups':
  26. qualifyUser("admin", true);
  27. $response = implode("\n",getBackups());
  28. break;
  29. case 'get-calendar':
  30. echo json_encode(getCalendar());
  31. die();
  32. break;
  33. case 'show-file':
  34. $auth = ($_SERVER['HTTP_REFERER'] ? true : false);
  35. if ($auth === false) { die("WTF? Bro! This is an internal function only"); }
  36. showFile();
  37. die();
  38. break;
  39. case 'emby-image':
  40. qualifyUser(EMBYHOMEAUTH, true);
  41. getEmbyImage();
  42. die();
  43. break;
  44. case 'plex-image':
  45. qualifyUser(PLEXHOMEAUTH, true);
  46. getPlexImage();
  47. die();
  48. break;
  49. case 'emby-streams':
  50. qualifyUser(EMBYHOMEAUTH, true);
  51. echo getEmbyStreams(12, EMBYSHOWNAMES, $GLOBALS['USER']->role);
  52. die();
  53. break;
  54. case 'plex-streams':
  55. qualifyUser(PLEXHOMEAUTH, true);
  56. echo getPlexStreams(12, PLEXSHOWNAMES, $GLOBALS['USER']->role);
  57. die();
  58. break;
  59. case 'emby-recent':
  60. qualifyUser(EMBYHOMEAUTH, true);
  61. echo getEmbyRecent($_GET['type'], 12);
  62. die();
  63. break;
  64. case 'plex-recent':
  65. qualifyUser(PLEXHOMEAUTH, true);
  66. echo getPlexRecent(array("movie" => PLEXRECENTMOVIE, "season" => PLEXRECENTTV, "album" => PLEXRECENTMUSIC));
  67. die();
  68. break;
  69. case 'sabnzbd-update':
  70. qualifyUser(SABNZBDHOMEAUTH, true);
  71. echo sabnzbdConnect($_GET['list'] ? $_GET['list'] : die('Error!'));
  72. die();
  73. break;
  74. case 'nzbget-update':
  75. qualifyUser(NZBGETHOMEAUTH, true);
  76. echo nzbgetConnect($_GET['list'] ? $_GET['list'] : die('Error!'));
  77. die();
  78. break;
  79. case 'show-image':
  80. qualifyUser(NZBGETHOMEAUTH, true);
  81. header('Content-type: image/jpeg');
  82. echo file_get_contents($_GET['image']);
  83. die();
  84. break;
  85. default:
  86. sendNotification(false, 'Unsupported Action!');
  87. }
  88. break;
  89. case 'POST':
  90. // Check if the user is an admin and is allowed to commit values
  91. switch ($action) {
  92. case 'tvdb-get':
  93. $response = tvdbGet($_POST['id']);
  94. break;
  95. case 'tvdb-search':
  96. $response = tvdbSearch($_POST['name'], $_POST['type']);
  97. break;
  98. case 'search-plex':
  99. $response = searchPlex($_POST['searchtitle']);
  100. break;
  101. case 'validate-invite':
  102. $response = inviteCodes("check", $_POST['invitecode']);
  103. $response['notify'] = sendResult($response, "check", $_POST['checkurl'], "CODE_SUCCESS", "CODE_ERROR");
  104. break;
  105. case 'use-invite':
  106. //$response = inviteCodes("check", $_POST['invitecode']);
  107. //$response = inviteCodes("use", $_POST['invitecode']);
  108. if(inviteCodes("check", $_POST['invitecode'])){
  109. $response = inviteCodes("use", $_POST['invitecode'], $_POST['inviteuser']);
  110. $response['notify'] = sendResult(plexUserShare($_POST['inviteuser']), "check", $_POST['checkurl'], "INVITE_SUCCESS", "INVITE_ERROR");
  111. }
  112. break;
  113. case 'join-plex':
  114. $response = plexJoin($_POST['joinuser'], $_POST['joinemail'], $_POST['joinpassword']);
  115. $response['notify'] = sendResult($response, "check", $_POST['checkurl'], "JOIN_SUCCESS", "JOIN_ERROR");
  116. break;
  117. default: // Stuff that you need admin for
  118. qualifyUser('admin', true);
  119. switch ($action) {
  120. case 'test-email':
  121. sendResult(sendTestEmail($_POST['emailto'], $_POST['emailsenderemail'], $_POST['emailhost'], $_POST['emailauth'], $_POST['emailusername'], $_POST['emailpassword'], $_POST['emailtype'], $_POST['emailport'], $_POST['emailsendername']), "flask", "E-Mail TEST", "SUCCESS", "ERROR");
  122. break;
  123. case 'check-url':
  124. sendResult(frameTest($_POST['checkurl']), "flask", $_POST['checkurl'], "IFRAME_CAN_BE_FRAMED", "IFRAME_CANNOT_BE_FRAMED");
  125. break;
  126. case 'backup-now':
  127. sendResult(backupDB(), "database", "Backup", "BACKUP_CREATED", "BACKUP_ERROR");
  128. break;
  129. case 'upload-images':
  130. uploadFiles('images/', array('jpg', 'png', 'svg', 'jpeg', 'bmp', 'gif'));
  131. sendNotification(true);
  132. break;
  133. case 'upload-avatar':
  134. uploadAvatar(USER_HOME.$GLOBALS['USER']->username.'/', array('jpg', 'png', 'svg', 'jpeg', 'bmp', 'gif'));
  135. sendNotification(true);
  136. break;
  137. case 'remove-images':
  138. removeFiles('images/'.(isset($_POST['file'])?$_POST['file']:''));
  139. sendNotification(true);
  140. break;
  141. case 'update-config':
  142. sendNotification(updateConfig($_POST));
  143. break;
  144. case 'update-appearance':
  145. // Custom CSS Special Case START
  146. if (isset($_POST['customCSS'])) {
  147. if ($_POST['customCSS']) {
  148. write_ini_file($_POST['customCSS'], 'custom.css');
  149. } else {
  150. unlink('custom.css');
  151. }
  152. $response['show_apply'] = true;
  153. }
  154. unset($_POST['customCSS']);
  155. // Custom CSS Special Case END
  156. if (!empty($_POST)) {
  157. $response['notify'] = sendNotification(updateDBOptions($_POST),false,false);
  158. }
  159. break;
  160. case 'deleteDB':
  161. deleteDatabase();
  162. sendNotification(true, 'Database Deleted!');
  163. break;
  164. case 'upgradeInstall':
  165. upgradeInstall();
  166. $response['notify'] = sendNotification(true, 'Performing Checks', false);
  167. $response['tab']['goto'] = 'updatedb.php';
  168. break;
  169. case 'forceBranchInstall':
  170. upgradeInstall(GIT_BRANCH);
  171. $response['notify'] = sendNotification(true, 'Performing Checks', false);
  172. $response['tab']['goto'] = 'updatedb.php';
  173. break;
  174. case 'deleteLog':
  175. sendNotification(unlink(FAIL_LOG));
  176. break;
  177. case 'deleteOrgLog':
  178. sendNotification(unlink("org.log"));
  179. break;
  180. case 'submit-tabs':
  181. $response['notify'] = sendNotification(updateTabs($_POST) , false, false);
  182. $response['show_apply'] = true;
  183. break;
  184. default:
  185. sendNotification(false, 'Unsupported Action!');
  186. }
  187. }
  188. break;
  189. case 'PUT':
  190. sendNotification(false, 'Unsupported Action!');
  191. break;
  192. case 'DELETE':
  193. sendNotification(false, 'Unsupported Action!');
  194. break;
  195. default:
  196. sendNotification(false, 'Unknown Request Type!');
  197. }
  198. if ($response) {
  199. header('Content-Type: application/json');
  200. echo json_encode($response);
  201. die();
  202. } else {
  203. sendNotification(false, 'Error: No Output Specified!');
  204. }