-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.php
More file actions
116 lines (79 loc) · 2.42 KB
/
functions.php
File metadata and controls
116 lines (79 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
require_once './My_MySQLi.php';
require_once './IsbnService.php';
require_once './IsbnServiceJson.php';
/**
* PHP functions for metadataProxy
*
* @author K.Heim
*/
/**
* Prevalidates ISBN by removing whitespace, backslashes, special characters and hyphens.
* @param String $string
* @return String
*/
function preValidate($string) {
//remove whitespaceChars
$trimmed = trim($string);
//remove backslashes and html-characters
$stripped = stripcslashes($trimmed);
$htmlstripped = htmlspecialchars($stripped);
//remove hyphens
$result = preg_replace('/-/', '', $htmlstripped);
return $result;
}
/**
* Validates ISBN with a regular expression
* throws an exception, if validation fails.
* @param type $string
* @return boolean
* @throws Exception
*/
function validateIsbn($string) {
$pattern = '/^[0-9]{12}[0-9xX]$/';
if (preg_match($pattern, $string) && !empty($string)) {
return true;
}
throw new Exception("validateIsbn: Validation Error", 666);
}
/**
* checks local storage (mysql) if data already exists
* @param type $isbn
* @return boolean
*/
function checkMySQL($isbn) {
$mysql = new My_MySQLi("localhost", "root", "", "metadataproxy");
$query = "SELECT * FROM metadata WHERE isbn = '".$isbn."';";
$result = $mysql->query($query);
if ($result->num_rows > 0) {
$row = $result->fetch_row();
return $row[1];
} else {
return NULL;
}
}
/**
* Gets metadata from OCLC with IsbnServiceJSON.
* @param type $isbn
* @return JSON-String
*/
function getMetadatafromOclc($isbn) {
$oclcData = new IsbnServiceJson($isbn);
$resultString = $oclcData->getServiceData($isbn);
return $resultString;
}
/**
* inserts the new data from OCLC to the local storage (mysql)
* @param type $metadata, $isbn
*/
function insertMetadataintoDB($isbn, $metadata) {
//clear $metadata: replace ' with UTF-8-value
$specialCharacter = array("'");
$replaceCharacter = array("\xE2\x80\x99");
$cleanedMetadata = str_replace($specialCharacter,$replaceCharacter, $metadata);
$mysql = new My_MySQLi("localhost", "root", "", "metadataproxy");
$query = "INSERT INTO `metadata` "
. "(`isbn`, `jsonString`) "
. "VALUES ('".$isbn."','".$cleanedMetadata."');";
$mysql->query($query);
}