<?php
/**
 * @package Rjmprogramming_Data_URI_Helper
 * @version 1.0
 */
/*
Plugin Name: Rjmprogramming Data URI Helper
Description: Allow a Data URI Helper replace relative or absolute URLs, highlighted, with local data URIs.
Author: Robert James Metcalfe
Version: 1.0
Author URI: http://www.rjmprogramming.com.au/
*/

// This creates the Data URI Helper hyperlink
function rjmprogramming_Data_URI_Helper() {
	$data_uri_html="<div id=dbrowse style=display:none;><input style=display:inline-block; id=file type=file name=file></input><div id=dpf style=display:INLINE-BLOCK;><span class=readBytesButtons><button style=display:none; data-endbyte=4 data-startbyte=0>1-5</button><button style=display:none; data-endbyte=14 data-startbyte=5>6-15</button><button style=display:none; data-endbyte=7 data-startbyte=6>7-8</button><button name=button id=button value=Process>Process</button></span></div></div>";
	$data_uri_js="<scr" . "ipt type='text/javascript'>
	var urltoreplace='', urlprefixtoreplace='', urlsuffixtoreplace='';
	var typereplacement='application/octet-stream';

function readBlob(opt_startByte, opt_stopByte) {
    var w='';
    var files = document.getElementById('file').files;
    //alert('files.length=' + files.length);
    if (!files.length) {
      return;
    } else {
      typereplacement='application/octet-stream';
      w = files[0].name;
      if (('' + files[0].type) != '') { typereplacement=files[0].type; }
    //alert('w=' + w + ' and looking for ' + (urlprefixtoreplace + urltoreplace + urlsuffixtoreplace));
    }

    var file = files[0];
    var start = parseInt(opt_startByte) || 0;
    var stop = parseInt(opt_stopByte) || file.size - 1;
    var reader = new FileReader();

    reader.onloadend = function(evt) {
      if (evt.target.readyState == FileReader.DONE) { 
        //alert('start done');
        if (document.getElementById('content-textarea-clone')) {
        while (document.getElementById('content-textarea-clone').innerHTML.indexOf(urlprefixtoreplace + urltoreplace + urlsuffixtoreplace) != -1) {
         //alert(urlprefixtoreplace + urltoreplace + urlsuffixtoreplace + ' with string of Length ' + (urlprefixtoreplace + evt.target.result + urlsuffixtoreplace).length);
         document.getElementById('content-textarea-clone').innerHTML=document.getElementById('content-textarea-clone').innerHTML.replace(urlprefixtoreplace + urltoreplace + urlsuffixtoreplace, urlprefixtoreplace + evt.target.result.replace('application/octet-stream',typereplacement) + urlsuffixtoreplace);
        }
        }
        //alert('middle done');
        while (document.getElementById('content').value.indexOf(urlprefixtoreplace + urltoreplace + urlsuffixtoreplace) != -1) {
         //alert(urlprefixtoreplace + urltoreplace + urlsuffixtoreplace + ' with String of length ' + (urlprefixtoreplace + evt.target.result + urlsuffixtoreplace).length);
         document.getElementById('content').value=document.getElementById('content').value.replace(urlprefixtoreplace + urltoreplace + urlsuffixtoreplace, urlprefixtoreplace + evt.target.result.replace('application/octet-stream',typereplacement) + urlsuffixtoreplace);
        }
        document.getElementById('file').value='';
        //alert('all done');
      }
    };

    var blob = file.slice(start, stop + 1);
    reader.readAsDataURL(blob);
}

    function filechk() {
      if (document.getElementById('file').value == '') {
        setTimeout(filechk, 1000);
      } else {
        document.getElementById('button').click();
      }
    }

	function perhapsreplace(rora_url) {
	 if (rora_url != '') {
	  if (rora_url.toLowerCase().indexOf('http') == 0 || rora_url.toLowerCase().indexOf('//') == 0 || rora_url.toLowerCase().indexOf('/') == 0 || rora_url.toLowerCase().indexOf('..') == 0 || rora_url.toLowerCase().indexOf('.') == 0 || (rora_url.substring(0,1) == String.fromCharCode(39) && rora_url.slice(-1) == String.fromCharCode(39)) || (rora_url.substring(0,1) == String.fromCharCode(34) && rora_url.slice(-1) == String.fromCharCode(34))) {
	  if (document.getElementById('content')) {
	  if (('' + document.getElementById('content').className).indexOf('wp-editor-area') != -1) {
	  if (document.getElementById('content').value.indexOf(rora_url) != -1) {
	    urltoreplace=rora_url;
	    urlprefixtoreplace='';
	    urlsuffixtoreplace='';
	    if (rora_url.substring(0,1) == String.fromCharCode(39) && rora_url.slice(-1) == String.fromCharCode(39)) { urlprefixtoreplace=String.fromCharCode(39), urlsuffixtoreplace=String.fromCharCode(39); urltoreplace=rora_url.substring(1,eval(-1 + rora_url.length));  }
	    if (rora_url.substring(0,1) == String.fromCharCode(34) && rora_url.slice(-1) == String.fromCharCode(34)) { urlprefixtoreplace=String.fromCharCode(34), urlsuffixtoreplace=String.fromCharCode(34); urltoreplace=rora_url.substring(1,eval(-1 + rora_url.length));  }
  document.querySelector('.readBytesButtons').addEventListener('click', function(evt) {
    if (evt.target.tagName.toLowerCase() == 'button') {
      var startByte = evt.target.getAttribute('data-startbyte');
      var endByte = evt.target.getAttribute('data-endbyte');
      readBlob(startByte, endByte);
    }
  }, false);
        setTimeout(filechk, 1000);
        document.getElementById('file').click();
        
	    //alert('More to do allowing a data URI to replace ' + rora_url);
	  } else {
	    alert('Textarea content does not contain this ' + rora_url + ' string.');
	  }
	  } else {
	    alert('No textarea content with class wp-editor-area found.');
	  }
	  } else {
	    alert('No textarea content found.');
	  }
	  }
	 }
	}
	</scr" . "ipt>";
	$data_uri_link_words = "Data URI Highlighted Text Replacer";
	echo $data_uri_js . '<a style="cursor:pointer;text-decoration:underline;" target="_blank" title="Data URI helper ..." onmouseover="var xsel=' . 
	  "''" . '; xsel=window.getSelection().toString(); if (xsel.length == 0) {' . 
	  ' xsel=document.selection.createRange().htmlText;' . 
	  ' } if (xsel.length != 0) { document.getElementById(' . "'content'" . ').setSelectionRange(0,0); perhapsreplace(xsel);  } "  onclick="var xsel=' . 
	  "''" . '; xsel=window.getSelection().toString(); if (xsel.length == 0) {' . 
	  ' xsel=document.selection.createRange().htmlText;' . 
	  ' } if (xsel.length != 0) { document.getElementById(' . "'content'" . ').setSelectionRange(0,0); perhapsreplace(xsel);  } " ontouchstart="var xsel=' . 
	  '; xsel=window.getSelection().toString(); if (xsel.length == 0) {' . 
	  ' xsel=document.selection.createRange().htmlText; }' . 
	  ' if (xsel.length != 0) { document.getElementById(' . "'content'" . ').setSelectionRange(0,0); perhapsreplace(xsel); } " id="rjmprogramming_Data_URI_Helper"' . 
	  '>' . $data_uri_link_words . '</a>' . $data_uri_html;
}

// Now we set that function up to execute when the admin_notices action is called
add_action( 'admin_notices', 'rjmprogramming_Data_URI_Helper' );

// We need some CSS to position the hyperlink
function rjmprogramming_data_uri_helper_css() {
	// This makes sure that the positioning is also good for right-to-left languages
	$x = is_rtl() ? 'left' : 'right';

	echo "
	<style type='text/css'>
	#rjmprogramming_Data_URI_Helper {
		float: $x;
		padding-$x: 15px;
		padding-top: 5px;		
		margin: 0;
		font-size: 11px;
	}
	</style>
	";
}

add_action( 'admin_footer', 'rjmprogramming_data_uri_helper_css' );

?>
