<commit_before><?php

/*
  Copyright (c) 2005 <NAME>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

include_once("lib/logger.php");
include_once("lib/profiler.php");
include_once("include/common_funcs.php");
include_once("lib/Conteg_class.php");

class App {

  function App($rootdir, $args) {
    Profiler::StartTimer("WebApp", 1);
    Profiler::StartTimer("WebApp::Init", 1);
    Profiler::StartTimer("WebApp::TimeToDisplay", 1);

    register_shutdown_function(array('Logger','processShutdown'));

    ob_start();
    $this->rootdir = $rootdir;
    $this->debug = !empty($args["debug"]);
    $this->getAppVersion();
    Logger::Info("WebApp Initializing (" . $this->appversion . ")");
    Logger::Info("Path: " . get_include_path());
    $this->initAutoLoaders();

    Logger::Info("Turning Pandora flag on");
    $pandora = PandoraLog::singleton();
    $pandora->setFlag(true);


    $this->locations = array("scripts" => "htdocs/scripts",
        "css" => "htdocs/css",
        "tmp" => "tmp",
        "config" => "config");
    $this->request = $this->ParseRequest(NULL, $args);
    $this->locations["basedir"] = $this->request["basedir"];
    $this->locations["scriptswww"] = $this->request["basedir"] . "/scripts";
    $this->locations["csswww"] = $this->request["basedir"] . "/css";
    $this->locations["imageswww"] = $this->request["basedir"] . "/images";

    $this->InitProfiler();

    $this->cfg = ConfigManager::singleton($rootdir);
    $this->locations = array_merge($this->locations, $this->cfg->locations);
    $this->data = DataManager::singleton($this->cfg);

    set_error_handler(array($this, "HandleError"), E_ALL);

    DependencyManager::init($this->locations);

    if ($this->initialized()) {
      try {
        $this->session = SessionManager::singleton();
        // Set sticky debug flag
        if (isset($this->request["args"]["debug"])) {
          $this->debug = $_SESSION["debug"] = ($this->request["args"]["debug"] == 1);
        } else if (!empty($_SESSION["debug"])) {
          $this->debug = $_SESSION["debug"];
        }
        $this->cobrand = $this->GetRequestedConfigName($this->request);
        $this->cfg->GetConfig($this->cobrand, true, $this->cfg->servers["role"]);
        $this->ApplyConfigOverrides();
        $this->locations = DependencyManager::$locations = $this->cfg->locations;

        // And the google analytics flag
        if (isset($this->request["args"]["GAalerts"])) {
          $this->GAalerts = $this->session->temporary["GAalerts"] = ($this->request["args"]["GAalerts"] == 1) ? 1 : 0;
        } else if (!empty($this->session->temporary["GAalerts"])) {
          $this->GAalerts = $this->session->temporary["GAalerts"];
        } else {
          $this->GAalerts = 0;
        }

        $this->apiversion = any($this->request["args"]["apiversion"], ConfigManager::get("api.version.default"), 0);
        $this->tplmgr = TemplateManager::singleton($this->rootdir);
        $this->tplmgr->assign_by_ref("webapp", $this);
        $this->components = ComponentManager::singleton($this);
        $this->orm = OrmManager::singleton();
        //$this->tplmgr->SetComponents($this->components);
      } catch (Exception $e) {
        print $this->HandleException($e);
      }
    } else {
      $fname = "./templates/uninitialized.tpl";
      if (($path = file_exists_in_path($fname, true)) !== false) {
        print file_get_contents($path . "/" . $fname);
      }
    }
    $this->user = User::singleton();
    $this->user->InitActiveUser($this->request);

    // Merge permanent user settings from the URL
    if (!empty($this->request["args"]["settings"])) {
      foreach ($this->request["args"]["settings"] as $k=>$v) {
        $this->user->SetPreference($k, $v, "user");
      }
    }
    // ...and then do the same for session settings
    if (!empty($this->request["args"]["sess"])) {
      foreach ($this->request["args"]["sess"] as $k=>$v) {
        $this->user->SetPreference($k, $v, "temporary");
      }
    }

    // And finally, initialize abtests
    if (class_exists(ABTestManager)) {
      Profiler::StartTimer("WebApp::Init - abtests", 2);
      $this->abtests = ABTestmanager::singleton(array("cobrand" => $this->cobrand, "v" => $this->request["args"]["v"]));
      Profiler::StopTimer("WebApp::Init - abtests");
    }

    Profiler::StopTimer("WebApp::Init");
  }

  function Display($path=NULL, $args=NULL) {
    $path = any($path, $this->request["path"], "/");
    $args = any($args, $this->request["args"], array());

    if (!empty($this->components)) {
      try {
        $output = $this->components->Dispatch($path, $args);
      } catch (Exception $e) {
        //print_pre($e);
        $output["content"] = $this->HandleException($e);
      }

      $this->session->quit();

      $contegargs = any($this->cfg->servers["conteg"], array());
      if (is_array($this->sitecfg["conteg"]))
        $contegargs = array_merge($contegargs, $this->sitecfg["conteg"]);
      if (empty($contegargs["type"]))
        $contegargs["type"] = any($this->request["contenttype"], $output["responsetype"]);
      if (is_array($contegargs["policy"][$contegargs["type"]])) {
        $contegargs = array_merge($contegargs, $contegargs["policy"][$contegargs["type"]]);
      }

      if (empty($contegargs["modified"])) // Set modified time to mtime of base directory if not set
        $contegargs["modified"] = filemtime($this->rootdir);

      //header('Content-type: ' . any($output["type"], "text/html"));
      if ($output["type"] == "ajax" || $output["type"] == "jsonp") {
        print $this->tplmgr->PostProcess($output["content"], true);
      } else {
        print $this->tplmgr->PostProcess($output["content"]);
        if (!empty($this->request["args"]["timing"]))
          print Profiler::Display();
      }
      Profiler::StopTimer("WebApp::TimeToDisplay");
      Profiler::StartTimer("WebApp::Display() - Conteg", 1);
      new Conteg($contegargs);
      Profiler::StopTimer("WebApp::Display() - Conteg");
    }
  }

  function GetAppVersion() {
    $this->appversion = "development";
    $verfile = "config/elation.appversion";
    if (file_exists($verfile)) {
      $appver = trim(file_get_contents($verfile));
      if (!empty($appver))
        $this->appversion = $appver;
    }
    return $this->appversion;
  }

  function initialized() {
    $ret = false;
    if (is_writable($this->locations["tmp"])) {
      if (!file_exists($this->locations["tmp"] . "/initialized.txt")) {
        umask(0002);
        Logger::notice("App instance has not been initialized yet - doing so now");
        if (extension_loaded("apc")) {
          Logger::notice("Flushing APC cache");
          apc_clear_cache();
        }
<commit_msg>- Added percent-based profiling dump support<commit_after><?php

/*
  Copyright (c) 2005 <NAME>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library 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.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

include_once("lib/logger.php");
include_once("lib/profiler.php");
include_once("include/common_funcs.php");
include_once("lib/Conteg_class.php");

class App {

  function App($rootdir, $args) {
    Profiler::StartTimer("WebApp", 1);
    Profiler::StartTimer("WebApp::Init", 1);
    Profiler::StartTimer("WebApp::TimeToDisplay", 1);

    register_shutdown_function(array('Logger','processShutdown'));

    ob_start();
    $this->rootdir = $rootdir;
    $this->debug = !empty($args["debug"]);
    $this->getAppVersion();
    Logger::Info("WebApp Initializing (" . $this->appversion . ")");
    Logger::Info("Path: " . get_include_path());
    $this->initAutoLoaders();

    Logger::Info("Turning Pandora flag on");
    $pandora = PandoraLog::singleton();
    $pandora->setFlag(true);


    $this->locations = array("scripts" => "htdocs/scripts",
        "css" => "htdocs/css",
        "tmp" => "tmp",
        "config" => "config");
    $this->request = $this->ParseRequest(NULL, $args);
    $this->locations["basedir"] = $this->request["basedir"];
    $this->locations["scriptswww"] = $this->request["basedir"] . "/scripts";
    $this->locations["csswww"] = $this->request["basedir"] . "/css";
    $this->locations["imageswww"] = $this->request["basedir"] . "/images";

    $this->InitProfiler();
    $this->cfg = ConfigManager::singleton($rootdir);
    $this->InitProfiler(); // reinitialize after loading the config
    $this->locations = array_merge($this->locations, $this->cfg->locations);
    $this->data = DataManager::singleton($this->cfg);

    set_error_handler(array($this, "HandleError"), E_ALL);

    DependencyManager::init($this->locations);

    if ($this->initialized()) {
      try {
        $this->session = SessionManager::singleton();
        // Set sticky debug flag
        if (isset($this->request["args"]["debug"])) {
          $this->debug = $_SESSION["debug"] = ($this->request["args"]["debug"] == 1);
        } else if (!empty($_SESSION["debug"])) {
          $this->debug = $_SESSION["debug"];
        }
        $this->cobrand = $this->GetRequestedConfigName($this->request);
        $this->cfg->GetConfig($this->cobrand, true, $this->cfg->servers["role"]);
        $this->ApplyConfigOverrides();
        $this->locations = DependencyManager::$locations = $this->cfg->locations;

        // And the google analytics flag
        if (isset($this->request["args"]["GAalerts"])) {
          $this->GAalerts = $this->session->temporary["GAalerts"] = ($this->request["args"]["GAalerts"] == 1) ? 1 : 0;
        } else if (!empty($this->session->temporary["GAalerts"])) {
          $this->GAalerts = $this->session->temporary["GAalerts"];
        } else {
          $this->GAalerts = 0;
        }

        $this->apiversion = any($this->request["args"]["apiversion"], ConfigManager::get("api.version.default"), 0);
        $this->tplmgr = TemplateManager::singleton($this->rootdir);
        $this->tplmgr->assign_by_ref("webapp", $this);
        $this->components = ComponentManager::singleton($this);
        $this->orm = OrmManager::singleton();
        //$this->tplmgr->SetComponents($this->components);
      } catch (Exception $e) {
        print $this->HandleException($e);
      }
    } else {
      $fname = "./templates/uninitialized.tpl";
      if (($path = file_exists_in_path($fname, true)) !== false) {
        print file_get_contents($path . "/" . $fname);
      }
    }
    $this->user = User::singleton();
    $this->user->InitActiveUser($this->request);

    // Merge permanent user settings from the URL
    if (!empty($this->request["args"]["settings"])) {
      foreach ($this->request["args"]["settings"] as $k=>$v) {
        $this->user->SetPreference($k, $v, "user");
      }
    }
    // ...and then do the same for session settings
    if (!empty($this->request["args"]["sess"])) {
      foreach ($this->request["args"]["sess"] as $k=>$v) {
        $this->user->SetPreference($k, $v, "temporary");
      }
    }

    // And finally, initialize abtests
    if (class_exists(ABTestManager)) {
      Profiler::StartTimer("WebApp::Init - abtests", 2);
      $this->abtests = ABTestmanager::singleton(array("cobrand" => $this->cobrand, "v" => $this->request["args"]["v"]));
      Profiler::StopTimer("WebApp::Init - abtests");
    }

    Profiler::StopTimer("WebApp::Init");
  }

  function Display($path=NULL, $args=NULL) {
    $path = any($path, $this->request["path"], "/");
    $args = any($args, $this->request["args"], array());

    if (!empty($this->components)) {
      try {
        $output = $this->components->Dispatch($path, $args);
      } catch (Exception $e) {
        //print_pre($e);
        $output["content"] = $this->HandleException($e);
      }

      $this->session->quit();

      $contegargs = any($this->cfg->servers["conteg"], array());
      if (is_array($this->sitecfg["conteg"]))
        $contegargs = array_merge($contegargs, $this->sitecfg["conteg"]);
      if (empty($contegargs["type"]))
        $contegargs["type"] = any($this->request["contenttype"], $output["responsetype"]);
      if (is_array($contegargs["policy"][$contegargs["type"]])) {
        $contegargs = array_merge($contegargs, $contegargs["policy"][$contegargs["type"]]);
      }

      if (empty($contegargs["modified"])) // Set modified time to mtime of base directory if not set
        $contegargs["modified"] = filemtime($this->rootdir);

      //header('Content-type: ' . any($output["type"], "text/html"));
      if ($output["type"] == "ajax" || $output["type"] == "jsonp") {
        print $this->tplmgr->PostProcess($output["content"], true);
      } else {
        print $this->tplmgr->PostProcess($output["content"]);
        if (!empty($this->request["args"]["timing"]))
          print Profiler::Display();
      }
      Profiler::StopTimer("WebApp::TimeToDisplay");
      Profiler::StartTimer("WebApp::Display() - Conteg", 1);
      new Conteg($contegargs);
      Profiler::StopTimer("WebApp::Display() - Conteg");
      if (Profiler::$log) {
        Profiler::Log($this->cfg->locations["tmp"], $this->components->pagecfg["pagename"]);
      }
    }
  }

  function GetAppVersion() {
    $this->appversion = "development";
    $verfile = "config/elation.appversion";
    if (file_exists($verfile)) {
      $appver = trim(file_get_contents($verfile));
      if (!empty($appver))
        $this->appversion = $appver;
    }
    return $this->appversion;
  }

  function initialized() {
    $ret = false;
    if (is_writable($this->locations["tmp"])) {
      if (!file_exists($this->locations["tmp"] . "/initialized.txt")) {
        umask(0002);
        Logger::notice("App instance has not been initialized yet - doing so now");
        if (extension_loaded("apc")) {
          Logger::notice("Flushing APC cache");
          apc_clear_cache();
        }

{'fraction_non_alphanumeric': 0.19169605429502962, 'fraction_numerical': 0.0042584336948566105, 'mean_word_length': 3.434936559457067, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 238, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '3354799', 'n_tokens_mistral': 4724, 'n_tokens_neox': 4690, 'n_words': 909}