Brandan

WCCCA / CCOM Firemed twitter REVISION 4

Oct 28th, 2013
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.01 KB | None | 0 0
  1. <?PHP
  2. /***********************************************************************************************
  3.  ***           N O N C O N F I D E N T I A L  ---  O R E G O N 9 1 1                         ***
  4.  ***********************************************************************************************
  5.  *                                                                                             *
  6.  *                 Project Name : Computer Aided Dispatch                                      *
  7.  *                                                                                             *
  8.  *                     $Archive:: ./core/import/JOINTCAD_WA_CLACK.php                          *
  9.  *                                                                                             *
  10.  *                     $Author:: Brandan Tyler Lasley                                          *
  11.  *                                                                                             *
  12.  *                     $Modtime:: 10/28/13 15:35                                               *
  13.  *                                                                                             *
  14.  *                    $Revision:: 4                                                            *
  15.  *                                                                                             *
  16.  *---------------------------------------------------------------------------------------------*
  17.  * Functions: Gets calls from WCCCA PITSv2 website                                             *
  18.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  19.  
  20.  // Compatibility notice: To upgrade from older code to this newer version heres the things that changed.
  21.  // $HTML = file_get_contents("http://www.wccca.com/PITSv$WCCCA_Version/"); => $HTML = WCCCA_callInfo();
  22.  // $Use_Google_API_To_Get_Geo_Coordinates was removed and a new function $Use_Built_In_Geo_Coordinates
  23.  // was added.
  24.  // Debug code for the geocode system has also been added
  25.  // Additionally the 3 functions (critical) were added getloc(), WCCCA_callInfo(), post().
  26.  // However WCCCA_HIDDEN_CODE() was added, it has no purpose.
  27.  // The function getgeoloc() was completely removed.
  28.  
  29. // Setting
  30. $Use_Built_In_Geo_Coordinates = 0;
  31. $Use_Twitter                  = 0;
  32. $Print_Debug                  = 1;
  33. $WCCCA_Version                = 2; //unused, hard coded
  34. $uname                        = 'washco_firemed'; //twitter login name for washco
  35. $unamec                       = 'clackco_firemed'; //twitter login name for clackco
  36. $pwd                          = 'PASSWORD'; //twitter password
  37. $twitter_url                  = 'http://twitter.com/statuses/update.xml'; //twitter api url
  38.  
  39. // Ignore WCCCA PITSv2 HTML Errors (probably should fix)
  40. error_reporting(E_ERROR | E_PARSE);
  41.  
  42. // Download WCCCA PITSv2 full CAD site
  43. $HTML = WCCCA_callInfo();
  44.  
  45. // Extract Call and filter to show only call infomation
  46. // WCCCA:
  47. $WCCCA_Calls = (Seperate_Calls($HTML, 0));
  48.  
  49. // CCOM:
  50. $CCOM_Calls = (Seperate_Calls($HTML, 1));
  51.  
  52. $Raw_WCCCA_Calls = explode("\n", $WCCCA_Calls);
  53. $Raw_CCOM_Calls  = explode("\n", $CCOM_Calls);
  54.  
  55. // Add County infomation for WCCCA
  56. $converted_wccca_calls;
  57. foreach ($Raw_WCCCA_Calls as $call) {
  58.     if (!empty($call)) {
  59.         $converted_wccca_calls .= "County: W Call:" . $call . "\n";
  60.     } //!empty($call)
  61. } //$Raw_WCCCA_Calls as $call
  62.  
  63. // Add County infomation for CCOM
  64. $converted_ccom_calls;
  65. foreach ($Raw_CCOM_Calls as $call) {
  66.     if (!empty($call)) {
  67.         $converted_ccom_calls .= "County: C Call:" . $call . "\n";
  68.     } //!empty($call)
  69. } //$Raw_CCOM_Calls as $call
  70.  
  71.  
  72. // Combind CCOM and WCCCA
  73. $Total_calls = $converted_ccom_calls . $converted_wccca_calls;
  74.  
  75. // Split Total calls for looping
  76. $raw_calls = explode("\n", $Total_calls);
  77.  
  78. // Start the call loop
  79. foreach ($raw_calls as $call) {
  80.     if (!empty($call)) {
  81.        
  82.         // Get County
  83.         $county      = explode(' ', $call);
  84.         $cleancounty = $county[1];
  85.        
  86.         // Get Station
  87.         $station      = explode('Time: ', $call);
  88.         $station1     = explode(' ', $station[1]);
  89.         $cleanstation = $station1[5];
  90.        
  91.         // Get Agency
  92.         $agency      = $station1[4];
  93.         $cleanagency = str_replace("/", "", $agency);
  94.        
  95.         // Get Call
  96.         $raw_call  = explode("Call: ", $call);
  97.         $raw_call2 = explode(" Address:", $raw_call[1]);
  98.         $cleancall = $raw_call2[0];
  99.        
  100.         // Get Address
  101.         $raw_address   = explode("Address: ", $call);
  102.         $raw_address1  = explode(" Time:", $raw_address[1], 2);
  103.         $cleanlocation = preg_replace('/\W\w+\s*(\W*)$/', '$1', $raw_address1[0]);
  104.        
  105.         // Get GUID
  106.         $raw_guid  = str_replace("$cleanlocation", "", $raw_address1[0]);
  107.         $cleanguid = str_replace(' ', '', $raw_guid);
  108.        
  109.         // Get Units
  110.         $raw_units  = explode(" Time:", $call);
  111.         $raw_units1 = explode("/", $raw_units[1]);
  112.         $raw_units2 = explode("- ", $raw_units1[1]);
  113.         $cleanunits = $raw_units2[1];
  114.        
  115.         if ($Use_Built_In_Geo_Coordinates == 1) {
  116.             $geoaddr = getloc($cleanguid, $HTML);
  117.             $llatt   = $geoaddr[0];
  118.             $llonn   = $geoaddr[1];
  119.         } //$Use_Built_In_Geo_Coordinates == 1
  120.        
  121.         if (!empty($cleancounty)) {
  122.             if ($Print_Debug == 1) {
  123.                 $geoaddr = getloc($cleanguid, $HTML);
  124.                 print("County: $cleancounty Call: $cleancall Address: $cleanlocation Units: $cleanunits Agency: $cleanagency Station: $cleanstation GUID $cleanguid\n");
  125.                 print($geoaddr[0] . ", " . $geoaddr[1] . "\n");
  126.             } //$Print_Debug == 1
  127.             if ($Use_Twitter == 1) {
  128.                 //build a google maps url with an info bubble
  129.                 $map = 'http://maps.google.com/maps?z=12&q=' . $llatt . "+" . $llonn . "+(" . $cleancall . "," . $cleanlocation . ", Units: " . $cleanunits . ")";
  130.                
  131.                 //change the google map url to a tiny url (actually is.gd ur because it's smallerl) and change the spaces in the $map url to %20 so there is no bad http requests
  132.                 $mapfixed = rawurlencode($map);
  133.                 $tiny_url = file_get_contents("http://is.gd/api.php?longurl=" . $mapfixed);
  134.                
  135.                 //put the parts together for the twitter status line
  136.                 $status = $cleancall . " | " . $cleanlocation . " | Units: " . $cleanunits . " | " . $tiny_url; //assemble status for twitter
  137.                
  138.                 if ($cleancounty == "W") //if washington county, then post to washco_firemed
  139.                     {
  140.                     $curl_handle = curl_init();
  141.                     curl_setopt($curl_handle, CURLOPT_URL, "$twitter_url");
  142.                     curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
  143.                     curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  144.                     curl_setopt($curl_handle, CURLOPT_POST, 1);
  145.                     curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$status");
  146.                     curl_setopt($curl_handle, CURLOPT_USERPWD, "$uname:$pwd");
  147.                     $buffer = curl_exec($curl_handle);
  148.                     curl_close($curl_handle);
  149.                     if (empty($buffer)) {
  150.                         echo '<br/>Fail-WC';
  151.                     } //empty($buffer)
  152.                     else {
  153.                         echo $status;
  154.                     }
  155.                 } //$cleancounty == "W"
  156.                 elseif ($cleancounty == "C") //if clackamas county, then post to clackco_firemed
  157.                     {
  158.                     $curl_handle = curl_init();
  159.                     curl_setopt($curl_handle, CURLOPT_URL, "$twitter_url");
  160.                     curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
  161.                     curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  162.                     curl_setopt($curl_handle, CURLOPT_POST, 1);
  163.                     curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$status");
  164.                     curl_setopt($curl_handle, CURLOPT_USERPWD, "$unamec:$pwd");
  165.                     $buffer = curl_exec($curl_handle);
  166.                     curl_close($curl_handle);
  167.                     if (empty($buffer)) {
  168.                         echo '<br/>Fail-CC';
  169.                     } //empty($buffer)
  170.                     else {
  171.                         echo $status;
  172.                     }
  173.                 } //$cleancounty == "C"
  174.             } //$Use_Twitter == 1
  175.         } //!empty($cleancounty)
  176.     } //!empty($call)
  177. } //$raw_calls as $call
  178. exit;
  179.  
  180. function Seperate_Calls($HTML, $County)
  181. {
  182.     $dom = new DOMDocument();
  183.     $dom->loadHTML($HTML);
  184.     $xpath = new DOMXPath($dom);
  185.    
  186.     // Switch between WCCCA/CCOM
  187.     if ($County == 0) {
  188.         $tags = $xpath->query('//div[@id="wccca-incidents"]');
  189.     } //$County == 0
  190.     else {
  191.         $tags = $xpath->query('//div[@id="ccom-incidents"]');
  192.     }
  193.    
  194.     // For each call delete all the massive amount of blank spaces and new lines.
  195.     foreach ($tags as $tag) {
  196.         $calls = preg_replace('/\s+/', ' ', $tag->textContent);
  197.     } //$tags as $tag
  198.     // Seperate the calls (since we removed all the \n's)
  199.     $Filtered_Calls = explode("Call Type:", $calls);
  200.    
  201.     // Loop though seperated calls and recombind them into a list, then add a new line to them for easier processing.
  202.     foreach ($Filtered_Calls as $call) {
  203.         $Call_dump .= $call . "\n";
  204.     } //$Filtered_Calls as $call
  205.     // Remove any unintentional blank spaces that may occur from this.
  206.     $Call_dump = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "", $Call_dump);
  207.    
  208.     // Return the calls.
  209.     return ($Call_dump);
  210. }
  211.  
  212. function getloc($callnum, $HTML)
  213. {
  214.     // Delete useless code to narrow down to the exact code we want to format.
  215.     $Del      = strstr(strstr($HTML, 'LoadMarker('), 'updateMarkers()', true);
  216.     $format01 = str_replace("parseFloat(", "", $Del);
  217.     $format02 = str_replace(";resizeIncidentScrollBar('#wccca-incidents')", "", $format01);
  218.     $format03 = str_replace(";resizeIncidentScrollBar('#ccom-incidents')", "", $format02);
  219.     $format04 = str_replace("LoadMarker(", "", $format03);
  220.     $format05 = str_replace("(", "", $format04);
  221.     $format06 = str_replace(")", "", $format05);
  222.     $format07 = str_replace("LoadMarker(", "", trim(preg_replace('/\s\s+/', ' ', $format06)));
  223.     $format08 = str_replace(";", "\n", $format07);
  224.     $calls    = explode("\n", $format08);
  225.     foreach ($calls as $call) {
  226.         $callInfo = str_getcsv($call, ',', '\'');
  227.         if ($callnum == str_replace(" ", "", str_replace("'", "", $callInfo[3]))) {
  228.             $lat = $callInfo[0];
  229.             $lon = $callInfo[1];
  230.             break;
  231.         } else {
  232.             $lat = 0;
  233.             $lon = 0;
  234.         }
  235.     }
  236.     return array(
  237.         $lat,
  238.         $lon
  239.     );
  240. }
  241.  
  242. function WCCCA_callInfo()
  243. {
  244.     $Calls = post("http://www.wccca.com/PITSv2/Default.aspx", "__VIEWSTATE=");
  245.     return $Calls;
  246. }
  247.  
  248. function WCCCA_HIDDEN_CODE($input) // Get hidden code (NOT USED / POSSIBLE FUTURE USE?)
  249. {
  250.     $data    = file_get_contents("http://www.wccca.com/PITSv2/Default.aspx");
  251.     $pattern = "/id=\"__" . $input . "\" value=\"([^\"]+)\"/";
  252.     preg_match_all($pattern, $data, $xmlext);
  253.    
  254.     // Removes all slashes.
  255.     $code = str_replace("/", "", $xmlext[1][0]);
  256.     return $code;
  257. }
  258.  
  259. function post($url, $data, $optional_headers = null)
  260. {
  261.     $params = array(
  262.         'http' => array(
  263.             'method' => 'POST',
  264.             'content' => trim(preg_replace('/\s\s+/', ' ', $data))
  265.         )
  266.     );
  267.     if ($optional_headers !== null) {
  268.         $params['http']['header'] = $optional_headers;
  269.     }
  270.     $ctx = stream_context_create($params);
  271.     $fp  = @fopen($url, 'rb', false, $ctx);
  272.     if (!$fp) {
  273.         throw new Exception("Problem with $url, $php_errormsg");
  274.     }
  275.     $response = @stream_get_contents($fp);
  276.     if ($response === false) {
  277.         throw new Exception("Problem reading data from $url, $php_errormsg");
  278.     }
  279.     return $response;
  280. }
  281.  
  282. ?>
Add Comment
Please, Sign In to add comment