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 / 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 / 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 / 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 / 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 / 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');