functions.inc.php 5.6 KB

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