public function testFileUploadField2()
 {
     $this->setExpectedException('fValidationException');
     $_SERVER['REQUEST_METHOD'] = 'POST';
     $_SERVER['CONTENT_TYPE'] = 'multipart/form-data';
     $_FILES = array();
     $_FILES['file'] = array('name' => 'test.txt', 'type' => '', 'tmp_name' => './resources/text/example', 'error' => '', 'size' => 17);
     $uploader = new fUpload();
     $uploader->setMIMETypes(array('text/csv'), 'Please upload a CSV file');
     try {
         $v = new fValidation();
         $v->addFileUploadRule('file', $uploader);
         $v->validate();
     } catch (fValidationException $e) {
         $this->assertContains('File: Please upload a CSV file', $e->getMessage());
         throw $e;
     }
 }
Example #2
0
function build_json_response()
{
    if (!isset($_POST['json'])) {
        return array('error' => array('message' => "No JSON found"));
    }
    $data = json_decode($_POST['json'], true);
    if (!$data) {
        return array('error' => array('message' => "JSON could not be decoded"));
    }
    $_POST = $data;
    // fValidation inspects $_POST for field data
    $validator = new fValidation();
    $validator->addRequiredFields('title', 'details', 'venue', 'address', 'organizer', 'email', 'read_comic');
    $validator->addEmailFields('email');
    $validator->addRegexReplacement('#^(.*?): (.*)$#', '\\2 for <span class="field-name">\\1</span>');
    // If id is specified require secret
    $validator->addConditionalRule(array('id'), NULL, array('secret'));
    $messages = $validator->validate(TRUE, TRUE);
    if (!$data['read_comic']) {
        $messages['read_comic'] = 'You must have read the Ride Leading Comic';
    }
    if ($messages) {
        return array('error' => array('message' => 'There were errors in your fields', 'fields' => $messages));
    }
    $inputDateStrings = get($data['dates'], array());
    $validDates = array();
    $invalidDates = array();
    foreach ($inputDateStrings as $dateString) {
        $date = DateTime::createFromFormat('Y-m-d', $dateString);
        if ($date) {
            $validDates[] = $date;
        } else {
            $invalidDates[] = $dateString;
        }
    }
    if ($invalidDates) {
        $messages['dates'] = "Invalid dates: " . implode(', ', $invalidDates);
    }
    if (count($validDates) === 1) {
        $data['datestype'] = 'O';
        $data['datestring'] = date_format($validDates[0], 'l, F j');
    } else {
        // not dealing with 'consecutive'
        $data['datestype'] = 'S';
        $data['datestring'] = 'Scattered days';
    }
    // Converts data to an event, loading the existing one if id is included in data
    $event = Event::fromArray($data);
    // Else
    if ($event->exists() && !$event->secretValid($data['secret'])) {
        return array('error' => array('message' => 'Invalid secret, use link from email'));
    }
    $messages = $event->validate($return_messages = TRUE, $remove_column_names = TRUE);
    if (isset($_FILES['file'])) {
        $uploader = new fUpload();
        $uploader->setMIMETypes(array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'), 'The file uploaded is not an image');
        $uploader->setMaxSize('2MB');
        $uploader->setOptional();
        $file_message = $uploader->validate('file', TRUE);
        if ($file_message != null) {
            $messages['file'] = $file_message;
        }
        global $IMAGEDIR;
        $file = $uploader->move($IMAGEDIR, 'file');
        $event->setImage($file->getName());
    }
    if ($messages) {
        return array('error' => array('message' => 'There were errors in your fields', 'fields' => $messages));
    }
    // if needs secret generate and email
    if (!$event->exists()) {
        $includeSecret = true;
    } else {
        $includeSecret = false;
    }
    // If there are validation errors this starts spewing html, so we validate before
    $event->store();
    // Create/delete EventTimes to match the list of dates included
    EventTime::matchEventTimesToDates($event, $validDates);
    // Returns the created object
    $details = $event->toDetailArray(true);
    if ($includeSecret) {
        $details['secret'] = $event->getPassword();
        // Wait until after it is stored to ensure it has an id
        $event->emailSecret();
    }
    return $details;
}
$title = 'Hack Me Sticker';
require './header.php';
$cards = fRecordSet::build('Card', array('uid=' => $_GET['cardid']));
if ($cards->count() == 0) {
    fURL::redirect("/kiosk/addcard.php?cardid=" . $_GET['cardid']);
}
$card = $cards->getRecord(0);
$user = new User($card->getUserId());
$user->load();
# echo json_encode($_POST);
if (isset($_POST['print']) && $user->isMember()) {
    try {
        fRequest::validateCSRFToken($_POST['token']);
        $validator = new fValidation();
        $validator->addRequiredFields('more_info');
        $validator->validate();
        $data = array('donor_id' => $user->getId(), 'donor_name' => $user->getFull_Name(), 'donor_email' => $user->getEmail(), 'dispose_date' => date('Y-m-d', strtotime("+2 weeks")), 'more_info' => $_POST['more_info']);
        $data_string = json_encode($data);
        $ch = curl_init('http://kiosk.london.hackspace.org.uk:12345/print/hackme');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
        $result = curl_exec($ch);
        curl_close($ch);
        echo "<p>Your sticker is being printed now.</p>";
    } catch (fValidationException $e) {
        $e->printMessage();
    }
}
?>
Example #4
0
 public function testAddURL6()
 {
     $this->setExpectedException('fValidationException');
     $_POST['foo'] = "http://flourishlib.com/foo bar/";
     $_GET['bar'] = "http://flourishlib.com/foo bar/";
     try {
         $v = new fValidation();
         $v->addURLFields('foo', 'bar');
         $v->validate();
     } catch (fValidationException $e) {
         $this->assertContains('Foo: Please enter a URL in the form http://www.example.com/page', $e->getMessage());
         $this->assertContains('Bar: Please enter a URL in the form http://www.example.com/page', $e->getMessage());
         throw $e;
     }
 }