Alexander Makarov
12 years ago
11 changed files with 9 additions and 130 deletions
@ -1,63 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace app\models; |
|
||||||
|
|
||||||
use yii\base\Model; |
|
||||||
|
|
||||||
/** |
|
||||||
* ContactForm is the model behind the contact form. |
|
||||||
*/ |
|
||||||
class ContactForm extends Model |
|
||||||
{ |
|
||||||
public $name; |
|
||||||
public $email; |
|
||||||
public $subject; |
|
||||||
public $body; |
|
||||||
public $verifyCode; |
|
||||||
|
|
||||||
/** |
|
||||||
* @return array the validation rules. |
|
||||||
*/ |
|
||||||
public function rules() |
|
||||||
{ |
|
||||||
return array( |
|
||||||
// name, email, subject and body are required |
|
||||||
array('name, email, subject, body', 'required'), |
|
||||||
// email has to be a valid email address |
|
||||||
array('email', 'email'), |
|
||||||
// verifyCode needs to be entered correctly |
|
||||||
array('verifyCode', 'captcha'), |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* @return array customized attribute labels |
|
||||||
*/ |
|
||||||
public function attributeLabels() |
|
||||||
{ |
|
||||||
return array( |
|
||||||
'verifyCode' => 'Verification Code', |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Sends an email to the specified email address using the information collected by this model. |
|
||||||
* @param string $email the target email address |
|
||||||
* @return boolean whether the model passes validation |
|
||||||
*/ |
|
||||||
public function contact($email) |
|
||||||
{ |
|
||||||
if ($this->validate()) { |
|
||||||
$name = '=?UTF-8?B?' . base64_encode($this->name) . '?='; |
|
||||||
$subject = '=?UTF-8?B?' . base64_encode($this->subject) . '?='; |
|
||||||
$headers = "From: $name <{$this->email}>\r\n" . |
|
||||||
"Reply-To: {$this->email}\r\n" . |
|
||||||
"MIME-Version: 1.0\r\n" . |
|
||||||
"Content-type: text/plain; charset=UTF-8"; |
|
||||||
mail($email, $subject, $this->body, $headers); |
|
||||||
return true; |
|
||||||
} else { |
|
||||||
return false; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,58 +0,0 @@ |
|||||||
<?php |
|
||||||
|
|
||||||
namespace app\models; |
|
||||||
|
|
||||||
use Yii; |
|
||||||
use yii\base\Model; |
|
||||||
|
|
||||||
/** |
|
||||||
* LoginForm is the model behind the login form. |
|
||||||
*/ |
|
||||||
class LoginForm extends Model |
|
||||||
{ |
|
||||||
public $username; |
|
||||||
public $password; |
|
||||||
public $rememberMe = true; |
|
||||||
|
|
||||||
/** |
|
||||||
* @return array the validation rules. |
|
||||||
*/ |
|
||||||
public function rules() |
|
||||||
{ |
|
||||||
return array( |
|
||||||
// username and password are both required |
|
||||||
array('username, password', 'required'), |
|
||||||
// password is validated by validatePassword() |
|
||||||
array('password', 'validatePassword'), |
|
||||||
// rememberMe must be a boolean value |
|
||||||
array('rememberMe', 'boolean'), |
|
||||||
); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Validates the password. |
|
||||||
* This method serves as the inline validation for password. |
|
||||||
*/ |
|
||||||
public function validatePassword() |
|
||||||
{ |
|
||||||
$user = User::findByUsername($this->username); |
|
||||||
if (!$user || !$user->validatePassword($this->password)) { |
|
||||||
$this->addError('password', 'Incorrect username or password.'); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* Logs in a user using the provided username and password. |
|
||||||
* @return boolean whether the user is logged in successfully |
|
||||||
*/ |
|
||||||
public function login() |
|
||||||
{ |
|
||||||
if ($this->validate()) { |
|
||||||
$user = User::findByUsername($this->username); |
|
||||||
Yii::$app->user->login($user, $this->rememberMe ? 3600*24*30 : 0); |
|
||||||
return true; |
|
||||||
} else { |
|
||||||
return false; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
Loading…
Reference in new issue