Completed
Push — next ( 3119bc...f5cb54 )
by Thomas
70:06 queued 62:34
created

cachelog::validateDate()   C

Complexity

Conditions 15
Paths 13

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 24
nc 13
nop 6
dl 0
loc 33
rs 5.0504
c 0
b 0
f 0

How to fix   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
 * for license information see LICENSE.md
4
 *
5
 *
6
 *   get/set has to be committed with save
7
 *   add/remove etc. is executed instantly
8
 ***************************************************************************/
9
10
require_once __DIR__ . '/logtypes.inc.php';
11
12
class cachelog
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
{
14
    const LOGTYPE_FOUND = 1;
15
    const LOGTYPE_NOTFOUND = 2;
16
    const LOGTYPE_NOTE = 3;
17
    const LOGTYPE_ATTENDED = 7;
18
    const LOGTYPE_WILLATTEND = 8;
19
    const LOGTYPE_ARCHIVED = 9;
20
    const LOGTYPE_ACTIVE = 10;
21
    const LOGTYPE_DISABLED = 11;
22
    const LOGTYPE_LOCKED = 13;
23
    const LOGTYPE_LOCKED_INVISIBLE = 14;
24
25
    public $nLogId = 0;
26
27
    public $reCacheLog;
28
29
    public static function logIdFromUUID($uuid)
30
    {
31
        $cacheid = sql_value("SELECT `id` FROM `cache_logs` WHERE `uuid`='&1'", 0, $uuid);
32
33
        return $cacheid;
34
    }
35
36
    public static function fromUUID($uuid)
37
    {
38
        $logid = self::logIdFromUUID($uuid);
39
        if ($logid == 0) {
40
            return null;
41
        }
42
43
        return new cachelog($logid);
44
    }
45
46 View Code Duplication
    public static function createNew($nCacheId, $nUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
    {
48
        global $opt;
49
50
        // check if user is allowed to log this cache!
51
        $cache = new cache($nCacheId);
52
        if ($cache->exist() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
53
            return false;
54
        }
55
        if ($cache->allowLog() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
56
            return false;
57
        }
58
59
        $oCacheLog = new cachelog(ID_NEW);
60
        $oCacheLog->setUserId($nUserId);
61
        $oCacheLog->setCacheId($nCacheId);
62
        $oCacheLog->setNode($opt['logic']['node']['id']);
63
64
        return $oCacheLog;
65
    }
66
67 View Code Duplication
    public static function createNewFromCache($oCache, $nUserId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69
        global $opt;
70
71
        // check if user is allowed to log this cache!
72
        if ($oCache->exist() == false) {
73
            return false;
74
        }
75
        if ($oCache->allowLog() == false) {
76
            return false;
77
        }
78
79
        $oCacheLog = new cachelog(ID_NEW);
80
        $oCacheLog->setUserId($nUserId);
81
        $oCacheLog->setCacheId($oCache->getCacheId());
82
        $oCacheLog->setNode($opt['logic']['node']['id']);
83
84
        return $oCacheLog;
85
    }
86
87
    public function __construct($nNewLogId = ID_NEW)
88
    {
89
        $this->reCacheLog = new rowEditor('cache_logs');
90
        $this->reCacheLog->addPKInt('id', null, false, RE_INSERT_AUTOINCREMENT);
91
        $this->reCacheLog->addString('uuid', '', false, RE_INSERT_AUTOUUID);
92
        $this->reCacheLog->addInt('node', 0, false);
93
        $this->reCacheLog->addDate('date_created', time(), true, RE_INSERT_IGNORE);
94
        $this->reCacheLog->addDate('last_modified', time(), true, RE_INSERT_IGNORE);
95
        $this->reCacheLog->addInt('cache_id', 0, false);
96
        $this->reCacheLog->addInt('user_id', 0, false);
97
        $this->reCacheLog->addInt('type', 0, false);
98
        $this->reCacheLog->addInt('oc_team_comment', 0, false);
99
        $this->reCacheLog->addDate('date', time(), false);
100
        $this->reCacheLog->addInt('needs_maintenance', 0, false);
101
        $this->reCacheLog->addInt('listing_outdated', 0, false);
102
        $this->reCacheLog->addString('text', '', false);
103
        $this->reCacheLog->addInt('text_html', 1, false);
104
        $this->reCacheLog->addInt('text_htmledit', 1, false);
105
        $this->reCacheLog->addInt('owner_notified', 0, false);
106
        $this->reCacheLog->addInt('picture', 0, false);
107
108
        $this->nLogId = $nNewLogId + 0;
109
110
        if ($nNewLogId == ID_NEW) {
111
            $this->reCacheLog->addNew(null);
112
        } else {
113
            $this->reCacheLog->load($this->nLogId);
114
        }
115
    }
116
117
    public function exist()
118
    {
119
        return $this->reCacheLog->exist();
120
    }
121
122
    public function getLogId()
123
    {
124
        return $this->nLogId;
125
    }
126
127
    public function getUserId()
128
    {
129
        return $this->reCacheLog->getValue('user_id');
130
    }
131
132
    public function setUserId($value)
133
    {
134
        return $this->reCacheLog->setValue('user_id', $value);
135
    }
136
137
    public function getCacheId()
138
    {
139
        return $this->reCacheLog->getValue('cache_id');
140
    }
141
142
    public function setCacheId($value)
143
    {
144
        return $this->reCacheLog->setValue('cache_id', $value);
145
    }
146
147
    public function getType()
148
    {
149
        return $this->reCacheLog->getValue('type');
150
    }
151
152
    public function setType($value, $force = false)
153
    {
154
        if (!$force) {
155
            $nValidLogTypes = $this->getValidLogTypes();
156
            if (array_search($value, $nValidLogTypes) === false) {
157
                return false;
158
            }
159
        }
160
161
        return $this->reCacheLog->setValue('type', $value);
162
    }
163
164
    public function getOcTeamComment()
165
    {
166
        return $this->reCacheLog->getValue('oc_team_comment');
167
    }
168
169
    public function setOcTeamComment($value)
170
    {
171
        return $this->reCacheLog->setValue('oc_team_comment', $value);
172
    }
173
174
    public function getDate()
175
    {
176
        return $this->reCacheLog->getValue('date');
177
    }
178
179
    public function setDate($value)
180
    {
181
        return $this->reCacheLog->setValue('date', $value);
182
    }
183
184
    public function getNeedsMaintenance()
185
    {
186
        return $this->reCacheLog->getValue('needs_maintenance');
187
    }
188
189
    public function setNeedsMaintenance($value)
190
    {
191
        return $this->reCacheLog->setValue('needs_maintenance', $value);
192
    }
193
194
    public function getListingOutdated()
195
    {
196
        return $this->reCacheLog->getValue('listing_outdated');
197
    }
198
199
    public function setListingOutdated($value)
200
    {
201
        return $this->reCacheLog->setValue('listing_outdated', $value);
202
    }
203
204
    public function getText()
205
    {
206
        return $this->reCacheLog->getValue('text');
207
    }
208
209
    public function setText($value)
210
    {
211
        return $this->reCacheLog->setValue('text', $value);
212
    }
213
214
    public function getTextHtml()
215
    {
216
        return $this->reCacheLog->getValue('text_html');
217
    }
218
219
    public function setTextHtml($value)
220
    {
221
        return $this->reCacheLog->setValue('text_html', $value ? 1 : 0);
222
    }
223
224
    public function getTextHtmlEdit()
225
    {
226
        return $this->reCacheLog->getValue('text_html');
227
    }
228
229
    public function setTextHtmlEdit($value)
230
    {
231
        return $this->reCacheLog->setValue('text_htmledit', $value ? 1 : 0);
232
    }
233
234
    public function getUUID()
235
    {
236
        return $this->reCacheLog->getValue('uuid');
237
    }
238
239
    public function getLastModified()
240
    {
241
        return $this->reCacheLog->getValue('last_modified');
242
    }
243
244
    public function getDateCreated()
245
    {
246
        return $this->reCacheLog->getValue('date_created');
247
    }
248
249
    public function getNode()
250
    {
251
        return $this->reCacheLog->getValue('node');
252
    }
253
254
    public function setNode($value)
255
    {
256
        return $this->reCacheLog->setValue('node', $value);
257
    }
258
259
    public function getOwnerNotified()
260
    {
261
        return $this->reCacheLog->getValue('owner_notified') != 0;
262
    }
263
264
    /**
265
     * @param integer $value
266
     * @return bool
267
     */
268
    public function setOwnerNotified($value)
269
    {
270
        return $this->reCacheLog->setValue('owner_notified', $value ? 1 : 0);
271
    }
272
273
    public function getAnyChanged()
274
    {
275
        return $this->reCacheLog->getAnyChanged();
276
    }
277
278
    // return if successfull (with insert)
279
    public function save()
280
    {
281
        // additional safeguard against setting unallowd log flags
282
        if (!sql_value(
283
            "SELECT `maintenance_logs` FROM `log_types` WHERE `id`='&1'",
284
            false,
285
            $this->getType()
286
        )
287
        ) {
288
            $this->setNeedsMaintenance(false);
289
            $this->setListingOutdated(false);
290
        }
291
292
        sql_slave_exclude();
293
        $saved = $this->reCacheLog->save();
294
        if ($saved && $this->nLogId == ID_NEW) {
295
            $this->nLogId = $this->reCacheLog->getValue('id');
296
        }
297
298
        return $saved;
299
    }
300
301
    public function updatePictureStat()
302
    {
303
        sql(
304
            "UPDATE `cache_logs` SET `picture` =
305
               (SELECT COUNT(*) FROM `pictures` WHERE `object_type`=1 AND `object_id`='&1')
306
             WHERE `id`= '&1'",
307
            $this->getLogId()
308
        );
309
    }
310
311 View Code Duplication
    public function allowView()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
312
    {
313
        global $login;
314
315
        $login->verify();
316
        $allow = sql_value(
317
            "SELECT `cache_status`.`allow_user_view`
318
             FROM `caches`
319
             INNER JOIN `cache_status`
320
                 ON `caches`.`status`=`cache_status`.`id`
321
             WHERE `caches`.`cache_id`='&1'",
322
            0,
323
            $this->getCacheId()
324
        );
325
        if ($allow == 1) {
326
            return true;
327
        } elseif ($login->userid == sql_value(
328
            "SELECT `user_id` FROM `caches` WHERE `cache_id`='&1'",
329
            0,
330
            $this->getCacheId()
331
        )) {
332
            return true;
333
        }
334
335
        return false;
336
    }
337
338 View Code Duplication
    public function allowEdit()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
    {
340
        global $login;
341
342
        $login->verify();
343
        if ($this->getUserId() == $login->userid) {
344
            return true;
345
        }
346
347
        return false;
348
    }
349
350
    public function getValidLogTypes()
351
    {
352
        $cache = new cache($this->getCacheId());
353
        if ($cache->exist() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
354
            return [];
355
        }
356
        // if ($cache->allowLog() == false)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
357
        //     return array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% 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...
358
        // Logic Error - log types are still valid when no NEW logs are allowed for the cache.
359
        // (Would e.g. block admin logs and log-type restoring for locked caches.)
360
        return get_cache_log_types($this->getCacheId(), $this->getType());  // depends on userid
361
    }
362
363
    public static function isDuplicate($cacheId, $userId, $logType, $logDate, $logText)
364
    {
365
        // get info if exact the same values are already in database
366
        return sql_value(
367
            "SELECT COUNT(`id`)
368
             FROM `cache_logs`
369
             WHERE
370
                `cache_id`='&1'
371
                AND `user_id`='&2'
372
                AND `type`='&3'
373
                AND `date`='&4'
374
                AND `text`='&5'",
375
            0,
376
            $cacheId,
377
            $userId,
378
            $logType,
379
            $logDate,
380
            $logText
381
        ) != 0;
382
    }
383
384
    public static function isMasslogging($userId)
385
    {
386
        global $opt;
387
388
        // check for probably wrong-dated mass logs
389
390
        $rs = sql(
391
            "
392
                    SELECT `date`, `text`
393
                    FROM `cache_logs`
394
                    WHERE `id`= (
395
                        SELECT `id`
396
                        FROM `cache_logs`
397
                        WHERE `user_id`='&1'
398
                        ORDER BY `date_created` DESC,
399
                                 `id` DESC
400
                        LIMIT 1)",
401
            $userId
402
        );
403
404
        $rLastLog = sql_fetch_array($rs);
405
        sql_free_result($rs);
406
407
        if ($rLastLog) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $rLastLog of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
408
            $rs = sql(
409
                "
410
                        SELECT COUNT(*) AS `masslogs`
411
                        FROM `cache_logs`
412
                        WHERE `user_id`='&1'
413
                            AND `date`='&2'
414
                            AND `text`='&3'",
415
                $userId,
416
                $rLastLog['date'],
417
                $rLastLog['text']
418
            );
419
420
            $r = sql_fetch_array($rs);
421
            $masslogs = $r['masslogs'];
422
            sql_free_result($rs);
423
        } else {
424
            $masslogs = 0;
425
        }
426
427
        return ($masslogs > $opt['logic']['masslog']['count']);
428
    }
429
430
    public static function validateDate($year, $month, $day, $hour, $minute, $submit)
431
    {
432
        $dateOk = false;
433
434
        if (is_numeric($month)
435
            && is_numeric($day)
436
            && is_numeric($year)
437
            && ($hour . $minute == '' || is_numeric($hour))
438
            && ($minute == '' || is_numeric($minute))
439
        ) {
440
            $dateOk = checkdate($month, $day, $year)
441
                && ($year >= 2000)
442
                && ($hour >= 0)
443
                && ($hour <= 23)
444
                && ($minute >= 0)
445
                && ($minute <= 59);
446
            if ($dateOk && $submit) {
447
                $dateOk = (
448
                    mktime(
449
                        $hour + 0,
450
                        $minute + 0,
451
                        0,
452
                        $month,
453
                        $day,
454
                        $year
455
                    )
456
                    < time()
457
                );
458
            }
459
        }
460
461
        return $dateOk;
462
    }
463
}
464