Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Alle Basisfunktionen
- * +Sidebar returnen / TODO per MySQL generieren
- **/
- class base
- {
- private $con;
- private $action_next;
- private $action_out;
- private $action_wait;
- private $invite_only;
- public function __construct(){
- $this->con = $GLOBALS['DB'];
- $this->action_next = "home";
- $this->action_out = "Platzhalter";
- $this->action_wait = 3;
- if($GLOBALS['inviteonly'] === true)
- $this->invite_only = true;
- else
- $this->invite_only = false;
- }
- public function pagetitle(){
- // TODO verarbeitung
- if(!isset($_GET['page']))
- return "Default";
- else
- return $_GET['page'];
- }
- public function get_elapsed_time($ts)
- {
- $mins = floor((time() - $ts) / 60);
- $hours = floor($mins / 60);
- $mins -= $hours * 60;
- $days = floor($hours / 24);
- $hours -= $days * 24;
- $weeks = floor($days / 7);
- $days -= $weeks * 7;
- $months = floor($weeks / 4);
- $weeks -= $months * 4;
- $t = "";
- if ($months > 0)
- return "$months month" . ($months > 1 ? "s" : "");
- if ($weeks > 0)
- return "$weeks week" . ($weeks > 1 ? "s" : "");
- if ($days > 0)
- return "$days day" . ($days > 1 ? "s" : "");
- if ($hours > 0)
- return "$hours hour" . ($hours > 1 ? "s" : "");
- if ($mins > 0)
- return "$mins minute" . ($mins > 1 ? "s" : "");
- return "< 1 minute";
- }
- public function isInviteOnly(){
- return $this->invite_only;
- }
- public static function getTS(){
- $time = new DateTime();
- $ts = $time->getTimestamp();
- return $ts;
- }
- public function get_nextfile() {
- return $this->action_next;
- }
- public function get_cooldown() {
- return $this->action_wait;
- }
- public function convert_class($n = 0){
- switch($n){
- default;
- return 'User';
- break;
- case 0;
- return 'User';
- break;
- case 1;
- return 'Power-User';
- break;
- case 2;
- return 'VIP';
- break;
- case 3;
- return 'Uploader';
- break;
- case 4;
- return 'Moderator';
- break;
- case 5;
- return 'Administrator';
- break;
- case 6;
- return 'Sysop';
- break;
- case 7;
- return 'Undefined';
- break;
- case 8;
- return 'Undefined';
- break;
- case 9;
- return 'Undefined';
- break;
- case 10;
- return 'Coder';
- break;
- }
- }
- public function validate_email($email) {
- if(filter_var($email, FILTER_VALIDATE_EMAIL) !== false)
- return true;
- else
- return false;
- }
- /*public function validate_email($email) {
- if(strlen($email) > 256)
- return false;
- $pattern = '#^\S{1,64}@\S+\.\S{2,6}$#i';
- return (bool) preg_match($pattern, $email);
- }*/
- public function getOut($a){
- if($a == "header"){
- $o = explode("|", $this->action_out);
- return $o[0];
- }elseif($a == "output"){
- if($this->action_out == "Platzhalter")
- return "Kein Output definiert.";
- else{
- $o = explode("|", $this->action_out);
- unset($o[0]);
- $o = array_merge($o);
- $r = "";
- foreach($o as $k => $v){
- $r .= $v;
- $r .= "<br><br>";
- }
- return $r;
- }
- }
- }
- private function addOutput($output, $keep = true){
- $old_output = $this->action_out;
- if($old_output == "Platzhalter")
- $this->action_out = $output;
- else{
- $new_parts = explode("|", $output);
- $old_parts = explode("|", $old_output);
- $new_output = array();
- $h = array();
- if($keep)
- $h[0] = $old_parts[0];
- else
- $h[0] = $new_parts[0];
- unset($old_parts[0]);
- $new_output = array_merge($h, $old_parts);
- $new_output[] = $new_parts[1];
- $this->action_out = implode("|", $new_output);
- }
- }
- private function setNextPage($page = "home", $cooldown = 3){
- if($page != $this->action_next)
- $this->action_next = $page;
- if($cooldown != $this->action_wait)
- $this->action_wait = $cooldown;
- }
- private function getNewSSID(){
- return self::randomstring(50, true, false, false);
- }
- public function doLogin($user, $pw){
- $qry = $this->con->prepare('SELECT id, passhash FROM users WHERE name = :name');
- $qry->bindParam(':name', $user, PDO::PARAM_STR);
- $qry->execute();
- if($qry->rowCount() > 0){
- //$row = $qry->fetchAll(PDO::FETCH_ASSOC);
- $row = $qry->fetchObject();
- if(md5($pw) == $row->passhash){
- $_SESSION['logged_in'] = true;
- $_SESSION['SSID'] = self::getNewSSID();
- $_SESSION['user_id'] = $row->id;
- $_SESSION['user_name'] = $user;
- $_SESSION['old_ts'] = self::getTS();
- $_SESSION['new_ts'] = self::getTS();
- self::addOutput("Erfolg|Du hast dich eingeloggt.", false);
- self::setNextPage("homeinternal",5);
- }else{
- self::addOutput("Fehler|Username und Passwort stimmen nicht überein.");
- self::setNextPage("home",15);
- }
- }else{
- self::addOutput("Fehler|Unbekannter Username!");
- self::setNextPage("home",15);
- }
- }
- public function check_session(){
- self::update_session();
- if($_SESSION['new_ts'] >= $_SESSION['old_ts']+300){
- self::update_last_activity();
- }
- }
- private function update_session(){
- $_SESSION['new_ts'] = self::getTS();
- }
- private function update_last_activity(){
- $q = $this->con->prepare('UPDATE users SET last_activity = :nts WHERE id = :uid');
- $q->bindParam(':uid', $_SESSION['user_id'], PDO::PARAM_INT);
- $q->bindParam(':nts', $_SESSION['new_ts'], PDO::PARAM_INT);
- $q->execute();
- $_SESSION['old_ts'] = $_SESSION['new_ts'];
- }
- public function doLogout(){
- if(array_key_exists('SSID', $_SESSION)){
- session_destroy();
- //unset($_SESSION);
- self::addOutput("Erfolg|Du wurdest ausgeloggt.");
- self::setNextPage("home",3);
- }
- }
- public function logincheck(){
- if(array_key_exists('SSID', $_SESSION)){
- return true;
- }else
- return false;
- }
- private function checkname($n){
- $qry = $this->con->prepare('SELECT name FROM users WHERE name = :name');
- $qry->bindParam(':name', $n, PDO::PARAM_STR);
- $qry->execute();
- if($qry->rowCount () == 0){
- $cs = "abcdefghijklmnopqrsßtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
- if(strlen($n) < 12 || strlen($n) > 3){
- $t = false;
- for ($i = 0; $i < strlen($n); ++$i){
- if (strpos($cs, $n[$i]) === false){
- $t = false;
- break;
- }else
- $t = true;
- }
- if($t)
- return true;
- else
- return false;
- }else
- return false;
- }else
- return false;
- }
- public function createAccount($user, $pw, $repeat, $email, $invitecode = '', $override = false){
- $s = true;
- if(!$override)
- // general
- if(empty($user) || empty($pw) || empty($repeat) || empty($email)){
- self::addOutput("Fehler|Du musst alle Felder ausüllen");
- $s = false;
- }
- if($user == $pw){
- self::addOutput("Fehler|Dein Passwort darf nicht mit deinem Benutzernamen identisch sein.");
- $s = false;
- }
- // Name
- if(self::checkname($user) !== true){
- self::addOutput("Fehler|Name fehlerhaft oder in Verwendung.<br>Mindestens 3 Zeichen - Maximal 12 Zeichen<br>Erlaubte Zeichen: a-z A-Z 0-9");
- $s = false;
- }
- // pw
- if($pw != $repeat){
- self::addOutput("Fehler|Passwörter stimmen nicht überein");
- $s = false;
- }
- if(strlen($pw) > 50 || strlen($pw) < 8){
- self::addOutput("Fehler|Passwortlänge fehlerhaft.<br>Mindestens 8 Zeichen - Maximal 50 Zeichen");
- $s = false;
- }
- //email
- $qry = $this->con->prepare('SELECT email FROM users WHERE email = :mail');
- $qry->bindParam(':mail', $email, PDO::PARAM_STR);
- $qry->execute();
- if($qry->rowCount () != 0){
- self::addOutput("Fehler|Mail-Adresse wird verwendet");
- $s = false;
- }
- if (self::validate_email($email) != true) {
- self::addOutput("Fehler|Mail-Adresse scheint ungültig zu sein");
- $s = false;
- }
- // invite // TODO
- if($this->invite_only === true && empty($invitecode)){
- self::addOutput("Fehler|Du hast keinen Invitecode eingegeben");
- $s = false;
- }elseif($this->invite_only === false){
- $invitecode = 'nocode';
- }
- if($s){
- //process data
- $passhash = md5($pw);
- $time = self::getTS();
- //insert new user
- $qry = $this->con->prepare("INSERT INTO users (`name`, `email`, `passhash`, `invitecode`, `registration_ts`)
- VALUES (:name, :email, :passhash, :invitecode, :rts)");
- $qry->bindParam(':name', $user, PDO::PARAM_STR);
- $qry->bindParam(':email', $email, PDO::PARAM_STR);
- $qry->bindParam(':passhash', $passhash, PDO::PARAM_STR);
- $qry->bindParam(':invitecode', $invitecode, PDO::PARAM_STR);
- $qry->bindParam(':rts', $time, PDO::PARAM_INT);
- if($qry->execute()){
- self::addOutput("Erfolg|Account wurde angelegt", false);
- self::setNextPage("homeinternal",3);
- }else{
- //var_dump( $qry );
- //exit;
- self::addOutput("Fehler|Account konnte nicht angelegt werden.");
- self::setNextPage("home",10);
- }
- }else{
- self::setNextPage("home");
- }
- }
- public function getSidebar(){
- $ret = "";
- $ret .= "<div id=\"sidebar_container\">";
- $ret .= " <div class=\"sidebar\">";
- $ret .= " <h3>Latest News</h3>";
- $ret .= " <h4>Devlog</h4>";
- $ret .= " <h5>15.12.15</h5>";
- $ret .= " <p>Template gefunden und in das CMS integriert.</p>";
- $ret .= " </div>";
- $ret .= " <div class=\"sidebar\">";
- //$ret .= " <h3>Latest News</h3>";
- $ret .= " <h4>Devlog</h4>";
- $ret .= " <h5>17.12.15</h5>";
- $ret .= " <p>Datenbank eingebunden.</p>";
- $ret .= " </div>";
- $ret .= " <div class=\"sidebar\">";
- //$ret .= " <h3>Latest News</h3>";
- $ret .= " <h4>Devlog</h4>";
- $ret .= " <h5>29.12.15</h5>";
- $ret .= " <p>Accounts können angelegt werden. Viele Funktionen für die base-class geschrieben.</p>";
- $ret .= " <p>Next:</p>";
- $ret .= " <ul>";
- $ret .= " <li>Loginsystem</li>";
- $ret .= " <li>internal area</li>";
- $ret .= " </ul>";
- $ret .= " </div>";
- $ret .= "</div>";
- return $ret;
- }
- function randomstring($length = 10, $numbers = TRUE, $letters = TRUE, $otherChars = FALSE){
- $ranges = array();
- if (0 >= (int)$length) {
- $length = 10;
- }
- if (TRUE === $numbers) {
- $ranges[] = range(0, 9);
- }
- if (TRUE === $letters) {
- $ranges[] = range('a', 'z');
- $ranges[] = range('A', 'Z');
- }
- if (!empty($otherChars)) {
- $ranges[] = str_split((string)$otherChars);
- }
- if (empty($ranges)) {
- $ranges[] = str_split(sha1(rand()));
- }
- $keys = call_user_func_array('array_merge', $ranges);
- while (count($keys) < (int)$length) {
- $keys = array_merge($keys, $keys);
- }
- shuffle($keys);
- $str = implode('', array_slice($keys, 0, (int)$length));
- return $str;
- }
- }
- ?>
Add Comment
Please, Sign In to add comment