Initial commit
[webchat.git] / xmpphp / chat_backend.php
blob5aded787e7f711dbe3954ed86f020b2ae5752f24
1 <?php
2 // This script connects to a jabberserver via bosh and send messages
3 // to a participant and recieves messages and relays to it the
4 // webchat user.
6 // This takes the following arguments:
7 // * op
8 // Tell the bridge what to do. Can have the following parameters:
9 // * InitConnection
10 // Initialise the connection
11 // * GetEvents
12 // Wait till we recieve an event
13 // * SendMessage
14 // Send a message to the other side
15 // * Disconnect
16 // Disconnects the session
17 // * message
18 // If op is SendMessage contains the message to be send
20 // This Script returns JSON containing the following parameters:
21 // * status:
22 // If greater than 0: some error happend
23 // If equals to 0: everything fine
24 // * message
25 // Containts chat messages or presence informations
26 // * error_msg
27 // A errorstring to display the user
29 ini_set('display_errors','true');
31 error_reporting(E_ALL & E_STRICT);
33 require_once('FirePHPCore/fb.php');
35 function handleConnection($conn) {
36 try {
37 $conn->connect('http://localhost:5280/http-bind', 1, true);
38 $payloads = $conn->processUntil(array('message', 'presence', 'end_stream', 'session_start'),3);
40 $conn->presence($status="Cheese!");
41 if ((is_array($payloads)) && (count($payloads) > 0)) {
42 foreach($payloads as $event) {
43 $pl = $event[1];
44 switch($event[0]) {
45 case 'message':
46 $retval = array('status' => 0, 'message' => $pl['body'], 'nick' => $pl['from']);
47 break;
48 case 'presence':
49 $retval = array('status' => 0, 'message' => sprintf('%s: %s', $pl['show'], $pl['status']));
50 break;
51 case 'session_start':
52 $conn->getRoster();
53 $conn->sendVCard(array('nickname' => $_SESSION['chat_nick']));
54 $conn->presence($status="Cheese!");
55 $retval = array('status' => 0, 'message' => 'Connection established');
56 break;
57 default:
58 // got nothing, we need to return something, so the connection stays alive.
59 $retval = array('status' => -1, 'message' => '');
60 break;
62 if (is_array($retval)) {
63 echo json_encode($retval);
64 } else {
65 // got nothing, we need to return something, so the connection stays alive.
66 echo json_encode(array('status' => -1, 'message' => ''));
69 } else {
70 // got nothing, we need to return something, so the connection stays alive.
71 echo json_encode(array('status' => -1, 'message' => ''));
73 } catch(XMPPHP_Exception $e) {
74 echo json_encode(array('status' => 1, 'message' => sprintf('Connection error: %s', $e->getMessage())));
77 $conn->saveSession();
82 $now = microtime(true);
83 /* Settings */
84 $_SETTINGS['to'] = 'tim@boese-ban.de';
85 session_start();
86 $valid_ops = array('InitConnection', 'GetEvents', 'SendMessage', 'Disconnect');
87 $op = $_GET['op'];
88 if (in_array($op,$valid_ops)) {
89 // activate full error reporting
90 error_reporting(E_ALL & E_STRICT);
92 include 'XMPPHP/BOSH.php';
94 switch($op) {
95 case 'InitConnection':
96 $_SESSION = array();
97 $_SESSION['chat_nick'] = $_POST['nick'];
98 $conn = new XMPPHP_BOSH('localhost', 5222, $_SESSION['chat_nick'], '', 'xmpphp', 'localhost');
99 $conn->autoSubscribe();
101 handleConnection($conn);
102 break;
103 case 'GetEvents':
104 $conn = new XMPPHP_BOSH('localhost', 5222, $_SESSION['chat_nick'], '', 'xmpphp', 'localhost');
105 $conn->autoSubscribe();
106 handleConnection($conn);
107 break;
108 case 'SendMessage':
109 $message = $_GET['message'];
110 $conn = new XMPPHP_BOSH('localhost', 5222, $_SESSION['chat_nick'], '', 'xmpphp', 'localhost');
111 $conn->autoSubscribe();
112 try {
113 //printf("9: %f\n", microtime(true) - $now);
114 $conn->connect('http://localhost:5280/http-bind', 1, true);
115 //printf("10: %f\n", microtime(true) - $now);
116 $conn->message($_SETTINGS['to'], $message);
117 //printf("11: %f\n", microtime(true) - $now);
118 echo json_encode(array('status' => 0, 'message' => 'OK'));
119 } catch(XMPPHP_Exception $e) {
120 echo json_encode(array('status' => 1, 'message' => sprintf('Connection error: %s', $e->getMessage())));
121 die();
123 break;