PageRenderTime 61ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/system/application/libraries/remotefile.php

http://xtraupload.googlecode.com/
PHP | 344 lines | 256 code | 44 blank | 44 comment | 32 complexity | e2948301aa7b9bbdf968bb0e91dcfe37 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * XtraUpload
  4. *
  5. * A turn-key open source web 2.0 PHP file uploading package requiring PHP v5
  6. *
  7. * @package XtraUpload
  8. * @author Matthew Glinski
  9. * @copyright Copyright (c) 2006, XtraFile.com
  10. * @license http://xtrafile.com/docs/license
  11. * @link http://xtrafile.com
  12. * @since Version 2.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * XtraUpload Remote File Upload Library
  18. *
  19. * @package XtraUpload
  20. * @subpackage Library
  21. * @category Library
  22. * @author Matthew Glinski
  23. * @link http://xtrafile.com/docs/pages/files
  24. */
  25. // ------------------------------------------------------------------------
  26. class CI_Remotefile
  27. {
  28. public $buffer = 204800;
  29. public $tmp_dir = './temp';
  30. public $error = '';
  31. private $headers = '';
  32. public $CI = '';
  33. /**
  34. * File Download Constructor
  35. *
  36. * The constructor sets up the download system as ready for files
  37. */
  38. function CI_Remotefile()
  39. {
  40. $this->CI =& get_instance();
  41. log_message('debug', "Remote File Transfer Class Initialized");
  42. }
  43. /**
  44. * Setup
  45. *
  46. * Sets Config Vars
  47. *
  48. * @access public
  49. * @param Config Array
  50. * @return null
  51. */
  52. function setup($config = array())
  53. {
  54. if (count($config) > 0)
  55. {
  56. $this->_initialize($config);
  57. }
  58. }
  59. function getHeaders($url)
  60. {
  61. $this->headers = get_headers($url, true);
  62. return $this->headers['Content-Length'];
  63. }
  64. function headersToArray($string)
  65. {
  66. $lines = explode("\n", $string);
  67. foreach( $lines as $line)
  68. {
  69. $header = explode(":", $line);
  70. $this->headers[$header[0]] = (isset($header[1]) ? $header[1] : '' );
  71. }
  72. }
  73. function remoteSize()
  74. {
  75. return $this->headers['Content-Length'];
  76. }
  77. function getReferer()
  78. {
  79. if(isset($this->headers['Referer']))
  80. {
  81. return $this->headers['Referer'];
  82. }
  83. else
  84. {
  85. return false;
  86. }
  87. }
  88. function isRedirect()
  89. {
  90. if(isset($this->headers['Location']))
  91. {
  92. return $this->headers['Location'];
  93. }
  94. else
  95. {
  96. return false;
  97. }
  98. }
  99. function fetchFile($url, $fid, $max_size, $fp=NULL)
  100. {
  101. $parsedurl = parse_url($url, PHP_URL_SCHEME);
  102. if($parsedurl == 'http')
  103. {
  104. return $this->_httpTransfer($url, $fid, $max_size, $fp);
  105. }
  106. else if($parsedurl == 'ftp')
  107. {
  108. return $this->_ftpTransfer($url, $fid, ($max_size * 1024 * 1024));
  109. }
  110. else
  111. {
  112. exit;
  113. }
  114. }
  115. private function _httpTransfer($url, $fid, $max_size, $fp=NULL)
  116. {
  117. $nurl = $url;
  118. $parsedurl = parse_url($url);
  119. if(isset($parsedurl['user']))
  120. {
  121. $user = $parsedurl['user'];
  122. }
  123. if(isset($parsedurl['pass']))
  124. {
  125. $pass = $parsedurl['pass'];
  126. }
  127. $host = $parsedurl["host"];
  128. $hostname = $host;
  129. $port = "80";
  130. $query = "";
  131. $port = $port ? $port : "80";
  132. // Follow redirection
  133. $this->getHeaders($url);
  134. $isRedi = false;
  135. while($this->isRedirect())
  136. {
  137. $nurl = $this->isRedirect();
  138. $isRedi = true;
  139. $this->getHeaders($nurl);
  140. }
  141. if($isRedi)
  142. {
  143. $url = $nurl;
  144. $parsedurl = parse_url($url);
  145. // Get items of new url
  146. $referer = $this->getReferer();
  147. $host = $parsedurl["host"];
  148. $hostname = $host;
  149. $port = $port ? $port : "80";
  150. }
  151. $sh = fsockopen($host, $port, $errid, $errmsg, 30);
  152. if (!$sh)
  153. {
  154. return false;
  155. }
  156. if (!$parsedurl["path"])
  157. {
  158. $parsedurl["path"] = "/";
  159. }
  160. $request = "";
  161. $request.= "GET ".$parsedurl["path"].(isset($parsedurl["query"]) ? '?'.$parsedurl["query"] : '')." HTTP/1.0\r\n";
  162. $request.= "Host: $hostname\r\n";
  163. if (isset($referer) && $referer != "")
  164. {
  165. $request.= "Referer: ".$referer."\r\n";
  166. }
  167. if (isset($pass) || isset($user))
  168. {
  169. $request.= "Authorization: Basic ".base64_encode($user.":".$pass)."\r\n";
  170. }
  171. $request.= "\r\n";
  172. //Send The Request
  173. fwrite($sh, $request);
  174. // if no filepointer is given, make a temp file and open it for writing
  175. if(!$fp)
  176. {
  177. $sendFileName = true;
  178. $tmpfname = tempnam($this->tmp_dir, "RFT-");
  179. $fp = fopen($tmpfname, "wb");
  180. }
  181. $size = $this->remoteSize();
  182. if($size > ($max_size * 1024*1024))
  183. {
  184. fclose($sh);
  185. fclose($fp);
  186. return false;
  187. }
  188. $this->CI->db->insert('progress', array('progress' => 0, 'curr_time' => $_SERVER['REQUEST_TIME'] , 'total' => $size, 'start_time' => $_SERVER['REQUEST_TIME'], 'fid' => $fid));
  189. $i = $p = 0;
  190. $endHeaders = false;
  191. $rstr='';
  192. // download the file
  193. while(!feof($sh))
  194. {
  195. $string = fread($sh, $this->buffer);
  196. if(!$endHeaders)
  197. {
  198. if($test = stristr($string, "\r\n\r\n"))
  199. {
  200. if($isRedi)
  201. {
  202. $headers = explode("\r\n\r\n", $string);
  203. $headers = $headers[0];
  204. $this->headersToArray($headers);
  205. $size = $this->remoteSize();
  206. $this->CI->db->where('fid', $fid);
  207. $this->CI->db->update('progress', array('total' => $size));
  208. }
  209. $string = str_replace("\r\n\r\n", '', $test);
  210. $endHeaders = true;
  211. }
  212. else
  213. {
  214. continue;
  215. }
  216. }
  217. $p += strlen($string);
  218. fwrite($fp, $string);
  219. $string = NULL;
  220. if($i % 10 == 0)
  221. {
  222. $this->CI->db->where('fid', $fid);
  223. $this->CI->db->update('progress', array('progress' => $p, 'curr_time' => time()));
  224. }
  225. }
  226. fclose($sh);
  227. fclose($fp);
  228. // if passed a file pointer return true, if not return temp file name
  229. if(!$sendFileName)
  230. {
  231. return true;
  232. }
  233. else
  234. {
  235. return $tmpfname;
  236. }
  237. }
  238. private function _ftpTransfer($url, $fid, $max_size, $fp=NULL)
  239. {
  240. $url = trim($url);
  241. $nurl = $url;
  242. $parsedurl = parse_url($url);
  243. $user = "anonymous";
  244. if(isset($parsedurl['user']))
  245. {
  246. $user = $parsedurl['user'];
  247. }
  248. $pass = 'xtraupload.v2@gmail.com';
  249. if(isset($parsedurl['pass']))
  250. {
  251. $pass = $parsedurl['pass'];
  252. }
  253. $host = $hostname = $parsedurl["host"];
  254. $port = isset($parsedurl["port"]) ? $parsedurl["port"] : "21";
  255. $path = substr($parsedurl['path'], 1);
  256. $this->CI->load->library('ftp');
  257. $config['hostname'] = $hostname;
  258. $config['username'] = $user;
  259. $config['password'] = $pass;
  260. $config['port'] = $port;
  261. $config['passive'] = TRUE;
  262. $config['debug'] = FALSE;
  263. $this->CI->ftp->connect($config);
  264. if($this->CI->ftp->error)
  265. {
  266. $error = $this->CI->ftp->get_error();
  267. log_message('error', $error);
  268. $this->error = $error;
  269. show_error($error);
  270. return false;
  271. }
  272. $size = $this->CI->ftp->remote_filesize($path);
  273. if(!$size or ($max_size < $size))
  274. {
  275. log_message('error', "CAN NOT FTP SIZE");
  276. show_error("CAN NOT FTP SIZE");
  277. $this->error = 'CAN NOT FTP SIZE';
  278. return false;
  279. }
  280. $this->CI->db->insert('progress',
  281. array(
  282. 'progress' => 0,
  283. 'curr_time' => $_SERVER['REQUEST_TIME'],
  284. 'total' => $size,
  285. 'start_time' => $_SERVER['REQUEST_TIME'],
  286. 'fid' => $fid)
  287. );
  288. $fname = $this->CI->ftp->download_xu2($path, $fid, $max_size);
  289. if(!$fname)
  290. {
  291. $this->error = 'CAN NOT FTP TRANSFER';
  292. log_message('error', "CAN NOT FTP TRANSFER");
  293. show_error("CAN NOT FTP TRANSFER");
  294. return false;
  295. }
  296. $this->CI->ftp->close();
  297. return $fname;
  298. }
  299. }
  300. ?>