Translation update done using Pootle.
[phpmyadmin/sankalp_k.git] / scripts / openid.php
blob9a5eb67eda30bcafa8cc7d59c147c2e9e4ab8240
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 require_once 'OpenID/RelyingParty.php';
21 /**
22 * Map of authenticated users to MySQL user/password pairs.
24 $AUTH_MAP = array(
25 'http://launchpad.net/~username' => array(
26 'user' => 'root',
27 'password' => '',
31 /**
32 * Simple function to show HTML page with given content.
34 function show_page($contents) {
35 header('Content-Type: text/html; charset=utf-8');
36 echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
38 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
39 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
40 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
41 <head>
42 <link rel="icon" href="../favicon.ico" type="image/x-icon" />
43 <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
44 <title>phpMyAdmin OpenID signon example</title>
45 </head>
46 <body>
47 <?php
48 if (isset($_SESSION) && isset($_SESSION['PMA_single_signon_error_message'])) {
49 echo '<p class="error">' . $_SESSION['PMA_single_signon_message'] . '</p>';
50 unset($_SESSION['PMA_single_signon_message']);
52 echo $contents;
54 </body>
55 </html>
56 <?php
59 /* Need to have cookie visible from parent directory */
60 session_set_cookie_params(0, '/', '', 0);
61 /* Create signon session */
62 $session_name = 'SignonSession';
63 session_name($session_name);
64 session_start();
66 // Determine realm and return_to
67 $base = 'http';
68 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
69 $base .= 's';
71 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
73 $realm = $base . '/';
74 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
75 if ($returnTo[strlen($returnTo) - 1] != '/') {
76 $returnTo .= '/';
78 $returnTo .= 'openid.php';
80 /* Display form */
81 if (!count($_GET) && !count($_POST) || isset($_GET['phpMyAdmin'])) {
82 /* Show simple form */
83 $content = '<form action="openid.php" method="post">
84 OpenID: <input type="text" name="identifier" /><br />
85 <input type="submit" name="start" />
86 </form>
87 </body>
88 </html>';
89 show_page($content);
90 exit;
93 /* Grab identifier */
94 if (isset($_POST['identifier'])) {
95 $identifier = $_POST['identifier'];
96 } else if (isset($_SESSION['identifier'])) {
97 $identifier = $_SESSION['identifier'];
98 } else {
99 $identifier = null;
102 /* Create OpenID object */
103 try {
104 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
105 } catch (OpenID_Exception $e) {
106 $contents = "<div class='relyingparty_results'>\n";
107 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
108 $contents .= "</div class='relyingparty_results'>";
109 show_page($contents);
110 exit;
113 /* Redirect to OpenID provider */
114 if (isset($_POST['start'])) {
115 try {
116 $authRequest = $o->prepare();
117 } catch (OpenID_Exception $e) {
118 $contents = "<div class='relyingparty_results'>\n";
119 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
120 $contents .= "</div class='relyingparty_results'>";
121 show_page($contents);
122 exit;
125 $url = $authRequest->getAuthorizeURL();
127 header("Location: $url");
128 exit;
129 } else {
130 /* Grab query string */
131 if (!count($_POST)) {
132 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
133 } else {
134 // I hate php sometimes
135 $queryString = file_get_contents('php://input');
138 /* Check reply */
139 $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
141 $id = $message->get('openid.claimed_id');
143 if (!empty($id) && isset($AUTH_MAP[$id])) {
144 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
145 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
146 session_write_close();
147 /* Redirect to phpMyAdmin (should use absolute URL here!) */
148 header('Location: ../index.php');
149 } else {
150 show_page('<p>User not allowed!</p>');
151 exit;