gt:     if (sessionId == null || "".equals(sessionId)) {
context: /*
   * 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;
  
  /**
   * Quercus session handling
   */
  public class SessionModule extends AbstractQuercusModule 
    implements ModuleStartupListener {
    private static final L10N L = new L10N(SessionModule.class);
  
    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(env);
    }
  
    /**
     * 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.createStringOld(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.createStringOld(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);
  
        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.createStringOld(env.getWorkDir().getPath());
      }
      
      return value;
    }
  
    /**
     * Sets the session cookie parameters
     */
    public Value session_set_cookie_params(Env env,
                                           long lifetime,
                                           @Optional Value path,
                                           @Optional Value domain,
                                           @Optional Value secure)
    {
      env.setIni("session.cookie_lifetime", String.valueOf(lifetime));
  
      if (path.isset())
        env.setIni("session.cookie_path", path.toString());
  
      if (domain.isset())
        env.setIni("session.cookie_domain", domain.toString());
  
      if (secure.isset())
        env.setIni("session.cookie_secure", secure.toBoolean() ? "1" : "0");
  
      return NullValue.NULL;
    }
  
    /**
     * Sets the session save handler
     */
    public boolean session_set_save_handler(Env env,
                                            Callback open,
                                            Callback close,
                                            Callback read,
                                            Callback write,
                                            Callback directory,
                                            Callback gc)
  
    {
      SessionCallback cb
        = new SessionCallback(open, close, read, write, directory, gc);
  
      env.setSessionCallback(cb);
  
      return true;
    }
  
    /**
     * Start the session
     */
    public static boolean session_start(Env env)
    {
      if (env.getSession() != null) {
        env.notice(L.l("session has already been started"));
        return true;
      }
  
      SessionCallback callback = env.getSessionCallback();
  
      Value sessionIdValue = (Value) env.getSpecialValue("caucho.session_id");
      String sessionId = null;
  
      HttpServletResponse response = env.getResponse();
  
      env.removeConstant("SID");
  
      String cookieName = env.getIni("session.name").toString();
      boolean generateCookie = true;
      boolean create = false;
  
      if (callback != null) {
        String savePath = env.getIni("session.save_path").toString();
  
        if (savePath == null || "".equals(savePath))
          callback.open(env, env.getWorkDir().getPath(), cookieName);
        else
          callback.open(env, savePath, cookieName);
      }
  
      //
      // Use cookies to transmit session id
      // 
      if (env.getIni("session.use_cookies").toBoolean()) {
        if (sessionIdValue != null)
          sessionId = sessionIdValue.toString();
  
        if (sessionId == null || "".equals(sessionId)) {
          Cookie []cookies = env.getRequest().getCookies();
  
          for (int i = 0; cookies != null && i < cookies.length; i++) {
            if (cookies[i].getName().equals(cookieName)
                && ! "".equals(cookies[i].getValue())) {
              sessionId = cookies[i].getValue();
              generateCookie = false;
            }
          }
        }
  
        if (! generateCookie)
          env.addConstant("SID", env.getEmptyString(), false);
      }
  
      //
      // Use URL rewriting to transmit session id
      //
  
      if (env.getIniBoolean("session.use_trans_sid")
          && ! env.getIniBoolean("session.use_only_cookies")) {
        if (sessionId == null) {
          if (sessionIdValue != null)
            sessionId = sessionIdValue.toString();
  
          if (sessionId == null || "".equals(sessionId))
            sessionId = env.getRequest().getParameter(cookieName);
  
          if (sessionId == null || "".equals(sessionId)) {
            sessionId = env.generateSessionId();
            create = true;
          }
        }
  
        env.addConstant("SID", env.createStringOld(cookieName + '=' + sessionId),
                        false);
        
        OutputModule.pushUrlRewriter(env);
      }
      
  