<?php
    
/*
     * MyUID PHP4 API V0.1.0 BETA
     * Copyright (c) 2004 Kevin O'Shea and MyUID
     * You are free to use and distribute this API
     * for free, but selling it for profit is strictly
     * prohibited.
     *
     * This software comes with NO WARRANTY
     */

    // THIS IS A BETA VERSION OF MYUID SOFTWARE, USE IT AT YOUR OWN RISK
    
    // We need the PEAR::XML_RPC library
    
require_once "XML/RPC.php";
    
    
/*
     * This is the main class for connectivity with the MyUID
     * user database.  http://www.myuid.com/
     */
    
class MyUID {
        
/* 
         * Most variables effecting MyUID's existince are stored in $_SESSION,
         * here is a list of the SESSION variables this class sets and what they
         * mean.
         */

        // in -- Boolean, whether the user is logged in
        // uid -- Integer, the users identification number
        // profile -- This is a hash array of all the profile data for the user,
        //      you can find out about the individual elements under load_profile()
        
        // connection to the MyUID server
        
var $client;
        
        
// Your API key
        
var $key "YOU MUST SET THIS";
        
        
/*
         * Constructor, connect to the XML-RPC server
         */
        
function MyUID()
        {
            
$this->client = new XML_RPC_Client("/api/xmlrpc.php""www.myuid.com"80);
            
$this->key = new XML_RPC_Value($this->key"string");
        }
        
        
/*
         * Start a session with special cookie expire times
         */
        
function start_session() {
            
$ses_class = new session();

            
session_set_save_handler (array(&$ses_class'_open'),
                 array(&
$ses_class'_close'),
                 array(&
$ses_class'_read'),
                 array(&
$ses_class'_write'),
                 array(&
$ses_class'_destroy'),
                 array(&
$ses_class'_gc'));

            if (!isset(
$_COOKIE['sesend'])) {
                
setcookie("sesend"time()+(20*60),
                    
time()+(20*60));
                
$_COOKIE['sesend'] = time()+(20*60);
            }
            
session_set_cookie_params($_COOKIE['sesend'] - time());
            
session_start();
        }

        
/*
         * Redirect the user to MyUID to check if they are actually
         * logged in, if already have data on the platter take care of
         * it.  $url should be a full url (http://youresite.com) to where
         * the data should come back (probably the current page).
         */
        
function login_check($url) {
            if (
$_SESSION['in'] != true) {
                if (isset(
$_GET['in'])) {
                    if (
$_GET['in'] == "false") {
                        return -
11;
                    } else
                        return 
$this->verify_login($_GET['in'], $_GET['code']);
                } else
                    
header("Location: http://www.myuid.com/api/checklogin.php" .
                        
"?return=" $url);
            } else
                return 
"true";
        }

        
/*
         * This function verifies that the login code is valid, and finds out
         * how much longer the user will be logged in for.  Then it calls a
         * function to load the users profile.
         */
        
function verify_login($uid$code) {
            
$ip = new XML_RPC_Value($_SERVER['REMOTE_ADDR'], "string");
            
$uid = new XML_RPC_Value($uid"int");
            
$code = new XML_RPC_Value($code"string");
            
$params = array($this->key$uid$ip$code);
            
$msg = new XML_RPC_Message("loginVerify"$params);
            
$response $this->client->send($msg);
            
$val $response->value();
            
$val $val->scalarval();
            if (
$val 0)
                return 
$val;
            else {
                
setcookie("sesend"$valtime()+((($val-time())*2)));
                
$_COOKIE['sesend'] = $val;
                
$_SESSION['uid'] = $uid->scalarval();
                
$_SESSION['in'] = true;
                
$this->update_profile();
                return 
0;
            }
        }

        
/*
         * This function compares timestamps on the local profile with the one
         * on the server, and then updates the profile if it has changed.  You
         * can run this every page load if you are so inclined.
         */
        
function update_profile() {
            if (
$_SESSION['in']) {
                
$uid = new XML_RPC_Value($_SESSION['uid'], "int");
                
$params = array($this->key$uid);
                
$msg = new XML_RPC_Message("checkTimes"$params);
                
$response $this->client->send($msg);
                
$vals $response->value();
                
$stamp $vals->arraymem(0);
                if (
$stamp->scalarval() != $_SESSION['profile']['stamp']) {
                    
$_SESSION['profile']['stamp'] = $stamp->scalarval();
                    
$this->load_profile($_SESSION['uid'], $_SESSION['profile']);
                }
                
$stamp $vals->arraymem(1);
                if (
trim($stamp->scalarval()) != trim($_COOKIE['sesend'])) {
                    
session_unset();
                    
session_destroy();
                }
            }
        }

        
/*
         * This function gets the users profile from the server, and then loads
         * it into $dest.  You only need to call this when the profile is out of
         * date or you need a new copy.
         */
        
function load_profile($uid, &$dest) {
            
$uid = new XML_RPC_Value($uid"int");
            
$params = array($this->key$uid);
            
$msg = new XML_RPC_Message("getProfile"$params);
            
$response $this->client->send($msg);
            
$vals $response->value();
            while (list(
$field$val) = $vals->structeach()) {
                if (
$field == "error")
                    return 
$val->scalarval();
                
$dest[$field] = $val->scalarval();
            }
        }
    }

    
/*
     * This is a custom session saving class, we use it so the session can last
     * a certain period of time, instead of only until normal garbage collection
     * occurs.  You can easily adapt this to save sessions in a database.
     */
    
class session {
        
// Variables for locating the session storage
        
var $path;
        var 
$name;
        
        
/* open a session */
        
function _open($path$name) {
            
$this->path $path;
              
$this->name $name;
            return 
true;
        }

        
/* close session */
        
function _close() {
            
// Nothing to do here
            
return true;
        }

        
/* read session data from file */
        
function _read($ses_id) {
            
$sess_file $this->path "/sess_$ses_id";
            if (
$fp = @fopen($sess_file"r")) {
                
$sess_data fgets($fp);
                return 
$sess_data;
            } else
                return 
"";
        }

        
/* write new session data to file */
        
function _write($ses_id$data) {
            
$sess_file $this->path "/sess_$ses_id";
            if (
$fp = @fopen($sess_file"w")) {
                return(
fwrite($fp$data));
            } else
                return 
false;
        }

        
/* Destroy session record file */
        
function _destroy($ses_id) {
            
$sess_file $this->path "/sess_$ses_id";
            unset(
$_SESSION);
            return(@
unlink($sess_file));
        }

        
/* Garbage collection, deletes old sessions */
        
function _gc($life) {
            if ((
time() > $_COOKIE['sesend']) || !isset($_COOKIE['sesend']))
                
$this->_destroy(session_id());
            return 
true;
        }
    }
?>