]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/vCard.php
Release 6.1.4
[Github/sugarcrm.git] / include / vCard.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 /*********************************************************************************
39
40  * Description:  
41  ********************************************************************************/
42
43 class vCard
44 {
45         var $properties = array();
46         var $name = 'no_name';
47         
48         function clear(){
49                 $this->properties = array();    
50         }
51         
52         function loadContact($contactid, $module='Contacts') {
53                 global $app_list_strings;
54
55                 require_once($GLOBALS['beanFiles'][$GLOBALS['beanList'][$module]]);
56                 $contact = new $GLOBALS['beanList'][$module]();
57                 $contact->retrieve($contactid);
58                 
59                 // cn: bug 8504 - CF/LB break Outlook's vCard import
60                 $bad = array("\n", "\r");
61                 $good = array("=0A", "=0D");
62                 $encoding = '';
63                 if(strpos($contact->primary_address_street, "\n") || strpos($contact->primary_address_street, "\r")) {
64                         $contact->primary_address_street = str_replace($bad, $good, $contact->primary_address_street);
65                         $encoding = 'QUOTED-PRINTABLE';
66                 }
67                 
68                 $this->setName(from_html($contact->first_name), from_html($contact->last_name), $app_list_strings['salutation_dom'][from_html($contact->salutation)]);
69                 if ( isset($contact->birthdate) )
70             $this->setBirthDate(from_html($contact->birthdate));
71                 $this->setPhoneNumber(from_html($contact->phone_fax), 'FAX');
72                 $this->setPhoneNumber(from_html($contact->phone_home), 'HOME');
73                 $this->setPhoneNumber(from_html($contact->phone_mobile), 'CELL');
74                 $this->setPhoneNumber(from_html($contact->phone_work), 'WORK');
75                 $this->setEmail(from_html($contact->email1));
76                 $this->setAddress(from_html($contact->primary_address_street), from_html($contact->primary_address_city), from_html($contact->primary_address_state), from_html($contact->primary_address_postalcode), from_html($contact->primary_address_country), 'WORK', $encoding);
77                 if ( isset($contact->account_name) )
78             $this->setORG(from_html($contact->account_name), from_html($contact->department));
79         else
80             $this->setORG('', from_html($contact->department));
81                 $this->setTitle($contact->title);
82         }
83         
84         function setTitle($title){
85                 $this->setProperty("TITLE",$title );    
86         }
87         function setORG($org, $dep){
88                 $this->setProperty("ORG","$org;$dep" ); 
89         }
90         function setAddress($address, $city, $state,$postal, $country, $type, $encoding=''){
91                 if(!empty($encoding)) {
92                         $encoding = ";ENCODING={$encoding}";
93                 }
94                 $this->setProperty("ADR;$type$encoding",";;$address;$city;$state;$postal;$country" );   
95         }
96         
97         function setName($first_name, $last_name, $prefix){
98                 $this->name = strtr($first_name.'_'.$last_name, ' ' , '_');
99                 $this->setProperty('N',$last_name.';'.$first_name.';'.$prefix );
100                 $this->setProperty('FN',"$prefix $first_name $last_name"); 
101         }
102         
103         function setEmail($address){
104                 $this->setProperty('EMAIL;INTERNET', $address);
105         }
106         
107         function setPhoneNumber( $number, $type){
108                 $this->setProperty("TEL;$type", $number);
109         }
110         function setBirthDate($date){
111                         $this->setProperty('BDAY',$date);
112         }
113         function getProperty($name){
114                 if(isset($this->properties[$name]))
115                         return $this->properties[$name];
116                 return null;    
117         }
118         
119         function setProperty($name, $value){
120                 $this->properties[$name] = $value;              
121         }
122         
123         function toString(){
124                 $temp = "BEGIN:VCARD\n";
125                 foreach($this->properties as $key=>$value){
126                         $temp .= $key. ':'.$value."\n"; 
127                 }       
128                 $temp.= "END:VCARD\n";
129                 
130                 
131                 return $temp;
132         }       
133         
134         function saveVCard(){
135                 global $locale;
136                 
137                 $content = $this->toString();
138                 header("Content-Disposition: attachment; filename={$this->name}.vcf");
139                 header("Content-Type: text/x-vcard; charset=".$locale->getExportCharset());
140                 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
141                 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
142                 header("Cache-Control: max-age=0");
143                 header("Pragma: public");
144                 header("Content-Length: ".strlen($content));
145
146                 print $locale->translateCharset($content, 'UTF-8', $locale->getExportCharset());
147         }
148         
149         function importVCard($filename, $module='Contacts'){
150                 global $current_user;
151                 $lines =        file($filename);
152                 $start = false;
153                 $contact = loadBean($module);
154
155                 $contact->title = 'imported';
156                 $contact->assigned_user_id = $current_user->id;
157                 $fullname = '';
158         $email_suffix = 1;
159                                                         
160                 for($index = 0; $index < sizeof($lines); $index++){
161                         $line = $lines[$index];
162                         
163             // check the encoding and change it if needed
164             $locale = new Localization();
165             $encoding = $locale->detectCharset($line);
166             if ( $encoding != $GLOBALS['sugar_config']['default_charset'] ) {
167                 $line = $locale->translateCharset($line,$encoding);
168             }
169                         $line = trim($line);
170                         if($start){
171                                 //VCARD is done
172                                 if(substr_count(strtoupper($line), 'END:VCARD')){
173                                         if(!isset($contact->last_name)){
174                                                 $contact->last_name = $fullname;        
175                                         }
176                     break;
177                                 }
178                                 $keyvalue = explode(':',$line);
179                                 if(sizeof($keyvalue)==2){
180                                         $value = $keyvalue[1];
181                                         for($newindex= $index + 1;  $newindex < sizeof($lines), substr_count($lines[$newindex], ':') == 0; $newindex++){
182                                                         $value .= $lines[$newindex];
183                                                         $index = $newindex;
184                                         }
185                                         $values = explode(';',$value );
186                                         $key = strtoupper($keyvalue[0]);
187                                         $key = strtr($key, '=', '');
188                                         $key = strtr($key, ',',';');
189                                         $keys = explode(';' ,$key);
190                                         
191                                         if($keys[0] == 'TEL'){
192                                                 if(substr_count($key, 'WORK') > 0){
193                                                                 if(substr_count($key, 'FAX') > 0){
194                                                                                 if(!isset($contact->phone_fax)){
195                                                                                         $contact->phone_fax = $value;
196                                                                                 }
197                                                                 }else{
198                                                                         if(!isset($contact->phone_work)){
199                                                                                         $contact->phone_work = $value;
200                                                                         }       
201                                                                 }
202                                                 }
203                                                 if(substr_count($key, 'HOME') > 0){
204                                                                 if(substr_count($key, 'FAX') > 0){
205                                                                                 if(!isset($contact->phone_fax)){
206                                                                                         $contact->phone_fax = $value;
207                                                                                 }
208                                                                 }else{
209                                                                         if(!isset($contact->phone_home)){
210                                                                                         $contact->phone_home = $value;
211                                                                         }       
212                                                                 }
213                                                 }
214                                                 if(substr_count($key, 'CELL') > 0){
215                                                                 if(!isset($contact->phone_mobile)){
216                                                                                 $contact->phone_mobile = $value;
217                                                                 }       
218                                                                 
219                                                 }
220                                                 if(substr_count($key, 'FAX') > 0){
221                                                                                 if(!isset($contact->phone_fax)){
222                                                                                         $contact->phone_fax = $value;
223                                                 }
224                                                 
225                                                 }
226                                                         
227                                         }
228                                         if($keys[0] == 'N'){
229                                                 if(sizeof($values) > 0)
230                                                         $contact->last_name = $values[0];
231                                                 if(sizeof($values) > 1)
232                                                         $contact->first_name = $values[1];
233                                                 if(sizeof($values) > 2)
234                                                         $contact->salutation = $values[2];                                                      
235                                                 
236                                                 
237                                                         
238                                         }
239                                         if($keys[0] == 'FN'){
240                                                 $fullname = $value;                             
241                                                 
242                                                         
243                                         }
244                                         
245                 }
246                                         if($keys[0] == 'ADR'){
247                                                 if(substr_count($key, 'WORK') > 0 && (substr_count($key, 'POSTAL') > 0|| substr_count($key, 'PARCEL') == 0)){
248
249                                                                 if(!isset($contact->primary_address_street) && sizeof($values) > 2){
250                                         $textBreaks = array("\n", "\r");
251                                         $vcardBreaks = array("=0A", "=0D");
252                                                                                 $contact->primary_address_street = str_replace($vcardBreaks, $textBreaks, $values[2]);
253                                                                 }
254                                                                 if(!isset($contact->primary_address_city) && sizeof($values) > 3){
255                                                                                 $contact->primary_address_city = $values[3];
256                                                                 }
257                                                                 if(!isset($contact->primary_address_state) && sizeof($values) > 4){
258                                                                                 $contact->primary_address_state = $values[4];
259                                                                 }
260                                                                 if(!isset($contact->primary_address_postalcode) && sizeof($values) > 5){
261                                                                                 $contact->primary_address_postalcode = $values[5];
262                                                                 }
263                                                                 if(!isset($contact->primary_address_country) && sizeof($values) > 6){
264                                                                                 $contact->primary_address_country = $values[6];
265                                                                 }
266                                                 }
267                                         }
268                                         
269                                         if($keys[0] == 'TITLE'){
270                                                 $contact->title = $value;
271                                                 
272                                         }
273                                         if($keys[0] == 'EMAIL'){
274                         $field = 'email' . $email_suffix;
275                                                 if(!isset($contact->$field)) {
276                                                    $contact->$field = $value;
277                                                 }
278                                                 
279                                                 if($email_suffix == 1) {
280                                                    $_REQUEST['email1'] = $value;
281                                                 }
282                                                 
283                                                 $email_suffix++;                
284                                         }
285                                         
286                                         if($keys[0] == 'ORG'){
287                         $GLOBALS['log']->debug('I found a company name');
288                                                 if(!empty($value)){
289                             $GLOBALS['log']->debug('I found a company name (fer real)');
290                             if ( is_a($contact,"Contact") || is_a($contact,"Lead") ) {
291                                 $GLOBALS['log']->debug('And Im dealing with a person!');
292                                 $accountBean = loadBean('Accounts');
293                                 // It's a contact, we better try and match up an account
294                                                                 $full_company_name = trim($values[0]);
295                                 // Do we have a full company name match?
296                                 $result = $accountBean->retrieve_by_string_fields(array('name' => $full_company_name, 'deleted' => 0));
297                                 if ( ! isset($result->id) ) {
298                                     // Try to trim the full company name down, see if we get some other matches
299                                     $vCardTrimStrings = array('/ltd\.*/i'=>'',
300                                                               '/llc\.*/i'=>'',
301                                                               '/gmbh\.*/i'=>'',
302                                                               '/inc\.*/i'=>'',
303                                                               '/\.com/i'=>'',
304                                         );
305                                     // Allow users to override the trimming strings
306                                     if ( file_exists('custom/include/vCardTrimStrings.php') ) {
307                                         require_once('custom/include/vCardTrimStrings.php');
308                                     }
309                                     $short_company_name = trim(preg_replace(array_keys($vCardTrimStrings),$vCardTrimStrings,$full_company_name)," ,.");
310                                     
311                                     $GLOBALS['log']->debug('Trying an extended search for: '.$short_company_name);
312                                     $result = $accountBean->retrieve_by_string_fields(array('name' => $short_company_name, 'deleted' => 0));
313                                 }
314                                 
315                                 if (  is_a($contact,"Lead") || ! isset($result->id) ) {
316                                     // We could not find a parent account, or this is a lead so only copy the name, no linking
317                                     $GLOBALS['log']->debug("Did not find a matching company ($full_company_name)");
318                                     $contact->account_id = '';
319                                     $contact->account_name = $full_company_name;
320                                 } else {
321                                     $GLOBALS['log']->debug("Found a matching company: ".$result->name);
322                                     $contact->account_id = $result->id;
323                                     $contact->account_name = $result->name;
324                                 }
325                                 $contact->department = $values[1];
326                             } else{
327                                                                 $contact->department = $value;
328                             }
329                                                 }
330                                                 
331                                         }
332                                         
333                                 }
334                                         
335                                 
336                                         
337                         
338                         //FOUND THE BEGINING OF THE VCARD
339                         if(!$start && substr_count(strtoupper($line), 'BEGIN:VCARD')){
340                                 $start = true;  
341                         }
342                         
343                 }
344         
345         if ( is_a($contact, "Contact") && empty($contact->account_id) && !empty($contact->account_name) ) {
346             $GLOBALS['log']->debug("Look ma! I'm creating a new account: ".$contact->account_name);
347             // We need to create a new account
348             $accountBean = loadBean('Accounts');
349             // Populate the newly created account with all of the contact information
350             foreach ( $contact->field_defs as $field_name => $field_def ) {
351                 if ( !empty($contact->$field_name) ) {
352                     $accountBean->$field_name = $contact->$field_name;
353                 }
354             }
355             $accountBean->name = $contact->account_name;
356             $accountBean->save();
357             $contact->account_id = $accountBean->id;
358         }
359         
360         $contactId = $contact->save();
361         return $contactId;
362         }
363         }
364         
365         
366         
367         
368         
369
370
371
372 ?>