text:
<?php

##|+PRIV
##|*IDENT=page-firewall-alias-edit
##|*NAME=Firewall: Alias: Edit
##|*DESCR=Allow access to the 'Firewall: Alias: Edit' page.
##|*MATCH=firewall_aliases_edit.php*
##|-PRIV

require_once("guiconfig.inc");
require_once("functions.inc");
require_once("filter.inc");
require_once("shaper.inc");

$pgtitle = array(gettext("Firewall"), gettext("Aliases"), gettext("Edit"));

if (isset($_POST['referer'])) {
	$referer = $_POST['referer'];
} else {
	$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/firewall_aliases.php');
}

// Keywords not allowed in names
$reserved_keywords = array("all", "pass", "block", "out", "queue", "max", "min", "pptp", "pppoe", "L2TP", "OpenVPN", "IPsec");

// Add all Load balance names to reserved_keywords
if (is_array($config['load_balancer']['lbpool'])) {
	foreach ($config['load_balancer']['lbpool'] as $lbpool) {
		$reserved_keywords[] = $lbpool['name'];
	}
}

$reserved_ifs = get_configured_interface_list(false, true);
$reserved_keywords = array_merge($reserved_keywords, $reserved_ifs, $reserved_table_names);
$max_alias_addresses = 5000;

if (!is_array($config['aliases']['alias'])) {
	$config['aliases']['alias'] = array();
}
$a_aliases = &$config['aliases']['alias'];

$tab = $_REQUEST['tab'];

if ($_POST) {
	$origname = $_POST['origname'];
}

// Debugging
if ($debug) {
	unlink_if_exists("{$g['tmp_path']}/alias_rename_log.txt");
}

$singular_types = array(
	'host'	=> gettext("host"),
	'network' => gettext("network"),
	'port' => gettext("port"),
	'url' => gettext("URL (IP)"),
	'url_ports' => gettext("URL (Port)"),
	'urltable' => gettext("URL Table (IP)"),
	'urltable_ports' => gettext("URL Table (Port)"),
);

function alias_same_type($name, $type) {
	global $config;

	foreach ($config['aliases']['alias'] as $alias) {
		if ($name == $alias['name']) {
			if (in_array($type, array("host", "network")) &&
			    in_array($alias['type'], array("host", "network"))) {
				return true;
			}

			if ($type == $alias['type']) {
				return true;
			} else {
				return false;
			}
		}
	}
	return true;
}

if (is_numericint($_GET['id'])) {
	$id = $_GET['id'];
}
if (isset($_POST['id']) && is_numericint($_POST['id'])) {
	$id = $_POST['id'];
}

if (isset($id) && $a_aliases[$id]) {
	$original_alias_name = $a_aliases[$id]['name'];
	$pconfig['name'] = $a_aliases[$id]['name'];
	$pconfig['detail'] = $a_aliases[$id]['detail'];
	$pconfig['address'] = $a_aliases[$id]['address'];
	$pconfig['type'] = $a_aliases[$id]['type'];
	$pconfig['descr'] = html_entity_decode($a_aliases[$id]['descr']);

	if (preg_match("/urltable/i", $a_aliases[$id]['type'])) {
		$pconfig['address'] = $a_aliases[$id]['url'];
		$pconfig['updatefreq'] = $a_aliases[$id]['updatefreq'];
	}
	if ($a_aliases[$id]['aliasurl'] <> "") {
		if (is_array($a_aliases[$id]['aliasurl'])) {
			$pconfig['address'] = implode(" ", $a_aliases[$id]['aliasurl']);
		} else {
			$pconfig['address'] = $a_aliases[$id]['aliasurl'];
		}
	}
}

if ($_POST) {
	unset($input_errors);
	$vertical_bar_err_text = gettext("Vertical bars (|) at start or end, or double in the middle of descriptions not allowed. Descriptions have been cleaned. Check and save again.");

	/* input validation */

	$reqdfields = explode(" ", "name");
	$reqdfieldsn = array(gettext("Name"));

	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);

	if (!is_validaliasname($_POST['name'])) {
		$input_errors[] = invalidaliasnamemsg($_POST['name']);
	}

	/* check for name conflicts */
	foreach ($a_aliases as $key => $alias) {
		if (($alias['name'] == $_POST['name']) && (empty($a_aliases[$id]) || ($key != $id))) {
			$input_errors[] = gettext("An alias with this name already exists.");
			break;
		}
	}

	/* Check for reserved keyword names */
	foreach ($reserved_keywords as $rk) {
		if ($rk == $_POST['name']) {
			$input_errors[] = sprintf(gettext("Cannot use a reserved keyword as an alias name: %s"), $rk);
		}
	}

	/* check for name interface description conflicts */
	foreach ($config['interfaces'] as $interface) {
		if ($interface['descr'] == $_POST['name']) {
			$input_errors[] = gettext("An interface description with this name already exists.");
			break;
		}
	}

	$alias = array();
	$address = array();
	$final_address_details = array();
	$alias['name'] = $_POST['name'];
	$alias['type'] = $_POST['type'];

	if (preg_match("/urltable/i", $_POST['type'])) {
		$address = "";

		/* item is a url table type */
		if ($_POST['address0']) {
			/* fetch down and add in */
			$_POST['address0'] = trim($_POST['address0']);
			$address[] = $_POST['address0'];
			$alias['url'] = $_POST['address0'];
			$alias['updatefreq'] = $_POST['address_subnet0'] ? $_POST['address_subnet0'] : 7;
			if (!is_URL($alias['url']) || empty($alias['url'])) {
				$input_errors[] = gettext("A valid URL must be provided.");
			} elseif (!process_alias_urltable($alias['name'], $alias['type'], $alias['url'], 0, true, true)) {
				$input_errors[] = gettext("Unable to fetch usable data from URL") . " " . htmlspecialchars($alias['url']);
			}
			if ($_POST["detail0"] <> "") {
				if ((strpos($_POST["detail0"], "||") === false) && (substr($_POST["detail0"], 0, 1) != "|") && (substr($_POST["detail0"], -1, 1) != "|")) {
					$final_address_details[] = $_POST["detail0"];
				} else {
					/* Remove leading and trailing vertical bars and replace multiple vertical bars with single, */
					/* and put in the output array so the text is at least redisplayed for the user. */
					$final_address_details[] = preg_replace('/\|\|+/', '|', trim($_POST["detail0"], "|"));
					$input_errors[] = $vertical_bar_err_text;
				}
			} else {
				$final_address_details[] = sprintf(gettext("Entry added %s"), date('r'));
			}
		}
	} else if ($_POST['type'] == "url" || $_POST['type'] == "url_ports") {
		$desc_fmt_err_found = false;

		/* item is a url type */
		for ($x = 0; $x < $max_alias_addresses - 1; $x++) {
			$_POST['address' . $x] = trim($_POST['address' . $x]);
			if ($_POST['address' . $x]) {
				/* fetch down and add in */
				$temp_filename = tempnam("{$g['tmp_path']}/", "alias_import");
				unlink_if_exists($temp_filename);
				$verify_ssl = isset($config['system']['checkaliasesurlcert']);
				mkdir($temp_filename);
				if (!download_file($_POST['address' . $x], $temp_filename . "/aliases", $verify_ssl)) {
					continue;
				}

				/* if the item is tar gzipped then extract */
				if (stristr($_POST['address' . $x], ".tgz")) {
					process_alias_tgz($temp_filename);
				}

				if (!isset($alias['aliasurl'])) {
					$alias['aliasurl'] = array();
				}

				$alias['aliasurl'][] = $_POST['address' . $x];
				if ($_POST["detail{$x}"] <> "") {
					if ((strpos($_POST["detail{$x}"], "||") === false) && (substr($_POST["detail{$x}"], 0, 1) != "|") && (substr($_POST["detail{$x}"], -1, 1) != "|")) {
						$final_address_details[] = $_POST["detail{$x}"];
					} else {
						/* Remove leading and trailing vertical bars and replace multiple vertical bars with single, */
						/* and put in the output array so the text is at least redisplayed for the user. */
						$final_address_details[] = preg_replace('/\|\|+/', '|', trim($_POST["detail{$x}"], "|"));
						if (!$desc_fmt_err_found) {
							$input_errors[] = $vertical_bar_err_text;
							$desc_fmt_err_found = true;
						}
					}
				} else {
					$final_address_details[] = sprintf(gettext("Entry added %s"), date('r'));
				}

				if (file_exists("{$temp_filename}/aliases")) {
					$address = parse_aliases_file("{$temp_filename}/aliases", $_POST['type'], 5000);
					if ($address == null) {
						/* nothing was found */
						$input_errors[] = sprintf(gettext("A valid URL must be provided. Could not fetch usable data from '%s'."), $_POST['address' . $x]);
					}
					mwexec("/bin/rm -rf " . escapeshellarg($temp_filename));
				} else {
					$input_errors[] = sprintf(gettext("URL '%s' is not valid."), $_POST['address' . $x]);
				}
			}
		}
		unset($desc_fmt_err_found);
		if ($_POST['type'] == "url_ports") {
			$address = group_ports($address);
		}
	} else {
		/* item is a normal alias type */
		$wrongaliases = "";
		$desc_fmt_err_found = false;
		$alias_address_count = 0;
		$input_addresses = array();

		// First trim and expand the input data.
		// Users can paste strings like "10.1.2.0/24 10.3.0.0/16 9.10.11.0/24" into an address box.
		// They can also put an IP range.
		// This loop expands out that stuff so it can easily be validated.
		for ($x = 0; $x < ($max_alias_addresses - 1); $x++) {
			if ($_POST["address{$x}"] <> "") {
				if ($_POST["detail{$x}"] <> "") {
					if ((strpos($_POST["detail{$x}"], "||") === false) && (substr($_POST["detail{$x}"], 0, 1) != "|") && (substr($_POST["detail{$x}"], -1, 1) != "|")) {
						$detail_text = $_POST["detail{$x}"];
					} else {
						/* Remove leading and trailing vertical bars and replace multiple vertical bars with single, */
						/* and put in the output array so the text is at least redisplayed for the user. */
						$detail_text = preg_replace('/\|\|+/', '|', trim($_POST["detail{$x}"], "|"));
						if (!$desc_fmt_err_found) {
							$input_errors[] = $vertical_bar_err_text;
							$desc_fmt_err_found = true;
						}
					}
				} else {
					$detail_text = sprintf(gettext("Entry added %s"), date('r'));
				}
				$address_items = explode(" ", trim($_POST["address{$x}"]));
				foreach ($address_items as $address_item) {
					$iprange_type = is_iprange($address_item);
					if ($iprange_type == 4) {
						list($startip, $endip) = explode('-', $address_item);
						if ($_POST['type'] == "network") {
							// For network type aliases, expand an IPv4 range into an array of subnets.
							$rangesubnets = ip_range_to_subnet_array($startip, $endip);
							foreach ($rangesubnets as $rangesubnet) {
								if ($alias_address_count > $max_alias_addresses) {
									break;
								}
								list($address_part, $subnet_part) = explode("/", $rangesubnet);
								$input_addresses[] = $address_part;
								$input_address_subnet[] = $subnet_part;
								$final_address_details[] = $detail_text;
								$alias_address_count++;
							}
						} else {
							// For host type aliases, expand an IPv4 range into a list of individual IPv4 addresses.
							$rangeaddresses = ip_range_to_address_array($startip, $endip, $max_alias_addresses - $alias_address_count);
							if (is_array($rangeaddresses)) {
								foreach ($rangeaddresses as $rangeaddress) {
									$input_addresses[] = $rangeaddress;
									$input_address_subnet[] = "";
									$final_address_details[] = $detail_text;
									$alias_address_count++;
								}
							} else {
								$input_errors[] = sprintf(gettext('Range is too large to expand into individual host IP addresses (%s)'), $address_item);
								$input_errors[] = sprintf(gettext('The maximum number of entries in an alias is %s'), $max_alias_addresses);
								// Put the user-entered data in the output anyway, so it will be re-displayed for correction.
								$input_addresses[] = $address_item;
								$input_address_subnet[] = "";
								$final_address_details[] = $detail_text;
							}
						}
					} else if ($iprange_type == 6) {
						$input_errors[] = sprintf(gettext('IPv6 address ranges are not supported (%s)'), $address_item);
						// Put the user-entered data in the output anyway, so it will be re-displayed for correction.
						$input_addresses[] = $address_item;
						$input_address_subnet[] = "";
						$final_address_details[] = $detail_text;
					} else {
						$subnet_type = is_subnet($address_item);
						if (($_POST['type'] == "host") && $subnet_type) {
							if ($subnet_type == 4) {
								// For host type aliases, if the user enters an IPv4 subnet, expand it into a list of individual IPv4 addresses.
								$subnet_size = subnet_size($address_item);
								if ($subnet_size > 0 &&
								    $subnet_size <= ($max_alias_addresses - $alias_address_count)) {
									$rangeaddresses = subnetv4_expand($address_item);
									foreach ($rangeaddresses as $rangeaddress) {
										$input_addresses[] = $rangeaddress;
										$input_address_subnet[] = "";
										$final_address_details[] = $detail_text;
										$alias_address_count++;
									}
								} else {
									$input_errors[] = sprintf(gettext('Subnet is too large to expand into individual host IP addresses (%s)'), $address_item);
									$input_errors[] = sprintf(gettext('The maximum number of entries in an alias is %s'), $max_alias_addresses);
									// Put the user-entered data in the output anyway, so it will be re-displayed for correction.
									$input_addresses[] = $address_item;
									$input_address_subnet[] = "";
									$final_address_details[] = $detail_text;
								}
							} else {
								$input_errors[] = sprintf(gettext('IPv6 subnets are not supported in host aliases (%s)'), $address_item);
								// Put the user-entered data in the output anyway, so it will be re-displayed for correction.
								$input_addresses[] = $address_item;
								$input_address_subnet[] = "";
								$final_address_details[] = $detail_text;
							}
						} else {
							list($address_part, $subnet_part) = explode("/", $address_item);
							if (!empty($subnet_part)) {
								if (is_subnet($address_item)) {
									$input_addresses[] = $address_part;
									$input_address_subnet[] = $subnet_part;
								} else {
									// The user typed something like "1.2.3.444/24" or "1.2.3.0/36" or similar rubbish.
									// Feed it through without splitting it apart, then it will be caught by the validation loop below.
									$input_addresses[] = $address_item;
									$input_address_subnet[] = "";
								}
							} else {
								$input_addresses[] = $address_part;
								$input_address_subnet[] = $_POST["address_subnet{$x}"];
							}
							$final_address_details[] = $detail_text;
							$alias_address_count++;
						}
					}
					if ($alias_address_count > $max_alias_addresses) {
						$input_errors[] = sprintf(gettext('The maximum number of entries in an alias has been exceeded (%s)'), $max_alias_addresses);
						break;
					}
				}
			}
		}

		// Validate the input data expanded above.
		foreach ($input_addresses as $idx => $input_address) {
			if (is_alias($input_address)) {
				if (!alias_same_type($input_address, $_POST['type'])) {
					// But alias type network can include alias type urltable. Feature#1603.
					if (!($_POST['type'] == 'network' &&
					    preg_match("/urltable/i", alias_get_type($input_address)))) {
						$wrongaliases .= " " . $input_address;
					}
				}
			} else if ($_POST['type'] == "port") {
				if (!is_port($input_address) && !is_portrange($input_address)) {
					$input_errors[] = sprintf(gettext("%s is not a valid port or alias."), $input_address);
				}
			} else if ($_POST['type'] == "host" || $_POST['type'] == "network") {
				if (is_subnet($input_address) ||
				    (!is_ipaddr($input_address) && !is_hostname($input_address))) {
					$input_errors[] = sprintf(gettext('%1$s is not a valid %2$s address, FQDN or alias.'), $input_address, $singular_types[$_POST['type']]);
				}
			}
			$tmpaddress = $input_address;
			if ($_POST['type'] != "host" && is_ipaddr($input_address) && $input_address_subnet[$idx] <> "") {
				if (!is_subnet($input_address . "/" . $input_address_subnet[$idx])) {
					$input_errors[] = sprintf(gettext('%1$s/%2$s is not a valid subnet.'), $input_address, $input_address_subnet[$idx]);
				} else {
					$tmpaddress .= "/" . $input_address_subnet[$idx];
				}
			}
			$address[] = $tmpaddress;
		}
		unset($desc_fmt_err_found);
		if ($wrongaliases <> "") {
			$input_errors[] = sprintf(gettext('The alias(es): %s cannot be nested because they are not of the same type.'), $wrongaliases);
		}
	}

	unset($vertical_bar_err_text);

	// Allow extending of the firewall edit page and include custom input validation
	pfSense_handle_custom_code("/usr/local/pkg/firewall_aliases_edit/input_validation");

	if (!$input_errors) {
		$alias['address'] = is_array($address) ? implode(" ", $address) : $address;
		$alias['descr'] = $_POST['descr'];
		$alias['type'] = $_POST['type'];
		$alias['detail'] = implode("||", $final_address_details);

		/*	 Check to see if alias name needs to be
		 *	 renamed on referenced rules and such
		 */
		if ($_POST['name'] <> $_POST['origname']) {
			// Firewall rules
			update_alias_names_upon_change(array('filter', 'rule'), array('source', 'address'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('filter', 'rule'), array('destination', 'address'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('filter', 'rule'), array('source', 'port'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('filter', 'rule'), array('destination', 'port'), $_POST['name'], $origname);
			// NAT Rules
			update_alias_names_upon_change(array('nat', 'rule'), array('source', 'address'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'rule'), array('source', 'port'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'rule'), array('destination', 'address'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'rule'), array('destination', 'port'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'rule'), array('target'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'rule'), array('local-port'), $_POST['name'], $origname);
			// NAT 1:1 Rules
			//update_alias_names_upon_change(array('nat', 'onetoone'), array('external'), $_POST['name'], $origname);
			//update_alias_names_upon_change(array('nat', 'onetoone'), array('source', 'address'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'onetoone'), array('destination', 'address'), $_POST['name'], $origname);
			// NAT Outbound Rules
			update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('source', 'network'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('sourceport'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('destination', 'address'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('dstport'), $_POST['name'], $origname);
			update_alias_names_upon_change(array('nat', 'outbound', 'rule'), array('target'), $_POST['name'], $origname);
			// Alias in an alias
			update_alias_names_upon_change(array('aliases', 'alias'), array('address'), $_POST['name'], $origname);
		}

		pfSense_handle_custom_code("/usr/local/pkg/firewall_aliases_edit/pre_write_config");

		if (isset($id) && $a_aliases[$id]) {
			if ($a_aliases[$id]['name'] <> $alias['name']) {
				foreach ($a_aliases as $aliasid => $aliasd) {
					if ($aliasd['address'] <> "") {
						$tmpdirty = false;
						$tmpaddr = explode(" ", $aliasd['address']);
						foreach ($tmpaddr as $tmpidx => $tmpalias) {
							if ($tmpalias == $a_aliases[$id]['name']) {
								$tmpaddr[$tmpidx] = $alias['name'];
								$tmpdirty = true;
							}
						}
						if ($tmpdirty == true) {
							$a_aliases[$aliasid]['address'] = implode(" ", $tmpaddr);
						}
					}
				}
			}
			$a_aliases[$id] = $alias;
		} else {
			$a_aliases[] = $alias;
		}

		// Sort list
		$a_aliases = msort($a_aliases, "name");

		if (write_config()) {
			mark_subsystem_dirty('aliases');
		}

		if (!empty($tab)) {
			header("Location: firewall_aliases.php?tab=" . htmlspecialchars ($tab));
		} else {
			header("Location: firewall_aliases.php");
		}
		exit;

	} else {
		//we received input errors, copy data to prevent retype
		$pconfig['name'] = $_POST['name'];
		$pconfig['descr'] = $_POST['descr'];
		if (($_POST['type'] == 'url') || ($_POST['type'] == 'url_ports')) {
			$pconfig['address'] = implode(" ", $alias['aliasurl']);
		} else {
			$pconfig['address'] = implode(" ", $address);
		}
		$pconfig['type'] = $_POST['type'];
		$pconfig['detail'] = implode("||", $final_address_details);
	}
}

include("head.inc");

$section_str = array(
	'network' => gettext("Network(s)"),
	'host'	=> gettext("Host(s)"),
	'port' => gettext("Port(s)"),
	'url' => gettext("URL (IPs)"),
	'url_ports' => gettext("URL (Ports)"),
	'urltable' => gettext("URL Table (IPs)"),
	'urltable_ports' => gettext("URL Table (Ports)")
);

$btn_str = array(
	'network' => gettext("Add Network"),
	'host'	=> gettext("Add Host"),
	'port' => gettext("Add Port"),
	'url' => gettext("Add URL"),
	'url_ports' => gettext("Add URL"),
	'urltable' => gettext("Add URL Table"),
	'urltable_ports' => gettext("Add URL Table")
);

$label_str = array(
	'network' => gettext("Network or FQDN"),
	'host'	=> gettext("IP or FQDN"),
	'port' => gettext("Port"),
	'url' => gettext("URL (IPs)"),
	'url_ports' => gettext("URL (Ports)"),
	'urltable' => gettext("URL Table (IPs)"),
	'urltable_ports' => gettext("URL Table (Ports)")
);

$special_cidr_usage_text = gettext("The value after the \"/\" is the update frequency in days.");

$help = array(
	'network' => gettext("Networks are specified in CIDR format. Select the CIDR mask that pertains to each entry. /32 specifies a single IPv4 host, /128 specifies a single IPv6 host, /24 specifies 255.255.255.0, /64 specifies a normal IPv6 network, etc. Hostnames (FQDNs) may also be specified, using a /32 mask for IPv4 or /128 for IPv6. An IP range such as 192.168.1.1-192.168.1.254 may also be entered and a list of CIDR networks will be derived to fill the range."),
	'host' => gettext("Enter as many hosts as desired. Hosts must be specified by their IP address or fully qualified domain name (FQDN). FQDN hostnames are periodically re-resolved and updated. If multiple IPs are returned by a DNS query, all are used. An IP range such as 192.168.1.1-192.168.1.10 or a small subnet such as 192.168.1.16/28 may also be entered and a list of individual IP addresses will be generated."),
	'port' => gettext("Enter ports as desired, with a single port or port range per entry. Port ranges can be expressed by separating with a colon."),
	'url' => gettext("Enter as many URLs as desired. After saving, the URLs will be downloaded and the items imported into the alias. Use only with small sets of IP addresses (less than 3000)."),
	'url_ports' => gettext("Enter as many URLs as desired. After saving, the URLs will be downloaded and the items imported into the alias. Use only with small sets of Ports (less than 3000)."),
	'urltable' => gettext("Enter a single URL containing a large number of IPs and/or Subnets. After saving, the URLs will be downloaded and a table file containing these addresses will be created. This will work with large numbers of addresses (30,000+) or small numbers.") .
		"<br /><b>" . $special_cidr_usage_text . "</b>",
	'urltable_ports' => gettext("Enter a single URL containing a list of Port numbers and/or Port ranges. After saving, the URL will be downloaded.") .
		"<br /><b>" . $special_cidr_usage_text . "</b>",
);

// Tab type specific patterns.
// Intentionally loose (valid character check only, no pattern recognition).
// Can be tightened up with pattern recognition as desired for each tab type.
// Network and host types allow an optional CIDR following the address or an address range using dash separator,
// and there may be multiple items separated by spaces - "192.168.1.0/24 192.168.2.4-192.168.2.19"
// On submit, strings like that are parsed and expanded into the appropriate individual entries and then validated.
$pattern_str = array(
	'network'			=> '[a-zA-Z0-9_:.-]+(/[0-9]+)?( [a-zA-Z0-9_:.-]+(/[0-9]+)?)*',	// Alias Name, Host Name, IP Address, FQDN, Network or IP Address Range
	'host'				=> '[a-zA-Z0-9_:.-]+(/[0-9]+)?( [a-zA-Z0-9_:.-]+(/[0-9]+)?)*',	// Alias Name, Host Name, IP Address, FQDN
	'port'				=> '[a-zA-Z0-9_:]+',	// Alias Name, Port Number, or Port Number Range
	'url'				=> '.*',				// Alias Name or URL
	'url_ports'			=> '.*',				// Alias Name or URL
	'urltable'			=> '.*',				// Alias Name or URL
	'urltable_ports'	=> '.*'					// Alias Name or URL
);

$types = array(
	'host'	=> gettext("Host(s)"),
	'network' => gettext("Network(s)"),
	'port' => gettext("Port(s)"),
	'url' => gettext("URL (IPs)"),
	'url_ports' => gettext("URL (Ports)"),
	'urltable' => gettext("URL Table (IPs)"),
	'urltable_ports' => gettext("URL Table (Ports)"),
);

if (empty($tab)) {
	if (preg_match("/url/i", $pconfig['type'])) {
		$tab = 'url';
	} else if ($pconfig['type'] == 'host') {
		$tab = 'ip';
	} else {
		$tab = $pconfig['type'];
	}
}

if ($input_errors) {
	print_input_errors($input_errors);
}

$form = new Form;

$form->addGlobal(new Form_Input(
	'tab',
	null,
	'hidden',
	$tab
));

$form->addGlobal(new Form_Input(
	'origname',
	null,
	'hidden',
	$pconfig['name']
));

if (isset($id) && $a_aliases[$id]) {
	$form->addGlobal(new Form_Input(
		'id',
		null,
		'hidden',
		$id
	));
}

$section = new Form_Section('Properties');

$section->addInput(new Form_Input(
	'name',
	'Name',
	'text',
	$pconfig['name']
))->setPattern('[a-zA-Z0-9_]+')->setHelp('The name of the alias may only consist '.
	'of the characters "a-z, A-Z, 0-9 and _".');

$section->addInput(new Form_Input(
	'descr',
	'Description',
	'text',
	$pconfig['descr']
))->setHelp('A description may be entered here for administrative reference (not parsed).');

$section->addInput(new Form_Select(
	'type',
	'Type',
	isset($pconfig['type']) ? $pconfig['type'] : $tab,
	$types
));

$form->add($section);

$section = new Form_Section($section_str[$tab]);
// Make somewhere to park the help text, and give it a class so we can update it later
$section->addInput(new Form_StaticText(
	'Hint',
	'<span class="helptext">' . $help[$tab] . '</span>'
));

// If no addresses have been defined, we'll make up a blank set
if ($pconfig['address'] == "") {
	$pconfig['address'] = '';
	$pconfig['address_subnet'] = '';
	$pconfig['detail'] = '';
}

$counter = 0;
$addresses = explode(" ", $pconfig['address']);
$details = explode("||", $pconfig['detail']);

while ($counter < count($addresses)) {
	if (($pconfig['type'] != "host") && is_subnet($addresses[$counter])) {
		list($address, $address_subnet) = explode("/", $addresses[$counter]);
	} else {
		$address = $addresses[$counter];
		if (isset($pconfig['updatefreq'])) {
			// Note: There is only 1 updatefreq possible.
			// The alias types that use updatefreq only allow a single entry.
			$address_subnet = $pconfig['updatefreq'];
		} else {
			$address_subnet = "";
		}
	}

	$group = new Form_Group($counter == 0 ? $label_str[$tab]:'');
	$group->addClass('repeatable');

	$group->add(new Form_IpAddress(
		'address' . $counter,
		$tab == 'port' ? 'Port':'Address',
		$address
	))->addMask('address_subnet' . $counter, $address_subnet)->setWidth(4)->setPattern($pattern_str[$tab]);

	$group->add(new Form_Input(
		'detail' . $counter,
		'Description',
		'text',
		$details[$counter]
	))->setWidth(4);

	$group->add(new Form_Button(
		'deleterow' . $counter,
		'Delete',
		null,
		'fa-trash'
	))->addClass('btn-warning');

	$section->add($group);
	$counter++;
}

$form->addGlobal(new Form_Button(
	'addrow',
	$btn_str[$tab],
	null,
	'fa-plus'
))->addClass('btn-success addbtn');

$form->add($section);

print $form;
?>

<script type="text/javascript">
//<![CDATA[
addressarray = <?= json_encode(array_exclude($pconfig['name'], get_alias_list($pconfig['type']))) ?>;

events.push(function() {

	var disable_subnets;

	function typechange() {
		var tab = $('#type').find('option:selected').val();

		disable_subnets = (tab == 'host') || (tab == 'port') || (tab == 'url') || (tab == 'url_ports');

		// Enable/disable address_subnet so its value gets POSTed or not, as appropriate.
		$("[id^='address_subnet']").prop("disabled", disable_subnets);

		// Show or hide the slash plus address_subnet field so the user does not even see it if it is not relevant.
		hideMask('address_subnet', disable_subnets);

		// Set the help text to match the tab
		var helparray = <?=json_encode($help);?>;
		$('.helptext').html(helparray[tab]);

		// Set the section heading by tab type
		var sectionstr = <?=json_encode($section_str);?>;
		$('.panel-title:last').text(sectionstr[tab]);

		var buttonstr = <?=json_encode($btn_str);?>;
		$('.btn-success').prop('value', buttonstr[tab]);
		$('.btn-success').html('<i class="fa fa-plus icon-embed-btn"></i>' + buttonstr[tab]);

		// Set the input field label by tab
		var labelstr = <?=json_encode($label_str);?>;
		$('.repeatable:first').find('label').text(labelstr[tab]);

		// Set the input field pattern by tab type
		var patternstr = <?=json_encode($pattern_str);?>;
		for (i = 0; i < <?=$counter;?>; i++) {
			$('#address' + i).prop('pattern', patternstr[tab]);
		}

		// Hide and disable rows other than the first
		hideRowsAfter(1, (tab == 'urltable') || (tab == 'urltable_ports'));

		// The add button and delete buttons must not show on URL Table IP or URL table ports
		if ((tab == 'urltable') || (tab == 'urltable_ports')) {
			hideClass('addbtn', true);
			$('[id^=deleterow]').hide();
		} else {
			hideClass('addbtn', false);
			$('[id^=deleterow]').show();
		}
	}

	// Hide and disable all rows >= that specified
	function hideRowsAfter(row, hide) {
		var idx = 0;

		$('.repeatable').each(function(el) {
			if (idx >= row) {
				hideRow(idx, hide);
			}

			idx++;
		});
	}

	function hideRow(row, hide) {
		if (hide) {
			$('#deleterow' + row).parent('div').parent().addClass('hidden');
		} else {
			$('#deleterow' + row).parent('div').parent().removeClass('hidden');
		}

		// We need to disable the elements so they are not submitted in the POST
		$('#address' + row).prop("disabled", hide);
		$('#address_subnet' + row).prop("disabled", hide || disable_subnets);
		$('#detail' + row).prop("disabled", hide);
		$('#deleterow' + row).prop("disabled", hide);
	}

	// On load . .
	typechange();

	// Suppress "Delete row" button if there are fewer than two rows
	checkLastRow();

	// Autocomplete
	$('[id^=address]').each(function() {
		if (this.id.substring(0, 8) != "address_") {
			$(this).autocomplete({
				source: addressarray
			});
		}
	});

	// on click . .
	$('#type').on('change', function() {
		typechange();
	});

});
//]]>
</script>

<?php
include("foot.inc");


text:
  // [START get_messaging_object]
  // Retrieve Firebase Messaging object.
  const messaging = firebase.messaging();
  // [END get_messaging_object]
  // IDs of divs that display Instance ID token UI or request permission UI.
  const tokenDivId = 'token_div';
  const permissionDivId = 'permission_div';
  // [START refresh_token]
  // Callback fired if Instance ID token is updated.
  messaging.onTokenRefresh(function() {
    messaging.getToken()
    .then(function(refreshedToken) {
      console.log('Token refreshed.');
      // Indicate that the new Instance ID token has not yet been sent to the
      // app server.
      setTokenSentToServer(false);
      // Send Instance ID token to app server.
      sendTokenToServer(refreshedToken);
      // [START_EXCLUDE]
      // Display new Instance ID token and clear UI of all previous messages.
      resetUI();
      // [END_EXCLUDE]
    })
    .catch(function(err) {
      console.log('Unable to retrieve refreshed token ', err);
      showToken('Unable to retrieve refreshed token ', err);
    });
  });
  // [END refresh_token]
  // [START receive_message]
  // Handle incoming messages. Called when:
  // - a message is received while the app has focus
  // - the user clicks on an app notification created by a sevice worker
  //   `messaging.setBackgroundMessageHandler` handler.
  messaging.onMessage(function(payload) {
    console.log("Message received. ", payload);
    // [START_EXCLUDE]
    // Update the UI to include the received message.
    appendMessage(payload);
    // [END_EXCLUDE]
  });
  // [END receive_message]
  function resetUI() {
    clearMessages();
    showToken('loading...');
    // [START get_token]
    // Get Instance ID token. Initially this makes a network call, once retrieved
    // subsequent calls to getToken will return from cache.
    messaging.getToken()
    .then(function(currentToken) {
      if (currentToken) {
        sendTokenToServer(currentToken);
        updateUIForPushEnabled(currentToken);
      } else {
        // Show permission request.
        console.log('No Instance ID token available. Request permission to generate one.');
        // Show permission UI.
        updateUIForPushPermissionRequired();
        setTokenSentToServer(false);
      }
    })
    .catch(function(err) {
      console.log('An error occurred while retrieving token. ', err);
      showToken('Error retrieving Instance ID token. ', err);
      setTokenSentToServer(false);
    });
  }
  // [END get_token]
  function showToken(currentToken) {
    // Show token in console and UI.
    var tokenElement = document.querySelector('#token');
    tokenElement.textContent = currentToken;
  }
  // Send the Instance ID token your application server, so that it can:
  // - send messages back to this app
  // - subscribe/unsubscribe the token from topics
  function sendTokenToServer(currentToken) {
    if (!isTokenSentToServer()) {
      console.log('Sending token to server...');
      // TODO(developer): Send the current token to your server.
      setTokenSentToServer(true);
    } else {
      console.log('Token already sent to server so won\'t send it again ' +
          'unless it changes');
    }
  }
  function isTokenSentToServer() {
    if (window.localStorage.getItem('sentToServer') == 1) {
          return true;
    }
    return false;
  }
  function setTokenSentToServer(sent) {
    if (sent) {
      window.localStorage.setItem('sentToServer', 1);
    } else {
      window.localStorage.setItem('sentToServer', 0);
    }
  }
  function showHideDiv(divId, show) {
    const div = document.querySelector('#' + divId);
    if (show) {
      div.style = "display: visible";
    } else {
      div.style = "display: none";
    }
  }
  function requestPermission() {
    console.log('Requesting permission...');
    // [START request_permission]
    messaging.requestPermission()
    .then(function() {
      console.log('Notification permission granted.');
      // TODO(developer): Retrieve an Instance ID token for use with FCM.
      // [START_EXCLUDE]
      // In many cases once an app has been granted notification permission, it
      // should update its UI reflecting this.
      resetUI();
      // [END_EXCLUDE]
    })
    .catch(function(err) {
      console.log('Unable to get permission to notify.', err);
    });
    // [END request_permission]
  }
  function deleteToken() {
    // Delete Instance ID token.
    // [START delete_token]
    messaging.getToken()
    .then(function(currentToken) {
      messaging.deleteToken(currentToken)
      .then(function() {
        console.log('Token deleted.');
        setTokenSentToServer(false);
        // [START_EXCLUDE]
        // Once token is deleted update UI.
        resetUI();
        // [END_EXCLUDE]
      })
      .catch(function(err) {
        console.log('Unable to delete token. ', err);
      });
      // [END delete_token]
    })
    .catch(function(err) {
      console.log('Error retrieving Instance ID token. ', err);
      showToken('Error retrieving Instance ID token. ', err);
    });
  }
  // Add a message to the messages element.
  function appendMessage(payload) {
    const messagesElement = document.querySelector('#messages');
    const dataHeaderELement = document.createElement('h5');
    const dataElement = document.createElement('pre');
    dataElement.style = 'overflow-x:hidden;'
    dataHeaderELement.textContent = 'Received message:';
    dataElement.textContent = JSON.stringify(payload, null, 2);
    messagesElement.appendChild(dataHeaderELement);
    messagesElement.appendChild(dataElement);
  }
  // Clear the messages element of all children.
  function clearMessages() {
    const messagesElement = document.querySelector('#messages');
    while (messagesElement.hasChildNodes()) {
      messagesElement.removeChild(messagesElement.lastChild);
    }
  }
  function updateUIForPushEnabled(currentToken) {
    showHideDiv(tokenDivId, true);
    showHideDiv(permissionDivId, false);
    showToken(currentToken);
  }
  function updateUIForPushPermissionRequired() {
    showHideDiv(tokenDivId, false);
    showHideDiv(permissionDivId, true);
  }
  resetUI();

redpajama_set_name: RedPajamaGithub


text:
import collections

import pytest

from fenrir.http.parser import HTTPParser, ParserError

ParserResult = collections.namedtuple(
    "ParserResult",
    ["http_version", "method", "path", "query", "headers"],
)

class TestHTTPParser:

    @pytest.mark.parametrize(
        ("lines", "expected"),
        [
            (
                [b"GET / HTTP/1.1\r\n", b"\r\n"],
                ParserResult(b"HTTP/1.1", b"GET", b"/", None, []),
            ),
            (
                [b"GET /?q=wat HTTP/1.1\r\n", b"\r\n"],
                ParserResult(b"HTTP/1.1", b"GET", b"/", b"q=wat", []),
            ),
            (
                [b"GET / HTTP/1.1\r\n", b"Foo: Bar\r\n", b"\r\n"],
                ParserResult(
                    b"HTTP/1.1",
                    b"GET",
                    b"/",
                    None,
                    [(b"Foo", b"Bar")],
                ),
            ),
            (
                [
                    b"GET / HTTP/1.1\r\n",
                    b"Foo: Bar\r\n",
                    b"Wat: Ok\r\n",
                    b"Foo: BarTwo\r\n",
                    b"\r\n",
                ],
                ParserResult(
                    b"HTTP/1.1",
                    b"GET",
                    b"/",
                    None,
                    [(b"Foo", b"Bar"), (b"Wat", b"Ok"), (b"Foo", b"BarTwo")],
                ),
            ),
        ],
    )
    def test_parse_success(self, lines, expected):
        parser = HTTPParser()
        for line in lines:
            assert parser.parse(line) == len(line)

        assert parser.finished
        assert parser.http_version == expected.http_version
        assert parser.method == expected.method
        assert parser.path == expected.path
        assert parser.query == expected.query
        assert parser.headers == expected.headers

    @pytest.mark.parametrize(
        "lines",
        [
            [b"GET / HTTP/1.1\r\n", b"Foo : Bar\r\n"],
        ],
    )
    def test_parse_error(self, lines):
        parser = HTTPParser()
        for line in lines[:-1]:
            assert parser.parse(line) == len(line)

        with pytest.raises(ParserError):
            parser.parse(lines[-1])

    def test_parse_offset_length(self):
        msg = (
            b"GET / HTTP/1.1\r\n"
            b"Foo: Bar\r\n"
            b"\r\n"
        )

        parser = HTTPParser()

        assert parser.parse(msg, 5) == 5
        assert parser.parse(msg, 10, 5) == 10
        assert parser.parse(msg, offset=15) == 13

        assert parser.finished
        assert parser.http_version == b"HTTP/1.1"
        assert parser.method == b"GET"
        assert parser.path == b"/"
        assert parser.query is None
        assert parser.headers == [(b"Foo", b"Bar")]

    def test_parse_past_data_end(self):
        msg = (
            b"GET / HTTP/1.1\r\n"
            b"Foo: Bar\r\n"
            b"\r\n"
        )

        parser = HTTPParser()

        with pytest.raises(ValueError):
            parser.parse(msg, 10000)

    def test_parser_error_improperly_escaped_urls(self):
        """
        RFC 7230 Section 3.1.1 states:

        Recipients of an invalid request-line SHOULD respond with either a 400
        (Bad Request) error or a 301 (Moved Permanently) redirect with the
        request-target properly encoded. A recipient SHOULD NOT attempt to
        autocorrect and then process the request without a redirect, since the
        invalid request-line might be deliberately crafted to bypass security
        filters along the request chain.
        """
        parser = HTTPParser()

        with pytest.raises(ParserError):
            parser.parse(b"GET /foo bar/ HTTP/1.1\r\n\r\n")

    def test_parser_error_space_in_field_name(self):
        """
        RFC 7320 Section 3.2.4 states:

        No whitespace is allowed between the header field-name and colon. In
        the past, differences in the handling of such whitespace have led to
        security vulnerabilities in request routing and response handling. A
        server MUST reject any received request message that contains
        whitespace between a header field-name and colon with a response code
        of 400 (Bad Request).
        """
        parser = HTTPParser()

        with pytest.raises(ParserError):
            parser.parse(b"GET / HTTP/1.1\r\nFoo : Bar\r\n\r\n")

    def test_header_values_ignore_leading_trailing_whitespace(self):
        """
        RFC 7320 Section 3.2.4 states:

        A field value might be preceded and/or followed by optional whitespace
        (OWS); a single SP preceding the field-value is preferred for
        consistent readability by humans. The field value does not include any
        leading or trailing whitespace: OWS occurring before the first
        non-whitespace octet of the field value or after the last
        non-whitespace octet of the field value ought to be excluded by parsers
        when extracting the field value from a header field.
        """
        parser = HTTPParser()
        parser.parse(b"GET / HTTP/1.1\r\nFoo: Bar    \r\n\r\n")

        assert parser.headers == [(b"Foo", b"Bar")]

    def test_parser_accepts_extraneous_newlines(self):
        """
        RFC 7230 Section 3.5 states:

        In the interest of robustness, a server that is expecting to receive
        and parse a request-line SHOULD ignore at least one empty line (CRLF)
        received prior to the request-line.
        """
        parser = HTTPParser()
        parser.parse(b"\r\n\r\n")
        parser.parse(b"\r\n\nGET / HTTP/1.1\r\n\r\n")

        assert parser.http_version == b"HTTP/1.1"
        assert parser.method == b"GET"
        assert parser.path == b"/"
        assert parser.query is None
        assert parser.headers == []

    def test_parser_handles_newlines(self):
        """
        RFC 7230 Section 3.5 states:

        Although the line terminator for the start-line and header fields is
        the sequence CRLF, a recipient MAY recognize a single LF as a line
        terminator and ignore any preceding CR.
        """
        parser = HTTPParser()
        parser.parse(b"GET / HTTP/1.1\nFoo: Bar\n\n")

        assert parser.http_version == b"HTTP/1.1"
        assert parser.method == b"GET"
        assert parser.path == b"/"
        assert parser.query is None
        assert parser.headers == [(b"Foo", b"Bar")]


text:
package main

import (
	"fmt"
	"io/ioutil"
	"log"
	"os"
)

func updateKV(vaultDir, key, glacierId string) error {
	dbPath := makePath(vaultDir, CONF_DIR, DB)
	kv := LoadBadger(dbPath)
	return updateVaultFileWithDigest(kv, key, glacierId)
}

func pushFiles(ctx *AWSContext) {
	vaultDir := ctx.baseDirectory()
	setAwsEnv(vaultDir)
	cacheFilePath := makePath(vaultDir, CONF_DIR, CACHE)
	svc := NewService(ctx.awsRegion())
	files, err := ioutil.ReadDir(cacheFilePath)
	if err != nil {
		log.Fatal("error access cache directory")
	}
	if len(files) == 0 {
		fmt.Println("Nothing to push")
	} else {
		fmt.Println("Start pushing")
	}
	for _, fi := range files {
		fn := makePath(cacheFilePath, fi.Name())
		output, err := UploadFile(fn, ctx.remote(), svc)
		if err != nil {
			continue // silently fail
		}
		glacierId := output.ArchiveId
		err = updateKV(vaultDir, fi.Name(), *glacierId)
		if err != nil {
			continue // silently fail
		}
		os.Remove(fn)
	}
}


text:
web-application-development-101
===============================

A top-down approach to becoming an affective application developmer for the web.


text: A Boilerplate of a MEAN Stack using RestfulExpress


text:
package Q;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

public class Label_Assigner
{

	 @SuppressWarnings({ "unchecked", "null" })
	public static void main(String[] args) throws Exception
    {
	        String jsonTxt_Q = null;

		    // GET JSON DATA
	        File Q_Value_file = new File("/home/matthias/Workbench/SUTD/nytimes_corpus/wdtk-parent/wdtk-examples/JSON_Output/user_29.json");

	        //raed in the Q values
	        if (Q_Value_file.exists())
	        {
	            InputStream is = new FileInputStream("/home/matthias/Workbench/SUTD/nytimes_corpus/wdtk-parent/wdtk-examples/JSON_Output/user_29.json");
	            jsonTxt_Q = IOUtils.toString(is);
	        }
	        //reformat
	        jsonTxt_Q = ( jsonTxt_Q.substring(1, jsonTxt_Q.length()-1) ).replace("\\","");

	        Gson json_Q = new Gson();
	        Map<String, ArrayList<String>> massive_Q_storage_map = new HashMap<String, ArrayList<String>>();
	        massive_Q_storage_map = (Map<String, ArrayList<String>>) json_Q.fromJson(jsonTxt_Q, massive_Q_storage_map.getClass());
	        System.out.println(massive_Q_storage_map);

	        BufferedReader reader = new BufferedReader(new FileReader(new File("/home/matthias/Workbench/SUTD/nytimes_corpus/wdtk-parent/wdtk-examples/wikidata-properties-dumper-master/outputs/properties-en.json")));
	        Map<String, Map<String, Object>> map =
	        new Gson().fromJson(reader, HashMap.class);

	        // Read in all the sentences, that are in files, in this folder
	        final File folder = new File("/home/matthias/Workbench/SUTD/nytimes_corpus/wdtk-parent/wdtk-examples/JSON_Output/");

	        String filename_for_sentences = null;

            for (final File fileEntry : folder.listFiles())
            {
            	Document doc = null;

            	BufferedReader br = new BufferedReader(new FileReader(fileEntry));
    	    	try
    	    	{
    	    		//Store the filename
    	    		filename_for_sentences = fileEntry.getName();

	                StringBuilder sb = new StringBuilder();
		            String line = br.readLine();

		            while (line != null)
		            {
		                sb.append(line);
		                sb.append(System.lineSeparator());
		                line = br.readLine();
		            }
		            String everything = sb.toString();
		            //System.out.println(everything);

		            doc = Jsoup.parse(everything);
    	    	}
    	    	finally
    	    	{
    	            br.close();
    	        }

	            Elements sentences = doc.getElementsByTag("sentence");

	            String sentence_number = null;

	            for (Element sentence : sentences)
		        {
		        	//hold the q values we need to search over
	            	//List<String> Q_value_references_for_sentence_entities = new ArrayList<String>();
	            	//List<String> English_names_references_for_sentence_entities = new ArrayList<String>();

	            	Map<String,String> Q_value_references_for_sentence_entities__and_ENGLISH_NAME = new HashMap<String,String>();

	            	//store the sentence number
	            	sentence_number = sentence.select("sentence").text();
	            	sentence_number = sentence_number.substring(0, sentence_number.indexOf(" "));

	            	//get all the entities in this sentence
		        	Elements pers = sentence.select("PERSON");
		        	Elements locs = sentence.select("LOCATION");
		        	Elements orgs = sentence.select("ORGANIZATION");

		        	//collect all the elements to a list, all the elements of one sentence
		        	List<String> sentence_entity_list = new ArrayList<String>();

		        	for (Element per : pers)
			        {
		        		sentence_entity_list.add(per.text().trim());
			        }
		        	for (Element loc : locs)
			        {
		        		sentence_entity_list.add(loc.text().trim());
			        }
		        	for (Element org : orgs)
			        {
		        		sentence_entity_list.add(org.text().trim());
			        }

		        	// for the list of Q values to keys
		        	for (Entry<String, ArrayList<String>> DB__key_to_value_item : massive_Q_storage_map.entrySet())
		        	{
		        		for (String entity : sentence_entity_list)
		        		{
			        		if (DB__key_to_value_item.getKey().toLowerCase().contains(entity.toLowerCase()))
			        		{
			        		    //for all the entries( Qs) associated with that key
			        			//add them to Q_value_references_for_sentence_entities

			        			for (String Q_value : DB__key_to_value_item.getValue())
			        			{
			        				//Q_value_references_for_sentence_entities.add( Q_value );
			        				//English_names_references_for_sentence_entities.add(DB__key_to_value_item.getKey());

			        				Q_value_references_for_sentence_entities__and_ENGLISH_NAME.put( Q_value, DB__key_to_value_item.getKey());
			        			}
			        		}
		        		}
		        	}

                    //czeher
					Map<String, Pair<String,String>> URL_and_entities = new HashMap<String, Pair<String,String>>();

				    for (Entry<String,String> entity_1: Q_value_references_for_sentence_entities__and_ENGLISH_NAME.entrySet())
				    {

				      for (Entry<String,String> entity_2: Q_value_references_for_sentence_entities__and_ENGLISH_NAME.entrySet())
				      {
				    	    if( entity_1.getValue().toLowerCase().contains( entity_2.getValue().toLowerCase() ) )
				    	    {
				    	    	continue;
				    	    }
				    	    else
				    	    {
					     		String URL_czech = "http://milenio.dcc.uchile.cl/sparql?default-graph-uri=&query=PREFIX+%3A+%3Chttp%3A%2F%2Fwww.wikidata.org%2Fentity%2F%3E%0D%0ASELECT+*+WHERE+%7B%0D%0A+++%3A"
					     					       + entity_1.getKey() + "+%3FsimpleProperty+%3A"
					     					       + entity_2.getKey() + "%0D%0A%7D%0D%0A&format=text%2Fhtml&timeout=0&debug=on";

					    		URL wikidata_page = new URL(URL_czech);
					    		HttpURLConnection wiki_connection = (HttpURLConnection)wikidata_page.openConnection();
					    		InputStream wikiInputStream = null;

								try
								{
								    // try to connect and use the input stream
								    wiki_connection.connect();
								    wikiInputStream = wiki_connection.getInputStream();
								}
								catch(IOException error)
								{
								    // failed, try using the error stream
								    wikiInputStream = wiki_connection.getErrorStream();
								}
								// parse the input stream using Jsoup
								Document docx = Jsoup.parse(wikiInputStream, null, wikidata_page.getProtocol()+"://"+wikidata_page.getHost()+"/");

								Elements link_text = docx.select("table.sparql > tbody > tr:nth-child(2) > td > a");
								//link_text.text();
								for (Element l : link_text)
							    {
									String output = l.text();
									output = output.substring(0, output.length()-1);

							       URL_and_entities.put(output, Pair.of(entity_1.getValue(), entity_2.getValue()));

							    }
				    	    }
				        }
				    }

                    //now we're back at the sentence level
					//iter hash
				    for (Entry<String, Pair<String, String>> url : URL_and_entities.entrySet())
				    {

				 		String P_value = url.getKey().substring(url.getKey().lastIndexOf('P'));

				 		System.out.println( "File name: " + filename_for_sentences );
				 		System.out.println( "sentence_number: " + sentence_number );

				 		String value = (String) map.get("properties").get( P_value );

						String first__english_lang_Q = URL_and_entities.get( url.getKey() ).getLeft();
						String second_english_lang_Q = URL_and_entities.get( url.getKey() ).getRight();

						System.out.println("`(" + value + ")'" + "`( " + first__english_lang_Q + ", " + second_english_lang_Q + ")'");

				    }

				}

            }

	}

}


text:
layout: post
title: Eliminate bad referral spam with extreme prejudice
published: true
---

Spam bots be like, "Hey I'm just going increase my search ranking by having this code visit your site." It doesn't hurt, except it does, by injecting a lot of confusion to those unaware of their site stats.

##IF you don't block this you'll see increasing traffic + increasing bounce rate.

A lot of small business sites probably see this often. It is the first thing a decent SEO does when setting up your Google analytics stuff.

##How to get rid of it

A good way that most people can block spam referrals is by using a Segment.

[Analytics edge post on creating a filtered segment](http://www.analyticsedge.com/2015/01/advanced-segment-eliminate-spam-referrals/)

Here is how to do it.

1. Create a new segment named *company name* Referral Filter

2. Filter > sessions > include > Hostname + matches regex + "companywebsite.com.au"

3. Filter > sessions > exclude > Medium + exactly matches + referral - AND - Source - matches regex > spamreferralwebsite.com|spamreferralwebsite.com

4. Limit domain listing to 4 before creating a new (or) then source + matches regex + domain|domain

For power users they can try using [Simo Ahava's spam filter insertion tool](http://www.simoahava.com/analytics/spam-filter-insertion-tool/)


text:
'use strict';

window.App = require('config/app');
require('config/router');
require('config/store');

// Load all modules in order automagically. Ember likes things to work this
// way so everything is in the App.* namespace.
var folderOrder = [
    'initializers', 'mixins', 'routes', 'models',
    'views', 'controllers', 'helpers',
    'templates', 'components'
  ];

folderOrder.forEach(function(folder) {
  window.require.list().filter(function(module) {
    return new RegExp('^' + folder + '/').test(module);
  }).forEach(function(module) {
    require(module);
  });
});


text:
Public Class HeaderedItemsViewModel
    Inherits ItemsViewModel
    Implements IHeaderedViewModel

    Private _Header As Object
    Public Property Header As Object Implements IHeaderedViewModel.Header
        Get
            Return _Header
        End Get
        Set(value As Object)
            _Header = value
            Me.OnPropertyChanged("Header")
        End Set
    End Property

End Class


text:
#ifndef JUCE_SHAPEBUTTON_H_INCLUDED
#define JUCE_SHAPEBUTTON_H_INCLUDED

//==============================================================================
/**
    A button that contains a filled shape.

    @see Button, ImageButton, TextButton, ArrowButton
*/
class JUCE_API  ShapeButton  : public Button
{
public:
    //==============================================================================
    /** Creates a ShapeButton.

        @param name             a name to give the component - see Component::setName()
        @param normalColour     the colour to fill the shape with when the mouse isn't over
        @param overColour       the colour to use when the mouse is over the shape
        @param downColour       the colour to use when the button is in the pressed-down state
    */
    ShapeButton (const String& name,
                 Colour normalColour,
                 Colour overColour,
                 Colour downColour);

    /** Destructor. */
    ~ShapeButton();

    //==============================================================================
    /** Sets the shape to use.

        @param newShape                 the shape to use
        @param resizeNowToFitThisShape  if true, the button will be resized to fit the shape's bounds
        @param maintainShapeProportions if true, the shape's proportions will be kept fixed when
                                        the button is resized
        @param hasDropShadow            if true, the button will be given a drop-shadow effect
    */
    void setShape (const Path& newShape,
                   bool resizeNowToFitThisShape,
                   bool maintainShapeProportions,
                   bool hasDropShadow);

    /** Set the colours to use for drawing the shape.

        @param normalColour     the colour to fill the shape with when the mouse isn't over
        @param overColour       the colour to use when the mouse is over the shape
        @param downColour       the colour to use when the button is in the pressed-down state
    */
    void setColours (Colour normalColour,
                     Colour overColour,
                     Colour downColour);

    /** Sets up an outline to draw around the shape.

        @param outlineColour        the colour to use
        @param outlineStrokeWidth   the thickness of line to draw
    */
    void setOutline (Colour outlineColour, float outlineStrokeWidth);

    /** This lets you specify a border to be left around the edge of the button when
        drawing the shape.
    */
    void setBorderSize (BorderSize<int> border);

    /** @internal */
    void paintButton (Graphics&, bool isMouseOverButton, bool isButtonDown) override;

private:
    //==============================================================================
    Colour normalColour, overColour, downColour, outlineColour;
    DropShadowEffect shadow;
    Path shape;
    BorderSize<int> border;
    bool maintainShapeProportions;
    float outlineWidth;

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ShapeButton)
};

#endif   // JUCE_SHAPEBUTTON_H_INCLUDED


text:
'use strict';

var assert = require('assert'),
    Promise = require('bluebird');

/**
 *
 *
 * @param {Function} git The gitftw module
 * @returns {Function} The gitftw with the new commands
 */
module.exports = function commonCommands(git) {
  [
    commit,
    version,
    clone,
    add,
    push,
    pull,
    checkout,
    merge,
    fetch,
    getCurrentBranch,
    getRemoteBranches,
    getLocalBranches,
    removeLocalBranch,
    removeRemoteBranch,
    isClean,
    tag,
    getTags,
    removeLocalTags,
    removeRemoteTags,
    removeTags
  ].forEach(git.command);

  return git;

  //////////// Commands implementation

  /**
   * Gets current installed git version
   * Executes `git version`
   *
   * @example
   * var git = require('gitftw');
   *
   * git.version().then(console.log) //outputs 1.8.2.3
   *
   *
   * @memberof git
   * @type {command}
   * @returns {Promise} Promise Resolves with the git version
   */
  function version() {
    var args = [
      'version'
    ];

    return git(args)
        .then(parseVersion);
  }

  /**
   * Commits the staging area
   * Executes `git commit -m "First commit"`
   *
   * It does not fail when there is not anything to commit
   *
   * @example
   * var git = require('gitftw');
   *
   * git.commit({
   *   message: 'First commit'
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} options.message The commit message
   * @param {Resolvable|Boolean} [options.force] Replace the tip of the current branch by creating
   *   a new commit. The --amend flag
   * @param {Resolvable|Boolean} [options.noVerify] This option bypasses the pre-commit
   *   and commit-msg hooks
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function commit(options) {
    assert.ok(options.message, 'message is mandatory');

    var args = [
      'commit',
      options.force ? '--amend' : null,
      options.noVerify ? '-n' : null,
      options.message ? '-m' : null,
      options.message ? options.message : null
    ];

    return git(args)
        .catch(passWarning)
        .then(silent);
  }

  /**
   * Clones a git repo
   *
   * If both a branch and a tag are specified, the branch takes precedence
   *
   * @example
   * var git = require('gitftw');
   *
   * git.clone({
   *  repository: 'git@github.com:jmendiara/node-lru-cache.git',
   *  directory: './cache' //optional
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} options.repository The git repository endpoint
   * @param {Resolvable|String} [options.directory] The directory where the repo will be cloned
   *   By default, git will clone it a new one specifed by the repo name
   * @param {Resolvable|String} [options.branch] The remote repo branch to checkout after clone.
   * @param {Resolvable|String} [options.tag] The remote repo tag to checkout after clone.
   * @param {Resolvable|String} [options.origin] Instead of using the remote name origin to keep
   *   track of the upstream repository, use this parameter value
   * @param {Resolvable|Boolean} [options.recursive] After the clone is created, initialize all
   * @param {Resolvable|Boolean} [options.bare] Make a bare Git repository.: neither remote-tracking
   *   branches nor the related configuration variables are created.
   * @param {Resolvable|Number} [options.depth] Create a shallow clone with a history truncated to
   *   the specified number of revisions.
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function clone(options) {
    assert.ok(options.repository, 'repository is mandatory');

    var branchOrTag = options.branch || options.tag;
    var args = [
      'clone',
      options.repository,
      options.directory,
      branchOrTag ? ('-b' + branchOrTag) : null,
      options.origin ? ('-o' + options.origin) : null,
      options.recursive ? '--recursive' : null,
      options.bare ? '--bare' : null,
      options.depth ? '--depth' : null,
      options.depth ? '' + options.depth : null
    ];

    return git(args)
        .then(silent);
  }

  /**
   * Adds filenames to the stashing area
   * Issues `git add README.md`
   *
   * @example
   * var git = require('gitftw');
   *
   * git.add({
   *   files: ['README.md', 'index.js']
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|Array<String>} options.files The files to be added, relative to the cwd
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function add(options) {
    assert.ok(options.files, 'files is mandatory');

    return options.files
        .filter(function(file) {
          //Git exits OK with empty filenames.
          //Avoid an unnecessary call to git in these cases by removing the filename
          return !!file;
        })
        .reduce(function(soFar, file) {
          var args = ['add', file];
          return soFar
              .then(gitFn(args))
              .then(silent);
        }, Promise.resolve());
  }

  /**
   * Push the change sets to server
   * Executes `git push origin master`
   *
   * Defaults to "origin", and don't follow configured refspecs
   * for the upstream
   *
   * If both a branch and a tag are specified, the branch takes precedence
   *
   * @example
   * var git = require('gitftw');
   *
   * git.push(); //the current branch to `origin`
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} [options.remote="origin"] The remote ref to push to
   * @param {Resolvable|String} [options.branch="HEAD"] The branch to push. HEAD will push the
   *   current branch
   * @param {Resolvable|String} [options.tag] The tag to push
   * @param {Resolvable|Boolean} [options.force] Force a remote update. Can cause the remote
   *   repository to lose commits; use it with care. --force flag
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function push(options) {
    var branchOrTag = options.branch || options.tag;

    var args = [
      'push',
      options.remote || 'origin',
      branchOrTag || 'HEAD',
      options.force ? '--force' : null
    ];

    return git(args)
        .then(silent);
  }

  /**
   * Pulls a remote branch into the current one
   * Executes `git pull origin master`
   *
   * remote defaults to "origin", and don't follow configured refspecs
   * for the upstream
   *
   * If both a branch and a tag are specified, the branch takes precedence
   *
   * When no branch and tag are specifies, this command will try
   * to pull the actual local branch name from the remote
   *
   * @example
   * var git = require('gitftw');
   *
   * //While in master...
   * git.getCurrentBranch().then(console.log)
   * //Outputs: master
   *
   * //Pulls origin/master into the current branch (master)
   * git.pull()
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} [options.remote="origin"] The remote
   * @param {Resolvable|String} [options.branch=currentBranch] The remote branch to pull
   * @param {Resolvable|String} [options.tag] The remote tag to pull
   * @param {Resolvable|Boolean} [options.rebase] Make a rebase (--rebase tag)
   * @param {callback} [cb] The execution callback result
   * @return {Promise} Resolves with undefined
   */
  function pull(options) {
    options = options || {};

    var branchOrTag = options.branch || options.tag;

    var args = [
      'pull',
      options.rebase ? '--rebase' : null,
      options.remote || 'origin',
      branchOrTag || git.getCurrentBranch
    ];

    return git(args)
        .then(silent);
  }

  /**
   * Checkout a local branch
   *
   * Executes `git checkout -B issues/12`
   *
   * If you specify create, it will try to create the branch,
   * or will checkout it if it already exists
   *
   * If both a branch and a tag are specified, the branch takes precedence
   *
   * Cannot use create and orphan both together
   *
   * @example
   * var git = require('gitftw');
   *
   * git.checkout({
   *   branch: 'master'
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} options.branch The branch to checkout
   * @param {Resolvable|String} [options.tag] The tag to checkout
   * @param {Resolvable|Boolean} [options.create] Try to create the branch (-B flag)
   * @param {Resolvable|Boolean} [options.oldCreate] Try to create the branch (-b flag). Do
   *  not use along with 'create'.
   * @param {Resolvable|Boolean} [options.orphan] Create an orphan branch (--orphan flag)
   * @param {Resolvable|Boolean} [options.force] When switching branches, proceed even if
   *  the index or the working tree differs from HEAD. This is used to throw
   *  away local changes. (-f flag)
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function checkout(options) {
    var branchOrTag = options.branch || options.tag;

    assert.ok(branchOrTag, 'branch or tag is mandatory');

    if ((options.create || options.oldCreate) && options.orphan) {
      throw new Error('create and orphan cannot be specified both together');
    }

    if (options.create && options.oldCreate) {
      throw new Error('create and oldCreate cannot be specified both together');
    }

    var args = [
      'checkout',
      options.create ? '-B' : options.oldCreate ? '-b' : null,
      options.orphan ? '--orphan' : null,
      branchOrTag,
      options.force ? '-f' : null
    ];

    return git(args)
        .then(silent);
  }

  /**
   * Merges branches a branch in the current one
   * Executes `git merge --no-ff origin/issues13 -m "Remote branch merge"
   *
   * @example
   * var git = require('gitftw');
   *
   * //while in master...
   * git.merge({
   *   branch: 'issue/12',
   *   message: 'Merge branch issue/12 into master'
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} [options.remote] The remote where the branch is located
   * @param {Resolvable|String} options.branch The branch to merge
   * @param {Resolvable|String} options.message The merge message
   * @param {Resolvable|Boolean} [options.noFF] Make a no fast forward merge,
   *  creating a new sha (--no-ff flag)
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function merge(options) {
    assert.ok(options.branch, 'branch is mandatory');
    assert.ok(options.message, 'message is mandatory');

    var args = [
      'merge',
      options.noFF ? '--no-ff' : null,
      options.remote ? (options.remote + '/' + options.branch) : options.branch,
      '-m',
      options.message
    ];

    return git(args)
        .then(silent);
  }

  /**
   * Fetches a remote
   * Executes `git fetch origin --tags`
   *
   * remote defaults to "origin", and don't follow configured refspecs
   * for the upstream
   *
   * @example
   * var git = require('gitftw');
   *
   * git.fetch(); //fetches origin
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} [options.remote="origin"] The remote to fetch
   * @param {Resolvable|Boolean} [options.tags] Fetch the tags (--tags flag)
   * @param {Resolvable|Boolean} [options.prune] remove any remote-tracking references that no
   *   longer exist on the remote (--prune)
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function fetch(options) {
    options = options || {};

    var args = [
      'fetch',
      options.remote || 'origin',
      options.tags ? '--tags' : null,
      options.prune ? '--prune' : null
    ];

    return git(args)
        .then(silent);
  }

  /**
   * Gets the current branch
   * Executes `git rev-parse --abbrev-ref HEAD`
   *
   * @example
   * var git = require('gitftw');
   *
   * //while in master...
   * git.getCurrentBranch().then(console.log); //outputs 'master';
   *
   * @memberof git
   * @type {command}
   *
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with the current branch
   */
  function getCurrentBranch() {
    var args = [
      'rev-parse',
      '--abbrev-ref',
      'HEAD'
    ];

    return git(args);
  }

  /**
   * Removes a local branch
   *
   * @example
   * var git = require('gitftw');
   *
   * //while in master...
   * git.removeLocalBranch({
   *   branch: 'master'
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} options.branch The branch name
   * @param {Resolvable|Boolean} [options.force] force the delete (-D)
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function removeLocalBranch(options) {
    assert.ok(options.branch, 'branch is mandatory');

    var args = [
      'branch',
      options.force ? '-D' : '-d',
      options.branch
    ];

    return git(args)
        .then(silent);
  }

  /**
   * Removes a remote branch
   *
   * @example
   * var git = require('gitftw');
   *
   * //while in master...
   * git.removeRemoteBranch({
   *   branch: 'master'
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} options.branch The branch name
   * @param {Resolvable|String} [options.remote="origin"] The remote ref where the branch will be removed
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function removeRemoteBranch(options) {
    assert.ok(options.branch, 'branch is mandatory');

    var args = [
      'push',
      options.remote || 'origin',
      ':' + options.branch
    ];

    return git(args)
        .then(silent);
  }

  /**
   * Gets the current workspace is clean or has something in the working tree
   * Executes `git diff-index --quiet HEAD .`
   *
   * @example
   * var git = require('gitftw');
   *
   * git.isClean()
   *     .then(function(clean) {
   *       if (clean) {
   *         console.log('The git workspace is clean');
   *       } else {
   *         console.log('The git workspace is dirty');
   *       }
   *     });
   *
   * @memberof git
   * @type {command}
   *
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with true if the workspace is clean.
   *   false otherwise
   */
  function isClean() {
    var args = [
      'diff-index',
      '--quiet',
      'HEAD',
      '.'
    ];

    return git(args)
        .then(function() {
          return true;
        })
        .catch(function(err) {
          if (err.code === 1) {
            return false;
          }
          return Promise.reject(err);
        });
  }

  /**
   * Gets all remote branches in a remote
   * Executes `git ls-remote --heads origin`
   *
   * @example
   * var git = require('gitftw');
   *
   * //remove local tags in 'origin'
   * git.getRemoteBranches({
   *   remote: 'upstream'
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} [options.remote="origin"] The remote ref to ask for branches
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with Array<String>
   */
  function getRemoteBranches(options) {
    options = options || {};
    var args = [
      'ls-remote',
      '--heads',
      options.remote || 'origin'
    ];

    return git(args)
        .then(parseRemoteBranches);

  }

  /**
   * Gets all local branches
   * Executes ` git for-each-ref --format='%(refname:short)' refs/heads/`
   *
   * @example
   * var git = require('gitftw');
   *
   * //remove local tags in 'origin'
   * git.getLocalBranches();
   *
   * @memberof git
   * @type {command}
   *
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with Array<String>
   */
  function getLocalBranches() {
    var args = [
      'for-each-ref',
      '--format=\%(refname:short)',
      'refs/heads/'
    ];

    return git(args)
        .then(parseMultiline);
  }

  /**
   * Creates a git tag
   * Executes `git tag v1.0.0 -m "v1.0.0" -a`
   *
   * @example
   * var git = require('gitftw');
   *
   * git.tag({
   *  tag: 'v1.2.0'
   * })
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} options.tag The tag name
   * @param {Resolvable|String} [options.message] The tag message. Mandatory when creating
   *  an annotated tag
   * @param {Resolvable|Boolean} [options.annotated] Should this tag be annotated?
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function tag(options) {
    assert.ok(options.tag, 'tag name is mandatory');
    if (options.annotated) {
      assert.ok(options.message, 'message is mandatory when creating an annotated tag');
    }

    var args = [
      'tag',
      options.tag,
      options.annotated ? '-a' : null,
      options.message ? '-m' : null,
      options.message ? options.message : null
    ];

    return git(args)
        .then(silent);
  }

  /**
   * Gets the local tags
   * Executes `git tag`
   *
   * @example
   * var git = require('gitftw');
   *
   * git.getTags().then(console.log); //outputs ['v1.0.0', 'v1.0.1']
   *
   * @memberof git
   * @type {command}
   *
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with the array of tags
   */
  function getTags() {
    var args = [
      'tag'
    ];

    return git(args)
        .then(parseMultiline);
  }

  /**
   * Removes a set of tags from the local repo
   * Executes `git tag -d v1.0.0`
   *
   * It does not fail when the tag does not exists
   *
   * @example
   * var git = require('gitftw');
   *
   * //remove all local tags
   * git.removeLocalTags({
   *   tags: git.getTags //It's a resolvable. You can specify also ['v1.0.0', 'v1.0.1']
   * });
   *
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|Array<String>} options.tags The tags to remove
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function removeLocalTags(options) {
    assert.ok(options.tags, 'tags is mandatory');

    return options.tags
        .reduce(function(soFar, tag) {
          var args = [
            'tag',
            '-d',
            tag
          ];
          return soFar
              .then(gitFn(args))
              .catch(passWarning)
              .then(silent);
        }, Promise.resolve());
  }

  /**
   * Removes a tag from the remote repo
   * Executes `git push origin :refs/tags/v1.0.0`
   *
   * @example
   * var git = require('gitftw');
   *
   * //remove local tags in 'origin'
   * git.removeRemoteTags({
   *   tags: git.getTags //It's a resolvable. You can specify also ['v1.0.0', 'v1.0.1']
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} [options.remote="origin"] The remote ref where the tag will be removed
   * @param {Resolvable|Array<String>} options.tags The tags to remove
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function removeRemoteTags(options) {
    assert.ok(options.tags, 'tag is mandatory');

    return options.tags
        .reduce(function(soFar, tag) {
          var args = [
            'push',
            options.remote || 'origin',
            ':refs/tags/' + tag
          ];

          return soFar
              .then(gitFn(args))
              .then(silent);
        }, Promise.resolve());
  }

  /**
   * Removes a set of tags from the remote and local repo
   *
   * @example
   * var git = require('gitftw');
   *
   * //remove all local tags in local and remote 'origin'
   * git.removeTags({
   *   tags: git.getTags //It's a resolvable. You can specify also ['v1.0.0', 'v1.0.1']
   * });
   *
   * @memberof git
   * @type {command}
   *
   * @param {Object} options The options object. All its properties are {@link Resolvable}
   * @param {Resolvable|String} [options.remote="origin"] The remote ref where the tag will be removed
   * @param {Resolvable|Array<String>} options.tags The tags to remove
   * @param {callback} [cb] The execution callback result
   * @returns {Promise} Promise Resolves with undefined
   */
  function removeTags(options) {
    return Promise.resolve()
        .then(git.removeLocalTags.bind(null, options))
        .then(git.removeRemoteTags.bind(null, options));
  }

  //////////////// Utilities

  /**
   * An identity fn over git to make more functional style code
   * inside promises
   *
   * @param {Array}  args
   * @returns {Function}
   */
  function gitFn(args) {
    return function() {
      return git(args);
    };
  }
};

/**
 * Analizes the error given and rejects when it's not a warning
 *
 * @private
 * @param {Error} err The recoverable error
 * @returns {String|Promise} the error message String when its a recoverable error
 *  a rejected promise otherwise
 */
function passWarning(err) {
  if (err.code === 1) {
    //This is a recoverable error. We dont wanna fail
    return err.output;
  }

  return Promise.reject(err);
}

/**
 * Parses the git version
 * @private
 * @param {Resolvable|String} str The 'git version' command output
 * @throws {Error} parsing error
 * @returns {String} the version number
 */
function parseVersion(str) {
  var match = /git version ([0-9\.]+)/.exec(str);

  if (match) {
    return match[1];
  } else {
    throw new Error('Unable to parse version response', str);
  }
}

/**
 * Parses multiline git result
 * @private
 * @param {String} str A multiline command output
 * @returns {Array<String>} the single lines
 */
function parseMultiline(str) {
  if (!str) {
    return [];
  }

  return str.split(/\n/);
}

/**
 * Parses ls-remote --heads
 * @private
 * @param {String} str The 'git ls-remote --heads origin' command output
 * @returns {Array<String>} the remote branches
 */
function parseRemoteBranches(str) {
  var res = [],
      regex = /[\w]{40}\s+refs\/heads\/(.+)/g,
      match;

  while ((match = regex.exec(str)) != null) {
    if (match.index === regex.lastIndex) {
      regex.lastIndex++;
    }
    res.push(match[1]);
  }

  if (!res.length) {
    throw new Error('Unable to parse ls-remote response', str);
  }
  return res;
}

/**
 * A function used to remove the commands output
 * @private
 */
function silent() {
  return '';
}


text:
import Ember from 'ember';
const { get, set } = Ember;

export default Ember.Controller.extend({
  queryParams: ['activeComponent'],
  activeComponent: 'tether-dialog',
  isShowingBasic: false,
  isShowingTranslucent: false,
  isShowingWithoutOverlay: false,
  isShowingWithoutOverlayClickOutsideToClose: false,
  isShowingCustomStyles: false,
  isShowingTargetSelector: false,
  isShowingTargetView: false,
  isShowingTargetElement: false,
  isShowingSubclassed: false,
  isShowingInPlace: false,
  isInPlace: true,
  isShowingCenteredScrolling: false,
  exampleTargetAttachment: 'middle left',
  exampleAttachment: 'middle right',
  customContainerClassNames: 'custom-styles-modal-container',
  nextAttachment: function(val) {
    switch (val) {
      case 'middle right':
        return 'bottom center';
      case 'bottom center':
        return 'middle left';
      case 'middle left':
        return 'top center';
      case 'top center':
        return 'middle right';
    }
    return false;
  },
  actions: {
    toggleActiveComponent() {
      if (get(this, 'activeComponent') === 'modal-dialog') {
        set(this, 'activeComponent', 'tether-dialog');
      } else {
        set(this, 'activeComponent', 'modal-dialog');
      }
    },
    toggleBasic: function() {
      this.toggleProperty('isShowingBasic');
    },
    toggleTranslucent: function() {
      this.toggleProperty('isShowingTranslucent');
    },
    toggleWithoutOverlay: function() {
      this.toggleProperty('isShowingWithoutOverlay');
    },
    toggleWithoutOverlayClickOutsideToClose: function() {
      this.toggleProperty('isShowingWithoutOverlayClickOutsideToClose');
    },
    toggleCustomStyles: function() {
      this.toggleProperty('isShowingCustomStyles');
    },
    toggleTargetSelector: function() {
      if (this.get('isShowingTargetSelector')) {
        var newTargetAttachment = this.nextAttachment(this.get('exampleTargetAttachment'));
        var newAttachment = this.nextAttachment(this.get('exampleAttachment'));
        this.set('exampleTargetAttachment', newTargetAttachment);
        this.set('exampleAttachment', newAttachment);
        if (newTargetAttachment !== 'middle left') {
          return;
        }
      }
      this.toggleProperty('isShowingTargetSelector');
    },
    toggleTargetView: function() {
      if (this.get('isShowingTargetView')) {
        var newTargetAttachment = this.nextAttachment(this.get('exampleTargetAttachment'));
        var newAttachment = this.nextAttachment(this.get('exampleAttachment'));
        this.set('exampleTargetAttachment', newTargetAttachment);
        this.set('exampleAttachment', newAttachment);
        if (newTargetAttachment !== 'middle left') {
          return;
        }
      }
      this.toggleProperty('isShowingTargetView');
    },
    toggleTargetElement: function() {
      if (this.get('isShowingTargetElement')) {
        var newTargetAttachment = this.nextAttachment(this.get('exampleTargetAttachment'));
        var newAttachment = this.nextAttachment(this.get('exampleAttachment'));
        this.set('exampleTargetAttachment', newTargetAttachment);
        this.set('exampleAttachment', newAttachment);
        if (newTargetAttachment !== 'middle left') {
          return;
        }
      }
      this.toggleProperty('isShowingTargetElement');
    },
    toggleSubclassed: function() {
      this.toggleProperty('isShowingSubclassed');
    },
    toggleInPlace: function() {
      this.toggleProperty('isShowingInPlace');
    },
    toggleIsInPlace: function() {
      this.toggleProperty('isInPlace');
    },
    toggleCenteredScrolling: function() {
      this.toggleProperty('isShowingCenteredScrolling');

      if (this.get('isShowingCenteredScrolling')) {
        Ember.$('#modal-overlays').addClass('active');
        Ember.$('body').addClass('centered-modal-showing');
      } else {
        Ember.$('#modal-overlays').removeClass('active');
        Ember.$('body').removeClass('centered-modal-showing');
      }
    },
    closeTargetSelector: function() {
      this.set('isShowingTargetSelector', false);
      this.set('exampleTargetAttachment', 'middle left');
      this.set('exampleAttachment', 'middle right');
    },
    closeTargetView: function() {
      this.set('isShowingTargetView', false);
      this.set('exampleTargetAttachment', 'middle left');
      this.set('exampleAttachment', 'middle right');
    },
    closeTargetElement: function() {
      this.set('isShowingTargetElement', false);
      this.set('exampleTargetAttachment', 'middle left');
      this.set('exampleAttachment', 'middle right');
    }
  }
});


text:
var LoginRecord = require('../../models/loginRecord.js');

exports.verification = function (ip) {
  return function (fn) {
    LoginRecord.find({loginIp: ip},{failed:1},function (err, result) {
      if (err) return fn(err, null);
      if (result.length) {
        var history = result[0].failed;
        for (var i = 0; i < history.length; i++) {
          if(history[0].getTime()+86400000 < new Date().getTime()) {
            history.shift();
          }
        }
        result[0].save(function (err) {if (err) console.log('登录验证数据保存失败'+err);});
        var length = history.length;
        if (length >= 5) return fn(null, {result:false});
        fn(null, {result:true, num: 5 - length});
      } else {
        new LoginRecord({
          loginIp: ip,
          failed: [],
          history: []
        }).save(function (err, result) {
          if (err) return fn(err, null);
          fn(null, {result:true, num: 5});
        });
      }
    });
  };
};

exports.addloginLog = function (obj) {
  if (obj.result) {
    LoginRecord.update({loginIp:obj.ip},{
      failed: [],
      $push: { history: { userName: obj.userName, password: obj.password, userAgent: obj.userAgent, loginTime: new Date(), result: obj.result}}
    },function (err, result) {
      if (err) return console.log('添加登录记录失败:'+err);
    });
  } else {
    LoginRecord.update({loginIp:obj.ip},{
      $push: {
        failed: new Date(),
        history: { userName: obj.userName, password: obj.password, userAgent: obj.userAgent, loginTime: new Date(), result: obj.result}
      }
    },function (err, result) {
      if (err) return console.log('添加登录记录失败:'+err);
    });
  }
};


text:
import os
import Config


text:
"use strict";

Game.propsMap = function(width, height) {
    this.width = width;
    this.height = height;
    this.props = [];
    //Create map container
    var temptiles = [];
    for (var x = 0; x < this.width; x++) {
        // Create the nested array for the y values
        temptiles.push([]);
        // Add all the tiles
        for (var y = 0; y < this.height; y++) {
            temptiles[x].push("") //Game.Tile.nullTile);
        }
    }

    //create trees
    var treeGenerator = new ROT.Map.Cellular(this.width * 2, this.height * 2);
    treeGenerator.randomize(0.3);
    var totalIterations = 1;
    treeGenerator.create(function(x, y, v) {
        if (v === 1 && Game.map.getTile(x, y).walkable) {
            temptiles[x][y] = Game.Prop.tree
        }
    });
    this.props = temptiles
};

// Gets the prop for a given coordinate set
Game.propsMap.prototype.getPropAt = function(x, y) {
    //var nullProp = new Game.Props(x,y)//create rock

    if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
        return null;
        //return nullProp;
    } else {
        var prop = this.props[x][y]
        if (prop == "") {
            return null
                //return nullProp;
        } else {
            return prop
        }
    }
};

// Gets the prop char for a given coordinate set, return "." si pas de prop
Game.propsMap.prototype.getPropCharAt = function(x, y) {
    if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
        return "";
    } else {
        var prop = this.props[x][y]
        if (prop == null || prop == "") {
            return ""
        } else {
            return prop.glyph.char
        }
    }
};
84
//draw all visible props
Game.propsMap.prototype.draw = function(centerX, centerY) {
    //use map visibility boundary
    for (var x = Game.map.topLeftX; x < Game.map.topLeftX + Game.mainScreenWidth; x++) {
        for (var y = Game.map.topLeftY; y < Game.map.topLeftY + Game.mainScreenHeight; y++) {
            var prop = this.getPropAt(x, y)
            if (prop != null) {
                var char = prop.glyph.char
                if (char != " ") {
                    var terrainChar = Game.map.getTile(x, y).glyph.char
                    Game.display.draw(x - Game.map.topLeftX, y - Game.map.topLeftY, [terrainChar, prop.glyph.char], prop.glyph.fg, prop.glyph.bg);
                }
            }
        }
    }
};

//draw all visible props
Game.propsMap.prototype.drawOnMiniMap = function(centerX, centerY) {
    //use map visibility boundary
    for (var x = 0; x < Game.mapScreenWidth; x++) {
        for (var y = 0; y < Game.mapScreenHeight; y++) {
            var prop = this.getPropAt(x, y)
            if (prop != null) {
                Game.drawIfNeeded(Game.mapDisplay, x, y, " ", 'black', prop.glyph.fg)
            }
        }
    }
};

Game.propsMap.prototype.pushToDir = function(x, y, dx, dy) {
    //if destination is walkable
    if (Game.map.getTile(x + dx, y + dy).walkable) {
        var propToMove = this.getPropAt(x, y)
        var propAtDest = this.getPropAt(x + dx, y + dy)
        //move the prop
        if (propAtDest == null && propToMove.pushable) {
            this.props[x][y] = null
            this.props[x + dx][y + dy] = propToMove
            this.patternCheckAround(x + dx, y + dy);
            Game.moves += 1
        } else if (propToMove.destroyable) { //destroy the prop
            this.props[x][y] = propToMove.destroyProduct
            this.patternCheckAround(x, y);
            Game.moves += 1
        }
    }
    else{//sinon, try to destroy the object
        var propToMove = this.getPropAt(x, y)
        if (propToMove.destroyable) { //destroy the prop
            this.props[x][y] = propToMove.destroyProduct
            this.patternCheckAround(x, y);
            Game.moves += 1
        }
    }
};

//create new prop at given coords
Game.propsMap.prototype.create = function(x, y, prop) {
    if (this.getPropAt(x, y) == null) {
        this.props[x][y] = prop
        this.patternCheckAround(x, y);
    }
};

//remove prop at given coords
Game.propsMap.prototype.removePropAt = function(x, y) {
    if (this.getPropAt(x, y) != null) {
        this.props[x][y] = null
    }
};

//check if the last moved prop is part of a pattern
Game.propsMap.prototype.patternCheckAround = function(lastMovedPropX, lastMovedPropY) {
    for (var key in Game.recipes) {
        // console.log(key);
        // console.log(Game.recipes[key]);
        var patternToFind = Game.recipes[key].pattern
        var patternIndX = 0;
        var patternIndY = 0;
        var founded = false;
        var findedPropsCoord = [];
        var startY = 0
        var patternWidth = patternToFind[0].length //number of column in pattern
        var patternHeight = patternToFind.length //number of row in pattern
            //pour chaque ligne
        for (var y = lastMovedPropY - 2; y < lastMovedPropY + 2; y++) {
            //pour chaque case de la ligne
            for (var x = lastMovedPropX - 2; x < lastMovedPropX + 2; x++) {
                var findedChar = this.getPropCharAt(x, y)
                if (findedChar == patternToFind[0][0] || patternToFind[0][0] == "") //first cell of the pattern has been found
                {
                    //list of finded props initialization
                    findedPropsCoord = [];
                    if (findedChar != ".") {
                        findedPropsCoord.push({ x, y })
                    }

                    //on parcours le pattern
                    patternIndX = 1;
                    patternIndY = 0;
                    var findedChar = this.getPropCharAt(x + patternIndX, y + patternIndY)
                    while (findedChar == patternToFind[patternIndY][patternIndX] || patternToFind[patternIndY][patternIndX] == "") {
                        //on compléte la liste des prop trouvées
                        if (patternToFind[patternIndY][patternIndX] != "") {
                            findedPropsCoord.push({ x: x + patternIndX, y: y + patternIndY })
                        }
                        patternIndX += 1 //case suivante
                        if (patternIndX == patternWidth) { //ligne suivante
                            patternIndY += 1
                            patternIndX = 0
                            if (patternIndY == patternHeight) { //colone suivante
                                //pattern is fully finded starting at x:y
                                console.log("pattern " + key + " finded at " + x + " " + y)
                                    //destroy finded props
                                for (var i = 0; i < findedPropsCoord.length; i++) {
                                    this.removePropAt(findedPropsCoord[i].x, findedPropsCoord[i].y)
                                }
                                //replace lastmovedprops by result
                                if (Game.recipes[key].kind == "ressource") {
                                    //create resulting resource at last moved porp position
                                    console.log("create " + Game.recipes[key].result.glyph.char)
                                    this.create(lastMovedPropX, lastMovedPropY, Game.recipes[key].result)
                                } else if (Game.recipes[key].kind == "building") {
                                    //create resulting object table at pattren position
                                    console.log("create " + Game.recipes[key].result)
                                    for (var resultY = 0; resultY < Game.recipes[key].result.length; resultY++) {
                                        for (var resultX = 0; resultX < Game.recipes[key].result[0].length; resultX++) {
                                            if (Game.recipes[key].result[resultY][resultX] != ".") {
                                                this.create(x + resultX, y + resultY, Game.recipes[key].result[resultY][resultX])
                                            }
                                        }
                                    }
                                }
                                break
                            }
                        }
                        findedChar = this.getPropCharAt(x + patternIndX, y + patternIndY)
                    }
                }
            }
        }
    }
};