Skip to content

Instantly share code, notes, and snippets.

View KaiserWerk's full-sized avatar

Robin K. KaiserWerk

  • Germany
View GitHub Profile
@KaiserWerk
KaiserWerk / str_cmp_sec.php
Last active March 2, 2018 15:16
Function for comparing two strings in a way timing attacks won't work
<?php
/*
* Make sure $compare_length is greater than (or equal to) the maximum length of
* both $string1 and $string2
*
* It's a user setting so different maximum lengths can be handled.
*
* Edit: the built-in PHP function hash_equals might me worth a look:
* http://php.net/manual/de/function.hash-equals.php
*/
@KaiserWerk
KaiserWerk / haversine.php
Last active April 30, 2018 13:56
PHP Haversine - Calculate the distance between two points considering the curvature of earth (useful for long distances)
<?php
/*
* PHP Haversine - Calculate the distance between two points considering the curvature of earth
* Checkmate, flat-earthers!
*
* The result will be given in km. if you want meters, set $earth_radius to 6371000.
*/
function getDistance($latitude1, $longitude1, $latitude2, $longitude2) {
$earth_radius = 6371; // this is in km so the result will be in km as well
@KaiserWerk
KaiserWerk / ICS.php
Created December 18, 2017 11:13 — forked from jakebellacera/ICS.php
A convenient script to generate iCalendar (.ics) files on the fly in PHP.
<?php
/**
* ICS.php
* =======
* Use this class to create an .ics file.
*
* Usage
* -----
* Basic usage - generate ics file contents (see below for available properties):
@KaiserWerk
KaiserWerk / email_attachments_ses_simple.php
Created December 28, 2017 10:31 — forked from sandys/email_attachments_ses_simple.php
Script to send email using Amazon SES with attachments in PHP
<?php
require("./amazon-sdk/sdk.class.php");
// on ubuntu - this script can be run using php5-cli and php5-curl
//Provide the Key and Secret keys from amazon here.
$AWS_KEY = "kkk";
$AWS_SECRET_KEY = "kkkk+xKcdkB";
//certificate_authority true means will read CA of amazon sdk and false means will read CA of OS
$CA = true;
@KaiserWerk
KaiserWerk / hipchat-notification.php
Created January 10, 2018 10:52
Send notification to HipChat Room
$data = array(
'color' => 'purple',
'message' => 'Buy all ze things! (allthethings)',
'notify' => false,
'message_format' => 'text'
);
$ch = curl_init('https://<your_url>.hipchat.com/v2/room/<room_no>/notification?auth_token=<token>');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
@KaiserWerk
KaiserWerk / check-for-ssl.php
Last active February 6, 2018 15:20
A function for checking the current page call happened via SSL or not
<?php
function isSSL() {
if (isset($_SERVER['HTTPS']) ) {
if (!empty($_SERVER['HTTPS'])) {
return true;
}
if (strtolower($_SERVER['HTTPS'] === "on") { // for IIS
return true;
}
} elseif (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) {
@KaiserWerk
KaiserWerk / slack_message.php
Created April 26, 2018 09:26
Send a message to a slack channel with PHP
<?php
/**
* Sends a Message to a Slack Channel.
*
* @param string $message
* @param string $channel
* @return string A json string
*/
function sendSlackMessage($message, $channel)
@KaiserWerk
KaiserWerk / readme.md
Created April 30, 2018 13:54 — forked from RaVbaker/readme.md
[HOWTO] Rewrite all urls to one index.php in Apache

Redirect All Requests To Index.php Using .htaccess

In one of my pet projects, I redirect all requests to index.php, which then decides what to do with it:

Simple Example

This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to index.php:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

@KaiserWerk
KaiserWerk / DiscordClient.class.php
Created May 4, 2018 12:18
This class lets you send messages to Discord through their webhook service.
<?php
/*
* This class lets you send a message to Discord through a webhook.
*
* Usage:
* $discord = new DiscordClient('URL-FROM-DISCORD-GOES-HERE');
* $discord->name('Optional'); // If not set, uses the name set in Discord
* $discord->avatar('Optional'); // If not set, uses the avatar set in Discord
* $discord->message('Here is where the message can optionally go.'); // If not set, uses the message provided in $this->send()
* $discord->send('Here is where the message can optionally go.');
@KaiserWerk
KaiserWerk / discord-message.php
Last active May 26, 2018 18:42
Send a message to a Discord channel
<?php
$webhookurl = '';
// You can use Markdown
$msg = "Testmessage [https://google.de](<URL>)";
$fields = json_encode(['content' => $msg]);
$ch = curl_init($webhookurl);
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);