dolibarr 21.0.0-beta
emailcollector.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
33{
34 global $langs, $conf;
35
36 $langs->load("emailcollector@emailcollector");
37
38 $h = 0;
39 $head = array();
40
41 $head[$h][0] = DOL_URL_ROOT . '/admin/emailcollector_card.php?id='.$object->id;
42 $head[$h][1] = $langs->trans("EmailCollector");
43 $head[$h][2] = 'card';
44 $h++;
45
46 /*if (isset($object->fields['note_public']) || isset($object->fields['note_private'])) {
47 $nbNote = 0;
48 if (!empty($object->note_private)) $nbNote++;
49 if (!empty($object->note_public)) $nbNote++;
50 $head[$h][0] = dol_buildpath('/emailcollector/emailcollector_note.php', 1).'?id='.$object->id;
51 $head[$h][1] = $langs->trans('Notes');
52 if ($nbNote > 0) $head[$h][1].= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
53 $head[$h][2] = 'note';
54 $h++;
55 }*/
56
57 /*require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
58 require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
59 $upload_dir = $conf->emailcollector->dir_output . "/emailcollector/" . dol_sanitizeFileName($object->ref);
60 $nbFiles = count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview.*\.png)$'));
61 $nbLinks=Link::count($db, $object->element, $object->id);
62 $head[$h][0] = dol_buildpath("/emailcollector/emailcollector_document.php", 1).'?id='.$object->id;
63 $head[$h][1] = $langs->trans('Documents');
64 if (($nbFiles+$nbLinks) > 0) $head[$h][1].= '<span class="badge marginleftonlyshort">'.($nbFiles+$nbLinks).'</span>';
65 $head[$h][2] = 'document';
66 $h++;
67
68 $head[$h][0] = dol_buildpath("/emailcollector/emailcollector_agenda.php", 1).'?id='.$object->id;
69 $head[$h][1] = $langs->trans("Events");
70 $head[$h][2] = 'agenda';
71 $h++;
72 */
73
74 // Show more tabs from modules
75 // Entries must be declared in modules descriptor with line
76 //$this->tabs = array(
77 // 'entity:+tabname:Title:@emailcollector:/emailcollector/mypage.php?id=__ID__'
78 //); // to add new tab
79 //$this->tabs = array(
80 // 'entity:-tabname:Title:@emailcollector:/emailcollector/mypage.php?id=__ID__'
81 //); // to remove a tab
82 complete_head_from_modules($conf, $langs, $object, $head, $h, 'emailcollector');
83
84 complete_head_from_modules($conf, $langs, $object, $head, $h, 'emailcollector', 'remove');
85
86 return $head;
87}
88
95function getParts($structure)
96{
97 return isset($structure->parts) ? $structure->parts : false;
98}
99
106function getDParameters($part)
107{
108 return $part->ifdparameters ? $part->dparameters : false;
109}
110
118function getAttachments($jk, $mbox)
119{
120 $structure = imap_fetchstructure($mbox, $jk, FT_UID); // @phan-suppress-current-line PhanTypeMismatchArgumentInternal
121 $parts = getParts($structure);
122
123 $fpos = 2;
124 $attachments = array();
125 $nb = count($parts);
126 if ($nb && !empty($parts)) {
127 for ($i = 1; $i < $nb; $i++) {
128 $part = $parts[$i];
129
130 if ($part->ifdisposition && strtolower($part->disposition) == "attachment") {
131 $ext = $part->subtype;
132 $params = getDParameters($part);
133
134 if ($params) {
135 $filename = $part->dparameters[0]->value;
136 $filename = imap_utf8($filename);
137 $attachments[] = array('type' => $part->type, 'filename' => $filename, 'pos' => $fpos);
138 }
139 }
140 $fpos++;
141 }
142 }
143 return $attachments;
144}
145
155function getFileData($jk, $fpos, $type, $mbox)
156{
157 $merge = imap_fetchbody($mbox, $jk, $fpos, FT_UID); // @phan-suppress-current-line PhanTypeMismatchArgumentInternal
158 $data = getDecodeValue($merge, $type);
159
160 return $data;
161}
162
171function saveAttachment($path, $filename, $data)
172{
173 $tmp = explode('.', $filename);
174 $ext = array_pop($tmp);
175 $filename = implode('.', $tmp);
176 if (!file_exists($path)) {
177 if (dol_mkdir($path) < 0) {
178 return -1;
179 }
180 }
181
182 $i = 1;
183 $filepath = $path . $filename . '.' . $ext;
184
185 while (file_exists($filepath)) {
186 $filepath = $path . $filename . '(' . $i . ').' . $ext;
187 $i++;
188 }
189 file_put_contents($filepath, $data);
190 return $filepath;
191}
192
200function getDecodeValue($message, $coding)
201{
202 switch ($coding) {
203 case 0: //text
204 case 1: //multipart
205 $message = imap_8bit($message);
206 break;
207 case 2: //message
208 $message = imap_binary($message);
209 break;
210 case 3: //application
211 case 5: //image
212 case 6: //video
213 case 7: //other
214 $message = imap_base64($message);
215 break;
216 case 4: //audio
217 $message = imap_qprint($message);
218 break;
219 }
220
221 return $message;
222}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
saveAttachment($path, $filename, $data)
Save the attached file into a directory with a given name.
emailcollectorPrepareHead($object)
Prepare array of tabs for EmailCollector.
getFileData($jk, $fpos, $type, $mbox)
Get content of a joined file from its position into a given email.
getDParameters($part)
Array with joined files.
getParts($structure)
Get parts of a message.
getDecodeValue($message, $coding)
Decode content of a message.
getAttachments($jk, $mbox)
Get attachments of a given mail.
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode='add', $filterorigmodule='')
Complete or removed entries into a head array (used to build tabs).
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79