1
0

CommandHandler.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /* $Id: CommandHandler.php 2692 2007-03-22 14:33:58Z b4rt $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * CommandHandler
  17. */
  18. class CommandHandler
  19. {
  20. // private fields
  21. // messages-array
  22. var $_messages = array();
  23. // commands-array
  24. var $_commands = array();
  25. // =========================================================================
  26. // public static methods
  27. // =========================================================================
  28. /**
  29. * add command for transfer
  30. *
  31. * @param $transfer
  32. * @param $command
  33. * @return boolean
  34. */
  35. function add($transfer, $command) {
  36. global $instanceCommandHandler;
  37. // initialize if needed
  38. if (!isset($instanceCommandHandler))
  39. CommandHandler::initialize();
  40. // call instance-method
  41. return $instanceCommandHandler->instance_add($transfer, $command);
  42. }
  43. /**
  44. * send command(s) to transfer
  45. *
  46. * @param $transfer
  47. * @return boolean
  48. */
  49. function send($transfer) {
  50. global $instanceCommandHandler;
  51. // return false if not set
  52. if (!isset($instanceCommandHandler))
  53. return false;
  54. // call instance-method
  55. return $instanceCommandHandler->instance_send($transfer);
  56. }
  57. /**
  58. * cleans command(s) of transfer
  59. *
  60. * @param $transfer
  61. * @return boolean
  62. */
  63. function clean($transfer) {
  64. global $instanceCommandHandler;
  65. // initialize if needed
  66. if (!isset($instanceCommandHandler))
  67. CommandHandler::initialize();
  68. // call instance-method
  69. return $instanceCommandHandler->instance_clean($transfer);
  70. }
  71. /**
  72. * getMessages
  73. *
  74. * @return array
  75. */
  76. function getMessages() {
  77. global $instanceCommandHandler;
  78. return (isset($instanceCommandHandler))
  79. ? $instanceCommandHandler->_messages
  80. : array();
  81. }
  82. /**
  83. * initialize CommandHandler.
  84. */
  85. function initialize() {
  86. global $instanceCommandHandler;
  87. // create instance
  88. if (!isset($instanceCommandHandler))
  89. $instanceCommandHandler = new CommandHandler();
  90. }
  91. // =========================================================================
  92. // ctor
  93. // =========================================================================
  94. /**
  95. * do not use this, use only the public static methods !
  96. *
  97. * @return CommandHandler
  98. */
  99. function CommandHandler() {
  100. $this->_messages = array();
  101. $this->_commands = array();
  102. }
  103. // =========================================================================
  104. // public methods
  105. // =========================================================================
  106. /**
  107. * add command for transfer
  108. *
  109. * @param $transfer
  110. * @param $command
  111. * @return boolean
  112. */
  113. function instance_add($transfer, $command) {
  114. if (!isset($this->_commands[$transfer]))
  115. $this->_commands[$transfer] = array();
  116. if ((!in_array($command, $this->_commands[$transfer])) && (strlen($command) > 0)) {
  117. array_push($this->_commands[$transfer], $command);
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }
  123. /**
  124. * send command(s) to transfer
  125. *
  126. * @param $transfer
  127. * @return boolean
  128. */
  129. function instance_send($transfer) {
  130. return (empty($this->_commands[$transfer]))
  131. ? false
  132. : $this->_writeCommandFile($transfer);
  133. }
  134. /**
  135. * cleans command(s) of transfer
  136. *
  137. * @param $transfer
  138. * @return boolean
  139. */
  140. function instance_clean($transfer) {
  141. global $cfg;
  142. // if set unset
  143. if (isset($this->_commands[$transfer]))
  144. unset($this->_commands[$transfer]);
  145. // if exist remove command-file
  146. $file = $cfg["transfer_file_path"].$transfer.'.cmd';
  147. if (@file_exists($file))
  148. @unlink($file);
  149. // return
  150. return true;
  151. }
  152. // =========================================================================
  153. // private methods
  154. // =========================================================================
  155. /**
  156. * write the command-file
  157. *
  158. * @param $transfer
  159. * @return boolean
  160. */
  161. function _writeCommandFile($transfer) {
  162. global $cfg;
  163. $file = $cfg["transfer_file_path"].$transfer.'.cmd';
  164. $handle = false;
  165. $handle = @fopen($file, "w");
  166. if (!$handle) {
  167. $msg = "cannot open command-file ".$file." for writing.";
  168. array_push($this->_messages , $msg);
  169. AuditAction($cfg["constants"]["error"], "CommandHandler _writeCommandFile-Error : ".$msg);
  170. return false;
  171. }
  172. $result = @fwrite($handle, implode("\n", $this->_commands[$transfer])."\n");
  173. @fclose($handle);
  174. if ($result === false) {
  175. $msg = "cannot write content to command-file ".$file.".";
  176. array_push($this->_messages , $msg);
  177. AuditAction($cfg["constants"]["error"], "CommandHandler _writeCommandFile-Error : ".$msg);
  178. return false;
  179. }
  180. // unset
  181. unset($this->_commands[$transfer]);
  182. // return
  183. return true;
  184. }
  185. }
  186. ?>