Skip to content

Instantly share code, notes, and snippets.

View artoodetoo's full-sized avatar
:octocat:
gitting

Alexandr Materukhin artoodetoo

:octocat:
gitting
View GitHub Profile
@artoodetoo
artoodetoo / basic_auth.php
Created April 24, 2014 06:20
Basic Auth with loop after Cancel and Logout option
<?php
$login = 'user';
$pass = 'userpass';
if (empty($_SERVER['PHP_AUTH_USER']) ||
$_SERVER['PHP_AUTH_USER'] !== $login ||
$_SERVER['PHP_AUTH_PW'] !== $pass) {
header('WWW-Authenticate: Basic realm="Enter user/userpass as login and password"');
header('HTTP/1.0 401 Unauthorized');
@artoodetoo
artoodetoo / gist:0379133f2c9fe88407ff
Created June 19, 2014 05:21
PHP: Find value in array using a user-defined comparison function
<?php
/**
* Искать в массиве с помощью пользовательской функции.
* Поиск идет до первого не false результата.
* @param mixed $needle что ищем
* @param array $haystack где ищем
* @param callable $callback функция сравнения
* @return mixed false или индекс массива
*/
@artoodetoo
artoodetoo / WPAuth.php
Last active August 29, 2015 14:16
Authenticate by WordPress
<?php
class WPAuth
{
private $config;
public function __construct(array $config)
{
$this->config = array_replace(
[
@artoodetoo
artoodetoo / l18n.php
Created March 18, 2015 05:06
Internationalization functions: user preffered language and translate
<?php
function userLanguage(array $preferred = ['en', 'ru'])
{
static $language;
// calculate preferred language and do it only once
if (!isset($language)) {
$language = $preferred[0];
// user has choosen the language
if (isset($_COOKIE['lang']) && in_array($_COOKIE['lang'], $preferred)) {
@artoodetoo
artoodetoo / long_file_proceed.php
Last active August 29, 2015 14:17
Long XML file progress skeleton (CLI)
<?php
/**
* Parse XML item with attributes. Typically dumped table row.
* @param string $buffer XML item
* @return array false| array [name => value, ...]
*/
function parse_row($buffer)
{
$buffer = trim($buffer);
@artoodetoo
artoodetoo / .htaccess
Last active November 11, 2019 11:52
Image thumbnail helper
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/thumb/
RewriteRule ^(.*)$ thumb.php [QSA,L]
</IfModule>
@artoodetoo
artoodetoo / deep_load.php
Created April 17, 2015 09:28
Unlimited dimensions INI file
<?php
/**
* Unroll dot-sequenced keys to new dimension
* @param array $source
* @return array
*/
function deep_load($source)
{
$result = [];
@artoodetoo
artoodetoo / loop.php
Created February 12, 2016 17:33
Start infinite loop PHP from web
<?php
class FileHelper
{
public static function openAndLock($filename)
{
$fh = fopen($filename, 'r+');
if (!$fh || !flock($fh, LOCK_EX)) {
return false;
}
@artoodetoo
artoodetoo / file_get_contents_ex.php
Created February 20, 2016 18:56
Use file_get_contents() with cookies and proxy (optionally with auth)
<?php
function file_get_contents_ex($url, $cookie = null, $proxy = null, $auth = null)
{
$opts = array(
'http' => array(
'method' => 'GET',
'user-agent'=> 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36',
'header' => "Accept-language: en\r\n" .
(!empty($auth) ? "Proxy-Authorization: Basic {$auth}\r\n" : '') .
@artoodetoo
artoodetoo / nogap.php
Last active April 1, 2023 22:19
Efficient Geo IP location in MySQL database
#!/usr/bin/env php
<?php
/*
* Filter to fill the IP gaps in a MaxMind GeoLite tables.
*
* For every missing range in the file it puts a dummy one.
*/
$types = [
'asnum' => [0, 0, 1, "%s,%s,\"-\"\n"],
'blocks' => [2, 0, 1, "\"%s\",\"%s\",\"1\"\n"],