]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/vCard.php
Release 6.2.2
[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             //bug45856 IIS Doesn't like this to be set and it causes the vCard to not get saved
157             if (preg_match('iis', $_SERVER['SERVER_SOFTWARE']))
158             {
159                 header("Content-Length: ".strlen($content));
160             }
161         }
162
163                 print $locale->translateCharset($content, 'UTF-8', $locale->getExportCharset());
164         }
165
166         function importVCard($filename, $module='Contacts'){
167                 global $current_user;
168                 $lines =        file($filename);
169                 $start = false;
170                 $contact = loadBean($module);
171
172                 $contact->title = 'imported';
173                 $contact->assigned_user_id = $current_user->id;
174                 $fullname = '';
175         $email_suffix = 1;
176
177                 for($index = 0; $index < sizeof($lines); $index++){
178                         $line = $lines[$index];
179
180             // check the encoding and change it if needed
181             $locale = new Localization();
182             $encoding = $locale->detectCharset($line);
183             if ( $encoding != $GLOBALS['sugar_config']['default_charset'] ) {
184                 $line = $locale->translateCharset($line,$encoding);
185             }
186                         $line = trim($line);
187                         if($start){
188                                 //VCARD is done
189                                 if(substr_count(strtoupper($line), 'END:VCARD')){
190                                         if(!isset($contact->last_name)){
191                                                 $contact->last_name = $fullname;
192                                         }
193                     break;
194                                 }
195                                 $keyvalue = explode(':',$line);
196                                 if(sizeof($keyvalue)==2){
197                                         $value = $keyvalue[1];
198                                         for($newindex= $index + 1;  $newindex < sizeof($lines), substr_count($lines[$newindex], ':') == 0; $newindex++){
199                                                         $value .= $lines[$newindex];
200                                                         $index = $newindex;
201                                         }
202                                         $values = explode(';',$value );
203                                         $key = strtoupper($keyvalue[0]);
204                                         $key = strtr($key, '=', '');
205                                         $key = strtr($key, ',',';');
206                                         $keys = explode(';' ,$key);
207
208                                         if($keys[0] == 'TEL'){
209                                                 if(substr_count($key, 'WORK') > 0){
210                                                                 if(substr_count($key, 'FAX') > 0){
211                                                                                 if(!isset($contact->phone_fax)){
212                                                                                         $contact->phone_fax = $value;
213                                                                                 }
214                                                                 }else{
215                                                                         if(!isset($contact->phone_work)){
216                                                                                         $contact->phone_work = $value;
217                                                                         }
218                                                                 }
219                                                 }
220                                                 if(substr_count($key, 'HOME') > 0){
221                                                                 if(substr_count($key, 'FAX') > 0){
222                                                                                 if(!isset($contact->phone_fax)){
223                                                                                         $contact->phone_fax = $value;
224                                                                                 }
225                                                                 }else{
226                                                                         if(!isset($contact->phone_home)){
227                                                                                         $contact->phone_home = $value;
228                                                                         }
229                                                                 }
230                                                 }
231                                                 if(substr_count($key, 'CELL') > 0){
232                                                                 if(!isset($contact->phone_mobile)){
233                                                                                 $contact->phone_mobile = $value;
234                                                                 }
235
236                                                 }
237                                                 if(substr_count($key, 'FAX') > 0){
238                                                                                 if(!isset($contact->phone_fax)){
239                                                                                         $contact->phone_fax = $value;
240                                                 }
241
242                                                 }
243
244                                         }
245                                         if($keys[0] == 'N'){
246                                                 if(sizeof($values) > 0)
247                                                         $contact->last_name = $values[0];
248                                                 if(sizeof($values) > 1)
249                                                         $contact->first_name = $values[1];
250                                                 if(sizeof($values) > 2)
251                                                         $contact->salutation = $values[2];
252
253
254
255                                         }
256                                         if($keys[0] == 'FN'){
257                                                 $fullname = $value;
258
259
260                                         }
261
262                 }
263                                         if($keys[0] == 'ADR'){
264                                                 if(substr_count($key, 'WORK') > 0 && (substr_count($key, 'POSTAL') > 0|| substr_count($key, 'PARCEL') == 0)){
265
266                                                                 if(!isset($contact->primary_address_street) && sizeof($values) > 2){
267                                         $textBreaks = array("\n", "\r");
268                                         $vcardBreaks = array("=0A", "=0D");
269                                                                                 $contact->primary_address_street = str_replace($vcardBreaks, $textBreaks, $values[2]);
270                                                                 }
271                                                                 if(!isset($contact->primary_address_city) && sizeof($values) > 3){
272                                                                                 $contact->primary_address_city = $values[3];
273                                                                 }
274                                                                 if(!isset($contact->primary_address_state) && sizeof($values) > 4){
275                                                                                 $contact->primary_address_state = $values[4];
276                                                                 }
277                                                                 if(!isset($contact->primary_address_postalcode) && sizeof($values) > 5){
278                                                                                 $contact->primary_address_postalcode = $values[5];
279                                                                 }
280                                                                 if(!isset($contact->primary_address_country) && sizeof($values) > 6){
281                                                                                 $contact->primary_address_country = $values[6];
282                                                                 }
283                                                 }
284                                         }
285
286                                         if($keys[0] == 'TITLE'){
287                                                 $contact->title = $value;
288
289                                         }
290                                         if($keys[0] == 'EMAIL'){
291                         $field = 'email' . $email_suffix;
292                                                 if(!isset($contact->$field)) {
293                                                    $contact->$field = $value;
294                                                 }
295
296                                                 if($email_suffix == 1) {
297                                                    $_REQUEST['email1'] = $value;
298                                                 }
299
300                                                 $email_suffix++;
301                                         }
302
303                                         if($keys[0] == 'ORG'){
304                         $GLOBALS['log']->debug('I found a company name');
305                                                 if(!empty($value)){
306                             $GLOBALS['log']->debug('I found a company name (fer real)');
307                             if ( is_a($contact,"Contact") || is_a($contact,"Lead") ) {
308                                 $GLOBALS['log']->debug('And Im dealing with a person!');
309                                 $accountBean = loadBean('Accounts');
310                                 // It's a contact, we better try and match up an account
311                                                                 $full_company_name = trim($values[0]);
312                                 // Do we have a full company name match?
313                                 $result = $accountBean->retrieve_by_string_fields(array('name' => $full_company_name, 'deleted' => 0));
314                                 if ( ! isset($result->id) ) {
315                                     // Try to trim the full company name down, see if we get some other matches
316                                     $vCardTrimStrings = array('/ltd\.*/i'=>'',
317                                                               '/llc\.*/i'=>'',
318                                                               '/gmbh\.*/i'=>'',
319                                                               '/inc\.*/i'=>'',
320                                                               '/\.com/i'=>'',
321                                         );
322                                     // Allow users to override the trimming strings
323                                     if ( file_exists('custom/include/vCardTrimStrings.php') ) {
324                                         require_once('custom/include/vCardTrimStrings.php');
325                                     }
326                                     $short_company_name = trim(preg_replace(array_keys($vCardTrimStrings),$vCardTrimStrings,$full_company_name)," ,.");
327
328                                     $GLOBALS['log']->debug('Trying an extended search for: '.$short_company_name);
329                                     $result = $accountBean->retrieve_by_string_fields(array('name' => $short_company_name, 'deleted' => 0));
330                                 }
331
332                                 if (  is_a($contact,"Lead") || ! isset($result->id) ) {
333                                     // We could not find a parent account, or this is a lead so only copy the name, no linking
334                                     $GLOBALS['log']->debug("Did not find a matching company ($full_company_name)");
335                                     $contact->account_id = '';
336                                     $contact->account_name = $full_company_name;
337                                 } else {
338                                     $GLOBALS['log']->debug("Found a matching company: ".$result->name);
339                                     $contact->account_id = $result->id;
340                                     $contact->account_name = $result->name;
341                                 }
342                                 $contact->department = $values[1];
343                             } else{
344                                                                 $contact->department = $value;
345                             }
346                                                 }
347
348                                         }
349
350                                 }
351
352
353
354
355                         //FOUND THE BEGINING OF THE VCARD
356                         if(!$start && substr_count(strtoupper($line), 'BEGIN:VCARD')){
357                                 $start = true;
358                         }
359
360                 }
361
362         if ( is_a($contact, "Contact") && empty($contact->account_id) && !empty($contact->account_name) ) {
363             $GLOBALS['log']->debug("Look ma! I'm creating a new account: ".$contact->account_name);
364             // We need to create a new account
365             $accountBean = loadBean('Accounts');
366             // Populate the newly created account with all of the contact information
367             foreach ( $contact->field_defs as $field_name => $field_def ) {
368                 if ( !empty($contact->$field_name) ) {
369                     $accountBean->$field_name = $contact->$field_name;
370                 }
371             }
372             $accountBean->name = $contact->account_name;
373             $accountBean->save();
374             $contact->account_id = $accountBean->id;
375         }
376
377         $contactId = $contact->save();
378         return $contactId;
379         }
380         }
381
382
383
384
385
386
387
388
389 ?>