00001 #!/usr/bin/php -q
00002 <?php
00003 {
00004
00005
00006
00007
00008
00010 $host = "192.168.0.36";
00011 $dbname = "bulmafact";
00012 $port = "5432";
00013 $user = "tborras";
00014 $password = "tborras";
00015
00016
00017
00018 $parm_error_log = '/tmp/wakeup.log';
00019
00020
00021 $parm_debug_on = 1;
00022
00023
00024 $parm_temp_dir = '/tmp';
00025
00026
00027 $parm_call_dir = '/var/spool/asterisk/outgoing';
00028
00029
00030 $parm_maxretries = 3;
00031
00032
00033 $parm_waittime = 60;
00034
00035
00036 $parm_retrytime = 60;
00037
00038
00039 $parm_wakeupcallerid = '"WakeUp" <*62>';
00040
00041
00042
00043
00044
00045
00046 $parm_chan_ext = 0;
00047
00048
00049
00050 $parm_application = 'MusicOnHold';
00051 $parm_data = '';
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 GLOBAL $stdin, $stdout, $stdlog, $result, $parm_debug_on, $parm_test_mode;
00064
00065
00066 ob_implicit_flush(false);
00067 set_time_limit(30);
00068 error_reporting(0);
00069
00070 $stdin = fopen( 'php:
00071 $stdout = fopen( 'php:
00072
00073
00074
00075 if ($parm_debug_on)
00076 {
00077 $stdlog = fopen( $parm_error_log, 'w' );
00078 fputs( $stdlog, "---Start---\n" );
00079 }
00080
00081
00082
00083
00084 while ( !feof($stdin) )
00085 {
00086 $temp = fgets( $stdin );
00087
00088 if ($parm_debug_on)
00089 fputs( $stdlog, $temp );
00090
00091
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
00106
00107
00108
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
00119 $callerid = $agivar[agi_callerid];
00120
00121
00122 if (preg_match('/<([ 0-9]+)>/', $callerid, $match) )
00123 {
00124 $cidn = trim($match[1]);
00125 }
00126 else
00127 {
00128 if (preg_match('/([0-9]+)/', $callerid, $match) )
00129 {
00130 $cidn = trim($match[1]);
00131 }
00132 else
00133 $cidn = -1;
00134 }
00135
00136
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
00146
00147
00148
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
00156 if (strstr( $file, $dir_check ) )
00157 $out[$outc++] = $file;
00158 }
00159 closedir( $dir_handle );
00160
00161
00162
00163
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
00178 if ( strlen( $rc[result]) < 4 || $rc[result] < 0) {
00179 $rc[result] = 0;
00180 $rc = execute_agi( "STREAM FILE custom/contrasenyaincorrecta \"\" ");
00181 }
00182
00184 if ( strlen($rc[result])>= 4) {
00185
00186 $trabajador = $rc[result];
00187 if (comprueba_validacion($trabajador, $almacen) == FALSE) {
00188 $rc = execute_agi( "STREAM FILE custom/contrasenyaincorrecta \"\" ");
00189 $rc[result] = 0;
00190 }
00191 }
00192
00193 }
00194 }
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 }
00268 }
00269 return TRUE;
00270 }
00271
00272
00273
00274
00275
00276
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 ?>