/**
  * @dataProvider tempnamProvider
  */
 public function testTempnam($dir, $prefix, $expected_path_prefix)
 {
     $filename = SplOverride::tempnam($dir, $prefix);
     $this->assertTrue(StringUtil::startsWith($filename, $expected_path_prefix));
     $this->assertTrue(strlen($filename) > strlen($expected_path_prefix));
     $this->assertTrue(file_exists($filename));
     $this->assertEquals(0600, fileperms($filename) & 0xfff);
     $this->assertEquals(0, filesize($filename));
 }
 /**
  * Extract metadata from HTTP response headers.
  */
 private static function extractMetaData($headers)
 {
     $metadata = [];
     foreach ($headers as $key => $value) {
         if (StringUtil::startsWith(strtolower($key), self::METADATA_HEADER_PREFIX)) {
             $metadata_key = substr($key, strlen(self::METADATA_HEADER_PREFIX));
             $metadata[$metadata_key] = $value;
         }
     }
     return $metadata;
 }
예제 #3
0
 /**
  * Create file with unique file name.
  *
  * @param string $dir
  *   The directory where the temporary filename will be created.
  * @param string $prefix
  *   The prefix of the generated temporary filename.
  * @return bool|string
  *   a string with the new temporary file name on success, otherwise FALSE.
  *
  * @see http://php.net/manual/en/function.tempnam.php
  */
 public static function tempnam($dir, $prefix)
 {
     // Force $dir into a VFS temp path if it's not already one.
     $temp_root = static::sys_get_temp_dir();
     if (!StringUtil::startsWith($dir, $temp_root)) {
         $dir = $temp_root . '/' . str_replace('\\', '/', $dir);
     }
     // Create all intermediate directories if needed.
     @mkdir($dir, 0777, true);
     // Generate a unique non-existing file name.
     for ($retry = 0; $retry < 10 || file_exists($filename); $retry++) {
         $filename = $dir . '/' . uniqid($prefix, true);
     }
     if (file_exists($filename)) {
         trigger_error('Fail to generate a unique name for the temporary file', E_USER_ERROR);
         return false;
     }
     // tempnam requires the file to be created with permission set to 0600.
     if (touch($filename) === false || chmod($filename, static::DEFAULT_TMPFILE_MODE) === false) {
         trigger_error('Fail to create and change permission of temporary file ' . $filename, E_USER_ERROR);
         return false;
     }
     return $filename;
 }
예제 #4
0
 /**
  * Parse and extract the bucket and object names from the supplied filename.
  *
  * @param string $filename The filename in the format gs://bucket_name or
  * gs://bucket_name/object_name.
  * @param string &$bucket The extracted bucket.
  * @param string &$object The extracted bucket. Can be null if the filename
  * contains only bucket name.
  *
  * @return bool true if the filename is successfully parsed, false otherwise.
  */
 public static function parseFilename($filename, &$bucket, &$object)
 {
     $bucket = null;
     $object = null;
     // $filename may contain nasty characters like # and ? that can throw off
     // parse_url(). It is best to do a manual parse here.
     $gs_prefix_len = strlen(self::GS_PREFIX);
     if (!StringUtil::startsWith($filename, self::GS_PREFIX)) {
         return false;
     }
     $first_slash_pos = strpos($filename, '/', $gs_prefix_len);
     if ($first_slash_pos === false) {
         $bucket = substr($filename, $gs_prefix_len);
     } else {
         $bucket = substr($filename, $gs_prefix_len, $first_slash_pos - $gs_prefix_len);
         // gs://bucket_name/ is treated the same as gs://bucket_name where
         // $object should be set to null.
         if ($first_slash_pos != strlen($filename) - 1) {
             $object = substr($filename, $first_slash_pos);
         }
     }
     if (strlen($bucket) == 0) {
         return false;
     }
     // Validate bucket & object names.
     if (self::validateBucketName($bucket) === false) {
         trigger_error(sprintf('Invalid cloud storage bucket name \'%s\'', $bucket), E_USER_ERROR);
         return false;
     }
     if (isset($object) && self::validateObjectName($object) === false) {
         trigger_error(sprintf('Invalid cloud storage object name \'%s\'', $object), E_USER_ERROR);
         return false;
     }
     return true;
 }
예제 #5
0
파일: Mail.php 프로젝트: bitcpf/djangoage
 /**
  * Parse a MIME part and set the Message object accordingly.
  *
  * @param resource $part A MIME part, returned from mailparse_msg_get_part,
  *    to be parse.
  * @param string $raw_mail The string holding the raw content of the email
  *    $part is extracted from.
  * @param Message& $email The Message object to be set.
  */
 private static function parseMimePart($part, $raw_mail, &$email)
 {
     $data = mailparse_msg_get_part_data($part);
     $type = ArrayUtil::findByKeyOrDefault($data, 'content-type', 'text/plain');
     $start = $data['starting-pos-body'];
     $end = $data['ending-pos-body'];
     $encoding = ArrayUtil::findByKeyOrDefault($data, 'transfer-encoding', '');
     $content = self::decodeContent(substr($raw_mail, $start, $end - $start), $encoding);
     if (isset($data['content-disposition'])) {
         $filename = ArrayUtil::findByKeyOrDefault($data, 'disposition-filename', uniqid());
         $content_id = ArrayUtil::findByKeyOrNull($data, 'content-id');
         if ($content_id != null) {
             $content_id = "<{$content_id}>";
         }
         $email->addAttachment($filename, $content, $content_id);
     } else {
         if ($type == 'text/html') {
             $email->setHtmlBody($content);
         } else {
             if ($type == 'text/plain') {
                 $email->setTextBody($content);
             } else {
                 if (!StringUtil::startsWith($type, 'multipart/')) {
                     trigger_error("Ignore MIME part with unknown Content-Type {$type}. " . "Did you forget to specifcy Content-Disposition header?", E_USER_WARNING);
                 }
             }
         }
     }
 }
예제 #6
0
 /**
  * Parse and extract the bucket and object names from the supplied filename.
  *
  * @param string $filename The filename in the format gs://bucket_name or
  * gs://bucket_name/object_name.
  * @param string &$bucket The extracted bucket.
  * @param string &$object The extracted object. Can be null if the filename
  * contains only bucket name.
  *
  * @return bool true if the filename is successfully parsed, false otherwise.
  */
 public static function parseFilename($filename, &$bucket, &$object)
 {
     $bucket = null;
     $object = null;
     // $filename may contain nasty characters like # and ? that can throw off
     // parse_url(). It is best to do a manual parse here.
     $gs_prefix_len = strlen(self::GS_PREFIX);
     if (!StringUtil::startsWith($filename, self::GS_PREFIX)) {
         return false;
     }
     $first_slash_pos = strpos($filename, '/', $gs_prefix_len);
     if ($first_slash_pos === false) {
         $bucket = substr($filename, $gs_prefix_len);
     } else {
         $bucket = substr($filename, $gs_prefix_len, $first_slash_pos - $gs_prefix_len);
         // gs://bucket_name/ is treated the same as gs://bucket_name where
         // $object should be set to null.
         if ($first_slash_pos != strlen($filename) - 1) {
             $object = substr($filename, $first_slash_pos);
         }
     }
     if (strlen($bucket) == 0) {
         return false;
     }
     // Substitute default bucket name.
     if (ini_get('google_app_engine.gcs_default_keyword')) {
         if ($bucket === self::GS_DEFAULT_BUCKET_KEYWORD) {
             $bucket = self::getDefaultGoogleStorageBucketName();
             if (!$bucket) {
                 throw new \InvalidArgumentException('Application does not have a default Cloud Storage Bucket, ' . 'must specify a bucket name');
             }
         }
     }
     // Validate bucket & object names.
     if (self::validateBucketName($bucket) === false) {
         trigger_error(sprintf('Invalid cloud storage bucket name \'%s\'', $bucket), E_USER_ERROR);
         return false;
     }
     if (isset($object) && self::validateObjectName($object) === false) {
         trigger_error(sprintf('Invalid cloud storage object name \'%s\'', $object), E_USER_ERROR);
         return false;
     }
     return true;
 }