<commit_before><?php

class ImageUpload
{
  /**
   * The instance of the $_FILES array
   *
   * @var array
   */
  private $_files;

  /**
   * The path to uplaod the images
   *
   * @var string
   */
  private $path;

  /**
   * Constructor function
   */
  public function __construct($_files = null, $path = null)
  {
    $this->_files = $_files;
    $this->path = $path;
  }

  /**
   * Set $_files
   *
   * @param       $_files       The $_FILES array
   */
  public function setFiles($_files)
  {
    $this->_files = $_files;
  }

  /**
   * Get $_files
   *
   * @return      @array        The $_FILES array
   */
  public function getFiles()
  {
    return $this->_files;
  }

  /**
   * Set $path
   *
   * @param       $path         The path to upload images
   */
  public function setPath($path)
  {
    $this->path = $path;
  }

  /**
   * Get $path
   *
   * @return      string        The path to upload images
   */
  public function getPath()
  {
    return $this->path;
  }

  /**
   * Checks the files and path parameters
   *
   * @var         string         The name of the input file element
   */
  private function checkParameters($image)
  {
    // Checking if both _file and path are valid
    if (!is_array($this->_files)) {
      throw new Exception("Invalid FILES parameter");
    }
    if (!is_array($this->_files[$image])) {
      throw new Exception("No image with given name uploaded");
    }
    if (!file_exists($this->path)) {
      throw new Exception("Given path does not exists");
    }
  }

  /**
   * Checks $_FILES[$image]['error']
   *
   * @var         string        The name of the input file element
   */
  private function checkFilesError($image)
  {
    if ( !isset($this->_files[$image]['error']) || is_array($this->_files[$image]['error']) ) {
      throw new Exception("Invalid parameters");
    }

    switch ($this->_files[$image]['error']) {

      case UPLOAD_ERR_OK:
        break;

      case UPLOAD_ERR_NO_FILE:
        throw new Exception('No file sent.');

      case UPLOAD_ERR_INI_SIZE:

      case UPLOAD_ERR_FORM_SIZE:
        throw new Exception('Exceeded filesize limit.');

      default:
        throw new Exception('Unknown errors.');
    }
  }

  /**
   * Makes a list of security checks before uploading
   * Throws an exception on any error
   *
   * @var         string        The name of the input file element
   */
  private function securityCheck($image)
  {
    $this->checkParameters($image);
    $this->checkFilesError($image);
  }

  /**
   * Uploads a particular image
   *
   * @var         $image        The name of the input file element
   *
   * @return      boolean       Whether the upload was successfull or not
   */
  public function upload($image)
  {<commit_msg>Added checks for image mime types<commit_after><?php

class ImageUpload
{
  /**
   * The instance of the $_FILES array
   *
   * @var array
   */
  private $_files;

  /**
   * The path to uplaod the images
   *
   * @var string
   */
  private $path;

  /**
   * List of valid mime types
   *
   * @var array
   */
  private static $ALLOWED_MIME_TYPES = array(
    "image/gif",
    "image/jpg",
    "image/png",
    "image/bmp",
  );

  /**
   * Constructor function
   */
  public function __construct($_files = null, $path = null)
  {
    $this->_files = $_files;
    $this->path = $path;
  }

  /**
   * Set $_files
   *
   * @param       $_files       The $_FILES array
   */
  public function setFiles($_files)
  {
    $this->_files = $_files;
  }

  /**
   * Get $_files
   *
   * @return      @array        The $_FILES array
   */
  public function getFiles()
  {
    return $this->_files;
  }

  /**
   * Set $path
   *
   * @param       $path         The path to upload images
   */
  public function setPath($path)
  {
    $this->path = $path;
  }

  /**
   * Get $path
   *
   * @return      string        The path to upload images
   */
  public function getPath()
  {
    return $this->path;
  }

  /**
   * Checks the files and path parameters
   *
   * @var         string         The name of the input file element
   */
  private function checkParameters($image)
  {
    // Checking if both _file and path are valid
    if (!is_array($this->_files)) {
      throw new Exception("Invalid FILES parameter");
    }
    if (!is_array($this->_files[$image])) {
      throw new Exception("No image with given name uploaded");
    }
    if (!file_exists($this->path)) {
      throw new Exception("Given path does not exists");
    }
  }

  /**
   * Checks $_FILES[$image]['error']
   *
   * @var         string        The name of the input file element
   */
  private function checkFilesError($image)
  {
    if ( !isset($this->_files[$image]['error']) || is_array($this->_files[$image]['error']) ) {
      throw new Exception("Invalid parameters");
    }

    switch ($this->_files[$image]['error']) {

      case UPLOAD_ERR_OK:
        break;

      case UPLOAD_ERR_NO_FILE:
        throw new Exception('No file sent.');

      case UPLOAD_ERR_INI_SIZE:

      case UPLOAD_ERR_FORM_SIZE:
        throw new Exception('Exceeded filesize limit.');

      default:
        throw new Exception('Unknown errors.');
    }
  }

  /**
   * Checks the mime type of the image
   *
   * @var         string        The name of the input file element
   */
  private function checkMimeType($image)
  {
    // Extracting mime type using getimagesize
    $image_info = getimagesize($this->_files[$image]["tmp_name"]);
    if ($image_info === null) {
      throw new  Exception("Invalid image type");
    }
    
    $mime_type = $image_info["mime"];

    if (!in_array($mime_type, self::$ALLOWED_MIME_TYPES)) {
      throw new Exception("Invalid image MIME type");
    }
  }

  /**
   * Makes a list of security checks before uploading
   * Throws an exception on any error
   *
   * @var         string        The name of the input file element
   */
  private function securityCheck($image)
  {
    $this->checkParameters($image);
    $this->checkFilesError($image);
    $this->checkMimeType($image);
  }

  /**
   * Uploads a particular image
   *
   * @var         $image        The name of the input file element
   *
   * @return      boolean       Whether the upload was successfull or not
   */
  public function upload($image)
  {
{'fraction_non_alphanumeric': 0.12642947903430748, 'fraction_numerical': 0.0, 'mean_word_length': 2.1532298447671505, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '3263742', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1941, 'n_words': 544}