This file is indexed.

/usr/share/yate/scripts/libyate.php is in yate-scripts 5.4.0-1-1ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<?php

/* libyate.php
 * This file is part of the YATE Project http://YATE.null.ro
 *
 * PHP object-oriented interface library for Yate
 *
 * Yet Another Telephony Engine - a fully featured software PBX and IVR
 * Copyright (C) 2004-2014 Null Team
 *
 * This software is distributed under multiple licenses;
 * see the COPYING file in the main directory for licensing
 * information for this specific distribution.
 *
 * This use of this software may be subject to additional restrictions.
 * See the LEGAL file in the main directory for details.
 *
 * This program 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.
 */

/*
    WARNING: This file is for PHP 5
    To modify it for PHP 4 use the following command (needs sed version 4)

    sed -i.bak -e 's/static \(function\)/\1/' libyate.php
*/

/**
 * The Yate class encapsulates the object oriented interface of PHP to Yate
 */
class Yate
{
    /** String: Type of the event */
    var $type;
    /** String: Name of the message */
    var $name;
    /** String: Return value of the message */
    var $retval;
    /** Number: Time the message was generated */
    var $origin;
    /** String: Temporarily unique message ID */
    var $id;
    /** Boolean: Was the message handled or not */
    var $handled;
    /** Array: Message parameters, $obj->params["name"]="value" */
    var $params;

    /**
     * Static function to output a string to Yate's stderr or logfile
     * @param $str String to output
     */
    static function Output($str)
    {
	global $yate_stderr, $yate_output;
	if ($str === true)
	    $yate_output = true;
	else if ($str === false)
	    $yate_output = false;
	else if ($yate_output)
	    _yate_print("%%>output:$str\n");
	else
	    fputs($yate_stderr, "$str\n");
    }

    /**
     * Static function to output a string to Yate's stderr or logfile
     *  only if debugging was enabled.
     * @param $str String to output, or boolean (true/false) to set debugging
     */
    static function Debug($str)
    {
	global $yate_stderr, $yate_debug;
	if ($str === true)
	    $yate_debug = true;
	else if ($str === false)
	    $yate_debug = false;
	else if ($yate_debug)
	    Yate::Output($str);
    }

    /**
     * Static function to get the unique argument passed by Yate at start time
     * @return First (and only) command line argument passed by Yate
     */
    static function Arg()
    {
	if (isset($_SERVER['argv'][1]))
	    return $_SERVER['argv'][1];
	return null;
    }

    /**
     * Static function to convert a Yate string representation to a boolean
     * @param $str String value to convert
     * @return True if $str is "true", false otherwise
     */
    static function Str2bool($str)
    {
	return ($str == "true") ? true : false;
    }

    /**
     * Static function to convert a boolean to a Yate string representation
     * @param $bool Boolean value to convert
     * @return The string "true" if $bool was true, "false" otherwise
     */
    static function Bool2str($bool)
    {
	return $bool ? "true" : "false";
    }

    /**
     * Static function to convert a string to its Yate escaped format
     * @param $str String to escape
     * @param $extra (optional) Character to escape in addition to required ones
     * @return Yate escaped string
     */
    static function Escape($str, $extra = "")
    {
	if ($str === true)
	    return "true";
	if ($str === false)
	    return "false";
	$str = $str . "";
	$s = "";
	$n = strlen($str);
	for ($i=0; $i<$n; $i++) {
	    $c = $str{$i};
	    if ((ord($c) < 32) || ($c == ':') || ($c == $extra)) {
		$c = chr(ord($c) + 64);
		$s .= '%';
	    }
	    else if ($c == '%')
		$s .= $c;
	    $s .= $c;
	}
	return $s;
    }

    /**
     * Static function to convert a Yate escaped string back to a plain string
     * @param $str Yate escaped string to unescape
     * @return Unescaped string
     */
    static function Unescape($str)
    {
	$s = "";
	$n = strlen($str);
	for ($i=0; $i<$n; $i++) {
	    $c = $str{$i};
	    if ($c == '%') {
		$i++;
		$c = $str{$i};
		if ($c != '%')
		    $c = chr(ord($c) - 64);
	    }
	    $s .= $c;
	}
	return $s;
    }

    /**
     * Install a Yate message handler
     * @param $name Name of the messages to handle
     * @param $priority (optional) Priority to insert in chain, default 100
     * @param $filtname (optional) Name of parameter to filter for
     * @param $filtvalue (optional) Matching value of filtered parameter
     */
    static function Install($name, $priority = 100, $filtname = "", $filtvalue = "")
    {
	$name=Yate::Escape($name);
	if ($filtname)
	    $filtname=":$filtname:$filtvalue";
	_yate_print("%%>install:$priority:$name$filtname\n");
    }

    /**
     * Uninstall a Yate message handler
     * @param $name Name of the messages to stop handling
     */
    static function Uninstall($name)
    {
	$name=Yate::Escape($name);
	_yate_print("%%>uninstall:$name\n");
    }

    /**
     * Install a Yate message watcher
     * @param $name Name of the messages to watch
     */
    static function Watch($name)
    {
	$name=Yate::Escape($name);
	_yate_print("%%>watch:$name\n");
    }

    /**
     * Uninstall a Yate message watcher
     * @param $name Name of the messages to stop watching
     */
    static function Unwatch($name)
    {
	$name=Yate::Escape($name);
	_yate_print("%%>unwatch:$name\n");
    }

    /**
     * Changes a local module parameter
     * @param $name Name of the parameter to modify
     * @param $value New value to set in the parameter
     */
    static function SetLocal($name, $value)
    {
	if (($value === true) || ($value === false))
	    $value = Yate::Bool2str($value);
	$name=Yate::Escape($name);
	$value=Yate::Escape($value);
	_yate_print("%%>setlocal:$name:$value\n");
    }

    /**
     * Asynchronously retrieve a local module parameter
     * @param $name Name of the parameter to read
     */
    static function GetLocal($name)
    {
	Yate::SetLocal($name, "");
    }

    /**
     * Constructor. Creates a new outgoing message
     * @param $name Name of the new message
     * @param $retval (optional) Default return
     * @param $id (optional) Identifier of the new message
     */
    function Yate($name, $retval = "", $id = "")
    {
	if ($id == "")
	    $id=uniqid(rand(),1);
	$this->type="outgoing";
	$this->name=$name;
	$this->retval=$retval;
	$this->origin=time();
	$this->handled=false;
	$this->id=$id;
	$this->params=array();
    }

    /**
     * Retrieve the value of a named parameter
     * @param $key Name of the parameter to retrieve
     * @param $defvalue (optional) Default value to return if parameter is not set
     * @return Value of the $key parameter or $defvalue
     */
    function GetValue($key, $defvalue = null)
    {
	if (isset($this->params[$key]))
	    return $this->params[$key];
	return $defvalue;
    }

    /**
     * Set a named parameter
     * @param $key Name of the parameter to set
     * @param $value Value to set in the parameter
     */
    function SetParam($key, $value)
    {
	if (($value === true) || ($value === false))
	    $value = Yate::Bool2str($value);
	$this->params[$key] = $value;
    }

    /**
     * Fill the parameter array from a text representation
     * @param $parts A numerically indexed array with the key=value parameters
     * @param $offs (optional) Offset in array to start processing from
     */
    function FillParams(&$parts, $offs = 0)
    {
	$n = count($parts);
	for ($i=$offs; $i<$n; $i++) {
	    $s=$parts[$i];
	    $q=strpos($s,'=');
	    if ($q === false)
		$this->params[Yate::Unescape($s)]=NULL;
	    else
		$this->params[Yate::Unescape(substr($s,0,$q))]=Yate::Unescape(substr($s,$q+1));
	}
    }

    /**
     * Dispatch the message to Yate for handling
     * @param $message Message object to dispatch
     */
    function Dispatch()
    {
	if ($this->type != "outgoing") {
	    Yate::Output("PHP bug: attempt to dispatch message type: " . $this->type);
	    return;
	}
	$i=Yate::Escape($this->id);
	$t=0+$this->origin;
	$n=Yate::Escape($this->name);
	$r=Yate::Escape($this->retval);
	$p="";
	$pa = array(&$p);
	array_walk($this->params, "_yate_message_walk", $pa);
	_yate_print("%%>message:$i:$t:$n:$r$p\n");
	$this->type="dispatched";
    }

    /**
     * Acknowledge the processing of a message passed from Yate
     * @param $handled (optional) True if Yate should stop processing, default false
     */
    function Acknowledge()
    {
	if ($this->type != "incoming") {
	    Yate::Output("PHP bug: attempt to acknowledge message type: " . $this->type);
	    return;
	}
	$i=Yate::Escape($this->id);
	$k=Yate::Bool2str($this->handled);
	$n=Yate::Escape($this->name);
	$r=Yate::Escape($this->retval);
	$p="";
	$pa = array(&$p);
	array_walk($this->params, "_yate_message_walk", $pa);
	_yate_print("%%<message:$i:$k:$n:$r$p\n");
	$this->type="acknowledged";
    }

    /**
     * This static function processes just one input line.
     * It must be called in a loop to keep messages running. Or else.
     * @return false if we should exit, true if we should keep running,
     *  or an Yate object instance. Remember to use === and !== operators
     *  when comparing against true and false.
     */
    static function GetEvent()
    {
	global $yate_stdin, $yate_socket;
	if ($yate_socket) {
	    $line = @socket_read($yate_socket,8192,PHP_NORMAL_READ);
	    // check for error
	    if ($line == false)
		return false;
	    // check for EOF
	    if ($line === "")
		return false;
	}
	else {
	    if ($yate_stdin == false)
		return false;
	    // check for EOF
	    if (feof($yate_stdin))
		return false;
	    $line=fgets($yate_stdin,8192);
	    // check for async read no data
	    if ($line == false)
		return true;
	}
	$line=str_replace("\n", "", $line);
	if ($line == "")
	    return true;
	$ev=true;
	$part=explode(":", $line);
	switch ($part[0]) {
	    case "%%>message":
		/* incoming message str_id:int_time:str_name:str_retval[:key=value...] */
		$ev=new Yate(Yate::Unescape($part[3]),Yate::Unescape($part[4]),Yate::Unescape($part[1]));
		$ev->type="incoming";
		$ev->origin=0+$part[2];
		$ev->FillParams($part,5);
		break;
	    case "%%<message":
		/* message answer str_id:bool_handled:str_name:str_retval[:key=value...] */
		$ev=new Yate(Yate::Unescape($part[3]),Yate::Unescape($part[4]),Yate::Unescape($part[1]));
		$ev->type="answer";
		$ev->handled=Yate::Str2bool($part[2]);
		$ev->FillParams($part,5);
		break;
	    case "%%<install":
		/* install answer num_priority:str_name:bool_success */
		$ev=new Yate(Yate::Unescape($part[2]),"",0+$part[1]);
		$ev->type="installed";
		$ev->handled=Yate::Str2bool($part[3]);
		break;
	    case "%%<uninstall":
		/* uninstall answer num_priority:str_name:bool_success */
		$ev=new Yate(Yate::Unescape($part[2]),"",0+$part[1]);
		$ev->type="uninstalled";
		$ev->handled=Yate::Str2bool($part[3]);
		break;
	    case "%%<watch":
		/* watch answer str_name:bool_success */
		$ev=new Yate(Yate::Unescape($part[1]));
		$ev->type="watched";
		$ev->handled=Yate::Str2bool($part[2]);
		break;
	    case "%%<unwatch":
		/* unwatch answer str_name:bool_success */
		$ev=new Yate(Yate::Unescape($part[1]));
		$ev->type="unwatched";
		$ev->handled=Yate::Str2bool($part[2]);
		break;
	    case "%%<connect":
		/* connect answer str_role:bool_success */
		$ev=new Yate(Yate::Unescape($part[1]));
		$ev->type="connected";
		$ev->handled=Yate::Str2bool($part[2]);
		break;
	    case "%%<setlocal":
		/* local parameter answer str_name:str_value:bool_success */
		$ev=new Yate(Yate::Unescape($part[1]),Yate::Unescape($part[2]));
		$ev->type="setlocal";
		$ev->handled=Yate::Str2bool($part[3]);
		break;
	    case "Error in":
		/* We are already in error so better stay quiet */
		break;
	    default:
		Yate::Output("PHP parse error: " . $line);
	}
	return $ev;
    }

    /**
     * This static function initializes globals in the PHP Yate External Module.
     * It should be called before any other method.
     * @param $async (optional) True if asynchronous, polled mode is desired
     * @param $addr Hostname to connect to or UNIX socket path
     * @param $port TCP port to connect to, zero to use UNIX sockets
     * @param $role Role of this connection - "global" or "channel"
     * @return True if initialization succeeded, false if failed
     */
    static function Init($async = false, $addr = "", $port = 0, $role = "")
    {
	global $yate_stdin, $yate_stdout, $yate_stderr;
	global $yate_socket, $yate_debug, $yate_output;
	$yate_debug = false;
	$yate_stdin = false;
	$yate_stdout = false;
	$yate_stderr = false;
	$yate_output = false;
	$yate_socket = false;
	if ($addr) {
	    $ok = false;
	    if (!function_exists("socket_create")) {
		$yate_stderr = fopen("php://stderr","w");
		Yate::Output("PHP sockets missing, initialization failed");
		return false;
	    }
	    if ($port) {
		$yate_socket = @socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
		$ok = @socket_connect($yate_socket,$addr,$port);
	    }
	    else {
		$yate_socket = @socket_create(AF_UNIX,SOCK_STREAM,0);
		$ok = @socket_connect($yate_socket,$addr,$port);
	    }
	    if (($yate_socket === false) || !$ok) {
		$yate_socket = false;
		$yate_stderr = fopen("php://stderr","w");
		Yate::Output("Socket error, initialization failed");
		return false;
	    }
	    $yate_output = true;
	}
	else {
	    $yate_stdin = fopen("php://stdin","r");
	    $yate_stdout = fopen("php://stdout","w");
	    $yate_stderr = fopen("php://stderr","w");
	    $role = "";
	}
	flush();
	set_error_handler("_yate_error_handler");
	ob_implicit_flush(1);
	if ($async && function_exists("stream_set_blocking") && $yate_stdin)
	    stream_set_blocking($yate_stdin,false);
	if ($role)
	    _yate_print("%%>connect:$role\n");
	return true;
    }

}

/* Internal error handler callback - output plain text to stderr */
function _yate_error_handler($errno, $errstr, $errfile, $errline)
{
    $str = " [$errno] $errstr in $errfile line $errline\n";
    switch ($errno) {
	case E_USER_ERROR:
	    Yate::Output("PHP fatal: $str");
	    exit(1);
	    break;
	case E_WARNING:
	case E_USER_WARNING:
	    Yate::Output("PHP error: $str");
	    break;
	case E_NOTICE:
	case E_USER_NOTICE:
	    Yate::Output("PHP warning: $str");
	    break;
	default:
	    Yate::Output("PHP unknown error: $str");
    }
}

/* Internal function */
function _yate_print($str)
{
    global $yate_stdout, $yate_socket;
    if ($yate_socket)
	@socket_write($yate_socket, $str);
    else if ($yate_stdout)
	fputs($yate_stdout, $str);
}

/* Internal function */
function _yate_message_walk($item, $key, &$result)
{
    // workaround to pass by reference the 3rd parameter to message_walk:
    // the reference is placed in an array and the array is passed by value
    // taken from: http://hellsgate.online.ee/~glen/array_walk2.php
    $result[0] .= ':' . Yate::Escape($key,'=') . '=' . Yate::Escape($item);
}

/* vi: set ts=8 sw=4 sts=4 noet: */
?>