Skip to content

Commit 854d05b

Browse files
committed
Imported code for the Social Authentication module.
0 parents  commit 854d05b

File tree

207 files changed

+32085
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

207 files changed

+32085
-0
lines changed

Changelog

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Changelog:
2+
----------
3+
4+
2010-10-17 1.0.0 Maurice Makaay
5+
6+
- Initial release.
7+

README

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Module : Social Authentication
2+
Version : 1.0.0
3+
Authors : Maurice Makaay
4+
5+
This module provides OpenID (e.g. Google, Yahoo) and oAuth (Twitter, Facebook)
6+
authentication support for Phorum.
7+
8+
9+
1. Installation:
10+
----------------
11+
12+
- Unpack the archive;
13+
14+
- Move the directory "social_authentication" to the directory "mods"
15+
within your Phorum installation;
16+
17+
- Login as administrator in Phorum's administrative interface and
18+
go to the "Modules" section;
19+
20+
- Enable the module "Social Authentication".
21+

TODO

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Setting: can administrators login via OpenID?

api.php

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
if (!defined('PHORUM')) return;
4+
5+
/**
6+
* Load the configuration of all available authentication providers.
7+
*
8+
* @return array
9+
* An array, containing a list of providers. Each provider in the
10+
* list is an array, containing the data from the provider's
11+
* config.ini file.
12+
*/
13+
function mod_social_authentication_list_providers()
14+
{
15+
$providers = array();
16+
17+
$dir = dirname(__FILE__) . '/providers';
18+
$dh = opendir($dir);
19+
if (!$dh) trigger_error("Cannot open directory: $dir", E_USER_ERROR);
20+
21+
while ($entry = readdir($dh))
22+
{
23+
if ($entry[0] == '.' || $entry == 'TEMPLATE') continue;
24+
if (!file_exists("$dir/$entry/config.ini") ||
25+
!file_exists("$dir/$entry/button.png")) {
26+
phorum_admin_error(
27+
"Provider '" . htmlspecialchars($entry) . "' does not " .
28+
"contain both the files config.ini and button.png"
29+
);
30+
continue;
31+
}
32+
33+
$providers[$entry] = parse_ini_file("$dir/$entry/config.ini", TRUE);
34+
$providers[$entry]['id'] = $entry;
35+
}
36+
37+
uasort($providers, 'mod_social_authentication_cmp_providers');
38+
39+
return $providers;
40+
}
41+
42+
function mod_social_authentication_cmp_providers($a, $b)
43+
{
44+
$name_a = strtolower($a['provider']['name']);
45+
$name_b = strtolower($b['provider']['name']);
46+
return strcmp($name_a, $name_b);
47+
}
48+
49+
/**
50+
* Load the configuration for an authentication provider.
51+
*
52+
* @param string $id
53+
* @return array
54+
*/
55+
function mod_social_authentication_get_provider($id)
56+
{
57+
$id = basename($id);
58+
$file = dirname(__FILE__) . '/providers/' . $id . '/config.ini';
59+
if (file_exists($file)) {
60+
$config = parse_ini_file($file, TRUE);
61+
} else {
62+
trigger_error("Illegal provider requested: $id", E_USER_ERROR);
63+
}
64+
65+
return $config;
66+
}
67+
68+
?>

db.php

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
if (!defined("PHORUM")) return;
4+
5+
// The table name for storing authentication associations.
6+
$GLOBALS["PHORUM"]["socialauth_table"] =
7+
"{$GLOBALS["PHORUM"]["DBCONFIG"]["table_prefix"]}_user_socialauth";
8+
9+
/**
10+
* Retrieve the information for an authentication id association.
11+
*
12+
* @param string $auth_id
13+
* The authentication id for which to retrieve information.
14+
* @return NULL|array
15+
* An array containing data for the OpenID (fields are user_id,
16+
* add_datetime and openid) or NULL if the OpenID is not available.
17+
*/
18+
function socialauth_db_get($auth_id)
19+
{
20+
global $PHORUM;
21+
22+
$auth_id = phorum_db_interact(DB_RETURN_QUOTED, $auth_id);
23+
24+
return phorum_db_interact(
25+
DB_RETURN_ASSOC,
26+
"SELECT *
27+
FROM {$PHORUM['socialauth_table']}
28+
WHERE auth_id = '$auth_id'"
29+
);
30+
}
31+
32+
/**
33+
* Retrieve the authentication id's that are associated to a given user.
34+
*
35+
* @param integer $user_id
36+
* The id of the user for which to retrieve the authentication associations.
37+
* @return array
38+
* An array containing data record for the authentication associations.
39+
* Each data record is an array that contains the fields user_id,
40+
* add_datetime and auth_id.
41+
*/
42+
function socialauth_get_for_user($user_id)
43+
{
44+
global $PHORUM;
45+
46+
settype($user_id, 'int');
47+
48+
return phorum_db_interact(
49+
DB_RETURN_ASSOCS,
50+
"SELECT *
51+
FROM {$PHORUM['socialauth_table']}
52+
WHERE user_id = $user_id"
53+
);
54+
}
55+
56+
/**
57+
* Associate an authentication id with a user.
58+
*
59+
* @param string $auth_id
60+
* The authentication id to associate with a user.
61+
* @param integer $user_id
62+
* The id of the user to associate the OpenID with.
63+
*/
64+
function socialauth_db_associate($auth_id, $user_id)
65+
{
66+
global $PHORUM;
67+
68+
settype($user_id, 'int');
69+
70+
// Check if the user exists.
71+
$user = phorum_db_user_get($user_id);
72+
if ($user === NULL) trigger_error(
73+
"socialauth_db_associate(): " .
74+
"cannot associate $auth_id with user " .
75+
"id $user_id: no such user", E_USER_ERROR
76+
);
77+
78+
// Check if the association already exists.
79+
$assoc = socialauth_db_get($auth_id);
80+
if ($assoc !== NULL) {
81+
if ($assoc['user_id'] === $user_id) {
82+
return; // already set to the correct user_id
83+
} else trigger_error(
84+
"socialauth_db_associate(): " .
85+
"cannot associate $auth_id with user " .
86+
"id $user_id: the OpenID is already associated with user " .
87+
"id {$assoc['user_id']}", E_USER_ERROR
88+
);
89+
}
90+
91+
// All is ok. Create the association.
92+
$auth_id = phorum_db_interact(DB_RETURN_QUOTED, $auth_id);
93+
phorum_db_interact(
94+
DB_RETURN_RES,
95+
"INSERT INTO {$PHORUM['socialauth_table']}
96+
(auth_id, user_id, add_datetime)
97+
VALUES ('$auth_id', $user_id, " . time() . ")"
98+
);
99+
}
100+
101+
/**
102+
* Disassociate an authentication id from a user.
103+
*
104+
* @param string $auth_id
105+
* The authentication id to disassociate from a user.
106+
* @param integer $user_id
107+
* The id of the user to disassociate the authentication id from.
108+
*/
109+
function socialauth_db_disassociate($auth_id, $user_id)
110+
{
111+
global $PHORUM;
112+
113+
$auth_id = phorum_db_interact(DB_RETURN_QUOTED, $auth_id);
114+
settype($user_id, 'int');
115+
116+
phorum_db_interact(
117+
DB_RETURN_RES,
118+
"DELETE FROM {$PHORUM['socialauth_table']}
119+
WHERE auth_id = '$auth_id'
120+
AND
121+
user_id = $user_id"
122+
);
123+
}
124+
?>

db/upgrade/mysql/2010110601.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
if(!empty($PHORUM['DBCONFIG']['charset'])) {
4+
$charset_str = " DEFAULT CHARACTER SET {$PHORUM['DBCONFIG']['charset']}";
5+
} else {
6+
$charset_str = "";
7+
}
8+
9+
$table = "{$PHORUM["DBCONFIG"]["table_prefix"]}_user_socialauth";
10+
11+
$upgrade_queries[]="
12+
CREATE TABLE $table (
13+
user_id int unsigned NOT NULL default '0',
14+
auth_id varchar(255) NOT NULL default '',
15+
add_datetime int unsigned NOT NULL default '0',
16+
17+
PRIMARY KEY (user_id, auth_id),
18+
UNIQUE KEY (auth_id)
19+
)$charset_str
20+
";
21+
22+
?>

images/ajax-loader.gif

2.49 KB
Loading

0 commit comments

Comments
 (0)