1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package installer::windows::registry;
25
26use installer::files;
27use installer::globals;
28use installer::worker;
29use installer::windows::msiglobal;
30use installer::windows::idtglobal;
31
32#####################################################
33# Generating the component name from a registryitem
34#####################################################
35
36sub get_registry_component_name
37{
38	my ($registryref, $allvariables) = @_;
39
40	# In this function exists the rule to create components from registryitems
41	# Rule:
42	# The componentname can be directly taken from the ModuleID.
43	# All registryitems belonging to one module can get the same component.
44
45	my $componentname = "";
46	my $isrootmodule = 0;
47
48	if ( $registryref->{'ModuleID'} ) { $componentname = $registryref->{'ModuleID'}; }
49
50	$componentname =~ s/\\/\_/g;
51	$componentname =~ s/\//\_/g;
52	$componentname =~ s/\-/\_/g;
53	$componentname =~ s/\_\s*$//g;
54
55	$componentname = lc($componentname);	# componentnames always lowercase
56
57	if ( $componentname eq "gid_module_root" ) { $isrootmodule = 1; }
58
59	# Attention: Maximum length for the componentname is 72
60
61	# identifying this component as registryitem component
62	$componentname = "registry_" . $componentname;
63
64	$componentname =~ s/gid_module_/g_m_/g;
65	$componentname =~ s/_optional_/_o_/g;
66	$componentname =~ s/_javafilter_/_jf_/g;
67
68	# This componentname must be more specific
69	my $addon = "_";
70	if ( $allvariables->{'PRODUCTNAME'} ) { $addon = $addon . $allvariables->{'PRODUCTNAME'}; }
71	if ( $allvariables->{'PRODUCTVERSION'} ) { $addon = $addon . $allvariables->{'PRODUCTVERSION'}; }
72	$addon = lc($addon);
73	$addon =~ s/ //g;
74	$addon =~ s/-//g;
75	$addon =~ s/\.//g;
76
77	my $styles = "";
78	if ( $registryref->{'Styles'} ) { $styles = $registryref->{'Styles'}; }
79
80	# Layer links must have unique Component GUID for all products. This is necessary, because only the
81	# uninstallation of the last product has to delete registry keys.
82	if ( $styles =~ /\bLAYER_REGISTRY\b/ )
83	{
84		$componentname = "g_m_root_registry_layer_ooo_reglayer";
85		# Styles USE_URELAYERVERSION, USE_OOOBASEVERSION
86		if ( $styles =~ /\bUSE_URELAYERVERSION\b/ ) { $addon = "_ure_" . $allvariables->{'URELAYERVERSION'}; }
87		if ( $styles =~ /\bUSE_OOOBASEVERSION\b/ ) { $addon = "_basis_" . $allvariables->{'OOOBASEVERSION'}; }
88		$addon =~ s/\.//g;
89	}
90
91	$componentname = $componentname . $addon;
92
93	if (( $styles =~ /\bLANGUAGEPACK\b/ ) && ( $installer::globals::languagepack )) { $componentname = $componentname . "_lang"; }
94	if ( $styles =~ /\bALWAYS_REQUIRED\b/ ) { $componentname = $componentname . "_forced"; }
95
96	# Attention: Maximum length for the componentname is 72
97	# %installer::globals::allregistrycomponents_in_this_database_ : resetted for each database
98	# %installer::globals::allregistrycomponents_ : not resetted for each database
99	# Component strings must be unique for the complete product, because they are used for
100	# the creation of the globally unique identifier.
101
102	my $fullname = $componentname;  # This can be longer than 72
103
104	if (( exists($installer::globals::allregistrycomponents_{$fullname}) ) && ( ! exists($installer::globals::allregistrycomponents_in_this_database_{$fullname}) ))
105	{
106		# This is not allowed: One component cannot be installed with different packages.
107		installer::exiter::exit_program("ERROR: Windows registry component \"$fullname\" is already included into another package. This is not allowed.", "get_registry_component_name");
108	}
109
110	if ( exists($installer::globals::allregistrycomponents_{$fullname}) )
111	{
112		$componentname = $installer::globals::allregistrycomponents_{$fullname};
113	}
114	else
115	{
116		if ( length($componentname) > 70 )
117		{
118			$componentname = generate_new_short_registrycomponentname($componentname); # This has to be unique for the complete product, not only one package
119		}
120
121		$installer::globals::allregistrycomponents_{$fullname} = $componentname;
122		$installer::globals::allregistrycomponents_in_this_database_{$fullname} = 1;
123	}
124
125	if ( $isrootmodule ) { $installer::globals::registryrootcomponent = $componentname; }
126
127	return $componentname;
128}
129
130#########################################################
131# Create a shorter version of a long component name,
132# because maximum length in msi database is 72.
133# Attention: In multi msi installation sets, the short
134# names have to be unique over all packages, because
135# this string is used to create the globally unique id
136# -> no resetting of
137# %installer::globals::allshortregistrycomponents
138# after a package was created.
139#########################################################
140
141sub generate_new_short_registrycomponentname
142{
143	my ($componentname) = @_;
144
145	my $startversion = substr($componentname, 0, 60); # taking only the first 60 characters
146	my $subid = installer::windows::msiglobal::calculate_id($componentname, 9); # taking only the first 9 digits
147	my $shortcomponentname = $startversion . "_" . $subid;
148
149	if ( exists($installer::globals::allshortregistrycomponents{$shortcomponentname}) ) { installer::exiter::exit_program("Failed to create unique component name: \"$shortcomponentname\"", "generate_new_short_registrycomponentname"); }
150
151	$installer::globals::allshortregistrycomponents{$shortcomponentname} = 1;
152
153	return $shortcomponentname;
154}
155
156##############################################################
157# Returning identifier for registry table.
158##############################################################
159
160sub get_registry_identifier
161{
162	my ($registry) = @_;
163
164	my $identifier = "";
165
166	if ( $registry->{'gid'} ) { $identifier = $registry->{'gid'}; }
167
168	$identifier = lc($identifier);	# always lower case
169
170	# Attention: Maximum length is 72
171
172	$identifier =~ s/gid_regitem_/g_r_/;
173	$identifier =~ s/_soffice_/_s_/;
174	$identifier =~ s/_clsid_/_c_/;
175	$identifier =~ s/_currentversion_/_cv_/;
176	$identifier =~ s/_microsoft_/_ms_/;
177	$identifier =~ s/_manufacturer_/_mf_/;
178	$identifier =~ s/_productname_/_pn_/;
179	$identifier =~ s/_productversion_/_pv_/;
180	$identifier =~ s/_staroffice_/_so_/;
181	$identifier =~ s/_software_/_sw_/;
182	$identifier =~ s/_capabilities_/_cap_/;
183	$identifier =~ s/_classpath_/_cp_/;
184	$identifier =~ s/_extension_/_ex_/;
185	$identifier =~ s/_fileassociations_/_fa_/;
186	$identifier =~ s/_propertysheethandlers_/_psh_/;
187	$identifier =~ s/__/_/g;
188
189	# Saving this in the registry collector
190
191	$registry->{'uniquename'} = $identifier;
192
193	return $identifier;
194}
195
196##################################################################
197# Returning root value for registry table.
198##################################################################
199
200sub get_registry_root
201{
202	my ($registry) = @_;
203
204	my $rootvalue = 0;	# Default: Parent is KKEY_CLASSES_ROOT
205	my $scproot = "";
206
207	if ( $registry->{'ParentID'} ) { $scproot = $registry->{'ParentID'}; }
208
209	if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE" ) { $rootvalue = -1; }
210
211	if ( $scproot eq "PREDEFINED_HKEY_CLASSES_ROOT" ) { $rootvalue = 0; }
212
213	if ( $scproot eq "PREDEFINED_HKEY_CURRENT_USER_ONLY" ) { $rootvalue = 1; }
214
215	if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE_ONLY" ) { $rootvalue = 2; }
216
217	return $rootvalue;
218}
219
220##############################################################
221# Returning key for registry table.
222##############################################################
223
224sub get_registry_key
225{
226	my ($registry, $allvariableshashref) = @_;
227
228	my $key = "";
229
230	if ( $registry->{'Subkey'} ) { $key = $registry->{'Subkey'}; }
231
232	if ( $key =~ /\%/ ) { $key = installer::worker::replace_variables_in_string($key, $allvariableshashref); }
233
234	return $key;
235}
236
237##############################################################
238# Returning name for registry table.
239##############################################################
240
241sub get_registry_name
242{
243	my ($registry, $allvariableshashref) = @_;
244
245	my $name = "";
246
247	if ( $registry->{'Name'} ) { $name = $registry->{'Name'}; }
248
249	if ( $name =~ /\%/ ) { $name = installer::worker::replace_variables_in_string($name, $allvariableshashref); }
250
251	return $name;
252}
253
254##############################################################
255# Returning value for registry table.
256##############################################################
257
258sub get_registry_value
259{
260	my ($registry, $allvariableshashref) = @_;
261
262	my $value = "";
263
264	if ( $registry->{'Value'} ) { $value = $registry->{'Value'}; }
265
266	$value =~ s/\\\"/\"/g;	# no more masquerading of '"'
267	$value =~ s/\\\\\s*$/\\/g;	# making "\\" at end of value to "\"
268	$value =~ s/\<progpath\>/\[INSTALLLOCATION\]/;
269	$value =~ s/\[INSTALLLOCATION\]\\/\[INSTALLLOCATION\]/;	# removing "\" after "[INSTALLLOCATION]"
270
271	if ( $value =~ /\%/ ) { $value = installer::worker::replace_variables_in_string($value, $allvariableshashref); }
272
273	return $value;
274}
275
276##############################################################
277# Returning 64 bit value for registry table.
278##############################################################
279
280sub get_registry_val64
281{
282	my ($registry, $allvariableshashref) = @_;
283
284	my $value = "";
285
286	if ( $registry->{'Val64'} ) { $value = $registry->{'Val64'}; }
287
288	$value =~ s/\\\"/\"/g;	# no more masquerading of '"'
289	$value =~ s/\\\\\s*$/\\/g;	# making "\\" at end of value to "\"
290	$value =~ s/\<progpath\>/\[INSTALLLOCATION\]/;
291	$value =~ s/\[INSTALLLOCATION\]\\/\[INSTALLLOCATION\]/;	# removing "\" after "[INSTALLLOCATION]"
292
293	if ( $value =~ /\%/ ) { $value = installer::worker::replace_variables_in_string($value, $allvariableshashref); }
294
295	return $value;
296}
297
298##############################################################
299# Returning component for registry table.
300##############################################################
301
302sub get_registry_component
303{
304	my ($registry, $allvariables) = @_;
305
306	# All registry items belonging to one module can
307	# be included into one component
308
309	my $componentname = get_registry_component_name($registry, $allvariables);
310
311	# saving componentname in the registryitem collector
312
313	$registry->{'componentname'} = $componentname;
314
315	return $componentname;
316}
317
318######################################################
319# Adding the content of
320# @installer::globals::userregistrycollector
321# to the registry table. The content was collected
322# in create_files_table() in file.pm.
323######################################################
324
325sub add_userregs_to_registry_table
326{
327	my ( $registrytable, $allvariables ) = @_;
328
329	for ( my $i = 0; $i <= $#installer::globals::userregistrycollector; $i++ )
330	{
331		my $onefile = $installer::globals::userregistrycollector[$i];
332
333		my $styles = "";
334		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
335
336		my %registry = ();
337
338		$registry{'Registry'} = $onefile->{'userregkeypath'};
339		$registry{'Root'} = "1";  # always HKCU
340		$registry{'Key'} = "Software\\$allvariables->{'MANUFACTURER'}\\$allvariables->{'PRODUCTNAME'} $allvariables->{'PRODUCTVERSION'}\\";
341		if ( $onefile->{'needs_user_registry_key'} ) { $registry{'Key'} = $registry{'Key'} . "StartMenu"; }
342		else { $registry{'Key'} = $registry{'Key'} . "ShellNew"; }
343		$registry{'Name'} = $onefile->{'Name'};
344		$registry{'Value'} = "1";
345		$registry{'Component_'} = $onefile->{'componentname'};
346
347		my $oneline = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
348					. $registry{'Name'} . "\t" . $registry{'Value'} . "\t" . $registry{'Component_'} . "\n";
349
350		push(@{$registrytable}, $oneline);
351	}
352}
353
354######################################################
355# Creating the file Registry.idt dynamically
356# Content:
357# Registry Root Key Name Value Component_
358######################################################
359
360sub create_registry_table
361{
362	my ($registryref, $allregistrycomponentsref, $basedir, $languagesarrayref, $allvariableshashref) = @_;
363
364	for ( my $m = 0; $m <= $#{$languagesarrayref}; $m++ )
365	{
366		my $onelanguage = ${$languagesarrayref}[$m];
367
368		my @registrytable = ();
369		my @reg64table = ();
370
371		installer::windows::idtglobal::write_idt_header(\@registrytable, "registry");
372		installer::windows::idtglobal::write_idt_header(\@reg64table, "reg64");
373
374		for ( my $i = 0; $i <= $#{$registryref}; $i++ )
375		{
376			my $oneregistry = ${$registryref}[$i];
377
378			# Controlling the language!
379			# Only language independent folderitems or folderitems with the correct language
380			# will be included into the table
381
382			if (! (!(( $oneregistry->{'ismultilingual'} )) || ( $oneregistry->{'specificlanguage'} eq $onelanguage )) )  { next; }
383
384			my %registry = ();
385
386			$registry{'Registry'} = get_registry_identifier($oneregistry);
387			$registry{'Root'} = get_registry_root($oneregistry);
388			$registry{'Key'} = get_registry_key($oneregistry, $allvariableshashref);
389			$registry{'Name'} = get_registry_name($oneregistry, $allvariableshashref);
390			$registry{'Value'} = get_registry_value($oneregistry, $allvariableshashref);
391			$registry{'Val64'} = get_registry_val64($oneregistry, $allvariableshashref);
392			$registry{'Component_'} = get_registry_component($oneregistry, $allvariableshashref);
393
394			# Collecting all components
395			if (!(installer::existence::exists_in_array($registry{'Component_'}, $allregistrycomponentsref)))
396			{
397				push(@{$allregistrycomponentsref}, $registry{'Component_'});
398			}
399
400			# Collecting all components with DONT_DELETE style
401			my $style = "";
402			if ( $oneregistry->{'Styles'} ) { $style = $oneregistry->{'Styles'}; }
403			if ( $style =~ /\bDONT_DELETE\b/ ) { $installer::globals::dontdeletecomponents{$registry{'Component_'}} = 1; }
404
405			# Saving upgradekey to write this into setup.ini for minor upgrades
406			if ( $style =~ /\bUPGRADEKEY\b/ ) { $installer::globals::minorupgradekey = $registry{'Key'}; }
407
408			# Collecting all registry components with ALWAYS_REQUIRED style
409			if ( ! ( $style =~ /\bALWAYS_REQUIRED\b/ ))
410			{
411				# Setting a component condition for unforced registry components!
412				# Only write into registry, if WRITE_REGISTRY is set.
413				if ( $oneregistry->{'ComponentCondition'} ) { $oneregistry->{'ComponentCondition'} = "(" . $oneregistry->{'ComponentCondition'} . ") AND (WRITE_REGISTRY=1)"; }
414				else { $oneregistry->{'ComponentCondition'} = "WRITE_REGISTRY=1"; }
415			}
416
417			# Collecting all component conditions
418			if ( $oneregistry->{'ComponentCondition'} )
419			{
420				if ( ! exists($installer::globals::componentcondition{$registry{'Component_'}}))
421				{
422					$installer::globals::componentcondition{$registry{'Component_'}} = $oneregistry->{'ComponentCondition'};
423				}
424			}
425
426			my $oneline = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
427						. $registry{'Name'} . "\t" . $registry{'Value'} . "\t" . $registry{'Component_'} . "\n";
428
429			my $oneline64 = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
430						. $registry{'Name'} . "\t" . $registry{'Val64'} . "\t" . $registry{'Component_'} . "\n";
431
432			if ( ! ( $style =~ /\bX64_ONLY\b/ )) { push(@registrytable, $oneline); }	# standard registry table for 32 Bit
433			if (( $style =~ /\bX64\b/ ) || ( $style =~ /\bX64_ONLY\b/ )) { push(@reg64table , $oneline64); }
434		}
435
436		# If there are added user registry keys for files collected in
437		# @installer::globals::userregistrycollector (file.pm), then
438		# this registry keys have to be added now. This is necessary for
439		# files in PREDEFINED_OSSHELLNEWDIR, because their component
440		# needs as KeyPath a RegistryItem in HKCU.
441
442		if ( $installer::globals::addeduserregitrykeys ) { add_userregs_to_registry_table(\@registrytable, $allvariableshashref); }
443
444		# Saving the file
445
446		my $registrytablename = $basedir . $installer::globals::separator . "Registry.idt" . "." . $onelanguage;
447		installer::files::save_file($registrytablename ,\@registrytable);
448		my $infoline = "Created idt file: $registrytablename\n";
449		push(@installer::globals::logfileinfo, $infoline);
450
451		$registrytablename = $basedir . $installer::globals::separator . "Reg64.idt" . "." . $onelanguage;
452		installer::files::save_file($registrytablename ,\@reg64table );
453		$infoline = "Created idt file: $registrytablename\n";
454		push(@installer::globals::logfileinfo, $infoline);
455	}
456}
457
4581;
459