Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.05 KB | None | 0 0
  1. <?php
  2.  
  3. /*function basic_connect() {   //  Delete as soon as possible -> use connect::query() o connect::connect_simple() instead
  4.     $db = new mysqli('localhost', USER, PASS, DATAB);
  5.     if ($db->connect_errno) {
  6.     echo '<div id="submit"><h2>Error!</h2><p>Couldn\'t connect to the database (' . $db->connect_errno .  ')</p><p><a href="JavaScript:void(0);" onclick="history.back()">Go back?</a></p>';
  7.     return false;
  8.     }
  9.     return $db;
  10. }
  11. */
  12.  
  13.  
  14. function isodd($num) { /* Check if a number is even or odd */
  15. $a = ($num % 2) ? 0 : 1;
  16. return $a;
  17. }
  18.  
  19. function table_class($i) {
  20.     switch(isodd($i)) {
  21.     case 0:
  22.     return 'light';
  23.    
  24.     case 1:
  25.     return 'dark';
  26.  
  27.     default:
  28.     return 'light';
  29.  
  30.     }
  31. }
  32.  
  33.  
  34. /* handle queries and more repetitive tasks (e.g. displaying tables and statistics)
  35.  
  36. For quick queries where no further calls to mysqli object are needed, use connect::query ->> will return the result from a query
  37. When needed to access the mysqli object, .e.g to get affected rows in last query, etc., just use connect::connect_simple -> will return the mysqli object itself
  38.  
  39. */
  40.  
  41. class connect {
  42.  
  43.     public static function query($query) {    // We send a query and return the resulting array
  44.     $db = new mysqli('localhost', USER, PASS, DATAB);
  45.     if ($db->connect_errno) {
  46.     return false;  
  47.     }
  48.     $result = $db->query($query);
  49.     $db->close();
  50.     return $result;
  51.  
  52.      }
  53.  
  54.  
  55.  
  56.     public static function connect_simple() {  
  57.     $db = new mysqli('localhost', USER, PASS, DATAB);
  58.     if ($db->connect_errno) {
  59.     return false;  
  60.     }
  61.     return $db;
  62.     }
  63.  
  64.    
  65.    
  66.     function retrieve($query) {        // certain predefined queries to send to connect::query()
  67.  
  68.     switch($query) {
  69.  
  70.     case 'now':
  71.     return $this->query('select * from anime where status = 1 order by name');
  72.  
  73.     case 'old':
  74.     return $this->query('select * from anime where status = 0 order by name');
  75.  
  76.     case 'total_series':
  77.     return $this->query('select * from anime where count >= 0');
  78.  
  79.     case 'count1':
  80.     return $this->query('select * from anime where count = 1 or count = 0');
  81.    
  82.     case 'count2':
  83.     return $this->query('select * from anime where count > 1');
  84.     }
  85. }
  86.  
  87.  
  88.     private function _destruct() {    // Does this even work or do something?
  89.     if (isset($result)) { $result->free(); }
  90.     if (isset($db)) { $db->close(); }
  91.     }
  92.  
  93. }
  94.  
  95.  
  96.  
  97.  
  98. /* handle the different actions from index.php */
  99.  
  100. function index_action($act) {
  101.  
  102. switch($act) {
  103.  
  104. case 'error':
  105. require('error.php');
  106. break;
  107.  
  108. case 'add':
  109. require('add.php');
  110. break;
  111.  
  112. case 'submit':
  113. require('submit.php');
  114. break;
  115.  
  116. case 'delete':
  117. require('submit.php');
  118. break;
  119.  
  120. case 'login':
  121. require('login.php');
  122. break;
  123.  
  124. case 'logout':
  125. require('logout.php');
  126. break;
  127.  
  128. case 'changelog':
  129. require('changelog.php');
  130. break;
  131.  
  132. default:
  133. include('main.php');
  134. }
  135. }
  136.  
  137.  
  138.  
  139. /* Here we will process all the data we get form the DB. Apart from making it more secure, try to get rid of incongruencies, format it, etc.
  140. To be used both in the tables and elsewhere, e.g. statistics */
  141.  
  142. class anime {
  143. private $data, $id, $name, $totaleps, $watchedeps, $year, $season, $status, $rating, $count, $comment; 
  144.  
  145.  
  146.     function __construct($db_data) {        // whenever we create an object of this class we need to input the raw data from the db
  147.      
  148.      $this->data = $db_data;
  149.     }
  150.  
  151.     function get_id() {
  152.     $this->id = $this->data['animeID'];
  153.     return $this->id;
  154.     }
  155.  
  156.     function get_name() {
  157.     $this->name = $this->data['name'];
  158.     $this->name = ucwords($this->name);
  159.     $this->name = htmlentities($this->name, ENT_QUOTES, 'UTF-8');
  160.     return $this->name;
  161.     }
  162.  
  163.  
  164.     function get_totaleps() {
  165.     $this->totaleps = $this->data['total-episodes'];
  166.     $this->totaleps = (int)$this->totaleps;
  167.     if ($this->totaleps < 10) { $this->totaleps = 0 . $this->totaleps; }
  168.     if ($this->totaleps == 0) { return 'N/A'; }
  169.     else { return $this->totaleps; }
  170.     }
  171.  
  172.  
  173.     function get_watched() {
  174.     $this->watchedeps = $this->data['total-watched'];
  175.     $this->watchedeps = (int)$this->watchedeps;
  176.     if ($this->watchedeps < 10) { $this->watchedeps = 0 . $this->watchedeps; }
  177.     return $this->watchedeps;
  178.     }
  179.  
  180.  
  181.     function get_year() {
  182.     $this->year = $this->data['year-watched'];
  183.     $this->year = (int)$this->year;
  184.     if ($this->year == 0) { return 'N/A'; }
  185.     else if ($this->year < 1970) { return 'N/A'; }
  186.     else if ($this->year > (date('Y') + 2)) { return 'N/A'; }
  187.     else { return $this->year; }
  188.     }
  189.  
  190.  
  191.     function get_season() {
  192.     $this->season = $this->data['season-watched'];
  193.     $this->season = (int)$this->season;
  194.     switch ($this->season) {
  195.         case 1:
  196.         return 'Winter';
  197.         case 2:
  198.         return 'Spring';
  199.         case 3:
  200.         return 'Summer';
  201.         case 4:
  202.         return 'Fall';
  203.         default:
  204.         return 'N/A';
  205.         }
  206.     }
  207.  
  208.     function get_status() {
  209.     $this->status = (int)$this->data['status'];
  210.     if ($this->status != 1 && $this->status != 0) { return 0; }
  211.     else { return $this->status; }
  212.     }
  213.  
  214.  
  215.  
  216.  
  217.     function ratingtext($param, $param2)  {  //$param is the rating value from the db. $param2 determines which array to use
  218.  
  219.     $rating_now = array('No rating', 'I\'m not enjoying it', 'I\'m liking it &hearts;', 'I\'m loving it &hearts;&hearts;', 'Favorite &hearts;&hearts;&hearts;');
  220.     $rating_old = array('No rating', 'I didn\'t enjoy it', 'I liked it &hearts;', 'I loved it &hearts;&hearts;', 'Favorite &hearts;&hearts;&hearts;');
  221.  
  222.     if ($param2 == 0) {
  223.         $rating = &$rating_old;
  224.     }
  225.  
  226.     else {
  227.         $rating = &$rating_now;
  228.     }
  229.  
  230.     switch($param) {
  231.         case 0:
  232.         return sprintf('<span class="norating">%s</span>', $rating[0]);
  233.         case 1:
  234.         return sprintf('<span class="dislike">%s</span>', $rating[1]);
  235.         case 2:
  236.         return sprintf('<span class="like">%s</span>', $rating[2]);
  237.         case 3:
  238.         return sprintf('<span class="love">%s</span>', $rating[3]);
  239.         case 4:
  240.         return sprintf('<span class="favorite">%s</span>', $rating[4]);
  241.         default:
  242.         return 'N/A';
  243.         }  
  244.     }
  245.  
  246.  
  247.  
  248.     function get_rating() {
  249.     $this->rating = (int)$this->data['rating'];
  250.     if ($this->get_status() == 0) { return $this->ratingtext($this->rating, 0); }
  251.     else { return $this->ratingtext($this->rating, 1); }
  252.  
  253.     }
  254.  
  255.     function get_count() {
  256.     $this->count = (int)$this->data['count'];
  257.     if ($this->count < 1) { return 'Dropped'; }
  258.     else if ($this->count > 1 && $this->get_status() == 1) { return sprintf('%d <abbr class="cursor_help" title="Rewatching"> (R)</abbr>', $this->count); }
  259.     else { return $this->count; }
  260.     }
  261.  
  262.     function get_comment() {
  263.     $this->comment = $this->data['comment'];
  264.     if (strlen($this->comment) == 0) { return 'No comment'; }
  265.     else { return $this->comment; }
  266.     }
  267.  
  268.  
  269. } /* end class anime */
  270.  
  271.  
  272.  
  273.  
  274.     /* Function to draw the table with the data from the DB */
  275.  
  276. function draw_table($param)  // 0 = current, 1 = completed
  277. {
  278.  
  279. $db = new connect();
  280.  
  281.  
  282.     // This determines which data to retrieve from database. This allows us to use the same loop for both animes we're watching and completed animes, and allows us to potentially use it for any other table with other data
  283.     if ($param == 0) { $result = $db->retrieve('now'); }    
  284.     else { $result = $db->retrieve('old'); }
  285.    
  286.       $rows = $result->num_rows;
  287.  
  288.  
  289.     // We create the table header
  290.    
  291.        $format = '<table class="currently-watching"><tr><th class="number">#</th><th class="series">Series</th><th class="episodes">Eps</th><th class="rating">Impression</th><th class="season">Season</th><th class="count">Count</th>';
  292.  
  293.     if (check_login()) {   // If valid session, we also get the "ACTIONS" column
  294.         $format = sprintf('%s <th class="actions">Actions</th></tr>', $format);
  295.     }
  296.  
  297.  /* loop to retrieve the data from db and draw the table with it. For each row retrieved, we generate a table row. Every time we iterate, we will add a new row to the variable $format, so at the end it will contain the whole table */
  298.  
  299.     for ($i = 0; $i < $rows; $i++) {  
  300.    
  301.     $style = table_class($i);
  302.     $rowres = $result->fetch_assoc();
  303.     $table = new anime($rowres);
  304.  
  305.      $format =  sprintf("%s <tr class='%s'><td class='number'>%s</td><td class='series'>%s</td><td class='episodes'>%d / %d</td><td class='rating'>%s</td><td class='season'>%d (%s)</td><td class='count'>%s</td>", $format, $style, $i + 1, $table->get_name(), $table->get_watched(), $table->get_totaleps(), $table->get_rating(), $table->get_year(), $table->get_season(), $table->get_count());
  306.  
  307.  
  308.  
  309.  
  310.     if (check_login()) {   // If valid session, we also add the row with the actions available to manage it
  311.  
  312.     $format = sprintf('%s <td class="action">', $format);    
  313.  
  314.             if ($param == 0) {   // if watching, we give the option to update list after watching an episode
  315.             $format = sprintf('%s <a title="Increment episode by one" href="javascript:void(0)" onclick="increment_entry(%d)">Ep +1</a> | ', $format, $table->get_id());
  316.             }
  317.  
  318.         // We add delete links
  319.  
  320.             $format = sprintf('%s <a title="Delete?" href="javascript:void(0)" onclick="delete_entry(%d, \'%s\')">Del</a></td></tr>', $format, $table->get_id(), $table->get_name());
  321.        
  322.     }  else {  // if not, we just close the row
  323.         $format = $format . '</tr>';
  324.         }
  325.  
  326.        
  327.  
  328.     unset($table);
  329.  
  330.     } /* end for */
  331.  
  332.     unset($db);
  333.  
  334.      /* end else */
  335.  
  336.     $format = sprintf('%s </table>', $format); // we close the table
  337.  
  338.     return $format ;
  339. }
  340.  
  341.  
  342.  
  343.  
  344.  
  345. class statistics {  //statistics for the profile
  346.  
  347. private $sum2, $sum;
  348.  
  349.     function get_data($param) {     // we prepare to get the data depending on parameter supplied. Each of the queries matches one of the predefined actions in the class connect
  350.     switch ($param) {
  351.     case 1:
  352.     $query = 'count1';
  353.     break;
  354.  
  355.     case 2:
  356.     $query = 'count2';
  357.     break;
  358.  
  359.     case 3:
  360.     $query = 'total_series';
  361.     break;
  362.     }
  363.    
  364.     $this->db = new connect();
  365.     return $this->db->retrieve($query);
  366.     }
  367.  
  368.  
  369.  
  370.     function get_totalseries() {
  371.     $this->result = $this->get_data(3);
  372.     return $this->result->num_rows;
  373.     }
  374.  
  375.  
  376.  
  377.     function prep_eps1() {          // we get the shows we've watched only once or 0 -> no maths needed
  378.     $this->result = $this->get_data(1);
  379.     while ($this->rows = $this->result->fetch_assoc()) {
  380.     $this->process = new anime($this->rows);
  381.     $this->sum = $this->sum + $this->process->get_watched();    // we only neeed to get watched episodes if we've only watched it once or 0 (= dropped)
  382.     unset ($this->process);
  383.     }
  384.     return $this->sum;
  385.     }
  386.  
  387.  
  388.  
  389.     function prep_eps2() {      // we get shows we've watched more than once
  390.     $this->result = $this->get_data(2);
  391.     while ($this->rows = $this->result->fetch_assoc()) {
  392.     $this->process = new anime($this->rows);
  393.  
  394.  /* The watched episodes can be either all of them (e.g. 12 out of 12) if not currently watching it, but it could also be that we're rewatching it and we've watched 5 out of 12, for example, so we can't just multiply total episodes * count. Afte rthis, we'd get the total episodes, and multiply it by total count - 1, since one of the counts is already implied in watched */
  395.  
  396.     $this->sum2 = $this->sum2 + $this->process->get_watched() + $this->process->get_totaleps() * ($this->process->get_count() - 1);
  397.     unset ($this->process);
  398.     }
  399.     return $this->sum2;
  400.     }
  401.  
  402.  
  403.     function get_total_episodes() {   // and now we sum the output from the two and get total episodes
  404.     return $this->prep_eps2() + $this->prep_eps1();
  405.  
  406.     }
  407. }
  408.  
  409.  
  410.  
  411. function check_login() {         // check if user is logged in, either through $_SESSION or cookies
  412.  
  413.   if (isset($_SESSION['user'])) {
  414.    return true;
  415.   }
  416.  
  417.  
  418.   else if (isset($_COOKIE['SAL_AUTH'])) {
  419.    
  420.     $cookie = $_COOKIE['SAL_AUTH'];
  421.  
  422.     $array_cookie = explode("[]", $cookie);
  423.     $user = $array_cookie[0];
  424.     $pass = $array_cookie[1];
  425.  
  426.      
  427.    
  428.      $query = sprintf('select * from auth where User = "%s" and Password = "%s"', $user, $pass);
  429.       $result = connect::query($query);
  430.      
  431.         if ($result->num_rows == 1) {
  432.           $_SESSION['user'] = $user;
  433.          return true;      
  434.         }
  435.         else { return false; }
  436.    
  437.     } // end else if
  438.  
  439.    else { return false; }
  440.  
  441. } // end function check_login
  442.  
  443.  
  444.  
  445. function redir_index() {  // redirects in 2 seconds to index.php using javascript (if possible use headers though)
  446. $redirect = '<script>setTimeout("to_index()", 2000)</script>';
  447. echo $redirect;
  448. }
  449.  
  450.  
  451. function login_form() {   // Display the login form  (??? should go back to html portion, the function doesn't make any sense)
  452.  
  453. ?>
  454.  
  455. <h3>User: Kirino<br />
  456. Password: imouto</h3>
  457.  
  458.  
  459. <form name="login" method="post" action="log_user.php">
  460. <table><tr>
  461. <td>User: </td><td><input type="text" name="user" /></td></tr>
  462. <tr><td>Password: </td><td><input type="password" name="pass" /></td></tr>
  463. <tr><td colspan="2"  style="text-align: center">
  464. <input type="submit" value="Log in" /></td></tr></table>
  465. </form>
  466.  
  467. <?php
  468. }
  469.  
  470.  
  471.  
  472. function get_error($errno) {       // return the error message based on $_GET['type'] when directed to error.php
  473.  
  474. switch($errno) {
  475. case 'nofound':
  476. return '<p>No records in database!<br />';
  477.  
  478. case 'connect':
  479. return '<p>There was an error connecting with the database or processing the data (' . $no . ') <br />';
  480.  
  481. case 'emptylogin':
  482. return '<p>You didn\'t provide your username or password. <a href="javascript:void(0)" onclick="history.back()">Go back?</a>';
  483.  
  484. case 'errorlogin':
  485. return '<p>Your username and password didn\'t match. <a href="javascript:void(0)" onclick="history.back()">Go back?</a>';
  486.  
  487. default:
  488. return '<p>There was an error<br />';
  489.  
  490. }
  491. }
  492. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement