SourceForge: coils/coils: src/coils/logic/workflow/tools/oie.php@c6d3c2528dc9
src/coils/logic/workflow/tools/oie.php
author awilliam@whitemice.org
Wed Oct 26 07:27:49 2011 -0400 (6 months ago)
changeset 3071 c6d3c2528dc9
parent 3070 0f02b62b1611
parent 3069 c2ade14617e3
permissions -rw-r--r--
Fix-up oie.php
     1 <?PHP
     2 #
     3 # Copyright (c) 2010, 2011 Adam Tauno Williams <awilliam@whitemice.org>
     4 #
     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:
    11 #
    12 # The above copyright notice and this permission notice shall be included in
    13 # all copies or substantial portions of the Software.
    14 #
    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
    21 #
    22 
    23 class OIEProcess {
    24    private $http_root;
    25    private $route_name;
    26    private $parameters;
    27    private $headers;   
    28    private $status;
    29 
    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();
    35        $this->status      = 0;
    36    }
    37 
    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) {
    41            $counter = 0;
    42            foreach($this->parameters as $key => $value) {
    43                if ($counter == 0) {
    44                    $url = sprintf('%s?%s=%s', $url, urlencode($key), urlencode($value));
    45                } else {
    46                    $url = sprintf('%s&%s=%s', $url, urlencode($key), urlencode($value));
    47                }
    48                $counter++;
    49            }
    50        }
    51        return $url;
    52    }
    53    
    54    function _process_header($_ch, $_header) {
    55        $tmp = explode(':', $_header);
    56        if (count($tmp) == 2) {
    57            $header = $tmp[0];
    58            $value = substr($tmp[1], 1);
    59            $this->headers[$header] = $value;
    60        }
    61        return strlen($_header);
    62    }
    63       
    64    function start($_username, $_secret, $_stream = null, $_mimetype='application/octet-stream')
    65    {
    66         #Prepare a stream that is the input message
    67         if ($_stream) {
    68             fseek($_stream, 0, SEEK_END);
    69             $size = ftell($_stream);
    70             fseek($_stream, 0, SEEK_SET);
    71             $close_stream = false;
    72         } else {
    73             # create an empty stream for the input as there is no input stream
    74             $_stream = tmpfile();
    75             $size = 0;
    76             $close_stream = true;
    77         }
    78 
    79         $url = $this->_build_url();
    80 
    81         # FIRE!
    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'));
    91 
    92         #Perform transaction
    93         if (curl_exec($oie_put)) {
    94             $response = curl_getinfo($oie_put); 
    95             if ($response['http_code'] == 301)
    96                $this->status = 'OK';
    97             else
    98                $this->status = 'OIERRR';
    99         } else $this->status = 'SYSERR';
   100         curl_close($oie_put);
   101         if ($close_stream)
   102             fclose($_stream);
   103         return $this->status;
   104     } /* end function invoke */
   105     
   106     function get_process_id() {
   107         if ($this->status == 'OK') {
   108             return $this->headers['X-COILS-WORKFLOW-PROCESS-ID'];
   109         }
   110         return null;
   111     }
   112     
   113     function get_message_id() {
   114         if ($this->status == 'OK') {
   115             return $this->headers['X-COILS-WORKFLOW-MESSAGE-UUID'];
   116         }
   117         return null;    
   118     }
   119     
   120 } /* end class OIEProcessInvocation */
   121 
   122 $HTTPROOT   = "http://opengroupware.mormail.com:8080";
   123 $ROUTENAME  = "TEST_MailBack";
   124 $PARAMETERS = array('myParameter'=>'YOYO MAMA', 'otherParam'=>4);
   125 
   126 $request = new OIEProcess($HTTPROOT, $ROUTENAME, $PARAMETERS);
   127 #
   128 # TEST: an empty input stream
   129 # if ($request->start('adam', '*******', null, 'text/plain') == 'OK') {
   130 #
   131 if ($request->start('adam', '********', fopen('/etc/passwd', 'r'), 'text/plain') == 'OK') {
   132     echo "\n";
   133     echo "Process ID: " . $request->get_process_id() . "\n";
   134     echo "Message UUID: " . $request->get_message_id() . "\n";    
   135 }