+//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'] ) {
+error_log("DACOOKIE: ". $_COOKIE['freeside_session_id']);
+ $GLOBALS['FREESIDE_SESSION_ID'] = $_COOKIE['freeside_session_id'];
+ 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) {