<commit_before><?php
/**
 *
 * @package       phpBB Extension - S3
 * @copyright (c) 2016 <NAME>
 * @license       http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
 *
 */

namespace AustinMaddox\s3\event;

use Aws\Exception\MultipartUploadException;
use Aws\S3\S3Client;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event listener
 */
class main_listener implements EventSubscriberInterface
{
    /** @var \phpbb\config\config */
    protected $config;

    /** @var \phpbb\template\template */
    protected $template;

    /** @var \phpbb\user */
    protected $user;

    protected $bucket;
    protected $log;
    protected $region;
    protected $s3_client;

    /**
     * Constructor
     *
     * @param \phpbb\config\config     $config   Config object
     * @param \phpbb\template\template $template Template object
     * @param \phpbb\user              $user     User object
     *
     * @return \AustinMaddox\s3\event\main_listener
     * @access public
     */
    public function __construct(\phpbb\config\config $config, \phpbb\template\template $template, \phpbb\user $user)
    {
        $this->config = $config;
        $this->template = $template;
        $this->user = $user;

        // TODO: Remove logging after dev/debug.
        $this->log = new Logger('phpbb');
        $this->log->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::DEBUG));

        $this->region = 'us-west-2';
        $this->bucket = 'warriormachines-phpbb-files';

        // Instantiate an AWS S3 client.
        $this->s3_client = new S3Client([
            'credentials' => [
                'key'    => $this->config['s3_aws_access_key_id'],
                'secret' => $this->config['s3_aws_secret_access_key'],
            ],
            'debug'       => false,
            'region'      => $this->region,
            'version'     => 'latest',
        ]);
    }

    static public function getSubscribedEvents()
    {
        return [
            'core.user_setup'                => 'user_setup',
            'core.modify_uploaded_file'      => 'modify_uploaded_file',
            'core.validate_config_variable'  => 'validate_config_variable',
        ];
    }

    public function user_setup($event)
    {
        $lang_set_ext = $event['lang_set_ext'];
        $lang_set_ext[] = [
            'ext_name' => 'AustinMaddox/s3',
            'lang_set' => 'common',
        ];
        $event['lang_set_ext'] = $lang_set_ext;
    }

    /**
     * Validate the AWS Access Key Id
     *
     * @param object $event The event object
     *
     * @return null
     * @access public
     */
    public function validate_config_variable($event)
    {
        $input = $event['cfg_array']['s3_aws_access_key_id'];

        // Check if the validate test is for s3.
        if (($event['config_definition']['validate'] == 's3_aws_access_key_id') && ($input !== '')) {
            // Store the error and input event data.
            $error = $event['error'];

            // Add error message if the input is not a valid AWS Access Key Id.
//            if (!preg_match('/^UA-\d{4,9}-\d{1,4}$/', $input)) {
//                $error[] = $this->user->lang('ACP_S3_AWS_ACCESS_KEY_ID', $input);
//            }

            // Update error event data.
            $event['error'] = $error;
        }
    }

    public function modify_uploaded_file($event)
    {
        $this->log->debug('##############################################');
        $this->log->debug(__METHOD__);
        $this->log->debug('##############################################');

        $result = null;
        $key = time();
        $data = 'path/to/file.png';

        try {
            $result = $this->s3_client->upload($this->bucket, $key, $data, 'public-read');
        } catch (MultipartUploadException $e) {
            $this->log->debug('MultipartUpload Failed', [$e->getMessage()]);
        }

        $object_url = ($result['ObjectURL']) ? $result['ObjectURL'] : $result['Location'];
        $this->log->debug($object_url);
    }
}<commit_msg>Successfully write uploaded attachments to S3 bucket<commit_after><?php
/**
 *
 * @package       phpBB Extension - S3
 * @copyright (c) 2016 <NAME>
 * @license       http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
 *
 */

namespace AustinMaddox\s3\event;

use Aws\Exception\MultipartUploadException;
use Aws\S3\S3Client;
use Monolog\Handler\ErrorLogHandler;
use Monolog\Logger;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Event listener
 */
class main_listener implements EventSubscriberInterface
{
    /** @var \phpbb\config\config */
    protected $config;

    /** @var \phpbb\template\template */
    protected $template;

    /** @var \phpbb\user */
    protected $user;

    protected $bucket;
    protected $log;
    protected $region;
    protected $s3_client;

    /**
     * Constructor
     *
     * @param \phpbb\config\config     $config   Config object
     * @param \phpbb\template\template $template Template object
     * @param \phpbb\user              $user     User object
     *
     * @return \AustinMaddox\s3\event\main_listener
     * @access public
     */
    public function __construct(\phpbb\config\config $config, \phpbb\template\template $template, \phpbb\user $user)
    {
        $this->config = $config;
        $this->template = $template;
        $this->user = $user;

        // TODO: Remove logging after dev/debug.
        $this->log = new Logger('phpbb');
        $this->log->pushHandler(new ErrorLogHandler(ErrorLogHandler::OPERATING_SYSTEM, Logger::DEBUG));

        $this->region = 'us-west-2';
        $this->bucket = 'warriormachines-phpbb-files';

        // Instantiate an AWS S3 client.
        $this->s3_client = new S3Client([
            'credentials' => [
                'key'    => $this->config['s3_aws_access_key_id'],
                'secret' => $this->config['s3_aws_secret_access_key'],
            ],
            'debug'       => false,
            'region'      => $this->region,
            'version'     => 'latest',
        ]);
    }

    static public function getSubscribedEvents()
    {
        return [
            'core.user_setup'               => 'user_setup',
            'core.modify_uploaded_file'     => 'modify_uploaded_file',
            'core.validate_config_variable' => 'validate_config_variable',
        ];
    }

    public function user_setup($event)
    {
        $lang_set_ext = $event['lang_set_ext'];
        $lang_set_ext[] = [
            'ext_name' => 'AustinMaddox/s3',
            'lang_set' => 'common',
        ];
        $event['lang_set_ext'] = $lang_set_ext;
    }

    /**
     * Validate the AWS Access Key Id
     *
     * @param object $event The event object
     *
     * @return null
     * @access public
     */
    public function validate_config_variable($event)
    {
        $input = $event['cfg_array']['s3_aws_access_key_id'];

        // Check if the validate test is for s3.
        if (($event['config_definition']['validate'] == 's3_aws_access_key_id') && ($input !== '')) {
            // Store the error and input event data.
            $error = $event['error'];

            // Add error message if the input is not a valid AWS Access Key Id.
//            if (!preg_match('/^UA-\d{4,9}-\d{1,4}$/', $input)) {
//                $error[] = $this->user->lang('ACP_S3_AWS_ACCESS_KEY_ID', $input);
//            }

            // Update error event data.
            $event['error'] = $error;
        }
    }

    public function modify_uploaded_file($event)
    {
        $this->log->debug(__METHOD__);
        $this->log->debug('##############################################');

        global $phpbb_root_path, $config;

        $filedata = $event['filedata'];
        $this->log->debug(print_r($filedata, true));

        $result = null;
        $path_parts = pathinfo($filedata['real_filename']);
        $key = uniqid(md5($path_parts['filename'])) . '.' . $filedata['extension'];
        $body = file_get_contents($phpbb_root_path . $config['upload_path'] . '/' . $filedata['physical_filename']);

        try {
            $result = $this->s3_client->upload($this->bucket, $key, $body, 'public-read');
        } catch (MultipartUploadException $e) {
            $this->log->debug('MultipartUpload Failed', [$e->getMessage()]);
        }

        $object_url = ($result['ObjectURL']) ? $result['ObjectURL'] : $result['Location'];

        // TODO: Maybe write this $object_url in place of the `physical_filename` in the `phpbb_attachments` db table?
        $this->log->debug($object_url);
    }
}
{'fraction_non_alphanumeric': 0.16334478808705613, 'fraction_numerical': 0.006872852233676976, 'mean_word_length': 2.2541930674617965, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3095871', 'n_tokens_mistral': 2695, 'n_tokens_neox': 2617, 'n_words': 516}