Hello World Beispiel

Das klassische Hello World Beispiel für WBCE. Ein einfaches Seitenmodul das einen Text speichert und im Frontend anzeigt. Perfekt zum Einstieg.


Ziel

Backend:  Redakteur gibt einen Text ein
Frontend: Text wird auf der Seite angezeigt

info.php

<?php
/**
 * Hello World — info.php
 * @package hello_world @version 1.0.0
 */
defined('WB_PATH') or die("This file cannot be accessed directly!");

$module_directory   = 'hello_world';
$module_name        = 'Hello World';
$module_function    = 'page';
$module_version     = '1.0.0';
$module_platform    = '1.6';
$module_author      = 'Dein Name';
$module_license     = 'GNU General Public License';
$module_description = 'Einfaches Beispielmodul — Text eingeben und anzeigen.';

install.php

<?php
defined('WB_PATH') or die("This file cannot be accessed directly!");

$database->query("CREATE TABLE IF NOT EXISTS `" . TABLE_PREFIX . "mod_hello_world` (
    `section_id` INT(11) UNSIGNED NOT NULL DEFAULT 0,
    `text`       TEXT NOT NULL,
    PRIMARY KEY (`section_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");

uninstall.php

<?php
defined('WB_PATH') or die("This file cannot be accessed directly!");

$database->query("DROP TABLE IF EXISTS `" . TABLE_PREFIX . "mod_hello_world`");

add.php

<?php
defined('WB_PATH') or die("This file cannot be accessed directly!");

$database->query("INSERT IGNORE INTO `" . TABLE_PREFIX . "mod_hello_world`
    (`section_id`, `text`) VALUES (" . (int)$section_id . ", 'Hello World!')");

modify.php — Backend-Formular

<?php
defined('WB_PATH') or die("This file cannot be accessed directly!");

$sid = (int)$section_id;
$res = $database->query("SELECT * FROM `" . TABLE_PREFIX . "mod_hello_world`
    WHERE `section_id` = $sid LIMIT 1");
$row = $res ? $res->fetchRow() : [];
$row = array_merge(['text' => ''], (array)$row);
?>
<form action="<?php echo WB_URL; ?>/modules/hello_world/save.php" method="post">
    <?php echo $admin->getFTAN(); ?>
    <input type="hidden" name="section_id" value="<?php echo $sid; ?>">
    <input type="hidden" name="page_id"    value="<?php echo (int)$page_id; ?>">
    <label>Text:</label>
    <textarea name="text" rows="5" style="width:100%"><?php
        echo htmlspecialchars($row['text'], ENT_QUOTES, 'UTF-8');
    ?></textarea>
    <button type="submit">Speichern</button>
</form>

save.php

<?php
require('../../config.php');
defined('WB_PATH') or die("This file cannot be accessed directly!");

require_once(WB_PATH . '/framework/class.admin.php');
$admin = new admin('Pages', 'modify', false, false);

if (!$admin->checkFTAN()) {
    $admin->print_error('Ungültige Anfrage.');
    exit;
}

$section_id = (int)($_POST['section_id'] ?? 0);
$page_id    = (int)($_POST['page_id']    ?? 0);
$text       = $database->escapeString(strip_tags($_POST['text'] ?? ''));

$database->query("UPDATE `" . TABLE_PREFIX . "mod_hello_world`
    SET `text` = '$text'
    WHERE `section_id` = $section_id");

header('Location: ' . ADMIN_URL . '/pages/modify.php?page_id=' . $page_id);
exit;

view.php — Frontend-Ausgabe

<?php
defined('WB_PATH') or die("This file cannot be accessed directly!");

$sid = (int)$section_id;
$res = $database->query("SELECT `text` FROM `" . TABLE_PREFIX . "mod_hello_world`
    WHERE `section_id` = $sid LIMIT 1");
$row = $res ? $res->fetchRow() : [];
$text = $row['text'] ?? '';
?>
<div class="hello-world">
    <?php echo nl2br(htmlspecialchars($text, ENT_QUOTES, 'UTF-8')); ?>
</div>

index.php (Directory-Schutz)

<?php header('Location: ../../'); exit;

Ergebnis

Backend:  Textfeld mit "Hello World!" vorausgefüllt
Frontend: Text wird sauber ausgegeben
Sicher:   FTAN-Schutz + escapeString + htmlspecialchars

Nächster Schritt: Platzhalter und Hooks — WBCE von innen erweitern