sessionmodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 666 行 · 第 1/2 页

JAVA
666
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.quercus.lib.session;import com.caucho.quercus.annotation.Optional;import com.caucho.quercus.env.*;import com.caucho.quercus.lib.OutputModule;import com.caucho.quercus.module.AbstractQuercusModule;import com.caucho.quercus.module.ModuleStartupListener;import com.caucho.quercus.module.IniDefinitions;import com.caucho.quercus.module.IniDefinition;import com.caucho.util.L10N;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletResponse;import java.util.logging.Logger;import java.util.Iterator;/** * Quercus session handling */public class SessionModule extends AbstractQuercusModule   implements ModuleStartupListener {  private static final L10N L = new L10N(SessionModule.class);  private static final Logger log    = Logger.getLogger(SessionModule.class.getName());  private static final IniDefinitions _iniDefinitions = new IniDefinitions();  /**   * Returns the default php.ini values.   */  public IniDefinitions getIniDefinitions()  {    return _iniDefinitions;  }  public String []getLoadedExtensions()  {    return new String[] { "session" };  }  public void startup(Env env)  {    if (env.getConfigVar("session.auto_start").toBoolean())      session_start(env);  }  /**   * Returns and/or sets the value of session.cache_limiter, affecting the   * cache related headers that are sent as a result of a call to   * {@link #session_start(Env)}.   *   * If the optional parameter is not supplied, this function simply returns the existing value.   * If the optional parameter is supplied, the returned value   * is the old value that was set before the new value is applied.   *   * Valid values are "nocache" (the default), "private", "private_no_expire",   * and "public". If a value other than these values is supplied, then a warning is produced   * and no cache related headers will be sent to the client.   */  public Value session_cache_limiter(Env env, @Optional String newValue)  {    Value value = env.getIni("session.cache_limiter");    if (newValue == null || "".equals(newValue)) // XXX: php/1k16      return value;    env.setIni("session.cache_limiter", newValue);    return value;  }  public Value session_cache_expire(Env env, @Optional Value newValue)  {    Value value = (LongValue) env.getSpecialValue("cache_expire");    if (value == null)      value = env.getIni("session.cache_expire");    if (newValue != null && ! newValue.isDefault())      env.setSpecialValue("cache_expire", newValue);    return LongValue.create(value.toLong());  }  /**   * Alias of session_write_close.   */  public static Value session_commit(Env env)  {    return session_write_close(env);  }  /**   * Encodes the session values.   */  public static boolean session_decode(Env env, StringValue value)  {    Value session = env.getGlobalValue("_SESSION");    if (! (session instanceof SessionArrayValue)) {      env.warning(L.l("session_decode requires valid session"));      return false;    }    return ((SessionArrayValue)session).decode(env, value.toString());  }  /**   * Encodes the session values.   */  public static String session_encode(Env env)  {    Value session = env.getGlobalValue("_SESSION");    if (! (session instanceof SessionArrayValue)) {      env.warning(L.l("session_encode requires valid session"));      return null;    }    return ((SessionArrayValue)session).encode();  }  /**   * Destroys the session   */  public static boolean session_destroy(Env env)  {    SessionArrayValue session = env.getSession();    if (session == null)      return false;    env.destroySession(session.getId());    return true;  }  /**   * Returns the session cookie parameters   */  public static ArrayValue session_get_cookie_params(Env env)  {    ArrayValue array = new ArrayValueImpl();    array.put(env, "lifetime", env.getIniLong("session.cookie_lifetime"));    array.put(env, "path", env.getIniString("session.cookie_path"));    array.put(env, "domain", env.getIniString("session.cookie_domain"));    array.put(env, "secure", env.getIniBoolean("session.cookie_secure"));    return array;  }  /**   * Returns the session id   */  public static String session_id(Env env, @Optional String id)  {    Value sessionIdValue = (Value) env.getSpecialValue("caucho.session_id");    String oldValue;    if (sessionIdValue != null)      oldValue = sessionIdValue.toString();    else      oldValue = "";    if (id != null && ! "".equals(id))      env.setSpecialValue("caucho.session_id", env.createString(id));    return oldValue;  }  /**   * Returns true if a session variable is registered.   */  public static boolean session_is_registered(Env env, String name)  {    return env.getGlobalValue("_SESSION").get(env.createString(name)).isset();  }  /**   * Returns the object's class name   */  public Value session_module_name(Env env, @Optional String newValue)  {    Value value = env.getIni("session.save_handler");    if (newValue != null && ! newValue.equals(""))      env.setIni("session.save_handler", newValue);    return value;  }  /**   * Returns the object's class name   */  public Value session_name(Env env, @Optional String newValue)  {    Value value = env.getIni("session.name");    if (newValue != null && ! newValue.equals(""))      env.setIni("session.name", newValue);    return value;  }  /**   * Regenerates the session id.   *    * This function first creates a new session id.  The old session values are   * migrated to this new session.  Then a new session cookie is sent (XXX: send   * only if URL rewriting is off?).  Changing the session ID should be transparent.   * Therefore, session callbacks should not be called.   */  public static boolean session_regenerate_id(Env env,                                              @Optional boolean deleteOld)  {    // php/1k82, php/1k83    if (env.getSession() == null)      return ! deleteOld;        String sessionId = env.generateSessionId();        if (deleteOld) {      session_destroy(env);      SessionArrayValue session = env.createSession(sessionId, true);    }    else {      SessionArrayValue session = env.getSession();            session.setId(sessionId);    }    // update environment to new session id    session_id(env, sessionId);    if (env.getIni("session.use_cookies").toBoolean())      generateSessionCookie(env, sessionId);    return true;  }  /**   * Registers global variables in the session.   */  public boolean session_register(Env env, Value []values)  {    Value session = env.getGlobalValue("_SESSION");    if (! session.isArray()) {      session_start(env);      session = env.getGlobalValue("_SESSION");    }    for (int i = 0; i < values.length; i++)      sessionRegisterImpl(env, (ArrayValue) session, values[i]);    return true;  }  /**   * Registers global variables in the session.   */  private void sessionRegisterImpl(Env env, ArrayValue session, Value nameV)  {    nameV = nameV.toValue();    if (nameV instanceof StringValue) {      String name = nameV.toString();      Value var = env.getGlobalVar(name);      Value value = session.get(nameV);      if (value.isset())	var.set(value);      session.put(nameV, var);    } else if (nameV.isArray()) {      ArrayValue array = (ArrayValue) nameV.toValue();      for (Value subValue : array.values()) {        sessionRegisterImpl(env, session, subValue);      }    }  }  /**   * Returns the session's save path   */  public Value session_save_path(Env env, @Optional String newValue)  {    Value value = env.getIni("session.save_path");    if (newValue != null && ! newValue.equals(""))      env.setIni("session.save_path", newValue);    if (value.isNull() || value.length() == 0) {      // XXX: should we create work directory if does not exist?      value = env.createString(env.getWorkDir().getPath());    }        return value;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?