UserCoreModel::getGeoProfiles()   D
last analyzed

Complexity

Conditions 11
Paths 512

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
nc 512
nop 6
dl 0
loc 51
rs 4.1313
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @title          User Core Model Class
4
 *
5
 * @author         Pierre-Henry Soria <[email protected]>
6
 * @copyright      (c) 2012-2019, 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\Error\CException\PH7InvalidArgumentException;
16
use PH7\Framework\Ip\Ip;
17
use PH7\Framework\Mvc\Model\DbConfig;
18
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...
19
use PH7\Framework\Mvc\Model\Engine\Model;
20
use PH7\Framework\Mvc\Model\Engine\Util\Various;
21
use PH7\Framework\Security\Security;
22
use PH7\Framework\Session\Session;
23
use PH7\Framework\Str\Str;
24
use PH7\Framework\Translate\Lang;
25
use stdClass;
26
27
// Abstract Class
28
class UserCoreModel extends Model
29
{
30
    /**
31
     * Cache lifetime set to 1 week.
32
     */
33
    const CACHE_TIME = 604800;
34
35
    const CACHE_GROUP = 'db/sys/mod/user';
36
37
    const HASH_VALIDATION_LENGTH = 40;
38
39
    const OFFLINE_STATUS = 0;
40
    const ONLINE_STATUS = 1;
41
    const BUSY_STATUS = 2;
42
    const AWAY_STATUS = 3;
43
44
    const VISITOR_GROUP = 1;
45
    const PENDING_GROUP = 9;
46
47
    const DATETIME_FORMAT = 'Y-m-d H:i:s';
48
49
    /** @var string */
50
    protected $sCurrentDate;
51
52
    /** @var string */
53
    protected $iProfileId;
54
55
    public function __construct()
56
    {
57
        parent::__construct();
58
59
        $this->sCurrentDate = (new CDateTime)->get()->dateTime(self::DATETIME_FORMAT);
60
        $this->iProfileId = (new Session)->get('member_id');
61
    }
62
63
    /**
64
     * @param Session $oSession
65
     *
66
     * @return stdClass
67
     */
68
    public function checkGroup(Session $oSession)
69
    {
70
        // Set default group ID if no user is logged in (and so, 'member_group_id' session doesn't exist)
71
        if (!$oSession->exists('member_group_id')) {
72
            $oSession->regenerateId();
73
            $oSession->set('member_group_id', PermissionCore::VISITOR_GROUP_ID);
74
        }
75
        $iMemberGroupId = (int)$oSession->get('member_group_id');
76
77
        $this->cache->start(
78
            self::CACHE_GROUP,
79
            'membership_groups' . $iMemberGroupId,
80
            static::CACHE_TIME
81
        );
82
83
        if (!$oPermissions = $this->cache->get()) {
84
            $rStmt = Db::getInstance()->prepare(
85
                'SELECT permissions FROM' . Db::prefix(DbTableName::MEMBERSHIP) .
86
                'WHERE groupId = :groupId LIMIT 1'
87
            );
88
            $rStmt->bindValue(':groupId', $iMemberGroupId, \PDO::PARAM_INT);
89
            $rStmt->execute();
90
            $sPermissions = $rStmt->fetchColumn();
91
            Db::free($rStmt);
92
            $oPermissions = ObjArr::toObject(unserialize($sPermissions));
93
            $this->cache->put($oPermissions);
0 ignored issues
show
Documentation introduced by
$oPermissions 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...
94
        }
95
96
        return $oPermissions;
97
    }
98
99
    /**
100
     * Login method for Members and Affiliate, but not for Admins since it has another method PH7\AdminModel::adminLogin() even more secure.
101
     *
102
     * @param string $sEmail Not case sensitive since on lot of mobile devices (such as iPhone), the first letter is uppercase.
103
     * @param string $sPassword
104
     * @param string $sTable Default DbTableName::MEMBER
105
     *
106
     * @return bool|string (boolean "true" or string "message")
107
     */
108
    public function login($sEmail, $sPassword, $sTable = DbTableName::MEMBER)
109
    {
110
        Various::checkModelTable($sTable);
111
112
        $rStmt = Db::getInstance()->prepare(
113
            'SELECT email, password FROM' . Db::prefix($sTable) . 'WHERE email = :email LIMIT 1'
114
        );
115
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
116
        $rStmt->execute();
117
        $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
118
        Db::free($rStmt);
119
120
        $sDbEmail = !empty($oRow->email) ? $oRow->email : '';
121
        $sDbPassword = !empty($oRow->password) ? $oRow->password : '';
122
123
        if (strtolower($sEmail) !== strtolower($sDbEmail)) {
124
            return CredentialStatusCore::EMAIL_DOES_NOT_EXIST;
125
        }
126
        if (!Security::checkPwd($sPassword, $sDbPassword)) {
127
            return CredentialStatusCore::PASSWORD_DOES_NOT_EXIST;
128
        }
129
130
        return true;
131
    }
132
133
    /**
134
     * Set Log Session.
135
     *
136
     * @param string $sEmail
137
     * @param string $sUsername
138
     * @param string $sFirstName
139
     * @param string $sTable
140
     *
141
     * @return void
142
     */
143
    public function sessionLog($sEmail, $sUsername, $sFirstName, $sTable = DbTableName::MEMBER)
144
    {
145
        Various::checkModelTable($sTable);
146
147
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix($sTable . '_log_sess') . '(email, username, firstName, ip)
148
        VALUES (:email, :username, :firstName, :ip)');
149
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
150
        $rStmt->bindValue(':username', $sUsername, \PDO::PARAM_STR);
151
        $rStmt->bindValue(':firstName', $sFirstName, \PDO::PARAM_STR);
152
        $rStmt->bindValue(':ip', Ip::get(), \PDO::PARAM_STR);
153
        $rStmt->execute();
154
        Db::free($rStmt);
155
    }
156
157
    /**
158
     * Read Profile Data.
159
     *
160
     * @param int $iProfileId The user ID
161
     * @param string $sTable Default DbTableName::MEMBER
162
     *
163
     * @return stdClass|bool The data of a member if exists, FALSE otherwise.
164
     */
165
    public function readProfile($iProfileId, $sTable = DbTableName::MEMBER)
166
    {
167
        $this->cache->start(self::CACHE_GROUP, 'readProfile' . $iProfileId . $sTable, static::CACHE_TIME);
168
169
        if (!$oData = $this->cache->get()) {
170
            Various::checkModelTable($sTable);
171
172
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
173
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
174
            $rStmt->execute();
175
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
176
            Db::free($rStmt);
177
            $this->cache->put($oData);
178
        }
179
180
        return $oData;
181
    }
182
183
    /**
184
     * Get the total number of members.
185
     *
186
     * @param string $sTable Default DbTableName::MEMBER
187
     * @param int $iDay Default '0'
188
     * @param string $sGender Values ​​available 'all', 'male', 'female'. 'couple' is only available to Members. Default 'all'
189
     *
190
     * @return int Total Users
191
     */
192
    public function total($sTable = DbTableName::MEMBER, $iDay = 0, $sGender = 'all')
193
    {
194
        Various::checkModelTable($sTable);
195
196
        $iDay = (int)$iDay;
197
        $bIsDay = ($iDay > 0);
198
199
        if ($sTable === DbTableName::MEMBER) {
200
            $bIsGender = GenderTypeUserCore::isGenderValid($sGender);
201
        } else {
202
            $bIsGender = GenderTypeUserCore::isGenderValid($sGender, GenderTypeUserCore::IGNORE_COUPLE_GENDER);
203
        }
204
205
        $sSqlDay = $bIsDay ? ' AND (joinDate + INTERVAL :day DAY) > NOW()' : '';
206
        $sSqlGender = $bIsGender ? ' AND sex = :gender' : '';
207
208
        $rStmt = Db::getInstance()->prepare('SELECT COUNT(profileId) FROM' . Db::prefix($sTable) . 'WHERE username <> :ghostUsername' . $sSqlDay . $sSqlGender);
209
        $rStmt->bindValue(':ghostUsername', PH7_GHOST_USERNAME, \PDO::PARAM_STR);
210
        if ($bIsDay) {
211
            $rStmt->bindValue(':day', $iDay, \PDO::PARAM_INT);
212
        }
213
        if ($bIsGender) {
214
            $rStmt->bindValue(':gender', $sGender, \PDO::PARAM_STR);
215
        }
216
        $rStmt->execute();
217
218
        $iTotalUsers = (int)$rStmt->fetchColumn();
219
        Db::free($rStmt);
220
221
        return $iTotalUsers;
222
    }
223
224
    /**
225
     * Update profile data.
226
     *
227
     * @param string $sSection
228
     * @param string $sValue
229
     * @param int $iProfileId Profile ID
230
     * @param string $sTable Default DbTableName::MEMBER
231
     *
232
     * @return void
233
     */
234
    public function updateProfile($sSection, $sValue, $iProfileId, $sTable = DbTableName::MEMBER)
235
    {
236
        Various::checkModelTable($sTable);
237
238
        $this->orm->update($sTable, $sSection, $sValue, 'profileId', $iProfileId);
239
    }
240
241
    /**
242
     * Update Privacy setting data.
243
     *
244
     * @param string $sSection
245
     * @param string $sValue
246
     * @param int $iProfileId Profile ID
247
     *
248
     * @return void
249
     */
250
    public function updatePrivacySetting($sSection, $sValue, $iProfileId)
251
    {
252
        $this->orm->update(
253
            DbTableName::MEMBER_PRIVACY,
254
            $sSection,
255
            $sValue,
256
            'profileId',
257
            $iProfileId
258
        );
259
    }
260
261
    /**
262
     * Change password of a member.
263
     *
264
     * @param string $sEmail
265
     * @param string $sNewPassword
266
     * @param string $sTable
267
     *
268
     * @return bool
269
     */
270
    public function changePassword($sEmail, $sNewPassword, $sTable)
271
    {
272
        Various::checkModelTable($sTable);
273
274
        $rStmt = Db::getInstance()->prepare(
275
            'UPDATE' . Db::prefix($sTable) . 'SET password = :newPassword WHERE email = :email LIMIT 1'
276
        );
277
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
278
        $rStmt->bindValue(':newPassword', Security::hashPwd($sNewPassword), \PDO::PARAM_STR);
279
280
        return $rStmt->execute();
281
    }
282
283
    /**
284
     * Set a new hash validation.
285
     *
286
     * @param int $iProfileId
287
     * @param string $sHash
288
     * @param string $sTable
289
     *
290
     * @return bool
291
     */
292
    public function setNewHashValidation($iProfileId, $sHash, $sTable)
293
    {
294
        Various::checkModelTable($sTable);
295
296
        $rStmt = Db::getInstance()->prepare('UPDATE' . Db::prefix($sTable) . 'SET hashValidation = :hash WHERE profileId = :profileId LIMIT 1');
297
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
298
        $rStmt->bindParam(':hash', $sHash, \PDO::PARAM_STR, self::HASH_VALIDATION_LENGTH);
299
300
        return $rStmt->execute();
301
    }
302
303
    /**
304
     * Check the hash validation.
305
     *
306
     * @param string $sEmail
307
     * @param string $sHash
308
     * @param string $sTable
309
     *
310
     * @return bool
311
     */
312
    public function checkHashValidation($sEmail, $sHash, $sTable)
313
    {
314
        Various::checkModelTable($sTable);
315
316
        $rStmt = Db::getInstance()->prepare(
317
            'SELECT COUNT(profileId) FROM' . Db::prefix($sTable) . 'WHERE email = :email AND hashValidation = :hash LIMIT 1'
318
        );
319
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
320
        $rStmt->bindParam(':hash', $sHash, \PDO::PARAM_STR, self::HASH_VALIDATION_LENGTH);
321
        $rStmt->execute();
322
323
        return $rStmt->fetchColumn() == 1;
324
    }
325
326
    /**
327
     * Search users.
328
     *
329
     * @param array $aParams
330
     * @param bool $bCount
331
     * @param int $iOffset
332
     * @param int $iLimit
333
     *
334
     * @return array|int Object for the users list returned or integer for the total number users returned.
335
     */
336
    public function search(array $aParams, $bCount, $iOffset, $iLimit)
337
    {
338
        $bCount = (bool)$bCount;
339
        $iOffset = (int)$iOffset;
340
        $iLimit = (int)$iLimit;
341
342
        $bIsMail = !empty($aParams[SearchQueryCore::EMAIL]) && Str::noSpaces($aParams[SearchQueryCore::EMAIL]);
343
        $bIsFirstName = !$bIsMail && !empty($aParams[SearchQueryCore::FIRST_NAME]) && Str::noSpaces($aParams[SearchQueryCore::FIRST_NAME]);
344
        $bIsMiddleName = !$bIsMail && !empty($aParams[SearchQueryCore::MIDDLE_NAME]) && Str::noSpaces($aParams[SearchQueryCore::MIDDLE_NAME]);
345
        $bIsLastName = !$bIsMail && !empty($aParams[SearchQueryCore::LAST_NAME]) && Str::noSpaces($aParams[SearchQueryCore::LAST_NAME]);
346
        $bIsSingleAge = !$bIsMail && !empty($aParams[SearchQueryCore::AGE]);
347
        $bIsAge = !$bIsMail && empty($aParams[SearchQueryCore::AGE]) && !empty($aParams[SearchQueryCore::MIN_AGE]) && !empty($aParams[SearchQueryCore::MAX_AGE]);
348
        $bIsHeight = !$bIsMail && !empty($aParams[SearchQueryCore::HEIGHT]);
349
        $bIsWeight = !$bIsMail && !empty($aParams[SearchQueryCore::WEIGHT]);
350
        $bIsCountry = !$bIsMail && !empty($aParams[SearchQueryCore::COUNTRY]) && Str::noSpaces($aParams[SearchQueryCore::COUNTRY]);
351
        $bIsCity = !$bIsMail && !empty($aParams[SearchQueryCore::CITY]) && Str::noSpaces($aParams[SearchQueryCore::CITY]);
352
        $bIsState = !$bIsMail && !empty($aParams[SearchQueryCore::STATE]) && Str::noSpaces($aParams[SearchQueryCore::STATE]);
353
        $bIsZipCode = !$bIsMail && !empty($aParams[SearchQueryCore::ZIP_CODE]) && Str::noSpaces($aParams[SearchQueryCore::ZIP_CODE]);
354
        $bIsSex = !$bIsMail && !empty($aParams[SearchQueryCore::SEX]) && is_array($aParams[SearchQueryCore::SEX]);
355
        $bIsMatchSex = !$bIsMail && !empty($aParams[SearchQueryCore::MATCH_SEX]);
356
        $bIsOnline = !$bIsMail && !empty($aParams[SearchQueryCore::ONLINE]);
357
        $bIsAvatar = !$bIsMail && !empty($aParams[SearchQueryCore::AVATAR]);
358
        $bHideUserLogged = !$bIsMail && !empty($this->iProfileId);
359
360
        $sSqlLimit = !$bCount ? 'LIMIT :offset, :limit' : '';
361
        $sSqlSelect = !$bCount ? '*' : 'COUNT(m.profileId)';
362
        $sSqlFirstName = $bIsFirstName ? ' AND LOWER(firstName) LIKE LOWER(:firstName)' : '';
363
        $sSqlMiddleName = $bIsMiddleName ? ' AND LOWER(middleName) LIKE LOWER(:middleName)' : '';
364
        $sSqlLastName = $bIsLastName ? ' AND LOWER(lastName) LIKE LOWER(:lastName)' : '';
365
        $sSqlSingleAge = $bIsSingleAge ? ' AND birthDate LIKE :birthDate ' : '';
366
        $sSqlAge = $bIsAge ? ' AND birthDate BETWEEN DATE_SUB(\'' . $this->sCurrentDate . '\', INTERVAL :age2 YEAR) AND DATE_SUB(\'' . $this->sCurrentDate . '\', INTERVAL :age1 YEAR) ' : '';
367
        $sSqlHeight = $bIsHeight ? ' AND height = :height ' : '';
368
        $sSqlWeight = $bIsWeight ? ' AND weight = :weight ' : '';
369
        $sSqlCountry = $bIsCountry ? ' AND country = :country ' : '';
370
        $sSqlCity = $bIsCity ? ' AND LOWER(city) LIKE LOWER(:city) ' : '';
371
        $sSqlState = $bIsState ? ' AND LOWER(state) LIKE LOWER(:state) ' : '';
372
        $sSqlZipCode = $bIsZipCode ? ' AND LOWER(zipCode) LIKE LOWER(:zipCode) ' : '';
373
        $sSqlEmail = $bIsMail ? ' AND email LIKE :email ' : '';
374
        $sSqlOnline = $bIsOnline ? ' AND userStatus = :userStatus AND lastActivity > DATE_SUB(\'' . $this->sCurrentDate . '\', INTERVAL ' . DbConfig::getSetting('userTimeout') . ' MINUTE) ' : '';
375
        $sSqlAvatar = $bIsAvatar ? $this->getUserWithAvatarOnlySql() : '';
376
        $sSqlHideLoggedProfile = $bHideUserLogged ? ' AND (m.profileId <> :profileId)' : '';
377
        $sSqlMatchSex = $bIsMatchSex ? ' AND FIND_IN_SET(:matchSex, matchSex)' : '';
378
379
        $sSqlSex = '';
380
        if ($bIsSex) {
381
            $sSqlSex = $this->getSexInClauseSql($aParams[SearchQueryCore::SEX]);
382
        }
383
384
        if (empty($aParams[SearchQueryCore::ORDER])) {
385
            $aParams[SearchQueryCore::ORDER] = SearchCoreModel::LATEST; // Default is "ORDER BY joinDate"
386
        }
387
        if (empty($aParams[SearchQueryCore::SORT])) {
388
            $aParams[SearchQueryCore::SORT] = SearchCoreModel::DESC; // Default is "descending"
389
        }
390
        $sSqlOrder = SearchCoreModel::order($aParams[SearchQueryCore::ORDER], $aParams[SearchQueryCore::SORT]);
391
392
        $rStmt = Db::getInstance()->prepare(
393
            'SELECT ' . $sSqlSelect . ' FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m LEFT JOIN' . Db::prefix(DbTableName::MEMBER_PRIVACY) . 'AS p USING(profileId)
394
            LEFT JOIN' . Db::prefix(DbTableName::MEMBER_INFO) . 'AS i USING(profileId) WHERE username <> :ghostUsername AND searchProfile = \'yes\'
395
            AND (groupId <> :visitorGroup) AND (groupId <> :pendingGroup) AND (ban = 0)' . $sSqlHideLoggedProfile . $sSqlFirstName . $sSqlMiddleName . $sSqlLastName . $sSqlMatchSex . $sSqlSex . $sSqlSingleAge . $sSqlAge . $sSqlCountry . $sSqlCity . $sSqlState .
396
            $sSqlZipCode . $sSqlHeight . $sSqlWeight . $sSqlEmail . $sSqlOnline . $sSqlAvatar . $sSqlOrder . $sSqlLimit
397
        );
398
399
        $rStmt->bindValue(':ghostUsername', PH7_GHOST_USERNAME, \PDO::PARAM_STR);
400
        $rStmt->bindValue(':visitorGroup', self::VISITOR_GROUP, \PDO::PARAM_INT);
401
        $rStmt->bindValue(':pendingGroup', self::PENDING_GROUP, \PDO::PARAM_INT);
402
403
        if ($bIsMatchSex) {
404
            $rStmt->bindValue(':matchSex', $aParams[SearchQueryCore::MATCH_SEX], \PDO::PARAM_STR);
405
        }
406
        if ($bIsFirstName) {
407
            $rStmt->bindValue(':firstName', '%' . $aParams[SearchQueryCore::FIRST_NAME] . '%', \PDO::PARAM_STR);
408
        }
409
        if ($bIsMiddleName) {
410
            $rStmt->bindValue(':middleName', '%' . $aParams[SearchQueryCore::MIDDLE_NAME] . '%', \PDO::PARAM_STR);
411
        }
412
        if ($bIsLastName) {
413
            $rStmt->bindValue(':lastName', '%' . $aParams[SearchQueryCore::LAST_NAME] . '%', \PDO::PARAM_STR);
414
        }
415
        if ($bIsSingleAge) {
416
            $rStmt->bindValue(':birthDate', '%' . $aParams[SearchQueryCore::AGE] . '%', \PDO::PARAM_STR);
417
        }
418
        if ($bIsAge) {
419
            $rStmt->bindValue(':age1', $aParams[SearchQueryCore::MIN_AGE], \PDO::PARAM_INT);
420
            $rStmt->bindValue(':age2', $aParams[SearchQueryCore::MAX_AGE], \PDO::PARAM_INT);
421
        }
422
        if ($bIsHeight) {
423
            $rStmt->bindValue(':height', $aParams[SearchQueryCore::HEIGHT], \PDO::PARAM_INT);
424
        }
425
        if ($bIsWeight) {
426
            $rStmt->bindValue(':weight', $aParams[SearchQueryCore::WEIGHT], \PDO::PARAM_INT);
427
        }
428
        if ($bIsCountry) {
429
            $rStmt->bindParam(':country', $aParams[SearchQueryCore::COUNTRY], \PDO::PARAM_STR, 2);
430
        }
431
        if ($bIsCity) {
432
            $rStmt->bindValue(':city', '%' . str_replace('-', ' ', $aParams[SearchQueryCore::CITY]) . '%', \PDO::PARAM_STR);
433
        }
434
        if ($bIsState) {
435
            $rStmt->bindValue(':state', '%' . str_replace('-', ' ', $aParams[SearchQueryCore::STATE]) . '%', \PDO::PARAM_STR);
436
        }
437
        if ($bIsZipCode) {
438
            $rStmt->bindValue(':zipCode', '%' . $aParams[SearchQueryCore::ZIP_CODE] . '%', \PDO::PARAM_STR);
439
        }
440
        if ($bIsMail) {
441
            $rStmt->bindValue(':email', '%' . $aParams[SearchQueryCore::EMAIL] . '%', \PDO::PARAM_STR);
442
        }
443
        if ($bIsOnline) {
444
            $rStmt->bindValue(':userStatus', self::ONLINE_STATUS, \PDO::PARAM_INT);
445
        }
446
        if ($bHideUserLogged) {
447
            $rStmt->bindValue(':profileId', $this->iProfileId, \PDO::PARAM_INT);
448
        }
449
        if (!$bCount) {
450
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
451
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
452
        }
453
454
        $rStmt->execute();
455
456
        if (!$bCount) {
457
            $aRow = $rStmt->fetchAll(\PDO::FETCH_OBJ);
458
            Db::free($rStmt);
459
460
            return $aRow;
461
        }
462
463
        $iTotalUsers = (int)$rStmt->fetchColumn();
464
        Db::free($rStmt);
465
466
        return $iTotalUsers;
467
    }
468
469
    /**
470
     * Check online status.
471
     *
472
     * @param int $iProfileId
473
     * @param int $iTime Number of minutes that a member becomes inactive (offline). Default 1 minute
474
     *
475
     * @return bool
476
     */
477
    public function isOnline($iProfileId, $iTime = 1)
478
    {
479
        $iProfileId = (int)$iProfileId;
480
        $iTime = (int)$iTime;
481
482
        $rStmt = Db::getInstance()->prepare('SELECT profileId FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = :profileId
483
            AND userStatus = :userStatus AND lastActivity >= DATE_SUB(:currentTime, INTERVAL :time MINUTE) LIMIT 1');
484
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
485
        $rStmt->bindValue(':userStatus', self::ONLINE_STATUS, \PDO::PARAM_INT);
486
        $rStmt->bindValue(':time', $iTime, \PDO::PARAM_INT);
487
        $rStmt->bindValue(':currentTime', $this->sCurrentDate, \PDO::PARAM_STR);
488
        $rStmt->execute();
489
490
        return $rStmt->rowCount() === 1;
491
    }
492
493
    /**
494
     * Set the user status.
495
     *
496
     * @param int iProfileId
497
     * @param int $iStatus Values: 0 = Offline, 1 = Online, 2 = Busy, 3 = Away
498
     *
499
     * @return void
500
     */
501
    public function setUserStatus($iProfileId, $iStatus)
502
    {
503
        $this->orm->update(DbTableName::MEMBER, 'userStatus', $iStatus, 'profileId', $iProfileId);
504
    }
505
506
    /**
507
     * Get the user status.
508
     *
509
     * @param int $iProfileId
510
     *
511
     * @return int The user status. 0 = Offline, 1 = Online, 2 = Busy, 3 = Away
512
     */
513
    public function getUserStatus($iProfileId)
514
    {
515
        $this->cache->start(self::CACHE_GROUP, 'userStatus' . $iProfileId, static::CACHE_TIME);
516
517
        if (!$iUserStatus = $this->cache->get()) {
518
            $rStmt = Db::getInstance()->prepare('SELECT userStatus FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = :profileId LIMIT 1');
519
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
520
            $rStmt->execute();
521
            $iUserStatus = (int)$rStmt->fetchColumn();
522
            Db::free($rStmt);
523
524
            $this->cache->put($iUserStatus);
525
        }
526
527
        return $iUserStatus;
528
    }
529
530
    /**
531
     * Update the notifications.
532
     *
533
     * @param string $sSection
534
     * @param string $sValue
535
     * @param int $iProfileId Profile ID
536
     *
537
     * @return void
538
     */
539
    public function setNotification($sSection, $sValue, $iProfileId)
540
    {
541
        $this->orm->update(DbTableName::MEMBER_NOTIFICATION, $sSection, $sValue, 'profileId', $iProfileId);
542
    }
543
544
    /**
545
     * Get the user notifications.
546
     *
547
     * @param int $iProfileId
548
     *
549
     * @return stdClass
550
     */
551
    public function getNotification($iProfileId)
552
    {
553
        $this->cache->start(self::CACHE_GROUP, 'notification' . $iProfileId, static::CACHE_TIME);
554
555
        if (!$oData = $this->cache->get()) {
556
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix(DbTableName::MEMBER_NOTIFICATION) . 'WHERE profileId = :profileId LIMIT 1');
557
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
558
            $rStmt->execute();
559
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
560
            Db::free($rStmt);
561
            $this->cache->put($oData);
562
        }
563
564
        return $oData;
565
    }
566
567
    /**
568
     * Check notifications.
569
     *
570
     * @param int $iProfileId
571
     * @param string $sNotifName Notification name.
572
     *
573
     * @return bool Returns TRUE if the notification is wanted, FALSE otherwise.
574
     */
575
    public function isNotification($iProfileId, $sNotifName)
576
    {
577
        $this->cache->start(self::CACHE_GROUP, 'isNotification' . $iProfileId, static::CACHE_TIME);
578
579
        if (!$bNotification = $this->cache->get()) {
580
            $sSql = 'SELECT ' . $sNotifName . ' FROM' . Db::prefix(DbTableName::MEMBER_NOTIFICATION) .
581
                'WHERE profileId = :profileId AND ' . $sNotifName . ' = 1 LIMIT 1';
582
583
            $rStmt = Db::getInstance()->prepare($sSql);
584
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
585
            $rStmt->execute();
586
            $bNotification = $rStmt->rowCount() === 1;
587
            Db::free($rStmt);
588
            $this->cache->put($bNotification);
0 ignored issues
show
Documentation introduced by
$bNotification 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...
589
        }
590
591
        return $bNotification;
592
    }
593
594
    /**
595
     * Set the last activity of a user.
596
     *
597
     * @param int $iProfileId
598
     * @param string $sTable Default DbTableName::MEMBER
599
     *
600
     * @return void
601
     */
602
    public function setLastActivity($iProfileId, $sTable = DbTableName::MEMBER)
603
    {
604
        Various::checkModelTable($sTable);
605
606
        $this->orm->update($sTable, 'lastActivity', $this->sCurrentDate, 'profileId', $iProfileId);
607
    }
608
609
    /**
610
     * Set the last edit account of a user.
611
     *
612
     * @param int $iProfileId
613
     * @param string $sTable Default DbTableName::MEMBER
614
     *
615
     * @return void
616
     */
617
    public function setLastEdit($iProfileId, $sTable = DbTableName::MEMBER)
618
    {
619
        Various::checkModelTable($sTable);
620
621
        $this->orm->update($sTable, 'lastEdit', $this->sCurrentDate, 'profileId', $iProfileId);
622
    }
623
624
    /**
625
     * Approve a profile.
626
     *
627
     * @param int $iProfileId
628
     * @param int $iStatus 1 = approved | 0 = not approved
629
     * @param string $sTable Default DbTableName::MEMBER
630
     *
631
     * @return void
632
     */
633
    public function approve($iProfileId, $iStatus, $sTable = DbTableName::MEMBER)
634
    {
635
        Various::checkModelTable($sTable);
636
637
        $this->orm->update($sTable, 'active', $iStatus, 'profileId', $iProfileId);
638
    }
639
640
    /**
641
     * Get member data. The validation hash, and other useful data for sending the activation email (hash, email, username, firstName).
642
     *
643
     * @param string $sEmail User's email address.
644
     * @param string $sTable Default DbTableName::MEMBER
645
     *
646
     * @return stdClass|bool Returns the data member (email, username, firstName, hashValidation) on success, otherwise returns false if there is an error.
647
     */
648
    public function getHashValidation($sEmail, $sTable = DbTableName::MEMBER)
649
    {
650
        Various::checkModelTable($sTable);
651
652
        $rStmt = Db::getInstance()->prepare(
653
            'SELECT email, username, firstName, hashValidation FROM' . Db::prefix($sTable) .
654
            'WHERE email = :email AND active = :emailActivation LIMIT 1'
655
        );
656
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
657
        $rStmt->bindValue(':emailActivation', RegistrationCore::EMAIL_ACTIVATION, \PDO::PARAM_INT);
658
        $rStmt->execute();
659
        $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
660
        Db::free($rStmt);
661
662
        return $oRow;
663
    }
664
665
    /**
666
     * Valid on behalf of a user with the hash.
667
     *
668
     * @param string $sEmail
669
     * @param string $sHash
670
     * @param string $sTable Default DbTableName::MEMBER
671
     *
672
     * @return bool
673
     */
674
    public function validateAccount($sEmail, $sHash, $sTable = DbTableName::MEMBER)
675
    {
676
        Various::checkModelTable($sTable);
677
678
        $rStmt = Db::getInstance()->prepare('UPDATE' . Db::prefix($sTable) . 'SET active = :noActivation WHERE email = :email AND hashValidation = :hash AND active = :emailActivation LIMIT 1');
679
        $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
680
        $rStmt->bindValue(':noActivation', RegistrationCore::NO_ACTIVATION, \PDO::PARAM_INT);
681
        $rStmt->bindValue(':emailActivation', RegistrationCore::EMAIL_ACTIVATION, \PDO::PARAM_INT);
682
        $rStmt->bindParam(':hash', $sHash, \PDO::PARAM_STR, self::HASH_VALIDATION_LENGTH);
683
684
        return $rStmt->execute();
685
    }
686
687
    /**
688
     * Adding a User.
689
     *
690
     * @param array $aData
691
     *
692
     * @return int The ID of the User.
693
     */
694
    public function add(array $aData)
695
    {
696
        $sHashValidation = !empty($aData['hash_validation']) ? $aData['hash_validation'] : null;
697
698
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER) . '(email, username, password, firstName, lastName, sex, matchSex, birthDate, active, lang, ip, hashValidation, joinDate, lastActivity)
699
            VALUES (:email, :username, :password, :firstName, :lastName, :sex, :matchSex, :birthDate, :active, :lang, :ip, :hashValidation, :joinDate, :lastActivity)');
700
        $rStmt->bindValue(':email', trim($aData['email']), \PDO::PARAM_STR);
701
        $rStmt->bindValue(':username', trim($aData['username']), \PDO::PARAM_STR);
702
        $rStmt->bindValue(':password', Security::hashPwd($aData['password']), \PDO::PARAM_STR);
703
        $rStmt->bindValue(':firstName', $aData['first_name'], \PDO::PARAM_STR);
704
        $rStmt->bindValue(':lastName', $aData['last_name'], \PDO::PARAM_STR);
705
        $rStmt->bindValue(':sex', $aData['sex'], \PDO::PARAM_STR);
706
        $rStmt->bindValue(':matchSex', Form::setVal($aData['match_sex']), \PDO::PARAM_STR);
707
        $rStmt->bindValue(':birthDate', $aData['birth_date'], \PDO::PARAM_STR);
708
        $rStmt->bindValue(':active', (!empty($aData['is_active']) ? $aData['is_active'] : RegistrationCore::NO_ACTIVATION), \PDO::PARAM_INT);
709
        $rStmt->bindValue(':lang', (!empty($aData['lang']) ? substr($aData['lang'], 0, 5) : Lang::DEFAULT_LOCALE), \PDO::PARAM_STR);
710
        $rStmt->bindValue(':ip', $aData['ip'], \PDO::PARAM_STR);
711
        $rStmt->bindParam(':hashValidation', $sHashValidation, \PDO::PARAM_STR, self::HASH_VALIDATION_LENGTH);
712
        $rStmt->bindValue(':joinDate', $this->sCurrentDate, \PDO::PARAM_STR);
713
        $rStmt->bindValue(':lastActivity', $this->sCurrentDate, \PDO::PARAM_STR);
714
        $rStmt->execute();
715
        $this->setKeyId(Db::getInstance()->lastInsertId()); // Set the user's ID
716
        Db::free($rStmt);
717
718
        $this->setInfoFields($aData);
719
        $this->setDefaultPrivacySetting();
720
        $this->setDefaultNotification();
721
722
        // Last one, update the membership with the correct details
723
        $this->updateMembership(
724
            (int)DbConfig::getSetting('defaultMembershipGroupId'),
725
            $this->getKeyId(),
726
            $this->sCurrentDate
727
        );
728
729
        return $this->getKeyId();
730
    }
731
732
    /**
733
     * @param array $aData
734
     *
735
     * @return bool
736
     */
737
    public function setInfoFields(array $aData)
738
    {
739
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER_INFO) . '(profileId, middleName, country, city, state, zipCode, description, punchline, website, socialNetworkSite)
740
            VALUES (:profileId, :middleName, :country, :city, :state, :zipCode, :description, :punchline, :website, :socialNetworkSite)');
741
        $rStmt->bindValue(':profileId', $this->getKeyId(), \PDO::PARAM_INT);
742
        $rStmt->bindValue(':middleName', (!empty($aData['middle_name']) ? $aData['middle_name'] : ''), \PDO::PARAM_STR);
743
        $rStmt->bindValue(':country', (!empty($aData['country']) ? $aData['country'] : ''), \PDO::PARAM_STR);
744
        $rStmt->bindValue(':city', (!empty($aData['city']) ? $aData['city'] : ''), \PDO::PARAM_STR);
745
        $rStmt->bindValue(':state', (!empty($aData['state']) ? $aData['state'] : ''), \PDO::PARAM_STR);
746
        $rStmt->bindValue(':zipCode', (!empty($aData['zip_code']) ? $aData['zip_code'] : ''), \PDO::PARAM_STR);
747
        $rStmt->bindValue(':description', (!empty($aData['description']) ? $aData['description'] : ''), \PDO::PARAM_STR);
748
        $rStmt->bindValue(':punchline', (!empty($aData['punchline']) ? $aData['punchline'] : ''), \PDO::PARAM_STR);
749
        $rStmt->bindValue(':website', (!empty($aData['website']) ? trim($aData['website']) : ''), \PDO::PARAM_STR);
750
        $rStmt->bindValue(':socialNetworkSite', (!empty($aData['social_network_site']) ? trim($aData['social_network_site']) : ''), \PDO::PARAM_STR);
751
752
        return $rStmt->execute();
753
    }
754
755
    /**
756
     * Set the default privacy settings.
757
     *
758
     * @return bool Returns TRUE on success or FALSE on failure.
759
     */
760
    public function setDefaultPrivacySetting()
761
    {
762
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER_PRIVACY) .
763
            '(profileId, privacyProfile, searchProfile, userSaveViews)
764
            VALUES (:profileId, \'all\', \'yes\', \'yes\')');
765
        $rStmt->bindValue(':profileId', $this->getKeyId(), \PDO::PARAM_INT);
766
        return $rStmt->execute();
767
    }
768
769
    /**
770
     * Set the default notifications.
771
     *
772
     * @return bool Returns TRUE on success or FALSE on failure.
773
     */
774
    public function setDefaultNotification()
775
    {
776
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER_NOTIFICATION) .
777
            '(profileId, enableNewsletters, newMsg, friendRequest)
778
            VALUES (:profileId, 1, 1, 1)');
779
        $rStmt->bindValue(':profileId', $this->getKeyId(), \PDO::PARAM_INT);
780
        return $rStmt->execute();
781
    }
782
783
    /**
784
     * To avoid flooding!
785
     * Waiting time before a new registration with the same IP address.
786
     *
787
     * @param string $sIp
788
     * @param int $iWaitTime In minutes!
789
     * @param string $sCurrentTime In date format: 0000-00-00 00:00:00
790
     * @param string $sTable Default DbTableName::MEMBER
791
     *
792
     * @return bool Return TRUE if the weather was fine, FALSE otherwise.
793
     */
794
    public function checkWaitJoin($sIp, $iWaitTime, $sCurrentTime, $sTable = DbTableName::MEMBER)
795
    {
796
        Various::checkModelTable($sTable);
797
798
        $rStmt = Db::getInstance()->prepare('SELECT profileId FROM' . Db::prefix($sTable) .
799
            'WHERE ip = :ip AND DATE_ADD(joinDate, INTERVAL :waitTime MINUTE) > :currentTime LIMIT 1');
800
        $rStmt->bindValue(':ip', $sIp, \PDO::PARAM_STR);
801
        $rStmt->bindValue(':waitTime', $iWaitTime, \PDO::PARAM_INT);
802
        $rStmt->bindValue(':currentTime', $sCurrentTime, \PDO::PARAM_STR);
803
        $rStmt->execute();
804
805
        return $rStmt->rowCount() === 0;
806
    }
807
808
809
    /********** AVATAR **********/
810
811
    /**
812
     * Update or add a new avatar.
813
     *
814
     * @param int $iProfileId
815
     * @param string|null $sAvatar NULL to remove the avatar.
816
     * @param int $iApproved
817
     *
818
     * @return bool
819
     */
820
    public function setAvatar($iProfileId, $sAvatar, $iApproved)
821
    {
822
        $sSql = 'UPDATE' . Db::prefix(DbTableName::MEMBER) .
823
            'SET avatar = :avatar, approvedAvatar = :approved WHERE profileId = :profileId LIMIT 1';
824
825
        $rStmt = Db::getInstance()->prepare($sSql);
826
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
827
        $rStmt->bindValue(':avatar', $sAvatar, \PDO::PARAM_STR);
828
        $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_INT);
829
830
        return $rStmt->execute();
831
    }
832
833
    /**
834
     * Get avatar.
835
     *
836
     * @param int $iProfileId
837
     * @param string|null $iApproved (1 = approved | 0 = pending | NULL = approved and pending)
838
     *
839
     * @return stdClass The Avatar (SQL alias is pic), profileId and approvedAvatar
840
     */
841
    public function getAvatar($iProfileId, $iApproved = null)
842
    {
843
        $this->cache->start(self::CACHE_GROUP, 'avatar' . $iProfileId, static::CACHE_TIME);
844
845
        if (!$oData = $this->cache->get()) {
846
            $bIsApproved = $iApproved !== null;
847
848
            $sSqlApproved = $bIsApproved ? ' AND approvedAvatar = :approved ' : ' ';
849
            $rStmt = Db::getInstance()->prepare('SELECT profileId, avatar AS pic, approvedAvatar FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = :profileId' . $sSqlApproved . 'LIMIT 1');
850
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
851
            if ($bIsApproved) {
852
                $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_STR);
853
            }
854
            $rStmt->execute();
855
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
856
            Db::free($rStmt);
857
            $this->cache->put($oData);
858
        }
859
860
        return $oData;
861
    }
862
863
    /**
864
     * Delete an avatar in the database.
865
     *
866
     * @param int $iProfileId
867
     *
868
     * @return bool
869
     */
870
    public function deleteAvatar($iProfileId)
871
    {
872
        return $this->setAvatar($iProfileId, null, 1);
873
    }
874
875
876
    /********** BACKGROUND **********/
877
878
    /**
879
     * Get file of a user background.
880
     *
881
     * @param int $iProfileId
882
     * @param int|null $iApproved (1 = approved | 0 = pending | NULL = approved and pending) Default NULL
883
     *
884
     * @return string
885
     */
886
    public function getBackground($iProfileId, $iApproved = null)
887
    {
888
        $this->cache->start(self::CACHE_GROUP, 'background' . $iProfileId, static::CACHE_TIME);
889
890
        if (!$sFile = $this->cache->get()) {
891
            $bIsApproved = $iApproved !== null;
892
893
            $sSqlApproved = $bIsApproved ? ' AND approved = :approved ' : ' ';
894
            $rStmt = Db::getInstance()->prepare('SELECT file FROM' . Db::prefix(DbTableName::MEMBER_BACKGROUND) . 'WHERE profileId = :profileId' . $sSqlApproved . 'LIMIT 1');
895
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
896
            if ($bIsApproved) {
897
                $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_STR);
898
            }
899
            $rStmt->execute();
900
            $sFile = $rStmt->fetchColumn();
901
            Db::free($rStmt);
902
903
            $this->cache->put($sFile);
904
        }
905
906
        return $sFile;
907
    }
908
909
    /**
910
     * Add profile background.
911
     *
912
     * @param int $iProfileId
913
     * @param string $sFile
914
     * @param int $iApproved
915
     *
916
     * @return bool
917
     */
918
    public function addBackground($iProfileId, $sFile, $iApproved = 1)
919
    {
920
        $rStmt = Db::getInstance()->prepare('INSERT INTO' . Db::prefix(DbTableName::MEMBER_BACKGROUND) . '(profileId, file, approved) VALUES (:profileId, :file, :approved)');
921
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
922
        $rStmt->bindValue(':file', $sFile, \PDO::PARAM_STR);
923
        $rStmt->bindValue(':approved', $iApproved, \PDO::PARAM_INT);
924
925
        return $rStmt->execute();
926
    }
927
928
    /**
929
     * Delete profile background.
930
     *
931
     * @param int $iProfileId
932
     *
933
     * @return bool
934
     */
935
    public function deleteBackground($iProfileId)
936
    {
937
        $rStmt = Db::getInstance()->prepare('DELETE FROM' . Db::prefix(DbTableName::MEMBER_BACKGROUND) . 'WHERE profileId = :profileId');
938
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
939
        return $rStmt->execute();
940
    }
941
942
    /**
943
     * Delete User.
944
     *
945
     * @param int $iProfileId
946
     * @param string $sUsername
947
     *
948
     * @return void
949
     */
950
    public function delete($iProfileId, $sUsername)
951
    {
952
        $sUsername = (string)$sUsername;
953
        $iProfileId = (int)$iProfileId;
954
955
        if ($sUsername === PH7_GHOST_USERNAME) {
956
            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...
957
        }
958
959
        $oDb = Db::getInstance();
960
961
        // DELETE MESSAGES
962
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MESSAGE) . 'WHERE sender = ' . $iProfileId);
963
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MESSAGE) . 'WHERE recipient = ' . $iProfileId);
964
965
        // DELETE MESSAGES OF MESSENGER
966
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MESSENGER) . 'WHERE fromUser = ' . Db::getInstance()->quote($sUsername));
967
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MESSENGER) . 'WHERE toUser = ' . Db::getInstance()->quote($sUsername));
968
969
        // DELETE PROFILE COMMENTS
970
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_PROFILE) . 'WHERE sender = ' . $iProfileId);
971
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_PROFILE) . 'WHERE recipient = ' . $iProfileId);
972
973
        // DELETE PICTURE COMMENTS
974
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_PICTURE) . 'WHERE sender = ' . $iProfileId);
975
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_PICTURE) . 'WHERE recipient = ' . $iProfileId);
976
977
        // DELETE VIDEO COMMENTS
978
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_VIDEO) . 'WHERE sender = ' . $iProfileId);
979
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_VIDEO) . 'WHERE recipient = ' . $iProfileId);
980
981
        // DELETE NOTE COMMENTS
982
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_NOTE) . 'WHERE sender = ' . $iProfileId);
983
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_NOTE) . 'WHERE recipient = ' . $iProfileId);
984
985
        // DELETE BLOG COMMENTS
986
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_BLOG) . 'WHERE sender = ' . $iProfileId);
987
988
        // DELETE GAME COMMENTS
989
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::COMMENT_GAME) . 'WHERE sender = ' . $iProfileId);
990
991
        // DELETE PICTURES ALBUMS AND PICTURES
992
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::PICTURE) . 'WHERE profileId = ' . $iProfileId);
993
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::ALBUM_PICTURE) . 'WHERE profileId = ' . $iProfileId);
994
995
        // DELETE VIDEOS ALBUMS AND VIDEOS
996
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::VIDEO) . 'WHERE profileId = ' . $iProfileId);
997
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::ALBUM_VIDEO) . 'WHERE profileId = ' . $iProfileId);
998
999
        // DELETE FRIENDS
1000
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_FRIEND) . 'WHERE profileId = ' . $iProfileId);
1001
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_FRIEND) . 'WHERE friendId = ' . $iProfileId);
1002
1003
        // DELETE WALL
1004
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_WALL) . 'WHERE profileId = ' . $iProfileId);
1005
1006
        // DELETE BACKGROUND
1007
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_BACKGROUND) . 'WHERE profileId = ' . $iProfileId);
1008
1009
        // DELETE NOTES
1010
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::NOTE_CATEGORY) . 'WHERE profileId = ' . $iProfileId);
1011
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::NOTE) . 'WHERE profileId = ' . $iProfileId);
1012
1013
        // DELETE LIKE
1014
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::LIKE) . 'WHERE keyId LIKE ' . Db::getInstance()->quote('%' . $sUsername . '.html'));
1015
1016
        // DELETE PROFILE VISITS
1017
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_WHO_VIEW) . 'WHERE profileId = ' . $iProfileId);
1018
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_WHO_VIEW) . 'WHERE visitorId = ' . $iProfileId);
1019
1020
        // DELETE REPORT
1021
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::REPORT) . 'WHERE spammerId = ' . $iProfileId);
1022
1023
        // DELETE TOPICS of FORUMS
1024
        /*
1025
        No! Ghost Profile is ultimately the best solution!
1026
        WARNING: Do not change this part of code without asking permission to Pierre-Henry Soria
1027
        */
1028
        //$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...
1029
        //$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...
1030
1031
        // DELETE NOTIFICATIONS
1032
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_NOTIFICATION) . 'WHERE profileId = ' . $iProfileId . ' LIMIT 1');
1033
1034
        // DELETE PRIVACY SETTINGS
1035
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_PRIVACY) . 'WHERE profileId = ' . $iProfileId . ' LIMIT 1');
1036
1037
        // DELETE INFO FIELDS
1038
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER_INFO) . 'WHERE profileId = ' . $iProfileId . ' LIMIT 1');
1039
1040
        // DELETE USER
1041
        $oDb->exec('DELETE FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = ' . $iProfileId . ' LIMIT 1');
1042
1043
        unset($oDb); // Destruction of the object
1044
    }
1045
1046
    /**
1047
     * @param string $sUsernameSearch
1048
     * @param string $sTable Default DbTableName::MEMBER
1049
     *
1050
     * @return array data of users (profileId, username, sex)
1051
     */
1052
    public function getUsernameList($sUsernameSearch, $sTable = DbTableName::MEMBER)
1053
    {
1054
        Various::checkModelTable($sTable);
1055
1056
        $rStmt = Db::getInstance()->prepare('SELECT profileId, username, sex FROM' . Db::prefix($sTable) . 'WHERE username <> :ghostUsername AND username LIKE :username');
1057
        $rStmt->bindValue(':ghostUsername', PH7_GHOST_USERNAME, \PDO::PARAM_STR);
1058
        $rStmt->bindValue(':username', '%' . $sUsernameSearch . '%', \PDO::PARAM_STR);
1059
        $rStmt->execute();
1060
        $aRow = $rStmt->fetchAll(\PDO::FETCH_OBJ);
1061
        Db::free($rStmt);
1062
1063
        return $aRow;
1064
    }
1065
1066
    /**
1067
     * Get (all) profile data.
1068
     *
1069
     * @param string $sOrder
1070
     * @param int|null $iOffset
1071
     * @param int|null $iLimit
1072
     *
1073
     * @return array Data of users
1074
     */
1075
    public function getProfiles($sOrder = SearchCoreModel::LAST_ACTIVITY, $iOffset = null, $iLimit = null)
1076
    {
1077
        $bIsLimit = $iOffset !== null && $iLimit !== null;
1078
        $bHideUserLogged = !empty($this->iProfileId);
1079
        $bOnlyAvatarsSet = (bool)DbConfig::getSetting('profileWithAvatarSet');
1080
1081
        $iOffset = (int)$iOffset;
1082
        $iLimit = (int)$iLimit;
1083
1084
        $sOrder = SearchCoreModel::order($sOrder, SearchCoreModel::DESC);
1085
1086
        $sSqlLimit = $bIsLimit ? 'LIMIT :offset, :limit' : '';
1087
        $sSqlHideLoggedProfile = $bHideUserLogged ? ' AND (m.profileId <> :profileId)' : '';
1088
        $sSqlShowOnlyWithAvatars = $bOnlyAvatarsSet ? $this->getUserWithAvatarOnlySql() : '';
1089
1090
        $rStmt = Db::getInstance()->prepare(
1091
            'SELECT * FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m LEFT JOIN' . Db::prefix(DbTableName::MEMBER_PRIVACY) . 'AS p USING(profileId)
1092
            LEFT JOIN' . Db::prefix(DbTableName::MEMBER_INFO) . 'AS i USING(profileId) WHERE (username <> :ghostUsername) AND
1093
            (searchProfile = \'yes\') AND (username IS NOT NULL) AND (firstName IS NOT NULL) AND (sex IS NOT NULL) AND (matchSex IS NOT NULL) AND
1094
            (country IS NOT NULL) AND (city IS NOT NULL) AND (groupId <> :visitorGroup) AND (groupId <> :pendingGroup) AND (ban = 0)' .
1095
            $sSqlHideLoggedProfile . $sSqlShowOnlyWithAvatars . $sOrder . $sSqlLimit
1096
        );
1097
1098
        $rStmt->bindValue(':ghostUsername', PH7_GHOST_USERNAME, \PDO::PARAM_STR);
1099
        $rStmt->bindValue(':visitorGroup', self::VISITOR_GROUP, \PDO::PARAM_INT);
1100
        $rStmt->bindValue(':pendingGroup', self::PENDING_GROUP, \PDO::PARAM_INT);
1101
1102
        if ($bHideUserLogged) {
1103
            $rStmt->bindValue(':profileId', $this->iProfileId, \PDO::PARAM_INT);
1104
        }
1105
1106
        if ($bIsLimit) {
1107
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
1108
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
1109
        }
1110
1111
        $rStmt->execute();
1112
        $aRow = $rStmt->fetchAll(\PDO::FETCH_OBJ);
1113
        Db::free($rStmt);
1114
1115
        return $aRow;
1116
    }
1117
1118
    /**
1119
     * Get users from the location data.
1120
     *
1121
     * @param string $sCountryCode The country code. e.g. US, CA, FR, ES, BE, NL
1122
     * @param string $sCity
1123
     * @param bool $bCount
1124
     * @param string $sOrder
1125
     * @param int|null $iOffset
1126
     * @param int|null $iLimit
1127
     *
1128
     * @return array|stdClass|int Object with the users list returned or integer for the total number users returned.
1129
     */
1130
    public function getGeoProfiles($sCountryCode, $sCity, $bCount, $sOrder, $iOffset = null, $iLimit = null)
1131
    {
1132
        $bLimit = $iOffset !== null && $iLimit !== null;
1133
1134
        $bCount = (bool)$bCount;
1135
        $iOffset = (int)$iOffset;
1136
        $iLimit = (int)$iLimit;
1137
1138
        $sOrder = !$bCount ? SearchCoreModel::order($sOrder, SearchCoreModel::DESC) : '';
1139
1140
        $sSqlLimit = (!$bCount || $bLimit) ? 'LIMIT :offset, :limit' : '';
1141
        $sSqlSelect = !$bCount ? '*' : 'COUNT(m.profileId)';
1142
1143
        $sSqlCity = !empty($sCity) ? 'AND (city LIKE :city)' : '';
1144
1145
        $rStmt = Db::getInstance()->prepare(
1146
            'SELECT ' . $sSqlSelect . ' FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m LEFT JOIN' . Db::prefix(DbTableName::MEMBER_INFO) . 'AS i USING(profileId)
1147
            WHERE (username <> :ghostUsername) AND (country = :country) ' . $sSqlCity . ' AND (username IS NOT NULL)
1148
            AND (firstName IS NOT NULL) AND (sex IS NOT NULL) AND (matchSex IS NOT NULL) AND (country IS NOT NULL)
1149
            AND (city IS NOT NULL) AND (groupId <> :visitorGroup) AND (groupId <> :pendingGroup) AND (ban = 0)' . $sOrder . $sSqlLimit
1150
        );
1151
1152
        $rStmt->bindValue(':ghostUsername', PH7_GHOST_USERNAME, \PDO::PARAM_STR);
1153
        $rStmt->bindValue(':visitorGroup', self::VISITOR_GROUP, \PDO::PARAM_INT);
1154
        $rStmt->bindValue(':pendingGroup', self::PENDING_GROUP, \PDO::PARAM_INT);
1155
1156
        $rStmt->bindParam(':country', $sCountryCode, \PDO::PARAM_STR, 2);
1157
1158
        if (!empty($sCity)) {
1159
            $rStmt->bindValue(':city', '%' . $sCity . '%', \PDO::PARAM_STR);
1160
        }
1161
1162
        if (!$bCount || $bLimit) {
1163
            $rStmt->bindParam(':offset', $iOffset, \PDO::PARAM_INT);
1164
            $rStmt->bindParam(':limit', $iLimit, \PDO::PARAM_INT);
1165
        }
1166
1167
        $rStmt->execute();
1168
1169
        if (!$bCount) {
1170
            $aRow = $rStmt->fetchAll(\PDO::FETCH_OBJ);
1171
            Db::free($rStmt);
1172
1173
            return $aRow;
1174
        }
1175
1176
        $iTotalUsers = (int)$rStmt->fetchColumn();
1177
        Db::free($rStmt);
1178
1179
        return $iTotalUsers;
1180
    }
1181
1182
    /**
1183
     * Updating the privacy settings.
1184
     *
1185
     * @param int $iProfileId
1186
     *
1187
     * @return stdClass
1188
     */
1189
    public function getPrivacySetting($iProfileId)
1190
    {
1191
        $this->cache->start(self::CACHE_GROUP, 'privacySetting' . $iProfileId, static::CACHE_TIME);
1192
1193
        if (!$oData = $this->cache->get()) {
1194
            $iProfileId = (int)$iProfileId;
1195
1196
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix(DbTableName::MEMBER_PRIVACY) . 'WHERE profileId = :profileId LIMIT 1');
1197
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1198
            $rStmt->execute();
1199
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
1200
            Db::free($rStmt);
1201
            $this->cache->put($oData);
1202
        }
1203
1204
        return $oData;
1205
    }
1206
1207
    /**
1208
     * Get the Profile ID of a user.
1209
     *
1210
     * @param string|null $sEmail Default NULL
1211
     * @param string|null $sUsername Default NULL
1212
     * @param string $sTable Default DbTableName::MEMBER
1213
     *
1214
     * @return int|bool The Member ID if it is found or FALSE if not found.
1215
     */
1216
    public function getId($sEmail = null, $sUsername = null, $sTable = DbTableName::MEMBER)
1217
    {
1218
        $this->cache->start(self::CACHE_GROUP, 'id' . $sEmail . $sUsername . $sTable, static::CACHE_TIME);
1219
1220
        if (!$iProfileId = $this->cache->get()) {
1221
            Various::checkModelTable($sTable);
1222
1223
            if (!empty($sEmail)) {
1224
                $rStmt = Db::getInstance()->prepare('SELECT profileId FROM' . Db::prefix($sTable) . 'WHERE email = :email LIMIT 1');
1225
                $rStmt->bindValue(':email', $sEmail, \PDO::PARAM_STR);
1226
            } else {
1227
                $rStmt = Db::getInstance()->prepare('SELECT profileId FROM' . Db::prefix($sTable) . 'WHERE username = :username LIMIT 1');
1228
                $rStmt->bindValue(':username', $sUsername, \PDO::PARAM_STR);
1229
            }
1230
1231
            $rStmt->execute();
1232
1233
            if ($rStmt->rowCount() === 0) {
1234
                return false;
1235
            }
1236
1237
            $iProfileId = (int)$rStmt->fetchColumn();
1238
            Db::free($rStmt);
1239
            $this->cache->put($iProfileId);
1240
        }
1241
1242
        return $iProfileId;
1243
    }
1244
1245
    /**
1246
     * @param int $iProfileId
1247
     * @param string $sTable Default DbTableName::MEMBER
1248
     *
1249
     * @return string The email address of a member
1250
     */
1251
    public function getEmail($iProfileId, $sTable = DbTableName::MEMBER)
1252
    {
1253
        $this->cache->start(self::CACHE_GROUP, 'email' . $iProfileId . $sTable, static::CACHE_TIME);
1254
1255
        if (!$sEmail = $this->cache->get()) {
1256
            Various::checkModelTable($sTable);
1257
1258
            $rStmt = Db::getInstance()->prepare('SELECT email FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1259
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1260
            $rStmt->execute();
1261
            $sEmail = $rStmt->fetchColumn();
1262
            Db::free($rStmt);
1263
1264
            $this->cache->put($sEmail);
1265
        }
1266
1267
        return $sEmail;
1268
    }
1269
1270
    /**
1271
     * Retrieves the username from the user ID.
1272
     *
1273
     * @param int $iProfileId
1274
     * @param string $sTable Default DbTableName::MEMBER
1275
     *
1276
     * @return string The Username of member
1277
     */
1278
    public function getUsername($iProfileId, $sTable = DbTableName::MEMBER)
1279
    {
1280
        if ($iProfileId === PH7_ADMIN_ID) {
1281
            return t('Administration of %site_name%');
1282
        }
1283
1284
        $this->cache->start(self::CACHE_GROUP, 'username' . $iProfileId . $sTable, static::CACHE_TIME);
1285
1286
        if (!$sUsername = $this->cache->get()) {
1287
            Various::checkModelTable($sTable);
1288
1289
            $rStmt = Db::getInstance()->prepare('SELECT username FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1290
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1291
            $rStmt->execute();
1292
            $sUsername = $rStmt->fetchColumn();
1293
            Db::free($rStmt);
1294
1295
            $this->cache->put($sUsername);
1296
        }
1297
1298
        return $sUsername;
1299
    }
1300
1301
    /**
1302
     * Retrieves the first name from the user ID.
1303
     *
1304
     * @param int $iProfileId
1305
     * @param string $sTable Default DbTableName::MEMBER
1306
     *
1307
     * @return string The first name of member
1308
     */
1309
    public function getFirstName($iProfileId, $sTable = DbTableName::MEMBER)
1310
    {
1311
        $this->cache->start(self::CACHE_GROUP, 'firstName' . $iProfileId . $sTable, static::CACHE_TIME);
1312
1313
        if (!$sFirstName = $this->cache->get()) {
1314
            Various::checkModelTable($sTable);
1315
1316
            $rStmt = Db::getInstance()->prepare('SELECT firstName FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1317
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1318
            $rStmt->execute();
1319
            $sFirstName = $rStmt->fetchColumn();
1320
            Db::free($rStmt);
1321
1322
            $this->cache->put($sFirstName);
1323
        }
1324
1325
        return $sFirstName;
1326
    }
1327
1328
    /**
1329
     * Get Gender (sex) of a user.
1330
     *
1331
     * @param int|null $iProfileId Default NULL
1332
     * @param string $sUsername Default NULL
1333
     * @param string $sTable Default DbTableName::MEMBER
1334
     *
1335
     * @return string The sex of a member
1336
     */
1337
    public function getSex($iProfileId = null, $sUsername = null, $sTable = DbTableName::MEMBER)
1338
    {
1339
        $this->cache->start(self::CACHE_GROUP, 'sex' . $iProfileId . $sUsername . $sTable, static::CACHE_TIME);
1340
1341
        if (!$sSex = $this->cache->get()) {
1342
            Various::checkModelTable($sTable);
1343
1344
            if (!empty($iProfileId)) {
1345
                $rStmt = Db::getInstance()->prepare('SELECT sex FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1346
                $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1347
            } else {
1348
                $rStmt = Db::getInstance()->prepare('SELECT sex FROM' . Db::prefix($sTable) . 'WHERE username=:username LIMIT 1');
1349
                $rStmt->bindValue(':username', $sUsername, \PDO::PARAM_STR);
1350
            }
1351
1352
            $rStmt->execute();
1353
            $sSex = $rStmt->fetchColumn();
1354
            Db::free($rStmt);
1355
1356
            $this->cache->put($sSex);
1357
        }
1358
1359
        return $sSex;
1360
    }
1361
1362
    /**
1363
     * Get Match sex for a member (so only from the Members table, because Affiliates and Admins don't have match sex).
1364
     *
1365
     * @param int $iProfileId
1366
     *
1367
     * @return string The User's birthdate.
1368
     */
1369
    public function getMatchSex($iProfileId)
1370
    {
1371
        $this->cache->start(self::CACHE_GROUP, 'matchsex' . $iProfileId, static::CACHE_TIME);
1372
1373
        if (!$sMatchSex = $this->cache->get()) {
1374
            $rStmt = Db::getInstance()->prepare('SELECT matchSex FROM' . Db::prefix(DbTableName::MEMBER) . 'WHERE profileId = :profileId LIMIT 1');
1375
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1376
            $rStmt->execute();
1377
            $sMatchSex = $rStmt->fetchColumn();
1378
            Db::free($rStmt);
1379
1380
            $this->cache->put($sMatchSex);
1381
        }
1382
1383
        return $sMatchSex;
1384
    }
1385
1386
    /**
1387
     * Get Date of Birth of a user.
1388
     *
1389
     * @param int $iProfileId
1390
     * @param string $sTable Default DbTableName::MEMBER
1391
     *
1392
     * @return string The User's birthdate.
1393
     */
1394
    public function getBirthDate($iProfileId, $sTable = DbTableName::MEMBER)
1395
    {
1396
        $this->cache->start(self::CACHE_GROUP, 'birthdate' . $iProfileId . $sTable, static::CACHE_TIME);
1397
1398
        if (!$sBirthDate = $this->cache->get()) {
1399
            Various::checkModelTable($sTable);
1400
1401
            $rStmt = Db::getInstance()->prepare('SELECT birthDate FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1402
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1403
            $rStmt->execute();
1404
            $sBirthDate = $rStmt->fetchColumn();
1405
            Db::free($rStmt);
1406
1407
            $this->cache->put($sBirthDate);
1408
        }
1409
1410
        return $sBirthDate;
1411
    }
1412
1413
    /**
1414
     * Get user's group.
1415
     *
1416
     * @param int $iProfileId
1417
     * @param string sTable Default DbTableName::MEMBER
1418
     *
1419
     * @return int The Group ID of a member
1420
     */
1421
    public function getGroupId($iProfileId, $sTable = DbTableName::MEMBER)
1422
    {
1423
        $this->cache->start(self::CACHE_GROUP, 'groupId' . $iProfileId . $sTable, static::CACHE_TIME);
1424
1425
        if (!$iGroupId = $this->cache->get()) {
1426
            Various::checkModelTable($sTable);
1427
1428
            $rStmt = Db::getInstance()->prepare('SELECT groupId FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1429
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1430
            $rStmt->execute();
1431
            $iGroupId = (int)$rStmt->fetchColumn();
1432
            Db::free($rStmt);
1433
1434
            $this->cache->put($iGroupId);
1435
        }
1436
1437
        return $iGroupId;
1438
    }
1439
1440
    /**
1441
     * Get the membership(s) data.
1442
     *
1443
     * @param int|null $iGroupId Group ID. Select only the specific membership from a group ID.
1444
     *
1445
     * @return stdClass|array The membership(s) data.
1446
     */
1447
    public function getMemberships($iGroupId = null)
1448
    {
1449
        $this->cache->start(self::CACHE_GROUP, DbTableName::MEMBERSHIP . $iGroupId, static::CACHE_TIME);
1450
1451
        if (!$mData = $this->cache->get()) {
1452
            $bIsGroupId = !empty($iGroupId);
1453
            $sSqlGroup = $bIsGroupId ? ' WHERE groupId = :groupId ' : ' ';
1454
1455
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix(DbTableName::MEMBERSHIP) . $sSqlGroup . 'ORDER BY enable ASC, groupId ASC');
1456
            if (!empty($iGroupId)) {
1457
                $rStmt->bindValue(':groupId', $iGroupId, \PDO::PARAM_INT);
1458
            }
1459
            $rStmt->execute();
1460
            $mData = $bIsGroupId ? $rStmt->fetch(\PDO::FETCH_OBJ) : $rStmt->fetchAll(\PDO::FETCH_OBJ);
1461
            Db::free($rStmt);
1462
            $this->cache->put($mData);
1463
        }
1464
1465
        return $mData;
1466
    }
1467
1468
    /**
1469
     * Get the membership details of a user.
1470
     *
1471
     * @param int $iProfileId
1472
     *
1473
     * @return stdClass The membership detais.
1474
     */
1475
    public function getMembershipDetails($iProfileId)
1476
    {
1477
        $this->cache->start(self::CACHE_GROUP, 'membershipDetails' . $iProfileId, static::CACHE_TIME);
1478
1479
        if (!$oData = $this->cache->get()) {
1480
            $sSql = 'SELECT m.*, g.expirationDays, g.name AS membershipName FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m INNER JOIN ' . Db::prefix(DbTableName::MEMBERSHIP) .
1481
                'AS g USING(groupId) WHERE profileId = :profileId LIMIT 1';
1482
1483
            $rStmt = Db::getInstance()->prepare($sSql);
1484
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1485
            $rStmt->execute();
1486
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
1487
            Db::free($rStmt);
1488
            $this->cache->put($oData);
1489
        }
1490
1491
        return $oData;
1492
    }
1493
1494
    /**
1495
     * Check if membership is expired.
1496
     *
1497
     * @param int $iProfileId
1498
     * @param string $sCurrentTime In date format: 0000-00-00 00:00:00
1499
     *
1500
     * @return bool
1501
     */
1502
    public function checkMembershipExpiration($iProfileId, $sCurrentTime)
1503
    {
1504
        $sSqlQuery = 'SELECT m.profileId FROM' . Db::prefix(DbTableName::MEMBER) . 'AS m INNER JOIN' .
1505
            Db::prefix(DbTableName::MEMBERSHIP) . 'AS pay USING(groupId) WHERE
1506
            (pay.expirationDays = 0 OR DATE_ADD(m.membershipDate, INTERVAL pay.expirationDays DAY) >= :currentTime) AND
1507
            (m.profileId = :profileId) LIMIT 1';
1508
1509
        $rStmt = Db::getInstance()->prepare($sSqlQuery);
1510
1511
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1512
        $rStmt->bindValue(':currentTime', $sCurrentTime, \PDO::PARAM_INT);
1513
        $rStmt->execute();
1514
1515
        return $rStmt->rowCount() === 1;
1516
    }
1517
1518
    /**
1519
     * Update the membership group of a user.
1520
     *
1521
     * @param int $iNewGroupId The new ID of membership group.
1522
     * @param int $iProfileId The user ID.
1523
     * @param string|null $sDateTime In date format: 0000-00-00 00:00:00
1524
     *
1525
     * @return bool Returns TRUE on success or FALSE on failure.
1526
     */
1527
    public function updateMembership($iNewGroupId, $iProfileId, $sDateTime = null)
1528
    {
1529
        $bIsTime = !empty($sDateTime);
1530
1531
        $sSqlTime = $bIsTime ? ',membershipDate = :dateTime ' : ' ';
1532
1533
        $sSqlQuery = 'UPDATE' . Db::prefix(DbTableName::MEMBER) . 'SET groupId = :groupId' .
1534
            $sSqlTime . 'WHERE profileId = :profileId LIMIT 1';
1535
1536
        $rStmt = Db::getInstance()->prepare($sSqlQuery);
1537
        $rStmt->bindValue(':groupId', $iNewGroupId, \PDO::PARAM_INT);
1538
        $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1539
        if ($bIsTime) {
1540
            $rStmt->bindValue(':dateTime', $sDateTime, \PDO::PARAM_STR);
1541
        }
1542
1543
        return $rStmt->execute();
1544
    }
1545
1546
    /**
1547
     * Get Info Fields from profile ID.
1548
     *
1549
     * @param int $iProfileId
1550
     * @param string $sTable Default DbTableName::MEMBER_INFO
1551
     *
1552
     * @return stdClass
1553
     */
1554
    public function getInfoFields($iProfileId, $sTable = DbTableName::MEMBER_INFO)
1555
    {
1556
        $this->cache->start(self::CACHE_GROUP, 'infoFields' . $iProfileId . $sTable, static::CACHE_TIME);
1557
1558
        if (!$oData = $this->cache->get()) {
1559
            Various::checkModelTable($sTable);
1560
1561
            $rStmt = Db::getInstance()->prepare('SELECT * FROM' . Db::prefix($sTable) . 'WHERE profileId = :profileId LIMIT 1');
1562
            $rStmt->bindValue(':profileId', $iProfileId, \PDO::PARAM_INT);
1563
            $rStmt->execute();
1564
            $oColumns = $rStmt->fetch(\PDO::FETCH_OBJ);
1565
            Db::free($rStmt);
1566
1567
            $oData = new stdClass;
1568
            foreach ($oColumns as $sColumn => $sValue) {
1569
                if ($sColumn !== 'profileId') {
1570
                    $oData->$sColumn = $sValue;
1571
                }
1572
            }
1573
            $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...
1574
        }
1575
1576
        return $oData;
1577
    }
1578
1579
1580
    /**
1581
     * @param string $sTable DB country table name.
1582
     *
1583
     * @return array
1584
     *
1585
     * @throws PH7InvalidArgumentException
1586
     */
1587
    public function getCountries($sTable = DbTableName::MEMBER_COUNTRY)
1588
    {
1589
        $iNinetyDaysTime = 7776000;
1590
        $this->cache->start(self::CACHE_GROUP, 'countriesList' . $sTable, $iNinetyDaysTime);
1591
1592
        if (!$aCountries = $this->cache->get()) {
1593
            Various::checkModelTable($sTable);
1594
1595
            $sSqlQuery = 'SELECT countryCode FROM' . Db::prefix($sTable);
1596
            $rStmt = Db::getInstance()->prepare($sSqlQuery);
1597
            $rStmt->execute();
1598
            $aCountries = $rStmt->fetchAll(\PDO::FETCH_OBJ);
1599
            Db::free($rStmt);
1600
            $this->cache->put($aCountries);
1601
        }
1602
1603
        return $aCountries;
1604
    }
1605
1606
    /**
1607
     * Add countries for members
1608
     *
1609
     * @param string $sCountryCode e.g. en, fr, be, ru, nl, ...
1610
     *
1611
     * @return bool|int
1612
     *
1613
     * @throws PH7InvalidArgumentException If the table arg is incorrect.
1614
     */
1615
    public function addCountry($sCountryCode, $sTable = DbTableName::MEMBER_COUNTRY)
1616
    {
1617
        Various::checkModelTable($sTable);
1618
1619
        return $this->orm->insert($sTable, ['countryCode' => $sCountryCode]);
1620
    }
1621
1622
    /**
1623
     * @param string $sTable
1624
     *
1625
     * @throws PH7InvalidArgumentException If the table arg is incorrect.
1626
     */
1627
    public function clearCountries($sTable = DbTableName::MEMBER_COUNTRY)
1628
    {
1629
        Various::checkModelTable($sTable);
1630
1631
        $oDb = Db::getInstance();
1632
        $oDb->exec('TRUNCATE' . Db::prefix($sTable));
1633
        unset($oDb);
1634
    }
1635
1636
    /**
1637
     * @return string
1638
     */
1639
    public function getUserWithAvatarOnlySql()
1640
    {
1641
        return ' AND avatar IS NOT NULL AND approvedAvatar = 1';
1642
    }
1643
1644
    /**
1645
     * @param array $aSex
1646
     *
1647
     * @return string
1648
     */
1649
    private function getSexInClauseSql(array $aSex)
1650
    {
1651
        $sGender = '';
1652
1653
        foreach ($aSex as $sSex) {
1654
            if ($sSex === GenderTypeUserCore::MALE) {
1655
                $sGender .= "'" . GenderTypeUserCore::MALE . "',";
1656
            }
1657
1658
            if ($sSex === GenderTypeUserCore::FEMALE) {
1659
                $sGender .= "'" . GenderTypeUserCore::FEMALE . "',";
1660
            }
1661
1662
            if ($sSex === GenderTypeUserCore::COUPLE) {
1663
                $sGender .= "'" . GenderTypeUserCore::COUPLE . "',";
1664
            }
1665
        }
1666
1667
        $sInClauseValue = rtrim($sGender, ','); // Removes the last extra comma
1668
1669
        if (!empty($sInClauseValue)) {
1670
            return ' AND sex IN (' . $sInClauseValue . ') ';
1671
        }
1672
1673
        return '';
1674
    }
1675
1676
    /**
1677
     * Clone is set to private to stop cloning.
1678
     */
1679
    private function __clone()
1680
    {
1681
    }
1682
}
1683