1<?php
2
3require_once dirname(__FILE__).'/../pfccommand.class.php';
4
5class pfcCommand_connect extends pfcCommand
6{
7  function run(&$xml_reponse, $p)
8  {
9    $clientid    = $p["clientid"];
10    $param       = $p["param"];
11    $params      = $p["params"];
12    $sender      = $p["sender"];
13    $recipient   = $p["recipient"];
14    $recipientid = $p["recipientid"];
15    $getoldmsg   = isset($p["getoldmsg"]) ? $p["getoldmsg"] : true;
16    $joinoldchan = isset($p["joinoldchan"]) ? $p["joinoldchan"] : true;
17
18    // nickname must be given to be able to connect to the chat
19    $nick = $params[0];
20
21    $c  =& pfcGlobalConfig::Instance();
22    $u  =& pfcUserConfig::Instance();
23    $ct =& pfcContainer::Instance();
24
25    // reset the message id indicator (see getnewmsg.class.php)
26    // i.e. be ready to re-get all last posted messages
27    if ($getoldmsg)
28      $this->_resetChannelIdentifier($clientid);
29
30    // check if the user is alone on the server, and give it the admin status if yes
31    $isadmin = $ct->getUserMeta($u->nickid, 'isadmin');
32    if ($isadmin == NULL)
33      $isadmin = $c->isadmin;
34    if ($c->firstisadmin && !$isadmin)
35    {
36      $users = $ct->getOnlineNick(NULL);
37      if (isset($users["nickid"]) &&
38          (count($users["nickid"]) == 0 || (count($users["nickid"]) == 1 && $users["nickid"][0] == $u->nickid)))
39        $isadmin = true;
40    }
41
42    // create the nickid and setup some user meta
43    $nickid = $u->nickid;
44    $ct->joinChan($nickid, NULL); // join the server
45    // store the user ip
46    $ip = ( $c->get_ip_from_xforwardedfor && isset($_SERVER["HTTP_X_FORWARDED_FOR"])) ?
47      $_SERVER["HTTP_X_FORWARDED_FOR"] :
48      $_SERVER["REMOTE_ADDR"];
49    if ($ip == "::1") $ip = "127.0.0.1"; // fix for konqueror & localhost
50    $ct->setUserMeta($nickid, 'ip', $ip);
51    // store the admin flag
52    $ct->setUserMeta($nickid, 'isadmin', $isadmin);
53    // store the customized nick metadata
54    foreach($c->nickmeta as $k => $v)
55      $ct->setUserMeta($nickid, $k, $v);
56
57    // run the /nick command to assign the user nick
58    $cmdp = array();
59    $cmdp["param"] = $nick;
60    $cmd =& pfcCommand::Factory('nick');
61    $ret = $cmd->run($xml_reponse, $cmdp);
62    if ($ret)
63    {
64      $chanlist = (count($u->channels) == 0) ? $c->channels : $u->getChannelNames();
65      for($i = 0 ; $i < count($chanlist) ; $i++)
66      {
67        $cmdp = array();
68        $cmdp["param"] = $chanlist[$i];
69        $cmd =& pfcCommand::Factory( $i < count($chanlist)-1 || !$joinoldchan ? 'join2' : 'join' );
70        $cmd->run($xml_reponse, $cmdp);
71      }
72
73      $pvlist = (count($u->privmsg) == 0) ? $c->privmsg : $u->getPrivMsgNames();
74      for($i = 0 ; $i < count($pvlist) ; $i++)
75      {
76        $cmdp = array();
77        $cmdp["params"][0] = $pvlist[$i];
78        $cmd =& pfcCommand::Factory( $i < count($pvlist)-1 || !$joinoldchan ? 'privmsg2' : 'privmsg' );
79        $cmd->run($xml_reponse, $cmdp);
80      }
81
82      $xml_reponse->script("pfc.handleResponse('".$this->name."', 'ok', Array('".addslashes($nick)."'));");
83    }
84    else
85    {
86      $xml_reponse->script("pfc.handleResponse('".$this->name."', 'ko', Array('".addslashes($nick)."'));");
87    }
88  }
89
90  /**
91   * reset the channel identifiers
92   */
93  function _resetChannelIdentifier($clientid)
94  {
95    $c  =& pfcGlobalConfig::Instance();
96    $u  =& pfcUserConfig::Instance();
97    $ct =& pfcContainer::Instance();
98
99    // reset the channel identifiers
100    require_once(dirname(__FILE__)."/join.class.php");
101    $channels = array();
102    if (count($u->channels) == 0)
103      $channels = $c->channels;
104    else
105      foreach($u->channels as $chan)
106        $channels[] = $chan["name"];
107    foreach($channels as $channame)
108    {
109      $chanrecip = pfcCommand_join::GetRecipient($channame);
110      $chanid    = pfcCommand_join::GetRecipientId($channame);
111      // reset the fromid flag
112      $from_id_sid = "pfc_from_id_".$c->getId()."_".$clientid."_".$chanid;
113      $from_id     = $ct->getLastId($chanrecip)-$c->max_msg+1;
114      $_SESSION[$from_id_sid] = ($from_id<0) ? 0 : $from_id;
115      // reset the oldmsg flag
116      $oldmsg_sid = "pfc_oldmsg_".$c->getId()."_".$clientid."_".$chanid;
117      $_SESSION[$oldmsg_sid] = true;
118    }
119    // reset the private messages identifiers
120    if (count($u->privmsg) > 0)
121    {
122      foreach($u->privmsg as $recipientid2 => $pv)
123      {
124        $recipient2  = $pv['recipient'];
125        // reset the fromid flag
126        $from_id_sid = "pfc_from_id_".$c->getId()."_".$clientid."_".$recipientid2;
127        $from_id     = $ct->getLastId($recipient2)-$c->max_msg+1;
128        $_SESSION[$from_id_sid] = ($from_id<0) ? 0 : $from_id;
129        // reset the oldmsg flag
130        $oldmsg_sid = "pfc_oldmsg_".$c->getId()."_".$clientid."_".$recipientid2;
131        $_SESSION[$oldmsg_sid] = true;
132      }
133    }
134  }
135
136}
137
138?>