forked from EvilInsultGenerator/website
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_insult.php
77 lines (73 loc) · 2.41 KB
/
generate_insult.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
<?php
include 'db.conf.php';
//function defination to convert array to xml
function array_to_xml($array, &$xml_user_info)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
if (!is_numeric($key)) {
$subnode = $xml_user_info->addChild("$key");
array_to_xml($value, $subnode);
} else {
$subnode = $xml_user_info->addChild("item$key");
array_to_xml($value, $subnode);
}
} else {
$xml_user_info->addChild("$key", htmlspecialchars("$value"));
}
}
}
/*ini_set("display_errors", "1");
error_reporting(E_ALL);*/
if (empty($_GET['lang'])) {
// empty $_GET['lang'] defaults to english
$lang = "en";
} else {
$lang = $_GET['lang'];
}
if (empty($_GET['type'])) {
// empty $_GET['type'] defaults to text
$type = "text";
} else {
$type = $_GET['type'];
}
$cLang = htmlentities($lang);
$stmt = $pdo->prepare($sql_insult);
$stmt->bindParam(':lang', $cLang, PDO::PARAM_STR);
$stmt->bindParam(':active', $active_insult, PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if (empty($result)) {
echo "****** **** *** ********";
} else {
$filtered = array_map('htmlspecialchars', array_map('stripslashes', $result));
$number = $filtered['number'];
$upcnt = $pdo->prepare($update_counter);
$upcnt->bindParam(':number', $number, PDO::PARAM_INT);
$upcnt->execute();
switch ($type) {
case "json":
echo json_encode($filtered);
break;
case "plain":
echo $filtered['insult'];
break;
case "xml":
Header('Content-type: text/xml');
//creating object of SimpleXMLElement
$xml_insults = new SimpleXMLElement("<?xml version=\"1.0\"?><insult_info></insult_info>");
//function call to convert array to xml
array_to_xml($filtered, $xml_insults);
$dom = new DOMDocument("1.0");
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml_insults->asXML());
echo $dom->saveXML();
break;
// Default to plain/text
default:
echo $filtered['insult'];
break;
}
}
?>