Skip to content

Commit

Permalink
minor improvements to install procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
bitslip6 committed Apr 29, 2022
1 parent 7c8baaa commit 79bced0
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/bitfire_pure.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function process_server2(array $server) : Request {


function process_ip(array $server) : string {
$header_name = strtoupper(Config::str_up('ip_header', 'REMOTE_ADDR'));
$header_name = Config::str_up('ip_header', 'REMOTE_ADDR');
$ip = "n/a";
switch ($header_name) {
case "FORWARDED":
Expand Down
14 changes: 11 additions & 3 deletions src/dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,16 @@ function package_to_ver($path) : string {

function dump_dirs() : array {
$root = \BitFireSvr\find_wordpress_root($_SERVER['DOCUMENT_ROOT']);
$ver = \BitFireSvr\get_wordpress_version($root);
$rootver = \BitFireSvr\get_wordpress_version($root);
if ($root == NULL) { return NULL; }

$d1 = "$root/wp-content/plugins";
$d2 = "$root/wp-content/themes";
$all_paths = array_merge(get_subdirs($d1), get_subdirs($d2), get_subdirs("$root/wp-includes"), get_subdirs("$root/wp-admin"));
$all_paths = array_merge(get_subdirs($d1), get_subdirs($d2), ["$root/wp-includes", "$root/wp-admin"]);
$all_subs = array();
foreach ($all_paths as $full) {
$path = str_replace($root, "", $full);
$ver = $rootver;
if (file_exists("{$full}/package.json")) {
$ver = package_to_ver("{$full}/package.json");
}
Expand Down Expand Up @@ -377,7 +378,14 @@ function serve_settings(string $dashboard_path) {
// authentication guard
validate_auth($_SERVER['PHP_AUTH_PW']??'')->run();

render_view("settings.html", "BitFire Settings", array("dashboard_path" => $dashboard_path));
render_view("settings.html", "BitFire Settings", array(
"dashboard_path" => $dashboard_path,
"has" => array(
"shmop" => function_exists("shmop_open"),
"apcu" => function_exists("apcu_store"),
"shm" => function_exists("shm_put_var")
)
));
}


Expand Down
2 changes: 1 addition & 1 deletion src/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* call replace function $fn if config $param = $value. replace with $new_value
*/
function replace_if_config(string $param, string $value, callable $fn, string $new_value) : bool {
if (CFG::str($param) == $value) { return $fn("$param = '$value'", "$param = '$new_value'"); }
if (CFG::str($param) == $value) { return $fn("/$param\s*=\s*['\"]?$value['\"]?/", "$param = \"$new_value\""); }
return false;
}

Expand Down
13 changes: 10 additions & 3 deletions src/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ function remove_lines(FileData $file, int $num_lines) : FileData {
function persist_data(string $key, string $value) : \TF\Effect {
$effect = Effect::new();
if (CFG::enabled(CONFIG_COOKIES)) {
$maybe_cookie = \TF\decrypt_tracking_cookie($_COOKIE[CFG::str(CONFIG_USER_TRACK_COOKIE)] ?? '', CFG::str(CONFIG_ENCRYPT_KEY), $_SERVER[CFG::str("ip_header")], $_SERVER['HTTP_USER_AGENT']);
$maybe_cookie = \TF\decrypt_tracking_cookie($_COOKIE[CFG::str(CONFIG_USER_TRACK_COOKIE)] ?? '', CFG::str(CONFIG_ENCRYPT_KEY), $_SERVER[CFG::str_up("ip_header")], $_SERVER['HTTP_USER_AGENT']);
if (!$maybe_cookie->empty()) {
$maybe_cookie->set_if_empty();
}
Expand Down Expand Up @@ -944,15 +944,22 @@ function prof_sort(array $a, array $b) : int {


/**
* replace file contents inline
* replace file contents inline, $find can be a regex or string
*/
function file_replace(string $filename, string $find, string $replace) : bool {
if (!file_exists($filename)) {
if (!touch($filename)) { return false; }
}
$in = file_get_contents($filename);
\TF\debug("file replace [%s] [%s] in len [%d] ",$filename, $replace, strlen($in));
$out = str_replace($find, $replace, $in);
// regex
if ($find[0] == "/") {
$out = preg_replace($find, $replace, $in);
}
// standard replace
else {
$out = str_replace($find, $replace, $in);
}
\TF\debug("file replace out len: " . strlen($out));
return file_write($filename, $out);
}
Expand Down
19 changes: 19 additions & 0 deletions src/wordpress/admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace BitFireWordpres;

use FunctionalWP\Effect;

function index() : \FunctionalWP\Effect {
$effect = Effect::new()->admin_nav(new \FunctionalWP\MenuItem("BitFire Dashboard", "BitFire", "\BitFireWordpress\show_dashboard", "https://bitfire.co/icon.png"));
return $effect;
}

function show_dashboard() : void {
\FunctionalWP\EffectRunner(make_dashboard_effect());
}

function make_dashboard_effect() : \FunctionalWP\Effect {
return Effect::new()->out("BitFire Wordpress Page Out");
}

\FunctionalWP\EffectRunner(index());
78 changes: 78 additions & 0 deletions src/wordpress/func.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
namespace FunctionalWP;

class MenuItem {
public $_title;
public $_label;
public $_fn;
public $_icon_url;
public $_id;
public function __construct(string $title, string $label, callable $fn, string $icon_url, string $id = "") {
static $id_num = 0;
$this->_id = "functional_wp_id_$id_num";
$this->_title = $title;
$this->_fn = $fn;
$this->_icon_url = $icon_url;
$id_num++;
}
}


class Effect {
private $out = '';
private $response = 0;
private $exit = false;
private $headers = array();
private $cookie = '';
private $cache = array();
private $file_outs = array();
private $nav = array();
private $admin_nav = array();
private $status = 0;

public static function new() : Effect { return new Effect(); }

public function nav(MenuItem $item) { $this->nav[] = $item; return $this; }
public function admin_nav(MenuItem $item) { $this->admin_nav[] = $item; return $this; }
// response content effect
public function out(string $line) : Effect { $this->out .= $line; return $this; }
// response header effect
public function header(string $name, string $value) : Effect { $this->headers[$name] = $value; return $this; }
// response cookie effect
public function cookie(string $value) : Effect { $this->cookie = $value; return $this; }
// response code effect
public function response_code(int $code) : Effect { $this->response = $code; return $this; }
// update cache entry effect
public function update(\TF\CacheItem $item) : Effect { $this->cache[$item->key] = $item; return $this; }
// exit the script effect (when run is called)
public function exit(bool $should_exit = true) : Effect { $this->exit = $should_exit; return $this; }
// an effect status code that can be read later
public function status(int $status) : Effect { $this->status = $status; return $this; }
// an effect to write a file to the filesystem
public function file(\TF\FileMod $mod) : Effect { $this->file_outs[] = $mod; return $this; }

// return true if the effect will exit
public function read_exit() : bool { return $this->exit; }
// return the effect content
public function read_out() : string { return $this->out; }
// return the effect headers
public function read_headers() : array { return $this->headers; }
// return the effect cookie (only 1 cookie supported)
public function read_cookie() : string { return $this->cookie; }
// return the effect cache update
public function read_cache() : array { return $this->cache; }
// return the effect response code
public function read_code() : int { return $this->response; }
// return the effect function status code
public function read_status() : int { return $this->status; }
// return the effect filesystem changes
public function read_files() : array { return $this->file_outs; }
}


function EffectRunner(Effect $effect) {
foreach ($effect->admin_nav as $item) {
\add_menu_page($item->title, $item->label, "Administrator", $item->_id, $item->_fn, $item->_icon_url);
}
}

11 changes: 9 additions & 2 deletions startup.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,20 @@ function onerr($errno, $errstr, $errfile, $errline, $context = NULL) : bool {

try {
\TF\parse_ini(WAF_DIR."config.ini");
\TF\debug("begin " . BITFIRE_SYM_VER);
\TF\debug("bitfire ver " . BITFIRE_SYM_VER);

// handle IP level blocks, requires single stat call for test
if (\BitFire\Config::enabled("allow_ip_block", false)) {
$blockfile = BLOCK_DIR . DS . at($_SERVER, Config::str_up('ip_header', 'REMOTE_ADDR'), '127.0.0.1');
if (file_exists($blockfile) && filemtime($blockfile) > time()) {
$m1 = microtime(true);
$block = array("blocked IP address");
\TF\debug("ip block: [" . round((($m1-$GLOBALS['start_time'])*1000),3) . "ms] time: " . \TF\utc_date("m/d @H.i.s") . " GMT");
exit(include WAF_DIR."views/block.php");
}
}


// todo: clean up
if (strlen(Config::str('pro_key')>20) && file_exists(WAF_DIR."src/pro.php") ) { @include_once WAF_DIR . "src/pro.php"; @include_once WAF_DIR . "src/proapi.php"; }
$bitfire = \Bitfire\BitFire::get_instance();
Expand All @@ -77,7 +80,7 @@ function onerr($errno, $errstr, $errfile, $errline, $context = NULL) : bool {
->doifnot(array($bitfire, 'cache_behind'));

register_shutdown_function('\BitFire\post_request', $bitfire->_request, null, null);
\TF\debug("end");
\TF\debug("bitfire end");
}
catch (\Exception $e) {
\BitFire\onerr($e->getCode(), $e->getMessage(), $e->getFile(), $e->getLine());
Expand All @@ -90,3 +93,7 @@ function onerr($errno, $errstr, $errfile, $errline, $context = NULL) : bool {
//file_put_contents("/tmp/prof.pass.json", json_encode($data, JSON_PRETTY_PRINT));

restore_error_handler();

// add support for startup chaining
$autoload = Config::str("auto_prepend_file");
if ($autoload !== "") { @include $autoload; }
18 changes: 10 additions & 8 deletions views/hashes.html
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ <h5 class="modal-title" id="pass_title">Set BitFire Password</h5>
var diffed = {};
var self_url = '<?php echo $self;?>';
var file_list = <?php echo json_encode($file_list['files']); ?>;
var num_files = file_list.length;
var num_files = 0;

var content = GBI("hash_template");
var hash_renderer = _.template(content.innerText);
Expand Down Expand Up @@ -682,13 +682,16 @@ <h5 class="modal-title" id="pass_title">Set BitFire Password</h5>
// html += "// Local File ADDS Lines: " + ops[i][3] + " - " + last_line + "<br />\n";
html2 = "";
for(j=ops[i][3]; j<ops[i][4]; j++) {
let evil_line = evil(l2[j]);
if (num_replace++ < 100) {
line_nums += j + "\n";
let markup = Prism.highlight(l2[j], Prism.languages.php, 'php') + "<br />\n";
html2 += (evil_line) ? "<div class='evil'>" + markup + "</div>": markup;
if (l2[j].includes("/bitfire/startup.php")) { has_diff = false; } // ignore our bitfire include
else {
let evil_line = evil(l2[j]);
if (num_replace++ < 100) {
line_nums += j + "\n";
let markup = Prism.highlight(l2[j], Prism.languages.php, 'php') + "<br />\n";
html2 += (evil_line) ? "<div class='evil'>" + markup + "</div>": markup;
}
is_evil |= evil_line;
}
is_evil |= evil_line;
}
if (is_evil)
if (num_replace >= 100) { html += " /* ... " + (100 - num_replace) + " lines truncated... */\n"; line_nums += "\n"; }
Expand Down Expand Up @@ -724,7 +727,6 @@ <h5 class="modal-title" id="pass_title">Set BitFire Password</h5>
// console.log(has_diff, html);
if (has_diff) {
num_files++;
//console.log("HAS DIFF!", num_files);
if (is_evil) {
document.getElementById("icon"+id).src="https://bitfire.co/assets/malware.png";
if (!do_toggle) {
Expand Down
39 changes: 37 additions & 2 deletions views/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,13 @@ <h4 class="card-header-title">
<span class="visually-hidden">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu">
<?php if ($has['shmop']) { ?>
<li><span class="dropdown-item pointer" onclick="soption('cache_type', 'shmop')" href="#">SHMOP</span></li>
<?php } if ($has['apcu']) { ?>
<li><span class="dropdown-item pointer" onclick="soption('cache_type', 'apcu')" href="#">APCU</span></li>
<?php } if ($has['shm']) { ?>
<li><span class="dropdown-item pointer" onclick="soption('cache_type', 'shm')" href="#">SHM</span></li>
<?php } ?>
<li><span class="dropdown-item pointer" onclick="soption('cache_type', 'nop')" href="#">none</span></li>
</ul>
</div>
Expand Down Expand Up @@ -895,7 +899,7 @@ <h4 class="card-header-title">

<h4 class="card-header-title">
Optional Settings
<small class="text-muted">
<small class="text-muted ml-4">
<a href="https://bitfire.co/system_settings">Learn about BitFire System Settings</a>
</small>
</h4>
Expand Down Expand Up @@ -961,9 +965,40 @@ <h4 class="card-header-title">
</div>
</div>

</div>
</div>
</div>
</div>

<hr class="my-5">

<div class="card">
<div class="card-header">

<h4 class="card-header-title">
PRO / PREMIUM licensing
<small class="text-muted ml-4">
<a href="https://bitfire.co/pricing">Register for PRO / PREMIUM license</a>
</small>
</h4>

</div>
<div class="card-body">
<div class="row">

<div class="col-12 col-md-6">

<div class="form-group">
<label class="form-label">
BitFire PRO License
</label>
<small class="form-text text-muted">Receive your license via email after purchase</small>
<?php echo \BitFire\text_input('pro_key'); ?>
</div>

</div>
</div>
</div>
</div>



Expand Down

0 comments on commit 79bced0

Please sign in to comment.