<commit_before><?php
/**
 * User Application - Admin Controller - /apps/user/admin/main.php
 */

defined('IN_WITY') or die('Access denied');

/**
 * UserAdminController is the Admin Controller of the User Application
 * 
 * @package Apps
 * @author <NAME> <<EMAIL>>
 * @version 0.3-26-04-2013
 */
class UserAdminController extends WController {
	/**
	 * @var Instance of UserAdminModel
	 */
	private $model;
	
	public function __construct() {
		include_once 'model.php';
		$this->model = new UserAdminModel();
		
		include_once 'view.php';
		$this->setView(new UserAdminView($this->model));
	}
	
	/**
	 * List action handler
	 * Displays a list of users in the database
	 */
	protected function listing() {
		// Admin check
		$admin_check = WRequest::get('admin_check');
		if (!empty($admin_check)) {
			$notify = WRequest::get('notify');
			foreach ($admin_check as $user_id => $action) {
				$db_data = $this->model->getUser($user_id);
				if (isset($db_data['valid']) && $db_data['valid'] == 2) {
					if ($action == 'validate') {
						$this->model->updateUser($user_id, array('valid' => 1));
						// Send email notification
						if ($notify) {
							$this->model->sendEmail(
								$db_data['email'],
								WLang::get('user_account_validated_subject', WConfig::get('config.site_name')),
								str_replace(
									array('{site_name}', '{base}'),
									array(WConfig::get('config.site_name'), WRoute::getBase()),
									WLang::get('user_account_validated_email')
								)
							);
						}
						WNote::success('user_account_validated', WLang::get('user_account_validated', $db_data['nickname']));
					} else if ($action == 'refuse') {
						$config = $this->model->getConfig();
						if ($config['keep_users']) {
							$this->model->updateUser($user_id, array('valid' => 0));
						} else {
							$this->model->deleteUser($user_id);
						}
						// Send email notification
						if ($notify) {
							$this->model->sendEmail(
								$db_data['email'],
								WLang::get('user_account_refused_subject', WConfig::get('config.site_name')),
								str_replace(
									array('{site_name}', '{base}'),
									array(WConfig::get('config.site_name'), WRoute::getBase()),
									WLang::get('user_account_refused_email')
								)
							);
						}
						WNote::success('user_account_refused', WLang::get('user_account_refused', $db_data['nickname']));
					}
				} else {
					WNote::success('user_account_invalid', WLang::get('user_account_invalid', $db_data['nickname']));
				}
			}
		}
		
		// Sorting criterias given by URL
		$criterias = $this->getOption(0);
		$count = sscanf(str_replace('-', ' ', $criterias), '%s %s %d', $sortBy, $sens, $page);
		if (empty($page) || $page <= 0) {
			$page = 1;
		}
		
		// Filters
		$filters = WRequest::getAssoc(array('nickname', 'email', 'firstname', 'lastname', 'groupe'));
		
		$this->view->listing($sortBy, $sens, $page, $filters);
	}
	
	/**
	 * Automatically creates the user or updates it depending on the data given
	 * If needed, the User form is displayed
	 */
	protected function user_form($user_id = null, $db_data = array()) {
		$add_case = empty($user_id);
		if (!empty($_POST)) {
			$data = WRequest::getAssoc(array('nickname', 'password', 'password_conf', 'email', 'firstname', 'lastname', 'groupe', 'type', 'access'));
			$errors = array();
			
			// Check nickname availabililty
			if ($add_case || $data['nickname'] != $db_data['nickname']) {
				if (($e = $this->model->checkNickname($data['nickname'])) !== true) {
					$errors[] = WLang::get($e);
				}
			} else {
				unset($data['nickname']);
			}
			
			// Matching passwords
			if (!empty($data['password']) || !empty($data['password_conf'])) {
				if ($data['password'] === $data['password_conf']) {
					$data['password'] = <PASSWORD>($data['password']);
					if (!$add_case && $data['password'] == $db_data['password']) {
						unset($data['password']); // don't change password if it's the same
					}
				} else {
					$errors[] = WLang::get('error_password_not_matching');
				}
			} else if ($add_case) {
				$errors[] = WLang::get('error_no_password');
			}
			unset($data['password_conf']);
			
			// Email availabililty
			if ($add_case || $data['email'] != $db_data['email']) {
				if (($e = $this->model->checkEmail($data['email'])) !== true) {
					$errors[] = WLang::get($e);
				}
			}
			
			// Firstname and Lastname
			if (!$add_case && $data['firstname'] == $db_data['firstname']) {
				unset($data['firstname']);
			}
			if (!$add_case && $data['lastname'] == $db_data['lastname']) {
				unset($data['lastname']);
			}
			
			// Groupe
			$data['groupe'] = intval($data['groupe']);
			if (!$add_case && $data['groupe'] == $db_data['groupe']) {
				unset($data['groupe']);
			}
			
			// User access rights
			$data['access'] = $this->model->treatAccessData($data['type'], $data['access']);
			if (!$add_case && $data['access'] == $db_data['access']) {
				unset($data['access']);
			}
			unset($data['type']);
			
			if (empty($errors)) {
				if ($add_case) { // ADD case
					if ($this->model->createUser($data)) {
						// Send email if requested
						if (WRequest::get('email_confirmation') == 'on') {
							$mail = WHelper::load('phpmailer');
							$mail->CharSet = 'utf-8';
							$mail->From = WConfig::get('config.email');
							$mail->FromName = WConfig::get('config.site_name');
							$mail->Subject = WLang::get('user_register_email_subject', WConfig::get('config.site_name'));
							$mail->Body = 
"Bonjour,
<br /><br />
Un compte utilisateur vient de vous être créé sur le site ".WConfig::get('config.site_name').".<br /><br />
Pour vous connecter, rendez-vous à l'adresse <a href=\"".WRoute::getBase()."/user/login/\">".WRoute::getBase()."/user/login/</a>.
<br /><br />
Voici vos données de connexion :<br />
<strong>Identifiant :</strong> ".$data['nickname']."<br />
<strong>Mot de passe :</strong> ".$data['password_conf']."<commit_msg>Fixes #65 - Bug with password display in the email notification<commit_after><?php
/**
 * User Application - Admin Controller - /apps/user/admin/main.php
 */

defined('IN_WITY') or die('Access denied');

/**
 * UserAdminController is the Admin Controller of the User Application
 * 
 * @package Apps
 * @author <NAME> <<EMAIL>>
 * @version 0.3-26-04-2013
 */
class UserAdminController extends WController {
	/**
	 * @var Instance of UserAdminModel
	 */
	private $model;
	
	public function __construct() {
		include_once 'model.php';
		$this->model = new UserAdminModel();
		
		include_once 'view.php';
		$this->setView(new UserAdminView($this->model));
	}
	
	/**
	 * List action handler
	 * Displays a list of users in the database
	 */
	protected function listing() {
		// Admin check
		$admin_check = WRequest::get('admin_check');
		if (!empty($admin_check)) {
			$notify = WRequest::get('notify');
			foreach ($admin_check as $user_id => $action) {
				$db_data = $this->model->getUser($user_id);
				if (isset($db_data['valid']) && $db_data['valid'] == 2) {
					if ($action == 'validate') {
						$this->model->updateUser($user_id, array('valid' => 1));
						// Send email notification
						if ($notify) {
							$this->model->sendEmail(
								$db_data['email'],
								WLang::get('user_account_validated_subject', WConfig::get('config.site_name')),
								str_replace(
									array('{site_name}', '{base}'),
									array(WConfig::get('config.site_name'), WRoute::getBase()),
									WLang::get('user_account_validated_email')
								)
							);
						}
						WNote::success('user_account_validated', WLang::get('user_account_validated', $db_data['nickname']));
					} else if ($action == 'refuse') {
						$config = $this->model->getConfig();
						if ($config['keep_users']) {
							$this->model->updateUser($user_id, array('valid' => 0));
						} else {
							$this->model->deleteUser($user_id);
						}
						// Send email notification
						if ($notify) {
							$this->model->sendEmail(
								$db_data['email'],
								WLang::get('user_account_refused_subject', WConfig::get('config.site_name')),
								str_replace(
									array('{site_name}', '{base}'),
									array(WConfig::get('config.site_name'), WRoute::getBase()),
									WLang::get('user_account_refused_email')
								)
							);
						}
						WNote::success('user_account_refused', WLang::get('user_account_refused', $db_data['nickname']));
					}
				} else {
					WNote::success('user_account_invalid', WLang::get('user_account_invalid', $db_data['nickname']));
				}
			}
		}
		
		// Sorting criterias given by URL
		$criterias = $this->getOption(0);
		$count = sscanf(str_replace('-', ' ', $criterias), '%s %s %d', $sortBy, $sens, $page);
		if (empty($page) || $page <= 0) {
			$page = 1;
		}
		
		// Filters
		$filters = WRequest::getAssoc(array('nickname', 'email', 'firstname', 'lastname', 'groupe'));
		
		$this->view->listing($sortBy, $sens, $page, $filters);
	}
	
	/**
	 * Automatically creates the user or updates it depending on the data given
	 * If needed, the User form is displayed
	 */
	protected function user_form($user_id = null, $db_data = array()) {
		$add_case = empty($user_id);
		if (!empty($_POST)) {
			$data = WRequest::getAssoc(array('nickname', 'password', 'password_conf', 'email', 'firstname', 'lastname', 'groupe', 'type', 'access'));
			$errors = array();
			
			// Check nickname availability
			if ($add_case || $data['nickname'] != $db_data['nickname']) {
				if (($e = $this->model->checkNickname($data['nickname'])) !== true) {
					$errors[] = WLang::get($e);
				}
			} else {
				unset($data['nickname']);
			}
			
			// Matching passwords
			if (!empty($data['password']) || !empty($data['password_conf'])) {
				if ($data['password'] === $data['password_conf']) {
					$password_original = $data['password'];
					$data['password'] = <PASSWORD>($data['password']);
					if (!$add_case && $data['password'] == $db_data['password']) {
						unset($data['password']); // don't change password if it's the same
					}
				} else {
					$errors[] = WLang::get('error_password_not_matching');
				}
			} else if ($add_case) {
				$errors[] = WLang::get('error_no_password');
			}
			unset($data['password_conf']);
			
			// Email availability
			if ($add_case || $data['email'] != $db_data['email']) {
				if (($e = $this->model->checkEmail($data['email'])) !== true) {
					$errors[] = WLang::get($e);
				}
			}
			
			// Firstname and Lastname
			if (!$add_case && $data['firstname'] == $db_data['firstname']) {
				unset($data['firstname']);
			}
			if (!$add_case && $data['lastname'] == $db_data['lastname']) {
				unset($data['lastname']);
			}
			
			// Groupe
			$data['groupe'] = intval($data['groupe']);
			if (!$add_case && $data['groupe'] == $db_data['groupe']) {
				unset($data['groupe']);
			}
			
			// User access rights
			$data['access'] = $this->model->treatAccessData($data['type'], $data['access']);
			if (!$add_case && $data['access'] == $db_data['access']) {
				unset($data['access']);
			}
			unset($data['type']);
			
			if (empty($errors)) {
				if ($add_case) { // ADD case
					if ($this->model->createUser($data)) {
						// Send email if requested
						if (WRequest::get('email_confirmation') == 'on') {
							$mail = WHelper::load('phpmailer');
							$mail->CharSet = 'utf-8';
							$mail->From = WConfig::get('config.email');
							$mail->FromName = WConfig::get('config.site_name');
							$mail->Subject = WLang::get('user_register_email_subject', WConfig::get('config.site_name'));
							$mail->Body = 
"Bonjour,
<br /><br />
Un compte utilisateur vient de vous être créé sur le site ".WConfig::get('config.site_name').".<br /><br />
Pour vous connecter, rendez-vous à l'adresse <a href=\"".WRoute::getBase()."/user/login/\">".WRoute::getBase()."/user/login/</a>.
<br /><br />
Voici vos données de connexion :<br />
<strong>Identifiant :</strong> ".$data['nickname']."<br />
<strong>Mot de passe :</strong> ".$password_original."
{'fraction_non_alphanumeric': 0.20705842903847765, 'fraction_numerical': 0.0030178556459049375, 'mean_word_length': 3.532674772036474, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6852993', 'n_tokens_mistral': 4963, 'n_tokens_neox': 3951, 'n_words': 754}