@@ -50,33 +50,20 @@ function scm_ping($params)
50
50
{
51
51
global $ PROGRAM , $ eventum_url ;
52
52
53
- $ ping_url = $ eventum_url . 'scm_ping.php ' ;
54
- $ params [ ' json ' ] = 1 ;
53
+ $ ping_url = $ eventum_url . 'scm_ping.php?scm= ' . $ params [ ' scm ' ] ;
54
+ $ status = json_post ( $ ping_url , $ params , 1 ) ;
55
55
56
- $ res = wget ($ ping_url , $ params );
57
- if (!$ res ) {
58
- throw new RuntimeException ("Couldn't read response from $ ping_url " );
56
+ if ($ status ['code ' ]) {
57
+ throw new RuntimeException ($ status ['message ' ], $ status ['code ' ]);
59
58
}
60
59
61
- list ($ headers , $ data ) = $ res ;
62
- // status line is first header in response
63
- $ status = array_shift ($ headers );
64
- list ($ proto , $ status , $ msg ) = explode (' ' , trim ($ status ), 3 );
65
- if ($ status != '200 ' ) {
66
- throw new RuntimeException ("Could not ping the Eventum SCM handler script: HTTP status code: $ status $ msg " );
67
- }
68
-
69
- $ status = json_decode ($ data , true );
70
- // if response is json, try to figure error from there
71
- if (is_array ($ status )) {
72
- if ($ status ['code ' ]) {
73
- throw new RuntimeException ($ status ['message ' ], $ status ['code ' ]);
74
- }
75
- $ data = $ status ['message ' ];
60
+ $ message = trim ($ status ['message ' ]);
61
+ if (!$ message ) {
62
+ return ;
76
63
}
77
64
78
65
// prefix response with our name
79
- foreach (explode ("\n" , trim ( $ data ) ) as $ line ) {
66
+ foreach (explode ("\n" , $ message ) as $ line ) {
80
67
echo "$ PROGRAM : $ line \n" ;
81
68
}
82
69
}
@@ -165,3 +152,64 @@ function wget($url, $params, $headers = true)
165
152
166
153
return $ data ;
167
154
}
155
+
156
+ /**
157
+ * POST json encoded data to $url
158
+ *
159
+ * @param string $url
160
+ * @param array $data
161
+ * @param bool $assoc
162
+ * @return array|stdClass result with extra 'meta' key
163
+ * @author Elan Ruusamäe <glen@delfi.ee>
164
+ */
165
+ function json_post ($ url , $ data , $ assoc = false )
166
+ {
167
+ // see if schema in url is supported
168
+ $ scheme = parse_url ($ url , PHP_URL_SCHEME );
169
+ if (!in_array ($ scheme , stream_get_wrappers ())) {
170
+ throw new RuntimeException ("$ scheme:// scheme not supported. Load openssl php extension? " );
171
+ }
172
+
173
+ $ body = json_encode ($ data );
174
+ $ headers = array (
175
+ 'Expect: ' ,
176
+ 'Content-Type: application/json ' ,
177
+ 'Accept: application/json ' ,
178
+ 'Content-Length: ' . strlen ($ body ),
179
+ );
180
+
181
+ $ options = array (
182
+ 'method ' => 'POST ' ,
183
+ 'content ' => $ body ,
184
+ 'header ' => implode ("\r\n" , $ headers )
185
+ );
186
+ $ options = array (
187
+ $ scheme => $ options
188
+ );
189
+
190
+ $ context = stream_context_create ($ options );
191
+
192
+ $ stream = @fopen ($ url , 'r ' , false , $ context );
193
+ if (!$ stream ) {
194
+ $ error = error_get_last ();
195
+ throw new RuntimeException ($ error ['message ' ]);
196
+ }
197
+
198
+ $ meta = stream_get_meta_data ($ stream );
199
+ $ result = stream_get_contents ($ stream );
200
+ fclose ($ stream );
201
+
202
+ $ response = json_decode ($ result , $ assoc );
203
+ if (!$ response ) {
204
+ throw new InvalidArgumentException ("Unable to decode: $ result " );
205
+ }
206
+ if ($ assoc ) {
207
+ $ response ['meta ' ] = $ meta ;
208
+ $ response ['raw ' ] = $ result ;
209
+ } else {
210
+ $ response ->raw = $ result ;
211
+ $ response ->meta = $ meta ;
212
+ }
213
+
214
+ return $ response ;
215
+ }
0 commit comments