Ftp_Client.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. /**
  3. * An FTP transfer wrapper using the OSInet Finite_State_Machine
  4. *
  5. * @copyright (c) 2007 OSI
  6. * @author Frédéric G. MARAND
  7. * @license Licensed under the CeCILL 2.0
  8. * @version CVS: $Id: Ftp_Client.php,v 1.1 2007-06-03 21:27:57 marand Exp $
  9. * @link
  10. * @since Not applicable yet
  11. * @package osinetoffice
  12. * @subpackage bo_up_ingram
  13. *
  14. */
  15. /**
  16. * Save current reporting level while we set it
  17. */
  18. $_ftpEr = error_reporting(E_ALL | E_STRICT);
  19. /**
  20. * Class implements a finite-state-machine-based
  21. * FTP client with *very* limited functionality,
  22. * but that can display progress information
  23. * states are:
  24. * - init: not yet set up
  25. * - offline: no link established
  26. * - online: client connected to server
  27. * - live: client connected and logged in
  28. * - active: a data transfer operation is under way
  29. * - unsafe: something failed, disconnect can happen out of our control
  30. */
  31. class Ftp_Client extends Finite_State_Machine
  32. {
  33. /**
  34. * Code currently depends on the actual FSM location. Future versions should
  35. * probably locate it on their own.
  36. */
  37. const FTP_FSM = 'e:/src/osinetoffice/lib/Ftp.xml';
  38. /**
  39. * remote properties
  40. */
  41. private $fRemoteFile;
  42. private $fRemoteHost;
  43. private $fRemotePass;
  44. private $fRemoteUser;
  45. private $fRemoteWd;
  46. /**
  47. * local properties
  48. */
  49. private $fLocalWd;
  50. private $fLocalFile;
  51. private $fLocalFp; // file pointer to local file, used in get/put/continue
  52. /**
  53. * FTP properties
  54. */
  55. private $fFtpCallback; // name of callback function for progress information
  56. private $fFtpGoal; // bytes to be transferred
  57. private $fFtpConnection; // connection
  58. /**
  59. * Initialize parameters from an array
  60. *
  61. * @todo should really check names and values.
  62. * @param array $params
  63. * @return fsm_result
  64. */
  65. public function setParameters($params)
  66. {
  67. foreach ($params as $name => $value)
  68. {
  69. $field_name = "f$name";
  70. $this->$field_name = $value;
  71. }
  72. $ret = $this->apply_event('CheckParameters');
  73. return $ret;
  74. }
  75. /**
  76. * ============ utility functions ============
  77. */
  78. /**
  79. * format a boolean value as a string
  80. *
  81. * @param boolean $val
  82. * @return string
  83. */
  84. static function stringFromBoolean($val)
  85. {
  86. return $val ? 'true' : 'false';
  87. }
  88. /**
  89. * Enter description here...
  90. *
  91. * @param unknown_type $status
  92. * @return unknown
  93. */
  94. static function stringFromFtp($status)
  95. {
  96. switch ($status)
  97. {
  98. case FTP_FINISHED: $ret = 'FTP_FINISHED'; break;
  99. case FTP_MOREDATA: $ret = 'FTP_MOREDATA'; break;
  100. case FTP_FAILED: $ret = 'FTP_MOREDATA'; break;
  101. default: $ret = 'FTP_INVALID_STATUS'; break;
  102. }
  103. return $ret;
  104. }
  105. /**
  106. * ============ event handlers ============
  107. */
  108. /**
  109. * implement change remote directory
  110. * @return string (boolean)
  111. */
  112. protected function eventChdir()
  113. {
  114. $ret = ftp_chdir($this->fFtpConnection, $this->fRemoteWd);
  115. return ftp_client::stringFromBoolean($ret);
  116. }
  117. /**
  118. * does the instance have all necessary info for a FTP transfer ?
  119. *
  120. * @return string (boolean)
  121. */
  122. public function eventCheckParameters()
  123. {
  124. $ret = isset($this->fRemoteHost)
  125. && isset($this->fRemoteUser)
  126. && isset($this->fRemotePass)
  127. && isset($this->fRemoteWd)
  128. && isset($this->fRemoteFile)
  129. && isset($this->fLocalWd)
  130. && isset($this->fLocalFile)
  131. && isset($this->fFtpCallback)
  132. ;
  133. $ret = ftp_client::stringFromBoolean($ret);
  134. // echo func_name() . ", ret = $ret\n";
  135. return $ret;
  136. }
  137. /**
  138. * implementation of Close event
  139. *
  140. * @return string (boolean)
  141. */
  142. protected function eventClose()
  143. {
  144. $ret = 'true';
  145. // echo "Trying to close socket...";
  146. if (is_resource($this->fFtpConnection))
  147. {
  148. try
  149. {
  150. // echo "Trying to close connection...";
  151. ftp_close($this->fFtpConnection);
  152. // echo "Done.";
  153. }
  154. catch (Exception $e)
  155. {
  156. print_r($e);
  157. $ret = 'false';
  158. }
  159. }
  160. // echo "Socket closed\n";
  161. return $ret;
  162. }
  163. /**
  164. * implementation of Connect event
  165. *
  166. * @return string (boolean)
  167. */
  168. protected function eventConnect()
  169. {
  170. // echo func_name() . "\n";
  171. $this->fFtpConnection = ftp_connect($this->fRemoteHost); // default port, default timeout
  172. $ret = is_resource($this->fFtpConnection);
  173. return Ftp_Client::stringFromBoolean($ret);
  174. }
  175. /**
  176. * implementation of continue
  177. * @return string FTP_FINISHED | FTP_MOREDATA | FTP_FAILED
  178. */
  179. protected function eventContinue()
  180. {
  181. // echo func_name();
  182. if ($this->fFtpCallback)
  183. {
  184. call_user_func($this->fFtpCallback, $this, 'pre');
  185. }
  186. $ret = ftp_nb_continue($this->fFtpConnection);
  187. if ($ret == FTP_FINISHED)
  188. {
  189. fclose($this->fLocalFp);
  190. }
  191. if ($this->fFtpCallback)
  192. call_user_func($this->fFtpCallback, $this, 'post');
  193. $ret = Ftp_Client::stringFromFtp($ret);
  194. return $ret;
  195. }
  196. /**
  197. * implementation of get
  198. *
  199. * @return string FTP_FINISHED | FTP_MOREDATA | FTP_FAILED
  200. * @throws Exception fail creating local file
  201. */
  202. protected function eventGet()
  203. {
  204. // echo func_name() . "\n";
  205. $this->fLocalFp = fopen($this->fLocalFile, "wb");
  206. if (!is_resource($this->fLocalFp))
  207. {
  208. $ret = Ftp_Client::stringFromFtp(FTP_FAILED);
  209. // throw new Exception(func_name() . ": could not create local file $this->fLocalFile");
  210. return $ret;
  211. }
  212. $this->fFtpGoal = ftp_size($this->fFtpConnection, $this->fRemoteFile);
  213. $ret = ftp_nb_fget($this->fFtpConnection, $this->fLocalFp,
  214. $this->fRemoteFile, FTP_BINARY);
  215. if ($ret == FTP_FINISHED)
  216. {
  217. fclose($this->fLocalFp);
  218. }
  219. call_user_func($this->fFtpCallback, $this, 'post');
  220. // echo func_name() . " => $ret\n"; flush();
  221. return Ftp_Client::stringFromFtp($ret);
  222. }
  223. /**
  224. * implementation of login
  225. * @return string (boolean)
  226. */
  227. protected function eventLogin()
  228. {
  229. // echo func_name() . "\n";
  230. $ret = ftp_login($this->fFtpConnection, $this->fRemoteUser, $this->fRemotePass);
  231. return Ftp_Client::stringFromBoolean($ret);
  232. }
  233. /**
  234. * handler must be implemented, but does nothing
  235. * @return void
  236. */
  237. protected function eventProgress()
  238. {
  239. return; // the FSM needs nothing for this
  240. }
  241. /**
  242. * @return void
  243. */
  244. public function __construct()
  245. {
  246. $this->load_fsm(Ftp_Client::FTP_FSM);
  247. // print_r($this->f_transitions);
  248. parent::__construct();
  249. // print_r($this);die();
  250. }
  251. /**
  252. * close connection if it hasn't been done, to prevent connection
  253. * lingering on the server if avoidable
  254. * @return void
  255. */
  256. public function __destruct()
  257. {
  258. // echo func_name() . PHP_EOL;
  259. if ($this->is_event_allowed('Close'))
  260. {
  261. $this->apply_event('Close');
  262. }
  263. if(is_resource($this->fLocalFp))
  264. try
  265. {
  266. echo "Trying to close file...";
  267. fclose($this->fLocalFp);
  268. echo "Done\n";
  269. }
  270. catch (Exception $e)
  271. {
  272. print_r($e);
  273. }
  274. // echo "End of " . func_name() . "\n";
  275. }
  276. /**
  277. * Make sure name can be resolved
  278. * ignore hosts that don't resolve in DNS or hosts file
  279. *
  280. * @param string $host
  281. * @return boolean
  282. */
  283. public function isHostValid($host = null)
  284. {
  285. $ret = is_array(gethostbynamel($host));
  286. return $ret;
  287. }
  288. /**
  289. * If a transfer is under way, return the progress percentile, otherwise 0.0
  290. *
  291. * @return float
  292. */
  293. public function getProgress()
  294. {
  295. if ((!$this->f_state == 'active') || (!is_resource($this->fLocalFp)))
  296. {
  297. $ret = 0.0;
  298. }
  299. else
  300. {
  301. $pos = ftell($this->fLocalFp);
  302. $ret = $pos / $this->fFtpGoal;
  303. }
  304. return $ret;
  305. }
  306. }
  307. /**
  308. * Restore previous reporting level
  309. */
  310. error_reporting($_ftpEr);