Completed
Branch master (f2ae55)
by Pierre-Henry
36:39
created

UserCoreModel::getBackground()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 14
nc 5
nop 2
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
/**
3
 * @title          User Core Model Class
4
 *
5
 * @author         Pierre-Henry Soria <[email protected]>
6
 * @copyright      (c) 2012-2018, 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 / Core / Model
9
 */
10
11
namespace PH7;
12
13
use PH7\Framework\CArray\ObjArr;
14
use PH7\Framework\Date\CDateTime;
15
use PH7\Framework\Ip\Ip;
16
use PH7\Framework\Mvc\Model\DbConfig;
17
use PH7\Framework\Mvc\Model\Engine\Db;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PH7\Db.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
18
use PH7\Framework\Mvc\Model\Engine\Model;
19
use PH7\Framework\Mvc\Model\Engine\Util\Various;
20
use PH7\Framework\Security\Security;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PH7\Security.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
21
use PH7\Framework\Session\Session;
22
use PH7\Framework\Str\Str;
23
use stdClass;
24
25
// Abstract Class
26
class UserCoreModel extends Model
27
{
28
    const CACHE_GROUP = 'db/sys/mod/user';
29
    const CACHE_TIME = 604800;
30
31
    const OFFLINE_STATUS = 0;
32
    const ONLINE_STATUS = 1;
33
    const BUSY_STATUS = 2;
34
    const AWAY_STATUS = 3;
35
36
    /** @var string */
37
    protected $sCurrentDate;
38
39
    /** @var string */
40
    protected $iProfileId;
41
42
    public function __construct()
43
    {
44
        parent::__construct();
45
46
        $this->sCurrentDate = (new CDateTime)->get()->dateTime('Y-m-d H:i:s');
47
        $this->iProfileId = (new Session)->get('member_id');
48
    }
49
50
    public static function checkGroup()
51
    {
52
        $oSession = new Session;
53
54
        if (!$oSession->exists('member_group_id')) {
55
            $oSession->regenerateId();
56
            $oSession->set('member_group_id', PermissionCore::VISITOR_GROUP_ID);
57
        }
58
59
        $rStmt = Db::getInstance()->prepare('SELECT permissions FROM' . Db::prefix(DbTableName::MEMBERSHIP) . 'WHERE groupId = :groupId LIMIT 1');
60
        $rStmt->bindValue(':groupId', $oSession->get('member_group_id'), \PDO::PARAM_INT);
61
        $rStmt->execute();
62
        $sPermissions = $rStmt->fetchColumn();
63
        Db::free($rStmt);
64
65
        return ObjArr::toObject(unserialize($sPermissions));
66
    }
67
68
    /**
69
     * Login method for Members and Affiliate, but not for Admins since it has another method PH7\AdminModel::adminLogin() even more secure.
70
     *
71
     * @param string $sEmail Not case sensitive since on lot of mobile devices (such as iPhone), the first letter is uppercase.
72
     * @param string $sPassword
73
     * @param string $sTable Default DbTableName::MEMBER
74
     *
75
     * @return bool|string (boolean "true" or string "message")
76
     */
77
    public function login($sEmail, $sPassword, $sTable = DbTableName::MEMBER)
78
    {
79
        Various::checkModelTable($sTable);
80
81
        $rStmt = Db::getInstance()->prepare('SELECT email, password FROM' . Db::prefix($sTable) . 'WHERE email = :email LIMIT 1');
82
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
83
        $rStmt->execute();
84
        $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
85
        Db::free($rStmt);
86
87
        $sDbEmail = (!empty($oRow->email)) ? $oRow->email : '';
88
        $sDbPassword = (!empty($oRow->password)) ? $oRow->password : '';
89
90
        if (strtolower($sEmail) !== strtolower($sDbEmail)) {
91
            return 'email_does_not_exist';
92
        }
93
        if (!Security::checkPwd($sPassword, $sDbPassword)) {
94
            return 'password_does_not_exist';
95
        }
96
97
        return true;
98
    }
99
100
    /**
101
     * Set Log Session.
102
     *
103
     * @param string $sEmail
104
     * @param string $sUsername
105
     * @param string $sFirstName
106
     * @param string $sTable
107
     * @param string $sTable Default DbTableName::MEMBER
108
     *
109
     * @return void
110
     */
111
    public function sessionLog($sEmail, $sUsername, $sFirstName, $sTable = DbTableName::MEMBER)
112
    {
113
        Various::checkModelTable($sTable);
114
115
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix($sTable . '_log_sess') . '(email, username, firstName, ip)
116
        VALUES (:email, :username, :firstName, :ip)');
117
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
118
        $rStmt->bindValue(':username', $sUsername, \PDO::PARAM_STR);
119
        $rStmt->bindValue(':firstName', $sFirstName, \PDO::PARAM_STR);
120
        $rStmt->bindValue(':ip', Ip::get(), \PDO::PARAM_STR);
121
        $rStmt->execute();
122
        Db::free($rStmt);
123
    }
124
125
    /**
126
     * Read Profile Data.
127
     *
128
     * @param int $iProfileId The user ID
129
     * @param string $sTable Default DbTableName::MEMBER
130
     *
131
     * @return stdClass|bool The data of a member if exists, FALSE otherwise.
132
     */
133
    public function readProfile($iProfileId, $sTable = DbTableName::MEMBER)
134
    {
135
        $this->cache->start(self::CACHE_GROUP, 'readProfile' . $iProfileId . $sTable, static::CACHE_TIME);
136
137
        if (!$oData = $this->cache->get()) {
138
            Various::checkModelTable($sTable);
139
140
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
141
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
142
            $rStmt->execute();
143
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
144
            Db::free($rStmt);
145
            $this->cache->put($oData);
146
        }
147
148
        return $oData;
149
    }
150
151
    /**
152
     * Get the total number of members.
153
     *
154
     * @param string $sTable Default DbTableName::MEMBER
155
     * @param int $iDay Default '0'
156
     * @param string $sGender Values ​​available 'all', 'male', 'female'. 'couple' is only available to Members. Default 'all'
157
     *
158
     * @return int Total Users
159
     */
160
    public function total($sTable = DbTableName::MEMBER, $iDay = 0, $sGender = 'all')
161
    {
162
        Various::checkModelTable($sTable);
163
164
        $iDay = (int)$iDay;
165
166
        $bIsDay = ($iDay > 0);
167
        $bIsGender = ($sTable === DbTableName::MEMBER ? ($sGender === 'male' || $sGender === 'female' || $sGender === 'couple') : ($sGender === 'male' || $sGender === 'female'));
168
169
        $sSqlDay = $bIsDay ? ' AND (joinDate + INTERVAL :day DAY) > NOW()' : '';
170
        $sSqlGender = $bIsGender ? ' AND sex = :gender' : '';
171
172
        $rStmt = Db::getInstance()->prepare('SELECT COUNT(profileId) AS totalUsers FROM' . Db::prefix($sTable) . 'WHERE username <> \'' . PH7_GHOST_USERNAME . '\'' . $sSqlDay . $sSqlGender);
173
        if ($bIsDay) {
174
            $rStmt->bindValue(':day', $iDay, \PDO::PARAM_INT);
175
        }
176
        if ($bIsGender) {
177
            $rStmt->bindValue(':gender', $sGender, \PDO::PARAM_STR);
178
        }
179
        $rStmt->execute();
180
181
        $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
182
        Db::free($rStmt);
183
184
        return (int)$oRow->totalUsers;
185
    }
186
187
    /**
188
     * Update profile data.
189
     *
190
     * @param string $sSection
191
     * @param string $sValue
192
     * @param int $iProfileId Profile ID
193
     * @param string $sTable Default DbTableName::MEMBER
194
     *
195
     * @return void
196
     */
197
    public function updateProfile($sSection, $sValue, $iProfileId, $sTable = DbTableName::MEMBER)
198
    {
199
        Various::checkModelTable($sTable);
200
201
        $this->orm->update($sTable, $sSection, $sValue, 'profileId', $iProfileId);
202
    }
203
204
    /**
205
     * Update Privacy setting data.
206
     *
207
     * @param string $sSection
208
     * @param string $sValue
209
     * @param int $iProfileId Profile ID
210
     *
211
     * @return void
212
     */
213
    public function updatePrivacySetting($sSection, $sValue, $iProfileId)
214
    {
215
        $this->orm->update(DbTableName::MEMBER_PRIVACY, $sSection, $sValue, 'profileId', $iProfileId);
216
    }
217
218
    /**
219
     * Change password of a member.
220
     *
221
     * @param string $sEmail
222
     * @param string $sNewPassword
223
     * @param string $sTable
224
     *
225
     * @return bool
226
     */
227
    public function changePassword($sEmail, $sNewPassword, $sTable)
228
    {
229
        Various::checkModelTable($sTable);
230
231
        $rStmt = Db::getInstance()->prepare('UPDATE' . Db::prefix($sTable) . 'SET password = :newPassword WHERE email = :email LIMIT 1');
232
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
233
        $rStmt->bindValue(':newPassword', Security::hashPwd($sNewPassword), \PDO::PARAM_STR);
234
235
        return $rStmt->execute();
236
    }
237
238
    /**
239
     * Set a new hash validation.
240
     *
241
     * @param int $iProfileId
242
     * @param string $sHash
243
     * @param string $sTable
244
     *
245
     * @return bool
246
     */
247
    public function setNewHashValidation($iProfileId, $sHash, $sTable)
248
    {
249
        Various::checkModelTable($sTable);
250
251
        $rStmt = Db::getInstance()->prepare('UPDATE' . Db::prefix($sTable) . 'SET hashValidation = :hash WHERE profileId = :profileId LIMIT 1');
252
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
253
        $rStmt->bindParam(':hash', $sHash, \PDO::PARAM_STR, 40);
254
255
        return $rStmt->execute();
256
    }
257
258
    /**
259
     * Check the hash validation.
260
     *
261
     * @param string $sEmail
262
     * @param string $sHash
263
     * @param string $sTable
264
     *
265
     * @return bool
266
     */
267
    public function checkHashValidation($sEmail, $sHash, $sTable)
268
    {
269
        Various::checkModelTable($sTable);
270
271
        $rStmt = Db::getInstance()->prepare('SELECT COUNT(profileId) FROM' . Db::prefix($sTable) . 'WHERE email = :email AND hashValidation = :hash LIMIT 1');
272
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
273
        $rStmt->bindParam(':hash', $sHash, \PDO::PARAM_STR, 40);
274
        $rStmt->execute();
275
276
        return $rStmt->fetchColumn() == 1;
277
    }
278
279
    /**
280
     * Search users.
281
     *
282
     * @param array $aParams
283
     * @param bool $bCount
284
     * @param int $iOffset
285
     * @param int $iLimit
286
     *
287
     * @return array|int Object for the users list returned or integer for the total number users returned.
288
     */
289
    public function search(array $aParams, $bCount, $iOffset, $iLimit)
290
    {
291
        $bCount = (bool)$bCount;
292
        $iOffset = (int)$iOffset;
293
        $iLimit = (int)$iLimit;
294
295
        $bIsMail = !empty($aParams[SearchQueryCore::EMAIL]) && Str::noSpaces($aParams[SearchQueryCore::EMAIL]);
296
        $bIsFirstName = !$bIsMail && !empty($aParams[SearchQueryCore::FIRST_NAME]) && Str::noSpaces($aParams[SearchQueryCore::FIRST_NAME]);
297
        $bIsMiddleName = !$bIsMail && !empty($aParams[SearchQueryCore::MIDDLE_NAME]) && Str::noSpaces($aParams[SearchQueryCore::MIDDLE_NAME]);
298
        $bIsLastName = !$bIsMail && !empty($aParams[SearchQueryCore::LAST_NAME]) && Str::noSpaces($aParams[SearchQueryCore::LAST_NAME]);
299
        $bIsSingleAge = !$bIsMail && !empty($aParams[SearchQueryCore::AGE]);
300
        $bIsAge = !$bIsMail && empty($aParams[SearchQueryCore::AGE]) && !empty($aParams[SearchQueryCore::MIN_AGE]) && !empty($aParams[SearchQueryCore::MAX_AGE]);
301
        $bIsHeight = !$bIsMail && !empty($aParams[SearchQueryCore::HEIGHT]);
302
        $bIsWeight = !$bIsMail && !empty($aParams[SearchQueryCore::WEIGHT]);
303
        $bIsCountry = !$bIsMail && !empty($aParams[SearchQueryCore::COUNTRY]) && Str::noSpaces($aParams[SearchQueryCore::COUNTRY]);
304
        $bIsCity = !$bIsMail && !empty($aParams[SearchQueryCore::CITY]) && Str::noSpaces($aParams[SearchQueryCore::CITY]);
305
        $bIsState = !$bIsMail && !empty($aParams[SearchQueryCore::STATE]) && Str::noSpaces($aParams[SearchQueryCore::STATE]);
306
        $bIsZipCode = !$bIsMail && !empty($aParams[SearchQueryCore::ZIP_CODE]) && Str::noSpaces($aParams[SearchQueryCore::ZIP_CODE]);
307
        $bIsSex = !$bIsMail && !empty($aParams[SearchQueryCore::SEX]) && is_array($aParams[SearchQueryCore::SEX]);
308
        $bIsMatchSex = !$bIsMail && !empty($aParams[SearchQueryCore::MATCH_SEX]);
309
        $bIsOnline = !$bIsMail && !empty($aParams[SearchQueryCore::ONLINE]);
310
        $bIsAvatar = !$bIsMail && !empty($aParams[SearchQueryCore::AVATAR]);
311
        $bHideUserLogged = !$bIsMail && !empty($this->iProfileId);
312
313
        $sSqlLimit = !$bCount ? 'LIMIT :offset, :limit' : '';
314
        $sSqlSelect = !$bCount ? '*' : 'COUNT(m.profileId) AS totalUsers';
315
        $sSqlFirstName = $bIsFirstName ? ' AND firstName = :firstName' : '';
316
        $sSqlMiddleName = $bIsMiddleName ? ' AND middleName = :middleName' : '';
317
        $sSqlLastName = $bIsLastName ? ' AND lastName = :lastName' : '';
318
        $sSqlSingleAge = $bIsSingleAge ? ' AND birthDate LIKE :birthDate ' : '';
319
        $sSqlAge = $bIsAge ? ' AND birthDate BETWEEN DATE_SUB(\'' . $this->sCurrentDate . '\', INTERVAL :age2 YEAR) AND DATE_SUB(\'' . $this->sCurrentDate . '\', INTERVAL :age1 YEAR) ' : '';
320
        $sSqlHeight = $bIsHeight ? ' AND height = :height ' : '';
321
        $sSqlWeight = $bIsWeight ? ' AND weight = :weight ' : '';
322
        $sSqlCountry = $bIsCountry ? ' AND country = :country ' : '';
323
        $sSqlCity = $bIsCity ? ' AND city LIKE :city ' : '';
324
        $sSqlState = $bIsState ? ' AND state LIKE :state ' : '';
325
        $sSqlZipCode = $bIsZipCode ? ' AND zipCode LIKE :zipCode ' : '';
326
        $sSqlEmail = $bIsMail ? ' AND email LIKE :email ' : '';
327
        $sSqlOnline = $bIsOnline ? ' AND userStatus = :userStatus AND lastActivity > DATE_SUB(\'' . $this->sCurrentDate . '\', INTERVAL ' . DbConfig::getSetting('userTimeout') . ' MINUTE) ' : '';
328
        $sSqlAvatar = $bIsAvatar ? $this->getUserWithAvatarOnlySql() : '';
329
        $sSqlHideLoggedProfile = $bHideUserLogged ? ' AND (m.profileId <> :profileId)' : '';
330
331
        if (empty($aParams[SearchQueryCore::ORDER])) {
332
            $aParams[SearchQueryCore::ORDER] = SearchCoreModel::LATEST; // Default is "ORDER BY joinDate"
333
        }
334
335
        if (empty($aParams[SearchQueryCore::SORT])) {
336
            $aParams[SearchQueryCore::SORT] = SearchCoreModel::ASC; // Default is "ascending"
337
        }
338
339
        $sSqlOrder = SearchCoreModel::order($aParams[SearchQueryCore::ORDER], $aParams[SearchQueryCore::SORT]);
340
341
        $sSqlMatchSex = $bIsMatchSex ? ' AND matchSex LIKE :matchSex ' : '';
342
343
        if ($bIsSex) {
344
            $sGender = '';
345
            $aSex = $aParams[SearchQueryCore::SEX];
346
            foreach ($aSex as $sSex) {
347
                if ($sSex === 'male') {
348
                    $sGender .= '\'male\',';
349
                }
350
351
                if ($sSex === 'female') {
352
                    $sGender .= '\'female\',';
353
                }
354
355
                if ($sSex === 'couple') {
356
                    $sGender .= '\'couple\',';
357
                }
358
            }
359
360
            $sSqlSex = ' AND sex IN (' . rtrim($sGender, ',') . ') ';
361
        } else {
362
            $sSqlSex = '';
363
        }
364
365
        $rStmt = Db::getInstance()->prepare(
366
            'SELECT ' . $sSqlSelect . ' FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m LEFT JOIN' . Db::prefix(DbTableName::MEMBER_PRIVACY) . 'AS p USING(profileId)
367
            LEFT JOIN' . Db::prefix(DbTableName::MEMBER_INFO) . 'AS i USING(profileId) WHERE username <> \'' . PH7_GHOST_USERNAME . '\' AND searchProfile = \'yes\'
368
            AND (groupId <> 1) AND (groupId <> 9) AND (ban = 0)' . $sSqlHideLoggedProfile . $sSqlFirstName . $sSqlMiddleName . $sSqlLastName . $sSqlMatchSex . $sSqlSex . $sSqlSingleAge . $sSqlAge . $sSqlCountry . $sSqlCity . $sSqlState .
369
            $sSqlZipCode . $sSqlHeight . $sSqlWeight . $sSqlEmail . $sSqlOnline . $sSqlAvatar . $sSqlOrder . $sSqlLimit
370
        );
371
372
        if ($bIsMatchSex) {
373
            $rStmt->bindValue(':matchSex', '%' . $aParams[SearchQueryCore::MATCH_SEX] . '%', \PDO::PARAM_STR);
374
        }
375
        if ($bIsFirstName) {
376
            $rStmt->bindValue(':firstName', $aParams[SearchQueryCore::FIRST_NAME], \PDO::PARAM_STR);
377
        }
378
        if ($bIsMiddleName) {
379
            $rStmt->bindValue(':middleName', $aParams[SearchQueryCore::MIDDLE_NAME], \PDO::PARAM_STR);
380
        }
381
        if ($bIsLastName) {
382
            $rStmt->bindValue(':lastName', $aParams[SearchQueryCore::LAST_NAME], \PDO::PARAM_STR);
383
        }
384
        if ($bIsSingleAge) {
385
            $rStmt->bindValue(':birthDate', '%' . $aParams[SearchQueryCore::AGE] . '%', \PDO::PARAM_STR);
386
        }
387
        if ($bIsAge) {
388
            $rStmt->bindValue(':age1', $aParams[SearchQueryCore::MIN_AGE], \PDO::PARAM_INT);
389
        }
390
        if ($bIsAge) {
391
            $rStmt->bindValue(':age2', $aParams[SearchQueryCore::MAX_AGE], \PDO::PARAM_INT);
392
        }
393
        if ($bIsHeight) {
394
            $rStmt->bindValue(':height', $aParams[SearchQueryCore::HEIGHT], \PDO::PARAM_INT);
395
        }
396
        if ($bIsWeight) {
397
            $rStmt->bindValue(':weight', $aParams[SearchQueryCore::WEIGHT], \PDO::PARAM_INT);
398
        }
399
        if ($bIsCountry) {
400
            $rStmt->bindParam(':country', $aParams[SearchQueryCore::COUNTRY], \PDO::PARAM_STR, 2);
401
        }
402
        if ($bIsCity) {
403
            $rStmt->bindValue(':city', '%' . str_replace('-', ' ', $aParams[SearchQueryCore::CITY]) . '%', \PDO::PARAM_STR);
404
        }
405
        if ($bIsState) {
406
            $rStmt->bindValue(':state', '%' . str_replace('-', ' ', $aParams[SearchQueryCore::STATE]) . '%', \PDO::PARAM_STR);
407
        }
408
        if ($bIsZipCode) {
409
            $rStmt->bindValue(':zipCode', '%' . $aParams[SearchQueryCore::ZIP_CODE] . '%', \PDO::PARAM_STR);
410
        }
411
        if ($bIsMail) {
412
            $rStmt->bindValue(':email', '%' . $aParams[SearchQueryCore::EMAIL] . '%', \PDO::PARAM_STR);
413
        }
414
        if ($bIsOnline) {
415
            $rStmt->bindValue(':userStatus', self::ONLINE_STATUS, \PDO::PARAM_INT);
416
        }
417
        if ($bHideUserLogged) {
418
            $rStmt->bindValue(':profileId', $this->iProfileId, \PDO::PARAM_INT);
419
        }
420
        if (!$bCount) {
421
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
422
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
423
        }
424
425
        $rStmt->execute();
426
427
        if (!$bCount) {
428
            $aRow = $rStmt->fetchAll(\PDO::FETCH_OBJ);
429
            Db::free($rStmt);
430
431
            return $aRow;
432
        }
433
434
        $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
435
        Db::free($rStmt);
436
437
        return (int)$oRow->totalUsers;
438
    }
439
440
    /**
441
     * Check online status.
442
     *
443
     * @param int $iProfileId
444
     * @param int $iTime Number of minutes that a member becomes inactive (offline). Default 1 minute
445
     *
446
     * @return bool
447
     */
448
    public function isOnline($iProfileId, $iTime = 1)
449
    {
450
        $iProfileId = (int)$iProfileId;
451
        $iTime = (int)$iTime;
452
453
        $rStmt = Db::getInstance()->prepare('SELECT profileId FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = :profileId
454
            AND userStatus = :userStatus AND lastActivity >= DATE_SUB(:currentTime, INTERVAL :time MINUTE) LIMIT 1');
455
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
456
        $rStmt->bindValue(':userStatus', self::ONLINE_STATUS, \PDO::PARAM_INT);
457
        $rStmt->bindValue(':time', $iTime, \PDO::PARAM_INT);
458
        $rStmt->bindValue(':currentTime', $this->sCurrentDate, \PDO::PARAM_STR);
459
        $rStmt->execute();
460
461
        return $rStmt->rowCount() === 1;
462
    }
463
464
    /**
465
     * Set the user status.
466
     *
467
     * @param int iProfileId
468
     * @param int $iStatus Values: 0 = Offline, 1 = Online, 2 = Busy, 3 = Away
469
     *
470
     * @return void
471
     */
472
    public function setUserStatus($iProfileId, $iStatus)
473
    {
474
        $this->orm->update(DbTableName::MEMBER, 'userStatus', $iStatus, 'profileId', $iProfileId);
475
    }
476
477
    /**
478
     * Get the user status.
479
     *
480
     * @param int $iProfileId
481
     *
482
     * @return int The user status. 0 = Offline, 1 = Online, 2 = Busy, 3 = Away
483
     */
484
    public function getUserStatus($iProfileId)
485
    {
486
        $this->cache->start(self::CACHE_GROUP, 'userStatus' . $iProfileId, static::CACHE_TIME);
487
488
        if (!$iUserStatus = $this->cache->get()) {
489
            $rStmt = Db::getInstance()->prepare('SELECT userStatus FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = :profileId LIMIT 1');
490
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
491
            $rStmt->execute();
492
            $iUserStatus = (int)$rStmt->fetchColumn();
493
            Db::free($rStmt);
494
495
            $this->cache->put($iUserStatus);
496
        }
497
498
        return $iUserStatus;
499
    }
500
501
    /**
502
     * Update the notifications.
503
     *
504
     * @param string $sSection
505
     * @param string $sValue
506
     * @param int $iProfileId Profile ID
507
     *
508
     * @return void
509
     */
510
    public function setNotification($sSection, $sValue, $iProfileId)
511
    {
512
        $this->orm->update(DbTableName::MEMBER_NOTIFICATION, $sSection, $sValue, 'profileId', $iProfileId);
513
    }
514
515
    /**
516
     * Get the user notifications.
517
     *
518
     * @param int $iProfileId
519
     *
520
     * @return stdClass
521
     */
522
    public function getNotification($iProfileId)
523
    {
524
        $this->cache->start(self::CACHE_GROUP, 'notification' . $iProfileId, static::CACHE_TIME);
525
526
        if (!$oData = $this->cache->get()) {
527
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix(DbTableName::MEMBER_NOTIFICATION) . 'WHERE profileId = :profileId LIMIT 1');
528
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
529
            $rStmt->execute();
530
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
531
            Db::free($rStmt);
532
            $this->cache->put($oData);
533
        }
534
535
        return $oData;
536
    }
537
538
    /**
539
     * Check notifications.
540
     *
541
     * @param int $iProfileId
542
     * @param string $sNotifName Notification name.
543
     *
544
     * @return bool Returns TRUE if the notification is wanted, FALSE otherwise.
545
     */
546
    public function isNotification($iProfileId, $sNotifName)
547
    {
548
        $this->cache->start(self::CACHE_GROUP, 'isNotification' . $iProfileId, static::CACHE_TIME);
549
550
        if (!$bData = $this->cache->get()) {
551
            $sSql = 'SELECT ' . $sNotifName . ' FROM' . Db::prefix(DbTableName::MEMBER_NOTIFICATION) .
552
                'WHERE profileId = :profileId AND ' . $sNotifName . ' = 1 LIMIT 1';
553
554
            $rStmt = Db::getInstance()->prepare($sSql);
555
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
556
            $rStmt->execute();
557
            $bData = ($rStmt->rowCount() === 1);
558
            Db::free($rStmt);
559
            $this->cache->put($bData);
0 ignored issues
show
Documentation introduced by
$bData is of type boolean, but the function expects a string.

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...
560
        }
561
562
        return $bData;
563
    }
564
565
    /**
566
     * Set the last activity of a user.
567
     *
568
     * @param int $iProfileId
569
     * @param string $sTable Default DbTableName::MEMBER
570
     *
571
     * @return void
572
     */
573
    public function setLastActivity($iProfileId, $sTable = DbTableName::MEMBER)
574
    {
575
        Various::checkModelTable($sTable);
576
577
        $this->orm->update($sTable, 'lastActivity', $this->sCurrentDate, 'profileId', $iProfileId);
578
    }
579
580
    /**
581
     * Set the last edit account of a user.
582
     *
583
     * @param int $iProfileId
584
     * @param string $sTable Default DbTableName::MEMBER
585
     *
586
     * @return void
587
     */
588
    public function setLastEdit($iProfileId, $sTable = DbTableName::MEMBER)
589
    {
590
        Various::checkModelTable($sTable);
591
592
        $this->orm->update($sTable, 'lastEdit', $this->sCurrentDate, 'profileId', $iProfileId);
593
    }
594
595
    /**
596
     * Approve a profile.
597
     *
598
     * @param int $iProfileId
599
     * @param int $iStatus 1 = apprved | 0 = not approved
600
     * @param string $sTable Default DbTableName::MEMBER
601
     *
602
     * @return void
603
     */
604
    public function approve($iProfileId, $iStatus, $sTable = DbTableName::MEMBER)
605
    {
606
        Various::checkModelTable($sTable);
607
608
        $this->orm->update($sTable, 'active', $iStatus, 'profileId', $iProfileId);
609
    }
610
611
    /**
612
     * Get member data. The hash of course but also some useful data for sending the activation email. (hash, email, username, firstName).
613
     *
614
     * @param string $sEmail User's email address.
615
     * @param string $sTable Default DbTableName::MEMBER
616
     *
617
     * @return stdClass|bool Returns the data member (email, username, firstName, hashValidation) on success, otherwise returns false if there is an error.
618
     */
619
    public function getHashValidation($sEmail, $sTable = DbTableName::MEMBER)
620
    {
621
        Various::checkModelTable($sTable);
622
623
        $rStmt = Db::getInstance()->prepare('SELECT email, username, firstName, hashValidation FROM' . Db::prefix($sTable) . 'WHERE email = :email AND active = 2');
624
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
625
        $rStmt->execute();
626
        $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
627
        Db::free($rStmt);
628
629
        return $oRow;
630
    }
631
632
    /**
633
     * Valid on behalf of a user with the hash.
634
     *
635
     * @param string $sEmail
636
     * @param string $sHash
637
     * @param string $sTable Default DbTableName::MEMBER
638
     *
639
     * @return bool
640
     */
641
    public function validateAccount($sEmail, $sHash, $sTable = DbTableName::MEMBER)
642
    {
643
        Various::checkModelTable($sTable);
644
645
        $rStmt = Db::getInstance()->prepare('UPDATE' . Db::prefix($sTable) . 'SET active = 1 WHERE email = :email AND hashValidation = :hash AND active = 2');
646
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
647
        $rStmt->bindParam(':hash', $sHash, \PDO::PARAM_STR, 40);
648
649
        return $rStmt->execute();
650
    }
651
652
    /**
653
     * Adding a User.
654
     *
655
     * @param array $aData
656
     *
657
     * @return int The ID of the User.
658
     */
659
    public function add(array $aData)
660
    {
661
        $sHashValidation = (!empty($aData['hash_validation']) ? $aData['hash_validation'] : null);
662
663
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER) . '(email, username, password, firstName, lastName, sex, matchSex, birthDate, active, ip, hashValidation, joinDate, lastActivity)
664
            VALUES (:email, :username, :password, :firstName, :lastName, :sex, :matchSex, :birthDate, :active, :ip, :hashValidation, :joinDate, :lastActivity)');
665
        $rStmt->bindValue(':email', trim($aData['email']), \PDO::PARAM_STR);
666
        $rStmt->bindValue(':username', trim($aData['username']), \PDO::PARAM_STR);
667
        $rStmt->bindValue(':password', Security::hashPwd($aData['password']), \PDO::PARAM_STR);
668
        $rStmt->bindValue(':firstName', $aData['first_name'], \PDO::PARAM_STR);
669
        $rStmt->bindValue(':lastName', $aData['last_name'], \PDO::PARAM_STR);
670
        $rStmt->bindValue(':sex', $aData['sex'], \PDO::PARAM_STR);
671
        $rStmt->bindValue(':matchSex', Form::setVal($aData['match_sex']), \PDO::PARAM_STR);
672
        $rStmt->bindValue(':birthDate', $aData['birth_date'], \PDO::PARAM_STR);
673
        $rStmt->bindValue(':active', (!empty($aData['is_active']) ? $aData['is_active'] : 1), \PDO::PARAM_INT);
674
        $rStmt->bindValue(':ip', $aData['ip'], \PDO::PARAM_STR);
675
        $rStmt->bindParam(':hashValidation', $sHashValidation, \PDO::PARAM_STR, 40);
676
        $rStmt->bindValue(':joinDate', $this->sCurrentDate, \PDO::PARAM_STR);
677
        $rStmt->bindValue(':lastActivity', $this->sCurrentDate, \PDO::PARAM_STR);
678
        $rStmt->execute();
679
        $this->setKeyId(Db::getInstance()->lastInsertId()); // Set the user's ID
680
        Db::free($rStmt);
681
        $this->setInfoFields($aData);
682
        $this->setDefaultPrivacySetting();
683
        $this->setDefaultNotification();
684
685
        // Last one, update the membership with the correct details
686
        $this->updateMembership(
687
            (int)DbConfig::getSetting('defaultMembershipGroupId'),
688
            $this->getKeyId(),
689
            $this->sCurrentDate
690
        );
691
692
        return $this->getKeyId();
693
    }
694
695
    /**
696
     * @param array $aData
697
     *
698
     * @return bool
699
     */
700
    public function setInfoFields(array $aData)
701
    {
702
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER_INFO) . '(profileId, middleName, country, city, state, zipCode, description, website, socialNetworkSite)
703
            VALUES (:profileId, :middleName, :country, :city, :state, :zipCode, :description, :website, :socialNetworkSite)');
704
        $rStmt->bindValue(':profileId', $this->getKeyId(), \PDO::PARAM_INT);
705
        $rStmt->bindValue(':middleName', (!empty($aData['middle_name']) ? $aData['middle_name'] : ''), \PDO::PARAM_STR);
706
        $rStmt->bindValue(':country', (!empty($aData['country']) ? $aData['country'] : ''), \PDO::PARAM_STR);
707
        $rStmt->bindValue(':city', (!empty($aData['city']) ? $aData['city'] : ''), \PDO::PARAM_STR);
708
        $rStmt->bindValue(':state', (!empty($aData['state']) ? $aData['state'] : ''), \PDO::PARAM_STR);
709
        $rStmt->bindValue(':zipCode', (!empty($aData['zip_code']) ? $aData['zip_code'] : ''), \PDO::PARAM_STR);
710
        $rStmt->bindValue(':description', (!empty($aData['description']) ? $aData['description'] : ''), \PDO::PARAM_STR);
711
        $rStmt->bindValue(':website', (!empty($aData['website']) ? trim($aData['website']) : ''), \PDO::PARAM_STR);
712
        $rStmt->bindValue(':socialNetworkSite', (!empty($aData['social_network_site']) ? trim($aData['social_network_site']) : ''), \PDO::PARAM_STR);
713
714
        return $rStmt->execute();
715
    }
716
717
    /**
718
     * Set the default privacy settings.
719
     *
720
     * @return bool Returns TRUE on success or FALSE on failure.
721
     */
722
    public function setDefaultPrivacySetting()
723
    {
724
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER_PRIVACY) .
725
            '(profileId, privacyProfile, searchProfile, userSaveViews)
726
            VALUES (:profileId, \'all\', \'yes\', \'yes\')');
727
        $rStmt->bindValue(':profileId', $this->getKeyId(), \PDO::PARAM_INT);
728
        return $rStmt->execute();
729
    }
730
731
    /**
732
     * Set the default notifications.
733
     *
734
     * @return bool Returns TRUE on success or FALSE on failure.
735
     */
736
    public function setDefaultNotification()
737
    {
738
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER_NOTIFICATION) .
739
            '(profileId, enableNewsletters, newMsg, friendRequest)
740
            VALUES (:profileId, 1, 1, 1)');
741
        $rStmt->bindValue(':profileId', $this->getKeyId(), \PDO::PARAM_INT);
742
        return $rStmt->execute();
743
    }
744
745
    /**
746
     * To avoid flooding!
747
     * Waiting time before a new registration with the same IP address.
748
     *
749
     * @param string $sIp
750
     * @param int $iWaitTime In minutes!
751
     * @param string $sCurrentTime In date format: 0000-00-00 00:00:00
752
     * @param string $sTable Default DbTableName::MEMBER
753
     *
754
     * @return bool Return TRUE if the weather was fine, FALSE otherwise.
755
     */
756
    public function checkWaitJoin($sIp, $iWaitTime, $sCurrentTime, $sTable = DbTableName::MEMBER)
757
    {
758
        Various::checkModelTable($sTable);
759
760
        $rStmt = Db::getInstance()->prepare('SELECT profileId FROM' . Db::prefix($sTable) .
761
            'WHERE ip = :ip AND DATE_ADD(joinDate, INTERVAL :waitTime MINUTE) > :currentTime LIMIT 1');
762
        $rStmt->bindValue(':ip', $sIp, \PDO::PARAM_STR);
763
        $rStmt->bindValue(':waitTime', $iWaitTime, \PDO::PARAM_INT);
764
        $rStmt->bindValue(':currentTime', $sCurrentTime, \PDO::PARAM_STR);
765
        $rStmt->execute();
766
767
        return $rStmt->rowCount() === 0;
768
    }
769
770
771
    /********** AVATAR **********/
772
773
    /**
774
     * Update or add a new avatar.
775
     *
776
     * @param int $iProfileId
777
     * @param string $sAvatar
778
     * @param int $iApproved
779
     *
780
     * @return bool
781
     */
782
    public function setAvatar($iProfileId, $sAvatar, $iApproved)
783
    {
784
        $rStmt = Db::getInstance()->prepare('UPDATE' . Db::prefix(DbTableName::MEMBER) . 'SET avatar = :avatar, approvedAvatar = :approved WHERE profileId = :profileId');
785
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
786
        $rStmt->bindValue(':avatar', $sAvatar, \PDO::PARAM_STR);
787
        $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_INT);
788
789
        return $rStmt->execute();
790
    }
791
792
    /**
793
     * Get avatar.
794
     *
795
     * @param int $iProfileId
796
     * @param string|null $iApproved (1 = approved | 0 = pending | NULL = approved and pending)
797
     *
798
     * @return stdClass The Avatar (SQL alias is pic), profileId and approvedAvatar
799
     */
800
    public function getAvatar($iProfileId, $iApproved = null)
801
    {
802
        $this->cache->start(self::CACHE_GROUP, 'avatar' . $iProfileId, static::CACHE_TIME);
803
804
        if (!$oData = $this->cache->get()) {
805
            $bIsApproved = $iApproved !== null;
806
807
            $sSqlApproved = $bIsApproved ? ' AND approvedAvatar = :approved ' : ' ';
808
            $rStmt = Db::getInstance()->prepare('SELECT profileId, avatar AS pic, approvedAvatar FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = :profileId' . $sSqlApproved . 'LIMIT 1');
809
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
810
            if ($bIsApproved) {
811
                $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_STR);
812
            }
813
            $rStmt->execute();
814
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
815
            Db::free($rStmt);
816
            $this->cache->put($oData);
817
        }
818
819
        return $oData;
820
    }
821
822
    /**
823
     * Delete an avatar in the database.
824
     *
825
     * @param int $iProfileId
826
     *
827
     * @return bool
828
     */
829
    public function deleteAvatar($iProfileId)
830
    {
831
        return $this->setAvatar($iProfileId, null, 1);
832
    }
833
834
835
    /********** BACKGROUND **********/
836
837
    /**
838
     * Get file of a user background.
839
     *
840
     * @param int $iProfileId
841
     * @param int|null $iApproved (1 = approved | 0 = pending | NULL = approved and pending) Default NULL
842
     *
843
     * @return string
844
     */
845
    public function getBackground($iProfileId, $iApproved = null)
846
    {
847
        $this->cache->start(self::CACHE_GROUP, 'background' . $iProfileId, static::CACHE_TIME);
848
849
        if (!$sFile = $this->cache->get()) {
850
            $bIsApproved = $iApproved !== null;
851
852
            $sSqlApproved = $bIsApproved ? ' AND approved = :approved ' : ' ';
853
            $rStmt = Db::getInstance()->prepare('SELECT file FROM' . Db::prefix(DbTableName::MEMBER_BACKGROUND) . 'WHERE profileId = :profileId' . $sSqlApproved . 'LIMIT 1');
854
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
855
            if ($bIsApproved) {
856
                $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_STR);
857
            }
858
            $rStmt->execute();
859
            $sFile = $rStmt->fetchColumn();
860
            Db::free($rStmt);
861
862
            $this->cache->put($sFile);
863
        }
864
865
        return $sFile;
866
    }
867
868
    /**
869
     * Add profile background.
870
     *
871
     * @param int $iProfileId
872
     * @param string $sFile
873
     * @param int $iApproved
874
     *
875
     * @return bool
876
     */
877
    public function addBackground($iProfileId, $sFile, $iApproved = 1)
878
    {
879
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER_BACKGROUND) . '(profileId, file, approved) VALUES (:profileId, :file, :approved)');
880
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
881
        $rStmt->bindValue(':file', $sFile, \PDO::PARAM_STR);
882
        $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_STR);
883
884
        return $rStmt->execute();
885
    }
886
887
    /**
888
     * Delete profile background.
889
     *
890
     * @param int $iProfileId
891
     *
892
     * @return bool
893
     */
894
    public function deleteBackground($iProfileId)
895
    {
896
        $rStmt = Db::getInstance()->prepare('DELETE FROM' . Db::prefix(DbTableName::MEMBER_BACKGROUND) . 'WHERE profileId = :profileId');
897
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
898
        return $rStmt->execute();
899
    }
900
901
    /**
902
     * Delete User.
903
     *
904
     * @param int $iProfileId
905
     * @param string $sUsername
906
     *
907
     * @return void
908
     */
909
    public function delete($iProfileId, $sUsername)
910
    {
911
        $sUsername = (string)$sUsername;
912
        $iProfileId = (int)$iProfileId;
913
914
        if ($sUsername === PH7_GHOST_USERNAME) {
915
            exit('You cannot delete this profile!');
0 ignored issues
show
Coding Style Compatibility introduced by
The method delete() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
916
        }
917
918
        $oDb = Db::getInstance();
919
920
        // DELETE MESSAGES
921
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MESSAGE) . 'WHERE sender = ' . $iProfileId);
922
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MESSAGE) . 'WHERE recipient = ' . $iProfileId);
923
924
        // DELETE MESSAGES OF MESSENGER
925
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MESSENGER) . 'WHERE fromUser = ' . Db::getInstance()->quote($sUsername));
926
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MESSENGER) . 'WHERE toUser = ' . Db::getInstance()->quote($sUsername));
927
928
        // DELETE PROFILE COMMENTS
929
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_PROFILE) . 'WHERE sender = ' . $iProfileId);
930
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_PROFILE) . 'WHERE recipient = ' . $iProfileId);
931
932
        // DELETE PICTURE COMMENTS
933
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_PICTURE) . 'WHERE sender = ' . $iProfileId);
934
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_PICTURE) . 'WHERE recipient = ' . $iProfileId);
935
936
        // DELETE VIDEO COMMENTS
937
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_VIDEO) . 'WHERE sender = ' . $iProfileId);
938
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_VIDEO) . 'WHERE recipient = ' . $iProfileId);
939
940
        // DELETE NOTE COMMENTS
941
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_NOTE) . 'WHERE sender = ' . $iProfileId);
942
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_NOTE) . 'WHERE recipient = ' . $iProfileId);
943
944
        // DELETE BLOG COMMENTS
945
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_BLOG) . 'WHERE sender = ' . $iProfileId);
946
947
        // DELETE GAME COMMENTS
948
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_GAME) . 'WHERE sender = ' . $iProfileId);
949
950
        // DELETE PICTURES ALBUMS AND PICTURES
951
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::PICTURE) . 'WHERE profileId = ' . $iProfileId);
952
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::ALBUM_PICTURE) . 'WHERE profileId = ' . $iProfileId);
953
954
        // DELETE VIDEOS ALBUMS AND VIDEOS
955
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::VIDEO) . 'WHERE profileId = ' . $iProfileId);
956
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::ALBUM_VIDEO) . 'WHERE profileId = ' . $iProfileId);
957
958
        // DELETE FRIENDS
959
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_FRIEND) . 'WHERE profileId = ' . $iProfileId);
960
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_FRIEND) . 'WHERE friendId = ' . $iProfileId);
961
962
        // DELETE WALL
963
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_WALL) . 'WHERE profileId = ' . $iProfileId);
964
965
        // DELETE BACKGROUND
966
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_BACKGROUND) . 'WHERE profileId = ' . $iProfileId);
967
968
        // DELETE NOTES
969
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::NOTE_CATEGORY) . 'WHERE profileId = ' . $iProfileId);
970
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::NOTE) . 'WHERE profileId = ' . $iProfileId);
971
972
        // DELETE LIKE
973
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::LIKE) . 'WHERE keyId LIKE ' . Db::getInstance()->quote('%' . $sUsername . '.html'));
974
975
        // DELETE PROFILE VISITS
976
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_WHO_VIEW) . 'WHERE profileId = ' . $iProfileId);
977
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_WHO_VIEW) . 'WHERE visitorId = ' . $iProfileId);
978
979
        // DELETE REPORT
980
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::REPORT) . 'WHERE spammerId = ' . $iProfileId);
981
982
        // DELETE TOPICS of FORUMS
983
        /*
984
        No! Ghost Profile is ultimately the best solution!
985
        WARNING: Do not change this part of code without asking permission from Pierre-Henry Soria
986
        */
987
        //$oDb->exec('DELETE FROM' . Db::prefix(DbTableName::FORUM_MESSAGE) . 'WHERE profileId = ' . $iProfileId);
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
988
        //$oDb->exec('DELETE FROM' . Db::prefix(DbTableName::FORUM_TOPIC) . 'WHERE profileId = ' . $iProfileId);
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
989
990
        // DELETE NOTIFICATIONS
991
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_NOTIFICATION) . 'WHERE profileId = ' . $iProfileId . ' LIMIT 1');
992
993
        // DELETE PRIVACY SETTINGS
994
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_PRIVACY) . 'WHERE profileId = ' . $iProfileId . ' LIMIT 1');
995
996
        // DELETE INFO FIELDS
997
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_INFO) . 'WHERE profileId = ' . $iProfileId . ' LIMIT 1');
998
999
        // DELETE USER
1000
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = ' . $iProfileId . ' LIMIT 1');
1001
1002
        unset($oDb); // Destruction of the object
1003
    }
1004
1005
    /**
1006
     * @param string $sUsernameSearch
1007
     * @param string $sTable Default DbTableName::MEMBER
1008
     *
1009
     * @return array data of users (profileId, username, sex)
1010
     */
1011
    public function getUsernameList($sUsernameSearch, $sTable = DbTableName::MEMBER)
1012
    {
1013
        Various::checkModelTable($sTable);
1014
1015
        $rStmt = Db::getInstance()->prepare('SELECT profileId, username, sex FROM' . Db::prefix($sTable) . 'WHERE username <> \'' . PH7_GHOST_USERNAME . '\' AND username LIKE :username');
1016
        $rStmt->bindValue(':username', '%' . $sUsernameSearch . '%', \PDO::PARAM_STR);
1017
        $rStmt->execute();
1018
        $aRow = $rStmt->fetchAll(\PDO::FETCH_OBJ);
1019
        Db::free($rStmt);
1020
1021
        return $aRow;
1022
    }
1023
1024
    /**
1025
     * Get (all) profile data.
1026
     *
1027
     * @param string $sOrder
1028
     * @param int $iOffset
1029
     * @param int $iLimit
1030
     *
1031
     * @return array Data of users
1032
     */
1033
    public function getProfiles($sOrder = SearchCoreModel::LAST_ACTIVITY, $iOffset = null, $iLimit = null)
1034
    {
1035
        $bIsLimit = $iOffset !== null && $iLimit !== null;
1036
        $bHideUserLogged = !empty($this->iProfileId);
1037
        $bOnlyAvatarsSet = (bool)DbConfig::getSetting('profileWithAvatarSet');
1038
1039
        $iOffset = (int)$iOffset;
1040
        $iLimit = (int)$iLimit;
1041
1042
        $sOrder = SearchCoreModel::order($sOrder, SearchCoreModel::DESC);
1043
1044
        $sSqlLimit = $bIsLimit ? 'LIMIT :offset, :limit' : '';
1045
        $sSqlHideLoggedProfile = $bHideUserLogged ? ' AND (m.profileId <> :profileId)' : '';
1046
        $sSqlShowOnlyWithAvatars = $bOnlyAvatarsSet ? $this->getUserWithAvatarOnlySql() : '';
1047
1048
        $rStmt = Db::getInstance()->prepare(
1049
            'SELECT * FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m LEFT JOIN' . Db::prefix(DbTableName::MEMBER_PRIVACY) . 'AS p USING(profileId)
1050
            LEFT JOIN' . Db::prefix(DbTableName::MEMBER_INFO) . 'AS i USING(profileId) WHERE (username <> \'' . PH7_GHOST_USERNAME . '\') AND (searchProfile = \'yes\')
1051
            AND (username IS NOT NULL) AND (firstName IS NOT NULL) AND (sex IS NOT NULL) AND (matchSex IS NOT NULL) AND (country IS NOT NULL)
1052
            AND (city IS NOT NULL) AND (groupId <> 1) AND (groupId <> 9) AND (ban = 0)' .
1053
            $sSqlHideLoggedProfile . $sSqlShowOnlyWithAvatars . $sOrder . $sSqlLimit
1054
        );
1055
1056
        if ($bHideUserLogged) {
1057
            $rStmt->bindValue(':profileId', $this->iProfileId, \PDO::PARAM_INT);
1058
        }
1059
1060
        if ($bIsLimit) {
1061
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
1062
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
1063
        }
1064
1065
        $rStmt->execute();
1066
        $aRow = $rStmt->fetchAll(\PDO::FETCH_OBJ);
1067
        Db::free($rStmt);
1068
1069
        return $aRow;
1070
    }
1071
1072
    /**
1073
     * Get users from the location data.
1074
     *
1075
     * @param string $sCountryCode The country code. e.g. US, CA, FR, ES, BE, NL
1076
     * @param string $sCity
1077
     * @param bool $bCount
1078
     * @param string $sOrder
1079
     * @param int $iOffset
1080
     * @param int $iLimit
1081
     *
1082
     * @return array|stdClass|int Object with the users list returned or integer for the total number users returned.
1083
     */
1084
    public function getGeoProfiles($sCountryCode, $sCity, $bCount, $sOrder, $iOffset = null, $iLimit = null)
1085
    {
1086
        $bLimit = $iOffset !== null && $iLimit !== null;
1087
1088
        $bCount = (bool)$bCount;
1089
        $iOffset = (int)$iOffset;
1090
        $iLimit = (int)$iLimit;
1091
1092
        $sOrder = !$bCount ? SearchCoreModel::order($sOrder, SearchCoreModel::DESC) : '';
1093
1094
        $sSqlLimit = (!$bCount || $bLimit) ? 'LIMIT :offset, :limit' : '';
1095
        $sSqlSelect = !$bCount ? '*' : 'COUNT(m.profileId) AS totalUsers';
1096
1097
        $sSqlCity = !empty($sCity) ? 'AND (city LIKE :city)' : '';
1098
1099
        $rStmt = Db::getInstance()->prepare(
1100
            'SELECT ' . $sSqlSelect . ' FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m LEFT JOIN' . Db::prefix(DbTableName::MEMBER_INFO) . 'AS i USING(profileId)
1101
            WHERE (username <> \'' . PH7_GHOST_USERNAME . '\') AND (country = :country) ' . $sSqlCity . ' AND (username IS NOT NULL)
1102
            AND (firstName IS NOT NULL) AND (sex IS NOT NULL) AND (matchSex IS NOT NULL) AND (country IS NOT NULL)
1103
            AND (city IS NOT NULL) AND (groupId <> 1) AND (groupId <> 9) AND (ban = 0)' . $sOrder . $sSqlLimit
1104
        );
1105
        $rStmt->bindParam(':country', $sCountryCode, \PDO::PARAM_STR, 2);
1106
1107
        if (!empty($sCity)) {
1108
            $rStmt->bindValue(':city', '%' . $sCity . '%', \PDO::PARAM_STR);
1109
        }
1110
1111
        if (!$bCount || $bLimit) {
1112
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
1113
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
1114
        }
1115
1116
        $rStmt->execute();
1117
1118
        if (!$bCount) {
1119
            $aRow = $rStmt->fetchAll(\PDO::FETCH_OBJ);
1120
            Db::free($rStmt);
1121
1122
            return $aRow;
1123
        }
1124
1125
        $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
1126
        Db::free($rStmt);
1127
1128
        return (int)$oRow->totalUsers;
1129
    }
1130
1131
    /**
1132
     * Updating the privacy settings.
1133
     *
1134
     * @param int $iProfileId
1135
     *
1136
     * @return stdClass
1137
     */
1138
    public function getPrivacySetting($iProfileId)
1139
    {
1140
        $this->cache->start(self::CACHE_GROUP, 'privacySetting' . $iProfileId, static::CACHE_TIME);
1141
1142
        if (!$oData = $this->cache->get()) {
1143
            $iProfileId = (int)$iProfileId;
1144
1145
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix(DbTableName::MEMBER_PRIVACY) . 'WHERE profileId = :profileId LIMIT 1');
1146
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1147
            $rStmt->execute();
1148
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
1149
            Db::free($rStmt);
1150
            $this->cache->put($oData);
1151
        }
1152
1153
        return $oData;
1154
    }
1155
1156
    /**
1157
     * Get the Profile ID of a user.
1158
     *
1159
     * @param string $sEmail Default NULL
1160
     * @param string $sUsername Default NULL
1161
     * @param string $sTable Default DbTableName::MEMBER
1162
     *
1163
     * @return int|bool The Member ID if it is found or FALSE if not found.
1164
     */
1165
    public function getId($sEmail = null, $sUsername = null, $sTable = DbTableName::MEMBER)
1166
    {
1167
        $this->cache->start(self::CACHE_GROUP, 'id' . $sEmail . $sUsername . $sTable, static::CACHE_TIME);
1168
1169
        if (!$iProfileId = $this->cache->get()) {
1170
            Various::checkModelTable($sTable);
1171
1172
            if (!empty($sEmail)) {
1173
                $rStmt = Db::getInstance()->prepare('SELECT profileId FROM' . Db::prefix($sTable) . 'WHERE email = :email LIMIT 1');
1174
                $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
1175
            } else {
1176
                $rStmt = Db::getInstance()->prepare('SELECT profileId FROM' . Db::prefix($sTable) . 'WHERE username = :username LIMIT 1');
1177
                $rStmt->bindValue(':username', $sUsername, \PDO::PARAM_STR);
1178
            }
1179
1180
            $rStmt->execute();
1181
1182
            if ($rStmt->rowCount() === 0) {
1183
                return false;
1184
            }
1185
1186
            $iProfileId = (int)$rStmt->fetchColumn();
1187
            Db::free($rStmt);
1188
            $this->cache->put($iProfileId);
1189
        }
1190
1191
        return $iProfileId;
1192
    }
1193
1194
    /**
1195
     * @param int $iProfileId
1196
     * @param string $sTable Default DbTableName::MEMBER
1197
     *
1198
     * @return string The email address of a member
1199
     */
1200
    public function getEmail($iProfileId, $sTable = DbTableName::MEMBER)
1201
    {
1202
        $this->cache->start(self::CACHE_GROUP, 'email' . $iProfileId . $sTable, static::CACHE_TIME);
1203
1204
        if (!$sEmail = $this->cache->get()) {
1205
            Various::checkModelTable($sTable);
1206
1207
            $rStmt = Db::getInstance()->prepare('SELECT email FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1208
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1209
            $rStmt->execute();
1210
            $sEmail = $rStmt->fetchColumn();
1211
            Db::free($rStmt);
1212
1213
            $this->cache->put($sEmail);
1214
        }
1215
1216
        return $sEmail;
1217
    }
1218
1219
    /**
1220
     * Retrieves the username from the user ID.
1221
     *
1222
     * @param int $iProfileId
1223
     * @param string $sTable Default DbTableName::MEMBER
1224
     *
1225
     * @return string The Username of member
1226
     */
1227
    public function getUsername($iProfileId, $sTable = DbTableName::MEMBER)
1228
    {
1229
        if ($iProfileId === PH7_ADMIN_ID) {
1230
            return t('Administration of %site_name%');
1231
        }
1232
1233
        $this->cache->start(self::CACHE_GROUP, 'username' . $iProfileId . $sTable, static::CACHE_TIME);
1234
1235
        if (!$sUsername = $this->cache->get()) {
1236
            Various::checkModelTable($sTable);
1237
1238
            $rStmt = Db::getInstance()->prepare('SELECT username FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1239
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1240
            $rStmt->execute();
1241
            $sUsername = $rStmt->fetchColumn();
1242
            Db::free($rStmt);
1243
1244
            $this->cache->put($sUsername);
1245
        }
1246
1247
        return $sUsername;
1248
    }
1249
1250
    /**
1251
     * Retrieves the first name from the user ID.
1252
     *
1253
     * @param int $iProfileId
1254
     * @param string $sTable Default DbTableName::MEMBER
1255
     *
1256
     * @return string The first name of member
1257
     */
1258
    public function getFirstName($iProfileId, $sTable = DbTableName::MEMBER)
1259
    {
1260
        $this->cache->start(self::CACHE_GROUP, 'firstName' . $iProfileId . $sTable, static::CACHE_TIME);
1261
1262
        if (!$sFirstName = $this->cache->get()) {
1263
            Various::checkModelTable($sTable);
1264
1265
            $rStmt = Db::getInstance()->prepare('SELECT firstName FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1266
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1267
            $rStmt->execute();
1268
            $sFirstName = $rStmt->fetchColumn();
1269
            Db::free($rStmt);
1270
1271
            $this->cache->put($sFirstName);
1272
        }
1273
1274
        return $sFirstName;
1275
    }
1276
1277
    /**
1278
     * Get Gender (sex) of a user.
1279
     *
1280
     * @param int $iProfileId Default NULL
1281
     * @param string $sUsername Default NULL
1282
     * @param string $sTable Default DbTableName::MEMBER
1283
     *
1284
     * @return string The sex of a member
1285
     */
1286
    public function getSex($iProfileId = null, $sUsername = null, $sTable = DbTableName::MEMBER)
1287
    {
1288
        $this->cache->start(self::CACHE_GROUP, 'sex' . $iProfileId . $sUsername . $sTable, static::CACHE_TIME);
1289
1290
        if (!$sSex = $this->cache->get()) {
1291
            Various::checkModelTable($sTable);
1292
1293
            if (!empty($iProfileId)) {
1294
                $rStmt = Db::getInstance()->prepare('SELECT sex FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1295
                $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1296
            } else {
1297
                $rStmt = Db::getInstance()->prepare('SELECT sex FROM' . Db::prefix($sTable) . 'WHERE username=:username LIMIT 1');
1298
                $rStmt->bindValue(':username', $sUsername, \PDO::PARAM_STR);
1299
            }
1300
1301
            $rStmt->execute();
1302
            $sSex = $rStmt->fetchColumn();
1303
            Db::free($rStmt);
1304
1305
            $this->cache->put($sSex);
1306
        }
1307
1308
        return $sSex;
1309
    }
1310
1311
    /**
1312
     * Get Match sex for a member (so only from the Members table, because Affiliates and Admins don't have match sex).
1313
     *
1314
     * @param int $iProfileId
1315
     *
1316
     * @return string The User's birthdate.
1317
     */
1318
    public function getMatchSex($iProfileId)
1319
    {
1320
        $this->cache->start(self::CACHE_GROUP, 'matchsex' . $iProfileId, static::CACHE_TIME);
1321
1322
        if (!$sMatchSex = $this->cache->get()) {
1323
            $rStmt = Db::getInstance()->prepare('SELECT matchSex FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = :profileId LIMIT 1');
1324
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1325
            $rStmt->execute();
1326
            $sMatchSex = $rStmt->fetchColumn();
1327
            Db::free($rStmt);
1328
1329
            $this->cache->put($sMatchSex);
1330
        }
1331
1332
        return $sMatchSex;
1333
    }
1334
1335
    /**
1336
     * Get Birth Date of a user.
1337
     *
1338
     * @param int $iProfileId
1339
     * @param string $sTable Default DbTableName::MEMBER
1340
     *
1341
     * @return string The User's birthdate.
1342
     */
1343
    public function getBirthDate($iProfileId, $sTable = DbTableName::MEMBER)
1344
    {
1345
        $this->cache->start(self::CACHE_GROUP, 'birthdate' . $iProfileId . $sTable, static::CACHE_TIME);
1346
1347
        if (!$sBirthDate = $this->cache->get()) {
1348
            Various::checkModelTable($sTable);
1349
1350
            $rStmt = Db::getInstance()->prepare('SELECT birthDate FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1351
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1352
            $rStmt->execute();
1353
            $sBirthDate = $rStmt->fetchColumn();
1354
            Db::free($rStmt);
1355
1356
            $this->cache->put($sBirthDate);
1357
        }
1358
1359
        return $sBirthDate;
1360
    }
1361
1362
    /**
1363
     * Get user's group.
1364
     *
1365
     * @param int $iProfileId
1366
     * @param string sTable Default DbTableName::MEMBER
1367
     *
1368
     * @return int The Group ID of a member
1369
     */
1370
    public function getGroupId($iProfileId, $sTable = DbTableName::MEMBER)
1371
    {
1372
        $this->cache->start(self::CACHE_GROUP, 'groupId' . $iProfileId . $sTable, static::CACHE_TIME);
1373
1374
        if (!$iGroupId = $this->cache->get()) {
1375
            Various::checkModelTable($sTable);
1376
1377
            $rStmt = Db::getInstance()->prepare('SELECT groupId FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1378
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1379
            $rStmt->execute();
1380
            $iGroupId = (int)$rStmt->fetchColumn();
1381
            Db::free($rStmt);
1382
1383
            $this->cache->put($iGroupId);
1384
        }
1385
1386
        return $iGroupId;
1387
    }
1388
1389
    /**
1390
     * Get the membership(s) data.
1391
     *
1392
     * @param int $iGroupId Group ID. Select only the specific membership from a group ID.
1393
     *
1394
     * @return stdClass|array The membership(s) data.
1395
     */
1396
    public function getMemberships($iGroupId = null)
1397
    {
1398
        $this->cache->start(self::CACHE_GROUP, DbTableName::MEMBERSHIP . $iGroupId, static::CACHE_TIME);
1399
1400
        if (!$mData = $this->cache->get()) {
1401
            $bIsGroupId = !empty($iGroupId);
1402
            $sSqlGroup = ($bIsGroupId) ? ' WHERE groupId = :groupId ' : ' ';
1403
1404
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix(DbTableName::MEMBERSHIP) . $sSqlGroup . 'ORDER BY enable DESC, name ASC');
1405
            if (!empty($iGroupId)) $rStmt->bindValue(':groupId', $iGroupId, \PDO::PARAM_INT);
1406
            $rStmt->execute();
1407
            $mData = ($bIsGroupId) ? $rStmt->fetch(\PDO::FETCH_OBJ) : $rStmt->fetchAll(\PDO::FETCH_OBJ);
1408
            Db::free($rStmt);
1409
            $this->cache->put($mData);
1410
        }
1411
1412
        return $mData;
1413
    }
1414
1415
    /**
1416
     * Get the membership details of a user.
1417
     *
1418
     * @param int $iProfileId
1419
     *
1420
     * @return stdClass The membership detais.
1421
     */
1422
    public function getMembershipDetails($iProfileId)
1423
    {
1424
        $this->cache->start(self::CACHE_GROUP, 'membershipdetails' . $iProfileId, static::CACHE_TIME);
1425
1426
        if (!$oData = $this->cache->get()) {
1427
            $sSql = 'SELECT m.*, g.expirationDays, g.name AS membershipName FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m INNER JOIN ' . Db::prefix(DbTableName::MEMBERSHIP) .
1428
                'AS g USING(groupId) WHERE profileId = :profileId LIMIT 1';
1429
1430
            $rStmt = Db::getInstance()->prepare($sSql);
1431
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1432
            $rStmt->execute();
1433
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
1434
            Db::free($rStmt);
1435
            $this->cache->put($oData);
1436
        }
1437
1438
        return $oData;
1439
    }
1440
1441
    /**
1442
     * Check if membership is expired.
1443
     *
1444
     * @param int $iProfileId
1445
     * @param string $sCurrentTime In date format: 0000-00-00 00:00:00
1446
     *
1447
     * @return bool
1448
     */
1449
    public function checkMembershipExpiration($iProfileId, $sCurrentTime)
1450
    {
1451
        $sSqlQuery = 'SELECT m.profileId FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m INNER JOIN' .
1452
            Db::prefix(DbTableName::MEMBERSHIP) . 'AS pay USING(groupId) WHERE
1453
            (pay.expirationDays = 0 OR DATE_ADD(m.membershipDate, INTERVAL pay.expirationDays DAY) >= :currentTime) AND
1454
            (m.profileId = :profileId) LIMIT 1';
1455
1456
        $rStmt = Db::getInstance()->prepare($sSqlQuery);
1457
1458
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1459
        $rStmt->bindValue(':currentTime', $sCurrentTime, \PDO::PARAM_INT);
1460
        $rStmt->execute();
1461
1462
        return $rStmt->rowCount() === 1;
1463
    }
1464
1465
    /**
1466
     * Update the membership group of a user.
1467
     *
1468
     * @param int $iNewGroupId The new ID of membership group.
1469
     * @param int $iProfileId The user ID.
1470
     * @param string $sDateTime In date format: 0000-00-00 00:00:00
1471
     *
1472
     * @return bool Returns TRUE on success or FALSE on failure.
1473
     */
1474
    public function updateMembership($iNewGroupId, $iProfileId, $sDateTime = null)
1475
    {
1476
        $bIsTime = !empty($sDateTime);
1477
1478
        $sSqlTime = $bIsTime ? ',membershipDate = :dateTime ' : ' ';
1479
1480
        $sSqlQuery = 'UPDATE' . Db::prefix(DbTableName::MEMBER) . 'SET groupId = :groupId' .
1481
            $sSqlTime . 'WHERE profileId = :profileId LIMIT 1';
1482
1483
        $rStmt = Db::getInstance()->prepare($sSqlQuery);
1484
        $rStmt->bindValue(':groupId', $iNewGroupId, \PDO::PARAM_INT);
1485
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1486
        if ($bIsTime) {
1487
            $rStmt->bindValue(':dateTime', $sDateTime, \PDO::PARAM_STR);
1488
        }
1489
1490
        return $rStmt->execute();
1491
    }
1492
1493
    /**
1494
     * Get Info Fields from profile ID.
1495
     *
1496
     * @param int $iProfileId
1497
     * @param string $sTable Default DbTableName::MEMBER_INFO
1498
     *
1499
     * @return stdClass
1500
     */
1501
    public function getInfoFields($iProfileId, $sTable = DbTableName::MEMBER_INFO)
1502
    {
1503
        $this->cache->start(self::CACHE_GROUP, 'infoFields' . $iProfileId . $sTable, static::CACHE_TIME);
1504
1505
        if (!$oData = $this->cache->get()) {
1506
            Various::checkModelTable($sTable);
1507
1508
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1509
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1510
            $rStmt->execute();
1511
            $oColumns = $rStmt->fetch(\PDO::FETCH_OBJ);
1512
            Db::free($rStmt);
1513
1514
            $oData = new stdClass;
1515
            foreach ($oColumns as $sColumn => $sValue) {
1516
                if ($sColumn !== 'profileId') {
1517
                    $oData->$sColumn = $sValue;
1518
                }
1519
            }
1520
            $this->cache->put($oData);
0 ignored issues
show
Documentation introduced by
$oData is of type object<stdClass>, but the function expects a string.

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...
1521
        }
1522
1523
        return $oData;
1524
    }
1525
1526
    /**
1527
     * @return string
1528
     */
1529
    public function getUserWithAvatarOnlySql()
1530
    {
1531
        return ' AND avatar IS NOT NULL AND approvedAvatar = 1';
1532
    }
1533
1534
    /**
1535
     * Clone is set to private to stop cloning.
1536
     */
1537
    private function __clone()
1538
    {
1539
    }
1540
}
1541