functions.inc.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. function pa($array) {
  3. echo '<pre>';
  4. print_r($array);
  5. echo '</pre>';
  6. }
  7. function error($message) {
  8. echo $message;
  9. }
  10. function getObjectsAsArray($objects, $keys) {
  11. $return = array();
  12. if(is_array($objects)) {
  13. foreach($objects as $object) {
  14. $return[] = array();
  15. foreach($keys as $key) {
  16. $keyCall = 'get' . ucfirst($key);
  17. $return[sizeof($return) - 1][$key] = $object->$keyCall();
  18. }
  19. }
  20. } else {
  21. foreach($keys as $key) {
  22. $keyCall = 'get' . ucfirst($key);
  23. $return[0][$key] = $object->$keyCall();
  24. }
  25. }
  26. return $return;
  27. }
  28. function scanDocuments($path) {
  29. global $db;
  30. global $CONFIG;
  31. $files = recursiveScanDir($path);
  32. $allDocuments = getObjectsAsArray(Document::getAllDocuments(), array("id", "fileName", "path", "labelId", "draft", "created", "lastChange", "type", "mailUid"));
  33. handleFile($files, $allDocuments);
  34. if(sizeof($allDocuments) > 0) { // Documents got removed
  35. foreach($allDocuments as $document) {
  36. Document::removeDocumentByID($document['id']);
  37. }
  38. }
  39. $newDocumentsNumber = sizeof($allDocuments);
  40. return array(
  41. "new" => $newDocumentsNumber
  42. );
  43. }
  44. function handleFile($filename, &$allDocuments) {
  45. global $db;
  46. global $CONFIG;
  47. if(is_array($filename)) {
  48. foreach($filename as $dir) {
  49. handleFile($dir, $allDocuments);
  50. }
  51. } else {
  52. $found = false;
  53. foreach($allDocuments as $key => $document) {
  54. if($document['fileName'] == explode('/', $filename)[sizeof(explode('/', $filename)) - 1]) {
  55. $labelPath = Label::getLabelById($document['labelId'])->getPath();
  56. if(str_replace($CONFIG['documentPath'], "", $filename) == $labelPath . "/" . $document['path'] . $document['fileName']) {
  57. // found
  58. // update stuff
  59. unset($allDocuments[$key]); // remove from array
  60. $found = true;
  61. break;
  62. }
  63. // same filename, path unknown
  64. }
  65. }
  66. if(!$found) {
  67. $fileTime = date("Y-m-d H:i:s", filemtime($filename));
  68. $path = explode('/', str_replace($CONFIG['documentPath'], "", $filename));
  69. $labelPath = $path[0];
  70. unset($path[0]);
  71. unset($path[sizeof($path)]);
  72. $path = implode('/', $path) . '/';
  73. if($path == '/') {
  74. $path = "";
  75. }
  76. Document::addDocument('file', explode('/', $filename)[sizeof(explode('/', $filename)) - 1], $path, Label::getLabelByPath($labelPath)->getId(), '', $fileTime, $fileTime);
  77. }
  78. }
  79. }
  80. function searchMails() {
  81. global $user;
  82. $mailboxes = Mailbox::getAllMailBoxes();
  83. foreach($mailboxes as $mailbox) {
  84. $mailbox->listFolders();
  85. }
  86. foreach($mailboxes as $mailbox) {
  87. foreach($mailbox->getFolders() as $folder) {
  88. $mbFolder = MailboxFolder::getMailboxFolderByName($folder);
  89. if($mbFolder != false) {
  90. $mailbox->changeFolder($mbFolder->getFolderName());
  91. $messageCount = imap_num_msg($mailbox->getMailbox());
  92. for($i = 1; $i <= $messageCount; ++$i) {
  93. $headers = imap_header($mailbox->getMailbox(), $i);
  94. pa($headers);
  95. //$uid = imap_msgno($mailbox->getMailbox(), imap_uid($mailbox->getMailbox(), $i));
  96. //echo $uid;
  97. $uid = imap_uid($mailbox->getMailbox(), $i);
  98. $documents = Document::getDocumentsByMailInfo($mailbox->getId(), $uid, $mbFolder->getLabelId());
  99. if(sizeof($documents) < 1) {
  100. Document::addDocument('mail', $headers->subject, $headers->from[0]->mailbox . '@' . $headers->from[0]->host, $mbFolder->getLabelId(), '', '\'' . $headers->date . '\'', '\'' . $headers->date . '\'', $uid, $mailbox->getId());
  101. }
  102. }
  103. }
  104. }
  105. }
  106. }
  107. function getEditableLink($elementId, $type, $pk, $title, $value, $class = '') {
  108. if($type == 'text' || $type == 'password') {
  109. $class = 'editable-element-text';
  110. } else if($type == 'select' && $elementId == 'protocol') {
  111. $class = 'editable-element-select-protocol';
  112. } else if($type == 'select' && $elementId == 'use-ssl') {
  113. $class = 'editable-element-select-use-ssl';
  114. if($value == '/ssl') {
  115. $value = 'On';
  116. } else {
  117. $value = 'Off';
  118. }
  119. } else if($type == 'select' && $elementId == 'no-valid-cert') {
  120. $class = 'editable-element-select-no-valid-cert';
  121. if($value == '/novalidate-cert') {
  122. $value = 'On';
  123. } else {
  124. $value = 'Off';
  125. }
  126. } else if($type == 'select' && $elementId == 'mailaccount') {
  127. $class = 'editable-element-select-mailaccount';
  128. }
  129. $link = '<a href="#" id="' . $elementId . '" class="' . $class . '" data-type="' . $type . '" data-pk="' . $pk . '" data-url="ajax.php" data-title="' . $title . '">' . $value . '</a>';
  130. return $link;
  131. }
  132. function getExcerptFromString($string, $count = 50) {
  133. if(strlen($string) > $count) {
  134. $string = substr($string, 0, $count);
  135. $string = substr($string, 0, strrpos($string, " "));
  136. $string = $string . " ...";
  137. }
  138. return $string;
  139. }
  140. function startsWith($haystack, $needle)
  141. {
  142. $length = strlen($needle);
  143. return (substr($haystack, 0, $length) === $needle);
  144. }
  145. function endsWith($haystack, $needle)
  146. {
  147. $length = strlen($needle);
  148. if ($length == 0) {
  149. return true;
  150. }
  151. return (substr($haystack, -$length) === $needle);
  152. }
  153. function recursiveScanDir($dir, &$results = array()){
  154. $files = scandir($dir);
  155. foreach($files as $key => $value){
  156. $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
  157. if(!is_dir($path)) {
  158. $results[] = $path;
  159. } else if(is_dir($path) && $value != "." && $value != "..") {
  160. $results[$path] = array();
  161. recursiveScanDir($path, $results[$path]);
  162. }
  163. }
  164. return $results;
  165. }
  166. function in_array_r($needle, &$haystack, $strict = false) {
  167. foreach ($haystack as $item) {
  168. if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
  169. echo $item;
  170. return true;
  171. }
  172. }
  173. return false;
  174. }
  175. ?>