Table of Contents

Ning authentication

What does it do?

It authenticates users by logging into a Ning community.

Installation

Paste the code into “inc/auth/ning.class.php” and replace

Then add the following line to “conf/local.php”:

$conf['authtype'] = 'ning';

Make sure that the file does not contain empty lines at the beginning and at the end, otherwise you will get PHP warnings.

WARNING: Passwords Sent As Plain Text

Username and password are sent as plaintext in the example code below.

To use a secure SSL connection modify the checkDomain function by setting $url to use https and adding a user agent to the curl options:

      function checkDomain($domain) {
          $url = "https://$domain/main/external/info?format=serialize";
          $curl = curl_init($url);
          curl_setopt ($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");   // set user agent to allow SSL to work
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
          $result = curl_exec($curl);
          if ($result && is_array($info = unserialize($result)) && $info['version']) {
              return true;
          }
          return false;
 

Code

ning.class.php
<?php
/**
 * auth/ning.class.php
 *
 * ning authorization class
 *
 * @author    Norbert Mocsnik
 */
 
define('DOKU_AUTH', dirname(__FILE__));
require_once(DOKU_AUTH . '/basic.class.php');
 
class NingConfig {
    function getDomain() {
        return 'myningnetwork.ning.com';
    }
 
    function getAdmins() {
        return array(
            'first@example.com',
            'second@example.com'
            );
    }
}
 
/**
 *  Ning Authentification API and plugin configuration options.
 *  Extracted from the Ning Auth API for Wordpress.
 */
class NingIdApi {
    /**
     *  Checks whether domain is valid.
     *
     *  @param      $domain     string      Domain name (w/o schema and path)
     *  @return     boolean
     */
    function checkDomain($domain) {
        $url = "http://$domain/main/external/info?format=serialize";
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($curl);
        if ($result && is_array($info = unserialize($result)) && $info['version']) {
            return true;
        }
        return false;
    }
 
    /**
     *  Authorizes ning user and returns some information about him/her.
     *  Returns array with user information or NULL if authentification fails
     *
     *  @param      $domain     string      Ning network domain (w/o schema and path)
     *  @param      $email      string      Screen name or email
     *  @param      $password   string      Password
     *  @return     {name, email, avatar_url}
     */
    function authorize($domain, $email, $password) {
        $url = "http://$domain/main/external/auth?format=serialize";
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, "email=".urlencode($email)."&password=".urlencode($password));
        curl_setopt($curl, CURLOPT_USERAGENT, "botd Mozilla/4.0 (Compatible; Ning Auth API)");
        $result = curl_exec($curl);
        if ($result && is_array($info = unserialize($result)) && count($info) && $info['email'] && $info['name']) {
            return $info;
        }
        return NULL;
    }
}
 
class auth_ning extends auth_basic {
    var $ning_name;
    var $ning_email;
    var $ning_grps;
 
    /**
     * Check user+password [ MUST BE OVERRIDDEN ]
     *
     * Checks if the given user exists and the given
     * plaintext password is correct
     *
     * May be ommited if trustExternal is used.
     *
     * @author  Norbert Mocsnik
     * @return  bool
     */
    function checkPass($user,$pass){
        if ($info = NingIdApi::authorize(NingConfig::getDomain(), $user, $pass)) {
            $this->ning_name = $info['name'];
            $this->ning_email = $info['email'];
 
            $this->ning_grps = array('user');
            if (in_array($user, NingConfig::getAdmins())) {
                $this->ning_grps[] = 'admin';
            }
 
            return true;
        }
 
        return false;
    }
 
    /**
     * Return user info [ MUST BE OVERRIDDEN ]
     *
     * Returns info about the given user needs to contain
     * at least these fields:
     *
     * name string  full name of the user
     * mail string  email addres of the user
     * grps array   list of groups the user is in
     *
     * @author  Norbert Mocsnik
     * @return  array containing user data or false
     */
    function getUserData($user) {
        return array(
            'name' => $this->ning_name,
            'mail' => $this->ning_email,
            'grps' => $this->ning_grps
        );
    }
}
 
 
?>