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