/package/app/app/infra/general/file_transfer_managers/httpMgr.class.php

https://github.com/richhl/kalturaCE · PHP · 251 lines · 161 code · 51 blank · 39 comment · 18 complexity · 3dd5bd67db9d56d0063402a13c844a3c MD5 · raw file

  1. <?php
  2. /**
  3. * Extends the 'kFileTransferMgr' class & implements a file transfer manager using the FTP protocol.
  4. * For additional comments please look at the 'kFileTransferMgr' class.
  5. *
  6. * @package infra
  7. * @subpackage Storage
  8. */
  9. class httpMgr extends kFileTransferMgr
  10. {
  11. const HTTP_USER_AGENT = "\"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6\"";
  12. /**
  13. * @var resource
  14. */
  15. protected $ch;
  16. /**
  17. * @var string
  18. */
  19. protected $server;
  20. // instances of this class should be created usign the 'getInstance' of the 'kFileTransferMgr' class
  21. protected function __construct()
  22. {
  23. // do nothing
  24. }
  25. /**
  26. * @param string $url
  27. * @return string
  28. */
  29. public static function encodeUrl($url)
  30. {
  31. return str_replace(array(' ', '[', ']'), array('%20', '%5B', '%5D'), $url);
  32. }
  33. /**********************************************************************/
  34. /* Implementation of abstract functions from class 'kFileTransferMgr' */
  35. /**********************************************************************/
  36. // ftp connect to server:port
  37. protected function doConnect($http_server, &$http_port)
  38. {
  39. // try connecting to server
  40. if (!$http_port || $http_port == 0)
  41. $http_port = 80;
  42. $http_server .= $http_server . ':' . $http_port;
  43. try
  44. {
  45. $url_parts = parse_url($http_server);
  46. if(isset($url_parts["scheme"]))
  47. {
  48. if($url_parts["scheme"] != "http" && $url_parts["scheme"] != "https" )
  49. {
  50. KalturaLog::err("URL [$http_server] is not http");
  51. return false;
  52. }
  53. }
  54. else
  55. {
  56. $http_server = 'http://' . $http_server;
  57. }
  58. }
  59. catch ( Exception $exception )
  60. {
  61. $http_server = 'http://' . $http_server;
  62. }
  63. $this->server = $http_server;
  64. $this->ch = curl_init();
  65. curl_setopt($this->ch, CURLOPT_USERAGENT, self::HTTP_USER_AGENT);
  66. curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
  67. curl_setopt($this->ch, CURLOPT_NOSIGNAL, true);
  68. curl_setopt($this->ch, CURLOPT_FORBID_REUSE, true);
  69. curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
  70. curl_setopt($this->ch, CURLOPT_HEADER, false);
  71. curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
  72. return $this->ch;
  73. }
  74. // login to an existing connection with given user/pass
  75. protected function doLogin($http_user, $http_pass, $ftp_passive_mode = true)
  76. {
  77. if(is_null($http_user) && is_null($http_pass))
  78. return true;
  79. curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  80. curl_setopt($this->ch, CURLOPT_USERPWD, "{$http_user}:{$http_pass}");
  81. return true;
  82. }
  83. // login using a public key - not supported in FTP
  84. protected function doLoginPubKey($user, $pubKeyFile, $privKeyFile, $passphrase = null)
  85. {
  86. return false; // NOT SUPPORTED
  87. }
  88. // upload a file to the server (ftp_mode is irrelevant
  89. protected function doPutFile ($remote_file, $local_file, $ftp_mode, $http_field_name = null, $http_file_name = null)
  90. {
  91. $url = $this->server . '/' . $remote_file;
  92. $url = self::encodeUrl($url);
  93. curl_setopt($this->ch, CURLOPT_URL, $url);
  94. curl_setopt($this->ch, CURLOPT_POST, true);
  95. curl_setopt($this->ch, CURLOPT_RANGE, false);
  96. curl_setopt($this->ch, CURLOPT_HEADER, false);
  97. $params = null;
  98. if($http_field_name)
  99. {
  100. $params = array($http_field_name => file_get_contents($local_file));
  101. }
  102. elseif($http_file_name)
  103. {
  104. $params = array($http_file_name => '@' . $local_file);
  105. }
  106. else
  107. {
  108. $params = file_get_contents($local_file);
  109. }
  110. curl_setopt($this->ch, CURLOPT_POSTFIELDS, $params);
  111. $results = curl_exec($this->ch);
  112. if(!$results)
  113. {
  114. $errNumber = curl_errno($this->ch);
  115. $errDescription = curl_error($this->ch);
  116. if(!$results)
  117. throw new kFileTransferMgrException($errDescription, $errNumber);
  118. }
  119. return $results;
  120. }
  121. // download a file from the server (ftp_mode is irrelevant)
  122. protected function doGetFile ($remote_file, $local_file, $ftp_mode)
  123. {
  124. $url = $this->server . '/' . $remote_file;
  125. $url = self::encodeUrl($url);
  126. curl_setopt($this->ch, CURLOPT_URL, $url);
  127. curl_setopt($this->ch, CURLOPT_POST, false);
  128. curl_setopt($this->ch, CURLOPT_RANGE, false);
  129. curl_setopt($this->ch, CURLOPT_HEADER, false);
  130. $results = curl_exec($this->ch);
  131. if(!$results)
  132. {
  133. $errNumber = curl_errno($this->ch);
  134. $errDescription = curl_error($this->ch);
  135. throw new kFileTransferMgrException($errDescription, $errNumber);
  136. }
  137. if($local_file)
  138. file_put_contents($local_file, $results);
  139. return $results;
  140. }
  141. // create a new directory on the server
  142. protected function doMkDir ($remote_path)
  143. {
  144. return false;
  145. }
  146. // chmod to the given remote file
  147. protected function doChmod ($remote_file, $chmod_code)
  148. {
  149. return false;
  150. }
  151. // check if the given file/dir exists on the server
  152. protected function doFileExists($remote_file)
  153. {
  154. $url = $this->server . '/' . $remote_file;
  155. $url = self::encodeUrl($url);
  156. curl_setopt($this->ch, CURLOPT_URL, $url);
  157. curl_setopt($this->ch, CURLOPT_POST, false);
  158. curl_setopt($this->ch, CURLOPT_RANGE, '0-0');
  159. curl_setopt($this->ch, CURLOPT_HEADER, true);
  160. $results = curl_exec($this->ch);
  161. if(!$results)
  162. {
  163. curl_setopt($this->ch, CURLOPT_RANGE, false);
  164. curl_setopt($this->ch, CURLOPT_NOBODY, true);
  165. $results = curl_exec($this->ch);
  166. }
  167. if(!$results)
  168. return false;
  169. return true;
  170. }
  171. // return the current working directory
  172. protected function doPwd()
  173. {
  174. return '/';
  175. }
  176. // delete a file and return true/false according to success
  177. protected function doDelFile ($remote_file)
  178. {
  179. return false;
  180. }
  181. // delete a directory and return true/false according to success
  182. protected function doDelDir ($remote_path)
  183. {
  184. return false;
  185. }
  186. protected function doList ($remoteDir)
  187. {
  188. return false;
  189. }
  190. /*******************/
  191. /* Other functions */
  192. /*******************/
  193. // closes the FTP connection.
  194. public function __destruct( )
  195. {
  196. // close the connection
  197. if ($this->ch)
  198. curl_close($this->ch);
  199. }
  200. }