]> git.sur5r.net Git - contagged/blob - entry.php
tagging
[contagged] / entry.php
1 <?
2   require_once('init.php');
3   ldap_login();
4
5   $users = get_users();
6
7   //select template to use
8   if( $_SESSION[ldapab][username] &&
9      ($_REQUEST[mode]=='edit' || $_REQUEST[mode]=='copy')){
10     $template='entry_edit.tpl';
11   }elseif($_REQUEST[mode]=='vcf'){
12     $template='entry_vcf.tpl';
13   }else{
14     $template='entry_show.tpl';
15   }
16
17   $dn = $_REQUEST[dn];
18   #$dn = 'cn=bar foo, ou=contacts, o=cosmocode, c=de';
19
20   //save data if asked for
21   if($_SESSION[ldapab][username] && $_REQUEST[save]){
22     // prepare special data
23     $_REQUEST[entry][jpegPhoto][]=_getUploadData();
24     $_REQUEST[entry][marker] = explode(',',$_REQUEST[entry][markers]);
25     $_REQUEST[entry][marker] = array_map('trim',$_REQUEST[entry][marker]);
26     $dn = _saveData();
27   }
28
29   if(empty($dn)){
30     if(!$_REQUEST[mode]=='edit'){
31       $smarty->assign('error','No dn was given');
32       $template = 'error.tpl';
33     }
34   }elseif($_REQUEST[del]){
35     _delEntry($dn);
36   }elseif(!_fetchData($dn)){
37     $smarty->assign('error',"The requested entry '$dn' was not found");
38     $template = 'error.tpl';
39   }
40
41   //prepare templates
42   $smarty->assign('dn',$dn);
43   $smarty->assign('managers',$users);
44   tpl_std();
45   tpl_orgs();
46   tpl_markers();
47   tpl_categories();
48   tpl_timezone();
49   tpl_country();
50   //display templates
51   if($_REQUEST[mode]=='vcf'){
52     $entry = $smarty->get_template_vars('entry');
53     $filename = $entry[givenname].'_'.$entry[name].'.vcf';
54     header("Content-Disposition: attachment; filename=\"$filename\"");
55     header("Content-type: text/x-vcard; name=\"$filename\"; charset=utf-8");
56     $smarty->display($template);
57   }else{
58     header('Content-Type: text/html; charset=utf-8');
59     $smarty->display('header.tpl');
60     $smarty->display('list_filter.tpl');
61     $smarty->display($template);
62     $smarty->display('footer.tpl');
63   }
64
65   //--------------------------------------------------------------
66
67   /**
68    * fetches the Data from the LDAP directory and assigns it to
69    * the global smarty object using tpl_entry()
70    */
71   function _fetchData($dn){
72     global $LDAP_CON;
73     global $conf;
74     global $smarty;
75     global $users; //contains the users for manager role
76
77     $sr = ldap_search($LDAP_CON,$dn,'(objectClass=inetOrgPerson)');
78     if(!ldap_count_entries($LDAP_CON,$sr)){
79       return false;
80     }
81     $result = ldap_get_binentries($LDAP_CON, $sr);
82     $entry  = $result[0];
83
84     //remove dn from entry when copy
85     if($_REQUEST[mode] == 'copy'){
86       $entry[dn]='';
87     }
88
89     //assign entry to template:
90     tpl_entry($entry);
91
92 /*print '<pre>';
93 print_r($entry);
94 print '</pre>';*/
95
96     // make username from dn for manager:
97     $smarty->assign('managername',$users[$entry[manager][0]]);
98     return true;
99   }
100
101   /**
102    * saves the data from $_REQUEST[entry] to the LDAP directory
103    *
104    * returns given or constructed dn
105    */
106   function _saveData(){
107     global $LDAP_CON;
108     global $conf;
109     $entries = namedentries();
110     $entries['mail']='mail';  //special field mail isn't in entries so we add it here
111     if($conf[extended]){
112       $entries['marker']='marker'; //same for marker inextended schema
113     }
114
115     $entry = $_REQUEST[entry];
116     $dn    = $_REQUEST[dn];
117     //construct new dn
118     $now    = time();
119     $newdn  = 'uid='.$now;
120     if($_REQUEST[type] == 'private'){
121       $newdn .= ', '.$conf[privatebook].', '.$_SESSION[ldapab][binddn];
122     }else{
123       $newdn .= ', '.$conf[publicbook];
124     }
125     $entry[cn]          = $entry[givenname].' '.$entry[name];;
126     $entry = prepare_ldap_entry($entry);
127
128 /*print '<pre>';
129 print_r($entry);
130 print '</pre>';*/
131
132     if(empty($dn)){
133       //new entry
134       $entry[uid][] = $now;
135       $r = ldap_add($LDAP_CON,$newdn,$entry);
136       tpl_ldaperror();
137       return $newdn;
138     }else{
139       // in extended mode we have to make sure the right classes are set
140       if($conf[extended]){
141         ldap_store_objectclasses($dn,array('inetOrgPerson','contactPerson'));
142       }
143       // in openxchange mode we have to make sure the right classes are set
144       if ($conf[openxchange]){
145         ldap_store_objectclasses($dn,array('inetOrgPerson','OXUserObject'));
146       }
147       //modify entry (touches only our attributes)
148       foreach (array_keys($entries) as $key){
149         if($key == 'dn'){
150           continue;
151         }elseif(empty($entry[$key])){
152           if($key == 'jpegPhoto' && !$_REQUEST['delphoto']){
153             continue;
154           }
155           unset($del);
156           $del[$key]=array();
157           $r = @ldap_mod_replace($LDAP_CON,$dn,$del);
158           tpl_ldaperror("del $key");
159         }else{
160           unset($add);
161           $add[$key]=$entry[$key];
162           $r = @ldap_mod_replace($LDAP_CON,$dn,$add);
163           tpl_ldaperror("mod $key");
164         }
165       }
166       return $dn;
167     }
168   }
169
170   /**
171    * does as the name says - delete the whole entry
172    */
173   function _delEntry($dn){
174     global $LDAP_CON;
175     if(ldap_full_delete($LDAP_CON,$dn,true)){
176       header("Location: index.php");
177       exit;
178     }
179   }
180
181   /**
182    * gets the binary data from an uploaded file
183    */
184   function _getUploadData(){
185     $file = $_FILES[photoupload];
186
187     if (is_uploaded_file($file[tmp_name])) {
188       if(preg_match('=image/p?jpe?g=',$file[type])){
189         $fh = fopen($file[tmp_name],'r');
190         $data = fread($fh,$file[size]);
191         fclose($fh);
192         unlink($file[tmp_name]);
193         return $data;
194       }
195     }
196     return '';
197   }
198 ?>