]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/IO/ExternalScript.php
960cfd4b8b67eb97f3e5adb52775e54393168bb8
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7.5dev - 2013-06-11
40 use Galette\Core\Preferences
;
43 * External script call
46 * @name ExternalScript
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2013-2014 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since Available since 0.7.5dev - 2010-06-11
59 private $as_json = true; //TODO: parametize?
65 * @param Preferences $pref Galette preferences
67 public function __construct(Preferences
$pref)
69 $uri = $pref->pref_new_contrib_script
;
70 list($protocol,) = explode('://', $uri);
72 if ($protocol == $uri) {
74 'An URI must be specified',
81 //FIXME: should probably be changed to use pref_galette_url and Slim routing
82 $this->protocol
= 'http';
83 if (isset($_SERVER['HTTPS'])) {
84 $this->protocol
= 'https';
86 $this->method
= 'galette';
87 $selfs = explode('/', $_SERVER['PHP_SELF']);
89 $self = implode('/', $selfs);
90 $uri = $protocol . '://' . $_SERVER['SERVER_NAME'] . $self .
91 '/' . GALETTE_BASE_PATH
. str_replace($protocol . '://', '', $uri);
94 $this->protocol
= $protocol;
95 $this->method
= $protocol;
99 $this->method
= $protocol;
100 $this->protocol
= 'http';
105 $this->method
= trim($protocol, 's');
106 $this->protocol
= 'https';
109 throw new \
RuntimeException('Unknown protocol.');
114 __CLASS__
. ' instancied with method ' . $this->method
.
115 ' and protocol ' . $this->protocol
,
119 if ($protocol !== 'file') {
120 $this->uri
= str_replace(
122 $this->protocol
. '://',
126 if (file_exists($uri)) {
127 $this->uri
= str_replace(
133 throw new \
RuntimeException(
134 __METHOD__
. 'File ' . $uri . ' does not exists!'
140 __CLASS__
. ' URI set to ' . $this->uri
,
148 * @param array $params Array of params to send
152 public function send($params)
154 if (!is_array($params) ||
count($params) == 0) {
155 throw new \
RuntimeException(__METHOD__
. ': parameters are mandatory.');
161 switch ($this->method
) {
164 if ($this->as_json
=== true) {
165 $uri .= '?params=' . json_encode($params);
167 $url_params = http_build_query($params, 'galette_');
168 $uri .= '?' . $url_params;
170 curl_setopt($ch, CURLOPT_URL
, $uri);
171 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
172 $this->output
= curl_exec($ch);
173 if ($this->output
!== false) {
183 curl_setopt($ch, CURLOPT_URL
, $this->uri
);
184 curl_setopt($ch, CURLOPT_POST
, count($params));
185 curl_setopt($ch, CURLOPT_RETURNTRANSFER
, 1);
186 if ($this->as_json
=== true) {
191 'params' => json_encode($params)
195 curl_setopt($ch, CURLOPT_POSTFIELDS
, $params);
197 $this->output
= curl_exec($ch);
198 if ($this->output
!== false) {
207 if ($this->as_json
=== true) {
208 $params = json_encode($params);
211 foreach ($params as $k => $v) {
212 $imploded .= ' ' . $k . '=' . $v;
217 $descriptors = array(
218 0 => array('pipe', 'r'),
219 1 => array('pipe', 'w'),
220 2 => array('pipe', 'w')
223 $process = proc_open(
228 fwrite($pipes[0], $params);
232 $output = stream_get_contents($pipes[1]);
233 if (trim($output) !== '') {
234 $this->output
.= "\n\nStdOut:\n" . $output;
238 $errors = stream_get_contents($pipes[2]);
239 if (trim($errors) !== '') {
240 $this->output
.= "\n\nStdErr:\n" . $errors;
243 //closes pipes and process
246 proc_close($process);
248 if (trim($this->output
) === '') {
255 throw new \
RuntimeException(
256 __METHOD__
. ': unknown method ' . $this->method
261 __METHOD__
. ' result: ' . $result . "output:\n" . $this->output
,
269 * Get full output (only relevant is method is file)
273 public function getOutput()
275 return $this->output
;