00001 <?php 00026 class UserGuestVerifierFilter extends BaseFilter implements Filterable { 00032 protected function __construct () { 00033 // Call parent constructor 00034 parent::__construct(__CLASS__); 00035 } 00036 00042 public final static function createUserGuestVerifierFilter () { 00043 // Get a new instance 00044 $filterInstance = new UserGuestVerifierFilter(); 00045 00046 // Return the instance 00047 return $filterInstance; 00048 } 00049 00057 public function execute (Requestable $requestInstance, Responseable $responseInstance) { 00058 // Get username from request 00059 $userName = $requestInstance->getRequestElement('user'); 00060 00061 // Is the username set? 00062 if (is_null($userName)) { 00063 // Not found in form so stop the filtering process 00064 $requestInstance->requestIsValid(false); 00065 00066 // Add a message to the response 00067 $responseInstance->addFatalMessage('username_guest_unset'); 00068 00069 // Abort here 00070 return false; 00071 } elseif (empty($userName)) { 00072 // Empty field! 00073 $requestInstance->requestIsValid(false); 00074 00075 // Add a message to the response 00076 $responseInstance->addFatalMessage('username_guest_empty'); 00077 00078 // Abort here 00079 return false; 00080 } elseif ($this->ifUserGuestIsTaken($userName) === false) { 00081 // Username is already taken 00082 $requestInstance->requestIsValid(false); 00083 00084 // Add a message to the response 00085 $responseInstance->addFatalMessage('username_guest_not_found'); 00086 00087 // Abort here 00088 return false; 00089 } 00090 00091 // Set the element for compatiblity reasons 00092 $requestInstance->setRequestElement('username', $userName); 00093 } 00094 00101 private function ifUserGuestIsTaken ($userName) { 00102 // Default is already taken 00103 $alreadyTaken = true; 00104 00105 // Initialize instance 00106 $userInstance = null; 00107 00108 // Get a registry instance 00109 $registry = Registry::getRegistry(); 00110 00111 // Is the user already there? 00112 if ($registry->instanceExists('user')) { 00113 // Use the instance for checking for the email 00114 $userInstance = $registry->getInstance('user'); 00115 $userInstance->setUserGuest($userName); 00116 } else { 00117 // If this instance is created then the username *does* exist 00118 try { 00119 // Get a new instance 00120 $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('guest_class'), 'createGuestByUsername'), array($userName)); 00121 00122 // Remember this user instance in our registry for later usage 00123 $registry->addInstance('user', $userInstance); 00124 } catch (UsernameMissingException $e) { 00125 // User was not found 00126 } 00127 } 00128 00129 // Does the username exist? 00130 if ((is_null($userInstance)) || ($userInstance->ifUsernameExists() === false)) { 00131 // This username is still available 00132 $alreadyTaken = false; 00133 } 00134 00135 // Return the result 00136 return $alreadyTaken; 00137 } 00138 } 00139 00140 // [EOF] 00141 ?>
1.5.6