\service.php
  1 <?php
  2     require_once("serviceconfiguration.php");
  3 ?>
  4 <?php
  5 
  6     /**
  7      * !!!!!!!!
  8      * Please do not access variables and properties
  9      * from classes "Client", "Stream", "ChatRoom" directly.
 10      * Use setters, getters and implemented public functions.
 11      */
 12 
 13      //set the encoding of this php file script
 14      //Configuration::$LOCAL_ENCODING = "cp1250";
 15 
 16 
 17     /**
 18      * OnApplicationStarted event is triggered when Radical Bridge
 19      * application is loaded into the stream server memory.
 20      */
 21     function _onApplicationStarted()
 22     {
 23         Logger::func("onApplictionStarted");
 24     }
 25 
 26 
 27 
 28 
 29 
 30     /**
 31      * OnApplicationStopped event is triggered a few moments before
 32      * Radical Bridge application is unloaded from the stream server memory.
 33      * Typically it is just before the stream server is properly terminated,
 34      * or also in case that there are no connected clients for a long time.
 35      * WARNING: onApplicationStopped is not triggered if the stream server crashes down.
 36      */
 37     function _onApplicationStopped()
 38     {
 39         Logger::func("onApplicationStopped");
 40     }
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49     /**
 50      * OnClientWantsToConnect event is triggered when a client attempts
 51      * to connect to the Radical Bridge (before he/she is assigned into
 52      * the chat room). Developers should always implement this function.
 53      * “AcceptConnection” or “RejectConnection” method must be called
 54      * somewhere in the body of this function.
 55      *
 56      * @param Client $client - client who wants to connect to the system
 57      */
 58     function _onClientWantsToConnect(&$client)
 59     {
 60         Logger::func("onClientWantsToConnect");
 61         //accept client's connection
 62         $client->AcceptConnection();
 63         Logger::info("User ".$client->getUID()." wants to connect.");
 64         //set client nick name
 65         $client->setNickName($client->getUID());
 66         $BR = chr(13).chr(10);
 67         //send room message
 68         $client->SendRoomMessage(
 69                 "Admin", "000000", "User \"" . $client->getNickName() .
 70                 "\" has entered the room.");
 71         //send private message about the chat
 72         $client->SendPrivateMessage(
 73                 "Admin", $client->getUID(), "000000",
 74                 "This chat is written in Radical Chat Framework for PHP - ".
 75                 "the first available solution which enables developers ".
 76                 "to write flash videoconferencing applications entirely ".
 77                 "in PHP language without any further knowledge about ".
 78                 "Flash, Actionscript, RTMP protocol or stream server API. ".
 79                 "The framework unique design speeds up the video chat ".
 80                 "development process from 3-6 months to 2-4 days.".$BR.
 81                 "The design of this application could be altered by editing ".
 82                 "the XML configuration file on ".
 83                 "{{FONT COLOR='#0000FF'}}{{A HREF='config.xml' ".
 84                 "TARGET='_blank'}}./config.xml{{/A}}{{/FONT}}".$BR.
 85                 "The chat rules could be altered by editing ".
 86                 "{{FONT COLOR='#0000FF'}}{{A HREF='./service/service.php' ".
 87                 "TARGET='_blank'}}".
 88                 "./service/service.php file.".
 89                 "{{/A}}{{/FONT}}".$BR.
 90                 "If you want more information, please see ".
 91                 "{{FONT COLOR='#0000FF'}}".
 92                 "{{A HREF='http://en.cze.cz/Radical-Flash-Chat/' ".
 93                 "TARGET='_blank'}}".
 94                 "http://en.cze.cz/Radical-Flash-Chat/{{/A}}{{/FONT}}".$BR.$BR);
 95     }
 96 
 97 
 98 
 99 
100 
101 
102 
103 
104 
105     /**
106      * OnClientConnectionWasAccepted event comes after onClientWantsToConnect,
107      * but only if a client was accepted by “AcceptConnection” method. Usually
108      * it is not necessary to implement the body of this function.
109      *
110      * @param Client $client client who was accepted and entered
111      * the chat successfully
112      */
113     function _onClientConnectionWasAccepted(&$client)
114     {
115         Logger::func("onClientConnectionWasAccepted");
116     }
117 
118 
119 
120 
121 
122 
123 
124 
125 
126     /**
127      * OnClientConnectionWasRejected event comes after onClientWantsToConnect
128      * If the client was fired (kicked out) by “RejectConnection” method.
129      * Usually it is not necessary to implement the body of this function.
130      *
131      * @param Client $client handler of the client which connection
132      * was rejected in the onClientWantsToConnect event
133      */
134     function _onClientConnectionWasRejected($client)
135     {
136         Logger::func("onClientConnectionWasRejected");
137     }
138 
139 
140 
141 
142 
143 
144 
145 
146     /**
147      * OnClientDisconnected event occurs when the client is disconnected
148      * by “RejectConnection” or if a connection is somehow terminated,
149      * for example because of network malfunction or because the client
150      * turned off the computer or closed the web browser.
151      *
152      * @param Client $client handler of the client who has disconnected
153      * himself from the server. This parameter is not transfered back
154      * to the stream server.
155      * @param ChatRoom $room handler to the ChatRoom, where client
156      * belonged before he disconnected himself. This parameter is
157      * transfered back to the stream server.
158      */
159     function _onClientDisconnected($client, &$room = null)
160     {
161         //WARNING room can be null if user was rejected
162         //        before he was assigned into the room
163 
164         Logger::func("onClientDisconnected");
165         Logger::info("User ".$client->getUID()." is disconnected.");
166         //send the message that client left the room
167         $room->SendRoomMessage("Admin","000000","User " .
168                                $client->getNickName() . " left the room");
169     }
170 
171 
172 
173 
174 
175 
176 
177 
178     /**
179      * OnClientWantsToStartStreaming event is triggered every time when any
180      * client wants to publish live audio or video stream.
181      * Mostly it is a live video from a web camera.
182      *
183      * @param Client $client handler of the client who wants to publish
184      * the stream
185      * @param Stream $stream stream descriptor
186      */
187     function _onClientWantsToStartStreaming(&$client, $stream)
188     {
189         Logger::func("onClientWantsToStartStreaming");
190 
191         //deny streaming by default
192         $client->setStreamingRejected();
193 
194         //initialize mutex for transaction operation
195         $mutexName = "Mutex".$client->getRoomUID();
196         $mutex = new Mutex($mutexName);
197         //try to begin transaction on persistent object
198         //Radical Chat persistent object is a temporary object
199         // which is not stored in any database
200         $mutex->lock();
201         {
202             //open persistent object
203             $po = new PersistentObject($mutexName);
204             //test if persistent object has been setted or if it is necessary
205             //to set it right now
206             if(!isset($po->vars["WhoIsStreaming"]))
207             {
208                 $po->vars["WhoIsStreaming"] = "noone";
209                 $po->vars["verified"] = time();
210                 $po->save();
211             }
212 
213             //test if there is anyone else who is streaming
214             if(($po->vars["WhoIsStreaming"]=="noone") ||
215                (time() - $po->vars["verified"] > 3))
216             {
217                 //if there is noone else
218                 
219                 //accept the streaming
220                 $client->setStreamingAccepted();
221                 //show information about the stream
222                 $client->SendPrivateMessage(
223                         "Admin", $client->getUID(),"C00000", "You are " .
224                         "attempting to start streaming.{{BR/}}{{BR/}} Information: " .
225                         $stream->getInformations());
226 
227                 //save information about client to the persistent object
228                 $po->vars["WhoIsStreaming"] = $client->getUID();
229                 $po->vars["verified"] = time();
230                 $po->save();
231 
232                 //this client will call oncheckclients every three seconds
233                 $ssc = $client->getStreamServerCommands();
234                 //set interval for onCheckClients to three seconds
235                 $ssc->client_setNextCheckDefaultValue($client->getUID(), 3);
236             }
237             else
238             {
239                 //send the message that someone else is streaming
240                 $client->SendPrivateMessage(
241                         "Admin", $client->getUID(),"C00000", "Sorry ".
242                         "your stream cannot be published because someone ".
243                         "else \"".$po->vars["WhoIsStreaming"]."\" ".
244                         "is streaming.");
245             }
246 
247             //unlock the mutex
248             $mutex->unlock();
249         }
250     }
251 
252 
253 
254 
255 
256 
257     /**
258      * OnClientStoppedStreaming is a notification event that indicates
259      * that the client stopped streaming.
260      *
261      * @param Client $client handler of the client who stopped streaming
262      * @param Stream $stream stream descriptor
263      */
264     function _onClientStoppedStreaming($client, $stream)
265     {
266         Logger::func("onClientStoppedStreaming");
267     }
268 
269 
270 
271 
272 
273 
274 
275 
276     /**
277      * OnClientWantsToPlayStream event is triggered when some client
278      * attempts to play live audio or video.
279      *
280      * @param Client $client handler of the client who wants to play
281      * some stream
282      * @param Stream $stream stream descriptor
283      */
284     function _onClientWantsToPlayStream(&$client, $stream)
285     {
286         Logger::func("onClientWantsToPlayStream");
287         //accept playing stream
288         $client->setPlayStreamAccepted();
289         //send information about the stream
290         $client->SendPrivateMessage(
291                 "Admin",$client->getUID(),"00C000", "Client is " .
292                 "attempting to play the stream.{{BR/}} Information are " .
293                 $stream->getInformations());
294     }
295 
296 
297 
298 
299 
300 
301     /**
302      * OnClientStoppedPlayingStream is a notification event triggered
303      * when some client stopped playing live audio or video.
304      *
305      * @param Client $client handler of the client who stopped playing
306      * the stream
307      * @param Stream $stream stream descriptor
308      */
309     function _onClientStoppedPlayingStream($client, $stream)
310     {
311         Logger::func("onClientStoppedPlayingStream");
312     }
313 
314 
315 
316 
317 
318 
319 
320     /**
321      * OnCheckClients - This event is executed at a periodic
322      * time interval, which depends on the property NextCheck of each client
323      * in the system. Every one second Radical Bridge decrements NextCheck
324      * value of each client in all stream server chat rooms by one. When
325      * NextCheck value is equal to 0, client's manipulator (handler) is pushed
326      * to the associative array and passed to the onCheckClients event
327      * there the property NextCheck must be reset, otherwise the concrete
328      * client will never more call onCheckClients again. This function is very
329      * useful for such systems, where on the beginning each user has an account
330      * with some amount of money or credits and later, if he/she runs
331      * out of credit, then his/her connection to the chat is terminated.
332      *
333      * @param array[int]Client $clients array of handlers of the clients which
334      * has to be checked.
335      */
336     function _onCheckClients(&$clients)
337     {
338         Logger::func("onCheckClients");
339         foreach($clients as $client)
340         {
341             //cast to client is here only because we need code insight
342             //or intellisense when typing the code
343             $client = Client::castToClient($client);
344 
345             //initialize mutex for transaction operation
346             $mutexName = "Mutex".$client->getRoomUID();
347             $mutex = new Mutex($mutexName);
348             //try to begin transaction on persistent object
349             //Radical Chat persistent object is a temporary object
350             // which is not stored in any database
351             $mutex->lock();
352             {
353                 //open persistent object
354                 $po = new PersistentObject($mutexName);
355                 //test if persistent object has been setted or if it is necessary
356                 //to set it right now
357                 $po->vars["WhoIsStreaming"] = $client->getUID();
358                 $po->vars["verified"] = time();
359                 $po->save();
360 
361                 //unlock the mutex
362                 $mutex->unlock();
363             }
364         }
365     }
366 
367 
368     /**
369      * OnClientSendMessage function is called every time when a client sends
370      * a text message, but only in case that the client's flag
371      * dispatchEventIfClientSendTextMessage is set to true.
372      *
373      * @param Client $client handler of the client who is sending the
374      * message
375      * @param sstring $toUID UID of the recipient
376      * @param sstring $color the color of the message
377      * @param sstring $message the text of the message
378      */
379     function _onClientSendMessage(&$client,$toUID,$color,$message)
380     {
381         Logger::func("onClientSendMessage");
382         Logger::info($message);
383         $prefix = "prefix ";
384         $postfix = " postfix. This message was altered on the fly.".
385         $client->SendPrivateMessage($client->getNickName(), $toUID, $color,
386                 $prefix . $message . $postfix);
387     }
388 
389 
390 
391 
392 
393 
394 
395     /**
396      * OnUniversalCall event is triggered when custom BasicButton
397      * in “Radical Flash Chat”  application
398      * (application in client’s web browser) is pressed.
399      *
400      * @param array[int]Client $clients array of clients
401      * @param $senderName action which caused this call,
402      * for example button click
403      * @param $eventName - name of the event,
404      * please see the class ChatEvents
405      * @param $value optional parameter
406      */
407     function _onUniversalCall(&$clients,$senderName,$eventName,$value)
408     {
409         Logger::func("onUniversalCall");
410 
411         //If basic button was clicked then
412         //clients[0] is client who clicked the button
413         //clients[1..n] clients selected in clients[0] userslist
414         if($eventName==ChatEvents::$EVENT_BASICBUTTONCLICKED_WITHSELECTEDCLIENTS)
415         {
416             //if kickOut button was pressed
417             if($senderName=="KickOutButton")
418             {
419                 //client who is kicking out
420                 $clientWhoIsKickingOut = Client::castToClient($clients[0]);
421                 $clientWhoIsKickingOut->SendPrivateMessage(
422                         "Admin",$clientWhoIsKickingOut->getUID(),"000000",
423                         "You are attempting to KickOut clients.");
424 
425                 //kickout all other clients
426                 for($a = 1; $a<sizeof($clients); $a++)
427                 {
428                     $clients[$a]->SendPrivateMessage(
429                             "Admin",$clients[$a]->getUID(),"000000",
430                             "You were kicked out by " .
431                             $clientWhoIsKickingOut->getNickName());
432                 }
433             }
434 
435             //if PirvateButton was pressed
436             if($senderName=="PrivateButton")
437             {
438                     //Client who wants to go to private chat
439                     $client = Client::castToClient($clients[0]);
440                     $client->SendPrivateMessage("Admin", $client->getUID(),
441                             "000000", "You are trying to invite some other ".
442                             "user to the private chat. ".
443                             "Take a look on service.php - event ".
444                             "onUniversalCall and setup private chat there.");
445 
446                     $ssc = $client->getStreamServerCommands();
447                     //the new web browser window should be opened where
448                     //two users can communicate one with each other
449                     $ssc->client_OpenWebPage($client->getUID(),
450                             "index.php","_blank");
451                     ///the other user can be redirected to the same page
452                     //$ssc->client_OpenWebPage("performer",
453                     //        "http://127.0.0.1/","_self");
454             }
455         }
456     }
457 
458 
459 
460 
461 
462 
463     /**
464      * OnChatRoomCreated event occurs when new chat room is created just
465      * before the first client attempts to enter it.
466      *
467      * @param ChatRoom $room handler of the room which is going to be
468      * created
469      */
470     function _onChatRoomCreated(&$room)
471     {
472         Logger::func("onChatRoomCreated");
473         //send the message that chat room has been created
474         $room->SendRoomMessage("Admin","00FF00",
475                 "Chat room has been created");
476         //try to alter room skin
477         $ssc = $room->getStreamServerCommands();
478         $skin = $ssc->room_getSkin($room->getUID());
479         //set room name
480         $skin->updateSkinObject(
481             '<Skin><BG><SkinObject><Type>HtmlTextArea</Type>
482                     <Name>RoomName</Name>
483                     <Visible>true</Visible>
484                     <Position>
485                             <Left>{PerformerVideoPanel.Left}+10</Left>
486                             <Top>{PerformerVideoPanel.Top}+5</Top>
487                             <Width>400</Width>
488                             <Height>50</Height>
489                     </Position>'.
490                     '<HtmlText>{{FONT SIZE="12" COLOR="#000000" '.
491                     'LETTERSPACING="0" KERNING="0"}}'.
492                     '{{B}}'.$room->getUID().'{{/B}}{{/FONT}}</HtmlText>'.
493                     '<IsTextSelectable>false</IsTextSelectable>
494             </SkinObject></BG></Skin>');
495     }
496 
497 
498 
499 
500 
501 
502 
503         /**
504          * OnCheckChatRooms - very similar to onCheckClients, but associative
505          * array contains chat room handlers instead of client handlers.
506          * This function is very useful for online SMS chats.
507          *
508          * @param ChatRoom[] $rooms array of handlers of the chatrooms
509          * which need to be checked
510          */
511         function _onCheckChatRooms(&$rooms)
512         {
513                 Logger::func("onCheckChatRooms");
514                 foreach($rooms as $room)
515                 {
516                         
517                 }
518         }
519 
520 
521 
522 
523 
524 
525 
526     /**
527      * OnChatRoomClosed event is invoked when a room is about to close,
528      * just after the last client left the room.
529      *
530      * @param ChatRoom $room handler of the chat room which has been closed
531      */
532     function _onChatRoomClosed($room)
533     {
534         Logger::func("onChatRoomClosed");
535     }
536 
537 
538 
539 
540 ?>
541 <?php
542     //handle service
543     require_once("servicehandler.php");
544 ?>
545