public function lintPath($path) { if (is_executable($path)) { if ($this->getEngine()->isBinaryFile($path)) { $mime = Filesystem::getMimeType($path); switch ($mime) { // Archives case 'application/jar': case 'application/java-archive': case 'application/x-bzip2': case 'application/x-gzip': case 'application/x-rar-compressed': case 'application/x-tar': case 'application/zip': // Audio // Audio case 'audio/midi': case 'audio/mpeg': case 'audio/mp4': case 'audio/x-wav': // Fonts // Fonts case 'application/vnd.ms-fontobject': case 'application/x-font-ttf': case 'application/x-woff': // Images // Images case 'application/x-shockwave-flash': case 'image/gif': case 'image/jpeg': case 'image/png': case 'image/tiff': case 'image/x-icon': case 'image/x-ms-bmp': // Miscellaneous // Miscellaneous case 'application/msword': case 'application/pdf': case 'application/postscript': case 'application/rtf': case 'application/vnd.ms-excel': case 'application/vnd.ms-powerpoint': // Video // Video case 'video/mpeg': case 'video/quicktime': case 'video/x-flv': case 'video/x-msvideo': case 'video/x-ms-wmv': $this->raiseLintAtPath(self::LINT_INVALID_EXECUTABLE, pht("'%s' files should not be executable.", $mime)); return; } // Path is a binary file, which makes it a valid executable. return; } else { if ($this->getShebang($path)) { // Path contains a shebang, which makes it a valid executable. return; } else { $this->raiseLintAtPath(self::LINT_INVALID_EXECUTABLE, pht('Executable files should either be binary or contain a shebang.')); } } } }
public function execute(PhutilArgumentParser $args) { $console = PhutilConsole::getConsole(); $iterator = $this->buildIterator($args); if (!$iterator) { throw new PhutilArgumentUsageException('Either specify a list of files to update, or use `--all` ' . 'to update all files.'); } $update = array('mime' => $args->getArg('rebuild-mime'), 'dimensions' => $args->getArg('rebuild-dimensions')); // If the user didn't select anything, rebuild everything. if (!array_filter($update)) { foreach ($update as $key => $ignored) { $update[$key] = true; } } $is_dry_run = $args->getArg('dry-run'); $failed = array(); foreach ($iterator as $file) { $fid = 'F' . $file->getID(); if ($update['mime']) { $tmp = new TempFile(); Filesystem::writeFile($tmp, $file->loadFileData()); $new_type = Filesystem::getMimeType($tmp); if ($new_type == $file->getMimeType()) { $console->writeOut("%s: Mime type not changed (%s).\n", $fid, $new_type); } else { if ($is_dry_run) { $console->writeOut("%s: Would update Mime type: '%s' -> '%s'.\n", $fid, $file->getMimeType(), $new_type); } else { $console->writeOut("%s: Updating Mime type: '%s' -> '%s'.\n", $fid, $file->getMimeType(), $new_type); $file->setMimeType($new_type); $file->save(); } } } if ($update['dimensions']) { if (!$file->isViewableImage()) { $console->writeOut("%s: Not an image file.\n", $fid); continue; } $metadata = $file->getMetadata(); $image_width = idx($metadata, PhabricatorFile::METADATA_IMAGE_WIDTH); $image_height = idx($metadata, PhabricatorFile::METADATA_IMAGE_HEIGHT); if ($image_width && $image_height) { $console->writeOut("%s: Image dimensions already exist.\n", $fid); continue; } if ($is_dry_run) { $console->writeOut("%s: Would update file dimensions (dry run)\n", $fid); continue; } $console->writeOut('%s: Updating metadata... ', $fid); try { $file->updateDimensions(); $console->writeOut("done.\n"); } catch (Exception $ex) { $console->writeOut("failed!\n"); $console->writeErr("%s\n", (string) $ex); $failed[] = $file; } } } if ($failed) { $console->writeOut("**Failures!**\n"); $ids = array(); foreach ($failed as $file) { $ids[] = 'F' . $file->getID(); } $console->writeOut("%s\n", implode(', ', $ids)); return 1; } else { $console->writeOut("**Success!**\n"); return 0; } return 0; }
private function getFileMimeType($data) { $tmp = new TempFile(); Filesystem::writeFile($tmp, $data); return Filesystem::getMimeType($tmp); }
private function uploadFile($data, $name, $desc) { $result = array('guid' => null, 'mime' => null, 'size' => null); if ($this->getArgument('skip-binaries')) { return $result; } $result['size'] = $size = strlen($data); if (!$size) { return $result; } $tmp = new TempFile(); Filesystem::writeFile($tmp, $data); $mime_type = Filesystem::getMimeType($tmp); $result['mime'] = $mime_type; echo "Uploading {$desc} '{$name}' ({$mime_type}, {$size} bytes)...\n"; try { $guid = $this->getConduit()->callMethodSynchronous('file.upload', array('data_base64' => base64_encode($data), 'name' => $name)); $result['guid'] = $guid; } catch (Exception $e) { echo "Failed to upload {$desc} '{$name}'.\n"; if (!phutil_console_confirm('Continue?', $default_no = false)) { throw new ArcanistUsageException('Aborted due to file upload failure. You can use --skip-binaries ' . 'to skip binary uploads.'); } } return $result; }
public static function newFromFileData($data, array $params = array()) { $selector = PhabricatorEnv::newObjectFromConfig('storage.engine-selector'); $engines = $selector->selectStorageEngines($data, $params); if (!$engines) { throw new Exception("No valid storage engines are available!"); } $data_handle = null; $engine_identifier = null; $exceptions = array(); foreach ($engines as $engine) { $engine_class = get_class($engine); try { // Perform the actual write. $data_handle = $engine->writeFile($data, $params); if (!$data_handle || strlen($data_handle) > 255) { // This indicates an improperly implemented storage engine. throw new PhabricatorFileStorageConfigurationException("Storage engine '{$engine_class}' executed writeFile() but did " . "not return a valid handle ('{$data_handle}') to the data: it " . "must be nonempty and no longer than 255 characters."); } $engine_identifier = $engine->getEngineIdentifier(); if (!$engine_identifier || strlen($engine_identifier) > 32) { throw new PhabricatorFileStorageConfigurationException("Storage engine '{$engine_class}' returned an improper engine " . "identifier '{$engine_identifier}': it must be nonempty " . "and no longer than 32 characters."); } // We stored the file somewhere so stop trying to write it to other // places. break; } catch (Exception $ex) { if ($ex instanceof PhabricatorFileStorageConfigurationException) { // If an engine is outright misconfigured (or misimplemented), raise // that immediately since it probably needs attention. throw $ex; } // If an engine doesn't work, keep trying all the other valid engines // in case something else works. phlog($ex); $exceptions[] = $ex; } } if (!$data_handle) { throw new PhutilAggregateException("All storage engines failed to write file:", $exceptions); } $file_name = idx($params, 'name'); $file_name = self::normalizeFileName($file_name); // If for whatever reason, authorPHID isn't passed as a param // (always the case with newFromFileDownload()), store a '' $authorPHID = idx($params, 'authorPHID'); $file = new PhabricatorFile(); $file->setName($file_name); $file->setByteSize(strlen($data)); $file->setAuthorPHID($authorPHID); $file->setContentHash(PhabricatorHash::digest($data)); $file->setStorageEngine($engine_identifier); $file->setStorageHandle($data_handle); // TODO: This is probably YAGNI, but allows for us to do encryption or // compression later if we want. $file->setStorageFormat(self::STORAGE_FORMAT_RAW); if (isset($params['mime-type'])) { $file->setMimeType($params['mime-type']); } else { $tmp = new TempFile(); Filesystem::writeFile($tmp, $data); $file->setMimeType(Filesystem::getMimeType($tmp)); } $file->save(); return $file; }
#!/usr/bin/env php <?php $root = dirname(dirname(dirname(__FILE__))); require_once $root . '/scripts/__init_script__.php'; $args = new PhutilArgumentParser($argv); $args->setTagline('test Filesystem::getMimeType()'); $args->setSynopsis(<<<EOHELP **mime.php** [__options__] __file__ Determine the mime type of a file. EOHELP ); $args->parseStandardArguments(); $args->parse(array(array('name' => 'default', 'param' => 'mimetype', 'help' => 'Use __mimetype__ as default instead of builtin default.'), array('name' => 'file', 'wildcard' => true))); $file = $args->getArg('file'); if (count($file) !== 1) { $args->printHelpAndExit(); } $file = reset($file); $default = $args->getArg('default'); if ($default) { echo Filesystem::getMimeType($file, $default) . "\n"; } else { echo Filesystem::getMimeType($file) . "\n"; }
private static function buildFromFileData($data, array $params = array()) { if (isset($params['storageEngines'])) { $engines = $params['storageEngines']; } else { $size = strlen($data); $engines = PhabricatorFileStorageEngine::loadStorageEngines($size); if (!$engines) { throw new Exception(pht('No configured storage engine can store this file. See ' . '"Configuring File Storage" in the documentation for ' . 'information on configuring storage engines.')); } } assert_instances_of($engines, 'PhabricatorFileStorageEngine'); if (!$engines) { throw new Exception(pht('No valid storage engines are available!')); } $file = self::initializeNewFile(); $data_handle = null; $engine_identifier = null; $exceptions = array(); foreach ($engines as $engine) { $engine_class = get_class($engine); try { list($engine_identifier, $data_handle) = $file->writeToEngine($engine, $data, $params); // We stored the file somewhere so stop trying to write it to other // places. break; } catch (PhabricatorFileStorageConfigurationException $ex) { // If an engine is outright misconfigured (or misimplemented), raise // that immediately since it probably needs attention. throw $ex; } catch (Exception $ex) { phlog($ex); // If an engine doesn't work, keep trying all the other valid engines // in case something else works. $exceptions[$engine_class] = $ex; } } if (!$data_handle) { throw new PhutilAggregateException(pht('All storage engines failed to write file:'), $exceptions); } $file->setByteSize(strlen($data)); $file->setContentHash(self::hashFileContent($data)); $file->setStorageEngine($engine_identifier); $file->setStorageHandle($data_handle); // TODO: This is probably YAGNI, but allows for us to do encryption or // compression later if we want. $file->setStorageFormat(self::STORAGE_FORMAT_RAW); $file->readPropertiesFromParameters($params); if (!$file->getMimeType()) { $tmp = new TempFile(); Filesystem::writeFile($tmp, $data); $file->setMimeType(Filesystem::getMimeType($tmp)); } try { $file->updateDimensions(false); } catch (Exception $ex) { // Do nothing } $file->save(); return $file; }
public function execute(PhutilArgumentParser $args) { $console = PhutilConsole::getConsole(); $viewer = $this->getViewer(); $from = $args->getArg('from'); if ($from) { $user = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withUsernames(array($from))->executeOne(); if (!$user) { throw new PhutilArgumentUsageException(pht("No such user '%s' exists.", $from)); } $from = $user; } $tos = $args->getArg('to'); $ccs = $args->getArg('cc'); if (!$tos && !$ccs) { throw new PhutilArgumentUsageException(pht('Specify one or more users to send mail to with `%s` and `%s`.', '--to', '--cc')); } $names = array_merge($tos, $ccs); $users = id(new PhabricatorPeopleQuery())->setViewer($viewer)->withUsernames($names)->execute(); $users = mpull($users, null, 'getUsername'); foreach ($tos as $key => $username) { if (empty($users[$username])) { throw new PhutilArgumentUsageException(pht("No such user '%s' exists.", $username)); } $tos[$key] = $users[$username]->getPHID(); } foreach ($ccs as $key => $username) { if (empty($users[$username])) { throw new PhutilArgumentUsageException(pht("No such user '%s' exists.", $username)); } $ccs[$key] = $users[$username]->getPHID(); } $subject = $args->getArg('subject'); if ($subject === null) { $subject = pht('No Subject'); } $tags = $args->getArg('tag'); $attach = $args->getArg('attach'); $is_bulk = $args->getArg('bulk'); $console->writeErr("%s\n", pht('Reading message body from stdin...')); $body = file_get_contents('php://stdin'); $mail = id(new PhabricatorMetaMTAMail())->addTos($tos)->addCCs($ccs)->setSubject($subject)->setBody($body)->setIsBulk($is_bulk)->setMailTags($tags); if ($args->getArg('html')) { $mail->setBody(pht('(This is a placeholder plaintext email body for a test message ' . 'sent with %s.)', '--html')); $mail->setHTMLBody($body); } else { $mail->setBody($body); } if ($from) { $mail->setFrom($from->getPHID()); } foreach ($attach as $attachment) { $data = Filesystem::readFile($attachment); $name = basename($attachment); $mime = Filesystem::getMimeType($attachment); $file = new PhabricatorMetaMTAAttachment($data, $name, $mime); $mail->addAttachment($file); } PhabricatorWorker::setRunAllTasksInProcess(true); $mail->save(); $console->writeErr("%s\n\n phabricator/ \$ ./bin/mail show-outbound --id %d\n\n", pht('Mail sent! You can view details by running this command:'), $mail->getID()); }
#!/usr/bin/env php <?php require_once dirname(__FILE__) . '/../__init_script__.php'; $args = new PhutilArgumentParser($argv); $args->parseStandardArguments(); $args->parse(array(array('name' => 'attach', 'param' => 'file', 'help' => pht('Attach a file to the request.')), array('name' => 'url', 'wildcard' => true))); $uri = $args->getArg('url'); if (count($uri) !== 1) { throw new PhutilArgumentUsageException('Specify exactly one URL to retrieve.'); } $uri = head($uri); $method = 'GET'; $data = ''; $timeout = 30; $future = id(new HTTPSFuture($uri, $data))->setMethod($method)->setTimeout($timeout); $attach_file = $args->getArg('attach'); if ($attach_file !== null) { $future->attachFileData('file', Filesystem::readFile($attach_file), basename($attach_file), Filesystem::getMimeType($attach_file)); } print_r($future->resolve());
private static function buildFromFileData($data, array $params = array()) { if (isset($params['storageEngines'])) { $engines = $params['storageEngines']; } else { $size = strlen($data); $engines = PhabricatorFileStorageEngine::loadStorageEngines($size); if (!$engines) { throw new Exception(pht('No configured storage engine can store this file. See ' . '"Configuring File Storage" in the documentation for ' . 'information on configuring storage engines.')); } } assert_instances_of($engines, 'PhabricatorFileStorageEngine'); if (!$engines) { throw new Exception(pht('No valid storage engines are available!')); } $file = self::initializeNewFile(); $aes_type = PhabricatorFileAES256StorageFormat::FORMATKEY; $has_aes = PhabricatorKeyring::getDefaultKeyName($aes_type); if ($has_aes !== null) { $default_key = PhabricatorFileAES256StorageFormat::FORMATKEY; } else { $default_key = PhabricatorFileRawStorageFormat::FORMATKEY; } $key = idx($params, 'format', $default_key); // Callers can pass in an object explicitly instead of a key. This is // primarily useful for unit tests. if ($key instanceof PhabricatorFileStorageFormat) { $format = clone $key; } else { $format = clone PhabricatorFileStorageFormat::requireFormat($key); } $format->setFile($file); $properties = $format->newStorageProperties(); $file->setStorageFormat($format->getStorageFormatKey()); $file->setStorageProperties($properties); $data_handle = null; $engine_identifier = null; $exceptions = array(); foreach ($engines as $engine) { $engine_class = get_class($engine); try { list($engine_identifier, $data_handle) = $file->writeToEngine($engine, $data, $params); // We stored the file somewhere so stop trying to write it to other // places. break; } catch (PhabricatorFileStorageConfigurationException $ex) { // If an engine is outright misconfigured (or misimplemented), raise // that immediately since it probably needs attention. throw $ex; } catch (Exception $ex) { phlog($ex); // If an engine doesn't work, keep trying all the other valid engines // in case something else works. $exceptions[$engine_class] = $ex; } } if (!$data_handle) { throw new PhutilAggregateException(pht('All storage engines failed to write file:'), $exceptions); } $file->setByteSize(strlen($data)); $file->setContentHash(self::hashFileContent($data)); $file->setStorageEngine($engine_identifier); $file->setStorageHandle($data_handle); $file->readPropertiesFromParameters($params); if (!$file->getMimeType()) { $tmp = new TempFile(); Filesystem::writeFile($tmp, $data); $file->setMimeType(Filesystem::getMimeType($tmp)); } try { $file->updateDimensions(false); } catch (Exception $ex) { // Do nothing } $file->save(); return $file; }