File: gestion/functions.js

Recommend this page to a friend!
  Classes of Pierre FAUQUE  >  OPDS PHP Ebook Publisher Class  >  gestion/functions.js  >  Download  
File: gestion/functions.js
Role: Application script
Content type: text/plain
Description: Script of application
Class: OPDS PHP Ebook Publisher Class
Publish and distribute ebooks for download
Author: By
Last change:
Date: 4 months ago
Size: 6,580 bytes
 

Contents

Class file image Download
/*
// OPDS basic gestion (only add entities and relations, not modify)
// Version: 0.1
// Pierre FAUQUE, <pierre@fauque.net>
// Script: 2014, Script->Class: 2019, Gestion: may 2020
// Encoding: UTF-8
// Text editor: GNU/Linux Debian Vi
// File: function.js (v0.1)
// Role: General javascript functions (library)
*/

//==================================================
// 18 feb 2006. (c) Pierre FAUQUE, pierre@fauque.net
// NB: Fonctions is...() only return TRUE or FALSE.
// Fonctions isValid...() return OK or an error message
// The other functions unnecessary for this application (ex: hours, etc.) have been removed.
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Tests if the character passed in parameter (c) is an authorized character (list in parameter)
function isCharInList(c,list) {
    var c,list;
    if (list.indexOf(c.toLowerCase()) < 0) { return false; } else { return true; }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Tests if all the characters of the string passed in parameter (str) are allowed
// Function dependent on isCharInList()
function isStringInList(str,list) {
    var str,list,n;
    for (n=0; n<str.length; n++) {
        if (!isCharInList(str.substring(n,n+1),list)) { return false; }
    }
    return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Tests if the characters passed in parameter correspond to a valid Top Level Domain (TDL)
// Possible to add TLD
function isTLD(tld) {
    var tld,domains;
    domains = "com|net|int|org|edu|mil|gov|af|al|dz|as|ad|ao|ai|aq|ag|ar|am|aw|ac|au|at|"
        + "az|bh|bd|bb|by|be|bz|bj|bm|bt|bo|ba|bw|bv|br|io|bn|bg|bf|bi|gg|je|kh|cm|ca|"
        + "cv|ky|td|cl|cn|cx|cc|co|km|cg|ck|cr|ci|hr|cf|cu|cy|cz|dk|dj|dm|do|tp|ec|eg|"
        + "sv|gq|er|ee|et|fk|fo|fj|fi|gf|pf|tf|fr|fx|ga|gm|ge|de|gh|gi|gr|gl|gd|gp|gu|"
        + "gt|gn|gw|gy|ht|hm|hn|hk|hu|is|in|id|ir|iq|ie|im|il|it|jm|jp|jo|kz|ke|ki|kp|"
        + "kr|kw|kg|la|lv|lb|ls|lr|ly|li|lt|lu|mo|mk|mg|mw|my|mv|ml|mt|mh|mq|mr|mu|yt|"
        + "mx|fm|md|mc|mn|ms|ma|mz|mm|mp|na|nr|np|an|nl|nc|nz|ni|ne|ng|nu|nf|no|om|pk|"
        + "pw|pa|pg|py|pe|ph|pn|pl|pt|pr|qa|re|ro|ru|rw|gs|lc|ws|sm|st|sa|sn|sc|sl|sg|"
        + "sk|si|sb|so|za|es|lk|sh|kn|pm|vc|sd|sr|sj|sz|se|ch|sy|tw|tj|tz|th|bs|tg|tk|"
        + "to|tt|tn|tr|tm|tc|tv|um|ug|ua|uk|us|uy|ae|uz|vu|va|ve|vn|vg|vi|wf|eh|ye|yu|"
        + "zr|zm|zw|eu|"
        + "biz|info|aero";
    if (domains.indexOf(tld.toLowerCase()) < 0) { return false; } else { return true; }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Tests the format of an email address.
// Function dependent on isTLD()
// Login not null, must contain @ in the address, a . in the domain (>5 chars), a valid TLD
function isEmail(address) {
    var address,parts,domains;
    if (address.indexOf("@") <= 0) { return false; }
    parts = address.split("@");
    if (parts[0].length == 0) { return false; }
    if (parts[1].length < 5) { return false; }
    if (parts[1].indexOf(".") <= 0) { return false; }
    domains = parts[1].split(".");
    if (!isTLD(domains[domains.length-1])) { return false; }
    return true;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Tests the format of a date (YYYY-MM-DD, MySQL Format).
function isDate(date) {
    var date, reg = new RegExp("^[0-9]{4}[-]{1}[0-9]{2}[-]{1}[0-9]{2}$","g");
    if(reg.test(date)) { return true; } else { return false; }
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Test the validity of a date (YYYY-MM-DD).
// Days and months set to 0 are possible, if unknown (JB Poquelin born on 1622-01-00, day=?)
// Function dependent on isDate()
function isValidDate(date) {
    var date,tab;
    if(isDate(date)) {
        tab=date.split('-');
        if((tab[0]*1)<0) { return "No year before Jesus Christ"; }
        if((tab[1]*1)<0 || (tab[1]*1)>12) { return "Incorrect month"; }
        if((tab[2]*1)<0 || (tab[2]*1)>31) { return "Incorrect day"; }
        return "OK";
    }
    return "Incorrect date format";
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Tests the validity of a text field (length and characters allowed)
// Function dependent on isStringInList()
function isValidText(champ,chars,li,ls) {
    var champ,chars,li,ls,lg,msg;
    lg = champ.length;
    if(li || ls) {
        if(lg < li) { msg = "Input too short:\nMinimum : "+li; return msg; }
        if(lg > ls) { msg = "Input too long:\nMaximum : "+ls; return msg; }
    }
    if(!isStringInList(champ,chars)) {
        pos = " (pos: "+whereIsBadChar(champ,chars)+")";
        msg = "Forbidden characters !"+pos; return msg;
     }
    return "OK";
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Removes leading and trailing spaces
function trim(myString) {
    return myString.replace(/^\s+/g,'').replace(/\s+$/g,'');
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 16/05/2020. Remove accents from a string (to capitalize)
function noaccent(string) {
    var string;
    var accentsA = new RegExp('[???]', 'gi');
    var accentsE = new RegExp('[????]', 'gi');
    var accentsI = new RegExp('[???]', 'gi');
    var accentsO = new RegExp('[???]', 'gi');
    var accentsU = new RegExp('[???]', 'gi');
    var accentsY = new RegExp('[?]', 'gi');
    var accentsC = new RegExp('[?]', 'gi');
    string = string.replace(accentsA, 'a');
    string = string.replace(accentsE, 'e');
    string = string.replace(accentsI, 'i');
    string = string.replace(accentsO, 'o');
    string = string.replace(accentsU, 'u');
    string = string.replace(accentsY, 'y');
    string = string.replace(accentsC, 'c');
    return string;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// 16/05/2020. Return the position of the forbidden character or -1 if no bad char
// Function dependent on isCharInList()
function whereIsBadChar(str,list) {
        var str,list,n;
        for (n=0; n<str.length; n++) {
                if (!isCharInList(str.substring(n,n+1),list)) { return n; }
        }
        return -1;
}

//==========================================================================================
// Various variables
var ascii = "abcdefghijklmnopqrstuvwxyz";
var letters = "abcdefghijklmnopqrstuvwxyz????????????";
var digits = "0123456789";


For more information send a message to info at phpclasses dot org.