示例#1
0
 /**
  * Puts together the POST body when uploading files
  *
  * @param array  $postData
  * @param array  $files An array of files like `[string $fileName => string|resource|array $file]`.
  *                      In case $file is an array it provides additional control on file name and contents type.
  *                      Available keys are `path`, `fileContents`, `fileName` and `contentType`. One of `path` or
  *                      `fileContents` is required.
  *                      If `path` is set then `fileName` and `contentType` are optional.
  *                      If `fileContents` is set then `fileName` and `contentType` are required.
  *
  *                      examples:
  *                      <ul>
  *                      <li>['file1' => ['path' => '/tmp/file.php']]</li>
  *                      <li>['file1' => ['path' => '/tmp/file.php'], 'file2' => ['path' => '/tmp/file2.png']]</li>
  *                      <li>['file1' => ['fileContents' => '<?php echo 1;', 'fileName' => 'o.png', 'contentType' => 'image/png']]</li>
  *                      </ul>
  * @param string $formBoundary
  *
  * @return string
  * @author Panagiotis Vagenas <*****@*****.**>
  * @since  0-dev
  */
 protected static function getUploadFormData($postData = [], $files = [], $formBoundary = '__FORM_BOUNDARY__')
 {
     $postData = (array) $postData;
     $formBoundary = preg_replace('/\\W/', '', $formBoundary);
     $payload = [];
     foreach ($postData as $paramName => $value) {
         $payload[] = '--' . $formBoundary;
         $payload[] = 'Content-Disposition: form-data; name="' . $paramName . '"';
         $payload[] = '';
         $payload[] = $value;
     }
     /** @var resource|string|array $file */
     foreach ($files as $paramName => $file) {
         $fileData = self::getFileData($file);
         if (isset($fileData['error'])) {
             Cli::writeError("Something went wrong with file {$paramName} (error: {$fileData['error']}), skipping...");
             continue;
         }
         $payload[] = '--' . $formBoundary;
         $payload[] = 'Content-Disposition: form-data; name="' . $paramName . '"; filename="' . $fileData['fileName'] . '"';
         $payload[] = "Content-Type: {$fileData['contentType']}";
         $payload[] = '';
         $payload[] = $fileData['fileContents'];
         $payload[] = '';
     }
     $payload[] = '--' . $formBoundary;
     $payload[] = '';
     return implode(CRLF, $payload);
 }
示例#2
0
 /**
  * @param string $msg A message to display before exiting
  *
  * @author Panagiotis Vagenas <*****@*****.**>
  * @since  0-dev
  */
 public static function exitWithFailedRequest($msg = '')
 {
     if ($msg) {
         Cli::writeError($msg);
     }
     self::exitWith(self::EXIT_CODE_VALID_REQUEST_FAILED);
 }