2015/02/25

How to clear file name from disallowed characters on Windows

// Clears given filename from disallowed characters on Windows: https://msdn.microsoft.com/en-us/library/aa365247
function clearFileName($fileName){
$fileName = str_replace("<", "", $fileName);
$fileName = str_replace(">", "", $fileName);
$fileName = str_replace(":", "", $fileName);
$fileName = str_replace("/", "", $fileName);
$fileName = str_replace("\\", "", $fileName);
$fileName = str_replace("|", "", $fileName);
$fileName = str_replace("?", "", $fileName);
$fileName = str_replace("*", "", $fileName);
return $fileName;
}
view raw gistfile1.php hosted with ❤ by GitHub

2015/02/24

How to perform login to onet.pl using curl php ?

Download this library
https://github.com/php-curl-class/php-curl-class
It may need some changes so you can download this working lib
https://drive.google.com/file/d/0Bw4oKsVETs62ZEdTNzJqcV9OVnM/view?usp=sharing
version with appropriate changes made by me and check below example:

public static function curlOnetLogin(){
$url = 'https://konto.onet.pl/login.html?app_id=poczta.onet.pl.front';
$login = 'YOUR_LOGIN@onet.pl';
$password = 'YOUR_PASSWORD';
$f = fopen('onet_request.txt', 'a+');
fwrite($f, "\n==========\n[".date("Y-m-d H:i:s", time())."]\n");
// open login page and save cookies
$curl = new curl();
$cookieName = dirname(__FILE__).'/onetCookie.txt';
$curl -> setCookieFile($cookieName);
$curl -> setOpt(CURLOPT_COOKIEFILE, $cookieName);
$curl -> setOpt(CURLOPT_STDERR, $f);
$curl -> setOpt(CURLOPT_VERBOSE, true);
$curl -> setOpt(CURLOPT_SSL_VERIFYPEER, false);
$curl -> setOpt(CURLOPT_SSL_VERIFYHOST, false);
$curl -> setOpt(CURLOPT_FOLLOWLOCATION, true);
$curl -> setOpt(CURLOPT_RETURNTRANSFER, true);
$curl -> setOpt(CURLOPT_TIMEOUT, 30);
$curl -> setOpt(CURLOPT_URL, $url);
$pageContent = $curl -> exec();
while($curl -> response_headers['Location']){
$curl -> setOpt(CURLOPT_URL, $curl -> response_headers['Location']);
$curl -> exec();
}
// perform login
$curl -> setOpt(CURLOPT_POST, true);
$curl -> setOpt(CURLOPT_URL, $url);
$curl -> setOpt(CURLOPT_POSTFIELDS, http_build_query( array(
'noscript' => true,
'login' => $login,
'password' => $password,
'perm' => 0,
'perm' => 1,
'provider' => "",
'access_token' => "",
'referrer' => "",
'cookie' => 'onet_ubi, onetzuo_ticket, onet_cid, __gfp_64b, onet_cinf, onet_sid, __utma, __utmc, __utmz, __utma, __utmc, __utmz, onet_uoi, onet_crt_adtech',
'script' => 226,
'adblock' => 1
), '', '&'));
$pageContent = $curl -> exec();
fclose($f);
return $pageContent;
}
view raw gistfile1.php hosted with ❤ by GitHub

2015/02/04

Email validation with regulara expressions in C#

static string cutEmail(string str)
{
string email = null;
Regex rx = new Regex(@"(?<email>[\w!#$%&'*+\-/=?\^_`{|}~]+(\.[\w!#$%&'*+\-/=?\^_`{|}~]+)*@((([\-\w]+\.)+[a-zA-Z]{2,4})|(([0-9]{1,3}\.){3}[0-9]{1,3})))", RegexOptions.None);
Match match = rx.Match(str);
if (match.Success)
email = match.Groups["email"].Value;
return email;
}
view raw gistfile1.cs hosted with ❤ by GitHub