1<?php
2/**
3 * checknickchange.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 */
22require_once dirname(__FILE__)."/../pfci18n.class.php";
23require_once dirname(__FILE__)."/../pfcuserconfig.class.php";
24require_once dirname(__FILE__)."/../pfcproxycommand.class.php";
25
26/**
27 * pfcProxyCommand_checknickchange
28 *
29 * @author Stephane Gully <stephane.gully@gmail.com>
30 */
31class pfcProxyCommand_checknickchange extends pfcProxyCommand
32{
33  function run(&$xml_reponse, $p)
34  {
35    $clientid    = $p["clientid"];
36    $param       = $p["param"];
37    $sender      = $p["sender"];
38    $recipient   = $p["recipient"];
39    $recipientid = $p["recipientid"];
40    $owner       = isset($p["owner"]) ? $p["owner"] : '';
41    $c  =& pfcGlobalConfig::Instance();
42    $u  =& pfcUserConfig::Instance();
43    $ct =& pfcContainer::Instance();
44
45    $newnick = phpFreeChat::FilterNickname($param);
46    $oldnick = $ct->getNickname($u->nickid);
47
48    if ( $this->name == 'nick' )
49    {
50
51      // if the user want to change his nickname but the frozen_nick is enable
52      // then send him a warning
53      if ( $this->name == 'nick' &&
54           $oldnick != '' &&
55           $newnick != $oldnick &&
56           $c->frozen_nick == true &&
57           $owner != $this->proxyname )
58      {
59          $msg = _pfc("You are not allowed to change your nickname");
60          $xml_reponse->script("pfc.handleResponse('".$this->proxyname."', 'nick', '".addslashes($msg)."');");
61          return false;
62      }
63
64      $newnickid = $ct->getNickId($newnick);
65      $oldnickid = $u->nickid;
66
67      if ($newnick == $oldnick &&
68          $newnickid == $oldnickid)
69      {
70        $xml_reponse->script("pfc.handleResponse('".$this->name."', 'notchanged', '".addslashes($newnick)."');");
71        return true;
72      }
73
74      // now check the nickname is not yet used (unsensitive case)
75      // 'BoB' and 'bob' must be considered same nicknames
76      $nick_in_use = $this->_checkNickIsUsed($newnick, $oldnickid);
77      if ($nick_in_use)
78      {
79        if ($c->frozen_nick)
80          $xml_reponse->script("pfc.handleResponse('nick', 'notallowed', '".addslashes($newnick)."');");
81        else
82          $xml_reponse->script("pfc.handleResponse('nick', 'isused', '".addslashes($newnick)."');");
83        return false;
84      }
85    }
86
87    // allow nick changes only from the parameters array (server side)
88    if ($this->name != 'connect' && // don't check anything on the connect process or it could block the periodic refresh
89        $c->frozen_nick == true &&
90        $oldnick != $c->nick &&
91        $c->nick != '' && // don't change the nickname to empty or the asknick popup will loop indefinatly
92        $owner != $this->proxyname)
93    {
94      // change the user nickname
95      $cmdp = $p;
96      $cmdp["param"] = $c->nick;
97      $cmdp["owner"] = $this->proxyname;
98      $cmd =& pfcCommand::Factory("nick");
99      return $cmd->run($xml_reponse, $cmdp);
100    }
101
102    // forward the command to the next proxy or to the final command
103    return $this->next->run($xml_reponse, $p);
104  }
105
106  function _checkNickIsUsed($newnick, $oldnickid)
107  {
108    $c =& pfcGlobalConfig::Instance();
109    $ct =& pfcContainer::Instance();
110    $nick_in_use = false;
111    $online_users = $ct->getOnlineNick(NULL);
112    if (isset($online_users["nickid"]))
113      foreach($online_users["nickid"] as $nid)
114      {
115        if (preg_match("/^".preg_quote($ct->getNickname($nid))."$/i",$newnick))
116        {
117          // the nick match
118          // just allow the owner to change his capitalised letters
119          if ($nid != $oldnickid)
120            return true;
121        }
122      }
123  }
124}
125
126?>
127