<?php
    
/*
     * MyUID PHP4 API V0.0.4 ALPHA
     * 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 IS AN ALPHA VERSION OF MYUID SOFTWARE, USE IT AT YOUR OWN RISK
     
    /*
     * 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()
        
        // variables for XML parsing
        
var $curtag;
        
        
// Your API key
        
var $key "YOU MUST SET THIS";
        
        
/*
         * Start a session with special cookie expire times
         */
        
function start_session() {
            if (
$_COOKIE['seslen'] < 20*60$_COOKIE['seslen'] = 20*60;
            
ini_set("session.gc_maxlifetime"$_COOKIE['seslen']);
            
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 
"You must log in at myuid.com";
                    } 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 $_SERVER['REMOTE_ADDR'];
            
$fp fopen("http://www.myuid.com/api/verifylogin.php?uid=" .
                
urlencode($uid) . "&code=" urlencode($code) . 
                
"&ip=" urlencode($ip), "r");
            
$val fgets($fp);
            
fclose($fp);
            if (
$val == "Malformed request")
                return 
$val;
            elseif (
$val == "-1")
                return 
"Login not valid";
            else {
                
setcookie("seslen"$val-time(), $val);
                
$_SESSION['ends'] = $val;
                
$_SESSION['uid'] = $uid;
                
$_SESSION['in'] = true;
                
$this->update_profile();
                return 
"true";
            }
        }

        
/*
         * 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() {
            
$fp fopen("http://www.myuid.com/api/modifytime.php?uid=" .
                
urlencode($_SESSION['uid']), "r");
            
$stamp trim(fgets($fp));
            if (
$stamp != $_SESSION['profile']['stamp']) {
                
$_SESSION['profile']['stamp'] = $stamp;
                
$this->load_profile();
            }
            
$stamp trim(fgets($fp));
            if (
$stamp != $_SESSION['ends']) {
                
session_unset();
                
session_destroy();
            }
        }

        
/*
         * This function downloads an XML file from the server which contains
         * the users profile.  It then parses it and loads it into the session.
         * This shouldn't be called if the profile is already loaded, unless you
         * just want to update it again.
         *
         * This XML parser is a bit sloppy at the moment, but it does the job.
         * Feel free to revise, or I will eventually.
         */
        
function load_profile() {
            
$fp fopen("http://www.myuid.com/api/usercard.php?uid=" .
                
$_SESSION['uid'] . "&code=" $this->key"r");
            
$xml_parser xml_parser_create();
            
xml_set_element_handler($xml_parser, array(&$this"start_element")
                , array(&
$this"end_element"));
            
xml_set_character_data_handler($xml_parser, array(&$this,
                
"xml_data"));
            while (
$data fgets($fp)) {
                if (!
xml_parse($xml_parser$datafeof($fp)))
                    die(
sprintf("XML error: %s at line %d",
                               
xml_error_string(xml_get_error_code($xml_parser)),
                               
xml_get_current_line_number($xml_parser)));
            }
            
xml_parser_free($xml_parser);
        }

        
/*
         * Parse start tags of XML, this is used for loading up the user's
         * profile.
         */
        
function start_element($parser$name$attrs) {
            
$this->curtag $name;
        }

        
/*
         * Parse end tags of XML
         */
        
function end_element($parser$name) {
            
$this->curtag NULL;
        }

        
/*
         * Parse data between tags
         */
        
function xml_data($parser$data) {
            if ((
$this->curtag != "") && ($data != "") && ($this->curtag !=
                
"USER"))
                
$_SESSION['profile'][strtolower($this->curtag)] = trim($data);
        }
    }
?>