mediafile.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Abstraction for media files
  18. *
  19. * @category Media
  20. * @package GNUsocial
  21. *
  22. * @author Robin Millette <robin@millette.info>
  23. * @author Miguel Dantas <biodantas@gmail.com>
  24. * @author Zach Copley <zach@status.net>
  25. * @author Mikael Nordfeldth <mmn@hethane.se>
  26. * @author Diogo Cordeiro <diogo@fc.up.pt>
  27. * @copyright 2008-2009, 2019-2020 Free Software Foundation http://fsf.org
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. */
  30. defined('GNUSOCIAL') || die();
  31. /**
  32. * Class responsible for abstracting media files
  33. */
  34. class MediaFile
  35. {
  36. public $id;
  37. public $filepath;
  38. public $filename;
  39. public $fileRecord;
  40. public $fileurl;
  41. public $short_fileurl;
  42. public $mimetype;
  43. /**
  44. * MediaFile constructor.
  45. *
  46. * @param string $filepath The path of the file this media refers to. Required
  47. * @param string $mimetype The mimetype of the file. Required
  48. * @param string|null $filehash The hash of the file, if known. Optional
  49. * @param int|null $id The DB id of the file. Int if known, null if not.
  50. * If null, it searches for it. If -1, it skips all DB
  51. * interactions (useful for temporary objects)
  52. *
  53. * @throws ClientException
  54. * @throws NoResultException
  55. * @throws ServerException
  56. */
  57. public function __construct(string $filepath, string $mimetype, ?string $filehash = null, ?int $id = null)
  58. {
  59. $this->filepath = $filepath;
  60. $this->filename = basename($this->filepath);
  61. $this->mimetype = $mimetype;
  62. $this->filehash = self::getHashOfFile($this->filepath, $filehash);
  63. $this->id = $id;
  64. // If id is -1, it means we're dealing with a temporary object and don't want to store it in the DB,
  65. // or add redirects
  66. if ($this->id !== -1) {
  67. if (!empty($this->id)) {
  68. // If we have an id, load it
  69. $this->fileRecord = new File();
  70. $this->fileRecord->id = $this->id;
  71. if (!$this->fileRecord->find(true)) {
  72. // If we have set an ID, we need that ID to exist!
  73. throw new NoResultException($this->fileRecord);
  74. }
  75. } else {
  76. // Otherwise, store it
  77. $this->fileRecord = $this->storeFile();
  78. }
  79. $this->fileurl = common_local_url(
  80. 'attachment',
  81. ['attachment' => $this->fileRecord->id]
  82. );
  83. $this->short_fileurl = common_shorten_url($this->fileurl);
  84. }
  85. }
  86. public function attachToNotice(Notice $notice)
  87. {
  88. File_to_post::processNew($this->fileRecord, $notice);
  89. }
  90. public function getPath()
  91. {
  92. return File::path($this->filename);
  93. }
  94. public function shortUrl()
  95. {
  96. return $this->short_fileurl;
  97. }
  98. public function getEnclosure()
  99. {
  100. return $this->getFile()->getEnclosure();
  101. }
  102. public function delete()
  103. {
  104. @unlink($this->filepath);
  105. }
  106. public function getFile()
  107. {
  108. if (!$this->fileRecord instanceof File) {
  109. throw new ServerException('File record did not exist for MediaFile');
  110. }
  111. return $this->fileRecord;
  112. }
  113. /**
  114. * Calculate the hash of a file.
  115. *
  116. * This won't work for files >2GiB because PHP uses only 32bit.
  117. *
  118. * @param string $filepath
  119. * @param null|string $filehash
  120. *
  121. * @return string
  122. * @throws ServerException
  123. *
  124. */
  125. public static function getHashOfFile(string $filepath, $filehash = null)
  126. {
  127. assert(!empty($filepath), __METHOD__ . ': filepath cannot be null');
  128. if ($filehash === null) {
  129. // Calculate if we have an older upload method somewhere (Qvitter) that
  130. // doesn't do this before calling new MediaFile on its local files...
  131. $filehash = hash_file(File::FILEHASH_ALG, $filepath);
  132. if ($filehash === false) {
  133. throw new ServerException('Could not read file for hashing');
  134. }
  135. }
  136. return $filehash;
  137. }
  138. /**
  139. * Retrieve or insert as a file in the DB
  140. *
  141. * @return object File
  142. * @throws ServerException
  143. *
  144. * @throws ClientException
  145. */
  146. protected function storeFile()
  147. {
  148. try {
  149. $file = File::getByHash($this->filehash);
  150. // We're done here. Yes. Already. We assume sha256 won't collide on us anytime soon.
  151. return $file;
  152. } catch (NoResultException $e) {
  153. // Well, let's just continue below.
  154. }
  155. $fileurl = common_local_url('attachment_view', ['filehash' => $this->filehash]);
  156. $file = new File;
  157. $file->filename = $this->filename;
  158. $file->urlhash = File::hashurl($fileurl);
  159. $file->url = $fileurl;
  160. $file->filehash = $this->filehash;
  161. $file->size = filesize($this->filepath);
  162. if ($file->size === false) {
  163. throw new ServerException('Could not read file to get its size');
  164. }
  165. $file->date = time();
  166. $file->mimetype = $this->mimetype;
  167. $file_id = $file->insert();
  168. if ($file_id === false) {
  169. common_log_db_error($file, 'INSERT', __FILE__);
  170. // TRANS: Client exception thrown when a database error was thrown during a file upload operation.
  171. throw new ClientException(_m('There was a database error while saving your file. Please try again.'));
  172. }
  173. // Set file geometrical properties if available
  174. try {
  175. $image = ImageFile::fromFileObject($file);
  176. $orig = clone $file;
  177. $file->width = $image->width;
  178. $file->height = $image->height;
  179. $file->update($orig);
  180. // We have to cleanup after ImageFile, since it
  181. // may have generated a temporary file from a
  182. // video support plugin or something.
  183. // FIXME: Do this more automagically.
  184. if ($image->getPath() != $file->getPath()) {
  185. $image->unlink();
  186. }
  187. } catch (ServerException $e) {
  188. // We just couldn't make out an image from the file. This
  189. // does not have to be UnsupportedMediaException, as we can
  190. // also get ServerException from files not existing etc.
  191. }
  192. return $file;
  193. }
  194. /**
  195. * The maximum allowed file size, as a string
  196. */
  197. public static function maxFileSize()
  198. {
  199. $value = self::maxFileSizeInt();
  200. if ($value > 1024 * 1024) {
  201. $value = $value / (1024 * 1024);
  202. // TRANS: Number of megabytes. %d is the number.
  203. return sprintf(_m('%dMB', '%dMB', $value), $value);
  204. } elseif ($value > 1024) {
  205. $value = $value / 1024;
  206. // TRANS: Number of kilobytes. %d is the number.
  207. return sprintf(_m('%dkB', '%dkB', $value), $value);
  208. } else {
  209. // TRANS: Number of bytes. %d is the number.
  210. return sprintf(_m('%dB', '%dB', $value), $value);
  211. }
  212. }
  213. /**
  214. * The maximum allowed file size, as an int
  215. */
  216. public static function maxFileSizeInt(): int
  217. {
  218. return common_config('attachments', 'file_quota');
  219. }
  220. /**
  221. * Encodes a file name and a file hash in the new file format, which is used to avoid
  222. * having an extension in the file, removing trust in extensions, while keeping the original name
  223. *
  224. * @param null|string $original_name
  225. * @param string $filehash
  226. * @param null|string|bool $ext from File::getSafeExtension
  227. *
  228. * @return string
  229. * @throws ClientException
  230. * @throws ServerException
  231. */
  232. public static function encodeFilename($original_name, string $filehash, $ext = null): string
  233. {
  234. if (empty($original_name)) {
  235. $original_name = _m('Untitled attachment');
  236. }
  237. // If we're given an extension explicitly, use it, otherwise...
  238. $ext = $ext ?:
  239. // get a replacement extension if configured, returns false if it's blocked,
  240. // null if no extension
  241. File::getSafeExtension($original_name);
  242. if ($ext === false) {
  243. throw new ClientException(_m('Blacklisted file extension.'));
  244. }
  245. if (!empty($ext)) {
  246. // Remove dots if we have them (make sure they're not repeated)
  247. $ext = preg_replace('/^\.+/', '', $ext);
  248. $original_name = preg_replace('/\.+.+$/i', ".{$ext}", $original_name);
  249. }
  250. $enc_name = bin2hex($original_name);
  251. return "{$enc_name}-{$filehash}";
  252. }
  253. /**
  254. * Decode the new filename format
  255. *
  256. * @return false | null | string on failure, no match (old format) or original file name, respectively
  257. */
  258. public static function decodeFilename(string $encoded_filename)
  259. {
  260. // Should match:
  261. // hex-hash
  262. // thumb-id-widthxheight-hex-hash
  263. // And return the `hex` part
  264. $ret = preg_match('/^(.*-)?([^-]+)-[^-]+$/', $encoded_filename, $matches);
  265. if ($ret === false) {
  266. return false;
  267. } elseif ($ret === 0 || !ctype_xdigit($matches[2])) {
  268. return null; // No match
  269. } else {
  270. $filename = hex2bin($matches[2]);
  271. // Matches extension
  272. if (preg_match('/^(.+?)\.(.+)$/', $filename, $sub_matches) === 1) {
  273. $ext = $sub_matches[2];
  274. // Previously, there was a blacklisted extension array, which could have an alternative
  275. // extension, such as phps, to replace php. We want to turn it back (this is deprecated,
  276. // as it no longer makes sense, since we don't trust trust files based on extension,
  277. // but keep the feature)
  278. $blacklist = common_config('attachments', 'extblacklist');
  279. if (is_array($blacklist)) {
  280. foreach ($blacklist as $upload_ext => $safe_ext) {
  281. if ($ext === $safe_ext) {
  282. $ext = $upload_ext;
  283. break;
  284. }
  285. }
  286. }
  287. return "{$sub_matches[1]}.{$ext}";
  288. } else {
  289. // No extension, don't bother trying to replace it
  290. return $filename;
  291. }
  292. }
  293. }
  294. /**
  295. * Create a new MediaFile or ImageFile object from an upload
  296. *
  297. * Tries to set the mimetype correctly, using the most secure method available and rejects the file otherwise.
  298. * In case the upload is an image, this function returns an new ImageFile (which extends MediaFile)
  299. * The filename has a new format:
  300. * bin2hex("{$original_name}.{$ext}")."-{$filehash}"
  301. * This format should be respected. Notice the dash, which is important to distinguish it from the previous
  302. * format ("{$hash}.{$ext}")
  303. *
  304. * @param string $param Form name
  305. * @param Profile|null $scoped
  306. * @return ImageFile|MediaFile
  307. * @throws ClientException
  308. * @throws InvalidFilenameException
  309. * @throws NoResultException
  310. * @throws NoUploadedMediaException
  311. * @throws ServerException
  312. * @throws UnsupportedMediaException
  313. * @throws UseFileAsThumbnailException
  314. */
  315. public static function fromUpload(string $param = 'media', ?Profile $scoped = null)
  316. {
  317. // The existence of the "error" element means PHP has processed it properly even if it was ok.
  318. if (!(isset($_FILES[$param], $_FILES[$param]['error']))) {
  319. throw new NoUploadedMediaException($param);
  320. }
  321. switch ($_FILES[$param]['error']) {
  322. case UPLOAD_ERR_OK: // success, jump out
  323. break;
  324. case UPLOAD_ERR_INI_SIZE:
  325. case UPLOAD_ERR_FORM_SIZE:
  326. // TRANS: Exception thrown when too large a file is uploaded.
  327. // TRANS: %s is the maximum file size, for example "500b", "10kB" or "2MB".
  328. throw new ClientException(sprintf(
  329. _m('That file is too big. The maximum file size is %s.'),
  330. self::maxFileSize()
  331. ));
  332. case UPLOAD_ERR_PARTIAL:
  333. @unlink($_FILES[$param]['tmp_name']);
  334. // TRANS: Client exception.
  335. throw new ClientException(_m('The uploaded file was only partially uploaded.'));
  336. case UPLOAD_ERR_NO_FILE:
  337. // No file; probably just a non-AJAX submission.
  338. throw new NoUploadedMediaException($param);
  339. case UPLOAD_ERR_NO_TMP_DIR:
  340. // TRANS: Client exception thrown when a temporary folder is not present to store a file upload.
  341. throw new ClientException(_m('Missing a temporary folder.'));
  342. case UPLOAD_ERR_CANT_WRITE:
  343. // TRANS: Client exception thrown when writing to disk is not possible during a file upload operation.
  344. throw new ClientException(_m('Failed to write file to disk.'));
  345. case UPLOAD_ERR_EXTENSION:
  346. // TRANS: Client exception thrown when a file upload operation has been stopped by an extension.
  347. throw new ClientException(_m('File upload stopped by extension.'));
  348. default:
  349. common_log(LOG_ERR, __METHOD__ . ': Unknown upload error ' . $_FILES[$param]['error']);
  350. // TRANS: Client exception thrown when a file upload operation has failed with an unknown reason.
  351. throw new ClientException(_m('System error uploading file.'));
  352. }
  353. $filehash = strtolower(self::getHashOfFile($_FILES[$param]['tmp_name']));
  354. try {
  355. $file = File::getByHash($filehash);
  356. // If no exception is thrown the file exists locally, so we'll use that and just add redirections.
  357. // but if the _actual_ locally stored file doesn't exist, getPath will throw FileNotFoundException
  358. $filepath = $file->getPath();
  359. $mimetype = $file->mimetype;
  360. } catch (FileNotFoundException | NoResultException $e) {
  361. // We have to save the upload as a new local file. This is the normal course of action.
  362. if ($scoped instanceof Profile) {
  363. // Throws exception if additional size does not respect quota
  364. // This test is only needed, of course, if we're uploading something new.
  365. File::respectsQuota($scoped, $_FILES[$param]['size']);
  366. }
  367. $mimetype = self::getUploadedMimeType($_FILES[$param]['tmp_name'], $_FILES[$param]['name']);
  368. $media = common_get_mime_media($mimetype);
  369. $basename = basename($_FILES[$param]['name']);
  370. if ($media == 'image') {
  371. // Use -1 for the id to avoid adding this temporary file to the DB
  372. $img = new ImageFile(-1, $_FILES[$param]['tmp_name']);
  373. // Validate the image by re-encoding it. Additionally normalizes old formats to WebP,
  374. // keeping GIF untouched if animated
  375. $outpath = $img->resizeTo($img->filepath);
  376. $ext = image_type_to_extension($img->preferredType(), false);
  377. }
  378. $filename = self::encodeFilename($basename, $filehash, isset($ext) ? $ext : File::getSafeExtension($basename));
  379. $filepath = File::path($filename);
  380. if ($media == 'image') {
  381. $result = rename($outpath, $filepath);
  382. } else {
  383. $result = move_uploaded_file($_FILES[$param]['tmp_name'], $filepath);
  384. }
  385. if (!$result) {
  386. // TRANS: Client exception thrown when a file upload operation fails because the file could
  387. // TRANS: not be moved from the temporary folder to the permanent file location.
  388. // UX: too specific
  389. throw new ClientException(_m('File could not be moved to destination directory.'));
  390. }
  391. if ($media == 'image') {
  392. return new ImageFile(null, $filepath);
  393. }
  394. }
  395. return new self($filepath, $mimetype, $filehash);
  396. }
  397. /**
  398. * Create a new MediaFile or ImageFile object from an url
  399. *
  400. * Tries to set the mimetype correctly, using the most secure method available and rejects the file otherwise.
  401. * In case the url is an image, this function returns an new ImageFile (which extends MediaFile)
  402. * The filename has the following format: bin2hex("{$original_name}.{$ext}")."-{$filehash}"
  403. *
  404. * @param string $url Remote media URL
  405. * @param Profile|null $scoped
  406. * @param string|null $name
  407. * @return ImageFile|MediaFile
  408. * @throws ClientException
  409. * @throws FileNotFoundException
  410. * @throws InvalidFilenameException
  411. * @throws NoResultException
  412. * @throws ServerException
  413. * @throws UnsupportedMediaException
  414. * @throws UseFileAsThumbnailException
  415. */
  416. public static function fromUrl(string $url, ?Profile $scoped = null, ?string $name = null)
  417. {
  418. if (!common_valid_http_url($url)) {
  419. // TRANS: Server exception. %s is a URL.
  420. throw new ServerException(sprintf('Invalid remote media URL %s.', $url));
  421. }
  422. $temp_filename = tempnam(sys_get_temp_dir(), 'tmp' . common_timestamp());
  423. try {
  424. $fileData = HTTPClient::quickGet($url);
  425. file_put_contents($temp_filename, $fileData);
  426. unset($fileData); // No need to carry this in memory.
  427. $filehash = strtolower(self::getHashOfFile($temp_filename));
  428. try {
  429. $file = File::getByHash($filehash);
  430. // If no exception is thrown the file exists locally, so we'll use that and just add redirections.
  431. // but if the _actual_ locally stored file doesn't exist, getPath will throw FileNotFoundException
  432. $filepath = $file->getPath();
  433. $mimetype = $file->mimetype;
  434. } catch (FileNotFoundException | NoResultException $e) {
  435. // We have to save the downloaded as a new local file. This is the normal course of action.
  436. if ($scoped instanceof Profile) {
  437. // Throws exception if additional size does not respect quota
  438. // This test is only needed, of course, if we're uploading something new.
  439. File::respectsQuota($scoped, filesize($temp_filename));
  440. }
  441. $mimetype = self::getUploadedMimeType($temp_filename, $name ?? false);
  442. $media = common_get_mime_media($mimetype);
  443. $basename = basename($name ?? $temp_filename);
  444. if ($media == 'image') {
  445. // Use -1 for the id to avoid adding this temporary file to the DB
  446. $img = new ImageFile(-1, $temp_filename);
  447. // Validate the image by re-encoding it. Additionally normalizes old formats to PNG,
  448. // keeping JPEG and GIF untouched
  449. $outpath = $img->resizeTo($img->filepath);
  450. $ext = image_type_to_extension($img->preferredType(), false);
  451. }
  452. $filename = self::encodeFilename($basename, $filehash, isset($ext) ? $ext : File::getSafeExtension($basename));
  453. $filepath = File::path($filename);
  454. if ($media == 'image') {
  455. $result = rename($outpath, $filepath);
  456. } else {
  457. $result = rename($temp_filename, $filepath);
  458. }
  459. if (!$result) {
  460. // TRANS: Client exception thrown when a file upload operation fails because the file could
  461. // TRANS: not be moved from the temporary folder to the permanent file location.
  462. throw new ServerException(_m('File could not be moved to destination directory.'));
  463. }
  464. if ($media == 'image') {
  465. return new ImageFile(null, $filepath);
  466. }
  467. }
  468. return new self($filepath, $mimetype, $filehash);
  469. } catch (Exception $e) {
  470. unlink($temp_filename); // Garbage collect
  471. throw $e;
  472. }
  473. }
  474. public static function fromFilehandle($fh, Profile $scoped = null)
  475. {
  476. $stream = stream_get_meta_data($fh);
  477. // So far we're only handling filehandles originating from tmpfile(),
  478. // so we can always do hash_file on $stream['uri'] as far as I can tell!
  479. $filehash = hash_file(File::FILEHASH_ALG, $stream['uri']);
  480. try {
  481. $file = File::getByHash($filehash);
  482. // Already have it, so let's reuse the locally stored File
  483. // by using getPath we also check whether the file exists
  484. // and throw a FileNotFoundException with the path if it doesn't.
  485. $filename = basename($file->getPath());
  486. $mimetype = $file->mimetype;
  487. } catch (FileNotFoundException $e) {
  488. // This happens if the file we have uploaded has disappeared
  489. // from the local filesystem for some reason. Since we got the
  490. // File object from a sha256 check in fromFilehandle, it's safe
  491. // to just copy the uploaded data to disk!
  492. fseek($fh, 0); // just to be sure, go to the beginning
  493. // dump the contents of our filehandle to the path from our exception
  494. // and report error if it failed.
  495. if (false === file_put_contents($e->path, fread($fh, filesize($stream['uri'])))) {
  496. // TRANS: Client exception thrown when a file upload operation fails because the file could
  497. // TRANS: not be moved from the temporary folder to the permanent file location.
  498. throw new ClientException(_m('File could not be moved to destination directory.'));
  499. }
  500. if (!chmod($e->path, 0664)) {
  501. common_log(LOG_ERR, 'Could not chmod uploaded file: ' . _ve($e->path));
  502. }
  503. $filename = basename($file->getPath());
  504. $mimetype = $file->mimetype;
  505. } catch (NoResultException $e) {
  506. if ($scoped instanceof Profile) {
  507. File::respectsQuota($scoped, filesize($stream['uri']));
  508. }
  509. $mimetype = self::getUploadedMimeType($stream['uri']);
  510. $filename = strtolower($filehash) . '.' . File::guessMimeExtension($mimetype);
  511. $filepath = File::path($filename);
  512. $result = copy($stream['uri'], $filepath) && chmod($filepath, 0664);
  513. if (!$result) {
  514. common_log(LOG_ERR, 'File could not be moved (or chmodded) from ' . _ve($stream['uri']) . ' to ' . _ve($filepath));
  515. // TRANS: Client exception thrown when a file upload operation fails because the file could
  516. // TRANS: not be moved from the temporary folder to the permanent file location.
  517. throw new ClientException(_m('File could not be moved to destination directory.'));
  518. }
  519. }
  520. return new self($filename, $mimetype, $filehash);
  521. }
  522. /**
  523. * Attempt to identify the content type of a given file.
  524. *
  525. * @param string $filepath filesystem path as string (file must exist)
  526. * @param bool $originalFilename (optional) for extension-based detection
  527. *
  528. * @return string
  529. *
  530. * @fixme this seems to tie a front-end error message in, kinda confusing
  531. *
  532. * @throws ServerException
  533. *
  534. * @throws ClientException if type is known, but not supported for local uploads
  535. */
  536. public static function getUploadedMimeType(string $filepath, $originalFilename = false)
  537. {
  538. // We only accept filenames to existing files
  539. $mimetype = null;
  540. // From CodeIgniter
  541. // We'll need this to validate the MIME info string (e.g. text/plain; charset=us-ascii)
  542. $regexp = '/^([a-z\-]+\/[a-z0-9\-\.\+]+)(;\s[^\/]+)?$/';
  543. /**
  544. * Fileinfo extension - most reliable method
  545. *
  546. * Apparently XAMPP, CentOS, cPanel and who knows what
  547. * other PHP distribution channels EXPLICITLY DISABLE
  548. * ext/fileinfo, which is otherwise enabled by default
  549. * since PHP 5.3 ...
  550. */
  551. if (function_exists('finfo_file')) {
  552. $finfo = @finfo_open(FILEINFO_MIME);
  553. // It is possible that a FALSE value is returned, if there is no magic MIME database
  554. // file found on the system
  555. if (is_resource($finfo)) {
  556. $mime = @finfo_file($finfo, $filepath);
  557. finfo_close($finfo);
  558. /* According to the comments section of the PHP manual page,
  559. * it is possible that this function returns an empty string
  560. * for some files (e.g. if they don't exist in the magic MIME database)
  561. */
  562. if (is_string($mime) && preg_match($regexp, $mime, $matches)) {
  563. $mimetype = $matches[1];
  564. }
  565. }
  566. }
  567. /* This is an ugly hack, but UNIX-type systems provide a "native" way to detect the file type,
  568. * which is still more secure than depending on the value of $_FILES[$field]['type'], and as it
  569. * was reported in issue #750 (https://github.com/EllisLab/CodeIgniter/issues/750) - it's better
  570. * than mime_content_type() as well, hence the attempts to try calling the command line with
  571. * three different functions.
  572. *
  573. * Notes:
  574. * - the DIRECTORY_SEPARATOR comparison ensures that we're not on a Windows system
  575. * - many system admins would disable the exec(), shell_exec(), popen() and similar functions
  576. * due to security concerns, hence the function_usable() checks
  577. */
  578. if (DIRECTORY_SEPARATOR !== '\\') {
  579. $cmd = 'file --brief --mime ' . escapeshellarg($filepath) . ' 2>&1';
  580. if (empty($mimetype) && function_exists('exec')) {
  581. /* This might look confusing, as $mime is being populated with all of the output
  582. * when set in the second parameter. However, we only need the last line, which is
  583. * the actual return value of exec(), and as such - it overwrites anything that could
  584. * already be set for $mime previously. This effectively makes the second parameter a
  585. * dummy value, which is only put to allow us to get the return status code.
  586. */
  587. $mime = @exec($cmd, $mime, $return_status);
  588. if ($return_status === 0 && is_string($mime) && preg_match($regexp, $mime, $matches)) {
  589. $mimetype = $matches[1];
  590. }
  591. }
  592. if (empty($mimetype) && function_exists('shell_exec')) {
  593. $mime = @shell_exec($cmd);
  594. if (strlen($mime) > 0) {
  595. $mime = explode("\n", trim($mime));
  596. if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)) {
  597. $mimetype = $matches[1];
  598. }
  599. }
  600. }
  601. if (empty($mimetype) && function_exists('popen')) {
  602. $proc = @popen($cmd, 'r');
  603. if (is_resource($proc)) {
  604. $mime = @fread($proc, 512);
  605. @pclose($proc);
  606. if ($mime !== false) {
  607. $mime = explode("\n", trim($mime));
  608. if (preg_match($regexp, $mime[(count($mime) - 1)], $matches)) {
  609. $mimetype = $matches[1];
  610. }
  611. }
  612. }
  613. }
  614. }
  615. // Fall back to mime_content_type(), if available (still better than $_FILES[$field]['type'])
  616. if (empty($mimetype) && function_exists('mime_content_type')) {
  617. $mimetype = @mime_content_type($filepath);
  618. // It's possible that mime_content_type() returns FALSE or an empty string
  619. if ($mimetype == false && strlen($mimetype) > 0) {
  620. throw new ServerException(_m('Could not determine file\'s MIME type.'));
  621. }
  622. }
  623. // Unclear types are such that we can't really tell by the auto
  624. // detect what they are (.bin, .exe etc. are just "octet-stream")
  625. $unclearTypes = ['application/octet-stream',
  626. 'application/vnd.ms-office',
  627. 'application/zip',
  628. 'text/plain',
  629. 'text/html', // Ironically, Wikimedia Commons' SVG_logo.svg is identified as text/html
  630. // TODO: for XML we could do better content-based sniffing too
  631. 'text/xml',];
  632. $supported = common_config('attachments', 'supported');
  633. // If we didn't match, or it is an unclear match
  634. if ($originalFilename && (!$mimetype || in_array($mimetype, $unclearTypes))) {
  635. try {
  636. $type = common_supported_filename_to_mime($originalFilename);
  637. return $type;
  638. } catch (UnknownExtensionMimeException $e) {
  639. // FIXME: I think we should keep the file extension here (supported should be === true here)
  640. } catch (Exception $e) {
  641. // Extension parsed but no connected mimetype, so $mimetype is our best guess
  642. }
  643. }
  644. // If $config['attachments']['supported'] equals boolean true, accept any mimetype
  645. if ($supported === true || array_key_exists($mimetype, $supported)) {
  646. // FIXME: Don't know if it always has a mimetype here because
  647. // finfo->file CAN return false on error: http://php.net/finfo_file
  648. // so if $supported === true, this may return something unexpected.
  649. return $mimetype;
  650. }
  651. // We can conclude that we have failed to get the MIME type
  652. $media = common_get_mime_media($mimetype);
  653. if ('application' !== $media) {
  654. // TRANS: Client exception thrown trying to upload a forbidden MIME type.
  655. // TRANS: %1$s is the file type that was denied, %2$s is the application part of
  656. // TRANS: the MIME type that was denied.
  657. $hint = sprintf(_m('"%1$s" is not a supported file type on this server. ' .
  658. 'Try using another %2$s format.'), $mimetype, $media);
  659. } else {
  660. // TRANS: Client exception thrown trying to upload a forbidden MIME type.
  661. // TRANS: %s is the file type that was denied.
  662. $hint = sprintf(_m('"%s" is not a supported file type on this server.'), $mimetype);
  663. }
  664. throw new ClientException($hint);
  665. }
  666. /**
  667. * Title for a file, to display in the interface (if there's no better title) and
  668. * for download filenames
  669. *
  670. * @param $file File object
  671. * @returns string
  672. */
  673. public static function getDisplayName(File $file): string
  674. {
  675. if (empty($file->filename)) {
  676. return _m('Untitled attachment');
  677. }
  678. // New file name format is "{bin2hex(original_name.ext)}-{$hash}"
  679. $filename = self::decodeFilename($file->filename);
  680. // If there was an error in the match, something's wrong with some piece
  681. // of code (could be a file with utf8 chars in the name)
  682. $log_error_msg = "Invalid file name for File with id={$file->id} " .
  683. "({$file->filename}). Some plugin probably did something wrong.";
  684. if ($filename === false) {
  685. common_log(LOG_ERR, $log_error_msg);
  686. } elseif ($filename === null) {
  687. // The old file name format was "{hash}.{ext}" so we didn't have a name
  688. // This extracts the extension
  689. $ret = preg_match('/^.+?\.+?(.+)$/', $file->filename, $matches);
  690. if ($ret !== 1) {
  691. common_log(LOG_ERR, $log_error_msg);
  692. return _m('Untitled attachment');
  693. }
  694. $ext = $matches[1];
  695. // There's a blacklisted extension array, which could have an alternative
  696. // extension, such as phps, to replace php. We want to turn it back
  697. // (currently defaulted to empty, but let's keep the feature)
  698. $blacklist = common_config('attachments', 'extblacklist');
  699. if (is_array($blacklist)) {
  700. foreach ($blacklist as $upload_ext => $safe_ext) {
  701. if ($ext === $safe_ext) {
  702. $ext = $upload_ext;
  703. break;
  704. }
  705. }
  706. }
  707. $filename = "untitled.{$ext}";
  708. }
  709. return $filename;
  710. }
  711. }