1<?php 2/** 3 * getnewmsg.class.php 4 * 5 * Copyright © 2006 Stephane Gully <stephane.gully@gmail.com> 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the 19 * Free Software Foundation, 51 Franklin St, Fifth Floor, 20 * Boston, MA 02110-1301 USA 21 */ 22 23require_once(dirname(__FILE__)."/../pfccommand.class.php"); 24 25class pfcCommand_getnewmsg extends pfcCommand 26{ 27 function run(&$xml_reponse, $p) 28 { 29 $clientid = $p["clientid"]; 30 $param = $p["param"]; 31 $sender = $p["sender"]; 32 $recipient = $p["recipient"]; 33 $recipientid = $p["recipientid"]; 34 35 $c =& pfcGlobalConfig::Instance(); 36 // do nothing if the recipient is not defined 37 if ($recipient == "") return; 38 39 // check this methode is not being called 40 if( isset($_SESSION["pfc_lock_readnewmsg_".$c->getId()."_".$clientid]) ) 41 { 42 // kill the lock if it has been created more than 10 seconds ago 43 $last_10sec = time()-10; 44 $last_lock = $_SESSION["pfc_lock_readnewmsg_".$c->getId()."_".$clientid]; 45 if ($last_lock < $last_10sec) $_SESSION["pfc_lock_".$c->getId()."_".$clientid] = 0; 46 if ( $_SESSION["pfc_lock_readnewmsg_".$c->getId()."_".$clientid] != 0 ) exit; 47 } 48 // create a new lock 49 $_SESSION["pfc_lock_readnewmsg_".$c->getId()."_".$clientid] = time(); 50 51 // read the last from_id value 52 $container =& pfcContainer::Instance(); 53 $from_id_sid = "pfc_from_id_".$c->getId()."_".$clientid."_".$recipientid; 54 55 $from_id = 0; 56 if (isset($_SESSION[$from_id_sid])) 57 $from_id = $_SESSION[$from_id_sid]; 58 else 59 { 60 $from_id = $container->getLastId($recipient) - $c->max_msg - 1; 61 if ($from_id < 0) $from_id = 0; 62 } 63 // check if this is the first time you get messages 64 $oldmsg_sid = "pfc_oldmsg_".$c->getId()."_".$clientid."_".$recipientid; 65 $oldmsg = false; 66 if (isset($_SESSION[$oldmsg_sid])) 67 { 68 unset($_SESSION[$oldmsg_sid]); 69 $oldmsg = true; 70 } 71 72 $new_msg = $container->read($recipient, $from_id); 73 $new_from_id = $new_msg["new_from_id"]; 74 $data = $new_msg["data"]; 75 76 // transform new message in html format 77 $js = array(); 78 $data_sent = false; 79 foreach ($data as $d) 80 { 81 $m_id = $d["id"]; 82 $m_date = date($c->date_format, $d["timestamp"] + $c->time_offset); 83 $m_time = date($c->time_format, $d["timestamp"] + $c->time_offset); 84 $m_sender = $d["sender"]; 85 $m_recipientid = $recipientid; 86 $m_cmd = $d["cmd"]; 87 $m_param = phpFreeChat::PostFilterMsg(pfc_make_hyperlink($d["param"])); 88 $js[] = array($m_id, 89 $m_date, 90 $m_time, 91 $m_sender, 92 $m_recipientid, 93 $m_cmd, 94 $m_param, 95 date($c->date_format, time() + $c->time_offset) == $m_date ? 1 : 0, // posted today ? 96 $oldmsg ? 1 : 0); // is it a new message in the current session or is it a part of the history 97 $data_sent = true; 98 } 99 if (count($js) != 0) 100 { 101 require_once dirname(__FILE__).'/../pfcjson.class.php'; 102 $json = new pfcJSON(); 103 $js = $json->encode($js); 104 $xml_reponse->script("pfc.handleResponse('".$this->name."', 'ok', (".$js."));"); 105 } 106 107 if ($data_sent) 108 { 109 // store the new msg id 110 $_SESSION[$from_id_sid] = $new_from_id; 111 } 112 113 // remove the lock 114 $_SESSION["pfc_lock_readnewmsg_".$c->getId()."_".$clientid] = 0; 115 116 } 117} 118 119?> 120