functions.inc.php 5.6 KB

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