/
index.php
177 lines (157 loc) · 5.83 KB
/
index.php
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
if(isset($_GET['q']))
{
$urlQuery = urlencode('"cat '.$_GET['q'].'"');
$dbQuery = htmlentities($urlQuery);
$databaseConnected = false;
$queryInCache = false;
$useCache = true;
if(isset($_GET['cache']) && is_numeric($_GET['cache']) && $_GET['cache'] == 0)
$useCache = false;
$catImgUrls = array();
try
{
$db = new PDO("sqlite:cache.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$databaseConnected = true;
}
catch(PDOException $e)
{
$databaseConnected = false;
}
if($databaseConnected && $useCache)
{
try
{
$request = $db->prepare('SELECT * FROM cachingTable WHERE queryText = :queryText AND lastQueried > :lastQueried');
$request->execute(array('queryText'=>$dbQuery, 'lastQueried'=>date("Y-m-d", strtotime('-1 month'))));
$nbResults = 0;
while($result = $request->fetch())
{
++$nbResults;
$tmpurls = unserialize($result['urls']);
foreach($tmpurls as $url)
$catImgUrls[] = $url;
}
$request->closeCursor();
$queryInCache = $nbResults > 0;
//update lastQueried value
$request = $db->prepare('UPDATE cachingTable SET lastQueried = :lastQueried WHERE queryText = :queryText');
$request->execute(array('queryText'=>$dbQuery, 'lastQueried'=>date("Y-m-d")));
}
catch(PDOException $e)
{
$queryInCache = false;
}
}
if(!$databaseConnected || !$queryInCache)
{
//the database could not be accessed, or did not contain this query in cache, therefore we must load to populate the cache
$catImgUrls = array();
$url = 'https://lite.qwant.com/?q='.$urlQuery.'&t=images';
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTMLfile($url);
$a = new DOMXPath($dom);
$results = $a->query('//*[@class="resultimgs"]');
$catimgs = array();
for($i = 0; $i < $results->length; ++$i)
{
$resultimg = $results->item($i);
$metadata = array();
$img = $resultimg->getElementsByTagName('img');
$metadata['src'] = $img->item(0)->getAttribute('src');
$url = $resultimg->getElementsByTagName('a');
$metadata['url'] = $url->item(0)->getAttribute('href');
$imgmeta = $resultimg->getElementsByTagName('div')->item(0);
$metadata['title'] = $imgmeta->getElementsByTagName('h2')->item(0)->nodeValue;
$metadata['domainname'] = $imgmeta->getElementsByTagName('p')->item(0)->nodeValue;
$metadata['size'] = $imgmeta->getElementsByTagName('p')->item(1)->nodeValue;
$catImgUrls[] = $metadata;
}
if($databaseConnected)
{
$request = $db->prepare('INSERT OR REPLACE INTO cachingTable(queryText, page, lastQueried, urls) VALUES(:queryText, :page, :lastQueried, :urls)');
$request->execute(array('queryText'=>$dbQuery, 'page'=>0, 'lastQueried'=>date("Y-m-d"), 'urls'=>serialize($catImgUrls) ));
//delete cached values that are more than one month old
$request = $db->prepare('DELETE FROM cachingTable WHERE lastQueried <= :lastQueried');
$request->execute(array('lastQueried'=>date("Y-m-d", strtotime('-1 month'))));
}
}
?>
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title><?php echo $_GET['q']; ?> at CatCatGo</title>
<link rel="stylesheet" href="./style.css">
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
</head>
<body>
<header>
<a href="./"><img id="logo" src="./CatCatGo.svg" /></a>
<form method="get" action="./">
<input type="search" name="q" id="q" class="textarea" value="<?php echo $_GET['q']; ?>" />
<input type="submit" class="submit" value="" />
</form>
<aside>
Results powered by <a href="https://www.qwant.com/">Qwant</a>
</aside>
</header>
<section>
<?php
$count = 0;
foreach($catImgUrls as $catimg)
{
?>
<article>
<a href="<?php echo $catimg['url']; ?>"><img src="<?php echo $catimg['src']; ?>" /></a>
<div class="metadata">
<a href="<?php echo $catimg['url']; ?>"><h2><?php echo $catimg['title']; ?></h2></a>
<?php echo $catimg['size']; ?> -- <a class="domainname" href="http://<?php echo parse_url($catimg['url'])['host']; ?>"><?php echo $catimg['domainname']; ?></a>
</div>
</article>
<?php
$count++;
}
if($count == 0)
{
?>
<div id="catsanscat"><img src="./catsanscat.png" alt="no results" /></div>
<?php
}
?>
</section>
</body>
<?php
}
else
{
?>
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<title>CatCatGo</title>
<link rel="stylesheet" href="./style.css">
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
</head>
<body>
<div id="search-block">
<div id="logo">
<img src="./CatCatGo.svg" />
<h1>CatCatGo</h1>
</div>
<form method="get" action="./">
<input type="search" name="q" id="q" class="textarea" />
<input type="submit" class="submit" value="" />
</form>
</div>
</body>
</html>
<?php
}
?>