Category Archives: PHP

WordPress upload add another file extensions

Just add the following code to you functions.php in theme:

add_filter('upload_mimes', 'custom_upload_mimes');
function custom_upload_mimes ( $existing_mimes=array() ) {
  // add
  $existing_mimes['xpi'] = 'application/x-xpi';
  $existing_mimes['zip'] = 'application/x-zip';
  $existing_mimes['bat'] = 'application/x-bat';

  // remove
  unset( $existing_mimes['exe'] );

  return $existing_mimes;
}

Thanx to http://howto.blbosti.com/2010/03/wordpress-modify-the-upload-list-of-allowed-file-types/

PHP

Eclipse PHP (PDT) Syntax Formatter


Simple and well-formed php code formatter you may use:
http://balakhonov-yuriy.ru/php-formatter-for-eclipse/

Just copy file:

http://balakhonov-yuriy.ru/demo/va000137.pdt.tools.formatter_0.92.4.v20081027.jar

to Eclipse/plugins folder, restart Eclipse and configure PHP->Code Style->Formatter.

Greate plugin! Thanx 2 author!

PHP

Singleton design pattern in PHP


When you need a single instance of some class, use pattern:

class MySingleton
{
	static private $instance = null;

	static public function createInstance($args = null)
	{
		if (self::$instance == null)
			self::$instance = new self($args);

		return self::$instance;
	}
	static public function destroyInstance()
	{
		return (self::$instance = null);
	}

	protected function __construct($args = null)
	{
		// your code...
	}
	protected function __clone()
	{
	}
}

Also, can be useful What is new in PHP 5.3 – part 2: late static binding

PHP

Convert windows-1251 to utf8 encoding

function convertCp1251ToUtf8($str)
{
	static $table = array(
		"\xA8" => "\xD0\x81",
		"\xB8" => "\xD1\x91",
		"\xA1" => "\xD0\x8E",
		"\xA2" => "\xD1\x9E",
		"\xAA" => "\xD0\x84",
		"\xAF" => "\xD0\x87",
		"\xB2" => "\xD0\x86",
		"\xB3" => "\xD1\x96",
		"\xBA" => "\xD1\x94",
		"\xBF" => "\xD1\x97",
		"\x8C" => "\xD3\x90",
		"\x8D" => "\xD3\x96",
		"\x8E" => "\xD2\xAA",
		"\x8F" => "\xD3\xB2",
		"\x9C" => "\xD3\x91",
		"\x9D" => "\xD3\x97",
		"\x9E" => "\xD2\xAB",
		"\x9F" => "\xD3\xB3",
	);
	
	return preg_replace('#[\x80-\xFF]#se',
	' "$0" >= "\xF0" ? "\xD1".chr(ord("$0")-0x70) :
					   ("$0" >= "\xC0" ? "\xD0".chr(ord("$0")-0x30) :
						(isset($table["$0"]) ? $table["$0"] : "")
					   )',
	$str
	);
}

Special thanx to http://webkev.com/2011/03/01/konvertiruem-windows-1251-v-utf-8-s-pomoshhyu-php/

PHP

Shift cookies in current context

function  ShiftCookies($strHeaders)
{
	// Parse cookies
	$aCookies = ParseCookies($strHeaders);
	
	// Shift cookies in current context
	foreach ($aCookies as $cookie)
		@setrawcookie($cookie['name'], $cookie['value'], $cookie['expires'], $cookie['path'], $cookie['domain']);
}

ParseCookies is a function from post http://gik.firetrot.com/index.php/2011/08/03/parse-cookies-from-header/

PHP

Parse cookies from Header

If you want to get cookies from a header, use the following:

function ParseCookies($strHeaders)
{
	$result = array();
	
	if (!empty($strHeaders))
	{
		$aHeaders = explode("\n", trim($strHeaders));
		$strCookieStartLine = 'Set-Cookie:';
		
		foreach ($aHeaders as $line)
		{
			if (substr($line, 0, strlen($strCookieStartLine)) === $strCookieStartLine)
			{
				$aTmp = array();
				
				$aPairs = explode(';', trim(str_replace($strCookieStartLine, '', $line)));
				foreach ($aPairs as $pair)
				{
					$aKeyValues = explode('=', trim($pair), 2);
					if (count($aKeyValues) == 2)
					{
						switch ($aKeyValues[0])
						{
							case 'path':
							case 'domain':
								$aTmp[trim($aKeyValues[0])] = urldecode(trim($aKeyValues[1]));
								break;
							case 'expires':
								$aTmp[trim($aKeyValues[0])] = strtotime(urldecode(trim($aKeyValues[1])));
								break;
							default:
								$aTmp['name'] = trim($aKeyValues[0]);
								$aTmp['value'] = trim($aKeyValues[1]);
								break;
						}
					}
				}
				
				$result[] = $aTmp;
			}
		}
	}
	
	return $result;
}

This is author realization. Post a feedback if errors occurs.

PHP

Post Request in PHP


Today we post a request!

function PostRequest($url, $referer, $headersAddon, $_data)
{
	// convert variables array to string:
	$data = array();    
	while( list($n,$v) = each($_data) ) { $data[] = "$n=$v"; }
	$data = implode('&', $data);
	// format --> test1=a&test2=b etc.
	
	// parse the given URL
	$url = parse_url($url);
	if ($url['scheme'] != 'http') { die('Only HTTP request are supported !'); }
	
	// extract host and path:
	$host = $url['host'];
	$path = $url['path'];
	
	// open a socket connection on port 80
	$fp = fsockopen($host, 80);
	
	// send the request headers:
	fputs($fp, "POST $path HTTP/1.1\r\n");
	fputs($fp, "Host: $host\r\n");
	fputs($fp, "Referer: $referer\r\n");
	if ($headersAddon)
		fputs($fp, $headersAddon . "\r\n");
	fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
	fputs($fp, "Content-length: ". strlen($data) ."\r\n");
	fputs($fp, "Connection: close\r\n\r\n");
	fputs($fp, $data);
	
	$result = ''; 
	while(!feof($fp))
	{
		// receive the results of the request
		$result .= fgets($fp, 128);
	}
	
	// close the socket connection:
	fclose($fp);
	
	// split the result header from the content
	$result = explode("\r\n\r\n", $result, 2);
	
	$header = isset($result[0]) ? $result[0] : '';
	$content = isset($result[1]) ? $result[1] : '';
	
	// return as array:
	return array($header, $content);
}

The code is absolutely worked.

Snipet linked from: http://www.jonasjohn.de/snippets/php/post-request.htm