Message reader for StudiVZ - I get always a redirect! Help?

ciever2k

Mitglied
Hallo zusammen, ich wollte mir einen kleinen Nachrichtenleser schreiben, allerdings kriege ich in Zeile 197 / 199 immer einen Redirect auf die Startseite von StudiVZ. Die IDs für die Sicherheitsmechamismen kriege ich ohne Probleme, diese setze ich auch im POST Ajax Request, aber trotzdem will es nicht...

Wer es testen will brauch das ZendFramework, wäre dankbar für ein paar Tips.

Eine Untersuchung mit Wireshark zeigt mir, das die versendeten Header identisch sind.

PHP:
#!/usr/bin/php
$USERNAMEEMAIL = 'YOURSTUDIVZLOGIN';
$PASSWORD = 'YOURPRIVATEPASSWORD';

set_include_path(dirname(__FILE__) . '/../libs/ZendFramework-1.7.2/library' . PATH_SEPARATOR . get_include_path());

require_once("config.inc.php");
require_once("Zend/Loader.php");

spl_autoload_register(array('Zend_Loader', 'autoload'));

/**
 * Checkcode Variablen
 */
$checkcodeName = array( 
	"Search_getFriendlist",
	"Chat_token",
	"Chat_setStatus",
	"Messages_action",
	"Messages_deleteMsg",
	"Messages_history",
	"Messages_replyMessage",
	"Messages_writeMessage",
	"Messages_forwardMessage",
	"Messages_readInboxMessage"
);

/**
 * Print informations of a response.
 * @param $response The response object by request().
 */
function print_response( $response ) {
	if($response->isError()) {
        	echo "Error transmitting data.\n";
        	echo "Server reply was: " . $response->getStatus() .
         	    " " . $response->getMessage() . "\n";
	} else {
	        echo "Status: " . $response->getStatus() . "\n";
	        echo "Message: " . $response->getMessage() . "\n";
	        print_r($response->getHeaders());
	}
}

/**
 * @param $name Name of the input field.
 * @param $string Text which will analyse.
 */
function getHiddenInputValue( $name, $string ) {
	$regex = '/<input type="hidden" name="'.$name.'" value="(.*)"/';
	$anzahl = preg_match( $regex, $string, $treffer );
	return $treffer[1];
}

/**
 * @param $metaName The name of the meta name tag in html.
 * @param $body The complete html source.
 * @return Indexed array of all meta codes.
 * @see $checkcodeName
 */
function getCheckcodes( $body ) {
        $body = str_replace( "\n", "", $body );
        $body = str_replace( "\r", "", $body );

	$codes = array();

	global $checkcodeName;

	foreach( $checkcodeName as $metaName ) {

		$regex = '/<meta name="'.$metaName.'" content="(.*)"/';

		$anzahl = preg_match_all( $regex, $body, $treffer_checkcodes );

		$str = $treffer_checkcodes[1][0];

		$pos = strpos( $str, "\"" );

		echo $metaName . ": " . substr( $str, 0, $pos )."\n";

		$codes = array_merge( $codes, array($metaName => substr( $str, 0, $pos )));
	}

	return $codes;
}

function getCodesWithNames( $codeline ) {
	$assignment = explode( "&", $codeline );
	$codes = array();
	foreach($assignment as $c) {
		$code = explode( "=", $c );
		if( $code[0]{0} == 'a' ) {
			$code[0] = substr($code[0],4);
		}
		$codes = array_merge( $codes, array( $code[0] => $code[1] ));
	}

	return $codes;
}

/**
 * 
 * @param $body The html source of the message site.
 * @return An array of the ids, 0 => message ids, 1 => sender ids.
 */
function getMessageIds( $body ) {
	$body = str_replace( "\n", "", $body );
	$body = str_replace( "\r", "", $body );

	$regex = '/\'Messages.ToggleMessage\', event, this , {\'messageId\':\'\d*\'/';
	$anzahl = preg_match_all( $regex, $body, $treffer_message_ids );

	$treffer_message_ids = $treffer_message_ids[0];

	for( $i=0; $i<count($treffer_message_ids); $i++ ) {
		$l = $treffer_message_ids[$i];
		$l = split( "{", $l );
		$l = $l[1];
		
		$l = explode( ":", $l );
		$l = $l[1];

		$l = substr( $l, 1, strlen($l)-2);

		$treffer_message_ids[$i] = $l;
	}

	$regex = '/\'senderIds\':\'\w*\'/';
	$anzahl = preg_match_all( $regex, $body, $treffer_sender_ids );

	$treffer_sender_ids = $treffer_sender_ids[0];

        for( $i=0; $i<count($treffer_sender_ids); $i++ ) {
		$l = explode( ":", $treffer_sender_ids[$i] );
		$l = $l[1];

		$l = substr( $l, 1, strlen($l)-2);

		$treffer_sender_ids[$i] = $l;
	}

	return array( $treffer_message_ids, $treffer_sender_ids );
}

/**
 * Retrives all informations of a response and packs their in a array.
 * @param $respone The Zend_Http_Response object
 * @return The assoc-array of all informations if the response object.
 */
function splitResponse( $response ) {
	$values = array(
		'headers' => $response->getHeaders(),
		'body' => $response->getBody(),
		'status' => $response->getStatus(),
		'message' => $response->getMessage()
	);
	return $values;
}

/**
 * Retrives all messages of the first message site.
 * @param $messageBody The body text of the first message site.
 * @param $client The Zend_Http_Client object.
 */
function retriveAllMessages( $messageBody, $client ) {
	
	$codes = getCheckcodes( $messageBody );

	$c = getCodesWithNames($codes['Messages_readInboxMessage']);

	$ids = getMessageIds($messageBody);

	for( $i=0; $i<1/*count($ids[0])*/; $i++ ) {
		$messageid = $ids[0][$i];
		$senderid  = $ids[1][$i];

		echo "message id: " . $messageid . "\n";
		echo "sender id:  " . $senderid . "\n";

		$client->setUri("http://www.studivz.net/Ajax");
		$params = array(
			//'checkcode'	=> $c['checkcode'],
			'checkcode'	=> 'undefined',
			'formkey'	=> $c['formkey'],
			'iv'		=> $c['iv'],
			'messageId'	=> $messageid
		);

		print_r($params);

		$client->setParameterPost($params);
		$response = $client->request('POST');
		$responseInfo = splitResponse($response);
	
		//echo $responseInfo['body'];	
	}

}

/********************
 * Start
 ********************/

$client = new Zend_Http_Client('http://www.studivz.net', array(
	'useragent'		=> $sys['useragent'],
	'timeout'		=> $sys['timeout'],
	'httpversion'		=> $sys['httpversion']
));
$client->setCookieJar();

$response = $client->request();

$responseInfo = splitResponse($response);

$hiddenNames = array( "formkey", "iv", "checkcode" );
$hiddenIds = array();

foreach( $hiddenNames as $name ) {
        $hiddenIds[$name] = getHiddenInputValue( $name, $responseInfo['body'] );
}

/**
 * Login 
 */
$params = array(
	'email'		=> $USERNAMEEMAIL,
	'password'	=> $PASSWORD,
 	'login'		=> 'Einloggen',
	'jsEnabled'	=> 'true',
        'formkey'       => $hiddenIds['formkey'],
        'iv'            => $hiddenIds['iv'],
        'checkcode'     => $hiddenIds['checkcode']
);

$client->setUri("https://secure.studivz.net/Login");
$client->setParameterPost($params);

$response = $client->request('POST');

$header = $response->getHeaders();
$body = $response->getBody();


/** 
 * Messages
 */
$client->setUri("http://www.studivz.net/Messages/tid/106");
$client->setHeaders(array(
	'Accept-encoding' 	=> '',
	'Accept-Language'	=> 'de-de,de;q=0.8,en-us;q=0.5,en;q=0.3',
	'Accept' 		=> 'application/json, text/javascript, */*',
	'Accept-Charset' 	=> 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
	'Keep-Alive' 		=> '300',
	'Connection' 		=> 'keep-alive',
	'Content-Type' 		=> 'application/x-www-form-urlencoded; charset=UTF-8',
	'X-Request-With' 	=> 'XMLHttpRequest',
	'Referer' 		=> 'http://www.studivz.net/Messages/tid/106',
	'Pragma' 		=> 'no-cache',
	'Cache-Control' 	=> 'no-cache',
));

$response = $client->request();

$responseInfo = splitResponse($response);

$messageSite = $responseInfo['body']; 

retriveAllMessages($messageSite, $client);

?>
 
Vielen Dank Maik. Nur ist jetzt Zeile 197 das Ende der Funktion retriveAllMessages und 199 ein Kommentar, ich frage mich, wie da ein Redirect entstehen kann...
 
Zurück