Completed
Push — master ( 67a224...a05403 )
by Simon
02:08
created

ShortList::isBrowser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/**
4
 * @file
5
 * This is the model for the short list URLs.
6
 */
7
8
/**
9
 * A model to store the short list of items and a unique URL.
10
 */
11
class ShortList extends DataObject
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
12
{
13
14
    /**
15
     * Fields.
16
     */
17
    private static $db = array(
18
        'SessionID'     => 'varchar(32)',
19
        'URL'           => 'Varchar(100)',
20
        'UserAgent'     => 'Varchar(512)'
21
    );
22
23
    /**
24
     * Attaches to many items.
25
     */
26
    private static $has_many = array(
27
        'ShortListItems' => 'ShortListItem'
28
    );
29
30
    /**
31
     * Where the URL is unique.
32
     */
33
    private static $indexes = array(
34
        'UniqueURL'     => array(
35
            'type'      => 'unique',
36
            'value'     => 'URL'
37
        )
38
    );
39
40
    /**
41
     * Ensure we populate these fields before a save.
42
     */
43
    public function onBeforeWrite()
44
    {
45
46
        // Run other beforewrites first.
47
        parent::onBeforeWrite();
48
49
        if (!$this->isBrowser()) {
50
            return false;
51
        }
52
53
        // If this is the first save...
54
        if (!$this->ID) {
55
            // Ensure the session exists before querying it.
56
            if (!Session::request_contains_session_id()) {
57
                Session::start();
58
            }
59
60
            // Store the sesion and has information in the database.
61
            $this->SessionID = session_id();
62
63
            if (is_null($this->SessionID)) {
64
                return false;
65
            }
66
67
            $gen = new RandomGenerator();
68
            $uniqueurl = substr($gen->randomToken(), 0, 32);
69
70
            while (ShortList::get()->filter('URL', $uniqueurl)->count() > 0) {
71
                $uniqueurl = substr($gen->randomToken(), 0, 32);
72
            }
73
74
            $this->URL = $uniqueurl;
75
            $this->UserAgent = Controller::curr()->getRequest()->getHeader('User-Agent');
76
        }
77
    }
78
79
    /**
80
     * All URLs are prefixed with URLSegment from config.
81
     */
82
    public function Link($action = null)
83
    {
84
        return Config::inst()->get('ShortList', 'URLSegment') . $this->URL;
85
    }
86
87
    /**
88
     * A basic browser detection.
89
     * * if a browser is not here - assume it's a crawler or a bot. In which case, it shouldn't create a shortlist.
90
     *
91
     * @see http://stackoverflow.com/a/1537636
92
     * */
93
    public static function isBrowser()
94
    {
95
        // Regular expression to match common browsers
96
        $browserlist = '/(opera|aol|msie|firefox|chrome|konqueror|safari|netscape|navigator|mosaic|lynx|amaya|omniweb|avant|camino|flock|seamonkey|mozilla|gecko)+/i';
97
98
        $validBrowser = preg_match($browserlist, Controller::curr()->getRequest()->getHeader('User-Agent')]) === 1;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ']', expecting ',' or ')'
Loading history...
99
100
        return $validBrowser;
101
    }
102
}
103