2 require_once('inc/init.php');
3 require_once('inc/Contact_Vcard_Parse.php');
6 if(!$_SESSION['ldapab']['username']){
7 header("Location: login.php");
12 if(isset($_FILES['userfile'])){
13 if (is_uploaded_file($_FILES['userfile']['tmp_name'])){
14 if(preg_match('/\.vcf$/i', $_FILES['userfile']['name'])){
16 $vcfparser = new Contact_Vcard_Parse();
17 $vcards = $vcfparser->fromFile($_FILES['userfile']['tmp_name']);
19 $error = "Only *.vcf accepted";
22 switch($_FILES['userfile']['error']){
23 case 0: //no error; possible file attack!
24 $error = "There was a problem with your upload.";
26 case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
27 $error = "The file you are trying to upload is too big.";
29 case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
30 $error = "The file you are trying to upload is too big.";
32 case 3: //uploaded file was only partially uploaded
33 $error = "The file you are trying upload was only partially uploaded.";
35 case 4: //no file was uploaded
36 $error = "You must select a VCF file for upload.";
38 default: //a default error, just in case! :)
39 $error = "There was a problem with your upload.";
45 //prepare templates for all found entries
48 foreach ($vcards as $vcard){
49 $entry = vcard_entry($vcard);
50 $smarty->clear_all_assign();
52 $smarty->assign('entry',$entry);
53 $list .= $smarty->fetch('import_entry.tpl');
61 $smarty->assign('error',$error);
62 $smarty->assign('list',$list);
64 $smarty->display('header.tpl');
65 $smarty->display('import.tpl');
66 $smarty->display('footer.tpl');
69 function vcard_entry($vcf){
73 $entry['name'] = $vcf['N'][0]['value'][0][0];
74 $entry['givenname'] = trim($vcf['N'][0]['value'][1][0].' '.$vcf['N'][0]['value'][2][0]);
75 $entry['title'] = $vcf['N'][0]['value'][3][0];
76 $entry['organization'] = $vcf['ORG'][0]['value'][0][0];
77 $entry['office'] = $vcf['ORG'][0]['value'][1][0];
78 $entry['note'] = $vcf['NOTE'][0]['value'][0][0];
79 $entry['url'] = $vcf['URL'][0]['value'][0][0];
80 $entry['photo'] = $vcf['PHOTO'][0]['value'][0][0];
81 $bday = $vcf['BDAY'][0]['value'][0][0];
82 $entry['birthday'] = substr($bday,0,4).'-'.substr($bday,4,2).'-'.substr($bday,6,2);
84 foreach((array) $vcf['TEL'] as $tel){
85 if( empty($entry['phone']) &&
86 (my_array_search('WORK',(array) $tel['param']['TYPE']) != false ||
87 my_array_search('VOICE',(array) $tel['param']['TYPE']) != false))
90 $entry['phone'] = $tel['value'][0][0];
91 }elseif(empty($entry['fax']) &&
92 my_array_search('FAX',(array) $tel['param']['TYPE']) !== false){
93 $entry['fax'] = $tel['value'][0][0];
94 }elseif(empty($entry['mobile']) &&
95 my_array_search('CELL',(array) $tel['param']['TYPE']) !== false){
96 $entry['mobile'] = $tel['value'][0][0];
97 }elseif(empty($entry['pager']) &&
98 my_array_search('PAGER',(array) $tel['param']['TYPE']) !== false){
99 $entry['pager'] = $tel['value'][0][0];
100 }elseif(empty($entry['homephone']) &&
101 my_array_search('HOME',(array) $tel['param']['TYPE']) !== false &&
102 my_array_search('VOICE',(array) $tel['param']['TYPE']) !== false){
103 $entry['homephone'] = $tel['value'][0][0];
106 foreach((array) $vcf['EMAIL'] as $mail){
107 if (! in_array($mail['value'][0][0], (array)$entry['mail']))
108 $entry['mail'][] = $mail['value'][0][0];
110 foreach((array) $vcf['ADR'] as $adr){
111 if(my_array_search('HOME',(array)$adr['param']['TYPE']) !== false){
112 $entry['homestreet'] = $adr['value'][2][0]."\n". //str
113 $adr['value'][5][0]." ". //plz
114 $adr['value'][3][0]; //ort
116 }elseif(my_array_search('WORK',(array)$adr['param']['TYPE']) !== false){
117 $entry['street'] = $adr['value'][2][0];
118 $entry['location'] = $adr['value'][3][0];
119 $entry['zip'] = $adr['value'][5][0];
120 $entry['country'] = $adr['value'][6][0];
121 $entry['state'] = $adr['value'][4][0];
128 function my_array_search ($needle, $haystack)
130 foreach ($haystack as $value)
132 if ($value == $needle)
136 else if ($value == strtolower($needle))