PHP lint, compatibility check with php 7, README

This commit is contained in:
Juan 2018-08-02 13:43:57 +02:00
parent 7b1548c1d5
commit edc0553712
21 changed files with 197 additions and 200 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
config.my.php

6
README.md Normal file
View File

@ -0,0 +1,6 @@
## Getting Started
Test the incoming calls script.
```
$ cat test.txt | php incoming.php
```

67
agi.php
View File

@ -1,55 +1,65 @@
<?php
class Agi
{
private static $initialized = FALSE;
private static $agivars = array ();
class Agi {
private static $initialized = false;
private static $agivars = [];
static function init ()
{
/**
* Initializes the AGI class, must be called once when the
* application starts.
*/
static function init() {
if (self::$initialized)
return;
self::$initialized = TRUE;
pcntl_signal (SIGHUP, SIG_IGN);
pcntl_signal (SIGTERM, SIG_IGN);
while (!feof (STDIN))
{
$agivar = trim (fgets (STDIN, 4096));
self::$initialized = true;
pcntl_signal(SIGHUP, SIG_IGN);
pcntl_signal(SIGTERM, SIG_IGN);
while (!feof(STDIN)) {
$agivar = trim(fgets(STDIN, 4096));
if ($agivar === '')
break;
$agivar = explode (':', $agivar);
$agivar = explode(':', $agivar);
if (count($agivar) == 2)
self::$agivars[$agivar[0]] = trim ($agivar[1]);
self::$agivars[$agivar[0]] = trim($agivar[1]);
}
}
static function get ($agivar)
{
if (self::$initialized && isset (self::$agivars[$agivar]))
/**
* Returns the value for an AGI variable.
*
* @param string $agivar The variable name
* @return string The variable value
*/
static function get($agivar) {
if (self::$initialized && isset(self::$agivars[$agivar]))
return self::$agivars[$agivar];
else
return NULL;
return null;
}
static function exec ($cmd, &$result = NULL)
{
/**
* Sends a command to the
*
* @param string $cmd The command
* @param string $result The result string
* @return int The command status
*/
static function exec($cmd, &$result = null) {
if (!self::$initialized)
return -1;
fwrite (STDOUT, "$cmd\n");
fflush (STDOUT);
fwrite(STDOUT, "$cmd\n");
fflush(STDOUT);
$res = trim (fgets (STDIN, 4096));
$res = trim(fgets(STDIN, 4096));
if (preg_match ("/^([0-9]{1,3}) (.*)/", $res, $matches))
{
if (preg_match ('/^result=([0-9\-]*)( ?\((.*)\))?$/', $matches[2], $match))
{
if (preg_match("/^([0-9]{1,3})(.*)/", $res, $matches)) {
if (preg_match('/^result=([0-9\-]*)( ?\((.*)\))?$/', $matches[2], $match)) {
$ret = (int) $match[1];
if ($num > 0)
$result = $match[3];
@ -60,4 +70,3 @@ class Agi
return -1;
}
}

View File

@ -4,19 +4,17 @@
*
* Do not modify this file! Instead, copy it to config.my.php and make
* your changes there.
**/
*/
return [
/**
* Database parameters.
**/
'db' => [
'host' => 'localhost'
,'port' => 3306
,'schema' => 'pbx'
,'user' => 'pbx'
,'pass' => ''
]
/**
* Database parameters.
*/
'db' => [
'host' => 'localhost'
,'port' => 3306
,'schema' => 'pbx'
,'user' => 'pbx'
,'pass' => ''
]
];

2
copyright.txt Executable file → Normal file
View File

@ -1,4 +1,4 @@
Copyright (C) 2015 - Juan Ferrer Toribio
Copyright (C) 2018 - Juan Ferrer Toribio
This package is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

2
debian/changelog vendored
View File

@ -1,4 +1,4 @@
vn-asterisk (1.1.1) stable; urgency=low
vn-asterisk (1.1.2) stable; urgency=low
* Initial Release.

4
debian/control vendored
View File

@ -4,8 +4,8 @@ Maintainer: Juan Ferrer Toribio <juan@verdnatura.es>
Build-Depends: build-essential, debhelper
Standards-Version: 3.9.3
Section: misc
Homepage: http://www.verdnatura.es
Vcs-Git: git://www.verdnatura.es/var/git/vn-asterisk
Homepage: https://verdnatura.es/
Vcs-Git: http://git.verdnatura.es/vn-asterisk
Package: vn-asterisk
Architecture: all

2
debian/copyright vendored
View File

@ -1,6 +1,6 @@
Format: http://dep.debian.net/deps/dep5
Name: vn-asterisk
Source: git://www.verdnatura.es/var/git/
Source: http://git.verdnatura.es/
Files: *
Copyright: 2011-2015 Juan Ferrer Toribio <juan@verdnatura.es>

1
debian/postinst vendored
View File

@ -2,4 +2,3 @@
#chown asterisk:asterisk /etc/asterisk/extensions.conf
/etc/init.d/asterisk reload

1
debian/postrm vendored
View File

@ -1,4 +1,3 @@
#!/bin/bash
/etc/init.d/asterisk reload

1
debian/rules vendored
View File

@ -2,4 +2,3 @@
%:
dh $@

View File

@ -2,9 +2,7 @@
require_once (__DIR__.'/../php-vn-lib/env.php');
set_include_path (__DIR__.PATH_SEPARATOR.get_include_path ());
set_include_path(__DIR__.PATH_SEPARATOR.get_include_path());
const _DEV_MODE = TRUE;
const _CONFIG_DIR = __DIR__.'/../../.config';
const _DEV_MODE = true;
const _LOG_DIR = '/tmp';

View File

@ -1,69 +1,67 @@
#!/usr/bin/php -q
<?php
require_once ('agi.php');
@include_once __DIR__.'/env.php';
if (file_exists(__DIR__.'/env.php'))
include_once __DIR__.'/env.php';
require_once 'agi.php';
require_once 'vn-autoload.php';
$app = new Vn\Lib\App ('vn-asterisk');
$app->init ();
$db = $app->getSysConn ();
$app = new Vn\Lib\App('vn-asterisk');
$app->init();
$db = $app->getSysConn();
Agi::init ();
Agi::init();
// Formats the caller phone number
$callerId = str_replace ('+', '00', Agi::get ('agi_callerid'));
$callerId = str_replace('+', '00', Agi::get('agi_callerid'));
$countryPrefix = $db->getValue ('SELECT countryPrefix FROM config');
$prefixLen = strlen ($countryPrefix);
$countryPrefix = $db->getValue('SELECT countryPrefix FROM config');
$prefixLen = strlen($countryPrefix);
if (substr ($callerId, 0, $prefixLen) === $countryPrefix)
$callerId = substr ($callerId, $prefixLen);
if (substr($callerId, 0, $prefixLen) === $countryPrefix)
$callerId = substr($callerId, $prefixLen);
// Checks if phone number is on the blacklist
if ($db->getValue ('SELECT COUNT(*) > 0 FROM blacklist WHERE phone = #', [$callerId]))
{
Agi::exec ('HANGUP');
exit ();
if ($db->getValue('SELECT COUNT(*) > 0 FROM blacklist WHERE phone = #', [$callerId])) {
Agi::exec('HANGUP');
exit;
}
// Checks whether its a festive day
$sundayFestive = $db->getValue ('SELECT sundayFestive FROM config');
$sundayFestive = $db->getValue('SELECT sundayFestive FROM config');
if (date ('N') == 7 && $sundayFestive)
{
Agi::exec ('SET VARIABLE MACRO playback');
Agi::exec ('SET VARIABLE ARG1 out-of-ours');
exit ();
if (date('N') == 7 && $sundayFestive) {
Agi::exec('SET VARIABLE MACRO playback');
Agi::exec('SET VARIABLE ARG1 out-of-ours');
exit;
}
// Checks schedules
$hasSchedule = $db->getValue (
$hasSchedule = $db->getValue(
'SELECT COUNT(*) > 0 FROM pbx.schedule
WHERE weekDay = WEEKDAY(CURDATE())
AND CURTIME() BETWEEN timeStart AND timeEnd'
);
if ($hasSchedule)
{
Agi::exec ('SET VARIABLE MACRO queue');
Agi::exec ('SET VARIABLE ARG1 1200');
exit ();
if ($hasSchedule) {
Agi::exec('SET VARIABLE MACRO queue');
Agi::exec('SET VARIABLE ARG1 1200');
exit;
}
// Gets the customer from the phone number
$customer = $db->getValue ('SELECT clientFromPhone(#)', [$callerId]);
$customer = $db->getValue('SELECT clientFromPhone(#)', [$callerId]);
if ($customer)
{
if ($customer) {
// Gets the customer salesperson extension
$extension = $db->getValue (
$extension = $db->getValue(
'SELECT s.extension
FROM sip s
JOIN vn2008.Trabajadores t ON t.user_id = s.user_id
@ -71,16 +69,11 @@ if ($customer)
,[$customer]
);
if ($extension)
{
Agi::exec ('SET VARIABLE MACRO exten');
Agi::exec ("SET VARIABLE ARG1 $extension");
}
else
{
Agi::exec ('SET VARIABLE MACRO playback');
Agi::exec ('SET VARIABLE ARG1 busy');
if ($extension) {
Agi::exec('SET VARIABLE MACRO exten');
Agi::exec("SET VARIABLE ARG1 $extension");
} else {
Agi::exec('SET VARIABLE MACRO playback');
Agi::exec('SET VARIABLE ARG1 busy');
}
}
?>

View File

@ -4,46 +4,45 @@
* @param v_phone The caller phone number
* @return The customer id or %NULL if customer not exists or is inactive
**/
USE pbx;
DROP FUNCTION IF EXISTS customer_from_phone;
DROP FUNCTION IF EXISTS pbx.clientFromPhone;
DELIMITER $$
CREATE FUNCTION customer_from_phone (v_phone VARCHAR(255))
RETURNS INT
DETERMINISTIC
CREATE FUNCTION pbx.clientFromPhone (vPhone VARCHAR(255))
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE v_customer INT DEFAULT NULL;
SET @phone = v_phone COLLATE 'utf8_unicode_ci';
-- Searchs a customer associated to the phone number
DECLARE vClient INT DEFAULT NULL;
-- SET vPhone = vPhone COLLATE 'utf8_unicode_ci';
-- Searchs a customer associated to the phone number
DROP TEMPORARY TABLE IF EXISTS tmp.customer;
CREATE TEMPORARY TABLE tmp.customer
ENGINE = MEMORY
SELECT id_cliente customer
FROM vn2008.Clientes c
WHERE telefono = @phone
OR movil = @phone
WHERE telefono = vPhone
OR movil = vPhone
UNION
SELECT id_cliente
FROM vn2008.Consignatarios
WHERE telefono = @phone
OR movil = @phone
WHERE telefono = vPhone
OR movil = vPhone
UNION
SELECT r.id_cliente
FROM vn2008.Relaciones r
JOIN vn2008.Contactos c ON r.Id_Contacto = c.Id_Contacto
WHERE telefono = @phone
OR movil = @phone;
WHERE c.telefono = vPhone
OR c.movil = vPhone;
SELECT t.customer INTO v_customer
SELECT t.customer INTO vClient
FROM tmp.customer t
JOIN vn2008.Clientes c ON c.id_cliente = t.customer
WHERE c.activo
LIMIT 1;
LIMIT 1;
DROP TEMPORARY TABLE tmp.customer;
RETURN v_customer;
RETURN vClient;
END$$
DELIMITER ;

View File

@ -4,18 +4,17 @@
* @param v_extension The extension to check format
* @return %TRUE if it's well formated
**/
USE pbx;
DROP PROCEDURE IF EXISTS extension_is_valid;
DROP PROCEDURE IF EXISTS pbx.extensionIsValid;
DELIMITER $$
CREATE PROCEDURE extension_is_valid (v_extension VARCHAR(255))
CREATE PROCEDURE pbx.extensionIsValid (vExtension VARCHAR(255))
BEGIN
DECLARE v_is_valid BOOLEAN;
DECLARE vIsValid BOOLEAN;
SET v_is_valid = v_extension IS NULL
OR (v_extension REGEXP '^[0-9]{4}$'
AND MOD(v_extension, 100) != 0);
SET vIsValid = vExtension IS NULL
OR (vExtension REGEXP '^[0-9]{4}$'
AND MOD(vExtension, 100) != 0);
IF NOT v_is_valid
IF NOT vIsValid
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'EXTENSION_INVALID_FORMAT';

43
sql/phone-format.sql Normal file
View File

@ -0,0 +1,43 @@
/**
* Reformats a phone number
*
* @param v_phone The phone to format
* @return The formated phone or %NULL if bad sintax
**/
DROP FUNCTION IF EXISTS pbx.phoneFormat;
DELIMITER $$
CREATE FUNCTION pbx.phoneFormat (vPhone VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE vI INT DEFAULT 0;
DECLARE vChr VARCHAR(1);
DECLARE vLen INT DEFAULT LENGTH(vPhone);
DECLARE vNewPhone VARCHAR(255) DEFAULT '';
WHILE vI < vLen
DO
SET vChr = SUBSTR(vPhone, vI + 1, 1);
IF vChr REGEXP '^[0-9]$'
THEN
SET vNewPhone = CONCAT(vNewPhone, vChr);
ELSEIF vChr = '+' AND vI = 0
THEN
SET vNewPhone = CONCAT(vNewPhone, '00');
END IF;
SET vI = vI + 1;
END WHILE;
IF vNewPhone REGEXP '^0+$' OR vNewPhone = '' THEN
RETURN NULL;
END IF;
IF vNewPhone REGEXP '^0034' THEN
SET vNewPhone = SUBSTR(vNewPhone, 5);
END IF;
RETURN vNewPhone;
END$$
DELIMITER ;

24
sql/phone-is-valid.sql Normal file
View File

@ -0,0 +1,24 @@
/**
* Checks whether a passed phone number has valid sintax
*
* @param v_phone The phone to check format
* @return %TRUE if it's well formated
**/
DROP PROCEDURE IF EXISTS pbx.phoneIsValid;
DELIMITER $$
CREATE PROCEDURE pbx.phoneIsValid (vPhone VARCHAR(255))
BEGIN
DECLARE vIsValid BOOLEAN;
SET vIsValid = vPhone IS NULL
OR (vPhone REGEXP '^[0-9]+$'
AND vPhone NOT REGEXP '^0+$'
AND vPhone NOT REGEXP '^0034');
IF NOT vIsValid
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'PHONE_INVALID_FORMAT';
END IF;
END$$
DELIMITER ;

View File

@ -1,44 +0,0 @@
/**
* Reformats a phone number
*
* @param v_phone The phone to format
* @return The formated phone or %NULL if bad sintax
**/
USE pbx;
DROP FUNCTION IF EXISTS phone_format;
DELIMITER $$
CREATE FUNCTION phone_format (phone VARCHAR(255))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
DECLARE i INT DEFAULT 0;
DECLARE chr VARCHAR(1);
DECLARE len INT DEFAULT LENGTH(phone);
DECLARE newPhone VARCHAR(255) DEFAULT '';
WHILE i < len
DO
SET chr = SUBSTR(phone, i+1, 1);
IF chr REGEXP '^[0-9]$'
THEN
SET newPhone = CONCAT(newPhone, chr);
ELSEIF chr = '+' AND i = 0
THEN
SET newPhone = CONCAT(newPhone, '00');
END IF;
SET i = i + 1;
END WHILE;
IF newPhone REGEXP '^0+$' OR newPhone = '' THEN
RETURN NULL;
END IF;
IF newPhone REGEXP '^0034' THEN
SET newPhone = SUBSTR(newPhone, 5);
END IF;
RETURN newPhone;
END$$
DELIMITER ;

View File

@ -1,25 +0,0 @@
/**
* Checks whether a passed phone number has valid sintax
*
* @param v_phone The phone to check format
* @return %TRUE if it's well formated
**/
USE pbx;
DROP PROCEDURE IF EXISTS phone_is_valid;
DELIMITER $$
CREATE PROCEDURE phone_is_valid (v_phone VARCHAR(255))
BEGIN
DECLARE v_is_valid BOOLEAN;
SET v_is_valid = v_phone IS NULL
OR (v_phone REGEXP '^[0-9]+$'
AND v_phone NOT REGEXP '^0+$'
AND v_phone NOT REGEXP '^0034');
IF NOT v_is_valid
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'PHONE_INVALID_FORMAT';
END IF;
END$$
DELIMITER ;

View File

@ -4,17 +4,16 @@
* @param v_queue The queue to check format
* @return %TRUE if it's well formated
**/
USE pbx;
DROP PROCEDURE IF EXISTS queue_is_valid;
DROP PROCEDURE IF EXISTS pbx.queueIsValid;
DELIMITER $$
CREATE PROCEDURE queue_is_valid (v_queue VARCHAR(255))
CREATE PROCEDURE pbx.queueIsValid (vQueue VARCHAR(255))
BEGIN
DECLARE v_is_valid BOOLEAN;
DECLARE vIsValid BOOLEAN;
SET v_is_valid = v_queue IS NULL
OR v_queue REGEXP '^[1-9][0-9]00$';
SET vIsValid = vQueue IS NULL
OR vQueue REGEXP '^[1-9][0-9]00$';
IF NOT v_is_valid
IF NOT vIsValid
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'QUEUE_INVALID_FORMAT';

View File

@ -1 +1 @@
agi_callerid: +34626953640
agi_callerid: +34963242100