Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/inc/lib/user_api_key_manager.class.php (1 issue)

1
<?php
2
3
/**
4
 * Manage user api keys.
5
 *
6
 * @license see /license.txt
7
 * @author Laurent Opprecht <[email protected]> for the Univesity of Geneva
8
 */
9
class UserApiKeyManager
10
{
11
    /**
12
     * The name of the default service.
13
     *
14
     * @return string
15
     */
16
    public static function default_service()
17
    {
18
        return 'chamilo';
19
    }
20
21
    public static function end_of_time()
22
    {
23
        $time = 2147483647; //mysql int max value
24
    }
25
26
    public static function get_by_id($id)
27
    {
28
        $table = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
29
        $sql = "SELECT * FROM $table WHERE id=$id";
30
        $res = Database::query($sql);
31
        if (Database::num_rows($res) < 1) {
32
            return false;
33
        }
34
        $result = Database::fetch_array($res, 'ASSOC');
35
36
        return $result;
37
    }
38
39
    /**
40
     * @param int    $duration      in seconds
41
     * @param int    $user_id
42
     * @param string $api_service
43
     * @param string $api_end_point
44
     *
45
     * @return AccessToken
46
     */
47
    public static function create_temp_token($api_service = null, $duration = 60, $user_id = null, $api_end_point = null)
48
    {
49
        $time = time();
50
        $validity_start_date = $time;
51
        $validity_end_date = $time + $duration;
52
53
        return self::create_token($user_id, $api_key = null, $api_service, $api_end_point, $validity_start_date, $validity_end_date);
54
    }
55
56
    /**
57
     * @param int    $user_id
58
     * @param string $api_key
59
     * @param string $api_service
60
     * @param string $api_end_point
61
     * @param int    $validity_start_date
62
     * @param int    $validity_end_date
63
     * @param string $description
64
     *
65
     * @return AccessToken
66
     */
67
    public static function create_token($user_id = null, $api_key = null, $api_service = null, $api_end_point = null, $validity_start_date = null, $validity_end_date = null, $description = '')
68
    {
69
        $time = time();
70
        $user_id = $user_id ? $user_id : Chamilo::user()->user_id();
71
        $api_key = $api_key ? $api_key : uniqid('', true);
72
        $api_service = $api_service ? $api_service : self::default_service();
73
        $api_end_point = $api_end_point ? $api_end_point : '';
74
        $validity_start_date = $validity_start_date ? $validity_start_date : $time;
75
        $validity_end_date = $validity_end_date ? $validity_end_date : self::end_of_time();
0 ignored issues
show
Are you sure the usage of self::end_of_time() targeting UserApiKeyManager::end_of_time() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
        $created_date = $time;
77
78
        $user_id = (int) $user_id;
79
        $api_key = Database::escape_string($api_key);
80
        $api_service = Database::escape_string($api_service);
81
        $api_end_point = Database::escape_string($api_end_point);
82
        $validity_start_date = date('Y-m-d H:i:s', $validity_start_date);
83
        $validity_end_date = date('Y-m-d H:i:s', $validity_end_date);
84
        $created_date = date('Y-m-d H:i:s', $created_date);
85
86
        $values = [];
87
        $values['user_id'] = $user_id;
88
        $values['api_key'] = $api_key;
89
        $values['api_service'] = $api_service;
90
        $values['api_end_point'] = $api_end_point;
91
        $values['validity_start_date'] = $validity_start_date;
92
        $values['validity_end_date'] = $validity_end_date;
93
        $values['created_date'] = $created_date;
94
95
        $table = Database::get_main_table(TABLE_MAIN_USER_API_KEY);
96
97
        $id = Database::insert($table, $values);
98
99
        return AccessToken::create($id, $user_id, $api_key);
100
    }
101
}
102