summaryrefslogtreecommitdiff
path: root/fs_selfservice/wordpress/freeside_selfservice.php
blob: 2041eed9049c4e7e2e1355884923573db37515dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
/*
Plugin Name: Freeside signup and self-service plugin
Plugin URI:  http://freeside.biz/freeside
Description: Call the Freeside signup and self-service APIs from within Wordpress
Version:     0.20170403
Author:      Freeside Internet Services, Inc.
Author URI:  https://freeside.biz/freeside/
License:     GPL3
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: freeside_selfserivce
Domain Path: /languages
*/

add_action('admin_init', 'freeside_admin_init' );

function freeside_admin_init {
  register_setting( 'misc', 'freeside_selfservice_url', 'https://freeside.server:8080' );
}

function flatten($hash) {
  if ( !is_array($hash) ) return $hash;
  $flat = array();

  array_walk($hash, function($value, $key, &$to) { 
    array_push($to, $key, $value);
  }, $flat);

  foreach ($hash as $key => $value) {
    $flat[] = $key;
    $flat[] = $value;
  }

  return($flat);
}

class FreesideSelfService  {

    //Change this to match the location of your selfservice xmlrpc.cgi or daemon
    #var $URL = 'https://localhost/selfservice/xmlrpc.cgi';
    # XXX freeide_selfservice_url config value
    #var $URL = 'http://localhost/selfservice/xmlrpc.cgi';
    var $URL = get_opgion('freeside_selfservice_url');

    function FreesideSelfService() {
      $this;
    }

    public function __call($name, $arguments) {

        error_log("[FreesideSelfService] $name called, sending to ". $this->URL);

        $request = xmlrpc_encode_request("FS.ClientAPI_XMLRPC.$name", flatten($arguments[0]));
        $context = stream_context_create( array( 'http' => array(
            'method' => "POST",
            'header' => "Content-Type: text/xml",
            'content' => $request
        )));
        $file = file_get_contents($this->URL, false, $context);
        $response = xmlrpc_decode($file);
        if (xmlrpc_is_fault($response)) {
            trigger_error("[FreesideSelfService] XML-RPC communication error: $response[faultString] ($response[faultCode])");
        } else {
            //error_log("[FreesideSelfService] $response");
            return $response;
        }
    }

}

?>