ajax.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 'get-ping':
  34. echo getPing($_GET['url'], $_GET['style'], true);
  35. die();
  36. break;
  37. case 'show-file':
  38. $auth = ($_SERVER['HTTP_REFERER'] ? true : false);
  39. if ($auth === false) { die("WTF? Bro! This is an internal function only"); }
  40. showFile();
  41. die();
  42. break;
  43. case 'emby-image':
  44. qualifyUser(EMBYHOMEAUTH, true);
  45. getEmbyImage();
  46. die();
  47. break;
  48. case 'plex-image':
  49. qualifyUser(PLEXHOMEAUTH, true);
  50. getPlexImage();
  51. die();
  52. break;
  53. case 'emby-streams':
  54. qualifyUser(EMBYHOMEAUTH, true);
  55. echo getEmbyStreams(12, EMBYSHOWNAMES, $GLOBALS['USER']->role);
  56. die();
  57. break;
  58. case 'plex-streams':
  59. qualifyUser(PLEXHOMEAUTH, true);
  60. echo getPlexStreams(12, PLEXSHOWNAMES, $GLOBALS['USER']->role);
  61. die();
  62. break;
  63. case 'ombi-requests':
  64. qualifyUser(PLEXHOMEAUTH, true);
  65. echo buildOmbiList($GLOBALS['USER']->role, $GLOBALS['USER']->username);
  66. die();
  67. break;
  68. case 'emby-recent':
  69. qualifyUser(EMBYHOMEAUTH, true);
  70. echo getEmbyRecent(array("Movie" => EMBYRECENTMOVIE, "Episode" => EMBYRECENTTV, "MusicAlbum" => EMBYRECENTMUSIC, "Series" => EMBYRECENTTV));
  71. die();
  72. break;
  73. case 'plex-recent':
  74. qualifyUser(PLEXHOMEAUTH, true);
  75. echo getPlexRecent(array("movie" => PLEXRECENTMOVIE, "season" => PLEXRECENTTV, "album" => PLEXRECENTMUSIC));
  76. die();
  77. break;
  78. case 'sabnzbd-update':
  79. qualifyUser(SABNZBDHOMEAUTH, true);
  80. echo sabnzbdConnect($_GET['list'] ? $_GET['list'] : die('Error!'));
  81. die();
  82. break;
  83. case 'nzbget-update':
  84. qualifyUser(NZBGETHOMEAUTH, true);
  85. echo nzbgetConnect($_GET['list'] ? $_GET['list'] : die('Error!'));
  86. die();
  87. break;
  88. case 'show-image':
  89. qualifyUser(NZBGETHOMEAUTH, true);
  90. header('Content-type: image/jpeg');
  91. echo file_get_contents($_GET['image']);
  92. die();
  93. break;
  94. default:
  95. sendNotification(false, 'Unsupported Action!');
  96. }
  97. break;
  98. case 'POST':
  99. // Check if the user is an admin and is allowed to commit values
  100. switch ($action) {
  101. case 'tvdb-get':
  102. $response = tvdbGet($_POST['id']);
  103. break;
  104. case 'tvdb-search':
  105. $response = tvdbSearch($_POST['name'], $_POST['type']);
  106. break;
  107. case 'search-plex':
  108. $response = searchPlex($_POST['searchtitle']);
  109. break;
  110. case 'validate-invite':
  111. $response = inviteCodes("check", $_POST['invitecode']);
  112. $response['notify'] = sendResult($response, "check", $_POST['checkurl'], "CODE_SUCCESS", "CODE_ERROR");
  113. break;
  114. case 'use-invite':
  115. if(inviteCodes("check", $_POST['invitecode'])){
  116. $response = inviteCodes("use", $_POST['invitecode'], $_POST['inviteuser']);
  117. $response['notify'] = sendResult(plexUserShare($_POST['inviteuser']), "check", $_POST['checkurl'], "INVITE_SUCCESS", "INVITE_ERROR");
  118. }
  119. break;
  120. case 'join-plex':
  121. $response = plexJoin($_POST['joinuser'], $_POST['joinemail'], $_POST['joinpassword']);
  122. $response['notify'] = sendResult($response, "check", $_POST['checkurl'], "JOIN_SUCCESS", "JOIN_ERROR");
  123. break;
  124. default: // Stuff that you need admin for
  125. qualifyUser('admin', true);
  126. switch ($action) {
  127. case 'ombi-action':
  128. sendResult(ombiAction($_POST['id'], $_POST['action_type'], $_POST['type']), "search", "OMBI ", "action completed successfully", "an error occured");
  129. break;
  130. case 'get-emails':
  131. $response = printEmails(getEmails($_POST['type']));
  132. break;
  133. case 'mass-email':
  134. massEmail($_POST['emailto'],$_POST['emailsubject'],$_POST['emailmessage']);
  135. $response['notify'] = sendNotification(true, 'E-Mail Sent', false);
  136. break;
  137. case 'test-email':
  138. 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");
  139. break;
  140. case 'check-url':
  141. sendResult(frameTest($_POST['checkurl']), "flask", $_POST['checkurl'], "IFRAME_CAN_BE_FRAMED", "IFRAME_CANNOT_BE_FRAMED");
  142. break;
  143. case 'backup-now':
  144. sendResult(backupDB(), "database", "Backup", "BACKUP_CREATED", "BACKUP_ERROR");
  145. break;
  146. case 'upload-images':
  147. uploadFiles('images/', array('jpg', 'png', 'svg', 'jpeg', 'bmp', 'gif'));
  148. sendNotification(true);
  149. break;
  150. case 'upload-avatar':
  151. uploadAvatar(USER_HOME.$GLOBALS['USER']->username.'/', array('jpg', 'png', 'svg', 'jpeg', 'bmp', 'gif'));
  152. sendNotification(true);
  153. break;
  154. case 'remove-images':
  155. removeFiles('images/'.(isset($_POST['file'])?$_POST['file']:''));
  156. sendNotification(true);
  157. break;
  158. case 'remove-file':
  159. removeFiles($_POST['file']);
  160. sendNotification(true);
  161. break;
  162. case 'update-config':
  163. $response['notify'] = sendNotification(updateConfig($_POST));
  164. $response['show_apply'] = true;
  165. break;
  166. case 'update-appearance':
  167. // Custom CSS Special Case START
  168. if (isset($_POST['customCSS'])) {
  169. if ($_POST['customCSS']) {
  170. write_ini_file($_POST['customCSS'], 'custom.css');
  171. } else {
  172. unlink('custom.css');
  173. }
  174. $response['notify'] = sendNotification(true,false,false);
  175. $response['show_apply'] = true;
  176. }
  177. unset($_POST['customCSS']);
  178. // Custom CSS Special Case END
  179. if (!empty($_POST)) {
  180. $response['notify'] = sendNotification(updateDBOptions($_POST),false,false);
  181. $response['show_apply'] = true;
  182. }
  183. break;
  184. case 'deleteDB':
  185. deleteDatabase();
  186. sendNotification(true, 'Database Deleted!');
  187. break;
  188. case 'upgradeInstall':
  189. upgradeInstall();
  190. $response['notify'] = sendNotification(true, 'Performing Checks', false);
  191. $response['tab']['goto'] = 'updatedb.php';
  192. break;
  193. case 'forceBranchInstall':
  194. upgradeInstall(GIT_BRANCH);
  195. $response['notify'] = sendNotification(true, 'Performing Checks', false);
  196. $response['tab']['goto'] = 'updatedb.php';
  197. break;
  198. case 'deleteLog':
  199. $response['notify'] = sendNotification(unlink(FAIL_LOG));
  200. $response['show_apply'] = true;
  201. break;
  202. case 'deleteChat':
  203. $response['notify'] = sendNotification(unlink("chatpack.db"));
  204. $response['show_apply'] = true;
  205. $response['reload'] = true;
  206. break;
  207. case 'deleteOrgLog':
  208. $response['notify'] = sendNotification(unlink("org.log"));
  209. $response['show_apply'] = true;
  210. break;
  211. case 'submit-tabs':
  212. $response['notify'] = sendNotification(updateTabs($_POST) , false, false);
  213. $response['show_apply'] = true;
  214. break;
  215. default:
  216. sendNotification(false, 'Unsupported Action!');
  217. }
  218. }
  219. break;
  220. case 'PUT':
  221. sendNotification(false, 'Unsupported Action!');
  222. break;
  223. case 'DELETE':
  224. sendNotification(false, 'Unsupported Action!');
  225. break;
  226. default:
  227. sendNotification(false, 'Unknown Request Type!');
  228. }
  229. if ($response) {
  230. header('Content-Type: application/json');
  231. echo json_encode($response);
  232. die();
  233. } else {
  234. sendNotification(false, 'Error: No Output Specified!');
  235. }