Skip to content

Instantly share code, notes, and snippets.

View ameenross's full-sized avatar

Ameen Ross ameenross

View GitHub Profile
@ameenross
ameenross / .config_autostart_lockScreen.sh.desktop
Last active June 3, 2022 13:29
Log Gnome login/logout and lock screen events
[Desktop Entry]
Type=Application
Exec=./lockScreen.sh lock_screen.log
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en]=Log lock screen events
Name=Log lock screen events
Comment[en]=
Comment=
@ameenross
ameenross / check_regex.php
Last active April 29, 2021 10:34 — forked from smichaelsen/check_regex.php
PHP: Check if string is valid regular expression
<?php
/**
* @param string $regex
* @return bool
*/
function isValidRegularExpression($regex) {
set_error_handler(function() {}, E_WARNING);
$valid = preg_match($regex, null) !== FALSE;
restore_error_handler();
@ameenross
ameenross / switch.php
Last active March 15, 2021 14:15
Switch syntax
<?php
/**
* This is for everyone who hates the indenting behavior of switch/case.
*
* If using PSR-2 and you're getting this error:
* PSR2.ControlStructures.SwitchDeclaration.WrongOpenercase
* @see https://github.com/squizlabs/PHP_CodeSniffer/issues/3016
*/
$var = 'foo';
@ameenross
ameenross / parseFtpRawlist.php
Created February 26, 2020 13:19
ftp_rawlist result parse function
<?php
/**
* @param string $row
* @return array
* An assoc with the following properties:
* - name
* - type: one of directory, file, link, unknown
* - size
* - owner
@ameenross
ameenross / AppServiceProvider.php
Last active October 23, 2021 21:41
Laravel "forbidden" validation: fail validation when an attribute is in the request
<?php
class AppServiceProvider
{
public function boot()
{
// The closure will only be called if the attribute is in the request.
Validator::extend('forbidden', function ($attribute, $value, $parameters) {
// Always return false.
return false;
@ameenross
ameenross / favicon.ico
Last active March 12, 2019 13:45
Got 404? No more! Place this file in your webroot, and your application will no longer waste CPU cycles to handle this request, log the 404 etc. The webserver will just respond with an HTTP 200 and serve the file.
It's time for user agents to stop requesting this file.
- https://www.w3.org/2005/10/howto-favicon
- https://blog.whatwg.org/the-road-to-html-5-link-relations#rel-icon
@ameenross
ameenross / imagick-fonts.php
Last active March 4, 2019 10:45
Just output a table with fonts and their appearance in PHP Imagick. Inspired by Stefano: http://php.net/manual/en/imagick.queryfonts.php#121010
<?php
foreach (Imagick::queryFonts() as $fontName) {
$image = new Imagick;
$draw = new ImagickDraw;
$draw->setGravity(Imagick::GRAVITY_CENTER);
$draw->setFont($fontName);
$draw->setFontSize(12);
$draw->setFillColor('black');
$image->newImage(300, 20, 'lightblue');
@ameenross
ameenross / suppress-warning-from-count-on-non-countable.php
Last active December 20, 2018 13:48
PHP 7.2 suppress warning from count
<?php
/**
* This is an alternative way of handling warnings from calling count() on
* non-countables. It's just an error handler that drops the warning, and calls
* the previous error handler for everything else (if there is one).
*/
$previous = set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use (&$previous) {
// Do nothing with an E_WARNING from count.
<?php
/**
* Some PHP frameworks are sensitive to non-fatal PHP errors (even notices).
* Those same frameworks are strict about other things, like typing, and only
* use strict comparisons. On the other hand, they use count() on scalars
* without doing a type check, and rely on the bogus return value of `1`.
* The PHP devs considered this legacy behavior of PHP's count (to return `1`
* for scalars) a problem, and relying on it a BUG. So in PHP 7.2, the function
* still returns the same values, but it now triggers an E_WARNING.
* Since those frameworks use count on scalars in various places, now PHP
@ameenross
ameenross / php-serenata.sh
Last active August 23, 2018 08:50
PHP proxy shell script
#!/bin/bash
php -n \
-d "extension=mbstring.so" \
-d "extension=pdo.so" \
-d "extension=pdo_sqlite.so" \
-d "extension=json.so" \
-d "extension=phar.so" \
-d "extension=tokenizer.so" \
-d "extension=xml.so" \