Passed
Push — master ( 8085c4...3786e6 )
by Goffy
03:03
created

index.php (2 issues)

Labels
Severity
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;
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';
34
35
// Permissions
36
$permGlobalView = $permissionsHandler->getPermGlobalView();
37
if (!$permGlobalView) {
38
    $GLOBALS['xoopsTpl']->assign('error', _NOPERM);
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 directories with auto_update
69
        $crDirectories = 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...
70
        $crDirectories->add(new \Criteria('dir_autoupdate', 1));
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...
71
        $directoriesCount = $directoriesHandler->getCount($crDirectories);
72
        if ($directoriesCount > 0) {
73
            //check number of API calls
74
            $lastUpdate = 0;
75
            $crLogs = new \CriteriaCompo();
76
            $crLogs->add(new \Criteria('log_datecreated', (time() - 3600), '>'));
77
            $logsCount = $logsHandler->getCount($crLogs);
78
            if ($permGlobalRead && $logsCount < 60 && 'list' == $op) {
79
                $githubClient = GithubClient::getInstance();
80
                $githubClient->executeUpdate();
81
            }
82
            unset($crLogs);
83
        }
84
        unset($crDirectories);
85
        $crLogs = new \CriteriaCompo();
86
        $crLogs->add(new \Criteria('log_type', Constants::LOG_TYPE_UPDATE_END));
87
        $crLogs->add(new \Criteria('log_result', 'OK'));
88
        $crLogs->setStart(0);
89
        $crLogs->setLimit(1);
90
        $crLogs->setSort('log_id');
91
        $crLogs->setOrder('DESC');
92
        $logsAll = $logsHandler->getAll($crLogs);
93
        foreach (\array_keys($logsAll) as $i) {
94
            $lastUpdate = $logsAll[$i]->getVar('log_datecreated');
95
            $GLOBALS['xoopsTpl']->assign('lastUpdate', \formatTimestamp($lastUpdate, 'm'));
96
        }
97
        unset($crLogs);
98
        $crLogs = new \CriteriaCompo();
99
        $crLogs->setStart(0);
100
        $crLogs->setLimit(1);
101
        $crLogs->setSort('log_id');
102
        $crLogs->setOrder('DESC');
103
        $logsAll = $logsHandler->getAll($crLogs);
104
        foreach (\array_keys($logsAll) as $i) {
105
            if ($lastUpdate < $logsAll[$i]->getVar('log_datecreated')) {
106
                if (\strpos($logsAll[$i]->getVar('log_result'), 'API rate limit exceeded') > 0) {
107
                    $GLOBALS['xoopsTpl']->assign('apiexceed', true);
108
                } else {
109
                    $GLOBALS['xoopsTpl']->assign('apierror', true);
110
                }
111
            }
112
        }
113
        unset($crLogs);
114
115
        $start = Request::getInt('start', 0);
116
        $limit = Request::getInt('limit', $helper->getConfig('userpager'));
117
        $menu  = Request::getInt('menu', 0);
118
119
        $crDirectories = new \CriteriaCompo();
120
        $crDirectories->add(new \Criteria('dir_online', 1));
121
        $directoriesCount = $directoriesHandler->getCount($crDirectories);
122
        $GLOBALS['xoopsTpl']->assign('directoriesCount', $directoriesCount);
123
        if ($directoriesCount > 0) {
124
            $directoriesAll = $directoriesHandler->getAll($crDirectories);
125
            // Get All Directories
126
            $directories = [];
127
            foreach (\array_keys($directoriesAll) as $i) {
128
                $directories[$i] = $directoriesAll[$i]->getValuesDirectories();
129
                $dirName = $directoriesAll[$i]->getVar('dir_name');
130
                $dirFilterRelease = (bool)$directoriesAll[$i]->getVar('dir_filterrelease');
131
                $repos = [];
132
                $crRepositories = new \CriteriaCompo();
133
                $crRepositories->add(new \Criteria('repo_user', $dirName));
134
                $crRepositories->add(new \Criteria('repo_status', Constants::STATUS_UPDATED));
135
                $crRepositories->add(new \Criteria('repo_status', Constants::STATUS_UPTODATE), 'OR');
136
                $repositoriesCountTotal = $repositoriesHandler->getCount($crRepositories);
137
                if ('any' === $filterRelease && $dirFilterRelease) {
138
                    $crRepositories->add(new \Criteria('repo_prerelease', 1) );
139
                    $crRepositories->add(new \Criteria('repo_release', 1), 'OR');
140
                }
141
                if ('final' === $filterRelease && $dirFilterRelease) {
142
                    $crRepositories->add(new \Criteria('repo_release', 1));
143
                }
144
                $repositoriesCount = $repositoriesHandler->getCount($crRepositories);
145
                $crRepositories->setStart($start);
146
                $crRepositories->setLimit($limit);
147
                switch ($filterSortby) {
148
                    case 'name':
149
                    default:
150
                        $crRepositories->setSort('repo_name');
151
                        $crRepositories->setOrder('ASC');
152
                        break;
153
                    case 'update':
154
                        $crRepositories->setSort('repo_updatedat');
155
                        $crRepositories->setOrder('DESC');
156
                        break;
157
                }
158
                if ($repositoriesCount > 0) {
159
                    $repositoriesAll = $repositoriesHandler->getAll($crRepositories);
160
                    foreach (\array_keys($repositoriesAll) as $j) {
161
                        $repoId = $repositoriesAll[$j]->getVar('repo_id');
162
                        $repos[$j] = $repositoriesAll[$j]->getValuesRepositories();
163
                        $repos[$j]['readme'] = ['content_clean' => _MA_WGGITHUB_README_NOFILE];
164
                        if ($repositoriesAll[$j]->getVar('repo_readme') > 0) {
165
                            $crReadmes = new \CriteriaCompo();
166
                            $crReadmes->add(new \Criteria('rm_repoid', $repoId));
167
                            $readmesAll = $readmesHandler->getAll($crReadmes);
168
                            foreach ($readmesAll as $readme) {
169
                                $repos[$j]['readme'] = $readme->getValuesReadmes();
170
                            }
171
                            unset($crReadmes, $readmesAll);
172
                        }
173
                        if ($repositoriesAll[$j]->getVar('repo_prerelease') > 0 || $repositoriesAll[$j]->getVar('repo_release') > 0) {
174
                            //$repos[$j]['releases'] = [];
175
                            $crReleases = new \CriteriaCompo();
176
                            $crReleases->add(new \Criteria('rel_repoid', $repoId));
177
                            $releasesAll = $releasesHandler->getAll($crReleases);
178
                            foreach ($releasesAll as $release) {
179
                                $repos[$j]['releases'][] = $release->getValuesReleases();
180
                            }
181
                            unset($crReleases, $releasesAll);
182
                        }
183
                    }
184
                    unset($crRepositories, $repositoriesAll);
185
                }
186
                if ($repositoriesCount === $repositoriesCountTotal) {
187
                    $directories[$i]['countRepos'] = str_replace(['%s', '%t'], [$dirName, $repositoriesCountTotal], _MA_WGGITHUB_REPOSITORIES_COUNT2);
188
                } else {
189
                    $directories[$i]['countRepos'] = str_replace(['%s', '%r', '%t'], [$dirName, $repositoriesCount, $repositoriesCountTotal], _MA_WGGITHUB_REPOSITORIES_COUNT1);
190
                }
191
                $directories[$i]['repos'] = $repos;
192
                $directories[$i]['previousRepos'] = $start > 0;
193
                $directories[$i]['previousOp'] = '&amp;start=' . ($start - $limit) . '&amp;limit=' . $limit . '&amp;release=' . $filterRelease . '&amp;sortby=' . $filterSortby;
194
                $directories[$i]['nextRepos'] = ($repositoriesCount - $start) > $limit;
195
                $directories[$i]['nextOp'] = '&amp;start=' . ($start + $limit) . '&amp;limit=' . $limit . '&amp;release=' . $filterRelease . '&amp;sortby=' . $filterSortby;
196
                $GLOBALS['xoopsTpl']->assign('start', $start);
197
                $GLOBALS['xoopsTpl']->assign('limit', $limit);
198
                $GLOBALS['xoopsTpl']->assign('menu', $menu);
199
                $GLOBALS['xoopsTpl']->assign('directories', $directories);
200
            }
201
202
            unset($crDirectories, $directories);
203
            // Display Navigation
204
            if ($directoriesCount > $limit) {
205
                require_once \XOOPS_ROOT_PATH . '/class/pagenav.php';
206
                $pagenav = new \XoopsPageNav($directoriesCount, $limit, $start, 'start', 'op=list&limit=' . $limit);
207
                $GLOBALS['xoopsTpl']->assign('pagenav', $pagenav->renderNav(4));
208
            }
209
            $GLOBALS['xoopsTpl']->assign('lang_thereare', \sprintf(\_MA_WGGITHUB_INDEX_THEREARE, $directoriesCount));
210
            $GLOBALS['xoopsTpl']->assign('divideby', $helper->getConfig('divideby'));
211
            $GLOBALS['xoopsTpl']->assign('numb_col', $helper->getConfig('numb_col'));
212
        }
213
214
        break;
215
    case 'update_dir':
216
        // Permissions
217
        if (!$permGlobalRead) {
218
            $GLOBALS['xoopsTpl']->assign('error', \_NOPERM);
219
            require __DIR__ . '/footer.php';
220
        }
221
        $dirName = Request::getString('dir_name', '');
222
        $githubClient = GithubClient::getInstance();
223
        $result = $githubClient->executeUpdate($dirName);
224
        $redir = 'index.php?op=list_afterupdate&amp;start=' . $start . '&amp;limit=' . $limit . '&amp;release=' . $filterRelease . '&amp;sortby=' . $filterSortby;
225
        if ($result) {
226
            \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_SUCCESS);
227
        } else {
228
            \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_ERROR_API);
229
        }
230
231
        break;
232
    case 'update_readme':
233
        // Permissions
234
        if (!$permReadmeUpdate) {
235
            $GLOBALS['xoopsTpl']->assign('error', \_NOPERM);
236
            require __DIR__ . '/footer.php';
237
        }
238
        $repoId = Request::getInt('repo_id', 0);
239
        $repoUser  = Request::getString('repo_user', 'none');
240
        $repoName  = Request::getString('repo_name', 'none');
241
        $result = $helper->getHandler('Readmes')->updateReadmes($repoId, $repoUser, $repoName);
242
        $redir = 'index.php?op=list_afterupdate&amp;start=' . $start . '&amp;limit=' . $limit . '&amp;release=' . $filterRelease . '&amp;sortby=' . $filterSortby;
243
        if ($result) {
244
            \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_SUCCESS);
245
        } else {
246
            \redirect_header($redir, 2, \_MA_WGGITHUB_READGH_ERROR_API);
247
        }
248
        break;
249
}
250
251
$GLOBALS['xoopsTpl']->assign('table_type', $helper->getConfig('table_type'));
252
// Breadcrumbs
253
$xoBreadcrumbs[] = ['title' => \_MA_WGGITHUB_INDEX];
254
// Keywords
255
wggithubMetaKeywords($helper->getConfig('keywords') . ', ' . \implode(',', $keywords));
256
unset($keywords);
257
// Description
258
wggithubMetaDescription(\_MA_WGGITHUB_INDEX_DESC);
259
$GLOBALS['xoopsTpl']->assign('xoops_mpageurl', WGGITHUB_URL.'/index.php');
260
$GLOBALS['xoopsTpl']->assign('xoops_icons32_url', XOOPS_ICONS32_URL);
261
$GLOBALS['xoopsTpl']->assign('wggithub_upload_url', WGGITHUB_UPLOAD_URL);
262
require __DIR__ . '/footer.php';
263