Merge remote-tracking branch 'origin/QA_4_7' into QA_4_7
[phpmyadmin.git] / examples / openid.php
blob68b7dfed980dfd4fad494a98f667d001c36a666a
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 https://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 /* Change this to true if using phpMyAdmin over https */
24 $secure_cookie = false;
26 /**
27 * Map of authenticated users to MySQL user/password pairs.
29 $AUTH_MAP = array(
30 'https://launchpad.net/~username' => array(
31 'user' => 'root',
32 'password' => '',
36 /**
37 * Simple function to show HTML page with given content.
39 * @param string $contents Content to include in page
41 * @return void
43 function Show_page($contents)
45 header('Content-Type: text/html; charset=utf-8');
46 echo '<?xml version="1.0" encoding="utf-8"?>' , "\n";
48 <!DOCTYPE HTML>
49 <html lang="en" dir="ltr">
50 <head>
51 <link rel="icon" href="../favicon.ico" type="image/x-icon" />
52 <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
53 <meta charset="utf-8" />
54 <title>phpMyAdmin OpenID signon example</title>
55 </head>
56 <body>
57 <?php
58 if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
59 echo '<p class="error">' , $_SESSION['PMA_single_signon_message'] , '</p>';
60 unset($_SESSION['PMA_single_signon_message']);
62 echo $contents;
64 </body>
65 </html>
66 <?php
69 function Die_error($e)
71 $contents = "<div class='relyingparty_results'>\n";
72 $contents .= "<pre>" . htmlspecialchars($e->getMessage()) . "</pre>\n";
73 $contents .= "</div class='relyingparty_results'>";
74 Show_page($contents);
75 exit;
79 /* Need to have cookie visible from parent directory */
80 session_set_cookie_params(0, '/', '', $secure_cookie, true);
81 /* Create signon session */
82 $session_name = 'SignonSession';
83 session_name($session_name);
84 @session_start();
86 // Determine realm and return_to
87 $base = 'http';
88 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
89 $base .= 's';
91 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
93 $realm = $base . '/';
94 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
95 if ($returnTo[strlen($returnTo) - 1] != '/') {
96 $returnTo .= '/';
98 $returnTo .= 'openid.php';
100 /* Display form */
101 if (!count($_GET) && !count($_POST) || isset($_GET['phpMyAdmin'])) {
102 /* Show simple form */
103 $content = '<form action="openid.php" method="post">
104 OpenID: <input type="text" name="identifier" /><br />
105 <input type="submit" name="start" />
106 </form>
107 </body>
108 </html>';
109 Show_page($content);
110 exit;
113 /* Grab identifier */
114 if (isset($_POST['identifier']) && is_string($_POST['identifier'])) {
115 $identifier = $_POST['identifier'];
116 } else if (isset($_SESSION['identifier']) && is_string($_SESSION['identifier'])) {
117 $identifier = $_SESSION['identifier'];
118 } else {
119 $identifier = null;
122 /* Create OpenID object */
123 try {
124 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
125 } catch (Exception $e) {
126 Die_error($e);
129 /* Redirect to OpenID provider */
130 if (isset($_POST['start'])) {
131 try {
132 $authRequest = $o->prepare();
133 } catch (Exception $e) {
134 Die_error($e);
137 $url = $authRequest->getAuthorizeURL();
139 header("Location: $url");
140 exit;
141 } else {
142 /* Grab query string */
143 if (!count($_POST)) {
144 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
145 } else {
146 // I hate php sometimes
147 $queryString = file_get_contents('php://input');
150 /* Check reply */
151 try {
152 $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
153 } catch (Exception $e) {
154 Die_error($e);
157 $id = $message->get('openid.claimed_id');
159 if (!empty($id) && isset($AUTH_MAP[$id])) {
160 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
161 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
162 session_write_close();
163 /* Redirect to phpMyAdmin (should use absolute URL here!) */
164 header('Location: ../index.php');
165 } else {
166 Show_page('<p>User not allowed!</p>');
167 exit;