Completed
Branch master (b53644)
by Pierre-Henry
33:47
created

app/system/modules/connect/inc/class/Microsoft.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * @title          Microsoft OAuth (Windows Live) Class
4
 *
5
 * @author         Pierre-Henry Soria <[email protected]>
6
 * @copyright      (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
7
 * @license        GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
8
 * @package        PH7 / App / System / Module / Connect / Inc / Class
9
 * @version        0.6
10
 */
11
namespace PH7;
12
defined('PH7') or exit('Restricted access');
13
14
use
15
PH7\Framework\File\Import,
16
PH7\Framework\Date\CDateTime,
17
PH7\Framework\Config\Config,
18
PH7\Framework\Mvc\Model\DbConfig,
19
PH7\Framework\Ip\Ip,
20
PH7\Framework\Util\Various,
21
PH7\Framework\Geo\Ip\Geo,
22
PH7\Framework\Mvc\Router\Uri;
23
24
class Microsoft extends Api
25
{
26
27
    private $_oClient, $_sUsername, $_iProfileId, $_aUserInfo;
0 ignored issues
show
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
28
29
    /**
30
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
31
     */
32
    public function __construct()
33
    {
34
        parent::__construct();
35
36
        // Import the library
37
        Import::lib('Service.Microsoft.Live.oauth_client');
38
        Import::lib('Service.Microsoft.Live.http');
39
40
        $this->_oClient = new \oauth_client_class;
41
42
        $this->_setConfig();
43
44
        /* API permissions */
45
        $this->_oClient->scope = 'wl.basic wl.emails wl.birthday';
46
47
        if(($bSuccess = $this->_oClient->Initialize()))
48
        {
49
            if(($bSuccess = $this->_oClient->Process()))
50
            {
51
                if(strlen($this->_oClient->authorization_error))
52
                {
53
                    $this->_oClient->error = $this->_oClient->authorization_error;
54
                    $bSuccess = false;
55
                }
56
                elseif(strlen($this->_oClient->access_token))
57
                {
58
                    $bSuccess = $this->_oClient->CallAPI(
59
                        'https://apis.live.net/v5.0/me',
60
                        'GET',
61
                        array(),
62
                        array('FailOnAccessError' => true),
63
                        $oUserData
64
                    );
65
                }
66
            }
67
68
            $bSuccess = $this->_oClient->Finalize($bSuccess);
69
        }
70
71
        if($this->_oClient->exit)
72
            exit(1);
73
74
        if($bSuccess)
75
        {
76
            // User info is ok? Here we will be connect the user and/or adding the login and registering routines...
77
            $oUserModel = new UserCoreModel;
78
79
            if(!$iId = $oUserModel->getId($oUserData->emails->account))
80
            {
81
                // Add User if it does not exist in our database
82
                $this->add(escape($oUserData, true), $oUserModel);
83
84
                $this->oDesign->setFlashMsg( t('You have now been registered! %0%', (new Registration)->sendMail($this->_aUserInfo, true)->getMsg()) );
85
                $this->sUrl = Uri::get('connect','main','register');
86
            }
87
            else
88
            {   // Login
89
                $this->setLogin($iId, $oUserModel);
90
                $this->sUrl = Uri::get('connect','main','home');
91
            }
92
93
            unset($oUserModel);
94
        }
95
        else
96
        {
97
            // For testing purposes, if there was an error, let's kill the script
98
            $this->oDesign->setFlashMsg(t('Oops! An error has occurred. Please try again later.'));
99
            $this->sUrl = Uri::get('connect','main','index');
100
        }
101
102
    }
103
104
    /**
105
     * @param object $oProfile
106
     * @param object \PH7\UserCoreModel $oUserModel
107
     * @return void
108
     */
109
    public function add($oProfile, UserCoreModel $oUserModel)
110
    {
111
        $oUser = new UserCore;
112
        $sBirthDate = (isset($oProfile->birth_month, $oProfile->birth_day, $oProfile->birth_year)) ? $oProfile->birth_month . '/' . $oProfile->birth_day . '/' . $oProfile->birth_year : date('m/d/Y', strtotime('-30 year'));
113
        $sSex = $this->checkGender($oProfile->gender);
114
        $sMatchSex = $oUser->getMatchSex($sSex);
115
        $this->_sUsername = $oUser->findUsername($oProfile->name, $oProfile->first_name, $oProfile->last_name);
116
        unset($oUser);
117
118
        $this->_aUserInfo = [
119
            'email' => $oProfile->emails->account,
120
            'username' => $this->_sUsername,
121
            'password' => Various::genRndWord(8,30),
122
            'first_name' => (!empty($oProfile->first_name)) ? $oProfile->first_name : '',
123
            'last_name' => (!empty($oProfile->last_name)) ? $oProfile->last_name : '',
124
            'sex' => $sSex,
125
            'match_sex' => array($sMatchSex),
126
            'birth_date' => (new CDateTime)->get($sBirthDate)->date('Y-m-d'),
127
            'country' => Geo::getCountryCode(),
128
            'city' => Geo::getCity(),
129
            'state' => Geo::getState(),
130
            'zip_code' => Geo::getZipCode(),
131
            'description' => '',
132
            'website' => '',
133
            'social_network_site' => '',
134
            'ip' => Ip::get(),
135
            'prefix_salt' => Various::genRnd(),
136
            'suffix_salt' => Various::genRnd(),
137
            'hash_validation' => Various::genRnd(),
138
            'is_active' => DbConfig::getSetting('userActivationType')
139
        ];
140
141
        $this->_iProfileId = $oUserModel->add($this->_aUserInfo);
142
    }
143
144
    /**
145
     * Set Configuration of Microsoft OAuth API.
146
     *
147
     * @return void
148
     */
149
    private function _setConfig()
150
    {
151
        $this->_oClient->server = 'Microsoft';
152
        $this->_oClient->redirect_uri = Uri::get('connect','main','login','google');
153
154
        $this->_oClient->client_id = Config::getInstance()->values['module.api']['microsoft.client_id'];
155
        $this->_oClient->client_secret = Config::getInstance()->values['module.api']['microsoft.client_secret_key'];
156
    }
157
158
}
159