<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][14660] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=14660">14660</a></dd>
<dt>Author</dt> <dd>gled</dd>
<dt>Date</dt> <dd>2009-08-28 07:20:41 -0500 (Fri, 28 Aug 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>a little refactor, and added kill on hangup ;)</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunkcontribgledphpclassivrphp">freeswitch/trunk/contrib/gled/php/class.ivr.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchtrunkcontribgledphpREADME">freeswitch/trunk/contrib/gled/php/README</a></li>
<li><a href="#freeswitchtrunkcontribgledphpevent_listenerphp">freeswitch/trunk/contrib/gled/php/event_listener.php</a></li>
<li><a href="#freeswitchtrunkcontribgledphpprocesshandlerphp">freeswitch/trunk/contrib/gled/php/processhandler.php</a></li>
<li><a href="#freeswitchtrunkcontribgledphptestphp">freeswitch/trunk/contrib/gled/php/test.php</a></li>
</ul>

<h3>Property Changed</h3>
<ul>
<li><a href="#freeswitchtrunkcontribgledphp">freeswitch/trunk/contrib/gled/php/</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunkcontribgledphp"></a>
<div class="propset"><h4>Property changes: freeswitch/trunk/contrib/gled/php</h4>
<pre class="diff"><span>
<span class="cx">Name: svn:ignore
</span><span class="cx">   + pid.event_listener.php
</span></span></pre></div>
<a id="freeswitchtrunkcontribgledphpREADME"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/contrib/gled/php/README (0 => 14660)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/gled/php/README                                (rev 0)
+++ freeswitch/trunk/contrib/gled/php/README        2009-08-28 12:20:41 UTC (rev 14660)
</span><span class="lines">@@ -0,0 +1,33 @@
</span><ins>+Simple translation for AGI based scripts.
+
+* TODO:
+        + transform class.ivr.php to phpagi names so that scripts are not aware they're no more under *.
+        + add bindings to more functions ( record session, get variable, dial, ...).
+        + config file for event_listener ESL connection and other stuff...
+        + refactor check_hangup to already read data, and not reading and discarding stuff like that, it's awfull...
+
+* Files:
+        + class.ivr.php:
+                - simple bindings for IVR functions.
+
+        + processhandler.php:
+                - included by event_listener.php, ease the creation of always running daemons launched by cron, to be sure there's only
+                one instance running at the same time.
+
+        + event_listener.php:
+                - listen via event_socket ( inbound mode ) to CHANNEL_HANGUP_COMPLETE events, checking if variable_pid2kill is present, and 
+                if so,         will send a SIGHUP to that pid ( AGI behaviour ).
+
+* Usage example:
+        + launch event_listener.php via cron on the box running the scripts ( be sure that you edited the socket connection part ).
+        + launch fs_ivrd on that same box ( ./fs_ivrd -h host -p port ).
+        + write an extension to be handled by your scripts ( be carefull of the full path to the script ):
+                &lt;condition field=&quot;destination_number&quot; expression=&quot;^sockettest$&quot;&gt;
+                        &lt;action application=&quot;log&quot; data=&quot;WARNING ${caller_id_number}:${destination_number} EVENTSOCKET_TEST&quot;/&gt;
+                        &lt;action application=&quot;set&quot; data=&quot;ivr_path=/usr/local/src/freeswitch/contrib/gled/php/test.php&quot;/&gt;
+                        &lt;action application=&quot;socket&quot; data=&quot;127.0.0.1:9099 full&quot;/&gt;
+                        &lt;action application=&quot;hangup&quot;/&gt;
+                &lt;/condition&gt;
+        + be sure the test.php is executable ( chmod +755 test.php ).
+        + call that extention ;)
+        
</ins></span></pre></div>
<a id="freeswitchtrunkcontribgledphpclassivrphp"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/gled/php/class.ivr.php (14659 => 14660)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/gled/php/class.ivr.php        2009-08-28 09:58:06 UTC (rev 14659)
+++ freeswitch/trunk/contrib/gled/php/class.ivr.php        2009-08-28 12:20:41 UTC (rev 14660)
</span><span class="lines">@@ -1,17 +1,22 @@
</span><span class="cx"> &lt;?php
</span><ins>+require_once('ESL.php');
+
</ins><span class="cx"> declare(ticks=1);
</span><span class="cx"> pcntl_signal(SIGPIPE,  &quot;sig_fs&quot;);
</span><ins>+pcntl_signal(SIGHUP,  &quot;sig_fs&quot;);
+pcntl_signal(SIGTERM,  &quot;sig_fs&quot;);
</ins><span class="cx"> 
</span><span class="cx"> function sig_fs($signo) {
</span><del>-        file_put_contents('/devel/log/ivr.log',date(&quot;Y-m-d H:i:s&quot;).&quot;: BROKEN PIPE, call has been hung up\n&quot;,FILE_APPEND);
</del><ins>+        file_put_contents('/telephony/freeswitch/log/ivr.log',date(&quot;Y-m-d H:i:s&quot;).&quot; call has been hung up by $signo signal\n&quot;,FILE_APPEND);
</ins><span class="cx">         exit();
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> class IVR {
</span><span class="cx">         var $in = NULL;
</span><span class="cx">         var $out = NULL;
</span><ins>+        var $esl = NULL;
</ins><span class="cx">         var $con_data = array();
</span><del>-        var $logfile = '/devel/log/ivr.log';
</del><ins>+        var $logfile = '/telephony/freeswitch/log/ivr.log';
</ins><span class="cx"> 
</span><span class="cx">         function IVR() {
</span><span class="cx">                 ob_implicit_flush(true);
</span><span class="lines">@@ -28,10 +33,11 @@
</span><span class="cx">                 }
</span><span class="cx">                 $this-&gt;con_data = $this-&gt;parse_response($buf);
</span><span class="cx">                 $this-&gt;log(&quot;New call uuid:{$this-&gt;con_data['channel-unique-id']}&quot;);
</span><del>-/*
-                fwrite($this-&gt;out,&quot;myevents\n\n&quot;);
-                $buf = $this-&gt;wait_answer();
-*/
</del><ins>+
+                /*
+                        Set pid to kill on hangup :)
+                */
+                $this-&gt;set_var('pid2kill',getmypid());
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         function log($str) {
</span><span class="lines">@@ -137,8 +143,12 @@
</span><span class="cx">                         return $ret;
</span><span class="cx">                 $tmp = explode(&quot;\n&quot;,$tmp);
</span><span class="cx">                 foreach ($tmp as $line) {
</span><del>-                        list($key,$val) = explode(':',$line);
-                        $ret[strtolower($key)] = strtolower(trim( urldecode($val) ));
</del><ins>+                        if ( eregi(':',$line) ) {
+                                list($key,$val) = explode(':',$line);
+                                $ret[strtolower($key)] = strtolower(trim( urldecode($val) ));
+                        }
+                        else
+                                mail('tristan@telemaque.fr',&quot;Wrong line&quot;,$line);
</ins><span class="cx">                 }
</span><span class="cx">                 return $ret;
</span><span class="cx">         }
</span><span class="lines">@@ -167,6 +177,12 @@
</span><span class="cx">                 }        
</span><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        function set_var($var,$value) {
+                $ret = $this-&gt;parse_response($this-&gt;execute_wait('Set',&quot;$var=$value&quot;));
+                if ( ! $this-&gt;check_retcode($ret) )
+                        $this-&gt;log(&quot;Huhooo Set_Var wasn't ack'd&quot;);
+        }
+
</ins><span class="cx">         function hangup() {
</span><span class="cx">                 $ret = $this-&gt;parse_response($this-&gt;send_cmd('hangup'));
</span><span class="cx">                 if ( ! $this-&gt;check_retcode($ret) )
</span></span></pre></div>
<a id="freeswitchtrunkcontribgledphpevent_listenerphp"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/contrib/gled/php/event_listener.php (0 => 14660)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/gled/php/event_listener.php                                (rev 0)
+++ freeswitch/trunk/contrib/gled/php/event_listener.php        2009-08-28 12:20:41 UTC (rev 14660)
</span><span class="lines">@@ -0,0 +1,29 @@
</span><ins>+&lt;?php
+
+define (&quot;PID&quot;,&quot;pid.event_listener.php&quot;);
+require_once(&quot;processhandler.php&quot;);
+require_once(&quot;ESL.php&quot;);
+
+if ( ProcessHandler::isActive () )
+        die();
+else {
+        /* Init stuff */
+        ProcessHandler::activate ();
+
+        $esl = new eslConnection('127.0.0.1', '8021', 'ClueCon');
+        $esl-&gt;send('event PLAIN CHANNEL_HANGUP_COMPLETE');
+
+        while ($esl-&gt;connected() &amp;&amp; $e = $esl-&gt;recvEvent() ) {
+                switch($e-&gt;getType()) {
+                        case 'CHANNEL_HANGUP_COMPLETE':
+                                $pid2kill = $e-&gt;getHeader('variable_pid2kill');
+                                if ($pid2kill)
+                                        posix_kill($pid2kill,SIGHUP);
+                        default:
+                                usleep(500);
+                        break;
+                }
+        }
+}
+
+?&gt;
</ins></span></pre></div>
<a id="freeswitchtrunkcontribgledphpprocesshandlerphp"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/contrib/gled/php/processhandler.php (0 => 14660)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/gled/php/processhandler.php                                (rev 0)
+++ freeswitch/trunk/contrib/gled/php/processhandler.php        2009-08-28 12:20:41 UTC (rev 14660)
</span><span class="lines">@@ -0,0 +1,65 @@
</span><ins>+&lt;?php
+
+if (!defined ('PID'))
+  {
+    define (&quot;PID&quot;, &quot;/tmp/process_pid.php&quot;);
+  }
+class ProcessHandler
+{
+  function isActive ()
+  {
+    $pid = ProcessHandler::getPID ();
+    if ($pid == null)
+      {
+        $ret = false;
+      }
+    else
+      {
+        $ret = posix_kill ($pid, 0);
+      }
+    if ($ret == false)
+      {
+        ProcessHandler::activate ();
+      }
+    return $ret;
+  }
+  function activate ()
+  {
+    $pidfile = PID;
+    $pid = ProcessHandler::getPID ();
+    if ($pid != null &amp;&amp; $pid == getmypid ())
+      {
+        return &quot;Already running!\n&quot;;
+      }
+    else
+      {
+        $fp = fopen ($pidfile, &quot;w+&quot;);
+        if ($fp)
+          {
+            if (!fwrite ($fp, &quot;&lt;&quot;.&quot;?php\n\$pid = &quot;.getmypid ().&quot;;\n?&quot;.&quot;&gt;&quot;))
+              {
+                die (&quot;Can not create pid file!\n&quot;);
+              }
+            fclose ($fp);
+          }
+        else
+          {
+            die (&quot;Can not create pid file!\n&quot;);
+          }
+      }
+  }
+  function getPID ()
+  {
+    if (file_exists (PID))
+      {
+        require (PID);
+        return $pid;
+      }
+    else
+      {
+        return null;
+      }
+  }
+}
+?&gt;
+
</ins></span></pre></div>
<a id="freeswitchtrunkcontribgledphptestphp"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/contrib/gled/php/test.php (0 => 14660)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/gled/php/test.php                                (rev 0)
+++ freeswitch/trunk/contrib/gled/php/test.php        2009-08-28 12:20:41 UTC (rev 14660)
</span><span class="lines">@@ -0,0 +1,15 @@
</span><ins>+#!/usr/bin/php -q
+&lt;?php
+require_once('class.ivr.php');
+
+$ivr = new IVR();
+$ivr-&gt;answer();
+$ivr-&gt;log('Answered call');
+sleep(10);
+$ivr-&gt;log('Calling ivr_sleep');
+$ivr-&gt;ivr_sleep(50);
+$ivr-&gt;log('Hanging up');
+$ivr-&gt;hangup();
+$ivr-&gt;close();
+
+?&gt;
</ins><span class="cx">Property changes on: freeswitch/trunk/contrib/gled/php/test.php
</span><span class="cx">___________________________________________________________________
</span><span class="cx">Name: svn:executable
</span><span class="cx">   + *
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>