Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / examples / openid.php
blob563b23032a8a5da39601f33d3872143fcd2f279f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Single signon for phpMyAdmin using OpenID
6 * This is just example how to use single signon with phpMyAdmin, it is
7 * not intended to be perfect code and look, only shows how you can
8 * integrate this functionality in your application.
10 * It uses OpenID pear package, see http://pear.php.net/package/OpenID
12 * User first authenticates using OpenID and based on content of $AUTH_MAP
13 * the login information is passed to phpMyAdmin in session data.
15 * @package PhpMyAdmin
16 * @subpackage Example
19 if (false === @include_once 'OpenID/RelyingParty.php') {
20 exit;
23 /**
24 * Map of authenticated users to MySQL user/password pairs.
26 $AUTH_MAP = array(
27 'http://launchpad.net/~username' => array(
28 'user' => 'root',
29 'password' => '',
33 /**
34 * Simple function to show HTML page with given content.
36 * @return void
38 function show_page($contents)
40 header('Content-Type: text/html; charset=utf-8');
41 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
43 <!DOCTYPE HTML>
44 <html lang="en" dir="ltr">
45 <head>
46 <link rel="icon" href="../favicon.ico" type="image/x-icon" />
47 <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
48 <meta charset="utf-8" />
49 <title>phpMyAdmin OpenID signon example</title>
50 </head>
51 <body>
52 <?php
53 if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
54 echo '<p class="error">' . $_SESSION['PMA_single_signon_message'] . '</p>';
55 unset($_SESSION['PMA_single_signon_message']);
57 echo $contents;
59 </body>
60 </html>
61 <?php
64 /* Need to have cookie visible from parent directory */
65 session_set_cookie_params(0, '/', '', 0);
66 /* Create signon session */
67 $session_name = 'SignonSession';
68 session_name($session_name);
69 session_start();
71 // Determine realm and return_to
72 $base = 'http';
73 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
74 $base .= 's';
76 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
78 $realm = $base . '/';
79 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
80 if ($returnTo[strlen($returnTo) - 1] != '/') {
81 $returnTo .= '/';
83 $returnTo .= 'openid.php';
85 /* Display form */
86 if (!count($_GET) && !count($_POST) || isset($_GET['phpMyAdmin'])) {
87 /* Show simple form */
88 $content = '<form action="openid.php" method="post">
89 OpenID: <input type="text" name="identifier" /><br />
90 <input type="submit" name="start" />
91 </form>
92 </body>
93 </html>';
94 show_page($content);
95 exit;
98 /* Grab identifier */
99 if (isset($_POST['identifier'])) {
100 $identifier = $_POST['identifier'];
101 } else if (isset($_SESSION['identifier'])) {
102 $identifier = $_SESSION['identifier'];
103 } else {
104 $identifier = null;
107 /* Create OpenID object */
108 try {
109 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
110 } catch (OpenID_Exception $e) {
111 $contents = "<div class='relyingparty_results'>\n";
112 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
113 $contents .= "</div class='relyingparty_results'>";
114 show_page($contents);
115 exit;
118 /* Redirect to OpenID provider */
119 if (isset($_POST['start'])) {
120 try {
121 $authRequest = $o->prepare();
122 } catch (OpenID_Exception $e) {
123 $contents = "<div class='relyingparty_results'>\n";
124 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
125 $contents .= "</div class='relyingparty_results'>";
126 show_page($contents);
127 exit;
130 $url = $authRequest->getAuthorizeURL();
132 header("Location: $url");
133 exit;
134 } else {
135 /* Grab query string */
136 if (!count($_POST)) {
137 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
138 } else {
139 // I hate php sometimes
140 $queryString = file_get_contents('php://input');
143 /* Check reply */
144 $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
146 $id = $message->get('openid.claimed_id');
148 if (!empty($id) && isset($AUTH_MAP[$id])) {
149 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
150 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
151 session_write_close();
152 /* Redirect to phpMyAdmin (should use absolute URL here!) */
153 header('Location: ../index.php');
154 } else {
155 show_page('<p>User not allowed!</p>');
156 exit;