Skip to content

Instantly share code, notes, and snippets.

@Kissaki
Created September 26, 2015 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kissaki/09a66b9f97bb594fbd0b to your computer and use it in GitHub Desktop.
Save Kissaki/09a66b9f97bb594fbd0b to your computer and use it in GitHub Desktop.
Dump / Export SMF 1 modification customBBCode BBCodes
<?php
error_reporting(E_ALL);
include 'Settings.php';
$br = '<br>';
function error($text)
{
echo $text;
exit();
}
function sqlerror($mysqli, $text)
{
error('Failed to ' . $text . ', ' . "Query failed: (" . $mysqli->errno . ") " . $mysqli->error);
}
function dumpobj($obj)
{
echo '<pre>';
echo htmlspecialchars(print_r($obj, true), ENT_HTML5 | ENT_SUBSTITUTE);
echo '</pre>';
}
class DB
{
private $db_prefix;
private $TABLE;
private $custombbcprefix = 'customBBCode_';
private $mysqli;
private $count;
private $settings;
private $bbcs;
function __construct($db_server, $db_user, $db_passwd, $db_name, $db_prefix)
{
$this->db_prefix = $db_prefix;
$this->TABLE = "{$this->db_prefix}settings";
$this->mysqli = new mysqli($db_server, $db_user, $db_passwd, $db_name);
if ($this->mysqli->connect_errno)
{
error("Failed to connect to MySQL: (" . $this->mysqli->connect_errno . ") " . $this->mysqli->connect_error);
}
$this->fillCount();
$this->fillBBCData();
$this->parseBBCObjects();
}
public function getBBCCount()
{
return $this->count;
}
public function getBBCObjects()
{
return $this->bbcs;
}
private function fillCount()
{
$varname = $this->custombbcprefix . 'count';
$res = $this->mysqli->query("SELECT * FROM $this->TABLE WHERE variable = '$varname'");
if (!$res)
{
sqlerror($this->mysqli, 'get count');
}
if ($res->num_rows == 0)
{
error('Could not find BBC count');
}
if ($res->num_rows > 1)
{
error('Multiple declarations of BBC count');
}
$this->count = $res->fetch_assoc()['value'];
}
private function getBBCs()
{
$res = $this->mysqli->query("SELECT * FROM $this->TABLE WHERE variable LIKE '{$this->custombbcprefix}%' ORDER BY variable");
if (!$res)
{
sqlerror($this->msqli, 'select BBC stuff');
}
$rowcount = $res->num_rows;
return $res;
}
private function fillBBCData()
{
$res = $this->getBBCs();
$data = array();
while ($row = $res->fetch_assoc())
{
$name = $row['variable'];
$value = $row['value'];
$this->settings[$name] = $value;
}
}
private function parseBBCObjects()
{
if (!empty($this->bbcs))
{
error('wat?');
}
for ($i = 1; $i <= $this->count; $i = $i + 1)
{
$obj = new BBC();
$obj->enabled = $this->getSetting('enable', $i);
$obj->before = $this->getSetting('before', $i);
$obj->after = $this->getSetting('after', $i);
$obj->button = $this->getSetting('button', $i);
$obj->description = $this->getSetting('description', $i);
$obj->parse = $this->getSetting('parse', $i);
$obj->tag = $this->getSetting('tag', $i);
$obj->text = $this->getSetting('text', $i);
$obj->trim = $this->getSetting('trim', $i);
$obj->type = $this->getSetting('type', $i);
$this->bbcs[] = $obj;
}
}
private function getSetting($name, $i)
{
$name = $this->custombbcprefix . $name . '_' . $i;
return isset($this->settings[$name]) ? $this->settings[$name] : null;
}
public function dumpSettings()
{
echo dumpobj($this->settings);
}
}
class BBC
{
public $enabled;
public $before;
public $after;
public $button;
public $description;
public $parse;
public $tag;
public $text;
public $trim;
public $type;
public function toHTML()
{
return ''
. '<div>'
. htmlspecialchars($this->tag)
. '<br>'
. '<pre>' . htmlspecialchars($this->description) . '</pre>'
. '<textarea>' . htmlspecialchars($this->text, ENT_HTML5 | ENT_SUBSTITUTE) . '</textarea>' . '<br>'
. 'enabled: ' . $this->enabled . '<br>'
. 'before: ' . $this->before . '<br>'
. 'after: ' . $this->after . '<br>'
. 'button: ' . $this->button . '<br>'
. 'parse: ' . $this->parse . '<br>'
. '</div>';
}
}
$db = new DB($db_server, $db_user, $db_passwd, $db_name, $db_prefix);
$bbccount = $db->getBBCCount();
$objs = $db->getBBCObjects();
echo '<!DOCTYPE html><html><head><meta charset="utf-8">';
echo '<style>pre { background-color:hsla(0, 0%, 94%, 1); padding:4px; } textarea { display:block; width:100%; height:80px;}</style>';
echo '</head><body>';
echo "<h1>custom BBC</h1>";
echo "BBC count: $bbccount" . $br;
echo '<hr />';
$objs = $db->getBBCObjects();
foreach($objs AS $obj)
{
echo $obj->toHTML();
echo '<hr>';
}
echo '</body></html>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment