Example #1
0
 public function updated(Am_Event $event)
 {
     $user = $event->getUser();
     $oldUser = $event instanceof Am_Event_AbstractUserUpdate ? $event->getOldUser() : $event->getUser();
     $oldLogin = $oldUser->login;
     $newLogin = $event->getUser()->login;
     if (!file_exists($this->htpasswd)) {
         $f = fopen($this->htpasswd, 'x');
         if (!$f) {
             throw new Am_Exception_InternalError("Could not open file {$this->htpasswd} for creation");
         }
         $this->writeLine($f, $user);
         fclose($f);
     } else {
         $f = fopen($this->htpasswd, 'r');
         if (!$f) {
             throw new Am_Exception_InternalError("Could not open file {$this->htpasswd} for reading");
         }
         if (!flock($f, LOCK_EX)) {
             throw new Am_Exception_InternalError("Could not lock htpasswd file {$this->htpasswd} for updating");
         }
         $newFn = $this->htpasswd . '.' . uniqid();
         $fNew = fopen($newFn, 'x');
         if (!$fNew) {
             throw new Am_Exception_InternalError("Could not open file {$newFn} for creation");
         }
         $found = 0;
         while ($s = fgets($f, 8192)) {
             @(list($l, $p) = explode(':', $s));
             if (trim($l) != $oldLogin) {
                 fwrite($fNew, $s);
             } else {
                 $this->writeLine($fNew, $event->getUser(), $p);
                 $found++;
             }
         }
         if (!$found) {
             $this->writeLine($fNew, $event->getUser());
         }
         flock($f, LOCK_UN);
         fclose($f);
         fclose($fNew);
         if (!rename($newFn, $this->htpasswd)) {
             throw new Am_Exception_InternalError("Could not rename [{$newFn}] to {$this->htpasswd}");
         }
     }
     // now update htgroup
     $folders = $this->getDi()->resourceAccessTable->getAllowedResources($event->getUser(), ResourceAccess::FOLDER);
     foreach ($folders as $i => $folder) {
         $folders[$i] = $folder->pk();
     }
     if (!file_exists($this->htgroup)) {
         $f = fopen($this->htgroup, 'x');
         foreach ($folders as $id) {
             fwrite($f, "FOLDER_{$id}: {$newLogin}" . PHP_EOL);
         }
         fclose($f);
     } else {
         $f = fopen($this->htgroup, 'r');
         if (!$f) {
             throw new Am_Exception_InternalError("Could not open file {$this->htgroup} for reading");
         }
         if (!flock($f, LOCK_EX)) {
             throw new Am_Exception_InternalError("Could not lock htpasswd file {$this->htgroup} for updating");
         }
         $newFn = $this->htgroup . '.' . uniqid();
         $fNew = fopen($newFn, 'x');
         if (!$fNew) {
             throw new Am_Exception_InternalError("Could not open file {$newFn} for creation");
         }
         ///
         while ($s = fgets($f)) {
             if (!($colon = strpos(':', $s))) {
                 fwrite($fNew, $s);
                 continue;
             }
             $group = trim(substr($s, 0, $colon - 1));
             if (!preg_match('/^FOLDER_(\\d+)/', $group, $matches)) {
                 fwrite($fNew, $s);
                 continue;
             }
             $folder_id = intval($matches[1]);
             $records = preg_split('/\\s+/', trim(substr($s, $colon + 1)));
             $records = array_diff($records, array($oldLogin));
             if (in_array($folder_id, $folders)) {
                 $records[] = $newLogin;
             }
             fwrite($fNew, "FOLDER_{$folder_id}:" . implode(" ", $records) . PHP_EOL);
             $folders = array_diff($folders, array($folder_id));
         }
         foreach ($folders as $folder_id) {
             fwrite($fNew, "FOLDER_{$folder_id}:{$newLogin}" . PHP_EOL);
         }
         ///
         flock($f, LOCK_UN);
         fclose($fNew);
         fclose($f);
         if (!rename($newFn, $this->htgroup)) {
             throw new Am_Exception_InternalError("Could not rename [{$newFn}] to {$this->htgroup}");
         }
     }
 }
Example #2
0
 public function triggerEvent(Am_Event $e)
 {
     $oldUser = $e->getOldUser();
     $user = $e->getUser();
     if ($oldUser->unsubscribed != $user->unsubscribed) {
         Am_Di::getInstance()->hook->call(Am_Event::USER_UNSUBSCRIBED_CHANGED, array('user' => $user, 'unsubscribed' => $user->unsubscribed));
     }
 }