diff options
Diffstat (limited to 'demos/chat')
-rw-r--r-- | demos/chat/index.php | 56 | ||||
-rw-r--r-- | demos/chat/protected/App_Code/ChatBufferRecord.php | 126 | ||||
-rw-r--r-- | demos/chat/protected/App_Code/ChatUserManager.php | 168 | ||||
-rw-r--r-- | demos/chat/protected/App_Code/ChatUserRecord.php | 80 | ||||
-rw-r--r-- | demos/chat/protected/pages/Home.php | 110 | ||||
-rw-r--r-- | demos/chat/protected/pages/Login.php | 78 |
6 files changed, 309 insertions, 309 deletions
diff --git a/demos/chat/index.php b/demos/chat/index.php index 17ed21d0..7a9eaf42 100644 --- a/demos/chat/index.php +++ b/demos/chat/index.php @@ -1,29 +1,29 @@ -<?php
-
-$frameworkPath='../../framework/prado.php';
-
-/** The directory checks may be removed if performance is required **/
-$basePath=dirname(__FILE__);
-$assetsPath=$basePath."/assets";
-$runtimePath=$basePath."/protected/runtime";
-
-$sqliteDbDir = $basePath."/protected/App_Code";
-$sqliteDb = $sqliteDbDir."/chat.db";
-
-if(!is_file($frameworkPath))
- die("Unable to find prado framework path $frameworkPath.");
-if(!is_writable($assetsPath))
- die("Please make sure that the directory $assetsPath is writable by Web server process.");
-if(!is_writable($runtimePath))
- die("Please make sure that the directory $runtimePath is writable by Web server process.");
-if(!is_writable($sqliteDbDir))
- die("Please make sure that the directory $sqliteDbDir is writable by Web server process.");
-if(!is_writable($sqliteDb))
- die("Please make sure that the sqlite file $sqliteDb is writable by Web server process.");
-
-require_once($frameworkPath);
-
-$application=new TApplication;
-$application->run();
-
+<?php + +$frameworkPath='../../framework/prado.php'; + +/** The directory checks may be removed if performance is required **/ +$basePath=dirname(__FILE__); +$assetsPath=$basePath."/assets"; +$runtimePath=$basePath."/protected/runtime"; + +$sqliteDbDir = $basePath."/protected/App_Code"; +$sqliteDb = $sqliteDbDir."/chat.db"; + +if(!is_file($frameworkPath)) + die("Unable to find prado framework path $frameworkPath."); +if(!is_writable($assetsPath)) + die("Please make sure that the directory $assetsPath is writable by Web server process."); +if(!is_writable($runtimePath)) + die("Please make sure that the directory $runtimePath is writable by Web server process."); +if(!is_writable($sqliteDbDir)) + die("Please make sure that the directory $sqliteDbDir is writable by Web server process."); +if(!is_writable($sqliteDb)) + die("Please make sure that the sqlite file $sqliteDb is writable by Web server process."); + +require_once($frameworkPath); + +$application=new TApplication; +$application->run(); + ?>
\ No newline at end of file diff --git a/demos/chat/protected/App_Code/ChatBufferRecord.php b/demos/chat/protected/App_Code/ChatBufferRecord.php index f4d53db0..5a714de2 100644 --- a/demos/chat/protected/App_Code/ChatBufferRecord.php +++ b/demos/chat/protected/App_Code/ChatBufferRecord.php @@ -1,64 +1,64 @@ -<?php
-
-class ChatBufferRecord extends TActiveRecord
-{
- const TABLE='chat_buffer';
-
- public $id;
- public $for_user;
- public $from_user;
- public $message;
- private $_created_on;
-
- public function getCreated_On()
- {
- if($this->_created_on === null)
- $this->_created_on = time();
- return $this->_created_on;
- }
-
- public function setCreated_On($value)
- {
- $this->_created_on = $value;
- }
-
- public static function finder($className=__CLASS__)
- {
- return parent::finder($className);
- }
-
- public function saveMessage()
- {
- foreach(ChatUserRecord::finder()->findAll() as $user)
- {
- $message = new self;
- $message->for_user = $user->username;
- $message->from_user = $this->from_user;
- $message->message = $this->message;
- $message->save();
- if($user->username == $this->from_user)
- {
- $user->last_activity = time(); //update the last activity;
- $user->save();
- }
- }
- }
-
- public function getUserMessages($user)
- {
- $content = '';
- foreach($this->findAll('for_user = ?', $user) as $message)
- $content .= $this->formatMessage($message);
- $this->deleteAll('for_user = ? OR created_on < ?', $user, time() - 300); //5 min inactivity
- return $content;
- }
-
- protected function formatMessage($message)
- {
- $user = htmlspecialchars($message->from_user);
- $content = htmlspecialchars($message->message);
- return "<div class=\"message\"><strong>{$user}:</strong> <span>{$content}</span></div>";
- }
-}
-
+<?php + +class ChatBufferRecord extends TActiveRecord +{ + const TABLE='chat_buffer'; + + public $id; + public $for_user; + public $from_user; + public $message; + private $_created_on; + + public function getCreated_On() + { + if($this->_created_on === null) + $this->_created_on = time(); + return $this->_created_on; + } + + public function setCreated_On($value) + { + $this->_created_on = $value; + } + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } + + public function saveMessage() + { + foreach(ChatUserRecord::finder()->findAll() as $user) + { + $message = new self; + $message->for_user = $user->username; + $message->from_user = $this->from_user; + $message->message = $this->message; + $message->save(); + if($user->username == $this->from_user) + { + $user->last_activity = time(); //update the last activity; + $user->save(); + } + } + } + + public function getUserMessages($user) + { + $content = ''; + foreach($this->findAll('for_user = ?', $user) as $message) + $content .= $this->formatMessage($message); + $this->deleteAll('for_user = ? OR created_on < ?', $user, time() - 300); //5 min inactivity + return $content; + } + + protected function formatMessage($message) + { + $user = htmlspecialchars($message->from_user); + $content = htmlspecialchars($message->message); + return "<div class=\"message\"><strong>{$user}:</strong> <span>{$content}</span></div>"; + } +} + ?>
\ No newline at end of file diff --git a/demos/chat/protected/App_Code/ChatUserManager.php b/demos/chat/protected/App_Code/ChatUserManager.php index f28ee362..d0f242fd 100644 --- a/demos/chat/protected/App_Code/ChatUserManager.php +++ b/demos/chat/protected/App_Code/ChatUserManager.php @@ -1,85 +1,85 @@ -<?php
-
-class ChatUserManager extends TModule implements IUserManager
-{
- /**
- * @return string name for a guest user.
- */
- public function getGuestName()
- {
- return 'Guest';
- }
-
- /**
- * Returns a user instance given the user name.
- * @param string user name, null if it is a guest.
- * @return TUser the user instance
- */
- public function getUser($username=null)
- {
- $user=new TUser($this);
- $user->setIsGuest(true);
- if($username !== null)
- {
- $user->setIsGuest(false);
- $user->setName($username);
- $user->setRoles(array('normal'));
- }
- return $user;
- }
-
- /**
- * Add a new user to the database.
- * @param string username.
- */
- public function addNewUser($username)
- {
- $user = new ChatUserRecord();
- $user->username = $username;
- $user->save();
- }
-
- /**
- * @return boolean true if username already exists, false otherwise.
- */
- public function usernameExists($username)
- {
- return ChatUserRecord::finder()->findByUsername($username) instanceof ChatUserRecord;
- }
-
- /**
- * Validates if the username exists.
- * @param string user name
- * @param string password
- * @return boolean true if validation is successful, false otherwise.
- */
- public function validateUser($username,$password)
- {
- return $this->usernameExists($username);
- }
-
- /**
- * Saves user auth data into a cookie.
- * @param THttpCookie the cookie to receive the user auth data.
- * @since 3.1.1
- */
- public function saveUserToCookie($cookie)
- {
- // do nothing since we don't support cookie-based auth in this example
- }
-
- /**
- * Returns a user instance according to auth data stored in a cookie.
- * @param THttpCookie the cookie storing user authentication information
- * @return TUser the user instance generated based on the cookie auth data, null if the cookie does not have valid auth data.
- * @since 3.1.1
- */
- public function getUserFromCookie($cookie)
- {
- // do nothing since we don't support cookie-based auth in this example
- return null;
- }
-}
-
-
+<?php + +class ChatUserManager extends TModule implements IUserManager +{ + /** + * @return string name for a guest user. + */ + public function getGuestName() + { + return 'Guest'; + } + + /** + * Returns a user instance given the user name. + * @param string user name, null if it is a guest. + * @return TUser the user instance + */ + public function getUser($username=null) + { + $user=new TUser($this); + $user->setIsGuest(true); + if($username !== null) + { + $user->setIsGuest(false); + $user->setName($username); + $user->setRoles(array('normal')); + } + return $user; + } + + /** + * Add a new user to the database. + * @param string username. + */ + public function addNewUser($username) + { + $user = new ChatUserRecord(); + $user->username = $username; + $user->save(); + } + + /** + * @return boolean true if username already exists, false otherwise. + */ + public function usernameExists($username) + { + return ChatUserRecord::finder()->findByUsername($username) instanceof ChatUserRecord; + } + + /** + * Validates if the username exists. + * @param string user name + * @param string password + * @return boolean true if validation is successful, false otherwise. + */ + public function validateUser($username,$password) + { + return $this->usernameExists($username); + } + + /** + * Saves user auth data into a cookie. + * @param THttpCookie the cookie to receive the user auth data. + * @since 3.1.1 + */ + public function saveUserToCookie($cookie) + { + // do nothing since we don't support cookie-based auth in this example + } + + /** + * Returns a user instance according to auth data stored in a cookie. + * @param THttpCookie the cookie storing user authentication information + * @return TUser the user instance generated based on the cookie auth data, null if the cookie does not have valid auth data. + * @since 3.1.1 + */ + public function getUserFromCookie($cookie) + { + // do nothing since we don't support cookie-based auth in this example + return null; + } +} + + ?>
\ No newline at end of file diff --git a/demos/chat/protected/App_Code/ChatUserRecord.php b/demos/chat/protected/App_Code/ChatUserRecord.php index b68fbd4d..ee45cf84 100644 --- a/demos/chat/protected/App_Code/ChatUserRecord.php +++ b/demos/chat/protected/App_Code/ChatUserRecord.php @@ -1,41 +1,41 @@ -<?php
-
-class ChatUserRecord extends TActiveRecord
-{
- const TABLE='chat_users';
-
- public $username;
- private $_last_activity;
-
- public function getLast_Activity()
- {
- if($this->_last_activity === null)
- $this->_last_activity = time();
- return $this->_last_activity;
- }
-
- public function setLast_Activity($value)
- {
- $this->_last_activity = $value;
- }
-
- public static function finder($className=__CLASS__)
- {
- return parent::finder($className);
- }
-
- public function getUserList()
- {
- $this->deleteAll('last_activity < ?', time()-300); //5 min inactivity
- $content = '<ul>';
- foreach($this->findAll() as $user)
- {
- $content .= '<li>'.htmlspecialchars($user->username).'</li>';
- }
- $content .= '</ul>';
-
- return $content;
- }
-}
-
+<?php + +class ChatUserRecord extends TActiveRecord +{ + const TABLE='chat_users'; + + public $username; + private $_last_activity; + + public function getLast_Activity() + { + if($this->_last_activity === null) + $this->_last_activity = time(); + return $this->_last_activity; + } + + public function setLast_Activity($value) + { + $this->_last_activity = $value; + } + + public static function finder($className=__CLASS__) + { + return parent::finder($className); + } + + public function getUserList() + { + $this->deleteAll('last_activity < ?', time()-300); //5 min inactivity + $content = '<ul>'; + foreach($this->findAll() as $user) + { + $content .= '<li>'.htmlspecialchars($user->username).'</li>'; + } + $content .= '</ul>'; + + return $content; + } +} + ?>
\ No newline at end of file diff --git a/demos/chat/protected/pages/Home.php b/demos/chat/protected/pages/Home.php index 65cb22dc..bdcb2d3b 100644 --- a/demos/chat/protected/pages/Home.php +++ b/demos/chat/protected/pages/Home.php @@ -1,56 +1,56 @@ -<?php
-
-class Home extends TPage
-{
- public function onLoad($param)
- {
- $username = $this->Application->User->Name;
- if(!$this->Application->Modules['users']->usernameExists($username))
- {
- $auth = $this->Application->Modules['auth'];
- $auth->logout();
-
- //redirect to login page.
- $this->Response->Redirect($this->Service->ConstructUrl($auth->LoginPage));
- }
- }
-
- function processMessage($sender, $param)
- {
- if(strlen($this->userinput->Text) > 0)
- {
- $record = new ChatBufferRecord();
- $record->message = $this->userinput->Text;
- $record->from_user = $this->Application->User->Name;
- $record->saveMessage();
- $this->userinput->Text = '';
- $this->refresh($sender, $param);
- $this->CallbackClient->focus($this->userinput);
- }
- }
-
- function refresh($sender, $param)
- {
- //refresh the message list
- $content = ChatBufferRecord::finder()->getUserMessages($this->Application->User->Name);
- if(strlen($content) > 0)
- {
- $client = $this->Page->CallbackClient;
- $anchor = (string)time();
- $content .= "<a href=\"#\" id=\"{$anchor}\"> </a>";
- $client->appendContent("messages", $content);
- $client->focus($anchor);
- }
-
- //refresh the user list
- $lastUpdate = $this->getViewState('userList','');
- $users = ChatUserRecord::finder()->getUserList();
- if($lastUpdate != $users)
- {
- $this->Page->CallbackClient->update('users', $users);
- $this->setViewstate('userList', $users);
- }
- }
-}
-
+<?php + +class Home extends TPage +{ + public function onLoad($param) + { + $username = $this->Application->User->Name; + if(!$this->Application->Modules['users']->usernameExists($username)) + { + $auth = $this->Application->Modules['auth']; + $auth->logout(); + + //redirect to login page. + $this->Response->Redirect($this->Service->ConstructUrl($auth->LoginPage)); + } + } + + function processMessage($sender, $param) + { + if(strlen($this->userinput->Text) > 0) + { + $record = new ChatBufferRecord(); + $record->message = $this->userinput->Text; + $record->from_user = $this->Application->User->Name; + $record->saveMessage(); + $this->userinput->Text = ''; + $this->refresh($sender, $param); + $this->CallbackClient->focus($this->userinput); + } + } + + function refresh($sender, $param) + { + //refresh the message list + $content = ChatBufferRecord::finder()->getUserMessages($this->Application->User->Name); + if(strlen($content) > 0) + { + $client = $this->Page->CallbackClient; + $anchor = (string)time(); + $content .= "<a href=\"#\" id=\"{$anchor}\"> </a>"; + $client->appendContent("messages", $content); + $client->focus($anchor); + } + + //refresh the user list + $lastUpdate = $this->getViewState('userList',''); + $users = ChatUserRecord::finder()->getUserList(); + if($lastUpdate != $users) + { + $this->Page->CallbackClient->update('users', $users); + $this->setViewstate('userList', $users); + } + } +} + ?>
\ No newline at end of file diff --git a/demos/chat/protected/pages/Login.php b/demos/chat/protected/pages/Login.php index 831ab925..ec39f1f0 100644 --- a/demos/chat/protected/pages/Login.php +++ b/demos/chat/protected/pages/Login.php @@ -1,40 +1,40 @@ -<?php
-
-class Login extends TPage
-{
- /**
- * Check that the username is not already taken.
- * @param TControl custom validator that created the event.
- * @param TServerValidateEventParameter validation parameters.
- */
- function checkUsername($sender, $param)
- {
- $manager = $this->Application->Modules['users'];
- if($manager->usernameExists($this->username->Text))
- $param->IsValid = false;
- }
-
- /**
- * Create and login a new user, then redirect to the requested page.
- * @param TControl button control that created the event.
- * @param TEventParameter event parameters.
- */
- function createNewUser($sender, $param)
- {
- if($this->Page->IsValid)
- {
- $manager = $this->Application->Modules['users'];
- $manager->addNewUser($this->username->Text);
-
- //do manual login
- $user = $manager->getUser($this->username->Text);
- $auth = $this->Application->Modules['auth'];
- $auth->updateSessionUser($user);
- $this->Application->User = $user;
-
- $this->Response->redirect($auth->ReturnUrl);
- }
- }
-}
-
+<?php + +class Login extends TPage +{ + /** + * Check that the username is not already taken. + * @param TControl custom validator that created the event. + * @param TServerValidateEventParameter validation parameters. + */ + function checkUsername($sender, $param) + { + $manager = $this->Application->Modules['users']; + if($manager->usernameExists($this->username->Text)) + $param->IsValid = false; + } + + /** + * Create and login a new user, then redirect to the requested page. + * @param TControl button control that created the event. + * @param TEventParameter event parameters. + */ + function createNewUser($sender, $param) + { + if($this->Page->IsValid) + { + $manager = $this->Application->Modules['users']; + $manager->addNewUser($this->username->Text); + + //do manual login + $user = $manager->getUser($this->username->Text); + $auth = $this->Application->Modules['auth']; + $auth->updateSessionUser($user); + $this->Application->User = $user; + + $this->Response->redirect($auth->ReturnUrl); + } + } +} + ?>
\ No newline at end of file |