ajax.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. $db = new Database($CONFIG['dbHost'], $CONFIG['dbUser'], $CONFIG['dbPassword'], $CONFIG['dbDatabase']);
  13. switch($_REQUEST['action']) {
  14. case 'updateMailaccounts':
  15. switch($_POST['name']) {
  16. case 'hostname':
  17. header("Status: 200 OK");
  18. $db->updateQuery("UPDATE `mailboxes` SET `server`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  19. break;
  20. case 'port':
  21. header("Status: 200 OK");
  22. $db->updateQuery("UPDATE `mailboxes` SET `port`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  23. break;
  24. case 'protocol':
  25. header("Status: 200 OK");
  26. $db->updateQuery("UPDATE `mailboxes` SET `protocol`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  27. break;
  28. case 'use-ssl':
  29. header("Status: 200 OK");
  30. $db->updateQuery("UPDATE `mailboxes` SET `use_ssl`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  31. break;
  32. case 'no-valid-cert':
  33. header("Status: 200 OK");
  34. $db->updateQuery("UPDATE `mailboxes` SET `valid_ssl`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  35. break;
  36. case 'username':
  37. header("Status: 200 OK");
  38. $db->updateQuery("UPDATE `mailboxes` SET `username`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  39. break;
  40. case 'password':
  41. header("Status: 200 OK");
  42. $db->updateQuery("UPDATE `mailboxes` SET `password`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  43. break;
  44. default:
  45. header("Status: 400 Wrong Field");
  46. echo 'error';
  47. break;
  48. }
  49. break;
  50. case 'updateMailfolder':
  51. switch($_POST['name']) {
  52. case 'mailaccount':
  53. header("Status: 200 OK");
  54. $db->updateQuery("UPDATE `mailbox-folders` SET `mailbox_id`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  55. break;
  56. case 'mailfolder':
  57. header("Status: 200 OK");
  58. $db->updateQuery("UPDATE `mailbox-folders` SET `folder_name`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  59. break;
  60. default:
  61. header("Status: 400 Wrong Field");
  62. echo 'error';
  63. break;
  64. }
  65. break;
  66. case 'addDefaultMailaccount':
  67. header("Status: 200 OK");
  68. $db->insertQuery("INSERT INTO `mailboxes` (`user_id`) VALUES (" . $_POST['user-id'] . ");");
  69. break;
  70. case 'addDefaultMailfolder':
  71. header("Status: 200 OK");
  72. $db->insertQuery("INSERT INTO `mailbox-folders` (`label_id`) VALUES (" . $_POST['label-id'] . ");");
  73. break;
  74. case 'removeMailaccount':
  75. header("Status: 200 OK");
  76. $db->removeQuery("DELETE FROM `mailboxes` WHERE `id` = " . $_POST['id'] . ";");
  77. break;
  78. case 'removeMailboxFolder':
  79. header("Status: 200 OK");
  80. $db->removeQuery("DELETE FROM `mailbox-folders` WHERE `id` = " . $_POST['id'] . ";");
  81. break;
  82. case 'manageMailboxFolder':
  83. header("Status: 200 OK");
  84. $allMailBoxes = Mailbox::getAllMailboxes(false);
  85. $editBox = array(
  86. "options" => array(
  87. array(
  88. "type" => "select",
  89. "name" => "account",
  90. "value" => Mailbox::getMailboxNameFromMailaccountId($_GET['mbId']),
  91. "values" => array()
  92. ),
  93. array(
  94. "type" => "text",
  95. "name" => "folder",
  96. "value" => MailboxFolder::getMailboxFolderNameFromId($_GET['mfId'])
  97. )
  98. ),
  99. "title" => "Mailkonto bearbeiten",
  100. "mailboxFolderID" => $_GET['mfId']
  101. );
  102. foreach($allMailBoxes as $mailBox) {
  103. $editBox['options'][0]['values'][] = $mailBox->getUsername();
  104. }
  105. echo json_encode($editBox);
  106. break;
  107. case 'getMailAccountsByUid':
  108. header("Status: 200 OK");
  109. $mailboxes = Mailbox::getMailboxesByUserId($_REQUEST['uId']);
  110. $mbArray = array();
  111. foreach($mailboxes as $mailbox) {
  112. $mbArray[$mailbox->getId()] = $mailbox->getUsername();
  113. }
  114. echo json_encode($mbArray);
  115. break;
  116. case 'getNewDocumentBox':
  117. header("Status: 200 OK");
  118. // TODO: add reload on draft change
  119. // TODO: add draft-fields as options, depends on ^
  120. $editBox = array(
  121. "options" => array(
  122. array(
  123. "type" => "select",
  124. "name" => "vorlage",
  125. "value" => Document::getDefaultDraft()->filename,
  126. "values" => Document::getAllDrafts()
  127. ),
  128. array(
  129. "type" => "text",
  130. "name" => "filename",
  131. "value" => str_replace(".docx", "", Document::getDefaultDraft()->filename) . "_" . date("d_m_Y") . ".docx" // TODO: get value based on draft
  132. )
  133. ),
  134. "title" => "Neues Dokument"
  135. );
  136. echo json_encode($editBox);
  137. break;
  138. case 'getDraftVars':
  139. header("Status: 200 OK");
  140. // TODO: handle no draft given
  141. $documentHandle = new \PhpOffice\PhpWord\TemplateProcessor('drafts/' . $_REQUEST['draft']);
  142. $templateVars = $documentHandle->getVariables();
  143. echo json_encode(array_values($templateVars));
  144. break;
  145. case 'getMailboxStatus':
  146. header("Status: 200 OK");
  147. $mailbox = Mailbox::getMailboxById($_REQUEST['mailboxId'], true);
  148. $mailboxStatus = array(
  149. "connected" => $mailbox->getConnected()
  150. );
  151. echo json_encode($mailboxStatus);
  152. break;
  153. case 'saveNewDocument':
  154. header("Status: 200 OK");
  155. $newDocument = new DocumentHandler(Label::getLabelById($_REQUEST['labelId'])->getPath() . '/' . $_REQUEST['filename'], $_REQUEST['draft']);
  156. foreach($_REQUEST['draftVars'] as $key => $draftVar) {
  157. $newDocument->setVal($key, htmlspecialchars($draftVar));
  158. }
  159. $newDocument->saveFile();
  160. echo json_encode($_REQUEST);
  161. break;
  162. case 'getNewLabelBox':
  163. header("Status: 200 OK");
  164. $editBox = array(
  165. "options" => array(
  166. array(
  167. "type" => "text",
  168. "name" => "name",
  169. "value" => ""
  170. ),
  171. array(
  172. "type" => "text",
  173. "name" => "path",
  174. "value" => ""
  175. )
  176. ),
  177. "title" => "Neues Label"
  178. );
  179. echo json_encode($editBox);
  180. break;
  181. case 'saveNewLabel':
  182. header("Status: 200 OK");
  183. Label::addLabel($_REQUEST['name'], $_REQUEST['path']);
  184. // TODO: get status by DB (See: #40, #43)
  185. $return = array(
  186. "status" => "OK"
  187. );
  188. echo json_encode($return);
  189. break;
  190. case 'removeLabel':
  191. header("Status: 200 OK");
  192. Label::removeLabel($_REQUEST['labelId']);
  193. // TODO: get status by DB (See: #40, #43)
  194. $return = array(
  195. "status" => "OK"
  196. );
  197. echo json_encode($return);
  198. break;
  199. case 'saveMailFolder':
  200. header("Status: 200 OK");
  201. MailboxFolder::updateMailboxFolder($_REQUEST['mailboxFolderID'], $_REQUEST['folder'], Mailbox::getMailboxByUsername($_REQUEST['account'])->getId(), $_REQUEST['labelID']);
  202. // TODO: get status by DB (See: #40, #43)
  203. $return = array(
  204. "status" => "OK"
  205. );
  206. echo json_encode($return);
  207. break;
  208. case 'saveNewMailFolder':
  209. header("Status: 200 OK");
  210. MailboxFolder::addMailboxFolder($_REQUEST['folder'], Mailbox::getMailboxByUsername($_REQUEST['account'])->getId(), $_REQUEST['labelID']);
  211. // TODO: get status by DB (See: #40, #43)
  212. $return = array(
  213. "status" => "OK"
  214. );
  215. echo json_encode($return);
  216. break;
  217. case 'debugTest': // for testing single methods etc.
  218. pa(MailBox::getMailboxByUsername($_REQUEST['account'])->getId());
  219. break;
  220. default:
  221. header("Status: 400 No Action Defined");
  222. echo 'error';
  223. break;
  224. }
  225. //pa($_POST); // Debug
  226. ?>