]> git.sur5r.net Git - contagged/blob - import.php
Merge pull request #15 from cweiske/master
[contagged] / import.php
1 <?php
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 = array();
71
72
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);
83
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))
88             {
89         // Work phone
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];
104       }
105     }
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];
109     }
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
115
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];
122       }
123     }
124
125     return $entry;
126 }
127
128 function my_array_search ($needle, $haystack)
129 {
130         foreach ($haystack as $value)
131         {
132                 if ($value == $needle)
133                 {
134                         return true;
135                 }
136                 else if ($value == strtolower($needle))
137                 {
138                         return true;
139                 }
140
141         }
142         return false;
143 }
144
145
146 ?>