functions.inc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // Label functions
  3. function pa($array) {
  4. echo '<pre>';
  5. print_r($array);
  6. echo '</pre>';
  7. }
  8. function error($message) {
  9. echo $message;
  10. }
  11. function getLabels() {
  12. global $db;
  13. $return = array();
  14. $labels = $db->selectQuery("SELECT * FROM `labels`;");
  15. foreach($labels as $label) {
  16. $return[] = new Label($label->id, $label->name);
  17. }
  18. return $return;
  19. }
  20. function getLabelById($labelId) {
  21. global $db;
  22. $return = array();
  23. $labels = $db->selectQuery("SELECT * FROM `labels` WHERE `id` = " . $labelId . ";");
  24. foreach($labels as $label) {
  25. $return[] = new Label($label->id, $label->name);
  26. }
  27. return $return;
  28. }
  29. function getLabelByPath($path) {
  30. global $db;
  31. $return = array();
  32. $labels = $db->selectQuery("SELECT * FROM `labels` WHERE `path` = '" . $path . "';");
  33. foreach($labels as $label) {
  34. $return[] = new Label($label->id, $label->name);
  35. }
  36. return $return;
  37. }
  38. function getDocumentsByLabelId($labelId) {
  39. global $db;
  40. $return = array();
  41. $documents = $db->selectQuery("SELECT * FROM `documents` WHERE `label_id` = " . $labelId . ";");
  42. foreach($documents as $document) {
  43. $return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change, $document->type, $document->mail_uid);
  44. }
  45. return $return;
  46. }
  47. function getDocumentsByPath($path) {
  48. global $db;
  49. $return = array();
  50. $documents = $db->selectQuery("SELECT documents.* FROM `documents` JOIN `labels` ON labels.id = documents.label_id WHERE labels.path = '" . $path . "';");
  51. foreach($documents as $document) {
  52. $return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change, $document->type, $document->mail_uid);
  53. }
  54. return $return;
  55. }
  56. function getDocumentsByMailUidLabelId($mailUid, $labelId) {
  57. global $db;
  58. $return = array();
  59. $documents = $db->selectQuery("SELECT * FROM `documents` WHERE `mail_uid` = " . $mailUid . " AND `label_id` = " . $labelId . ";");
  60. foreach($documents as $document) {
  61. $return[] = new Document($document->id, $document->filename, $document->path, $document->label_id, $document->draft, $document->created, $document->last_change, $document->type, $document->mail_uid);
  62. }
  63. return $return;
  64. }
  65. function getMailboxes() {
  66. global $db;
  67. $return = array();
  68. $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes`;");
  69. foreach($mailboxes as $mailbox) {
  70. $useSsl = '';
  71. $noValidCert = '';
  72. if($mailbox->use_ssl) {
  73. $useSsl = '/ssl';
  74. }
  75. if(!$mailbox->valid_ssl) {
  76. $noValidCert = '/novalidate-cert';
  77. }
  78. $return[] = new Imap($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password); // TODO: Encrypt password
  79. }
  80. return $return;
  81. }
  82. function getMailboxesByUserId($userId) {
  83. global $db;
  84. $return = array();
  85. $mailboxes = $db->selectQuery("SELECT * FROM `mailboxes` WHERE `user_id` = " . $userId . ";");
  86. foreach($mailboxes as $mailbox) {
  87. $useSsl = '';
  88. $noValidCert = '';
  89. if($mailbox->use_ssl) {
  90. $useSsl = '/ssl';
  91. }
  92. if(!$mailbox->valid_ssl) {
  93. $noValidCert = '/novalidate-cert';
  94. }
  95. //$return[] = new Imap('{' . $mailbox->server . ':' . $mailbox->port . '/' . $mailbox->protocol . $useSsl . $noValidCert . '}', $mailbox->username, $mailbox->password); // TODO: Encrypt password
  96. $return[] = new Imap($mailbox->id, $mailbox->server, $mailbox->port, $mailbox->protocol, $useSsl, $noValidCert, $mailbox->username, $mailbox->password); // TODO: Encrypt password
  97. }
  98. return $return;
  99. }
  100. function getMailboxFolderByName($folderName) {
  101. global $db;
  102. $return = array();
  103. $folders = $db->selectQuery("SELECT * FROM `mailbox-folders` WHERE `folder_name` = '" . $folderName . "';");
  104. if(!$folders) {
  105. return false;
  106. }
  107. foreach($folders as $folder) {
  108. $return[] = new MailboxFolder($folder->folder_name, $folder->mailbox_id, $folder->label_id);
  109. }
  110. return $return;
  111. }
  112. function getMailboxFolderByLabelId($labelId) {
  113. global $db;
  114. $return = array();
  115. $folders = $db->selectQuery("SELECT * FROM `mailbox-folders` WHERE `label_id` = '" . $labelId . "';");
  116. if(!$folders) {
  117. return false;
  118. }
  119. foreach($folders as $folder) {
  120. $return[] = new MailboxFolder($folder->folder_name, $folder->mailbox_id, $folder->label_id);
  121. }
  122. return $return;
  123. }
  124. function addDocument($type, $fileName, $path, $labelId, $draft, $created, $lastChange, $mailUid) {
  125. global $db;
  126. $query = "INSERT INTO `documents`(`type`, `filename`, `path`, `label_id`, `draft`, `created`, `last_change`, `mail_uid`) VALUES ('" . $type . "', '" . $fileName . "', '" . $path . "', " . $labelId . ", '" . $draft . "', " . $created . ", " . $lastChange . ", " . $mailUid . ");";
  127. $db->insertQuery($query);
  128. }
  129. function searchNewFiles($scanDir) {
  130. global $db;
  131. global $CONFIG;
  132. $oldDocuments = getDocumentsByPath($scanDir);
  133. $files = scandir($CONFIG['documentPath'] . $scanDir);
  134. foreach($files as $file) {
  135. $existed = false;
  136. if($file === '.' || $file === '..') {
  137. continue;
  138. }
  139. foreach($oldDocuments as $oldDocument) {
  140. if($oldDocument->getFileName() === $file) { // TODO: Check update-date, maybe removed files
  141. $existed = true;
  142. break;
  143. }
  144. }
  145. if($existed) {
  146. continue;
  147. }
  148. addDocument('file', $file, '/', getLabelByPath($scanDir)[0]->getId(), '', 'NOW()', 'NOW()', 0); // TODO: get dates by filesystem
  149. }
  150. }
  151. function searchMails() {
  152. global $user;
  153. //$imap = new Imap('{mail.mmnx.de:993/imap/ssl/novalidate-cert}', 'mobi@mmnx.de', 'msmoro');
  154. //$imap->listFolders();
  155. $mailboxes = getMailBoxes();
  156. //pa($mailboxes);
  157. foreach($mailboxes as $mailbox) {
  158. $mailbox->listFolders();
  159. }
  160. foreach($mailboxes as $mailbox) {
  161. foreach($mailbox->getFolders() as $folder) {
  162. $mbFolder = getMailboxFolderByName($folder);
  163. if($mbFolder[0] != false) {
  164. $mailbox->changeFolder($mbFolder[0]->getFolderName());
  165. $messageCount = imap_num_msg($mailbox->getMailbox());
  166. for($i = 1; $i <= $messageCount; ++$i) {
  167. $headers = imap_header($mailbox->getMailbox(), $i);
  168. $uid = imap_uid($mailbox->getMailbox(), $i); // TODO: Get really unique ID, not folder-id
  169. $documents = getDocumentsByMailUidLabelId($uid, $mbFolder[0]->getLabelId());
  170. if(sizeof($documents) < 1) {
  171. addDocument('mail', $headers->subject, $headers->from[0]->mailbox . '@' . $headers->from[0]->host, $mbFolder[0]->getLabelId(), '', '\'' . $headers->date . '\'', '\'' . $headers->date . '\'', $uid);
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. function getEditableLink($elementId, $type, $pk, $title, $value) {
  179. if($type == 'text' || $type == 'password') {
  180. $class = 'editable-element-text';
  181. } else if($type == 'select' && $elementId == 'protocol') {
  182. $class = 'editable-element-select-protocol';
  183. } else if($type == 'select' && $elementId == 'use-ssl') {
  184. $class = 'editable-element-select-use-ssl';
  185. if($value == '/ssl') {
  186. $value = 'On';
  187. } else {
  188. $value = 'Off';
  189. }
  190. } else if($type == 'select' && $elementId == 'no-valid-cert') {
  191. $class = 'editable-element-select-no-valid-cert';
  192. if($value == '/novalidate-cert') {
  193. $value = 'On';
  194. } else {
  195. $value = 'Off';
  196. }
  197. }
  198. $link = '<a href="#" id="' . $elementId . '" class="' . $class . '" data-type="' . $type . '" data-pk="' . $pk . '" data-url="ajax.php" data-title="' . $title . '">' . $value . '</a>';
  199. return $link;
  200. }
  201. ?>