updated adodb package to work with php 7.1
[openemr.git] / phpmyadmin / examples / openid.php
blob7d9c44b3c3cd3d37dc6e7a7aa729fbd8033460ee
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 * @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 /* Need to have cookie visible from parent directory */
67 session_set_cookie_params(0, '/', '', false);
68 /* Create signon session */
69 $session_name = 'SignonSession';
70 session_name($session_name);
71 session_start();
73 // Determine realm and return_to
74 $base = 'http';
75 if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
76 $base .= 's';
78 $base .= '://' . $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'];
80 $realm = $base . '/';
81 $returnTo = $base . dirname($_SERVER['PHP_SELF']);
82 if ($returnTo[/*overload*/mb_strlen($returnTo) - 1] != '/') {
83 $returnTo .= '/';
85 $returnTo .= 'openid.php';
87 /* Display form */
88 if (!count($_GET) && !count($_POST) || isset($_GET['phpMyAdmin'])) {
89 /* Show simple form */
90 $content = '<form action="openid.php" method="post">
91 OpenID: <input type="text" name="identifier" /><br />
92 <input type="submit" name="start" />
93 </form>
94 </body>
95 </html>';
96 Show_page($content);
97 exit;
100 /* Grab identifier */
101 if (isset($_POST['identifier'])) {
102 $identifier = $_POST['identifier'];
103 } else if (isset($_SESSION['identifier'])) {
104 $identifier = $_SESSION['identifier'];
105 } else {
106 $identifier = null;
109 /* Create OpenID object */
110 try {
111 $o = new OpenID_RelyingParty($returnTo, $realm, $identifier);
112 } catch (OpenID_Exception $e) {
113 $contents = "<div class='relyingparty_results'>\n";
114 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
115 $contents .= "</div class='relyingparty_results'>";
116 Show_page($contents);
117 exit;
120 /* Redirect to OpenID provider */
121 if (isset($_POST['start'])) {
122 try {
123 $authRequest = $o->prepare();
124 } catch (OpenID_Exception $e) {
125 $contents = "<div class='relyingparty_results'>\n";
126 $contents .= "<pre>" . $e->getMessage() . "</pre>\n";
127 $contents .= "</div class='relyingparty_results'>";
128 Show_page($contents);
129 exit;
132 $url = $authRequest->getAuthorizeURL();
134 header("Location: $url");
135 exit;
136 } else {
137 /* Grab query string */
138 if (!count($_POST)) {
139 list(, $queryString) = explode('?', $_SERVER['REQUEST_URI']);
140 } else {
141 // I hate php sometimes
142 $queryString = file_get_contents('php://input');
145 /* Check reply */
146 $message = new OpenID_Message($queryString, OpenID_Message::FORMAT_HTTP);
148 $id = $message->get('openid.claimed_id');
150 if (!empty($id) && isset($AUTH_MAP[$id])) {
151 $_SESSION['PMA_single_signon_user'] = $AUTH_MAP[$id]['user'];
152 $_SESSION['PMA_single_signon_password'] = $AUTH_MAP[$id]['password'];
153 session_write_close();
154 /* Redirect to phpMyAdmin (should use absolute URL here!) */
155 header('Location: ../index.php');
156 } else {
157 Show_page('<p>User not allowed!</p>');
158 exit;