1 | <?php |
---|
2 | define('PHPGW_INCLUDE_ROOT','../'); |
---|
3 | define('PHPGW_API_INC','../phpgwapi/inc'); |
---|
4 | include_once(PHPGW_API_INC.'/class.db.inc.php'); |
---|
5 | include_once('class.dynamic_contacts.inc.php'); |
---|
6 | |
---|
7 | class db_functions |
---|
8 | { |
---|
9 | |
---|
10 | var $db; |
---|
11 | var $user_id; |
---|
12 | |
---|
13 | function db_functions(){ |
---|
14 | $this->db = new db(); |
---|
15 | $this->db->Halt_On_Error = 'no'; |
---|
16 | $this->db->connect( |
---|
17 | $_SESSION['phpgw_info']['expressomail']['server']['db_name'], |
---|
18 | $_SESSION['phpgw_info']['expressomail']['server']['db_host'], |
---|
19 | $_SESSION['phpgw_info']['expressomail']['server']['db_port'], |
---|
20 | $_SESSION['phpgw_info']['expressomail']['server']['db_user'], |
---|
21 | $_SESSION['phpgw_info']['expressomail']['server']['db_pass'], |
---|
22 | $_SESSION['phpgw_info']['expressomail']['server']['db_type'] |
---|
23 | ); |
---|
24 | $this -> user_id = $_SESSION['phpgw_info']['expressomail']['user']['account_id']; |
---|
25 | } |
---|
26 | |
---|
27 | // BEGIN of functions. |
---|
28 | function get_cc_contacts() |
---|
29 | { |
---|
30 | $result = array(); |
---|
31 | $stringDropDownContacts = ''; |
---|
32 | // Traz primeiro os usuarios que compartilharam com o usuario logado: |
---|
33 | $query = 'select id_related from phpgw_cc_contact_rels where id_contact='.$this -> user_id.' and id_typeof_contact_relation=1'; |
---|
34 | // Somente do usuario logado |
---|
35 | $query_related = 'A.id_owner ='.$this -> user_id; |
---|
36 | if (!$this->db->query($query)) |
---|
37 | return null; |
---|
38 | while($this->db->next_record()){ |
---|
39 | $row = $this->db->row(); |
---|
40 | $result[] = $row['id_related']; |
---|
41 | } |
---|
42 | $ids_related = implode(",",$result); |
---|
43 | |
---|
44 | // Se houver contatos compartilhados então altera a query. |
---|
45 | if($ids_related) |
---|
46 | $query_related = '(A.id_owner ='.$this -> user_id.' or A.id_owner in ('.$ids_related.'))'; |
---|
47 | |
---|
48 | // Traz os contatos pessoais e compartilhados |
---|
49 | $query = 'select A.names_ordered, C.connection_value from phpgw_cc_contact A, '. |
---|
50 | 'phpgw_cc_contact_conns B, phpgw_cc_connections C where '. |
---|
51 | 'A.id_contact = B.id_contact and B.id_connection = C.id_connection '. |
---|
52 | 'and B.id_typeof_contact_connection = 1 and '.$query_related.' group by '. |
---|
53 | 'A.names_ordered,C.connection_value order by A.names_ordered'; |
---|
54 | |
---|
55 | if (!$this->db->query($query)) |
---|
56 | return null; |
---|
57 | while($this->db->next_record()) |
---|
58 | $result[] = $this->db->row(); |
---|
59 | |
---|
60 | if (count($result) != 0) |
---|
61 | { |
---|
62 | // Monta string |
---|
63 | foreach($result as $contact) |
---|
64 | $stringDropDownContacts = $stringDropDownContacts . $contact['names_ordered']. ';' . $contact['connection_value'] . ','; |
---|
65 | //Retira ultima virgula. |
---|
66 | $stringDropDownContacts = substr($stringDropDownContacts,0,(strlen($stringDropDownContacts) - 1)); |
---|
67 | } |
---|
68 | else |
---|
69 | return null; |
---|
70 | |
---|
71 | return $stringDropDownContacts; |
---|
72 | } |
---|
73 | |
---|
74 | function get_cc_groups() |
---|
75 | { |
---|
76 | // Pesquisa no CC os Grupos Pessoais. |
---|
77 | $stringDropDownContacts = ''; |
---|
78 | $result = array(); |
---|
79 | $query = 'select title, short_name from phpgw_cc_groups where owner ='.$this -> user_id.' order by title'; |
---|
80 | // Executa a query |
---|
81 | if (!$this->db->query($query)) |
---|
82 | return null; |
---|
83 | // Retorna cada resultado |
---|
84 | while($this->db->next_record()) |
---|
85 | $result[] = $this->db->row(); |
---|
86 | // Se houver grupos .... |
---|
87 | if (count($result) != 0) |
---|
88 | { |
---|
89 | foreach($result as $group) |
---|
90 | $stringDropDownContacts .= $group['title']. ';' . $group['short_name'] . ','; |
---|
91 | //Retira ultima virgula. |
---|
92 | $stringDropDownContacts = substr($stringDropDownContacts,0,(strlen($stringDropDownContacts) - 1)); |
---|
93 | } |
---|
94 | else |
---|
95 | return null; |
---|
96 | |
---|
97 | return $stringDropDownContacts; |
---|
98 | } |
---|
99 | |
---|
100 | function getContactsByGroupAlias($alias) |
---|
101 | { |
---|
102 | |
---|
103 | $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ". |
---|
104 | "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ". |
---|
105 | "A.id_contact = B.id_contact and B.id_connection = C.id_connection ". |
---|
106 | "and B.id_typeof_contact_connection = 1 and ". |
---|
107 | "A.id_owner =".$this -> user_id." and ". |
---|
108 | "D.id_group = E.id_group and ". |
---|
109 | "D.id_connection = C.id_connection and E.short_name = '".$alias. |
---|
110 | "' order by A.names_ordered"; |
---|
111 | |
---|
112 | if (!$this->db->query($query)) |
---|
113 | { |
---|
114 | exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); |
---|
115 | } |
---|
116 | |
---|
117 | $return = false; |
---|
118 | |
---|
119 | while($this->db->next_record()) |
---|
120 | { |
---|
121 | $return[] = $this->db->row(); |
---|
122 | } |
---|
123 | |
---|
124 | return $return; |
---|
125 | } |
---|
126 | |
---|
127 | function getAddrs($array_addrs) { |
---|
128 | $array_addrs_final = array(); |
---|
129 | |
---|
130 | for($i = 0; $i < count($array_addrs); $i++){ |
---|
131 | $j = count($array_addrs_final); |
---|
132 | |
---|
133 | if(!strchr($array_addrs[$i],'@') |
---|
134 | && strchr($array_addrs[$i],'<') |
---|
135 | && strchr($array_addrs[$i],'>')) { |
---|
136 | |
---|
137 | $alias = substr($array_addrs[$i], strpos($array_addrs[$i],'<'), strpos($array_addrs[$i],'>')); |
---|
138 | $alias = str_replace('<','', str_replace('>','',$alias)); |
---|
139 | $arrayContacts = $this -> getContactsByGroupAlias($alias); |
---|
140 | |
---|
141 | if($arrayContacts) { |
---|
142 | foreach($arrayContacts as $index => $contact){ |
---|
143 | if($contact['names_ordered']) { |
---|
144 | $array_addrs_final[$j] = '"'.$contact['names_ordered'].'" <'.$contact['connection_value'].'>'; |
---|
145 | } |
---|
146 | else |
---|
147 | $array_addrs_final[$j] = $contact['connection_value']; |
---|
148 | |
---|
149 | $j++; |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | else |
---|
154 | $array_addrs_final[$j++] = $array_addrs[$i]; |
---|
155 | } |
---|
156 | return $array_addrs_final; |
---|
157 | } |
---|
158 | |
---|
159 | function get_dropdown_contacts(){ |
---|
160 | |
---|
161 | $contacts = $this -> get_cc_contacts(); |
---|
162 | $groups = $this -> get_cc_groups(); |
---|
163 | |
---|
164 | if(($contacts) && ($groups)) |
---|
165 | $stringDropDownContacts = $contacts . ',' . $groups; |
---|
166 | elseif ((!$contacts) && (!$groups)) |
---|
167 | $stringDropDownContacts = ''; |
---|
168 | elseif (($contacts) && (!$groups)) |
---|
169 | $stringDropDownContacts = $contacts; |
---|
170 | elseif ((!$contacts) && ($groups)) |
---|
171 | $stringDropDownContacts = $groups; |
---|
172 | |
---|
173 | if($_SESSION['phpgw_info']['user']['preferences']['expressoMail']['number_of_contacts'] && |
---|
174 | $_SESSION['phpgw_info']['user']['preferences']['expressoMail']['use_dynamic_contacts']) { |
---|
175 | $dynamic_contact = new dynamic_contacts(); |
---|
176 | $dynamic = $dynamic_contact->dynamic_contact_toString(); |
---|
177 | if ($dynamic) |
---|
178 | $stringDropDownContacts .= ($stringDropDownContacts ? ',' : '') . $dynamic; |
---|
179 | } |
---|
180 | return $stringDropDownContacts; |
---|
181 | } |
---|
182 | /* |
---|
183 | function update_preferences($params){ |
---|
184 | |
---|
185 | $aux = explode("##",$params['prefe_string']); |
---|
186 | $new_prefe = array(); |
---|
187 | |
---|
188 | foreach($aux as $key=>$tmp){ |
---|
189 | if($key == 0){ |
---|
190 | $new_prefe['max_email_per_page'] = $tmp; |
---|
191 | } |
---|
192 | if($key == 1){ |
---|
193 | $new_prefe['save_deleted_msg'] = $tmp; |
---|
194 | } |
---|
195 | if($key == 2){ |
---|
196 | $new_prefe['delete_trash_messages_after_n_days'] = $tmp; |
---|
197 | } |
---|
198 | if($key == 3){ |
---|
199 | $new_prefe['delete_and_show_previous_message'] = $tmp; |
---|
200 | } |
---|
201 | if($key == 4){ |
---|
202 | $new_prefe['alert_new_msg'] = $tmp; |
---|
203 | } |
---|
204 | if($key == 5){ |
---|
205 | $new_prefe['mainscreen_showmail'] = $tmp; |
---|
206 | } |
---|
207 | if($key == 10){ |
---|
208 | $new_prefe['signature'] = $tmp; |
---|
209 | } |
---|
210 | if($key == 7){ |
---|
211 | $new_prefe['hide_folders'] = $tmp; |
---|
212 | } |
---|
213 | if($key == 6){ |
---|
214 | $new_prefe['save_in_folder'] = $tmp; |
---|
215 | } |
---|
216 | if($key == 8){ |
---|
217 | $new_prefe['line_height'] = $tmp; |
---|
218 | } |
---|
219 | if($key == 9){ |
---|
220 | $new_prefe['font_size'] = $tmp; |
---|
221 | } |
---|
222 | if($key == 11){ |
---|
223 | $new_prefe['use_shortcuts'] = $tmp; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | $string_serial = serialize($new_prefe); |
---|
228 | $string_serial = get_magic_quotes_gpc() ? $string_serial : addslashes($string_serial); |
---|
229 | $query = "update phpgw_preferences set preference_value = '".$string_serial."' where preference_app = 'expressoMail'". |
---|
230 | " and preference_owner = '".$this->user_id."'"; |
---|
231 | |
---|
232 | if (!$this->db->query($query)) |
---|
233 | return "Failed!"; |
---|
234 | else |
---|
235 | return "OK!"; |
---|
236 | } |
---|
237 | */ |
---|
238 | function update_preferences($params){ |
---|
239 | $string_serial = urldecode($params['prefe_string']); |
---|
240 | $string_serial = get_magic_quotes_gpc() ? $string_serial : addslashes($string_serial); |
---|
241 | $query = "update phpgw_preferences set preference_value = '".$string_serial."' where preference_app = 'expressoMail'". |
---|
242 | " and preference_owner = '".$this->user_id."'"; |
---|
243 | |
---|
244 | if (!$this->db->query($query)) |
---|
245 | return $this->db->error; |
---|
246 | else |
---|
247 | return array("success" => true); |
---|
248 | } |
---|
249 | function getUserByEmail($params){ |
---|
250 | // Follow the referral |
---|
251 | $email = $params['email']; |
---|
252 | $query = 'select A.names_ordered, C.connection_name, C.connection_value, A.photo'. |
---|
253 | ' from phpgw_cc_contact A, phpgw_cc_contact_conns B, '. |
---|
254 | 'phpgw_cc_connections C where A.id_contact = B.id_contact'. |
---|
255 | ' and B.id_connection = C.id_connection and A.id_contact ='. |
---|
256 | '(select A.id_contact from phpgw_cc_contact A, phpgw_cc_contact_conns B,'. |
---|
257 | 'phpgw_cc_connections C where A.id_contact = B.id_contact'. |
---|
258 | ' and B.id_connection = C.id_connection and A.id_owner = '.$this -> user_id. |
---|
259 | ' and C.connection_value = \''.$email.'\') and '. |
---|
260 | 'C.connection_is_default = true and B.id_typeof_contact_connection = 2'; |
---|
261 | |
---|
262 | if (!$this->db->query($query)) |
---|
263 | return null; |
---|
264 | |
---|
265 | |
---|
266 | if($this->db->next_record()) { |
---|
267 | $result = $this->db->row(); |
---|
268 | |
---|
269 | $obj = array("cn" => $result['names_ordered'], |
---|
270 | "email" => $email, |
---|
271 | "type" => "personal", |
---|
272 | "telefone" => $result['connection_value']); |
---|
273 | |
---|
274 | if($result['photo']) |
---|
275 | $_SESSION['phpgw_info']['expressomail']['contact_photo'] = array($result['photo']); |
---|
276 | |
---|
277 | return $obj; |
---|
278 | } |
---|
279 | return $result; |
---|
280 | } |
---|
281 | |
---|
282 | function get_dynamic_contacts() |
---|
283 | { |
---|
284 | // Pesquisa os emails e ultima inserção nos contatos dinamicos. |
---|
285 | if(!$this->db->select('phpgw_expressomail_contacts','data', |
---|
286 | 'id_owner ='.$this -> user_id, |
---|
287 | __LINE__,__FILE__)) |
---|
288 | { |
---|
289 | return $this->db->Error; |
---|
290 | } |
---|
291 | while($this->db->next_record()) |
---|
292 | { |
---|
293 | $result[] = $this->db->row(); |
---|
294 | } |
---|
295 | if($result) foreach($result as $item) |
---|
296 | { |
---|
297 | $contacts = unserialize($item['data']); |
---|
298 | } |
---|
299 | if (count($contacts) == 0) |
---|
300 | { |
---|
301 | return null; |
---|
302 | } |
---|
303 | //Sort by email |
---|
304 | function cmp($a, $b) { return strcmp($a["email"], $b["email"]);} |
---|
305 | usort($contacts,"cmp"); |
---|
306 | return $contacts; |
---|
307 | } |
---|
308 | function update_contacts($contacts=array()) |
---|
309 | { |
---|
310 | // Atualiza um email nos contatos dinamicos. |
---|
311 | if(!$this->db->update('phpgw_expressomail_contacts ','data=\''.serialize($contacts).'\'', |
---|
312 | 'id_owner ='.$this -> user_id, |
---|
313 | __LINE__,__FILE__)) |
---|
314 | { |
---|
315 | return $this->db->Error; |
---|
316 | } |
---|
317 | return $contacts; |
---|
318 | } |
---|
319 | |
---|
320 | function insert_contact($contact) |
---|
321 | { |
---|
322 | $contacts[] = array( 'timestamp' => time(), |
---|
323 | 'email' => $contact ); |
---|
324 | |
---|
325 | // Insere um email nos contatos dinamicos. |
---|
326 | $query = 'INSERT INTO phpgw_expressomail_contacts (data, id_owner) ' . |
---|
327 | 'values ( \''.serialize($contacts).'\', '.$this->user_id.')'; |
---|
328 | |
---|
329 | if(!$this->db->query($query,__LINE__,__FILE__)) |
---|
330 | return $this->db->Error; |
---|
331 | return $contacts; |
---|
332 | } |
---|
333 | |
---|
334 | function remove_dynamic_contact($user_id,$line,$file) |
---|
335 | { |
---|
336 | $where = $user_id.' = id_owner'; |
---|
337 | $this->db->delete('phpgw_expressomail_contacts',$where,$line,$file); |
---|
338 | } |
---|
339 | |
---|
340 | function import_vcard($params){ |
---|
341 | |
---|
342 | include_once('class.imap_functions.inc.php'); |
---|
343 | $objImap = new imap_functions(); |
---|
344 | $msg_number = $params['msg_number']; |
---|
345 | $idx_file = $params['idx_file']; |
---|
346 | $msg_part = $params['msg_part']; |
---|
347 | $msg_folder = $params['msg_folder']; |
---|
348 | $encoding = strtolower($params['encoding']); |
---|
349 | $fileContent = ""; |
---|
350 | |
---|
351 | if($msg_number && $msg_part && $msg_folder && (intval($idx_file == '0' ? '1' : $idx_file))) { |
---|
352 | $mbox_stream = $objImap->open_mbox($msg_folder); |
---|
353 | $fileContent = imap_fetchbody($mbox_stream, $msg_number, $msg_part, FT_UID); |
---|
354 | include_once('class.imap_attachment.inc.php'); |
---|
355 | $imap_attachment = new imap_attachment(); |
---|
356 | $a = $imap_attachment->download_attachment($mbox_stream, $msg_number); |
---|
357 | $filename = $a[$idx_file]['name']; |
---|
358 | } |
---|
359 | else |
---|
360 | $filename = $idx_file; |
---|
361 | |
---|
362 | if($fileContent) { |
---|
363 | if($encoding == 'base64') |
---|
364 | $calendar = imap_base64($fileContent); |
---|
365 | else if($encoding == 'quoted-printable') |
---|
366 | $calendar = quoted_printable_decode($fileContent); |
---|
367 | else |
---|
368 | $calendar = $fileContent; |
---|
369 | } |
---|
370 | |
---|
371 | $uiicalendar = CreateObject("calendar.uiicalendar"); |
---|
372 | return $uiicalendar = $uiicalendar->import_from_mail($calendar); |
---|
373 | } |
---|
374 | |
---|
375 | } |
---|
376 | ?> |
---|