<commit_before><?php
/*=========================================================================
 MIDAS Server
 Copyright (c) Kitware SAS. 26 rue <NAME>. 69100 Villeurbanne, FRANCE
 All rights reserved.
 More information http://www.kitware.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0.txt

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
=========================================================================*/

// HTTPUPLOAD error codes
define('MIDAS_HTTPUPLOAD_UPLOAD_FAILED', -105);
define('MIDAS_HTTPUPLOAD_UPLOAD_TOKEN_GENERATION_FAILED', -140);
define('MIDAS_HTTPUPLOAD_INVALID_UPLOAD_TOKEN', -141);
define('MIDAS_HTTPUPLOAD_INPUT_FILE_OPEN_FAILED', -142);
define('MIDAS_HTTPUPLOAD_OUTPUT_FILE_OPEN_FAILED', -143);
define('MIDAS_HTTPUPLOAD_TMP_DIR_CREATION_FAILED', -144);
define('MIDAS_HTTPUPLOAD_PARAM_UNDEFINED', -150);

/**
 * This component is used for large uploads and is used by
 * the web api and the java uploader.  It generates an authenticated
 * upload token that can be used to start or resume an upload.
 */
class HttpuploadComponent extends AppComponent
  {

  var $tmpDirectory = '';
  var $tokenParamName = 'uploadtoken';
  var $testingEnable = false;

  /** Set the upload temporary directory */
  public function setTmpDirectory($dir)
    {
    $this->tmpDirectory = $dir;
    }

  /** Set whether we are in testing mode or not (boolean) */
  public function setTestingMode($testing)
    {
    $this->testingEnable = $testing;
    }

  /** Set the name of the uploadtoken parameter that is being passed */
  public function setTokenParamName($name)
    {
    $this->tokenParamName = $name;
    }

  /**
   * Generate an upload token that will act as the authentication token for the upload.
   * This token is the filename of a guaranteed unique file which will be placed under the
   * directory specified by the dirname parameter, which should be used to ensure that
   * the user can only write into a certain logical space.
   */
  public function generateToken($args, $dirname = '')
    {
    if(!array_key_exists('filename', $args))
      {
      throw new Exception('Parameter filename is not defined', MIDAS_HTTPUPLOAD_FILENAME_PARAM_UNDEFINED);
      }
    $dir = $dirname == '' ? '' : '/'.$dirname;
    $dir = $this->tmpDirectory.$dir;

    if(!file_exists($dir))
      {
      if(!is_writable($dir) || !mkdir($dir, 0700, true))
        {
        throw new Exception('Failed to create temporary upload dir', MIDAS_HTTPUPLOAD_TMP_DIR_CREATION_FAILED);
        }
      }
    // create a unique temporary file in the dirname directory
    $unique_identifier = basename(tempnam($dir, $args['filename']));
    if($dirname != '')
      {<commit_msg>BUG: refs #0458. Changed check to search for first existing parent dir.<commit_after><?php
/*=========================================================================
 MIDAS Server
 Copyright (c) Kitware SAS. 26 rue <NAME>. 69100 Villeurbanne, FRANCE
 All rights reserved.
 More information http://www.kitware.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0.txt

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
=========================================================================*/

// HTTPUPLOAD error codes
define('MIDAS_HTTPUPLOAD_UPLOAD_FAILED', -105);
define('MIDAS_HTTPUPLOAD_UPLOAD_TOKEN_GENERATION_FAILED', -140);
define('MIDAS_HTTPUPLOAD_INVALID_UPLOAD_TOKEN', -141);
define('MIDAS_HTTPUPLOAD_INPUT_FILE_OPEN_FAILED', -142);
define('MIDAS_HTTPUPLOAD_OUTPUT_FILE_OPEN_FAILED', -143);
define('MIDAS_HTTPUPLOAD_TMP_DIR_CREATION_FAILED', -144);
define('MIDAS_HTTPUPLOAD_PARAM_UNDEFINED', -150);

/**
 * This component is used for large uploads and is used by
 * the web api and the java uploader.  It generates an authenticated
 * upload token that can be used to start or resume an upload.
 */
class HttpuploadComponent extends AppComponent
  {

  var $tmpDirectory = '';
  var $tokenParamName = 'uploadtoken';
  var $testingEnable = false;

  /** Set the upload temporary directory */
  public function setTmpDirectory($dir)
    {
    $this->tmpDirectory = $dir;
    }

  /** Set whether we are in testing mode or not (boolean) */
  public function setTestingMode($testing)
    {
    $this->testingEnable = $testing;
    }

  /** Set the name of the uploadtoken parameter that is being passed */
  public function setTokenParamName($name)
    {
    $this->tokenParamName = $name;
    }

  /**
   * Generate an upload token that will act as the authentication token for the upload.
   * This token is the filename of a guaranteed unique file which will be placed under the
   * directory specified by the dirname parameter, which should be used to ensure that
   * the user can only write into a certain logical space.
   */
  public function generateToken($args, $dirname = '')
    {
    if(!array_key_exists('filename', $args))
      {
      throw new Exception('Parameter filename is not defined', MIDAS_HTTPUPLOAD_FILENAME_PARAM_UNDEFINED);
      }
    $dir = $dirname == '' ? '' : '/'.$dirname;
    $dir = $this->tmpDirectory.$dir;

    if(!file_exists($dir))
      {

      // this bit of logic will detect permissions problems, and prevent the
      // json encoding from breaking, when mkdir attempts to create a dir that
      // it doesn't have permissions for.
      //
      // search backwards up towards the root, look for the first existing dir
      $correctPermissions = false;
      // back up one directory to the current dir's root
      $dirRoot = dirname($dir);
      while(!$correctPermissions)
        {
        if(file_exists($dirRoot))
          {
          if(!is_writeable($dirRoot))
            {
            // if the closest existing root dir isn't writable that is an error
            throw new Exception('Failed to create temporary upload dir because a parent dir is not writeable', MIDAS_HTTPUPLOAD_TMP_DIR_CREATION_FAILED);
            }
          else
            {
            $correctPermissions = true;
            }
          }
        else
          {
          // back up one directory to the current dirRoot's root
          $dirRoot = dirname($dir);
          }
        }

      if(!mkdir($dir, 0700, true))
        {
        throw new Exception('Failed to create temporary upload dir', MIDAS_HTTPUPLOAD_TMP_DIR_CREATION_FAILED);
        }
      }
    // create a unique temporary file in the dirname directory
    $unique_identifier = basename(tempnam($dir, $args['filename']));
    if($dirname != '')
      {
{'fraction_non_alphanumeric': 0.12818417109385644, 'fraction_numerical': 0.010352812968260456, 'mean_word_length': 3.476829268292683, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '524489', 'n_tokens_mistral': 2049, 'n_tokens_neox': 2041, 'n_words': 709}