summaryrefslogtreecommitdiff
path: root/lib/type-todo.php
blob: f81354395424cafb14b827db02f2e40242106422 (plain)
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

	public function type($get){
		
		$search = $get["s"];
		$bang = $get["bang"];
		
		if(empty($search)){
			
			if(!empty($bang)){
				
				// !youtube
				$conn = pg_connect("host=localhost dbname=4get user=postgres password=postgres");
				
				pg_prepare($conn, "bang_get", "SELECT bang,name FROM bangs WHERE bang LIKE $1 ORDER BY bang ASC LIMIT 8");
				$q = pg_execute($conn, "bang_get", ["$bang%"]);
				
				$results = [];
				while($row = pg_fetch_array($q, null, PGSQL_ASSOC)){
					
					$results[] = [
						"s" => "!" . $row["bang"],
						"n" => $row["name"]
					];
				}
				
				return $results;
			}else{
				
				// everything is empty
				// lets just return a bang list
				return [
					[
						"s" => "!w",
						"n" => "Wikipedia",
						"u" => "https://en.wikipedia.org/wiki/Special:Search?search={%q%}"
					],
					[
						"s" => "!4ch",
						"n" => "4chan Board",
						"u" => "https://find.4chan.org/?q={%q%}"
					],
					[
						"s" => "!a",
						"n" => "Amazon",
						"u" => "https://www.amazon.com/s?k={%q%}"
					],
					[
						"s" => "!e",
						"n" => "eBay",
						"u" => "https://www.ebay.com/sch/items/?_nkw={%q%}"
					],
					[
						"s" => "!so",
						"n" => "Stack Overflow",
						"u" => "http://stackoverflow.com/search?q={%q%}"
					],
					[
						"s" => "!gh",
						"n" => "GitHub",
						"u" => "https://github.com/search?utf8=%E2%9C%93&q={%q%}"
					],
					[
						"s" => "!tw",
						"n" => "Twitter",
						"u" => "https://twitter.com/search?q={%q%}"
					],
					[
						"s" => "!r",
						"n" => "Reddit",
						"u" => "https://www.reddit.com/search?q={%q%}"
					],
				];
			}
		}
		
		// now we know search isnt empty
		if(!empty($bang)){
			
			// check if the bang exists
			$conn = pg_connect("host=localhost dbname=4get user=postgres password=postgres");
			
			pg_prepare($conn, "bang_get_single", "SELECT bang,name FROM bangs WHERE bang = $1 LIMIT 1");
			$q = pg_execute($conn, "bang_get_single", [$bang]);
			
			$row = pg_fetch_array($q, null, PGSQL_ASSOC);
			
			if(isset($row["bang"])){
				
				$bang = "!$bang ";
			}else{
				
				$bang = "";
			}
		}
		
		try{
			$res = $this->get(
				"https://duckduckgo.com/ac/",
				[
					"q" => strtolower($search)
				],
				ddg::req_xhr
			);
			
			$res = json_decode($res, true);
			
		}catch(Exception $e){
			
			throw new Exception("Failed to get /ac/");
		}
		
		$arr = [];
		for($i=0; $i<count($res); $i++){
			
			if($i === 8){break;}
			
			if(empty($bang)){
				
				$arr[] = [
					"s" => $res[$i]["phrase"]
				];
			}else{
				
				$arr[] = [
					"s" => $bang . $res[$i]["phrase"],
					"n" => $row["name"]
				];
			}
		}
		
		return $arr;
	}