<commit_before><!DOCTYPE html>
<?php
include("dbconfig.php");
session_start();

if (isset($_SESSION['login_user'])) {
    $user_email = $_SESSION['login_user'];
} else {
    $user_email = "error";
}

$load_accountID = "SELECT accountID FROM Account WHERE email_address = '$user_email'";
$user_accountID = mysqli_fetch_assoc(mysqli_query($conn, $load_accountID))['accountID'];

$collectionID = $_GET['collectionID'];

//Edit name or description of collection
if (isset($_POST['title']) && isset($_POST['description'])) {
    $newName = $_POST['title'];
    $newDescription = $_POST['description'];

    $editQuery = "UPDATE Collection
                            SET name='$newName',
                                description='$newDescription'
                                WHERE collectionID='$collectionID'";
    mysqli_query($conn, $editQuery);
}

//Add new photo to collection
if (isset($_POST['upload'])) {
    $imgFile = $_FILES['new_image']['name'];
    $tmp_dir = $_FILES['new_image']['tmp_name'];
    $imgSize = $_FILES['new_image']['size'];

    if ($imgFile) {
        $upload_dir = 'images/'; // upload directory	
        $imgExt = strtolower(pathinfo($imgFile, PATHINFO_EXTENSION)); // get image extension
        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
        // rename uploading image
        $userpic = rand(1000, 1000000) . $imgFile;

        // allow valid image file formats
        if (in_array($imgExt, $valid_extensions)) {
            // Check file size '5MB'
            if ($imgSize < 5000000) {
                //unlink($upload_dir . $edit_row['userPic']);
                move_uploaded_file($tmp_dir, $upload_dir . $userpic);
            } else {
                $errMSG = "Sorry, your file is too large.";
            }
        } else {
            $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        }
    } else {
        $errMSG = "Sorry, no file was selected.";
    }
    // if no error occured, continue ....
    if (!isset($errMSG)) {
        // insert photo into database
        $uploadQuery = "INSERT INTO BlogPhoto (accountID, isPhoto, image, text, collectionID) " .
                "VALUES ( '$user_accountID', '1', '$userpic', '$imgFile', '$collectionID')";
        $result = mysqli_query($conn, $uploadQuery)
                or die('Error making saveToDatabase query' . mysql_error());
    }
}

//Remove photo from collection
if (isset($_REQUEST["delete"])) {
    $delete = $_REQUEST["delete"];

    $delete_query = "DELETE FROM BlogPhoto WHERE bpID = $delete";

    $delete_result = mysqli_query($conn, $delete_query)
            or die('Error making deletion query' . mysql_error());
}

//Add or remove circle permission to collection
if (isset($_POST['grantcircle'])) {
    $grantCircleID = $_POST['grantcircle'];
    //if access right exist, remove it
    $searchQuery = "SELECT * FROM CircleAccessRight WHERE collectionID = $collectionID AND circleID = $grantCircleID";
    $result = mysqli_query($conn, $searchQuery)
            or die('Error making insert comments query' . mysql_error());
    $existingAccess = mysqli_fetch_array($result);
    if ($existingAccess) {
        echo 'had access';
        $removeQuery = "DELETE FROM CircleAccessRight WHERE collectionID = $collectionID AND circleID = $grantCircleID";
        $result = mysqli_query($conn, $removeQuery)
                or die('Error making delete access right query' . mysql_error());
    } else {
        $insertQuery = "INSERT INTO CircleAccessRight VALUES ( '$collectionID', '$grantCircleID')";
        $result = mysqli_query($conn, $insertQuery)
                or die('Error making insert annotation query' . mysql_error());
    }
}

// load current access rights for circles
$rightCircles = array();
$rightCircleQuery = mysqli_query($conn, "select FriendCircle.circleID, nameOfCircle from CircleAccessRight INNER JOIN FriendCircle ON CircleAccessRight.circleID = FriendCircle.circleID WHERE collectionID = ('$collectionID')");
$k = 0;
while ($row = mysqli_fetch_array($rightCircleQuery)) {
    $rightCircles[$k] = $row;
    $k = $k + 1;
}

// load all circles
$circles = array();
$circleQuery = mysqli_query($conn, "select FriendCircle.circleID, nameOfCircle from CircleMembership INNER JOIN FriendCircle ON CircleMembership.circleID = FriendCircle.circleID WHERE CircleMembership.accountID = ('$user_accountID')");
$k = 0;
while ($row = mysqli_fetch_array($circleQuery)) {
    $circles[$k] = $row;
    $k = $k + 1;
}

// remove individual access right
if (isset($_POST['removeInd'])) {
    $removeIndID = $_POST['removeInd'];
    echo $removeIndID;
    $removeQuery = "DELETE FROM FriendAccessRight WHERE collectionID = $collectionID AND accountID = $removeIndID";
    $result = mysqli_query($conn, $removeQuery)
            or die('Error making delete access right query' . mysql_error());
}

// add individual access right
if (isset($_POST['grantInd'])) {
    $grantIndID = $_POST['grantInd'];
    echo $grantIndID;
    $insertQuery = "INSERT INTO FriendAccessRight VALUES ( '$collectionID', '$grantIndID')";
    $result = mysqli_query($conn, $insertQuery)
            or die('Error making insert access right query' . mysql_error());
}

function getFriends($accountID, $conn) {
    $friends = array();
    $friendsQuery = "SELECT Account.accountID, name FROM Account WHERE "
            . "(Account.accountID in (SELECT Friendship.friend1ID FROM Friendship WHERE Friendship.friend2ID = '$accountID')) OR "
            . "(Account.accountID in (SELECT Friendship.friend2ID FROM Friendship WHERE Friendship.friend1ID = '$accountID'))";
    $friendsQueryResult = mysqli_query($conn, $friendsQuery);
    $k = 0;
    while ($row = mysqli_fetch_array($friendsQueryResult)) {
        $friends[$k] = $row;
        $k = $k + 1;
    }
    return $friends;
}

// get user's friends
$friends = getFriends($user_accountID, $conn);

// load current access rights for individuals
$rightPeople = array();
$rightIDs = array();
$rightPeopleQuery = mysqli_query($conn, "select Account.accountID, name from FriendAccessRight INNER JOIN Account ON FriendAccessRight.accountID = Account.accountID WHERE collectionID = ('$collectionID')");
$k = 0;
while ($row = mysqli_fetch_array($rightPeopleQuery)) {
    $rightPeople[$k] = $row;
    $rightIDs[$k] = $row[0];
    $k = $k + 1;
}
echo count($rightPeople);

function displayFriendsAndFriends($friends, $conn, $user_accountID, $rightIDs) {
    for ($x = 0; $x < count($friends); $x++) {
        $Friend = $friends[$x];
        if (!in_array($Friend[0], $rightIDs)) {
            echo "<option value = \"$Friend[0]\" >$Friend[1]</option>";
        } else {
            echo "<optgroup label = \"$Friend[1]\">";
        }
        $friendsOfFriend = getFriends($Friend[0], $conn);
        for ($y = 0; $y < count($friendsOfFriend); $y++) {
            $FFriend = $friendsOfFriend[$y];
            // only display if not already with permission
            if ($FFriend[0] != $user_accountID && !in_array($FFriend[0], $rightIDs)) {
                echo "<option value = \"$FFriend[0]\" >&nbsp;&nbsp;&nbsp;$FFriend[1]</option>";<commit_msg>allow photo to be removed from folder<commit_after><!DOCTYPE html>
<?php
include("dbconfig.php");
session_start();

if (isset($_SESSION['login_user'])) {
    $user_email = $_SESSION['login_user'];
} else {
    $user_email = "error";
}

$load_accountID = "SELECT accountID FROM Account WHERE email_address = '$user_email'";
$user_accountID = mysqli_fetch_assoc(mysqli_query($conn, $load_accountID))['accountID'];

$collectionID = $_GET['collectionID'];

//Edit name or description of collection
if (isset($_POST['title']) && isset($_POST['description'])) {
    $newName = $_POST['title'];
    $newDescription = $_POST['description'];

    $editQuery = "UPDATE Collection
                            SET name='$newName',
                                description='$newDescription'
                                WHERE collectionID='$collectionID'";
    mysqli_query($conn, $editQuery);
}

//Add new photo to collection
if (isset($_POST['upload'])) {
    $imgFile = $_FILES['new_image']['name'];
    $tmp_dir = $_FILES['new_image']['tmp_name'];
    $imgSize = $_FILES['new_image']['size'];

    if ($imgFile) {
        $upload_dir = 'images/'; // upload directory	
        $imgExt = strtolower(pathinfo($imgFile, PATHINFO_EXTENSION)); // get image extension
        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
        // rename uploading image
        $userpic = rand(1000, 1000000) . $imgFile;

        // allow valid image file formats
        if (in_array($imgExt, $valid_extensions)) {
            // Check file size '5MB'
            if ($imgSize < 5000000) {
                //unlink($upload_dir . $edit_row['userPic']);
                move_uploaded_file($tmp_dir, $upload_dir . $userpic);
            } else {
                $errMSG = "Sorry, your file is too large.";
            }
        } else {
            $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        }
    } else {
        $errMSG = "Sorry, no file was selected.";
    }
    // if no error occured, continue ....
    if (!isset($errMSG)) {
        // insert photo into database
        $uploadQuery = "INSERT INTO BlogPhoto (accountID, isPhoto, image, text, collectionID) " .
                "VALUES ( '$user_accountID', '1', '$userpic', '$imgFile', '$collectionID')";
        $result = mysqli_query($conn, $uploadQuery)
                or die('Error making saveToDatabase query' . mysql_error());
    }
}

//Remove photo from collection
if (isset($_REQUEST["delete"])) {
    $delete = $_REQUEST["delete"];

    // First need information to remove from folder
    $load_query = "SELECT accountID, image, text, timestamp, bpID FROM BlogPhoto WHERE bpID = $delete";
    $load_result = mysqli_query($conn, $load_query)
            or die('Error making deletion query' . mysql_error());
    $to_delete = mysqli_fetch_array($load_result);

    // Remove from database
    $delete_query = "DELETE FROM BlogPhoto WHERE bpID = $delete";

    $delete_result = mysqli_query($conn, $delete_query)
            or die('Error making deletion query' . mysql_error());
    // Then remove from folder
    unlink("images/$to_delete[1]");
}

//Add or remove circle permission to collection
if (isset($_POST['grantcircle'])) {
    $grantCircleID = $_POST['grantcircle'];
    //if access right exist, remove it
    $searchQuery = "SELECT * FROM CircleAccessRight WHERE collectionID = $collectionID AND circleID = $grantCircleID";
    $result = mysqli_query($conn, $searchQuery)
            or die('Error making insert comments query' . mysql_error());
    $existingAccess = mysqli_fetch_array($result);
    if ($existingAccess) {
        echo 'had access';
        $removeQuery = "DELETE FROM CircleAccessRight WHERE collectionID = $collectionID AND circleID = $grantCircleID";
        $result = mysqli_query($conn, $removeQuery)
                or die('Error making delete access right query' . mysql_error());
    } else {
        $insertQuery = "INSERT INTO CircleAccessRight VALUES ( '$collectionID', '$grantCircleID')";
        $result = mysqli_query($conn, $insertQuery)
                or die('Error making insert annotation query' . mysql_error());
    }
}

// load current access rights for circles
$rightCircles = array();
$rightCircleQuery = mysqli_query($conn, "select FriendCircle.circleID, nameOfCircle from CircleAccessRight INNER JOIN FriendCircle ON CircleAccessRight.circleID = FriendCircle.circleID WHERE collectionID = ('$collectionID')");
$k = 0;
while ($row = mysqli_fetch_array($rightCircleQuery)) {
    $rightCircles[$k] = $row;
    $k = $k + 1;
}

// load all circles
$circles = array();
$circleQuery = mysqli_query($conn, "select FriendCircle.circleID, nameOfCircle from CircleMembership INNER JOIN FriendCircle ON CircleMembership.circleID = FriendCircle.circleID WHERE CircleMembership.accountID = ('$user_accountID')");
$k = 0;
while ($row = mysqli_fetch_array($circleQuery)) {
    $circles[$k] = $row;
    $k = $k + 1;
}

// remove individual access right
if (isset($_POST['removeInd'])) {
    $removeIndID = $_POST['removeInd'];
    echo $removeIndID;
    $removeQuery = "DELETE FROM FriendAccessRight WHERE collectionID = $collectionID AND accountID = $removeIndID";
    $result = mysqli_query($conn, $removeQuery)
            or die('Error making delete access right query' . mysql_error());
}

// add individual access right
if (isset($_POST['grantInd'])) {
    $grantIndID = $_POST['grantInd'];
    echo $grantIndID;
    $insertQuery = "INSERT INTO FriendAccessRight VALUES ( '$collectionID', '$grantIndID')";
    $result = mysqli_query($conn, $insertQuery)
            or die('Error making insert access right query' . mysql_error());
}

// function to get all friends of an individual
function getFriends($accountID, $conn) {
    $friends = array();
    $friendsQuery = "SELECT Account.accountID, name FROM Account WHERE "
            . "(Account.accountID in (SELECT Friendship.friend1ID FROM Friendship WHERE Friendship.friend2ID = '$accountID')) OR "
            . "(Account.accountID in (SELECT Friendship.friend2ID FROM Friendship WHERE Friendship.friend1ID = '$accountID'))";
    $friendsQueryResult = mysqli_query($conn, $friendsQuery);
    $k = 0;
    while ($row = mysqli_fetch_array($friendsQueryResult)) {
        $friends[$k] = $row;
        $k = $k + 1;
    }
    return $friends;
}

// get user's friends
$friends = getFriends($user_accountID, $conn);

// load current access rights for individuals
$rightPeople = array();
$rightIDs = array();
$rightPeopleQuery = mysqli_query($conn, "select Account.accountID, name from FriendAccessRight INNER JOIN Account ON FriendAccessRight.accountID = Account.accountID WHERE collectionID = ('$collectionID')");
$k = 0;
while ($row = mysqli_fetch_array($rightPeopleQuery)) {
    $rightPeople[$k] = $row;
    $rightIDs[$k] = $row[0];
    $k = $k + 1;
}
echo count($rightPeople);

// Show friends and their friends in drop down menu
function displayFriendsAndFriends($friends, $conn, $user_accountID, $rightIDs) {
    for ($x = 0; $x < count($friends); $x++) {
        $Friend = $friends[$x];
        if (!in_array($Friend[0], $rightIDs)) {
            echo "<option value = \"$Friend[0]\" >$Friend[1]</option>";
        } else {
            echo "<optgroup label = \"$Friend[1]\">";
        }
        $friendsOfFriend = getFriends($Friend[0], $conn);
        for ($y = 0; $y < count($friendsOfFriend); $y++) {
            $FFriend = $friendsOfFriend[$y];
            // only display if not already with permission
            if ($FFriend[0] != $user_accountID && !in_array($FFriend[0], $rightIDs)) {
                echo "<option value = \"$FFriend[0]\" >&nbsp;&nbsp;&nbsp;$FFriend[1]</option>";
{'fraction_non_alphanumeric': 0.14554402049346096, 'fraction_numerical': 0.005999730349197789, 'mean_word_length': 3.311246730601569, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4395520', 'n_tokens_mistral': 4529, 'n_tokens_neox': 4310, 'n_words': 1150}