1- <?php
2-
3- namespace Stackify \Log \Transport ;
4-
5- use Stackify \Log \Transport \Config \Api ;
6-
7- /**
8- * This transport collects log data until the end of processing.
9- * It sends data executing shell curl and sending it to background.
10- */
11- class ExecTransport extends AbstractApiTransport
12- {
13-
14- protected $ curlPath = 'curl ' ;
15-
16- const ERROR_CURL = 'Command returned an error. [Command: "%s"] [Return code: %d] [Message: "%s"] ' ;
17- const SUCCESS_CURL = 'Command sent. [Command: "%s"] ' ;
18-
19- public function __construct ($ apiKey , array $ options = array ())
20- {
21- parent ::__construct ($ apiKey , $ options );
22- // @TODO windows support
23- }
24-
25- protected function getAllowedOptions ()
26- {
27- return array (
28- 'curlPath ' => '/.+/ ' ,
29- );
30- }
31-
32- protected function getTransportName ()
33- {
34- return 'ExecTransport ' ;
35- }
36-
37- protected function send ($ data )
38- {
39- $ url = Api::API_BASE_URL . Api::API_CALL_LOGS ;
40- $ cmd = "$ this ->curlPath -X POST " ;
41- foreach ($ this ->getApiHeaders () as $ name => $ value ) {
42- $ cmd .= " --header \"$ name: $ value \"" ;
43- }
44- $ escapedData = $ this ->escapeArg ($ data );
45- $ maxTime = Api::API_MAX_TIME ;
46- $ cmd .= " --data ' $ escapedData' ' $ url' --max-time $ maxTime " ;
47- if ($ this ->proxy ) {
48- $ cmd .= " --proxy ' $ this ->proxy ' " ;
49- }
50- if ($ this ->debug ) {
51- $ cmd .= ' --verbose ' ;
52- } else {
53- // return immediately while curl will run in the background
54- $ cmd .= ' > /dev/null 2>&1 & ' ;
55- }
56- $ output = array ();
57- $ r = exec ($ cmd , $ output , $ result );
58- if ($ this ->debug ) {
59- if ($ result !== 0 ) {
60- // curl returned some error
61- $ this ->logInternal (self ::ERROR_CURL , $ cmd , $ result , implode (' ' , $ output ));
62- } else {
63- $ this ->logInternal (self ::SUCCESS_CURL , $ cmd );
64- }
65- }
66- }
67-
68- private function escapeArg ($ string )
69- {
70- // @TODO test special chars
71- // http://stackoverflow.com/a/1250279/871861
72- return str_replace ("' " , "' \"' \"' " , $ string );
73- }
74-
1+ <?php
2+
3+ namespace Stackify \Log \Transport ;
4+
5+ use Stackify \Log \Transport \Config \Api ;
6+
7+ /**
8+ * This transport collects log data until the end of processing.
9+ * It sends data executing shell curl and sending it to background.
10+ */
11+ class ExecTransport extends AbstractApiTransport
12+ {
13+
14+ protected $ curlPath = 'curl ' ;
15+
16+ const ERROR_CURL = 'Command returned an error. [Command: "%s"] [Return code: %d] [Message: "%s"] ' ;
17+ const SUCCESS_CURL = 'Command sent. [Command: "%s"] ' ;
18+
19+ public function __construct ($ apiKey , array $ options = array ())
20+ {
21+ parent ::__construct ($ apiKey , $ options );
22+ // @TODO windows support
23+ }
24+
25+ protected function getAllowedOptions ()
26+ {
27+ return array (
28+ 'curlPath ' => '/.+/ ' ,
29+ );
30+ }
31+
32+ protected function getTransportName ()
33+ {
34+ return 'ExecTransport ' ;
35+ }
36+
37+ protected function send ($ data )
38+ {
39+ $ url = Api::API_BASE_URL . Api::API_CALL_LOGS ;
40+ $ cmd = "$ this ->curlPath -X POST " ;
41+ foreach ($ this ->getApiHeaders () as $ name => $ value ) {
42+ $ cmd .= " --header \"$ name: $ value \"" ;
43+ }
44+ $ escapedData = $ this ->escapeArg ($ data );
45+ $ maxTime = Api::API_MAX_TIME ;
46+ $ cmd .= " --data ' $ escapedData' ' $ url' --max-time $ maxTime " ;
47+ if ($ this ->proxy ) {
48+ $ cmd .= " --proxy ' $ this ->proxy ' " ;
49+ }
50+ if ($ this ->debug ) {
51+ $ cmd .= ' --verbose ' ;
52+ } else {
53+ // return immediately while curl will run in the background
54+ $ cmd .= ' > /dev/null 2>&1 & ' ;
55+ }
56+ $ output = array ();
57+ $ r = exec ($ cmd , $ output , $ result );
58+ // if debug mode is off, it makes no sense to check result,
59+ // because command is send to background
60+ if ($ this ->debug ) {
61+ if ($ result !== 0 ) {
62+ // curl returned some error
63+ $ this ->logError (self ::ERROR_CURL , $ cmd , $ result , implode (' ' , $ output ));
64+ } else {
65+ $ this ->logDebug (self ::SUCCESS_CURL , $ cmd );
66+ }
67+ }
68+ }
69+
70+ private function escapeArg ($ string )
71+ {
72+ // @TODO test special chars
73+ // http://stackoverflow.com/a/1250279/871861
74+ return str_replace ("' " , "' \"' \"' " , $ string );
75+ }
76+
7577}
0 commit comments