forked from fossology/fossology
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fossinit.php
executable file
·562 lines (503 loc) · 18.9 KB
/
fossinit.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#!/usr/bin/php
<?php
/***********************************************************
Copyright (C) 2008-2015 Hewlett-Packard Development Company, L.P.
Copyright (C) 2014-2015 Siemens AG
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************/
/** \brief Print Usage statement.
* \return No return, this calls exit.
**/
function explainUsage()
{
global $argv;
$usage = "Usage: " . basename($argv[0]) . " [options]
Update FOSSology database. This should be used immediately after an install or update. Options are:
-c path to fossology configuration files
-d {database name} default is 'fossology'
-f {file} update the schema with file generated by schema-export.php
-l update the license_ref table with fossology supplied licenses
-r {prefix} drop database with name starts with prefix
-v enable verbose preview (prints sql that would happen, but does not execute it, DB is not updated)
-h this help usage";
print "$usage\n";
exit(0);
}
/**
* @file fossinit.php
* @brief This program applies core-schema.dat to the database (which
* must exist) and updates the license_ref table.
* @return 0 for success, 1 for failure.
**/
use Fossology\Lib\Db\DbManager;
use Fossology\Lib\Db\Driver\Postgres;
/* Note: php 5 getopt() ignores options not specified in the function call, so add
* dummy options in order to catch invalid options.
*/
$AllPossibleOpts = "abc:d:ef:ghijklmnopqr:stuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
/* defaults */
$Verbose = false;
$DatabaseName = "fossology";
$UpdateLiceneseRef = false;
$sysconfdir = '';
$delDbPattern = 'the option -rfosstest will drop data bases with datname like "fosstest%"';
/* command-line options */
$Options = getopt($AllPossibleOpts);
foreach($Options as $optKey => $optVal)
{
switch($optKey)
{
case 'c': /* set SYSCONFIDR */
$sysconfdir = $optVal;
break;
case 'd': /* optional database name */
$DatabaseName = $optVal;
break;
case 'f': /* schema file */
$SchemaFilePath = $optVal;
break;
case 'h': /* help */
explainUsage();
case 'l': /* update the license_ref table */
$UpdateLiceneseRef = true;
break;
case 'v': /* verbose */
$Verbose = true;
break;
case 'r':
$delDbPattern = $optVal ? "$optVal%" : "fosstest%";
break;
default:
echo "Invalid Option \"$optKey\".\n";
explainUsage();
}
}
/* Set SYSCONFDIR and set global (for backward compatibility) */
$SysConf = bootstrap($sysconfdir);
$projectGroup = $SysConf['DIRECTORIES']['PROJECTGROUP'] ?: 'fossy';
$gInfo = posix_getgrnam($projectGroup);
posix_setgid($gInfo['gid']);
$groups = `groups`;
if (!preg_match("/\s$projectGroup\s/",$groups) && (posix_getgid() != $gInfo['gid']))
{
print "FATAL: You must be in group '$projectGroup'.\n";
exit(1);
}
require_once("$MODDIR/vendor/autoload.php");
require_once("$MODDIR/lib/php/common-db.php");
require_once("$MODDIR/lib/php/common-container.php");
require_once("$MODDIR/lib/php/common-cache.php");
require_once("$MODDIR/lib/php/common-sysconfig.php");
/* Initialize global system configuration variables $SysConfig[] */
ConfigInit($SYSCONFDIR, $SysConf);
/** delete from copyright where pfile_fk not in (select pfile_pk from pfile) */
/** add foreign constraint on copyright pfile_fk if not exist */
/** comment out for 2.5.0
require_once("$LIBEXECDIR/dbmigrate_2.0-2.5-pre.php");
Migrate_20_25($Verbose);
*/
if (empty($SchemaFilePath)) {
$SchemaFilePath = "$MODDIR/www/ui/core-schema.dat";
}
if (!file_exists($SchemaFilePath))
{
print "FAILED: Schema data file ($SchemaFilePath) not found.\n";
exit(1);
}
require_once("$MODDIR/lib/php/libschema.php");
$pgDriver = new Postgres($PG_CONN);
$libschema->setDriver($pgDriver);
$previousSchema = $libschema->getCurrSchema();
$isUpdating = array_key_exists('TABLE', $previousSchema) && array_key_exists('users', $previousSchema['TABLE']);
/* @var $dbManager DbManager */
if ($dbManager->existsTable('sysconfig'))
{
$sysconfig = $dbManager->createMap('sysconfig', 'variablename', 'conf_value');
if(!array_key_exists('Release', $sysconfig))
{
$sysconfig['Release'] = 0;
}
print "Old release was $sysconfig[Release]\n";
}
$migrateColumns = array('clearing_decision'=>array('reportinfo','clearing_pk','type_fk','comment'),
'license_ref_bulk'=>array('rf_fk','removing'));
if($isUpdating && !empty($sysconfig) && $sysconfig['Release'] == '2.6.3.1')
{
$dbManager->queryOnce('begin;
CREATE TABLE uploadtree_b AS (SELECT * FROM uploadtree_a);
DROP TABLE uploadtree_a;
CREATE TABLE uploadtree_a () INHERITS (uploadtree);
ALTER TABLE uploadtree_a ADD CONSTRAINT uploadtree_a_pkey PRIMARY KEY (uploadtree_pk);
INSERT INTO uploadtree_a SELECT * FROM uploadtree_b;
DROP TABLE uploadtree_b;
COMMIT;',__FILE__.'.rebuild.uploadtree_a');
}
$FailMsg = $libschema->applySchema($SchemaFilePath, $Verbose, $DatabaseName, $migrateColumns);
if ($FailMsg)
{
print "ApplySchema failed: $FailMsg\n";
exit(1);
}
$Filename = "$MODDIR/www/ui/init.ui";
$flagRemoved = !file_exists($Filename);
if (!$flagRemoved)
{
if ($Verbose)
{
print "Removing flag '$Filename'\n";
}
if (is_writable("$MODDIR/www/ui/"))
{
$flagRemoved = unlink($Filename);
}
}
if (!$flagRemoved)
{
print "Failed to remove $Filename\n";
print "Remove this file to complete the initialization.\n";
}
else
{
print "Database schema update completed successfully.\n";
}
/* initialize the license_ref table */
if ($UpdateLiceneseRef)
{
$row = $dbManager->getSingleRow("SELECT count(*) FROM license_ref",array(),'license_ref.count');
if ($row['count'] > 0) {
print "Update reference licenses\n";
initLicenseRefTable(false);
}
else if ($row['count'] == 0) {
/** import licenseref.sql */
$sqlstmts = file_get_contents("$LIBEXECDIR/licenseref.sql");
$dbManager->queryOnce($sqlstmts,$stmt=__METHOD__."$LIBEXECDIR/licenseref.sql");
$row_max = $dbManager->getSingleRow("SELECT max(rf_pk) from license_ref",array(),'license_ref.max.rf_pk');
$current_license_ref_rf_pk_seq = $row_max['max'];
$dbManager->getSingleRow("SELECT setval('license_ref_rf_pk_seq', $current_license_ref_rf_pk_seq)",array(),
'set next license_ref_rf_pk_seq value');
print "fresh install, import licenseref.sql \n";
}
}
if (array_key_exists('r', $Options))
{
$dbManager->prepare(__METHOD__.".getDelDbNames",'SELECT datname FROM pg_database WHERE datistemplate = false and datname like $1');
$resDelDbNames = $dbManager->execute(__METHOD__.".getDelDbNames",array($delDbPattern));
$delDbNames=pg_fetch_all($resDelDbNames);
pg_free_result($resDelDbNames);
foreach ($delDbNames as $deleteDatabaseName)
{
$dbManager->queryOnce("DROP DATABASE $deleteDatabaseName[datname]");
}
if ($Verbose)
{
echo "dropped " . count($delDbNames) . " databases ";
}
}
/* migration */
$currSchema = $libschema->getCurrSchema();
$sysconfig = $dbManager->createMap('sysconfig','variablename','conf_value');
global $LIBEXECDIR;
if($isUpdating && empty($sysconfig['Release'])) {
require_once("$LIBEXECDIR/dbmigrate_2.0-2.1.php"); // this is needed for all new installs from 2.0 on
Migrate_20_21($Verbose);
require_once("$LIBEXECDIR/dbmigrate_2.1-2.2.php");
print "Migrate data from 2.1 to 2.2 in $LIBEXECDIR\n";
Migrate_21_22($Verbose);
if($dbManager->existsTable('license_file_audit') && array_key_exists('clearing_pk', $currSchema['TABLE']['clearing_decision']))
{
require_once("$LIBEXECDIR/dbmigrate_2.5-2.6.php");
migrate_25_26($Verbose);
}
if(!array_key_exists('clearing_pk', $currSchema['TABLE']['clearing_decision']) && $isUpdating)
{
$timeoutSec = 20;
echo "Missing column clearing_decision.clearing_pk, you should update to version 2.6.2 before migration\n";
echo "Enter 'i' within $timeoutSec seconds to ignore this warning and run the risk of losing clearing decisions: ";
$handle = fopen ("php://stdin","r");
stream_set_blocking($handle,0);
for($s=0;$s<$timeoutSec;$s++)
{
sleep(1);
$line = fread($handle,1);
if ($line) {
break;
}
}
if(trim($line) != 'i')
{
echo "ABORTING!\n";
exit(26);
}
}
$sysconfig['Release'] = '2.6';
}
if(!$isUpdating)
{
require_once("$LIBEXECDIR/dbmigrate_2.1-2.2.php");
print "Creating default user\n";
Migrate_21_22($Verbose);
}
if(!$isUpdating || $sysconfig['Release'] == '2.6')
{
if(!$dbManager->existsTable('license_candidate'))
{
$dbManager->queryOnce("CREATE TABLE license_candidate (group_fk integer) INHERITS (license_ref)");
}
if ($isUpdating && array_key_exists('clearing_pk', $currSchema['TABLE']['clearing_decision']))
{
require_once("$LIBEXECDIR/dbmigrate_clearing-event.php");
$libschema->dropColumnsFromTable(array('reportinfo','clearing_pk','type_fk','comment'), 'clearing_decision');
}
$sysconfig['Release'] = '2.6.3';
}
if($sysconfig['Release'] == '2.6.3')
{
require_once("$LIBEXECDIR/dbmigrate_real-parent.php");
}
$expiredDbReleases = array('2.6.3', '2.6.3.1', '2.6.3.2');
if($isUpdating && (empty($sysconfig['Release']) || in_array($sysconfig['Release'], $expiredDbReleases)))
{
require_once("$LIBEXECDIR/fo_mapping_license.php");
print "Rename license (using $LIBEXECDIR) for SPDX validity\n";
renameLicensesForSpdxValidation($Verbose);
}
$expiredDbReleases[] = '2.6.3.3';
$expiredDbReleases[] = '3.0.0';
if($isUpdating && (empty($sysconfig['Release']) || in_array($sysconfig['Release'], $expiredDbReleases)))
{
require_once("$LIBEXECDIR/dbmigrate_bulk_license.php");
}
if(in_array($sysconfig['Release'], $expiredDbReleases))
{
$sysconfig['Release'] = '3.0.1';
}
// Update '3dfx' licence shortname to 'Glide'. Since shortname is used as an
// identifier, this is not done as part of the licenseref updates.
if($isUpdating && (empty($sysconfig['Release']) || $sysconfig['Release'] == '3.0.1'))
{
$dbManager->begin();
$row = $dbManager->getSingleRow("
SELECT rf1.rf_pk AS id_3dfx,
rf2.rf_pk AS id_glide
FROM license_ref rf1
INNER JOIN license_ref rf2 USING (rf_fullname)
WHERE rf1.rf_shortname='3DFX'
AND rf2.rf_shortname='Glide'
LIMIT 1", array(), 'old.3dfx.rf_pk');
if (count($row))
{
$id_3dfx = intval($row['id_3dfx']);
$id_glide = intval($row['id_glide']);
$dbManager->queryOnce("DELETE FROM license_ref WHERE rf_pk=$id_glide");
$dbManager->queryOnce("UPDATE license_ref SET rf_shortname='Glide' WHERE rf_pk=$id_3dfx");
}
$dbManager->commit();
$sysconfig['Release'] = "3.0.2";
}
if($isUpdating && (empty($sysconfig['Release']) || $sysconfig['Release'] == '3.0.2'))
{
require_once("$LIBEXECDIR/dbmigrate_multiple_copyright_decisions.php");
$sysconfig['Release'] = "3.1.0";
}
$dbManager->begin();
$dbManager->getSingleRow("DELETE FROM sysconfig WHERE variablename=$1",array('Release'),$sqlLog='drop.sysconfig.release');
$dbManager->insertTableRow('sysconfig',
array('variablename'=>'Release','conf_value'=>$sysconfig['Release'],'ui_label'=>'Release','vartype'=>2,'group_name'=>'Release','description'=>''));
$dbManager->commit();
/* email/url/author data migration to other table */
require_once("$LIBEXECDIR/dbmigrate_copyright-author.php");
/* sanity check */
require_once ("$LIBEXECDIR/sanity_check.php");
$checker = new SanityChecker($dbManager,$Verbose);
$errors = $checker->check();
if($errors>0)
{
echo "ERROR: $errors sanity check".($errors>1?'s':'')." failed\n";
}
exit($errors);
/**
* \brief Load the license_ref table with licenses.
*
* \param $Verbose display database load progress information. If $Verbose is false,
* this function only prints errors.
*
* \return 0 on success, 1 on failure
**/
function initLicenseRefTable($Verbose)
{
global $LIBEXECDIR;
global $dbManager;
if (!is_dir($LIBEXECDIR)) {
print "FATAL: Directory '$LIBEXECDIR' does not exist.\n";
return (1);
}
$dir = opendir($LIBEXECDIR);
if (!$dir) {
print "FATAL: Unable to access '$LIBEXECDIR'.\n";
return (1);
}
$dbManager->queryOnce("BEGIN");
$dbManager->queryOnce("DROP TABLE IF EXISTS license_ref_2",$stmt=__METHOD__.'.dropAncientBackUp');
/* create a new temp table structure only - license_ref_2 */
$dbManager->queryOnce("CREATE TABLE license_ref_2 as select * from license_ref WHERE 1=2",$stmt=__METHOD__.'.backUpData');
/** import licenseref.sql */
$sqlstmts = file_get_contents("$LIBEXECDIR/licenseref.sql");
$sqlstmts = str_replace("license_ref","license_ref_2", $sqlstmts);
$dbManager->queryOnce($sqlstmts);
$dbManager->prepare(__METHOD__.".newLic", "select * from license_ref_2");
$result_new = $dbManager->execute(__METHOD__.".newLic");
$dbManager->prepare(__METHOD__.'.licenseRefByShortname','SELECT * from license_ref where rf_shortname=$1');
/** traverse all records in user's license_ref table, update or insert */
while ($row = pg_fetch_assoc($result_new))
{
$rf_shortname = $row['rf_shortname'];
$escaped_name = pg_escape_string($rf_shortname);
$result_check = $dbManager->execute(__METHOD__.'.licenseRefByShortname',array($rf_shortname));
$count = pg_num_rows($result_check);
$rf_text = pg_escape_string($row['rf_text']);
$rf_url = pg_escape_string($row['rf_url']);
$rf_fullname = pg_escape_string($row['rf_fullname']);
$rf_notes = pg_escape_string($row['rf_notes']);
$rf_active = $row['rf_active'];
$marydone = $row['marydone'];
$rf_text_updatable = $row['rf_text_updatable'];
$rf_detector_type = $row['rf_detector_type'];
if ($count) // update when it is existing
{
$row_check = pg_fetch_assoc($result_check);
pg_free_result($result_check);
$rf_text_check = pg_escape_string($row_check['rf_text']);
$rf_url_check = pg_escape_string($row_check['rf_url']);
$rf_fullname_check = pg_escape_string($row_check['rf_fullname']);
$rf_notes_check = pg_escape_string($row_check['rf_notes']);
$rf_active_check = $row_check['rf_active'];
$marydone_check = $row_check['marydone'];
$rf_text_updatable_check = $row_check['rf_text_updatable'];
$rf_detector_type_check = $row_check['rf_detector_type'];
$sql = "UPDATE license_ref set ";
if ($rf_text_check != $rf_text && !empty($rf_text) && !(stristr($rf_text, 'License by Nomos'))) $sql .= " rf_text='$rf_text',";
if ($rf_url_check != $rf_url && !empty($rf_url)) $sql .= " rf_url='$rf_url',";
if ($rf_fullname_check != $rf_fullname && !empty($rf_fullname)) $sql .= " rf_fullname ='$rf_fullname',";
if ($rf_notes_check != $rf_notes && !empty($rf_notes)) $sql .= " rf_notes ='$rf_notes',";
if ($rf_active_check != $rf_active && !empty($rf_active)) $sql .= " rf_active ='$rf_active',";
if ($marydone_check != $marydone && !empty($marydone)) $sql .= " marydone ='$marydone',";
if ($rf_text_updatable_check != $rf_text_updatable && !empty($rf_text_updatable)) $sql .= " rf_text_updatable ='$rf_text_updatable',";
if ($rf_detector_type_check != $rf_detector_type && !empty($rf_detector_type)) $sql .= " rf_detector_type = '$rf_detector_type',";
$sql = substr_replace($sql ,"",-1);
if ($sql != "UPDATE license_ref set") // check if have something to update
{
$sql .= " where rf_shortname = '$escaped_name'";
$dbManager->queryOnce($sql);
}
}
else // insert when it is new
{
pg_free_result($result_check);
$sql = "INSERT INTO license_ref (rf_shortname, rf_text, rf_url, rf_fullname, rf_notes, rf_active, rf_text_updatable, rf_detector_type, marydone)"
. "VALUES ('$escaped_name', '$rf_text', '$rf_url', '$rf_fullname', '$rf_notes', '$rf_active', '$rf_text_updatable', '$rf_detector_type', '$marydone');";
$dbManager->queryOnce($sql);
}
}
pg_free_result($result_new);
$dbManager->queryOnce("DROP TABLE license_ref_2");
$dbManager->queryOnce("COMMIT");
return (0);
} // initLicenseRefTable()
function guessSysconfdir()
{
$rcfile = "fossology.rc";
$varfile = dirname(__DIR__).'/variable.list';
$sysconfdir = getenv('SYSCONFDIR');
if ((false===$sysconfdir) && file_exists($rcfile))
{
$sysconfdir = file_get_contents($rcfile);
}
if ((false===$sysconfdir) && file_exists($varfile))
{
$ini_array = parse_ini_file($varfile);
if($ini_array!==false && array_key_exists('SYSCONFDIR', $ini_array))
{
$sysconfdir = $ini_array['SYSCONFDIR'];
}
}
if (false===$sysconfdir)
{
$text = _("FATAL! System Configuration Error, no SYSCONFDIR.");
echo "$text\n";
exit(1);
}
return $sysconfdir;
}
/**
* \brief Determine SYSCONFDIR, parse fossology.conf
*
* \param $sysconfdir Typically from the caller's -c command line parameter
*
* \return the $SysConf array of values. The first array dimension
* is the group, the second is the variable name.
* For example:
* - $SysConf[DIRECTORIES][MODDIR] => "/mymoduledir/
*
* The global $SYSCONFDIR is also set for backward compatibility.
*
* \Note Since so many files expect directory paths that used to be in pathinclude.php
* to be global, this function will define the same globals (everything in the
* DIRECTORIES section of fossology.conf).
*/
function bootstrap($sysconfdir="")
{
if (empty($sysconfdir))
{
$sysconfdir = guessSysconfdir();
echo "assuming SYSCONFDIR=$sysconfdir\n";
}
$sysconfdir = trim($sysconfdir);
$GLOBALS['SYSCONFDIR'] = $sysconfdir;
/************* Parse fossology.conf *******************/
$ConfFile = "{$sysconfdir}/fossology.conf";
if (!file_exists($ConfFile))
{
$text = _("FATAL! Missing configuration file: $ConfFile");
echo "$text\n";
exit(1);
}
$SysConf = parse_ini_file($ConfFile, true);
if ($SysConf === false)
{
$text = _("FATAL! Invalid configuration file: $ConfFile");
echo "$text\n";
exit(1);
}
/* evaluate all the DIRECTORIES group for variable substitutions.
* For example, if PREFIX=/usr/local and BINDIR=$PREFIX/bin, we
* want BINDIR=/usr/local/bin
*/
foreach($SysConf['DIRECTORIES'] as $var=>$assign)
{
$toeval = "\$$var = \"$assign\";";
eval($toeval);
/* now reassign the array value with the evaluated result */
$SysConf['DIRECTORIES'][$var] = ${$var};
$GLOBALS[$var] = ${$var};
}
if (empty($MODDIR))
{
$text = _("FATAL! System initialization failure: MODDIR not defined in $SysConf");
echo "$text\n";
exit(1);
}
//require("i18n.php"); DISABLED until i18n infrastructure is set-up.
require_once("$MODDIR/lib/php/common.php");
require_once("$MODDIR/lib/php/Plugin/FO_Plugin.php");
return $SysConf;
}