Issues (136)

code/api/GitRepoFinder.php (12 issues)

1
<?php
2
3
4
5
class GitRepoFinder extends Object
6
{
7
8
    /**
9
     *
10
     *
11
     *
12
     * @var string
13
     */
14
    private static $_modules = array();
15
16
17
18
    /**
19
     * takes the preloaded modules and
20
     * adds any other ones you have listed on github
21
     */
22
23
    public static function get_all_repos($username = '', $getNamesWithPrefix = false)
24
    {
25
        # $oauth_token = GitRepoFinder::Config()->get('github_oauth_token');
26
27
        self::get_all_repos_no_oauth($username, $getNamesWithPrefix);
28
        if (! self::$_modules) {
29
            self::get_repos_with_auth($username, $getNamesWithPrefix);
30
        }
31
        return self::$_modules;
32
    }
33
34
    public static function get_all_repos_no_oauth($username = '', $getNamesWithPrefix = false)
35
    {
36
        $preSelected = Config::inst()->get('UpdateModules', 'modules_to_update');
37
        if (is_array($preSelected) && count($preSelected)) {
38
            return $preSelected;
39
        } else {
40
            if (!$username) {
41
                $username = Config::inst()->get('GitHubModule', "github_user_name");
42
            }
43
            print "<li>Retrieving List of modules from GitHub for user $username ... </li>";
44
            if (! count(self::$_modules)) {
0 ignored issues
show
self::_modules of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
            if (! count(/** @scrutinizer ignore-type */ self::$_modules)) {
Loading history...
45
                for ($page = 0; $page < 10; $page++) {
46
                    $ch = curl_init();
47
                    curl_setopt($ch, CURLOPT_URL, "https://api.github.com/users/".$username."/repos?per_page=100&page=$page");
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
                    curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, "https://api.github.com/users/".$username."/repos?per_page=100&page=$page");
Loading history...
48
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
49
                    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, true);
50
                    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
51
52
53
                    $string = curl_exec($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
                    $string = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
54
                    // close curl resource to free up system resources
55
                    curl_close($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

55
                    curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
56
                    $array = json_decode($string, true);
57
                    $count = count($array);
58
                    if ($count > 0) {
59
                        foreach ($array as $repo) {
60
                            //dont bother about forks
61
                            if (isset($repo["fork"]) && !$repo["fork"]) {
62
                                //make sure we are the owners
63
                                if ($repo["owner"]["login"] == $username) {
64
                                    $isSSModule =  (stripos($repo["name"], 'silverstripe-')  !== false);
65
                                    //check it is silverstripe module
66
                                    if (!$getNamesWithPrefix) {
67
                                        $name = $repo["name"];
68
                                    } else {
69
                                        $name = preg_replace('/silverstripe/', "", $repo["name"], $limit = 1);
70
                                    }
71
72
                                    //if(strlen($name) < strlen($repo["name"])) {
73
                                    if ($isSSModule) {
74
                                        //is it listed yet?
75
                                        if (!in_array($name, self::$_modules)) {
0 ignored issues
show
self::_modules of type string is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
                                        if (!in_array($name, /** @scrutinizer ignore-type */ self::$_modules)) {
Loading history...
76
                                            self::$_modules[] = $name;
77
                                        }
78
                                    } else {
79
                                        DB::alteration_message("skipping ".$repo["name"]." as it does not appear to me a silverstripe module, you can add it manually to this task, using the configs ... ");
80
                                    }
81
                                } else {
82
                                    DB::alteration_message("skipping ".$repo["name"]." as it has a different owner");
83
                                }
84
                            } elseif (isset($repo["name"])) {
85
                                DB::alteration_message("skipping ".$repo["name"]." as it is a fork");
86
                            }
87
                        }
88
                    } else {
89
                        $page = 11;
90
                    }
91
                }
92
            }
93
            print "<li>Found ".count(self::$_modules)." modules on GitHub ... </li>";
94
            if (count(self::$_modules)==0) {
95
                user_error("No modules found on GitHub. This is possibly because the limit of 60 requests an hour has been exceeded.");
96
            }
97
        }
98
        return self::$_modules;
99
    }
100
101
    public static function get_repos_with_auth($username = '', $getNamesWithPrefix = false)
102
    {
103
        $preSelected = Config::inst()->get('UpdateModules', 'modules_to_update');
104
        if (is_array($preSelected) && count($preSelected)) {
105
            self::$_modules = $preSelected;
0 ignored issues
show
Documentation Bug introduced by
It seems like $preSelected of type array is incompatible with the declared type string of property $_modules.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
        } else {
107
            if ($username) {
108
                $gitUserName = $username;
109
            } else {
110
                $gitUserName = Config::inst()->get('GitHubModule', "github_user_name");
111
            }
112
            print "<li>Retrieving List of modules from GitHub for user $username ... </li>";
113
            if (! count(self::$_modules)) {
0 ignored issues
show
self::_modules of type string is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

113
            if (! count(/** @scrutinizer ignore-type */ self::$_modules)) {
Loading history...
114
                $url = 'https://api.github.com/users/' . trim($gitUserName) . '/repos';
115
                $array  = array();
116
                for ($page = 0; $page < 10; $page++) {
117
                    $data = array(
118
                        'per_page' => 100,
119
                        'page'=>$page
120
                    );
121
122
                    $method = 'GET';
123
                    $ch = curl_init($url);
124
                    $header = "Content-Type: application/json";
125
126
                    if ($method == 'GET') {
127
                        $url .= '?'.http_build_query($data);
128
                    }
129
130
                    $gitApiUserName = trim(GitHubModule::Config()->get('git_api_login_username'));
0 ignored issues
show
It seems like GitHubModule::Config()->...it_api_login_username') can also be of type array; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
                    $gitApiUserName = trim(/** @scrutinizer ignore-type */ GitHubModule::Config()->get('git_api_login_username'));
Loading history...
131
                    $gitUserName = trim(GitHubModule::Config()->get('github_user_name'));
132
                    $gitApiUserPassword = trim(GitHubModule::Config()->get('git_api_login_password'));
133
134
                    $gitApiAccessToken = trim(GitHubModule::Config()->get('git_personal_access_token'));
135
                    if (trim($gitApiAccessToken)) {
136
                        $gitApiUserPassword = $gitApiAccessToken;
137
                    }
138
139
140
                    curl_setopt($ch, CURLOPT_VERBOSE, 1);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
                    curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_VERBOSE, 1);
Loading history...
141
                    curl_setopt($ch, CURLOPT_URL, $url);
142
                    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
143
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
144
                    curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));
145
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
146
                    curl_setopt(
147
                        $ch,
148
                        CURLOPT_USERAGENT,
149
                        'sunnysideupdevs'
150
                    );
151
152
153
                    if (isset($gitApiUserName) && isset($gitApiUserPassword)) {
154
                        curl_setopt($ch, CURLOPT_USERPWD, $gitApiUserName . ':' . $gitApiUserPassword);
155
                        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
156
                    }
157
158
159
                    $curlResult = curl_exec($ch);
0 ignored issues
show
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
                    $curlResult = curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
160
161
                    if (! $curlResult) {
162
                        GeneralMethods::output_to_screen('Could not retrieve list of modules from GitHub');
163
164
                        UpdateModules::$unsolvedItems["all"] =  ('Could not retrieve list of modules from GitHub');
165
                        die('');
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
166
                    }
167
168
                    $array = array_merge($array, json_decode($curlResult));
0 ignored issues
show
It seems like $curlResult can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
                    $array = array_merge($array, json_decode(/** @scrutinizer ignore-type */ $curlResult));
Loading history...
169
                }
170
171
172
                $modules = array();
173
174
                if (count($array) > 0) {
175
                    foreach ($array as $repo) {
176
177
                        // see http://stackoverflow.com/questions/4345554/convert-php-object-to-associative-array
178
                        $repo = json_decode(json_encode($repo), true);
179
180
181
                        //dont bother about forks
182
                        if (isset($repo["fork"]) && !$repo["fork"]) {
183
                            //make sure we are the owners
184
185
                            if ($repo["owner"]["login"] == $gitUserName) {
186
                                $isSSModule =  (stripos($repo["name"], 'silverstripe-')  !== false);
187
                                //check it is silverstripe module
188
189
                                if (!$getNamesWithPrefix) {
190
                                    $name = $repo["name"];
191
                                } else {
192
                                    $name = preg_replace('/silverstripe/', "", $repo["name"], $limit = 1);
193
                                }
194
195
                                //if(strlen($name) < strlen($repo["name"])) {
196
                                if ($isSSModule) {
197
                                    //is it listed yet?
198
                                    if (!in_array($name, $modules)) {
199
                                        array_push($modules, $name);
200
                                    }
201
                                } else {
202
                                    GeneralMethods::output_to_screen("skipping ".$repo["name"]." as it does not appear to me a silverstripe module");
203
                                }
204
                            } else {
205
                                GeneralMethods::output_to_screen("skipping ".$repo["name"]." as it has a different owner");
206
                            }
207
                        } elseif (isset($repo["name"])) {
208
                            DB::alteration_message("skipping ".$repo["name"]." as it is a fork");
209
                        }
210
                    }
211
                }
212
                self::$_modules = $modules;
213
            }
214
        }
215
        return self::$_modules;
216
    }
217
}
218