ajax.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. );
  101. foreach($allMailBoxes as $mailBox) {
  102. $editBox['options'][0]['values'][] = $mailBox->getUsername();
  103. }
  104. echo json_encode($editBox);
  105. break;
  106. case 'getMailAccountsByUid':
  107. header("Status: 200 OK");
  108. $mailboxes = Mailbox::getMailboxesByUserId($_REQUEST['uId']);
  109. $mbArray = array();
  110. foreach($mailboxes as $mailbox) {
  111. $mbArray[$mailbox->getId()] = $mailbox->getUsername();
  112. }
  113. echo json_encode($mbArray);
  114. break;
  115. case 'getNewDocumentBox':
  116. header("Status: 200 OK");
  117. // TODO: add reload on draft change
  118. // TODO: add draft-fields as options, depends on ^
  119. $editBox = array(
  120. "options" => array(
  121. array(
  122. "type" => "select",
  123. "name" => "vorlage",
  124. "value" => Document::getDefaultDraft()->filename,
  125. "values" => Document::getAllDrafts()
  126. ),
  127. array(
  128. "type" => "text",
  129. "name" => "filename",
  130. "value" => str_replace(".docx", "", Document::getDefaultDraft()->filename) . "_" . date("d_m_Y") . ".docx" // TODO: get value based on draft
  131. )
  132. ),
  133. "title" => "Neues Dokument"
  134. );
  135. echo json_encode($editBox);
  136. break;
  137. case 'getDraftVars':
  138. header("Status: 200 OK");
  139. // TODO: handle no draft given
  140. $documentHandle = new \PhpOffice\PhpWord\TemplateProcessor('drafts/' . $_REQUEST['draft']);
  141. $templateVars = $documentHandle->getVariables();
  142. echo json_encode(array_values($templateVars));
  143. break;
  144. case 'getMailboxStatus':
  145. header("Status: 200 OK");
  146. $mailbox = Mailbox::getMailboxById($_REQUEST['mailboxId'], true);
  147. $mailboxStatus = array(
  148. "connected" => $mailbox->getConnected()
  149. );
  150. echo json_encode($mailboxStatus);
  151. break;
  152. case 'saveNewDocument':
  153. header("Status: 200 OK");
  154. $newDocument = new DocumentHandler(Label::getLabelById($_REQUEST['labelId'])->getPath() . '/' . $_REQUEST['filename'], $_REQUEST['draft']);
  155. foreach($_REQUEST['draftVars'] as $key => $draftVar) {
  156. $newDocument->setVal($key, htmlspecialchars($draftVar));
  157. }
  158. $newDocument->saveFile();
  159. echo json_encode($_REQUEST);
  160. break;
  161. case 'getNewLabelBox':
  162. header("Status: 200 OK");
  163. $editBox = array(
  164. "options" => array(
  165. array(
  166. "type" => "text",
  167. "name" => "name",
  168. "value" => ""
  169. ),
  170. array(
  171. "type" => "text",
  172. "name" => "path",
  173. "value" => ""
  174. )
  175. ),
  176. "title" => "Neues Label"
  177. );
  178. echo json_encode($editBox);
  179. break;
  180. case 'saveNewLabel':
  181. header("Status: 200 OK");
  182. Label::addLabel($_REQUEST['name'], $_REQUEST['path']);
  183. // TODO: get status by DB (See: #40, #43)
  184. $return = array(
  185. "status" => "OK"
  186. );
  187. echo json_encode($return);
  188. break;
  189. case 'removeLabel':
  190. header("Status: 200 OK");
  191. Label::removeLabel($_REQUEST['labelId']);
  192. $return = array(
  193. "status" => "OK"
  194. );
  195. echo json_encode($return);
  196. break;
  197. case 'debugTest': // for testing single methods etc.
  198. pa(Document::getDefaultDraft());
  199. break;
  200. default:
  201. header("Status: 400 No Action Defined");
  202. echo 'error';
  203. break;
  204. }
  205. //pa($_POST); // Debug
  206. ?>