password.php

Go to the documentation of this file.
00001 #!/usr/bin/php -q
00002 <?php
00003 {
00004     // Version 1.11 2005-Nov-15
00005     // ------------
00006     // CONFIG Parms
00007     // ------------
00008 
00010         $host = "192.168.0.36";
00011         $dbname = "bulmafact";
00012         $port   = "5432";
00013         $user   = "tborras";
00014         $password = "tborras";
00015 
00016     
00017     // If the application is having problems you can log to this file
00018     $parm_error_log = '/tmp/wakeup.log';
00019     
00020     // Set to 1 to turn on the log file
00021     $parm_debug_on = 1; 
00022     
00023     // This is where the Temporary WakeUp Call Files will be created
00024     $parm_temp_dir = '/tmp';
00025     
00026     // This is where the WakeUp Call Files will be moved to when finished
00027     $parm_call_dir = '/var/spool/asterisk/outgoing';
00028 
00029     // How many times to try the call
00030     $parm_maxretries = 3;
00031     
00032     // How long to keep the phone ringing
00033     $parm_waittime = 60;                // How long to keep the phone ringing
00034     
00035     // Number of seconds between retries
00036     $parm_retrytime = 60;
00037     
00038     // Caller ID of who the wakeup call is from Change this to the extension you want to display on the phone
00039     $parm_wakeupcallerid = '"WakeUp" <*62>';
00040     
00041     
00042     // Set to 1 to use the Channel
00043     // Set to 0 for Caller ID,  Caller IS is assumed just a number ### or "Name Name" <##>
00044     // The big difference is using caller ID will call ANY phone with that extension number
00045     // Where using Channel will only wake up the one specific phone
00046     $parm_chan_ext = 0;
00047 
00048     // ----------------------------------------------------
00049     // Which application to run when the call is connected.  
00050     $parm_application = 'MusicOnHold';
00051         $parm_data = '';
00052 
00053         // -- Use this for the ANNOY application
00054         //$parm_application = 'AGI';
00055         //$parm_data = 'wakeconfirm.agi';
00056     // ----------------------------------------------------
00057 
00058     
00059     //-------------------
00060     // END CONFIG PARMS
00061     //-------------------
00062     
00063     GLOBAL      $stdin, $stdout, $stdlog, $result, $parm_debug_on, $parm_test_mode;
00064     
00065     // These setting are on the WIKI pages http://www.voip-info.org
00066     ob_implicit_flush(false);
00067     set_time_limit(30);
00068     error_reporting(0);
00069     
00070     $stdin = fopen( 'php://stdin', 'r' );
00071     $stdout = fopen( 'php://stdout', 'w' );
00072     
00073     // You will see a whole bunch of this its for development or if you change anything and
00074     // want to write to a log file in the TMP directory
00075     if ($parm_debug_on)
00076     {
00077         $stdlog = fopen( $parm_error_log, 'w' );
00078         fputs( $stdlog, "---Start---\n" );
00079     }
00080     
00081     
00082     // ASTERISK * Sends in a bunch of variables, This accepts them and puts them in an array
00083     // http://www.voip-info.org/tiki-index.php?page=Asterisk%20AGI%20php
00084     while ( !feof($stdin) ) 
00085     {
00086         $temp = fgets( $stdin );
00087         
00088         if ($parm_debug_on)
00089             fputs( $stdlog, $temp );
00090         
00091         // Strip off any new-line characters
00092         $temp = str_replace( "\n", "", $temp );
00093         
00094         $s = explode( ":", $temp );
00095         $agivar[$s[0]] = trim( $s[1] );
00096         if ( ( $temp == "") || ($temp == "\n") )
00097         {
00098             break;
00099         }
00100     } 
00101     
00102     $almacen = $agivar[agi_callerid];
00103     
00104     
00105     // There are two ways to contact a phone, by its channel or by its local 
00106     // extension number.  This next session will extract both sides
00107     
00108     // split the Channel  SIP/11-3ef4  or Zap/4-1 into is components
00109     $channel = $agivar[agi_channel];
00110     if (preg_match('.^([a-zA-Z]+)/([0-9]+)([0-9a-zA-Z-]*).', $channel, $match) )
00111     {
00112         $sta = trim($match[2]);
00113         $chan = trim($match[1]);
00114     }
00115     
00116     
00117     
00118     // Go Split the Caller ID into its components
00119     $callerid = $agivar[agi_callerid];
00120     
00121     // First look for the Extension in <####> 
00122     if (preg_match('/<([ 0-9]+)>/', $callerid, $match) )
00123     {
00124         $cidn = trim($match[1]);
00125     }
00126     else        // Did not find the <##...> look for the first number in the string to use as CID
00127     {
00128         if (preg_match('/([0-9]+)/', $callerid, $match) )
00129         {
00130             $cidn = trim($match[1]);
00131         }
00132         else
00133             $cidn = -1;         // I'm saying an error no caller id # found
00134     }
00135     
00136     // Check if we have an outstanding Wakeup Call file
00137     if ( $parm_chan_ext )
00138         $dir_check = "$chan.$sta.call";
00139     else
00140         $dir_check = "ext.$cidn.call";
00141     
00142     if ($parm_debug_on)
00143         fputs( $stdlog, "Checking Directory [$parm_call_dir] Check=[$dir_check]\n" );
00144     
00145     // I started to think we could have many outstanding wakup calls, but then
00146     // it got very confusing on how to delete just one of them.  I wasn't about
00147     // to prompt each and every one.  So I went back to JUST ONE wakeup call
00148     // But this will get a list of all of them incase of problems
00149     $outc=0;
00150     $dir_handle = opendir( $parm_call_dir );
00151     while( $file = readdir($dir_handle ) )   {
00152         if ($parm_debug_on)
00153             fputs( $stdlog, "File=$file\n" );
00154         
00155         // Check if we have an outstanding wakup call
00156         if (strstr( $file, $dir_check ) )
00157             $out[$outc++] = $file;
00158     }
00159     closedir( $dir_handle );
00160     
00161     
00162     //=========================================================================
00163     // This is where we interact with the caller.  Answer the phone and so on
00164     //=========================================================================
00165     
00166     
00167     $rc = execute_agi( "ANSWER ");
00168     
00169     sleep(1);
00170     
00171             $rc = execute_agi( "STREAM FILE custom/bienvenido_control \"0123456789\" ");
00172             $rc[result] = 0;
00173             while ( !$rc[result] ) {
00174                 $rc = execute_agi( "GET DATA custom/porfavorcontrasenya 15000 4 ");
00175                 
00176                 if ( $rc[result] != -1 )      {
00177                     // They entered 4 digits,  check if its a valid time or they hung up
00178                     if ( strlen( $rc[result]) < 4 || $rc[result] < 0)  {
00179                         $rc[result] = 0;
00180                         $rc = execute_agi( "STREAM FILE custom/contrasenyaincorrecta \"\" ");
00181                     } // end if
00182                     
00184                     if ( strlen($rc[result])>= 4) {
00185                         // Save the time entered
00186                         $trabajador = $rc[result];
00187                         if (comprueba_validacion($trabajador, $almacen) == FALSE) {
00188                            $rc = execute_agi( "STREAM FILE custom/contrasenyaincorrecta \"\" ");
00189                            $rc[result] = 0;
00190                         } // end if
00191                     } // end if
00192                     
00193                 } // end if
00194             } // end while
00195             
00196             $rc[result] = 0;
00197 
00198     if ($param_debug_on)
00199        fputs ($stdlog, "---DATABASE ---\n");
00200     $err = genera_validacion($trabajador, $almacen);
00201 
00202 
00203     if ( !$rc[result]) 
00204         $rc = execute_agi( "SAY DIGITS $trabajador \"\" ");
00205     if ( !$src[result])
00206         $rc = execute_agi( "STREAM FILE custom/diganombre \"\" ");
00207 
00208     $cad = $trabajador."_".$almacen."_".date("m-d-y_H:i:s");
00209 
00210     if ( !$rc[result])
00211         $rc = execute_agi( "RECORD FILE /tmp/password$cad wav \"0123456789\" 5000 ");
00212     if ( !$rc[result] )
00213         $rc = execute_agi( "STREAM FILE custom/gracias \"\" ");
00214 
00215     if ( !$rc[result] )
00216         $rc = execute_agi( "HANGUP");
00217     if ($parm_debug_on)
00218         fclose($stdlog);
00219         
00220     exit;
00221 }
00222 
00223 
00224 
00225 function genera_validacion($trab, $alm) {
00226     GLOBAL      $stdin, $stdout, $stdlog, $parm_debug_on, $host, $dbname, $port, $user, $password;
00227     fflush( $stdout );
00228     if ($parm_debug_on)
00229         fputs( $stdlog,"---- DATABASE ----". $trab ."--".$alm. "\n" );
00230     
00231         $conn = pg_connect("host=".$host." dbname=".$dbname." port=".$port." user=".$user." password=".$password);
00232         if (!$conn) {
00233         echo "An error occured.\n";
00234         exit;
00235         } else {
00236         pg_exec($conn, "SET datestyle TO 'European'");
00237         pg_exec($conn, "SET datestyle TO 'SQL'");
00238         pg_exec($conn, "SET client_encoding to 'UTF-8'");
00239         pg_exec($conn, "SELECT validacionasterisk('$trab','$alm')");
00240         }
00241         return 0;
00242 }
00243 
00244 
00245 
00246 function comprueba_validacion($trab, $alm) {
00247 
00248     GLOBAL      $stdin, $stdout, $stdlog, $parm_debug_on, $host, $dbname, $port, $user, $password;
00249     fflush( $stdout );
00250     if ($parm_debug_on)
00251         fputs( $stdlog,"---- DATABASE ----". $trab ."--".$alm. "\n" );
00252     
00253         $conn = pg_connect("host=".$host." dbname=".$dbname." port=".$port." user=".$user." password=".$password);
00254         if (!$conn) {
00255         echo "An error occured.\n";
00256         exit;
00257         } else {
00258         pg_exec($conn, "SET datestyle TO 'European'");
00259         pg_exec($conn, "SET datestyle TO 'SQL'");
00260         pg_exec($conn, "SET client_encoding to 'UTF-8'");
00261 
00262         $query = "SELECT * from cuadrante NATURAL LEFT JOIN horario NATURAL LEFT JOIN trabajador NATURAL LEFT JOIN ALMACEN where fechacuadrante  = now()::date AND passasterisktrabajador='".$trab."' AND extasteriskalmacen='".$alm."'  ";
00263         fputs( $stdlog, $query."\n");
00264         $result = pg_query($conn, $query );
00265         if (pg_num_rows($result) == 0) {
00266              return FALSE;
00267         } // end if
00268         } // end if
00269         return TRUE;
00270 }
00271 
00272 
00273 
00274 //--------------------------------------------------
00275 // This function will send out the command and get 
00276 //      the response back
00277 //--------------------------------------------------
00278 function execute_agi( $command )  {
00279     GLOBAL      $stdin, $stdout, $stdlog, $parm_debug_on;
00280     
00281     fputs( $stdout, $command . "\n" );
00282     fflush( $stdout );
00283     if ($parm_debug_on)
00284         fputs( $stdlog, $command . "\n" );
00285     
00286     $resp = fgets( $stdin, 4096 );
00287     
00288     if ($parm_debug_on)
00289         fputs( $stdlog, $resp );
00290     
00291     if ( preg_match("/^([0-9]{1,3}) (.*)/", $resp, $matches) )  {
00292         if (preg_match('/result=([-0-9a-zA-Z]*)(.*)/', $matches[2], $match))  {
00293             $arr['code'] = $matches[1];
00294             $arr['result'] = $match[1];
00295             if (isset($match[3]) && $match[3])
00296                 $arr['data'] = $match[3];
00297             return $arr;
00298         }  else  {
00299             if ($parm_debug_on)
00300                 fputs( $stdlog, "Couldn't figure out returned string, Returning code=$matches[1] result=0\n" ); 
00301             $arr['code'] = $matches[1];
00302             $arr['result'] = 0;
00303             return $arr;
00304         }
00305         }   else    {
00306         if ($parm_debug_on)
00307             fputs( $stdlog, "Could not process string, Returning -1\n" );
00308         $arr['code'] = -1;
00309         $arr['result'] = -1;
00310         return $arr;
00311         }
00312 } 
00313 ?>

Generated on Sat Dec 15 00:01:05 2007 for BulmaGes by  doxygen 1.5.1