+ * Warning: You must make sure that the arguments are correctly formated and
+ * sanitize use of special ldap keywords.
+ * @param string $dn the dn of the entry to be modified
+ * @param array $data the new values of the entry
+ * @param array $deleted_attribs attributes that should be deleted.
+ * @return bool result (false on error)
+ * @since 1.5.2
+ */
+ function ldap_modify($dn, $data, $deleted_attribs) {
+ /* Make sure connection is there */
+ if(!$this->open()) {
+ return false;
+ }
+
+ if(!@ldap_modify($this->linkid, $dn, $data)) {
+ $this->set_error(_("Write to address book failed"));
+ return false;
+ }
+
+ if (!@ldap_mod_del($this->linkid, $dn, $deleted_attribs)) {
+ $this->set_error(_("Unable to remove some field values"));
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Get error from LDAP resource if possible
+ *
+ * Should get error from server using the ldap_errno() and ldap_err2str() functions
+ * @param string $sError error message used when ldap error functions
+ * and connection resource are unavailable
+ * @return string error message
+ * @since 1.5.1
+ */
+ function ldap_error($sError) {
+ // it is possible that function_exists() tests are not needed
+ if(function_exists('ldap_err2str') &&
+ function_exists('ldap_errno') &&
+ is_resource($this->linkid)) {
+ return ldap_err2str(ldap_errno($this->linkid));
+ // return ldap_error($this->linkid);
+ } else {
+ return $sError;
+ }
+ }
+
+ /* ========================== Public ======================== */
+
+ /**
+ * Search the LDAP server
+ * @param string $expr search expression
+ * @return array search results
+ */
+ function search($expr) {
+ /* To be replaced by advanded search expression parsing */
+ if(is_array($expr)) return false;
+
+ // don't allow wide search when listing is disabled.
+ if ($expr=='*' && ! $this->listing) {
+ return array();
+ } elseif ($expr=='*') {
+ // allow use of wildcard when listing is enabled.
+ $expression = '(cn=*)';
+ } else {
+ /* Convert search from user's charset to the one used in ldap and sanitize */
+ $expr = $this->quotevalue($expr);
+
+ /* If search expr contains %s or %1$s, replace them with escaped values,
+ * so that a wrong printf()-style string is not created by mistake.
+ * (Probably overkill but who knows...) */
+ $expr = str_replace('%s', '\\25s', $expr);
+ $expr = str_replace('%1$s', '\\251$s', $expr);
+
+ /* Substitute %s or %1$s in printf()-formatted search_expresison with
+ * the value that the user searches for. */
+ $expression = sprintf($this->search_expression, $expr);
+
+ /* Undo sanitizing of * symbol */
+ $expression = str_replace('\2a','*',$expression);
+
+ /* Replace '**', '***' etc. with '*' in case it occurs in final
+ * search expression */
+ while(strstr($expression, '**')) {
+ $expression = str_replace('**', '*', $expression);
+ }
+ }
+
+ /* Add search filtering */
+ if ($this->filter!='')
+ $expression = '(&' . $this->filter . $expression . ')';
+
+ /* Use internal search function and return search results */
+ return $this->ldap_search($expression);
+ }
+
+ /**
+ * Lookup an alias
+ * @param string $alias alias
+ * @return array search results
+ * @since 1.5.2
+ */
+ function lookup($alias) {
+ /* Generate the dn and try to retrieve that single entry */
+ $cn = $this->quotevalue($alias);
+ $dn = 'cn=' . $cn . ',' . $this->basedn;
+
+ /* Do the search */
+ $result = $this->ldap_search($dn, true);
+ if (!is_array($result) || count($result) < 1)
+ return array();
+
+ return $result[0];
+ }
+
+ /**
+ * List all entries present in LDAP server
+ *
+ * maxrows setting might limit list of returned entries.
+ * Careful with this -- it could get quite large for big sites.
+ * @return array all entries in ldap server
+ */
+ function list_addr() {
+ if (! $this->listing)
+ return array();
+
+ /* set wide search expression */
+ $expression = '(cn=*)';
+
+ /* add filtering */
+ if ($this->filter!='')
+ $expression = '(&' . $this->filter . $expression .')';
+
+ /* use internal search function and return search results */
+ return $this->ldap_search($expression);
+ }
+
+ /**
+ * Add address
+ * @param array $userdata new data
+ * @return boolean
+ * @since 1.5.2
+ */
+ function add($userdata) {
+ if(!$this->writeable) {
+ return $this->set_error(_("Address book is read-only"));
+ }
+
+ /* Convert search from user's charset to the one used in ldap and sanitize */
+ $cn = $this->quotevalue($userdata['nickname']);
+ $dn = 'cn=' . $cn . ',' . trim($this->basedn);
+
+ /* See if user exists already */
+ $user = $this->ldap_search($dn, true);
+ if (!is_array($user)) {
+ return false;
+ } elseif (count($user) > 0) {
+ return $this->set_error(sprintf(_("User \"%s\" already exists"), $userdata['nickname']));
+ }
+
+ /* init variable */
+ $data = array();
+
+ /* Prepare data */
+ $data['cn'] = $cn;
+ $data['mail'] = $this->quotevalue($userdata['email']);
+ $data["objectclass"][0] = "top";
+ $data["objectclass"][1] = "person";
+ $data["objectclass"][2] = "organizationalPerson";
+ $data["objectclass"][3] = "inetOrgPerson";
+ /* sn is required in person object */
+ if(!empty($userdata['lastname'])) {
+ $data['sn'] = $this->quotevalue($userdata['lastname']);
+ } else {
+ $data['sn'] = ' ';
+ }
+ /* optional fields */
+ if(!empty($userdata['firstname']))
+ $data['givenName'] = $this->quotevalue($userdata['firstname']);
+ if(!empty($userdata['label'])) {
+ $data['description'] = $this->quotevalue($userdata['label']);
+ }
+ return $this->ldap_add($dn, $data);
+ }
+
+ /**
+ * Delete address
+ * @param array $aliases array of entries that have to be removed.
+ * @return boolean
+ * @since 1.5.2
+ */
+ function remove($aliases) {
+ if(!$this->writeable) {
+ return $this->set_error(_("Address book is read-only"));
+ }
+
+ foreach ($aliases as $alias) {
+ /* Convert nickname from user's charset and derive cn/dn */
+ $cn = $this->quotevalue($alias);
+ $dn = 'cn=' . $cn . ',' . $this->basedn;
+
+ if (!$this->ldap_remove($dn))
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Modify address
+ * @param string $alias modified alias
+ * @param array $userdata new data
+ * @return boolean
+ * @since 1.5.2
+ */
+ function modify($alias, $userdata) {
+ if(!$this->writeable) {
+ return $this->set_error(_("Address book is read-only"));
+ }
+
+ /* Convert search from user's charset to the one used in ldap and sanitize */
+ $sourcecn = $this->quotevalue($alias);
+ $sourcedn = 'cn=' . $sourcecn . ',' . trim($this->basedn);
+ $targetcn = $this->quotevalue($userdata['nickname']);
+ $targetdn = 'cn=' . $targetcn . ',' . trim($this->basedn);
+
+ /* Check that the dn to modify exists */
+ $sourceuser = $this->lookup($alias);
+ if (!is_array($sourceuser) || count($sourceuser) < 1)
+ return false;
+
+ /* Check if dn is going to change */
+ if ($alias != $userdata['nickname']) {
+
+ /* Check that the target dn doesn't exist */
+ $targetuser = $this->lookup($userdata['nickname']);
+ if (is_array($targetuser) && count($targetuser) > 0)
+ return $this->set_error(sprintf(_("User \"%s\" already exists"), $userdata['nickname']));
+
+ /* Rename from the source dn to target dn */
+ if (!$this->ldap_rename($sourcedn, 'cn=' . $targetcn, $this->basedn))
+ return $this->set_error(sprintf(_("Unable to rename user \"%s\" to \"%s\""), $alias, $userdata['nickname']));
+ }
+
+ // initial vars
+ $data = array();
+ $deleted_attribs = array();
+
+ /* Prepare data */
+ $data['cn'] = $this->quotevalue($targetcn);
+ $data['mail'] = $this->quotevalue($userdata['email']);
+ $data["objectclass"][0] = "top";
+ $data["objectclass"][1] = "person";
+ $data["objectclass"][2] = "organizationalPerson";
+ $data["objectclass"][3] = "inetOrgPerson";
+
+ if(!empty($userdata['firstname'])) {
+ $data['givenName'] = $this->quotevalue($userdata['firstname']);
+ } elseif (!empty($sourceuser['firstname'])) {
+ $deleted_attribs['givenName'] = $this->quotevalue($sourceuser['firstname']);
+ }
+
+ if(!empty($userdata['lastname'])) {
+ $data['sn'] = $this->quotevalue($userdata['lastname']);
+ } else {
+ // sn is required attribute in LDAP person object.
+ // SquirrelMail requires givenName or Surname
+ $data['sn'] = ' ';
+ }
+
+ if(!empty($userdata['label'])) {
+ $data['description'] = $this->quotevalue($userdata['label']);
+ } elseif (!empty($sourceuser['label'])) {
+ $deleted_attribs['description'] = $this->quotevalue($sourceuser['label']);
+ }
+
+ return $this->ldap_modify($targetdn, $data, $deleted_attribs);
+ }