FilesystemExtension.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. /**
  3. * @package Grav\Common\Twig
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Twig\Extension;
  9. use Grav\Common\Grav;
  10. use Grav\Common\Utils;
  11. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  12. use Twig\Extension\AbstractExtension;
  13. use Twig\TwigFilter;
  14. use Twig\TwigFunction;
  15. /**
  16. * Class FilesystemExtension
  17. * @package Grav\Common\Twig\Extension
  18. */
  19. class FilesystemExtension extends AbstractExtension
  20. {
  21. /** @var UniformResourceLocator */
  22. private $locator;
  23. public function __construct()
  24. {
  25. $this->locator = Grav::instance()['locator'];
  26. }
  27. /**
  28. * @return TwigFilter[]
  29. */
  30. public function getFilters()
  31. {
  32. return [
  33. new TwigFilter('file_exists', [$this, 'file_exists']),
  34. new TwigFilter('fileatime', [$this, 'fileatime']),
  35. new TwigFilter('filectime', [$this, 'filectime']),
  36. new TwigFilter('filemtime', [$this, 'filemtime']),
  37. new TwigFilter('filesize', [$this, 'filesize']),
  38. new TwigFilter('filetype', [$this, 'filetype']),
  39. new TwigFilter('is_dir', [$this, 'is_dir']),
  40. new TwigFilter('is_file', [$this, 'is_file']),
  41. new TwigFilter('is_link', [$this, 'is_link']),
  42. new TwigFilter('is_readable', [$this, 'is_readable']),
  43. new TwigFilter('is_writable', [$this, 'is_writable']),
  44. new TwigFilter('is_writeable', [$this, 'is_writable']),
  45. new TwigFilter('lstat', [$this, 'lstat']),
  46. new TwigFilter('getimagesize', [$this, 'getimagesize']),
  47. new TwigFilter('exif_read_data', [$this, 'exif_read_data']),
  48. new TwigFilter('read_exif_data', [$this, 'exif_read_data']),
  49. new TwigFilter('exif_imagetype', [$this, 'exif_imagetype']),
  50. new TwigFilter('hash_file', [$this, 'hash_file']),
  51. new TwigFilter('hash_hmac_file', [$this, 'hash_hmac_file']),
  52. new TwigFilter('md5_file', [$this, 'md5_file']),
  53. new TwigFilter('sha1_file', [$this, 'sha1_file']),
  54. new TwigFilter('get_meta_tags', [$this, 'get_meta_tags']),
  55. new TwigFilter('pathinfo', [$this, 'pathinfo']),
  56. ];
  57. }
  58. /**
  59. * Return a list of all functions.
  60. *
  61. * @return TwigFunction[]
  62. */
  63. public function getFunctions()
  64. {
  65. return [
  66. new TwigFunction('file_exists', [$this, 'file_exists']),
  67. new TwigFunction('fileatime', [$this, 'fileatime']),
  68. new TwigFunction('filectime', [$this, 'filectime']),
  69. new TwigFunction('filemtime', [$this, 'filemtime']),
  70. new TwigFunction('filesize', [$this, 'filesize']),
  71. new TwigFunction('filetype', [$this, 'filetype']),
  72. new TwigFunction('is_dir', [$this, 'is_dir']),
  73. new TwigFunction('is_file', [$this, 'is_file']),
  74. new TwigFunction('is_link', [$this, 'is_link']),
  75. new TwigFunction('is_readable', [$this, 'is_readable']),
  76. new TwigFunction('is_writable', [$this, 'is_writable']),
  77. new TwigFunction('is_writeable', [$this, 'is_writable']),
  78. new TwigFunction('lstat', [$this, 'lstat']),
  79. new TwigFunction('getimagesize', [$this, 'getimagesize']),
  80. new TwigFunction('exif_read_data', [$this, 'exif_read_data']),
  81. new TwigFunction('read_exif_data', [$this, 'exif_read_data']),
  82. new TwigFunction('exif_imagetype', [$this, 'exif_imagetype']),
  83. new TwigFunction('hash_file', [$this, 'hash_file']),
  84. new TwigFunction('hash_hmac_file', [$this, 'hash_hmac_file']),
  85. new TwigFunction('md5_file', [$this, 'md5_file']),
  86. new TwigFunction('sha1_file', [$this, 'sha1_file']),
  87. new TwigFunction('get_meta_tags', [$this, 'get_meta_tags']),
  88. new TwigFunction('pathinfo', [$this, 'pathinfo']),
  89. ];
  90. }
  91. /**
  92. * @param string $filename
  93. * @return bool
  94. */
  95. public function file_exists($filename): bool
  96. {
  97. if (!$this->checkFilename($filename)) {
  98. return false;
  99. }
  100. return file_exists($filename);
  101. }
  102. /**
  103. * @param string $filename
  104. * @return int|false
  105. */
  106. public function fileatime($filename)
  107. {
  108. if (!$this->checkFilename($filename)) {
  109. return false;
  110. }
  111. return fileatime($filename);
  112. }
  113. /**
  114. * @param string $filename
  115. * @return int|false
  116. */
  117. public function filectime($filename)
  118. {
  119. if (!$this->checkFilename($filename)) {
  120. return false;
  121. }
  122. return filectime($filename);
  123. }
  124. /**
  125. * @param string $filename
  126. * @return int|false
  127. */
  128. public function filemtime($filename)
  129. {
  130. if (!$this->checkFilename($filename)) {
  131. return false;
  132. }
  133. return filemtime($filename);
  134. }
  135. /**
  136. * @param string $filename
  137. * @return int|false
  138. */
  139. public function filesize($filename)
  140. {
  141. if (!$this->checkFilename($filename)) {
  142. return false;
  143. }
  144. return filesize($filename);
  145. }
  146. /**
  147. * @param string $filename
  148. * @return string|false
  149. */
  150. public function filetype($filename)
  151. {
  152. if (!$this->checkFilename($filename)) {
  153. return false;
  154. }
  155. return filetype($filename);
  156. }
  157. /**
  158. * @param string $filename
  159. * @return bool
  160. */
  161. public function is_dir($filename): bool
  162. {
  163. if (!$this->checkFilename($filename)) {
  164. return false;
  165. }
  166. return is_dir($filename);
  167. }
  168. /**
  169. * @param string $filename
  170. * @return bool
  171. */
  172. public function is_file($filename): bool
  173. {
  174. if (!$this->checkFilename($filename)) {
  175. return false;
  176. }
  177. return is_file($filename);
  178. }
  179. /**
  180. * @param string $filename
  181. * @return bool
  182. */
  183. public function is_link($filename): bool
  184. {
  185. if (!$this->checkFilename($filename)) {
  186. return false;
  187. }
  188. return is_link($filename);
  189. }
  190. /**
  191. * @param string $filename
  192. * @return bool
  193. */
  194. public function is_readable($filename): bool
  195. {
  196. if (!$this->checkFilename($filename)) {
  197. return false;
  198. }
  199. return is_readable($filename);
  200. }
  201. /**
  202. * @param string $filename
  203. * @return bool
  204. */
  205. public function is_writable($filename): bool
  206. {
  207. if (!$this->checkFilename($filename)) {
  208. return false;
  209. }
  210. return is_writable($filename);
  211. }
  212. /**
  213. * @param string $filename
  214. * @return array|false
  215. */
  216. public function lstat($filename)
  217. {
  218. if (!$this->checkFilename($filename)) {
  219. return false;
  220. }
  221. return lstat($filename);
  222. }
  223. /**
  224. * @param string $filename
  225. * @return array|false
  226. */
  227. public function getimagesize($filename)
  228. {
  229. if (!$this->checkFilename($filename)) {
  230. return false;
  231. }
  232. return getimagesize($filename);
  233. }
  234. /**
  235. * @param string $filename
  236. * @param string|null $required_sections
  237. * @param bool $as_arrays
  238. * @param bool $read_thumbnail
  239. * @return array|false
  240. */
  241. public function exif_read_data($filename, ?string $required_sections, bool $as_arrays = false, bool $read_thumbnail = false)
  242. {
  243. if (!Utils::functionExists('exif_read_data') || !$this->checkFilename($filename)) {
  244. return false;
  245. }
  246. return exif_read_data($filename, $required_sections, $as_arrays, $read_thumbnail);
  247. }
  248. /**
  249. * @param string $filename
  250. * @return int|false
  251. */
  252. public function exif_imagetype($filename)
  253. {
  254. if (!Utils::functionExists('exif_imagetype') || !$this->checkFilename($filename)) {
  255. return false;
  256. }
  257. return @exif_imagetype($filename);
  258. }
  259. /**
  260. * @param string $algo
  261. * @param string $filename
  262. * @param bool $binary
  263. * @return string|false
  264. */
  265. public function hash_file(string $algo, string $filename, bool $binary = false)
  266. {
  267. if (!$this->checkFilename($filename)) {
  268. return false;
  269. }
  270. return hash_file($algo, $filename, $binary);
  271. }
  272. /**
  273. * @param string $algo
  274. * @param string $filename
  275. * @param string $key
  276. * @param bool $binary
  277. * @return string|false
  278. */
  279. public function hash_hmac_file(string $algo, string $filename, string $key, bool $binary = false)
  280. {
  281. if (!$this->checkFilename($filename)) {
  282. return false;
  283. }
  284. return hash_hmac_file($algo, $filename, $key, $binary);
  285. }
  286. /**
  287. * @param string $filename
  288. * @param bool $binary
  289. * @return string|false
  290. */
  291. public function md5_file($filename, bool $binary = false)
  292. {
  293. if (!$this->checkFilename($filename)) {
  294. return false;
  295. }
  296. return md5_file($filename, $binary);
  297. }
  298. /**
  299. * @param string $filename
  300. * @param bool $binary
  301. * @return string|false
  302. */
  303. public function sha1_file($filename, bool $binary = false)
  304. {
  305. if (!$this->checkFilename($filename)) {
  306. return false;
  307. }
  308. return sha1_file($filename, $binary);
  309. }
  310. /**
  311. * @param string $filename
  312. * @return array|false
  313. */
  314. public function get_meta_tags($filename)
  315. {
  316. if (!$this->checkFilename($filename)) {
  317. return false;
  318. }
  319. return get_meta_tags($filename);
  320. }
  321. /**
  322. * @param string $path
  323. * @param int|null $flags
  324. * @return string|string[]
  325. */
  326. public function pathinfo($path, $flags = null)
  327. {
  328. return Utils::pathinfo($path, $flags);
  329. }
  330. /**
  331. * @param string $filename
  332. * @return bool
  333. */
  334. private function checkFilename($filename): bool
  335. {
  336. return is_string($filename) && (!str_contains($filename, '://') || $this->locator->isStream($filename));
  337. }
  338. }