Completed
Push — master ( f05331...ca6c78 )
by Nikita
05:18 queued 11s
created

Application::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 3
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
10
use samson\activerecord\dbQuery;
11
use samson\cms\CMS;
12
use samson\social\email\EmailStatus;
13
use samsonframework\core\RequestInterface;
14
use samsonframework\core\ResourcesInterface;
15
use samsonframework\core\SystemInterface;
16
use samsonphp\event\Event;
17
use samsonframework\orm\QueryInterface;
18
use samson\social\email\Email;
19
use samson\core\Core;
20
21
/**
22
 * Generic class for user sign in
23
 * @author Olexandr Nazarenko <[email protected]>
24
 * @copyright 2014 SamsonOS
25
 */
26
class Application extends \samson\core\CompressableExternalModule
27
{
28
    /** @var string Identifier */
29
    public $id = 'signin';
30
31
    /** @var Email Pointer to social email module */
32
    protected $social;
33
34
    /** @var QueryInterface Database query instance */
35
    protected $query;
36
37
    /** @var RequestInterface Request instance */
38
    protected $request;
39
40
    public function authorize($cms)
41
    {
42
        if ($cms->isCMS()) {
43
            if ( ! $this->social->authorized()) {
44
                if ( ! $this->social->cookieVerification()) {
45
                    if ( ! $this->request->is('signin')) {
46
                        $this->request->redirect('/'.$cms->id.'/signin');
47
                    }
48
                } else {
49
                    $this->request->redirect('/'.$cms->id.'/signin');
50
                }
51
            } else {
52
                if ($this->request->is('signin')) {
53
                    $this->request->redirect('/'.$cms->id);
54
                }
55
            }
56
        }
57
    }
58
59
    public function init(array $params = array())
60
    {
61
        //trace('cmsInit');
62
        // Old applications main page rendering
63
        Event::subscribe(\samsoncms\cms\Application::EVENT_IS_CMS, array($this, 'authorize'));
64
65
        // Call parent initialization
66
        return parent::init($params);
67
    }
68
69
    /**
70
     * Application constructor.
71
     *
72
     * @param string $path
73
     * @param ResourcesInterface $resources
74
     * @param SystemInterface $system
75
     */
76
    public function  __construct($path, ResourcesInterface $resources, SystemInterface $system)
0 ignored issues
show
Coding Style introduced by
Expected "function abc(...)"; found "function abc(...)"
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 2 found
Loading history...
77
    {
78
        // Inject dependencies
79
        $this->social  = m('socialemail');
0 ignored issues
show
Documentation Bug introduced by
It seems like m('socialemail') can also be of type object<samson\core\Module>. However, the property $social is declared as type object<samson\social\email\Email>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
80
        $this->request = url();
0 ignored issues
show
Documentation Bug introduced by
It seems like url() of type object<samson\core\URL> is incompatible with the declared type object<samsonframework\core\RequestInterface> of property $request.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
81
        $this->query   = new dbQuery();
82
83
        parent::__construct($path, $resources, $system);
84
    }
85
86
    //[PHPCOMPRESSOR(remove,start)]
87
    /** Module preparation */
88
    public function prepare()
89
    {
90
        // Create default user for first logins
91
        $adminUser        = '[email protected]';
92
        $hashedEmailValue = $this->social->hash($adminUser);
93
94
        // Try to find generic user
95
        $admin = $this->query
96
            ->entity($this->social->dbTable)
97
            ->where($this->social->dbEmailField, $adminUser)
98
            ->first();
99
100
        // Create user record if missing
101
        if ( ! isset($admin)) {
102
            $admin = new $this->social->dbTable();
103
        }
104
105
        // Fill in user credentials according to config
106
        $admin[$this->social->dbEmailField]        = $adminUser;
107
        $admin[$this->social->dbHashEmailField]    = $hashedEmailValue;
108
        $admin[$this->social->dbHashPasswordField] = $hashedEmailValue;
109
        $admin->save();
110
    }
111
    //[PHPCOMPRESSOR(remove,end)]
112
113
    /** Check the user's authorization */
114
    public function __HANDLER()
115
    {
116
        $this->authorize($this->social);
117
    }
118
119
    /** Main sign in template */
120
    public function __base()
121
    {
122
        // Change template
123
        $this->system->template('www/signin/signin_template.vphp');
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\app\signin\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
124
125
        // Render template with sign in form
126
        $this->html($this->view('www/signin/signin_form.vphp')->output())
127
             ->title(t('Авторизация', true));
128
    }
129
130
    /** User asynchronous sign in */
131
    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...
132
    {
133
        $user  = null;
134
        $error = '';
135
136
        if (isset($_POST['email']) && isset($_POST['password'])) {
137
            $email    = $this->social->hash($_POST['email']);
138
            $password = $this->social->hash($_POST['password']);
139
            $remember = isset($_POST['remember']) ? true : false;
140
141
            /** @var EmailStatus Perform email authorization */
142
            $auth = $this->social->authorizeWithEmail($email, $password, $remember, $user);
143
144
            if ($auth->code === EmailStatus::SUCCESS_EMAIL_AUTHORIZE) {
145
                // Fire login success event
146
                Event::fire('samson.cms.signin.login', array(&$user));
147
148
                return array('status' => '1');
149
            } else {
150
                $error .= $this->view('www/signin/signin_form.vphp')
151
                               ->errorClass('errorAuth')
152
                               ->userEmail("{$_POST['email']}")
153
                               ->focus('autofocus')
154
                               ->output();
155
156
                return array('status' => '0', 'html' => $error);
157
            }
158
        } else {
159
            $error .= $this->view('www/signin/signin_form')->errorClass('errorAuth')->output();
160
161
            return array('status' => '0', 'html' => $error);
162
        }
163
    }
164
165
    /** User logout */
166
    public function __logout()
167
    {
168
        $this->social->deauthorize();
169
170
        // Fire logout event
171
        Event::fire('samson.cms.signin.logout');
172
173
        $this->request->redirect('cms/signin');
174
    }
175
176
    /** Sending email with the correct address */
177
    public function __mail()
178
    {
179
        if (isset($_POST['email'])) {
180
            /** @var \samson\activerecord\user $user */
181
            $user   = null;
182
            $result = '';
183
            if ($this->query->entity('user')->where('email', $_POST['email'])->first($user)) {
184
                $user->confirmed = $this->social->hash(generate_password(20) . time());
0 ignored issues
show
Bug introduced by
Accessing confirmed on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
185
                $user->save();
0 ignored issues
show
Bug introduced by
The method save() does not seem to exist on object<samsonframework\orm\RecordInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
186
                $message = $this->view('www/signin/email/pass_recovery')->code($user->confirmed)->output();
0 ignored issues
show
Bug introduced by
Accessing confirmed on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
187
188
                mail_send($user->Email, '[email protected]', $message, t('Восстановление пароля!', true), 'SamsonCMS');
0 ignored issues
show
Bug introduced by
Accessing Email on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
189
190
                $result .= $this->view('www/signin/pass_recovery_mailsend')->output();
191
                $this->system->template('www/signin/signin_template.vphp');
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\app\signin\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
192
                $this->html($result)->title(t('Восстановление пароля', true));
193
            } else {
194
                $this->request->redirect();
195
            }
196
        } else {
197
            $this->request->redirect();
198
        }
199
    }
200
201
    /**
202
     * New password form.
203
     *
204
     * @param string $code Code password recovery
205
     *
206
     * @return bool
207
     */
208
    public function __confirm($code)
209
    {
210
        if ($this->query->entity('user')->where($this->social->dbConfirmField, $code)->first()) {
211
            $this->system->template('www/signin/signin_template.vphp');
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\app\signin\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
212
            $this->html($this->view('www/signin/new_pass_form')->code($code)->output())
213
                 ->title(t('Восстановление пароля', true));
214
        } else {
215
            return A_FAILED;
216
        }
217
    }
218
219
    /**
220
     * Setting new password and sign in
221
     *
222
     * @param string $code Code password recovery
223
     */
224
    public function __recovery($code)
225
    {
226
        if (isset($_POST['password']) && isset($_POST['confirm_password'])
227
            && $_POST['password'] == $_POST['confirm_password']
228
        ) {
229
            /** @var \samson\activerecord\user $user */
230
            $user = null;
231
            if ($this->query->where('confirmed', $code)->first($user)) {
232
                $user->confirmed    = 1;
0 ignored issues
show
Bug introduced by
Accessing confirmed on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
233
                $user->md5_password = md5($_POST['password']);
0 ignored issues
show
Bug introduced by
Accessing md5_password on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
234
                $user->Password     = $_POST['password'];
0 ignored issues
show
Bug introduced by
Accessing Password on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
235
                $user->save();
0 ignored issues
show
Bug introduced by
The method save() does not seem to exist on object<samsonframework\orm\RecordInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
236
237
                $auth = $this->social->authorizeWithEmail($user->md5_email, $user->md5_password, $user);
0 ignored issues
show
Bug introduced by
Accessing md5_email on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
Accessing md5_password on the interface samsonframework\orm\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Documentation introduced by
$user is of type object<samsonframework\orm\RecordInterface>, but the function expects a boolean|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
238
                if ($auth->code === EmailStatus::SUCCESS_EMAIL_AUTHORIZE) {
239
                    $this->request->redirect();
240
                }
241
            }
242
        } else {
243
            $result = '';
244
            $result .= m()->view('www/signin/pass_error')
245
                          ->message(t('Вы ввели некорректный пароль либо пароли не совпадают', true))
246
                          ->output();
247
            $this->system->template('www/signin/signin_template.vphp');
0 ignored issues
show
Documentation introduced by
The property system does not exist on object<samsoncms\app\signin\Application>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
248
            $this->html($result)->title(t('Ошибка восстановление пароля', true));
249
        }
250
    }
251
}
252