⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 phpagi.php

📁 voip开源项目
💻 PHP
📖 第 1 页 / 共 5 页
字号:
            $code = substr($code, 1);
            $command = false;
          }
          if($code == '')
            $command = true;
          elseif($mode == 'NUMERIC')
            $text .= $code;
          elseif($mode == 'UPPERCASE' && isset($alpha['k'.$code]))
            $text .= $alpha['k'.$code];
          elseif($mode == 'LOWERCASE' && isset($alpha['k'.$code]))
            $text .= strtolower($alpha['k'.$code]);
          elseif($mode == 'SYMBOL' && isset($symbol['k'.$code]))
            $text .= $symbol['k'.$code];
        }
        $this->say_punctuation($text);
      } while(substr($result['result'], -2) == '**');
      return $text;
    }

   /**
    * Say Puncutation in a string.
    *
    * @param string $text
    * @param string $escape_digits
    * @param integer $frequency
    * @return array, see evaluate for return information.
    */
    function say_punctuation($text, $escape_digits='', $frequency=8000)
    {
      for($i = 0; $i < strlen($text); $i++)
      {
        switch($text{$i})
        {
          case ' ': $ret .= 'SPACE ';
          case ',': $ret .= 'COMMA '; break;
          case '.': $ret .= 'PERIOD '; break;
          case '?': $ret .= 'QUESTION MARK '; break;
          case '!': $ret .= 'EXPLANATION POINT '; break;
          case ':': $ret .= 'COLON '; break;
          case ';': $ret .= 'SEMICOLON '; break;
          case '#': $ret .= 'POUND '; break;
          case '=': $ret .= 'EQUALS '; break;
          case '<': $ret .= 'LESS THAN '; break;
          case '(': $ret .= 'LEFT PARENTHESIS '; break;
          case '[': $ret .= 'LEFT BRACKET '; break;
          case '{': $ret .= 'LEFT BRACE '; break;
          case '@': $ret .= 'AT '; break;
          case '$': $ret .= 'DOLLAR SIGN '; break;
          case '&': $ret .= 'AMPERSAND '; break;
          case '%': $ret .= 'PERCENT '; break;
          case '>': $ret .= 'GREATER THAN '; break;
          case ')': $ret .= 'RIGHT PARENTHESIS '; break;
          case ']': $ret .= 'RIGHT BRACKET '; break;
          case '}': $ret .= 'RIGHT BRACE '; break;
          case '+': $ret .= 'PLUS '; break;
          case '-': $ret .= 'MINUS '; break;
          case '*': $ret .= 'ASTERISK '; break;
          case '/': $ret .= 'SLASH '; break;
          case "'": $ret .= 'SINGLE QUOTE '; break;
          case '`': $ret .= 'BACK TICK '; break;
          case '"': $ret .= 'QUOTE '; break;
          case '^': $ret .= 'CAROT '; break;
          case "\\": $ret .= 'BACK SLASH '; break;
          case '|': $ret .= 'BAR '; break;
          case '_': $ret .= 'UNDERSCORE '; break;
          case '~': $ret .= 'TILDE '; break;
          default: $ret .= $text{$i} . ' '; break;
        }
      }
      return $this->text2wav($ret, $escape_digits, $frequency);
    }

   /**
    * Create a new AGI_AsteriskManager.
    */
    function &new_AsteriskManager()
    {
      $this->asm = new AGI_AsteriskManager(NULL, $this->config);
      $this->asm->pagi =& $this;
      $this->config =& $this->asm->config;
      return $this->asm;
    }


   // *********************************************************************************************************
   // **                       PRIVATE                                                                       **
   // *********************************************************************************************************


   /**
    * Evaluate an AGI command.
    *
    * @access private
    * @param string $command
    * @return array ('code'=>$code, 'result'=>$result, 'data'=>$data)
    */
    function evaluate($command)
    {
      $broken = array('code'=>500, 'result'=>-1, 'data'=>'');

      // write command
      if(!@fwrite($this->out, trim($command) . "\n")) return $broken;
      fflush($this->out);

      // Read result.  Occasionally, a command return a string followed by an extra new line.
      // When this happens, our script will ignore the new line, but it will still be in the
      // buffer.  So, if we get a blank line, it is probably the result of a previous
      // command.  We read until we get a valid result or asterisk hangs up.  One offending
      // command is SEND TEXT.
      $count = 0;
      do
      {
        $str = trim(fgets($this->in, 4096));
      } while($str == '' && $count++ < 5);

      if($count >= 5)
      {
//        $this->conlog("evaluate error on read for $command");
        return $broken;
      }

      // parse result
      $ret['code'] = substr($str, 0, 3);
      $str = trim(substr($str, 3));

      if($str{0} == '-') // we have a multiline response!
      {
        $count = 0;
        $str = substr($str, 1) . "\n";
        $line = fgets($this->in, 4096);
        while(substr($line, 0, 3) != $ret['code'] && $count < 5)
        {
          $str .= $line;
          $line = fgets($this->in, 4096);
          $count = (trim($line) == '') ? $count + 1 : 0;
        }
        if($count >= 5)
        {
//          $this->conlog("evaluate error on multiline read for $command");
          return $broken;
        }
      }

      $ret['result'] = NULL;
      $ret['data'] = '';
      if($ret['code'] != AGIRES_OK) // some sort of error
      {
        $ret['data'] = $str;
        $this->conlog(print_r($ret, true));
      }
      else // normal AGIRES_OK response
      {
        $parse = explode(' ', trim($str));
        $in_token = false;
        foreach($parse as $token)
        {
          if($in_token) // we previously hit a token starting with ')' but not ending in ')'
          {
            $ret['data'] .= ' ' . trim($token, '() ');
            if($token{strlen($token)-1} == ')') $in_token = false;
          }
          elseif($token{0} == '(')
          {
            if($token{strlen($token)-1} != ')') $in_token = true;
            $ret['data'] .= ' ' . trim($token, '() ');
          }
          elseif(strpos($token, '='))
          {
            $token = explode('=', $token);
            $ret[$token[0]] = $token[1];
          }
          elseif($token != '')
            $ret['data'] .= ' ' . $token;
        }
        $ret['data'] = trim($ret['data']);
      }

      // log some errors
      if($ret['result'] < 0)
        $this->conlog("$command returned {$ret['result']}");

      return $ret;
    }

   /**
    * Log to console if debug mode.
    *
    * @example examples/ping.php Ping an IP address
    *
    * @param string $str
    * @param integer $vbl verbose level
    */
    function conlog($str, $vbl=1)
    {
      static $busy = false;

      if($this->config['phpagi']['debug'] != false)
      {
        if(!$busy) // no conlogs inside conlog!!!
        {
          $busy = true;
          $this->verbose($str, $vbl);
          $busy = false;
        }
      }
    }

   /**
    * Find an execuable in the path.
    *
    * @access private
    * @param string $cmd command to find
    * @param string $checkpath path to check
    * @return string the path to the command
    */
    function which($cmd, $checkpath=NULL)
    {
      global $_ENV;
      $chpath = is_null($checkpath) ? $_ENV['PATH'] : $checkpath;

      foreach(explode(':', $chpath) as $path)
        if(is_executable("$path/$cmd"))
          return "$path/$cmd";

      if(is_null($checkpath))
        return $this->which($cmd, '/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:'.
                                  '/usr/X11R6/bin:/usr/local/apache/bin:/usr/local/mysql/bin');
      return false;
    }

   /**
    * Make a folder recursively.
    *
    * @access private
    * @param string $folder
    * @param integer $perms
    */
    function make_folder($folder, $perms=0755)
    {
      $f = explode(DIRECTORY_SEPARATOR, $folder);
      $base = '';
      for($i = 0; $i < count($f); $i++)
      {
        $base .= $f[$i];
        if($f[$i] != '' && !file_exists($base))
          mkdir($base, $perms);
        $base .= DIRECTORY_SEPARATOR;
      }
    }	
  }

/**
 * error handler for phpagi.
 *
 * @param integer $level PHP error level
 * @param string $message error message
 * @param string $file path to file
 * @param integer $line line number of error
 * @param array $context variables in the current scope
 */
  function phpagi_error_handler($level, $message, $file, $line, $context)
  {
    if(ini_get('error_reporting') == 0) return; // this happens with an @

    @syslog(LOG_WARNING, $file . '[' . $line . ']: ' . $message);

    global $phpagi_error_handler_email;
    if(function_exists('mail') && !is_null($phpagi_error_handler_email)) // generate email debugging information
    {
      // decode error level
      switch($level)
      {
        case E_WARNING:
        case E_USER_WARNING:
          $level = "Warning";
          break;
        case E_NOTICE:
        case E_USER_NOTICE:
          $level = "Notice";
          break;
        case E_USER_ERROR:
          $level = "Error";
          break;
      }

      // build message
      $basefile = basename($file);
      $subject = "$basefile/$line/$level: $message";
      $message = "$level: $message in $file on line $line\n\n";

      if(function_exists('mysql_errno') && strpos(' '.strtolower($message), 'mysql'))
        $message .= 'MySQL error ' . mysql_errno() . ": " . mysql_error() . "\n\n";

      // figure out who we are
      if(function_exists('socket_create'))
      {
        $addr = NULL;
        $port = 80;
        $socket = @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
        @socket_connect($socket, '64.0.0.0', $port);
        @socket_getsockname($socket, $addr, $port);
        @socket_close($socket);
        $message .= "\n\nIP Address: $addr\n";
      }

      // include variables
      $message .= "\n\nContext:\n" . print_r($context, true);
      $message .= "\n\nGLOBALS:\n" . print_r($GLOBALS, true);
      $message .= "\n\nBacktrace:\n" . print_r(debug_backtrace(), true);

      // include code fragment
      if(file_exists($file))
      {
        $message .= "\n\n$file:\n";
        $code = @file($file);
        for($i = max(0, $line - 10); $i < min($line + 10, count($code)); $i++)
          $message .= ($i + 1)."\t$code[$i]";
      }

      // make sure message is fully readable (convert unprintable chars to hex representation)
      $ret = '';
      for($i = 0; $i < strlen($message); $i++)
      {
        $c = ord($message{$i});
        if($c == 10 || $c == 13 || $c == 9)
          $ret .= $message{$i};
        elseif($c < 16)
          $ret .= '\x0' . dechex($c);
        elseif($c < 32 || $c > 127)
          $ret .= '\x' . dechex($c);
        else
          $ret .= $message{$i};
      }
      $message = $ret;

      // send the mail if less than 5 errors
      static $mailcount = 0;
      if($mailcount < 5)
        @mail($phpagi_error_handler_email, $subject, $message);
      $mailcount++;
    }
  }
  $phpagi_error_handler_email = NULL;
?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -