Translated using Weblate (Interlingua)
[phpmyadmin.git] / examples / openid.php
blob0db0a466b8fbc7888352baa7d57ee31c80e54e09
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 /**
24 * Map of authenticated users to MySQL user/password pairs.
26 $AUTH_MAP = array(
27 'https://launchpad.net/~username' => array(
28 'user' => 'root',
29 'password' => '',
33 /**
34 * Simple function to show HTML page with given content.
36 * @param string $contents Content to include in page
38 * @return void
40 function Show_page($contents)
42 header('Content-Type: text/html; charset=utf-8');
43 echo '<?xml version="1.0" encoding="utf-8"?>' , "\n";
45 <!DOCTYPE HTML>
46 <html lang="en" dir="ltr">
47 <head>
48 <link rel="icon" href="../favicon.ico" type="image/x-icon" />
49 <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
50 <meta charset="utf-8" />
51 <title>phpMyAdmin OpenID signon example</title>
52 </head>
53 <body>
54 <?php
55 if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
56 echo '<p class="error">' , $_SESSION['PMA_single_signon_message'] , '</p>';
57 unset($_SESSION['PMA_single_signon_message']);
59 echo $contents;
61 </body>
62 </html>
63 <?php
66 function Die_error($e)
68 $contents = "<div class='relyingparty_results'>\n";
69 $contents .= "<pre>" . htmlspecialchars($e->getMessage()) . "</pre>\n";
70 $contents .= "</div class='relyingparty_results'>";
71 Show_page($contents);
72 exit;
76 /* Need to have cookie visible from parent directory */
77 session_set_cookie_params(0, '/', '', true, true);
78 /* Create signon session */
79 $session_name = 'SignonSession';
80 session_name($session_name);
81 @session_start();
83 // Determine realm and return_to
84 $base = 'http';
85 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
86 $base .= 's';
88 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
90 $realm = $base . '/';
91 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
92 if ($returnTo[mb_strlen($returnTo) - 1] != '/') {
93 $returnTo .= '/';
95 $returnTo .= 'openid.php';
97 /* Display form */
98 if (!count($_GET) && !count($_POST) || isset($_GET['phpMyAdmin'])) {
99 /* Show simple form */
100 $content = '<form action="openid.php" method="post">
101 OpenID: <input type="text" name="identifier" /><br />
102 <input type="submit" name="start" />
103 </form>
104 </body>
105 </html>';
106 Show_page($content);
107 exit;
110 /* Grab identifier */
111 if (isset($_POST['identifier']) && is_string($_POST['identifier'])) {
112 $identifier = $_POST['identifier'];
113 } else if (isset($_SESSION['identifier']) && is_string($_SESSION['identifier'])) {
114 $identifier = $_SESSION['identifier'];
115 } else {
116 $identifier = null;
119 /* Create OpenID object */
120 try {
121 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
122 } catch (Exception $e) {
123 Die_error($e);
126 /* Redirect to OpenID provider */
127 if (isset($_POST['start'])) {
128 try {
129 $authRequest = $o->prepare();
130 } catch (Exception $e) {
131 Die_error($e);
134 $url = $authRequest->getAuthorizeURL();
136 header("Location: $url");
137 exit;
138 } else {
139 /* Grab query string */
140 if (!count($_POST)) {
141 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
142 } else {
143 // I hate php sometimes
144 $queryString = file_get_contents('php://input');
147 /* Check reply */
148 try {
149 $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
150 } catch (Exception $e) {
151 Die_error($e);
154 $id = $message->get('openid.claimed_id');
156 if (!empty($id) && isset($AUTH_MAP[$id])) {
157 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
158 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
159 session_write_close();
160 /* Redirect to phpMyAdmin (should use absolute URL here!) */
161 header('Location: ../index.php');
162 } else {
163 Show_page('<p>User not allowed!</p>');
164 exit;