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

View File

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

4
debian/control vendored
View File

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

2
debian/copyright vendored
View File

@ -1,6 +1,6 @@
Format: http://dep.debian.net/deps/dep5 Format: http://dep.debian.net/deps/dep5
Name: vn-asterisk Name: vn-asterisk
Source: git://www.verdnatura.es/var/git/ Source: http://git.verdnatura.es/
Files: * Files: *
Copyright: 2011-2015 Juan Ferrer Toribio <juan@verdnatura.es> 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 #chown asterisk:asterisk /etc/asterisk/extensions.conf
/etc/init.d/asterisk reload /etc/init.d/asterisk reload

1
debian/postrm vendored
View File

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

1
debian/rules vendored
View File

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

View File

@ -2,9 +2,7 @@
require_once (__DIR__.'/../php-vn-lib/env.php'); 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 _DEV_MODE = true;
const _CONFIG_DIR = __DIR__.'/../../.config';
const _LOG_DIR = '/tmp'; const _LOG_DIR = '/tmp';

View File

@ -1,69 +1,67 @@
#!/usr/bin/php -q #!/usr/bin/php -q
<?php <?php
require_once ('agi.php'); if (file_exists(__DIR__.'/env.php'))
@include_once __DIR__.'/env.php'; include_once __DIR__.'/env.php';
require_once 'agi.php';
require_once 'vn-autoload.php'; require_once 'vn-autoload.php';
$app = new Vn\Lib\App ('vn-asterisk'); $app = new Vn\Lib\App('vn-asterisk');
$app->init (); $app->init();
$db = $app->getSysConn (); $db = $app->getSysConn();
Agi::init (); Agi::init();
// Formats the caller phone number // 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'); $countryPrefix = $db->getValue('SELECT countryPrefix FROM config');
$prefixLen = strlen ($countryPrefix); $prefixLen = strlen($countryPrefix);
if (substr ($callerId, 0, $prefixLen) === $countryPrefix) if (substr($callerId, 0, $prefixLen) === $countryPrefix)
$callerId = substr ($callerId, $prefixLen); $callerId = substr($callerId, $prefixLen);
// Checks if phone number is on the blacklist // Checks if phone number is on the blacklist
if ($db->getValue ('SELECT COUNT(*) > 0 FROM blacklist WHERE phone = #', [$callerId])) if ($db->getValue('SELECT COUNT(*) > 0 FROM blacklist WHERE phone = #', [$callerId])) {
{ Agi::exec('HANGUP');
Agi::exec ('HANGUP'); exit;
exit ();
} }
// Checks whether its a festive day // 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) if (date('N') == 7 && $sundayFestive) {
{ Agi::exec('SET VARIABLE MACRO playback');
Agi::exec ('SET VARIABLE MACRO playback'); Agi::exec('SET VARIABLE ARG1 out-of-ours');
Agi::exec ('SET VARIABLE ARG1 out-of-ours'); exit;
exit ();
} }
// Checks schedules // Checks schedules
$hasSchedule = $db->getValue ( $hasSchedule = $db->getValue(
'SELECT COUNT(*) > 0 FROM pbx.schedule 'SELECT COUNT(*) > 0 FROM pbx.schedule
WHERE weekDay = WEEKDAY(CURDATE()) WHERE weekDay = WEEKDAY(CURDATE())
AND CURTIME() BETWEEN timeStart AND timeEnd' AND CURTIME() BETWEEN timeStart AND timeEnd'
); );
if ($hasSchedule) if ($hasSchedule) {
{ Agi::exec('SET VARIABLE MACRO queue');
Agi::exec ('SET VARIABLE MACRO queue'); Agi::exec('SET VARIABLE ARG1 1200');
Agi::exec ('SET VARIABLE ARG1 1200'); exit;
exit ();
} }
// Gets the customer from the phone number // 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 // Gets the customer salesperson extension
$extension = $db->getValue ( $extension = $db->getValue(
'SELECT s.extension 'SELECT s.extension
FROM sip s FROM sip s
JOIN vn2008.Trabajadores t ON t.user_id = s.user_id JOIN vn2008.Trabajadores t ON t.user_id = s.user_id
@ -71,16 +69,11 @@ if ($customer)
,[$customer] ,[$customer]
); );
if ($extension) if ($extension) {
{ Agi::exec('SET VARIABLE MACRO exten');
Agi::exec ('SET VARIABLE MACRO exten'); Agi::exec("SET VARIABLE ARG1 $extension");
Agi::exec ("SET VARIABLE ARG1 $extension"); } else {
} Agi::exec('SET VARIABLE MACRO playback');
else Agi::exec('SET VARIABLE ARG1 busy');
{
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 * @param v_phone The caller phone number
* @return The customer id or %NULL if customer not exists or is inactive * @return The customer id or %NULL if customer not exists or is inactive
**/ **/
USE pbx; DROP FUNCTION IF EXISTS pbx.clientFromPhone;
DROP FUNCTION IF EXISTS customer_from_phone;
DELIMITER $$ DELIMITER $$
CREATE FUNCTION customer_from_phone (v_phone VARCHAR(255)) CREATE FUNCTION pbx.clientFromPhone (vPhone VARCHAR(255))
RETURNS INT RETURNS INT
DETERMINISTIC DETERMINISTIC
BEGIN BEGIN
DECLARE v_customer INT DEFAULT NULL; DECLARE vClient INT DEFAULT NULL;
SET @phone = v_phone COLLATE 'utf8_unicode_ci'; -- SET vPhone = vPhone COLLATE 'utf8_unicode_ci';
-- Searchs a customer associated to the phone number -- Searchs a customer associated to the phone number
DROP TEMPORARY TABLE IF EXISTS tmp.customer; DROP TEMPORARY TABLE IF EXISTS tmp.customer;
CREATE TEMPORARY TABLE tmp.customer CREATE TEMPORARY TABLE tmp.customer
ENGINE = MEMORY ENGINE = MEMORY
SELECT id_cliente customer SELECT id_cliente customer
FROM vn2008.Clientes c FROM vn2008.Clientes c
WHERE telefono = @phone WHERE telefono = vPhone
OR movil = @phone OR movil = vPhone
UNION UNION
SELECT id_cliente SELECT id_cliente
FROM vn2008.Consignatarios FROM vn2008.Consignatarios
WHERE telefono = @phone WHERE telefono = vPhone
OR movil = @phone OR movil = vPhone
UNION UNION
SELECT r.id_cliente SELECT r.id_cliente
FROM vn2008.Relaciones r FROM vn2008.Relaciones r
JOIN vn2008.Contactos c ON r.Id_Contacto = c.Id_Contacto JOIN vn2008.Contactos c ON r.Id_Contacto = c.Id_Contacto
WHERE telefono = @phone WHERE c.telefono = vPhone
OR movil = @phone; OR c.movil = vPhone;
SELECT t.customer INTO v_customer SELECT t.customer INTO vClient
FROM tmp.customer t FROM tmp.customer t
JOIN vn2008.Clientes c ON c.id_cliente = t.customer JOIN vn2008.Clientes c ON c.id_cliente = t.customer
WHERE c.activo WHERE c.activo
LIMIT 1; LIMIT 1;
DROP TEMPORARY TABLE tmp.customer; DROP TEMPORARY TABLE tmp.customer;
RETURN v_customer; RETURN vClient;
END$$ END$$
DELIMITER ; DELIMITER ;

View File

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

View File

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