Improve select all checkbox process on designer and fix a bug
[phpmyadmin.git] / examples / openid.php
blobe44baef620967fb652baa8fbcbd57689e98e2ff2
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 /**
70 * Display error and exit
72 * @param Exception $e Exception object
74 * @return void
76 function Die_error($e)
78 $contents = "<div class='relyingparty_results'>\n";
79 $contents .= "<pre>" . htmlspecialchars($e->getMessage()) . "</pre>\n";
80 $contents .= "</div class='relyingparty_results'>";
81 Show_page($contents);
82 exit;
86 /* Need to have cookie visible from parent directory */
87 session_set_cookie_params(0, '/', '', $secure_cookie, true);
88 /* Create signon session */
89 $session_name = 'SignonSession';
90 session_name($session_name);
91 @session_start();
93 // Determine realm and return_to
94 $base = 'http';
95 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
96 $base .= 's';
98 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
100 $realm = $base . '/';
101 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
102 if ($returnTo[strlen($returnTo) - 1] != '/') {
103 $returnTo .= '/';
105 $returnTo .= 'openid.php';
107 /* Display form */
108 if (!count($_GET) && !count($_POST) || isset($_GET['phpMyAdmin'])) {
109 /* Show simple form */
110 $content = '<form action="openid.php" method="post">
111 OpenID: <input type="text" name="identifier" /><br />
112 <input type="submit" name="start" />
113 </form>
114 </body>
115 </html>';
116 Show_page($content);
117 exit;
120 /* Grab identifier */
121 if (isset($_POST['identifier']) && is_string($_POST['identifier'])) {
122 $identifier = $_POST['identifier'];
123 } elseif (isset($_SESSION['identifier']) && is_string($_SESSION['identifier'])) {
124 $identifier = $_SESSION['identifier'];
125 } else {
126 $identifier = null;
129 /* Create OpenID object */
130 try {
131 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
132 } catch (Exception $e) {
133 Die_error($e);
136 /* Redirect to OpenID provider */
137 if (isset($_POST['start'])) {
138 try {
139 $authRequest = $o->prepare();
140 } catch (Exception $e) {
141 Die_error($e);
144 $url = $authRequest->getAuthorizeURL();
146 header("Location: $url");
147 exit;
148 } else {
149 /* Grab query string */
150 if (!count($_POST)) {
151 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
152 } else {
153 // I hate php sometimes
154 $queryString = file_get_contents('php://input');
157 /* Check reply */
158 try {
159 $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
160 } catch (Exception $e) {
161 Die_error($e);
164 $id = $message->get('openid.claimed_id');
166 if (!empty($id) && isset($AUTH_MAP[$id])) {
167 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
168 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
169 session_write_close();
170 /* Redirect to phpMyAdmin (should use absolute URL here!) */
171 header('Location: ../index.php');
172 } else {
173 Show_page('<p>User not allowed!</p>');
174 exit;