ajax.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. require('includes/contact.inc.php');
  16. $db = new Database($CONFIG['dbHost'], $CONFIG['dbUser'], $CONFIG['dbPassword'], $CONFIG['dbDatabase']);
  17. switch($_REQUEST['action']) {
  18. case 'updateMailaccounts':
  19. switch($_POST['name']) {
  20. case 'hostname':
  21. header("Status: 200 OK");
  22. $db->updateQuery("UPDATE `mailboxes` SET `server`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  23. break;
  24. case 'port':
  25. header("Status: 200 OK");
  26. $db->updateQuery("UPDATE `mailboxes` SET `port`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  27. break;
  28. case 'protocol':
  29. header("Status: 200 OK");
  30. $db->updateQuery("UPDATE `mailboxes` SET `protocol`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  31. break;
  32. case 'use-ssl':
  33. header("Status: 200 OK");
  34. $db->updateQuery("UPDATE `mailboxes` SET `use_ssl`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  35. break;
  36. case 'no-valid-cert':
  37. header("Status: 200 OK");
  38. $db->updateQuery("UPDATE `mailboxes` SET `valid_ssl`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  39. break;
  40. case 'username':
  41. header("Status: 200 OK");
  42. $db->updateQuery("UPDATE `mailboxes` SET `username`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  43. break;
  44. case 'password':
  45. header("Status: 200 OK");
  46. $db->updateQuery("UPDATE `mailboxes` SET `password`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  47. break;
  48. default:
  49. header("Status: 400 Wrong Field");
  50. echo 'error';
  51. break;
  52. }
  53. break;
  54. case 'updateMailfolder':
  55. switch($_POST['name']) {
  56. case 'mailaccount':
  57. header("Status: 200 OK");
  58. $db->updateQuery("UPDATE `mailbox-folders` SET `mailbox_id`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  59. break;
  60. case 'mailfolder':
  61. header("Status: 200 OK");
  62. $db->updateQuery("UPDATE `mailbox-folders` SET `folder_name`='" . $_POST['value'] . "' WHERE id=" . $_POST['pk'] . ";");
  63. break;
  64. default:
  65. header("Status: 400 Wrong Field");
  66. echo 'error';
  67. break;
  68. }
  69. break;
  70. case 'addDefaultMailaccount':
  71. header("Status: 200 OK");
  72. $db->insertQuery("INSERT INTO `mailboxes` (`user_id`) VALUES (" . $_POST['user-id'] . ");");
  73. break;
  74. case 'addDefaultMailfolder':
  75. header("Status: 200 OK");
  76. $db->insertQuery("INSERT INTO `mailbox-folders` (`label_id`) VALUES (" . $_POST['label-id'] . ");");
  77. break;
  78. case 'removeMailaccount':
  79. header("Status: 200 OK");
  80. $db->removeQuery("DELETE FROM `mailboxes` WHERE `id` = " . $_POST['id'] . ";");
  81. break;
  82. case 'removeMailboxFolder':
  83. header("Status: 200 OK");
  84. $db->removeQuery("DELETE FROM `mailbox-folders` WHERE `id` = " . $_POST['id'] . ";");
  85. break;
  86. case 'manageMailboxFolder':
  87. header("Status: 200 OK");
  88. $allMailBoxes = Mailbox::getAllMailboxes(true);
  89. $editBox = array(
  90. "options" => array(
  91. array(
  92. "type" => "select",
  93. "name" => "account",
  94. "value" => Mailbox::getMailboxNameFromMailaccountID($_GET['mbID']),
  95. "values" => array()
  96. ),
  97. array(
  98. "type" => "select",
  99. "name" => "folder",
  100. "value" => MailboxFolder::getMailboxFolderNameFromID($_GET['mfID']),
  101. "values" => array()
  102. )
  103. ),
  104. "title" => "Mailkonto bearbeiten",
  105. "mailboxFolderID" => $_GET['mfID']
  106. );
  107. foreach($allMailBoxes as $mailBox) {
  108. $mailBox->listFolders();
  109. $editBox['options'][0]['values'][] = $mailBox->getUsername();
  110. if($mailBox->getUsername() == Mailbox::getMailboxNameFromMailaccountID($_GET['mbID'])) {
  111. $editBox['options'][1]['values'] = $mailBox->getFolders();
  112. }
  113. }
  114. echo json_encode($editBox);
  115. break;
  116. case 'getMailAccountsByUid':
  117. header("Status: 200 OK");
  118. $mailboxes = Mailbox::getMailboxesByUserID($_REQUEST['uID']);
  119. $mbArray = array();
  120. foreach($mailboxes as $mailbox) {
  121. $mbArray[$mailbox->getID()] = $mailbox->getUsername();
  122. }
  123. echo json_encode($mbArray);
  124. break;
  125. case 'getNewDocumentBox':
  126. header("Status: 200 OK");
  127. $editBox = array(
  128. "options" => array(
  129. array(
  130. "type" => "select",
  131. "name" => "vorlage",
  132. "value" => Document::getDefaultDraft()->filename,
  133. "values" => Document::getAllDrafts()
  134. ),
  135. array(
  136. "type" => "text",
  137. "name" => "filename",
  138. "value" => str_replace(".docx", "", Document::getDefaultDraft()->filename) . "_" . date("d_m_Y") . ".docx"
  139. )
  140. ),
  141. "title" => "Neues Dokument"
  142. );
  143. echo json_encode($editBox);
  144. break;
  145. case 'getDraftVars':
  146. header("Status: 200 OK");
  147. $documentHandle = new \PhpOffice\PhpWord\TemplateProcessor('drafts/' . $_REQUEST['draft']);
  148. $templateVars = $documentHandle->getVariables();
  149. echo json_encode(array_values($templateVars));
  150. break;
  151. case 'getMailboxStatus':
  152. header("Status: 200 OK");
  153. $mailbox = Mailbox::getMailboxByID($_REQUEST['mailboxID'], true);
  154. $mailboxStatus = array(
  155. "connected" => $mailbox->getConnected()
  156. );
  157. echo json_encode($mailboxStatus);
  158. break;
  159. case 'saveNewDocument':
  160. header("Status: 200 OK");
  161. $newDocument = new DocumentHandler(Label::getLabelByID($_REQUEST['labelID'])->getPath() . '/' . $_REQUEST['filename'], $_REQUEST['draft']);
  162. foreach($_REQUEST['draftVars'] as $key => $draftVar) {
  163. $newDocument->setVal($key, htmlspecialchars($draftVar));
  164. }
  165. $newDocument->saveFile();
  166. echo json_encode($_REQUEST);
  167. break;
  168. case 'getNewLabelBox':
  169. header("Status: 200 OK");
  170. $editBox = array(
  171. "options" => array(
  172. array(
  173. "type" => "text",
  174. "name" => "name",
  175. "value" => ""
  176. ),
  177. array(
  178. "type" => "text",
  179. "name" => "path",
  180. "value" => ""
  181. )
  182. ),
  183. "title" => "Neues Label"
  184. );
  185. echo json_encode($editBox);
  186. break;
  187. case 'saveNewLabel':
  188. header("Status: 200 OK");
  189. Label::addLabel($_REQUEST['name'], $_REQUEST['path']);
  190. $return = array(
  191. "status" => "OK"
  192. );
  193. echo json_encode($return);
  194. break;
  195. case 'removeLabel':
  196. header("Status: 200 OK");
  197. Label::removeLabel($_REQUEST['labelID']);
  198. $return = array(
  199. "status" => "OK"
  200. );
  201. echo json_encode($return);
  202. break;
  203. case 'saveMailFolder':
  204. header("Status: 200 OK");
  205. MailboxFolder::updateMailboxFolder($_REQUEST['mailboxFolderID'], $_REQUEST['folder'], Mailbox::getMailboxByUsername($_REQUEST['account'])->getID(), $_REQUEST['labelID']);
  206. $return = array(
  207. "status" => "OK"
  208. );
  209. echo json_encode($return);
  210. break;
  211. case 'saveNewMailFolder':
  212. header("Status: 200 OK");
  213. MailboxFolder::addMailboxFolder($_REQUEST['folder'], Mailbox::getMailboxByUsername($_REQUEST['account'])->getID(), $_REQUEST['labelID']);
  214. $return = array(
  215. "status" => "OK"
  216. );
  217. echo json_encode($return);
  218. break;
  219. case 'getNewCallBox':
  220. header("Status: 200 OK");
  221. $allLabels = Label::getAllLabels();
  222. $editBox = array(
  223. "options" => array(
  224. array(
  225. "type" => "datetime",
  226. "name" => "call-date",
  227. "value" => ""
  228. ),
  229. array(
  230. "type" => "text",
  231. "name" => "caller-telnr",
  232. "value" => ""
  233. ),
  234. array(
  235. "type" => "select",
  236. "name" => "label-id",
  237. "value" => Label::getLabelByID($_REQUEST['labelID'])->getName(),
  238. "values" => array()
  239. ),
  240. array(
  241. "type" => "textarea",
  242. "name" => "call-notes",
  243. "value" => ""
  244. ),
  245. array(
  246. "type" => "checkbox",
  247. "name" => "call-set-reminder",
  248. "value" => ""
  249. )
  250. ),
  251. "title" => "Neuer Anruf"
  252. );
  253. foreach($allLabels as $label) {
  254. $editBox['options'][2]['values'][] = $label->getName();
  255. }
  256. echo json_encode($editBox);
  257. break;
  258. case 'saveNewCall':
  259. header("Status: 200 OK");
  260. Call::addCall($_REQUEST['userID'], $_REQUEST['callDate'], $_REQUEST['callerTelNr'], Label::getLabelByName($_REQUEST['labelID'])->getID(), $_REQUEST['callNotes']);
  261. $return = array(
  262. "status" => "OK"
  263. );
  264. echo json_encode($return);
  265. break;
  266. case 'getEvents':
  267. header("Status: 200 OK");
  268. $upcomingReminders = getObjectsAsArray(Reminder::getUpcomingRemindersByUserID($_REQUEST['userID']), array("id", "userID", "reminderDate", "remindedYet", "subject"));
  269. $return = array(
  270. "type" => $upcomingReminders == "" ? "ping" : "reminder",
  271. "reminders" => $upcomingReminders
  272. );
  273. echo json_encode($return);
  274. break;
  275. case 'setReminderReminded':
  276. header("Status: 200 OK");
  277. $reminder = Reminder::getReminderByID($_REQUEST['reminderID']);
  278. $reminder->setRemindedYet(1);
  279. $reminder->save();
  280. $return = array(
  281. "status" => "OK"
  282. );
  283. echo json_encode($return);
  284. break;
  285. case 'setReminderSnooze':
  286. header("Status: 200 OK");
  287. $reminder = Reminder::getReminderByID($_REQUEST['reminderID']);
  288. $reminder->setReminderDate(date("Y-m-d H:i:s", strtotime("+30 minutes")));
  289. $reminder->save();
  290. $return = array(
  291. "status" => "OK"
  292. );
  293. echo json_encode($return);
  294. break;
  295. case 'scanDocuments':
  296. header("Status: 200 OK");
  297. $return = scanDocuments($CONFIG['documentPath']);
  298. $return['status'] = "OK";
  299. echo json_encode($return);
  300. break;
  301. case 'getMailContent':
  302. header("Status: 200 OK");
  303. $mail = Mail::getMailByMailID($_REQUEST['mailID']);
  304. $mailboxFolder = Mailboxfolder::getMailboxfolderByID($mail->getMailboxFolderID());
  305. $mailbox = Mailbox::getMailboxByID($mailboxFolder->getMailboxID(), true);
  306. $mailbox->changeFolder($mailboxFolder->getFolderName());
  307. $bodyLines = explode(PHP_EOL, imap_fetchbody($mailbox->getMailbox(), $mail->getMailUID(), 1));
  308. $body = "%0D%0A"; // newline on top
  309. foreach($bodyLines as $line) {
  310. $body .= ">" . $line . '%0D%0A';
  311. }
  312. $return = array(
  313. "to" => $mail->getMailSender(),
  314. "subject" => $mail->getSubject(),
  315. "body" => $body
  316. );
  317. echo json_encode($return);
  318. case 'changeMailProcessed':
  319. header("Status: 200 OK");
  320. $mail = Mail::getMailByMailID($_REQUEST['mailID']);
  321. $mail->setProcessed($_REQUEST['value']);
  322. $mail->save();
  323. $return = array(
  324. "status" => "OK"
  325. );
  326. echo json_encode($return);
  327. break;
  328. case 'getMailaccountFoldersByUsername':
  329. header("Status: 200 OK");
  330. $mailbox = Mailbox::getMailboxByUsername($_REQUEST['account'], true);
  331. $mailbox->listFolders();
  332. echo json_encode($mailbox->getFolders());
  333. break;
  334. case 'getCalendarEvents':
  335. header("Status: 200 OK");
  336. // demo code
  337. $out = array();
  338. for($i=1; $i<=15; $i++){ //from day 01 to day 15
  339. $data = date('Y-m-d', strtotime("+".$i." days"));
  340. $out[] = array(
  341. 'id' => $i,
  342. 'title' => 'Event name '.$i,
  343. 'url' => "http://diesdas.de",
  344. 'class' => 'event-important',
  345. 'start' => strtotime($data).'000'
  346. );
  347. }
  348. echo json_encode(array('success' => 1, 'result' => $out));
  349. break;
  350. case 'getContactByID':
  351. header("Status: 200 OK");
  352. $contact = getObjectsAsArray(Contact::getContactByID($_REQUEST['contactID']), array("id", "organization", "department", "title", "degree", "forename", "surname", "street", "streetNumber", "postalCode", "city"));
  353. $communications = Contact::getCommunicationsByContactID($_REQUEST['contactID']);
  354. $return = array(
  355. "status" => "OK",
  356. "contact" => $contact,
  357. "communications" => $communications
  358. );
  359. echo json_encode($return);
  360. break;
  361. case 'updateContact':
  362. header("Status: 200 OK");
  363. $db->updateQuery("UPDATE `contacts` SET `organization` = \"" . $_REQUEST['organization'] . "\", `department` = \"" . $_REQUEST['department'] . "\", `title` = \"" . $_REQUEST['title'] . "\", `degree` = \"" . $_REQUEST['degree'] . "\", `forename` = \"" . $_REQUEST['forename'] . "\", `surname` = \"" . $_REQUEST['surname'] . "\", `street` = \"" . $_REQUEST['street'] . "\", `street_number` = \"" . $_REQUEST['streetNumber'] . "\", `postal_code` = \"" . $_REQUEST['postalCode'] . "\", `city` = \"" . $_REQUEST['city'] . "\" WHERE `id` = " . $_REQUEST['contactID'] . ";");
  364. if($_REQUEST['communications'] && sizeof($_REQUEST['communications']) > 0) {
  365. foreach($_REQUEST['communications'] as $communication) {
  366. if($communication['id'] == -1) { // new entry
  367. $db->insertQuery("INSERT INTO `contacts_fields` (`contact_id`, `type`, `name`, `value`) VALUES (" . $_REQUEST['contactID'] . ", " . $communication['type'] . ", \"" . $communication['name'] . "\", \"" . $communication['value'] . "\");");
  368. } else { // existing entry
  369. $db->updateQuery("UPDATE `contacts_fields` SET `name` = \"" . $communication['name'] . "\", `value` = \"" . $communication['value'] . "\" WHERE `id` = " . $communication['id'] . ";");
  370. }
  371. }
  372. }
  373. $return = array("status" => "OK");
  374. echo json_encode($return);
  375. break;
  376. case 'addContact':
  377. header("Status: 200 OK");
  378. $db->insertQuery("INSERT INTO `contacts`(`user_id`, `forename`, `surname`) VALUES (" . $_REQUEST['userID'] . ", \"Neuer\", \"Kontakt\");");
  379. $return = array("status" => "OK");
  380. echo json_encode($return);
  381. break;
  382. case 'getContacts':
  383. header("Status: 200 OK");
  384. $contacts = getObjectsAsArray(Contact::getAllContactsByUserID($_REQUEST['userID']), array("id", "organization", "department", "title", "degree", "forename", "surname", "street", "streetNumber", "postalCode", "city"));
  385. $return = array(
  386. "status" => "OK",
  387. "contacts" => $contacts
  388. );
  389. echo json_encode($return);
  390. break;
  391. case 'removeContact':
  392. header("Status: 200 OK");
  393. $db->removeQuery("DELETE FROM `contacts` WHERE `id` = " . $_REQUEST['contactID'] . ";");
  394. $return = array(
  395. "status" => "OK"
  396. );
  397. echo json_encode($return);
  398. break;
  399. case 'removeCommunication':
  400. header("Status: 200 OK");
  401. $db->removeQuery("DELETE FROM `contacts_fields` WHERE `id` = " . $_REQUEST['communicationID'] . ";");
  402. $return = array(
  403. "status" => "OK"
  404. );
  405. echo json_encode($return);
  406. break;
  407. case 'debugTest': // for testing single methods etc.
  408. break;
  409. default:
  410. header("Status: 400 No Action Defined");
  411. echo 'error';
  412. break;
  413. }
  414. //pa($_POST); // Debug
  415. ?>