function validate(&$data, $format, $strict = true) { global $ost; //Call parent to Validate the structure if (!parent::validate($data, $format, $strict) && $strict) { $this->exerr(400, __('Unexpected or invalid data received')); } // Use the settings on the thread entry on the ticket details // form to validate the attachments in the email $tform = TicketForm::objects()->one()->getForm(); $messageField = $tform->getField('message'); $fileField = $messageField->getWidget()->getAttachments(); // Nuke attachments IF API files are not allowed. if (!$messageField->isAttachmentsEnabled()) { $data['attachments'] = array(); } //Validate attachments: Do error checking... soft fail - set the error and pass on the request. if ($data['attachments'] && is_array($data['attachments'])) { foreach ($data['attachments'] as &$file) { if ($file['encoding'] && !strcasecmp($file['encoding'], 'base64')) { if (!($file['data'] = base64_decode($file['data'], true))) { $file['error'] = sprintf(__('%s: Poorly encoded base64 data'), Format::htmlchars($file['name'])); } } // Validate and save immediately try { $file['id'] = $fileField->uploadAttachment($file); } catch (FileUploadError $ex) { $file['error'] = $file['name'] . ': ' . $ex->getMessage(); } } unset($file); } return true; }
function validate(&$data, $format) { global $ost; //Call parent to Validate the structure if (!parent::validate($data, $format)) { $this->exerr(400, 'Unexpected or invalid data received'); } //Nuke attachments IF API files are not allowed. if (!$ost->getConfig()->allowAPIAttachments()) { $data['attachments'] = array(); } //Validate attachments: Do error checking... soft fail - set the error and pass on the request. if ($data['attachments'] && is_array($data['attachments'])) { foreach ($data['attachments'] as &$attachment) { if (!$ost->isFileTypeAllowed($attachment)) { $attachment['error'] = 'Invalid file type (ext) for ' . Format::htmlchars($attachment['name']); } elseif ($attachment['encoding'] && !strcasecmp($attachment['encoding'], 'base64')) { if (!($attachment['data'] = base64_decode($attachment['data'], true))) { $attachment['error'] = sprintf('%s: Poorly encoded base64 data', Format::htmlchars($attachment['name'])); } } } unset($attachment); } return true; }
public function testValidate() { $user1 = $user = $this->users('user1'); $user2 = $user = $this->users('user2'); // Update collection with wrong email $controller = new ApiController('api'); $controller->method = 'PUT'; $controller->model = new User('update'); $controller->data = array(array('id' => $user1->id, 'login' => $user1->login, 'email' => 'wrong_email'), array('id' => $user2->id, 'login' => $user2->login)); $result = $controller->validate(false); $this->assertFalse($result); $errors = $controller->getModelErrors(); $this->assertEquals(1, count($errors)); $this->assertArrayHasKey('email', $errors[0]); // Update collection with right email $controller = new ApiController('api'); $controller->method = 'PUT'; $controller->model = new User('update'); $controller->data = array(array('id' => $user1->id, 'login' => $user1->login, 'email' => '*****@*****.**'), array('id' => $user2->id, 'login' => $user2->login)); $result = $controller->validate(false); $this->assertTrue($result); $this->assertNull($controller->getModelErrors()); // Create collection with wrong email $controller = new ApiController('api'); $controller->method = 'POST'; $controller->model = new User('create'); $controller->data = array(array('login' => 'new_user_1', 'email' => 'wrong_email.com', 'password' => '1234567', 'password_repeat' => '1234567'), array('login' => 'new_user_2', 'email' => '*****@*****.**', 'password' => '1234567', 'password_repeat' => '1234567')); $result = $controller->validate(false); $errors = $controller->getModelErrors(); $this->assertEquals(1, count($errors)); $this->assertArrayHasKey('email', $errors[0]); // Create single record with wrong email $controller = new ApiController('api'); $controller->method = 'POST'; $controller->model = new User('create'); $controller->data = array('login' => 'new_user_1', 'email' => 'wrong_email.com', 'password' => '1234567', 'password_repeat' => '1234567'); $result = $controller->validate(false); $errors = $controller->getModelErrors(); $this->assertFalse($result); $this->assertEquals(1, count($errors)); $this->assertArrayHasKey('email', $errors); // Create single record with right email $controller = new ApiController('api'); $controller->method = 'POST'; $controller->model = new User('create'); $controller->data = array('login' => 'new_user_1', 'email' => '*****@*****.**', 'password' => '1234567', 'password_repeat' => '1234567'); $result = $controller->validate(false); $this->assertTrue($result); $this->assertNull($controller->getModelErrors()); }