Skip to content

Commit d84cd79

Browse files
zorg Code v4.11.0
Merge pull request #77 from zorgch/develop
2 parents d9fb5e9 + 9b3342a commit d84cd79

File tree

8 files changed

+282
-231
lines changed

8 files changed

+282
-231
lines changed

www/actions/anficker.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,29 @@
66
* @package zorg\Games\Anficker
77
*/
88
// Includes --------------------------------------------------------------------
9-
require_once dirname(__FILE__).'/../includes/main.inc.php';
9+
require_once __DIR__.'/../includes/config.inc.php';
1010
require_once INCLUDES_DIR.'anficker.inc.php';
1111

12+
/** Input validation */
13+
$doAction = filter_input(INPUT_POST, 'do', FILTER_DEFAULT, FILTER_REQUIRE_SCALAR) ?? null; // $_POST['do']
14+
$trainSpresim = filter_input(INPUT_POST, 'spresim-trainieren', FILTER_VALIDATE_BOOLEAN) ?? false; // $_POST['spresim-trainieren']
15+
$anfickId = filter_input(INPUT_POST, 'anfick_id', FILTER_VALIDATE_INT) ?? 0; // $_POST['anfick_id']
16+
$anfickScore = filter_input(INPUT_POST, 'note', FILTER_VALIDATE_INT) ?? 0; // $_POST['note']
17+
$anfick = htmlentities(filter_input(INPUT_POST, 'text', FILTER_SANITIZE_FULL_SPECIAL_CHARS)) ?? null; // $_POST['text']
18+
$returnUrl = '/tpl/175';
1219

1320
// Anficken -------------------------------------------------------------------
14-
if(isset($_POST['do']) && $_POST['do'] == 'anficken')
15-
{
21+
if($user->is_loggedin() && $doAction === 'anficken')
22+
{
1623
/**
1724
* Benoten NUR wenn spresim-trainieren gewählt wird
1825
* und eine Note vorhanden ist
1926
*/
20-
if (isset($_POST['spresim-trainieren']) && (isset($_POST['note']) && is_numeric($_POST['note']) && $_POST['note'] > 0))
21-
{
22-
Anficker::vote($_POST['anfick_id'], $_POST['note']);
23-
}
27+
if ($trainSpresim && $anfickId > 0 && $anfickScore > 0) Anficker::vote($anfickId, $anfickScore);
2428

25-
$textEscaped = htmlentities(addslashes($_POST['text']));
29+
if (!empty($anfick)) Anficker::addAnfick(max(0, $user->id), $anfick, $trainSpresim);
2630

27-
Anficker::addAnfick(max(0, $user->id), $textEscaped, $_POST['spresim-trainieren']);
28-
29-
header("Location: /tpl/175?del=no&spresimtrainieren=".$_POST['spresim-trainieren']."#anficker");
30-
exit;
31+
$returnUrl .= '?del=no&spresimtrainieren='.$trainSpresim.'#anficker';
3132
}
33+
header('Location: '.$returnUrl);
34+
exit;

www/actions/error_action.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
$tplId = filter_input(INPUT_GET, 'tpl', FILTER_VALIDATE_INT) ?? null; // $_GET['tpl']
1111
$doDelete = filter_input(INPUT_POST, 'del', FILTER_DEFAULT, FILTER_REQUIRE_SCALAR) ?? null; // $_POST['del']
1212
$showQuery = filter_input(INPUT_POST, 'query', FILTER_SANITIZE_FULL_SPECIAL_CHARS) ?? 0; // $_POST['query']
13-
$del_ids = filter_input(INPUT_POST, 'to_del', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY) ?? []; // $_POST['to_del']
13+
$del_ids = (isset($_POST['to_del']) ? call_user_func_array('array_merge', array($_POST['to_del'])) : null); // $_POST['to_del']
1414
$showNum = filter_input(INPUT_POST, 'num', FILTER_VALIDATE_INT) ?? 0; // $_POST['num']
1515
$urlParams = '';
1616

@@ -28,7 +28,7 @@
2828
}
2929

3030
/** Delete multiple SQL-Errors */
31-
if(count($del_ids) > 0 && $user->type >= USER_MEMBER)
31+
if(count($del_ids) > 0 && $user->typ >= USER_MEMBER)
3232
{
3333
$placeholders = implode(',', array_fill(0, count($del_ids), '?'));
3434
$sql = 'DELETE FROM sql_error WHERE id IN (' . $placeholders . ')';
@@ -39,7 +39,7 @@
3939
/** Change displayed number of SQL-Error */
4040
if($showNum > 0)
4141
{
42-
$_SESSION['error_num'] = $_POST['num'];
42+
$_SESSION['error_num'] = intval($_POST['num']);
4343
$urlParams = '?error_num='.$showNum;
4444
}
4545

www/actions/tpleditor.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,9 @@
260260
zorgDebugger::log()->debug('Notify Template-Owner: owner %d <-- edit by %d', [$notifyOtherTplOwner, $user->id]);
261261
if ($notifyOtherTplOwner !== $user->id)
262262
{
263-
$notification_text = t('change-notification-owner', 'tpl', [ $user->id2user($user->id), $frm['id'], $frm['title'] ]);
264-
$notification_status = $notification->send($notifyOtherTplOwner, 'messagesystem', ['from_user_id'=>$user->id, 'subject'=>t('change-notification-owner-subject', 'tpl'), 'text'=>$notification_text, 'message'=>$notification_text]);
263+
$username = $user->id2user($user->id);
264+
$notification_text = t('change-notification-owner', 'tpl', [ $username, $frm['id'], $frm['title'] ]);
265+
$notification_status = $notification->send($notifyOtherTplOwner, 'messagesystem', ['from_user_id'=>$user->id, 'subject'=>t('change-notification-owner-subject', 'tpl', [$username]), 'text'=>$notification_text, 'message'=>$notification_text]);
265266
zorgDebugger::log()->debug('$_TPLROOT[owner] Notification: %s', [$notification_status ? 'true' : 'false']);
266267
}
267268

www/includes/anficker.inc.php

Lines changed: 119 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -38,100 +38,146 @@ class Anficker
3838
/**
3939
* Anfick des User hinzufügen
4040
*
41-
* @author ?
42-
* @author IneX
43-
* @version 2.1
41+
* @version 2.5
4442
* @since 1.0 function added
4543
* @since 2.0 `IneX` code enhancements
4644
* @since 2.1 `16.04.2020` `IneX` migrated mysql-functions to mysqli
45+
* @since 2.5 `21.01.2024` `IneX` Bug #667 : Anficks werden x-mal gespeichert
4746
*
48-
* @see Anficker::logAnfick(), Anficker::getId()
47+
* @see self::logAnfick(), self::getId()
4948
* @param integer $user_id ID des Users, welcher gerade mit Spresim batteld
5049
* @param string $text Anfick des Users
5150
* @param boolean $spresim_trainieren Gibt an, ob Anfick des Users gespeichert werden soll oder nicht
5251
* @global object $db Globales Class-Object mit allen MySQL-Methoden
53-
* @global object $user Globales Class-Object mit den User-Methoden & Variablen
52+
* @return bool
5453
*
55-
* @todo Unterschied, ob Spresim trainieren oder nur battlen sollte möglich sein (Bug #487) (Mättä, 25.10.04) | IDEE: Eine möglich Lösung wäre, ein zusätzliches Flag in der Tabelle "battle_only" oder so...
56-
* @todo Müsste es nicht "REPLACE INTO..." sein?? Jetzt werden x-Einträge mit gleichem Text gemacht! (IneX, 8.6.09)
54+
* // TODO Bug #487 : Unterschied, ob Spresim trainieren oder nur battlen sollte möglich sein (Mättä, 25.10.04) | IDEE: Eine möglich Lösung wäre, ein zusätzliches Flag in der Tabelle "battle_only" oder so...
5755
*/
58-
static function addAnfick($user_id, $text, $spresim_trainieren=FALSE) {
59-
global $db, $user;
60-
61-
// nur Anfick speichern, wenn Spresim trainiert werden soll:
62-
//if ($spresim_trainieren == TRUE)
63-
//{
64-
if($text != '' && !empty($user_id))//$user->id2user($user_id)))
65-
{
66-
$sql = 'INSERT IGNORE INTO anficker_anficks ( text, user_id, datum) VALUES (?, ?, ?)';
67-
$insert_id = $db->query($sql, __FILE__, __LINE__, __METHOD__, [$text, $user_id, timestamp(true)]);
68-
}
69-
//else
70-
//{
71-
72-
73-
// Hier sollte was kommen, WENN SPRESIM NICHT TRAINIERT WERDEN SOLL... leider zur Zeit nicht realisierbar, IneX 8.6.09
74-
// IDEE: Eine möglich Lösung wäre, ein zusätzliches Flag in der Tabelle "battle_only" oder so... (IneX, 8.6.09)
75-
56+
static function addAnfick($user_id, $text, $spresim_trainieren=FALSE)
57+
{
58+
global $db;
7659

60+
if($user_id > 0 && !empty($text))
61+
{
62+
// nur Anfick speichern, wenn Spresim trainiert werden soll:
63+
//if ($spresim_trainieren == TRUE)
64+
//{
65+
/** Check if Anfick already exists */
66+
$existing_anfick_id = 0;
67+
$default_initial_score = 4.0;
68+
$sql_check = 'SELECT id, note, votes, user_id FROM anficker_anficks WHERE text=? LIMIT 1';
69+
$exists = $db->query($sql_check, __FILE__, __LINE__, __METHOD__, [$text]);
70+
if ($db->num($exists) > 0) {
71+
$af = $db->fetch($exists);
72+
$existing_anfick_id = intval($af['id']);
73+
$existing_anfick_score = floatval($af['note']);
74+
$existing_anfick_votes = intval($af['votes']);
75+
$existing_anfick_creator = intval($af['user_id']);
76+
}
77+
78+
/** Update existing Anfick */
79+
if ($existing_anfick_id > 0)
80+
{
81+
$new_votes = $existing_anfick_votes+1;
82+
$new_score = number_format(($default_initial_score+$existing_anfick_score)*$existing_anfick_votes/$new_votes, 8, '.', '');
83+
$update = [
84+
'note' => $new_score
85+
,'votes' => $new_votes
86+
,'user_id' => (empty($existing_anfick_creator) ? $user_id : $existing_anfick_creator)
87+
];
88+
$db->update('anficker_anficks', $existing_anfick_id, $update, __FILE__, __LINE__, __METHOD__);
89+
}
90+
/** Add new Anfick */
91+
else {
92+
$insert = [
93+
'text' => $text
94+
,'user_id' => $user_id
95+
,'datum' => timestamp(true)
96+
];
97+
$insert_id = $db->insert('anficker_anficks', $insert, __FILE__, __LINE__, __METHOD__);
98+
}
99+
100+
//else
101+
//{
102+
103+
// Hier sollte was kommen, WENN SPRESIM NICHT TRAINIERT WERDEN SOLL... leider zur Zeit nicht realisierbar, IneX 8.6.09
104+
// IDEE: Eine möglich Lösung wäre, ein zusätzliches Flag in der Tabelle "battle_only" oder so... (IneX, 8.6.09)
105+
106+
//}
77107
//}
78-
//}
79-
80-
// DEBUGGING
81-
//error_log('[DEBUG] ' . __FILE__ . ':' . __LINE__ . ' mysql_insert_id() = ' . mysql_insert_id());
82-
//error_log('[DEBUG] ' . __FILE__ . ':' . __LINE__ . ' Anficker::getId($text) = `' . $text . '`');
83-
84-
$anfick_id = ($insert_id > 0 ? $insert_id : Anficker::getId($text));
85-
86-
Anficker::logAnfick($anfick_id, $user_id, $user_id);
108+
$anfick_id = ($existing_anfick_id > 0 ? $existing_anfick_id : $insert_id);
109+
$log = self::logAnfick($anfick_id, $user_id, $user_id);
110+
return (!$log ? false : true);
111+
}
112+
return false;
87113
}
88114

89115

90116
/**
91117
* Anfick im Anfick-Log ergänzen
92118
*
93-
* @author ?
94-
* @author IneX
119+
* @see self::addAnfick()
120+
*
95121
* @version 2.0
96-
* @since 1.0
97-
* @see Anficker::addAnfick()
122+
* @since 1.0 Method added
123+
* @since 2.0 `IneX` SQL- and code optimziations
98124
*
99125
* @param integer $anfick_id ID des Anficks wo das Log ergänzt werden soll
100126
* @param integer $user_id ID des Users, welcher angefickt wurde
101127
* @param integer $anficker_id ID des Users, welcher den Anfick gemacht hat
102128
* @global object $db Globales Class-Object mit allen MySQL-Methoden
129+
* @return int|bool Returns INSERT id - or false
103130
*/
104131
static function logAnfick($anfick_id, $user_id, $anficker_id) {
105132
global $db;
106-
$sql = 'INSERT INTO anficker_log (datum, user_id, anficker_id, anfick_id) VALUES (?, ?, ?, ?)';
107-
//return $db->query($sql, __FILE__, __LINE__);
108-
$db->query($sql, __FILE__, __LINE__, __METHOD__, [timestamp(true), $user_id, $anficker_id, $anfick_id]);
133+
134+
$result = false;
135+
if ($anfick_id > 0 && $user_id > 0 && $anficker_id > 0) {
136+
$insert = [
137+
'datum' => timestamp(true)
138+
,'user_id' => intval($user_id)
139+
,'anficker_id' => intval($anficker_id)
140+
,'anfick_id' => intval($anfick_id)
141+
];
142+
$result = $db->insert('anficker_log', $insert, __FILE__, __LINE__, __METHOD__);
143+
}
144+
return $result;
109145
}
110146

111147

112148
static function deleteLog($user_id) {
113149
global $db;
114-
$sql = 'DELETE FROM anficker_log WHERE user_id=?';
115-
return $db->query($sql, __FILE__, __LINE__, __METHOD__, [$user_id]);
150+
151+
$result = false;
152+
if ($user_id > 0) {
153+
$sql = 'DELETE FROM anficker_log WHERE user_id=?';
154+
$result = $db->query($sql, __FILE__, __LINE__, __METHOD__, [$user_id]);
155+
}
156+
return $result;
116157
}
117158

118159

119160
static function getId($text) {
120161
global $db;
121-
$sql = 'SELECT id FROM anficker_anficks WHERE text=?';
122-
$rs = $db->fetch($db->query($sql, __FILE__, __LINE__, __METHOD__, [$text]));
123-
return $rs['id'];
162+
163+
$result = null;
164+
if (!empty($text)) {
165+
$sql = 'SELECT id FROM anficker_anficks WHERE text=?';
166+
$rs = $db->fetch($db->query($sql, __FILE__, __LINE__, __METHOD__, [$text]));
167+
$result = intval($rs['id']);
168+
}
169+
return $result;
124170
}
125171

126172

127173
/**
128174
* Anfick-Log ausgeben
129175
*
130-
* @author ?
131-
* @author IneX
176+
* @see self::logAnfick(), self::anfickenMit()
177+
*
132178
* @version 2.0
133-
* @since 1.0
134-
* @see Anficker::logAnfick(), Anficker::anfickenMit()
179+
* @since 1.0 method added
180+
* @since 2.0 `IneX` SQL- and code optimizations
135181
*
136182
* @param integer $user_id ID des Users, welcher gerade mit Spresim batteld
137183
* @global object $db Globales Class-Object mit allen MySQL-Methoden
@@ -140,8 +186,8 @@ static function getId($text) {
140186
static function getLog($user_id) {
141187
global $db;
142188

143-
//Anficker::addRandomAnfick2Log($user_id, ANFICKER_USER_ID);
144-
Anficker::logAnfick(Anficker::anfickenMit(), $user_id, ANFICKER_USER_ID);
189+
//self::addRandomAnfick2Log($user_id, ANFICKER_USER_ID);
190+
self::logAnfick(self::anfickenMit(), $user_id, ANFICKER_USER_ID);
145191

146192
$sql = 'SELECT anficker_anficks.note, anficker_anficks.id, anficker_log.datum, anficker_anficks.text, anficker_log.anficker_id FROM anficker_log
147193
LEFT JOIN anficker_anficks ON (anficker_anficks.id = anficker_log.anfick_id) WHERE anficker_log.user_id=? ORDER BY anficker_log.id ASC';
@@ -182,11 +228,11 @@ static function getNumAnficks($user_id=null)
182228
*
183229
* @deprecated
184230
*
185-
* @author ?
186-
* @author IneX
231+
* @see self::anfickenMit()
232+
*
187233
* @version 2.0
188-
* @since 1.0
189-
* @see Anficker::anfickenMit()
234+
* @since 1.0 method added
235+
* @since 2.0 `IneX` method deprecation mode activated
190236
*
191237
* @param integer $user_id ID des Users, welcher gerade mit Spresim batteld
192238
* @return array Gibt ganzes Log der Anfickerei für Ausgabe zurück
@@ -205,7 +251,7 @@ static function addRandomAnfick2Log($user_id) {
205251
$rs = $db->fetch($result);*/
206252

207253
//return Anficker::logAnfick($rs['id'], $user_id, ANFICKER_USER_ID);
208-
Anficker::logAnfick(Anficker::anfickenMit(), $user_id, ANFICKER_USER_ID);
254+
self::logAnfick(self::anfickenMit(), $user_id, ANFICKER_USER_ID);
209255
}
210256

211257

@@ -217,7 +263,7 @@ static function addRandomAnfick2Log($user_id) {
217263
* @since 1.0 `08.06.2009` `IneX` function added
218264
* @since 1.1 `16.04.2020` `IneX` code optimizations, migrated mysql-functions to mysqli
219265
*
220-
* @see Anficker::getNumAnficks()
266+
* @see self::getNumAnficks()
221267
* @global object $db Globales Class-Object mit allen MySQL-Methoden
222268
* @return integer ID des Anfick von Spresim
223269
*/
@@ -226,10 +272,10 @@ static function anfickenMit()
226272
global $db;
227273

228274
$sql = 'SELECT * FROM anficker_anficks ORDER BY note ASC';
229-
$result = $db->query($sql, __FILE__, __LINE__, __FUNCTION__);
275+
$result = $db->query($sql, __FILE__, __LINE__, __METHOD__);
230276

231277
/** zufällige id holen */
232-
$rs = Anficker::getNumAnficks();
278+
$rs = self::getNumAnficks();
233279
$id = rand(0, $rs['num']-1); // Zufalls #
234280
$id = rand($id, $rs['num']-1); // die besten bevorzugen.
235281
mysqli_data_seek($result, $id);
@@ -243,21 +289,25 @@ static function anfickenMit()
243289
/**
244290
* Anfick-Spruch benoten
245291
*
246-
* @author ?
247-
* @author IneX
248292
* @version 1.1
249-
* @since 1.0
293+
* @since 1.0 method added
294+
* @since 1.1 `IneX` SQL- and code optimizations
250295
*
251296
* @param integer $anfick_id ID des benoteten Anficks
252297
* @param integer $note Bewertung des Anficks
253298
* @global object $db Globales Class-Object mit allen MySQL-Methoden
299+
* @return integer
254300
*/
255-
static function vote($anfick_id, $note) {
301+
static function vote($anfick_id, $note)
302+
{
256303
global $db;
257-
$sql = 'UPDATE anficker_anficks SET note=((?+note*votes)/(votes+1)), votes=(votes+1) WHERE id=?'
258-
;
259-
//return $db->query($sql, __FILE__, __LINE__);
260-
$db->query($sql, __FILE__, __LINE__, __METHOD__, [$note, $anfick_id]);
261-
}
262304

305+
$result = 0;
306+
if ($anfick_id > 0 && $note > 0)
307+
{
308+
$sql = 'UPDATE anficker_anficks SET note=((?+note)*votes/(votes+1)), votes=(votes+1) WHERE id=?';
309+
$result = $db->query($sql, __FILE__, __LINE__, __METHOD__, [intval($note), intval($anfick_id)]);
310+
}
311+
return $result;
312+
}
263313
}

0 commit comments

Comments
 (0)