aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/language/language_file_loader.php
blob: 359202fd63285e4c40f85fbdf5c86880783a0610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\language;

use \phpbb\language\exception\language_file_not_found;

/**
 * Language file loader
 */
class language_file_loader
{
	/**
	 * @var string	Path to phpBB's root
	 */
	protected $phpbb_root_path;

	/**
	 * @var string	Extension of PHP files
	 */
	protected $php_ext;

	/**
	 * @var \phpbb\extension\manager	Extension manager
	 */
	protected $extension_manager;

	/**
	 * Constructor
	 *
	 * @param string	$phpbb_root_path	Path to phpBB's root
	 * @param string	$php_ext			Extension of PHP files
	 */
	public function __construct($phpbb_root_path, $php_ext)
	{
		$this->phpbb_root_path	= $phpbb_root_path;
		$this->php_ext			= $php_ext;

		$this->extension_manager = null;
	}

	/**
	 * Extension manager setter
	 *
	 * @param \phpbb\extension\manager	$extension_manager	Extension manager
	 */
	public function set_extension_manager(\phpbb\extension\manager $extension_manager)
	{
		$this->extension_manager = $extension_manager;
	}

	/**
	 * Loads language array for the given component
	 *
	 * @param string		$component	Name of the language component
	 * @param string|array	$locale		ISO code of the language to load, or array of ISO codes if you want to
	 * 									specify additional language fallback steps
	 * @param array			$lang		Array reference containing language strings
	 */
	public function load($component, $locale, &$lang)
	{
		$locale = (array) $locale;

		// Determine path to language directory
		$path = $this->phpbb_root_path . 'language/';

		$this->load_file($path, $component, $locale, $lang);
	}

	/**
	 * Loads language array for the given extension component
	 *
	 * @param string		$extension	Name of the extension
	 * @param string		$component	Name of the language component
	 * @param string|array	$locale		ISO code of the language to load, or array of ISO codes if you want to
	 * 									specify additional language fallback steps
	 * @param array			$lang		Array reference containing language strings
	 */
	public function load_extension($extension, $component, $locale, &$lang)
	{
		// Check if extension manager was loaded
		if ($this->extension_manager === null)
		{
			// If not, let's return
			return;
		}

		$locale = (array) $locale;

		// Determine path to language directory
		$path = $this->extension_manager->get_extension_path($extension, true) . 'language/';

		$this->load_file($path, $component, $locale, $lang);
	}

	/**
	 * Prepares language file loading
	 *
	 * @param string	$path		Path to search for file in
	 * @param string	$component	Name of the language component
	 * @param array		$locale		Array containing language fallback options
	 * @param array		$lang		Array reference of language strings
	 */
	protected function load_file($path, $component, $locale, &$lang)
	{
		// This is BC stuff and not the best idea as it makes language fallback
		// implementation quite hard like below.
		if (strpos($this->phpbb_root_path . $component, $path) === 0)
		{
			// Filter out the path
			$path_diff = str_replace($path, '', dirname($this->phpbb_root_path . $component));
			$language_file = basename($component, '.' . $this->php_ext);
			$component = '';

			// This step is needed to resolve language/en/subdir style $component
			// $path already points to the language base directory so we need to eliminate
			// the first directory from the path (that should be the language directory)
			$path_diff_parts = explode('/', $path_diff);

			if (sizeof($path_diff_parts) > 1)
			{
				array_shift($path_diff_parts);
				$component = implode('/', $path_diff_parts) . '/';
			}

			$component .= $language_file;
		}

		// Determine filename
		$filename = $component . '.' . $this->php_ext;

		// Determine path to file
		$file_path = $this->get_language_file_path($path, $filename, $locale);

		// Load language array
		$this->load_language_file($file_path, $lang);
	}

	/**
	 * This function implements language fallback logic
	 *
	 * @param string	$path		Path to language directory
	 * @param string	$filename	Filename to load language strings from
	 *
	 * @return string	Relative path to language file
	 *
	 * @throws language_file_not_found	When the path to the file cannot be resolved
	 */
	protected function get_language_file_path($path, $filename, $locales)
	{
		$language_file_path = $filename;

		// Language fallback logic
		foreach ($locales as $locale)
		{
			$language_file_path = $path . $locale . '/' . $filename;

			// If we are in install, try to use the updated version, when available
			if (defined('IN_INSTALL'))
			{
				$install_language_path = str_replace('language/', 'install/update/new/language/', $language_file_path);
				if (file_exists($install_language_path))
				{
					return $install_language_path;
				}
			}

			if (file_exists($language_file_path))
			{
				return $language_file_path;
			}
		}

		// The language file is not exist
		throw new language_file_not_found('Language file ' . $language_file_path . ' couldn\'t be opened.');
	}

	/**
	 * Loads language file
	 *
	 * @param string	$path	Path to language file to load
	 * @param array		$lang	Reference of the array of language strings
	 */
	protected function load_language_file($path, &$lang)
	{
		// Do not suppress error if in DEBUG mode
		if (defined('DEBUG'))
		{
			include $path;
		}
		else
		{
			@include $path;
		}
	}
}
sound => N_("Launch the sound system on your machine"), syslog => N_("Syslog is the facility by which many daemons use to log messages to various system log files. It is a good idea to always run syslog."), usb => N_("Load the drivers for your usb devices."), xfs => N_("Starts the X Font Server (this is mandatory for XFree to run)."), ); my ($name) = @_; my $s = $services{$name}; if ($s) { $s = translate($s); } else { $s = -e "$::prefix/etc/rc.d/init.d/$name" && cat_("$::prefix/etc/rc.d/init.d/$name"); $s ||= -e "$::prefix/etc/init.d/$name" && cat_("$::prefix/etc/init.d/$name"); $s ||= -e "$::prefix/etc/xinetd.d/$name" && cat_("$::prefix/etc/xinetd.d/$name"); $s =~ s/\\\s*\n#\s*//mg; if ($s =~ /^# description:\s+\S/sm) { ($s) = $s =~ /^# description:\s+(.*?)^(?:[^#]|# {0,2}\S)/sm; } else { ($s) = $s =~ /^#\s*(.*?)^[^#]/sm; } $s =~ s/#\s*//mg; } $s =~ s/\n/ /gm; $s =~ s/\s+$//; $s; } sub ask_install_simple { my ($in) = @_; my ($l, $on_services) = services(); $in->ask_many_from_list("drakxservices", N("Choose which services should be automatically started at boot time"), { list => $l, help => sub { description($_[0]) }, values => $on_services, sort => 1, }); } sub ask_install { my ($in) = @_; my %root_services = ( N("Printing") => [ qw(cups cupslpd lpr lpd oki4daemon hpoj cups-lpd) ], N("Internet") => [ qw(httpd boa tux roxen ftp pftp tftp proftpd wu-ftpd pure-ftpdipsec proftpd-xinetd ipchains iptables ipvsadm isdn4linux ibod jabber jabber-icq adsl squid portsentry prelude nessusd junkbuster radvd cddbp ippl iptoip jail.init) ], N("File sharing") => [ qw(nfs nfslock smb nettalk netfs mcserv autofs amd venus.init auth2.init codasrv.init update.init swat) ], N("System") => [ qw(usb usbd pcmcia irda xinetd inetd kudzu harddrake apmd sound network xfs alsa functions halt kheader killall mandrake_everytime mandrake_firstime random rawdevices single keytable syslog crond medusa-init portmap acon anacron atd gpm psacct wine acpid numlock jserver sensors mosix bpowerd bpowerfail fcron powertweak.init ups syslog-ng cvs apcupsd) ], N("Remote Administration") => [ qw(sshd telnetd telnet rsh rlogin rexec webmin cfd heartbeat ldirectord iplog mon vncserver netsaint olympusd drakxtools_http) ], # N("Network Client") => [ qw(ypbind nscd arpwatch fetchmail dnrd_rc diald rsync) ], # N("Network Server") => [ qw(named bootparamd ntpd xntpd chronyd postfix sendmail # imap imaps ipop2 ipop3 pop3s routed yppasswdd ypserv ldap dhcpd dhcrelay # hylafax innd identd rstatd rusersd rwalld rwhod gated # kadmin kprop krb524 krb5kdc krb5server hldsld bayonne sockd dhsd gnu-pop3d # gdips pptpd.conf vrrpd crossfire bnetd pvmd ircd sympa finger ntalk talk) ], N("Database Server") => [ qw(mysql postgresql) ], ); my %services_root; foreach my $root (keys %root_services) { $services_root{$_} = $root foreach @{$root_services{$root}}; } my ($l, $on_services) = services(); my %services; $services{$_} = 0 foreach @{$l || []}; $services{$_} = 1 foreach @{$on_services || []}; $in->ask_browse_tree_info('drakxservices', N("Choose which services should be automatically started at boot time"), { node_state => sub { $services{$_[0]} ? 'selected' : 'unselected' }, build_tree => sub { my ($add_node, $flat) = @_; $add_node->($_, !$flat && ($services_root{$_} || N("Other"))) foreach sort keys %services; }, grep_unselected => sub { grep { !$services{$_} } @_ }, toggle_nodes => sub { my ($set_state, @nodes) = @_; my $new_state = !$services{$nodes[0]}; foreach (@nodes) { $set_state->($_, $new_state ? 'selected' : 'unselected'); $services{$_} = $new_state; } }, get_status => sub { N("Services: %d activated for %d registered", scalar(grep { $_ } values %services), scalar(values %services)); }, get_info => sub { formatLines(description($_[0])) }, interactive_help_id => 'configureServices', }) or return $l, $on_services; #- no change on cancel. [ grep { $services{$_} } @$l ]; } sub ask_standalone_gtk { my ($_in) = @_; my ($l, $on_services) = services(); my @xinetd_services = map { $_->[0] } @{(services_raw())[1]}; require ugtk2; ugtk2->import(qw(:wrappers :create)); my $W = ugtk2->new(N("Services")); my ($x, $y, $w_popup); my $nopop = sub { $w_popup and $w_popup->destroy; undef $w_popup }; my $display = sub { my ($text) = @_; $nopop->(); gtkshow(gtkadd($w_popup = Gtk2::Window->new('popup'), gtksignal_connect(gtkadd(Gtk2::EventBox->new, gtkadd(gtkset_shadow_type(Gtk2::Frame->new, 'etched_out'), gtkset_justify(Gtk2::Label->new($text), 'left'))), button_press_event => sub { $nopop->() } )))->move($x, $y) if $text; }; my $update_service = sub { my ($service, $label) = @_; my $started = -e "/var/lock/subsys/$service"; $label->set_label($started ? N("running") : N("stopped")); }; my $b = Gtk2::EventBox->new; $b->set_events('pointer_motion_mask'); gtkadd($W->{window}, gtkadd($b, gtkpack_($W->create_box_with_title(N("Services and deamons")), 1, gtkset_size_request(create_scrolled_window(create_packtable({ col_spacings => 10, row_spacings => 3 }, map { my $service = $_; my $is_xinetd_service = member($service, @xinetd_services); my $infos = warp_text(description($_), 40); $infos ||= N("No additional information\nabout this service, sorry."); my $label = gtkset_justify(Gtk2::Label->new, 'left'); $update_service->($service, $label) if !$is_xinetd_service; [ gtkpack__(Gtk2::HBox->new(0,0), $_), gtkpack__(Gtk2::HBox->new(0,0), $label), gtkpack__(Gtk2::HBox->new(0,0), gtksignal_connect(Gtk2::Button->new(N("Info")), clicked => sub { $display->($infos) })), gtkpack__(Gtk2::HBox->new(0,0), gtkset_active(gtksignal_connect( Gtk2::CheckButton->new($is_xinetd_service ? N("Start when requested") : N("On boot")), clicked => sub { if ($_[0]->get_active) { push @$on_services, $service if !member($service, @$on_services); } else { @$on_services = grep { $_ ne $service } @$on_services; } }), member($service, @$on_services))), map { my $a = $_; gtkpack__(Gtk2::HBox->new(0,0), gtksignal_connect(Gtk2::Button->new(translate($a)), clicked => sub { my $action = $a eq "Start" ? 'restart' : 'stop'; local $_ = `service $service $action 2>&1`; s/\033\[[^mG]*[mG]//g; $update_service->($service, $label); $display->($_); })) if !$is_xinetd_service; } (N_("Start"), N_("Stop")) ] } @$l), [ $::isEmbedded ? 'automatic' : 'never', 'automatic' ]), -1, $::isEmbedded ? -1 : 400), 0, gtkpack(gtkset_border_width(Gtk2::HBox->new(0,0),5), $W->create_okcancel) )) ); $b->signal_connect(motion_notify_event => sub { my ($w, $e) = @_; my ($ox, $oy) = $w->window->get_origin; $x = $e->x+$ox; $y = $e->y+$oy }); $b->signal_connect(button_press_event => sub { $nopop->() }); $::isEmbedded and gtkflush(); $W->main or return; $on_services; } sub ask { my ($in) = @_; !$::isInstall && $in->isa('interactive::gtk') ? &ask_standalone_gtk : &ask_install; } sub doit { my ($in, $on_services) = @_; my ($l, $was_on_services) = services(); foreach (@$l) { my $before = member($_, @$was_on_services); my $after = member($_, @$on_services); if ($before != $after) { my $script = "/etc/rc.d/init.d/$_"; run_program::rooted($::prefix, "chkconfig", $after ? "--add" : "--del", $_); if ($after && cat_("$::prefix$script") =~ /^#\s+chkconfig:\s+-/m) { run_program::rooted($::prefix, "chkconfig", "--level", "35", $_, "on"); } if (!$after && !$::isInstall && !$in->isa('interactive::gtk')) { #- only done after install AND when not using the gtk frontend (since it allows one to start/stop services) #- this allows to skip stopping service "dm" run_program::rooted($::prefix, $script, "stop"); } } } } sub services_raw() { local $ENV{LANGUAGE} = 'C'; my (@services, @xinetd_services); foreach (run_program::rooted_get_stdout($::prefix, '/sbin/chkconfig', '--list')) { if (my ($xinetd_name, $on_off) = m!^\t(\S+):\s*(on|off)!) { push @xinetd_services, [ $xinetd_name, $on_off eq 'on' ]; } elsif (my ($name, $l) = m!^(\S+)\s+(0:(on|off).*)!) { push @services, [ $name, [ $l =~ /(\d+):on/g ] ]; } } \@services, \@xinetd_services; } #- returns: #--- the listref of installed services #--- the listref of "on" services sub services() { my ($services, $xinetd_services) = services_raw(); my @l = @$xinetd_services; if ($::isInstall) { push @l, map { [ $_->[0], @{$_->[1]} > 1 ] } @$services; } else { my $runlevel = (split " ", `/sbin/runlevel`)[1]; push @l, map { [ $_->[0], member($runlevel, @{$_->[1]}) ] } @$services; } @l = sort { $a->[0] cmp $b->[0] } @l; [ map { $_->[0] } @l ], [ map { $_->[0] } grep { $_->[1] } @l ];