From 3b3b6f1c3eac5836254c9b64544033912af3a33f Mon Sep 17 00:00:00 2001 From: flashwave Date: Sun, 6 Dec 2015 02:52:35 +0100 Subject: [PATCH] typescript because fuck rubberscript seriously why is javascript so globally loved --- libraries/BBcode.php | 4 +- libraries/Router.php | 14 +++ public/content/data/yuuno/js/yuuno.js | 143 ++----------------------- public/content/scripts/sakura.js | 126 ++++++++++++++++++++++ public/content/scripts/sakura.ts | 144 ++++++++++++++++++++++++++ sakura.php | 2 +- templates/yuuno/global/master.tpl | 14 +-- 7 files changed, 301 insertions(+), 146 deletions(-) create mode 100644 libraries/Router.php create mode 100644 public/content/scripts/sakura.js create mode 100644 public/content/scripts/sakura.ts diff --git a/libraries/BBcode.php b/libraries/BBcode.php index 8b15e32..dfedf67 100644 --- a/libraries/BBcode.php +++ b/libraries/BBcode.php @@ -47,11 +47,11 @@ class BBcode self::$bbcode->addCodeDefinition($builder->build()); // Box tag - $builder = new CodeDefinitionBuilder('box', '
Click to open
'); + $builder = new CodeDefinitionBuilder('box', '
Click to open
'); self::$bbcode->addCodeDefinition($builder->build()); // Box tag - $builder = new CodeDefinitionBuilder('box', '
{option}
'); + $builder = new CodeDefinitionBuilder('box', '
{option}
'); $builder->setUseOption(true); self::$bbcode->addCodeDefinition($builder->build()); diff --git a/libraries/Router.php b/libraries/Router.php new file mode 100644 index 0000000..68796ca --- /dev/null +++ b/libraries/Router.php @@ -0,0 +1,14 @@ + 0) { - objectCont[0].parentNode.removeChild(objectCont[0]); - } -} - -// Removing an element by ID -function removeId(id) { - // Get the element - var objectCont = document.getElementById(id); - - // If the element exists use the parent node to remove it - if(typeof(objectCont) != "undefined" && objectCont !== null) { - objectCont.parentNode.removeChild(objectCont); - } -} - // Show the full-page busy window function ajaxBusyView(show, message, type) { // Get elements @@ -312,7 +258,7 @@ function ajaxBusyView(show, message, type) { if(busyCont.style.opacity > 0) { busyCont.style.opacity = busyCont.style.opacity - 0.1; } else { // When we've reached 0 remove the container element and clear the fadeout interval - removeId('ajaxBusy'); + Sakura.removeId('ajaxBusy'); clearInterval(fadeOut); } }, 10); @@ -519,84 +465,9 @@ function submitPostHandler(result, busyView, resetCaptchaOnFailure) { } -// Encode UTF-8 -function utf8_encode(str) { - return unescape(encodeURIComponent(str)); -} - -// Decode UTF-8 -function utf8_decode(str) { - return decodeURIComponent(escape(str)); -} - -// Calculate the amount of unique characters in a string -function uniqueChars(str) { - // Create storage array and count var - var usedChars = []; - var count = 0; - - // Count the amount of unique characters - for(var i = 0; i < str.length; i++) { - // Check if we already counted this character - if(usedChars.indexOf(str[i]) == -1) { - // Push the character into the used array - usedChars.push(str[i]); - - // Up the count - count++; - } - } - - // Return the count - return count; -} - -// Alternative for Math.log2() since it's still experimental -function log2(num) { - return Math.log(num) / Math.log(2); -} - -// Calculate password entropy -function pwdEntropy(pwd) { - // Decode utf-8 chars - pwd = utf8_decode(pwd); - - // Count the amount of unique characters in the password and calculate the entropy - return uniqueChars(pwd) * log2(256); -} - // Check if password is within the minimum entropy value function checkPwdEntropy(pwd) { - return (pwdEntropy(pwd) >= sakuraVars.minPwdEntropy); -} - -// Check the length of a string -function checkStringLength(str, min, max) { - // Get length of string - var len = str.length; - - // Check if it meets the minimum - if(len < min) { - return false; - } - - // Check if it meets the maximum - if(len > max) { - return false; - } - - // If it passes both return true - return true; -} - -// Validate email address formats -function validateEmail(email) { - // The regex - var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,48})+$/; - // is of fix - - // Test it (which returns true or false) - return re.test(email); + return (Sakura.pwdEntropy(pwd) >= sakuraVars.minPwdEntropy); } // Check registration variables @@ -617,12 +488,12 @@ function registerVarCheck(id, mode, option) { break; case 'email': - check = validateEmail(input.value); + check = Sakura.validateEmail(input.value); break; case 'username': default: - check = checkStringLength(input.value, sakuraVars.minUserLen, sakuraVars.maxUserLen); + check = Sakura.stringLength(input.value, sakuraVars.minUserLen, sakuraVars.maxUserLen); break; } @@ -722,7 +593,7 @@ function commentReply(id, session, category, action, avatar) { // Remove it if it already exists if(replyBox) { - removeId('comment-reply-container-' + id); + Sakura.removeId('comment-reply-container-' + id); return false; } diff --git a/public/content/scripts/sakura.js b/public/content/scripts/sakura.js new file mode 100644 index 0000000..07ceca6 --- /dev/null +++ b/public/content/scripts/sakura.js @@ -0,0 +1,126 @@ +/* + * Shared client side code + */ +// Meta functions +var Sakura = (function () { + function Sakura() { + } + // Get or set a cookie value + Sakura.cookie = function (name, value) { + if (value === void 0) { value = null; } + // If value is null only get the cookie's value + if (value) { + // Delete the old instance + document.cookie = this.cookiePrefix + name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT; path=' + this.cookiePath; + // Assign the cookie + document.cookie = this.cookiePrefix + name + '=' + value + '; path=' + this.cookiePath; + // Pass the value through + return value; + } + else { + // Perform a regex on document.cookie + var get = new RegExp('(^|; )' + encodeURIComponent(this.cookiePrefix + name) + '=([^;]*)').exec(document.cookie); + // If anything was returned return it (professional phrasing) + return get ? get[2] : ''; + } + }; + // Unix timestamp + Sakura.epoch = function () { + return Math.floor(Date.now() / 1000); + }; + // Toggle a class + Sakura.toggleClass = function (element, name) { + // Check if the class already exists and if not add it + if (element.className.indexOf(name) < 0) { + element.className += ' ' + name; + } + else { + element.className = element.className.replace(name, '').trim(); + } + }; + // Remove every element with a specific class name + Sakura.removeByClass = function (name) { + // Get the elements + var objs = document.getElementsByClassName(name); + // Use a while loop to remove each element + while (objs.length > 0) { + objs[0].parentNode.removeChild(objs[0]); + } + }; + // Remove a single element with a specific id + Sakura.removeById = function (id) { + // Get the element + var obj = document.getElementById(id); + // If the element exists use the parent node to remove it + if (typeof (obj) != "undefined" && obj !== null) { + obj.parentNode.removeChild(obj); + } + }; + // Alternative for Math.log2() since it's still experimental + Sakura.log2 = function (num) { + return Math.log(num) / Math.log(2); + }; + // Get the number of unique characters in a string + Sakura.unique = function (string) { + // Store the already found character + var used = []; + // The amount of characters we've already found + var count = 0; + // Count the amount of unique characters + for (var i = 0; i < string.length; i++) { + // Check if we already counted this character + if (used.indexOf(string[i]) == -1) { + // Push the character into the used array + used.push(string[i]); + // Up the count + count++; + } + } + // Return the count + return count; + }; + // Calculate password entropy + Sakura.entropy = function (string) { + // Decode utf-8 encoded characters + string = utf8.decode(string); + // Count the unique characters in the string + var unique = this.unique(string); + // Do the entropy calculation + return unique * this.log2(256); + }; + // Validate string lengths + Sakura.stringLength = function (string, minimum, maximum) { + // Get length of string + var length = string.length; + // Check if it meets the minimum/maximum + if (length < minimum || length > maximum) { + return false; + } + // If it passes both return true + return true; + }; + // Validate email address formats + Sakura.validateEmail = function (email) { + // RFC compliant e-mail address regex + var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,48})+$/; + // Test it on the email var which'll return a boolean + return re.test(email); + }; + Sakura.cookiePrefix = ""; // Cookie prefix, gets prepended to cookie names + Sakura.cookiePath = "/"; // Cookie path, can in most cases be left untouched + return Sakura; +})(); +// UTF-8 functions +var utf8 = (function () { + function utf8() { + } + // Encode a utf-8 string + utf8.encode = function (string) { + return unescape(encodeURIComponent(string)); + }; + // Decode a utf-8 string + utf8.decode = function (string) { + return decodeURIComponent(escape(string)); + }; + return utf8; +})(); diff --git a/public/content/scripts/sakura.ts b/public/content/scripts/sakura.ts new file mode 100644 index 0000000..850acba --- /dev/null +++ b/public/content/scripts/sakura.ts @@ -0,0 +1,144 @@ +/* + * Shared client side code + */ + +// Meta functions +class Sakura { + public static cookiePrefix: string = ""; // Cookie prefix, gets prepended to cookie names + public static cookiePath: string = "/"; // Cookie path, can in most cases be left untouched + + // Get or set a cookie value + public static cookie(name: string, value: string = null): string { + // If value is null only get the cookie's value + if (value) { + // Delete the old instance + document.cookie = this.cookiePrefix + name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT; path=' + this.cookiePath; + + // Assign the cookie + document.cookie = this.cookiePrefix + name + '=' + value + '; path=' + this.cookiePath; + + // Pass the value through + return value; + } else { + // Perform a regex on document.cookie + var get = new RegExp('(^|; )' + encodeURIComponent(this.cookiePrefix + name) + '=([^;]*)').exec(document.cookie); + + // If anything was returned return it (professional phrasing) + return get ? get[2] : ''; + } + } + + // Unix timestamp + public static epoch(): number { + return Math.floor(Date.now() / 1000); + } + + // Toggle a class + public static toggleClass(element: HTMLElement, name: string): void { + // Check if the class already exists and if not add it + if (element.className.indexOf(name) < 0) { + element.className += ' ' + name; + } else { // If so remove it and kill additional spaces + element.className = element.className.replace(name, '').trim(); + } + } + + // Remove every element with a specific class name + public static removeByClass(name: string): void { + // Get the elements + var objs = document.getElementsByClassName(name); + + // Use a while loop to remove each element + while (objs.length > 0) { + objs[0].parentNode.removeChild(objs[0]); + } + } + + // Remove a single element with a specific id + public static removeById(id: string): void { + // Get the element + var obj = document.getElementById(id); + + // If the element exists use the parent node to remove it + if (typeof (obj) != "undefined" && obj !== null) { + obj.parentNode.removeChild(obj); + } + } + + // Alternative for Math.log2() since it's still experimental + public static log2(num: number): number { + return Math.log(num) / Math.log(2); + } + + // Get the number of unique characters in a string + public static unique(string: string): number { + // Store the already found character + var used: string[] = []; + + // The amount of characters we've already found + var count: number = 0; + + // Count the amount of unique characters + for (var i = 0; i < string.length; i++) { + // Check if we already counted this character + if (used.indexOf(string[i]) == -1) { + // Push the character into the used array + used.push(string[i]); + + // Up the count + count++; + } + } + + // Return the count + return count; + } + + // Calculate password entropy + public static entropy(string: string): number { + // Decode utf-8 encoded characters + string = utf8.decode(string); + + // Count the unique characters in the string + var unique: number = this.unique(string); + + // Do the entropy calculation + return unique * this.log2(256); + } + + // Validate string lengths + public static stringLength(string: string, minimum: number, maximum: number): boolean { + // Get length of string + var length = string.length; + + // Check if it meets the minimum/maximum + if (length < minimum || length > maximum) { + return false; + } + + // If it passes both return true + return true; + } + + // Validate email address formats + public static validateEmail(email: string): boolean { + // RFC compliant e-mail address regex + var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,48})+$/; + + // Test it on the email var which'll return a boolean + return re.test(email); + } +} + +// UTF-8 functions +class utf8 { + // Encode a utf-8 string + public static encode(string): string { + return unescape(encodeURIComponent(string)); + } + + // Decode a utf-8 string + public static decode(string): string { + return decodeURIComponent(escape(string)); + } +} diff --git a/sakura.php b/sakura.php index 15f9950..76e12c3 100644 --- a/sakura.php +++ b/sakura.php @@ -8,7 +8,7 @@ namespace Sakura; // Define Sakura version -define('SAKURA_VERSION', '20151204'); +define('SAKURA_VERSION', '20151206'); define('SAKURA_VLABEL', 'Eminence'); define('SAKURA_COLOUR', '#6C3082'); diff --git a/templates/yuuno/global/master.tpl b/templates/yuuno/global/master.tpl index ad7e82a..285bd49 100644 --- a/templates/yuuno/global/master.tpl +++ b/templates/yuuno/global/master.tpl @@ -32,18 +32,15 @@ {{ block('css') }} +