ajax.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. if(inviteCodes("check", $_POST['invitecode'])){
  107. $response = inviteCodes("use", $_POST['invitecode'], $_POST['inviteuser']);
  108. $response['notify'] = sendResult(plexUserShare($_POST['inviteuser']), "check", $_POST['checkurl'], "INVITE_SUCCESS", "INVITE_ERROR");
  109. }
  110. break;
  111. case 'join-plex':
  112. $response = plexJoin($_POST['joinuser'], $_POST['joinemail'], $_POST['joinpassword']);
  113. $response['notify'] = sendResult($response, "check", $_POST['checkurl'], "JOIN_SUCCESS", "JOIN_ERROR");
  114. break;
  115. default: // Stuff that you need admin for
  116. qualifyUser('admin', true);
  117. switch ($action) {
  118. case 'test-email':
  119. 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");
  120. break;
  121. case 'check-url':
  122. sendResult(frameTest($_POST['checkurl']), "flask", $_POST['checkurl'], "IFRAME_CAN_BE_FRAMED", "IFRAME_CANNOT_BE_FRAMED");
  123. break;
  124. case 'backup-now':
  125. sendResult(backupDB(), "database", "Backup", "BACKUP_CREATED", "BACKUP_ERROR");
  126. break;
  127. case 'upload-images':
  128. uploadFiles('images/', array('jpg', 'png', 'svg', 'jpeg', 'bmp', 'gif'));
  129. sendNotification(true);
  130. break;
  131. case 'upload-avatar':
  132. uploadAvatar(USER_HOME.$GLOBALS['USER']->username.'/', array('jpg', 'png', 'svg', 'jpeg', 'bmp', 'gif'));
  133. sendNotification(true);
  134. break;
  135. case 'remove-images':
  136. removeFiles('images/'.(isset($_POST['file'])?$_POST['file']:''));
  137. sendNotification(true);
  138. break;
  139. case 'update-config':
  140. sendNotification(updateConfig($_POST));
  141. break;
  142. case 'update-appearance':
  143. // Custom CSS Special Case START
  144. if (isset($_POST['customCSS'])) {
  145. if ($_POST['customCSS']) {
  146. write_ini_file($_POST['customCSS'], 'custom.css');
  147. } else {
  148. unlink('custom.css');
  149. }
  150. $response['show_apply'] = true;
  151. }
  152. unset($_POST['customCSS']);
  153. // Custom CSS Special Case END
  154. if (!empty($_POST)) {
  155. $response['notify'] = sendNotification(updateDBOptions($_POST),false,false);
  156. }
  157. break;
  158. case 'deleteDB':
  159. deleteDatabase();
  160. sendNotification(true, 'Database Deleted!');
  161. break;
  162. case 'upgradeInstall':
  163. upgradeInstall();
  164. $response['notify'] = sendNotification(true, 'Performing Checks', false);
  165. $response['tab']['goto'] = 'updatedb.php';
  166. break;
  167. case 'forceBranchInstall':
  168. upgradeInstall(GIT_BRANCH);
  169. $response['notify'] = sendNotification(true, 'Performing Checks', false);
  170. $response['tab']['goto'] = 'updatedb.php';
  171. break;
  172. case 'deleteLog':
  173. sendNotification(unlink(FAIL_LOG));
  174. break;
  175. case 'deleteOrgLog':
  176. sendNotification(unlink("org.log"));
  177. break;
  178. case 'submit-tabs':
  179. $response['notify'] = sendNotification(updateTabs($_POST) , false, false);
  180. $response['show_apply'] = true;
  181. break;
  182. default:
  183. sendNotification(false, 'Unsupported Action!');
  184. }
  185. }
  186. break;
  187. case 'PUT':
  188. sendNotification(false, 'Unsupported Action!');
  189. break;
  190. case 'DELETE':
  191. sendNotification(false, 'Unsupported Action!');
  192. break;
  193. default:
  194. sendNotification(false, 'Unknown Request Type!');
  195. }
  196. if ($response) {
  197. header('Content-Type: application/json');
  198. echo json_encode($response);
  199. die();
  200. } else {
  201. sendNotification(false, 'Error: No Output Specified!');
  202. }