emailcollector.lib.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /* Copyright (C) ---Put here your own copyright and developer email---
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file emailcollector/lib/emailcollector.lib.php
  19. * \ingroup emailcollector
  20. * \brief Library files with common functions for EmailCollector
  21. */
  22. /**
  23. * Prepare array of tabs for EmailCollector
  24. *
  25. * @param EmailCollector $object EmailCollector
  26. * @return array Array of tabs
  27. */
  28. function emailcollectorPrepareHead($object)
  29. {
  30. global $db, $langs, $conf;
  31. $langs->load("emailcollector@emailcollector");
  32. $h = 0;
  33. $head = array();
  34. $head[$h][0] = dol_buildpath("/admin/emailcollector_card.php", 1).'?id='.$object->id;
  35. $head[$h][1] = $langs->trans("EmailCollector");
  36. $head[$h][2] = 'card';
  37. $h++;
  38. /*if (isset($object->fields['note_public']) || isset($object->fields['note_private']))
  39. {
  40. $nbNote = 0;
  41. if (!empty($object->note_private)) $nbNote++;
  42. if (!empty($object->note_public)) $nbNote++;
  43. $head[$h][0] = dol_buildpath('/emailcollector/emailcollector_note.php', 1).'?id='.$object->id;
  44. $head[$h][1] = $langs->trans('Notes');
  45. if ($nbNote > 0) $head[$h][1].= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  46. $head[$h][2] = 'note';
  47. $h++;
  48. }*/
  49. /*require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  50. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  51. $upload_dir = $conf->emailcollector->dir_output . "/emailcollector/" . dol_sanitizeFileName($object->ref);
  52. $nbFiles = count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview.*\.png)$'));
  53. $nbLinks=Link::count($db, $object->element, $object->id);
  54. $head[$h][0] = dol_buildpath("/emailcollector/emailcollector_document.php", 1).'?id='.$object->id;
  55. $head[$h][1] = $langs->trans('Documents');
  56. if (($nbFiles+$nbLinks) > 0) $head[$h][1].= '<span class="badge marginleftonlyshort">'.($nbFiles+$nbLinks).'</span>';
  57. $head[$h][2] = 'document';
  58. $h++;
  59. $head[$h][0] = dol_buildpath("/emailcollector/emailcollector_agenda.php", 1).'?id='.$object->id;
  60. $head[$h][1] = $langs->trans("Events");
  61. $head[$h][2] = 'agenda';
  62. $h++;
  63. */
  64. // Show more tabs from modules
  65. // Entries must be declared in modules descriptor with line
  66. //$this->tabs = array(
  67. // 'entity:+tabname:Title:@emailcollector:/emailcollector/mypage.php?id=__ID__'
  68. //); // to add new tab
  69. //$this->tabs = array(
  70. // 'entity:-tabname:Title:@emailcollector:/emailcollector/mypage.php?id=__ID__'
  71. //); // to remove a tab
  72. complete_head_from_modules($conf, $langs, $object, $head, $h, 'emailcollector');
  73. complete_head_from_modules($conf, $langs, $object, $head, $h, 'emailcollector', 'remove');
  74. return $head;
  75. }
  76. /**
  77. * Get parts of a message
  78. *
  79. * @param object $structure Structure of message
  80. * @return object|boolean Parties du message|false en cas d'erreur
  81. */
  82. function getParts($structure)
  83. {
  84. return isset($structure->parts) ? $structure->parts : false;
  85. }
  86. /**
  87. * Array with joined files
  88. *
  89. * @param object $part Part of message
  90. * @return object|boolean Definition of message|false en cas d'erreur
  91. */
  92. function getDParameters($part)
  93. {
  94. return $part->ifdparameters ? $part->dparameters : false;
  95. }
  96. /**
  97. * Get attachments of a given mail
  98. *
  99. * @param integer $jk Number of email
  100. * @param object $mbox object connection imaap
  101. * @return array type, filename, pos
  102. */
  103. function getAttachments($jk, $mbox)
  104. {
  105. $structure = imap_fetchstructure($mbox, $jk);
  106. $parts = getParts($structure);
  107. $fpos = 2;
  108. $attachments = array();
  109. $nb = count($parts);
  110. if ($parts && $nb) {
  111. for ($i = 1; $i < $nb; $i++) {
  112. $part = $parts[$i];
  113. if ($part->ifdisposition && strtolower($part->disposition) == "attachment") {
  114. $ext = $part->subtype;
  115. $params = getDParameters($part);
  116. if ($params) {
  117. $filename = $part->dparameters[0]->value;
  118. $filename = imap_utf8($filename);
  119. $attachments[] = array('type' => $part->type, 'filename' => $filename, 'pos' => $fpos);
  120. }
  121. }
  122. $fpos++;
  123. }
  124. }
  125. return $attachments;
  126. }
  127. /**
  128. * Get content of a joined file from its position into a given email
  129. *
  130. * @param integer $jk numéro du mail
  131. * @param integer $fpos position de la pièce jointe
  132. * @param integer $type type de la pièce jointe
  133. * @param object $mbox object connection imaap
  134. * @return mixed data
  135. */
  136. function getFileData($jk, $fpos, $type, $mbox)
  137. {
  138. $mege = imap_fetchbody($mbox, $jk, $fpos);
  139. $data = getDecodeValue($mege, $type);
  140. return $data;
  141. }
  142. /**
  143. * Save joined file into a directory with a given name
  144. *
  145. * @param string $path Path to file
  146. * @param string $filename Name of file
  147. * @param mixed $data contenu à sauvegarder
  148. * @return string emplacement du fichier
  149. **/
  150. function saveAttachment($path, $filename, $data)
  151. {
  152. global $lang;
  153. $tmp = explode('.', $filename);
  154. $ext = array_pop($tmp);
  155. $filename = implode('.', $tmp);
  156. if (!file_exists($path)) {
  157. if (dol_mkdir($path) < 0) {
  158. return -1;
  159. }
  160. }
  161. $i = 1;
  162. $filepath = $path . $filename . '.' . $ext;
  163. while (file_exists($filepath)) {
  164. $filepath = $path . $filename . '(' . $i . ').' . $ext;
  165. $i++;
  166. }
  167. file_put_contents($filepath, $data);
  168. return $filepath;
  169. }
  170. /**
  171. * Decode content of a message
  172. *
  173. * @param string $message Message
  174. * @param integer $coding Type of content
  175. * @return string Decoded message
  176. **/
  177. function getDecodeValue($message, $coding)
  178. {
  179. switch ($coding) {
  180. case 0: //text
  181. case 1: //multipart
  182. $message = imap_8bit($message);
  183. break;
  184. case 2: //message
  185. $message = imap_binary($message);
  186. break;
  187. case 3: //application
  188. case 5: //image
  189. case 6: //video
  190. case 7: //other
  191. $message = imap_base64($message);
  192. break;
  193. case 4: //audio
  194. $message = imap_qprint($message);
  195. break;
  196. }
  197. return $message;
  198. }