Underworld1337

Untitled

Jul 31st, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.26 KB | None | 0 0
  1. <?php
  2. /**
  3.   * Alle Basisfunktionen
  4.   * +Sidebar returnen / TODO per MySQL generieren
  5.   **/
  6. class base
  7. {
  8.     private $con;
  9.    
  10.     private $action_next;
  11.     private $action_out;
  12.     private $action_wait;
  13.     private $invite_only;
  14.  
  15.     public function __construct(){
  16.         $this->con = $GLOBALS['DB'];
  17.         $this->action_next = "home";
  18.         $this->action_out = "Platzhalter";
  19.         $this->action_wait = 3;
  20.         if($GLOBALS['inviteonly'] === true)
  21.           $this->invite_only = true;
  22.         else
  23.           $this->invite_only = false;
  24.     }
  25.    
  26.     public function pagetitle(){
  27.         // TODO verarbeitung
  28.       if(!isset($_GET['page']))
  29.         return "Default";
  30.       else
  31.         return $_GET['page'];
  32.     }
  33.   public function get_elapsed_time($ts)
  34.   {
  35.     $mins = floor((time() - $ts) / 60);
  36.     $hours = floor($mins / 60);
  37.     $mins -= $hours * 60;
  38.     $days = floor($hours / 24);
  39.     $hours -= $days * 24;
  40.     $weeks = floor($days / 7);
  41.     $days -= $weeks * 7;
  42.     $months = floor($weeks / 4);
  43.     $weeks -= $months * 4;
  44.     $t = "";
  45.    
  46.     if ($months > 0)
  47.       return "$months month" . ($months > 1 ? "s" : "");
  48.     if ($weeks > 0)
  49.       return "$weeks week" . ($weeks > 1 ? "s" : "");
  50.     if ($days > 0)
  51.       return "$days day" . ($days > 1 ? "s" : "");
  52.     if ($hours > 0)
  53.       return "$hours hour" . ($hours > 1 ? "s" : "");
  54.     if ($mins > 0)
  55.       return "$mins minute" . ($mins > 1 ? "s" : "");
  56.     return "< 1 minute";
  57.   }
  58.    
  59.     public function isInviteOnly(){
  60.       return $this->invite_only;
  61.     }
  62.  
  63.     public static function getTS(){
  64.         $time = new DateTime();
  65.         $ts = $time->getTimestamp();
  66.         return $ts;
  67.     }
  68.  
  69.   public function get_nextfile() {
  70.     return $this->action_next;
  71.   }
  72.   public function get_cooldown() {
  73.     return $this->action_wait;
  74.   }
  75.  
  76.   public function convert_class($n = 0){
  77.     switch($n){
  78.       default;
  79.         return 'User';
  80.         break;
  81.       case 0;
  82.         return 'User';
  83.         break;
  84.       case 1;
  85.         return 'Power-User';
  86.         break;
  87.       case 2;
  88.         return 'VIP';
  89.         break;
  90.       case 3;
  91.         return 'Uploader';
  92.         break;
  93.       case 4;
  94.         return 'Moderator';
  95.         break;
  96.       case 5;
  97.         return 'Administrator';
  98.         break;
  99.       case 6;
  100.         return 'Sysop';
  101.         break;
  102.       case 7;
  103.         return 'Undefined';
  104.         break;
  105.       case 8;
  106.         return 'Undefined';
  107.         break;
  108.       case 9;
  109.         return 'Undefined';
  110.         break;
  111.       case 10;
  112.         return 'Coder';
  113.         break;
  114.     }
  115.   }
  116.  
  117.     public function validate_email($email) {
  118.       if(filter_var($email, FILTER_VALIDATE_EMAIL) !== false)
  119.         return true;
  120.       else
  121.         return false;
  122.     }
  123.  
  124.     /*public function validate_email($email) {
  125.       if(strlen($email) > 256)
  126.         return false;
  127.       $pattern = '#^\S{1,64}@\S+\.\S{2,6}$#i';
  128.       return (bool) preg_match($pattern, $email);
  129.     }*/
  130.    
  131.     public function getOut($a){
  132.       if($a == "header"){
  133.         $o = explode("|", $this->action_out);
  134.         return $o[0];
  135.       }elseif($a == "output"){
  136.         if($this->action_out == "Platzhalter")
  137.           return "Kein Output definiert.";
  138.         else{
  139.           $o = explode("|", $this->action_out);
  140.           unset($o[0]);
  141.           $o = array_merge($o);
  142.           $r = "";
  143.           foreach($o as $k => $v){
  144.             $r .= $v;
  145.             $r .= "<br><br>";
  146.           }
  147.           return $r;
  148.         }
  149.       }
  150.     }
  151.    
  152.     private function addOutput($output, $keep = true){
  153.       $old_output = $this->action_out;
  154.       if($old_output == "Platzhalter")
  155.         $this->action_out = $output;
  156.       else{
  157.         $new_parts = explode("|", $output);
  158.         $old_parts = explode("|", $old_output);
  159.         $new_output = array();
  160.         $h = array();
  161.         if($keep)
  162.           $h[0] = $old_parts[0];
  163.         else
  164.           $h[0] = $new_parts[0];
  165.         unset($old_parts[0]);
  166.         $new_output = array_merge($h, $old_parts);
  167.         $new_output[] = $new_parts[1];
  168.         $this->action_out = implode("|", $new_output);
  169.       }
  170.     }
  171.    
  172.     private function setNextPage($page = "home", $cooldown = 3){
  173.       if($page != $this->action_next)
  174.         $this->action_next = $page;
  175.    
  176.       if($cooldown != $this->action_wait)
  177.         $this->action_wait = $cooldown;
  178.     }
  179.    
  180.     private function getNewSSID(){
  181.       return self::randomstring(50, true, false, false);
  182.     }
  183.    
  184.     public function doLogin($user, $pw){
  185.       $qry = $this->con->prepare('SELECT id, passhash FROM users WHERE name = :name');
  186.       $qry->bindParam(':name', $user, PDO::PARAM_STR);
  187.       $qry->execute();
  188.       if($qry->rowCount() > 0){
  189.         //$row = $qry->fetchAll(PDO::FETCH_ASSOC);
  190.         $row = $qry->fetchObject();
  191.         if(md5($pw) == $row->passhash){
  192.           $_SESSION['logged_in'] = true;
  193.           $_SESSION['SSID'] = self::getNewSSID();
  194.           $_SESSION['user_id'] = $row->id;
  195.           $_SESSION['user_name'] = $user;
  196.         $_SESSION['old_ts'] = self::getTS();
  197.         $_SESSION['new_ts'] = self::getTS();
  198.           self::addOutput("Erfolg|Du hast dich eingeloggt.", false);
  199.         self::setNextPage("homeinternal",5);
  200.         }else{
  201.           self::addOutput("Fehler|Username und Passwort stimmen nicht überein.");
  202.           self::setNextPage("home",15);
  203.         }
  204.       }else{
  205.         self::addOutput("Fehler|Unbekannter Username!");
  206.         self::setNextPage("home",15);
  207.       }
  208.     }
  209.  
  210.   public function check_session(){
  211.     self::update_session();
  212.     if($_SESSION['new_ts'] >= $_SESSION['old_ts']+300){
  213.       self::update_last_activity();
  214.     }
  215.   }
  216.   private function update_session(){
  217.     $_SESSION['new_ts'] = self::getTS();
  218.   }
  219.   private function update_last_activity(){
  220.       $q = $this->con->prepare('UPDATE users SET last_activity = :nts WHERE id = :uid');
  221.       $q->bindParam(':uid', $_SESSION['user_id'], PDO::PARAM_INT);
  222.       $q->bindParam(':nts', $_SESSION['new_ts'], PDO::PARAM_INT);
  223.       $q->execute();
  224.     $_SESSION['old_ts'] = $_SESSION['new_ts'];
  225.   }
  226.    
  227.     public function doLogout(){
  228.       if(array_key_exists('SSID', $_SESSION)){
  229.         session_destroy();
  230.         //unset($_SESSION);
  231.         self::addOutput("Erfolg|Du wurdest ausgeloggt.");
  232.         self::setNextPage("home",3);
  233.       }
  234.     }
  235.    
  236.     public function logincheck(){
  237.       if(array_key_exists('SSID', $_SESSION)){
  238.         return true;
  239.       }else
  240.         return false;
  241.     }
  242.    
  243.     private function checkname($n){
  244.       $qry = $this->con->prepare('SELECT name FROM users WHERE name = :name');
  245.       $qry->bindParam(':name', $n, PDO::PARAM_STR);
  246.       $qry->execute();
  247.       if($qry->rowCount () == 0){
  248.         $cs = "abcdefghijklmnopqrsßtuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  249.         if(strlen($n) < 12 || strlen($n) > 3){
  250.           $t = false;
  251.           for ($i = 0; $i < strlen($n); ++$i){
  252.             if (strpos($cs, $n[$i]) === false){
  253.               $t = false;
  254.               break;
  255.             }else
  256.               $t = true;
  257.           }
  258.           if($t)
  259.             return true;
  260.           else
  261.             return false;
  262.         }else
  263.           return false;
  264.       }else
  265.         return false;
  266.     }
  267.    
  268.     public function createAccount($user, $pw, $repeat, $email, $invitecode = '', $override = false){
  269.       $s = true;
  270.       if(!$override)
  271.       // general
  272.       if(empty($user) || empty($pw) || empty($repeat) || empty($email)){
  273.         self::addOutput("Fehler|Du musst alle Felder aus&uuml;llen");
  274.         $s = false;
  275.       }
  276.      
  277.       if($user == $pw){
  278.         self::addOutput("Fehler|Dein Passwort darf nicht mit deinem Benutzernamen identisch sein.");
  279.         $s = false;
  280.       }
  281.      
  282.       // Name
  283.       if(self::checkname($user) !== true){
  284.         self::addOutput("Fehler|Name fehlerhaft oder in Verwendung.<br>Mindestens 3 Zeichen - Maximal 12 Zeichen<br>Erlaubte Zeichen: a-z A-Z 0-9");
  285.         $s = false;
  286.       }
  287.      
  288.       // pw
  289.       if($pw != $repeat){
  290.         self::addOutput("Fehler|Passwörter stimmen nicht &uuml;berein");
  291.         $s = false;
  292.       }
  293.      
  294.       if(strlen($pw) > 50 || strlen($pw) < 8){
  295.         self::addOutput("Fehler|Passwortl&auml;nge fehlerhaft.<br>Mindestens 8 Zeichen - Maximal 50 Zeichen");
  296.         $s = false;
  297.       }
  298.      
  299.       //email
  300.       $qry = $this->con->prepare('SELECT email FROM users WHERE email = :mail');
  301.       $qry->bindParam(':mail', $email, PDO::PARAM_STR);
  302.       $qry->execute();
  303.       if($qry->rowCount () != 0){
  304.         self::addOutput("Fehler|Mail-Adresse wird verwendet");
  305.         $s = false;
  306.       }
  307.  
  308.       if (self::validate_email($email) != true) {
  309.         self::addOutput("Fehler|Mail-Adresse scheint ung&uuml;ltig zu sein");
  310.         $s = false;
  311.       }
  312.      
  313.       // invite // TODO
  314.       if($this->invite_only === true && empty($invitecode)){
  315.         self::addOutput("Fehler|Du hast keinen Invitecode eingegeben");
  316.         $s = false;
  317.       }elseif($this->invite_only === false){
  318.         $invitecode = 'nocode';
  319.       }
  320.      
  321.       if($s){
  322.      
  323.        
  324.         //process data
  325.         $passhash = md5($pw);
  326.         $time = self::getTS();
  327.    
  328.         //insert new user
  329.         $qry = $this->con->prepare("INSERT INTO users (`name`, `email`, `passhash`, `invitecode`, `registration_ts`)
  330.                                                VALUES (:name, :email, :passhash, :invitecode, :rts)");
  331.         $qry->bindParam(':name', $user, PDO::PARAM_STR);
  332.         $qry->bindParam(':email', $email, PDO::PARAM_STR);
  333.         $qry->bindParam(':passhash', $passhash, PDO::PARAM_STR);
  334.         $qry->bindParam(':invitecode', $invitecode, PDO::PARAM_STR);
  335.         $qry->bindParam(':rts', $time, PDO::PARAM_INT);
  336.        
  337.       if($qry->execute()){
  338.         self::addOutput("Erfolg|Account wurde angelegt", false);
  339.         self::setNextPage("homeinternal",3);
  340.       }else{
  341.          //var_dump( $qry );
  342.       //exit;
  343.         self::addOutput("Fehler|Account konnte nicht angelegt werden.");
  344.         self::setNextPage("home",10);
  345.       }
  346.     }else{
  347.         self::setNextPage("home");
  348.       }
  349.     }
  350.    
  351.    
  352.     public function getSidebar(){
  353.         $ret = "";
  354.         $ret .= "<div id=\"sidebar_container\">";
  355.         $ret .= "  <div class=\"sidebar\">";
  356.         $ret .= "   <h3>Latest News</h3>";
  357.         $ret .= "    <h4>Devlog</h4>";
  358.         $ret .= "    <h5>15.12.15</h5>";
  359.         $ret .= "    <p>Template gefunden und in das CMS integriert.</p>";
  360.         $ret .= "  </div>";
  361.         $ret .= "  <div class=\"sidebar\">";
  362.         //$ret .= "   <h3>Latest News</h3>";
  363.         $ret .= "    <h4>Devlog</h4>";
  364.         $ret .= "    <h5>17.12.15</h5>";
  365.         $ret .= "    <p>Datenbank eingebunden.</p>";
  366.         $ret .= "  </div>";
  367.         $ret .= "  <div class=\"sidebar\">";
  368.         //$ret .= "   <h3>Latest News</h3>";
  369.         $ret .= "    <h4>Devlog</h4>";
  370.         $ret .= "    <h5>29.12.15</h5>";
  371.         $ret .= "    <p>Accounts können angelegt werden. Viele Funktionen für die base-class geschrieben.</p>";
  372.         $ret .= "    <p>Next:</p>";
  373.         $ret .= "    <ul>";
  374.         $ret .= "      <li>Loginsystem</li>";
  375.         $ret .= "      <li>internal area</li>";
  376.         $ret .= "    </ul>";
  377.         $ret .= "  </div>";
  378.         $ret .= "</div>";
  379.         return $ret;
  380.     }
  381.  
  382.     function randomstring($length = 10, $numbers = TRUE, $letters = TRUE, $otherChars = FALSE){
  383.         $ranges = array();
  384.  
  385.         if (0 >= (int)$length) {
  386.             $length = 10;
  387.         }
  388.         if (TRUE === $numbers) {
  389.             $ranges[] = range(0, 9);
  390.         }
  391.         if (TRUE === $letters) {
  392.             $ranges[] = range('a', 'z');
  393.             $ranges[] = range('A', 'Z');
  394.         }
  395.         if (!empty($otherChars)) {
  396.             $ranges[] = str_split((string)$otherChars);
  397.         }
  398.         if (empty($ranges)) {
  399.             $ranges[] = str_split(sha1(rand()));
  400.         }
  401.         $keys = call_user_func_array('array_merge', $ranges);
  402.  
  403.         while (count($keys) < (int)$length) {
  404.             $keys = array_merge($keys, $keys);
  405.         }
  406.         shuffle($keys);
  407.  
  408.         $str = implode('', array_slice($keys, 0, (int)$length));
  409.  
  410.         return $str;
  411.     }
  412. }
  413. ?>
Add Comment
Please, Sign In to add comment