ajax.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. // AJAX handling
  3. // Includes
  4. require('includes/config.inc.php');
  5. require('includes/database.inc.php');
  6. require('includes/functions.inc.php');
  7. require('includes/document.inc.php');
  8. require('includes/label.inc.php');
  9. require('includes/mailbox.inc.php');
  10. require('includes/mailboxfolder.inc.php');
  11. require('includes/documenthandler.inc.php');
  12. require('includes/call.inc.php');
  13. require('includes/reminder.inc.php');
  14. require('includes/mail.inc.php');
  15. $db = new Database($CONFIG['dbHost'], $CONFIG['dbUser'], $CONFIG['dbPassword'], $CONFIG['dbDatabase']);
  16. switch($_REQUEST['action']) {
  17. case 'updateMailaccounts':
  18. switch($_POST['name']) {
  19. case 'hostname':
  20. header("Status: 200 OK");
  21. $db->updateQuery("UPDATE `mailboxes` SET `server`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  22. break;
  23. case 'port':
  24. header("Status: 200 OK");
  25. $db->updateQuery("UPDATE `mailboxes` SET `port`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  26. break;
  27. case 'protocol':
  28. header("Status: 200 OK");
  29. $db->updateQuery("UPDATE `mailboxes` SET `protocol`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  30. break;
  31. case 'use-ssl':
  32. header("Status: 200 OK");
  33. $db->updateQuery("UPDATE `mailboxes` SET `use_ssl`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  34. break;
  35. case 'no-valid-cert':
  36. header("Status: 200 OK");
  37. $db->updateQuery("UPDATE `mailboxes` SET `valid_ssl`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  38. break;
  39. case 'username':
  40. header("Status: 200 OK");
  41. $db->updateQuery("UPDATE `mailboxes` SET `username`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  42. break;
  43. case 'password':
  44. header("Status: 200 OK");
  45. $db->updateQuery("UPDATE `mailboxes` SET `password`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  46. break;
  47. default:
  48. header("Status: 400 Wrong Field");
  49. echo 'error';
  50. break;
  51. }
  52. break;
  53. case 'updateMailfolder':
  54. switch($_POST['name']) {
  55. case 'mailaccount':
  56. header("Status: 200 OK");
  57. $db->updateQuery("UPDATE `mailbox-folders` SET `mailbox_id`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  58. break;
  59. case 'mailfolder':
  60. header("Status: 200 OK");
  61. $db->updateQuery("UPDATE `mailbox-folders` SET `folder_name`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  62. break;
  63. default:
  64. header("Status: 400 Wrong Field");
  65. echo 'error';
  66. break;
  67. }
  68. break;
  69. case 'addDefaultMailaccount':
  70. header("Status: 200 OK");
  71. $db->insertQuery("INSERT INTO `mailboxes` (`user_id`) VALUES (" . $_POST['user-id'] . ");");
  72. break;
  73. case 'addDefaultMailfolder':
  74. header("Status: 200 OK");
  75. $db->insertQuery("INSERT INTO `mailbox-folders` (`label_id`) VALUES (" . $_POST['label-id'] . ");");
  76. break;
  77. case 'removeMailaccount':
  78. header("Status: 200 OK");
  79. $db->removeQuery("DELETE FROM `mailboxes` WHERE `id` = " . $_POST['id'] . ";");
  80. break;
  81. case 'removeMailboxFolder':
  82. header("Status: 200 OK");
  83. $db->removeQuery("DELETE FROM `mailbox-folders` WHERE `id` = " . $_POST['id'] . ";");
  84. break;
  85. case 'manageMailboxFolder':
  86. header("Status: 200 OK");
  87. $allMailBoxes = Mailbox::getAllMailboxes(true);
  88. $editBox = array(
  89. "options" => array(
  90. array(
  91. "type" => "select",
  92. "name" => "account",
  93. "value" => Mailbox::getMailboxNameFromMailaccountID($_GET['mbID']),
  94. "values" => array()
  95. ),
  96. array(
  97. "type" => "select",
  98. "name" => "folder",
  99. "value" => MailboxFolder::getMailboxFolderNameFromID($_GET['mfID']),
  100. "values" => array()
  101. )
  102. ),
  103. "title" => "Mailkonto bearbeiten",
  104. "mailboxFolderID" => $_GET['mfID']
  105. );
  106. foreach($allMailBoxes as $mailBox) {
  107. $mailBox->listFolders();
  108. $editBox['options'][0]['values'][] = $mailBox->getUsername();
  109. if($mailBox->getUsername() == Mailbox::getMailboxNameFromMailaccountID($_GET['mbID'])) {
  110. $editBox['options'][1]['values'] = $mailBox->getFolders();
  111. }
  112. }
  113. echo json_encode($editBox);
  114. break;
  115. case 'getMailAccountsByUid':
  116. header("Status: 200 OK");
  117. $mailboxes = Mailbox::getMailboxesByUserID($_REQUEST['uID']);
  118. $mbArray = array();
  119. foreach($mailboxes as $mailbox) {
  120. $mbArray[$mailbox->getID()] = $mailbox->getUsername();
  121. }
  122. echo json_encode($mbArray);
  123. break;
  124. case 'getNewDocumentBox':
  125. header("Status: 200 OK");
  126. $editBox = array(
  127. "options" => array(
  128. array(
  129. "type" => "select",
  130. "name" => "vorlage",
  131. "value" => Document::getDefaultDraft()->filename,
  132. "values" => Document::getAllDrafts()
  133. ),
  134. array(
  135. "type" => "text",
  136. "name" => "filename",
  137. "value" => str_replace(".docx", "", Document::getDefaultDraft()->filename) . "_" . date("d_m_Y") . ".docx"
  138. )
  139. ),
  140. "title" => "Neues Dokument"
  141. );
  142. echo json_encode($editBox);
  143. break;
  144. case 'getDraftVars':
  145. header("Status: 200 OK");
  146. $documentHandle = new \PhpOffice\PhpWord\TemplateProcessor('drafts/' . $_REQUEST['draft']);
  147. $templateVars = $documentHandle->getVariables();
  148. echo json_encode(array_values($templateVars));
  149. break;
  150. case 'getMailboxStatus':
  151. header("Status: 200 OK");
  152. $mailbox = Mailbox::getMailboxByID($_REQUEST['mailboxID'], true);
  153. $mailboxStatus = array(
  154. "connected" => $mailbox->getConnected()
  155. );
  156. echo json_encode($mailboxStatus);
  157. break;
  158. case 'saveNewDocument':
  159. header("Status: 200 OK");
  160. $newDocument = new DocumentHandler(Label::getLabelByID($_REQUEST['labelID'])->getPath() . '/' . $_REQUEST['filename'], $_REQUEST['draft']);
  161. foreach($_REQUEST['draftVars'] as $key => $draftVar) {
  162. $newDocument->setVal($key, htmlspecialchars($draftVar));
  163. }
  164. $newDocument->saveFile();
  165. echo json_encode($_REQUEST);
  166. break;
  167. case 'getNewLabelBox':
  168. header("Status: 200 OK");
  169. $editBox = array(
  170. "options" => array(
  171. array(
  172. "type" => "text",
  173. "name" => "name",
  174. "value" => ""
  175. ),
  176. array(
  177. "type" => "text",
  178. "name" => "path",
  179. "value" => ""
  180. )
  181. ),
  182. "title" => "Neues Label"
  183. );
  184. echo json_encode($editBox);
  185. break;
  186. case 'saveNewLabel':
  187. header("Status: 200 OK");
  188. Label::addLabel($_REQUEST['name'], $_REQUEST['path']);
  189. $return = array(
  190. "status" => "OK"
  191. );
  192. echo json_encode($return);
  193. break;
  194. case 'removeLabel':
  195. header("Status: 200 OK");
  196. Label::removeLabel($_REQUEST['labelID']);
  197. $return = array(
  198. "status" => "OK"
  199. );
  200. echo json_encode($return);
  201. break;
  202. case 'saveMailFolder':
  203. header("Status: 200 OK");
  204. MailboxFolder::updateMailboxFolder($_REQUEST['mailboxFolderID'], $_REQUEST['folder'], Mailbox::getMailboxByUsername($_REQUEST['account'])->getID(), $_REQUEST['labelID']);
  205. $return = array(
  206. "status" => "OK"
  207. );
  208. echo json_encode($return);
  209. break;
  210. case 'saveNewMailFolder':
  211. header("Status: 200 OK");
  212. MailboxFolder::addMailboxFolder($_REQUEST['folder'], Mailbox::getMailboxByUsername($_REQUEST['account'])->getID(), $_REQUEST['labelID']);
  213. $return = array(
  214. "status" => "OK"
  215. );
  216. echo json_encode($return);
  217. break;
  218. case 'getNewCallBox':
  219. header("Status: 200 OK");
  220. $allLabels = Label::getAllLabels();
  221. $editBox = array(
  222. "options" => array(
  223. array(
  224. "type" => "datetime",
  225. "name" => "call-date",
  226. "value" => ""
  227. ),
  228. array(
  229. "type" => "text",
  230. "name" => "caller-telnr",
  231. "value" => ""
  232. ),
  233. array(
  234. "type" => "select",
  235. "name" => "label-id",
  236. "value" => Label::getLabelByID($_REQUEST['labelID'])->getName(),
  237. "values" => array()
  238. ),
  239. array(
  240. "type" => "textarea",
  241. "name" => "call-notes",
  242. "value" => ""
  243. ),
  244. array(
  245. "type" => "checkbox",
  246. "name" => "call-set-reminder",
  247. "value" => ""
  248. )
  249. ),
  250. "title" => "Neuer Anruf"
  251. );
  252. foreach($allLabels as $label) {
  253. $editBox['options'][2]['values'][] = $label->getName();
  254. }
  255. echo json_encode($editBox);
  256. break;
  257. case 'saveNewCall':
  258. header("Status: 200 OK");
  259. Call::addCall($_REQUEST['userID'], $_REQUEST['callDate'], $_REQUEST['callerTelNr'], Label::getLabelByName($_REQUEST['labelID'])->getID(), $_REQUEST['callNotes']);
  260. $return = array(
  261. "status" => "OK"
  262. );
  263. echo json_encode($return);
  264. break;
  265. case 'getEvents':
  266. header("Status: 200 OK");
  267. $upcomingReminders = getObjectsAsArray(Reminder::getUpcomingRemindersByUserID($_REQUEST['userID']), array("id", "userID", "reminderDate", "remindedYet"));
  268. $return = array(
  269. "type" => $upcomingReminders == "" ? "ping" : "reminder",
  270. "reminders" => $upcomingReminders
  271. );
  272. echo json_encode($return);
  273. break;
  274. case 'setReminderReminded':
  275. header("Status: 200 OK");
  276. $reminder = Reminder::getReminderByID($_REQUEST['reminderID']);
  277. $reminder->setRemindedYet(1);
  278. $reminder->save();
  279. $return = array(
  280. "status" => "OK"
  281. );
  282. echo json_encode($return);
  283. break;
  284. case 'setReminderSnooze':
  285. header("Status: 200 OK");
  286. $reminder = Reminder::getReminderByID($_REQUEST['reminderID']);
  287. $reminder->setReminderDate(date("Y-m-d H:i:s", strtotime("+30 minutes")));
  288. $reminder->save();
  289. $return = array(
  290. "status" => "OK"
  291. );
  292. echo json_encode($return);
  293. break;
  294. case 'scanDocuments':
  295. header("Status: 200 OK");
  296. $return = scanDocuments($CONFIG['documentPath']);
  297. $return['status'] = "OK";
  298. echo json_encode($return);
  299. break;
  300. case 'getMailContent':
  301. header("Status: 200 OK");
  302. $mail = Mail::getMailByMailID($_REQUEST['mailID']);
  303. $mailboxFolder = Mailboxfolder::getMailboxfolderByID($mail->getMailboxFolderID());
  304. $mailbox = Mailbox::getMailboxByID($mailboxFolder->getMailboxID(), true);
  305. $mailbox->changeFolder($mailboxFolder->getFolderName());
  306. $bodyLines = explode(PHP_EOL, imap_fetchbody($mailbox->getMailbox(), $mail->getMailUID(), 1));
  307. $body = "%0D%0A"; // newline on top
  308. foreach($bodyLines as $line) {
  309. $body .= ">" . $line . '%0D%0A';
  310. }
  311. $return = array(
  312. "to" => $mail->getMailSender(),
  313. "subject" => $mail->getSubject(),
  314. "body" => $body
  315. );
  316. echo json_encode($return);
  317. case 'changeMailProcessed':
  318. header("Status: 200 OK");
  319. $mail = Mail::getMailByMailID($_REQUEST['mailID']);
  320. $mail->setProcessed($_REQUEST['value']);
  321. $mail->save();
  322. $return = array(
  323. "status" => "OK"
  324. );
  325. echo json_encode($return);
  326. break;
  327. case 'getMailaccountFoldersByUsername':
  328. header("Status: 200 OK");
  329. $mailbox = Mailbox::getMailboxByUsername($_REQUEST['account'], true);
  330. $mailbox->listFolders();
  331. echo json_encode($mailbox->getFolders());
  332. break;
  333. case 'debugTest': // for testing single methods etc.
  334. break;
  335. default:
  336. header("Status: 400 No Action Defined");
  337. echo 'error';
  338. break;
  339. }
  340. //pa($_POST); // Debug
  341. ?>