Issues (1240)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

modules/gmaps/libraries/Gmap.php (11 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php defined('SYSPATH') or die('No direct access allowed.');
2
/**
3
 * Google Maps API integration.
4
 *
5
 * $Id: Gmap.php 4302 2009-05-01 02:49:41Z kiall $
6
 *
7
 * @package    Gmaps
8
 * @author     Kohana Team
9
 * @copyright  (c) 2007-2008 Kohana Team
10
 * @license    http://kohanaphp.com/license.html
11
 */
12
class Gmap_Core
13
{
14
    
15
    // Map settings
16
    protected $id;
17
    protected $options;
18
    protected $center;
19
    protected $control;
20
    protected $overview_control;
21
    protected $type_control = false;
22
23
    // Map types
24
    protected $types = array();
25
    protected $default_types = array(
26
        'G_NORMAL_MAP','G_SATELLITE_MAP','G_HYBRID_MAP','G_PHYSICAL_MAP'
27
    );
28
    
29
    // Markers icons
30
    protected $icons = array();
31
32
    // Map markers
33
    protected $markers = array();
34
35
    /**
36
     * Set the GMap center point.
37
     *
38
     * @param string $id HTML map id attribute
39
     * @param array $options array of GMap constructor options
0 ignored issues
show
Should the type for parameter $options not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
40
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
41
     */
42
    public function __construct($id = 'map', $options = null)
43
    {
44
        // Set map ID and options
45
        $this->id = $id;
46
        $this->options = new Gmap_Options((array) $options);
47
    }
48
49
    /**
50
     * Return GMap javascript url
51
     *
52
     * @param   string  API component
53
     * @param   array   API parameters
54
     * @return  string
55
     */
56
     public static function api_url($component = 'jsapi', $parameters = null, $separator = '&amp;')
57
     {
58
         if (empty($parameters['ie'])) {
59
             // Set input encoding to UTF-8
60
            $parameters['ie'] = 'utf-8';
61
         }
62
63
         if (empty($parameters['oe'])) {
64
             // Set ouput encoding to input encoding
65
            $parameters['oe'] = $parameters['ie'];
66
         }
67
68
         if (empty($parameters['key'])) {
69
             // Set the API key last
70
            $parameters['key'] = Kohana::config('gmaps.api_key');
71
         }
72
73
         return 'http://'.Kohana::config('gmaps.api_domain').'/'.$component.'?'.http_build_query($parameters, '', $separator);
74
     }
75
76
    /**
77
     * Retrieves the latitude and longitude of an address.
78
     *
79
     * @param string $address address
80
     * @return array longitude, latitude
81
     */
82
    public static function address_to_ll($address)
83
    {
84
        $lat = null;
85
        $lon = null;
86
87
        if ($xml = Gmap::address_to_xml($address)) {
88
            // Get the latitude and longitude from the Google Maps XML
89
            // NOTE: the order (lon, lat) is the correct order
90
            list($lon, $lat) = explode(',', $xml->Response->Placemark->Point->coordinates);
91
        }
92
93
        return array($lat, $lon);
94
    }
95
96
    /**
97
     * Retrieves the XML geocode address lookup.
98
     * ! Results of this method are cached for 1 day.
99
     *
100
     * @param string $address adress
101
     * @return object SimpleXML
102
     */
103
    public static function address_to_xml($address)
104
    {
105
        static $cache;
106
107
        // Load Cache
108
        if ($cache === null) {
109
            $cache = Cache::instance();
110
        }
111
        
112
        // Address cache key
113
        $key = 'gmap-address-'.sha1($address);
114
115
        if ($xml = $cache->get($key)) {
116
            // Return the cached XML
117
            return simplexml_load_string($xml);
118
        } else {
119
            // Setup the retry counter and retry delay
120
            $remaining_retries = Kohana::config('gmaps.retries');
121
            $retry_delay = Kohana::config('gmaps.retry_delay');
122
123
            // Set the XML URL
124
            $xml_url = Gmap::api_url('maps/geo', array('output' => 'xml', 'q' => $address), '&');
125
126
            // Disable error reporting while fetching the feed
127
            $ER = error_reporting(~E_NOTICE);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ER. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
128
129
            // Enter the request/retry loop.
130
            while ($remaining_retries) {
131
                // Load the XML
132
                $xml = simplexml_load_file($xml_url);
133
134
                if (is_object($xml) and ($xml instanceof SimpleXMLElement) and (int) $xml->Response->Status->code === 200) {
135
                    // Cache the XML
136
                    $cache->set($key, $xml->asXML(), array('gmaps'), 86400);
137
138
                    // Since the geocode was successful, theres no need to try again
139
                    $remaining_retries = 0;
140
                } elseif ((int) $xml->Response->Status->code === 620) {
141
                    /* Goole is rate limiting us - either we're making too many requests too fast, or
142
                     * we've exceeded the 15k per 24hour limit. */
143
144
                    // Reduce the number of remaining retries
145
                    $remaining_retries--;
146
                    if (! $remaining_retries) {
147
                        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Gmap_Core::address_to_xml of type object.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
148
                    }
149
150
                    // Sleep for $retry_delay microseconds before trying again.
151
                    usleep($retry_delay);
152
                } else {
153
                    // Invalid XML response
154
                    $xml = false;
155
156
                    // Dont retry.
157
                    $remaining_retries = 0;
158
                }
159
            }
160
161
            // Turn error reporting back on
162
            error_reporting($ER);
163
        }
164
165
        return $xml;
166
    }
167
168
    /**
169
     * Returns an image map
170
     *
171
     * @param integer $lat latitude or an array of marker points
172
     * @param integer $lon longitude
173
     * @param integer $zoom zoom level (1-19)
174
     * @param string $type map type (roadmap or mobile)
0 ignored issues
show
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
175
     * @param integer $width map width
176
     * @param integer $height map height
177
     * @return string
178
     */
179
    public static function static_map($lat = 0, $lon = 0, $zoom = 6, $type = null, $width = 300, $height = 300)
180
    {
181
        // Valid map types
182
        $types = array('roadmap', 'mobile');
183
184
        // Maximum width and height are 640px
185
        $width = min(640, abs($width));
186
        $height = min(640, abs($height));
187
188
        $parameters['size'] = $width.'x'.$height;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$parameters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $parameters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
189
190
        // Minimum zoom = 0, maximum zoom = 19
191
        $parameters['zoom'] = max(0, min(19, abs($zoom)));
192
193
        if (in_array($type, $types)) {
194
            // Set map type
195
            $parameters['maptype'] = $type;
196
        }
197
198
        if (is_array($lat)) {
199
            foreach ($lat as $_lat => $_lon) {
200
                $parameters['markers'][] = $_lat.','.$_lon;
201
            }
202
203
            $parameters['markers'] = implode('|', $parameters['markers']);
204
        } else {
205
            $parameters['center'] = $lat.','.$lon;
206
        }
207
208
        return Gmap::api_url('staticmap', $parameters);
209
    }
210
211
    /**
212
     * Set the GMap center point.
213
     *
214
     * @chainable
215
     * @param float $lat latitude
216
     * @param float $lon longitude
217
     * @param integer $zoom zoom level (1-19)
218
     * @param string $type default map type
219
     * @return Gmap_Core
220
     */
221
    public function center($lat, $lon, $zoom = 6, $type = 'G_NORMAL_MAP')
222
    {
223
        $zoom = max(0, min(19, abs($zoom)));
224
        $type = ($type != 'G_NORMAL_MAP' and in_array($type, $this->default_types, true)) ? $type : 'G_NORMAL_MAP';
225
226
        // Set center location, zoom and default map type
227
        $this->center = array($lat, $lon, $zoom, $type);
228
229
        return $this;
230
    }
231
232
    /**
233
     * Set the GMap controls size.
234
     *
235
     * @chainable
236
     * @param string $size small or large
0 ignored issues
show
Should the type for parameter $size not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
237
     * @return Gmap_Core
238
     */
239
    public function controls($size = null)
240
    {
241
        // Set the control type
242
        $this->control = (strtolower($size) == 'small') ? 'Small' : 'Large';
243
244
        return $this;
245
    }
246
247
    /**
248
     * Set the GMap overview map.
249
     *
250
     * @chainable
251
     * @param integer $width width
0 ignored issues
show
Should the type for parameter $width not be string|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
252
     * @param integer $height height
0 ignored issues
show
Should the type for parameter $height not be string|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
253
     * @return Gmap_Core
254
     */
255
    public function overview($width = '', $height = '')
256
    {
257
        $size = (is_int($width) and is_int($height)) ? 'new GSize('.$width.','.$height.')' : '';
258
        $this->overview_control = 'map.addControl(new google.maps.OverviewMapControl('.$size.'));';
259
260
        return $this;
261
    }
262
263
    /**
264
     * Set the GMap type controls.
265
     * by default renders G_NORMAL_MAP, G_SATELLITE_MAP, and G_HYBRID_MAP
266
     *
267
     * @chainable
268
     * @param string $type map type
0 ignored issues
show
Should the type for parameter $type not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
269
     * @param string $action add or remove map type
270
     * @return Gmap_Core
271
     */
272
    public function types($type = null, $action = 'remove')
273
    {
274
        $this->type_control = true;
275
276
        if ($type !== null and in_array($type, $this->default_types, true)) {
277
            // Set the map type and action
278
            $this->types[$type] = (strtolower($action) == 'remove') ? 'remove' : 'add';
279
        }
280
281
        return $this;
282
    }
283
    
284
    /**
285
     * Create a custom marker icon
286
     *
287
     * @chainable
288
     * @param string $name icon name
289
     * @param array $options icon options
290
     * @return Gmap_Core
291
     */
292
    public function add_icon($name, array $options)
293
    {
294
        // Add a new cusotm icon
295
        $this->icons[] = new Gmap_Icon($name, $options);
296
297
        return $this;
298
    }
299
300
    /**
301
     * Set the GMap marker point.
302
     *
303
     * @chainable
304
     * @param float $lat latitude
305
     * @param float $lon longitude
306
     * @param string $html HTML for info window
307
     * @param array $options marker options
308
     * @return Gmap_Core
309
     */
310
    public function add_marker($lat, $lon, $html = '', $options = array())
311
    {
312
        // Add a new marker
313
        $this->markers[] = new Gmap_Marker($lat, $lon, $html, $options);
314
315
        return $this;
316
    }
317
318
    /**
319
     * Render the map into GMap Javascript.
320
     *
321
     * @param string $template template name
322
     * @param array $extra extra fields passed to the template
323
     * @return string
324
     */
325
    public function render($template = 'gmaps/javascript', $extra = array())
326
    {
327
        // Latitude, longitude, zoom and default map type
328
        list($lat, $lon, $zoom, $default_type) = $this->center;
329
330
        // Map
331
        $map = 'var map = new google.maps.Map2(document.getElementById("'.$this->id.'"));';
332
333
        // Map controls
334
        $controls[] = empty($this->control) ? '' : 'map.addControl(new google.maps.'.$this->control.'MapControl());';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$controls was never initialized. Although not strictly required by PHP, it is generally a good practice to add $controls = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
335
336
        // Map Types
337
        if ($this->type_control === true) {
338
            if (count($this->types) > 0) {
339
                foreach ($this->types as $type => $action) {
340
                    $controls[] = 'map.'.$action.'MapType('.$type.');';
341
                }
342
            }
343
344
            $controls[] = 'map.addControl(new google.maps.MapTypeControl());';
345
        }
346
347
        if (! empty($this->overview_control)) {
348
            $controls[] = $this->overview_control;
349
        }
350
351
        // Map centering
352
        $center = 'map.setCenter(new google.maps.LatLng('.$lat.', '.$lon.'), '.$zoom.', '.$default_type.');';
353
        
354
        $data = array_merge($extra, array(
355
                'map' => $map,
356
                'options' => $this->options,
357
                'controls' => implode("\n", $controls),
358
                'center' => $center,
359
                'icons' => $this->icons,
360
                'markers' => $this->markers,
361
            ));
362
363
        // Render the Javascript
364
        return View::factory($template, $data)->render();
365
    }
366
} // End Gmap
367