irods.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Thomas Müller <thomas.mueller@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Storage;
  9. set_include_path(get_include_path() . PATH_SEPARATOR .
  10. \OC_App::getAppPath('files_external') . '/3rdparty/irodsphp/prods/src');
  11. ob_start();
  12. require_once 'ProdsConfig.inc.php';
  13. require_once 'ProdsStreamer.class.php';
  14. ob_end_clean();
  15. class iRODS extends \OC\Files\Storage\StreamWrapper{
  16. private $password;
  17. private $user;
  18. private $host;
  19. private $port;
  20. private $zone;
  21. private $root;
  22. private $use_logon_credentials;
  23. private $auth_mode;
  24. public function __construct($params) {
  25. if (isset($params['host']) && isset($params['user']) && isset($params['password'])) {
  26. $this->host = $params['host'];
  27. $this->port = $params['port'];
  28. $this->user = $params['user'];
  29. $this->password = $params['password'];
  30. $this->use_logon_credentials = $params['use_logon_credentials'];
  31. $this->zone = $params['zone'];
  32. $this->auth_mode = isset($params['auth_mode']) ? $params['auth_mode'] : '';
  33. $this->root = isset($params['root']) ? $params['root'] : '/';
  34. if ( ! $this->root || $this->root[0] !== '/') {
  35. $this->root='/'.$this->root;
  36. }
  37. // take user and password from the session
  38. if ($this->use_logon_credentials && isset($_SESSION['irods-credentials']) )
  39. {
  40. $this->user = $_SESSION['irods-credentials']['uid'];
  41. $this->password = $_SESSION['irods-credentials']['password'];
  42. }
  43. //create the root folder if necessary
  44. if ( ! $this->is_dir('')) {
  45. $this->mkdir('');
  46. }
  47. } else {
  48. throw new \Exception();
  49. }
  50. }
  51. public static function login( $params ) {
  52. $_SESSION['irods-credentials'] = $params;
  53. }
  54. public function getId(){
  55. return 'irods::' . $this->user . '@' . $this->host . '/' . $this->root;
  56. }
  57. /**
  58. * construct the rods url
  59. * @param string $path
  60. * @return string
  61. */
  62. public function constructUrl($path) {
  63. $path = rtrim($path,'/');
  64. if ( $path === '' || $path[0] !== '/') {
  65. $path = '/'.$path;
  66. }
  67. // adding auth method
  68. $userWithZone = $this->user.'.'.$this->zone;
  69. if ($this->auth_mode !== '') {
  70. $userWithZone .= '.'.$this->auth_mode;
  71. }
  72. // url wrapper schema is named rods
  73. return 'rods://'.$userWithZone.':'.$this->password.'@'.$this->host.':'.$this->port.$this->root.$path;
  74. }
  75. public function filetype($path) {
  76. return @filetype($this->constructUrl($path));
  77. }
  78. public function mkdir($path) {
  79. return @mkdir($this->constructUrl($path));
  80. }
  81. public function touch($path, $mtime=null) {
  82. // we cannot set a time
  83. if ($mtime != null) {
  84. return false;
  85. }
  86. $path = $this->constructUrl($path);
  87. // if the file doesn't exist we create it
  88. if (!file_exists($path)) {
  89. file_put_contents($path, '');
  90. return true;
  91. }
  92. // mtime updates are not supported
  93. return false;
  94. }
  95. /**
  96. * check if a file or folder has been updated since $time
  97. * @param string $path
  98. * @param int $time
  99. * @return bool
  100. */
  101. public function hasUpdated($path,$time) {
  102. // this it a work around for folder mtimes -> we loop it's content
  103. if ( $this->is_dir($path)) {
  104. $actualTime=$this->collectionMTime($path);
  105. return $actualTime>$time;
  106. }
  107. $actualTime=$this->filemtime($path);
  108. return $actualTime>$time;
  109. }
  110. /**
  111. * get the best guess for the modification time of an iRODS collection
  112. */
  113. private function collectionMTime($path) {
  114. $dh = $this->opendir($path);
  115. $lastCTime = $this->filemtime($path);
  116. while ($file = readdir($dh)) {
  117. if ($file != '.' and $file != '..') {
  118. $time = $this->filemtime($file);
  119. if ($time > $lastCTime) {
  120. $lastCTime = $time;
  121. }
  122. }
  123. }
  124. return $lastCTime;
  125. }
  126. }