Subversion Repositories SvarDOS

Rev

Rev 914 | Rev 920 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 914 Rev 919
1
<?php /*
1
<?php /*
2
 
2
 
3
  SvarDOS repo index builder
3
  SvarDOS repo index builder
4
  Copyright (C) Mateusz Viste 2012-2022
4
  Copyright (C) Mateusz Viste 2012-2022
5
 
5
 
6
  buildidx computes an index json file for the SvarDOS repository.
6
  buildidx computes an index json file for the SvarDOS repository.
7
  it must be executed pointing to a directory that stores packages (*.svp)
7
  it must be executed pointing to a directory that stores packages (*.svp)
8
  files. buildidx will generate the index file and save it into the package
8
  files. buildidx will generate the index file and save it into the package
9
  repository.
9
  repository.
10
 
10
 
11
  requires php-zip
11
  requires php-zip
12
 
12
 
13
  21 feb 2022: buildidx collects categories looking at the dir layout of each package + improved version string parsing (replaced version_compare call by dos_version_compare)
13
  21 feb 2022: buildidx collects categories looking at the dir layout of each package + improved version string parsing (replaced version_compare call by dos_version_compare)
14
  17 feb 2022: checking for non-8+3 filenames in packages and duplicates + devload no longer part of CORE
14
  17 feb 2022: checking for non-8+3 filenames in packages and duplicates + devload no longer part of CORE
15
  16 feb 2022: added warning about overlong version strings and wild files location
15
  16 feb 2022: added warning about overlong version strings and wild files location
16
  15 feb 2022: index is generated as json, contains all filenames and alt versions
16
  15 feb 2022: index is generated as json, contains all filenames and alt versions
17
  14 feb 2022: packages are expected to have the *.svp extension
17
  14 feb 2022: packages are expected to have the *.svp extension
18
  12 feb 2022: skip source packages from being processed (*.src.zip)
18
  12 feb 2022: skip source packages from being processed (*.src.zip)
19
  20 jan 2022: rewritten the code from ANSI C to PHP for easier maintenance
19
  20 jan 2022: rewritten the code from ANSI C to PHP for easier maintenance
20
  13 feb 2021: 'title' LSM field is no longer looked after
20
  13 feb 2021: 'title' LSM field is no longer looked after
21
  11 feb 2021: lsm headers are no longer checked, so it is compatible with the simpler lsm format used by SvarDOS
21
  11 feb 2021: lsm headers are no longer checked, so it is compatible with the simpler lsm format used by SvarDOS
22
  13 jan 2021: removed the identification line, changed CRC32 to bsum, not creating the listing.txt file and stopped compressing index
22
  13 jan 2021: removed the identification line, changed CRC32 to bsum, not creating the listing.txt file and stopped compressing index
23
  23 apr 2017: uncompressed index is no longer created, added CRC32 of zib (bin only) files, if present
23
  23 apr 2017: uncompressed index is no longer created, added CRC32 of zib (bin only) files, if present
24
  28 aug 2016: listing.txt is always written inside the repo dir (instead of inside current dir)
24
  28 aug 2016: listing.txt is always written inside the repo dir (instead of inside current dir)
25
  27 aug 2016: accepting full paths to repos (starting with /...)
25
  27 aug 2016: accepting full paths to repos (starting with /...)
26
  07 dec 2013: rewritten buildidx in ANSI C89
26
  07 dec 2013: rewritten buildidx in ANSI C89
27
  19 aug 2013: add a compressed version of the index file to repos (index.gz)
27
  19 aug 2013: add a compressed version of the index file to repos (index.gz)
28
  22 jul 2013: creating a listing.txt file with list of packages
28
  22 jul 2013: creating a listing.txt file with list of packages
29
  18 jul 2013: writing the number of packaged into the first line of the lst file
29
  18 jul 2013: writing the number of packaged into the first line of the lst file
30
  11 jul 2013: added a switch to 7za to make it case insensitive when extracting lsm files
30
  11 jul 2013: added a switch to 7za to make it case insensitive when extracting lsm files
31
  10 jul 2013: changed unzip calls to 7za (to handle cases when appinfo is compressed with lzma)
31
  10 jul 2013: changed unzip calls to 7za (to handle cases when appinfo is compressed with lzma)
32
  04 feb 2013: added CRC32 support
32
  04 feb 2013: added CRC32 support
33
  22 sep 2012: forked 1st version from FDUPDATE builder
33
  22 sep 2012: forked 1st version from FDUPDATE builder
34
*/
34
*/
35
 
35
 
36
$PVER = "20220221";
36
$PVER = "20220221";
37
 
37
 
38
 
38
 
39
// computes the BSD sum of a file and returns it
39
// computes the BSD sum of a file and returns it
40
function file2bsum($fname) {
40
function file2bsum($fname) {
41
  $result = 0;
41
  $result = 0;
42
 
42
 
43
  $fd = fopen($fname, 'rb');
43
  $fd = fopen($fname, 'rb');
44
  if ($fd === false) return(0);
44
  if ($fd === false) return(0);
45
 
45
 
46
  while (!feof($fd)) {
46
  while (!feof($fd)) {
47
 
47
 
48
    $buff = fread($fd, 1024 * 1024);
48
    $buff = fread($fd, 1024 * 1024);
49
 
49
 
50
    $slen = strlen($buff);
50
    $slen = strlen($buff);
51
    for ($i = 0; $i < $slen; $i++) {
51
    for ($i = 0; $i < $slen; $i++) {
52
      // rotr
52
      // rotr
53
      $result = ($result >> 1) | ($result << 15);
53
      $result = ($result >> 1) | ($result << 15);
54
      // add and truncate to 16 bits
54
      // add and truncate to 16 bits
55
      $result += ord($buff[$i]);
55
      $result += ord($buff[$i]);
56
      $result &= 0xffff;
56
      $result &= 0xffff;
57
    }
57
    }
58
  }
58
  }
59
 
59
 
60
  fclose($fd);
60
  fclose($fd);
61
  return($result);
61
  return($result);
62
}
62
}
63
 
63
 
64
 
64
 
65
// translates a version string into a array of integer values.
65
// translates a version string into a array of integer values.
66
// Accepted formats follow:
66
// Accepted formats follow:
67
//    300.12.1
67
//    300.12.1
68
//    1
68
//    1
69
//    12.2.34.2-4.5
69
//    12.2.34.2-4.5
70
//    1.2c
70
//    1.2c
71
//    1.01 beta+3
71
//    1.01 beta+3
72
//    2013-12-31
72
//    2013-12-31
73
//    20220222 alpha
73
//    20220222 alpha
74
function vertoarr($verstr) {
74
function vertoarr($verstr) {
75
  $subver = array(0,0,0,0);
75
  $subver = array(0,0,0,0);
76
 
76
 
77
  // switch string to lcase for easier processing and trim any leading or trailing white spaces
77
  // switch string to lcase for easier processing and trim any leading or trailing white spaces
78
  $verstr = strtolower(trim($verstr));
78
  $verstr = strtolower(trim($verstr));
79
 
79
 
80
  // replace all '-' and '/' characters to '.' (uniformization of sub-version parts delimiters)
80
  // replace all '-' and '/' characters to '.' (uniformization of sub-version parts delimiters)
81
  $verstr = strtr($verstr, '-/', '..');
81
  $verstr = strtr($verstr, '-/', '..');
82
 
82
 
83
  // is there a subversion value? (for example "+4" in "1.05+4")
83
  // is there a subversion value? (for example "+4" in "1.05+4")
84
  $i = strrpos($verstr, '+', 1);
84
  $i = strrpos($verstr, '+', 1);
85
  if ($i !== false) {
85
  if ($i !== false) {
86
    // validate the svar-version is a proper integer
86
    // validate the svar-version is a proper integer
87
    $svarver = substr($verstr, $i + 1);
87
    $svarver = substr($verstr, $i + 1);
88
    if (! preg_match('/[1-9][0-9]*/', $svarver)) {
88
    if (! preg_match('/[1-9][0-9]*/', $svarver)) {
89
      return(false);
89
      return(false);
90
    }
90
    }
91
    $subver[3] = intval($svarver); // set the +rev as a very minor item
91
    $subver[3] = intval($svarver); // set the +rev as a very minor item
92
    $verstr = substr($verstr, 0, $i);
92
    $verstr = substr($verstr, 0, $i);
93
  }
93
  }
94
 
94
 
95
  // is the version ending with ' alpha', 'beta'?
95
  // is the version ending with ' alpha', 'beta'?
96
  if (preg_match('/ (alpha|beta)$/', $verstr)) {
96
  if (preg_match('/ (alpha|beta)$/', $verstr)) {
97
    $i = strrpos($verstr, ' ');
97
    $i = strrpos($verstr, ' ');
98
    $greek = substr($verstr, $i + 1);
98
    $greek = substr($verstr, $i + 1);
99
    $verstr = trim(substr($verstr, 0, $i));
99
    $verstr = trim(substr($verstr, 0, $i));
100
    if ($greek == 'alpha') {
100
    if ($greek == 'alpha') {
101
      $subver[2] = 1;
101
      $subver[2] = 1;
102
    } else if ($greek == 'beta') {
102
    } else if ($greek == 'beta') {
103
      $subver[2] = 2;
103
      $subver[2] = 2;
104
    } else if ($greek == 'rc') {
104
    } else if ($greek == 'rc') {
105
      $subver[2] = 3;
105
      $subver[2] = 3;
106
    } else {
106
    } else {
107
      return(false);
107
      return(false);
108
    }
108
    }
109
  } else {
109
  } else {
110
    $subver[2] = 99;
110
    $subver[2] = 99;
111
  }
111
  }
112
 
112
 
113
  // does the version string have a single-letter subversion? (1.0c)
113
  // does the version string have a single-letter subversion? (1.0c)
114
  if (preg_match('/[a-z]$/', $verstr)) {
114
  if (preg_match('/[a-z]$/', $verstr)) {
115
    $subver[1] = ord(substr($verstr, -1));
115
    $subver[1] = ord(substr($verstr, -1));
116
    $verstr = substr_replace($verstr, '', -1); // remove last character from string
116
    $verstr = substr_replace($verstr, '', -1); // remove last character from string
117
  }
117
  }
118
 
118
 
119
  // validate the format is supported, should be something no more complex than 1.05.3.33
119
  // validate the format is supported, should be something no more complex than 1.05.3.33
120
  if (! preg_match('/[0-9][0-9.]{0,20}/', $verstr)) {
120
  if (! preg_match('/^[0-9][0-9.]{0,20}$/', $verstr)) {
121
    return(false);
121
    return(false);
122
  }
122
  }
123
 
123
 
124
  // NOTE: a zero right after a separator and trailed with a digit (as in 1.01)
124
  // NOTE: a zero right after a separator and trailed with a digit (as in 1.01)
125
  //       has a special meaning
125
  //       has a special meaning
126
  $exploded = explode('.', $verstr);
126
  $exploded = explode('.', $verstr);
127
  if (count($exploded) > 16) {
127
  if (count($exploded) > 16) {
128
    return(false);
128
    return(false);
129
  }
129
  }
130
  $exploded[16] = $subver[0]; // unused yet
130
  $exploded[16] = $subver[0]; // unused yet
131
  $exploded[17] = $subver[1]; // a-z (1.0c)
131
  $exploded[17] = $subver[1]; // a-z (1.0c)
132
  $exploded[18] = $subver[2]; // alpha/beta
132
  $exploded[18] = $subver[2]; // alpha/beta
133
  $exploded[19] = $subver[3]; // svar-ver (1.0+5)
133
  $exploded[19] = $subver[3]; // svar-ver (1.0+5)
134
  for ($i = 0; $i < 20; $i++) if (empty($exploded[$i])) $exploded[$i] = '0';
134
  for ($i = 0; $i < 20; $i++) if (empty($exploded[$i])) $exploded[$i] = '0';
135
 
135
 
136
  ksort($exploded);
136
  ksort($exploded);
137
 
137
 
138
  return($exploded);
138
  return($exploded);
139
}
139
}
140
 
140
 
141
 
141
 
142
function dos_version_compare($v1, $v2) {
142
function dos_version_compare($v1, $v2) {
143
  $v1arr = vertoarr($v1);
143
  $v1arr = vertoarr($v1);
144
  $v2arr = vertoarr($v2);
144
  $v2arr = vertoarr($v2);
145
  for ($i = 0; $i < count($v1arr); $i++) {
145
  for ($i = 0; $i < count($v1arr); $i++) {
146
    $r = strcmp($v1arr[$i], $v2arr[$i]);
146
    $r = strcmp($v1arr[$i], $v2arr[$i]);
147
    if ($r != 0) return($r);
147
    if ($r != 0) return($r);
148
  }
148
  }
149
  return(0);
149
  return(0);
150
}
150
}
151
 
151
 
152
 
152
 
153
// reads file fil from zip archive z and returns its content, or false on error
153
// reads file fil from zip archive z and returns its content, or false on error
154
function read_file_from_zip($z, $fil) {
154
function read_file_from_zip($z, $fil) {
155
  $zip = new ZipArchive;
155
  $zip = new ZipArchive;
156
  if ($zip->open($z, ZipArchive::RDONLY) !== true) {
156
  if ($zip->open($z, ZipArchive::RDONLY) !== true) {
157
    echo "ERROR: failed to open zip file '{$z}'\n";
157
    echo "ERROR: failed to open zip file '{$z}'\n";
158
    return(false);
158
    return(false);
159
  }
159
  }
160
 
160
 
161
  // load the appinfo/pkgname.lsm file
161
  // load the appinfo/pkgname.lsm file
162
  $res = $zip->getFromName($fil, 8192, ZipArchive::FL_NOCASE);
162
  $res = $zip->getFromName($fil, 8192, ZipArchive::FL_NOCASE);
163
 
163
 
164
  $zip->close();
164
  $zip->close();
165
  return($res);
165
  return($res);
166
}
166
}
167
 
167
 
168
 
168
 
169
function read_list_of_files_in_zip($z) {
169
function read_list_of_files_in_zip($z) {
170
  $zip = new ZipArchive;
170
  $zip = new ZipArchive;
171
  if ($zip->open($z, ZipArchive::RDONLY) !== true) {
171
  if ($zip->open($z, ZipArchive::RDONLY) !== true) {
172
    echo "ERROR: failed to open zip file '{$z}'\n";
172
    echo "ERROR: failed to open zip file '{$z}'\n";
173
    return(false);
173
    return(false);
174
  }
174
  }
175
 
175
 
176
  $res = array();
176
  $res = array();
177
  for ($i = 0; $i < $zip->numFiles; $i++) $res[] = $zip->getNameIndex($i);
177
  for ($i = 0; $i < $zip->numFiles; $i++) $res[] = $zip->getNameIndex($i);
178
 
178
 
179
  $zip->close();
179
  $zip->close();
180
  return($res);
180
  return($res);
181
}
181
}
182
 
182
 
183
 
183
 
184
// reads a LSM string and returns it in the form of an array
184
// reads a LSM string and returns it in the form of an array
185
function parse_lsm($s) {
185
function parse_lsm($s) {
186
  $res = array();
186
  $res = array();
187
  for ($l = strtok($s, "\n"); $l !== false; $l = strtok("\n")) {
187
  for ($l = strtok($s, "\n"); $l !== false; $l = strtok("\n")) {
188
    // the line is "token: value", let's find the colon
188
    // the line is "token: value", let's find the colon
189
    $colpos = strpos($l, ':');
189
    $colpos = strpos($l, ':');
190
    if (($colpos === false) || ($colpos === 0)) continue;
190
    if (($colpos === false) || ($colpos === 0)) continue;
191
    $tok = strtolower(trim(substr($l, 0, $colpos)));
191
    $tok = strtolower(trim(substr($l, 0, $colpos)));
192
    $val = trim(substr($l, $colpos + 1));
192
    $val = trim(substr($l, $colpos + 1));
193
    $res[$tok] = $val;
193
    $res[$tok] = $val;
194
  }
194
  }
195
  return($res);
195
  return($res);
196
}
196
}
197
 
197
 
198
 
198
 
199
// on PHP 8+ there is str_starts_with(), but not on PHP 7 so I use this
199
// on PHP 8+ there is str_starts_with(), but not on PHP 7 so I use this
200
function str_head_is($haystack, $needle) {
200
function str_head_is($haystack, $needle) {
201
  return strpos($haystack, $needle) === 0;
201
  return strpos($haystack, $needle) === 0;
202
}
202
}
203
 
203
 
204
 
204
 
205
// returns an array that contains CORE packages (populated from the core subdirectory in pkgdir)
205
// returns an array that contains CORE packages (populated from the core subdirectory in pkgdir)
206
function load_core_list($repodir) {
206
function load_core_list($repodir) {
207
  $res = array();
207
  $res = array();
208
 
208
 
209
  foreach (scandir($repodir . '/core/') as $f) {
209
  foreach (scandir($repodir . '/core/') as $f) {
210
    if (!preg_match('/\.svp$/', $f)) continue;
210
    if (!preg_match('/\.svp$/', $f)) continue;
211
    $res[] = explode('.', $f)[0];
211
    $res[] = explode('.', $f)[0];
212
  }
212
  }
213
  return($res);
213
  return($res);
214
}
214
}
215
 
215
 
216
 
216
 
217
// ***************** MAIN ROUTINE *********************************************
217
// ***************** MAIN ROUTINE *********************************************
218
 
218
 
219
//echo "SvarDOS repository index generator ver {$PVER}\n";
219
//echo "SvarDOS repository index generator ver {$PVER}\n";
220
 
220
 
221
if (($_SERVER['argc'] != 2) || ($_SERVER['argv'][1][0] == '-')) {
221
if (($_SERVER['argc'] != 2) || ($_SERVER['argv'][1][0] == '-')) {
222
  echo "usage: php buildidx.php repodir\n";
222
  echo "usage: php buildidx.php repodir\n";
223
  exit(1);
223
  exit(1);
224
}
224
}
225
 
225
 
226
$repodir = $_SERVER['argv'][1];
226
$repodir = $_SERVER['argv'][1];
227
 
227
 
228
$pkgfiles = scandir($repodir);
228
$pkgfiles = scandir($repodir);
229
$pkgcount = 0;
229
$pkgcount = 0;
230
 
230
 
231
 
231
 
232
// load the list of CORE and MSDOS_COMPAT packages
232
// load the list of CORE and MSDOS_COMPAT packages
233
 
233
 
234
$core_packages_list = load_core_list($repodir);
234
$core_packages_list = load_core_list($repodir);
235
$msdos_compat_list = explode(' ', 'append assign attrib chkdsk choice command comp cpidos debug defrag deltree diskcomp diskcopy display edit edlin exe2bin fc fdapm fdisk find format help himemx kernel keyb label localcfg mem mirror mode more move nlsfunc print replace share shsucdx sort swsubst tree undelete unformat xcopy');
235
$msdos_compat_list = explode(' ', 'append assign attrib chkdsk choice command comp cpidos debug defrag deltree diskcomp diskcopy display edit edlin exe2bin fc fdapm fdisk find format help himemx kernel keyb label localcfg mem mirror mode more move nlsfunc print replace share shsucdx sort swsubst tree undelete unformat xcopy');
236
 
236
 
237
// do a list of all svp packages with their available versions and descriptions
237
// do a list of all svp packages with their available versions and descriptions
238
 
238
 
239
$pkgdb = array();
239
$pkgdb = array();
240
foreach ($pkgfiles as $fname) {
240
foreach ($pkgfiles as $fname) {
241
  if (!preg_match('/\.svp$/i', $fname)) continue; // skip non-svp files
241
  if (!preg_match('/\.svp$/i', $fname)) continue; // skip non-svp files
242
 
242
 
243
  if (!preg_match('/^[a-zA-Z0-9+. _-]*\.svp$/', $fname)) {
243
  if (!preg_match('/^[a-zA-Z0-9+. _-]*\.svp$/', $fname)) {
244
    echo "ERROR: {$fname} has a very weird name\n";
244
    echo "ERROR: {$fname} has a very weird name\n";
245
    continue;
245
    continue;
246
  }
246
  }
247
 
247
 
248
  $path_parts = pathinfo($fname);
248
  $path_parts = pathinfo($fname);
249
  $pkgnam = explode('-', $path_parts['filename'])[0];
249
  $pkgnam = explode('-', $path_parts['filename'])[0];
250
  $pkgfullpath = realpath($repodir . '/' . $fname);
250
  $pkgfullpath = realpath($repodir . '/' . $fname);
251
 
251
 
252
  $lsm = read_file_from_zip($pkgfullpath, "appinfo/{$pkgnam}.lsm");
252
  $lsm = read_file_from_zip($pkgfullpath, "appinfo/{$pkgnam}.lsm");
253
  if ($lsm == false) {
253
  if ($lsm == false) {
254
    echo "ERROR: {$fname} does not contain an LSM file at the expected location\n";
254
    echo "ERROR: {$fname} does not contain an LSM file at the expected location\n";
255
    continue;
255
    continue;
256
  }
256
  }
257
  $lsmarray = parse_lsm($lsm);
257
  $lsmarray = parse_lsm($lsm);
258
  if (empty($lsmarray['version'])) {
258
  if (empty($lsmarray['version'])) {
259
    echo "ERROR: lsm file in {$fname} does not contain a version\n";
259
    echo "ERROR: lsm file in {$fname} does not contain a version\n";
260
    continue;
260
    continue;
261
  }
261
  }
262
  if (strlen($lsmarray['version']) > 16) {
262
  if (strlen($lsmarray['version']) > 16) {
263
    echo "ERROR: version string in lsm file of {$fname} is too long (16 chars max)\n";
263
    echo "ERROR: version string in lsm file of {$fname} is too long (16 chars max)\n";
264
    continue;
264
    continue;
265
  }
265
  }
266
  if (empty($lsmarray['description'])) {
266
  if (empty($lsmarray['description'])) {
267
    echo "ERROR: lsm file in {$fname} does not contain a description\n";
267
    echo "ERROR: lsm file in {$fname} does not contain a description\n";
268
    continue;
268
    continue;
269
  }
269
  }
270
 
270
 
271
  // validate the files present in the archive
271
  // validate the files present in the archive
272
  $listoffiles = read_list_of_files_in_zip($pkgfullpath);
272
  $listoffiles = read_list_of_files_in_zip($pkgfullpath);
273
  $pkgdir = $pkgnam;
273
  $pkgdir = $pkgnam;
274
 
274
 
275
  // special rule for "parent and children" packages
275
  // special rule for "parent and children" packages
276
  if (str_head_is($pkgnam, 'djgpp_')) $pkgdir = 'djgpp'; // djgpp_* packages put their files in djgpp
276
  if (str_head_is($pkgnam, 'djgpp_')) $pkgdir = 'djgpp'; // djgpp_* packages put their files in djgpp
277
  if ($pkgnam == 'fbc_help') $pkgdir = 'fbc'; // FreeBASIC help goes to the FreeBASIC dir
277
  if ($pkgnam == 'fbc_help') $pkgdir = 'fbc'; // FreeBASIC help goes to the FreeBASIC dir
278
  if ($pkgnam == 'clamdb') $pkgdir = 'clamav'; // data patterns for clamav
278
  if ($pkgnam == 'clamdb') $pkgdir = 'clamav'; // data patterns for clamav
279
 
279
 
280
  // array used to detect duplicated entries after lower-case conversion
280
  // array used to detect duplicated entries after lower-case conversion
281
  $duparr = array();
281
  $duparr = array();
282
 
282
 
283
  // will hold the list of categories that this package belongs to
283
  // will hold the list of categories that this package belongs to
284
  $catlist = array();
284
  $catlist = array();
285
 
285
 
286
  foreach ($listoffiles as $f) {
286
  foreach ($listoffiles as $f) {
287
    $f = strtolower($f);
287
    $f = strtolower($f);
288
    $path_array = explode('/', $f);
288
    $path_array = explode('/', $f);
289
    // emit a warning when non-8+3 filenames are spotted and find duplicates
289
    // emit a warning when non-8+3 filenames are spotted and find duplicates
290
    foreach ($path_array as $item) {
290
    foreach ($path_array as $item) {
291
      if (empty($item)) continue; // skip empty items at end of paths (eg. appinfo/)
291
      if (empty($item)) continue; // skip empty items at end of paths (eg. appinfo/)
292
      if (!preg_match("/[a-z0-9!#$%&'()@^_`{}~-]{1,8}(\.[a-z0-9!#$%&'()@^_`{}~-]{1,3}){0,1}/", $item)) {
292
      if (!preg_match("/[a-z0-9!#$%&'()@^_`{}~-]{1,8}(\.[a-z0-9!#$%&'()@^_`{}~-]{1,3}){0,1}/", $item)) {
293
        echo "WARNING: {$fname} contains a non-8+3 path (or weird char): {$item} (in $f)\n";
293
        echo "WARNING: {$fname} contains a non-8+3 path (or weird char): {$item} (in $f)\n";
294
      }
294
      }
295
    }
295
    }
296
    // look for dups
296
    // look for dups
297
    if (array_search($f, $duparr) !== false) {
297
    if (array_search($f, $duparr) !== false) {
298
      echo "WARNING: {$fname} contains a duplicated entry: '{$f}'\n";
298
      echo "WARNING: {$fname} contains a duplicated entry: '{$f}'\n";
299
    } else {
299
    } else {
300
      $duparr[] = $f;
300
      $duparr[] = $f;
301
    }
301
    }
302
    // LSM file is ok
302
    // LSM file is ok
303
    if ($f === "appinfo/{$pkgnam}.lsm") continue;
303
    if ($f === "appinfo/{$pkgnam}.lsm") continue;
304
    if ($f === "appinfo/") continue;
304
    if ($f === "appinfo/") continue;
305
    // CORE and MSDOS_COMPAT packages are premium citizens and can do a little more
305
    // CORE and MSDOS_COMPAT packages are premium citizens and can do a little more
306
    $core_or_msdoscompat = 0;
306
    $core_or_msdoscompat = 0;
307
    if (array_search($pkgnam, $core_packages_list) !== false) {
307
    if (array_search($pkgnam, $core_packages_list) !== false) {
308
      $catlist[] = 'core';
308
      $catlist[] = 'core';
309
      $core_or_msdoscompat = 1;
309
      $core_or_msdoscompat = 1;
310
    }
310
    }
311
    if (array_search($pkgnam, $msdos_compat_list) !== false) {
311
    if (array_search($pkgnam, $msdos_compat_list) !== false) {
312
      $catlist[] = 'msdos_compat';
312
      $catlist[] = 'msdos_compat';
313
      $core_or_msdoscompat = 1;
313
      $core_or_msdoscompat = 1;
314
    }
314
    }
315
    if ($core_or_msdoscompat == 1) {
315
    if ($core_or_msdoscompat == 1) {
316
      if (str_head_is($f, 'bin/')) continue;
316
      if (str_head_is($f, 'bin/')) continue;
317
      if (str_head_is($f, 'cpi/')) continue;
317
      if (str_head_is($f, 'cpi/')) continue;
318
      if (str_head_is($f, "doc/{$pkgdir}/")) continue;
318
      if (str_head_is($f, "doc/{$pkgdir}/")) continue;
319
      if ($f === 'doc/') continue;
319
      if ($f === 'doc/') continue;
320
      if (str_head_is($f, "nls/{$pkgdir}.")) continue;
320
      if (str_head_is($f, "nls/{$pkgdir}.")) continue;
321
      if ($f === 'nls/') continue;
321
      if ($f === 'nls/') continue;
322
    }
322
    }
323
    // the help package is allowed to put files in... help
323
    // the help package is allowed to put files in... help
324
    if (($pkgnam == 'help') && (str_head_is($f, 'help/'))) continue;
324
    if (($pkgnam == 'help') && (str_head_is($f, 'help/'))) continue;
325
    // must be category-prefixed file, add it to the list of categories for this package
325
    // must be category-prefixed file, add it to the list of categories for this package
326
    $catlist[] = explode('/', $f)[0];
326
    $catlist[] = explode('/', $f)[0];
327
    // well-known "category" dirs are okay
327
    // well-known "category" dirs are okay
328
    if (str_head_is($f, "progs/{$pkgdir}/")) continue;
328
    if (str_head_is($f, "progs/{$pkgdir}/")) continue;
329
    if ($f === 'progs/') continue;
329
    if ($f === 'progs/') continue;
330
    if (str_head_is($f, "devel/{$pkgdir}/")) continue;
330
    if (str_head_is($f, "devel/{$pkgdir}/")) continue;
331
    if ($f === 'devel/') continue;
331
    if ($f === 'devel/') continue;
332
    if (str_head_is($f, "games/{$pkgdir}/")) continue;
332
    if (str_head_is($f, "games/{$pkgdir}/")) continue;
333
    if ($f === 'games/') continue;
333
    if ($f === 'games/') continue;
334
    if (str_head_is($f, "drivers/{$pkgdir}/")) continue;
334
    if (str_head_is($f, "drivers/{$pkgdir}/")) continue;
335
    if ($f === 'drivers/') continue;
335
    if ($f === 'drivers/') continue;
336
    echo "WARNING: {$fname} contains a file in an illegal location: {$f}\n";
336
    echo "WARNING: {$fname} contains a file in an illegal location: {$f}\n";
337
  }
337
  }
338
 
338
 
339
  // do I understand the version string?
339
  // do I understand the version string?
340
  if (vertoarr($lsmarray['version']) === false) echo "WARNING: {$fname} parsing of version string failed ('{$lsmarray['version']}')\n";
340
  if (vertoarr($lsmarray['version']) === false) echo "WARNING: {$fname} parsing of version string failed ('{$lsmarray['version']}')\n";
341
 
341
 
342
  $meta['fname'] = $fname;
342
  $meta['fname'] = $fname;
343
  $meta['desc'] = $lsmarray['description'];
343
  $meta['desc'] = $lsmarray['description'];
344
  $meta['cats'] = array_unique($catlist);
344
  $meta['cats'] = array_unique($catlist);
345
 
345
 
346
  $pkgdb[$pkgnam][$lsmarray['version']] = $meta;
346
  $pkgdb[$pkgnam][$lsmarray['version']] = $meta;
347
}
347
}
348
 
348
 
349
 
349
 
350
$db = array();
350
$db = array();
351
$cats = array();
351
$cats = array();
352
 
352
 
353
// ******** compute the version-sorted list of packages with a single *********
353
// ******** compute the version-sorted list of packages with a single *********
354
// ******** description and category list for each package ********************
354
// ******** description and category list for each package ********************
355
 
355
 
356
// iterate over each svp package
356
// iterate over each svp package
357
foreach ($pkgdb as $pkg => $versions) {
357
foreach ($pkgdb as $pkg => $versions) {
358
 
358
 
359
  // sort filenames by version, highest first
359
  // sort filenames by version, highest first
360
  uksort($versions, "dos_version_compare");
360
  uksort($versions, "dos_version_compare");
361
  $versions = array_reverse($versions, true);
361
  $versions = array_reverse($versions, true);
362
 
362
 
363
  foreach ($versions as $ver => $meta) {
363
  foreach ($versions as $ver => $meta) {
364
    $fname = $meta['fname'];
364
    $fname = $meta['fname'];
365
    $desc = $meta['desc'];
365
    $desc = $meta['desc'];
366
 
366
 
367
    $bsum = file2bsum(realpath($repodir . '/' . $fname));
367
    $bsum = file2bsum(realpath($repodir . '/' . $fname));
368
 
368
 
369
    $meta2['ver'] = strval($ver);
369
    $meta2['ver'] = strval($ver);
370
    $meta2['bsum'] = $bsum;
370
    $meta2['bsum'] = $bsum;
371
 
371
 
372
    if (empty($db[$pkg]['desc'])) $db[$pkg]['desc'] = $desc;
372
    if (empty($db[$pkg]['desc'])) $db[$pkg]['desc'] = $desc;
373
    if (empty($db[$pkg]['cats'])) {
373
    if (empty($db[$pkg]['cats'])) {
374
      $db[$pkg]['cats'] = $meta['cats'];
374
      $db[$pkg]['cats'] = $meta['cats'];
375
      $cats = array_unique(array_merge($cats, $meta['cats']));
375
      $cats = array_unique(array_merge($cats, $meta['cats']));
376
    }
376
    }
377
    $db[$pkg]['versions'][$fname] = $meta2;
377
    $db[$pkg]['versions'][$fname] = $meta2;
378
  }
378
  }
379
 
379
 
380
  $pkgcount++;
380
  $pkgcount++;
381
 
381
 
382
}
382
}
383
 
383
 
384
if ($pkgcount < 100) echo "WARNING: an unexpectedly low number of packages has been found in the repo ({$pkgcount})\n";
384
if ($pkgcount < 100) echo "WARNING: an unexpectedly low number of packages has been found in the repo ({$pkgcount})\n";
385
 
385
 
386
$json_blob = json_encode($db);
386
$json_blob = json_encode($db);
387
if ($json_blob === false) {
387
if ($json_blob === false) {
388
  echo "ERROR: JSON convertion failed! -> ";
388
  echo "ERROR: JSON convertion failed! -> ";
389
  switch (json_last_error()) {
389
  switch (json_last_error()) {
390
    case JSON_ERROR_DEPTH:
390
    case JSON_ERROR_DEPTH:
391
      echo 'maximum stack depth exceeded';
391
      echo 'maximum stack depth exceeded';
392
      break;
392
      break;
393
    case JSON_ERROR_STATE_MISMATCH:
393
    case JSON_ERROR_STATE_MISMATCH:
394
      echo 'underflow of the modes mismatch';
394
      echo 'underflow of the modes mismatch';
395
      break;
395
      break;
396
    case JSON_ERROR_CTRL_CHAR:
396
    case JSON_ERROR_CTRL_CHAR:
397
      echo 'unexpected control character found';
397
      echo 'unexpected control character found';
398
      break;
398
      break;
399
    case JSON_ERROR_UTF8:
399
    case JSON_ERROR_UTF8:
400
      echo 'malformed utf-8 characters';
400
      echo 'malformed utf-8 characters';
401
      break;
401
      break;
402
    default:
402
    default:
403
      echo "unknown error";
403
      echo "unknown error";
404
      break;
404
      break;
405
  }
405
  }
406
  echo "\n";
406
  echo "\n";
407
}
407
}
408
 
408
 
409
file_put_contents($repodir . '/_index.json', $json_blob);
409
file_put_contents($repodir . '/_index.json', $json_blob);
410
 
410
 
411
$cats_json = json_encode($cats);
411
$cats_json = json_encode($cats);
412
file_put_contents($repodir . '/_cats.json', $cats_json);
412
file_put_contents($repodir . '/_cats.json', $cats_json);
413
 
413
 
414
exit(0);
414
exit(0);
415
 
415
 
416
?>
416
?>
417
 
417