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