Issues (54)

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.

app/Search/SearchableDocument.php (8 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
2
3
namespace Colligator\Search;
4
5
use Colligator\Document;
6
use Colligator\XisbnResponse;
7
8
class SearchableDocument
9
{
10
    /**
11
     * @var DocumentsIndex
12
     */
13
    protected $docIndex;
14
15
    /**
16
     * @var string
17
     */
18
    protected $sortableCallCodePattern = '/FA ([0-9]+)(\/([A-Z]))?/';
19
20
    /**
21
     * @param Document       $doc
22
     * @param DocumentsIndex $docIndex
23
     */
24
    public function __construct(Document $doc, DocumentsIndex $docIndex)
25
    {
26
        $this->doc = $doc;
27
        $this->docIndex = $docIndex;
28
    }
29
30
    /**
31
     * Generate ElasticSearch index payload.
32
     *
33
     * @return array
34
     */
35
    public function toArray()
36
    {
37
        $body = $this->doc->bibliographic;  // PHP makes a copy for us
38
39
        $body['id'] = $this->doc->id;
40
        $body['bibsys_id'] = $this->doc->bibsys_id;
41
42
        // Remove some stuff we don't need
43
        foreach (['agency', 'catalogingRules', 'debug', 'modified', 'extent', 'cover_image'] as $key) {
44
            unset($body[$key]);
45
        }
46
47
        // Add local collections
48
        $body['collections'] = [];
49
        foreach ($this->doc->collections as $collection) {
50
            $body['collections'][] = $collection['name'];
51
        }
52
53
        // Add cover
54
        $body['cover'] = $this->doc->cover ? $this->doc->cover->toArray() : null;
55
56
        // Add subjects
57
        $body['subjects'] = [];
58
        foreach ($this->doc->subjects as $subject) {
59
            $body['subjects'][$subject['vocabulary'] ?: 'keywords'][] = [
60
                'id'        => array_get($subject, 'id'),
61
                'prefLabel' => str_replace('--', ' : ', array_get($subject, 'term')),
62
                'type'      => array_get($subject, 'type'),
63
                'count'     => $this->docIndex->getUsageCount(array_get($subject, 'id'), 'subject'),
64
            ];
65
        }
66
67
        // Add genres
68
        $body['genres'] = [];
69
        foreach ($this->doc->genres as $genre) {
70
            $body['genres'][$genre['vocabulary'] ?: 'keywords'][] = [
71
                'id'        => array_get($genre, 'id'),
72
                'prefLabel' => array_get($genre, 'term'),
73
                'count'     => $this->docIndex->getUsageCount(array_get($genre, 'id'), 'genre'),
74
            ];
75
        }
76
77
        // Add holdings
78
        $this->addHoldings($body, $this->doc);
79
80
        // Add xisbns
81
        $body['xisbns'] = (new XisbnResponse($this->doc->xisbn))->getSimpleRepr();
82
83
        // Add description
84
        $body['description'] = $this->doc->description;
85
86
        // Add 'other form'
87
        $otherFormId = array_get($body, 'other_form.id');
88
        if (!empty($otherFormId)) {
89
90
            // @TODO: https://github.com/scriptotek/colligator-backend/issues/34
91
            // Not sure how to handle this in Alma yet
92
            unset($body['other_form']);
93
            // $otherFormDoc = Document::where('bibsys_id', '=', $otherFormId)->firstOrFail();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
            // $body['other_form'] = [
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
95
            //     'id'         => $otherFormDoc->id,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
            //     'bibsys_id'  => $otherFormDoc->bibsys_id,
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
            //     'electronic' => $otherFormDoc->isElectronic(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
            // ];
99
            // $this->addHoldings($body['other_form'], $otherFormDoc);
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
        }
101
102
        // Add 'cannot_find_cover'
103
        $body['cannot_find_cover'] = $this->doc->cannot_find_cover;
104
105
        return $body;
106
    }
107
108
    public function sortableCallCode($holding)
109
    {
110
        $m = preg_match($this->sortableCallCodePattern, array_get($holding, 'callcode'), $matches);
111
        if ($m) {
112
            return intval($matches[1]);
113
            // TODO: OgsĂĄ ta hensyn til undersortering i $matches[3], men
114
            // denne er en blanding av romertall og alfabetisk sortering
115
            // https://github.com/scriptotek/colligator-backend/issues/28
116
        }
117
118
        return;
119
    }
120
121
    public function addHoldings(&$body, Document $doc)
122
    {
123
        if ($doc->isElectronic()) {
0 ignored issues
show
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
124
125
            // @ TODO: Virker ikke med Alma
126
            // https://github.com/scriptotek/colligator-backend/issues/34
127
            // $body['fulltext'] = $this->fulltextFromHoldings($doc->holdings);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
128
129
        } else {
130
            $body['holdings'] = [];
131
            foreach ($doc->holdings as $holding) {
132
                array_forget($holding, 'fulltext');
133
                array_forget($holding, 'bibliographic_record');
134
                array_forget($holding, 'nonpublic_notes');
135
                $s = $this->sortableCallCode($holding);
136
                if (!is_null($s)) {
137
                    $holding['callcodeSortable'] = $s;
138
                }
139
                $body['holdings'][] = $holding;
140
            }
141
        }
142
    }
143
144
    public function fulltextFromHoldings($holdings)
145
    {
146
        $fulltext = [
147
            'access' => false,
148
        ];
149
        foreach ($holdings as $holding) {
150
            if (!count($holding['fulltext'])) {
151
                continue;
152
            }
153
            if ($holding['location'] == 'UBO' || stripos($holding['fulltext'][0]['comment'], 'gratis') !== false) {
154
                $fulltext = $holding['fulltext'][0];
155
                $fulltext['access'] = true;
156
            }
157
        }
158
159
        return $fulltext;
160
    }
161
}
162