Overview

Namespaces

  • Composer
    • Autoload
  • Guzzle
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
        • Header
      • QueryAggregator
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Mock
    • Stream
  • Mockery
    • Adapter
      • Phpunit
    • CountValidator
    • Exception
    • Generator
      • StringManipulation
        • Pass
    • Loader
    • Matcher
  • None
  • Omnipay
    • Common
      • Exception
      • Message
    • Dummy
      • Message
    • Fatzebra
      • Message
  • PHP
  • Symfony
    • Component
      • EventDispatcher
        • Debug
        • DependencyInjection
        • Tests
          • Debug
          • DependencyInjection
      • HttpFoundation
        • File
          • Exception
          • MimeType
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
        • Tests
          • File
            • MimeType
          • Session
            • Attribute
            • Flash
            • Storage
              • Handler
              • Proxy
      • Yaml
        • Exception
        • Tests

Classes

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