Rev 1426 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1426 | daniel-mar | 1 | #!/usr/bin/env php |
2 | <?php |
||
3 | |||
4 | /* |
||
5 | * OIDplus 2.0 |
||
6 | * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft |
||
7 | * |
||
8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
||
9 | * you may not use this file except in compliance with the License. |
||
10 | * You may obtain a copy of the License at |
||
11 | * |
||
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
||
13 | * |
||
14 | * Unless required by applicable law or agreed to in writing, software |
||
15 | * distributed under the License is distributed on an "AS IS" BASIS, |
||
16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||
17 | * See the License for the specific language governing permissions and |
||
18 | * limitations under the License. |
||
19 | */ |
||
20 | |||
21 | // This script will be called at the ViaThinkSoft server side |
||
22 | |||
23 | use ViaThinkSoft\OIDplus\OIDplus; |
||
24 | |||
25 | require_once __DIR__.'/funcs.inc.php'; |
||
26 | |||
27 | include __DIR__.'/../../../../../includes/oidplus.inc.php'; |
||
28 | |||
29 | |||
30 | // Generate keypair with: |
||
31 | // openssl genpkey -algorithm RSA -out private.pem -pkeyopt rsa_keygen_bits:8192 |
||
32 | // openssl rsa -pubout -in private.pem -out public.pem |
||
33 | |||
34 | $argc = $_SERVER['argc']; // to please Eclipse for PHP |
||
35 | $argv = $_SERVER['argv']; // to please Eclipse for PHP |
||
36 | |||
37 | if (PHP_SAPI != 'cli') { |
||
38 | fwrite(STDERR, "This file can only be invoked in CLI mode.\n"); |
||
39 | die(); |
||
40 | } |
||
41 | |||
42 | if (DIRECTORY_SEPARATOR != '/') { |
||
43 | fwrite(STDERR, "This script can only run on Unix like systems\n"); |
||
44 | exit(2); |
||
45 | } |
||
46 | |||
47 | if ($argc != 4) { |
||
48 | fwrite(STDERR, "Usage: ".$argv[0]." <targetpath> <privkey> <force(1|0)>\n"); |
||
49 | exit(2); |
||
50 | } |
||
51 | |||
52 | $output_dir = $argv[1]; |
||
53 | $priv_key = $argv[2]; |
||
54 | $force = $argv[3]; |
||
55 | |||
56 | if (!is_dir($output_dir)) { |
||
57 | fwrite(STDERR, "Path $output_dir does not exist!\n"); |
||
58 | exit(1); |
||
59 | } |
||
60 | |||
61 | if (!is_file($priv_key)) { |
||
62 | fwrite(STDERR, "Private key file $priv_key does not exist!\n"); |
||
63 | exit(1); |
||
64 | } |
||
65 | |||
66 | if (($force != '1') && ($force != '0')) { |
||
67 | fwrite(STDERR, "Argument 'force' must be 0 or 1\n"); |
||
68 | exit(1); |
||
69 | } |
||
70 | |||
71 | |||
72 | // Step 0: We need two copies of a up-to-date GIT working copy (to compare old-new for changescripts) |
||
73 | // We do not delete the file in /tmp, because we can re-use it |
||
74 | // It's no secret, so other users can read it. They must not write to it, though. |
||
75 | |||
76 | foreach (['a','b'] as $ab) { |
||
77 | $tmpdir = '/tmp/oidplus_git_'.$ab; |
||
78 | if (!is_dir($tmpdir)) { |
||
1427 | daniel-mar | 79 | exec('git clone '.escapeshellarg(OIDplus::getEditionInfo()['gitrepo']).' '.escapeshellarg($tmpdir).' --quiet', $out, $ec); |
1426 | daniel-mar | 80 | if ($ec != 0) { |
81 | fwrite(STDERR, "GIT Clone failed\n"); |
||
82 | exit(1); |
||
83 | } |
||
84 | } |
||
1427 | daniel-mar | 85 | exec('cd '.escapeshellarg($tmpdir).' && git reset --hard --quiet && git checkout master --quiet && git pull --quiet', $out, $ec); |
1426 | daniel-mar | 86 | if ($ec != 0) { |
87 | fwrite(STDERR, "GIT Pull failed\n"); |
||
88 | exit(1); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | // Step 1: List all GIT commits and determine its OIDplus versions |
||
93 | |||
1427 | daniel-mar | 94 | $git_version_cache_file = OIDplus::localpath() . 'userdata/cache/git_version_cache.json'; |
1426 | daniel-mar | 95 | $git_version_cache = file_exists($git_version_cache_file) ? json_decode(file_get_contents($git_version_cache_file),true) : ["git-to-ver"=>[],"version-first-commit"=>[]]; |
96 | |||
97 | $ec = -1; |
||
98 | $out = array(); |
||
1427 | daniel-mar | 99 | exec('cd /tmp/oidplus_git_a && git reset --hard --quiet && git checkout master --quiet && git log --reverse --quiet', $out, $ec); |
1426 | daniel-mar | 100 | if ($ec != 0) { |
101 | fwrite(STDERR, "GIT Log failed\n"); |
||
102 | exit(1); |
||
103 | } else { |
||
104 | $cont = implode("\n", $out); |
||
105 | preg_match_all('%^commit (.+)\n.+trunk@(\d+) 02e%smU', $cont, $git_commits, PREG_SET_ORDER); |
||
106 | $svn_git = []; |
||
107 | foreach ($git_commits as $git_commit) { |
||
108 | $svn_git[$git_commit[1]] = $git_commit[2]; |
||
109 | } |
||
110 | preg_match_all('%^commit (.+)\n%smU', $cont, $git_commits, PREG_SET_ORDER); |
||
111 | foreach ($git_commits as $git_commit) { |
||
112 | if (isset($git_version_cache["git-to-ver"][$git_commit[1]])) continue; |
||
113 | |||
114 | $ary = explode('/',OIDplus::getEditionInfo()['gitrepo']); |
||
115 | $github_user = $ary[3]; |
||
116 | $github_project = $ary[4]; |
||
117 | $v3_versionfile = 'https://raw.githubusercontent.com/'.$github_user.'/'.$github_project.'/'.$git_commit[1].'/changelog.json.php'; |
||
118 | |||
119 | $svn = $svn_git[$git_commit[1]] ?? 0; |
||
120 | if (($svn >= 1) && ($svn <= 1425)) { |
||
121 | $ver = "2.0.0.$svn"; |
||
122 | $git_version_cache["git-to-ver"][$git_commit[1]] = $ver; |
||
123 | if (!isset($git_version_cache["version-first-commit"][$ver])) { |
||
124 | $git_version_cache["version-first-commit"][$ver] = $git_commit[1]; |
||
125 | } |
||
126 | } else if ($ver = OIDplus::getVersion($v3_versionfile)) { |
||
127 | $git_version_cache["git-to-ver"][$git_commit[1]] = $ver; |
||
128 | if (!isset($git_version_cache["version-first-commit"][$ver])) { |
||
129 | $git_version_cache["version-first-commit"][$ver] = $git_commit[1]; |
||
130 | } |
||
131 | } else { |
||
132 | fwrite(STDERR, "PROBLEM: " . $git_commit[1] . "\n"); |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | file_put_contents($git_version_cache_file, json_encode($git_version_cache, JSON_PRETTY_PRINT)); |
||
138 | |||
139 | // Step 2: Write change-scripts (for ZIP/TAR-GZ distribution channel) |
||
140 | // The order of $git_version_cache is very critical here! |
||
141 | |||
142 | $prev_commit = '-'; |
||
143 | $prev_version = '2.0.0.0'; |
||
144 | foreach ($git_version_cache["version-first-commit"] as $version => $version_first_commit) { |
||
1427 | daniel-mar | 145 | $changescript_file = $output_dir.'/changescript_'.$version.'.txt'; |
1426 | daniel-mar | 146 | if ($force || !file_exists($changescript_file) || !file_exists($changescript_file.'.gz')) { |
1427 | daniel-mar | 147 | echo "Generate changescript for version $version ($version_first_commit)\n"; |
1426 | daniel-mar | 148 | |
149 | if ($prev_commit == '-') { |
||
150 | $old_dir = '/tmp/oidplus_git_empty'; |
||
151 | if (!is_dir($old_dir)) mkdir($old_dir); |
||
152 | } else { |
||
153 | $old_dir = '/tmp/oidplus_git_a'; |
||
1427 | daniel-mar | 154 | exec('cd '.escapeshellarg($old_dir).' && git reset --hard --quiet && git checkout '.escapeshellarg($prev_commit).' --quiet >/dev/null 2>&1', $out, $ec); |
1426 | daniel-mar | 155 | if ($ec != 0) { |
156 | fwrite(STDERR, "GIT Checkout $prev_commit failed\n"); |
||
157 | exit(1); |
||
158 | } |
||
159 | hotfix_dir($prev_version, $old_dir); |
||
160 | } |
||
161 | |||
162 | $new_dir = '/tmp/oidplus_git_b'; |
||
1427 | daniel-mar | 163 | exec('cd '.escapeshellarg($new_dir).' && git reset --hard --quiet && git checkout '.escapeshellarg($version_first_commit).' --quiet >/dev/null 2>&1', $out, $ec); |
1426 | daniel-mar | 164 | if ($ec != 0) { |
165 | fwrite(STDERR, "GIT Checkout $version_first_commit failed\n"); |
||
166 | exit(1); |
||
167 | } |
||
168 | hotfix_dir($version, $new_dir); |
||
169 | |||
170 | oidplus_create_changescript($old_dir, $new_dir, $changescript_file, $prev_version, $version, $priv_key); |
||
171 | |||
172 | if ($prev_commit == '-') { |
||
173 | rmdir($old_dir); |
||
174 | } |
||
175 | } |
||
176 | $prev_commit = $version_first_commit; |
||
177 | $prev_version = $version; |
||
178 | } |