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
32use strict;
33
34#####################################################
35# Generating the component name from a registryitem
36#####################################################
37
38sub get_registry_component_name
39{
40	my ($registryref, $allvariables) = @_;
41
42	# In this function exists the rule to create components from registryitems
43	# Rule:
44	# The componentname can be directly taken from the ModuleID.
45	# All registryitems belonging to one module can get the same component.
46
47	my $componentname = "";
48	my $isrootmodule = 0;
49
50	if ( $registryref->{'ModuleID'} ) { $componentname = $registryref->{'ModuleID'}; }
51
52	$componentname =~ s/\\/\_/g;
53	$componentname =~ s/\//\_/g;
54	$componentname =~ s/\-/\_/g;
55	$componentname =~ s/\_\s*$//g;
56
57	$componentname = lc($componentname);	# componentnames always lowercase
58
59	if ( $componentname eq "gid_module_root" ) { $isrootmodule = 1; }
60
61	# Attention: Maximum length for the componentname is 72
62
63	# identifying this component as registryitem component
64	$componentname = "registry_" . $componentname;
65
66	$componentname =~ s/gid_module_/g_m_/g;
67	$componentname =~ s/_optional_/_o_/g;
68	$componentname =~ s/_javafilter_/_jf_/g;
69
70	# This componentname must be more specific
71	my $addon = "_";
72	if ( $allvariables->{'PRODUCTNAME'} ) { $addon = $addon . $allvariables->{'PRODUCTNAME'}; }
73	if ( $allvariables->{'PRODUCTVERSION'} ) { $addon = $addon . $allvariables->{'PRODUCTVERSION'}; }
74	$addon = lc($addon);
75	$addon =~ s/ //g;
76	$addon =~ s/-//g;
77	$addon =~ s/\.//g;
78
79	my $styles = "";
80	if ( $registryref->{'Styles'} ) { $styles = $registryref->{'Styles'}; }
81
82	$componentname = $componentname . $addon;
83
84	if (( $styles =~ /\bLANGUAGEPACK\b/ ) && ( $installer::globals::languagepack )) { $componentname = $componentname . "_lang"; }
85	if ( $styles =~ /\bALWAYS_REQUIRED\b/ ) { $componentname = $componentname . "_forced"; }
86
87	# Attention: Maximum length for the componentname is 72
88	# %installer::globals::allregistrycomponents_in_this_database_ : resetted for each database
89	# %installer::globals::allregistrycomponents_ : not resetted for each database
90	# Component strings must be unique for the complete product, because they are used for
91	# the creation of the globally unique identifier.
92
93	my $fullname = $componentname;  # This can be longer than 72
94
95	if (( exists($installer::globals::allregistrycomponents_{$fullname}) ) && ( ! exists($installer::globals::allregistrycomponents_in_this_database_{$fullname}) ))
96	{
97		# This is not allowed: One component cannot be installed with different packages.
98		installer::exiter::exit_program("ERROR: Windows registry component \"$fullname\" is already included into another package. This is not allowed.", "get_registry_component_name");
99	}
100
101	if ( exists($installer::globals::allregistrycomponents_{$fullname}) )
102	{
103		$componentname = $installer::globals::allregistrycomponents_{$fullname};
104	}
105	else
106	{
107		if ( length($componentname) > 70 )
108		{
109			$componentname = generate_new_short_registrycomponentname($componentname); # This has to be unique for the complete product, not only one package
110		}
111
112		$installer::globals::allregistrycomponents_{$fullname} = $componentname;
113		$installer::globals::allregistrycomponents_in_this_database_{$fullname} = 1;
114	}
115
116	if ( $isrootmodule ) { $installer::globals::registryrootcomponent = $componentname; }
117
118	return $componentname;
119}
120
121#########################################################
122# Create a shorter version of a long component name,
123# because maximum length in msi database is 72.
124# Attention: In multi msi installation sets, the short
125# names have to be unique over all packages, because
126# this string is used to create the globally unique id
127# -> no resetting of
128# %installer::globals::allshortregistrycomponents
129# after a package was created.
130#########################################################
131
132sub generate_new_short_registrycomponentname
133{
134	my ($componentname) = @_;
135
136	my $startversion = substr($componentname, 0, 60); # taking only the first 60 characters
137	my $subid = installer::windows::msiglobal::calculate_id($componentname, 9); # taking only the first 9 digits
138	my $shortcomponentname = $startversion . "_" . $subid;
139
140	if ( exists($installer::globals::allshortregistrycomponents{$shortcomponentname}) ) { installer::exiter::exit_program("Failed to create unique component name: \"$shortcomponentname\"", "generate_new_short_registrycomponentname"); }
141
142	$installer::globals::allshortregistrycomponents{$shortcomponentname} = 1;
143
144	return $shortcomponentname;
145}
146
147##############################################################
148# Returning identifier for registry table.
149##############################################################
150
151sub get_registry_identifier
152{
153	my ($registry) = @_;
154
155	my $identifier = "";
156
157	if ( $registry->{'gid'} ) { $identifier = $registry->{'gid'}; }
158
159	$identifier = lc($identifier);	# always lower case
160
161	# Attention: Maximum length is 72
162
163	$identifier =~ s/gid_regitem_/g_r_/;
164	$identifier =~ s/_soffice_/_s_/;
165	$identifier =~ s/_clsid_/_c_/;
166	$identifier =~ s/_currentversion_/_cv_/;
167	$identifier =~ s/_microsoft_/_ms_/;
168	$identifier =~ s/_manufacturer_/_mf_/;
169	$identifier =~ s/_productname_/_pn_/;
170	$identifier =~ s/_productversion_/_pv_/;
171	$identifier =~ s/_staroffice_/_so_/;
172	$identifier =~ s/_software_/_sw_/;
173	$identifier =~ s/_capabilities_/_cap_/;
174	$identifier =~ s/_classpath_/_cp_/;
175	$identifier =~ s/_extension_/_ex_/;
176	$identifier =~ s/_fileassociations_/_fa_/;
177	$identifier =~ s/_propertysheethandlers_/_psh_/;
178	$identifier =~ s/__/_/g;
179
180	# Saving this in the registry collector
181
182	$registry->{'uniquename'} = $identifier;
183
184	return $identifier;
185}
186
187##################################################################
188# Returning root value for registry table.
189##################################################################
190
191sub get_registry_root
192{
193	my ($registry) = @_;
194
195	my $rootvalue = 0;	# Default: Parent is KKEY_CLASSES_ROOT
196	my $scproot = "";
197
198	if ( $registry->{'ParentID'} ) { $scproot = $registry->{'ParentID'}; }
199
200	if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE" ) { $rootvalue = -1; }
201
202	if ( $scproot eq "PREDEFINED_HKEY_CLASSES_ROOT" ) { $rootvalue = 0; }
203
204	if ( $scproot eq "PREDEFINED_HKEY_CURRENT_USER_ONLY" ) { $rootvalue = 1; }
205
206	if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE_ONLY" ) { $rootvalue = 2; }
207
208	return $rootvalue;
209}
210
211##############################################################
212# Returning key for registry table.
213##############################################################
214
215sub get_registry_key
216{
217	my ($registry, $allvariableshashref) = @_;
218
219	my $key = "";
220
221	if ( $registry->{'Subkey'} ) { $key = $registry->{'Subkey'}; }
222
223	if ( $key =~ /\%/ ) { $key = installer::worker::replace_variables_in_string($key, $allvariableshashref); }
224
225	return $key;
226}
227
228##############################################################
229# Returning name for registry table.
230##############################################################
231
232sub get_registry_name
233{
234	my ($registry, $allvariableshashref) = @_;
235
236	my $name = "";
237
238	if ( $registry->{'Name'} ) { $name = $registry->{'Name'}; }
239
240	if ( $name =~ /\%/ ) { $name = installer::worker::replace_variables_in_string($name, $allvariableshashref); }
241
242	return $name;
243}
244
245##############################################################
246# Returning value for registry table.
247##############################################################
248
249sub get_registry_value
250{
251	my ($registry, $allvariableshashref) = @_;
252
253	my $value = "";
254
255	if ( $registry->{'Value'} ) { $value = $registry->{'Value'}; }
256
257	$value =~ s/\\\"/\"/g;	# no more masquerading of '"'
258	$value =~ s/\\\\\s*$/\\/g;	# making "\\" at end of value to "\"
259	$value =~ s/\<progpath\>/\[INSTALLLOCATION\]/;
260	$value =~ s/\[INSTALLLOCATION\]\\/\[INSTALLLOCATION\]/;	# removing "\" after "[INSTALLLOCATION]"
261
262	if ( $value =~ /\%/ ) { $value = installer::worker::replace_variables_in_string($value, $allvariableshashref); }
263
264	return $value;
265}
266
267##############################################################
268# Returning 64 bit value for registry table.
269##############################################################
270
271sub get_registry_val64
272{
273	my ($registry, $allvariableshashref) = @_;
274
275	my $value = "";
276
277	if ( $registry->{'Val64'} ) { $value = $registry->{'Val64'}; }
278
279	$value =~ s/\\\"/\"/g;	# no more masquerading of '"'
280	$value =~ s/\\\\\s*$/\\/g;	# making "\\" at end of value to "\"
281	$value =~ s/\<progpath\>/\[INSTALLLOCATION\]/;
282	$value =~ s/\[INSTALLLOCATION\]\\/\[INSTALLLOCATION\]/;	# removing "\" after "[INSTALLLOCATION]"
283
284	if ( $value =~ /\%/ ) { $value = installer::worker::replace_variables_in_string($value, $allvariableshashref); }
285
286	return $value;
287}
288
289
290######################################################
291# Adding the content of
292# @installer::globals::userregistrycollector
293# to the registry table. The content was collected
294# in create_files_table() in file.pm.
295######################################################
296
297sub add_userregs_to_registry_table
298{
299	my ( $registrytable, $allvariables ) = @_;
300
301	for ( my $i = 0; $i <= $#installer::globals::userregistrycollector; $i++ )
302	{
303		my $onefile = $installer::globals::userregistrycollector[$i];
304
305		my $styles = "";
306		if ( $onefile->{'Styles'} ) { $styles = $onefile->{'Styles'}; }
307
308		my %registry = ();
309
310		$registry{'Registry'} = $onefile->{'userregkeypath'};
311		$registry{'Root'} = "1";  # always HKCU
312		$registry{'Key'} = "Software\\$allvariables->{'MANUFACTURER'}\\$allvariables->{'PRODUCTNAME'} $allvariables->{'PRODUCTVERSION'}\\";
313		if ( $onefile->{'needs_user_registry_key'} ) { $registry{'Key'} = $registry{'Key'} . "StartMenu"; }
314		else { $registry{'Key'} = $registry{'Key'} . "ShellNew"; }
315		$registry{'Name'} = $onefile->{'Name'};
316		$registry{'Value'} = "1";
317		$registry{'Component_'} = $onefile->{'componentname'};
318
319		my $oneline = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
320					. $registry{'Name'} . "\t" . $registry{'Value'} . "\t" . $registry{'Component_'} . "\n";
321
322		push(@{$registrytable}, $oneline);
323	}
324}
325
326######################################################
327# Creating the file Registry.idt dynamically
328# Content:
329# Registry Root Key Name Value Component_
330######################################################
331sub prepare_registry_table ($$$)
332{
333	my ($registryref, $languagesarrayref, $allvariableshashref) = @_;
334
335    my %table_data = ();
336	foreach my $onelanguage (@$languagesarrayref)
337	{
338        my $table_items = [];
339		foreach my $oneregistry (@$registryref)
340		{
341			# Controlling the language!
342			# Only language independent folderitems or folderitems with the correct language
343			# will be included into the table
344
345			next if $oneregistry->{'ismultilingual'}
346                && $oneregistry->{'specificlanguage'} ne $onelanguage;
347
348			my %registry = ();
349
350			$registry{'Registry'} = get_registry_identifier($oneregistry);
351			$registry{'Root'} = get_registry_root($oneregistry);
352			$registry{'Key'} = get_registry_key($oneregistry, $allvariableshashref);
353			$registry{'Name'} = get_registry_name($oneregistry, $allvariableshashref);
354			$registry{'Value'} = get_registry_value($oneregistry, $allvariableshashref);
355			$registry{'Val64'} = get_registry_val64($oneregistry, $allvariableshashref);
356            my $component_name = get_registry_component_name($oneregistry, $allvariableshashref);
357            $oneregistry->{'componentname'} = $component_name;
358			$registry{'Component_'} = $component_name;
359
360			# Collecting all components with DONT_DELETE style
361			my $style = $oneregistry->{'Styles'} // "";
362            $registry{'styles'} = $style;
363
364			if ( $style =~ /\bDONT_DELETE\b/ )
365            {
366                $installer::globals::dontdeletecomponents{$component_name} = 1;
367            }
368
369			# Saving upgradekey to write this into setup.ini for minor upgrades
370			if ( $style =~ /\bUPGRADEKEY\b/ )
371            {
372                $installer::globals::minorupgradekey = $registry{'Key'};
373            }
374
375			# Collecting all registry components with ALWAYS_REQUIRED style
376			if ( ! ( $style =~ /\bALWAYS_REQUIRED\b/ ))
377			{
378				# Setting a component condition for unforced registry components!
379				# Only write into registry, if WRITE_REGISTRY is set.
380				if ( $oneregistry->{'ComponentCondition'} ) { $oneregistry->{'ComponentCondition'} = "(" . $oneregistry->{'ComponentCondition'} . ") AND (WRITE_REGISTRY=1)"; }
381				else { $oneregistry->{'ComponentCondition'} = "WRITE_REGISTRY=1"; }
382			}
383
384			# Collecting all component conditions
385			if ( $oneregistry->{'ComponentCondition'} )
386			{
387				if ( ! exists($installer::globals::componentcondition{$registry{'Component_'}}))
388				{
389					$installer::globals::componentcondition{$registry{'Component_'}} = $oneregistry->{'ComponentCondition'};
390				}
391			}
392
393            push @$table_items, \%registry;
394		}
395        $table_data{$onelanguage} = $table_items;
396    }
397
398    return \%table_data;
399}
400
401
402
403
404sub collect_registry_components ($)
405{
406    my ($table_data) = @_;
407
408    my %components = ();
409    foreach my $language_data (values %$table_data)
410    {
411        foreach my $item (@$language_data)
412        {
413			$components{$item->{'Component_'}} = 1;
414        }
415    }
416    return keys %components;
417}
418
419
420
421
422sub translate_component_names ($$$)
423{
424    my ($translation_map, $registry_items, $table_data) = @_;
425
426    my $replacement_count = 0;
427    foreach my $item (@$registry_items)
428    {
429        my $translated_name = $translation_map->{$item->{'componentname'}};
430        if (defined $translated_name)
431        {
432            $item->{'componentname'} = $translated_name;
433            ++$replacement_count;
434        }
435    }
436    $installer::logger::Lang->printf("replaced %d component names in registry items\n", $replacement_count);
437
438    $replacement_count = 0;
439    foreach my $language_data (values %$table_data)
440    {
441        foreach my $item (@$language_data)
442        {
443            my $translated_name = $translation_map->{$item->{'Component_'}};
444            if (defined $translated_name)
445            {
446                $item->{'Component_'} = $translated_name;
447                ++$replacement_count;
448            }
449        }
450    }
451    $installer::logger::Lang->printf("replaced %d component names in registry table\n", $replacement_count);
452}
453
454
455
456
457sub create_registry_table_32 ($$$$)
458{
459	my ($basedir, $languagesarrayref, $allvariableshashref, $table_data) = @_;
460
461	foreach my $onelanguage (@$languagesarrayref)
462	{
463		my @registrytable = ();
464        installer::windows::idtglobal::write_idt_header(\@registrytable, "registry");
465
466		foreach my $item (@{$table_data->{$onelanguage}})
467		{
468			next if $item->{'styles'} =~ /\bX64_ONLY\b/;
469
470			my $oneline = join("\t",
471                $item->{'Registry'},
472                $item->{'Root'},
473                $item->{'Key'},
474                $item->{'Name'},
475                $item->{'Value'},
476                $item->{'Component_'})
477                . "\n";
478
479            push(@registrytable, $oneline);
480		}
481
482		# If there are added user registry keys for files collected in
483		# @installer::globals::userregistrycollector (file.pm), then
484		# this registry keys have to be added now. This is necessary for
485		# files in PREDEFINED_OSSHELLNEWDIR, because their component
486		# needs as KeyPath a RegistryItem in HKCU.
487
488		if ( $installer::globals::addeduserregitrykeys )
489        {
490            add_userregs_to_registry_table(\@registrytable, $allvariableshashref);
491        }
492
493		# Save the database file.
494		my $registrytablename = $basedir . $installer::globals::separator . "Registry.idt" . "." . $onelanguage;
495		installer::files::save_file($registrytablename ,\@registrytable);
496        $installer::logger::Lang->printf("Created idt file: %s\n", $registrytablename);
497	}
498}
499
500
501
502
503sub create_registry_table_64 ($$$$)
504{
505	my ($basedir, $languagesarrayref, $allvariableshashref, $table_data) = @_;
506
507	foreach my $onelanguage (@$languagesarrayref)
508	{
509        my @reg64table = ();
510		installer::windows::idtglobal::write_idt_header(\@reg64table, "reg64");
511		foreach my $item (@{$table_data->{$onelanguage}})
512        {
513			next unless $item->{'styles'} =~ /\b(X64|X64_ONLY)\b/;
514
515            my $oneline64 = join("\t",
516                $item->{'Registry'},
517                $item->{'Root'},
518                $item->{'Key'},
519                $item->{'Name'},
520                $item->{'Val64'},
521                $item->{'Component_'})
522                . "\n";
523
524            push(@reg64table , $oneline64);
525        }
526
527        # Save the database file.
528		my $registrytablename = $basedir . $installer::globals::separator . "Reg64.idt" . "." . $onelanguage;
529		installer::files::save_file($registrytablename ,\@reg64table );
530        $installer::logger::Lang->printf("Created idt file: %s\n", $registrytablename);
531	}
532}
533
5341;
535