-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugman.php
119 lines (95 loc) · 3.88 KB
/
plugman.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
<?php
/**
* Plugin Name: Flashy
* Description: A plugin to integrate Flash games to your CloudArcade websites.
* Author: PlugMan <mail@plugman.dev>
*/
if(!defined('USER_ADMIN')){
exit;
}
define("plugman_base", "https://api.plugman.dev");
define("plugman_version", "v1");
define("plugman_api", plugman_base . "/flashy/" . plugman_version);
define("plugman_site_url", "//" . (isset($_SERVER["SERVER_NAME"]) ? $_SERVER["SERVER_NAME"] : php_uname("n")) . (in_array($_SERVER["SERVER_PORT"], [80, 443]) ? false : ":{$_SERVER["SERVER_PORT"]}") . (empty(explode("/", dirname($_SERVER["SCRIPT_NAME"]))[1]) ? false : dirname($_SERVER["SCRIPT_NAME"])));
function plugman_site_url($relativeUrl = "") {
$baseUrl = plugman_site_url;
// Parse the base URL
$parsedBaseUrl = parse_url($baseUrl);
// Extract the constant part of the path
$constantPath = isset($parsedBaseUrl['path']) ? trim($parsedBaseUrl['path'], '/') : '';
// Remove any paths between the constant part and the relative URL
$relativePath = preg_replace('#^/?' . $constantPath . '/?#', '', ltrim($relativeUrl, "/"));
// Rebuild the URL with the constant part and the modified relative path
$mergedUrl = "//{$parsedBaseUrl['host']}/{$relativePath}";
return $mergedUrl;
}
/**
* Function to make a GET request to a specified URL with array parameters using cURL.
*
* @param string $url The URL to which the request will be made.
* @param array $params An associative array of parameters to be included in the request.
* @return string|bool The response from the server, or FALSE on failure.
*/
function plugmanApi($path, $params = array()) {
$url = plugman_api . $path;
$queryString = http_build_query($params);
if (!empty($queryString)) {
$url .= '?' . $queryString;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($curl);
if(curl_errno($curl)) {
$error_msg = curl_error($curl);
return false;
}
curl_close($curl);
return json_decode($response, true);
}
/**
* Function to update a setting in the database.
* @param string $name The name of the setting.
* @param string $value The value of the setting.
* @param bool $ignore Whether to ignore the setting if it does not exist.
*/
function plugman_update_setting($name, $value, $ignore = false) {
$conn = open_connection();
// Check if the setting exists, if not, create it
if (!setting_exists($conn, $name)) {
create_setting($conn, $name, $value);
}
if($ignore) {
return;
}
// Update the setting
$sql = "UPDATE settings SET value = ? WHERE name = ?";
$st = $conn->prepare($sql);
$st->execute([$value, $name]);
}
/**
* Function to check if a setting exists in the database.
* @param PDO $conn The database connection.
* @param string $name The name of the setting.
* @return bool Whether the setting exists.
*/
function setting_exists($conn, $name) {
// Check if the setting name exists in the settings table
$sql = "SELECT COUNT(*) FROM settings WHERE name = ? LIMIT 1";
$st = $conn->prepare($sql);
$st->execute([$name]);
return $st->fetchColumn() > 0;
}
/**
* Function to create a setting in the database.
* @param PDO $conn The database connection.
* @param string $name The name of the setting.
* @param string $value The value of the setting.
*/
function create_setting($conn, $name, $value) {
// Create the setting in the settings table
$sql = "INSERT INTO settings (name, value, type, category, label, tooltip, description) VALUES (?, ?, ?, ?, ?, ?, ?)";
$st = $conn->prepare($sql);
$st->execute([$name, $value, "text", "other", $name, "", ""]);
}