Skip to content

Commit dabeb96

Browse files
author
flx5
committed
Modified API to make it possible to use it with PHP's include function.
1 parent 3aa19c2 commit dabeb96

File tree

6 files changed

+612
-549
lines changed

6 files changed

+612
-549
lines changed

api.php

Lines changed: 7 additions & 192 deletions
Original file line numberDiff line numberDiff line change
@@ -21,201 +21,16 @@
2121
|* along with InMaFSS; if not, see http://www.gnu.org/licenses/. *|
2222
\*================================================================================= */
2323

24-
require_once("global.php");
24+
require_once(dirname(__FILE__)."/global.php");
2525

26-
new API();
26+
require_once(INC."api.php");
2727

28-
class API {
29-
30-
private $data = Array();
31-
32-
public function __construct() {
33-
header('content-type: application/json; charset=utf-8');
34-
35-
if (!isset($_GET['key']) || !$this->CheckAuth($_GET['key'])) {
36-
$this->Error("You have not been authenticated!");
37-
}
38-
39-
if (!isset($_GET['action'])) {
40-
$this->Error("No action specified");
41-
}
42-
43-
if (!$this->HasPerm($_GET['action'])) {
44-
$this->Error("This action does not exist or you don't have sufficient rights for this action");
45-
}
46-
47-
switch ($_GET['action']) {
48-
case 'plan_update':
49-
if (!isset($_POST['data'])) {
50-
$this->Error("No file content found!");
51-
}
52-
53-
$_POST['data'] = urldecode($_POST['data']);
54-
55-
$files = explode(chr(1), $_POST['data']);
56-
$p = new parse();
57-
58-
foreach ($files as $file) {
59-
# $file = stripslashes($file);
60-
$file = utf8_decode($file);
61-
$file = substr($file, strpos($file, "<html>"));
62-
$p->parseHTML($file);
63-
}
64-
65-
$p->UpdateDatabase();
66-
$this->Output(Array('STATUS' => "OK", 'message' => 'Import completed'));
67-
break;
68-
69-
case 'replacements':
70-
71-
if (!$this->HasPerm('replacements_all') && !isset($_GET['g'])) {
72-
$this->Error("You must provide a Grade!");
73-
}
74-
75-
$view = $this->GetView();
76-
$view->AddRepacements();
77-
78-
$output = Array();
79-
80-
foreach ($view->replacements as $page) {
81-
foreach ($page as $grade => $val) {
82-
if (!isset($_GET['g']) || $grade == $_GET['g']) {
83-
foreach ($val as $k => $v) {
84-
$val[$k]['comment'] = preg_replace("/&nbsp;/", "", htmlentities($v['comment']));
85-
$val[$k]['replacement'] = preg_replace("/&nbsp;/", "", htmlentities($v['replacement']));
86-
}
87-
$output[$grade] = $val;
88-
}
89-
}
90-
}
91-
92-
$this->Output($output);
93-
break;
94-
95-
case 'other':
96-
97-
if (!isset($_GET['type'])) {
98-
$this->Error("Specify a type");
99-
}
100-
101-
$view = $this->GetView();
102-
$view->type = 1;
103-
$view->AddRepacements();
104-
105-
$output = Array();
106-
107-
108-
foreach ($view->replacements[1] as $k => $val) {
109-
if ($k == $_GET['type']) {
110-
$output[$k] = $val;
111-
}
112-
}
113-
114-
115-
$this->Output($output);
116-
117-
break;
118-
119-
case 'teacher_sub':
120-
$view = $this->GetView();
121-
$view->type = 1;
122-
$view->AddRepacements();
123-
124-
$output = Array();
125-
126-
foreach ($view->replacements as $page) { // Can only be one page!
127-
foreach ($page as $k => $val) {
128-
switch ($k) {
129-
case 't':
130-
case 'n':
131-
case 'g':
132-
case 's':
133-
case 'a':
134-
case 'r':
135-
continue;
136-
break;
137-
138-
default:
139-
if (!isset($_GET['short']) || $k == $_GET['short']) {
140-
foreach($val as $i=>$entry) {
141-
foreach($entry as $f=>$x)
142-
$val[$i][$f] = html_entity_decode($x, ENT_COMPAT, "UTF-8");
143-
}
144-
145-
$output[$k] = $val;
146-
}
147-
break;
148-
}
149-
}
150-
}
151-
152-
$this->Output($output);
153-
break;
154-
155-
case 'ticker':
156-
$view = $this->GetView();
157-
$this->Output($view->GetTickers());
158-
159-
break;
160-
161-
default:
162-
$this->Error("Unknown action!");
163-
break;
164-
}
165-
}
166-
167-
function Error($msg) {
168-
$this->Output(Array('STATUS' => "ERROR", 'message' => $msg));
169-
exit;
170-
}
171-
172-
function Output($output) {
173-
$output = getVar('core')->FormatJson(json_encode($output));
174-
echo $output;
175-
}
176-
177-
function CheckAuth($api) {
178-
179-
if (isset($_GET['licence'])) {
180-
if (strpos(file_get_contents("http://licence.flx5.com/inmafss.php?ver=" . getVersion() . "&licence=" . $_GET['licence']), "OK") !== false) {
181-
$this->data = Array("all");
182-
}
183-
}
184-
185-
$api = filter($api);
186-
$sql = dbquery("SELECT permissions FROM api WHERE apikey = '" . $api . "'");
187-
188-
if (mysql_num_rows($sql) != 1) {
189-
return false;
190-
}
191-
192-
$data = mysql_result($sql, 0);
193-
$data = explode("|", $data);
194-
$this->data = $data;
195-
return true;
196-
}
197-
198-
function HasPerm($perm) {
199-
return true; // todo!
200-
return in_array($perm, $this->data);
201-
}
202-
203-
function GetView() {
204-
if (!isset($_GET['day']) || !is_numeric($_GET['day']) || strlen($_GET['day']) != 10) {
205-
$this->Error("Day must be Unix Timestamp");
206-
}
207-
208-
require_once("inc/view.php");
209-
210-
$day = $_GET['day'];
211-
212-
$tfrom = gmmktime(0, 0, 0, date('m', $day), date('d', $day), date('Y', $day));
213-
214-
$view = new View(null, 99e99);
215-
$view->tfrom = $tfrom;
216-
return $view;
217-
}
28+
$key = null;
21829

30+
if (isset($_GET['key'])) {
31+
$key = $_GET['key'];
21932
}
22033

34+
$api = new API($key);
35+
$api->Exec();
22136
?>

global.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@
2222
\*================================================================================= */
2323

2424
define('DS', DIRECTORY_SEPARATOR);
25-
define('CWD', str_replace('manage' . DS, '', dirname(__FILE__) . DS));
25+
define('CWD', dirname(__FILE__) . DS);
26+
define('INC', CWD . "inc" . DS);
2627
define('PLUGIN_DIR', CWD . DS . "plugins" . DS);
2728

2829
$www = "/" . substr(dirname(__FILE__), strlen($_SERVER['DOCUMENT_ROOT']) + 1);
2930
define('WWW', $www);
3031

31-
register_shutdown_function('Shutdown');
32+
#register_shutdown_function('Shutdown');
3233
set_error_handler('error_handler');
3334
date_default_timezone_set('Europe/Berlin');
3435

@@ -60,6 +61,8 @@
6061
$config = new config();
6162
$vars = new variables(new core(), new lang($config->Get("lang")), new MySQL(), new tpl(), new Update(), new pluginManager(), false);
6263

64+
vars::Init($vars);
65+
6366
getVar("sql")->connect($config->Get("dbhost"), $config->Get("dbusr"), $config->Get("dbpass"), $config->Get("dbname"));
6467
getVar("pluginManager")->Init();
6568
getVar("update")->Init();
@@ -100,14 +103,28 @@ function config($var) {
100103
return $config->Get($var);
101104
}
102105

106+
class vars {
107+
private static $vars;
108+
109+
public static function Init($vars) {
110+
self::$vars = $vars;
111+
}
112+
113+
public static function getVar($var) {
114+
return self::$vars->Get($var);
115+
}
116+
117+
public static function setVar($var, $val) {
118+
self::$vars->Set($var, $val);
119+
}
120+
}
121+
103122
function getVar($var) {
104-
global $vars;
105-
return $vars->Get($var);
123+
return vars::getVar($var);
106124
}
107125

108126
function setVar($var, $val) {
109-
global $vars;
110-
$vars->Set($var, $val);
127+
vars::setVar($var, $val);
111128
}
112129

113130
function setPlugin($val, $actor) {
@@ -121,17 +138,17 @@ function getVersion() {
121138
}
122139

123140
function error_handler($errno, $errstr, $errfile, $errline) {
124-
if(error_reporting() == 0)
141+
if (error_reporting() == 0)
125142
return true; // Ignore Messages with an @ before!
126-
143+
127144
return false;
128145
}
129146

130147
function Shutdown() {
131148
$error = error_get_last();
132149
if ($error != null) {
133150
ob_end_clean();
134-
core::SystemError($error['message'], ' in '. $error['file']. ' on line '.$error['line']);
151+
core::SystemError($error['message'], ' in ' . $error['file'] . ' on line ' . $error['line']);
135152
}
136153
}
137154

0 commit comments

Comments
 (0)