@@ -20,34 +20,34 @@ discard block |
||
| 20 | 20 | * @copyright 2018 Ruben Dorado |
| 21 | 21 | * @license http://www.opensource.org/licenses/MIT The MIT License |
| 22 | 22 | */ |
| 23 | -class Persistence{ |
|
| 23 | +class Persistence { |
|
| 24 | 24 | |
| 25 | 25 | /* |
| 26 | 26 | * @param Configuration $config |
| 27 | 27 | * |
| 28 | 28 | * @return PDO |
| 29 | 29 | */ |
| 30 | - public static function getPDO($config){ |
|
| 30 | + public static function getPDO($config) { |
|
| 31 | 31 | $options = array( |
| 32 | 32 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION |
| 33 | 33 | ); |
| 34 | 34 | |
| 35 | - if($config->getDsn()){ |
|
| 36 | - try{ |
|
| 37 | - return new PDO($config->getDsn(),$config->getUser(),$config->getPassword(),$options); |
|
| 35 | + if ($config->getDsn()) { |
|
| 36 | + try { |
|
| 37 | + return new PDO($config->getDsn(), $config->getUser(), $config->getPassword(), $options); |
|
| 38 | 38 | } |
| 39 | - catch(Exception $e){ |
|
| 40 | - if(!$config->getUseOnMemoryDB()){ |
|
| 39 | + catch (Exception $e) { |
|
| 40 | + if (!$config->getUseOnMemoryDB()) { |
|
| 41 | 41 | throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage()); |
| 42 | 42 | } |
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - if($config->getUseOnMemoryDB()){ |
|
| 47 | - try{ |
|
| 48 | - return new PDO("sqlite::memory:",$options); |
|
| 46 | + if ($config->getUseOnMemoryDB()) { |
|
| 47 | + try { |
|
| 48 | + return new PDO("sqlite::memory:", $options); |
|
| 49 | 49 | } |
| 50 | - catch(Exception $e){ |
|
| 50 | + catch (Exception $e) { |
|
| 51 | 51 | throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage()); |
| 52 | 52 | } |
| 53 | 53 | } |
@@ -61,8 +61,8 @@ discard block |
||
| 61 | 61 | * @param $config Configuration |
| 62 | 62 | * |
| 63 | 63 | */ |
| 64 | - public static function crateDatabase($pdo, $config){ |
|
| 65 | - try{ |
|
| 64 | + public static function crateDatabase($pdo, $config) { |
|
| 65 | + try { |
|
| 66 | 66 | |
| 67 | 67 | $db_hit_table = $config->getHitTableName(); |
| 68 | 68 | $db_options_table = $config->getOptionsTableName(); |
@@ -78,7 +78,7 @@ discard block |
||
| 78 | 78 | $stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)"); |
| 79 | 79 | $stmt->execute(); |
| 80 | 80 | } |
| 81 | - catch(Exception $e){ |
|
| 81 | + catch (Exception $e) { |
|
| 82 | 82 | throw new Exception("Could not create the database. ".$e->getMessage()); |
| 83 | 83 | } |
| 84 | 84 | return true; |
@@ -91,7 +91,7 @@ discard block |
||
| 91 | 91 | * @param $config Configuration |
| 92 | 92 | * |
| 93 | 93 | */ |
| 94 | - public static function deleteDatabase($pdo, $config){ |
|
| 94 | + public static function deleteDatabase($pdo, $config) { |
|
| 95 | 95 | $resp = true; |
| 96 | 96 | |
| 97 | 97 | $db_hit_table = $config->getHitTableName(); |
@@ -113,14 +113,14 @@ discard block |
||
| 113 | 113 | * @param $config Configuration |
| 114 | 114 | * |
| 115 | 115 | */ |
| 116 | - private static function dropTable($pdo, $tableName){ |
|
| 117 | - try{ |
|
| 116 | + private static function dropTable($pdo, $tableName) { |
|
| 117 | + try { |
|
| 118 | 118 | |
| 119 | 119 | $stmt = $pdo->prepare("DROP TABLE $tableName"); |
| 120 | 120 | $stmt->execute(); |
| 121 | 121 | |
| 122 | 122 | } |
| 123 | - catch(Exception $e){ |
|
| 123 | + catch (Exception $e) { |
|
| 124 | 124 | throw new Exception("Problem deleting the table $tableName. ".$e->getMessage()); |
| 125 | 125 | } |
| 126 | 126 | return true; |
@@ -132,16 +132,16 @@ discard block |
||
| 132 | 132 | * @param $config Configuration |
| 133 | 133 | * |
| 134 | 134 | */ |
| 135 | - public static function checkTables($pdo, $config){ |
|
| 135 | + public static function checkTables($pdo, $config) { |
|
| 136 | 136 | $resp = true; |
| 137 | - try{ |
|
| 137 | + try { |
|
| 138 | 138 | |
| 139 | 139 | $resp = $resp && Persistence::checkHitTable($pdo, $config); |
| 140 | 140 | $resp = $resp && Persistence::checkOptionsTable($pdo, $config); |
| 141 | 141 | $resp = $resp && Persistence::checkFromTable($pdo, $config); |
| 142 | 142 | $resp = $resp && Persistence::checkUrlTable($pdo, $config); |
| 143 | 143 | } |
| 144 | - catch(Exception $e){ |
|
| 144 | + catch (Exception $e) { |
|
| 145 | 145 | return false; |
| 146 | 146 | } |
| 147 | 147 | return $resp; |
@@ -152,14 +152,14 @@ discard block |
||
| 152 | 152 | * @param $pdo PDO |
| 153 | 153 | * @param $config Configuration |
| 154 | 154 | */ |
| 155 | - public static function checkFromTable($pdo, $config){ |
|
| 156 | - try{ |
|
| 155 | + public static function checkFromTable($pdo, $config) { |
|
| 156 | + try { |
|
| 157 | 157 | $db_from_table = $config->getFromTableName(); |
| 158 | 158 | $stmt = $pdo->prepare("SELECT * FROM $db_from_table WHERE 1==0"); |
| 159 | 159 | $stmt->execute(); |
| 160 | 160 | |
| 161 | 161 | } |
| 162 | - catch(Exception $e){ |
|
| 162 | + catch (Exception $e) { |
|
| 163 | 163 | return false; |
| 164 | 164 | } |
| 165 | 165 | return true; |
@@ -169,13 +169,13 @@ discard block |
||
| 169 | 169 | * @param $pdo PDO |
| 170 | 170 | * @param $config Configuration |
| 171 | 171 | */ |
| 172 | - public static function checkUrlTable($pdo, $config){ |
|
| 173 | - try{ |
|
| 172 | + public static function checkUrlTable($pdo, $config) { |
|
| 173 | + try { |
|
| 174 | 174 | $db_url_table = $config->getUrlTableName(); |
| 175 | 175 | $stmt = $pdo->prepare("SELECT * FROM $db_url_table WHERE 1==0"); |
| 176 | 176 | $stmt->execute(); |
| 177 | 177 | } |
| 178 | - catch(Exception $e){ |
|
| 178 | + catch (Exception $e) { |
|
| 179 | 179 | return false; |
| 180 | 180 | } |
| 181 | 181 | return true; |
@@ -187,13 +187,13 @@ discard block |
||
| 187 | 187 | * @param $pdo PDO |
| 188 | 188 | * @param $config Configuration |
| 189 | 189 | */ |
| 190 | - public static function checkOptionsTable($pdo, $config){ |
|
| 191 | - try{ |
|
| 190 | + public static function checkOptionsTable($pdo, $config) { |
|
| 191 | + try { |
|
| 192 | 192 | $db_options_table = $config->getOptionsTableName(); |
| 193 | 193 | $stmt = $pdo->prepare("SELECT * FROM $db_options_table WHERE 1==0"); |
| 194 | 194 | $stmt->execute(); |
| 195 | 195 | } |
| 196 | - catch(Exception $e){ |
|
| 196 | + catch (Exception $e) { |
|
| 197 | 197 | return false; |
| 198 | 198 | } |
| 199 | 199 | return true; |
@@ -203,13 +203,13 @@ discard block |
||
| 203 | 203 | * @param $pdo PDO |
| 204 | 204 | * @param $config Configuration |
| 205 | 205 | */ |
| 206 | - public static function checkHitTable($pdo, $config){ |
|
| 207 | - try{ |
|
| 206 | + public static function checkHitTable($pdo, $config) { |
|
| 207 | + try { |
|
| 208 | 208 | $db_hit_table = $config->getHitTableName(); |
| 209 | 209 | $stmt = $pdo->prepare("SELECT * FROM $db_hit_table WHERE 1==0"); |
| 210 | 210 | $stmt->execute(); |
| 211 | 211 | } |
| 212 | - catch(Exception $e){ |
|
| 212 | + catch (Exception $e) { |
|
| 213 | 213 | return false; |
| 214 | 214 | } |
| 215 | 215 | return true; |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | * @param $config Configuration |
| 222 | 222 | * |
| 223 | 223 | */ |
| 224 | - public static function updateCount($pdo, $config, $options=[]){ |
|
| 224 | + public static function updateCount($pdo, $config, $options = []) { |
|
| 225 | 225 | |
| 226 | 226 | $db_hit_table = $config->getHitTableName(); |
| 227 | 227 | $db_options_table = $config->getOptionsTableName(); |
@@ -232,54 +232,54 @@ discard block |
||
| 232 | 232 | $store_time = true; |
| 233 | 233 | $store_user = true; |
| 234 | 234 | |
| 235 | - if(array_key_exists('url', $options)){ |
|
| 235 | + if (array_key_exists('url', $options)) { |
|
| 236 | 236 | $url = $options['url']; |
| 237 | 237 | } |
| 238 | - else if(array_key_exists('HTTP_HOST',$_SERVER)){ |
|
| 238 | + else if (array_key_exists('HTTP_HOST', $_SERVER)) { |
|
| 239 | 239 | $url = "http://".$_SERVER['HTTP_HOST']; |
| 240 | - if(array_key_exists('REQUEST_URI',$_SERVER)){ |
|
| 241 | - $url=$url.$_SERVER['REQUEST_URI']; |
|
| 240 | + if (array_key_exists('REQUEST_URI', $_SERVER)) { |
|
| 241 | + $url = $url.$_SERVER['REQUEST_URI']; |
|
| 242 | 242 | } |
| 243 | 243 | } |
| 244 | - else{ |
|
| 244 | + else { |
|
| 245 | 245 | $url = "No Info"; |
| 246 | 246 | } |
| 247 | 247 | |
| 248 | - if($config->getRemoveQueryString()){ |
|
| 248 | + if ($config->getRemoveQueryString()) { |
|
| 249 | 249 | $url = preg_replace('/\?.*/', '', $url); |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | - if(array_key_exists('id', $options)){ |
|
| 252 | + if (array_key_exists('id', $options)) { |
|
| 253 | 253 | $id = $options['id']; |
| 254 | 254 | } |
| 255 | - else{ |
|
| 255 | + else { |
|
| 256 | 256 | $id = $url; |
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | $stmt = $pdo->prepare("UPDATE $db_hit_table SET count = count + 1 WHERE id = ?"); |
| 260 | 260 | $stmt->execute([$id]); |
| 261 | - if( $stmt->rowCount() == 0 ){ |
|
| 261 | + if ($stmt->rowCount()==0) { |
|
| 262 | 262 | $stmt = $pdo->prepare("INSERT INTO $db_hit_table (id, count) VALUES (?, 1)"); |
| 263 | 263 | $stmt->execute([$id]); |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | $stmt = $pdo->prepare("UPDATE $db_url_table SET count = count + 1 WHERE id = ? and url = ?"); |
| 267 | 267 | $stmt->execute([$id, $url]); |
| 268 | - if( $stmt->rowCount() == 0 ){ |
|
| 268 | + if ($stmt->rowCount()==0) { |
|
| 269 | 269 | $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)"); |
| 270 | 270 | $stmt->execute([$id, $url]); |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | |
| 274 | - if($store_from){ |
|
| 274 | + if ($store_from) { |
|
| 275 | 275 | |
| 276 | - if(array_key_exists('from_id', $options)){ |
|
| 276 | + if (array_key_exists('from_id', $options)) { |
|
| 277 | 277 | $ids = [$options['from_id']]; |
| 278 | 278 | } |
| 279 | - else{ |
|
| 279 | + else { |
|
| 280 | 280 | $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info'; |
| 281 | - $ids = Persistence::findHitIdsByUrl($pdo,$config,$from_url); |
|
| 282 | - if(count($ids)==0){ |
|
| 281 | + $ids = Persistence::findHitIdsByUrl($pdo, $config, $from_url); |
|
| 282 | + if (count($ids)==0) { |
|
| 283 | 283 | $stmt = $pdo->prepare("INSERT INTO $db_url_table (id, url, count) VALUES (?, ?, 1)"); |
| 284 | 284 | $stmt->execute([$from_url, $from_url]); |
| 285 | 285 | $ids = [$from_url]; |
@@ -288,7 +288,7 @@ discard block |
||
| 288 | 288 | foreach ($ids as $from_id) { |
| 289 | 289 | $stmt = $pdo->prepare("UPDATE $db_from_table SET count = count + 1 WHERE id = ? and from_id = ?"); |
| 290 | 290 | $stmt->execute([$id, $from_id]); |
| 291 | - if( $stmt->rowCount() == 0 ){ |
|
| 291 | + if ($stmt->rowCount()==0) { |
|
| 292 | 292 | $stmt = $pdo->prepare("INSERT INTO $db_from_table (id, from_id, count) VALUES (?, ?, 1)"); |
| 293 | 293 | $stmt->execute([$id, $from_id]); |
| 294 | 294 | } |
@@ -296,13 +296,13 @@ discard block |
||
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | $user = null; |
| 299 | - if($store_user){ |
|
| 300 | - if(array_key_exists('user', $options)){ |
|
| 299 | + if ($store_user) { |
|
| 300 | + if (array_key_exists('user', $options)) { |
|
| 301 | 301 | $user = $options['user']; |
| 302 | 302 | } |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - if($store_time || $store_user){ |
|
| 305 | + if ($store_time || $store_user) { |
|
| 306 | 306 | $stmt = $pdo->prepare("INSERT INTO $db_options_table (id, time, user) VALUES (?, ?, ?)"); |
| 307 | 307 | $stmt->execute([$id, time(), $user]); |
| 308 | 308 | } |
@@ -315,19 +315,19 @@ discard block |
||
| 315 | 315 | * @param $config Configuration |
| 316 | 316 | * |
| 317 | 317 | */ |
| 318 | - public static function findHitIdsByUrl($pdo, $config, $url){ |
|
| 318 | + public static function findHitIdsByUrl($pdo, $config, $url) { |
|
| 319 | 319 | $resp = []; |
| 320 | - try{ |
|
| 320 | + try { |
|
| 321 | 321 | |
| 322 | 322 | $dbtable = $config->getUrlTableName(); |
| 323 | 323 | $stmt = $pdo->prepare("SELECT id,url,count FROM $dbtable WHERE url = '$url'"); |
| 324 | - if($stmt->execute()){ |
|
| 325 | - while($row = $stmt->fetch()){ |
|
| 324 | + if ($stmt->execute()) { |
|
| 325 | + while ($row = $stmt->fetch()) { |
|
| 326 | 326 | $resp[] = $row['id']; |
| 327 | 327 | } |
| 328 | 328 | } |
| 329 | 329 | } |
| 330 | - catch(Exception $e){ |
|
| 330 | + catch (Exception $e) { |
|
| 331 | 331 | throw new Exception("Error executing function 'findHitsByUrl'. ".$e->getMessage()); |
| 332 | 332 | } |
| 333 | 333 | return $resp; |
@@ -340,20 +340,20 @@ discard block |
||
| 340 | 340 | * @param $config Configuration |
| 341 | 341 | * |
| 342 | 342 | */ |
| 343 | - public static function getAllHits($pdo, $config){ |
|
| 343 | + public static function getAllHits($pdo, $config) { |
|
| 344 | 344 | $resp = []; |
| 345 | - try{ |
|
| 345 | + try { |
|
| 346 | 346 | |
| 347 | 347 | $dbtable = $config->getHitTableName(); |
| 348 | 348 | $stmt = $pdo->prepare("SELECT id,count FROM $dbtable"); |
| 349 | - if($stmt->execute()){ |
|
| 350 | - while($row = $stmt->fetch()){ |
|
| 351 | - $resp[] = [$row['id'],$row['count']]; |
|
| 349 | + if ($stmt->execute()) { |
|
| 350 | + while ($row = $stmt->fetch()) { |
|
| 351 | + $resp[] = [$row['id'], $row['count']]; |
|
| 352 | 352 | } |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | } |
| 356 | - catch(Exception $e){ |
|
| 356 | + catch (Exception $e) { |
|
| 357 | 357 | throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage()); |
| 358 | 358 | } |
| 359 | 359 | return $resp; |
@@ -365,36 +365,36 @@ discard block |
||
| 365 | 365 | * @param $config Configuration |
| 366 | 366 | * |
| 367 | 367 | */ |
| 368 | - public static function findUrls($pdo, $config, $by=[]){ |
|
| 368 | + public static function findUrls($pdo, $config, $by = []) { |
|
| 369 | 369 | $resp = []; |
| 370 | - try{ |
|
| 370 | + try { |
|
| 371 | 371 | $dbtable = $config->getUrlTableName(); |
| 372 | 372 | $qdata = []; |
| 373 | 373 | $tquery = []; |
| 374 | - if(array_key_exists('id',$by)){ |
|
| 374 | + if (array_key_exists('id', $by)) { |
|
| 375 | 375 | $qdata[] = $by['id']; |
| 376 | 376 | $tquery[] = "id = ?"; |
| 377 | 377 | } |
| 378 | 378 | |
| 379 | - if(array_key_exists('url',$by)){ |
|
| 379 | + if (array_key_exists('url', $by)) { |
|
| 380 | 380 | $qdata[] = $by['url']; |
| 381 | 381 | $tquery[] = "url = ?"; |
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | $sql = "SELECT id,url,count FROM $dbtable"; |
| 385 | - if(count($tquery) > 0){ |
|
| 386 | - $sql = $sql." WHERE ".join(" AND ",$tquery); |
|
| 385 | + if (count($tquery) > 0) { |
|
| 386 | + $sql = $sql." WHERE ".join(" AND ", $tquery); |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | $stmt = $pdo->prepare($sql); |
| 390 | - if($stmt->execute($qdata)){ |
|
| 391 | - while($row = $stmt->fetch()){ |
|
| 392 | - $resp[] = [$row['id'],$row['url'],$row['count']]; |
|
| 390 | + if ($stmt->execute($qdata)) { |
|
| 391 | + while ($row = $stmt->fetch()) { |
|
| 392 | + $resp[] = [$row['id'], $row['url'], $row['count']]; |
|
| 393 | 393 | } |
| 394 | 394 | } |
| 395 | 395 | |
| 396 | 396 | } |
| 397 | - catch(Exception $e){ |
|
| 397 | + catch (Exception $e) { |
|
| 398 | 398 | throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage()); |
| 399 | 399 | } |
| 400 | 400 | return $resp; |
@@ -405,41 +405,41 @@ discard block |
||
| 405 | 405 | * @param $config Configuration |
| 406 | 406 | * |
| 407 | 407 | */ |
| 408 | - public static function findIdByTimeUser($pdo, $config, $by=[]){ |
|
| 409 | - $resp = [ ]; |
|
| 410 | - try{ |
|
| 408 | + public static function findIdByTimeUser($pdo, $config, $by = []) { |
|
| 409 | + $resp = []; |
|
| 410 | + try { |
|
| 411 | 411 | $dbtable = $config->getOptionsTableName(); |
| 412 | 412 | $qdata = []; |
| 413 | 413 | $tquery = []; |
| 414 | - if(array_key_exists('from',$by)){ |
|
| 414 | + if (array_key_exists('from', $by)) { |
|
| 415 | 415 | $qdata[] = $by['from']; |
| 416 | 416 | $tquery[] = "time >= ?"; |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | - if(array_key_exists('to',$by)){ |
|
| 419 | + if (array_key_exists('to', $by)) { |
|
| 420 | 420 | $qdata[] = $by['to']; |
| 421 | 421 | $tquery[] = "time <= ?"; |
| 422 | 422 | } |
| 423 | 423 | |
| 424 | - if(array_key_exists('user',$by)){ |
|
| 424 | + if (array_key_exists('user', $by)) { |
|
| 425 | 425 | $qdata[] = $by['user']; |
| 426 | 426 | $tquery[] = "user = ?"; |
| 427 | 427 | } |
| 428 | 428 | |
| 429 | 429 | $sql = "SELECT id,time,user FROM $dbtable"; |
| 430 | - if(count($tquery) > 0){ |
|
| 431 | - $sql = $sql." WHERE ".join(" AND ",$tquery); |
|
| 430 | + if (count($tquery) > 0) { |
|
| 431 | + $sql = $sql." WHERE ".join(" AND ", $tquery); |
|
| 432 | 432 | } |
| 433 | 433 | |
| 434 | 434 | $stmt = $pdo->prepare($sql); |
| 435 | - if($stmt->execute($qdata)){ |
|
| 436 | - while($row = $stmt->fetch()){ |
|
| 437 | - $resp[] = [$row['id'],$row['time'],$row['user']]; |
|
| 435 | + if ($stmt->execute($qdata)) { |
|
| 436 | + while ($row = $stmt->fetch()) { |
|
| 437 | + $resp[] = [$row['id'], $row['time'], $row['user']]; |
|
| 438 | 438 | } |
| 439 | 439 | } |
| 440 | 440 | |
| 441 | 441 | } |
| 442 | - catch(Exception $e){ |
|
| 442 | + catch (Exception $e) { |
|
| 443 | 443 | throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage()); |
| 444 | 444 | } |
| 445 | 445 | return $resp; |
@@ -451,40 +451,40 @@ discard block |
||
| 451 | 451 | * @param $config Configuration |
| 452 | 452 | * |
| 453 | 453 | */ |
| 454 | - public static function findByFrom($pdo, $config, $by=[]){ |
|
| 454 | + public static function findByFrom($pdo, $config, $by = []) { |
|
| 455 | 455 | $resp = []; |
| 456 | - try{ |
|
| 456 | + try { |
|
| 457 | 457 | $dbFromtable = $config->getFromTableName(); |
| 458 | 458 | $dbUrltable = $config->getUrlTableName(); |
| 459 | 459 | $qdata = []; |
| 460 | 460 | $tquery = []; |
| 461 | 461 | |
| 462 | - if(array_key_exists('url',$by) && array_key_exists('id',$by)){ |
|
| 462 | + if (array_key_exists('url', $by) && array_key_exists('id', $by)) { |
|
| 463 | 463 | $qdata = [$by['url'], $by['id']]; |
| 464 | 464 | $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?"; |
| 465 | 465 | } |
| 466 | - else if(array_key_exists('url',$by)){ |
|
| 466 | + else if (array_key_exists('url', $by)) { |
|
| 467 | 467 | $qdata = [$by['url']]; |
| 468 | 468 | $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?"; |
| 469 | 469 | } |
| 470 | - else if(array_key_exists('id',$by)){ |
|
| 470 | + else if (array_key_exists('id', $by)) { |
|
| 471 | 471 | $qdata = [$by['id']]; |
| 472 | 472 | $tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?"; |
| 473 | 473 | } |
| 474 | - else{ |
|
| 474 | + else { |
|
| 475 | 475 | $qdata = []; |
| 476 | 476 | $tquery = "SELECT f.* FROM $dbFromtable f"; |
| 477 | 477 | } |
| 478 | 478 | |
| 479 | 479 | $stmt = $pdo->prepare($tquery); |
| 480 | - if($stmt->execute($qdata)){ |
|
| 481 | - while($row = $stmt->fetch()){ |
|
| 482 | - $resp[] = [$row['id'],$row['from_id'],$row['count']]; |
|
| 480 | + if ($stmt->execute($qdata)) { |
|
| 481 | + while ($row = $stmt->fetch()) { |
|
| 482 | + $resp[] = [$row['id'], $row['from_id'], $row['count']]; |
|
| 483 | 483 | } |
| 484 | 484 | } |
| 485 | 485 | |
| 486 | 486 | } |
| 487 | - catch(Exception $e){ |
|
| 487 | + catch (Exception $e) { |
|
| 488 | 488 | throw new Exception("Error executing function 'findByFrom'. ".$e->getMessage()); |
| 489 | 489 | } |
| 490 | 490 | return $resp; |
@@ -500,19 +500,19 @@ discard block |
||
| 500 | 500 | public static function getCounts($pdo, $config) |
| 501 | 501 | { |
| 502 | 502 | $resp = []; |
| 503 | - try{ |
|
| 503 | + try { |
|
| 504 | 504 | |
| 505 | 505 | $dbHitTable = $config->getHitTableName(); |
| 506 | 506 | $dbUrlTable = $config->getUrlTableName(); |
| 507 | 507 | $stmt = $pdo->prepare("SELECT h.id, u.url, h.count FROM $dbHitTable h, $dbUrlTable u WHERE h.id=u.id"); |
| 508 | - if($stmt->execute()){ |
|
| 509 | - while($row = $stmt->fetch()){ |
|
| 510 | - $resp[] = [$row[0],$row[1],$row[2],]; |
|
| 508 | + if ($stmt->execute()) { |
|
| 509 | + while ($row = $stmt->fetch()) { |
|
| 510 | + $resp[] = [$row[0], $row[1], $row[2], ]; |
|
| 511 | 511 | } |
| 512 | 512 | } |
| 513 | 513 | |
| 514 | 514 | } |
| 515 | - catch(Exception $e){ |
|
| 515 | + catch (Exception $e) { |
|
| 516 | 516 | throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
| 517 | 517 | } |
| 518 | 518 | return $resp; |
@@ -524,20 +524,20 @@ discard block |
||
| 524 | 524 | * @param $config Configuration |
| 525 | 525 | * |
| 526 | 526 | */ |
| 527 | - public static function getHitsWithOptions($pdo, $config){ |
|
| 527 | + public static function getHitsWithOptions($pdo, $config) { |
|
| 528 | 528 | $resp = []; |
| 529 | - try{ |
|
| 529 | + try { |
|
| 530 | 530 | |
| 531 | 531 | $dbOptionsTable = $config->getOptionsTableName(); |
| 532 | 532 | $stmt = $pdo->prepare("SELECT o.id, o.time, o.user FROM $dbOptionsTable o"); |
| 533 | - if($stmt->execute()){ |
|
| 534 | - while($row = $stmt->fetch()){ |
|
| 535 | - $resp[] = ['id'=>$row[0],'time'=>$row[1],'user'=>$row[2],]; |
|
| 533 | + if ($stmt->execute()) { |
|
| 534 | + while ($row = $stmt->fetch()) { |
|
| 535 | + $resp[] = ['id'=>$row[0], 'time'=>$row[1], 'user'=>$row[2], ]; |
|
| 536 | 536 | } |
| 537 | 537 | } |
| 538 | 538 | |
| 539 | 539 | } |
| 540 | - catch(Exception $e){ |
|
| 540 | + catch (Exception $e) { |
|
| 541 | 541 | throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
| 542 | 542 | } |
| 543 | 543 | return $resp; |
@@ -548,7 +548,7 @@ discard block |
||
| 548 | 548 | * @param $config Configuration |
| 549 | 549 | * |
| 550 | 550 | */ |
| 551 | - public static function getCountsById($pdo, $config){ |
|
| 551 | + public static function getCountsById($pdo, $config) { |
|
| 552 | 552 | return "Works"; |
| 553 | 553 | } |
| 554 | 554 | } |
@@ -35,8 +35,7 @@ discard block |
||
| 35 | 35 | if($config->getDsn()){ |
| 36 | 36 | try{ |
| 37 | 37 | return new PDO($config->getDsn(),$config->getUser(),$config->getPassword(),$options); |
| 38 | - } |
|
| 39 | - catch(Exception $e){ |
|
| 38 | + } catch(Exception $e){ |
|
| 40 | 39 | if(!$config->getUseOnMemoryDB()){ |
| 41 | 40 | throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage()); |
| 42 | 41 | } |
@@ -46,8 +45,7 @@ discard block |
||
| 46 | 45 | if($config->getUseOnMemoryDB()){ |
| 47 | 46 | try{ |
| 48 | 47 | return new PDO("sqlite::memory:",$options); |
| 49 | - } |
|
| 50 | - catch(Exception $e){ |
|
| 48 | + } catch(Exception $e){ |
|
| 51 | 49 | throw new Exception("Could not create a db connection. Check permissions, configuration, and documentation. ".$e->getMessage()); |
| 52 | 50 | } |
| 53 | 51 | } |
@@ -77,8 +75,7 @@ discard block |
||
| 77 | 75 | $stmt->execute(); |
| 78 | 76 | $stmt = $pdo->prepare("CREATE TABLE $db_url_table (id VARCHAR(255), url VARCHAR(255), count INT)"); |
| 79 | 77 | $stmt->execute(); |
| 80 | - } |
|
| 81 | - catch(Exception $e){ |
|
| 78 | + } catch(Exception $e){ |
|
| 82 | 79 | throw new Exception("Could not create the database. ".$e->getMessage()); |
| 83 | 80 | } |
| 84 | 81 | return true; |
@@ -119,8 +116,7 @@ discard block |
||
| 119 | 116 | $stmt = $pdo->prepare("DROP TABLE $tableName"); |
| 120 | 117 | $stmt->execute(); |
| 121 | 118 | |
| 122 | - } |
|
| 123 | - catch(Exception $e){ |
|
| 119 | + } catch(Exception $e){ |
|
| 124 | 120 | throw new Exception("Problem deleting the table $tableName. ".$e->getMessage()); |
| 125 | 121 | } |
| 126 | 122 | return true; |
@@ -140,8 +136,7 @@ discard block |
||
| 140 | 136 | $resp = $resp && Persistence::checkOptionsTable($pdo, $config); |
| 141 | 137 | $resp = $resp && Persistence::checkFromTable($pdo, $config); |
| 142 | 138 | $resp = $resp && Persistence::checkUrlTable($pdo, $config); |
| 143 | - } |
|
| 144 | - catch(Exception $e){ |
|
| 139 | + } catch(Exception $e){ |
|
| 145 | 140 | return false; |
| 146 | 141 | } |
| 147 | 142 | return $resp; |
@@ -158,8 +153,7 @@ discard block |
||
| 158 | 153 | $stmt = $pdo->prepare("SELECT * FROM $db_from_table WHERE 1==0"); |
| 159 | 154 | $stmt->execute(); |
| 160 | 155 | |
| 161 | - } |
|
| 162 | - catch(Exception $e){ |
|
| 156 | + } catch(Exception $e){ |
|
| 163 | 157 | return false; |
| 164 | 158 | } |
| 165 | 159 | return true; |
@@ -174,8 +168,7 @@ discard block |
||
| 174 | 168 | $db_url_table = $config->getUrlTableName(); |
| 175 | 169 | $stmt = $pdo->prepare("SELECT * FROM $db_url_table WHERE 1==0"); |
| 176 | 170 | $stmt->execute(); |
| 177 | - } |
|
| 178 | - catch(Exception $e){ |
|
| 171 | + } catch(Exception $e){ |
|
| 179 | 172 | return false; |
| 180 | 173 | } |
| 181 | 174 | return true; |
@@ -192,8 +185,7 @@ discard block |
||
| 192 | 185 | $db_options_table = $config->getOptionsTableName(); |
| 193 | 186 | $stmt = $pdo->prepare("SELECT * FROM $db_options_table WHERE 1==0"); |
| 194 | 187 | $stmt->execute(); |
| 195 | - } |
|
| 196 | - catch(Exception $e){ |
|
| 188 | + } catch(Exception $e){ |
|
| 197 | 189 | return false; |
| 198 | 190 | } |
| 199 | 191 | return true; |
@@ -208,8 +200,7 @@ discard block |
||
| 208 | 200 | $db_hit_table = $config->getHitTableName(); |
| 209 | 201 | $stmt = $pdo->prepare("SELECT * FROM $db_hit_table WHERE 1==0"); |
| 210 | 202 | $stmt->execute(); |
| 211 | - } |
|
| 212 | - catch(Exception $e){ |
|
| 203 | + } catch(Exception $e){ |
|
| 213 | 204 | return false; |
| 214 | 205 | } |
| 215 | 206 | return true; |
@@ -234,14 +225,12 @@ discard block |
||
| 234 | 225 | |
| 235 | 226 | if(array_key_exists('url', $options)){ |
| 236 | 227 | $url = $options['url']; |
| 237 | - } |
|
| 238 | - else if(array_key_exists('HTTP_HOST',$_SERVER)){ |
|
| 228 | + } else if(array_key_exists('HTTP_HOST',$_SERVER)){ |
|
| 239 | 229 | $url = "http://".$_SERVER['HTTP_HOST']; |
| 240 | 230 | if(array_key_exists('REQUEST_URI',$_SERVER)){ |
| 241 | 231 | $url=$url.$_SERVER['REQUEST_URI']; |
| 242 | 232 | } |
| 243 | - } |
|
| 244 | - else{ |
|
| 233 | + } else{ |
|
| 245 | 234 | $url = "No Info"; |
| 246 | 235 | } |
| 247 | 236 | |
@@ -251,8 +240,7 @@ discard block |
||
| 251 | 240 | |
| 252 | 241 | if(array_key_exists('id', $options)){ |
| 253 | 242 | $id = $options['id']; |
| 254 | - } |
|
| 255 | - else{ |
|
| 243 | + } else{ |
|
| 256 | 244 | $id = $url; |
| 257 | 245 | } |
| 258 | 246 | |
@@ -275,8 +263,7 @@ discard block |
||
| 275 | 263 | |
| 276 | 264 | if(array_key_exists('from_id', $options)){ |
| 277 | 265 | $ids = [$options['from_id']]; |
| 278 | - } |
|
| 279 | - else{ |
|
| 266 | + } else{ |
|
| 280 | 267 | $from_url = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'No referer info'; |
| 281 | 268 | $ids = Persistence::findHitIdsByUrl($pdo,$config,$from_url); |
| 282 | 269 | if(count($ids)==0){ |
@@ -326,8 +313,7 @@ discard block |
||
| 326 | 313 | $resp[] = $row['id']; |
| 327 | 314 | } |
| 328 | 315 | } |
| 329 | - } |
|
| 330 | - catch(Exception $e){ |
|
| 316 | + } catch(Exception $e){ |
|
| 331 | 317 | throw new Exception("Error executing function 'findHitsByUrl'. ".$e->getMessage()); |
| 332 | 318 | } |
| 333 | 319 | return $resp; |
@@ -352,8 +338,7 @@ discard block |
||
| 352 | 338 | } |
| 353 | 339 | } |
| 354 | 340 | |
| 355 | - } |
|
| 356 | - catch(Exception $e){ |
|
| 341 | + } catch(Exception $e){ |
|
| 357 | 342 | throw new Exception("Error executing function 'getAllHits'. ".$e->getMessage()); |
| 358 | 343 | } |
| 359 | 344 | return $resp; |
@@ -393,8 +378,7 @@ discard block |
||
| 393 | 378 | } |
| 394 | 379 | } |
| 395 | 380 | |
| 396 | - } |
|
| 397 | - catch(Exception $e){ |
|
| 381 | + } catch(Exception $e){ |
|
| 398 | 382 | throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage()); |
| 399 | 383 | } |
| 400 | 384 | return $resp; |
@@ -438,8 +422,7 @@ discard block |
||
| 438 | 422 | } |
| 439 | 423 | } |
| 440 | 424 | |
| 441 | - } |
|
| 442 | - catch(Exception $e){ |
|
| 425 | + } catch(Exception $e){ |
|
| 443 | 426 | throw new Exception("Error executing function 'getAllUrls'. ".$e->getMessage()); |
| 444 | 427 | } |
| 445 | 428 | return $resp; |
@@ -462,16 +445,13 @@ discard block |
||
| 462 | 445 | if(array_key_exists('url',$by) && array_key_exists('id',$by)){ |
| 463 | 446 | $qdata = [$by['url'], $by['id']]; |
| 464 | 447 | $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u WHERE (f.from_id = u.id and f.url = ?) or f.from_id = ?"; |
| 465 | - } |
|
| 466 | - else if(array_key_exists('url',$by)){ |
|
| 448 | + } else if(array_key_exists('url',$by)){ |
|
| 467 | 449 | $qdata = [$by['url']]; |
| 468 | 450 | $tquery = "SELECT f.* FROM $dbFromtable f,$dbUrltable u where f.from_id = u.id and u.url = ?"; |
| 469 | - } |
|
| 470 | - else if(array_key_exists('id',$by)){ |
|
| 451 | + } else if(array_key_exists('id',$by)){ |
|
| 471 | 452 | $qdata = [$by['id']]; |
| 472 | 453 | $tquery = "SELECT f.* FROM $dbFromtable f where f.from_id = ?"; |
| 473 | - } |
|
| 474 | - else{ |
|
| 454 | + } else{ |
|
| 475 | 455 | $qdata = []; |
| 476 | 456 | $tquery = "SELECT f.* FROM $dbFromtable f"; |
| 477 | 457 | } |
@@ -483,8 +463,7 @@ discard block |
||
| 483 | 463 | } |
| 484 | 464 | } |
| 485 | 465 | |
| 486 | - } |
|
| 487 | - catch(Exception $e){ |
|
| 466 | + } catch(Exception $e){ |
|
| 488 | 467 | throw new Exception("Error executing function 'findByFrom'. ".$e->getMessage()); |
| 489 | 468 | } |
| 490 | 469 | return $resp; |
@@ -511,8 +490,7 @@ discard block |
||
| 511 | 490 | } |
| 512 | 491 | } |
| 513 | 492 | |
| 514 | - } |
|
| 515 | - catch(Exception $e){ |
|
| 493 | + } catch(Exception $e){ |
|
| 516 | 494 | throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
| 517 | 495 | } |
| 518 | 496 | return $resp; |
@@ -536,8 +514,7 @@ discard block |
||
| 536 | 514 | } |
| 537 | 515 | } |
| 538 | 516 | |
| 539 | - } |
|
| 540 | - catch(Exception $e){ |
|
| 517 | + } catch(Exception $e){ |
|
| 541 | 518 | throw new Exception("Error reading the database. Method getCounts().".$e->getMessage()); |
| 542 | 519 | } |
| 543 | 520 | return $resp; |
@@ -118,8 +118,7 @@ |
||
| 118 | 118 | foreach ($data as $row) { |
| 119 | 119 | if (array_key_exists($row['user'], $count)) { |
| 120 | 120 | $count[$row['user']]++; |
| 121 | - } |
|
| 122 | - else { |
|
| 121 | + } else { |
|
| 123 | 122 | $count[$row['user']] = 1; |
| 124 | 123 | } |
| 125 | 124 | } |