chamilo /common/libraries/plugin/phpfreechat/src/pfccommand.class.php

Language PHP Lines 411
MD5 Hash 6cd93e5b0b770c8eb5583aae49ffafd5 Estimated Cost $4,516 (why?)
Repository https://bitbucket.org/renaatdemuynck/chamilo View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<?php
/**
 * pfccommand.class.php
 *
 * Copyright Š 2006 Stephane Gully <stephane.gully@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 */
require_once dirname(__FILE__) . "/pfci18n.class.php";
require_once dirname(__FILE__) . "/pfcuserconfig.class.php";

/**
 * pfcCommand is an abstract class (interface) which must be inherited by each concrete commands
 * Commands examples : /nick /me /update ...
 *
 * @example ../demo/demo27_customized_command.php
 * @author Stephane Gully <stephane.gully@gmail.com>
 */
class pfcCommand
{
    /**
     * Command name (lowercase)
     */
    var $name = '';
    
    /**
     * Contains the command syntaxe (how to use the command)
     */
    var $usage = '';
    
    /**
     * Not used for now
     */
    var $desc = '';
    var $help = '';

    /**
     * Used to instanciate a command
     * $tag is the command name : "nick", "me", "update" ...
     */
    function &Factory($name)
    {
        $c = & pfcGlobalConfig :: Instance();
        
        // instanciate the real command
        $cmd = NULL;
        $cmd_name = $name;
        $cmd_classname = "pfcCommand_" . $name;
        
        $cmd_filename = $c->cmd_path_default . '/' . $cmd_name . '.class.php';
        if (file_exists($cmd_filename))
            require_once ($cmd_filename);
        $cmd_filename = $c->cmd_path . '/' . $cmd_name . '.class.php';
        if (file_exists($cmd_filename))
            require_once ($cmd_filename);
        
        if (! class_exists($cmd_classname))
        {
            $tmp = NULL;
            return $tmp;
        }
        
        $cmd = & new $cmd_classname();
        $cmd->name = $cmd_name;
        
        // instanciate the proxies chaine
        $firstproxy = & $cmd;
        for($i = count($c->proxies) - 1; $i >= 0; $i --)
        {
            $proxy_name = $c->proxies[$i];
            $proxy_classname = "pfcProxyCommand_" . $proxy_name;
            
            // try to include the proxy class file from the default path or from the customized path
            $proxy_filename = $c->proxies_path_default . '/' . $proxy_name . ".class.php";
            if (file_exists($proxy_filename))
                require_once ($proxy_filename);
            $proxy_filename = $c->proxies_path . '/' . $proxy_name . ".class.php";
            if (file_exists($proxy_filename))
                require_once ($proxy_filename);
            
            if (! class_exists($proxy_classname))
                return $firstproxy;
            
     // instanciate the proxy
            $proxy = & new $proxy_classname();
            $proxy->name = $cmd_name;
            $proxy->proxyname = $proxy_name;
            $proxy->linkTo($firstproxy);
            $firstproxy = & $proxy;
        }
        
        /*
    $tmp = '';
    $cur = $firstproxy;
    while($cur)
    {
      $tmp .= (isset($cur->proxyname)?$cur->proxyname:$cur->name).'|';
      $cur = $cur->next;
    }
    $tmp .= var_export($firstproxy,true);
    file_put_contents('/tmp/debug1',$tmp);
*/
        
        // return the proxy, not the command (the proxy will forward the request to the real command)
        return $firstproxy;
    }

    /**
     * Constructor
     * @private
     */
    function __construct()
    {
    }

    /**
     * Virtual methode which must be implemented by concrete commands
     * It is called by the phpFreeChat::HandleRequest function to execute the wanted command
     */
    function run(&$xml_reponse, $p)
    {
        die(_pfc("%s must be implemented", get_class($this) . "::" . __FUNCTION__));
    }

    /**
     * Force whois reloading
     */
    function forceWhoisReload($nickid)
    {
        $c = & pfcGlobalConfig :: Instance();
        $u = & pfcUserConfig :: Instance();
        $ct = & pfcContainer :: Instance();
        
        // list the users in the same channel as $nickid
        $channels = $ct->getMeta("nickid-to-channelid", $nickid);
        $channels = $channels['value'];
        $channels = array_diff($channels, array('SERVER'));
        $otherids = array();
        foreach ($channels as $chan)
        {
            $ret = $ct->getOnlineNick($ct->decode($chan));
            $otherids = array_merge($otherids, $ret['nickid']);
        }
        
        // alert them that $nickid user info just changed
        foreach ($otherids as $otherid)
        {
            $cmdstr = 'whois2';
            $cmdp = array();
            $cmdp['params'] = array($nickid);
            pfcCommand :: AppendCmdToPlay($otherid, $cmdstr, $cmdp);
            
        /*
      $cmdtoplay = $ct->getUserMeta($otherid, 'cmdtoplay');
      $cmdtoplay = ($cmdtoplay == NULL) ? array() : unserialize($cmdtoplay);
      $cmdtmp = array("whois2",    // cmdname 
                      $nicktorewhois,   // param 
                      NULL,       // sender 
                      NULL,       // recipient 
                      NULL,       // recipientid 
                      );
      if (!in_array($cmdtmp, $cmdtoplay))
      {
        $cmdtoplay[] = $cmdtmp;
        $ct->setUserMeta($otherid, 'cmdtoplay', serialize($cmdtoplay));
      }
      */
        }
    }

    /**
     * Add command to be played onto command stack
     * @param $nickid is the user that entered the command
     * @param $cmdstr is the command
     * @param $cmdp is the command's parameters
     * @return false if $nickid is blank, true for all other values of $nickid
     */
    function AppendCmdToPlay($nickid, $cmdstr, $cmdp)
    {
        $c = & pfcGlobalConfig :: Instance();
        $u = & pfcUserConfig :: Instance();
        
        $ct = & pfcContainer :: Instance();
        
        // check for empty nickid
        if ($nickid == "")
            return false;
        
     // get new command id
        $cmdtoplay_id = $ct->incMeta("nickid-to-cmdtoplayid", $nickid, 'cmdtoplayid');
        if (count($cmdtoplay_id["value"]) == 0)
            $cmdtoplay_id = 0;
        else
            $cmdtoplay_id = $cmdtoplay_id["value"][0];
        
     // create command array
        $cmdtoplay = array();
        $cmdtoplay['cmdstr'] = $cmdstr;
        $cmdtoplay['params'] = $cmdp;
        
        // store command to play
        $ct->setCmdMeta($nickid, $cmdtoplay_id, serialize($cmdtoplay));
        
        return true;
    }

    /**
     * Run all commands to be played for a user
     * @param $nickid is the user that entered the command
     * @param $context
     * @param $xml_reponse
     */
    function RunPendingCmdToPlay($nickid, $context, &$xml_reponse)
    {
        $c = & pfcGlobalConfig :: Instance();
        $u = & pfcUserConfig :: Instance();
        $ct = & pfcContainer :: Instance();
        
        // Get all queued commands to be played
        $cmdtoplay_ids = $ct->getCmdMeta($nickid);
        // process each command and parse content
        foreach ($cmdtoplay_ids as $cid)
        {
            // take a command from the list
            $cmdtoplay = $ct->getCmdMeta($nickid, $cid);
            $cmdtoplay = ($cmdtoplay == NULL || count($cmdtoplay) == 0) ? array() : unserialize($cmdtoplay[0]);
            
            // play the command
            $cmd = & pfcCommand :: Factory($cmdtoplay['cmdstr']);
            $cmdp = $cmdtoplay['params'];
            if (! isset($cmdp['param']))
                $cmdp['param'] = '';
            if (! isset($cmdp['sender']))
                $cmdp['sender'] = $context['sender'];
            if (! isset($cmdp['recipient']))
                $cmdp['recipient'] = $context['recipient'];
            if (! isset($cmdp['recipientid']))
                $cmdp['recipientid'] = $context['recipientid'];
            $cmdp['clientid'] = $context['clientid']; // the clientid must be the current user one
            $cmdp['cmdtoplay'] = true; // used to run some specials actions in the command (ex:  if the cmdtoplay is a 'leave' command, then show an alert to the kicked or banished user)
            if ($c->debug)
                $cmd->run($xml_reponse, $cmdp);
            else
                @$cmd->run($xml_reponse, $cmdp);
            
     // delete command when complete
            $ct->rmMeta("nickid-to-cmdtoplay", $nickid, $cid);
        }
    }

    function trace(&$xml_reponse, $msg, $data = NULL)
    {
        if ($data != NULL)
        {
            require_once dirname(__FILE__) . '/pfcjson.class.php';
            $json = new pfcJSON();
            $js = $json->encode($data);
            $xml_reponse->script("trace('" . $msg . " -> " . $js . "');");
        }
        else
            $xml_reponse->script("trace('" . $msg . "');");
    
    }

    function ParseCommand($cmd_str, $one_parameter = false)
    {
        $pattern_quote = '/([^\\\]|^)"([^"]+[^\\\])"/';
        $pattern_quote = '/"([^"]+)"/';
        $pattern_noquote = '/([^"\s]+)/';
        $pattern_command = '/^\/([a-z0-9]+)\s*([a-z0-9]+)\s*([a-z0-9]+)\s*(.*)/';
        $result = array();
        
        // parse the command name (ex: '/invite')
        if (preg_match($pattern_command, $cmd_str, $res))
        {
            $cmd = $res[1];
            $clientid = $res[2];
            $recipientid = $res[3];
            $params_str = $res[4];
            
            // don't parse multiple parameters for special commands with only one parameter
            // this make possible to send double quotes (") in these commands
            if ($one_parameter || $cmd == 'send' || $cmd == 'notice' || $cmd == 'me')
            {
                $result['cmdstr'] = $cmd_str;
                $result['cmdname'] = $cmd;
                $result['params'] = array($clientid, $recipientid, $params_str);
                return $result;
            }
            
            // parse the quotted parameters (ex: '/invite "nickname with spaces"')
            preg_match_all($pattern_quote, $params_str, $res1, PREG_OFFSET_CAPTURE);
            $params_res = $res1[1];
            // split the parameters string
            $nospaces = preg_split($pattern_quote, $params_str, - 1, PREG_SPLIT_OFFSET_CAPTURE | PREG_SPLIT_NO_EMPTY);
            foreach ($nospaces as $p)
            {
                // parse the splited blocks with unquotted parameter pattern (ex: '/invite nicknamewithoutspace')
                preg_match_all($pattern_noquote, $p[0], $res2, PREG_OFFSET_CAPTURE);
                foreach ($res2[1] as $p2)
                {
                    $p2[1] += $p[1];
                    $params_res[] = $p2;
                }
            }
            
            // order the array by offset
            $params = array();
            foreach ($params_res as $p)
                $params[$p[1]] = $p[0];
            ksort($params);
            $params = array_values($params);
            $params = array_map("trim", $params);
            $params = array_merge(array($clientid, $recipientid), $params);
            
            $result['cmdstr'] = $cmd_str;
            $result['cmdname'] = $cmd;
            $result['params'] = $params;
        }
        return $result;
    }
    
/*
  // THIS IS ANOTHER WAY TO PARSE THE PARAMETERS
  // IT'S NOT SIMPLIER BUT MAYBE FASTER
  // @todo : take the faster methode
  function ParseCommand($cmd_str, $one_parameter = false)
  {
    $pattern_command = '/^\/([a-z0-9]+)\s*([a-z0-9]+)\s*([a-z0-9]+)\s*(.*)/';
    $result = array();
    
    // parse the command name (ex: '/invite')
    if (preg_match($pattern_command, $cmd_str, $res))
    {
      $cmd         = $res[1];
      $clientid    = $res[2];
      $recipientid = $res[3];
      $params_str  = $res[4];

      // don't parse multiple parameters for special commands with only one parameter
      // this make possible to send double quotes (") in these commands
      if ($one_parameter || $cmd == 'send' || $cmd == 'notice' || $cmd == 'me')
      {
        $result['cmdstr']  = $cmd_str;
        $result['cmdname'] = $cmd;
        $result['params']  = array($clientid, $recipientid, $params_str);
        return $result;
      }

      $params = array($clientid, $recipientid);
      $sep    = preg_match('/[^\\\\]"/',$params_str) ? '"' : ' ';
      if ($sep == ' ') $params_str = ' ' . $params_str;
      $offset = 0;
      while (1)
      {
        $i1 = strpos($params_str,$sep,$offset);
        // capture the parameter value
        if ($i1 !== FALSE)
        {
          // remove multi-separators
          while (1)
          {
            if (strpos($params_str,$sep,$i1+1) - $i1 == 1)
              $i1++;
            else
              break;
          }
          // search the parameter terminason
          $offset = $i1+1;
          $i2 = strpos($params_str,$sep,$offset);
          if ($i2 !== FALSE)
          {
            $offset = $i2 + ($sep == '"' ? 1 : 0);
            $p = substr($params_str, $i1+1, $i2-$i1-1);
            if (!preg_match('/^\s*$/',$p))
              $params[] = $p;
          }
          else
            break;
        }
        else
          break;
      }
      // append the tail
      if ($offset < strlen($params_str))
        $params[] = substr($params_str,$offset);
      
      $result['cmdstr']  = $cmd_str;
      $result['cmdname'] = $cmd;
      $result['params']  = $params;
    }
    return $result;
  }
*/

}

?>
Back to Top