security/core#100 Escape uploaded data to prevent Reflected Cross site scripting...
[civicrm-core.git] / tools / bin / scripts / set-version.php
1 #!/usr/bin/env php
2 <?php
3
4 // Update the data-files within this repo to reflect a new version number.
5 // Example usage:
6 // git checkout origin/master -b master-4.7.29
7 // ./tools/bin/scripts/set-version.php 4.7.29 --commit
8 // git commit -m "Update to version 4.7.29"
9 // git push origin master
10
11 /* *********************************************************************** */
12 /* Boot */
13
14 $civicrm_root = dirname(dirname(dirname(__DIR__)));
15 chdir($civicrm_root);
16
17 /* *********************************************************************** */
18 /* Parse inputs -- $oldVersion, $newVersion, $doCommit */
19
20 $oldVersion = (string) simplexml_load_file("xml/version.xml")->version_no;
21 if (!isVersionValid($oldVersion)) {
22 fatal("failed to read old version from \"xml/version.xml\"\n");
23 }
24
25 $newVersion = @$argv[1];
26 if (!isVersionValid($newVersion)) {
27 fatal("failed to read new version\n");
28 }
29
30 switch (@$argv[2]) {
31 case '--commit':
32 $doCommit = 1;
33 break;
34 case '--no-commit':
35 $doCommit = 0;
36 break;
37 default:
38 fatal("Must specify --commit or --no-commit\n");
39 }
40
41 /* *********************************************************************** */
42 /* Main */
43
44 echo "Changing version from $oldVersion to $newVersion...\n";
45
46 $verName = makeVerName($newVersion);
47 $phpFile = initFile("CRM/Upgrade/Incremental/php/{$verName}.php", function () use ($verName) {
48 ob_start();
49 global $camelNumber;
50 $camelNumber = $verName;
51 require 'CRM/Upgrade/Incremental/php/Template.php';
52 unset($camelNumber);
53 return ob_get_clean();
54 });
55
56 $sqlFile = initFile("CRM/Upgrade/Incremental/sql/{$newVersion}.mysql.tpl", function () use ($newVersion) {
57 return "{* file to handle db changes in $newVersion during upgrade *}\n";
58 });
59
60 updateFile("xml/version.xml", function ($content) use ($newVersion, $oldVersion) {
61 return str_replace($oldVersion, $newVersion, $content);
62 });
63
64 if (file_exists("civicrm-version.php")) {
65 updateFile("civicrm-version.php", function ($content) use ($newVersion, $oldVersion) {
66 return str_replace($oldVersion, $newVersion, $content);
67 });
68 }
69
70 updateFile("sql/civicrm_generated.mysql", function ($content) use ($newVersion, $oldVersion) {
71 return str_replace($oldVersion, $newVersion, $content);
72 });
73
74 updateFile("sql/test_data_second_domain.mysql", function ($content) use ($newVersion, $oldVersion) {
75 return str_replace($oldVersion, $newVersion, $content);
76 });
77
78 if ($doCommit) {
79 $files = "xml/version.xml sql/civicrm_generated.mysql sql/test_data_second_domain.mysql " . escapeshellarg($phpFile) . ' ' . escapeshellarg($sqlFile);
80 passthru("git add $files");
81 passthru("git commit $files -m " . escapeshellarg("Set version to $newVersion"));
82 }
83
84 /* *********************************************************************** */
85 /* Helper functions */
86
87 /**
88 * Update the content of a file.
89 *
90 * @param string $file
91 * @param callable $callback
92 * Function(string $originalContent) => string $newContent.
93 */
94 function updateFile($file, $callback) {
95 if (!file_exists($file)) {
96 die("File does not exist: $file\n");
97 }
98 echo "Update \"$file\"\n";
99 $content = file_get_contents($file);
100 $content = $callback($content);
101 file_put_contents($file, $content);
102 }
103
104 /**
105 * Initialize a file (if it doesn't already exist).
106 * @param string $file
107 * @param callable $callback
108 * Function() => string $newContent.
109 */
110 function initFile($file, $callback) {
111 if (file_exists($file)) {
112 echo "File \"$file\" already exists.\n";
113 }
114 else {
115 echo "Initialize \"$file\"\n";
116 $content = $callback();
117 file_put_contents($file, $content);
118 }
119 return $file;
120 }
121
122 /**
123 * Render a pretty string for a major/minor version number.
124 *
125 * @param string $version
126 * Ex: '5.10.alpha1'
127 * @return string
128 * Ex: 'FiveTen'.
129 */
130 function makeVerName($version) {
131 list ($a, $b) = explode('.', $version);
132 require_once 'CRM/Utils/EnglishNumber.php';
133 return CRM_Utils_EnglishNumber::toCamelCase($a) . CRM_Utils_EnglishNumber::toCamelCase($b);
134 }
135
136 function isVersionValid($v) {
137 return $v && preg_match('/^[0-9a-z\.\-]+$/', $v);
138 }
139
140 /**
141 * @param $error
142 */
143 function fatal($error) {
144 echo $error;
145 echo "usage: set-version.php <new-version> [--commit|--no-commit]\n";
146 echo " With --commit, any changes will be committed automatically the current git branch.\n";
147 echo " With --no-commit, any changes will be left uncommitted.\n";
148 exit(1);
149 }