<commit_before><?php
require_once "util/ExpandableTree.php";

function verifyCallingFromCLI() {
    if (php_sapi_name() != 'cli') {
        throw new Exception('Illegal invocation. Should only be called from CLI.');
    }
}

function hasRedis() {
    $redisUrl = WorkbenchConfig::get()->value("redisUrl");
    return !empty($redisUrl) && class_exists("Redis");
}

function redis() {
    if (!isset($GLOBALS['REDIS'])) {
        if (!hasRedis()) {
            throw new Exception("Redis connection requested but not configured or library not found.");
        }

        $redisUrl = WorkbenchConfig::get()->value("redisUrl");
        $r = new Redis();
        $r->connect(parse_url($redisUrl, PHP_URL_HOST), parse_url($redisUrl, PHP_URL_PORT));
        if (!is_array(parse_url($redisUrl, PHP_URL_PASS))) {
            $r->auth(parse_url($redisUrl, PHP_URL_PASS));
        }

        $GLOBALS['REDIS'] = $r;
    }
    return $GLOBALS['REDIS'];
}

function workbenchLog($logLevel, $type, $message = "") {
    if (!WorkbenchConfig::get()->value("enableLogging")) {
        return;
    }

    $sfdcHost = "";
    $orgId = "";
    $userId = "";
    if (WorkbenchContext::isEstablished()) {
        try {
            $sfdcHost = WorkbenchContext::get()->getHost();
            $info = WorkbenchContext::get()->getUserInfo();
            $orgId = $info->organizationId;
            $userId = $info->userId;
        } catch (Exception $e) {
            // ignore and just use defaults
        }
    }

    $pieces = array("timestamp" => date(DATE_ISO8601),
                    "level"     => logLevelToStr($logLevel),
                    "type"      => $type,
                    "origin"    => isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'],
                    "method"    => $_SERVER['REQUEST_METHOD'],
                    "version"   => $GLOBALS["WORKBENCH_VERSION"],
                    "script"    => $_SERVER['SCRIPT_NAME'],
                    "sfdc"      => $sfdcHost,
                    "org"       => $orgId,
                    "user"      => $userId,
                    "message"   => $message
              );

    call_user_func('_handle_logs_' . WorkbenchConfig::get()->value("logHandler"), $logLevel, json_encode($pieces));
}

function _handle_logs_syslog($logLevel, $msg) {
    openlog("forceworkbench", LOG_ODELAY, WorkbenchConfig::get()->value("syslogFacility"));
    syslog($logLevel, $msg);
    closelog();
}

function _handle_logs_file($logLevel, $msg) {
    $logFile = fopen(WorkbenchConfig::get()->value("logFile"), 'a') or die("can't open log file");
    fwrite($logFile, "forceworkbench=$msg\n");
    fclose($logFile);
}

function _handle_logs_stdout($logLevel, $msg) {
    echo "forceworkbench=$msg\n";
}

function logLevelToStr($logLevel) {
    switch($logLevel) {
        case LOG_EMERG:   return "EMERGENCY";
        case LOG_ALERT:   return "ALERT";
        case LOG_CRIT:    return "CRITICAL";
        case LOG_ERR:     return "ERROR";
        case LOG_WARNING: return "WARNING";
        case LOG_NOTICE:  return "NOTICE";
        case LOG_INFO:    return "INFO";
        case LOG_DEBUG:   return "DEBUG";
        default:          return "";
    }
}

function httpError($code, $reason) {
    header("HTTP/1.1 $code");
    print "<h1>$code</h1>";
    print $reason;
    exit;
}

function getCsrfToken() {
    return md5(WorkbenchConfig::get()->value("csrfSecret") . session_id() . $_SERVER['SCRIPT_NAME']);
}

function validateCsrfToken($doError = true) {
   if (!isset($_REQUEST['CSRF_TOKEN']) || $_REQUEST['CSRF_TOKEN'] != getCsrfToken()) {

       workbenchLog(LOG_ERR, "C", (isset($_REQUEST['CSRF_TOKEN']) ? $_REQUEST['CSRF_TOKEN'] : "NULL") .
                                  "`" .
                                  getCsrfToken());
       
       if ($doError) {
           httpError("403 Forbidden", "Invalid or missing required CSRF token");
       } else {
           return false;
       }
   }
   return true;
}

function getCsrfFormTag() {
    return "\n<input type='hidden' name='CSRF_TOKEN' value='" . getCsrfToken() . "'/>\n";
}

function usingSslFromUserToWorkbench() {
    return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
           (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
}

function usingSslFromWorkbenchToSfdc() {
    return !WorkbenchContext::isEstablished() || WorkbenchContext::get()->isSecure();
}

function usingSslEndToEnd() {
    return usingSslFromUserToWorkbench() && usingSslFromWorkbenchToSfdc();
}

function toBytes ($size_str) {<commit_msg>only log csrf errors when doError=true<commit_after><?php
require_once "util/ExpandableTree.php";

function verifyCallingFromCLI() {
    if (php_sapi_name() != 'cli') {
        throw new Exception('Illegal invocation. Should only be called from CLI.');
    }
}

function hasRedis() {
    $redisUrl = WorkbenchConfig::get()->value("redisUrl");
    return !empty($redisUrl) && class_exists("Redis");
}

function redis() {
    if (!isset($GLOBALS['REDIS'])) {
        if (!hasRedis()) {
            throw new Exception("Redis connection requested but not configured or library not found.");
        }

        $redisUrl = WorkbenchConfig::get()->value("redisUrl");
        $r = new Redis();
        $r->connect(parse_url($redisUrl, PHP_URL_HOST), parse_url($redisUrl, PHP_URL_PORT));
        if (!is_array(parse_url($redisUrl, PHP_URL_PASS))) {
            $r->auth(parse_url($redisUrl, PHP_URL_PASS));
        }

        $GLOBALS['REDIS'] = $r;
    }
    return $GLOBALS['REDIS'];
}

function workbenchLog($logLevel, $type, $message = "") {
    if (!WorkbenchConfig::get()->value("enableLogging")) {
        return;
    }

    $sfdcHost = "";
    $orgId = "";
    $userId = "";
    if (WorkbenchContext::isEstablished()) {
        try {
            $sfdcHost = WorkbenchContext::get()->getHost();
            $info = WorkbenchContext::get()->getUserInfo();
            $orgId = $info->organizationId;
            $userId = $info->userId;
        } catch (Exception $e) {
            // ignore and just use defaults
        }
    }

    $pieces = array("timestamp" => date(DATE_ISO8601),
                    "level"     => logLevelToStr($logLevel),
                    "type"      => $type,
                    "origin"    => isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'],
                    "method"    => $_SERVER['REQUEST_METHOD'],
                    "version"   => $GLOBALS["WORKBENCH_VERSION"],
                    "script"    => $_SERVER['SCRIPT_NAME'],
                    "sfdc"      => $sfdcHost,
                    "org"       => $orgId,
                    "user"      => $userId,
                    "message"   => $message
              );

    call_user_func('_handle_logs_' . WorkbenchConfig::get()->value("logHandler"), $logLevel, json_encode($pieces));
}

function _handle_logs_syslog($logLevel, $msg) {
    openlog("forceworkbench", LOG_ODELAY, WorkbenchConfig::get()->value("syslogFacility"));
    syslog($logLevel, $msg);
    closelog();
}

function _handle_logs_file($logLevel, $msg) {
    $logFile = fopen(WorkbenchConfig::get()->value("logFile"), 'a') or die("can't open log file");
    fwrite($logFile, "forceworkbench=$msg\n");
    fclose($logFile);
}

function _handle_logs_stdout($logLevel, $msg) {
    echo "forceworkbench=$msg\n";
}

function logLevelToStr($logLevel) {
    switch($logLevel) {
        case LOG_EMERG:   return "EMERGENCY";
        case LOG_ALERT:   return "ALERT";
        case LOG_CRIT:    return "CRITICAL";
        case LOG_ERR:     return "ERROR";
        case LOG_WARNING: return "WARNING";
        case LOG_NOTICE:  return "NOTICE";
        case LOG_INFO:    return "INFO";
        case LOG_DEBUG:   return "DEBUG";
        default:          return "";
    }
}

function httpError($code, $reason) {
    header("HTTP/1.1 $code");
    print "<h1>$code</h1>";
    print $reason;
    exit;
}

function getCsrfToken() {
    return md5(WorkbenchConfig::get()->value("csrfSecret") . session_id() . $_SERVER['SCRIPT_NAME']);
}

function validateCsrfToken($doError = true) {
   if (!isset($_REQUEST['CSRF_TOKEN']) || $_REQUEST['CSRF_TOKEN'] != getCsrfToken()) {

       if ($doError) {
           httpError("403 Forbidden", "Invalid or missing required CSRF token");
       } else {
           workbenchLog(LOG_ERR, "C", (isset($_REQUEST['CSRF_TOKEN']) ? $_REQUEST['CSRF_TOKEN'] : "NULL") .
                        "`" .
                        getCsrfToken());

           return false;
       }
   }
   return true;
}

function getCsrfFormTag() {
    return "\n<input type='hidden' name='CSRF_TOKEN' value='" . getCsrfToken() . "'/>\n";
}

function usingSslFromUserToWorkbench() {
    return (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
           (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https');
}

function usingSslFromWorkbenchToSfdc() {
    return !WorkbenchContext::isEstablished() || WorkbenchContext::get()->isSecure();
}

function usingSslEndToEnd() {
    return usingSslFromUserToWorkbench() && usingSslFromWorkbenchToSfdc();
}

function toBytes ($size_str) {
{'fraction_non_alphanumeric': 0.16277813782674444, 'fraction_numerical': 0.002592352559948153, 'mean_word_length': 2.7379895034315704, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7018288', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2908, 'n_words': 523}