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 / 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 / result.txt
Created January 21, 2018 11:46
Flatten PHP array
C:\>php flatten.php
array(4) {
[0]=>
int(11)
[1]=>
int(12)
[2]=>
int(13)
[3]=>
int(14)
@artoodetoo
artoodetoo / readme.txt
Last active May 23, 2018 23:11
DST problem demo
For timezone 'Australia/Melbourne':
2018-03-31 10:00:00
+ 24*60*60 == 2018-04-01 09:00:00
strtotime(+1 day) == 2018-04-01 10:00:00
DateTime->add(1d) == 2018-04-01 10:00:00
2018-10-06 10:00:00
+ 24*60*60 == 2018-10-07 11:00:00
strtotime(+1 day) == 2018-10-07 10:00:00
DateTime->add(1d) == 2018-10-07 10:00:00
@artoodetoo
artoodetoo / app.js
Last active January 20, 2019 10:12
Example webpack config to concat jQuery plugins but use external jQuery instance
window.moment = require('moment');
require('daterangepicker');
require('bootstrap-select');
@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;
}