PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/include/vCard.php

https://github.com/nerdystudmuffin/dashlet-subpanels
PHP | 301 lines | 206 code | 53 blank | 42 comment | 59 complexity | 9c2a3f22212c4b3652a29df94ec715db MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  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 - 2009 SugarCRM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU 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 General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU 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 General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU 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. * Description:
  39. ********************************************************************************/
  40. class vCard{
  41. var $properties = array();
  42. var $name = 'no_name';
  43. function clear(){
  44. $this->properties = array();
  45. }
  46. function loadContact($contactid, $module='Contacts') {
  47. global $app_list_strings;
  48. require_once($GLOBALS['beanFiles'][$GLOBALS['beanList'][$module]]);
  49. $contact = new $GLOBALS['beanList'][$module]();
  50. $contact->retrieve($contactid);
  51. // cn: bug 8504 - CF/LB break Outlook's vCard import
  52. $bad = array("\n", "\r");
  53. $good = array("=0A", "=0D");
  54. $encoding = '';
  55. if(strpos($contact->primary_address_street, "\n") || strpos($contact->primary_address_street, "\r")) {
  56. $contact->primary_address_street = str_replace($bad, $good, $contact->primary_address_street);
  57. $encoding = 'QUOTED-PRINTABLE';
  58. }
  59. $this->setName(from_html($contact->first_name), from_html($contact->last_name), $app_list_strings['salutation_dom'][from_html($contact->salutation)]);
  60. $this->setBirthDate(from_html($contact->birthdate));
  61. $this->setPhoneNumber(from_html($contact->phone_fax), 'FAX');
  62. $this->setPhoneNumber(from_html($contact->phone_home), 'HOME');
  63. $this->setPhoneNumber(from_html($contact->phone_mobile), 'CELL');
  64. $this->setPhoneNumber(from_html($contact->phone_work), 'WORK');
  65. $this->setEmail(from_html($contact->email1));
  66. $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);
  67. $this->setORG(from_html($contact->account_name), from_html($contact->department));
  68. $this->setTitle($contact->title);
  69. }
  70. function setTitle($title){
  71. $this->setProperty("TITLE",$title );
  72. }
  73. function setORG($org, $dep){
  74. $this->setProperty("ORG","$org;$dep" );
  75. }
  76. function setAddress($address, $city, $state,$postal, $country, $type, $encoding=''){
  77. if(!empty($encoding)) {
  78. $encoding = ";ENCODING={$encoding}";
  79. }
  80. $this->setProperty("ADR;$type$encoding",";;$address;$city;$state;$postal;$country" );
  81. }
  82. function setName($first_name, $last_name, $prefix){
  83. $this->name = strtr($first_name.'_'.$last_name, ' ' , '_');
  84. $this->setProperty('N',$last_name.';'.$first_name.';'.$prefix );
  85. $this->setProperty('FN',"$prefix $first_name $last_name");
  86. }
  87. function setEmail($address){
  88. $this->setProperty('EMAIL;INTERNET', $address);
  89. }
  90. function setPhoneNumber( $number, $type){
  91. $this->setProperty("TEL;$type", $number);
  92. }
  93. function setBirthDate($date){
  94. $this->setProperty('BDAY',$date);
  95. }
  96. function getProperty($name){
  97. if(isset($this->properties[$name]))
  98. return $this->properties[$name];
  99. return null;
  100. }
  101. function setProperty($name, $value){
  102. $this->properties[$name] = $value;
  103. }
  104. function toString(){
  105. $temp = "BEGIN:VCARD\n";
  106. foreach($this->properties as $key=>$value){
  107. $temp .= $key. ':'.$value."\n";
  108. }
  109. $temp.= "END:VCARD\n";
  110. return $temp;
  111. }
  112. function saveVCard(){
  113. global $locale;
  114. $content = $this->toString();
  115. header("Content-Disposition: attachment; filename={$this->name}.vcf");
  116. header("Content-Type: text/x-vcard; charset=".$locale->getExportCharset());
  117. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
  118. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT" );
  119. header("Cache-Control: max-age=0");
  120. header("Pragma: public");
  121. header("Content-Length: ".strlen($content));
  122. print $locale->translateCharset($content, 'UTF-8', $locale->getExportCharset());
  123. }
  124. function importVCard($filename, $module='Contacts'){
  125. global $current_user;
  126. $lines = file($filename);
  127. $start = false;
  128. require_once($GLOBALS['beanFiles'][$GLOBALS['beanList'][$module]]);
  129. $contact = new $GLOBALS['beanList'][$module]();
  130. $contact->title = 'imported';
  131. $contact->assigned_user_id = $current_user->id;
  132. $fullname = '';
  133. for($index = 0; $index < sizeof($lines); $index++){
  134. $line = $lines[$index];
  135. $line = trim($line);
  136. if($start){
  137. //VCARD is done
  138. if(substr_count(strtoupper($line), 'END:VCARD')){
  139. if(!isset($contact->last_name)){
  140. $contact->last_name = $fullname;
  141. }
  142. return $contact->save();
  143. }
  144. $keyvalue = split(':',$line);
  145. if(sizeof($keyvalue)==2){
  146. $value = $keyvalue[1];
  147. for($newindex= $index + 1; $newindex < sizeof($lines), substr_count($lines[$newindex], ':') == 0; $newindex++){
  148. $value .= $lines[$newindex];
  149. $index = $newindex;
  150. }
  151. $values = split(';',$value );
  152. $key = strtoupper($keyvalue[0]);
  153. $key = strtr($key, '=', '');
  154. $key = strtr($key, ',',';');
  155. $keys = split(';' ,$key);
  156. if($keys[0] == 'TEL'){
  157. if(substr_count($key, 'WORK') > 0){
  158. if(substr_count($key, 'FAX') > 0){
  159. if(!isset($contact->phone_fax)){
  160. $contact->phone_fax = $value;
  161. }
  162. }else{
  163. if(!isset($contact->phone_work)){
  164. $contact->phone_work = $value;
  165. }
  166. }
  167. }
  168. if(substr_count($key, 'HOME') > 0){
  169. if(substr_count($key, 'FAX') > 0){
  170. if(!isset($contact->phone_fax)){
  171. $contact->phone_fax = $value;
  172. }
  173. }else{
  174. if(!isset($contact->phone_home)){
  175. $contact->phone_home = $value;
  176. }
  177. }
  178. }
  179. if(substr_count($key, 'CELL') > 0){
  180. if(!isset($contact->phone_mobile)){
  181. $contact->phone_mobile = $value;
  182. }
  183. }
  184. if(substr_count($key, 'FAX') > 0){
  185. if(!isset($contact->phone_fax)){
  186. $contact->phone_fax = $value;
  187. }
  188. }
  189. }
  190. if($keys[0] == 'N'){
  191. if(sizeof($values) > 0)
  192. $contact->last_name = $values[0];
  193. if(sizeof($values) > 1)
  194. $contact->first_name = $values[1];
  195. }
  196. if($keys[0] == 'FN'){
  197. $fullname = $value;
  198. }
  199. }
  200. if($keys[0] == 'ADR'){
  201. if(substr_count($key, 'WORK') > 0 && (substr_count($key, 'POSTAL') > 0|| substr_count($key, 'PARCEL') == 0)){
  202. if(!isset($contact->primary_address_street) && sizeof($values) > 2){
  203. $textBreaks = array("\n", "\r");
  204. $vcardBreaks = array("=0A", "=0D");
  205. $contact->primary_address_street = str_replace($vcardBreaks, $textBreaks, $values[2]);
  206. }
  207. if(!isset($contact->primary_address_city) && sizeof($values) > 3){
  208. $contact->primary_address_city = $values[3];
  209. }
  210. if(!isset($contact->primary_address_state) && sizeof($values) > 4){
  211. $contact->primary_address_state = $values[4];
  212. }
  213. if(!isset($contact->primary_address_postalcode) && sizeof($values) > 5){
  214. $contact->primary_address_postalcode = $values[5];
  215. }
  216. if(!isset($contact->primary_address_country) && sizeof($values) > 6){
  217. $contact->primary_address_country = $values[6];
  218. }
  219. }
  220. }
  221. if($keys[0] == 'TITLE'){
  222. $contact->title = $value;
  223. }
  224. if($keys[0] == 'EMAIL'){
  225. if(!isset($contact->email1)){
  226. //Set email1 as REQUEST variable for legacy email save
  227. $_REQUEST['email1'] = $value;
  228. $contact->email1 = $value;
  229. }else if(!isset($contact->email2)){
  230. $contact->email2 = $value;
  231. }
  232. }
  233. if($keys[0] == 'ORG'){
  234. if(sizeof($values) > 1){
  235. $contact->department = $values[1];
  236. }
  237. }
  238. }
  239. //FOUND THE BEGINING OF THE VCARD
  240. if(!$start && substr_count(strtoupper($line), 'BEGIN:VCARD')){
  241. $start = true;
  242. }
  243. }
  244. return $contact->save();
  245. }
  246. }
  247. ?>