ajax.php 8.4 KB

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