hs-php-session-0.0.9.3: PHP session and values serialization

Copyright(c) 2013-2014 Edward Blake
LicenseBSD-style
MaintainerEdward L. Blake <edwardlblake@gmail.com>
Stabilityexperimental
Portabilityportable
Safe HaskellSafe
LanguageHaskell98

Data.PHPSession

Contents

Description

Encodes and decodes serialized PHP sessions in the format used by the "php" setting for session.serialize_handler, as well as encodes and decodes PHP values in general in the format used by PHP's serialize/unserialize.

An example of using decodePHPSessionValue and convFrom to decode and convert values from the serialized contents of a ByteString to [(Int,ByteString)]:

import qualified Data.PHPSession as PHPSess

getArrayValues :: LBS.ByteString -> [(Int, LBS.ByteString)]
getArrayValues encoded =
  case PHPSess.decodePHPSessionValue encoded of
    Nothing -> [] :: [(Int,LBS.ByteString)]
    Just b -> PHPSess.convFrom b

Starting from a value output from the following PHP code:

<?php echo serialize(array(0 => 'Hello', 5 => 'World')); /* Outputs: "a:2:{i:0;s:5:\"Hello\";i:5;s:5:\"World\";}" */

The following can be computed:

>>> getArrayValues $ LBS.pack "a:2:{i:0;s:5:\"Hello\";i:5;s:5:\"World\";}"
[(0,"Hello"),(5,"World")]
Synopsis

Decode from ByteString

decodePHPSession :: ByteString -> Maybe PHPSessionVariableList #

Decodes a ByteString containing a serialization of a list of session variables using the "php" session serialization format into a PHPSessionVariableList

decodePHPSessionValue :: ByteString -> Maybe PHPSessionValue #

Decodes a ByteString containing a session serialization of a value into a PHPSessionValue. The format being decoded is similar if not probably the same format used by PHP's serialize/unserialize functions. Nothing is returned if the input bytestring could not be parsed correctly.

Encode to ByteString

encodePHPSession :: PHPSessionVariableList -> ByteString #

Encode a PHPSessionVariableList into a ByteString containing the serialization of a list of session variables using the "php" session serialization format.

encodePHPSessionValue :: PHPSessionValue -> ByteString #

Encode a PHPSessionValue into a ByteString containing the serialization of a PHP value. The format being encoded into is similar if not probably the same format used by PHP's serialize/unserialize functions.

Convert to PHPSessionValue

convTo :: ConversionToPHPValue a => a -> PHPSessionValue #

convTo is a convenience function that converts natively typed values to PHPSessionValue, with the resulting PHP type determined by the type cast or inferred.

arrayOfPHPStrings :: PHPSessionValue
arrayOfPHPStrings =
  let str1 = "Hello" :: BS.ByteString
      str2 = "World"
   in convTo [(0 :: Int, str1), (1, str2)]

In the above example code, the OverloadedStrings language extension is assumed.

Convert from PHPSessionValue

convFrom :: ConversionFromPHPValueOrMismatch b => PHPSessionValue -> b #

convFrom and convFromSafe are convenience functions that translate PHP values stored as PHPSessionValue into appropriate Haskell types depending on the desired type cast or inferred. Functions provided in this module provide non-coerced type translations and so will either carry on the translation or signal the fact that the attempted conversion will alter the type of the value. For situations where altering value types is expected, alternative conversion functions with similar type signatures are provided in modules within Data.PHPSession.ImplicitConv.

The example arrayOfPHPStrings definition given in convTo can be reverted back to Haskell types, which evaluates to [(0,"Hello"),(1,"World")].

>>> convFrom arrayOfPHPStrings :: [(Int,LBS.ByteString)]
[(0,"Hello"),(1,"World")]

However, if the desired type signature is changed to a completely different type, then a runtime exception is thrown:

>>> convFrom arrayOfPHPStrings :: [(Int,Int)]
*** Exception: Type mismatch converting from (PHPSessionValueString "Hello") to Int

Where there is the possibility that the value being sought may be NULL, the type should be (Maybe a).

convFromSafe :: ConversionFromPHPValueOrMismatch b => PHPSessionValue -> Either String b #

A version of convFrom which returns a Left with an error message instead of throwing an exception.

Decode only part of a ByteString

decodePartialPHPSessionValue :: ByteString -> Maybe (PHPSessionValue, ByteString) #

Decodes as much of a ByteString as needed into a PHPSessionValue and returns the rest of the string. Decoding ends at either the end of the string or when the extent of the current nested structure is met when an extra closing curly brace is encountered. The format being decoded is similar if not probably the same format used by PHP's serialize/unserialize functions. Nothing is returned if the input bytestring could not be parsed correctly.

PHP session types

type PHPSessionVariableList = [(ByteString, PHPSessionValue)] #

Holds the "top-level" session variables and their value contents.

data PHPSessionClassName #

Represents the name of a PHP class.

Instances
Eq PHPSessionClassName # 
Instance details

Defined in Data.PHPSession.Types

Show PHPSessionClassName # 
Instance details

Defined in Data.PHPSession.Types

ConversionFromPHPValueOrMismatch (PHPSessionClassName, [(PHPSessionValue, PHPSessionValue)]) # 
Instance details

Defined in Data.PHPSession.Conv

ConversionFromPHPValueOrMismatch (PHPSessionClassName, ByteString) # 
Instance details

Defined in Data.PHPSession.Conv

ConversionToPHPValue (PHPSessionClassName, [(PHPSessionValue, PHPSessionValue)]) # 
Instance details

Defined in Data.PHPSession.Conv

ConversionToPHPValue (PHPSessionClassName, ByteString) # 
Instance details

Defined in Data.PHPSession.Conv

ConversionFromPHPImplicitValueOrMismatch (PHPSessionClassName, [(PHPSessionValue, PHPSessionValue)]) # 
Instance details

Defined in Data.PHPSession.ImplicitConv.PHPTypeCoercion

ConversionFromPHPImplicitValueOrMismatch (PHPSessionClassName, ByteString) # 
Instance details

Defined in Data.PHPSession.ImplicitConv.PHPTypeCoercion

data PHPSessionValue #

PHPSessionValue Represents a PHP value, which may be a number, string, array, object, boolean, null, or references.

Instances
Eq PHPSessionValue # 
Instance details

Defined in Data.PHPSession.Types

Ord PHPSessionValue # 
Instance details

Defined in Data.PHPSession.Types

Show PHPSessionValue # 
Instance details

Defined in Data.PHPSession.Types

ConversionToPHPValue PHPSessionValue # 
Instance details

Defined in Data.PHPSession.Conv

ConversionFromPHPValueOrMismatch a => ConversionFromPHPValueOrMismatch [(a, PHPSessionValue)] # 
Instance details

Defined in Data.PHPSession.Conv

ConversionFromPHPValueOrMismatch b => ConversionFromPHPValueOrMismatch [(PHPSessionValue, b)] # 
Instance details

Defined in Data.PHPSession.Conv

ConversionFromPHPValueOrMismatch [(PHPSessionValue, PHPSessionValue)] # 
Instance details

Defined in Data.PHPSession.Conv

ConversionToPHPValue [(PHPSessionValue, PHPSessionValue)] # 
Instance details

Defined in Data.PHPSession.Conv

ConversionFromPHPImplicitValueOrMismatch b => ConversionFromPHPImplicitValueOrMismatch [(PHPSessionValue, b)] # 
Instance details

Defined in Data.PHPSession.ImplicitConv.PHPTypeCoercion

ConversionFromPHPImplicitValueOrMismatch [(PHPSessionValue, PHPSessionValue)] # 
Instance details

Defined in Data.PHPSession.ImplicitConv.PHPTypeCoercion

ConversionFromPHPValueOrMismatch (PHPSessionClassName, [(PHPSessionValue, PHPSessionValue)]) # 
Instance details

Defined in Data.PHPSession.Conv

ConversionToPHPValue (PHPSessionClassName, [(PHPSessionValue, PHPSessionValue)]) # 
Instance details

Defined in Data.PHPSession.Conv

ConversionFromPHPImplicitValueOrMismatch (PHPSessionClassName, [(PHPSessionValue, PHPSessionValue)]) # 
Instance details

Defined in Data.PHPSession.ImplicitConv.PHPTypeCoercion

data PHPSessionAttr #

PHPSessionAttr are values associated with PHPSessionValueMisc to inspect and generally re-encode the necessary information for that value.