Skip to content

Instantly share code, notes, and snippets.

@ygerasimov
Created March 19, 2011 18:43
Show Gist options
  • Save ygerasimov/877700 to your computer and use it in GitHub Desktop.
Save ygerasimov/877700 to your computer and use it in GitHub Desktop.
<?php
// $Id$
/**
* Copyright (c) 2010 Madcap BV (http://www.madcap.nl)
* All rights reserved.
*
* Permission is granted for use, copying, modification, distribution,
* and distribution of modified versions of this work as long as the
* above copyright notice is included.
*/
/**
* @file gem_sync.inc
* Define connection object.
*/
require_once './includes/xmlrpc.inc';
/*
* Object used to connect to remote xmlrpc server
*/
class gem_sync {
// Username/password for connection.
var $username;
var $password;
// Url of remote server.
var $url;
// cookies, that are to be sent
var $cookie;
// Session ID.
var $sessid;
// Remote user object.
var $user;
/**
* Constructor. Opens session.
*/
function gem_sync() {
// assign variables
$this->username = variable_get('gem_sync_user', '');
$this->password = variable_get('gem_sync_password', '');
$host = variable_get('gem_sync_remote_url', '');
$this->url = $host . '/services/xmlrpc';
}
/**
* Log in to the remote server and save the session data.
*/
function login() {
// Do request to get anonymous sessid.
$result = $this->send(array('system.connect'), array());
// save anonymous sessid
$this->sessid = $result['sessid'];
// Do login.
$result = $this->send(array('user.login'), array($this->username, $this->password));
$this->sessid = $result['sessid'];
$this->user = $result['user'];
}
/**
* Logout from the remote server.
*/
function logout() {
$this->user = NULL;
return $this->send('user.logout', array());
}
/**
* Do the request to server
*
* @param str $method
* Name of method to call.
* @param array $args
* Argument to send.
* @return array $result
* First element result from drupal_http_request(), second element is parsed message.
*/
function send($protocol_args, $function_args, $noerror = FALSE) {
if (!is_array($protocol_args)) {
$protocol_args = array($protocol_args);
}
// In the end $protocol_args needs to be URL of XMLRPC endpoint, function name, session ID.
// The one exception to this is system.connect, which never requires a session id (it is the
// equivalent of your first page hit to a drupal site, establishing an anonymous session).
if ($protocol_args[0] == 'system.connect') {
$protocol_args = array($this->url, $protocol_args[0]);
}
else {
$protocol_args = array($this->url, $protocol_args[0], $this->sessid);
}
$output = call_user_func_array('xmlrpc', array_merge($protocol_args, $function_args));
if (!$noerror) {
// Error logging.
$error_message = xmlrpc_error_msg();
if (!empty($error_message)) {
if (module_exists('devel')) {
//dsm($error_message);
}
watchdog('gem_sync_xmlrpcsend', $error_message);
return array('error' => TRUE, 'message' => $error_message);
}
}
return $output;
}
/**
* Get saved remote user details
*
* @return <type>
*/
function get_remote_user() {
return $this->user;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment