]> git.sur5r.net Git - contagged/blobdiff - entry.php
show: restructuring, adding new fields, ...
[contagged] / entry.php
index 594e1eb2b95fb54d8b9e1474eb4e3749413816ee..ec73909242f0d362b93c6fd239f78dbace847c25 100644 (file)
--- a/entry.php
+++ b/entry.php
@@ -5,30 +5,48 @@
   $users = get_users();
 
   //select template to use
-  if( $_SESSION[ldapab][username] &&
-     ($_REQUEST[mode]=='edit' || $_REQUEST[mode]=='copy')){
+  if (empty($_REQUEST['mode'])) { $_REQUEST['mode']='show'; }
+  if( $_SESSION['ldapab']['username'] &&
+     ($_REQUEST['mode']=='edit' || $_REQUEST['mode']=='copy')){
     $template='entry_edit.tpl';
-  }elseif($_REQUEST[mode]=='vcf'){
+  }elseif($_REQUEST['mode']=='vcf'){
     $template='entry_vcf.tpl';
   }else{
     $template='entry_show.tpl';
   }
 
-  $dn = $_REQUEST[dn];
-  #$dn = 'cn=bar foo, ou=contacts, o=cosmocode, c=de';
+  if (empty($_REQUEST['dn'])) {
+    $dn = "";
+  }else{
+    $dn = $_REQUEST['dn'];
+    #$dn = 'cn=bar foo, ou=contacts, o=cosmocode, c=de';
+  }
 
   //save data if asked for
-  if($_SESSION[ldapab][username] && $_REQUEST[save]){
-    $_REQUEST[entry][jpegPhoto][]=_getUploadData();
+  if($_SESSION['ldapab']['username'] && !empty($_REQUEST['save']) && $_REQUEST['save']){
+    // prepare special data
+    $_REQUEST['entry']['photo']  = _getUploadData();
+    $_REQUEST['entry']['marker'] = explode(',',$_REQUEST['entry']['markers']);
+    unset($_REQUEST['entry']['markers']);
+
+    foreach(array_keys($_REQUEST['entry']) as $field){
+        if($FIELDS['_'.$field]){
+            // entry has to be handled as array -> clean it up (trim, unique, sort)
+            $_REQUEST['entry'][$field] = array_map('trim',$_REQUEST['entry'][$field]);
+            $_REQUEST['entry'][$field] = array_unique($_REQUEST['entry'][$field]);
+            $_REQUEST['entry'][$field] = array_filter($_REQUEST['entry'][$field]);
+            natcasesort($_REQUEST['entry'][$field]);
+        }
+    }
     $dn = _saveData();
   }
 
   if(empty($dn)){
-    if(!$_REQUEST[mode]=='edit'){
+    if(!$_REQUEST['mode']=='edit'){
       $smarty->assign('error','No dn was given');
       $template = 'error.tpl';
     }
-  }elseif($_REQUEST[del]){
+  }elseif(!empty($_REQUEST['del']) && $_REQUEST['del']){
     _delEntry($dn);
   }elseif(!_fetchData($dn)){
     $smarty->assign('error',"The requested entry '$dn' was not found");
   tpl_timezone();
   tpl_country();
   //display templates
-  if($_REQUEST[mode]=='vcf'){
+  if($_REQUEST['mode']=='vcf'){
     $entry = $smarty->get_template_vars('entry');
-    $filename = $entry[givenname].'_'.$entry[name].'.vcf';
+    $filename = $entry['givenname'].'_'.$entry['name'].'.vcf';
     header("Content-Disposition: attachment; filename=\"$filename\"");
-    header("Content-type: text/x-vcard; name=\"$filename\"");
+    header("Content-type: text/x-vcard; name=\"$filename\"; charset=utf-8");
     $smarty->display($template);
   }else{
-    $smarty->display('header.tpl');
+    header('Content-Type: text/html; charset=utf-8');
     $smarty->display($template);
-    $smarty->display('footer.tpl');
   }
 
   //--------------------------------------------------------------
@@ -77,8 +94,8 @@
     $entry  = $result[0];
 
     //remove dn from entry when copy
-    if($_REQUEST[mode] == 'copy'){
-      $entry[dn]='';
+    if(!empty($_REQUEST['mode']) && $_REQUEST['mode'] == 'copy'){
+      $entry['dn']='';
     }
 
     //assign entry to template:
@@ -89,61 +106,61 @@ print_r($entry);
 print '</pre>';*/
 
     // make username from dn for manager:
-    $smarty->assign('managername',$users[$entry[manager][0]]);
+    if (empty($entry['manager'])) { $entry['manager']=array(""); }
+    if (empty($users[$entry['manager'][0]])) { $users[$entry['manager'][0]]=''; }
+    $smarty->assign('managername',$users[$entry['manager'][0]]);
     return true;
   }
 
   /**
-   * saves the data from $_REQUEST[entry] to the LDAP directory
+   * saves the data from $_REQUEST['entry'] to the LDAP directory
    *
    * returns given or constructed dn
    */
   function _saveData(){
     global $LDAP_CON;
     global $conf;
-    $entries = namedentries();
-    $entries['mail']='mail';  //special field mail isn't in entries so we add it here
-    if($conf[extended]){
-      $entries['marker']='marker'; //same for marker inextended schema
-    }
+    global $FIELDS;
+    global $OCLASSES;
 
-    $entry = $_REQUEST[entry];
-    $dn    = $_REQUEST[dn];
+    $entry = $_REQUEST['entry'];
+    $dn    = $_REQUEST['dn'];
     //construct new dn
     $now    = time();
     $newdn  = 'uid='.$now;
-    if($_REQUEST[type] == 'private'){
-      $newdn .= ', '.$conf[privatebook].', '.$_SESSION[ldapab][binddn];
+    if (empty($_REQUEST['type'])) { $_REQUEST['type']='public'; }
+    if($_REQUEST['type'] == 'private'){
+      $newdn .= ', '.$conf['privatebook'].', '.$_SESSION['ldapab']['binddn'];
     }else{
-      $newdn .= ', '.$conf[publicbook];
+      $newdn .= ', '.$conf['publicbook'];
     }
-    $entry[cn]          = $entry[givenname].' '.$entry[name];;
+    $entry['displayname'] = $entry['givenname'].' '.$entry['name'];;
     $entry = prepare_ldap_entry($entry);
 
-/*print '<pre>';
+/*
+print '<pre>';
 print_r($entry);
-print '</pre>';*/
+print '</pre>';
+*/
 
     if(empty($dn)){
       //new entry
-      $entry[uid][] = $now;
+      $entry['uid'][] = $now;
       $r = ldap_add($LDAP_CON,$newdn,$entry);
       tpl_ldaperror();
       return $newdn;
     }else{
-      // in extended mode we have to make sure the right classes are set
-      if($conf[extended]){
-        ldap_store_objectclasses($dn,array('inetOrgPerson','contactPerson'));
-      }
-      // in openxchange mode we have to make sure the right classes are set
-      if ($conf[openxchange]){
-        ldap_store_objectclasses($dn,array('inetOrgPerson','OXUserObject'));
-      }
-      //modify entry (touches only our attributes)
-      foreach (array_keys($entries) as $key){
+      // update the objectClasses
+      ldap_store_objectclasses($dn,$OCLASSES);
+      unset($entry['objectclass']);
+
+      //modify entry attribute by attribute - this ensure we don't delete unknown stuff
+      foreach (array_values($FIELDS) as $key){
         if($key == 'dn'){
           continue;
         }elseif(empty($entry[$key])){
+          // field is empty -> handle deletion (except for photo unless deletion triggered)
+          if (empty($_REQUEST['delphoto'])) { $_REQUEST['delphoto']=0; }
           if($key == 'jpegPhoto' && !$_REQUEST['delphoto']){
             continue;
           }
@@ -177,17 +194,17 @@ print '</pre>';*/
    * gets the binary data from an uploaded file
    */
   function _getUploadData(){
-    $file = $_FILES[photoupload];
+    $file = $_FILES['photoupload'];
 
-    if (is_uploaded_file($file[tmp_name])) {
-      if(preg_match('=image/p?jpe?g=',$file[type])){
-        $fh = fopen($file[tmp_name],'r');
-        $data = fread($fh,$file[size]);
+    if (is_uploaded_file($file['tmp_name'])) {
+      if(preg_match('=image/p?jpe?g=',$file['type'])){
+        $fh = fopen($file['tmp_name'],'r');
+        $data = fread($fh,$file['size']);
         fclose($fh);
-        unlink($file[tmp_name]);
+        unlink($file['tmp_name']);
         return $data;
       }
     }
     return '';
   }
-?>
+