Bug 18854: Make sure offset will not be < 0 - protect from DoS
[koha.git] / C4 / UsageStats.pm
1 package C4::UsageStats;
2
3 # This file is part of Koha.
4 #
5 # Copyright 2014 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21 use C4::Context;
22 use POSIX qw(strftime);
23 use LWP::UserAgent;
24 use JSON;
25
26 use Koha::Libraries;
27
28 =head1 NAME
29
30 C4::UsageStats
31
32 =head1 DESCRIPTION
33
34 This package contains what is needed to report Koha statistics to hea
35 hea.koha-community.org is the server that centralize Koha setups informations
36 Koha libraries are encouraged to provide informations about their collections,
37 their structure,...
38
39 =head2 NeedUpdate
40
41   $needUpdateYN = C4::UsageStats::NeedUpdate;
42
43 Returns Y (1) if the last update is more than 1 month old
44 This way, even if the cronjob is run every minute, the webservice will be called
45 only once a month !
46
47 =cut
48
49 sub NeedUpdate {
50     my $lastupdated = C4::Context->preference('UsageStatsLastUpdateTime') || 0;
51     my $now = strftime( "%s", localtime );
52
53     # Need to launch cron.
54     return 1 if $now - $lastupdated >= 2592000;
55
56     # Data don't need to be updated
57     return 0;
58 }
59
60 sub BuildReport {
61     my $report;
62     my @libraries;
63     if( C4::Context->preference('UsageStatsLibrariesInfo') ) {
64         my $libraries = Koha::Libraries->search;
65         while ( my $library = $libraries->next ) {
66             push @libraries, { name => $library->branchname, url => $library->branchurl, country => $library->branchcountry, geolocation => $library->geolocation, };
67         }
68     }
69     $report = {
70         installation => {
71             koha_id => C4::Context->preference('UsageStatsID')          || 0,
72             name    => C4::Context->preference('UsageStatsLibraryName') || q||,
73             url     => C4::Context->preference('UsageStatsLibraryUrl')  || q||,
74             type    => C4::Context->preference('UsageStatsLibraryType') || q||,
75             country => C4::Context->preference('UsageStatsCountry')     || q||,
76             geolocation => C4::Context->preference('UsageStatsGeolocation') || q||,
77         },
78         libraries => \@libraries,
79     };
80
81     # Get database volumetry.
82     foreach (
83         qw/biblio items auth_header old_issues old_reserves borrowers aqorders subscription/
84       )
85     {
86         $report->{volumetry}{$_} = _count($_);
87     }
88
89     # Get systempreferences.
90     foreach (
91         qw/
92         AcqCreateItem
93         AcqWarnOnDuplicateInvoice
94         AcqViewBaskets
95         BasketConfirmations
96         OrderPdfFormat
97         casAuthentication
98         casLogout
99         AllowPKIAuth
100         DebugLevel
101         delimiter
102         noItemTypeImages
103         virtualshelves
104         AutoLocation
105         IndependentBranches
106         SessionStorage
107         Persona
108         AuthDisplayHierarchy
109         AutoCreateAuthorities
110         BiblioAddsAuthorities
111         AuthorityMergeLimit
112         AuthorityMergeMode
113         UseAuthoritiesForTracings
114         CatalogModuleRelink
115         hide_marc
116         IntranetBiblioDefaultView
117         LabelMARCView
118         OpacSuppression
119         SeparateHoldings
120         UseControlNumber
121         advancedMARCeditor
122         DefaultClassificationSource
123         EasyAnalyticalRecords
124         autoBarcode
125         item-level_itypes
126         marcflavour
127         PrefillItem
128         z3950NormalizeAuthor
129         SpineLabelAutoPrint
130         SpineLabelShowPrintOnBibDetails
131         BlockReturnOfWithdrawnItems
132         CalculateFinesOnReturn
133         AgeRestrictionOverride
134         AllFinesNeedOverride
135         AllowFineOverride
136         AllowItemsOnHoldCheckout
137         AllowItemsOnHoldCheckoutSCO
138         AllowNotForLoanOverride
139         AllowRenewalLimitOverride
140         AllowReturnToBranch
141         AllowTooManyOverride
142         AutomaticItemReturn
143         AutoRemoveOverduesRestrictions
144         CircControl
145         HomeOrHoldingBranch
146         InProcessingToShelvingCart
147         IssueLostItem
148         IssuingInProcess
149         ManInvInNoissuesCharge
150         OverduesBlockCirc
151         RenewalPeriodBase
152         RenewalSendNotice
153         RentalsInNoissuesCharge
154         ReturnBeforeExpiry
155         ReturnToShelvingCart
156         TransfersMaxDaysWarning
157         UseBranchTransferLimits
158         useDaysMode
159         UseTransportCostMatrix
160         UseCourseReserves
161         finesCalendar
162         FinesIncludeGracePeriod
163         finesMode
164         RefundLostOnReturnControl
165         WhenLostChargeReplacementFee
166         WhenLostForgiveFine
167         AllowHoldDateInFuture
168         AllowHoldPolicyOverride
169         AllowHoldsOnDamagedItems
170         AllowHoldsOnPatronsPossessions
171         AutoResumeSuspendedHolds
172         canreservefromotherbranches
173         decreaseLoanHighHolds
174         DisplayMultiPlaceHold
175         emailLibrarianWhenHoldIsPlaced
176         ExpireReservesMaxPickUpDelay
177         OPACAllowHoldDateInFuture
178         OPACAllowUserToChooseBranch
179         ReservesControlBranch
180         ReservesNeedReturns
181         SuspendHoldsIntranet
182         SuspendHoldsOpac
183         TransferWhenCancelAllWaitingHolds
184         AllowAllMessageDeletion
185         AllowOfflineCirculation
186         CircAutocompl
187         CircAutoPrintQuickSlip
188         DisplayClearScreenButton
189         FilterBeforeOverdueReport
190         FineNotifyAtCheckin
191         itemBarcodeFallbackSearch
192         itemBarcodeInputFilter
193         previousIssuesDefaultSortOrder
194         RecordLocalUseOnReturn
195         AudioAlerts
196         SpecifyDueDate
197         todaysIssuesDefaultSortOrder
198         UpdateTotalIssuesOnCirc
199         UseTablesortForCirc
200         WaitingNotifyAtCheckin
201         AllowSelfCheckReturns
202         AutoSelfCheckAllowed
203         FRBRizeEditions
204         OPACFRBRizeEditions
205         AmazonCoverImages
206         OPACAmazonCoverImages
207         Babeltheque
208         BakerTaylorEnabled
209         GoogleJackets
210         HTML5MediaEnabled
211         IDreamBooksReadometer
212         IDreamBooksResults
213         IDreamBooksReviews
214         LibraryThingForLibrariesEnabled
215         LocalCoverImages
216         OPACLocalCoverImages
217         NovelistSelectEnabled
218         XISBN
219         OpenLibraryCovers
220         OpenLibrarySearch
221         UseKohaPlugins
222         SyndeticsEnabled
223         TagsEnabled
224         CalendarFirstDayOfWeek
225         opaclanguagesdisplay
226         AuthoritiesLog
227         BorrowersLog
228         CataloguingLog
229         FinesLog
230         IssueLog
231         LetterLog
232         ReturnLog
233         SubscriptionLog
234         BiblioDefaultView
235         COinSinOPACResults
236         DisplayOPACiconsXSLT
237         hidelostitems
238         HighlightOwnItemsOnOPAC
239         OpacAddMastheadLibraryPulldown
240         OPACDisplay856uAsImage
241         OpacHighlightedWords
242         OpacKohaUrl
243         OpacMaintenance
244         OpacPublic
245         OpacSeparateHoldings
246         OPACShowBarcode
247         OPACShowCheckoutName
248         OpacShowFiltersPulldownMobile
249         OPACShowHoldQueueDetails
250         OpacShowLibrariesPulldownMobile
251         OpacShowRecentComments
252         OPACShowUnusedAuthorities
253         OpacStarRatings
254         opacthemes
255         OPACURLOpenInNewWindow
256         OpacAuthorities
257         opacbookbag
258         OpacBrowser
259         OpacBrowseResults
260         OpacCloud
261         OPACFinesTab
262         OpacHoldNotes
263         OpacItemLocation
264         OpacPasswordChange
265         OPACPatronDetails
266         OPACpatronimages
267         OPACPopupAuthorsSearch
268         OpacTopissue
269         opacuserlogin
270         QuoteOfTheDay
271         RequestOnOpac
272         reviewson
273         ShowReviewer
274         ShowReviewerPhoto
275         SocialNetworks
276         suggestion
277         AllowPurchaseSuggestionBranchChoice
278         OpacAllowPublicListCreation
279         OpacAllowSharingPrivateLists
280         OpacRenewalAllowed
281         OpacRenewalBranch
282         OPACViewOthersSuggestions
283         SearchMyLibraryFirst
284         AnonSuggestions
285         EnableOpacSearchHistory
286         OPACPrivacy
287         opacreadinghistory
288         TrackClicks
289         PatronSelfRegistration
290         OPACShelfBrowser
291         AutoEmailOpacUser
292         AutoEmailPrimaryAddress
293         autoMemberNum
294         BorrowerRenewalPeriodBase
295         checkdigit
296         EnableBorrowerFiles
297         EnhancedMessagingPreferences
298         ExtendedPatronAttributes
299         intranetreadinghistory
300         memberofinstitution
301         patronimages
302         TalkingTechItivaPhoneNotification
303         uppercasesurnames
304         IncludeSeeFromInSearches
305         OpacGroupResults
306         QueryAutoTruncate
307         QueryFuzzy
308         QueryStemming
309         QueryWeightFields
310         TraceCompleteSubfields
311         TraceSubjectSubdivisions
312         UseICU
313         UseQueryParser
314         defaultSortField
315         displayFacetCount
316         OPACdefaultSortField
317         OPACItemsResultsDisplay
318         expandedSearchOption
319         IntranetNumbersPreferPhrase
320         OPACNumbersPreferPhrase
321         opacSerialDefaultTab
322         RenewSerialAddsSuggestion
323         RoutingListAddReserves
324         RoutingSerials
325         SubscriptionHistory
326         Display856uAsImage
327         DisplayIconsXSLT
328         template
329         yuipath
330         HidePatronName
331         intranetbookbag
332         StaffDetailItemSelection
333         viewISBD
334         viewLabeledMARC
335         viewMARC
336         ILS-DI
337         OAI-PMH
338         version
339         /
340       )
341     {
342         $report->{systempreferences}{$_} = C4::Context->preference($_);
343     }
344     return $report;
345 }
346
347 =head2 ReportToCommunity
348
349   ReportToCommunity;
350
351 Send to hea.koha-community.org database informations
352
353 =cut
354
355 sub ReportToCommunity {
356     my $data = shift;
357     my $json = encode_json($data);
358
359     my $url = "https://hea.koha-community.org/upload.pl";
360     my $ua = LWP::UserAgent->new;
361     my $res = $ua->post(
362         $url,
363         'Content-type' => 'application/json;charset=utf-8',
364         Content => $json,
365     );
366     my $content = decode_json( $res->decoded_content );
367     if ( $content->{koha_id} ) {
368         C4::Context->set_preference( 'UsageStatsID', $content->{koha_id} );
369     }
370     if ( $content->{id} ) {
371         C4::Context->set_preference( 'UsageStatsPublicID', $content->{id} );
372     }
373 }
374
375 =head2 _count
376
377   $data = _count($table);
378
379 Count the number of records in $table tables
380
381 =cut
382
383 sub _count {
384     my $table = shift;
385
386     my $dbh = C4::Context->dbh;
387     my $sth = $dbh->prepare("SELECT count(*) from $table");
388     $sth->execute;
389     return $sth->fetchrow_array;
390 }
391
392 1;