summaryrefslogtreecommitdiffstats
path: root/examples/server
diff options
context:
space:
mode:
authorJosh Hoyt <josh@janrain.com>2006-01-26 00:35:33 +0000
committerJosh Hoyt <josh@janrain.com>2006-01-26 00:35:33 +0000
commita5207076f5ea67febef7488243dec83d7bdc3d00 (patch)
tree6d4ea82bb0609e1900e521843fff3cddaca8202e /examples/server
parenta6d18d1e0f8f987654080be37455e92fcb0662a1 (diff)
downloadphp-openid-a5207076f5ea67febef7488243dec83d7bdc3d00.zip
php-openid-a5207076f5ea67febef7488243dec83d7bdc3d00.tar.gz
php-openid-a5207076f5ea67febef7488243dec83d7bdc3d00.tar.bz2
[project @ Added server example]
Diffstat (limited to 'examples/server')
-rw-r--r--examples/server/about.php18
-rw-r--r--examples/server/common.php109
-rw-r--r--examples/server/failure.php11
-rw-r--r--examples/server/idpage.inc29
-rw-r--r--examples/server/server.php35
-rw-r--r--examples/server/success.php11
6 files changed, 213 insertions, 0 deletions
diff --git a/examples/server/about.php b/examples/server/about.php
new file mode 100644
index 0000000..b8c7063
--- /dev/null
+++ b/examples/server/about.php
@@ -0,0 +1,18 @@
+<?php $title = 'PHP OpenID Server Example'; ?>
+<html>
+ <head>
+ <title><?php print $title; ?></title>
+ </head>
+ <body>
+ <h1><?php print $title; ?></h1>
+ <p>
+ This is an example PHP OpenID server. It is using the <a
+ href="http://www.openidenabled.com/openid/libraries/php">JanRain
+ PHP OpenID library</a>. This server will approve any request
+ for the URL <?php
+$esc_id = htmlspecialchars($success_identity, ENT_QUOTES);
+print "<a href='$esc_id'>$esc_id</a>";
+ ?> and reject requests for any other URL.
+ </p>
+ </body>
+</html>
diff --git a/examples/server/common.php b/examples/server/common.php
new file mode 100644
index 0000000..5eac8a0
--- /dev/null
+++ b/examples/server/common.php
@@ -0,0 +1,109 @@
+<?php
+
+$path_extra = dirname(dirname(dirname(__FILE__)));
+$path = ini_get('include_path');
+$path = $path_extra . ':' . $path;
+ini_set('include_path', $path);
+
+require_once "Auth/OpenID/Server.php";
+require_once "Auth/OpenID/Store/FileStore.php";
+
+define('DEFAULT_STORE_DIR', '/tmp/php_example_store');
+
+function serverRootURL()
+{
+ $server = $_SERVER['SERVER_NAME'];
+ $req_port = $_SERVER['SERVER_PORT'];
+
+ list($proto, $_) = explode('/', $_SERVER['SERVER_PROTOCOL'], 2);
+ $proto = strtolower($proto);
+
+ if ($proto != 'http') {
+ trigger_error("I don't know how to build a URL for $proto",
+ E_USER_WARNING);
+ return false;
+ }
+
+ if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on") {
+ $proto .= 's';
+ $default_port = '443';
+ } else {
+ $default_port = '80';
+ $port = ($req_port == '443') ? '' : (':' . $req_port);
+ }
+
+ $port = ($req_port == $default_port) ? "" : (":" . $req_port);
+
+ $pat = "%s://%s%s";
+ return sprintf($pat, $proto, $server, $port);
+}
+
+function getCurrentURL($full=false)
+{
+ $tail = $full ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME'];
+ return serverRootURL() . $tail;
+}
+
+function getParentURL()
+{
+ return serverRootURL() . dirname($_SERVER['SCRIPT_NAME']);
+}
+
+function relURL($path)
+{
+ if (substr($path, 0, 1) != '/') {
+ $path = '/' . $path;
+ }
+ return getParentURL() . $path;
+}
+
+function newServer($store_dir=DEFAULT_STORE_DIR, $server_url=null)
+{
+ if (!isset($server_url)) {
+ $server_url = getParentURL();
+ }
+ if (!file_exists($store_dir) && !mkdir($store_dir)) {
+ print "Could not create the FileStore directory '$store_path'. ".
+ " Please check the effective permissions.";
+ exit(0);
+ }
+
+ $store = new Auth_OpenID_FileStore($store_dir);
+ return new Auth_OpenID_Server($server_url, &$store);
+}
+
+function returnKV($kv)
+{
+ header('Content-Type: text/plain; charset=us-ascii');
+ print $kv;
+}
+
+function redirect($redir_url)
+{
+ header('Location: ' . $redir_url);
+ header('Content-Type: text/plain; charset=us-ascii');
+ print 'Please wait; you are being redirected to ' . $redir_url;
+}
+
+function showError($error, $status, $message)
+{
+ header('HTTP/1.1 ' . $status . ' ' . $message);
+ header('Content-Type: text/plain; charset=us-ascii');
+ print "An error occurred when processing your request:\n$error\n\n";
+ var_export($_SERVER);
+}
+
+function linkURL($url) {
+ $esc_url = htmlspecialchars($url, ENT_QUOTES);
+ return "<a href='$esc_url'>$esc_url</a>";
+}
+
+$parent = getParentURL();
+$success_identity = relURL('success.php');
+$failure_identity = relURL('failure.php');
+$server_url = relURL('server.php');
+
+$esc_server = htmlspecialchars($server_url, ENT_QUOTES);
+$esc_success = htmlspecialchars($success_identity, ENT_QUOTES);
+$esc_failure = htmlspecialchars($failure_identity, ENT_QUOTES);
+?> \ No newline at end of file
diff --git a/examples/server/failure.php b/examples/server/failure.php
new file mode 100644
index 0000000..683ee24
--- /dev/null
+++ b/examples/server/failure.php
@@ -0,0 +1,11 @@
+<?php
+require_once "common.php";
+
+$target = $failure_identity;
+$other_type = 'return success';
+$type = 'cancel';
+$title = 'Cancel Identity Page';
+$other = $success_identity;
+
+require "idpage.inc";
+?> \ No newline at end of file
diff --git a/examples/server/idpage.inc b/examples/server/idpage.inc
new file mode 100644
index 0000000..d7eda16
--- /dev/null
+++ b/examples/server/idpage.inc
@@ -0,0 +1,29 @@
+<?php
+if (getCurrentURL(true) != $target) {
+ redirect($target);
+ return;
+}
+
+$link_here = linkURL($target);
+$link_there = linkURL($other);
+
+print "<html>
+ <head>
+ <title>$title</title>
+ <link rel='openid.server' href='$esc_server' />
+ </head>
+ <body>
+ <h1>$title</h1>
+ <p>
+ This page is part of the
+ <a href='$esc_server'>PHP OpenID server example</a>.
+ Any requests to verify this URL ($link_here) will $type to consumers.
+ </p>
+ <p>
+ To test this server with a URL that will $other_type, use $link_there.
+ </p>
+ </body>
+</html>
+";
+
+?> \ No newline at end of file
diff --git a/examples/server/server.php b/examples/server/server.php
new file mode 100644
index 0000000..38b0c19
--- /dev/null
+++ b/examples/server/server.php
@@ -0,0 +1,35 @@
+<?php
+
+require_once "common.php";
+
+$server = newServer();
+
+function isAuthorized($identity_url, $trust_root) {
+ global $success_identity;
+ return ($identity_url === $success_identity);
+}
+
+list($status, $info) = $server->getOpenIDResponse('isAuthorized');
+
+switch($status) {
+case Auth_OpenID_REMOTE_ERROR:
+case Auth_OpenID_REMOTE_OK:
+ returnKV($info);
+ return;
+case Auth_OpenID_REDIRECT:
+ redirect($info);
+ return;
+case Auth_OpenID_DO_AUTH:
+ redirect($info->getCancelURL());
+case Auth_OpenID_DO_ABOUT:
+ include "about.php";
+ return;
+case Auth_OpenID_LOCAL_ERROR:
+ showError($info, '400', 'Bad request');
+ return;
+default:
+ $error = "Internal error: unknown status $status";
+ showError($error, '500', 'Internal error');
+ return;
+}
+?> \ No newline at end of file
diff --git a/examples/server/success.php b/examples/server/success.php
new file mode 100644
index 0000000..70031c2
--- /dev/null
+++ b/examples/server/success.php
@@ -0,0 +1,11 @@
+<?php
+require_once "common.php";
+
+$target = $success_identity;
+$type = 'return success';
+$other_type = 'cancel';
+$title = 'Success Identity Page';
+$other = $failure_identity;
+
+require "idpage.inc";
+?> \ No newline at end of file