]> git.sur5r.net Git - contagged/blob - import.php
help formatting
[contagged] / import.php
1 <?
2 require_once('inc/init.php');
3 require_once('inc/Contact_Vcard_Parse.php');
4 ldap_login();
5
6 if(! $_SESSION['ldapab']['username'] ){
7   header("Location: login.php");
8   exit;
9 }
10
11 $error = '';
12 if(isset($_FILES['userfile'])){
13   if (is_uploaded_file($_FILES['userfile']['tmp_name'])){
14     if(preg_match('/\.vcf$/i', $_FILES['userfile']['name'])){
15       //parse VCF
16       $vcfparser = new Contact_Vcard_Parse();
17       $vcards    = $vcfparser->fromFile($_FILES['userfile']['tmp_name']);
18     }else{
19         $error = "Only *.vcf accepted";
20     }
21   }else{
22     switch($_FILES['userfile']['error']){
23      case 0: //no error; possible file attack!
24        $error = "There was a problem with your upload.";
25        break;
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.";
28        break;
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.";
31        break;
32      case 3: //uploaded file was only partially uploaded
33        $error = "The file you are trying upload was only partially uploaded.";
34        break;
35      case 4: //no file was uploaded
36        $error = "You must select a VCF file for upload.";
37        break;
38      default: //a default error, just in case!  :)
39        $error = "There was a problem with your upload.";
40        break;
41     }
42   }
43 }
44
45 //prepare templates for all found entries
46 $list = '';
47 if(count($vcards)){
48   foreach ($vcards as $vcard){
49     $entry = vcard_entry($vcard);
50     $smarty->clear_all_assign();
51     tpl_std();
52     $smarty->assign('entry',$entry);
53     $list .= $smarty->fetch('import_entry.tpl');
54   }
55 }
56
57 //prepare templates
58 tpl_std();
59 tpl_orgs();
60 tpl_markers();
61 $smarty->assign('error',$error);
62 $smarty->assign('list',$list);
63 //display templates
64 $smarty->display('header.tpl');
65 $smarty->display('import.tpl');
66 $smarty->display('footer.tpl');
67
68
69 function vcard_entry($vcf){
70 $entry['name']         = $vcf['N'][0]['value'][0][0];
71 $entry['givenname']    = trim($vcf['N'][0]['value'][1][0].' '.$vcf['N'][0]['value'][2][0]);
72 $entry['title']        = $vcf['N'][0]['value'][3][0];
73 $entry['organization'] = $vcf['ORG'][0]['value'][0][0];
74 $entry['office']       = $vcf['ORG'][0]['value'][1][0];
75 $entry['note']         = $vcf['NOTE'][0]['value'][0][0];
76 $entry['url']          = $vcf['URL'][0]['value'][0][0];
77 $bday                  = $vcf['BDAY'][0]['value'][0][0];
78 $entry['anniversary']  = substr($bday,0,4).'-'.substr($bday,4,2).'-'.substr($bday,6,2);
79
80 foreach($vcf['TEL'] as $tel){
81   if(   empty($entry['phone']) &&
82         array_search('WORK',$tel['param']['TYPE']) !== FALSE &&
83         array_search('VOICE',$tel['param']['TYPE']) !== FALSE){
84     // Work phone
85     $entry['phone'] = $tel['value'][0][0];
86   }elseif(empty($entry['fax']) &&
87           array_search('FAX',$tel['param']['TYPE']) !== FALSE){
88     $entry['fax'] = $tel['value'][0][0];
89   }elseif(empty($entry['mobile']) &&
90           array_search('CELL',$tel['param']['TYPE']) !== FALSE){
91     $entry['mobile'] = $tel['value'][0][0];
92   }elseif(empty($entry['pager']) &&
93           array_search('PAGER',$tel['param']['TYPE']) !== FALSE){
94     $entry['pager'] = $tel['value'][0][0];
95   }elseif(empty($entry['homephone']) &&
96           array_search('HOME',$tel['param']['TYPE']) !== FALSE &&
97           array_search('VOICE',$tel['param']['TYPE']) !== FALSE){
98     $entry['homephone'] = $tel['value'][0][0];
99   }
100 }
101 foreach($vcf['EMAIL'] as $mail){
102   $entry['mail'][] = $mail['value'][0][0];
103 }
104 foreach($vcf['ADR'] as $adr){
105   if(array_search('HOME',$adr['param']['TYPE']) !== FALSE){
106     $entry['homestreet'] = $adr['value'][2][0]."\n". //str
107                            $adr['value'][5][0]." ". //plz
108                            $adr['value'][3][0];      //ort
109
110   }elseif(array_search('WORK',$adr['param']['TYPE']) !== FALSE){
111     $entry['street']   = $adr['value'][2][0];
112     $entry['location'] = $adr['value'][3][0];
113     $entry['zip']      = $adr['value'][5][0];
114   }
115 }
116
117 /*
118 print '<pre>';
119 print_r($entry);
120 print '</pre>';
121 */
122
123 return $entry;
124 }
125
126
127 ?>