-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_opensource.php
More file actions
287 lines (217 loc) · 9.67 KB
/
search_opensource.php
File metadata and controls
287 lines (217 loc) · 9.67 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
$username = "";
$password = "";
//Base script from https://owlcation.com/stem/Simple-search-PHP-MySQL
mysql_connect("localhost", $username, $password) or die("Error connecting to database: ".mysql_error());
/*
localhost - it's location of the mysql server, usually localhost
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("ows_index") or die(mysql_error());
/* ows_index is the name of database we've created */
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<?php
require_once 'HTTP/Request2.php';
$request = new Http_Request2('https://api.cognitive.microsoft.com/bing/v5.0/search');
$request->setAdapter('curl');
#$request->setAdapter('curl');
$url = $request->getUrl();
$headers = array(
// Request headers
'Ocp-Apim-Subscription-Key' => '',
);
$request->setHeader($headers);
$query = $_GET['query'];
$parameters = array(
// Request parameters
'q' => $query,
'count' => '10',
'offset' => '0',
'mkt' => 'en-us',
'safesearch' => 'Moderate',
);
$url->setQueryVariables($parameters);
$request->setMethod(HTTP_Request2::METHOD_GET);
// Request body
$request->setBody("{body}");
// gets value sent over search form
$min_length = 1;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
print '<title>SuperAI.online - Search results for: '.$query.'</title>';
?>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<link rel="stylesheet" type="text/css" href="./search-style/style2.css"/>
</head>
<body>
<section id="banner2">
<br>
<h1><a href="search.html"><img src="superai-logo.png" alt="SuperAI Search"></a></h1>
<p>The next generation AI Search Engine Powered by Mohawk Search.</p>
</section>
<div class="container">
<p>
<form action="search.php" method="get">
<div class="row">
<div>
<div id="custom-search-input">
<div class="input-group col-md-12">
<input type="text" class="form-control input-lg" name="query" id="query"/>
<span class="input-group-btn">
<button class="btn btn-info btn-lg" type="submit">
<i class="glyphicon glyphicon-search"></i>
</button>
</span>
</div>
</div>
</div>
</div>
</form>
</p>
<?php
think($query);
print "You searched for: "." ".$query."<br><br>";
$query = "+".$query;
$query = str_replace(' ', ' +', $query);
$raw_results = mysql_query("SELECT title, anchor_text, hostname, page, ((1.3 * (MATCH(title) AGAINST ('$query' IN BOOLEAN MODE))) + (0.6 * (MATCH(anchor_text) AGAINST ('$query' IN BOOLEAN MODE)))) AS relevance FROM pages WHERE (MATCH(title, anchor_text) AGAINST ('$query' IN BOOLEAN MODE) ) ORDER BY relevance DESC") or die(mysql_error());
$raw_results2 = mysql_query("SELECT title, anchor_text, hostname, page FROM pages
WHERE title != '' AND match (anchor_text) against ('$query' IN BOOLEAN MODE) or match (title) against ('$query' WITH QUERY EXPANSION) order by anchor_text <> 'query2', anchor_text LIMIT 150") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo '<p><h3><a href="http://'.$results[2].$results[3].'">'.$results[0]."</h3></a>".$results[2].$results[3]."<br><br>".$results[1]."</p>".PHP_EOL;
}
}
else{ // if there is no match=ing rows do following
echo "No results from SuperAI.online with strict matching, expanded results...";
while($results = mysql_fetch_array($raw_results2)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo '<p><h3><a href="http://'.$results[2].$results[3].'">'.$results[0]."</h3></a>".$results[2].$results[3]."<br><br>".$results[1]."</p>".PHP_EOL;
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
echo "<br>Results from Bing<br>";
try
{
$response = $request->send();
//echo $response->getBody();
$result = json_decode($response->getBody(), true);
print "<p>";
foreach ($result['webPages']['value'] as $item) {
echo '<a href="'.$item['url'].'">'.$item['name'].'</a>';
print "<br>";
print $item['snippet'];
print "<br>";
print $item['displayUrl'];
print "<br><br>";
//Make variable $last_title have bing's name
$last_title = $item['name'].PHP_EOL;
}
print "</p>";
}
catch (HttpException $ex)
{
echo $ex;
}
}
}
else{ // if query length is less than minimum
echo "Minimum length is ".$min_length;
}
?>
</p>
<br>
<br>
<iframe src="//rcm-na.amazon-adsystem.com/e/cm?o=1&p=13&l=ez&f=ifr&linkID=f95bacbb71cea7597e498f2cb1d0ab21&t=ppctweakies-20&tracking_id=ppctweakies-20" width="468" height="60" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe>
</div>
<!-- Start of StatCounter Code for Default Guide -->
<script type="text/javascript">
var sc_project=11120522;
var sc_invisible=1;
var sc_security="6a588ac2";
var sc_https=1;
var sc_remove_link=1;
var scJsHost = (("https:" == document.location.protocol) ?
"https://secure." : "http://www.");
document.write("<sc"+"ript type='text/javascript' src='" +
scJsHost+
"statcounter.com/counter/counter.js'></"+"script>");
</script>
<noscript><div class="statcounter"><img class="statcounter"
src="//c.statcounter.com/11120522/0/6a588ac2/1/" alt="web
analytics"></div></noscript>
<!-- End of StatCounter Code for Default Guide -->
</body>
</html>
<?php
//Save to title.txt on server
$file = './title/title.txt';
file_put_contents($file, $last_title, FILE_APPEND | LOCK_EX);
function think ($query_input)
{
$tsubasa_age = date("Y") - 1989;
$current_month = date("M");
if ($current_month < 9){
$tsubasa_age = $tsubasa_age - 1;
}
if (preg_match("/hello/i", $query_input)){
echo 'Hi, I\'m glad you decided to talk to me.<br>';
echo "I was created by Tsubasa Kato, age $tsubasa_age a.k.a stingraze.<br>";
echo "You can talk to me more below.<br>";
echo '<iframe width="350" height="430" src="https://console.api.ai/api-client/demo/embedded/c2b9ee77-8217-4d92-8712-b4c21f50261d"></iframe><br>';
}
if ($query_input === 'hi'){
echo 'Hi, I\'m glad you decided to talk to me.<br>';
echo "I was created by Tsubasa Kato, age $tsubasa_age a.k.a stingraze.<br>";
echo "You can talk to me more below.<br>";
echo '<iframe width="350" height="430" src="https://console.api.ai/api-client/demo/embedded/c2b9ee77-8217-4d92-8712-b4c21f50261d"></iframe><br>';
}
if (preg_match("/hi!/i", $query_input)){
echo 'Hello!<br>';
echo "I was born in 2016, but my core engine, Mohawk Search was born in 2003. <br>";
}
if (preg_match("/bonjour/i", $query_input)){
echo 'Bonjour!<br>';
}
if (preg_match("/Good morning/i", $query_input)){
echo 'Good morning! How\'s the weather at your place?<br>';
}
if (preg_match("/Sing for me/i", $query_input)){
echo 'I can\'t sing yet, but wait for improvements made in the near future!<br>';
echo "Check out<a href=\"../../demo/tts.html\"> this demo</a> if you'd like. <br>";
}
//Get Weather Function
//Needs better RegExp
if (preg_match("/weather for/i", $query_input)){
if (str_word_count($query_input) >= 3){
preg_match('/[^ ]*$/', $query_input, $last_word);
$weather_city = $last_word[0];
echo "Today's weather for $weather_city<br>";
get_weather($weather_city);
}
}
}
//Get Weather Function
function get_weather($weather_input) {
$url = "http://api.openweathermap.org/data/2.5/weather?q=".$weather_input."&units=metric&APPID=0d8d4c272b4c41de6eee373b7bfe354b";
$weather = json_decode(file_get_contents($url), true);
$weather_icon = '<img src="http://openweathermap.org/img/w/%s.png" style="width:200px">';
echo sprintf($weather_icon, $weather['weather'][0]['icon']);
echo "<br>";
print($weather['weather'][0]['main']." "."Temperature ".$weather['main']['temp']."°C Humidity: ". $weather['main']['humidity']."% ".$weather['wind']['speed']. " m/s");
echo "<br>";
}
?>