Skip to content

Instantly share code, notes, and snippets.

@gwarnants
gwarnants / is_base64.php
Last active July 9, 2024 08:18
Check if a string seems to be base64 encoded
<?php
/**
* @param string $str
* @return bool
*/
function is_base64($str)
{
return (bool)preg_match('`^[a-zA-Z0-9+/]+={0,2}$`', $str);
}
@gwarnants
gwarnants / httpd.conf
Last active August 29, 2015 14:02
Cross version Apache access control
<Directory "/var/web/www/">
# use mod_authz_host since Apache 2.4
<IfDefine APACHE24>
Require local
</IfDefine>
# use mod_access before 2.4
<IfDefine !APACHE24>
Order Deny,Allow
@gwarnants
gwarnants / .htaccess
Last active February 24, 2016 12:42
Hijack PHP easter eggs using .htaccess. This could be an alternative solution when no possibility to disable expose_php in php.ini
RewriteEngine On
RewriteCond %{QUERY_STRING} ^=PHPE9568F3[4-6]-D428-11d2-A769-00AA001ACF42$ [OR]
RewriteCond %{QUERY_STRING} ^=PHPB8B5F2A0-3C92-11d3-A3A9-4C7B08C10000$
RewriteRule (.*) - [F]
@gwarnants
gwarnants / gist:64c13183000e0127472b
Last active February 11, 2016 14:08
Drupal admin menu shortcut
<?php
/**
* Implements hook_menu().
*/
function foobar_menu() {
$items['admin/add-image-style'] = array(
'title' => 'Shortcut to add image style',
'page callback' => 'drupal_goto',
'page arguments' => array('/admin/config/media/image-styles/add'),
@gwarnants
gwarnants / composer.json
Last active March 18, 2019 14:47
Auto-configure PHP_CodeSniffer settings directly from composer.json
{
"require-dev": {
"squizlabs/php_codesniffer": "2.*"
},
"scripts": {
"phpcs-setup": [
"\"vendor/bin/phpcs\" --config-set default_standard ruleset.xml",
"\"vendor/bin/phpcs\" --config-set colors 1",
"\"vendor/bin/phpcs\" --config-set show_progress 1",
"\"vendor/bin/phpcs\" --config-set report_width 120",
@gwarnants
gwarnants / .htaccess
Created December 10, 2015 13:11
Sample rewrite rule depending on server date/time
# more info : http://httpd.apache.org/docs/current/en/expr.html#vars
# deny access from monday to friday and between 9am to 6pm
RewriteCond %{TIME_WDAY} ^1|2|3|4|5$
RewriteCond %{TIME_HOUR} ^0?9|10|11|12|13|14|15|16|17|18$
RewriteRule ^nsfw\.html$ - [L]
@gwarnants
gwarnants / gist:59e7d0707c75b95ab012
Created February 11, 2016 14:00
Parse XML with namespaces using SimpleXML
<?php
$xml = <<<ENDXML
<catalog xmlns:ns1="http://localhost/ns1" xmlns:ns2="http://localhost/ns2">
<product id="1" ns2:order="2">
<name>Test</name>
<ns1:price>100</ns1:price>
</product>
</catalog>
ENDXML;
@gwarnants
gwarnants / array_linearize.php
Created March 4, 2016 11:24
Flatten a multidimensional array to a single dimensional array by joining nested keys together
<?php
/**
* Flatten a multidimensional array to a single dimensional array by joining nested keys together
*
* @param array $array The array to flatten
* @param string $glue Glue string to join keys together
* @return array
*/
function array_linearize(array $array, $glue = '/')
{
@gwarnants
gwarnants / template.php
Last active August 4, 2016 13:49
Add HTTP status header as a body class
<?php
/**
* Implements hook_preprocess_html().
*/
function <theme>_preprocess_html(&$variables)
{
$header = drupal_get_http_header('status');
if ($header && preg_match('`^[0-9]+`', $header, $matches)) {
switch ($matches[0]) {
@gwarnants
gwarnants / template.php
Created August 5, 2016 08:43
Disable pages indexation in Drupal (meta robots=noindex)
<?php
/**
* Implements hook_preprocess_html().
*/
function <theme>_preprocess_html(&$vars)
{
$meta = array(
'#tag' => 'meta',
'#attributes' => array(