3 # Copyright (c) 2010, 2011 Adam Tauno Williams <awilliam@whitemice.org>
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 function __construct($_http_root, $_route_name, $_parameters=array()) {
31 $this->http_root = $_http_root;
32 $this->route_name = $_route_name;
33 $this->parameters = $_parameters;
34 $this->headers = array();
38 function _build_url() {
39 $url = sprintf('%s/dav/Workflow/Routes/%s/input', $this->http_root, $this->route_name);
40 if (count($this->parameters) > 0) {
42 foreach($this->parameters as $key => $value) {
44 $url = sprintf('%s?%s=%s', $url, urlencode($key), urlencode($value));
46 $url = sprintf('%s&%s=%s', $url, urlencode($key), urlencode($value));
54 function _process_header($_ch, $_header) {
55 $tmp = explode(':', $_header);
56 if (count($tmp) == 2) {
58 $value = substr($tmp[1], 1);
59 $this->headers[$header] = $value;
61 return strlen($_header);
64 function start($_username, $_secret, $_stream = null, $_mimetype='application/octet-stream')
66 #Prepare a stream that is the input message
68 fseek($_stream, 0, SEEK_END);
69 $size = ftell($_stream);
70 fseek($_stream, 0, SEEK_SET);
71 $close_stream = false;
73 # create an empty stream for the input as there is no input stream
79 $url = $this->_build_url();
82 $oie_put = curl_init();
83 curl_setopt($oie_put, CURLOPT_URL, $url);
84 curl_setopt($oie_put, CURLOPT_TIMEOUT, 5);
85 curl_setopt($oie_put, CURLOPT_PUT, true);
86 curl_setopt($oie_put, CURLOPT_HTTPHEADER, array('Content-Type: ' . $_mimetype));
87 curl_setopt($oie_put, CURLOPT_USERPWD, sprintf('%s:%s', $_username, $_secret));
88 curl_setopt($oie_put, CURLOPT_INFILE, $_stream);
89 curl_setopt($oie_put, CURLOPT_INFILESIZE, $size);
90 curl_setopt($oie_put, CURLOPT_HEADERFUNCTION, array(&$this,'_process_header'));
93 if (curl_exec($oie_put)) {
94 $response = curl_getinfo($oie_put);
95 if ($response['http_code'] == 301)
98 $this->status = 'OIERRR';
99 } else $this->status = 'SYSERR';
100 curl_close($oie_put);
103 return $this->status;
104 } /* end function invoke */
106 function get_process_id() {
107 if ($this->status == 'OK') {
108 return $this->headers['X-COILS-WORKFLOW-PROCESS-ID'];
113 function get_message_id() {
114 if ($this->status == 'OK') {
115 return $this->headers['X-COILS-WORKFLOW-MESSAGE-UUID'];
120 } /* end class OIEProcessInvocation */
122 $HTTPROOT = "http://opengroupware.mormail.com:8080";
123 $ROUTENAME = "TEST_MailBack";
124 $PARAMETERS = array('myParameter'=>'YOYO MAMA', 'otherParam'=>4);
126 $request = new OIEProcess($HTTPROOT, $ROUTENAME, $PARAMETERS);
128 # TEST: an empty input stream
129 # if ($request->start('adam', '*******', null, 'text/plain') == 'OK') {
131 if ($request->start('adam', '********', fopen('/etc/passwd', 'r'), 'text/plain') == 'OK') {
133 echo "Process ID: " . $request->get_process_id() . "\n";
134 echo "Message UUID: " . $request->get_message_id() . "\n";