Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.01 KB | None | 0 0
  1. <?php
  2.     include("connection.php");
  3.    
  4.     error_reporting(E_ALL);
  5.    
  6.     $con = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE);
  7.    
  8.     $langFile = file_get_contents($_SERVER["DOCUMENT_ROOT"]."/lang/en.json");
  9.     $lang = json_decode($langFile, true);
  10.    
  11.     function testInclude() {
  12.         echo "loaded";
  13.     }
  14.    
  15.     // Require the page to be logged in
  16.     function requireLogin() {
  17.         global $currentPage;
  18.         if(!isLoggedIn()) {
  19.             redirect("login?redirect=".$currentPage);
  20.         }      
  21.     }
  22.    
  23.     // Require the page not to be logged in
  24.     function requireLogout() {
  25.         if(isLoggedIn()) {
  26.             redirect("index");
  27.         }  
  28.     }
  29.    
  30.     // Require the page to be logged in with at least the given level
  31.     function requireLevel($level) {
  32.         global $currentPage;
  33.         if(isLoggedIn()) {
  34.             $id = getLogin();
  35.             if(getLevelById($id) <  $level) {
  36.                 redirect("not_authorized", 401);
  37.             }
  38.         }
  39.         else {
  40.             redirect("login?redirect=".$currentPage);
  41.         }  
  42.     }  
  43.  
  44.     // Redirect the user to the given url
  45.     function redirect($url, $statusCode = 303) {
  46.         if(substr($url, 0, 4) == "http") {
  47.             header("Location:".$url, true, $statusCode);
  48.             die();
  49.         }  
  50.         else {
  51.             header("Location: https://dupbit.com/".$url, true, $statusCode);
  52.             die();         
  53.         }
  54.     }
  55.    
  56.     // Redirect the user to the previous page
  57.     function backdirect() {
  58.         if (isset($_SERVER["HTTP_REFERER"])) {
  59.             redirect($_SERVER["HTTP_REFERER"]);
  60.         }
  61.         else {
  62.             redirect("index");
  63.         }  
  64.     }
  65.  
  66.     // Return string without illegal chars for filename
  67.     function filename($string) {
  68.         return preg_replace('/[\\\\\/:*?"<>|]/', '', $string);
  69.     }  
  70.    
  71.     // Register a user with the given username, password, email and level
  72.     function register($username, $password, $email, $level = 0) {
  73.         global $con;
  74.  
  75.         $options = [
  76.             'cost' => 10,
  77.         ];
  78.  
  79.         $username = mysqli_real_escape_string($con, $username);
  80.         $email = mysqli_real_escape_string($con, $email);
  81.         $password = password_hash($password, PASSWORD_BCRYPT, $options);
  82.         $emailhash = password_hash($password, PASSWORD_BCRYPT, $options);
  83.         mysqli_query($con, "INSERT INTO users (username, password, email, level) VALUES ('$username', '$password', '$email', '$level')");
  84.  
  85.         sendMail($email, getIDByUsername($username), $username, $emailhash);
  86.     }
  87.    
  88.     function sendMail($email, $id, $username, $hash){
  89.         $to = $email;
  90.         $subject = "Welcome to Dupbit! Confirm your email " . $username . "!";
  91.         $message = '
  92.         <!DOCTYPE html>
  93.         <html lang="en"
  94.         <html>
  95.         <head>
  96.         <title>Confirm Email</title>
  97.         </head>
  98.         <body>
  99.         <a href=https://dupbit.com/action/validate.php?id=' . $id . '&hash=' . $hash . '>Activate account</a>
  100.         </body>
  101.         </html>
  102.         ';
  103.  
  104.         $headers = "MIME-Version: 1.0" . "\r\n";
  105.         $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  106.         $headers .= 'From: Dupbit <noreply@dupbit.com>' . "\r\n";
  107.  
  108.         mail($to,$subject,$message,$headers);
  109.     }
  110.  
  111.     function confirmChangesMail($email, $id, $username, $hash){
  112.         $to = $email;
  113.         $subject = "Please confirm these changes to your account";
  114.         $message = '
  115.         <!DOCTYPE html>
  116.         <html lang="en">
  117.         <head>
  118.         <title> Confirm account update </title>
  119.         </head>
  120.         <body>
  121.         <a href=https://dupbit.com/action/validate.php?id=' . $id . '&hash=' . $hash . '> Confirm changes</a>
  122.         </body>
  123.         </html>
  124.         ';
  125.  
  126.         $headers = "MIME-Version: 1.0" . "\r\n";
  127.         $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  128.         $headers .= 'From: Dupbit <noreply@dupbit.com>' . "\r\n";
  129.  
  130.         mail($to, $subject, $message, $headers);       
  131.     }
  132.  
  133.     function recoverAccount(){
  134.  
  135.     }
  136.  
  137.     function sendEmail2($email, $subject, $message){
  138.         $headers = "MIME-Version: 1.0" . "\r\n";
  139.         $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
  140.         $headers .= 'From: Dupbit <noreply@dupbit.com>' . "\r\n";      
  141.     }
  142.  
  143.     // Unregister a user with given id
  144.     function unregister($id) {
  145.         global $con;
  146.         mysqli_query($con, "DELETE FROM users WHERE id = '$id'");      
  147.     }
  148.    
  149.     // Verify if the given username, password and email make a valid user instance
  150.     function verifyRegistration($username, $password, $confirmpassword, $email) {
  151.         $errorCode = 0;
  152.         $errorCode += verifyUsername($username);
  153.         $errorCode += verifyPassword($password);
  154.         $errorCode += verifyPasswordMatch($password, $confirmpassword);
  155.         $errorCode += verifyEmail($email);
  156.         return $errorCode; 
  157.     }
  158.    
  159.     // Verify if the given username is valid for registration
  160.     function verifyUsername($username) {
  161.         $errorCode = 0;
  162.         if (isRegistered($username)) {
  163.             $errorCode += pow(2,0);
  164.         }
  165.         if (strlen($username) < 3) {
  166.             $errorCode += pow(2,1);
  167.         }
  168.         if (strlen($username) > 20) {
  169.             $errorCode += pow(2,2);
  170.         }  
  171.         if (!verifyUsernameChars($username)) {
  172.             $errorCode += pow(2,3);
  173.         }
  174.         return $errorCode;     
  175.     }
  176.  
  177.     // Verify if the given username is valid for registration
  178.     function verifyPassword($password) {
  179.         $errorCode = 0;
  180.         if (strlen($password) < 8) {
  181.             $errorCode += pow(2,4);
  182.         }
  183.         if (strlen($password) > 30) {
  184.             $errorCode += pow(2,5);
  185.         }  
  186.         if (!verifyPasswordChars($password)) {
  187.             $errorCode += pow(2,6);
  188.         }  
  189.         return $errorCode;     
  190.     }
  191.    
  192.     // Check if passwords match
  193.     function verifyPasswordMatch($password, $confirmpassword) {
  194.         $errorCode = 0;
  195.         if ($password !== $confirmpassword) {
  196.             $errorCode += pow(2,7);
  197.         }  
  198.         return $errorCode;
  199.     }  
  200.  
  201.     // Verify if the email is valid
  202.     function verifyEmail($email) {
  203.         $errorCode = 0;
  204.         if (isInUse($email)) {
  205.           $errorCode += pow(2,8);
  206.         }      
  207.         if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  208.           $errorCode += pow(2,9);
  209.         }
  210.         return $errorCode; 
  211.     }
  212.    
  213.     // Verify string for valid chars
  214.     function verifyUsernameChars($string) {
  215.         return !preg_match('/[^A-Za-z0-9._-]/', $string);
  216.     }
  217.    
  218.     // Verify string for valid chars
  219.     function verifyPasswordChars($string) {
  220.         return !preg_match('/[^A-Za-z0-9!"#$%&\'()*+,-.\/:;<=>?@[\]^_`{|}~]/', $string);
  221.     }  
  222.  
  223.     // Get the error message of this errorCode 
  224.     function getErrorMessage($errorCode) {
  225.         global $lang;
  226.         switch($errorCode) {
  227.             case 0:
  228.                 $errorMessage = $lang["username.availability"];
  229.                 break;
  230.             case 1:
  231.                 $errorMessage = $lang["username.tooshort"];
  232.                 break;
  233.             case 2:
  234.                 $errorMessage = $lang["username.toolong"];
  235.                 break;
  236.             case 3:
  237.                 $errorMessage = $lang["username.invalidchars"];
  238.                 break;
  239.             case 4:
  240.                 $errorMessage = $lang["password.tooshort"];
  241.                 break;
  242.             case 5:
  243.                 $errorMessage = $lang["password.toolong"];
  244.                 break;
  245.             case 6:
  246.                 $errorMessage = $lang["password.invalidchars"];
  247.                 break;
  248.             case 7:
  249.                 $errorMessage = $lang["password.match"];
  250.                 break;
  251.             case 8:
  252.                 $errorMessage = $lang["email.availability"];
  253.                 break;
  254.             case 9:
  255.                 $errorMessage = $lang["email.format"]; 
  256.                 break;         
  257.         }
  258.         return $errorMessage;
  259.     }
  260.    
  261.     // Decode errorCode
  262.     function decodeErrorCode($errorCode) {
  263.         $errorMessageList = array();
  264.         $binErrorCode = decbin($errorCode);
  265.         $n = strlen($binErrorCode);
  266.         for ($i = 1; $i <= $n; $i++) {
  267.             if ($binErrorCode[$n - $i] == "1") {
  268.                 array_push($errorMessageList, getErrorMessage($i-1));
  269.             }
  270.         }
  271.         return $errorMessageList;
  272.     }  
  273.    
  274.     // Check if the given username is registered   
  275.     function isRegistered($username) {
  276.         global $con;
  277.         $username = mysqli_real_escape_string($con, $username);
  278.         $query = mysqli_query($con, "SELECT username FROM users WHERE username = '$username'");
  279.         return mysqli_num_rows($query) == 1;
  280.     }
  281.  
  282.     // Check if the given email is in use  
  283.     function isInUse($email) {
  284.         global $con;
  285.         $email = mysqli_real_escape_string($con, $email);
  286.         $query = mysqli_query($con, "SELECT email FROM users WHERE email = '$email'");
  287.         return mysqli_num_rows($query) == 1;
  288.     }
  289.  
  290.     // Make a login session for the given id
  291.     function login($uid) {
  292.         session_start();
  293.         $_SESSION["login"] = $uid; 
  294.         session_write_close();
  295.     }
  296.    
  297.     // Check if the user is logged in
  298.     function isLoggedIn() {
  299.         session_start();
  300.         $login = isset($_SESSION["login"]);
  301.         session_write_close();
  302.         return $login;
  303.     }
  304.    
  305.     // Return the id of the current login session
  306.     function getLogin() {
  307.         if (isLoggedIn()) {
  308.             session_start();
  309.             $login = $_SESSION["login"];
  310.             session_write_close();
  311.         }
  312.         else {
  313.             $login = null;
  314.         }
  315.         return $login; 
  316.     }
  317.    
  318.     // Verify if the given username and password make a valid login
  319.     function verifyLogin($username, $password) {
  320.         global $con;
  321.         $username = mysqli_real_escape_string($con, $username);
  322.         $id = getIDByUsername($username);
  323.         $valid = password_verify($password, getPasswordByID($id));
  324.         return $valid;
  325.     }
  326.    
  327.     // Destroy the current login session
  328.     function logout() {
  329.         session_start();
  330.         if (isset($_SESSION["login"])) {
  331.             unset($_SESSION["login"]);
  332.         }
  333.         session_write_close(); 
  334.     }
  335.  
  336.     function validate($id, $emailhash){
  337.         $password = getPasswordByID($id);
  338.         $notActivated = (getLevelByID($id) == 0);
  339.  
  340.         if (password_verify($password, $emailhash) and $notActivated) {
  341.             setLevel($id, 1);
  342.             login($id);
  343.             redirect("index");
  344.         } else {
  345.             redirect("not_authorized", 401);
  346.         }      
  347.     }
  348.  
  349.     // Return the id of the user with given username   
  350.     function getIDByUsername($username) {
  351.         global $con;
  352.         $username = mysqli_real_escape_string($con, $username);
  353.         $query = mysqli_query($con, "SELECT id FROM users WHERE username = '$username'");
  354.         if(mysqli_num_rows($query) == 1) {         
  355.             $row = mysqli_fetch_array($query);
  356.             $id = intval($row["id"]);
  357.         }
  358.         else {
  359.             $id = null;
  360.         }
  361.         return $id;
  362.     }
  363.  
  364.     // Return the username of the user with given id
  365.     function getUsernameByID($id) {
  366.         global $con;
  367.         $query = mysqli_query($con, "SELECT username FROM users WHERE id = '$id'");
  368.         if(mysqli_num_rows($query) == 1) {
  369.             $row = mysqli_fetch_array($query);
  370.             $username = $row["username"];
  371.         }
  372.         else {
  373.             $username = null;
  374.         }
  375.         return $username;
  376.     }
  377.    
  378.     // Set the username of the user with the given id to the given username
  379.     function setUsername($id, $username) {
  380.         global $con;
  381.         $username = mysqli_real_escape_string($con, $username);
  382.         echo $username;
  383.         mysqli_query($con, "UPDATE users SET username = '$username' WHERE id = '$id'");    
  384.     }
  385.    
  386.     // Return the password of the user with given id
  387.     function getPasswordByID($id) {
  388.         global $con;
  389.         $query = mysqli_query($con, "SELECT password FROM users WHERE id = '$id'");
  390.         if(mysqli_num_rows($query) == 1) {
  391.             $row = mysqli_fetch_array($query);
  392.             $password = $row["password"];
  393.         }
  394.         else {
  395.             $password = null;
  396.         }
  397.         return $password;
  398.     }
  399.    
  400.     // Set the password of the user with the given id to the given username
  401.     function setPassword($id, $password) {
  402.         global $con;
  403.         $password = password_hash($password, PASSWORD_BCRYPT);
  404.         mysqli_query($con, "UPDATE users SET password = '$password' WHERE id = '$id'");    
  405.     }  
  406.    
  407.     // Return the email of the user with given id
  408.     function getEmailByID($id) {
  409.         global $con;
  410.         $query = mysqli_query($con, "SELECT email FROM users WHERE id = '$id'");
  411.         if(mysqli_num_rows($query) == 1) {
  412.             $row = mysqli_fetch_array($query);
  413.             $email = $row["email"];
  414.         }
  415.         else {
  416.             $email = null;
  417.         }
  418.         return $email;
  419.     }
  420.    
  421.     // Set the email of the user with the given id to the given username
  422.     function setEmail($id, $email) {
  423.         global $con;
  424.         $email = mysqli_real_escape_string($con, $email);
  425.         mysqli_query($con, "UPDATE users SET email = '$email' WHERE id = '$id'");      
  426.     }  
  427.        
  428.     // Return the level of the user with given id  
  429.     function getLevelByID($id) {
  430.         global $con;
  431.         $query = mysqli_query($con, "SELECT level FROM users WHERE id = '$id'");
  432.         if(mysqli_num_rows($query) == 1) {
  433.             $row = mysqli_fetch_array($query);
  434.             $level = intval($row["level"]);
  435.         }
  436.         else {
  437.             $level = null;
  438.         }
  439.         return $level;
  440.     }
  441.    
  442.     // Get all users
  443.     function getUsers() {
  444.         global $con;
  445.         $query = mysqli_query($con, "SELECT * FROM users");
  446.         $data = array();
  447.         $i = 0;
  448.         while($row = mysqli_fetch_assoc($query)) {
  449.             $data[$i]["id"] = $row["id"];
  450.             $data[$i]["username"] = $row["username"];
  451.             $data[$i]["password"] = $row["password"];
  452.             $data[$i]["email"] = $row["email"];
  453.             $data[$i]["level"] = $row["level"];
  454.             $data[$i]["registrationTimestamp"] = $row["registrationTimestamp"];
  455.             $i++;
  456.         }
  457.         return $data;
  458.     }
  459.    
  460.     // Set the level of the user with the given id to the given level
  461.     function setLevel($id, $level) {
  462.         global $con;
  463.         mysqli_query($con, "UPDATE users SET level = '$level' WHERE id = '$id'");      
  464.     }  
  465.  
  466.     // Register the client's IP and the current timestamp of login attempt with the given username
  467.     function addLoginAttempt($username, $success) {
  468.         global $con;
  469.         $ip = getIP();
  470.         $id = getIDByUsername($username);
  471.         if($id == null) {
  472.             mysqli_query($con, "INSERT INTO loginAttempts (username, uid, ip, success) VALUES ('$username', NULL, '$ip', '$success')");
  473.         }
  474.         else {
  475.             mysqli_query($con, "INSERT INTO loginAttempts (username, uid, ip, success) VALUES ('$username', '$id', '$ip', '$success')");
  476.         }  
  477.     }  
  478.  
  479.     // Get all login attempts
  480.     function getLoginAttempts() {
  481.         global $con;
  482.         $query = mysqli_query($con, "SELECT * FROM loginAttempts ORDER BY Timestamp DESC");
  483.         $data = array();
  484.         $i = 0;
  485.         while($row = mysqli_fetch_assoc($query)) {
  486.             $data[$i]["uid"] = $row["uid"];
  487.             $data[$i]["username"] = $row["username"];
  488.             $data[$i]["ip"] = $row["ip"];
  489.             $data[$i]["success"] = $row["success"];
  490.             $data[$i]["timestamp"] = $row["timestamp"];
  491.             $i++;
  492.         }
  493.         return $data;
  494.     }
  495.    
  496.     // Return client's IP address
  497.     function getIP() {
  498.         if (getenv('HTTP_CLIENT_IP'))
  499.             $ipaddress = getenv('HTTP_CLIENT_IP');
  500.         else if(getenv('HTTP_X_FORWARDED_FOR'))
  501.             $ipaddress = getenv('HTTP_X_FORWARDED_FOR');
  502.         else if(getenv('HTTP_X_FORWARDED'))
  503.             $ipaddress = getenv('HTTP_X_FORWARDED');
  504.         else if(getenv('HTTP_FORWARDED_FOR'))
  505.             $ipaddress = getenv('HTTP_FORWARDED_FOR');
  506.         else if(getenv('HTTP_FORWARDED'))
  507.             $ipaddress = getenv('HTTP_FORWARDED');
  508.         else if(getenv('REMOTE_ADDR'))
  509.             $ipaddress = getenv('REMOTE_ADDR');
  510.         else
  511.             $ipaddress = 'UNKNOWN';
  512.         return $ipaddress; 
  513.     }  
  514.    
  515.     // Register a namechange to the given username of a user with given ID
  516.     function addUsernameChange($id, $username) {
  517.         global $con;
  518.         $username = mysqli_real_escape_string($con, $username);
  519.         mysqli_query($con, "INSERT INTO usernameChanges (uid, username) VALUES ('$id', '$username')");
  520.     }
  521.  
  522.     // Get all namechanges of a user with given id
  523.     function getUsernameChangeHistory($id) {
  524.         global $con;
  525.         $query = mysqli_query($con, "SELECT * FROM usernameChanges WHERE uid = '$id'");
  526.         $data = array();
  527.         $i = 0;
  528.         while($row = mysqli_fetch_assoc($query)) {
  529.             $data[$i]["uid"] = $row["uid"];
  530.             $data[$i]["username"] = $row["username"];
  531.             $data[$i]["timestamp"] = $row["timestamp"];
  532.             $i++;
  533.         }
  534.         return $data;
  535.     }  
  536.  
  537.     // Get latest namechange of a user with given id
  538.     function getLatestUsernameChange($id) {
  539.         global $con;
  540.         $query = mysqli_query($con, "SELECT * FROM usernameChanges WHERE uid = '$id' ORDER BY Timestamp DESC LIMIT 1");
  541.         $data = array();
  542.         while($row = mysqli_fetch_assoc($query)) {
  543.             $data["uid"] = $row["uid"];
  544.             $data["username"] = $row["username"];
  545.             $data["timestamp"] = $row["timestamp"];
  546.         }
  547.         return $data;
  548.     }  
  549.  
  550.     // Return if the user with given id can do a namechange
  551.     function canDoUsernameChange($id) {
  552.         $data = getLatestUsernameChange($id);
  553.         $old = new DateTime($data["timestamp"]);
  554.         $now = new DateTime();
  555.         $interval = $old->diff($now);
  556.         return ($interval->days >= 30);
  557.     }
  558.    
  559.     // Add a song with given title and artist
  560.     function addSong($ytid, $title, $artist, $uid) {
  561.         global $con;
  562.         $title = mysqli_real_escape_string($con, $title);
  563.         $artist = mysqli_real_escape_string($con, $artist);
  564.         mysqli_query($con, "INSERT INTO music.songs (ytid, title, artist, uid) VALUES ('$ytid', '$title', '$artist', '$uid')");
  565.         return mysqli_insert_id($con);
  566.     }
  567.    
  568.     // Remove a song with given id
  569.     function removeSong($id) {
  570.         global $con;
  571.         mysqli_query($con, "DELETE FROM music.songs WHERE id = '$id'");
  572.     }
  573.  
  574.     // Set the title of the song with given id to the given title
  575.     function setTitle($id, $title) {
  576.         global $con;
  577.         $title = mysqli_real_escape_string($con, $title);
  578.         mysqli_query($con, "UPDATE music.songs SET title = '$title' WHERE id = '$id'");    
  579.     }
  580.  
  581.     // Set the title of the song with given id to the given title
  582.     function setArtist($id, $artist) {
  583.         global $con;
  584.         $artist = mysqli_real_escape_string($con, $artist);
  585.         mysqli_query($con, "UPDATE music.songs SET artist = '$artist' WHERE id = '$id'");      
  586.     }          
  587.    
  588.     // Add a playlist with given name for the given user
  589.     function addPlaylist($name, $uid) {
  590.         global $con;
  591.         if($name == null) {
  592.             $name = "New Playlist";
  593.         }
  594.         $name = mysqli_real_escape_string($con, $name);    
  595.         mysqli_query($con, "INSERT INTO music.playlists (name, uid) VALUES ('$name', '$uid')");
  596.         return mysqli_insert_id($con);
  597.     }
  598.  
  599.     // Remove a playlist with given id
  600.     function removePlaylist($id) {
  601.         global $con;
  602.         mysqli_query($con, "DELETE FROM music.playlists WHERE id = '$id'");
  603.     }
  604.  
  605.     // Add song with given id to playlist with given id
  606.     function addSongToPlaylist($sid, $pid) {
  607.         global $con;   
  608.         mysqli_query($con, "INSERT INTO music.songInPlaylist (sid, pid) VALUES ('$sid', '$pid')");     
  609.     }
  610.  
  611.     // Remove song with given id from playlist with given id
  612.     function removeSongFromPlaylist($sid, $pid) {
  613.         global $con;   
  614.         mysqli_query($con, "DELETE FROM music.songInPlaylist WHERE sid = '$sid' AND pid = '$pid'");    
  615.     }
  616.  
  617.     // Get all songs of user with given id 
  618.     function getSongsOf($uid) {
  619.         global $con;
  620.         $query = mysqli_query($con, "SELECT * FROM music.songs WHERE uid = '$uid' ORDER BY artist, title");
  621.         $data = array();
  622.         $i = 0;
  623.         while($row = mysqli_fetch_assoc($query)) {         
  624.             $data[$i]["id"] = $row["id"];
  625.             $data[$i]["ytid"] = $row["ytid"];
  626.             $data[$i]["title"] = htmlentities($row["title"], ENT_QUOTES);
  627.             $data[$i]["artist"] = htmlentities($row["artist"], ENT_QUOTES);
  628.             $data[$i]["uid"] = $row["uid"];
  629.             $i++;
  630.         }
  631.         return $data;
  632.     }
  633.    
  634.     // Get owner of the song with given id
  635.     function getUserOfSong($sid) {
  636.         global $con;
  637.         $query = mysqli_query($con, "SELECT uid FROM music.songs WHERE id = '$sid'");
  638.         if(mysqli_num_rows($query) == 1) {
  639.             $row = mysqli_fetch_array($query);
  640.             $uid = $row["uid"];
  641.         }
  642.         else {
  643.             $uid = null;
  644.         }
  645.         return $uid;       
  646.     }
  647.    
  648.     // Get song
  649.     function getSong($sid) {
  650.         global $con;
  651.         $query = mysqli_query($con, "SELECT * FROM music.songs WHERE id = '$sid'");
  652.         if(mysqli_num_rows($query) == 1) {
  653.             $data = mysqli_fetch_array($query);
  654.         }
  655.         else {
  656.             $data = null;
  657.         }
  658.         return $data;      
  659.     }  
  660.  
  661.     // Get all playlist of user with given id  
  662.     function getPlaylistsOf($uid) {
  663.         global $con;
  664.         $query = mysqli_query($con, "SELECT * FROM music.playlists WHERE uid = '$uid' ORDER BY name");
  665.         $data = array();
  666.         $i = 0;
  667.         while($row = mysqli_fetch_assoc($query)) {         
  668.             $data[$i]["id"] = $row["id"];
  669.             $data[$i]["name"] = $row["name"];
  670.             $data[$i]["uid"] = $row["uid"];
  671.             $i++;
  672.         }
  673.         return $data;
  674.     }
  675.  
  676.     // Get owner of the playlist with given id
  677.     function getUserOfPlaylist($pid) {
  678.         global $con;
  679.         $query = mysqli_query($con, "SELECT uid FROM music.playlist WHERE id = '$pid'");
  680.         if(mysqli_num_rows($query) == 1) {
  681.             $row = mysqli_fetch_array($query);
  682.             $uid = $row["uid"];
  683.         }
  684.         else {
  685.             $uid = null;
  686.         }
  687.         return $uid;       
  688.     }  
  689.  
  690.     // Get all playlist of user with given id  
  691.     function getPlaylistsOfSong($sid) {
  692.         global $con;
  693.         $query = mysqli_query($con, "SELECT * FROM music.songInPlaylist JOIN music.playlists WHERE pid = id AND sid = '$sid' ORDER BY name");
  694.         $data = array();
  695.         $i = 0;
  696.         while($row = mysqli_fetch_assoc($query)) {         
  697.             $data[$i] = $row;
  698.             $i++;
  699.         }
  700.         return $data;
  701.     }  
  702.  
  703.     // Get all songs in playlist with given id 
  704.     function getSongsIn($pid) {
  705.         global $con;
  706.         $query = mysqli_query($con, "SELECT * FROM music.songInPlaylist JOIN music.songs WHERE sid = id AND pid = '$pid' ORDER BY artist, title");
  707.         $data = array();
  708.         $i = 0;
  709.         while($row = mysqli_fetch_assoc($query)) {         
  710.             $data[$i]["id"] = $row["id"];
  711.             $data[$i]["ytid"] = $row["ytid"];
  712.             $data[$i]["title"] = htmlentities($row["title"], ENT_QUOTES);
  713.             $data[$i]["artist"] = htmlentities($row["artist"], ENT_QUOTES);
  714.             $data[$i]["uid"] = $row["uid"];
  715.             $i++;
  716.         }
  717.         return $data;
  718.     }
  719. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement