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