FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-cache.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2012 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: LGPL-2.1-only
6 */
7 
39 function ReportCacheGet($CacheKey)
40 {
41  global $PG_CONN;
42  global $UserCacheStat;
43 
44  /* Purge old entries ~ 1/500 of the times this fcn is called */
45  if (random_int(1, 500) == 1) {
46  ReportCachePurgeByDate(" now() - interval '365 days'");
47  }
48 
53  if ($UserCacheStat == 0) {
54  $sql = "SELECT cache_on FROM report_cache_user WHERE user_fk='$_SESSION[UserId]';";
55  $result = pg_query($PG_CONN, $sql);
56  DBCheckResult($result, $sql, __FILE__, __LINE__);
57  $row = pg_fetch_assoc($result);
58  pg_free_result($result);
59  if (! empty($row['cache_on']) && ($result['cache_on'] == 'N')) {
60  $UserCacheStat = 2;
61  return; /* cache is off for this user */
62  }
63  }
64 
65  $EscKey = pg_escape_string($CacheKey);
66 
67  // update time last accessed
68  $sql = "UPDATE report_cache SET report_cache_tla = now() WHERE report_cache_key='$EscKey';";
69  $result = pg_query($PG_CONN, $sql);
70  DBCheckResult($result, $sql, __FILE__, __LINE__);
71  pg_free_result($result);
72 
73  // Get the cached data
74  $sql = "SELECT report_cache_value FROM report_cache WHERE report_cache_key='$EscKey';";
75  $result = pg_query($PG_CONN, $sql);
76  DBCheckResult($result, $sql, __FILE__, __LINE__);
77  $row = pg_fetch_assoc($result);
78  if ($row == false) {
79  return;
80  }
81  $cashedvalue = $row['report_cache_value'];
82  pg_free_result($result);
83  return $cashedvalue;
84 } // ReportCacheGet()
85 
86 
94 function ReportCachePut($CacheKey, $CacheValue)
95 {
96  global $PG_CONN;
97  global $UserCacheStat;
98 
99  /* Check if user has cache turned off
100  * CacheUserStat is set in ReportCacheGet
101  * If it isn't, it is safe to fallback to the default
102  * behavior (cache is on).
103  */
104  if ($UserCacheStat == 2) {
105  return;
106  }
107 
108  $EscKey = pg_escape_string($CacheKey);
109  $EscValue = pg_escape_string($CacheValue);
110 
111  /* Parse the key. If the key is a uri */
112  /* look for upload =>, if not found, look for item => */
113  /* in order to get the upload key */
114  $ParsedURI = array();
115  parse_str($EscKey, $ParsedURI);
116  /* use 'upload= ' to define the upload in the cache key */
117  if (array_key_exists("upload", $ParsedURI)) {
118  $Upload = $ParsedURI['upload'];
119  }
120  if (array_key_exists("item", $ParsedURI)) {
121  $sql = "SELECT upload_fk FROM uploadtree WHERE uploadtree_pk='$ParsedURI[item]';";
122  $result = pg_query($PG_CONN, $sql);
123  DBCheckResult($result, $sql, __FILE__, __LINE__);
124 
125  $row = pg_fetch_assoc($result);
126  $Upload = $row['upload_fk'];
127  pg_free_result($result);
128  }
129  if (empty($Upload)) {
130  $Upload = "Null";
131  }
132 
133  $sql = "INSERT INTO report_cache (report_cache_key, report_cache_value, report_cache_uploadfk)
134  VALUES ('$EscKey', '$EscValue', $Upload);";
135  $result = pg_query($PG_CONN, $sql);
136  DBCheckResult($result, $sql, __FILE__, __LINE__);
137  pg_free_result($result);
138  $PGError = pg_last_error($PG_CONN);
139  /* If duplicate key, do an update, else report the error */
140  if (strpos($PGError, "uplicate") > 0) {
141  $sql = "UPDATE report_cache SET report_cache_value = '$EscValue', " .
142  "report_cache_tla=now() WHERE report_cache_key = '$EscKey';";
143  $result = pg_query($PG_CONN, $sql);
144  DBCheckResult($result, $sql, __FILE__, __LINE__);
145  pg_free_result($result);
146  }
147 } // ReportCacheInit()
148 
153 {
154  global $PG_CONN;
155  $sql = "DELETE FROM report_cache;";
156  $result = pg_query($PG_CONN, $sql);
157  DBCheckResult($result, $sql, __FILE__, __LINE__);
158  pg_free_result($result);
159 } // ReportCachePurgeAll()
160 
167 function ReportCachePurgeByDate($PurgeDate)
168 {
169  global $PG_CONN;
170  $sql = "DELETE FROM report_cache WHERE report_cache_tla < $PurgeDate;";
171  $result = pg_query($PG_CONN, $sql);
172  DBCheckResult($result, $sql, __FILE__, __LINE__);
173  pg_free_result($result);
174 } // ReportCachePurgeByDate()
175 
182 function ReportCachePurgeByUpload($UploadPK)
183 {
184  global $PG_CONN;
185  $sql = "DELETE FROM report_cache WHERE report_cache_uploadfk = $UploadPK;";
186  $result = pg_query($PG_CONN, $sql);
187  DBCheckResult($result, $sql, __FILE__, __LINE__);
188  pg_free_result($result);
189 } // ReportCachePurgeByUpload()
190 
199 function ReportCachePurgeByKey($CacheKey)
200 {
201  global $PG_CONN;
202  $ParsedURI = array();
203  $EscKey = pg_escape_string($CacheKey);
204  parse_str($EscKey, $ParsedURI);
205 
206  $sql = "DELETE FROM report_cache WHERE report_cache_key = '$EscKey';";
207  $result = pg_query($PG_CONN, $sql);
208  DBCheckResult($result, $sql, __FILE__, __LINE__);
209  $Err = pg_last_error($PG_CONN);
210  pg_free_result($result);
211 
212  return $Err;
213 } // ReportCachePurgeByKey()
ReportCacheGet($CacheKey)
This function is used by Output() to see if the requested report is in the report cache.
ReportCachePurgeByDate($PurgeDate)
Purge from the report cache records that have been accessed previous to $PurgeDate.
ReportCachePut($CacheKey, $CacheValue)
This function is used to write a record to the report cache. If the record already exists,...
ReportCachePurgeByKey($CacheKey)
Purge from the report cache the record with $CacheKey.
ReportCachePurgeAll()
Purge all records from the report cache.
ReportCachePurgeByUpload($UploadPK)
Purge from the report cache records for upload $UploadPK.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN