Sunday, December 24, 2017

IMAGE THUMBNAILER

#!/bin/bash
#set -x 
declare -i THUMBCLOBBER=${2:-0}
declare -i REBUILDHTML=${1:-0}
declare -r THUMBNAILSIZE="100x100"
declare -r THUMBDIR=".thumb" 
declare -r IMGEXT="{jpg,jpeg,img,gif,png}"
declare -r THUMBSHTML=".thumbs.html" 
declare -r THUMBPREFIX="_thumb_"
declare -r TOGGLENULLGLOB="$(shopt -p nullglob)"
declare -r TOGGLEFAILGLOB="$(shopt -p failglob)"
declare -i CLOSEHTMLFLAG=0 

  ## any additional argument means rebuild all thumbnails 
[[ ! -z $1 ]] && THUMBCLOBBER=1
[[ ! -z $2 ]] && THUMBCLOBBER=0 && REBUILDHTML=1 

function get_html_clock() { 
cat << EOF

<script>
var clsStopwatch = function() {
  // Private vars
  var startAt = 0; // Time of last start / resume. (0 if not running)
  var lapTime = 0; // Time on the clock when last stopped in milliseconds

  var now = function() {
    return (new Date()).getTime(); 
   }; 
 
  // Public methods
  // Start or resume
  this.start = function() {
    startAt = startAt ? startAt : now();
   };

  // Stop or pause
  this.stop = function() {
    // If running, update elapsed time otherwise keep it
    lapTime = startAt ? lapTime + now() - startAt : lapTime;
    startAt = 0; // Paused
   };

  // Reset
  this.reset = function() {
    lapTime = startAt = 0;
   };

  // Duration
  this.time = function() {
    return lapTime + (startAt ? now() - startAt : 0); 
   };
};

var x = new clsStopwatch();
var \$time;
var clocktimer;

function pad(num, size) {
 var s = "0000" + num;
 return s.substr(s.length - size);
}

function formatTime(time) {
 var h = m = s = ms = 0;
 var newTime = '';

 h = Math.floor( time / (60 * 60 * 1000) );
 time = time % (60 * 60 * 1000);
 m = Math.floor( time / (60 * 1000) );
 time = time % (60 * 1000);
 s = Math.floor( time / 1000 );
 ms = time % 1000;

 newTime = pad(h, 2) + ':' + pad(m, 2) + ':' + pad(s, 2);
 return newTime;
}

function startTime() {
 \$time = document.getElementById('clock');
 update();
    start(); 
}

function update() {
 \$time.innerHTML = formatTime(x.time());
}

function start() {
 clocktimer = setInterval("update()", 1);
 x.start();
}

function stop() {
 x.stop();
 clearInterval(clocktimer);
}

function reset() {
 stop();
 x.reset();
 update();
}
</script>

<style type="text/css">
.clock {
    position: fixed;
    background: rgba(255,255,255,.5);
    bottom: 0px;
    padding: 10px;
    left: 40%;
    width: 180px;
}
</style>
EOF
}

[[ ! -d "$THUMBDIR" ]] && mkdir "$THUMBDIR"
if [[ ! -f "$THUMBSHTML" ]] || (($THUMBCLOBBER)) || (($REBUILDHTML)); then
 echo "Begin - searching for images, generating thumbnails and index file ... "  
 echo "<html><head><title>generated thumbs</title>$(get_html_clock)</head><body onload=\"startTime()\"><div class=\"clock\" id=\"clock\"></div>" > "$THUMBSHTML"
else 
   ## rid the last line since we will be appending.. 
 echo "Appending to existing thumbnail file $THUMBSHTML ... "  
 sed -i '$d' "$THUMBSHTML"
fi 

echo "Making thumbnails for all $IMGEXT files ... " 
## use ls not echo in the eval since the echo is eval'd and the glob needs to match we can't add a newline 
## ls will parse the glob and spit out newlines that we can use 
#set -x 
shopt -s nullglob  
shopt -s failglob

 ## Url encodes the troublesome ascii range of 20-47. 
function urlencode() { 
 ## Note, keep the % replacement all the way on the lefthand side so it doesn't 
 ## get applied to the subsequent subtitutions. \x25 the period '.' was removed 
 ## since it causes trouble to sed and isn't necessary. 
 sed 's/\x25/%25/g;s/\x20/%20/g;s/\x21/%21/g;s/\x22/%22/g;s/\x23/%23/g;s/\x5c\x24/%24/g;s/\x26/%26/g;s/\x27/%27/g;s/\x28/%28/g;s/\x29/%29/g;s/\x2A/%2A/g;s/\x2B/%2B/g;s/\x2C/%2C/g;s/\x2D/%2D/g;s/\x3A/%3A/g;s/\x3F/%3F/g;s/\x7C/%7C/g;s/\x5c\x5B/%5B/g'  <<< "${1}"
} 

while IFS= read -r -d '' each_img; do 
 THUMBNAME="${THUMBDIR}/${THUMBPREFIX}$(basename "${each_img}")" ## get the base name, we're following links
if [[ ! -f "$THUMBNAME" ]] || (($THUMBCLOBBER)) || (($REBUILDHTML)); then 
 ((! $REBUILDHTML)) && echo "Creating $THUMBNAILSIZE thumbnail for $each_img in ${PWD}/${THUMBDIR} ... "  
  ((! $REBUILDHTML)) && /usr/bin/convert -- "${each_img}" -resize "$THUMBNAILSIZE" "$THUMBNAME"  
 if (($REBUILDHTML)); then 
   if [[ -f "$THUMBNAME" ]]; then 
      echo "Rebuilding Html skipping create thumb for $THUMBNAME exists already.. " 
   else 
      echo "*** Rebuilding html file yet $THUMBNAME does not yet exist !! .. " 
      continue; ## jump to top of for loop without writing this html entry
   fi  
 fi    
   ## Encode any %'s into a percent in html, %25. URL Encode single quotes \x27->%27, #'s \x23->%23.                 
 echo "<a href='${each_img}'><img src='http://localhost:8000$(pwd)/$(urlencode "${THUMBNAME}")' alt='${each_img}'/></a>" >> "$THUMBSHTML"
 CLOSEHTMLFLAG=1 
else 
 echo "Skipping ${PWD}/${THUMBDIR} file already exists ... "
fi 
done < <(find . -maxdepth 1 \( -type f -o -type l \) ! -path .thumb \( -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' -o -name '*.JPG' -o -name '*.gif' \) -exec readlink -fz {} \;);          

if (($CLOSEHTMLFLAG)); then 
 echo -en "</body></html>" >> "$THUMBSHTML"
else 
 IMAGES=$(grep -c "img src" "$THUMBSHTML") 
 if ((! ! $IMAGES)); then 
   echo "</body></html>" >> "$THUMBSHTML"
 else 
   rm "$THUMBSHTML"; # no images so html file is empty.. better to be tidy and nip it here than
 fi                  # offload to other programs that use this file..  
 echo "No images to process .. " 
fi  

eval "$TOGGLENULLGLOB"
eval "$TOGGLEFAILGLOB"
echo "Done ..."
exit 0 
alias mvthm='_(){ thumbs.bsh "$1" "$2";if [[ -f ".thumbs.html" ]];then firefox ".thumbs.html"; fi; }; _'

Monday, December 18, 2017

Simple Self-Contained RECAPTCHA Example

<!doctype html>
<html>
    <head>
        <title>Google reCAPTCHA demo</title>
        <script src='https://www.google.com/recaptcha/api.js'></script>
    </head>
    <body>
        <form method="post" action="recaptcha_test.php">
            <div class="g-recaptcha" data-sitekey="CHANGE THIS TO YOUR SITE KEY"></div>
            <input type="submit" />
        </form>
    </body>
</html>

<?php

    if($_SERVER["REQUEST_METHOD"] === "POST")
    {
        // Copy this (the html above too) to recaptcha_test.php (/tmp/recaptcha_test.php)   
        // cd to directory, start up php -S localhost:9000, 
        // then browse to http://localhost:9000/recaptcha_test.php
        
        //form submitted    

        //check if other form details are correct

        //verify captcha
        $recaptcha_secret = "CHANGE THIS TO YOUR SECRET KEY";
        $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$recaptcha_secret."&response=".$_POST['g-recaptcha-response']);
        $response = json_decode($response, true);
        if($response["success"] === true)
        {
            echo "Logged In Successfully";
        }
        else
        {
            echo "You are a robot";
        }
    }
?>

Monday, November 27, 2017

Increase Productivity With Dynamic Enumerized Location Aliases

## Save directory location, Goto directory location 
## surprisingly effective at increasing productivity 
## save it in one shell and all shells immediately have 
## access to it. s1 in a shell; g1 gets there in any 
## other shell. 
alias s1='pwd > "/tmp/__${USER}_savedir1__"' 
alias s2='pwd > "/tmp/__${USER}_savedir2__"' 
alias s3='pwd > "/tmp/__${USER}_savedir3__"' 
alias g1='cd "$(< "/tmp/__${USER}_savedir1__")"' 
alias g2='cd "$(< "/tmp/__${USER}_savedir2__")"' 
alias g3='cd "$(< "/tmp/__${USER}_savedir3__")"' 

Sunday, September 24, 2017

Zeitgeist Official Spyware -- Should be Removed

Zeitgeist has been quasi-inexplicably bundled by Ubuntu for quite some time, what it does is track files on your computer. This is not necessary or useful to the user and is a burden to the processor. All rationales for zeitgeist smack of cover stories alá CIA duh. All the programs and software related to ZEITGEIST are highly questionable including yet not limited to: rhythmbox, GNOME (yes all of it), evince, nautilus, firefox, compiz, geany :(, gedit, gtk-recordmydesktop.

If you want to see what data is being surreptitiously collected on your Ubuntu/[other linux flavors w/gnome?] Linux box, download the sqlitebrowser:

$> sudo apt-get install sqlitebrowser

Fire it up and open up the activity.sqlite database in ~/.local/share/zeitgeist.

Then execute the following query:
SELECT t.value, datetime(e.timestamp/1000, 'unixepoch') as timestamp, u.value as subj_uri FROM uri u, text t, event e WHERE t.id = e.subj_text and u.id=e.subj_id ORDER BY e.timestamp DESC

You will likely see a list of files that you accessed that zeitgeist found interesting.
...
"Security & Privacy" "2017-09-08 18:32:43" "application://unity-activity-log-manager-panel.desktop"
"Brightness & Lock" "2017-09-08 18:32:31" "application://unity-screen-panel.desktop"
"Power" "2017-09-08 18:27:55" "application://unity-power-panel.desktop"
"Power" "2017-09-08 18:27:24" "application://unity-power-panel.desktop"
"Brightness & Lock" "2017-09-08 18:26:37" "application://unity-screen-panel.desktop"
"System Settings" "2017-09-08 18:26:26" "application://unity-control-center.desktop"
"LibreOffice Calc" "2017-09-08 18:26:10" "application://libreoffice-calc.desktop"
"Displays" "2017-09-08 18:26:01" "application://unity-display-panel.desktop"
"System Settings" "2017-09-08 18:25:18" "application://unity-control-center.desktop"

ACTOR TABLE -- ~/.local/share/zeitgeist/activity.sqlite db
"1" "application://zeitgeist-datahub.desktop"
"2" "application://compiz.desktop"
"3" "application://geany.desktop"
"4" "application://evince.desktop"
"5" "application://gtk-recordmydesktop.desktop"
"6" "application://firefox.desktop"
"7" "application://gedit.desktop"
"8" "application://nautilus.desktop"
"9" "application://eog.desktop"

Thursday, September 21, 2017

Bash Shell Simple Stop/Start Signal Handling

#!/bin/bash 
declare -i i=0
declare SLEEP_FLAG=1
builtin trap '{ SLEEP_FLAG=1;while :;do ((${SLEEP_FLAG}))&&sleep 1||break;done;}' TSTP
builtin trap '{ SLEEP_FLAG=0;}' CONT ## Why don't all shell scripts do this?? 
builtin trap '{ echo -en "\nUseful addition to a shell script that may run for a while.\n";exit 0;}' INT 
clear 
while :; do 
 i+=1
 if ((${i}%7==0)); then 
  echo -en "** Warning: file system is getting full ... ${i}" 
  sleep .5
  clear
 else
  echo -en "looping around ... ${i}" 
  sleep .25
  echo -en "\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r\r" 
 fi 
((${i}==75)) && echo "Sending SIGTSTP to myself! To restart me \$>kill -SIGCONT $$" && kill -SIGTSTP $$
done
exit 0 

Sunday, July 16, 2017

Bash Options Handler

#!/usr/bin/env bash

#############################################################################
###########################################################################
### Created by A.M.Danischewski (c) 2017+ v1.00
### Issues: If you find any issues emai1 me at my <first name> dot 
###         <my last name> at gmail dot com.  
###
### This is a simple effective options handler, that easily and 
### automatically handles long and short options. Hack away at it! =) 
### 
### E.g. options_handler.bsh -e e_value -l --super s_value of default 
###      options_handler.bsh --extended e_value dispersed -l -s s_value default value 
### This program is free software: you can redistribute it and/or modify. 
###########################################################################
#############################################################################

declare -a OPTIONS
declare -A OPTIONVALUES
declare -i DEFAULT_OPTION_SET=0 
declare -r DEFAULT_INDEX="default"

 ### This is a commandline options handler, it loads two arrays the first 
 ### array OPTIONS holds the OPTION FLAGS that were passed. The second 
 ### array is an associative array that holds option arguments if any 
 ### that were passed in. 
 ### Note: This handler does not accept spaces in long option names. 
function process_options() { 
while (($#>0)); do
case "$1" in
    -e|--extended)
    OPTIONS+=("-e")
     ## No argument option. 
    #[[ ! "$2" =~ ^- ]] && OPTIONVALUES["-e"]="$2" && shift 
    ;;
    -s|--super)
    OPTIONS+=("-s")
    [[ ! "$2" =~ ^- ]] && OPTIONVALUES["-s"]="$2" && shift 
    ;;
    -l|--longoption)
    OPTIONS+=("-l")
    [[ ! "$2" =~ ^- ]] && OPTIONVALUES["-l"]="$2" && shift 
    ;;
    --default)
    ;&
    *)
    if [[ "$1" =~ ^- ]]; then ## Handle unexpected options. 
       echo "*** Error option $1 not found" >&2 
       ## Add your logic. 
       shift 
    fi 
    ## Handle the default argument. 
    ((! ${DEFAULT_OPTION_SET})) && OPTIONS+=("${DEFAULT_INDEX}") && DEFAULT_OPTION_SET=1
    while [[ ! "$1" =~ ^- ]] && [[ $# -gt 0 ]]; do ## Look ma, no quotes. o_0
     OPTIONVALUES["${DEFAULT_INDEX}"]+=" ${1}" && shift
    done 
    [[ "$1" =~ ^- ]] && continue ## We have a flag again, skip shifting. 
    ;;
esac
shift ## Shift to next option. 
done
} 

function print_options() { 
for a in ${OPTIONS[@]}; do 
 echo "Got: $a with arg: ${OPTIONVALUES[${a}]}"
done   
}

process_options "$@" 
print_options
exit 0 

Sunday, June 25, 2017

Dual Booting - Ubuntu w/Windows 10 Easy

How to dual boot an existing Windows 10 with an external hard drive.

Tested on the ASUS E200HA / Windows 10 / Ubuntu 17.04. 

First download and create a bootable USB drive with Ubuntu 17.04 (tested)
or whatever your favorite Linux distro that has UEFI support is;
this tutorial will reference Debian commands, map them as necessary.

Step 1 - Enter the BIOS - Keep tapping F2 when the computer first starts -- otherwise, if it still boots Windows 10 go to Settings->Update & security->
Recovery->Advanced startup->Restart Now ->Troubleshoot->Advanced Options->UEFI Firmware Settings->Restart

Step 2 - In the BIOS, disable Fast Boot and disable Windows from the
Boot Priority List (anything else or bust)

Step 3 - Insert the USB and when the computer starts enter the BIOS
(hit F2 when starting on the Asus E200HA) Select from the
Override Boot Manager options the USB stick and boot your Linux distro,
get wifi set up and run $> sudo apt-get update

Step 4 - Attach the external hard drive, format as necessary with an
ext 4 partition for the Linux installation, or you can wait and allow
the installation software do it for you.

Step 5 - Install Linux to the external hard drive, on the screen where
it asks where you want to put the software on the Ubuntu installation
install workflow -- select SOMETHING ELSE (so you can get to the disk
formatting screens) IMPORTANT -- where it says "Device for bootloader
installation", look at the list of drives above and select from the drop
down list the same partition that has the label in the list above of
Windows Boot Manager efi. That's the partition we need to install our
bootloader to. Make sure you have at least one formatted ext4 partition
and have the mount point /. Finish installing Linux to your external
hard drive.

Step 6 - After the installation completes, type $> mount to see where
your external hard drive is mounted if it's not /target, then unmount it
$> sudo umount /dev/sda1 (if that's what it is). If it's not already mounted
on /target create a new directory on the root /:
Note: If you created other partitions make sure to unmount them and move
all the mounts to under /target (so the chroot kernel can see them).
$> sudo mkdir -p /target
Then mount sda on /target:
$> sudo mount /dev/sda1 /target
Then bind mount the critical directories:
$> for a in /sys /proc /run /dev; do sudo mount --bind "$a" "/target${a}"; done
Then mount the EFI directory, use gparted to locate the
Windows Boot Manager efi partition :
$> sudo mount /dev/sdXY /target/boot/efi
Chroot to the new install:
$> sudo chroot /target
On chroot:
$> sudo apt-get update
$> sudo apt-get install grub2
$> sudo update-grub2
$> exit
Reboot:
$> echo Cross fingers =\) && reboot

Step 7 -- Quickly yank the USB thumbdrive out before the computer gets
started (to make sure the USB hardware addresses don't screw anything
up). Your computer should hopefully automatically bring you back into
Ubuntu this time on the external hard drive no longer the USB thumbdrive.
Re-run an update of grub2:
$> sudo apt-get update
$> sudo update-grub2
DONE.

Step 8 - You should now be able to reboot with the external hardrive attached and it will go to the Ubuntu Grub2 bootloader, thereafter that you can select Windows Boot Manager and get into Windows 10 if you need to. If you don't have your external hard drive then you can still get to Windows 10 by going into the BIOS (hit F2) and select from the Boot Override options the Windows Boot Manager. Enjoy.

Wednesday, February 8, 2017

Grab Rickroll Images - Quick w/out the Clicks

#!/usr/bin/env bash 
#############################################################################
###########################################################################
### Created by A.M.Danischewski (c) 2017+ v1.00
### Issues: If you find any issues emai1 me at my <first name> dot 
###         <my last name> at gmail dot com.  
###
### This is a simple program intended to grab the oft rickroll'd images from 
### those sensationalist click-through image sites. 
### 
### This program requires (to work to full capacity) by default: 
### curl, wget, sed, grep, uniq, sort
### 
### This program is free software: you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation, either version 3 of the License, or
### (at your option) any later version.
###
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
### GNU General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program.  If not, see <http://www.gnu.org/licenses/>.
###########################################################################
#############################################################################
url="${1:-http://allrookie.com/perfectly-timed-sports-moments-to-pump-you-up-for-the-upcoming-olympics/}"
html_prefix="${2:-rickroll}_"
master_file="${html_prefix}_master.txt"
start_index=${3:-1}
end_index=${4:-30}
cleanup_flag=${5:-1}

 ## Get all pages 
for((a=${start_index};a<=${end_index};a++)); do 
 echo "Fetching: ${url%/}/${a}/  -->  ${html_prefix}${a}.html ..."
 curl -A mozilla -s "${url%/}/${a}/" > ${html_prefix}${a}.html
done

 ## Get all image links 
while read b; do  
 sed 's/http/\nhttp/g' "${b}" | sed -r 's/(^.{250})(.*)/\1/g' | grep "^http" | sed -r 's/(PNG|GIF|JPEG|JPG)(.*)/\1/ig' | grep -Ei "(PNG|GIF|JPEG|JPG)$" >> "${master_file}.tmp" 
done < <(ls ${html_prefix}*html)

 ## Uniq and sort the image links 
sort "${master_file}.tmp" | uniq > "${master_file}"
rm -v "${master_file}.tmp"

 ## Fetch image links 
while read c; do 
 wget --tries=3 -E -e robots=off -nc --random-wait --content-disposition --no-check-certificate -p --restrict-file-names=windows,lowercase,ascii --header "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.$(($RANDOM%10))) Gecko/20100101 Firefox/19.0" -nd "${c}"
done < "${master_file}" 

((${cleanup_flag})) && rm -v "${html_prefix}"*html

exit 0