wordpress plugin, RT#75279
[freeside.git] / fs_selfservice / wordpress / freeside_selfservice.php
1 <?php
2 /*
3 Plugin Name: Freeside signup and self-service
4 Plugin URI:  http://freeside.biz/freeside
5 Description: Call the Freeside signup and self-service APIs from within Wordpress
6 Version:     0.20170417
7 Author:      Freeside Internet Services, Inc.
8 Author URI:  https://freeside.biz/freeside/
9 License URI: https://www.gnu.org/licenses/gpl-3.0.html
10 Text Domain: freeside_selfserivce
11 Domain Path: /languages
12 License:     LGPL
13
14 The Freeside signup and self-service plugin is free software: you can
15 redistribute it and/or modify it under the terms of the GNU Lesser General
16 Public License as published by the Free Software Foundation, either version
17 3 of the License, or any later version.
18  
19 The Freeside signup and self-service plugin is distributed in the hope that
20 it will be useful, but WITHOUT ANY WARRANTY; without even the implied
21 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Lesser General Public License for more details.
23  
24 You should have received a copy of the GNU General Public License
25 along with this plugin. If not, see
26 https://www.gnu.org/licenses/lgpl-3.0.en.html
27 */
28
29 add_action('admin_init', 'freeside_admin_init' );
30
31 add_action('init', 'freeside_init');
32
33 function freeside_admin_init() {
34   register_setting( 'general', 'freeside_server', 'freeside.example.com' );
35   add_settings_field( 'freeside_server', 'Freeside server', 'freeside_server_input', 'general' );
36 }
37
38 function freeside_server_input() {
39   $value = get_option('freeside_server');
40   //$value = ($value ? $value : 'freeside.example.com');
41   ?>
42     <INPUT TYPE="text" ID="freeside_server" NAME="freeside_server" VALUE="<?php echo htmlspecialchars($value); ?>">
43   <?php
44 }
45
46 //TODO: remove freeside_server on uninstall
47
48 function freeside_init() {
49   //error_log("FINALLY action run ". $FREESIDE_PROCESS_LOGIN);
50
51   //error_log($GLOBALS['$FREESIDE_PROCESS_LOGIN']);
52   if ( ! $GLOBALS['FREESIDE_PROCESS_LOGIN'] ) {
53 error_log("DACOOKIE: ". $_COOKIE['freeside_session_id']);
54     $GLOBALS['FREESIDE_SESSION_ID'] = $_COOKIE['freeside_session_id'];
55     return;
56   } else {
57     $GLOBALS['FREESIDE_PROCESS_LOGIN'] = false;
58   }
59
60   $freeside = new FreesideSelfService();
61
62   $response = $freeside->login( array( 
63     'email'    => strtolower($_POST['freeside_email']),
64     'username' => strtolower($_POST['freeside_username']),
65     'domain'   => strtolower($_POST['freeside_domain']),
66     'password' => $_POST['freeside_password'],
67   ) );
68
69   #error_log("[login] received response from freeside: $response");
70
71   $error = $response['error'];
72   error_log($error);
73
74   if ( $error ) {
75
76     $url  = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
77     $url .= $_SERVER['SERVER_NAME'];
78     $url .= $_SERVER['REQUEST_URI'];
79
80     wp_redirect(dirname($url). '/example_login.php?username='. urlencode($_POST['freeside_username']).
81                              '&domain='.   urlencode($_POST['freeside_domain']).
82                              '&email='.    urlencode($_POST['freeside_email']).
83                              '&freeside_error='.    urlencode($error)
84           );
85     exit;
86
87   }
88
89   // sucessful login
90
91   $session_id = $response['session_id'];
92
93   error_log("[login] logged into freeside with session_id=$freeside_session_id, setting cookie");
94
95 // now what?  for now, always redirect to the main page (or the select a
96 // customer diversion).
97 // eventually, other options?
98
99   setcookie('freeside_session_id', $session_id);
100
101   $GLOBALS['FREESIDE_LOGIN_RESPONSE'] = $response;
102
103 }
104
105 function freeside_flatten($hash) {
106   if ( !is_array($hash) ) return $hash;
107   $flat = array();
108
109   array_walk($hash, function($value, $key, &$to) { 
110     array_push($to, $key, $value);
111   }, $flat);
112
113   foreach ($hash as $key => $value) {
114     $flat[] = $key;
115     $flat[] = $value;
116   }
117
118   return($flat);
119 }
120
121 class FreesideSelfService {
122
123     function FreesideSelfService() {
124       $this;
125     }
126
127     public function __call($name, $arguments) {
128     
129         $URL = 'http://'. get_option('freeside_server'). ':8080';
130         error_log("[FreesideSelfService] $name called, sending to ". $URL);
131
132         $request = xmlrpc_encode_request("FS.ClientAPI_XMLRPC.$name", freeside_flatten($arguments[0]));
133         $context = stream_context_create( array( 'http' => array(
134             'method' => "POST",
135             'header' => "Content-Type: text/xml",
136             'content' => $request
137         )));
138         $file = file_get_contents($URL, false, $context);
139         $response = xmlrpc_decode($file);
140         if (xmlrpc_is_fault($response)) {
141             trigger_error("[FreesideSelfService] XML-RPC communication error: $response[faultString] ($response[faultCode])");
142         } else {
143             //error_log("[FreesideSelfService] $response");
144             return $response;
145         }
146     }
147
148 }
149
150 ?>