start of wordpress plugin, RT#75279
[freeside.git] / fs_selfservice / wordpress / freeside_selfservice.php
1 <?php
2 /*
3 Plugin Name: Freeside signup and self-service plugin
4 Plugin URI:  http://freeside.biz/freeside
5 Description: Call the Freeside signup and self-service APIs from within Wordpress
6 Version:     0.20170403
7 Author:      Freeside Internet Services, Inc.
8 Author URI:  https://freeside.biz/freeside/
9 License:     GPL3
10 License URI: https://www.gnu.org/licenses/gpl-3.0.html
11 Text Domain: freeside_selfserivce
12 Domain Path: /languages
13 */
14
15 add_action('admin_init', 'freeside_admin_init' );
16
17 function freeside_admin_init {
18   register_setting( 'misc', 'freeside_selfservice_url', 'https://freeside.server:8080' );
19 }
20
21 function flatten($hash) {
22   if ( !is_array($hash) ) return $hash;
23   $flat = array();
24
25   array_walk($hash, function($value, $key, &$to) { 
26     array_push($to, $key, $value);
27   }, $flat);
28
29   foreach ($hash as $key => $value) {
30     $flat[] = $key;
31     $flat[] = $value;
32   }
33
34   return($flat);
35 }
36
37 class FreesideSelfService  {
38
39     //Change this to match the location of your selfservice xmlrpc.cgi or daemon
40     #var $URL = 'https://localhost/selfservice/xmlrpc.cgi';
41     # XXX freeide_selfservice_url config value
42     #var $URL = 'http://localhost/selfservice/xmlrpc.cgi';
43     var $URL = get_opgion('freeside_selfservice_url');
44
45     function FreesideSelfService() {
46       $this;
47     }
48
49     public function __call($name, $arguments) {
50
51         error_log("[FreesideSelfService] $name called, sending to ". $this->URL);
52
53         $request = xmlrpc_encode_request("FS.ClientAPI_XMLRPC.$name", flatten($arguments[0]));
54         $context = stream_context_create( array( 'http' => array(
55             'method' => "POST",
56             'header' => "Content-Type: text/xml",
57             'content' => $request
58         )));
59         $file = file_get_contents($this->URL, false, $context);
60         $response = xmlrpc_decode($file);
61         if (xmlrpc_is_fault($response)) {
62             trigger_error("[FreesideSelfService] XML-RPC communication error: $response[faultString] ($response[faultCode])");
63         } else {
64             //error_log("[FreesideSelfService] $response");
65             return $response;
66         }
67     }
68
69 }
70
71 ?>