summaryrefslogtreecommitdiff
path: root/fs_selfservice/wordpress/freeside_selfservice.php
blob: f619e8177e9a678ef1474a4f5e8c65d6947b7fbe (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
Plugin Name: Freeside signup and self-service
Plugin URI:  http://freeside.biz/freeside
Description: Call the Freeside signup and self-service APIs from within Wordpress
Version:     0.20170417
Author:      Freeside Internet Services, Inc.
Author URI:  https://freeside.biz/freeside/
License URI: https://www.gnu.org/licenses/gpl-3.0.html
Text Domain: freeside_selfserivce
Domain Path: /languages
License:     LGPL

The Freeside signup and self-service plugin is free software: you can
redistribute it and/or modify it under the terms of the GNU Lesser General
Public License as published by the Free Software Foundation, either version
3 of the License, or any later version.
 
The Freeside signup and self-service plugin is distributed in the hope that
it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this plugin. If not, see
https://www.gnu.org/licenses/lgpl-3.0.en.html
*/

add_action('admin_init', 'freeside_admin_init' );

add_action('init', 'freeside_init');

function freeside_admin_init() {
  register_setting( 'general', 'freeside_server', 'freeside.example.com' );
  add_settings_field( 'freeside_server', 'Freeside server', 'freeside_server_input', 'general' );
}

function freeside_server_input() {
  $value = get_option('freeside_server');
  //$value = ($value ? $value : 'freeside.example.com');
  ?>
    <INPUT TYPE="text" ID="freeside_server" NAME="freeside_server" VALUE="<?php echo htmlspecialchars($value); ?>">
  <?php
}

//TODO: remove freeside_server on uninstall

function freeside_init() {
  //error_log("FINALLY action run ". $FREESIDE_PROCESS_LOGIN);

  //error_log($GLOBALS['$FREESIDE_PROCESS_LOGIN']);
  if ( ! $GLOBALS['FREESIDE_PROCESS_LOGIN'] ) {
    return;
  } else {
    $GLOBALS['FREESIDE_PROCESS_LOGIN'] = false;
  }

  $freeside = new FreesideSelfService();

  $response = $freeside->login( array( 
    'email'    => strtolower($_POST['freeside_email']),
    'username' => strtolower($_POST['freeside_username']),
    'domain'   => strtolower($_POST['freeside_domain']),
    'password' => $_POST['freeside_password'],
  ) );

  #error_log("[login] received response from freeside: $response");

  $error = $response['error'];
  error_log($error);

  if ( $error ) {

    $url  = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
    $url .= $_SERVER['SERVER_NAME'];
    $url .= $_SERVER['REQUEST_URI'];

    wp_redirect(dirname($url). '/example_login.php?username='. urlencode($_POST['freeside_username']).
                             '&domain='.   urlencode($_POST['freeside_domain']).
                             '&email='.    urlencode($_POST['freeside_email']).
                             '&freeside_error='.    urlencode($error)
          );
    exit;

  }

  // sucessful login

  $session_id = $response['session_id'];

  //error_log("[login] logged into freeside with session_id=$freeside_session_id, setting cookie");

// now what?  for now, always redirect to the main page (or the select a
// customer diversion).
// eventually, other options?

  setcookie('freeside_session_id', $session_id);

  $GLOBALS['FREESIDE_LOGIN_RESPONSE'] = $response;

}

function freeside_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 {

    function FreesideSelfService() {
      $this;
    }

    public function __call($name, $arguments) {
    
        $URL = 'http://'. get_option('freeside_server'). ':8080';
        error_log("[FreesideSelfService] $name called, sending to ". $URL);

        $request = xmlrpc_encode_request("FS.ClientAPI_XMLRPC.$name", freeside_flatten($arguments[0]));
        $context = stream_context_create( array( 'http' => array(
            'method' => "POST",
            'header' => "Content-Type: text/xml",
            'content' => $request
        )));
        $file = file_get_contents($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;
        }
    }

}

?>