Skip to content

Commit

Permalink
Add firewall management page
Browse files Browse the repository at this point in the history
  • Loading branch information
dequeues committed Jan 30, 2016
1 parent e67096b commit 842b336
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 14 deletions.
59 changes: 49 additions & 10 deletions admin/modules/cloudflare/class/cloudflare.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,34 @@ public function request($request_data)
{
$ch = curl_init();

if (isset($request_data['method']) && isset($request_data['post_data']) && $request_data['method'] == 'POST')
if (isset($request_data['method']))
{
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_data['post_data']));
}
if ($request_data['method'] == 'POST')
{
curl_setopt($ch, CURLOPT_POST, 1);
if (isset($request_data['post_data']))
{
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_data['post_data']));
}
}

if (isset($request_data['method']) && isset($request_data['patch_data']) && $request_data['method'] == 'PATCH')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_data['patch_data']));
if ($request_data['method'] == 'PATCH')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
if (isset($request_data['patch_data']))
{
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_data['patch_data']));
}
}

if ($request_data['method'] == 'DELETE')
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
if (isset($request_data['delete_data']))
{
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_data['delete_data']));
}
}
}

$url = $this->api_url . $request_data['endpoint'];
Expand All @@ -41,7 +59,6 @@ public function request($request_data)
{
$url = $url . "?". http_build_query($request_data['url_parameters']);
}

curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "MyBB CloudFlare Manager Plugin");
curl_setopt($ch, CURLOPT_URL, $url);
Expand Down Expand Up @@ -145,7 +162,7 @@ public function challenge_ip($ip, $notes = '')
return $this->update_access_rule("challenge", $ip, $notes);
}

private function update_access_rule($mode, $ip, $notes = '')
public function update_access_rule($mode, $ip, $notes = '')
{
$data = $this->request (
array (
Expand Down Expand Up @@ -203,6 +220,28 @@ public function ipv46_setting($setting = NULL)
return $data;
}

public function get_access_rules()
{
$data = $this->request(
array (
'endpoint' => "/zones/{$this->zone_id}/firewall/access_rules/rules"
)
);
return $data;
}

public function delete_firewall_rule($rule_id)
{
$data = $this->request(
array (
'endpoint' => "/zones/{$this->zone_id}/firewall/access_rules/rules/{$rule_id}",
'method' => 'DELETE'
)
);

return $data;
}

public function fetch_recent_visitors($type, $time)
{
$data = array(
Expand Down
99 changes: 99 additions & 0 deletions admin/modules/cloudflare/cloudflare_manage_firewall.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php


// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

$page->add_breadcrumb_item("CloudFlare Manager", "index.php?module=cloudflare");
$page->add_breadcrumb_item("Manage Firewall", "index.php?module=cloudflare-manage_firewall");
$page->output_header("CloudFlare Manager - Manage Firewall");

function main_page()
{
global $cloudflare, $mybb;
$request = $cloudflare->get_access_rules();
$table = new Table;
$table->construct_header("Mode");
$table->construct_header("IP Address");
$table->construct_header("Notes");
$table->construct_header("Modify");

foreach($request->result as $rule)
{
$table->construct_cell($rule->mode);
$table->construct_cell($rule->configuration->value);
$table->construct_cell($rule->notes);
$table->construct_cell("<a href=\"index.php?module=cloudflare-manage_firewall&action=modify_rule_by_ip&ip={$rule->configuration->value}&my_post_key={$mybb->post_code}&current_mode={$rule->mode}&current_notes={$rule->notes}\">Modify</a>&nbsp;/&nbsp;<a href=\"index.php?module=cloudflare-manage_firewall&action=delete_rule_by_id&rule_id={$rule->id}&ip_address={$rule->configuration->value}&my_post_key={$mybb->post_code}\">Delete</a>");
$table->construct_row();
}

$table->output("Firewall Rules");

}

if ($mybb->input['action'] == 'modify_rule_by_ip')
{
if (isset($mybb->input['update_rule']))
{
if(!verify_post_check($mybb->input['my_post_key']))
{
flash_message($lang->invalid_post_verify_key2, 'error');
admin_redirect("index.php?module=cloudflare-manage_firewall");
}

$request = $cloudflare->update_access_rule($mybb->get_input('mode'), $mybb->get_input('ip_address'), $mybb->get_input('notes'));

if (!empty($request['success']))
{
flash_message("Updated the firewall rule with IP {$mybb->get_input('ip_address')}", "success");
admin_redirect("index.php?module=cloudflare-manage_firewall");
}
else
{
flash_message($request['errors'], "error");
admin_redirect("index.php?module=cloudflare-manage_firewall");
}
}

$form = new Form('index.php?module=cloudflare-manage_firewall&amp;action=modify_rule_by_ip', 'post');
$form_container = new FormContainer("Modify Firewall Rule");
$form_container->output_row("IP Address", "The IP address you would like to whitelist", $form->generate_text_box('ip_address', $mybb->get_input('ip')));
$form_container->output_row('Mode', '', $form->generate_select_box("mode", array("whitelist" => "Whitelist", "block" => "Blacklist", "challenge" => "Challenge"), $mybb->get_input('current_mode')));
$form_container->output_row("Notes", "Any notes you would like to add", $form->generate_text_box('notes', $mybb->get_input('current_notes')));
echo $form->generate_hidden_field('update_rule', 'update');
$form_container->end();
$buttons[] = $form->generate_submit_button("Submit");
$form->output_submit_wrapper($buttons);
$form->end();
}
elseif ($mybb->input['action'] == 'delete_rule_by_id')
{
if(!verify_post_check($mybb->input['my_post_key']))
{
flash_message($lang->invalid_post_verify_key2, 'error');
admin_redirect("index.php?module=cloudflare-manage_firewall");
}

$request = $cloudflare->delete_firewall_rule($mybb->get_input('rule_id'));

if (!empty($request->success))
{
flash_message("Updated the firewall rule with IP {$mybb->get_input('ip_address')}", "success");
admin_redirect("index.php?module=cloudflare-manage_firewall");
}
else
{
flash_message($request->errors[0]->message, "error");
admin_redirect("index.php?module=cloudflare-manage_firewall");
}

}
else
{
main_page();
}

?>
10 changes: 6 additions & 4 deletions admin/modules/cloudflare/module_meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ function cloudflare_action_handler($action)
'update_snapshot' => array('active' => 'update_snapshot', 'file' => 'cloudflare_update_snapshot.php'),
'ipv46' => array('active' => 'ipv46', 'file' => 'cloudflare_ipv46.php'),
'topthreats' => array('active' => 'topthreats', 'file' => 'cloudflare_topthreats.php'),
'manage_firewall' => array('active' => 'manage_firewall', 'file' => 'cloudflare_manage_firewall.php')
);

$actions = $plugins->run_hooks("admin_cloudflare_action_handler", $actions);

$sub_menu = array();
$sub_menu['Access'] = array(
10 => array("id" => "whitelist", "title" => "Whitelist", "link" => "index.php?module=cloudflare-whitelist"),
20 => array("id" => "blacklist", "title" => "Blacklist", "link" => "index.php?module=cloudflare-blacklist"),
30 => array("id" => "challenge", "title" => "Challenge", "link" => "index.php?module=cloudflare-challenge"),
40 => array("id" => "ipv46", "title" => "IPv6 Support", "link" => "index.php?module=cloudflare-ipv46"),
10 => array("id" => "manage_firewall", "title" => "Manage Firewall", "link" => "index.php?module=cloudflare-manage_firewall"),
20 => array("id" => "whitelist", "title" => "Whitelist", "link" => "index.php?module=cloudflare-whitelist"),
30 => array("id" => "blacklist", "title" => "Blacklist", "link" => "index.php?module=cloudflare-blacklist"),
40 => array("id" => "challenge", "title" => "Challenge", "link" => "index.php?module=cloudflare-challenge"),
50 => array("id" => "ipv46", "title" => "IPv6 Support", "link" => "index.php?module=cloudflare-ipv46"),
);

$sub_menu['Security'] = array (
Expand Down

0 comments on commit 842b336

Please sign in to comment.