xmpphp 的一些补丁

xmpphp对于注册新用户,添加好友等功能暂时还没有,以下是我在网上找到的补丁:

<?php

	public function registerNewUser($username, $password = NULL, $email='') {
		if (!isset ($password))
			$password = $this->genRandomString(15);

		$id = 'reg_' . $this->getID();
		$xml = "<iq type='set' id='$id'>
				                <query xmlns='jabber:iq:register'>
				                    <username>" . $username . "</username>
				                    <password>" . $password . "</password>
				                    <email>$email</email>
				                    <name></name>
				                </query>
				            </iq>";
		$this->addIdHandler($id, 'register_new_user_handler');
		$this->send($xml);
	}

	/**
	* Handler for new user registration
	*
	* @param XML Object $xml
	*/
	public function register_new_user_handler($xml) {
		//dpm($xml);
		//var_dump($xml);
		switch ($xml->attrs['type']) {
			case 'error' :
				$this->event('new_user_registered', 'error');
				break;
			case 'result' :
				// dpm($xml->subs);
				$query = $xml->sub('query');
				$username = '';
				$password = '';
				if (!$query->subs){
					break;
				}
				if (!is_array($query->subs)) {
					foreach ($query->subs as $key => $value) {
						switch ($value->name) {
							case 'username' :
								$username = $value->data;
								break;

							case 'password' :
								$password = $value->data;
								break;
						}
					}
				}
				$this->event('new_user_registered', array (
					'jid' => $username . "@{$this->server}",
					'password' => $password
				));
			default :
				$this->event('new_user_registered', 'default');
		}
	}

	/**
	 * Add contact to your roster
	 */
	public function addRosterContact($jid, $name, $groups = array ()) {
		// return if there is no jid specified
		if (!$jid)
			return;
		// set name to the jid if none is specified
		if (!$name) {
			$name = $jid;
		}
		$id = $this->getID();
		$xml = "<iq type='set' id='$id'>";
		$xml .= "<query xmlns='jabber:iq:roster'>";
		$xml .= "<item jid='$jid' name='$name'>";
		foreach ($groups as $group) {
			$xml .= "<group>$group</group>";
		}
		$xml .= "</item>";
		$xml .= "</query>";
		$xml .= "</iq>";
		$xml .= "<presence to='" . $jid . "' type='subscribe'/>";
		$this->addIdHandler($id, 'add_roster_contact_handler');
		$this->send($xml);
	}

	/**
	* @param XML Object $xml
	*/
	protected function add_roster_contact_handler($xml) {
		// do anything you wish here
		$this->event('contact_added');
	}

	/**
	*
	* @param $jid
	*       Contact you wish to remove
	*/

	public function deleteRosterContact($jid) {
		$id = $this->getID();
		$xml = "<iq type='set' id='$id'>";
		$xml .= "<query xmlns='jabber:iq:roster'>";
		$xml .= "<item jid='" . $jid . "' subscription='remove' />";
		$xml .= "</query>";
		$xml .= "</iq>";
		$this->addIdHandler($id, 'delete_roster_contact_handler');
		$this->send($xml);
	}

	/**
	*
	* @param XML Object $xml
	*/
	protected function delete_roster_contact_handler($xml) {
		// do any handling you wish here
		$this->event('contact_removed');
	}

?>

注册用户需要先通过一个已有账户登陆(admin账户最保险)再调用registerNewUser.

 


参见:

http://hi.baidu.com/txjlrk/blog/item/ff3edc0c3eca28326059f3a7.html

http://anandafit.info/2011/05/10/register-new-xmpp-users-with-xmpphp/

http://code.google.com/p/xmpphp/issues/detail?id=64


Total views.

© 2013 - 2018. All rights reserved.

Powered by Hydejack v6.6.1