Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) 2004, 2005 Fabien Potencier <fabien.potencier@symfony-project.com>
6
 * (c) 2004, 2005 Sean Kerr <sean@code-box.org>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
/**
13
 * Provides support for session storage using a PDO database abstraction layer.
14
 *
15
 * <b>parameters:</b> see sfDatabaseSessionStorage
16
 *
17
 * @package    symfony
18
 * @subpackage storage
19
 * @author     Mathew Toth <developer@poetryleague.com>
20
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
21
 * @author     Sean Kerr <sean@code-box.org>
22
 * @version    SVN: $Id: sfPDOSessionStorage.class.php 13143 2008-11-18 22:22:01Z FabianLange $
23
 */
24
class sfPDOSessionStorage extends sfDatabaseSessionStorage
25
{
26
  /**
27
   * Destroys a session.
28
   *
29
   * @param  string $id  A session ID
30
   *
31
   * @return bool true, if the session was destroyed, otherwise an exception is thrown
32
   *
33
   * @throws <b>DatabaseException</b> If the session cannot be destroyed
34
   */
35
  public function sessionDestroy($id)
36
  {
37
    // get table/column
38
    $db_table  = $this->options['db_table'];
39
    $db_id_col = $this->options['db_id_col'];
40
 
41
    // delete the record associated with this id
42
    $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_id_col.'= ?';
43
 
44
    try
45
    {
46
      $stmt = $this->db->prepare($sql);
47
      $stmt->bindParam(1, $id, PDO::PARAM_STR);
48
      $stmt->execute();
49
    }
50
    catch (PDOException $e)
51
    {
52
      throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
53
    }
54
 
55
    return true;
56
  }
57
 
58
  /**
59
   * Cleans up old sessions.
60
   *
61
   * @param  int $lifetime  The lifetime of a session
62
   *
63
   * @return bool true, if old sessions have been cleaned, otherwise an exception is thrown
64
   *
65
   * @throws <b>DatabaseException</b> If any old sessions cannot be cleaned
66
   */
67
  public function sessionGC($lifetime)
68
  {
69
    // get table/column
70
    $db_table    = $this->options['db_table'];
71
    $db_time_col = $this->options['db_time_col'];
72
 
73
    // delete the record associated with this id
74
    $sql = 'DELETE FROM '.$db_table.' WHERE '.$db_time_col.' < '.(time() - $lifetime);
75
 
76
    try
77
    {
78
      $this->db->query($sql);
79
    }
80
    catch (PDOException $e)
81
    {
82
      throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
83
    }
84
 
85
    return true;
86
  }
87
 
88
  /**
89
   * Reads a session.
90
   *
91
   * @param  string $id  A session ID
92
   *
93
   * @return string      The session data if the session was read or created, otherwise an exception is thrown
94
   *
95
   * @throws <b>DatabaseException</b> If the session cannot be read
96
   */
97
  public function sessionRead($id)
98
  {
99
    // get table/columns
100
    $db_table    = $this->options['db_table'];
101
    $db_data_col = $this->options['db_data_col'];
102
    $db_id_col   = $this->options['db_id_col'];
103
    $db_time_col = $this->options['db_time_col'];
104
 
105
    try
106
    {
107
      $sql = 'SELECT '.$db_data_col.' FROM '.$db_table.' WHERE '.$db_id_col.'=?';
108
 
109
      $stmt = $this->db->prepare($sql);
110
      $stmt->bindParam(1, $id, PDO::PARAM_STR, 255);
111
 
112
      $stmt->execute();
113
      // it is recommended to use fetchAll so that PDO can close the DB cursor
114
      // we anyway expect either no rows, or one row with one column. fetchColumn, seems to be buggy #4777
115
      $sessionRows = $stmt->fetchAll(PDO::FETCH_NUM);
116
      if (count($sessionRows) == 1)
117
      {
118
        return $sessionRows[0][0];
119
      }
120
      else
121
      {
122
        // session does not exist, create it
123
        $sql = 'INSERT INTO '.$db_table.'('.$db_id_col.', '.$db_data_col.', '.$db_time_col.') VALUES (?, ?, ?)';
124
 
125
        $stmt = $this->db->prepare($sql);
126
        $stmt->bindParam(1, $id, PDO::PARAM_STR);
127
        $stmt->bindValue(2, '', PDO::PARAM_STR);
128
        $stmt->bindValue(3, time(), PDO::PARAM_INT);
129
        $stmt->execute();
130
 
131
        return '';
132
      }
133
    }
134
    catch (PDOException $e)
135
    {
136
      throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
137
    }
138
  }
139
 
140
  /**
141
   * Writes session data.
142
   *
143
   * @param  string $id    A session ID
144
   * @param  string $data  A serialized chunk of session data
145
   *
146
   * @return bool true, if the session was written, otherwise an exception is thrown
147
   *
148
   * @throws <b>DatabaseException</b> If the session data cannot be written
149
   */
150
  public function sessionWrite($id, $data)
151
  {
152
    // get table/column
153
    $db_table    = $this->options['db_table'];
154
    $db_data_col = $this->options['db_data_col'];
155
    $db_id_col   = $this->options['db_id_col'];
156
    $db_time_col = $this->options['db_time_col'];
157
 
158
    $sql = 'UPDATE '.$db_table.' SET '.$db_data_col.' = ?, '.$db_time_col.' = '.time().' WHERE '.$db_id_col.'= ?';
159
 
160
    try
161
    {
162
      $stmt = $this->db->prepare($sql);
163
      $stmt->bindParam(1, $data, PDO::PARAM_STR);
164
      $stmt->bindParam(2, $id, PDO::PARAM_STR);
165
      $stmt->execute();
166
    }
167
    catch (PDOException $e)
168
    {
169
      throw new sfDatabaseException(sprintf('PDOException was thrown when trying to manipulate session data. Message: %s', $e->getMessage()));
170
    }
171
 
172
    return true;
173
  }
174
}