Passed
Push — master ( 9b803d...bcd4f7 )
by Goffy
03:19
created

index.php (9 issues)

1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * wgGitHub module for xoops
14
 *
15
 * @copyright      2020 XOOPS Project (https://xooops.org)
16
 * @license        GPL 2.0 or later
17
 * @package        wggithub
18
 * @since          1.0
19
 * @min_xoops      2.5.10
20
 * @author         Goffy - XOOPS Development Team - Email:<[email protected]> - Website:<https://wedega.com>
21
 */
22
23
use Xmf\Request;
0 ignored issues
show
The type Xmf\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use XoopsModules\Wggithub;
25
use XoopsModules\Wggithub\{
26
    Constants,
27
    Helper,
28
    Github\GithubClient
29
};
30
31
require __DIR__ . '/header.php';
32
$GLOBALS['xoopsOption']['template_main'] = 'wggithub_index.tpl';
33
include_once \XOOPS_ROOT_PATH . '/header.php';
0 ignored issues
show
The constant XOOPS_ROOT_PATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
34
35
// Permissions
36
$permGlobalView = $permissionsHandler->getPermGlobalView();
37
if (!$permGlobalView) {
38
    $GLOBALS['xoopsTpl']->assign('error', _NOPERM);
0 ignored issues
show
The constant _NOPERM was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
39
    require __DIR__ . '/footer.php';
40
}
41
$permGlobalRead   = $permissionsHandler->getPermGlobalRead();
42
$permReadmeUpdate = $permissionsHandler->getPermReadmeUpdate();
43
44
$op            = Request::getCmd('op', 'list');
45
$filterRelease = Request::getString('release', 'all');
46
$filterSortby  = Request::getString('sortby', 'update');
47
48
$GLOBALS['xoopsTpl']->assign('release', $filterRelease);
49
$GLOBALS['xoopsTpl']->assign('sortby', $filterSortby);
50
51
// Define Stylesheet
52
$GLOBALS['xoTheme']->addStylesheet($style, null);
53
$GLOBALS['xoTheme']->addStylesheet(WGGITHUB_URL . '/assets/css/tabs.css', null);
54
$keywords = [];
55
// 
56
$GLOBALS['xoopsTpl']->assign('xoops_icons32_url', XOOPS_ICONS32_URL);
57
$GLOBALS['xoopsTpl']->assign('wggithub_url', WGGITHUB_URL);
58
$GLOBALS['xoopsTpl']->assign('wggithub_image_url', WGGITHUB_IMAGE_URL);
59
//
60
$GLOBALS['xoopsTpl']->assign('permReadmeUpdate', $permReadmeUpdate);
61
$GLOBALS['xoopsTpl']->assign('permGlobalRead', $permGlobalRead);
62
63
switch ($op) {
64
    case 'show':
65
    case 'list':
66
    case 'apiexceed':
67
    default:
68
        //check number of API calls
69
        $lastUpdate = 0;
70
        $crLogs = new \CriteriaCompo();
0 ignored issues
show
The type CriteriaCompo was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
        $crLogs->add(new \Criteria('log_datecreated', (time() - 3600), '>'));
0 ignored issues
show
The type Criteria was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
72
        $logsCount = $logsHandler->getCount($crLogs);
73
        if ($permGlobalRead && $logsCount < 60 && 'list' == $op) {
74
            executeUpdate();
75
        }
76
77
        unset($crLogs);
78
        $crLogs = new \CriteriaCompo();
79
        $crLogs->add(new \Criteria('log_type', Constants::LOG_TYPE_UPDATE_END));
80
        $crLogs->add(new \Criteria('log_result', 'OK'));
81
        $crLogs->setStart(0);
82
        $crLogs->setLimit(1);
83
        $crLogs->setSort('log_id');
84
        $crLogs->setOrder('DESC');
85
        $logsAll = $logsHandler->getAll($crLogs);
86
        foreach (\array_keys($logsAll) as $i) {
87
            $lastUpdate = $logsAll[$i]->getVar('log_datecreated');
88
            $GLOBALS['xoopsTpl']->assign('lastUpdate', \formatTimestamp($lastUpdate, 'm'));
0 ignored issues
show
The function formatTimestamp was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

88
            $GLOBALS['xoopsTpl']->assign('lastUpdate', /** @scrutinizer ignore-call */ \formatTimestamp($lastUpdate, 'm'));
Loading history...
89
        }
90
        unset($crLogs);
91
        $crLogs = new \CriteriaCompo();
92
        $crLogs->setStart(0);
93
        $crLogs->setLimit(1);
94
        $crLogs->setSort('log_id');
95
        $crLogs->setOrder('DESC');
96
        $logsAll = $logsHandler->getAll($crLogs);
97
        foreach (\array_keys($logsAll) as $i) {
98
            if ($lastUpdate < $logsAll[$i]->getVar('log_datecreated')) {
99
                if (\strpos($logsAll[$i]->getVar('log_result'), 'API rate limit exceeded') > 0) {
100
                    $GLOBALS['xoopsTpl']->assign('apiexceed', true);
101
                } else {
102
                    $GLOBALS['xoopsTpl']->assign('apierror', true);
103
                }
104
            }
105
        }
106
        unset($crLogs);
107
108
        $start = Request::getInt('start', 0);
109
        $limit = Request::getInt('limit', $helper->getConfig('userpager'));
110
        $menu  = Request::getInt('menu', 0);
111
112
        $crDirectories = new \CriteriaCompo();
113
        $crDirectories->add(new \Criteria('dir_online', 1));
114
        $directoriesCount = $directoriesHandler->getCount($crDirectories);
115
        $GLOBALS['xoopsTpl']->assign('directoriesCount', $directoriesCount);
116
        if ($directoriesCount > 0) {
117
            $directoriesAll = $directoriesHandler->getAll($crDirectories);
118
            // Get All Directories
119
            $directories = [];
120
            foreach (\array_keys($directoriesAll) as $i) {
121
                $directories[$i] = $directoriesAll[$i]->getValuesDirectories();
122
                $dirName = $directoriesAll[$i]->getVar('dir_name');
123
                $dirFilterRelease = (bool)$directoriesAll[$i]->getVar('dir_filterrelease');
124
                $repos = [];
125
                $crRepositories = new \CriteriaCompo();
126
                $crRepositories->add(new \Criteria('repo_user', $dirName));
127
                $repositoriesCountTotal = $repositoriesHandler->getCount($crRepositories);
128
                if ('any' === $filterRelease && $dirFilterRelease) {
129
                    $crRepositories->add(new \Criteria('repo_prerelease', 1) );
130
                    $crRepositories->add(new \Criteria('repo_release', 1), 'OR');
131
                }
132
                if ('final' === $filterRelease && $dirFilterRelease) {
133
                    $crRepositories->add(new \Criteria('repo_release', 1));
134
                }
135
                $repositoriesCount = $repositoriesHandler->getCount($crRepositories);
136
                $crRepositories->setStart($start);
137
                $crRepositories->setLimit($limit);
138
                switch ($filterSortby) {
139
                    case 'name':
140
                    default:
141
                        $crRepositories->setSort('repo_name');
142
                        $crRepositories->setOrder('ASC');
143
                        break;
144
                    case 'update':
145
                        $crRepositories->setSort('repo_updatedat');
146
                        $crRepositories->setOrder('DESC');
147
                        break;
148
                }
149
                if ($repositoriesCount > 0) {
150
                    $repositoriesAll = $repositoriesHandler->getAll($crRepositories);
151
                    foreach (\array_keys($repositoriesAll) as $j) {
152
                        $repoId = $repositoriesAll[$j]->getVar('repo_id');
153
                        $repos[$j] = $repositoriesAll[$j]->getValuesRepositories();
154
                        $repos[$j]['readme'] = ['content_clean' => _MA_WGGITHUB_README_NOFILE];
155
                        if ($repositoriesAll[$j]->getVar('repo_readme') > 0) {
156
                            $crReadmes = new \CriteriaCompo();
157
                            $crReadmes->add(new \Criteria('rm_repoid', $repoId));
158
                            $readmesAll = $readmesHandler->getAll($crReadmes);
159
                            foreach ($readmesAll as $readme) {
160
                                $repos[$j]['readme'] = $readme->getValuesReadmes();
161
                            }
162
                            unset($crReadmes, $readmesAll);
163
                        }
164
                        if ($repositoriesAll[$j]->getVar('repo_prerelease') > 0 || $repositoriesAll[$j]->getVar('repo_release') > 0) {
165
                            //$repos[$j]['releases'] = [];
166
                            $crReleases = new \CriteriaCompo();
167
                            $crReleases->add(new \Criteria('rel_repoid', $repoId));
168
                            $releasesAll = $releasesHandler->getAll($crReleases);
169
                            foreach ($releasesAll as $release) {
170
                                $repos[$j]['releases'][] = $release->getValuesReleases();
171
                            }
172
                            unset($crReleases, $releasesAll);
173
                        }
174
                    }
175
                    unset($crRepositories, $repositoriesAll);
176
                }
177
                if ($repositoriesCount === $repositoriesCountTotal) {
178
                    $directories[$i]['countRepos'] = str_replace(['%s', '%t'], [$dirName, $repositoriesCountTotal], _MA_WGGITHUB_REPOSITORIES_COUNT2);
179
                } else {
180
                    $directories[$i]['countRepos'] = str_replace(['%s', '%r', '%t'], [$dirName, $repositoriesCount, $repositoriesCountTotal], _MA_WGGITHUB_REPOSITORIES_COUNT1);
181
                }
182
                $directories[$i]['repos'] = $repos;
183
                $directories[$i]['previousRepos'] = $start > 0;
184
                $directories[$i]['previousOp'] = '&amp;start=' . ($start - $limit) . '&amp;limit=' . $limit . '&amp;release=' . $filterRelease . '&amp;sortby=' . $filterSortby;
185
                $directories[$i]['nextRepos'] = ($repositoriesCount - $start) > $limit;
186
                $directories[$i]['nextOp'] = '&amp;start=' . ($start + $limit) . '&amp;limit=' . $limit . '&amp;release=' . $filterRelease . '&amp;sortby=' . $filterSortby;
187
                $GLOBALS['xoopsTpl']->assign('start', $start);
188
                $GLOBALS['xoopsTpl']->assign('limit', $limit);
189
                $GLOBALS['xoopsTpl']->assign('menu', $menu);
190
191
                $GLOBALS['xoopsTpl']->assign('directories', $directories);
192
            }
193
194
            unset($crDirectories, $directories);
195
            // Display Navigation
196
            if ($directoriesCount > $limit) {
197
                require_once \XOOPS_ROOT_PATH . '/class/pagenav.php';
198
                $pagenav = new \XoopsPageNav($directoriesCount, $limit, $start, 'start', 'op=list&limit=' . $limit);
0 ignored issues
show
The type XoopsPageNav was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
199
                $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4));
200
            }
201
            $GLOBALS['xoopsTpl']->assign('lang_thereare', \sprintf(\_MA_WGGITHUB_INDEX_THEREARE, $directoriesCount));
202
            $GLOBALS['xoopsTpl']->assign('divideby', $helper->getConfig('divideby'));
203
            $GLOBALS['xoopsTpl']->assign('numb_col', $helper->getConfig('numb_col'));
204
        }
205
206
        break;
207
    case 'update_dir':
208
        // Permissions
209
        if (!$permGlobalRead) {
210
            $GLOBALS['xoopsTpl']->assign('error', \_NOPERM);
211
            require __DIR__ . '/footer.php';
212
        }
213
        $dirName = Request::getString('dir_name', '');
214
        $result = executeUpdate($dirName);
215
        $redir = 'index.php?op=list_afterupdate&amp;start=' . $start . '&amp;limit=' . $limit . '&amp;release=' . $filterRelease . '&amp;sortby=' . $filterSortby;
216
        if ($result) {
217
            \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_SUCCESS);
0 ignored issues
show
The function redirect_header was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

217
            /** @scrutinizer ignore-call */ 
218
            \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_SUCCESS);
Loading history...
218
        } else {
219
            \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_ERROR_API);
220
        }
221
222
        break;
223
    case 'update_readme':
224
        // Permissions
225
        if (!$permReadmeUpdate) {
226
            $GLOBALS['xoopsTpl']->assign('error', \_NOPERM);
227
            require __DIR__ . '/footer.php';
228
        }
229
        $repoId = Request::getInt('repo_id', 0);
230
        $repoUser  = Request::getString('repo_user', 'none');
231
        $repoName  = Request::getString('repo_name', 'none');
232
        $result = $helper->getHandler('Readmes')->updateReadmes($repoId, $repoUser, $repoName);
233
        $redir = 'index.php?op=list_afterupdate&amp;start=' . $start . '&amp;limit=' . $limit . '&amp;release=' . $filterRelease . '&amp;sortby=' . $filterSortby;
234
        if ($result) {
235
            \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_SUCCESS);
236
        } else {
237
            \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_ERROR_API);
238
        }
239
        break;
240
}
241
242
$GLOBALS['xoopsTpl']->assign('table_type', $helper->getConfig('table_type'));
243
// Breadcrumbs
244
$xoBreadcrumbs[] = ['title' => \_MA_WGGITHUB_INDEX];
245
// Keywords
246
wggithubMetaKeywords($helper->getConfig('keywords') . ', ' . \implode(',', $keywords));
247
unset($keywords);
248
// Description
249
wggithubMetaDescription(\_MA_WGGITHUB_INDEX_DESC);
250
$GLOBALS['xoopsTpl']->assign('xoops_mpageurl', WGGITHUB_URL.'/index.php');
251
$GLOBALS['xoopsTpl']->assign('xoops_icons32_url', XOOPS_ICONS32_URL);
252
$GLOBALS['xoopsTpl']->assign('wggithub_upload_url', WGGITHUB_UPLOAD_URL);
253
require __DIR__ . '/footer.php';
254
255
256
/**
257
 * Execute update of repositories and all related tables
258
 * @param string $dirName
259
 * @return bool
260
 */
261
function executeUpdate($dirName = ''){
262
263
    $githubClient = GithubClient::getInstance();
264
    $helper = Helper::getInstance();
265
    $directoriesHandler = $helper->getHandler('Directories');
266
    $repositoriesHandler = $helper->getHandler('Repositories');
267
    $releasesHandler = $helper->getHandler('Releases');
268
    $readmesHandler = $helper->getHandler('Readmes');
269
    $logsHandler = $helper->getHandler('Logs');
270
271
    $logsHandler->updateTableLogs(Constants::LOG_TYPE_UPDATE_START, '', 'OK');
272
    $crDirectories = new \CriteriaCompo();
273
    if ('' !== $dirName) {
274
        $crDirectories->add(new \Criteria('dir_name', $dirName));
275
    } else {
276
        $crDirectories->add(new \Criteria('dir_autoupdate', 1));
277
    }
278
    $crDirectories->add(new \Criteria('dir_online', 1));
279
    $directoriesAll = $directoriesHandler->getAll($crDirectories);
280
    // Get All Directories
281
    $directories = [];
282
    foreach (\array_keys($directoriesAll) as $i) {
283
        $directories[$i] = $directoriesAll[$i]->getValuesDirectories();
284
        $dirName = $directoriesAll[$i]->getVar('dir_name');
285
        $repos = [];
286
        for ($j = 1; $j <= 9; $j++) {
287
            $repos[$j] = [];
288
            if (Constants::DIRECTORY_TYPE_ORG == $directoriesAll[$i]->getVar('dir_type')) {
289
                $repos = $githubClient->getOrgRepositories($dirName, 100, $j);
290
            } else {
291
                $repos = $githubClient->getUserRepositories($dirName, 100, $j);
292
            }
293
            if (false === $repos) {
294
                return false;
295
                break 1;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
296
            }
297
            if (count($repos) > 0) {
298
                $repositoriesHandler->updateTableRepositories($dirName, $repos, true);
299
            } else {
300
                break 1;
301
            }
302
            if (count($repos) < 100) {
303
                break 1;
304
            }
305
        }
306
    }
307
    unset($directories);
308
309
    $releasesHandler->updateRepoReleases();
310
    $readmesHandler->updateRepoReadme();
311
312
    $logsHandler->updateTableLogs(Constants::LOG_TYPE_UPDATE_END, '', 'OK');
313
314
    return true;
315
}
316