diff --git a/md/install-zint.md b/md/install-zint.md index f1a9001..de008b1 100644 --- a/md/install-zint.md +++ b/md/install-zint.md @@ -12,3 +12,5 @@ cmake .. make make install ``` + +https://gist.github.com/anthonyaxenov/0ca957e4aab7bae3636746dc696346ec diff --git a/php/AMI.php b/php/AMI.php new file mode 100644 index 0000000..8eae8d9 --- /dev/null +++ b/php/AMI.php @@ -0,0 +1,158 @@ +connect(); + } + + /** + * Соединяет со шлюзом. + * Connects with gateway. + * + * @return bool + */ + public function connect() { + $this->connection = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + if ($this->connection === FALSE) { + echo "Cannot socket_create(): ".socket_strerror(socket_last_error())."\n"; + return FALSE; + } + if (socket_connect($this->connection, self::AMI_HOST, self::AMI_PORT) === FALSE) { + echo "Cannot socket_connect(): ".socket_strerror(socket_last_error())."\n"; + return FALSE; + } + return TRUE; + } + + /** + * Авторизирует на шлюзе. + * Authorizes on gateway. + * + * @return bool + */ + public function auth(): bool { + $command = [ + "Action: Login", + "Username: ".self::AMI_USER, + "Secret: ".self::AMI_PASS, + ]; + $this->command($command); + return $this->lastRead["Response"] === "Success"; + } + + /** + * Пишет данные в сокет. + * Writes data into socket. + * + * @param array $command + * @return int + */ + public function write(array $command): int { + $this->lastActionID = rand(10000000000000000, 99999999900000000); + $string = implode("\r\n", $command); + socket_write($this->connection, "ActionID: {$this->lastActionID}\r\n{$string}\r\n\r\n"); + return $this->lastActionID; + } + + /** + * Читает данные из сокета. + * Reads data from socket. + * + * @return array + */ + public function read(): array { + $result = []; + $data = socket_read($this->connection, 1024); + $data = str_replace("Asterisk Call Manager/1.1\r\n", '', $data); + $data = explode("\r\n", trim($data)); + foreach ($data as $key => $string) { + $pos = strpos($string, ':'); + $key = substr($string, 0, $pos); + $string = substr($string, $pos + 2, strlen($string)); + $result[$key] = $string; + } + return $this->lastRead = $result; + } + + /** + * Отправляет команду шлюзу и возвращает ответ. + * Sends command throuth socket and returns response. + * + * @param array $command + * @return array + */ + public function command(array $command): array { + $this->write($command); + return $this->read(); + } + + /** + * Отправляет СМС. + * Sends SMS. + * + * @param string $phone_number + * @param string $sms_text + * @param int $sim_id + * @return int SMS ID + */ + public function sendSMS(int $dispatch_id, string $phone_number, string $sms_text, int $sim_id): int { + $this->sms_data['sim_id'] = $sim_id; + $this->sms_data['phone_number'] = $phone_number; + $this->sms_data['text'] = $sms_text; + $this->sms_data['date_send'] = date('Y-m-d H:i:s'); + // $this->sms_data['date_status'] = "NULL"; + $this->sms_data['status'] = 0; + $smsid = $this->writeSmsReport(); + $command = [ + "Action: smscommand", + "command: gsm send sms ".($sim_id + 1)." {$phone_number} \"".urlencode($sms_text)."\" ID{$smsid}", + ]; + $this->command($command); + return $smsid; + } + + /** + * Отключает от шлюза и закрывает сокет. + * Disconnects from gateway and closes socket. + */ + public function disconnect() { + if ($this->connection) { + $this->write(["Action: Logoff"]); + socket_close($this->connection); + } + } +} diff --git a/php/DiskSpace.php b/php/DiskSpace.php index c03abf3..c800459 100644 --- a/php/DiskSpace.php +++ b/php/DiskSpace.php @@ -3,7 +3,8 @@ * Простой класс для получения данных о пространстве на диске, разделе, в директории * Может работать некорректно на shared-хостингах и при попытке получить данные * о корневом разделе. - * https://gist.github.com/anthonyaxenov/5db7be8b514f30ab2658150f2c018740 + * + * @see https://gist.github.com/anthonyaxenov/5db7be8b514f30ab2658150f2c018740 */ class DiskSpace { diff --git a/php/is-cli.php b/php/is-cli.php index ef8012c..a90b99a 100644 --- a/php/is-cli.php +++ b/php/is-cli.php @@ -4,6 +4,7 @@ * Checks if script running under php-cli * * @return bool + * @see https://gist.github.com/anthonyaxenov/c1b237af14ec91aead19be66cdfc29e3 */ function is_cli(): bool {