User Tools

Site Tools


br-223-functions


Funções de apoio


<?php

# ========================================================================================
#
#   FOLLOWZUP PROJECT
#
# ========================================================================================
#
#   Copyright (C) 2016 Followzup.com
#
#   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 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/>
#
# ========================================================================================

    /* =============================================
       ID Generator based on current date and time,
       prefixed by:
          - "z" for Users;
          - "c" for Channels;
          - "d" for Devices;
          - "i" for Interfaces;
          - "k" for RSA keys.
       Characters "0", "o", "1" and "l" are replaced
       by "a" to avoid mistakes.
       ============================================= */

    function idgenerator ($wparam)
    {

        if     ( $wparam == "usr" ) $wprefix = "z";
        elseif ( $wparam == "chn" ) $wprefix = "c";
        elseif ( $wparam == "dev" ) $wprefix = "d";
        elseif ( $wparam == "int" ) $wprefix = "i";
        elseif ( $wparam == "key" ) $wprefix = "k";
        else return "";

        $wtime  = explode(" ",microtime());
        $wcode1 = str_pad(strval(base_convert($wtime[1]-1000000000,10,36)),6,"0",STR_PAD_LEFT);
        $wcode2 = str_pad(strval(base_convert($wtime[0]*1000000,10,36)),4,"0",STR_PAD_LEFT);
        $wkey   = $wcode1 . $wcode2 . rand(2,9);

        return $wprefix . strtr("$wkey","01lo","aaaa");

    }

    /* =============================================
       RSA key generator for Devices and Channels.
       ============================================= */

    function keygenerator ($wlength)
    {

        $wparm = array ( "digest_alg" => "sha1",
                         "private_key_bits" => $wlength,
                         "private_key_type" => OPENSSL_KEYTYPE_RSA );

        $wres = openssl_pkey_new($wparm);
        openssl_pkey_export($wres,$wprikey);
        $wdet = openssl_pkey_get_details($wres);
        $wrsa = $wdet["rsa"];
        $wprikey  = base64_encode($wprikey);
        $wpubkey  = base64_encode($wdet["key"]);
        $wmodulus = bin2hex($wrsa["n"]);
        $wpubexpo = bin2hex($wrsa["e"]);
        $wpriexpo = bin2hex($wrsa["d"]);
        $wprime1  = bin2hex($wrsa["p"]);
        $wprime2  = bin2hex($wrsa["q"]);
        $wdmp1    = bin2hex($wrsa["dmp1"]);
        $wdmq1    = bin2hex($wrsa["dmq1"]);
        $wiqmp    = bin2hex($wrsa["iqmp"]);

        return array ( "pub" => $wpubkey,
                       "pri" => $wprikey,
                       "mod" => $wmodulus,
                       "pux" => $wpubexpo,
                       "prx" => $wpriexpo,
                       "pr1" => $wprime1,
                       "pr2" => $wprime2,
                       "dmp" => $wdmp1,
                       "dmq" => $wdmq1,
                       "iqm" => $wiqmp );

    }

    /* =============================================
       Sanitize functions to clean IDs, Tags, Text,
       E-mail and URL string parameters.
       ============================================= */

    function sanitizeID ($wstring)
    {

        return preg_replace ( "/[^a-z0-9]/", "", strtolower ( "$wstring" ) );

    }

    function sanitizeTag ($wstring)
    {

        $wstring = preg_replace ( "/[^A-Za-z0-9\-]/", "", "$wstring" );
        $wstring = preg_replace ( "/-+/", "-", "$wstring" );

        return trim ( "$wstring", " \-" );

    }

    function sanitizeText ($wstring)
    {

        $subs = array ( chr(34) => "", chr(39) => "", chr(96) => "" );

        return trim ( filter_var ( strtr ( "$wstring", $subs ), FILTER_SANITIZE_STRING ) );

    }

    function sanitizeEmail ($wstring)
    {

        $subs = array ( chr(34) => "", chr(39) => "", chr(96) => "", chr(32) => "" );

        return filter_var ( strtr ( "$wstring", $subs ), FILTER_SANITIZE_EMAIL );

    }

    function sanitizeURL ($wstring)
    {

        $subs = array ( chr(34) => "", chr(39) => "", chr(96) => "", chr(32) => "" );

        $wstring = filter_var ( strtr ( "$wstring", $subs ), FILTER_SANITIZE_URL );

        $wparse = parse_url("$wstring");

        if ( !isset($wparse["scheme"]) )  $wparse = parse_url("http://" . $wstring);

        if ( !isset($wparse["host"]) )    $wparse["host"] = "";

        if ( isset($wparse["port"]) )     $wparse["port"] = ":" . $wparse["port"];
        else                              $wparse["port"] = "";

        if ( isset($wparse["path"]) )     $wparse["path"] = preg_replace ( "/\/+/", "/", $wparse["path"] );
        else                              $wparse["path"] = "";

        if ( isset($wparse["query"]) )    $wparse["query"] = "?" . $wparse["query"];
        else                              $wparse["query"] = "";

        if ( isset($wparse["fragment"]) ) $wparse["fragment"] = "#" . $wparse["fragment"];
        else                              $wparse["fragment"] = "";

        if ( $wparse["host"] == "" ) return "";

        return "http://" . $wparse["host"] . $wparse["port"] . $wparse["path"] . $wparse["query"] . $wparse["fragment"];

    }

    /* =============================================
       Validate functions to verify IDs, Tags, URL
       and E-mail string parameters.
       ============================================= */

    function validateID ($wstring)
    {

        $subs = array ( chr(34) => "", chr(39) => "", chr(96) => "", chr(32) => "" );

        $wstring = strtr ( "$wstring", $subs );

        $wstring1 = sanitizeID($wstring);

        $w = substr($wstring1,0,1);

        if ( "$wstring1" == "$wstring" and strlen($wstring1) == 12 and ( $w == "z" or  $w == "c" or  $w == "d" or  $w == "i" or  $w == "k" ) ) return true;

        else return false;

    }

    function validateTag ($wstring)
    {

        $subs = array ( chr(34) => "", chr(39) => "", chr(96) => "", chr(32) => "" );

        $wstring = strtr ( "$wstring", $subs );

        $wstring1 = sanitizeTag($wstring);

        if ( $wstring1 == $wstring and strlen($wstring1) > 2 and strlen($wstring1) < 33 ) return true;

        else return false;

    }

    function validateEmail ($wstring)
    {

        $wstring = sanitizeEmail($wstring);

        $wmask = "/^(([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}){0,1}$/";

        if ( preg_match ( $wmask, $wstring ) ) return true;

        else return false;

    }

    function validateURL ($wstring)
    {

        $subs = array ( chr(34) => "", chr(39) => "", chr(96) => "", chr(32) => "" );

        $wstring = strtr ( "$wstring", $subs );

        $wstring1 = sanitizeURL($wstring);

        if ( "$wstring1" == "$wstring" ) return true;

        else return false;

    }

?>


br-223-functions.txt · Last modified: 2017/06/21 05:11 by admin

Page Tools