<?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.
";
<?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;
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
}
?>
<?php
if ($form['submit']->isSubmittedBy()) {
// ...
}
or
if ($form->isSubmitted() === $form['submit']) {
// ...
}
or
public function validate($form)
{
if ($form->isSubmitted()->getName() == 'reloadSelect')
return;
}
<?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);
<?php
class GistEdit {
private $data;
private static $_instance = NULL ;
public static function init () {
if (self::$_instance === NULL) {
self::$_instance = new self;
}
self::$_instance->data = array();
return self::$_instance;
}
public function edit ($file, $newContent = NULL, $newFileName = NULL) {
if ($newContent !== NULL) {
$this->data[$file]['content'] = $newContent ;
}
if ($newFileName !== NULL) {
$this->data[$file]['filename'] = $newFileName ;
}
return $this;
}
public function deleteFile ($file) {
$this->data[$file] = NULL ;
return $this;
}
public function newFile ($file, $content){
$this->data[$file]['content'] = $content;
return $this;
}
public function get () {
return $this->data;
}
}
class gistAPI {
private $url = "https://api.github.com" ;
private $user = "github" ;
public $ch ;
private $response ;
public $loginInfo ;
function __construct($id = NULL, $pass = NULL) {
if($id === NULL || $pass === NULL){
$loginInfo = NULL;
} else {
$loginInfo = array('username' => $id,
'password' => $pass);
}
$this->loginInfo = $loginInfo;
$this->chReset();
}
public function listGists ($type = "public", $user = NULL) {
switch ($type) {
case "public":
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/public");
break;
case "user":
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/users/" . ($user === NULL ? $this->user:$user) ."/gists");
break;
case "starred":
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/starred");
break;
}
return $this->returnCode();
}
public function getGist ($gistId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/".$gistId);
return $this->returnCode();
}
public function createGist ($files, $description = "", $public = false) {
$filesArray = array();
foreach ($files as $fileName => $content)
$filesArray[$fileName]['content'] = $content;
$postArray = array(
"files" => $filesArray,
"description" => $description,
"public" => $public
);
$jsonArray = json_encode($postArray);
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists");
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
return $this->returnCode();
}
public function editGist ($gistId, $files = NULL, $description = NULL) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
if ($files === NULL && $description !== NULL) {
$jsonArray = json_encode(array("description" => $description));
} elseif ($description === NULL && $files !== NULL) {
$jsonArray = json_encode(array("files" => $files));
} elseif ($description !== NULL && $files !== NULL) {
$jsonArray = json_encode(array("description" => $description, "files" => $files));
} else {
$this->chReset();
return 0;
}
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
return $this->returnCode();
}
public function gistCommits ($gistId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/" . $gistId . "/commits");
return $this->returnCode();
}
public function starGist ($gistId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PUT');
return $this->returnCode();
}
public function unstarGist ($gistId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
return $this->returnCode();
}
public function checkStarGist ($gistId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/star");
return $this->returnCode();
}
public function forkGist ($gistId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'POST');
return $this->returnCode();
}
public function listForkGist ($gistId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/forks");
return $this->returnCode();
}
public function deleteGist ($gistId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId);
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
return $this->returnCode();
}
public function gistComments ($gistId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/".$gistId."/comments");
return $this->returnCode();
}
public function getComment ($gistId, $commentId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
return $this->returnCode();
}
public function createComment ($gistId, $comment){
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments");
curl_setopt($this->ch, CURLOPT_POST, 1);
$jsonArray = json_encode(array("body" => $comment));
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
return $this->returnCode();
}
public function editComment ($gistId, $commentId, $comment) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
$jsonArray = json_encode(array("body" => $comment));
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $jsonArray);
return $this->returnCode();
}
public function deleteComment ($gistId, $commentId) {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/gists/". $gistId ."/comments/". $commentId);
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
return $this->returnCode();
}
public function getLimits() {
curl_setopt($this->ch, CURLOPT_URL, $this->url . "/rate_limit");
return $this->returnCode();
}
private function parseHeader ($header_text) {
$headers = array();
foreach (explode("\r\n", $header_text) as $i => $line){
if (strlen($line) > 1 && $i != 0){
list ($key, $value) = explode(': ', $line);
$headers[$key] = $value;
} else if ($i == 0){
$headers['http_code'] = $line;
}
}
return $headers;
}
private function returnCode () {
$this->response = curl_exec($this->ch);
$header_size = curl_getinfo($this->ch, CURLINFO_HEADER_SIZE);
$header = substr($this->response, 0, $header_size);
$body = substr($this->response, $header_size);
$return = array("header" => $this->parseHeader($header),
"body" => json_decode($body, true),
"raw" => $this->response);
$this->chReset();
return $return;
}
public function chReset () {
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_HEADER, true);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
if ($this->loginInfo !== NULL){
$this->user = $this->loginInfo['username'];
curl_setopt($this->ch, CURLOPT_USERAGENT, $this->loginInfo['username']);
curl_setopt($this->ch, CURLOPT_USERPWD, $this->loginInfo['username'].":".$this->loginInfo['password']);
} else {
curl_setopt($this->ch, CURLOPT_USERAGENT, "gistAPI v1.0");
}
unset($this->response);
}
function __destruct() {
curl_close($this->ch);
}
}
?>
<?php
class Fruit {
private $color = "red";
public function getColor() {
return $this->color;
}
public function &getColorByRef() {
return $this->color;
}
}
echo "\nTEST RUN 1:\n\n";
$fruit = new Fruit;
$color = $fruit->getColor();
echo "Fruit's color is $color\n";
$color = "green"; // does nothing, but bear with me
$color = $fruit->getColor();
echo "Fruit's color is $color\n";
echo "\nTEST RUN 2:\n\n";
$fruit = new Fruit;
$color = &$fruit->getColorByRef(); // also need to put & here
echo "Fruit's color is $color\n";
$color = "green"; // now this changes the actual property of $fruit
$x = $fruit->getColor();
echo "Fruit's color is $color\n";