]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/vCard.php
Release 6.2.0
[Github/sugarcrm.git] / include / vCard.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition 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         protected $properties = array();
46         
47         protected $name = 'no_name';
48
49         public function clear()
50         {
51                 $this->properties = array();
52         }
53
54         function loadContact($contactid, $module='Contacts') {
55                 global $app_list_strings;
56
57                 require_once($GLOBALS['beanFiles'][$GLOBALS['beanList'][$module]]);
58                 $contact = new $GLOBALS['beanList'][$module]();
59                 $contact->retrieve($contactid);
60                 // cn: bug 8504 - CF/LB break Outlook's vCard import
61                 $bad = array("\n", "\r");
62                 $good = array("=0A", "=0D");
63                 $encoding = '';
64                 if(strpos($contact->primary_address_street, "\n") || strpos($contact->primary_address_street, "\r")) {
65                         $contact->primary_address_street = str_replace($bad, $good, $contact->primary_address_street);
66                         $encoding = 'QUOTED-PRINTABLE';
67                 }
68
69                 $this->setName(from_html($contact->first_name), from_html($contact->last_name), $app_list_strings['salutation_dom'][from_html($contact->salutation)]);
70                 if ( isset($contact->birthdate) )
71             $this->setBirthDate(from_html($contact->birthdate));
72                 $this->setPhoneNumber(from_html($contact->phone_fax), 'FAX');
73                 $this->setPhoneNumber(from_html($contact->phone_home), 'HOME');
74                 $this->setPhoneNumber(from_html($contact->phone_mobile), 'CELL');
75                 $this->setPhoneNumber(from_html($contact->phone_work), 'WORK');
76                 $this->setEmail(from_html($contact->email1));
77                 $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);
78                 if ( isset($contact->account_name) )
79             $this->setORG(from_html($contact->account_name), from_html($contact->department));
80         else
81             $this->setORG('', from_html($contact->department));
82                 $this->setTitle($contact->title);
83         }
84
85         function setTitle($title){
86                 $this->setProperty("TITLE",$title );
87         }
88         function setORG($org, $dep){
89                 $this->setProperty("ORG","$org;$dep" );
90         }
91         function setAddress($address, $city, $state,$postal, $country, $type, $encoding=''){
92                 if(!empty($encoding)) {
93                         $encoding = ";ENCODING={$encoding}";
94                 }
95                 $this->setProperty("ADR;$type$encoding",";;$address;$city;$state;$postal;$country" );
96         }
97
98         function setName($first_name, $last_name, $prefix){
99                 $this->name = strtr($first_name.'_'.$last_name, ' ' , '_');
100                 $this->setProperty('N',$last_name.';'.$first_name.';;'.$prefix );
101                 $this->setProperty('FN',"$prefix $first_name $last_name");
102         }
103
104         function setEmail($address){
105                 $this->setProperty('EMAIL;INTERNET', $address);
106         }
107
108         function setPhoneNumber( $number, $type)
109         {
110                 if($type != 'FAX') {
111                     $this->setProperty("TEL;$type", $number);
112                 }
113                 else {
114                     $this->setProperty("TEL;WORK;$type", $number);
115                 }
116         }
117         function setBirthDate($date){
118                         $this->setProperty('BDAY',$date);
119         }
120         function getProperty($name){
121                 if(isset($this->properties[$name]))
122                         return $this->properties[$name];
123                 return null;
124         }
125
126         function setProperty($name, $value){
127                 $this->properties[$name] = $value;
128         }
129
130         function toString(){
131             global $locale;
132                 $temp = "BEGIN:VCARD\n";
133                 foreach($this->properties as $key=>$value){
134                     if(!empty($value)) {
135                             $temp .= $key. ';CHARSET='.strtolower($locale->getExportCharset()).':'.$value."\n";
136                     } else {
137                         $temp .= $key. ':'.$value."\n";
138                     }
139                 }
140                 $temp.= "END:VCARD\n";
141
142
143                 return $temp;
144         }
145
146         function saveVCard(){
147                 global $locale;
148                 $content = $this->toString();
149                 if ( !defined('SUGAR_PHPUNIT_RUNNER') ) {
150             header("Content-Disposition: attachment; filename={$this->name}.vcf");
151             header("Content-Type: text/x-vcard; charset=".$locale->getExportCharset());
152             header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
153             header("Last-Modified: " . TimeDate::httpTime() );
154             header("Cache-Control: max-age=0");
155             header("Pragma: public");
156             header("Content-Length: ".strlen($content));
157         }
158
159                 print $locale->translateCharset($content, 'UTF-8', $locale->getExportCharset());
160         }
161
162         function importVCard($filename, $module='Contacts'){
163                 global $current_user;
164                 $lines =        file($filename);
165                 $start = false;
166                 $contact = loadBean($module);
167
168                 $contact->title = 'imported';
169                 $contact->assigned_user_id = $current_user->id;
170                 $fullname = '';
171         $email_suffix = 1;
172
173                 for($index = 0; $index < sizeof($lines); $index++){
174                         $line = $lines[$index];
175
176             // check the encoding and change it if needed
177             $locale = new Localization();
178             $encoding = $locale->detectCharset($line);
179             if ( $encoding != $GLOBALS['sugar_config']['default_charset'] ) {
180                 $line = $locale->translateCharset($line,$encoding);
181             }
182                         $line = trim($line);
183                         if($start){
184                                 //VCARD is done
185                                 if(substr_count(strtoupper($line), 'END:VCARD')){
186                                         if(!isset($contact->last_name)){
187                                                 $contact->last_name = $fullname;
188                                         }
189                     break;
190                                 }
191                                 $keyvalue = explode(':',$line);
192                                 if(sizeof($keyvalue)==2){
193                                         $value = $keyvalue[1];
194                                         for($newindex= $index + 1;  $newindex < sizeof($lines), substr_count($lines[$newindex], ':') == 0; $newindex++){
195                                                         $value .= $lines[$newindex];
196                                                         $index = $newindex;
197                                         }
198                                         $values = explode(';',$value );
199                                         $key = strtoupper($keyvalue[0]);
200                                         $key = strtr($key, '=', '');
201                                         $key = strtr($key, ',',';');
202                                         $keys = explode(';' ,$key);
203
204                                         if($keys[0] == 'TEL'){
205                                                 if(substr_count($key, 'WORK') > 0){
206                                                                 if(substr_count($key, 'FAX') > 0){
207                                                                                 if(!isset($contact->phone_fax)){
208                                                                                         $contact->phone_fax = $value;
209                                                                                 }
210                                                                 }else{
211                                                                         if(!isset($contact->phone_work)){
212                                                                                         $contact->phone_work = $value;
213                                                                         }
214                                                                 }
215                                                 }
216                                                 if(substr_count($key, 'HOME') > 0){
217                                                                 if(substr_count($key, 'FAX') > 0){
218                                                                                 if(!isset($contact->phone_fax)){
219                                                                                         $contact->phone_fax = $value;
220                                                                                 }
221                                                                 }else{
222                                                                         if(!isset($contact->phone_home)){
223                                                                                         $contact->phone_home = $value;
224                                                                         }
225                                                                 }
226                                                 }
227                                                 if(substr_count($key, 'CELL') > 0){
228                                                                 if(!isset($contact->phone_mobile)){
229                                                                                 $contact->phone_mobile = $value;
230                                                                 }
231
232                                                 }
233                                                 if(substr_count($key, 'FAX') > 0){
234                                                                                 if(!isset($contact->phone_fax)){
235                                                                                         $contact->phone_fax = $value;
236                                                 }
237
238                                                 }
239
240                                         }
241                                         if($keys[0] == 'N'){
242                                                 if(sizeof($values) > 0)
243                                                         $contact->last_name = $values[0];
244                                                 if(sizeof($values) > 1)
245                                                         $contact->first_name = $values[1];
246                                                 if(sizeof($values) > 2)
247                                                         $contact->salutation = $values[2];
248
249
250
251                                         }
252                                         if($keys[0] == 'FN'){
253                                                 $fullname = $value;
254
255
256                                         }
257
258                 }
259                                         if($keys[0] == 'ADR'){
260                                                 if(substr_count($key, 'WORK') > 0 && (substr_count($key, 'POSTAL') > 0|| substr_count($key, 'PARCEL') == 0)){
261
262                                                                 if(!isset($contact->primary_address_street) && sizeof($values) > 2){
263                                         $textBreaks = array("\n", "\r");
264                                         $vcardBreaks = array("=0A", "=0D");
265                                                                                 $contact->primary_address_street = str_replace($vcardBreaks, $textBreaks, $values[2]);
266                                                                 }
267                                                                 if(!isset($contact->primary_address_city) && sizeof($values) > 3){
268                                                                                 $contact->primary_address_city = $values[3];
269                                                                 }
270                                                                 if(!isset($contact->primary_address_state) && sizeof($values) > 4){
271                                                                                 $contact->primary_address_state = $values[4];
272                                                                 }
273                                                                 if(!isset($contact->primary_address_postalcode) && sizeof($values) > 5){
274                                                                                 $contact->primary_address_postalcode = $values[5];
275                                                                 }
276                                                                 if(!isset($contact->primary_address_country) && sizeof($values) > 6){
277                                                                                 $contact->primary_address_country = $values[6];
278                                                                 }
279                                                 }
280                                         }
281
282                                         if($keys[0] == 'TITLE'){
283                                                 $contact->title = $value;
284
285                                         }
286                                         if($keys[0] == 'EMAIL'){
287                         $field = 'email' . $email_suffix;
288                                                 if(!isset($contact->$field)) {
289                                                    $contact->$field = $value;
290                                                 }
291
292                                                 if($email_suffix == 1) {
293                                                    $_REQUEST['email1'] = $value;
294                                                 }
295
296                                                 $email_suffix++;
297                                         }
298
299                                         if($keys[0] == 'ORG'){
300                         $GLOBALS['log']->debug('I found a company name');
301                                                 if(!empty($value)){
302                             $GLOBALS['log']->debug('I found a company name (fer real)');
303                             if ( is_a($contact,"Contact") || is_a($contact,"Lead") ) {
304                                 $GLOBALS['log']->debug('And Im dealing with a person!');
305                                 $accountBean = loadBean('Accounts');
306                                 // It's a contact, we better try and match up an account
307                                                                 $full_company_name = trim($values[0]);
308                                 // Do we have a full company name match?
309                                 $result = $accountBean->retrieve_by_string_fields(array('name' => $full_company_name, 'deleted' => 0));
310                                 if ( ! isset($result->id) ) {
311                                     // Try to trim the full company name down, see if we get some other matches
312                                     $vCardTrimStrings = array('/ltd\.*/i'=>'',
313                                                               '/llc\.*/i'=>'',
314                                                               '/gmbh\.*/i'=>'',
315                                                               '/inc\.*/i'=>'',
316                                                               '/\.com/i'=>'',
317                                         );
318                                     // Allow users to override the trimming strings
319                                     if ( file_exists('custom/include/vCardTrimStrings.php') ) {
320                                         require_once('custom/include/vCardTrimStrings.php');
321                                     }
322                                     $short_company_name = trim(preg_replace(array_keys($vCardTrimStrings),$vCardTrimStrings,$full_company_name)," ,.");
323
324                                     $GLOBALS['log']->debug('Trying an extended search for: '.$short_company_name);
325                                     $result = $accountBean->retrieve_by_string_fields(array('name' => $short_company_name, 'deleted' => 0));
326                                 }
327
328                                 if (  is_a($contact,"Lead") || ! isset($result->id) ) {
329                                     // We could not find a parent account, or this is a lead so only copy the name, no linking
330                                     $GLOBALS['log']->debug("Did not find a matching company ($full_company_name)");
331                                     $contact->account_id = '';
332                                     $contact->account_name = $full_company_name;
333                                 } else {
334                                     $GLOBALS['log']->debug("Found a matching company: ".$result->name);
335                                     $contact->account_id = $result->id;
336                                     $contact->account_name = $result->name;
337                                 }
338                                 $contact->department = $values[1];
339                             } else{
340                                                                 $contact->department = $value;
341                             }
342                                                 }
343
344                                         }
345
346                                 }
347
348
349
350
351                         //FOUND THE BEGINING OF THE VCARD
352                         if(!$start && substr_count(strtoupper($line), 'BEGIN:VCARD')){
353                                 $start = true;
354                         }
355
356                 }
357
358         if ( is_a($contact, "Contact") && empty($contact->account_id) && !empty($contact->account_name) ) {
359             $GLOBALS['log']->debug("Look ma! I'm creating a new account: ".$contact->account_name);
360             // We need to create a new account
361             $accountBean = loadBean('Accounts');
362             // Populate the newly created account with all of the contact information
363             foreach ( $contact->field_defs as $field_name => $field_def ) {
364                 if ( !empty($contact->$field_name) ) {
365                     $accountBean->$field_name = $contact->$field_name;
366                 }
367             }
368             $accountBean->name = $contact->account_name;
369             $accountBean->save();
370             $contact->account_id = $accountBean->id;
371         }
372
373         $contactId = $contact->save();
374         return $contactId;
375         }
376         }
377
378
379
380
381
382
383
384
385 ?>