~ubuntu-branches/ubuntu/gutsy/lasso/gutsy

« back to all changes in this revision

Viewing changes to php/examples/sample-sp/assertionConsumer.php

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2004-09-13 09:26:34 UTC
  • Revision ID: james.westby@ubuntu.com-20040913092634-01vdfl8j9cp94exa
Tags: upstream-0.4.1
ImportĀ upstreamĀ versionĀ 0.4.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*  
 
3
 * Service Provider Example -- AssertionConsumer
 
4
 *
 
5
 * Copyright (C) 2004 Entr'ouvert
 
6
 * http://lasso.entrouvert.org
 
7
 * 
 
8
 * Authors: Christophe Nowicki <cnowicki@easter-eggs.com>
 
9
 *
 
10
 * This program is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published by
 
12
 * the Free Software Foundation; either version 2 of the License, or
 
13
 * (at your option) any later version.
 
14
 * 
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 */
 
24
 
 
25
  $config = unserialize(file_get_contents('config.inc'));
 
26
  
 
27
  require_once 'DB.php';
 
28
  
 
29
 
 
30
  if (!$_GET['SAMLart']) {
 
31
        exit(1);
 
32
  }
 
33
  
 
34
  session_start();
 
35
 
 
36
  lasso_init();
 
37
 
 
38
  $server_dump = file_get_contents($config['server_dump_filename']);
 
39
 
 
40
  $server = LassoServer::newfromdump($server_dump);
 
41
 
 
42
  $login = new LassoLogin($server);
 
43
 
 
44
  $login->initRequest($_SERVER['QUERY_STRING'], lassoHttpMethodRedirect);
 
45
  $login->buildRequestMsg();
 
46
 
 
47
  $url = parse_url($login->msgUrl);
 
48
 
 
49
  $soap = sprintf(
 
50
        "POST %s HTTP/1.1\r\nHost: %s:%d\r\nContent-Length: %d\r\nContent-Type: text/xml\r\n\r\n%s\r\n",
 
51
  $url['path'], $url['host'], $url['port'], strlen($login->msgBody), $login->msgBody);
 
52
 
 
53
  # PHP 4.3.0 with OpenSSL support required
 
54
  $fp = fsockopen("ssl://" . $url['host'], $url['port'], $errno, $errstr, 30) or die($errstr ($errno));
 
55
  fwrite($fp, $soap);
 
56
  $ret = fgets($fp);
 
57
 
 
58
  if (!preg_match("/^HTTP\/1\\.. 200/i", $ret)) {
 
59
        die("Wrong artifact");
 
60
  }
 
61
 
 
62
  while (!feof($fp)) {
 
63
        $reponse .= @fread($fp, 8192);
 
64
  }
 
65
 
 
66
  fclose($fp);
 
67
 
 
68
  list($header, $body) = preg_split("/(\r\n\r\n|\n\n)/", $reponse, 2);
 
69
 
 
70
  $login->processResponseMsg($body); 
 
71
 
 
72
  $db = &DB::connect($config['dsn']);
 
73
 
 
74
  if (DB::isError($db)) 
 
75
          die($db->getMessage());
 
76
 
 
77
  $query = "SELECT user_id FROM nameidentifiers WHERE name_identifier='" . $login->nameIdentifier . "'"; 
 
78
  $res =& $db->query($query);
 
79
 
 
80
  if (DB::isError($res)) 
 
81
          die($res->getMessage());
 
82
 
 
83
  if ($res->numRows() > 0)
 
84
  {
 
85
        // User already exist in the database
 
86
        $row =& $res->fetchRow();
 
87
    $user_id = $row[0];
 
88
 
 
89
        # Get Identity Dump from the data base
 
90
        $query = "SELECT identity_dump FROM users WHERE user_id='$user_id'";
 
91
        $res =& $db->query($query);
 
92
 
 
93
        if (DB::isError($db)) 
 
94
          die($db->getMessage());
 
95
 
 
96
        $row =& $res->fetchRow();
 
97
 
 
98
        $login->setIdentityFromDump($row[0]);
 
99
 
 
100
        $res->free();
 
101
        
 
102
        $login->acceptSso();
 
103
        
 
104
        $session = $login->session; 
 
105
  
 
106
        $_SESSION["nameidentifier"] = $login->nameIdentifier;
 
107
        $_SESSION["session_dump"] = $session->dump();
 
108
        $_SESSION["user_id"] = $user_id;
 
109
 
 
110
        $url = "index.php?SID=". $SID;
 
111
  }
 
112
  else 
 
113
  {
 
114
    // New User
 
115
        $login->acceptSso();
 
116
 
 
117
        $identity = $login->identity;
 
118
        $identity_dump = $identity->dump();
 
119
 
 
120
        $session = $login->session;
 
121
 
 
122
        // Insert into users 
 
123
        $identity_dump_quoted = $db->quoteSmart($identity_dump);
 
124
        $query = "INSERT INTO users (user_id,identity_dump,created) VALUES(nextval('user_id_seq'), $identity_dump_quoted, NOW())";
 
125
        $res =& $db->query($query);
 
126
        if (DB::isError($res)) 
 
127
                print $res->getMessage(). "\n";
 
128
 
 
129
        // Get UserID
 
130
        $query = "SELECT last_value FROM user_id_seq";
 
131
        $res =& $db->query($query);
 
132
        if (DB::isError($res)) 
 
133
                print $res->getMessage(). "\n";
 
134
        $row = $res->fetchRow();
 
135
        $user_id = $row[0];
 
136
 
 
137
        // Insert into nameidentifiers
 
138
        $query = "INSERT INTO nameidentifiers VALUES('".$login->nameIdentifier."', '$user_id')";
 
139
        $res =& $db->query($query);
 
140
        if (DB::isError($res)) 
 
141
                print $res->getMessage(). "\n";
 
142
        
 
143
 
 
144
        $_SESSION["nameidentifier"] = $login->nameIdentifier;
 
145
        $_SESSION["session_dump"] = $session->dump();
 
146
        $_SESSION["user_id"] = $user_id;
 
147
 
 
148
        $url = "register.php?SID=". $SID;
 
149
  }
 
150
  
 
151
  // Update last_login
 
152
  $query = "UPDATE users SET last_login=NOW() WHERE user_id='$user_id'";
 
153
  $res =& $db->query($query);
 
154
  if (DB::isError($res)) 
 
155
        print $res->getMessage(). "\n";
 
156
 
 
157
  $db->disconnect();
 
158
        
 
159
  lasso_shutdown();
 
160
 
 
161
  header("Request-URI: $url");
 
162
  header("Content-Location: $url");
 
163
  header("Location: $url");
 
164
  exit();
 
165
?>