tazpanel diff hosts.cgi @ rev 527

Add hosts.cgi; update translations
author Aleksej Bobylev <al.bobylev@gmail.com>
date Wed Aug 26 05:51:08 2015 +0300 (2015-08-26)
parents
children 528e24253970
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/hosts.cgi	Wed Aug 26 05:51:08 2015 +0300
     1.3 @@ -0,0 +1,301 @@
     1.4 +#!/bin/sh
     1.5 +#
     1.6 +# Manage /etc/hosts CGI interface - to use hosts as Ad blocker
     1.7 +#
     1.8 +# Copyright (C) 2015 SliTaz GNU/Linux - BSD License
     1.9 +#
    1.10 +
    1.11 +
    1.12 +# Common functions from libtazpanel
    1.13 +
    1.14 +. lib/libtazpanel
    1.15 +get_config
    1.16 +header
    1.17 +
    1.18 +TITLE=$(_ 'Network')
    1.19 +xhtml_header "$(_ 'Use hosts file as Ad blocker')"
    1.20 +
    1.21 +found=$(mktemp)
    1.22 +
    1.23 +# Find the hosts list
    1.24 +hosts=$(echo "$QUERY_STRING&" | awk '
    1.25 +	BEGIN { RS="&"; FS="=" }
    1.26 +	$1=="host" { printf "%s ", $2 }
    1.27 +')
    1.28 +hosts=$(httpd -d "${hosts% }")
    1.29 +# now hosts='host1 host2 ... hostn'
    1.30 +
    1.31 +# Folder to save downloaded and installed hosts lists
    1.32 +HOSTSDIR='/var/cache/tazpanel/hosts'
    1.33 +mkdir -p "$HOSTSDIR"
    1.34 +
    1.35 +
    1.36 +
    1.37 +
    1.38 +# List the lists data: name, about URL, download URL, update frequency, code letter
    1.39 +listlist() {
    1.40 +	# Another free to use list, but not free to modify with its EULA (and very big: 12MB!)
    1.41 +	# hpHosts List	http://hosts-file.net/
    1.42 +	echo "
    1.43 +MVPs.org List	http://winhelp2002.mvps.org/hosts.htm	http://winhelp2002.mvps.org/hosts.zip	monthly	M
    1.44 +Dan Pollock List	http://someonewhocares.org/hosts/	http://someonewhocares.org/hosts/zero/hosts	regularly	P
    1.45 +Malware Domain List	http://www.malwaredomainlist.com/	http://www.malwaredomainlist.com/hostslist/hosts.txt	regularly	D
    1.46 +Peter Lowe List	http://pgl.yoyo.org/adservers/	http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&mimetype=plaintext	regularly	L
    1.47 +"
    1.48 +}
    1.49 +
    1.50 +# Get the list specifications: name and download URL
    1.51 +getlistspec() {
    1.52 +	# Input: code letter
    1.53 +	local line="$(listlist | grep "$1\$")"
    1.54 +	name="$(echo "$line" | cut -d$'\t' -f1)"
    1.55 +	url="$( echo "$line" | cut -d$'\t' -f3)"
    1.56 +}
    1.57 +
    1.58 +# Install the list
    1.59 +instlist() {
    1.60 +	# Input: list=code letter, url=download url
    1.61 +	file="$HOSTSDIR/$list"
    1.62 +	[ -f "$file" ] && rm "$file"
    1.63 +	busybox wget -O "$file" "$url"
    1.64 +	case $url in
    1.65 +		*zip)
    1.66 +			mv "$file" "$file.zip"
    1.67 +			busybox unzip "$file.zip" HOSTS >/dev/null
    1.68 +			mv HOSTS "$file"
    1.69 +			rm "$file.zip"
    1.70 +			;;
    1.71 +	esac
    1.72 +	# Add entries to hosts file
    1.73 +	awk -vl="$list" '$1=="0.0.0.0"||$1=="127.0.0.1"{printf "0.0.0.0 %s #%s\n", $2, l}' "$file" | fgrep -v localhost >> /etc/hosts
    1.74 +	# Clean the list
    1.75 +	echo -n > "$file"
    1.76 +	touch "$file.up"
    1.77 +
    1.78 +	# Remove the duplicate entries
    1.79 +	hostsnew=$(mktemp)
    1.80 +	grep "^0.0.0.0" /etc/hosts | sort -k2,2 -u -o "$hostsnew"
    1.81 +	sed -i '/^0.0.0.0/d' /etc/hosts
    1.82 +	cat "$hostsnew" >> /etc/hosts
    1.83 +	rm "$hostsnew"
    1.84 +
    1.85 +	# Prevent user-disabled entries to re-appeare
    1.86 +	grep "^#0.0.0.0" /etc/hosts | while read null host; do
    1.87 +		sed -i "/^0.0.0.0 $host #/d" /etc/hosts
    1.88 +	done
    1.89 +}
    1.90 +
    1.91 +# Remove the list
    1.92 +remlist() {
    1.93 +	# Input: list=code letter
    1.94 +	sed -i "/#$list$/d" /etc/hosts
    1.95 +	file="$HOSTSDIR/$list"
    1.96 +	rm "$file" "$file.up" "$file.avail"
    1.97 +}
    1.98 +
    1.99 +
   1.100 +
   1.101 +case " $(GET) " in
   1.102 +
   1.103 +	*\ add\ *)
   1.104 +		# Add given host
   1.105 +
   1.106 +		host="$(GET add)"
   1.107 +
   1.108 +		echo "0.0.0.0 $host #U" >> /etc/hosts
   1.109 +		echo -n '<p><span data-img="info"></span>'
   1.110 +		_ 'Host "%s" added to /etc/hosts.' "$host"
   1.111 +		echo '</p>'
   1.112 +		;;
   1.113 +
   1.114 +	*\ disable\ *)
   1.115 +		# Disable given hosts
   1.116 +
   1.117 +		for host in $hosts; do
   1.118 +			sed -i "s|^0.0.0.0[ \t][ \t]*$host\$|#\0|" /etc/hosts
   1.119 +			sed -i "s|^0.0.0.0[ \t][ \t]*$host .*|#\0|" /etc/hosts
   1.120 +		done
   1.121 +		r=$(echo "$hosts" | tr ' ' '\n' | wc -l)
   1.122 +		echo -n '<p><span data-img="info"></span>'
   1.123 +		_p  '%d record disabled' \
   1.124 +			'%d records disabled' "$r"   "$r"
   1.125 +		echo '</p>'
   1.126 +		;;
   1.127 +
   1.128 +	*\ instlist\ *)
   1.129 +		# Install list
   1.130 +
   1.131 +		list="$(GET instlist)"
   1.132 +		getlistspec "$list"
   1.133 +		echo "<p>$(_ 'Installing the "%s"...' "$name") "
   1.134 +		instlist
   1.135 +		echo "$(_ 'Done')</p>"
   1.136 +		;;
   1.137 +
   1.138 +	*\ uplist\ *)
   1.139 +		# Update list
   1.140 +
   1.141 +		list="$(GET uplist)"
   1.142 +		getlistspec "$list"
   1.143 +		echo "<p>$(_ 'Updating the "%s"...' "$name") "
   1.144 +		remlist; instlist
   1.145 +		echo "$(_ 'Done')</p>"
   1.146 +		;;
   1.147 +
   1.148 +	*\ remlist\ *)
   1.149 +		# Remove list
   1.150 +
   1.151 +		list="$(GET remlist)"
   1.152 +		getlistspec "$list"
   1.153 +		echo "<p>$(_ 'Removing the "%s"...' "$name") "
   1.154 +		remlist
   1.155 +		echo "$(_ 'Done')</p>"
   1.156 +		;;
   1.157 +
   1.158 +esac
   1.159 +
   1.160 +# When search term given
   1.161 +term=$(GET term)
   1.162 +if [ -z "$term" ]; then
   1.163 +	getdb hosts | fgrep 0.0.0.0 > "$found"
   1.164 +	r=$(wc -l < "$found")
   1.165 +	echo -n '<p><span data-img="info"></span>'
   1.166 +	_p  '%d record used for Ad blocking' \
   1.167 +		'%d records used for Ad blocking' "$r"   "$r"
   1.168 +else
   1.169 +	getdb hosts | fgrep 0.0.0.0 | fgrep "$term" > "$found"
   1.170 +	r=$(wc -l < "$found")
   1.171 +	echo -n '<p><span data-img="info"></span>'
   1.172 +	_p  '%d record found for "%s"' \
   1.173 +		'%d records found for "%s"' "$r"   "$r" "$term"
   1.174 +fi
   1.175 +
   1.176 +[ "$r" -gt 100 ] && _ ' (The list is limited to the first 100 entries.)'
   1.177 +echo '</p>'
   1.178 +
   1.179 +cat <<EOT
   1.180 +<section>
   1.181 +	<header>
   1.182 +		<span data-icon="list">$(_ 'Hosts')</span>
   1.183 +		<form>
   1.184 +			<input type="search" name="term" value="$(GET term)" results="5" autosave="hosts" autocomplete="on"/>
   1.185 +		</form>
   1.186 +	</header>
   1.187 +	<form class="wide">
   1.188 +		<pre class="scroll">
   1.189 +EOT
   1.190 +head -n100 "$found" | awk '{
   1.191 +	printf "<label><input type=\"checkbox\" name=\"host\" value=\"%s\"/> %s</label>\n", $2, $2;
   1.192 +}'
   1.193 +rm "$found"
   1.194 +cat <<EOT
   1.195 +</pre>
   1.196 +		<footer>
   1.197 +			<button type="submit" name="disable" data-icon="delete" data-root>$(_ 'Disable selected')</button>
   1.198 +		</footer>
   1.199 +	</form>
   1.200 +</section>
   1.201 +
   1.202 +<section>
   1.203 +	<header><span data-icon="add">$(_ 'Add')</span></header>
   1.204 +	<form class="wide">
   1.205 +		<div>
   1.206 +			$(_ 'Host:')
   1.207 +			<input type="text" name="add"/>
   1.208 +		</div>
   1.209 +		<footer>
   1.210 +			<button type="submit" data-icon="add" data-root>$(_ 'Add')</button>
   1.211 +		</footer>
   1.212 +	</form>
   1.213 +</section>
   1.214 +
   1.215 +<section>
   1.216 +	<header><span data-icon="admin">$(_ 'Manage lists')</span></header>
   1.217 +	<div>$(_ 'You can use one or more prepared hosts files to block advertisements, malware and other irritants.')</div>
   1.218 +	<form class="wide">
   1.219 +	<table class="wide zebra">
   1.220 +		<thead>
   1.221 +			<tr>
   1.222 +				<td>$(_ 'Name')</td>
   1.223 +				<td>$(_ 'Details')</td>
   1.224 +				<td>$(_ 'Updates')</td>
   1.225 +				<td>$(_ 'Actions')</td>
   1.226 +			</tr>
   1.227 +		</thead>
   1.228 +		<tbody>
   1.229 +EOT
   1.230 +
   1.231 +IFS=$'\t'
   1.232 +listlist | while read name info url updated letter; do
   1.233 +	[ -z "$name" ] && continue
   1.234 +
   1.235 +	cat <<EOT
   1.236 +<tr>
   1.237 +	<td>$name</td>
   1.238 +	<td><a data-icon="info" target="_blank" href="$info">$(_ 'info')</a></td>
   1.239 +	<td>
   1.240 +		$([ "$updated" == 'monthly'   ] && _ 'Updated monthly')
   1.241 +		$([ "$updated" == 'regularly' ] && _ 'Updated regularly')
   1.242 +	</td>
   1.243 +	<td>
   1.244 +EOT
   1.245 +
   1.246 +	if [ -e "$HOSTSDIR/$letter" ]; then
   1.247 +		# List installed
   1.248 +
   1.249 +		# Check for upgrades (once a day)
   1.250 +		if [ -f "$HOSTSDIR/$letter.up" ]; then
   1.251 +			# Update checked previously
   1.252 +			if [ "$(($(date -u +%s) - 86400))" -gt "$(date -ur "$HOSTSDIR/$letter.up" +%s)" ]; then
   1.253 +				# Update checked more than one day (86400 seconds) ago
   1.254 +				check='yes'
   1.255 +			else
   1.256 +				# Update checked within one day
   1.257 +				check='no'
   1.258 +			fi
   1.259 +		else
   1.260 +			# Update not checked yet
   1.261 +			check='yes'
   1.262 +		fi
   1.263 +
   1.264 +		if [ "$check" == 'yes' ]; then
   1.265 +			# Check for update (not really download)
   1.266 +			busybox wget -s --header "If-Modified-Since: $(date -Rur "$HOSTSDIR/$letter")" "$url"
   1.267 +			if [ "$?" -eq 0 ]; then
   1.268 +				# Upgrade available
   1.269 +				touch "$HOSTSDIR/$letter.avail"
   1.270 +			else
   1.271 +				# Upgrade not available
   1.272 +				rm "$HOSTSDIR/$letter.avail" 2>/dev/null
   1.273 +			fi
   1.274 +			touch "$HOSTSDIR/$letter.up"
   1.275 +		fi
   1.276 +
   1.277 +		if [ -f "$HOSTSDIR/$letter.avail" ]; then
   1.278 +			cat <<EOT
   1.279 +<button name="uplist" value="$letter" data-icon="upgrade">$(_ 'Upgrade')</button>
   1.280 +EOT
   1.281 +		fi
   1.282 +
   1.283 +		cat <<EOT
   1.284 +<button name="remlist" value="$letter" data-icon="remove">$(_ 'Remove')</button>
   1.285 +EOT
   1.286 +
   1.287 +	else
   1.288 +		# List not installed
   1.289 +		cat <<EOT
   1.290 +<button name="instlist" value="$letter" data-icon="install">$(_ 'Install')</button>
   1.291 +EOT
   1.292 +	fi
   1.293 +	echo '</td></tr>'
   1.294 +done
   1.295 +
   1.296 +	cat <<EOT
   1.297 +			</tbody>
   1.298 +		</table>
   1.299 +	</form>
   1.300 +</section>
   1.301 +EOT
   1.302 +
   1.303 +xhtml_footer
   1.304 +exit 0