<commit_before>package controllers;

import play.*;
import play.i18n.Lang;
import play.i18n.Messages;
import play.mvc.*;
import play.mvc.Http.Request;
import play.mvc.Scope.Session;
import utils.tags.TimeExtensions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import jobs.ProcessGisExport;
import jobs.ProcessGtfsSnapshotExport;
import jobs.ProcessGtfsSnapshotMerge;
import models.*;
import models.gis.GisExport;
import models.gis.GisUploadType;
import models.gtfs.GtfsSnapshot;
import models.gtfs.GtfsSnapshotExport;
import models.gtfs.GtfsSnapshotExportCalendars;
import models.gtfs.GtfsSnapshotMerge;
import models.gtfs.GtfsSnapshotSource;
import models.transit.Route;
import models.transit.RouteType;
import models.transit.ServiceCalendar;
import models.transit.Stop;
import models.transit.StopTime;
import models.transit.StopTimePickupDropOffType;
import models.transit.StopType;
import models.transit.Agency;
import models.transit.Trip;
import models.transit.TripPattern;
import models.transit.TripPatternStop;

@With(Secure.class)
public class Application extends Controller {
    @Before
    static void initSession() throws Throwable {

    	List<Agency> agencies = new ArrayList<Agency>();
    	
    	if(Security.isConnected()) {
            renderArgs.put("user", Security.connected());
            
            Account account = Account.find("username = ?", Security.connected()).first();
            
            if(account == null && Account.count() == 0) {
            	Bootstrap.index();
            }
            
            if(account.admin != null && account.admin)
            	agencies = Agency.find("order by name").fetch();
            else {
            	agencies.add(((Agency)Agency.findById(account.agency.id)));            	
            }
            
            renderArgs.put("agencies", agencies);
        }
    	else if (checkOAuth(request, session)) {
    	    renderArgs.put("user", Messages.get("secure.anonymous"));
    	    
    	    OAuthToken token = getToken(request, session);
    	    
    	    if (token.agency != null) {
    	        agencies.add(token.agency); 
    	    }
    	    else {
    	        agencies = Agency.find("order by name").fetch();
    	    }
    	    
    	    renderArgs.put("agencies", agencies);
    	}
        else {

        	if(Account.count() == 0)
        		Bootstrap.index();
        	else
        	    Secure.login();
        }

        if(session.get("agencyId") == null) {
            
        	Agency agency = agencies.get(0);

            session.put("agencyId", agency.id);
            session.put("agencyName", agency.name);
            session.put("lat", agency.defaultLat);
            session.put("lon", agency.defaultLon);
            session.put("zoom", 12); 
            
        } 
    }

    /**
     * Check if a user has access via OAuth.
     * @param session
     * @return
     */
    public static boolean checkOAuth(Request request, Session session) {
        if (!"true".equals(Play.configuration.getProperty("application.oauthEnabled")))
            return false;
        
        OAuthToken token = getToken(request, session);
        
        if (token == null)
            return false;
        
        // check if the token is valid
        long now = new Date().getTime();
        long timeout = Long.parseLong(Play.configuration.getProperty("application.oauthKeyTimeout"));
        return token.creationDate + timeout > now;           
    }

    /**
     * Get the OAuth token from the request/session.
     * Note that we support OAuth tokens in Authorization headers, or in the session, or in the GET param oauth_token.
     */
    public static OAuthToken getToken (Request request, Session session) {
        String token = null;
        if (request.params.get("oauth_token") != null)
            token = request.params.get("oauth_token");
        else if (request.headers.get("Authorization") != null) {
            String header = request.headers.get("Authorization").toString();
            if (header.startsWith("Bearer "))
                token = header.replace("Bearer ", "");
        }
        // fall back on the session as a last resort, since it may be stale
        else if (session != null && session.get("oauth_token") != null)
            token = session.get("oauth_token");
        
        if (token == null)
            return null;
        
        else
            return OAuthToken.find("token = ?", token).first();
    }
    
    public static void changePassword(String currentPassword, String newPassword) {
        
        if(Security.isConnected())
        {
            if(currentPassword != null && newPassword != null)
            {
                Boolean changed = Account.changePassword(Security.connected(), currentPassword, newPassword);
                
                if(changed)
                    Application.passwordChanged();
                else
                {
                    Boolean badPassword = true;
                    render(badPassword);<commit_msg>Play makes header names lowercase.<commit_after>package controllers;

import play.*;
import play.i18n.Lang;
import play.i18n.Messages;
import play.mvc.*;
import play.mvc.Http.Request;
import play.mvc.Scope.Session;
import utils.tags.TimeExtensions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ArrayUtils;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;
import jobs.ProcessGisExport;
import jobs.ProcessGtfsSnapshotExport;
import jobs.ProcessGtfsSnapshotMerge;
import models.*;
import models.gis.GisExport;
import models.gis.GisUploadType;
import models.gtfs.GtfsSnapshot;
import models.gtfs.GtfsSnapshotExport;
import models.gtfs.GtfsSnapshotExportCalendars;
import models.gtfs.GtfsSnapshotMerge;
import models.gtfs.GtfsSnapshotSource;
import models.transit.Route;
import models.transit.RouteType;
import models.transit.ServiceCalendar;
import models.transit.Stop;
import models.transit.StopTime;
import models.transit.StopTimePickupDropOffType;
import models.transit.StopType;
import models.transit.Agency;
import models.transit.Trip;
import models.transit.TripPattern;
import models.transit.TripPatternStop;

@With(Secure.class)
public class Application extends Controller {
    @Before
    static void initSession() throws Throwable {

    	List<Agency> agencies = new ArrayList<Agency>();
    	
    	if(Security.isConnected()) {
            renderArgs.put("user", Security.connected());
            
            Account account = Account.find("username = ?", Security.connected()).first();
            
            if(account == null && Account.count() == 0) {
            	Bootstrap.index();
            }
            
            if(account.admin != null && account.admin)
            	agencies = Agency.find("order by name").fetch();
            else {
            	agencies.add(((Agency)Agency.findById(account.agency.id)));            	
            }
            
            renderArgs.put("agencies", agencies);
        }
    	else if (checkOAuth(request, session)) {
    	    renderArgs.put("user", Messages.get("secure.anonymous"));
    	    
    	    OAuthToken token = getToken(request, session);
    	    
    	    if (token.agency != null) {
    	        agencies.add(token.agency); 
    	    }
    	    else {
    	        agencies = Agency.find("order by name").fetch();
    	    }
    	    
    	    renderArgs.put("agencies", agencies);
    	}
        else {

        	if(Account.count() == 0)
        		Bootstrap.index();
        	else
        	    Secure.login();
        }

        if(session.get("agencyId") == null) {
            
        	Agency agency = agencies.get(0);

            session.put("agencyId", agency.id);
            session.put("agencyName", agency.name);
            session.put("lat", agency.defaultLat);
            session.put("lon", agency.defaultLon);
            session.put("zoom", 12); 
            
        } 
    }

    /**
     * Check if a user has access via OAuth.
     * @param session
     * @return
     */
    public static boolean checkOAuth(Request request, Session session) {
        if (!"true".equals(Play.configuration.getProperty("application.oauthEnabled")))
            return false;
        
        OAuthToken token = getToken(request, session);
        
        if (token == null)
            return false;
        
        // check if the token is valid
        long now = new Date().getTime();
        long timeout = Long.parseLong(Play.configuration.getProperty("application.oauthKeyTimeout"));
        return token.creationDate + timeout > now;           
    }

    /**
     * Get the OAuth token from the request/session.
     * Note that we support OAuth tokens in Authorization headers, or in the session, or in the GET param oauth_token.
     */
    public static OAuthToken getToken (Request request, Session session) {
        String token = null;
        if (request.params.get("oauth_token") != null)
            token = request.params.get("oauth_token");
        else if (request.headers.get("authorization") != null) {
            String header = request.headers.get("authorization").value();
            if (header.startsWith("Bearer "))
                token = header.replace("Bearer ", "");
        }
        // fall back on the session as a last resort, since it may be stale
        else if (session != null && session.get("oauth_token") != null)
            token = session.get("oauth_token");
        
        if (token == null)
            return null;
        
        else
            return OAuthToken.find("token = ?", token).first();
    }
    
    public static void changePassword(String currentPassword, String newPassword) {
        
        if(Security.isConnected())
        {
            if(currentPassword != null && newPassword != null)
            {
                Boolean changed = Account.changePassword(Security.connected(), currentPassword, newPassword);
                
                if(changed)
                    Application.passwordChanged();
                else
                {
                    Boolean badPassword = true;
                    render(badPassword);
{'fraction_non_alphanumeric': 0.11089566020313943, 'fraction_numerical': 0.0016620498614958448, 'mean_word_length': 2.2389354066985647, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '645973', 'n_tokens_mistral': 3028, 'n_tokens_neox': 2977, 'n_words': 649}