* @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\install\helper\iohandler; use phpbb\path_helper; use phpbb\routing\router; /** * Input-Output handler for the AJAX frontend */ class ajax_iohandler extends iohandler_base { /** * @var path_helper */ protected $path_helper; /** * @var \phpbb\request\request_interface */ protected $request; /** * @var \phpbb\template\template */ protected $template; /** * @var router */ protected $router; /** * @var string */ protected $phpbb_root_path; /** * @var string */ protected $file_status; /** * @var string */ protected $form; /** * @var bool */ protected $request_client_refresh; /** * @var array */ protected $nav_data; /** * @var array */ protected $cookies; /** * @var array */ protected $download; /** * @var array */ protected $redirect_url; /** * @var resource */ protected $file_lock_pointer; /** * Constructor * * @param path_helper $path_helper * @param \phpbb\request\request_interface $request HTTP request interface * @param \phpbb\template\template $template Template engine * @param router $router Router * @param string $root_path Path to phpBB's root */ public function __construct(path_helper $path_helper, \phpbb\request\request_interface $request, \phpbb\template\template $template, router $router, $root_path) { $this->path_helper = $path_helper; $this->request = $request; $this->router = $router; $this->template = $template; $this->form = ''; $this->nav_data = array(); $this->cookies = array(); $this->download = array(); $this->redirect_url = array(); $this->file_status = ''; $this->phpbb_root_path = $root_path; parent::__construct(); } /** * {@inheritdoc} */ public function get_input($name, $default, $multibyte = false) { return $this->request->variable($name, $default, $multibyte); } /** * {@inheritdoc} */ public function get_raw_input($name, $default) { return $this->request->raw_variable($name, $default); } /** * {@inheritdoc} */ public function get_server_variable($name, $default = '') { return $this->request->server($name, $default); } /** * {@inheritdoc} */ public function get_header_variable($name, $default = '') { return $this->request->header($name, $default); } /** * {@inheritdoc} */ public function is_secure() { return $this->request->is_secure(); } /** * {@inheritdoc} */ public function add_user_form_group($title, $form) { $this->form = $this->generate_form_render_data($title, $form); } /** * {@inheritdoc} */ public function generate_form_render_data($title, $form) { $this->template->assign_block_vars('options', array( 'LEGEND' => $this->language->lang($title), 'S_LEGEND' => true, )); $not_button_form = false; foreach ($form as $input_name => $input_options) { if (!isset($input_options['type'])) { continue; } $tpl_ary = array(); $not_button_form = ($input_options['type'] !== 'submit' || $not_button_form); $tpl_ary['TYPE'] = $input_options['type']; $tpl_ary['TITLE'] = $this->language->lang($input_options['label']); $tpl_ary['KEY'] = $input_name; $tpl_ary['S_EXPLAIN'] = false; $tpl_ary['DISABLED'] = isset($input_options['disabled']) ? $input_options['disabled'] : false; $tpl_ary['IS_SECONDARY'] = isset($input_options['is_secondary']) ? $input_options['is_secondary'] : false; if (isset($input_options['default'])) { $default = $input_options['default']; $default = preg_replace_callback('#\{L_([A-Z0-9\-_]*)\}#s', array($this, 'lang_replace_callback'), $default); $tpl_ary['DEFAULT'] = $default; } if (isset($input_options['description'])) { $tpl_ary['TITLE_EXPLAIN'] = $this->language->lang($input_options['description']); $tpl_ary['S_EXPLAIN'] = true; } if (in_array($input_options['type'], array('select', 'radio'), true)) { for ($i = 0, $total = count($input_options['options']); $i < $total; $i++) { if (isset($input_options['options'][$i]['label'])) { $input_options['options'][$i]['label'] = $this->language->lang($input_options['options'][$i]['label']); } } $tpl_ary['OPTIONS'] = $input_options['options']; } $block_name = ($input_options['type'] === 'submit') ? 'submit_buttons' : 'options'; $this->template->assign_block_vars($block_name, $tpl_ary); } if (isset($form['database_update_submit']) && !$form['database_update_submit']['disabled']) { $this->template->assign_var('FORM_TITLE', $this->language->lang('UPDATE_CONTINUE_UPDATE_PROCESS')); } $this->template->assign_var('S_NOT_ONLY_BUTTON_FORM', $not_button_form); if (!$not_button_form) { $this->template->destroy_block_vars('options'); } $this->template->set_filenames(array( 'form_install' => 'installer_form.html', )); return $this->template->assign_display('form_install'); } /** * {@inheritdoc} */ public function send_response($no_more_output = false) { $json_data_array = $this->prepare_json_array($no_more_output); if (empty($json_data_array)) { return; } $json_data = json_encode($json_data_array); // Try to push content to the browser print(str_pad(' ', 4096) . "\n"); print($json_data . "\n\n"); flush(); } /** * Prepares iohandler's data to be sent out to the client. * * @param bool $no_more_output Whether or not there will be more output in this response * * @return array */ protected function prepare_json_array($no_more_output = false) { $json_array = array(); if (!empty($this->errors)) { $json_array['errors'] = $this->errors; $this->errors = array(); } if (!empty($this->warnings)) { $json_array['warnings'] = $this->warnings; $this->warnings = array(); } if (!empty($this->logs)) { $json_array['logs'] = $this->logs; $this->logs = array(); } if (!empty($this->success)) { $json_array['success'] = $this->success; $this->success = array(); } if (!empty($this->download)) { $json_array['download'] = $this->download; $this->download = array(); } if (!empty($this->form)) { $json_array['form'] = $this->form; $this->form = ''; } if (!empty($this->file_status)) { $json_array['file_status'] = $this->file_status; $this->file_status = ''; } // If current task name is set, we push progress message to the client side if (!empty($this->current_task_name)) { $json_array['progress'] = array( 'task_name' => $this->current_task_name, 'task_num' => $this->current_task_progress, 'task_count' => $this->task_progress_count, ); if ($this->restart_progress_bar) { $json_array['progress']['restart'] = 1; $this->restart_progress_bar = false; } } if (!empty($this->nav_data)) { $json_array['nav'] = $this->nav_data; $this->nav_data = array(); } if ($this->request_client_refresh) { $json_array['refresh'] = true; $this->request_client_refresh = false; } if (!empty($this->cookies)) { $json_array['cookies'] = $this->cookies; $this->cookies = array(); } if (!empty($this->redirect_url)) { $json_array['redirect'] = $this->redirect_url; $this->redirect_url = array(); } if ($no_more_output) { $json_array['over'] = true; } return $json_array; } /** * {@inheritdoc} */ public function set_progress($task_lang_key, $task_number) { parent::set_progress($task_lang_key, $task_number); $this->send_response(); } /** * {@inheritdoc} */ public function request_refresh() { $this->request_client_refresh = true; } /** * {@inheritdoc} */ public function set_active_stage_menu($menu_path) { $this->nav_data['active'] = $menu_path[count($menu_path) - 1]; $this->send_response(); } /** * {@inheritdoc} */ public function set_finished_stage_menu($menu_path) { $this->nav_data['finished'][] = $menu_path[count($menu_path) - 1]; $this->send_response(); } /** * {@inheritdoc} */ public function set_cookie($cookie_name, $cookie_value) { $this->cookies[] = array( 'name' => $cookie_name, 'value' => $cookie_value ); } /** * {@inheritdoc} */ public function add_download_link($route, $title, $msg = null) { $link_properties = array( 'href' => $this->router->generate($route), 'title' => $this->language->lang($title), 'download' => $this->language->lang('DOWNLOAD'), ); if ($msg !== null) { $link_properties['msg'] = htmlspecialchars_decode($this->language->lang($msg)); } $this->download[] = $link_properties; } /** * {@inheritdoc} */ public function render_update_file_status($status_array) { $this->template->assign_vars(array( 'T_IMAGE_PATH' => $this->path_helper->get_web_root_path() . 'adm/images/', )); foreach ($status_array as $block => $list) { foreach ($list as $filename) { $dirname = dirname($filename); $this->template->assign_block_vars($block, array( 'STATUS' => $block, 'FILENAME' => $filename, 'DIR_PART' => (!empty($dirname) && $dirname !== '.') ? dirname($filename) . '/' : false, 'FILE_PART' => basename($filename), )); } } $this->template->set_filenames(array( 'file_status' => 'installer_update_file_status.html', )); $this->file_status = $this->template->assign_display('file_status'); } /** * {@inheritdoc} */ public function redirect($url, $use_ajax = false) { $this->redirect_url = array('url' => $url, 'use_ajax' => $use_ajax); $this->send_response(true); } /** * Acquires a file lock */ public function acquire_lock() { $lock_file = $this->phpbb_root_path . 'store/io_lock.lock'; $this->file_lock_pointer = @fopen($lock_file, 'w+'); if ($this->file_lock_pointer) { flock($this->file_lock_pointer, LOCK_EX); } } /** * Release file lock */ public function release_lock() { if ($this->file_lock_pointer) { fwrite($this->file_lock_pointer, 'ok'); flock($this->file_lock_pointer, LOCK_UN); fclose($this->file_lock_pointer); } } /** * Callback function for language replacing * * @param array $matches * @return string */ public function lang_replace_callback($matches) { if (!empty($matches[1])) { return $this->language->lang($matches[1]); } return ''; } } 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
package fs; # $Id: fs.pm 259325 2009-08-17 12:51:59Z alefebvre $

use diagnostics;
use strict;

use common;
use log;
use devices;
use fs::type;
use fs::get;
use fs::format;
use fs::mount_options;
use fs::loopback;
use fs::mount;
use run_program;
use detect_devices;
use modules;
use fsedit;


sub read_fstab {
    my ($prefix, $file, @reading_options) = @_;

    if (member('keep_default', @reading_options)) {
	push @reading_options, 'freq_passno', 'keep_device_LABEL', 'keep_device_UUID';
    }

    my %comments;
    my $comment;
    my @l = grep {
	if (/^Filename\s*Type\s*Size/) {
	    0; #- when reading /proc/swaps
	} elsif (/^\s*#/) {
	    $comment .= chomp_($_) . "\n";
	    0;
	} else {
	    $comments{$_} = $comment if $comment;
	    $comment = '';
	    1;
	}
    } cat_("$prefix$file");

    #- attach comments at the end of fstab to the previous line
    $comments{$l[-1]} = $comment if $comment;

    map {
	my ($dev, $mntpoint, $fs_type, $options, $freq, $passno) = split;
	my $comment = $comments{$_};

	$options = 'defaults' if $options eq 'rw'; # clean-up for mtab read

	if ($fs_type eq 'supermount') {
	    log::l("dropping supermount");
	    $options = join(",", grep {
		if (/fs=(.*)/) {
		    $fs_type = $1;

		    #- with supermount, the type could be something like ext2:vfat
		    #- but this cannot be done without supermount, so switching to "auto"
		    $fs_type = 'auto' if $fs_type =~ /:/;

		    0;
		} elsif (/dev=(.*)/) {
		    $dev = $1;
		    0;
		} elsif ($_ eq '--') {
		    0;
		} else {
		    1;
		}
	    } split(',', $options));
	}
	s/\\040/ /g foreach $mntpoint, $dev, $options;

	if ($fs_type eq 'ext4') {
	    $options = join(",", grep {	!/extents/ } split(',', $options)) || 'defaults';
	}

	my $h = { 
		 mntpoint => $mntpoint, fs_type => $fs_type,
		 options => $options, comment => $comment,
		 if_(member('keep_freq_passno', @reading_options), freq => $freq, passno => $passno),
		};

	put_in_hash($h, fs::wild_device::to_subpart($dev));

	if ($h->{device_LABEL} && !$h->{device_alias} && member('keep_device_LABEL', @reading_options)) {
	    $h->{prefer_device_LABEL} = 1;
	} elsif ($h->{device_UUID} && !$h->{device_alias} && member('keep_device_UUID', @reading_options)) {
	    $h->{prefer_device_UUID} = 1;
	} else {
	    $h->{prefer_device} = 1;
	}

	if ($h->{options} =~ /credentials=/ && !member('verbatim_credentials', @reading_options)) {
	    require fs::remote::smb;
	    #- remove credentials=file with username=foo,password=bar,domain=zoo
	    #- the other way is done in fstab_to_string
	    my ($options, $unknown) = fs::mount_options::unpack($h);
	    my $file = delete $options->{'credentials='};
	    my $credentials = fs::remote::smb::read_credentials_raw($file);
	    if ($credentials->{username}) {
		$options->{"$_="} = $credentials->{$_} foreach qw(username password domain);
		fs::mount_options::pack($h, $options, $unknown);
	    }
	} elsif ($h->{fs_type} eq 'davfs2' && !member('verbatim_credentials', @reading_options)) {
	    require fs::remote::davfs;
	    if (my $credentials = fs::remote::davfs::read_credentials($h->{mntpoint})) {
		my ($options, $unknown) = fs::mount_options::unpack($h);
		$options->{"$_="} = $credentials->{$_} foreach qw(username password);
		fs::mount_options::pack($h, $options, $unknown);
	    }
	}

	$h;
    } @l;
}

sub merge_fstabs {
    my ($loose, $fstab, @l) = @_;

    foreach my $p (@$fstab) {
	my ($l1, $l2) = partition { fs::get::is_same_hd($_, $p) } @l;
	my ($p2) = @$l1 or next;
	@l = @$l2;

	$p->{mntpoint} = $p2->{mntpoint} if delete $p->{unsafeMntpoint};

	if (!$loose) {
	    $p->{fs_type} = $p2->{fs_type} if $p2->{fs_type};
	    $p->{options} = $p2->{options} if $p2->{options};
	    add2hash_($p, $p2);
	} else {
	    $p->{isMounted} ||= $p2->{isMounted};
	    $p->{real_mntpoint} ||= $p2->{real_mntpoint};
	}
	$p->{device_alias} ||= $p2->{device_alias} if $p->{device} ne $p2->{device} && $p2->{device} !~ m|/|;

	$p->{fs_type} && $p2->{fs_type} && $p->{fs_type} ne $p2->{fs_type}
	  && $p->{fs_type} ne 'auto' && $p2->{fs_type} ne 'auto' and
	    log::l("err, fstab and partition table do not agree for $p->{device} type: $p->{fs_type} vs $p2->{fs_type}");
    }
    @l;
}

sub add2all_hds {
    my ($all_hds, @l) = @_;

    @l = merge_fstabs('', [ fs::get::really_all_fstab($all_hds) ], @l);

    foreach (@l) {
	my $s = 
	    $_->{fs_type} eq 'nfs' ? 'nfss' :
	    $_->{fs_type} eq 'cifs' ? 'smbs' :
	    $_->{fs_type} eq 'davfs2' ? 'davs' :
	    isTrueLocalFS($_) || isSwap($_) || isOtherAvailableFS($_) ? '' :
	    'special';
	push @{$all_hds->{$s}}, $_ if $s;
    }
}

sub get_major_minor {
    my ($fstab) = @_;
    foreach (@$fstab) {
	eval {
	    my (undef, $major, $minor) = devices::entry($_->{device});
	    ($_->{major}, $_->{minor}) = ($major, $minor);
	} if !$_->{major};
    }
}

sub merge_info_from_mtab {
    my ($fstab) = @_;

    my @l1 = map { my $l = $_; 
		   my $h = fs::type::fs_type2subpart('swap');
		   $h->{$_} = $l->{$_} foreach qw(device major minor); 
		   $h;
	       } read_fstab('', '/proc/swaps');
    
    my @l2 = map { read_fstab('', $_) } '/etc/mtab', '/proc/mounts';

    foreach (@l1, @l2) {
	log::l("found mounted partition on $_->{device} with $_->{mntpoint}");
	if ($::isInstall && $_->{mntpoint} =~ m!^/tmp/\w*image$!) {
	    $_->{real_mntpoint} = delete $_->{mntpoint};
	}
	$_->{isMounted} = 1;
	set_isFormatted($_, 1);
    } 
    merge_fstabs('loose', $fstab, @l1, @l2);
}

# - when using "$loose", it does not merge in type&options from the fstab
sub merge_info_from_fstab {
    my ($fstab, $prefix, $uniq, $loose) = @_;

    my @l = grep { 
	if ($uniq) {
	    my $part = fs::get::mntpoint2part($_->{mntpoint}, $fstab);
	    !$part || fs::get::is_same_hd($part, $_); #- keep it only if it is the mount point AND the same device
	} else {
	    1;
	}
    } read_fstab($prefix, '/etc/fstab', 'keep_default');

    merge_fstabs($loose, $fstab, @l);
}

sub get_info_from_fstab {
    my ($all_hds) = @_;
    my @l = read_fstab($::prefix, '/etc/fstab', 'keep_default');
    add2all_hds($all_hds, @l);
}

sub prepare_write_fstab {
    my ($fstab, $o_prefix, $b_keep_credentials) = @_;
    $o_prefix ||= '';

    my %new;
    my (@smb_credentials, @davfs_credentials);
    my @l = map { 
	my $device = 
	  isLoopback($_) ? 
	      ($_->{mntpoint} eq '/' ? "/initrd/loopfs" : $_->{loopback_device}{mntpoint}) . $_->{loopback_file} :
	  fs::wild_device::from_part($o_prefix, $_);

	my $comment = $_->{comment};
	$comment = '' if $comment =~ m!^Entry for /dev/.* :!;
	$comment ||= "# Entry for /dev/$_->{device} :\n" if $device =~ /^(UUID|LABEL)=/;

	my $real_mntpoint = $_->{mntpoint} || ${{ '/tmp/hdimage' => '/mnt/hd' }}{$_->{real_mntpoint}};
	mkdir_p("$o_prefix$real_mntpoint") if $real_mntpoint =~ m|^/|;
	my $mntpoint = fs::type::carry_root_loopback($_) ? '/initrd/loopfs' : $real_mntpoint;

	my ($freq, $passno) =
	  exists $_->{freq} ?
	    ($_->{freq}, $_->{passno}) :
	  isTrueLocalFS($_) && !$_->{dmcrypt_name} && $_->{options} !~ /encryption=/ && (!$_->{is_removable} || member($_->{mntpoint}, fs::type::directories_needed_to_boot())) ? 
	    (1, $_->{mntpoint} eq '/' ? 1 : fs::type::carry_root_loopback($_) ? 0 : 2) : 
	    (0, 0);

	if (($device eq 'none' || !$new{$device}) && ($mntpoint eq 'swap' || !$new{$mntpoint})) {
	    #- keep in mind the new line for fstab.
	    $new{$device} = 1;
	    $new{$mntpoint} = 1;

	    my $options = $_->{options} || 'defaults';

	    if ($_->{fs_type} eq 'cifs' && $options =~ /password=/ && !$b_keep_credentials) {
		require fs::remote::smb;
		if (my ($opts, $smb_credentials) = fs::remote::smb::fstab_entry_to_credentials($_)) {
		    $options = $opts;
		    push @smb_credentials, $smb_credentials;
		}
	    } elsif ($_->{fs_type} eq 'davfs2' && !$b_keep_credentials) {
		require fs::remote::davfs;
		if (my ($opts, $davfs_credentials) = fs::remote::davfs::fstab_entry_to_credentials($_)) {
		    $options = $opts || 'defaults';
		    push @davfs_credentials, $davfs_credentials;
		}
	    }

	    my $fs_type = $_->{fs_type} || 'auto';

	    s/ /\\040/g foreach $mntpoint, $device, $options;

	    my $file_dep = $options =~ /\b(loop|bind)\b/ ? $device : '';

	    [ $file_dep, $mntpoint, $comment . join(' ', $device, $mntpoint, $fs_type, $options, $freq, $passno) . "\n" ];
	} else {
	    ();
	}
    } grep { $_->{device} && ($_->{mntpoint} || $_->{real_mntpoint}) && $_->{fs_type} && ($_->{isFormatted} || !$_->{notFormatted}) } @$fstab;

    sub sort_it {
	my (@l) = @_;

	if (my $file_based = find { $_->[0] } @l) {
	    my ($before, $other) = partition { $file_based->[0] =~ /^\Q$_->[1]/ } @l;
	    $file_based->[0] = ''; #- all dependencies are now in before
	    if (@$other && @$before) {
		sort_it(@$before), sort_it(@$other);
	    } else {
		sort_it(@l);
	    }
	} else {
	    sort { $a->[1] cmp $b->[1] } @l;
	}	
    }
    @l = sort_it(@l);

    join('', map { $_->[2] } @l), \@smb_credentials, \@davfs_credentials;
}

sub fstab_to_string {
    my ($all_hds, $o_prefix) = @_;
    my $fstab = [ fs::get::really_all_fstab($all_hds), @{$all_hds->{special}} ];
    my ($s, undef) = prepare_write_fstab($fstab, $o_prefix, 'keep_credentials');
    $s;
}

sub write_fstab {
    my ($all_hds, $o_prefix) = @_;
    log::l("writing $o_prefix/etc/fstab");
    my $fstab = [ fs::get::really_all_fstab($all_hds), @{$all_hds->{special}} ];
    my ($s, $smb_credentials, $davfs_credentials) = prepare_write_fstab($fstab, $o_prefix, '');
    renamef("$o_prefix/etc/fstab", "$o_prefix/etc/fstab.old");
    output("$o_prefix/etc/fstab", $s);
    require fs::remote::davfs;
    fs::remote::smb::save_credentials($_) foreach @$smb_credentials;
    fs::remote::davfs::save_credentials($davfs_credentials);
    fs::dmcrypt::save_crypttab($all_hds) if @{$all_hds->{dmcrypts}};
}

sub set_removable_mntpoints {
    my ($all_hds) = @_;

    my %names;
    foreach (@{$all_hds->{raw_hds}}) {
	my $name = detect_devices::suggest_mount_point($_) or next;
	$name eq 'zip' || $name eq 'cdrom' and next;
	
	my $s = ++$names{$name};
	$_->{mntpoint} ||= "/media/$name" . ($s == 1 ? '' : $s);
    }
}

sub get_raw_hds {
    my ($prefix, $all_hds) = @_;

    push @{$all_hds->{raw_hds}}, detect_devices::removables();
    $_->{is_removable} = 1 foreach @{$all_hds->{raw_hds}};

    get_major_minor($all_hds->{raw_hds});

    my @fstab = read_fstab($prefix, '/etc/fstab', 'keep_default');
    $all_hds->{nfss} = [ grep { $_->{fs_type} eq 'nfs' } @fstab ];
    $all_hds->{smbs} = [ grep { $_->{fs_type} eq 'cifs' } @fstab ];
    $all_hds->{davs} = [ grep { $_->{fs_type} eq 'davfs2' } @fstab ];
    $all_hds->{special} = [
       (grep { $_->{fs_type} eq 'tmpfs' } @fstab),
       { device => 'none', mntpoint => '/proc', fs_type => 'proc' },
    ];
}