HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/phpmyfaq/src/phpMyFAQ/Attachment/AttachmentFactory.php
<?php

/**
 * Attachment handler class.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public License,
 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
 * obtain one at http://mozilla.org/MPL/2.0/.
 *
 * @package   phpMyFAQ
 * @author    Anatoliy Belsky <ab@php.net>
 * @since     2009-08-21
 * @license   http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
 * @link      https://www.phpmyfaq.de
 * @copyright 2009-2022 phpMyFAQ Team
 */

namespace phpMyFAQ\Attachment;

use phpMyFAQ\Attachment;
use phpMyFAQ\Configuration;
use phpMyFAQ\Database;
use phpMyFAQ\Language;

/**
 * Class AttachmentFactory
 *
 * @package phpMyFAQ\Attachment
 */
class AttachmentFactory
{
    /**
     * Default encryption key.
     *
     * @var string|null
     */
    private static ?string $defaultKey = null;

    /**
     * Storage type.
     *
     * @var int|null
     */
    private static ?int $storageType = null;

    /**
     * File encryption is enabled.
     *
     * @var bool|null
     */
    private static ?bool $encryptionEnabled = null;

    /**
     * Create an attachment exemplar.
     *
     * @param int|null    $id  ID
     * @param string|null $key Key
     * @return File
     * @throws AttachmentException
     */
    public static function create(int $id = null, string $key = null): File
    {
        switch (self::$storageType) {
            case Attachment::STORAGE_TYPE_FILESYSTEM:
                $return = new File($id);
                break;

            default:
                throw new AttachmentException('Unknown attachment storage type');
        }

        /*
         * If encryption isn't enabled, just ignoring all keys
         */
        if (self::$encryptionEnabled) {
            $key = null === $key ? self::$defaultKey : $key;
        } else {
            $key = null;
        }

        $return->setKey($key);

        return $return;
    }

    /**
     * Fetch all record attachments.
     *
     * @param Configuration $config
     * @param int           $recordId ID of the record
     *
     * @throws AttachmentException
     *
     * @return File[]
     */
    public static function fetchByRecordId(Configuration $config, int $recordId): array
    {
        $files = [];

        $sql = sprintf(
            "SELECT
                id
            FROM
                %sfaqattachment
            WHERE
                record_id = %d
            AND
                record_lang = '%s'",
            Database::getTablePrefix(),
            $recordId,
            Language::$language
        );

        $result = $config->getDb()->fetchAll($config->getDb()->query($sql));
        if (!empty($result)) {
            foreach ($result as $item) {
                $files[] = self::create($item->id);
            }
        }
        reset($files);

        return $files;
    }

    /**
     * Initializing factory with global attachment settings.
     *
     * @param int    $storageType       Storage type
     * @param string $defaultKey        Default key
     * @param bool   $encryptionEnabled Enabled encryption?
     */
    public static function init(int $storageType, string $defaultKey, bool $encryptionEnabled): void
    {
        if (null === self::$storageType) {
            self::$storageType = $storageType;
        }

        if (null === self::$defaultKey) {
            self::$defaultKey = $defaultKey;
        }

        if (null === self::$encryptionEnabled) {
            self::$encryptionEnabled = $encryptionEnabled;
        }
    }

    /**
     * Re-arranges the $_FILES array for multiple file uploads.
     *
     * @param $filePost
     * @return array
     */
    public static function rearrangeUploadedFiles(&$filePost): array
    {
        $filesArray = [];
        $filesCount = count($filePost['name']);
        $filesKeys = array_keys($filePost);

        for ($i = 0; $i < $filesCount; $i++) {
            foreach ($filesKeys as $key) {
                $filesArray[$i][$key] = $filePost[$key][$i];
            }
        }

        return $filesArray;
    }
}