Esempio n. 1
0
            return $o != $observer;
        });
    }
    public function notify()
    {
        return array_walk($this->observers, function ($observer) {
            $observer->handle();
        });
    }
}
class SendWelcomeEmail implements Observer
{
    public function handle()
    {
        var_dump('send a welcome email.');
    }
}
class UpdateAccumulateScore implements Observer
{
    public function handle()
    {
        var_dump('update the accumulate score.');
    }
}
$login = new Login();
$sendWelcomeEmail = new SendWelcomeEmail();
$login->attach($sendWelcomeEmail);
$login->attach(new UpdateAccumulateScore());
$login->notify();
$login->detach($sendWelcomeEmail);
$login->notify();
Esempio n. 2
0
 public function tryToAuthenticateUser()
 {
     $username = isset($_REQUEST["username"]) ? trim($_REQUEST["username"]) : "";
     $password = isset($_REQUEST["password"]) ? $_REQUEST["password"] : "";
     if (empty($username)) {
         $this->getError()->setError("Username cannot be empty.");
     } else {
         if (empty($password)) {
             $this->getError()->setError("Password cannot be empty.");
         } else {
             $params = array("username" => $username, "password" => $password, "action" => "login");
             ob_start();
             // send the request
             CURLHandler::Post(SERVER_URL . 'loginApi.php', $params, false, true);
             $result = ob_get_contents();
             ob_end_clean();
             $ret = json_decode($result);
             if ($ret->error == 1) {
                 $this->getError()->setError($ret->message);
                 return $this->getError()->getErrorFlag();
             } else {
                 $id = $ret->userid;
                 $username = $ret->username;
                 $nickname = $ret->nickname;
                 $_SESSION["userid"] = $id;
                 $_SESSION["username"] = $username;
                 $_SESSION["nickname"] = $nickname;
                 // notifying other applications
                 $response = new Response();
                 $login = new Login();
                 $login->setResponse($response);
                 $login->notify($id, session_id());
                 return false;
             }
         }
     }
     return $this->getError()->getErrorFlag();
 }
Esempio n. 3
0
 public function tryToAuthenticateUser()
 {
     $username = isset($_REQUEST["username"]) ? trim($_REQUEST["username"]) : "";
     $password = isset($_REQUEST["password"]) ? $_REQUEST["password"] : "";
     if (empty($username)) {
         $this->getError()->setError("Username cannot be empty.");
     } else {
         if (empty($password)) {
             $this->getError()->setError("Password cannot be empty.");
         } else {
             $params = array("username" => $username, "password" => $password, "action" => "login");
             ob_start();
             // send the request
             CURLHandler::Post(SERVER_URL . 'loginApi.php', $params, false, true);
             $result = ob_get_contents();
             ob_end_clean();
             $ret = json_decode($result);
             if ($ret->error == 1) {
                 if (($key = array_search('User is deactivated.', $ret->message)) !== false) {
                     $ret->message[$key] = 'You need to be confirmed!<br /><a href="#" id="ping_admin">Ping the administrator ...</a>';
                 }
                 $this->getError()->setError($ret->message);
                 return $this->getError()->getErrorFlag();
             } else {
                 $id = $ret->userid;
                 $username = $ret->username;
                 $nickname = $ret->nickname;
                 $admin = $ret->admin;
                 Utils::setUserSession($id, $username, $nickname, $admin);
                 // notifying other applications
                 $response = new Response();
                 $login = new Login();
                 $login->setResponse($response);
                 $login->notify($id, session_id());
                 return false;
             }
         }
     }
     return $this->getError()->getErrorFlag();
 }