コード例 #1
0
ファイル: UseradminController.php プロジェクト: jaybill/Bolts
 function testdataAction()
 {
     $request = new Bolts_Request($this->getRequest());
     if ($this->getRequest()->isPost()) {
         $errors = array();
         $data_path = $request->data_path;
         $data_file = $data_path . "/users.dat";
         $image_dir = $data_path . "/images";
         $users_table = new Users();
         $users_roles_table = new UsersRoles();
         if ($request->has("email_domain")) {
             $email_domain = $request->email_domain;
         } else {
             $email_domain = "nowhere.com";
         }
         if (!file_exists($data_file)) {
             $errors[] = $this->_T("Data file missing. Check path.");
         } else {
             $users = unserialize(file_get_contents($data_file));
             if (!is_array($users)) {
                 $errors[] = $this->_T("Data file is corrupt or something.");
             }
         }
         if (count($errors) == 0) {
             $old_users = $users_table->fetchAll();
             foreach ($old_users as $old_user) {
                 if ($users_table->getMetaData($old_user->username, "is_test_user") == "true") {
                     $where = $users_table->getAdapter()->quoteInto("username = ?", $old_user->username);
                     $users_table->delete($where);
                     $users_roles_table->delete($where);
                 }
             }
             $count = 0;
             foreach ($users as $user) {
                 $tmp_user = array();
                 foreach ($user as $key => $value) {
                     if ($key != "avatar") {
                         $tmp_user[$key] = $value;
                     }
                 }
                 $tmp_user['email'] = strtolower($tmp_user['username'] . "@" . $email_domain);
                 $tmp_user['password'] = "******";
                 $destination_path = $users_table->getAvatarPath($user['username']);
                 $destination_filename = $users_table->getAvatarPath($user['username'], true);
                 if (!is_dir($destination_path)) {
                     mkdir($destination_path, 0777, true);
                 }
                 if (file_exists($destination_filename)) {
                     unlink($destination_filename);
                 }
                 $source_image = $image_dir . "/" . $user['avatar'];
                 copy($source_image, $destination_filename);
                 $role_data = array("username" => $tmp_user['username'], "role_id" => $tmp_user['role_id']);
                 $users_roles_table->insert($role_data);
                 unset($tmp_user['role_id']);
                 $users_table->insert($tmp_user);
                 $users_table->setMetaData($tmp_user['username'], "is_test_user", "true");
                 $save_users[] = $user;
                 $count++;
             }
             $this->view->success = "User data loaded. Created " . $count . " users.";
             Bolts_Registry::set('test_data_path', $request->data_path);
             $this->view->data_path = Bolts_Registry::get('test_data_path');
             $this->view->email_domain = $email_domain;
         } else {
             $this->view->errors = $errors;
             $this->view->data_path = Zend_Registry::get('basepath') . "/tmp/testdata";
             $this->view->email_domain = $request->email_domain;
         }
     } else {
         $this->view->data_path = Zend_Registry::get('basepath') . "/tmp/testdata";
         $this->view->email_domain = "nowhere.com";
         $this->view->notice = $this->_T("Warning: If you are reinstalling the test data, the old test data will be overwritten. Users created outside the test data should not be affected.");
     }
 }
コード例 #2
0
ファイル: UserController.php プロジェクト: richjoslin/rivety
	function registerAction()
	{
		$errors = array();
		$request = new RivetyCore_Request($this->getRequest());

		if ($this->_auth->hasIdentity()) $this->_redirect('/default/user/profile/username/' . $this->_identity->username);
		$users_table = new Users();
		$user = array();

		$pre_register_params = array();

		if ($request->has('url'))
		{
			$this->view->url_param = $request->url;
			$pre_register_params['return_url'] = $request->url;
		}
		else
		{
			$pre_register_params['return_url'] = false;
		}

		$pre_register_params = $this->_rivety_plugin->doFilter('default_pre_register', $pre_register_params); // FILTER HOOK
		foreach ($pre_register_params as $key=>$value)
		{
			if ($key == 'return_url') $this->view->url_param = $value;
			else $this->view->$key = $value;
		}

		if ($this->getRequest()->isPost())
		{
			$request->addValidator('username', 'Username is required.');
			$request->addValidator('email', 'Email address is required.');
			$request->addValidator('password', 'Password is required.');
			$request->addValidator('confirm', 'Password confirmation is required.');
			if (!$request->isValid()) $errors = array_merge($errors, $request->getValidationErrors());
			if (count($errors) == 0)
			{
				$user['username'] = $request->username;
				// if ($request->has('full_name'))
				// {
				// 	if (strlen($request->full_name) < 1) $user['full_name'] = $this->_T("Anonymous");
				// 	else $user['full_name'] = $request->full_name;
				// }
				// else
				// {
				// 	$user['full_name'] = $this->_T("Anonymous");
				// }
				$user['email'] = $request->email;
				$user['password'] = $request->password;
				$user['confirm'] = $request->confirm;

				// TODO: remove anything relating to birthday

				// if ($request->has('Birthday_Day') && $request->has('Birthday_Month') && $request->has('Birthday_Year'))
				// {
				// 	$user['birthday'] = strtotime($request->Birthday_Day ." ". $request->Birthday_Month ." ". $request->Birthday_Year);
				// }
				// else
				// {
				// 	$user['birthday'] = null;
				// }

				// validate username
				$username_validator = new Zend_Validate();
				$username_validator->addValidator(new Zend_Validate_StringLength(1, RivetyCore_Registry::get('username_length')));
				$username_validator->addValidator(new Zend_Validate_Alnum());

				if (!$username_validator->isValid($user['username']))
				{
					$show_username = "******".$user['username']."'";
					if (trim($user['username']) == "") $show_username = "******".$this->_T("empty")."]";
					$errors[] = $this->_T("%s is not a valid username. (Between %d and %d characters, only letters and numbers)",array($show_username,1,RivetyCore_Registry::get('username_length')));
					$this->screenAlert('error', $this->_T("%s is not a valid username. (Between %d and %d characters, only letters and numbers)",array($show_username,1,RivetyCore_Registry::get('username_length'))));
				}

				$user_where = $users_table->getAdapter()->quoteInto('username = ?', $user['username']);
				if ($users_table->getCountByWhereClause($user_where) > 0)
				{
					$errors[] = $this->_T("The username '%s' is already in use",$user['username']);
					$this->screenAlert('error', $this->_T("The username '%s' is already in use",$user['username']));
				}

				// validate email
				$email_validator = new Zend_Validate_EmailAddress();
				if (!$email_validator->isValid($user['email']))
				{
					$show_email = "'" . $user['email']."'";
					if (trim($user['email']) == "") $show_email = "[" . $this->_T("empty") . "]";
					$errors[] = $show_email . ' ' . $this->_T('is not a valid email.');
					$this->screenAlert('error', $show_email . ' ' . $this->_T('is not a valid email.'));
				}

				// make sure no one is using this email already
				$email_where = $users_table->getAdapter()->quoteInto('email = ?',$user['email']);
				if ($users_table->getCountByWhereClause($email_where) > 0)
				{
					$errors[] = $this->_T("Email is already in use.");
					$this->screenAlert('error', 'This email address is already in use.');
				}

				$password_validator = new Zend_Validate();
				$password_validator->addValidator(new Zend_Validate_StringLength(6, 32));
				// make sure password is at least six chars
				if (!$password_validator->isValid($user['password']))
				{
					$errors[] = $this->_T("Password must be between %d and %d characters", array(6, RivetyCore_Registry::get('password_length')));
					$this->screenAlert('error', $this->_T("Password must be between %d and %d characters", array(6, RivetyCore_Registry::get('password_length'))));
				}
				// if password is set, make sure it matches confirm
				if ($user['password'] != $user['confirm'])
				{
					$errors[] = $this->_T("Passwords don't match");
					$this->screenAlert('error', $this->_T("Passwords don't match"));
				}

				// // do we meet the minimum age?
				// $minimum_age = RivetyCore_Registry::get('minimum_registration_age', '13') ;
				// $years_ago = strtotime($minimum_age . ' years ago');
				// if ($user['birthday'] > $years_ago)
				// {
				// 	$errors[] = $this->_T("You must be at least %d years old to register.", $minimum_age);
				// }

				$params = array(
					'request' => $this->getRequest(),
					'user' => $user,
					'errors' => $errors,
				);
				$additional = $this->_rivety_plugin->doFilter($this->_mca, $params); // FILTER HOOK
				$errors = $additional['errors'];
				$user = $additional['user'];

				// convert birthday_ts to mysql date
				// $birthday_db = date(DB_DATETIME_FORMAT, $user['birthday']);
				if (count($errors) == 0)
				{
					$roles_table = new Roles();
					$users_roles_table = new UsersRoles();
					$default_role_shortname = RivetyCore_Registry::get('default_role_shortname');
					$role_data = array("username" => $user['username'], "role_id" => $roles_table->getIdByShortname($default_role_shortname));
					$users_roles_table->insert($role_data);

					$user_data = array(
						'username' => $user['username'],
						'email' => $user['email'],
						// 'full_name' => $user['full_name'],
						// 'birthday' => $birthday_db,
						'password' => $user['password'],
						'created_on' => date("Y-m-d H:i:s"),
						'ip' => getenv('REMOTE_ADDR'),
					);

					// if (array_key_exists('about_me', $additional['user']))
					// {
					// 	$user_data['about_me'] = $additional['user']['about_me'];
					// }

					// MAKE IT OFFICIAL
					$users_table->insert($user_data);

					// DO SOME PLUGINS
					$params = array(
						'user' => $user_data,
						'request' => $request,
						'username' => $user['username'],
						'autologin' => true,
						'autologin_username' => $user['username'],
						'autologin_password' => $user['password'],
						'autologin_password_hash' => md5($user['password']),
						'locale_code' => $this->locale_code,
					);
					$params = $this->_rivety_plugin->doFilter("default_post_register", $params); // FILTER HOOK
					$this->_rivety_plugin->doAction($this->_mca . "_post_register", $params); // ACTION HOOK (deprecated)

					// SET UP AUTO-LOGIN, OR DON'T
					if ($params['autologin'])
					{
						$appNamespace = new Zend_Session_Namespace('RivetyCore_Temp');
						$appNamespace->autoLogin = $params['autologin'];
						$appNamespace->autoLoginUsername = $params['autologin_username'];
						$appNamespace->autoLoginPassword = $params['autologin_password'];
						$appNamespace->autoLoginPasswordHash = $params['autologin_password_hash'];
					}

					// SEND THE USER ON THEIR WAY
					$url = '/default/user/postregister';
					// if there was a URL passed in then add that encoded URL as a param to the default redirect
					if ($request->has('url')) $url .= '/url/' . $request->url;
					$this->_redirect($url);
				}
			}
		}
		$this->view->user = $user;
		$this->view->pagetitle = $this->_T("Register");

		foreach ($errors as $error)
		{
			$this->screenAlert('error', $error);
		}
		$errors = null;

		switch ($this->format)
		{
			case 'json': die(!empty($this->screen_alerts) ? json_encode(array('messages' => $this->screen_alerts)) : '200 OK');
			default: break;
		}
	}