Completed
Push — master ( e632c8...226a49 )
by Vitaly
06:04
created

Application::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
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
    /** @var string Identifier */
20
    public $id = 'cms-signin';
21
22
    public function authorize($social)
23
    {
24
        if (m('cms')->isCMS()) {
25
            if ($social->id == 'socialemail') {
26
                if (!m('social')->authorized()) {
27
                    if (!m('socialemail')->cookieVerification()) {
28
                        if (!url()->is('cms-signin')) {
29
                            url()->redirect('/cms/signin');
30
                        }
31
                    } else {
32
                        url()->redirect('/cms/signin');
33
                    }
34
                } else {
35
                    if (url()->is('cms-signin')) {
36
                        url()->redirect('/cms');
37
                    }
38
                }
39
            }
40
        }
41
    }
42
43
    /** Check the user's authorization */
44
    public function __HANDLER()
45
    {
46
        $this->authorize();
0 ignored issues
show
Bug introduced by
The call to authorize() misses a required argument $social.

This check looks for function calls that miss required arguments.

Loading history...
47
    }
48
49
    /** Main sign in template */
50
    public function __base()
51
    {
52
        // m('social')->prepare();
53
        s()->template('www/signin/signin_template.vphp');
54
        $result = '';
55
        $result .= m()->view('www/signin/signin_form.vphp')->output();
56
        m()->html($result)->title(t('Авторизация', true));
57
    }
58
59
    /** User asynchronous sign in */
60
    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...
61
    {
62
        $user = null;
63
        $remember = false;
64
        $error = '';
65
        if (isset($_POST['email']) && isset($_POST['password'])) {
66
            $email = md5($_POST['email']);
67
            $password = md5($_POST['password']);
68
            if (isset($_POST['remember'])) {
69
                $remember = true;
70
            }
71
            $auth = m('socialemail')->authorizeWithEmail($email, $password, $remember, $user);
72
            if ($auth->code == EmailStatus::SUCCESS_EMAIL_AUTHORIZE) {
73
                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...
74
                    // Fire login success event
75
                    Event::fire('samson.cms.signin.login', array(& $user));
76
                    return array('status' => '1');
77 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...
78
                    $error .= m()->view('www/signin/signin_form.vphp')
79
                        ->focus('autofocus')
80
                        ->errorClass('errorAuth')
81
                        ->output();
82
                    return array('status' => '0', 'html' => $error);
83
                }
84 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...
85
                $error .= m()->view('www/signin/signin_form.vphp')
86
                    ->errorClass('errorAuth')
87
                    ->userEmail("{$_POST['email']}")
88
                    ->focus('autofocus')
89
                    ->output();
90
                return array('status' => '0', 'html' => $error);
91
            }
92 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...
93
            $error .= m()->view('www/signin/signin_form')->errorClass('errorAuth')->output();
94
            return array('status' => '0', 'html' => $error);
95
        }
96
    }
97
98
    /** User logout */
99
    public function __logout()
100
    {
101
        m('socialemail')->deauthorize();
102
        // Fire logout event
103
        Event::fire('samson.cms.signin.logout');
104
        //url()->redirect();
105
    }
106
107
    /** Sending email with the correct address */
108
    public function __mail()
109
    {
110
        if (isset($_POST['email'])) {
111
            /** @var \samson\activerecord\user $user */
112
            $user = null;
113
            $result = '';
114
            if (dbQuery('user')->email($_POST['email'])->first($user)) {
115
                $user->confirmed = md5(generate_password(20) . time());
116
                $user->save();
117
                $message = m()->view('www/signin/email/pass_recovery')->code($user->confirmed)->output();
118
119
                mail_send($user->Email, '[email protected]', $message, t('Восстановление пароля!', true), 'SamsonCMS');
120
121
                $result .= m()->view('www/signin/pass_recovery_mailsend')->output();
122
                s()->template('www/signin/signin_template.vphp');
123
                m()->html($result)->title(t('Восстановление пароля', true));
124
            } else {
125
                url()->redirect();
126
            }
127
        } else {
128
            url()->redirect();
129
        }
130
    }
131
132
    /**
133
     * New password form
134
     * @param string $code Code password recovery
135
     */
136
    public function __confirm($code)
137
    {
138
        if (dbQuery('user')->confirmed($code)->first()) {
139
            $result = '';
140
            $result .= m()->view('www/signin/new_pass_form')->code($code)->output();
141
            s()->template('www/signin/signin_template.vphp');
142
            m()->html($result)->title(t('Восстановление пароля', true));
143
        } else {
144
            e404();
145
        }
146
    }
147
148
    /**
149
     * Setting new password and sign in
150
     * @param string $code Code password recovery
151
     */
152
    public function __recovery($code)
153
    {
154
        if (isset($_POST['password']) && isset($_POST['confirm_password'])
155
            && $_POST['password'] == $_POST['confirm_password']
156
        ) {
157
            /** @var \samson\activerecord\user $user */
158
            $user = null;
159
            if (dbQuery('user')->confirmed($code)->first($user)) {
160
                $user->confirmed = 1;
161
                $user->md5_password = md5($_POST['password']);
162
                $user->Password = $_POST['password'];
163
                $user->save();
164
                if (m('socialemail')->authorizeWithEmail($user->md5_email, $user->md5_password, $user)
165
                        ->code == EmailStatus::SUCCESS_EMAIL_AUTHORIZE
166
                ) {
167
                    url()->redirect();
168
                }
169
            }
170
        } else {
171
            $result = '';
172
            $result .= m()->view('www/signin/pass_error')
173
                ->message(t('Вы ввели некорректный пароль либо пароли не совпадают', true))
174
                ->output();
175
            s()->template('www/signin/signin_template.vphp');
176
            m()->html($result)->title(t('Ошибка восстановление пароля', true));
177
        }
178
    }
179
}
180