* @copyright 2005 Janrain, Inc.
* @license http://www.gnu.org/copyleft/lesser.html LGPL
*/
/**
* Data.
*/
$store_types = array("Filesystem" => "Auth_OpenID_FileStore",
"MySQL" => "Auth_OpenID_MySQLStore",
"PostgreSQL" => "Auth_OpenID_PostgreSQLStore",
"SQLite" => "Auth_OpenID_SQLiteStore");
/**
* Main.
*/
$messages = array();
session_start();
init_session();
if (!check_session()) {
render_form();
} else {
print generate_config();
}
/**
* Functions.
*/
function check_url($url) {
$p = parse_url($url);
if ($p === false) {
return false;
}
if (!array_key_exists('host', $p)) {
return false;
}
return true;
}
function build_url() {
$port = (($_SERVER['SERVER_PORT'] == 80) ? null : $_SERVER['SERVER_PORT']);
$parts = explode("/", $_SERVER['SERVER_PROTOCOL']);
$scheme = strtolower($parts[0]);
if ($port) {
return sprintf("%s://%s:%s%s/server.php", $scheme, $_SERVER['SERVER_NAME'], $port, dirname($_SERVER['PHP_SELF']));
} else {
return sprintf("%s://%s%s/server.php", $scheme, $_SERVER['SERVER_NAME'], dirname($_SERVER['PHP_SELF']));
}
}
function check_session() {
global $messages;
if ($_GET && isset($_GET['clear'])) {
session_destroy();
$_SESSION = array();
init_session();
return false;
}
if (isset($_GET['generate'])) {
if (!$_SESSION['server_url']) {
$messages[] = "Please enter a server URL.";
}
if (!$_SESSION['store_type']) {
$messages[] = "No store type chosen.";
} else {
switch ($_SESSION['store_type']) {
case "Filesystem":
if (!$_SESSION['store_data']['fs_path']) {
$messages[] = "Please specify a filesystem store path.";
}
break;
case "SQLite":
if (!$_SESSION['store_data']['sqlite_path']) {
$messages[] = "Please specify a SQLite database path.";
}
break;
default:
if (!($_SESSION['store_data']['host'] &&
$_SESSION['store_data']['database'] &&
$_SESSION['store_data']['username'] &&
$_SESSION['store_data']['password'])) {
$messages[] = "Please specify database connection details.";
}
}
}
}
if ($_SESSION['store_type'] &&
$_SESSION['server_url'] &&
(parse_url($_SESSION['server_url']) !== false) &&
((($_SESSION['store_type'] == 'Filesystem') &&
$_SESSION['store_data']['fs_path']) ||
(($_SESSION['store_type'] == 'SQLite') &&
$_SESSION['store_data']['sqlite_path']) ||
($_SESSION['store_data']['host'] &&
$_SESSION['store_data']['username'] &&
$_SESSION['store_data']['database'] &&
$_SESSION['store_data']['password']))) {
return true;
}
return false;
}
function render_form() {
global $store_types, $fields, $messages;
$basedir_msg = "";
if (ini_get('open_basedir')) {
$basedir_msg = "Note: Due to the ".
"open_basedir setting, be sure to ".
"choose a path in one of the following directories:
";
}
$sqlite_found = false;
if (extension_loaded('sqlite') ||
@dl('sqlite.' . PHP_SHLIB_SUFFIX)) {
$sqlite_found = true;
}
$mysql_found = false;
if (extension_loaded('mysql') ||
@dl('mysql.' . PHP_SHLIB_SUFFIX)) {
$mysql_found = true;
}
$pgsql_found = false;
if (extension_loaded('pgsql') ||
@dl('pgsql.' . PHP_SHLIB_SUFFIX)) {
$pgsql_found = true;
}
?>
This form will auto-generate an OpenID example server configuration for use with the OpenID server example.
} function init_session() { global $messages; // Set a guess value for the server url. if (!array_key_exists('server_url', $_SESSION)) { $_SESSION['server_url'] = build_url(); } foreach (array('server_url', 'include_path', 'store_type') as $key) { if (!isset($_SESSION[$key])) { $_SESSION[$key] = ""; } } if (!isset($_SESSION['store_data'])) { $_SESSION['store_data'] = array(); } if (!isset($_SESSION['users'])) { $_SESSION['users'] = array(); } if (!isset($_SESSION['trust_roots'])) { $_SESSION['trust_roots'] = array(); } foreach (array('server_url', 'include_path', 'store_type') as $field) { if (array_key_exists($field, $_GET)) { $_SESSION[$field] = $_GET[$field]; } } foreach (array('username', 'password', 'database', 'host', 'fs_path', 'sqlite_path') as $field) { if (array_key_exists($field, $_GET)) { $_SESSION['store_data'][$field] = $_GET[$field]; } } if ($_GET && isset($_GET['openid_url']) && isset($_GET['p1']) && isset($_GET['p2']) && $_GET['p1'] == $_GET['p2'] && $_GET['p1']) { if (check_url($_GET['openid_url'])) { $_SESSION['users'][$_GET['openid_url']] = sha1($_GET['p1']); } else { $messages[] = "Cannot add OpenID URL; '".$_GET['openid_url']."' doesn't look like a URL."; } } else if ($_GET && isset($_GET['trust_root']) && $_GET['trust_root']) { if (!in_array($_GET['trust_root'], $_SESSION['trust_roots'])) { $_SESSION['trust_roots'][] = $_GET['trust_root']; } } else if ($_GET && isset($_GET['del_user'])) { unset($_SESSION['users'][$_GET['del_user']]); } } function generate_config() { ?>Put the following text into print dirname(__FILE__); print DIRECTORY_SEPARATOR; ?>config.php.
if ($_SESSION['include_path']) { ?> /** * Set any extra include paths needed to use the library */ set_include_path(get_include_path() . PATH_SEPARATOR . " print $_SESSION['include_path']; ?>"); } ?> /** * The URL for the server. * * This is the location of server.php. For example: * * $server_url = 'http://example.com/~user/server.php'; * * This must be a full URL. */ $server_url = " print $_SESSION['server_url']; ?>"; /** * Initialize an OpenID store * * @return object $store an instance of OpenID store (see the * documentation for how to create one) */ function getOpenIDStore() { switch ($_SESSION['store_type']) { case "Filesystem": print "require_once \"Auth/OpenID/FileStore.php\";\n "; print "return new Auth_OpenID_FileStore(\"".$_SESSION['store_data']['fs_path']."\");\n"; break; case "SQLite": print "require_once \"Auth/OpenID/SQLiteStore.php\";\n "; print "return new Auth_OpenID_SQLiteStore(\"".$_SESSION['store_data']['sqlite_path']."\");\n"; break; case "MySQL": ?>require_once 'Auth/OpenID/MySQLStore.php'; require_once 'DB.php'; $dsn = array( 'phptype' => 'mysql', 'username' => ' print $_SESSION['store_data']['username']; ?>', 'password' => ' print $_SESSION['store_data']['password']; ?>', 'hostspec' => ' print $_SESSION['store_data']['host']; ?>' ); $db =& DB::connect($dsn); if (PEAR::isError($db)) { return null; } $db->query("USE print $_SESSION['store_data']['database']; ?>"); return new Auth_OpenID_MySQLStore($db); break; case "PostgreSQL": ?>require_once 'Auth/OpenID/PostgreSQLStore.php'; require_once 'DB.php'; $dsn = array( 'phptype' => 'pgsql', 'username' => ' print $_SESSION['store_data']['username']; ?>', 'password' => ' print $_SESSION['store_data']['password']; ?>', 'hostspec' => ' print $_SESSION['store_data']['host']; ?>', 'database' => ' print $_SESSION['store_data']['database']; ?>' ); $db =& DB::connect($dsn); if (PEAR::isError($db)) { return null; } return new Auth_OpenID_PostgreSQLStore($db); break; } ?> } /** * Users who are allowed to log in to this OpenID server. * * This is an array from URL to password hash. The URL must include * the proper OpenID server information in order to work with this * server. * * This must be set for the server to be usable. If it is not set, no * users will be able to log in. * * Example: * $openid_users = array( * 'http://joe.example.com/' => sha1('foo') * ) */ $openid_users = array( $i = 0; foreach ($_SESSION['users'] as $url => $hash) { $i++; print "\n '$url' => '$hash'"; if ($i < count($_SESSION['users'])) { print ","; } } ?> ); /** * Trusted sites is an array of trust roots. * * Sites in this list will not have to be approved by the user in * order to be used. It is OK to leave this value as-is. * * In a more robust server, this site should be a per-user setting. */ $trusted_sites = array( $i = 0; foreach ($_SESSION['trust_roots'] as $url) { $i++; print "\n '$url'"; if ($i < count($_SESSION['trust_roots'])) { print ","; } } ?> );} // end function generate_config () ?>