AmJano (14.10.2012), Gangstersheep (18.10.2012), Pwned (14.10.2012)
Thema: [Source] MyBB-API
-
14.10.2012, 22:06 #1
[Source] MyBB-API
Hallöchen, vor vier Tagen habe ich hier eine MyBB-API vorgestellt, allerdings hat die mir auch nicht gut gefallen, deswegen habe ich die Klasse heute nochmal überarbeitet, viel mehr Funktionen + Login-Check.
LG
PHP-Code:<?php
/*
* @ Name: MyBB API
* @ Version: 1.1
* @ Author: Nico Schmitz
* @ www: www.nico-schmitz.de
* @ date: 18.10.2012
*/
//MySQLi Connection
$db = @new mysqli('localhost','root','','forum');
class MyBBAPI
{
private $prefix; //Table-prefix
private $result;
private $sql;
private $password;
################
## mybb_users ##
################
private $uid; //User ID
private $username; //Username
private $password_db; //Password
private $salt; //Salt used to encrypt the password
private $loginkey; //Login key used to authenticate user's cookies
private $email; //Email address
private $postnum; //Post count
private $usergroup; //Primary usergroup ID
private $displaygroup; //Display group ID
private $usertitle; //Custom user title
private $regdate; //Registration date in UNIX timestamp
private $lastactive; //Last active date in UNIX timestamp
private $lastvisit; //Last visit date in UNIX timestamp
private $lastpost; //Last post date in UNIX timestamp
private $website; //URL to user's website
private $birthday; //Birthday
private $regip; //User's registration IP
private $lastip; //User's last IP used
private $timeonline; //User's time online
private $failedlogin; //Failed login
#####################
## mybb_usertitles ##
#####################
private $utid; //User title ID
private $posts; //Posts to gain this title
private $title; //The title shown
private $stars; //Number of stars
private $starimage; //Path to the image used when showing stars
################
## mybb_stats ##
################
private $dateline; //UNIX Timestamp of when statistics started
private $numusers; //Number of users
private $numthreads; //Number of threads
private $numposts; //Number of posts
#####################
## mybb_usergroups ##
#####################
private $gid; //Usergroup ID
private $type; //Type of group (core, custom, joinable)
private $title; //Usergroup name
private $description; //Description of usergroup
private $namestyle; //Style of the usernames for the users of this group
private $usertitle; //Default user title
private $stars; //Number of stars to display in the postbit for users in this group
private $starimage; //Path to the stars image
//construct the class
public function __construct($prefix)
{
$this->prefix = $prefix;
}
//Check Username and Password, function returns TRUE if login is okay
public function checkLogin($username,$password)
{
global $db;
$this->username = $username;
$this->password = $password;
$this->sql = "SELECT password,salt FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->password_db, $this->salt);
while($this->result->fetch())
{
if(md5(md5($this->salt).md5($this->password)) == $this->password_db)
{
return TRUE;
}
else
{
return FALSE;
}
}
}
#########################################
##### Table: mybb_users ##### START #####
#########################################
//returns UID of username
public function users_GetUID($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT uid FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->uid);
while($this->result->fetch())
{
return $this->uid;
}
}
//returns password of username
public function users_GetPassword($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT password FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->password_db);
while($this->result->fetch())
{
return $this->password_db;
}
}
//returns salt of username
public function users_GetSalt($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT salt FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->salt);
while($this->result->fetch())
{
return $this->salt;
}
}
//returns loginkey of username
public function users_GetLoginKey($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT loginkey FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->loginkey);
while($this->result->fetch())
{
return $this->loginkey;
}
}
//returns email of username
public function users_GetEmail($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT email FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->email);
while($this->result->fetch())
{
return $this->email;
}
}
####### muss noch #####
//returns postnum of username
public function users_GetPostnum($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT postnum FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->postnum);
while($this->result->fetch())
{
return $this->postnum;
}
}
//returns usergroup of username
public function users_GetUsergroup($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT usergroup FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->usergroup);
while($this->result->fetch())
{
return $this->usergroup;
}
}
//returns displaygroup of username
public function users_GetDisplaygroup($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT displaygroup FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->displaygroup);
while($this->result->fetch())
{
return $this->displaygroup;
}
}
//returns usertitle of username
public function users_GetUsertitle($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT usertitle FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->usertitle);
while($this->result->fetch())
{
return $this->usertitle;
}
}
//returns regdate of username
public function users_GetRegdate($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT regdate FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->regdate);
while($this->result->fetch())
{
return $this->regdate;
}
}
//returns lastactive of username
public function users_GetLastactive($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT lastactive FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->lastactive);
while($this->result->fetch())
{
return $this->lastactive;
}
}
//returns lastvisit of username
public function users_GetLastvisit($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT lastvisit FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->lastvisit);
while($this->result->fetch())
{
return $this->lastvisit;
}
}
//returns lastpost of username
public function users_GetLastpost($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT lastpost FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->lastpost);
while($this->result->fetch())
{
return $this->lastpost;
}
}
//returns website of username
public function users_GetWebsite($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT website FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->website);
while($this->result->fetch())
{
return $this->website;
}
}
//returns birthday of username
public function users_GetBirthday($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT birthday FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->birthday);
while($this->result->fetch())
{
return $this->birthday;
}
}
//returns regip of username
public function users_GetRegip($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT regip FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->regip);
while($this->result->fetch())
{
return $this->regip;
}
}
//returns lastip of username
public function users_GetLastip($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT lastip FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->lastip);
while($this->result->fetch())
{
return $this->lastip;
}
}
//returns timeonline of username
public function users_GetTimeonline($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT timeonline FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->timeonline);
while($this->result->fetch())
{
return $this->timeonline;
}
}
//returns failedlogin of username
public function users_GetFailedlogin($username)
{
global $db;
$this->username = $username;
$this->sql = "SELECT failedlogin FROM `$this->prefix"."users` WHERE `username` = '$this->username';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->failedlogin);
while($this->result->fetch())
{
return $this->failedlogin;
}
}
#######################################
##### Table: mybb_users ##### END #####
#######################################
##############################################
##### Table: mybb_usertitles ##### START #####
##############################################
//returns posts of usertitle
public function usertitles_GetPosts($utid)
{
global $db;
$this->utid = $utid;
$this->sql = "SELECT posts FROM `$this->prefix"."usertitles` WHERE `utid` = '$this->utid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->posts);
while($this->result->fetch())
{
return $this->posts;
}
}
//returns title of usertitle
public function usertitles_GetTitle($utid)
{
global $db;
$this->utid = $utid;
$this->sql = "SELECT title FROM `$this->prefix"."usertitles` WHERE `utid` = '$this->utid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->title);
while($this->result->fetch())
{
return $this->title;
}
}
//returns stars of usertitle
public function usertitles_GetStars($utid)
{
global $db;
$this->utid = $utid;
$this->sql = "SELECT stars FROM `$this->prefix"."usertitles` WHERE `utid` = '$this->utid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->stars);
while($this->result->fetch())
{
return $this->stars;
}
}
//returns starimage of usertitle
public function usertitles_GetStarimage($utid)
{
global $db;
$this->utid = $utid;
$this->sql = "SELECT starimage FROM `$this->prefix"."usertitles` WHERE `utid` = '$this->utid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->starimage);
while($this->result->fetch())
{
return $this->starimage;
}
}
############################################
##### Table: mybb_usertitles ##### END #####
############################################
#########################################
##### Table: mybb_stats ##### START #####
#########################################
//returns dateline of stats
public function stats_GetDateline()
{
global $db;
$this->sql = "SELECT dateline FROM `$this->prefix"."stats` LIMIT 1;";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->dateline);
while($this->result->fetch())
{
return $this->dateline;
}
}
//returns numusers of stats
public function stats_GetNumusers()
{
global $db;
$this->sql = "SELECT numusers FROM `$this->prefix"."stats` LIMIT 1;";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->numusers);
while($this->result->fetch())
{
return $this->numusers;
}
}
//returns numthreads of stats
public function stats_GetNumthreads()
{
global $db;
$this->sql = "SELECT numthreads FROM `$this->prefix"."stats` LIMIT 1;";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->numthreads);
while($this->result->fetch())
{
return $this->numthreads;
}
}
//returns numposts of stats
public function stats_GetNumposts()
{
global $db;
$this->sql = "SELECT numposts FROM `$this->prefix"."stats` LIMIT 1;";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->numposts);
while($this->result->fetch())
{
return $this->numposts;
}
}
#######################################
##### Table: mybb_stats ##### END #####
#######################################
##############################################
##### Table: mybb_usergroups ##### START #####
##############################################
//returns type of usergroups
public function stats_GetType($gid)
{
global $db;
$this->gid = $gid;
$this->sql = "SELECT type FROM `$this->prefix"."usergroups` WHERE gid = '$this->gid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->type);
while($this->result->fetch())
{
return $this->type;
}
}
//returns title of usergroups
public function stats_GetTitle($gid)
{
global $db;
$this->gid = $gid;
$this->sql = "SELECT title FROM `$this->prefix"."usergroups` WHERE gid = '$this->gid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->title);
while($this->result->fetch())
{
return $this->title;
}
}
//returns description of usergroups
public function stats_GetDescription($gid)
{
global $db;
$this->gid = $gid;
$this->sql = "SELECT description FROM `$this->prefix"."usergroups` WHERE gid = '$this->gid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->description);
while($this->result->fetch())
{
return $this->description;
}
}
//returns namestyle of usergroups
public function stats_GetNamestyle($gid)
{
global $db;
$this->gid = $gid;
$this->sql = "SELECT namestyle FROM `$this->prefix"."usergroups` WHERE gid = '$this->gid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->namestyle);
while($this->result->fetch())
{
return $this->namestyle;
}
}
//returns usertitle of usergroups
public function stats_GetUsertitle($gid)
{
global $db;
$this->gid = $gid;
$this->sql = "SELECT usertitle FROM `$this->prefix"."usergroups` WHERE gid = '$this->gid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->usertitle);
while($this->result->fetch())
{
return $this->usertitle;
}
}
//returns stars of usergroups
public function stats_GetStars($gid)
{
global $db;
$this->gid = $gid;
$this->sql = "SELECT stars FROM `$this->prefix"."usergroups` WHERE gid = '$this->gid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->stars);
while($this->result->fetch())
{
return $this->stars;
}
}
//returns starimage of usergroups
public function stats_GetStarimage($gid)
{
global $db;
$this->gid = $gid;
$this->sql = "SELECT starimage FROM `$this->prefix"."usergroups` WHERE gid = '$this->gid';";
$this->result = $db->prepare($this->sql);
$this->result->execute();
$this->result->bind_result($this->starimage);
while($this->result->fetch())
{
return $this->starimage;
}
}
############################################
##### Table: mybb_usergroups ##### END #####
############################################
//destruct the class
public function __destruct()
{
}
}
?>Geändert von Snees (18.10.2012 um 17:30 Uhr)
-
The Following 3 Users Say Thank You to Snees For This Useful Post:
-
14.10.2012, 23:46 #2Gelöschter BenutzerGast
AW: [Source] MyBB-API
$res = mysql_query("SELECT email,postnum,usergroup,regdate,lastvisit,lastacti ve,lastpost,regip,timeonline,warningpoints FROM mybb_users WHERE username = '$this->username';") or die(mysql_error());
Ich würde raten, den Username dort zu escapen.
-
14.10.2012, 23:59 #3
AW: [Source] MyBB-API
PHP-Code:$this->output = "<username>$this->username</username>\n\r<email>$this->email</email>\n\r<postnum>$this->postnum</postnum>\n\r<usergroup>$this->usergroup</usergroup>\n\r<regdate>$this->regdate</regdate>\n\r<lastvisit>$this->lastvisit</lastvisit>\n\r<lastactive>$this->lastactive</lastactive>\n\r<lastpost>$this->lastpost</lastpost>\n\r<regip>$this->regip</regip>\n\r<timeonline>$this->timeonline</timeonline>\n\r<warningpoints>$this->warningpoints</warningpoints>";
return $this->output;
php - How to convert array to SimpleXML - Stack Overflow
Du könntest die Daten auch optional im JSON-Format (json_encode) ausgeben lassen.
-
-
15.10.2012, 00:01 #4
AW: [Source] MyBB-API
Ich würd zu php5 und sqli raten, statt immernoch auf die sqlscheiße von php4 zu setzen... dann ist auch kein escaping mehr notwendig...
-
18.10.2012, 17:31 #5
AW: [Source] MyBB-API
Habe die Klasse komplett überarbeitet, nun mit mehr Funktionen, MySQLi und Login-Check.
LG
-
18.10.2012, 21:34 #6Gelöschter BenutzerGast
AW: [Source] MyBB-API
Warum speicherst du alle Daten nicht einem Array? Ist doch total unnötig für jede Information eine Funktion zu machen... die hätte man wenigstens dynamisch machen können, da der Code in der Funktion ja eh immer derselbe ist ^-^
-
18.10.2012, 21:43 #7
- Registriert seit
- 18.11.2011
- Beiträge
- 226
Thanked 165 Times in 82 PostsAW: [Source] MyBB-API
Sieht mir danach aus als hättest du gezwungener maßen versucht objekt orientiert zu schreiben.
Wofür jeden kack in einer Globalen variable, wenn man es eh nicht benötigt und bei jedem Funktionsaufruf wieder überschreibt?
Dann noch die 100 abfragen, das könnte man alles in einer machen.
-
The Following User Says Thank You to 3lit For This Useful Post:
DMW007 (18.10.2012)
-
18.10.2012, 21:52 #8
- Registriert seit
- 15.11.2011
- Beiträge
- 6.209
- Blog Entries
- 5
Thanked 9.130 Times in 3.005 PostsAW: [Source] MyBB-API
Kann 3lit nur zustimmen..
Aus einer Abfrage machst du 30, Coderedundanzen ohne Ende, dann noch misshandlungen von Schleifen wo es eigentlich gar keine gibt wie sowas
PHP-Code:while($this->result->fetch())
{
return $this->stars;
}
PHP-Code:if(md5(md5($this->salt).md5($this->password)) == $this->password_db)
{
return TRUE;
}
else
{
return FALSE;
}
-
18.10.2012, 22:09 #9
AW: [Source] MyBB-API
Es tut mir leid, dass ich nicht schon seit 10 Jahren mit PHP und vor allem nicht OOP programmiere, es aber trotzdem versuche und dann noch so frei bin und das Script hier zu Verfügung stelle.
Klar kann man das Ganze einfacher und mit Sicherheit auch performanter machen, aber ich wollte jedes Feld einzeln abfragen und nicht einmal alles abfragen und in einen Array packen.
-
The Following User Says Thank You to Snees For This Useful Post:
orion-x (18.10.2012)
-
18.10.2012, 23:02 #10
- Registriert seit
- 15.11.2011
- Beiträge
- 6.209
- Blog Entries
- 5
Thanked 9.130 Times in 3.005 PostsAW: [Source] MyBB-API
Also wer sowas ernsthaft programmiert kann nicht von sich behaupten überhaupt die Definition von OOP 1x gelesen zu haben..
Zitat von Nico
Dir ist aber schon klar, dass wir hier nicht von irgendwelchen Eigenheiten oder Kleinigkeiten reden, sondern von Grundsätzen?
Mindestens 80% deines Codes sind unnötig und bei einer typischen API-Abfrage sendest du locker 10 Anfragen an den DB-Server, wo eine vollkommen ausreicht.
Du verschleuderst nicht nur Ressourcen ohne Ende, sondern machst deine Software auch extrem schwer bis gar nicht mehr wartbar (wirtschaftlich zumindestens).
Wenn du Code publizierst musst du damit rechnen kritisiert zu werden, erst Recht wenn du Murks baust.
Und das ist bei dir eindeutig der Fall, da du Grundelemente falsch anwendest und dein Code großteils aus Coderedundanzen besteht.
Ich als Entwickler würde mich darüber freuen wenn ich Murks zusammencode und mich jemand darauf hinweist bevor ich damit auf die Schnautze falle, das spart mir ne menge Zeit, Arbeit und Ärger.
Jeder gute Entwickler sollte sachliche Kritik seines Codes schätzen.
Ich meine mir kann das prinzipiell egal sein ob du Müll codest oder nicht: Dein Snippet werde ich nie nutzen, weil ich sowas in 5min selbst schreibe wenn ich es brauche, und dann keinen astromomischen Wartungsaufwand habe und auch keine Serverfarm mieten muss um das Ding produktiv nutzen zu können.
War nur ein gut gemeinter Rat, da du mit DEM Codingstyl definitiv keine vernünftigen Projekte realisieren und dauerhaft am leben halten kannst.
Steht dir natürlich frei auf die Meinung von Leuten die wohl ein paar Jahre mehr Erfahrung haben wie du zu scheißen und selbst mit dem Kopf gegen die Wand zu rennen.
Ist schließlich dein Kopf und nicht meiner, also nur zu xD
Ähnliche Themen
-
MyBB 1.6.10 veröffentlicht
Von Devon im Forum Web-ApplicationsAntworten: 0Letzter Beitrag: 22.04.2013, 14:05 -
[MyBB] CounterPlugin
Von Snees im Forum Web-ApplicationsAntworten: 6Letzter Beitrag: 07.04.2013, 19:39 -
MyBB 1.8 Alpha veröffentlicht
Von Devon im Forum Web-ApplicationsAntworten: 0Letzter Beitrag: 28.01.2013, 11:48 -
MyBB 1.8 in den Startlöchern!
Von Devon im Forum Web-ApplicationsAntworten: 1Letzter Beitrag: 04.04.2012, 11:21 -
.PSD Button (MyBB)
Von Shane im Forum RessourcenAntworten: 0Letzter Beitrag: 20.03.2012, 22:44
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.