<commit_before><?php
/**
 * Proffer
 * An upload behavior plugin for CakePHP 3
 *
 * @author <NAME> <<EMAIL>>
 */

namespace Proffer\Model\Behavior;

use ArrayObject;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Exception;
use Proffer\Event\ProfferListener;
use Proffer\Lib\ProfferPath;

/**
 * Proffer behavior
 */
class ProfferBehavior extends Behavior
{
    /**
     * Initialize the behavior
     *
     * @param array $config Array of pass configuration
     * @return void
     */
    public function initialize(array $config)
    {
        $listener = new ProfferListener();
        $this->_table->eventManager()->on($listener);
    }

    /**
     * beforeMarshal event
     *
     * @param Event $event
     * @param ArrayObject $data
     * @param ArrayObject $options
     */
    public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
    {
        foreach ($this->config() as $field => $settings) {
            if ($this->_table->validator()->isEmptyAllowed($field, false) &&
                isset($data[$field]['error']) && $data[$field]['error'] === UPLOAD_ERR_NO_FILE
            ) {
                unset($data[$field]);
            }
        }
    }

    /**
     * beforeSave method
     *
     * @param Event $event The event
     * @param Entity $entity The entity
     * @param ArrayObject $options Array of options
     * @param ProfferPath $path Inject an instance of ProfferPath
     * @return true
     * @throws Exception
     */
    public function beforeSave(Event $event, Entity $entity, ArrayObject $options, ProfferPath $path = null)
    {
        foreach ($this->config() as $field => $settings) {
            if ($entity->has($field) && is_array($entity->get($field)) &&
                $entity->get($field)['error'] === UPLOAD_ERR_OK) {
//                if (!$this->isUploadedFile($entity->get($field)['tmp_name'])) {
//                    throw new Exception('File must be uploaded using HTTP post.');
//                }

                if (!$path) {
                    $path = new ProfferPath($this->_table, $entity, $field, $settings);
                }

                $event = new Event('Proffer.afterPath', $entity, ['path' => $path]);
                $this->_table->eventManager()->dispatch($event);
                if (!empty($event->result)) {
                    $path = $event->result;
                }

                $path->createPathFolder();

                if ($this->moveUploadedFile($entity->get($field)['tmp_name'], $path->fullPath())) {
                    $entity->set($field, $entity->get($field)['name']);
                    $entity->set($settings['dir'], $path->getSeed());

                    // Don't generate thumbnails for non-images
                    if (getimagesize($path->fullPath()) !== false) {
                        $this->makeThumbs($field, $path);
                    }
                } else {
                    throw new Exception('Cannot move file');
                }
            }
        }

        return true;
    }

    /**
     * afterDelete method
     *
     * Remove images from records which have been deleted, if they exist
     *
     * @param Event $event The passed event
     * @param Entity $entity The entity
     * @param ArrayObject $options Array of options
     * @param ProfferPath $path Inject and instance of ProfferPath
     * @return bool
     */
    public function afterDelete(Event $event, Entity $entity, ArrayObject $options, ProfferPath $path = null)
    {
        foreach ($this->config() as $field => $settings) {
            $dir = $entity->get($settings['dir']);

            if (!empty($entity) && !empty($dir)) {
                if (!$path) {
                    $path = new ProfferPath($this->_table, $entity, $field, $settings);
                }

                foreach ($settings['thumbnailSizes'] as $prefix => $dimensions) {
                    $filename = $path->fullPath($prefix);
                    unlink($filename);
                }

                $filename = $path->fullPath();
                unlink($filename);

                rmdir($path->getFolder());
            }
        }

        return true;
    }

    /**
     * Dispatch events to allow generation of thumbnails
     *
     * @param string $field The name of the upload field
     * @param ProfferPath $path The path array
     * @return void
     */
    protected function makeThumbs($field, ProfferPath $path)
    {
        foreach ($this->config($field)['thumbnailSizes'] as $prefix => $dimensions) {
            $eventParams = ['path' => $path, 'dimensions' => $dimensions, 'thumbnailMethod' => null];

            if (isset($this->config($field)['thumbnailMethod'])) {
                $params['thumbnailMethod'] = $this->config($field)['thumbnailMethod'];
            }

            // Event listener handles generation
            $event = new Event('Proffer.beforeThumbs', $this->_table, $eventParams);

            $this->_table->eventManager()->dispatch($event);
            if (!empty($event->result)) {
                $image = $event->result;
            }

            $event = new Event('Proffer.afterThumbs', $this->_table, [
                'path' => $path,
                'image' => $image,
                'prefix' => $prefix
            ]);

            $this->_table->eventManager()->dispatch($event);
            if (!empty($event->result)) {
                $image = $event->result;
            }
        }
    }

    /**
     * Wrapper method for is_uploaded_file so that we can test
     *
     * @param string $file The tmp_name path to the uploaded file
     * @return bool
     * @deprecated
     */
    protected function isUploadedFile($file)
    {
        return is_uploaded_file($file);
    }

    /**
     * Wrapper method for move_uploaded_file so that we can test
     *
     * @param string $file Path to the uploaded file
     * @param string $destination The destination file name
     * @return bool
     */
    protected function moveUploadedFile($file, $destination)
    {
        if ($this->is_uploaded_file($file)) {
            return move_uploaded_file($file, $destination);
        }

        return rename($file, $destination);
    }
}
<commit_msg>Small bugfix<commit_after><?php
/**
 * Proffer
 * An upload behavior plugin for CakePHP 3
 *
 * @author <NAME> <<EMAIL>>
 */

namespace Proffer\Model\Behavior;

use ArrayObject;
use Cake\Event\Event;
use Cake\ORM\Behavior;
use Cake\ORM\Entity;
use Exception;
use Proffer\Event\ProfferListener;
use Proffer\Lib\ProfferPath;

/**
 * Proffer behavior
 */
class ProfferBehavior extends Behavior
{
    /**
     * Initialize the behavior
     *
     * @param array $config Array of pass configuration
     * @return void
     */
    public function initialize(array $config)
    {
        $listener = new ProfferListener();
        $this->_table->eventManager()->on($listener);
    }

    /**
     * beforeMarshal event
     *
     * @param Event $event
     * @param ArrayObject $data
     * @param ArrayObject $options
     */
    public function beforeMarshal(Event $event, ArrayObject $data, ArrayObject $options)
    {
        foreach ($this->config() as $field => $settings) {
            if ($this->_table->validator()->isEmptyAllowed($field, false) &&
                isset($data[$field]['error']) && $data[$field]['error'] === UPLOAD_ERR_NO_FILE
            ) {
                unset($data[$field]);
            }
        }
    }

    /**
     * beforeSave method
     *
     * @param Event $event The event
     * @param Entity $entity The entity
     * @param ArrayObject $options Array of options
     * @param ProfferPath $path Inject an instance of ProfferPath
     * @return true
     * @throws Exception
     */
    public function beforeSave(Event $event, Entity $entity, ArrayObject $options, ProfferPath $path = null)
    {
        foreach ($this->config() as $field => $settings) {
            if ($entity->has($field) && is_array($entity->get($field)) &&
                $entity->get($field)['error'] === UPLOAD_ERR_OK) {

                if (!$path) {
                    $path = new ProfferPath($this->_table, $entity, $field, $settings);
                }

                $event = new Event('Proffer.afterPath', $entity, ['path' => $path]);
                $this->_table->eventManager()->dispatch($event);
                if (!empty($event->result)) {
                    $path = $event->result;
                }

                $path->createPathFolder();

                if ($this->moveUploadedFile($entity->get($field)['tmp_name'], $path->fullPath())) {
                    $entity->set($field, $entity->get($field)['name']);
                    $entity->set($settings['dir'], $path->getSeed());

                    // Don't generate thumbnails for non-images
                    if (getimagesize($path->fullPath()) !== false) {
                        $this->makeThumbs($field, $path);
                    }
                } else {
                    throw new Exception('Cannot move file');
                }
            }
        }

        return true;
    }

    /**
     * afterDelete method
     *
     * Remove images from records which have been deleted, if they exist
     *
     * @param Event $event The passed event
     * @param Entity $entity The entity
     * @param ArrayObject $options Array of options
     * @param ProfferPath $path Inject and instance of ProfferPath
     * @return bool
     */
    public function afterDelete(Event $event, Entity $entity, ArrayObject $options, ProfferPath $path = null)
    {
        foreach ($this->config() as $field => $settings) {
            $dir = $entity->get($settings['dir']);

            if (!empty($entity) && !empty($dir)) {
                if (!$path) {
                    $path = new ProfferPath($this->_table, $entity, $field, $settings);
                }

                foreach ($settings['thumbnailSizes'] as $prefix => $dimensions) {
                    $filename = $path->fullPath($prefix);
                    unlink($filename);
                }

                $filename = $path->fullPath();
                unlink($filename);

                rmdir($path->getFolder());
            }
        }

        return true;
    }

    /**
     * Dispatch events to allow generation of thumbnails
     *
     * @param string $field The name of the upload field
     * @param ProfferPath $path The path array
     * @return void
     */
    protected function makeThumbs($field, ProfferPath $path)
    {
        foreach ($this->config($field)['thumbnailSizes'] as $prefix => $dimensions) {
            $eventParams = ['path' => $path, 'dimensions' => $dimensions, 'thumbnailMethod' => null];

            if (isset($this->config($field)['thumbnailMethod'])) {
                $params['thumbnailMethod'] = $this->config($field)['thumbnailMethod'];
            }

            // Event listener handles generation
            $event = new Event('Proffer.beforeThumbs', $this->_table, $eventParams);

            $this->_table->eventManager()->dispatch($event);
            if (!empty($event->result)) {
                $image = $event->result;
            }

            $event = new Event('Proffer.afterThumbs', $this->_table, [
                'path' => $path,
                'image' => $image,
                'prefix' => $prefix
            ]);

            $this->_table->eventManager()->dispatch($event);
            if (!empty($event->result)) {
                $image = $event->result;
            }
        }
    }

    /**
     * Wrapper method for move_uploaded_file
     * This will check if the file has been uploaded or not before picking the correct method to move the file
     *
     * @param string $file Path to the uploaded file
     * @param string $destination The destination file name
     * @return bool
     */
    protected function moveUploadedFile($file, $destination)
    {
        if (is_uploaded_file($file)) {
            return move_uploaded_file($file, $destination);
        }

        return rename($file, $destination);
    }
}

{'fraction_non_alphanumeric': 0.13781443298969073, 'fraction_numerical': 0.00016494845360824742, 'mean_word_length': 1.907216494845361, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 132, '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': '6704719', 'n_tokens_mistral': 3343, 'n_tokens_neox': 3178, 'n_words': 806}