Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * R O U T E R  S C R I P T
  5.  * messages are ideally submitted to this smsc and are
  6.  * consequently routed via configured routing rules
  7.  * contained in routing.ini
  8.  *
  9.  * NOTE: Country codes represented as detailed in ISO 8601
  10.  */
  11.  
  12. require_once('/var/www/sms.solami.co.ke/Utilities/Url-lib.php');
  13.  
  14. /*
  15.  * Some rudimentary configs
  16.  */
  17. $msisdn_regexes = './routing_configs/country_msisdn_regexes.ini';
  18. $routing_path = './routing_configs/routing.ini';
  19. $kannel_configs = './routing_configs/kannel_configs.ini';
  20.  
  21. /*
  22.  * Main
  23.  */
  24.  print submit_message_to_kannel();
  25.  
  26. /*
  27.  * Fetch routing configs
  28.  */
  29. function get_routing_configs ($country="ke") {
  30.     global $routing_path;
  31.     $routes = parse_ini_file($routing_path, true);
  32.     print "SPECIFIC => [" . $country . "] " . print_r($routes[$country], true);
  33.     return $routes["$country"];
  34. }
  35.  
  36. /*
  37.  * Get msisdn filter regex
  38.  */
  39. function get_sanitize_regex ($key) {
  40.     global $msisdn_regexes;
  41.     $regexes = parse_ini_file($msisdn_regexes);
  42.     return $regexes[$key];
  43. }
  44.  
  45. /*
  46.  * Clean up the mobile number
  47.  */
  48. function sanitize_msisdn ($number) {
  49.     $sanitize_regex = get_sanitize_regex('ke');
  50.     return preg_replace($sanitize_regex,"254$1", $number);
  51. }
  52.  
  53. /*
  54.  * Pick a route that matches repeatedly and return all
  55.  * routes that match in an array
  56.  */
  57. function select_route ($msisdn, $country=null) {
  58.     $hits = [];
  59.     $msisdn = sanitize_msisdn($msisdn);
  60.     $routes = get_routing_configs($country);
  61.     print "ROUTES => " . print_r($routes, true);
  62.     array_walk($routes,
  63.         function ($route, $regex, $number)
  64.               use (&$hits)
  65.                 {
  66.                    if (preg_match(
  67.                        '/^' . $regex . '/',
  68.                        $number
  69.                   )) {
  70.                        $hits[] = $route;
  71.                    }
  72.          },
  73.          $msisdn
  74.         );
  75.  
  76.     /* SET ROUTE */
  77.     if (count($hits) == 0)
  78.     $route = 'INFOBIP';
  79.     else
  80.     $route = $hits[0];
  81.    
  82.     return $route;
  83. }
  84.  
  85. /*
  86.  * http://localhost/solami/api/route_msg.php?dest=%p&message=%a&source=%P&udh=%u&msgID=%I&box=%n&coding=%C&user=%n&METADATA=%D
  87.  * ----------------------------------------------------------------------------------------------------------------------------------
  88.  * from=22677
  89.  * to=254708146140
  90.  * text=Hi+click+http%3A%2F%2Fmuziq.co.ke%2Fb%2Fcca9f0+to+get+Handu+Hau+by+Charles+Njoroge.+Bonyeza+*699*20%23+For+Your+Slice+of+Gospel
  91.  * priority=0
  92.  * smsc=safaricom
  93.  * username=safaricom
  94.  * password=safaricom-pass
  95.  * dlr-mask=
  96.  * dlr-url=http%3A%2F%2F172.31.186.204%3A9000%2Fapi%2Fdlr%3Ftype%3D%25d%26time%3D%25t%26usr%3D%25n%26message%3D%25b%26kannel_id%3D%25F%26MSISDN%3D%25p%26dlrvalue%3D%25A%26message_id%3D176127627
  97.  * http://ke-pr-gw2:13013/cgi-bin/sendsms
  98.  * ----------------------------------------------------------------------------------------------------------------------------------
  99.  */
  100.  
  101. /*
  102.  * Upack data from the get params into data
  103.  * then return that data array
  104.  */
  105. function get_data () {
  106.     global $kannel_configs;
  107.     $gateway_configs = parse_ini_file($kannel_configs);
  108.     $data = [
  109.         'source' => $_GET['source'],
  110.         'msisdn' => $_GET['dest'],
  111.         'message' => urlencode($_GET['message']),
  112.         'smsc' => select_route($_GET['dest']),
  113.         'metadata' => $_GET['METADATA'],
  114.         ];
  115.     $data = array_merge($gateway_configs, $data);
  116.     print print_r($data, true);
  117.     return $data;
  118. }
  119.  
  120. /*
  121.  * Construct kannel URL
  122.  */
  123.  function construct_kannel_link ( $data ) {
  124.      /* Base URL */
  125.      $format_string .= "http://localhost:%d/cgi-bin/sendsms?";
  126.  
  127.      /* Auth */
  128.      $format_string .= "username=%s&password=%s&";
  129.      
  130.      /* Message Params */
  131.      $format_string .= "from=%s&to=%d&text=%s&";
  132.  
  133.      /* ! !  R O U T I N G  ! !  */
  134.      $format_string .= "smsc=%s&";
  135.  
  136.      /* Metadata */
  137.      $format_string .= "meta-data=%s";
  138.  
  139.      return vsprintf($format_string, $data);
  140.  }
  141.  
  142. /*
  143.  * Call kannel URL
  144.  */
  145. function submit_message_to_kannel () {
  146.     $url = construct_kannel_link(get_data());
  147.     $link = new Url($url);
  148.     $link->callUrl();
  149.     return $link;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement