+
+ /**
+ * 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);
+ }