1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-08-18 22:41:28 +02:00

[ticket/14039] Revamp updater

PHPBB3-14039
This commit is contained in:
Mate Bartus
2015-07-24 09:20:50 +02:00
parent f1047ac854
commit 8f5a0ad6f7
94 changed files with 4514 additions and 263 deletions

View File

@@ -0,0 +1,131 @@
<?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\install\helper\file_updater;
use phpbb\install\helper\update_helper;
/**
* File updater for generating archive with updated files
*/
class compression_file_updater implements file_updater_interface
{
/**
* @var \compress
*/
protected $compress;
/**
* @var update_helper
*/
protected $update_helper;
/**
* @var string
*/
protected $phpbb_root_path;
/**
* @var string
*/
protected $php_ext;
/**
* Constructor
*
* @param update_helper $update_helper
* @param string $phpbb_root_path
* @param string $php_ext
*/
public function __construct(update_helper $update_helper, $phpbb_root_path, $php_ext)
{
$this->compress = null;
$this->update_helper = $update_helper;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
}
/**
* Set the compression method
*
* @param string $method Compression method's file extension
*
* @return string Archive's filename
*/
public function init($method)
{
$this->update_helper->include_file('includes/functions_compress.' . $this->php_ext);
$archive_filename = 'update_archive_' . time() . '_' . uniqid();
$path = $this->phpbb_root_path . 'store/' . $archive_filename . '' . $method;
if ($method === '.zip')
{
$this->compress = new \compress_zip('w', $path);
}
else
{
$this->compress = new \compress_tar('w', $path, $method);
}
return $path;
}
/**
* Close archive writing process
*/
public function close()
{
$this->compress->close();
}
/**
* {@inheritdoc}
*/
public function delete_file($path_to_file)
{
// We do absolutely nothing here
}
/**
* {@inheritdoc}
*/
public function create_new_file($path_to_file_to_create, $source, $create_from_content = false)
{
if ($create_from_content)
{
$this->compress->add_data($source, $path_to_file_to_create);
}
else
{
$this->compress->add_custom_file($source, $path_to_file_to_create);
}
}
/**
* {@inheritdoc}
*/
public function update_file($path_to_file_to_update, $source, $create_from_content = false)
{
// Both functions are identical here
$this->create_new_file($path_to_file_to_update, $source, $create_from_content);
}
/**
* {@inheritdoc}
*/
public function get_method_name()
{
return 'compression';
}
}

View File

@@ -0,0 +1,69 @@
<?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\install\helper\file_updater;
use phpbb\di\service_collection;
use phpbb\install\exception\file_updater_failure_exception;
/**
* File updater factory
*/
class factory
{
/**
* @var array
*/
protected $file_updaters;
/**
* Constructor
*
* @param service_collection $collection File updater service collection
*/
public function __construct(service_collection $collection)
{
foreach ($collection as $service)
{
$this->register($service);
}
}
/**
* Register updater object
*
* @param file_updater_interface $updater Updater object
*/
public function register(file_updater_interface $updater)
{
$name = $updater->get_method_name();
$this->file_updaters[$name] = $updater;
}
/**
* Returns file updater object
*
* @param string $name Name of the updater method
*
* @throws file_updater_failure_exception When the specified file updater does not exist
*/
public function get($name)
{
if (!isset($this->file_updaters[$name]))
{
throw new file_updater_failure_exception();
}
return $this->file_updaters[$name];
}
}

View File

@@ -0,0 +1,210 @@
<?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\install\helper\file_updater;
use phpbb\filesystem\exception\filesystem_exception;
use phpbb\filesystem\filesystem;
use phpbb\install\exception\file_updater_failure_exception;
/**
* File updater for direct filesystem access
*/
class file_updater implements file_updater_interface
{
/**
* @var filesystem
*/
protected $filesystem;
/**
* @var string
*/
protected $phpbb_root_path;
/**
* Constructor
*
* @param filesystem $filesystem
* @param string $phpbb_root_path
*/
public function __construct(filesystem $filesystem, $phpbb_root_path)
{
$this->filesystem = $filesystem;
$this->phpbb_root_path = $phpbb_root_path;
}
/**
* {@inheritdoc}
*
* @throws file_updater_failure_exception When the file is not writable
* @throws filesystem_exception When the filesystem class fails
*/
public function delete_file($path_to_file)
{
$this->filesystem->remove($this->phpbb_root_path . $path_to_file);
}
/**
* {@inheritdoc}
*
* @throws file_updater_failure_exception When the file is not writable
* @throws filesystem_exception When the filesystem class fails
*/
public function create_new_file($path_to_file_to_create, $source, $create_from_content = false)
{
$path_to_file_to_create = $this->phpbb_root_path . $path_to_file_to_create;
$dir = dirname($path_to_file_to_create);
if (!$this->filesystem->exists($dir))
{
$this->make_dir($dir);
}
$original_dir_perms = false;
if (!$this->filesystem->is_writable($dir))
{
// Extract last 9 bits we actually need
$original_dir_perms = @fileperms($dir) & 511;
$this->filesystem->phpbb_chmod($dir, filesystem::CHMOD_ALL);
}
if (!$create_from_content)
{
try
{
$this->filesystem->copy($source, $path_to_file_to_create);
}
catch (filesystem_exception $e)
{
$this->write_file($path_to_file_to_create, $source, $create_from_content);
}
}
else
{
$this->write_file($path_to_file_to_create, $source, $create_from_content);
}
if ($original_dir_perms !== false)
{
$this->filesystem->phpbb_chmod($dir, $original_dir_perms);
}
}
/**
* {@inheritdoc}
*
* @throws file_updater_failure_exception When the file is not writable
* @throws filesystem_exception When the filesystem class fails
*/
public function update_file($path_to_file_to_update, $source, $create_from_content = false)
{
$path_to_file_to_update = $this->phpbb_root_path . $path_to_file_to_update;
$original_file_perms = false;
if (!$this->filesystem->is_writable($path_to_file_to_update))
{
// Extract last 9 bits we actually need
$original_file_perms = @fileperms($path_to_file_to_update) & 511;
$this->filesystem->phpbb_chmod($path_to_file_to_update, filesystem::CHMOD_WRITE);
}
if (!$create_from_content)
{
try
{
$this->filesystem->copy($source, $path_to_file_to_update, true);
}
catch (filesystem_exception $e)
{
$this->write_file($path_to_file_to_update, $source, $create_from_content);
}
}
else
{
$this->write_file($path_to_file_to_update, $source, $create_from_content);
}
if ($original_file_perms !== false)
{
$this->filesystem->phpbb_chmod($path_to_file_to_update, $original_file_perms);
}
}
/**
* Creates directory structure
*
* @param string $path Path to the directory where the file should be placed (and non-existent)
*/
private function make_dir($path)
{
if (is_dir($path))
{
return;
}
$path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
$dirs = explode('/', $path);
$dirs_to_create = array();
do
{
$path .= '../';
$dirs_to_create[] = array_pop($dirs);
}
while (!is_dir($path));
foreach ($dirs_to_create as $directory)
{
$path .= $directory;
$this->filesystem->mkdir($path, 493); // 493 === 0755
$path .= '/';
}
}
/**
* Fallback function for file writing
*
* @param string $path_to_file Path to the file's location
* @param string $source Path to file to copy or string with the new file's content
* @param bool|false $create_from_content Whether or not to use $source as the content, false by default
*
* @throws file_updater_failure_exception When the file is not writable
*/
private function write_file($path_to_file, $source, $create_from_content = false)
{
if (!$create_from_content)
{
$source = @file_get_contents($source);
}
$file_pointer = @fopen($path_to_file, 'w');
if (!is_resource($file_pointer))
{
throw new file_updater_failure_exception();
}
@fwrite($file_pointer, $source);
@fclose($file_pointer);
}
/**
* {@inheritdoc}
*/
public function get_method_name()
{
return 'direct_file';
}
}

View File

@@ -0,0 +1,49 @@
<?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\install\helper\file_updater;
interface file_updater_interface
{
/**
* Deletes a file
*
* @param string $path_to_file Path to the file to delete
*/
public function delete_file($path_to_file);
/**
* Creates a new file
*
* @param string $path_to_file_to_create Path to the new file's location
* @param string $source Path to file to copy or string with the new file's content
* @param bool $create_from_content Whether or not to use $source as the content, false by default
*/
public function create_new_file($path_to_file_to_create, $source, $create_from_content = false);
/**
* Update file
*
* @param string $path_to_file_to_update Path to the file's location
* @param string $source Path to file to copy or string with the new file's content
* @param bool $create_from_content Whether or not to use $source as the content, false by default
*/
public function update_file($path_to_file_to_update, $source, $create_from_content = false);
/**
* Returns the name of the file updater method
*
* @return string
*/
public function get_method_name();
}

View File

@@ -0,0 +1,136 @@
<?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\install\helper\file_updater;
use phpbb\install\helper\update_helper;
/**
* File updater for FTP updates
*/
class ftp_file_updater implements file_updater_interface
{
/**
* @var \transfer
*/
protected $transfer;
/**
* @var update_helper
*/
protected $update_helper;
/**
* @var string
*/
protected $phpbb_root_path;
/**
* @var string
*/
protected $php_ext;
/**
* Constructor
*
* @param update_helper $update_helper
* @param string $phpbb_root_path
* @param string $php_ext
*/
public function __constructor(update_helper $update_helper, $phpbb_root_path, $php_ext)
{
$this->transfer = null;
$this->update_helper = $update_helper;
$this->phpbb_root_path = $phpbb_root_path;
$this->php_ext = $php_ext;
}
/**
* Initialize FTP connection
*
* @param string $method
* @param string $host
* @param string $user
* @param string $pass
* @param string $path
* @param int $port
* @param int $timeout
*/
public function init($method, $host, $user, $pass, $path, $port, $timeout)
{
$this->update_helper->include_file('includes/functions_transfer.' . $this->php_ext);
$this->transfer = new $method($host, $user, $pass, $path, $port, $timeout);
$this->transfer->open_session();
}
/**
* Close FTP session
*/
public function close()
{
$this->transfer->close_session();
}
/**
* {@inheritdoc}
*/
public function delete_file($path_to_file)
{
$this->transfer->delete_file($path_to_file);
}
/**
* {@inheritdoc}
*/
public function create_new_file($path_to_file_to_create, $source, $create_from_content = false)
{
$dirname = dirname($path_to_file_to_create);
if ($dirname && !file_exists($this->phpbb_root_path . $dirname))
{
$this->transfer->make_dir($dirname);
}
if ($create_from_content)
{
$this->transfer->write_file($path_to_file_to_create, $source);
}
else
{
$this->transfer->copy_file($path_to_file_to_create, $source);
}
}
/**
* {@inheritdoc}
*/
public function update_file($path_to_file_to_update, $source, $create_from_content = false)
{
if ($create_from_content)
{
$this->transfer->write_file($path_to_file_to_update, $source);
}
else
{
$this->transfer->copy_file($path_to_file_to_update, $source);
}
}
/**
* {@inheritdoc}
*/
public function get_method_name()
{
return 'ftp';
}
}