/ Gists

Gists

On gists

Dekorator - Response

Nette

decorator-nette.php #

<?php
# https://forum.nette.org/cs/31783-mazanie-suboru-po-samotnej-akcii-stiahnut-subor#p202279

use Nette\Application\Responses\FileResponse;
use Nette\Http\IRequest;
use Nette\Http\IResponse;

final class DownloadAndDeleteFileResponse implements Nette\Application\IResponse
{
    private $response;

    public function __construct(FileResponse $response)
    {
        $this->response = $response;
    }

    public funcion send(IRequest $request, IResponse $response)
    {
        $this->response->send($request, $response);
        unlink($this->response->getFile());
    }

On gists

Zyla simple parser

PHP

parser.php #

<?php

$x = file_get_contents($file);

$final = preg_match("~<tr[^>]*>(.+)</tr>~si", $x, $match);

$lastTr = strpos($match[0], '</tr>');
$match[0] = substr($match[0], $lastTr + 8,  strlen($match[0]));


file_put_contents("frag.txt", $match[0]);

$f = fopen("frag.txt", "r");

$cols = array("kolo", "datum", "cas", "domaci", "hoste", "skore", "strelci_karty");

//$in_td = false;
$rows = array();

while (!feof($f))
{
   $r = fgets($f);
   # preskocit prazdne radky
   if ( trim($r) != '' )
   {
      if (strpos($r, '<tr') !== false)
      {
        // $in_td=true;
         $rows = array();
      }
      else
      {
         if (strpos($r, '</tr') !== false)
         {
            //$in_td = false;
            #zpracuju $rows

            if (count($rows) == 8)
            {
               #mam v prvni bunce kolo
               $kolo = array_shift($rows);
            }
            zapis_insert($kolo, $rows);
            //print_r($rows);

         }
         else
         {
            if (strpos($r, '<td') !== false)
            {
               $t = preg_replace('/<[^>]*>/', '', trim($r));
               $rows[] = $t;
            }
         }
      }
   }
}

On gists

Rand number between (start, end)

MySql MySql tricks

random-number-between.sql #

-- For range (min..max( (min inclusive, max exclusive) it is:
FLOOR( RAND() * (max-min) + min )

-- For range (min..max) (min+max inclusive) it is:
FLOOR( RAND() * (max-min+1) + min )

On gists

Bit operators - Johny

Bitwise operators

johny-ex.php #

<?php

// For each flag, assign a power of two
// This makes a mask with a single bit turned on
$male = 1;
$bald = 2;
$tall = 4;
$funny = 8;
$fictitious = 16;

// To set flags, use OR
// Init John to male:
$john = $male;

// Set John to tall and fictitious
$john = $john | $tall | $fictitious;
// or $john |= $tall | $fictitious;


// To check if John is tall, use AND
if ( $john & $tall ) echo "You tall!
";
if ( ! ($john & $funny) ) echo "You not funny!
";
// or  if ( ($john & $tall) == $tall ) 
// because since $tall only has one bit set to 1,
// something & $tall is either 0 or $tall
// but comparing to zero is faster
// than checking for equality

// To flip tall, use XOR
$john = $john ^ $tall;
// or $john ^= $tall;
$not = ( $john & $tall ) ? "" : "not ";
echo "You ".$not."tall...
";

// To unset tall, use & with the complement
$john = $john & (~ $tall);
$not = ( $john & $tall ) ? "" : "not ";
echo "You ".$not."tall, dude.
";

// To invert all flags, use NOT (~):
$antijohn = ~ $john;
$not = ( $antijohn & $funny ) ? "" : "not ";
echo "You ".$not."funny, antijohn.
";


On gists

Permission - bit operators

Bitwise operators

permission.php #

<?php


class Permissions {

	const ADD_CONTENT = 0x1;
	const ADD_OWN_CONTENT = 0x2;
	const EDIT_CONTENT = 0x4;
	const EDIT_OWN_CONTENT = 0x8;
	const DELETE_CONTENT = 0x10;
	const DELETE_OWN_CONTENT = 0x20;
	const ADD_COMMENT = 0x40;
	const VIEW_COMMENT = 0x80;

/*
	define("f0", 0x1); // 2^0
	define("f1", 0x2); // 2^1
	define("f2", 0x4); // 2^2
	define("f3", 0x8); // 2^3
	define("f4", 0x10); // 2^4
	define("f5", 0x20); // 2^5
	// ...
	define("f20", 0x1000000); // 2^20
	define("f21", 0x2000000); // 2^21
	define("f22", 0x4000000); // 2^22
	define("f23", 0x8000000); // 2^23
	define("f24", 0x10000000); // 2^24
	// ... up to 2^31
*/

}


function hasPermission($permissionToCheck, $userPermissions) {
		return $permissionToCheck & $userPermissions;
}

//Give the user some permissions
$myPermissions = Permissions::ADD_CONTENT | Permissions::EDIT_CONTENT | Permissions::VIEW_COMMENT;


//Can the user add content?
if (hasPermission(Permissions::ADD_CONTENT, $myPermissions)) echo 'User can add contnet';
else echo 'User cannot add content';

echo '<br />';

if (hasPermission(Permissions::EDIT_CONTENT, $myPermissions)) echo 'User can edit contnet';
else echo 'User cannot edit content';

echo '<br />';

if (hasPermission(Permissions::DELETE_CONTENT, $myPermissions)) echo 'User can delete contnet';
else echo 'User cannot delete content';


echo '<br />';

//Revoke a permission:
$myPermissions &= ~Permissions::ADD_CONTENT; 

//This should now be false
if (hasPermission(Permissions::ADD_CONTENT, $myPermissions)) echo 'User can add contnet';
else echo 'User cannot add content';

//Add a new permission:
$myPermissions |= Permissions::DELETE_OWN_CONTENT;

On gists

Check permission using bitwise operators

Bitwise operators

bitwise-permission-checking.php #

Initially, I found bitmasking to be a confusing concept and found no use for it. So I've whipped up this code snippet in case anyone else is confused:

<?php

    // The various details a vehicle can have
    $hasFourWheels = 1;
    $hasTwoWheels  = 2;
    $hasDoors      = 4;
    $hasRedColour  = 8;

    $bike          = $hasTwoWheels;
    $golfBuggy     = $hasFourWheels;
    $ford          = $hasFourWheels | $hasDoors;
    $ferrari       = $hasFourWheels | $hasDoors | $hasRedColour;

    $isBike        = $hasFourWheels & $bike; # False, because $bike doens't have four wheels
    $isGolfBuggy   = $hasFourWheels & $golfBuggy; # True, because $golfBuggy has four wheels
    $isFord        = $hasFourWheels & $ford; # True, because $ford $hasFourWheels

?>

And you can apply this to a lot of things, for example, security:

<?php

    // Security permissions:
    $writePost = 1;
    $readPost = 2;
    $deletePost = 4;
    $addUser = 8;
    $deleteUser = 16;
    
    // User groups:
    $administrator = $writePost | $readPosts | $deletePosts | $addUser | $deleteUser;
    $moderator = $readPost | $deletePost | $deleteUser;
    $writer = $writePost | $readPost;
    $guest = $readPost;

    // function to check for permission
    function checkPermission($user, $permission) {
        if($user & $permission) {
            return true;
        } else {
            return false;
        }
    }

    // Now we apply all of this!
    if(checkPermission($administrator, $deleteUser)) {
        deleteUser("Some User"); # This is executed because $administrator can $deleteUser
    }

?>

On gists

Sass recursive headings function (h1, h2, h3...)

SCSS

headings.scss #

// orig source: https://guwii.com/bytes/sass-function-loop-headings-h1h2h3h4h5h6/

@function headings($from:1, $to:6) {
    @if $from == $to {
        @return 'h#{$from}';
    } @else {
        @return 'h#{$from},' + headings($from+1, $to);
    }
}

On gists

Jakým tlačítkem byl odeslán formulář? SubmittedBy() ?

Nette Nette-Forms Nette-Tricks

submitted-by.php #

<?php

if ($form['submit']->isSubmittedBy()) {
    // ...
}
or

if ($form->isSubmitted() === $form['submit']) {
    // ...
}

or

public function validate($form)
{
	if ($form->isSubmitted()->getName() == 'reloadSelect')
		return;
}

On gists

Curl - PHP auth

PHP

curl-php-auth.php #

<?php
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
		$output = curl_exec($ch);
		$info = curl_getinfo($ch);
		curl_close($ch);