Exemplo n.º 1
0
 static function get_javascript()
 {
     // Make sure cURL is installed
     if (!function_exists('curl_exec')) {
         throw new Kohana_Exception('footer.cURL_not_installed');
         return false;
     }
     $settings = ORM::factory('settings', 1);
     $stat_id = $settings->stat_id;
     // If stats isn't set, ignore this
     if ($stat_id == 0) {
         return '';
     }
     $cache = Cache::instance();
     $tag = $cache->get(Kohana::config('settings.subdomain') . '_piwiktag');
     if (!$tag) {
         // Cache is Empty so Re-Cache
         // Grabbing the URL to update stats URL, Name, Reports, etc on the stats server
         $additional_query = '';
         if (isset($_SERVER["HTTP_HOST"])) {
             // Grab the site domain from the config and trim any whitespaces
             $site_domain = trim(Kohana::config('config.site_domain'));
             $slashornoslash = '';
             if (empty($site_domain) or $site_domain[0] != '/') {
                 $slashornoslash = '/';
             }
             // URL
             $val = 'http://' . $_SERVER["HTTP_HOST"] . $slashornoslash . $site_domain;
             $additional_query = '&val=' . base64_encode($val);
             // Site Name
             $site_name = utf8tohtml::convert(Kohana::config('settings.site_name'), TRUE);
             $additional_query .= '&sitename=' . base64_encode($site_name);
             // Version
             $version = Kohana::config('settings.ushahidi_version');
             $additional_query .= '&version=' . base64_encode($version);
             // Report Count
             $number_reports = ORM::factory("incident")->where("incident_active", 1)->count_all();
             $additional_query .= '&reports=' . base64_encode($number_reports);
             // Latitude
             $latitude = Kohana::config('settings.default_lat');
             $additional_query .= '&lat=' . base64_encode($latitude);
             // Longitude
             $longitude = Kohana::config('settings.default_lon');
             $additional_query .= '&lon=' . base64_encode($longitude);
         }
         $url = 'https://tracker.ushahidi.com/dev.px.php?task=tc&siteid=' . $stat_id . $additional_query;
         $curl_handle = curl_init();
         curl_setopt($curl_handle, CURLOPT_URL, $url);
         curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, self::$time_out);
         // Timeout set to 15 seconds. This is somewhat arbitrary and can be changed.
         curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
         // Set cURL to store data in variable instead of print
         curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false);
         $buffer = curl_exec($curl_handle);
         curl_close($curl_handle);
         try {
             $tag = (string) @simplexml_load_string($buffer);
             // This works because the tracking code is only wrapped in one tag
         } catch (Exception $e) {
             // In case the xml was malformed for whatever reason, we will just guess what the tag should be here
             $tag = '<!-- Piwik --><script type="text/javascript">jQuery(document).ready(function(){$(\'#piwik\').load(\'https://tracker.ushahidi.com/piwik/piwik.php?idsite=' . $stat_id . '&rec=1\');});</script><div id="piwik"></div><!-- End Piwik Tag -->';
         }
         // Reset Cache Here
         $cache->set(Kohana::config('settings.subdomain') . '_piwiktag', $tag, array('piwiktag'), 60);
         // 1 Day
     }
     return $tag;
 }
Exemplo n.º 2
0
    /**
     * Read in new layer JSON from shared connection
     * @param int $sharing_id - ID of the new Share Layer
     */
    public function share( $sharing_id = false )
    {   
        $json = "";
        $json_item = "";
        $json_array = array();
        $sharing_data = "";
        $clustering = Kohana::config('settings.allow_clustering');
        
        if ($sharing_id)
        {
            // Get This Sharing ID Color
            $sharing = ORM::factory('sharing')
                ->find($sharing_id);
            
            if( ! $sharing->loaded )
                return;
            
            $sharing_url = $sharing->sharing_url;
            $sharing_color = $sharing->sharing_color;
            
            if ($clustering)
            {
                // Database
                $db = new Database();
                
                // Start Date
                $start_date = (isset($_GET['s']) && !empty($_GET['s'])) ?
                    (int) $_GET['s'] : "0";

                // End Date
                $end_date = (isset($_GET['e']) && !empty($_GET['e'])) ?
                    (int) $_GET['e'] : "0";

                // SouthWest Bound
                $southwest = (isset($_GET['sw']) && !empty($_GET['sw'])) ?
                    $_GET['sw'] : "0";

                $northeast = (isset($_GET['ne']) && !empty($_GET['ne'])) ?
                    $_GET['ne'] : "0";
                
                // Get Zoom Level
                $zoomLevel = (isset($_GET['z']) && !empty($_GET['z'])) ?
                    (int) $_GET['z'] : 8;

                //$distance = 60;
                $distance = (10000000 >> $zoomLevel) / 100000;
                
                $filter = "";
                $filter .= ($start_date) ? 
                    " AND incident_date >= '" . date("Y-m-d H:i:s", $start_date) . "'" : "";
                $filter .= ($end_date) ? 
                    " AND incident_date <= '" . date("Y-m-d H:i:s", $end_date) . "'" : "";

                if ($southwest && $northeast)
                {
                    list($latitude_min, $longitude_min) = explode(',', $southwest);
                    list($latitude_max, $longitude_max) = explode(',', $northeast);

                    $filter .= " AND latitude >=".(float) $latitude_min.
                        " AND latitude <=".(float) $latitude_max;
                    $filter .= " AND longitude >=".(float) $longitude_min.
                        " AND longitude <=".(float) $longitude_max;
                }
                
                $query = $db->query("SELECT * FROM `".$this->table_prefix."sharing_incident` WHERE 1=1 $filter ORDER BY incident_id ASC "); 

                $markers = $query->result_array(FALSE);

                $clusters = array();    // Clustered
                $singles = array();     // Non Clustered

                // Loop until all markers have been compared
                while (count($markers))
                {
                    $marker  = array_pop($markers);
                    $cluster = array();

                    // Compare marker against all remaining markers.
                    foreach ($markers as $key => $target)
                    {
                        // This function returns the distance between two markers, at a defined zoom level.
                        // $pixels = $this->_pixelDistance($marker['latitude'], $marker['longitude'], 
                        // $target['latitude'], $target['longitude'], $zoomLevel);

                        $pixels = abs($marker['longitude']-$target['longitude']) + 
                            abs($marker['latitude']-$target['latitude']);
                        // echo $pixels."<BR>";
                        // If two markers are closer than defined distance, remove compareMarker from array and add to cluster.
                        if ($pixels < $distance)
                        {
                            unset($markers[$key]);
                            $target['distance'] = $pixels;
                            $cluster[] = $target;
                        }
                    }

                    // If a marker was added to cluster, also add the marker we were comparing to.
                    if (count($cluster) > 0)
                    {
                        $cluster[] = $marker;
                        $clusters[] = $cluster;
                    }
                    else
                    {
                        $singles[] = $marker;
                    }
                }

                // Create Json
                foreach ($clusters as $cluster)
                {
                    // Calculate cluster center
                    $bounds = $this->_calculateCenter($cluster);
                    $cluster_center = $bounds['center'];
                    $southwest = $bounds['sw'];
                    $northeast = $bounds['ne'];

                    // Number of Items in Cluster
                    $cluster_count = count($cluster);

                    $json_item = "{";
                    $json_item .= "\"type\":\"Feature\",";
                    $json_item .= "\"properties\": {";
                    $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a href='http://" . $sharing_url . "/reports/index/?c=0&sw=".$southwest."&ne=".$northeast."'>" . $cluster_count . " Reports</a>")) . "\",";
                    $json_item .= "\"link\": \"http://".$sharing_url."reports/index/?c=0&sw=".$southwest."&ne=".$northeast."\", ";        
                    $json_item .= "\"category\":[0], ";
                    $json_item .= "\"icon\": \"\", ";
                    $json_item .= "\"color\": \"".$sharing_color."\", ";
                    $json_item .= "\"timestamp\": \"0\", ";
                    $json_item .= "\"count\": \"" . $cluster_count . "\"";
                    $json_item .= "},";
                    $json_item .= "\"geometry\": {";
                    $json_item .= "\"type\":\"Point\", ";
                    $json_item .= "\"coordinates\":[" . $cluster_center . "]";
                    $json_item .= "}";
                    $json_item .= "}";

                    array_push($json_array, $json_item);
                }

                foreach ($singles as $single)
                {
                    $json_item = "{";
                    $json_item .= "\"type\":\"Feature\",";
                    $json_item .= "\"properties\": {";
                    $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a href='http://" . $sharing_url . "/reports/view/" . $single['id'] . "'>".$single['incident_title']."</a>")) . "\",";
                    $json_item .= "\"link\": \"http://".$sharing_url."reports/view/".$single['id']."\", ";
                    $json_item .= "\"category\":[0], ";
                    $json_item .= "\"icon\": \"\", ";
                    $json_item .= "\"color\": \"".$sharing_color."\", ";
                    $json_item .= "\"timestamp\": \"0\", ";
                    $json_item .= "\"count\": \"" . 1 . "\"";
                    $json_item .= "},";
                    $json_item .= "\"geometry\": {";
                    $json_item .= "\"type\":\"Point\", ";
                    $json_item .= "\"coordinates\":[" . $single['longitude'] . ", " . $single['latitude'] . "]";
                    $json_item .= "}";
                    $json_item .= "}";

                    array_push($json_array, $json_item);
                }

                $json = implode(",", $json_array);
                
            }
            else
            {
                // Retrieve all markers
                $markers = ORM::factory('sharing_incident')
                                        ->where('sharing_id', $sharing_id)
                                        ->find_all();

                foreach ($markers as $marker)
                {   
                    $json_item = "{";
                    $json_item .= "\"type\":\"Feature\",";
                    $json_item .= "\"properties\": {";
                    $json_item .= "\"id\": \"".$marker->incident_id."\", \n";

                    $encoded_title = utf8tohtml::convert($marker->incident_title,TRUE);

                    $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a href='http://" . $sharing_url . "/reports/view/" . $marker->incident_id . "'>".$encoded_title."</a>")) . "\",";
					$json_item .= "\"link\": \"http://".$sharing_url."reports/view/".$marker->incident_id."\", ";
                    $json_item .= "\"icon\": \"\", ";
                    $json_item .= "\"color\": \"".$sharing_color ."\", \n";
                    $json_item .= "\"timestamp\": \"" . strtotime($marker->incident_date) . "\"";
                    $json_item .= "},";
                    $json_item .= "\"geometry\": {";
                    $json_item .= "\"type\":\"Point\", ";
                    $json_item .= "\"coordinates\":[" . $marker->longitude . ", " . $marker->latitude . "]";
                    $json_item .= "}";
                    $json_item .= "}";

                    array_push($json_array, $json_item);
                }

                $json = implode(",", $json_array);
            }
        }
        
         header('Content-type: application/json; charset=utf-8');
        $this->template->json = $json;
    }
Exemplo n.º 3
0
 /**
  * Get encoded title linked to url
  * @param string $title - Item title
  * @param string $url - URL to link to
  * @return string
  */
 protected function get_title($title, $url)
 {
     $encoded_title = utf8tohtml::convert($title, TRUE);
     $encoded_title = str_ireplace('"', '&#34;', $encoded_title);
     $item_name = "<a href='{$url}'>" . $encoded_title . "</a>";
     $item_name = str_replace(array(chr(10), chr(13)), ' ', $item_name);
     return $item_name;
 }
Exemplo n.º 4
0
 /**
  * Get Geometry JSON
  * @param int $incident_id
  * @param string $incident_title
  * @param int $incident_date
  * @return array $geometry
  */
 private static function _get_geometry($incident_id, $incident_title, $incident_date, $on_the_back_end, $color, $link_target = "_self")
 {
     $geometry = array();
     if ($incident_id) {
         $db = new Database();
         // Get Incident Geometries via SQL query as ORM can't handle Spatial Data
         $sql = "SELECT id, AsText(geometry) as geometry, geometry_label, \n\t\t\t\tgeometry_comment, geometry_color, geometry_strokewidth FROM " . self::$table_prefix . "geometry \n\t\t\t\tWHERE incident_id=" . $incident_id;
         $query = $db->query($sql);
         $wkt = new Wkt();
         foreach ($query as $item) {
             $geom = $wkt->read($item->geometry);
             $geom_array = $geom->getGeoInterface();
             $json_item = "{";
             $json_item .= "\"type\":\"Feature\",";
             $json_item .= "\"properties\": {";
             $json_item .= "\"id\": \"" . $incident_id . "\", ";
             $json_item .= "\"feature_id\": \"" . $item->id . "\", ";
             $title = $item->geometry_label ? utf8tohtml::convert($item->geometry_label, TRUE) : utf8tohtml::convert($incident_title, TRUE);
             $fillcolor = $item->geometry_color ? utf8tohtml::convert($item->geometry_color, TRUE) : $color;
             $strokecolor = $item->geometry_color ? utf8tohtml::convert($item->geometry_color, TRUE) : $color;
             $strokewidth = $item->geometry_strokewidth ? $item->geometry_strokewidth : "3";
             if ($on_the_back_end) {
                 $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a href='" . url::base() . "admin/reports/edit/" . $incident_id . "'>" . $title . "</a>")) . "\",";
             } else {
                 $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a target='" . $link_target . "' href='" . url::base() . "reports/view/" . $incident_id . "'>" . $title . "</a>")) . "\",";
             }
             $json_item .= "\"description\": \"" . utf8tohtml::convert($item->geometry_comment, TRUE) . "\", ";
             $json_item .= "\"color\": \"" . $fillcolor . "\", ";
             $json_item .= "\"strokecolor\": \"" . $strokecolor . "\", ";
             $json_item .= "\"strokewidth\": \"" . $strokewidth . "\", ";
             $json_item .= "\"link\": \"" . url::base() . "reports/view/" . $incident_id . "\", ";
             $json_item .= "\"category\":[0], ";
             $json_item .= "\"timestamp\": \"" . strtotime($incident_date) . "\"";
             $json_item .= "},\"geometry\":" . json_encode($geom_array) . "}";
             $geometry[] = $json_item;
         }
     }
     return $geometry;
 }
Exemplo n.º 5
0
 /**
  * Get Geometry JSON
  * @param int $incident_id
  * @param string $incident_title
  * @param int $incident_date
  * @return array $geometry
  */
 private function _get_geometry($incident_id, $incident_title, $incident_date)
 {
     $geometry = array();
     if ($incident_id) {
         $geom_data = $this->_get_geometry_data_for_incident($incident_id);
         $wkt = new Wkt();
         foreach ($geom_data as $item) {
             $geom = $wkt->read($item->geometry);
             $geom_array = $geom->getGeoInterface();
             $json_item = "{";
             $json_item .= "\"type\":\"Feature\",";
             $json_item .= "\"properties\": {";
             $json_item .= "\"id\": \"" . $incident_id . "\", ";
             $json_item .= "\"feature_id\": \"" . $item->id . "\", ";
             $title = $item->geometry_label ? utf8tohtml::convert($item->geometry_label, TRUE) : utf8tohtml::convert($incident_title, TRUE);
             $fillcolor = $item->geometry_color ? utf8tohtml::convert($item->geometry_color, TRUE) : "ffcc66";
             $strokecolor = $item->geometry_color ? utf8tohtml::convert($item->geometry_color, TRUE) : "CC0000";
             $strokewidth = $item->geometry_strokewidth ? $item->geometry_strokewidth : "3";
             $json_item .= "\"name\":\"" . str_replace(chr(10), ' ', str_replace(chr(13), ' ', "<a href='" . url::base() . "reports/view/" . $incident_id . "'>" . $title . "</a>")) . "\",";
             $json_item .= "\"description\": \"" . utf8tohtml::convert($item->geometry_comment, TRUE) . "\", ";
             $json_item .= "\"color\": \"" . $fillcolor . "\", ";
             $json_item .= "\"strokecolor\": \"" . $strokecolor . "\", ";
             $json_item .= "\"strokewidth\": \"" . $strokewidth . "\", ";
             $json_item .= "\"link\": \"" . url::base() . "reports/view/" . $incident_id . "\", ";
             $json_item .= "\"category\":[0], ";
             $json_item .= "\"timestamp\": \"" . strtotime($incident_date) . "\"";
             $json_item .= "},\"geometry\":" . json_encode($geom_array) . "}";
             $geometry[] = $json_item;
         }
     }
     return $geometry;
 }