functions.inc.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. if($allDocuments) {
  56. foreach($allDocuments as $key => $document) {
  57. if($document['fileName'] == explode('/', $filename)[sizeof(explode('/', $filename)) - 1]) {
  58. $labelPath = Label::getLabelByID($document['labelID'])->getPath();
  59. if(str_replace($CONFIG['documentPath'], "", $filename) == $labelPath . "/" . $document['path'] . $document['fileName']) {
  60. // found
  61. // update stuff
  62. unset($allDocuments[$key]); // remove from array
  63. $found = true;
  64. break;
  65. }
  66. // same filename, path unknown
  67. }
  68. }
  69. }
  70. if(!$found) {
  71. $fileTime = date("Y-m-d H:i:s", filemtime($filename));
  72. $path = explode('/', str_replace($CONFIG['documentPath'], "", $filename));
  73. $labelPath = $path[0];
  74. unset($path[0]);
  75. unset($path[sizeof($path)]);
  76. $path = implode('/', $path) . '/';
  77. if($path == '/') {
  78. $path = "";
  79. }
  80. Document::addDocument('file', explode('/', $filename)[sizeof(explode('/', $filename)) - 1], $path, Label::getLabelByPath($labelPath)->getID(), '', $fileTime, $fileTime);
  81. }
  82. }
  83. }
  84. function searchMails() {
  85. global $user;
  86. $mailboxes = Mailbox::getAllMailBoxes();
  87. $lastMailOption = Option::getOptionByKey("lastMailSearch");
  88. if($lastMailOption == false) {
  89. $lastMailOption = new Option(-1, 'lastMailSearch', 0);
  90. }
  91. $date = date("d-M-Y", $lastMailOption->getValue());
  92. if($mailboxes) {
  93. foreach($mailboxes as $mailbox) {
  94. $mailbox->listFolders();
  95. foreach($mailbox->getFolders() as $folder) {
  96. $mbFolder = MailboxFolder::getMailboxFolderByName($folder);
  97. if($mbFolder != false) {
  98. $mailbox->changeFolder($mbFolder->getFolderName());
  99. $search = imap_search($mailbox->getMailbox(), 'SINCE "' . $date . '"');
  100. if($search) {
  101. foreach($search as $message) {
  102. $headers = imap_header($mailbox->getMailbox(), $message);
  103. if(!Mail::getMailByMessageID($headers->message_id)) {
  104. $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);
  105. $mail->save();
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. $lastMailOption->setValue(time());
  114. $lastMailOption->save();
  115. }
  116. function getEditableLink($elementID, $type, $pk, $title, $value, $class = '') {
  117. if($type == 'text' || $type == 'password') {
  118. $class = 'editable-element-text';
  119. } else if($type == 'select' && $elementID == 'protocol') {
  120. $class = 'editable-element-select-protocol';
  121. } else if($type == 'select' && $elementID == 'use-ssl') {
  122. $class = 'editable-element-select-use-ssl';
  123. if($value == '/ssl') {
  124. $value = 'On';
  125. } else {
  126. $value = 'Off';
  127. }
  128. } else if($type == 'select' && $elementID == 'no-valid-cert') {
  129. $class = 'editable-element-select-no-valid-cert';
  130. if($value == '/novalidate-cert') {
  131. $value = 'On';
  132. } else {
  133. $value = 'Off';
  134. }
  135. } else if($type == 'select' && $elementID == 'mailaccount') {
  136. $class = 'editable-element-select-mailaccount';
  137. }
  138. $link = '<a href="#" id="' . $elementID . '" class="' . $class . '" data-type="' . $type . '" data-pk="' . $pk . '" data-url="ajax.php" data-title="' . $title . '">' . $value . '</a>';
  139. return $link;
  140. }
  141. function getExcerptFromString($string, $count = 50) {
  142. if(strlen($string) > $count) {
  143. $string = substr($string, 0, $count);
  144. $string = substr($string, 0, strrpos($string, " "));
  145. $string = $string . " ...";
  146. }
  147. return $string;
  148. }
  149. function startsWith($haystack, $needle)
  150. {
  151. $length = strlen($needle);
  152. return (substr($haystack, 0, $length) === $needle);
  153. }
  154. function endsWith($haystack, $needle)
  155. {
  156. $length = strlen($needle);
  157. if ($length == 0) {
  158. return true;
  159. }
  160. return (substr($haystack, -$length) === $needle);
  161. }
  162. function recursiveScanDir($dir, &$results = array()){
  163. $files = scandir($dir);
  164. foreach($files as $key => $value){
  165. $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
  166. if(!is_dir($path)) {
  167. $results[] = $path;
  168. } else if(is_dir($path) && $value != "." && $value != "..") {
  169. $results[$path] = array();
  170. recursiveScanDir($path, $results[$path]);
  171. }
  172. }
  173. return $results;
  174. }
  175. function in_array_r($needle, &$haystack, $strict = false) {
  176. foreach ($haystack as $item) {
  177. if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
  178. echo $item;
  179. return true;
  180. }
  181. }
  182. return false;
  183. }
  184. ?>