Completed
Push — master ( 9f489a...6535a3 )
by Nikita
08:37
created

Application::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: nazarenko
5
 * Date: 20.10.2014
6
 * Time: 11:43
7
 */
8
namespace samsoncms\app\signin;
9
use samson\social\email\EmailStatus;
10
use samsonphp\event\Event;
11
12
/**
13
 * Generic class for user sign in
14
 * @author Olexandr Nazarenko <[email protected]>
15
 * @copyright 2014 SamsonOS
16
 */
17
class Application extends \samson\core\CompressableExternalModule
18
{
19
20
    public $id = 'signin';
21
22
    public static function authorize()
23
    {
24
        if (!m('social')->authorized()) {
25
            if (!m('socialemail')->cookieVerification()) {
26
                if (!url()->is('signin')) {
27
                    url()->redirect('/signin');
28
                }
29
            } else {
30
                url()->redirect('/signin');
31
            }
32
        } else {
33
            if (url()->is('signin')) {
34
                url()->redirect();
35
            }
36
        }
37
    }
38
39
40
    /** Check the user's authorization */
41
    public function __HANDLER()
42
    {
43
        self::authorize();
44
    }
45
46
    /** Main sign in template */
47
    public function __base()
48
    {
49
        // m('social')->prepare();
50
        s()->template('www/signin/signin_template.vphp');
51
        $result = '';
52
        $result .= m()->view('www/signin/signin_form.vphp')->output();
53
        m()->html($result)->title(t('Авторизация', true));
54
    }
55
56
    /** User asynchronous sign in */
57
    public function __async_login()
0 ignored issues
show
Coding Style introduced by
Method name "Application::__async_login" is not in camel caps format
Loading history...
58
    {
59
        $user = null;
60
        $remember = false;
61
        $error = '';
62
        if (isset($_POST['email']) && isset($_POST['password'])) {
63
            $email = md5($_POST['email']);
64
            $password = md5($_POST['password']);
65
            if (isset($_POST['remember'])) {
66
                $remember = true;
67
            }
68
            $auth = m('socialemail')->authorizeWithEmail($email, $password, $remember, $user);
69
            if ($auth->code == EmailStatus::SUCCESS_EMAIL_AUTHORIZE) {
70
                if (dbQuery('user')->cond('user_id', $user->id)->first()) {
0 ignored issues
show
Deprecated Code introduced by
The method samson\activerecord\dbQuery::cond() has been deprecated with message: @see self::where()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
71
                    // Fire login success event
72
                    Event::fire('samson.cms.signin.login', array(& $user));
73
                    return array('status' => '1');
74 View Code Duplication
                } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
                    $error .= m()->view('www/signin/signin_form.vphp')
76
                        ->focus('autofocus')
77
                        ->errorClass('errorAuth')
78
                        ->output();
79
                    return array('status' => '0', 'html' => $error);
80
                }
81 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
                $error .= m()->view('www/signin/signin_form.vphp')
83
                    ->errorClass('errorAuth')
84
                    ->userEmail("{$_POST['email']}")
85
                    ->focus('autofocus')
86
                    ->output();
87
                return array('status' => '0', 'html' => $error);
88
            }
89 View Code Duplication
        } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
            $error .= m()->view('www/signin/signin_form')->errorClass('errorAuth')->output();
91
            return array('status' => '0', 'html' => $error);
92
        }
93
    }
94
95
    /** User logout */
96
    public function __logout()
97
    {
98
        m('socialemail')->deauthorize();
99
        // Fire logout event
100
        Event::fire('samson.cms.signin.logout');
101
        url()->redirect();
102
    }
103
104
    /** Sending email with the correct address */
105
    public function __mail()
106
    {
107
        if (isset($_POST['email'])) {
108
            /** @var \samson\activerecord\user $user */
109
            $user = null;
110
            $result = '';
111
            if (dbQuery('user')->email($_POST['email'])->first($user)) {
112
                $user->confirmed = md5(generate_password(20) . time());
113
                $user->save();
114
                $message = m()->view('www/signin/email/pass_recovery')->code($user->confirmed)->output();
115
116
                mail_send($user->Email, '[email protected]', $message, t('Восстановление пароля!', true), 'SamsonCMS');
117
118
                $result .= m()->view('www/signin/pass_recovery_mailsend')->output();
119
                s()->template('www/signin/signin_template.vphp');
120
                m()->html($result)->title(t('Восстановление пароля', true));
121
            } else {
122
                url()->redirect();
123
            }
124
        } else {
125
            url()->redirect();
126
        }
127
    }
128
129
    /**
130
     * New password form
131
     * @param string $code Code password recovery
132
     */
133
    public function __confirm($code)
134
    {
135
        if (dbQuery('user')->confirmed($code)->first()) {
136
            $result = '';
137
            $result .= m()->view('www/signin/new_pass_form')->code($code)->output();
138
            s()->template('www/signin/signin_template.vphp');
139
            m()->html($result)->title(t('Восстановление пароля', true));
140
        } else {
141
            e404();
142
        }
143
    }
144
145
    /**
146
     * Setting new password and sign in
147
     * @param string $code Code password recovery
148
     */
149
    public function __recovery($code)
150
    {
151
        if (isset($_POST['password']) && isset($_POST['confirm_password'])
152
            && $_POST['password'] == $_POST['confirm_password']
153
        ) {
154
            /** @var \samson\activerecord\user $user */
155
            $user = null;
156
            if (dbQuery('user')->confirmed($code)->first($user)) {
157
                $user->confirmed = 1;
158
                $user->md5_password = md5($_POST['password']);
159
                $user->Password = $_POST['password'];
160
                $user->save();
161
                if (m('socialemail')->authorizeWithEmail($user->md5_email, $user->md5_password, $user)
162
                        ->code == EmailStatus::SUCCESS_EMAIL_AUTHORIZE
163
                ) {
164
                    url()->redirect();
165
                }
166
            }
167
        } else {
168
            $result = '';
169
            $result .= m()->view('www/signin/pass_error')
170
                ->message(t('Вы ввели некорректный пароль либо пароли не совпадают', true))
171
                ->output();
172
            s()->template('www/signin/signin_template.vphp');
173
            m()->html($result)->title(t('Ошибка восстановление пароля', true));
174
        }
175
    }
176
}
177