воскресенье, 20 октября 2013 г.

PHP: Class for encryption / decryption

PHP: Class for encryption / decryption




class Encryption {

    const CYPHER = MCRYPT_RIJNDAEL_256;
    const MODE = MCRYPT_MODE_CBC;
    const KEY = 'anysecretkey';

    public function encrypt($plaintext) {
        $td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, self::KEY, $iv);
        $crypttext = mcrypt_generic($td, $plaintext);
        mcrypt_generic_deinit($td);
        return base64_encode($iv . $crypttext);
    }

    public function decrypt($crypttext) {
        $crypttext = base64_decode($crypttext);
        $plaintext = '';
        $td = mcrypt_module_open(self::CYPHER, '', self::MODE, '');
        $ivsize = mcrypt_enc_get_iv_size($td);
        $iv = substr($crypttext, 0, $ivsize);
        $crypttext = substr($crypttext, $ivsize);
        if ($iv) {
            mcrypt_generic_init($td, self::KEY, $iv);
            $plaintext = mdecrypt_generic($td, $crypttext);
        }
        return trim($plaintext);
    }

}

// Using example:
// $encrypted_string = Encryption::encrypt('2');
// $decrypted_string = Encryption::decrypt($encrypted_string);


PHP: Inverse function for nl2br

PHP: Inverse function for nl2br


function br2nl($str) {
    $str = preg_replace("/(\r\n|\n|\r)/", "", $str);
    return preg_replace("=<br */?>=i", "\n", $str);
}

PHP: Test a Password Strength

PHP: Test a Password Strength



function PasswordStrength($password, $return_as_number = true) {
    if (strlen($password) == 0) {
        return 1;
    }

    $strength = 0;

    /* Get the length of the password */
    $length = strlen($password);

    /* Check if password is not all lower case */
    if (strtolower($password) != $password) {
        $strength += 1;
    }

    /* Check if password is not all upper case */
    if (strtoupper($password) == $password) {
        $strength += 1;
    }

    /* Check string length is 8-15 chars */
    if ($length >= 8 && $length <= 15) {
        $strength += 1;
    }

    /* Check if lenth is 16 - 35 chars */
    if ($length >= 16 && $length <= 35) {
        $strength += 2;
    }

    /* Check if length >35 chars */
    if ($length > 35) {
        $strength += 3;
    }

    /* Get the numbers in the password */
    preg_match_all('/[0-9]/', $password, $numbers);
    $strength += count($numbers[0]);

    /* Check for special chars */
    preg_match_all('/[|!@#$%&*\/=?,;.:\-_+~^\\\]/', $password, $specialchars);
    $strength += sizeof($specialchars[0]);

    /* Get the number of unique chars */
    $chars = str_split($password);
    $num_unique_chars = sizeof(array_unique($chars));
    $strength += $num_unique_chars * 2;

    /* Strength is a number 1-10 */
    $strength = $strength > 99 ? 99 : $strength;
    $strength = floor($strength / 10 + 1);

    /* Return number OR word */
    if ($return_as_number) {
        return $strength;
    } else {
        if (($strength >= 1) && ($strength < 3))
            return 'Weak';
        if (($strength >= 3) && ($strength < 6))
            return 'Normal';
        if (($strength >= 6) && ($strength < 8))
            return 'Good';
        if (($strength >= 8) && ($strength <= 10))
            return 'Awesome';
    }
}


Bootstrap: centering bootstrap 3 pagination

Bootstrap: centering bootstrap 3 pagination


<!-- for bootstrap 2.x -->
<div class="pagination pagination-centered">
  <ul>
    <li class="disabled"><a href="#">« prev</a></li>
    <li class="active"><a href="#">1</a></li>
    <li><a href="index.php?p=2">2</a></li>
    <li><a href="index.php?p=3">3</a></li>
    <li><a href="index.php?p=2">next »</a></li>
  </ul>
</div>

<!-- for bootstrap 3.x -->
<div class="text-center">
  <ul class="pagination">
    <li class="disabled"><a href="#">« Previous</a></li>
    <li class="active"><a title="Go to page 1 of 12" href="#">1</a></li>
    <li><a title="Go to page 2 of 12" href="/index.php?page=2&ipp=10">2</a></li>
    <li><a title="Go to page 3 of 12" href="/index.php?page=3&ipp=10">3</a></li>
    <li><a href="/index.php?page=2&ipp=10">Next »</a></li>
  </ul>
</div>


Make a thumbnail screenshot of the video


Make a thumbnail screenshot of the video



$source_path = $_SERVER['DOCUMENT_ROOT'] . 'content/videos/my_video.mp4';
$dest_path = $_SERVER['DOCUMENT_ROOT'] . 'content/videos/my_video.jpg';

//"-10" means offset of 10 seconds from start
exec("ffmpeg  -itsoffset -10  -i $source_path -s 320x240 $dest_path");

Постоянные читатели

Популярные сообщения