PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/MediaManager/src/Lib/ElFinder/elFinderVolumeS3.class.php

http://github.com/QuickAppsCMS/QuickApps-CMS
PHP | 725 lines | 304 code | 143 blank | 278 comment | 52 complexity | 0388bc2193aaf224d06a899b7e648c42 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, GPL-3.0
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * elFinder driver for Amazon S3 (SOAP) filesystem.
  6. *
  7. * @author Dmitry (dio) Levashov,
  8. * @author Alexey Sukhotin
  9. * */
  10. class elFinderVolumeS3 extends elFinderVolumeDriver {
  11. protected $driverId = 's3s';
  12. protected $s3;
  13. public function __construct() {
  14. $opts = array(
  15. 'accesskey' => '',
  16. 'secretkey' => '',
  17. 'bucket' => '',
  18. 'tmpPath' => '',
  19. );
  20. $this->options = array_merge($this->options, $opts);
  21. $this->options['mimeDetect'] = 'internal';
  22. }
  23. protected function init() {
  24. if (!$this->options['accesskey']
  25. || !$this->options['secretkey']
  26. || !$this->options['bucket']) {
  27. return $this->setError('Required options undefined.');
  28. }
  29. $this->s3 = new S3SoapClient($this->options['accesskey'], $this->options['secretkey']);
  30. $this->root = $this->options['path'];
  31. $this->rootName = 's3';
  32. return true;
  33. }
  34. protected function configure() {
  35. parent::configure();
  36. if (!empty($this->options['tmpPath'])) {
  37. if ((is_dir($this->options['tmpPath']) || @mkdir($this->options['tmpPath'])) && is_writable($this->options['tmpPath'])) {
  38. $this->tmpPath = $this->options['tmpPath'];
  39. }
  40. }
  41. $this->mimeDetect = 'internal';
  42. }
  43. /**
  44. * Return parent directory path
  45. *
  46. * @param string $path file path
  47. * @return string
  48. * @author Dmitry (dio) Levashov
  49. **/
  50. protected function _dirname($path) {
  51. $newpath = preg_replace("/\/$/", "", $path);
  52. $dn = substr($path, 0, strrpos($newpath, '/')) ;
  53. if (substr($dn, 0, 1) != '/') {
  54. $dn = "/$dn";
  55. }
  56. return $dn;
  57. }
  58. /**
  59. * Return file name
  60. *
  61. * @param string $path file path
  62. * @return string
  63. * @author Dmitry (dio) Levashov
  64. **/
  65. protected function _basename($path) {
  66. return basename($path);
  67. }
  68. /**
  69. * Join dir name and file name and return full path.
  70. * Some drivers (db) use int as path - so we give to concat path to driver itself
  71. *
  72. * @param string $dir dir path
  73. * @param string $name file name
  74. * @return string
  75. * @author Dmitry (dio) Levashov
  76. **/
  77. protected function _joinPath($dir, $name) {
  78. return $dir.DIRECTORY_SEPARATOR.$name;
  79. }
  80. /**
  81. * Return normalized path, this works the same as os.path.normpath() in Python
  82. *
  83. * @param string $path path
  84. * @return string
  85. * @author Troex Nevelin
  86. **/
  87. protected function _normpath($path) {
  88. $tmp = preg_replace("/^\//", "", $path);
  89. $tmp = preg_replace("/\/\//", "/", $tmp);
  90. $tmp = preg_replace("/\/$/", "", $tmp);
  91. return $tmp;
  92. }
  93. /**
  94. * Return file path related to root dir
  95. *
  96. * @param string $path file path
  97. * @return string
  98. * @author Dmitry (dio) Levashov
  99. **/
  100. protected function _relpath($path) {
  101. $newpath = $path;
  102. if (substr($path, 0, 1) != '/') {
  103. $newpath = "/$newpath";
  104. }
  105. $newpath = preg_replace("/\/$/", "", $newpath);
  106. $ret = ($newpath == $this->root) ? '' : substr($newpath, strlen($this->root)+1);
  107. return $ret;
  108. }
  109. /**
  110. * Convert path related to root dir into real path
  111. *
  112. * @param string $path file path
  113. * @return string
  114. * @author Dmitry (dio) Levashov
  115. **/
  116. protected function _abspath($path) {
  117. return $path == $this->separator ? $this->root : $this->root.$this->separator.$path;
  118. }
  119. /**
  120. * Return fake path started from root dir
  121. *
  122. * @param string $path file path
  123. * @return string
  124. * @author Dmitry (dio) Levashov
  125. **/
  126. protected function _path($path) {
  127. return $this->rootName.($path == $this->root ? '' : $this->separator.$this->_relpath($path));
  128. }
  129. /**
  130. * Return true if $path is children of $parent
  131. *
  132. * @param string $path path to check
  133. * @param string $parent parent path
  134. * @return bool
  135. * @author Dmitry (dio) Levashov
  136. **/
  137. protected function _inpath($path, $parent) {
  138. return $path == $parent || strpos($path, $parent.'/') === 0;
  139. }
  140. /**
  141. * Converting array of objects with name and value properties to
  142. * array[key] = value
  143. * @param array $metadata source array
  144. * @return array
  145. * @author Alexey Sukhotin
  146. **/
  147. protected function metaobj2array($metadata) {
  148. $arr = array();
  149. if (is_array($metadata)) {
  150. foreach ($metadata as $meta) {
  151. $arr[$meta->Name] = $meta->Value;
  152. }
  153. } else {
  154. $arr[$metadata->Name] = $metadata->Value;
  155. }
  156. return $arr;
  157. }
  158. /**
  159. * Return stat for given path.
  160. * Stat contains following fields:
  161. * - (int) size file size in b. required
  162. * - (int) ts file modification time in unix time. required
  163. * - (string) mime mimetype. required for folders, others - optionally
  164. * - (bool) read read permissions. required
  165. * - (bool) write write permissions. required
  166. * - (bool) locked is object locked. optionally
  167. * - (bool) hidden is object hidden. optionally
  168. * - (string) alias for symlinks - link target path relative to root path. optionally
  169. * - (string) target for symlinks - link target path. optionally
  170. *
  171. * If file does not exists - returns empty array or false.
  172. *
  173. * @param string $path file path
  174. * @return array|false
  175. * @author Dmitry (dio) Levashov,
  176. * @author Alexey Sukhotin
  177. **/
  178. protected function _stat($path) {
  179. $stat = array(
  180. 'size' => 0,
  181. 'ts' => time(),
  182. 'read' => true,
  183. 'write' => true,
  184. 'locked' => false,
  185. 'hidden' => false,
  186. 'mime' => 'directory',
  187. );
  188. if ($this->root == $path) {
  189. return $stat;
  190. }
  191. $np = $this->_normpath($path);
  192. try {
  193. $obj = $this->s3->GetObject(array('Bucket' => $this->options['bucket'], 'Key' => $np , 'GetMetadata' => true, 'InlineData' => false, 'GetData' => false));
  194. } catch (Exception $e) {
  195. }
  196. if (!isset($obj) || ($obj->GetObjectResponse->Status->Code != 200)) {
  197. $np .= '/';
  198. try {
  199. $obj = $this->s3->GetObject(array('Bucket' => $this->options['bucket'], 'Key' => $np , 'GetMetadata' => true, 'InlineData' => false, 'GetData' => false));
  200. } catch (Exception $e) {
  201. }
  202. }
  203. if (!(isset($obj) && $obj->GetObjectResponse->Status->Code == 200)) {
  204. return array();
  205. }
  206. $mime = '';
  207. $metadata = $this->metaobj2array($obj->GetObjectResponse->Metadata);
  208. $mime = $metadata['Content-Type'];
  209. if (!empty($mime)) {
  210. $stat['mime'] = ($mime == 'binary/octet-stream') ? 'directory' : $mime;
  211. }
  212. if (isset($obj->GetObjectResponse->LastModified)) {
  213. $stat['ts'] = strtotime($obj->GetObjectResponse->LastModified);
  214. }
  215. try {
  216. $files = $this->s3->ListBucket(array('Bucket' => $this->options['bucket'], 'Prefix' => $np, 'Delimiter' => '/'))->ListBucketResponse->Contents;
  217. } catch (Exception $e) {
  218. }
  219. if (!is_array($files)) {
  220. $files = array($files);
  221. }
  222. foreach ($files as $file) {
  223. if ($file->Key == $np) {
  224. $stat['size'] = $file->Size;
  225. }
  226. }
  227. return $stat;
  228. }
  229. /***************** file stat ********************/
  230. /**
  231. * Return true if path is dir and has at least one childs directory
  232. *
  233. * @param string $path dir path
  234. * @return bool
  235. * @author Alexey Sukhotin
  236. **/
  237. protected function _subdirs($path) {
  238. $stat = $this->_stat($path);
  239. if ($stat['mime'] == 'directory') {
  240. $files = $this->_scandir($path);
  241. foreach ($files as $file) {
  242. $fstat = $this->_stat($file);
  243. if ($fstat['mime'] == 'directory') {
  244. return true;
  245. }
  246. }
  247. }
  248. return false;
  249. }
  250. /**
  251. * Return object width and height
  252. * Ususaly used for images, but can be realize for video etc...
  253. *
  254. * @param string $path file path
  255. * @param string $mime file mime type
  256. * @return string|false
  257. * @author Dmitry (dio) Levashov
  258. * @author Naoki Sawada
  259. **/
  260. protected function _dimensions($path, $mime) {
  261. $ret = false;
  262. if ($imgsize = $this->getImageSize($path, $mime)) {
  263. $ret = $imgsize['dimensions'];
  264. }
  265. return $ret;
  266. }
  267. /******************** file/dir content *********************/
  268. /**
  269. * Return files list in directory
  270. *
  271. * @param string $path dir path
  272. * @return array
  273. * @author Dmitry (dio) Levashov,
  274. * @author Alexey Sukhotin
  275. **/
  276. protected function _scandir($path) {
  277. $s3path = preg_replace("/^\//", "", $path) . '/';
  278. $files = $this->s3->ListBucket(array('Bucket' => $this->options['bucket'], 'delimiter' => '/', 'Prefix' => $s3path))->ListBucketResponse->Contents;
  279. $finalfiles = array();
  280. foreach ($files as $file) {
  281. if (preg_match("|^" . $s3path . "[^/]*/?$|", $file->Key)) {
  282. $fname = preg_replace("/\/$/", "", $file->Key);
  283. $fname = $file->Key;
  284. if ($fname != preg_replace("/\/$/", "", $s3path)) {
  285. }
  286. $finalfiles[] = $fname;
  287. }
  288. }
  289. sort($finalfiles);
  290. return $finalfiles;
  291. }
  292. /**
  293. * Open file and return file pointer
  294. *
  295. * @param string $path file path
  296. * @param bool $write open file for writing
  297. * @return resource|false
  298. * @author Dmitry (dio) Levashov,
  299. * @author Alexey Sukhotin
  300. **/
  301. protected function _fopen($path, $mode="rb") {
  302. $tn = $this->getTempFile($path);
  303. $fp = $this->tmbPath
  304. ? @fopen($tn, 'w+')
  305. : @tmpfile();
  306. if ($fp) {
  307. try {
  308. $obj = $this->s3->GetObject(array('Bucket' => $this->options['bucket'], 'Key' => $this->_normpath($path) , 'GetMetadata' => true, 'InlineData' => true, 'GetData' => true));
  309. } catch (Exception $e) {
  310. }
  311. $mime = '';
  312. $metadata = $this->metaobj2array($obj->GetObjectResponse->Metadata);
  313. fwrite($fp, $obj->GetObjectResponse->Data);
  314. rewind($fp);
  315. return $fp;
  316. }
  317. return false;
  318. }
  319. /**
  320. * Close opened file
  321. *
  322. * @param resource $fp file pointer
  323. * @param string $path file path
  324. * @return bool
  325. * @author Dmitry (dio) Levashov
  326. **/
  327. protected function _fclose($fp, $path='') {
  328. @fclose($fp);
  329. if ($path) {
  330. @unlink($this->getTempFile($path));
  331. }
  332. }
  333. /******************** file/dir manipulations *************************/
  334. /**
  335. * Create dir and return created dir path or false on failed
  336. *
  337. * @param string $path parent dir path
  338. * @param string $name new directory name
  339. * @return string|bool
  340. * @author Dmitry (dio) Levashov,
  341. * @author Alexey Sukhotin
  342. **/
  343. protected function _mkdir($path, $name) {
  344. $newkey = $this->_normpath($path);
  345. $newkey = preg_replace("/\/$/", "", $newkey);
  346. $newkey = "$newkey/$name/";
  347. try {
  348. $obj = $this->s3->PutObjectInline(array('Bucket' => $this->options['bucket'], 'Key' => $newkey , 'ContentLength' => 0, 'Data' => ''));
  349. } catch (Exception $e) {
  350. }
  351. if (isset($obj)) {
  352. return "$path/$name";
  353. }
  354. return false;
  355. }
  356. /**
  357. * Create file and return it's path or false on failed
  358. *
  359. * @param string $path parent dir path
  360. * @param string $name new file name
  361. * @return string|bool
  362. * @author Dmitry (dio) Levashov,
  363. * @author Alexey Sukhotin
  364. **/
  365. protected function _mkfile($path, $name) {
  366. $newkey = $this->_normpath($path);
  367. $newkey = preg_replace("/\/$/", "", $newkey);
  368. $newkey = "$newkey/$name";
  369. try {
  370. $obj = $this->s3->PutObjectInline(array('Bucket' => $this->options['bucket'], 'Key' => $newkey , 'ContentLength' => 0, 'Data' => '', 'Metadata' => array(array('Name' => 'Content-Type', 'Value' => 'text/plain'))));
  371. } catch (Exception $e) {
  372. }
  373. if (isset($obj)) {
  374. return "$path/$name";
  375. }
  376. return false;
  377. }
  378. /**
  379. * Create symlink
  380. *
  381. * @param string $source file to link to
  382. * @param string $targetDir folder to create link in
  383. * @param string $name symlink name
  384. * @return bool
  385. * @author Dmitry (dio) Levashov
  386. **/
  387. protected function _symlink($source, $targetDir, $name) {
  388. return false;
  389. }
  390. /**
  391. * Copy file into another file (only inside one volume)
  392. *
  393. * @param string $source source file path
  394. * @param string $target target dir path
  395. * @param string $name file name
  396. * @return bool
  397. * @author Dmitry (dio) Levashov
  398. **/
  399. protected function _copy($source, $targetDir, $name) {
  400. return false;
  401. }
  402. /**
  403. * Move file into another parent dir.
  404. * Return new file path or false.
  405. *
  406. * @param string $source source file path
  407. * @param string $target target dir path
  408. * @param string $name file name
  409. * @return string|bool
  410. * @author Dmitry (dio) Levashov
  411. **/
  412. protected function _move($source, $targetDir, $name) {
  413. return false;
  414. }
  415. /**
  416. * Remove file
  417. *
  418. * @param string $path file path
  419. * @return bool
  420. * @author Dmitry (dio) Levashov
  421. **/
  422. protected function _unlink($path) {
  423. $newkey = $this->_normpath($path);
  424. $newkey = preg_replace("/\/$/", "", $newkey);
  425. try {
  426. $obj = $this->s3->DeleteObject(array('Bucket' => $this->options['bucket'], 'Key' => $newkey));
  427. } catch (Exception $e) {
  428. }
  429. /*$fp = fopen('/tmp/eltest.txt','a+');
  430. fwrite($fp, 'key='.$newkey);*/
  431. if (is_object($obj)) {
  432. //fwrite($fp, 'obj='.var_export($obj,true));
  433. if (isset($obj->DeleteObjectResponse->Code)) {
  434. $rc = $obj->DeleteObjectResponse->Code;
  435. if (substr($rc, 0, 1) == '2') {
  436. return true;
  437. }
  438. }
  439. }
  440. //fclose($fp);
  441. return false;
  442. }
  443. /**
  444. * Remove dir
  445. *
  446. * @param string $path dir path
  447. * @return bool
  448. * @author Dmitry (dio) Levashov
  449. **/
  450. protected function _rmdir($path) {
  451. return $this->_unlink($path . '/');
  452. }
  453. /**
  454. * Create new file and write into it from file pointer.
  455. * Return new file path or false on error.
  456. *
  457. * @param resource $fp file pointer
  458. * @param string $dir target dir path
  459. * @param string $name file name
  460. * @return bool|string
  461. * @author Dmitry (dio) Levashov
  462. **/
  463. protected function _save($fp, $dir, $name, $mime, $stat) {
  464. return false;
  465. }
  466. /**
  467. * Get file contents
  468. *
  469. * @param string $path file path
  470. * @return string|false
  471. * @author Dmitry (dio) Levashov
  472. **/
  473. protected function _getContents($path) {
  474. return false;
  475. }
  476. /**
  477. * Write a string to a file
  478. *
  479. * @param string $path file path
  480. * @param string $content new file content
  481. * @return bool
  482. * @author Dmitry (dio) Levashov
  483. **/
  484. protected function _filePutContents($path, $content) {
  485. return false;
  486. }
  487. /**
  488. * Extract files from archive
  489. *
  490. * @param string $path file path
  491. * @param array $arc archiver options
  492. * @return bool
  493. * @author Dmitry (dio) Levashov,
  494. * @author Alexey Sukhotin
  495. **/
  496. protected function _extract($path, $arc) {
  497. return false;
  498. }
  499. /**
  500. * Create archive and return its path
  501. *
  502. * @param string $dir target dir
  503. * @param array $files files names list
  504. * @param string $name archive name
  505. * @param array $arc archiver options
  506. * @return string|bool
  507. * @author Dmitry (dio) Levashov,
  508. * @author Alexey Sukhotin
  509. **/
  510. protected function _archive($dir, $files, $name, $arc) {
  511. return false;
  512. }
  513. /**
  514. * Detect available archivers
  515. *
  516. * @return void
  517. * @author Dmitry (dio) Levashov,
  518. * @author Alexey Sukhotin
  519. **/
  520. protected function _checkArchivers() {
  521. }
  522. }
  523. /**
  524. * SoapClient extension with Amazon S3 WSDL and request signing support
  525. *
  526. * @author Alexey Sukhotin
  527. **/
  528. class S3SoapClient extends SoapClient {
  529. private $accesskey = '';
  530. private $secretkey = '';
  531. public $client = NULL;
  532. public function __construct($key = '', $secret = '') {
  533. $this->accesskey = $key;
  534. $this->secretkey = $secret;
  535. parent::__construct('http://s3.amazonaws.com/doc/2006-03-01/AmazonS3.wsdl');
  536. }
  537. /**
  538. * Method call wrapper which adding S3 signature and default arguments to all S3 operations
  539. *
  540. * @author Alexey Sukhotin
  541. **/
  542. public function __call($method, $arguments) {
  543. /* Getting list of S3 web service functions which requires signing */
  544. $funcs = $this->__getFunctions();
  545. $funcnames = array();
  546. foreach ($funcs as $func) {
  547. preg_match("/\S+\s+([^\)]+)\(/", $func, $m);
  548. if (isset($m[1])) {
  549. $funcnames[] = $m[1];
  550. }
  551. }
  552. /* adding signature to arguments */
  553. if (in_array("{$method}", $funcnames)) {
  554. if (is_array($arguments[0])) {
  555. $arguments[0] = array_merge($arguments[0], $this->sign("{$method}"));
  556. } else {
  557. $arguments[0] = $this->sign("{$method}");
  558. }
  559. }
  560. /*$fp = fopen('/tmp/s3debug.txt', 'a+');
  561. fwrite($fp, 'method='."{$method}". ' timestamp='.date('Y-m-d H:i:s').' args='.var_export($arguments,true) . "\n");
  562. fclose($fp);*/
  563. return parent::__call($method, $arguments);
  564. }
  565. /**
  566. * Generating signature and timestamp for specified S3 operation
  567. *
  568. * @param string $operation S3 operation name
  569. * @return array
  570. * @author Alexey Sukhotin
  571. **/
  572. protected function sign($operation) {
  573. $params = array(
  574. 'AWSAccessKeyId' => $this->accesskey,
  575. 'Timestamp' => gmdate('Y-m-d\TH:i:s.000\Z'),
  576. );
  577. $sign_str = 'AmazonS3' . $operation . $params['Timestamp'];
  578. $params['Signature'] = base64_encode(hash_hmac('sha1', $sign_str, $this->secretkey, TRUE));
  579. return $params;
  580. }
  581. }