Back to index

moin  1.9.0~rc2
Classes | Functions | Variables
MoinMoin.auth._PHPsessionParser Namespace Reference

Classes

class  UnknownObject

Functions

def transformList
def parseValue
def parseSession
def loadSession
def listSessions

Variables

string s_prefix = "sess_"
string s_path = "/tmp"
tuple a = time.clock()
tuple p_s = loadSession("...")

Detailed Description

    MoinMoin - Parsing of PHP session files

    @copyright: 2005 MoinMoin:AlexanderSchremmer (Thanks to Spreadshirt)
    @license: GNU GPL, see COPYING for details.

Function Documentation

def MoinMoin.auth._PHPsessionParser.listSessions (   path = s_path,
  prefix = s_prefix 
)
Lists all sessions in a particular directory. 

Definition at line 129 of file _PHPsessionParser.py.

00129 
00130 def listSessions(path=s_path, prefix=s_prefix):
00131     """ Lists all sessions in a particular directory. """
00132     return [os.path.basename(x).replace(s_prefix, '') for x in os.listdir(s_path)
00133             if x.startswith(s_prefix)]

def MoinMoin.auth._PHPsessionParser.loadSession (   key,
  path = s_path,
  prefix = s_prefix 
)
Loads a particular session from the directory. The key needs to be the
    session id. 

Definition at line 111 of file _PHPsessionParser.py.

00111 
00112 def loadSession(key, path=s_path, prefix=s_prefix):
00113     """ Loads a particular session from the directory. The key needs to be the
00114         session id. """
00115     key = key.lower()
00116     filename = os.path.join(path, prefix + wikiutil.taintfilename(key))
00117 
00118     try:
00119         f = open(filename, "rb")
00120     except IOError, e:
00121         if e.errno == 2:
00122             return None # session does not exist
00123         else:
00124             raise
00125 
00126     blob = f.read()
00127     f.close()
00128     return parseSession(blob)

Here is the call graph for this function:

Parses the outer structure that is similar to a dict. 

Definition at line 97 of file _PHPsessionParser.py.

00097 
00098 def parseSession(boxed):
00099     """ Parses the outer structure that is similar to a dict. """
00100     current_pos = 0
00101     session_dict = {}
00102     while current_pos < len(boxed):
00103         name_end = boxed.find("|", current_pos) # TODO: replace by .index()?
00104         name = boxed[current_pos:name_end]
00105         current_pos = name_end+1
00106         data, current_pos = parseValue(boxed, current_pos)
00107         current_pos += 1
00108         session_dict[name] = data
00109 
00110     return session_dict

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.auth._PHPsessionParser.parseValue (   string,
  start = 0 
)
Parses the inner structure. 

Definition at line 35 of file _PHPsessionParser.py.

00035 
00036 def parseValue(string, start=0):
00037     """ Parses the inner structure. """
00038     # TODO: replace "string" by something else
00039     #print "Parsing %r" % (string[start:], )
00040 
00041     val_type = string[start]
00042     try:
00043         header_end = string.index(':', 3+start)
00044         first_data = string[start+2:header_end]
00045     except ValueError:
00046         first_data = None
00047 
00048     #print "Saw type %r, first_data is %r." % (val_type, first_data)
00049     if val_type == 'a': # array (in Python rather a mixture of a list and a dict)
00050         i = 0
00051         items = []
00052 
00053         current_pos = header_end+2
00054         data = string
00055         while i != (int(first_data) * 2):
00056             item, current_pos = parseValue(data, current_pos)
00057             items.append(item)
00058             i += 1
00059             current_pos += 1
00060 
00061         t_list = list(transformList(items))
00062         try:
00063             result = dict(t_list) # note that dict does not retain the order
00064         except TypeError:
00065             result = list(t_list)
00066             #print "Warning, could not convert to dict: %r" %  (result, )
00067         return result, current_pos
00068 
00069     if val_type == 's': # string
00070         current_pos = header_end+2
00071         end = current_pos + int(first_data)
00072         data = string[current_pos:end]
00073         current_pos = end+1
00074         if data.startswith("a:"): #Sometimes, arrays are marshalled as strings.
00075             try:
00076                 data = parseValue(data, 0)[0]
00077             except ValueError: #Hmm, wrongly guessed. Just an ordinary string
00078                 pass
00079         return data, current_pos
00080 
00081     if val_type in ('i', 'b'): # integer or boolean
00082         current_pos = start+2
00083         str_buffer = ""
00084         while current_pos != len(string):
00085             cur_char = string[current_pos]
00086             if cur_char.isdigit() or cur_char == "-":
00087                 str_buffer += cur_char
00088             else:
00089                 cast = (val_type == 'i') and int or (lambda x: bool(int(x)))
00090                 return cast(str_buffer), current_pos
00091             current_pos += 1
00092 
00093     if val_type == "N": # Null, called None in Python
00094         return None, start+1
00095 
00096     return UnknownObject(start), start+1

Here is the call graph for this function:

Here is the caller graph for this function:

Transforms a list [1, 2, 3, 4, ...] into a
    [(1, 2), (3, 4), ...] generator. 

Definition at line 28 of file _PHPsessionParser.py.

00028 
00029 def transformList(items):
00030     """ Transforms a list [1, 2, 3, 4, ...] into a
00031         [(1, 2), (3, 4), ...] generator. """
00032     for i in xrange(0, len(items), 2):
00033         yield (items[i], items[i+1])
00034     raise StopIteration

Here is the caller graph for this function:


Variable Documentation

tuple MoinMoin.auth._PHPsessionParser.a = time.clock()

Definition at line 137 of file _PHPsessionParser.py.

Definition at line 140 of file _PHPsessionParser.py.

Definition at line 18 of file _PHPsessionParser.py.

Definition at line 17 of file _PHPsessionParser.py.