Overview

Namespaces

  • Mapbender
    • Component
      • HTTP
    • CoreBundle
      • Command
      • Component
        • Exception
      • Controller
      • DataFixtures
        • ORM
      • DependencyInjection
      • Element
        • Type
      • Entity
      • EventListener
      • Extension
      • Form
        • DataTransformer
        • EventListener
        • Type
      • Security
      • Template
    • DrupalIntegrationBundle
      • DependencyInjection
      • Security
        • Authentication
          • Provider
          • Token
        • Authorization
          • Voter
        • Factory
        • Firewall
        • User
      • Session
    • KmlBundle
      • Element
    • ManagerBundle
      • Controller
      • Form
        • DataTransformer
        • Type
    • MonitoringBundle
      • Command
      • Component
      • Controller
      • DependencyInjection
      • Entity
      • EventListener
      • Form
    • PrintBundle
      • Component
      • Controller
    • WmcBundle
      • Component
        • Exception
      • Element
        • Type
      • Entity
      • Form
        • EventListener
        • Type
    • WmsBundle
      • Component
        • Exception
      • Controller
      • DependencyInjection
      • Element
        • Type
      • Entity
      • Event
      • Form
        • EventListener
        • Type
    • WmtsBundle
      • Component
        • Exception
      • Controller
      • Entity
      • Form
        • Type
  • None
  • PHP

Classes

  • DrupalSessionHandler
  • DrupalSessionStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  • Download
  1: <?php
  2: 
  3: namespace Mapbender\DrupalIntegrationBundle\Session;
  4: 
  5: use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
  6: use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag;
  7: use Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy;
  8: use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
  9: use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
 10: use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
 11: 
 12: class DrupalSessionStorage implements SessionStorageInterface
 13: {
 14:     /**
 15:      * Array of SessionBagInterface
 16:      *
 17:      * @var array
 18:      */
 19:     protected $bags;
 20: 
 21:     /**
 22:      * @var boolean
 23:      */
 24:     protected $started = false;
 25: 
 26:     /**
 27:      * @var boolean
 28:      */
 29:     protected $closed = false;
 30: 
 31:     /**
 32:      * @var AbstractProxy
 33:      */
 34:     protected $saveHandler;
 35: 
 36:     /**
 37:      * @var MetadataBag
 38:      */
 39:     protected $metadataBag;
 40: 
 41:     /**
 42:      * Constructor.
 43:      *
 44:      * Depending on how you want the storage driver to behave you probably
 45:      * want to override this constructor entirely.
 46:      *
 47:      * List of options for $options array with their defaults.
 48:      * @see http://php.net/session.configuration for options
 49:      * but we omit 'session.' from the beginning of the keys for convenience.
 50:      *
 51:      * ("auto_start", is not supported as it tells PHP to start a session before
 52:      * PHP starts to execute user-land code. Setting during runtime has no effect).
 53:      *
 54:      * cache_limiter, "nocache" (use "0" to prevent headers from being sent entirely).
 55:      * cookie_domain, ""
 56:      * cookie_httponly, ""
 57:      * cookie_lifetime, "0"
 58:      * cookie_path, "/"
 59:      * cookie_secure, ""
 60:      * entropy_file, ""
 61:      * entropy_length, "0"
 62:      * gc_divisor, "100"
 63:      * gc_maxlifetime, "1440"
 64:      * gc_probability, "1"
 65:      * hash_bits_per_character, "4"
 66:      * hash_function, "0"
 67:      * name, "PHPSESSID"
 68:      * referer_check, ""
 69:      * serialize_handler, "php"
 70:      * use_cookies, "1"
 71:      * use_only_cookies, "1"
 72:      * use_trans_sid, "0"
 73:      * upload_progress.enabled, "1"
 74:      * upload_progress.cleanup, "1"
 75:      * upload_progress.prefix, "upload_progress_"
 76:      * upload_progress.name, "PHP_SESSION_UPLOAD_PROGRESS"
 77:      * upload_progress.freq, "1%"
 78:      * upload_progress.min-freq, "1"
 79:      * url_rewriter.tags, "a=href,area=href,frame=src,form=,fieldset="
 80:      *
 81:      * @param array       $options Session configuration options.
 82:      * @param object      $handler SessionHandlerInterface.
 83:      * @param MetadataBag $metaBag MetadataBag.
 84:      */
 85:     public function __construct(array $options = array(), $handler = null, MetadataBag $metaBag = null)
 86:     {
 87:         ini_set('session.cache_limiter', ''); // disable by default because it's managed by HeaderBag (if used)
 88:         ini_set('session.use_cookies', 1);
 89: 
 90:         if (version_compare(phpversion(), '5.4.0', '>=')) {
 91:             session_register_shutdown();
 92:         } else {
 93:             register_shutdown_function('session_write_close');
 94:         }
 95: 
 96:         $this->setMetadataBag($metaBag);
 97:         $this->setOptions($options);
 98:         $this->setSaveHandler($handler);
 99:     }
100: 
101:     /**
102:      * Gets the save handler instance.
103:      *
104:      * @return AbstractProxy
105:      */
106:     public function getSaveHandler()
107:     {
108:         return $this->saveHandler;
109:     }
110: 
111:     /**
112:      * {@inheritdoc}
113:      */
114:     public function start()
115:     {
116:         $this->loadSession();
117:         if (!$this->saveHandler->isWrapper() && !$this->saveHandler->isSessionHandlerInterface()) {
118:             $this->saveHandler->setActive(false);
119:         }
120: 
121:         $this->started = true;
122: 
123:         return true;
124:     }
125: 
126:     /**
127:      * {@inheritdoc}
128:      */
129:     public function getId()
130:     {
131:         if (!$this->started) {
132:             return ''; // returning empty is consistent with session_id() behaviour
133:         }
134: 
135:         return $this->saveHandler->getId();
136:     }
137: 
138:     /**
139:      * {@inheritdoc}
140:      */
141:     public function setId($id)
142:     {
143:         $this->saveHandler->setId($id);
144:     }
145: 
146:     /**
147:      * {@inheritdoc}
148:      */
149:     public function getName()
150:     {
151:         return $this->saveHandler->getName();
152:     }
153: 
154:     /**
155:      * {@inheritdoc}
156:      */
157:     public function setName($name)
158:     {
159:         $this->saveHandler->setName($name);
160:     }
161: 
162:     /**
163:      * {@inheritdoc}
164:      */
165:     public function regenerate($destroy = false, $lifetime = null)
166:     {
167:         if (null !== $lifetime) {
168:             ini_set('session.cookie_lifetime', $lifetime);
169:         }
170: 
171:         if ($destroy) {
172:             $this->metadataBag->stampNew();
173:         }
174: 
175:         return session_regenerate_id($destroy);
176:     }
177: 
178:     /**
179:      * {@inheritdoc}
180:      */
181:     public function save()
182:     {
183:         session_write_close();
184: 
185:         if (!$this->saveHandler->isWrapper() && !$this->getSaveHandler()->isSessionHandlerInterface()) {
186:             $this->saveHandler->setActive(false);
187:         }
188: 
189:         $this->closed = true;
190:     }
191: 
192:     /**
193:      * {@inheritdoc}
194:      */
195:     public function clear()
196:     {
197:         // clear out the bags
198:         foreach ($this->bags as $bag) {
199:             $bag->clear();
200:         }
201: 
202:         // clear out the session
203:         $_SESSION = array();
204: 
205:         // reconnect the bags to the session
206:         $this->loadSession();
207:     }
208: 
209:     /**
210:      * {@inheritdoc}
211:      */
212:     public function registerBag(SessionBagInterface $bag)
213:     {
214:         $this->bags[$bag->getName()] = $bag;
215:     }
216: 
217:     /**
218:      * {@inheritdoc}
219:      */
220:     public function getBag($name)
221:     {
222:         if (!isset($this->bags[$name])) {
223:             throw new \InvalidArgumentException(sprintf('The SessionBagInterface %s is not registered.', $name));
224:         }
225: 
226:         if ($this->saveHandler->isActive() && !$this->started) {
227:             $this->loadSession();
228:         } elseif (!$this->started) {
229:             $this->start();
230:         }
231: 
232:         return $this->bags[$name];
233:     }
234: 
235:     /**
236:      * Sets the MetadataBag.
237:      *
238:      * @param MetadataBag $metaBag
239:      */
240:     public function setMetadataBag(MetadataBag $metaBag = null)
241:     {
242:         if (null === $metaBag) {
243:             $metaBag = new MetadataBag();
244:         }
245: 
246:         $this->metadataBag = $metaBag;
247:     }
248: 
249:     /**
250:      * Gets the MetadataBag.
251:      *
252:      * @return MetadataBag
253:      */
254:     public function getMetadataBag()
255:     {
256:         return $this->metadataBag;
257:     }
258: 
259:     /**
260:      * {@inheritdoc}
261:      */
262:     public function isStarted()
263:     {
264:         return $this->started;
265:     }
266: 
267:     /**
268:      * Sets session.* ini variables.
269:      *
270:      * For convenience we omit 'session.' from the beginning of the keys.
271:      * Explicitly ignores other ini keys.
272:      *
273:      * @param array $options Session ini directives array(key => value).
274:      *
275:      * @see http://php.net/session.configuration
276:      */
277:     public function setOptions(array $options)
278:     {
279:         $validOptions = array_flip(array(
280:             'cache_limiter', 'cookie_domain', 'cookie_httponly',
281:             'cookie_lifetime', 'cookie_path', 'cookie_secure',
282:             'entropy_file', 'entropy_length', 'gc_divisor',
283:             'gc_maxlifetime', 'gc_probability', 'hash_bits_per_character',
284:             'hash_function', 'name', 'referer_check',
285:             'serialize_handler', 'use_cookies',
286:             'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled',
287:             'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name',
288:             'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags',
289:         ));
290: 
291:         foreach ($options as $key => $value) {
292:             if (isset($validOptions[$key])) {
293:                 ini_set('session.'.$key, $value);
294:             }
295:         }
296:     }
297: 
298:     /**
299:      * Registers save handler as a PHP session handler.
300:      *
301:      * To use internal PHP session save handlers, override this method using ini_set with
302:      * session.save_handlers and session.save_path e.g.
303:      *
304:      *     ini_set('session.save_handlers', 'files');
305:      *     ini_set('session.save_path', /tmp');
306:      *
307:      * @see http://php.net/session-set-save-handler
308:      * @see http://php.net/sessionhandlerinterface
309:      * @see http://php.net/sessionhandler
310:      *
311:      * @param object $saveHandler Default null means NativeProxy.
312:      */
313:     public function setSaveHandler($saveHandler = null)
314:     {
315:         // Wrap $saveHandler in proxy
316:         if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
317:             $saveHandler = new SessionHandlerProxy($saveHandler);
318:         } elseif (!$saveHandler instanceof AbstractProxy) {
319:             $saveHandler = new NativeProxy();
320:         }
321: 
322:         $this->saveHandler = $saveHandler;
323: 
324:         if ($this->saveHandler instanceof \SessionHandlerInterface) {
325:             if (version_compare(phpversion(), '5.4.0', '>=')) {
326:                 session_set_save_handler($this->saveHandler, false);
327:             } else {
328:                 session_set_save_handler(
329:                     array($this->saveHandler, 'open'),
330:                     array($this->saveHandler, 'close'),
331:                     array($this->saveHandler, 'read'),
332:                     array($this->saveHandler, 'write'),
333:                     array($this->saveHandler, 'destroy'),
334:                     array($this->saveHandler, 'gc')
335:                 );
336:             }
337:         }
338:     }
339: 
340:     /**
341:      * Load the session with attributes.
342:      *
343:      * After starting the session, PHP retrieves the session from whatever handlers
344:      * are set to (either PHP's internal, or a custom save handler set with session_set_save_handler()).
345:      * PHP takes the return value from the read() handler, unserializes it
346:      * and populates $_SESSION with the result automatically.
347:      *
348:      * @param array|null $session
349:      */
350:     protected function loadSession(array &$session = null)
351:     {
352:         if (null === $session) {
353:             $session = &$_SESSION;
354:         }
355: 
356:         //die($session);
357:         //var_dump($this->metadataBag);
358: 
359:         $bags = array_merge($this->bags, array($this->metadataBag));
360: 
361:         foreach ($bags as $bag) {
362:             $key = $bag->getStorageKey();
363:             $session[$key] = isset($session[$key]) ? $session[$key] : array();
364:             $bag->initialize($session[$key]);
365:         }
366: 
367:         $this->started = true;
368:         $this->closed = false;
369:     }
370: }
371: 
Mapbender3 API documenation API documentation generated by ApiGen 2.8.0