Platform  3.1
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.TEClient.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
5  * @package FDL
6 */
7 /**
8  * Function to dialog with transformation server engine
9  *
10  * @author Anakeen 2007
11  * @version $Id: Class.TEClient.php,v 1.12 2007/08/14 09:39:33 eric Exp $
12  * @license http://creativecommons.org/licenses/by-nc-sa/2.0/fr/ Anakeen - licence CC
13  * @package FDL
14  */
15 /**
16  */
17 
18 include_once ("WHAT/Lib.FileMime.php");
19 
21 {
22  const error_connect = - 2;
23  const error_noengine = - 3;
24  const error_sendfile = - 4;
25  const error_emptyfile = - 5;
26  const error_convert = - 1;
27  const status_inprogress = 2;
28  const status_waiting = 3;
29  const status_done = 1;
30  /**
31  * host name of the transformation engine server
32  * @private string
33  */
34  private $host = 'localhost';
35  /**
36  * port number of the transformation engine server
37  * @private int
38  */
39  private $port = 51968;
40  /**
41  * initialize host and port
42  * @param string $host host name
43  * @param int $port port number
44  *
45  */
46  function __construct($host = "localhost", $port = 51968)
47  {
48  if ($host != "") $this->host = $host;
49  if ($port > 0) $this->port = $port;
50  }
51  /**
52  * send a request to do a transformation
53  * @param string $te_name Engine name
54  * @param string $fkey foreign key
55  * @param string $filename the path where is the original file
56  * @param string $callback url to activate after transformation is done
57  * @param array &$info transformation task info return "tid"=> ,"status"=> ,"comment=>
58  *
59  * @return string error message, if no error empty string
60  */
61  function sendTransformation($te_name, $fkey, $filename, $callback, &$info)
62  {
63  $err = "";
64 
65  clearstatcache(); // to reset filesize
66  $size = filesize($filename);
67  if ($size > 0) {
68  /* Lit l'adresse IP du serveur de destination */
69  $address = gethostbyname($this->host);
70  $service_port = $this->port;
71  /* Cree une socket TCP/IP. */
72  // echo "Essai de connexion à '$address' sur le port '$service_port'...\n";
73  // $result = socket_connect($socket, $address, $service_port);
74  $timeout = floatval(getParam("TE_TIMEOUT", 3));
75  $fp = @stream_socket_client("tcp://$address:$service_port", $errno, $errstr, $timeout);
76 
77  if (!$fp) {
78  $err = _("socket creation error") . " : $errstr ($errno)\n";
79  $info = array(
80  "status" => self::error_connect
81  );
82  }
83 
84  if ($err == "") {
85  $in = "CONVERT\n";
86  // echo "Envoi de la commande $in ...";
87  fputs($fp, $in);
88 
89  $out = trim(fgets($fp, 2048));
90  // echo "[$out].\n";
91  if ($out == "Continue") {
92  $basename = str_replace('"', '_', basename($filename));
93  $mime = getSysMimeFile($filename, $basename);
94 
95  $in = "<TE name=\"$te_name\" fkey=\"$fkey\" fname=\"$basename\" size=\"$size\" mime=\"$mime\" callback=\"$callback\"/>\n";
96  #echo "Envoi du header $in ...";
97  fputs($fp, $in);
98  $out = trim(fgets($fp));
99  $status = "KO";
100  if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
101  $status = $match[1];
102  }
103  if ($status == 'OK') {
104  //echo "Envoi du fichier $filename ...";
105  if (file_exists($filename)) {
106  $handle = @fopen($filename, "r");
107  if ($handle) {
108  while (!feof($handle)) {
109  $buffer = fread($handle, 2048);
110  $cout = fwrite($fp, $buffer, strlen($buffer));
111  }
112  fclose($handle);
113  }
114 
115  fflush($fp);
116  //echo "OK.\n";
117  // echo "Lire la réponse : \n\n";
118  $out = trim(fgets($fp));
119  if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
120  $status = $match[1];
121  }
122  if (preg_match("/<response[^>]*>(.*)<\/response>/i", $out, $match)) {
123  $outmsg = $match[1];
124  }
125  //echo "Response [$status]\n";
126  //echo "Message [$outmsg]\n";
127  if ($status == "OK") {
128  if (preg_match("/ id=[ ]*\"([^\"]*)\"/i", $outmsg, $match)) {
129  $tid = $match[1];
130  }
131  if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $outmsg, $match)) {
132  $status = $match[1];
133  }
134  if (preg_match("/<comment>(.*)<\/comment>/i", $outmsg, $match)) {
135  $comment = $match[1];
136  }
137  $info = array(
138  "tid" => $tid,
139  "status" => $status,
140  "comment" => $comment
141  );
142  } else {
143  $err = $outcode . " [$outmsg]";
144  }
145  }
146  } else {
147  $taskerr = '-';
148  if (preg_match("/<comment>(.*)<\/comment>/i", $out, $match)) {
149  $info = array(
150  "status" => self::error_noengine
151  );
152  $err = $match[1];
153  } else {
154  $err = _("Error sending file");
155  $info = array(
156  "status" => self::error_sendfile
157  );
158  }
159  }
160  }
161  //echo "Fermeture de la socket...";
162  fclose($fp);
163  }
164  } else {
165  $err = _("empty file");
166  $info = array(
167  "status" => self::error_emptyfile
168  );
169  }
170  return $err;
171  }
172  /**
173  * send a request to get information about a task
174  * @param int $tid_task identificator
175  * @param array &$info transformation task info return "tid"=> ,"status"=> ,"comment=>
176  *
177  * @return string error message, if no error empty string
178  */
179  function getInfo($tid, &$info)
180  {
181  $err = "";
182  /* Lit l'adresse IP du serveur de destination */
183  $address = gethostbyname($this->host);
184  $service_port = $this->port;
185  /* Cree une socket TCP/IP. */
186  // echo "Essai de connexion à '$address' sur le port '$service_port'...\n";
187  // $result = socket_connect($socket, $address, $service_port);
188  $fp = stream_socket_client("tcp://$address:$service_port", $errno, $errstr, 30);
189 
190  if (!$fp) {
191  $err = _("socket creation error") . " : $errstr ($errno)\n";
192  }
193 
194  if ($err == "") {
195 
196  $in = "INFO\n";
197  //echo "Envoi de la commande $in ...";
198  fputs($fp, $in);
199 
200  $out = trim(fgets($fp, 2048));
201  //echo "[$out].\n";
202  if ($out == "Continue") {
203 
204  $in = "<TASK id=\"$tid\" />\n";
205  //echo "Envoi du header $in ...";
206  fputs($fp, $in);
207 
208  $out = trim(fgets($fp));
209  if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
210  $status = $match[1];
211  }
212 
213  if ($status == "OK") {
214  //echo "<br>Response <b>$out</b>";
215  if (preg_match("/<task[^>]*>(.*)<\/task>/i", $out, $match)) {
216  $body = $match[1];
217  // echo "Response $body";
218  if (preg_match_all("|<[^>]+>(.*)</([^>]+)>|U", $body, $reg, PREG_SET_ORDER)) {
219 
220  foreach ($reg as $v) {
221  $info[$v[2]] = $v[1];
222  }
223  }
224  }
225  } else {
226  $msg = "";
227  if (preg_match("/<response[^>]*>(.*)<\/response>/i", $out, $match)) {
228  $msg = $match[1];
229  }
230  $err = $status . " [$msg]";
231  }
232  }
233  //echo "Fermeture de la socket...";
234  fclose($fp);
235  }
236 
237  return $err;
238  }
239  /**
240  * send a request to retrieve a transformation and to erase task from server
241  * the status must be D (Done) or K (Done but errors).
242  * @param string $tid Task identification
243  * @param string $filename the path where put the file (must be writeable)
244  *
245  * @return string error message, if no error empty string
246  */
248  {
250  $this->eraseTransformation($tid);
251  }
252  /**
253  * send a request for retrieve a transformation
254  * the status must be D (Done) or K (Done but errors).
255  * all working files are stayed into the server : be carreful to clean it after (use ::eraseTransformation)
256  * @param string $tid Task identification
257  * @param string $filename the path where put the file (must be writeable)
258  *
259  * @return string error message, if no error empty string
260  */
262  {
263 
264  $err = "";
265 
266  $handle = @fopen($filename, "w");
267  if (!$handle) {
268  $err = sprintf("cannot open file <%s> in write mode", $filename);
269  return $err;
270  }
271  /* Lit l'adresse IP du serveur de destination */
272  $address = gethostbyname($this->host);
273  $service_port = $this->port;
274  /* Cree une socket TCP/IP. */
275  //echo "Essai de connexion à '$address' sur le port '$service_port'...\n";
276  // $result = socket_connect($socket, $address, $service_port);
277  $fp = stream_socket_client("tcp://$address:$service_port", $errno, $errstr, 30);
278 
279  if (!$fp) {
280  $err = _("socket creation error") . " : $errstr ($errno)\n";
281  }
282 
283  if ($err == "") {
284  $in = "GET\n";
285  //echo "Envoi de la commande $in ...";
286  fputs($fp, $in);
287 
288  $out = trim(fgets($fp, 2048));
289  //echo "[$out].\n";
290  if ($out == "Continue") {
291 
292  $in = "<task id=\"$tid\" />\n";
293  //echo "Envoi du header $in ...";
294  fputs($fp, $in);
295  //echo "Recept du file size ...";
296  $out = trim(fgets($fp, 2048));
297  //echo "[$out]\n";
298  if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
299  $status = $match[1];
300  }
301  if ($status == "OK") {
302 
303  if (preg_match("/size=[ ]*\"([^\"]*)\"/i", $out, $match)) {
304  $size = $match[1];
305  }
306  //echo "Recept du fichier $filename ...";
307  if ($handle) {
308  $trbytes = 0;
309  do {
310  if ($size >= 2048) {
311  $rsize = 2048;
312  } else {
313  $rsize = $size;
314  }
315 
316  $buf = fread($fp, $rsize);
317  $l = strlen($buf);
318  $trbytes+= $l;
319  $size-= $l;
320  $wb = fwrite($handle, $buf);
321  //echo "file:$l []";
322 
323  } while ($size > 0);
324 
325  fclose($handle);
326  }
327  //echo "Wroted $filename\n.";
328  // echo "Lire la réponse : \n\n";
329  $out = trim(fgets($fp, 2048));
330  if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
331  $status = $match[1];
332  }
333  if ($status != "OK") {
334  if (preg_match("/<response[^>]*>(.*)<\/response>/i", $out, $match)) {
335  $msg = $match[1];
336  }
337  $err = "$status:$msg";
338  }
339  } else {
340  // status not OK
341  $msg = "";
342  if (preg_match("/<response[^>]*>(.*)<\/response>/i", $out, $match)) {
343  $msg = $match[1];
344  }
345  $err = "$status:$msg";
346  }
347  }
348  }
349  //echo "Fermeture de la socket...";
350  fclose($fp);
351  return $err;
352  }
353  /**
354  * erase transformation
355  * delete associated files in the server engine
356  * @param string $tid Task identification
357  * @param string $filename the path where put the file (must be writeable)
358  * @param array &$info transformation task info return "tid"=> ,"status"=> ,"comment=>
359  *
360  * @return string error message, if no error empty string
361  */
363  {
364  $err = "";
365  /* Lit l'adresse IP du serveur de destination */
366  $address = gethostbyname($this->host);
367  $service_port = $this->port;
368  /* Cree une socket TCP/IP. */
369  // echo "Essai de connexion à '$address' sur le port '$service_port'...\n";
370  // $result = socket_connect($socket, $address, $service_port);
371  $fp = stream_socket_client("tcp://$address:$service_port", $errno, $errstr, 30);
372 
373  if (!$fp) {
374  $err = _("socket creation error") . " : $errstr ($errno)\n";
375  }
376 
377  if ($err == "") {
378 
379  $in = "ABORT\n";
380  //echo "Envoi de la commande $in ...";
381  fputs($fp, $in);
382 
383  $out = trim(fgets($fp, 2048));
384  //echo "[$out].\n";
385  if ($out == "Continue") {
386 
387  $in = "<TASK id=\"$tid\" />\n";
388  //echo "Envoi du header $in ...";
389  fputs($fp, $in);
390 
391  $out = trim(fgets($fp));
392  if (preg_match("/status=[ ]*\"([^\"]*)\"/i", $out, $match)) {
393  $status = $match[1];
394  }
395  if ($status == "OK") {
396  //echo "<br>Response <b>$out</b>";
397  if (preg_match("/<task[^>]*>(.*)<\/task>/i", $out, $match)) {
398  $body = $match[1];
399  // echo "Response $body";
400  if (preg_match_all("|<[^>]+>(.*)</([^>]+)>|U", $body, $reg, PREG_SET_ORDER)) {
401 
402  foreach ($reg as $v) {
403  $info[$v[2]] = $v[1];
404  }
405  }
406  }
407  } else {
408  $msg = "";
409  if (preg_match("/<response[^>]*>(.*)<\/response>/i", $out, $match)) {
410  $msg = $match[1];
411  }
412  $err = $status . " [$msg]";
413  }
414  }
415  //echo "Fermeture de la socket...";
416  fclose($fp);
417  }
418 
419  return $err;
420  }
421 }
422 ?>
← centre documentaire © anakeen - published under CC License - Dynacase