function filterData(Array $in) {
foreach ($in as $key => $value) {
if (is_array($value)) {
filterData($in[$key]);
} else {
$value = trim($value);
$value = strip_tags($value);
$value = htmlspecialchars($value);
$in[$key] = $value;
}
}
return $in;
}
$cleanArray = filterData( json_decode($arr, true) );
//User model
public function checkEmail($email) {
$data = [
'email' => $email
];
$sql = "SELECT `id` FROM `$this->table` WHERE `email` = :email";
return $this->model->getCount( $sql, $data );
}
//Base model
public function getCount($sql, Array $args) {
$stmt = $this->connection->prepare($sql);
if ( $stmt->execute($args) ) {
return $stmt->rowCount();
}
return false;
}
/**
* AccountController constructor.
* @param ViewInterface $view
* @param ModelInterface $model
* @param AuthHandler $authHandler
* @Dependencies(
* 'app\Core\View\BaseView',
* 'src\Models\Auth\AuthModel',
* 'src\Middleware\Auth\AuthHandler'
* )
*/
public function __construct(ViewInterface $view,ModelInterface $model, AuthHandler $authHandler) {
$this->view = $view;
$this->model = $model;
$this->authHandler = $authHandler;
}
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('success', 'Спасибо за ваше письмо. Мы свяжемся с вами в ближайшее время.');
} else {
Yii::$app->session->setFlash('error', 'Ошибка отправки почты.');
}
return $this->refresh();
} else {
return $this->render('contact', ['model' => $model]);
}
}
function send($subject, $message, $from, $to) {
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: $from\r\n";
$headers .= "Reply-To: reply-to@example.com\r\n";
return mail($to, $subject, $message, $headers);
}
send("Тема письма", $render, "Отправитель: <info@site.ru>", 'email@email.ru');