Beispiel #1
0
<?php

/* loadcards.php - Loads $username's cards $cardlist into the array $cards.
 * Also updates information about card weights. */
require_once 'eatcsv.php';
if (!(isset($username) && isset($cardlist))) {
    die("Tried to open cardlist <tt>{$cardlist}</tt> of user <tt>{$username}</tt>, and failed.");
}
$cardlist_filename = 'cardlists/' . $username . '/' . $cardlist;
$cards = eatcsv($cardlist_filename) or die("Couldn't open file {$cardlist_filename}.");
/* Weight the cards, and choose a random card. Cards are given more weight if
 * the user has got them wrong very often, and if the card hasn't come up
 * in a while. */
$weights = array_map(function ($card) {
    /* Work out the proportion of times we have got the card right. If it
     * has never been tested before, then arbitrarily say that the
     * proportion is 0.5. */
    $propright = $card['correct'] + $card['incorrect'] > 0 ? $card['correct'] / ($card['correct'] + $card['incorrect']) : 0.5;
    $weight = (time() - $card['lasttested']) / 3600 + 1 / ($propright + 1) - 1 / 2;
    return $weight;
}, $cards);
//array_map
Beispiel #2
0
<?php

/* isloggedin.php - Checks if the user is logged in. If so, prepares the
* variables $username, $cardlist and $cardlist_filename. */
set_include_path('include/');
require_once 'eatcsv.php';
$passwordfile = eatcsv('users.csv');
$passwords = array();
foreach ($passwordfile as $user) {
    $passwords[$user['username']] = $user['password'];
}
session_start();
/* If the username and password are given, and they match, then the user is
 * logged in. Otherwise, they are not. */
$isloggedin = false;
if (isset($_SESSION['flashcards_username']) && isset($_SESSION['flashcards_password'])) {
    if (array_key_exists($_SESSION['flashcards_username'], $passwords) && $passwords[$_SESSION['flashcards_username']] == $_SESSION['flashcards_password']) {
        $isloggedin = true;
        $username = $_SESSION['flashcards_username'];
        /* Check whether the user has a directory in /cardlists/ or not.
         * If not, create a directory for them, copying across the files from
         * /cardlists/default/. */
        if (!file_exists('cardlists/' . $username)) {
            system('cp -r cardlists/default cardlists/' . $username);
        }
        system('touch cardlists/' . $username . '/' . $username);
        if (isset($_SESSION['flashcards_cardlist'])) {
            $cardlist = $_SESSION['flashcards_cardlist'];
        } else {
            $cardlist = $username;
        }
Beispiel #3
0
session_start();
require_once 'isloggedin.php';
if (!$isloggedin) {
    die('You are not logged in. Please go to <a href="login.php">login.php</a>.');
}
require_once 'loadcards.php';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $file = fopen($cardlist_filename, 'w') or $msg .= "Couldn't open file {$cardlist_filename}.";
    fputs($file, $_REQUEST['cardlist']) or $msg .= "Couldn't write to file {$cardlist_filename}. ";
    fclose($file);
    if (!isset($msg)) {
        $msg = "Written to file {$cardlist_filename}. ";
    }
    /* Sanitise the newly-written cardlist. Fill in missing numerical values as
     * zero, and use htmlspecialchars() on the strings. */
    $cards = eatcsv($cardlist_filename);
    foreach ($cards as &$card) {
        $card['side0'] = htmlspecialchars($card['side0']);
        $card['side1'] = htmlspecialchars($card['side1']);
        if (empty($card['correct'])) {
            $card['correct'] = 0;
        }
        if (empty($card['incorrect'])) {
            $card['incorrect'] = 0;
        }
        if (empty($card['lasttested'])) {
            $card['lasttested'] = 0;
        }
    }
    /* Rewrite the cardlist using the sanitised table. */
    $file = fopen($cardlist_filename, 'w');