1#!/usr/bin/env php
2<?php
3/***********************************************************
4 Copyright (C) 2020 Siemens AG
5 Author: Gaurav Mishra <mishra.gaurav@siemens.com>
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 ***********************************************************/
20
21use Fossology\Lib\Db\DbManager;
22
23require_once 'dbmigrate_3.7-3.8.php';
24require_once 'fossinit-common.php';
25
26/**
27 * @file
28 * @brief Recode the entries in copyright and relevant tables to UTF-8.
29 */
30
31/**
32 * @brief Check if current user is root.
33 *
34 * Check if current user is root
35 * @returns True if user is root, false otherwise.
36 */
37function checkCorrectUser()
38{
39  $processUser = posix_getpwuid(posix_geteuid());
40  if ($processUser['uid'] !== 0) {
41    return false;
42  }
43  return true;
44}
45
46function explainRecodeUsage()
47{
48  global $argv;
49
50  $usage = "Usage: " . basename($argv[0]) . " [options]
51  Recode the contents in copyright and child tables. Options are:
52  -c  path to fossology configuration files
53  -d  {database name} default is 'fossology'
54  -h  this help usage";
55  print "$usage\n";
56  exit(0);
57}
58
59/* Condition must be satisfied */
60if (!checkCorrectUser()) {
61  echo "Error: " . basename($argv[0]) . " must run as root user!\n";
62  exit(-2);
63}
64
65/* Note: php 5 getopt() ignores options not specified in the function call, so
66 * add dummy options in order to catch invalid options.
67 */
68$AllPossibleOpts = "abc:d:efghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
69
70/* defaults */
71$DatabaseName = "fossology";
72$sysconfdir = "{$SYSCONFDIR}";
73$force = true;
74
75/* command-line options */
76$Options = getopt($AllPossibleOpts);
77foreach($Options as $optKey => $optVal)
78{
79  switch($optKey)
80  {
81    case 'c': /* set SYSCONFIDR */
82      $sysconfdir = $optVal;
83      break;
84    case 'd': /* optional database name */
85      $DatabaseName = $optVal;
86      break;
87    case 'h': /* help */
88      explainRecodeUsage();
89      break;
90    default:
91      echo "Invalid Option \"$optKey\".\n";
92      explainUsage();
93  }
94}
95
96/* Set SYSCONFDIR and set global (for backward compatibility) */
97$SysConf = bootstrap($sysconfdir);
98$SysConf["DBCONF"]["dbname"] = $DatabaseName;
99$GLOBALS["SysConf"] = array_merge($GLOBALS["SysConf"], $SysConf);
100$projectGroup = $SysConf['DIRECTORIES']['PROJECTGROUP'] ?: 'fossy';
101$gInfo = posix_getgrnam($projectGroup);
102posix_setgid($gInfo['gid']);
103$groups = `groups`;
104if (!preg_match("/\s$projectGroup\s/",$groups) && (posix_getgid() != $gInfo['gid']))
105{
106  print "FATAL: You must be in group '$projectGroup'.\n";
107  exit(1);
108}
109
110require_once("$MODDIR/vendor/autoload.php");
111require_once("$MODDIR/lib/php/common-db.php");
112require_once("$MODDIR/lib/php/common-container.php");
113require_once("$MODDIR/lib/php/common-sysconfig.php");
114
115/* Initialize global system configuration variables $SysConfig[] */
116ConfigInit($SYSCONFDIR, $SysConf);
117
118$dbManager = $GLOBALS["container"]->get("db.manager");
119
120return recodeTables($dbManager, $MODDIR, $force);
121