-
Notifications
You must be signed in to change notification settings - Fork 5
/
report.php
150 lines (138 loc) · 4.52 KB
/
report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/* This file is part of NextDom.
*
* NextDom is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* NextDom is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with NextDom. If not, see <http://www.gnu.org/licenses/>.
*/
\define('DEBUG', false);
// Télécharger un document
/**
* Télécharger un document
*
* @param string $url Lien du document à télécharger.
* @param string $gitHubToken Token GitHub pour éviter les limitations
*
* @return string Contenu du fichier.
*/
function downloadContent($url, $gitHubToken = '')
{
if ($gitHubToken !== '') {
$toAdd = 'access_token=' . $gitHubToken;
// Test si un paramètre a déjà été passé
if (\strpos($url, '?') !== false) {
$url = $url . '&' . $toAdd;
} else {
$url = $url . '?' . $toAdd;
}
}
$url .= '&per_page=100';
if (DEBUG) {
\var_dump($url);
}
$content = false;
$curlSession = \curl_init();
if ($curlSession !== false) {
\curl_setopt($curlSession, CURLOPT_URL, $url);
\curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($curlSession, CURLOPT_USERAGENT, 'AlternativeMarketUpdate');
$content = \curl_exec($curlSession);
if (DEBUG) {
\var_dump($content);
}
\curl_close($curlSession);
}
return $content;
}
/**
* Test si l'URL pointe vers un document téléchargeable.
*/
function isDownloadable($url) {
$result = false;
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL,$url);
curl_setopt($curlSession, CURLOPT_NOBODY, 1);
curl_setopt($curlSession, CURLOPT_FAILONERROR, 1);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($curlSession) !== false) {
$result = true;
}
return $result;
}
/**
* Lire la liste des dépôts
*/
function getBaseList($filename) {
$result = false;
if (\file_exists($filename)) {
$content = \file_get_contents($filename);
$result = \json_decode($content, true);
}
return $result;
}
/**
* Scan une liste de dépots GitHub
*/
function scan($gitHubToken, $githubs, $baseList) {
$list = $baseList;
foreach ($githubs as $github) {
echo "=== $github ===\n";
$content = downloadContent('https://api.github.com/orgs/'.$github.'/repos', $gitHubToken);
if (strstr($content, '"message":"Not Found"') !== false) {
$content = downloadContent('https://api.github.com/users/'.$github.'/repos', $gitHubToken);
}
try {
$repos = json_decode($content, true);
foreach ($repos as $repo) {
$isPlugin = isDownloadable('https://raw.githubusercontent.com/'.$repo['full_name'].'/master/plugin_info/info.json');
$arraySearchResult = array_search($repo['full_name'], $list);
if ($arraySearchResult !== false) {
if (!$isPlugin) {
echo "Erreur : ".$repo['full_name']."\n";
}
array_splice($list, $arraySearchResult, 1);
}
else {
if ($isPlugin) {
echo "A rajouter : ".$repo['full_name']."\n";
}
}
}
}
catch (Exception $e) {
}
}
if (count($list) > 0) {
echo "=== Absents ===\n";
foreach ($list as $item) {
echo $item."\n";
}
}
}
// Lecture du token GitHub
$gitHubToken = '';
if (\file_exists('.github-token')) {
$gitHubToken = \file_get_contents('.github-token');
$gitHubToken = \str_replace("\n", "", $gitHubToken);
}
$reportContent = \file_get_contents('repositories.json');
$reportList = \json_decode($reportContent, true);
// Récupération de la liste des sources
$listContent = \file_get_contents('lists.json');
$sourcesList = \json_decode($listContent, true);
$reposList = [];
// Parcours des sources
foreach ($sourcesList as $source) {
$list = getBaseList('lists/'.$source.'.json');
$reposList = array_merge($reposList, $list);
}
scan($gitHubToken, $reportList, $reposList);