]> git.sur5r.net Git - contagged/blob - import.php
PHP5 compatibility and code cleanup
[contagged] / import.php
1 <?
2   require_once('init.php');
3   require_once('Contact_Vcard_Parse.php');
4   require_once('xml.php');
5   ldap_login();
6
7   if(! $_SESSION['ldapab']['username'] ){
8     header("Location: login.php");
9     exit;
10   }
11
12   $error = '';
13   if(isset($_FILES['userfile'])){
14     if (is_uploaded_file($_FILES['userfile']['tmp_name'])){
15       if(preg_match('/\.vcf$/i', $_FILES['userfile']['name'])){
16         //parse VCF
17         $vcfparser = new Contact_Vcard_Parse();
18         $vcards    = $vcfparser->fromFile($_FILES['userfile']['tmp_name']);
19       }elseif(preg_match('/^addressbook\.xml$/i', $_FILES['userfile']['name'])){
20         //parse Zaurus Addressbook
21         $contacts  = XML_unserialize(@join('',@file($_FILES['userfile']['tmp_name'])));
22       }else{
23           $error = "Only *.vcf or addressbook.xml are accepted";
24       }
25     }else{
26       switch($_FILES['userfile']['error']){
27        case 0: //no error; possible file attack!
28          $error = "There was a problem with your upload.";
29          break;
30        case 1: //uploaded file exceeds the upload_max_filesize directive in php.ini
31          $error = "The file you are trying to upload is too big.";
32          break;
33        case 2: //uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the html form
34          $error = "The file you are trying to upload is too big.";
35          break;
36        case 3: //uploaded file was only partially uploaded
37          $error = "The file you are trying upload was only partially uploaded.";
38          break;
39        case 4: //no file was uploaded
40          $error = "You must select a VCF file for upload.";
41          break;
42        default: //a default error, just in case!  :)
43          $error = "There was a problem with your upload.";
44          break;
45       }
46     }
47   }
48
49   //prepare templates for all found entries
50   $list = '';
51   if(count($vcards)){
52     foreach ($vcards as $vcard){
53       $entry = vcard_entry($vcard);
54       $smarty->clear_all_assign();
55       tpl_std();
56       $smarty->assign('entry',$entry);
57       $list .= $smarty->fetch('import_entry.tpl');
58     }
59   }elseif(count($contacts)){
60     foreach ($contacts['AddressBook']['Contacts']['Contact'] as $contact){
61       if (!is_array($contact)) continue;
62       $entry = zaurus_entry($contact);
63       $smarty->clear_all_assign();
64       tpl_std();
65       $smarty->assign('entry',$entry);
66       $list .= $smarty->fetch('import_entry.tpl');
67     }
68   }
69
70   //prepare templates
71   tpl_std();
72   tpl_orgs();
73   tpl_markers();
74   $smarty->assign('error',$error);
75   $smarty->assign('list',$list);
76   //display templates
77   $smarty->display('header.tpl');
78   $smarty->display('import.tpl');
79   $smarty->display('footer.tpl');
80
81
82 function zaurus_entry($cnct){
83   $entry['name']         = $cnct['LastName'];
84   $entry['givenname']    = trim($cnct['FirstName'].' '.$cnct['MiddleName']);
85   $entry['title']        = $cnct['JobTitle'];
86   $entry['organization'] = $cnct['Company'];
87   $entry['office']       = $cnct['Office'];
88   $entry['note']         = $cnct['Notes'];
89   $entry['mail']         = split(' ',$cnct['Emails']);
90   $entry['street']       = $cnct['BusinessStreet'];
91   $entry['location']     = $cnct['BusinessCity'];
92   $entry['zip']          = $cnct['BusinessZip'];
93   $entry['homestreet']   = $cnct['HomeStreet']."\n". //str
94                            $cnct['HomeZip']." ".     //plz
95                            $cnct['HomeCity'];        //ort
96   $entry['homephone']    = $cnct['HomePhone'];
97   $entry['phone']        = $cnct['BusinessPhone'];
98   $entry['fax']          = empty($cnct['BusinessFax']) ? $cnct['HomeFax'] : $cnct['BusinessFax'];
99   $entry['mobile']       = empty($cnct['HomeMobile']) ? $cnct['BusinessMobile'] : $cnct['HomeMobile'];
100   $entry['fax']          = empty($cnct['BusinessFax']) ? $cnct['HomeFax'] : $cnct['BusinessFax'];
101
102 #  $entry['anniversary'] = $cnt[''];
103
104   utf8_decode_array($entry);
105   return $entry;
106 }
107
108 function vcard_entry($vcf){
109   $entry['name']         = $vcf['N'][0]['value'][0][0];
110   $entry['givenname']    = trim($vcf['N'][0]['value'][1][0].' '.$vcf['N'][0]['value'][2][0]);
111   $entry['title']        = $vcf['N'][0]['value'][3][0];
112   $entry['organization'] = $vcf['ORG'][0]['value'][0][0];
113   $entry['office']       = $vcf['ORG'][0]['value'][1][0];
114   $entry['note']         = $vcf['NOTE'][0]['value'][0][0];
115   $entry['url']          = $vcf['URL'][0]['value'][0][0];
116   $bday                  = $vcf['BDAY'][0]['value'][0][0];
117   $entry['anniversary']  = substr($bday,0,4).'-'.substr($bday,4,2).'-'.substr($bday,6,2);
118
119   foreach($vcf['TEL'] as $tel){
120     if(   empty($entry['phone']) &&
121           array_search('WORK',$tel['param']['TYPE']) !== FALSE &&
122           array_search('VOICE',$tel['param']['TYPE']) !== FALSE){
123       // Work phone
124       $entry['phone'] = $tel['value'][0][0];
125     }elseif(empty($entry['fax']) &&
126             array_search('FAX',$tel['param']['TYPE']) !== FALSE){
127       $entry['fax'] = $tel['value'][0][0];
128     }elseif(empty($entry['mobile']) &&
129             array_search('CELL',$tel['param']['TYPE']) !== FALSE){
130       $entry['mobile'] = $tel['value'][0][0];
131     }elseif(empty($entry['pager']) &&
132             array_search('PAGER',$tel['param']['TYPE']) !== FALSE){
133       $entry['pager'] = $tel['value'][0][0];
134     }elseif(empty($entry['homephone']) &&
135             array_search('HOME',$tel['param']['TYPE']) !== FALSE &&
136             array_search('VOICE',$tel['param']['TYPE']) !== FALSE){
137       $entry['homephone'] = $tel['value'][0][0];
138     }
139   }
140   foreach($vcf['EMAIL'] as $mail){
141     $entry['mail'][] = $mail['value'][0][0];
142   }
143   foreach($vcf['ADR'] as $adr){
144     if(array_search('HOME',$adr['param']['TYPE']) !== FALSE){
145       $entry['homestreet'] = $adr['value'][2][0]."\n". //str
146                              $adr['value'][5][0]." ". //plz
147                              $adr['value'][3][0];      //ort
148
149     }elseif(array_search('WORK',$adr['param']['TYPE']) !== FALSE){
150       $entry['street']   = $adr['value'][2][0];
151       $entry['location'] = $adr['value'][3][0];
152       $entry['zip']      = $adr['value'][5][0];
153     }
154   }
155
156 /*
157   print '<pre>';
158   print_r($entry);
159   print '</pre>';
160 */
161
162   return $entry;
163 }
164
165
166 ?>