Skip to content

Instantly share code, notes, and snippets.

@levelsio
Last active April 20, 2024 01:51
Show Gist options
  • Star 52 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save levelsio/792dc0a37b4667ba54d56ed8c3451917 to your computer and use it in GitHub Desktop.
Save levelsio/792dc0a37b4667ba54d56ed8c3451917 to your computer and use it in GitHub Desktop.
Obfuscate your ebook so that people who didn't pay can read it, partly
<?php
/*
I wrote this function to progressively obfuscate text in MAKEbook.io. When it KINDA worked, I just used it.
It can take a lot of improvement. I kinda just tweaked the values until it was good enough. It's not SO progressive though.
It takes all the output of your PHP scripts via ob_start(), reroutes that to the obfuscation function.
You should check if user paid for book or not, then either run ob_start or not!
MIT license, do whatever you want with it!
by Pieter Levels
*/
ob_start("progressivelyObfuscateText");
function progressivelyObfuscateText($text) {
$constant=0;
$exponent=2;
$multiplier=10;
srand('12318232372'); /* this is your random seed, to make sure the page doesn't obfuscate in a different way every load! */
$chars=str_split($text);
$alphabet='abcdefghijklmnopqrstuvwxyz';
$alphabet=str_split($alphabet);
$vowels='aeiouy';
$vowels=str_split($vowels);
$consonants='bcdfghjklmnpqrstvwxzw';
$consonants=str_split($consonants);
$obfuscatedText='';
$i=0;
$y=-1;
$insideHTMLTag=false;
foreach($chars as $char) {
$i++;
$y++;
$progress=$i/1500*$multiplier + pow($i/1500,$exponent) -$constant;
if($chars[$y-3].$chars[$y-2].$chars[$y-1].$chars[$y]=='<h1>') {
// reset progress if we find <h1> because it means new chapter
// so we progressively obfuscate again
$i=0;
}
if($chars[$y-3].$chars[$y-2].$chars[$y-1].$chars[$y]=='<h2>') {
// reset progress if we find <h2> because it means new head
// so we progressively obfuscate again
$i=0;
}
if($chars[$y-2].$chars[$y-1].$chars[$y]=='<b>') {
$insideBoldTag=true;
$obfuscatedText.=$char;
continue;
}
if($chars[$y-3].$chars[$y-2].$chars[$y-1].$chars[$y]=='</b>') {
$insideBoldTag=false;
$obfuscatedText.=$char;
continue;
}
if($char=='<') {
$insideHTMLTag=true;
$obfuscatedText.=$char;
continue;
}
if($char=='>') {
$insideHTMLTag=false;
$obfuscatedText.=$char;
continue;
}
if($insideHTMLTag || $insideHeaderTag || $insideBoldTag || $insideLinkTag) {
$obfuscatedText.=$char;
continue;
}
if($y<25000 || !ctype_alnum($char) || $char==' ' || $char=='"' || $char=='$' || $char=='/' || $char==',' || $char=='http://' || $char=="'" || $char=="\n" || $char=="\t" || $char=="\r") {
$obfuscatedText.=$char;
continue;
}
if(floor(1-rand(0,$progress))==1) {
$obfuscatedText.=$char;
continue;
}
if(in_array($char,$consonants)) $char=$consonants[array_rand($consonants)];
if(in_array($char,$vowels)) $char=$vowels[array_rand($vowels)];
$obfuscatedText.=$char;
}
return $obfuscatedText;
}
?>
@ummahusla
Copy link

Nice

@ashking
Copy link

ashking commented Mar 26, 2018

That's cool! thanks for sharing...

@SeanBarry
Copy link

You missed 'y' in consonants and added 'w' twice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment