function Streams_related_validate() { switch (Q_Request::method()) { case 'POST': $required = array('toPublisherId', 'toStreamName', 'type', 'fromPublisherId', 'fromStreamName'); break; case 'DELETE': $required = array('toPublisherId', 'toStreamName', 'type', 'fromPublisherId', 'fromStreamName'); break; case 'PUT': $required = array('toPublisherId', 'toStreamName', 'type', 'fromPublisherId', 'fromStreamName', 'weight'); if (isset($_REQUEST['adjustWeights'])) { if (!is_numeric($_REQUEST['adjustWeights'])) { Q_Response::addError(new Q_Exception_WrongValue(array('field' => 'adjustWeights', 'range' => 'a numeric value'), 'adjustWeights')); } } break; case 'GET': $required = array(); break; } foreach ($required as $r) { if (!isset($_REQUEST[$r])) { Q_Response::addError(new Q_Exception_RequiredField(array('field' => $r))); } } }
function Users_account_validate() { Q_Valid::nonce(true); $birthday_year = $birthday_month = $birthday_day = null; extract($_REQUEST); $field_names = array('firstName' => 'First name', 'lastName' => 'Last name', 'username' => 'Username', 'gender' => 'Your gender', 'desired_gender' => 'Gender preference', 'orientation' => 'Orientation', 'relationship_status' => 'Status', 'zipcode' => 'Zipcode'); foreach ($field_names as $name => $label) { if (isset($_POST[$name]) and !$_POST[$name]) { Q_Response::addError(new Q_Exception_RequiredField(array('field' => $label), $name)); } } if (isset($birthday_year)) { if (!checkdate($birthday_month, $birthday_day, $birthday_year)) { $field = 'Birthday'; $range = 'a valid date'; Q_Response::addError(new Q_Exception_WrongValue(compact('field', 'range'), 'birthday')); } } global $Q_installing; if (isset($username) and isset($Q_installing)) { try { Q::event('Users/validate/username', compact('username')); } catch (Exception $e) { Q_Response::addError($e); } } }
function Streams_invite_validate() { if (Q_Request::method() === 'PUT') { return; } if (Q_Request::method() !== 'GET') { Q_Valid::nonce(true); } $fields = array('publisherId', 'streamName'); if (Q_Request::method() === 'POST') { if (Q_Valid::requireFields($fields)) { return; } foreach ($fields as $f) { if (strlen(trim($_REQUEST[$f])) === 0) { Q_Response::addError(new Q_Exception("{$f} can't be empty", $f)); } } } if (isset($_REQUEST['fullName'])) { $length_min = Q_Config::get('Streams', 'inputs', 'fullName', 'lengthMin', 5); $length_max = Q_Config::get('Streams', 'inputs', 'fullName', 'lengthMax', 30); if (strlen($_REQUEST['fullName']) < $length_min) { throw new Q_Exception("A user's full name can't be that short.", 'fullName'); } if (strlen($_REQUEST['fullName']) > $length_max) { throw new Q_Exception("A user's full name can't be that long.", 'fullName'); } } }
function Users_authorize_validate() { Q_Request::requireFields(array('client_id', 'redirect_uri', 'state')); if (empty($_REQUEST['response_type']) or $_REQUEST['response_type'] !== 'token') { Q_Response::addError(new Q_Exception_WrongValue(array('field' => 'response_type', 'range' => '"token"'), 'response_type')); } $min_length = Q_Config::expect('Users', 'authorize', 'stateMinLength'); if (!empty($_REQUEST['state']) and strlen($_REQUEST['state']) < $min_length) { Q_Response::addError(new Q_Exception_WrongValue(array('field' => 'state', 'range' => "at least {$min_length} characters"), 'state')); } }
function Users_authorize_validate() { $bad_fields = array(); foreach (array('client_id', 'redirect_uri', 'scope', 'state') as $field) { if (empty($_REQUEST[$field])) { Q_Response::addError(new Q_Exception_RequiredField(compact('field'), $field)); $bad_fields[$field] = true; } } if (empty($_REQUEST['response_type']) or $_REQUEST['response_type'] !== 'token') { Q_Response::addError(new Q_Exception_WrongValue(array('field' => 'response_type', 'range' => '"token"'), 'response_type')); } $min_length = Q_Config::expect('Users', 'authorize', 'stateMinLength'); if (!empty($_REQUEST['state']) and strlen($_REQUEST['state']) < $min_length) { Q_Response::addError(new Q_Exception_WrongValue(array('field' => 'state', 'range' => "at least {$min_length} characters"), 'state')); } }
function Streams_basic_validate() { Q_Valid::nonce(true); if (Q_Request::method() !== 'POST') { return; } $fields = array('firstName' => 'First name', 'lastName' => 'Last name', 'gender' => 'Gender', 'birthday_month' => 'Month', 'birthday_day' => 'Day', 'birthday_year' => 'Year'); if (isset($_REQUEST['fullName'])) { $length_min = Q_Config::get('Streams', 'inputs', 'fullName', 'lengthMin', 5); $length_max = Q_Config::get('Streams', 'inputs', 'fullName', 'lengthMax', 30); if (strlen($_REQUEST['fullName']) < $length_min) { Q_Response::addError(new Q_Exception("Your full name can't be that short.", 'fullName')); } if (strlen($_REQUEST['fullName']) > $length_max) { Q_Response::addError(new Q_Exception("Your full name can't be that long.", 'fullName')); } } if (Q_Response::getErrors()) { return; } if (!empty($_REQUEST['birthday_month']) or !empty($_REQUEST['birthday_day']) or !empty($_REQUEST['birthday_year'])) { foreach (array('birthday_month', 'birthday_day', 'birthday_year') as $field) { if (empty($_REQUEST[$field]) or !trim($_REQUEST[$field])) { throw new Q_Exception_RequiredField(compact('field'), $field); } } if (!checkdate($_REQUEST['birthday_month'], $_REQUEST['birthday_day'], $_REQUEST['birthday_year'])) { Q_Response::addError(new Q_Exception("Not a valid date", "birthday_day")); } if ($_REQUEST['birthday_year'] > date('Y') - 13) { // compliance with COPPA Q_Response::addError(new Q_Exception("You're still a kid.", "birthday_year")); } if ($_REQUEST['birthday_year'] < date('Y') - 100) { Q_Response::addError(new Q_Exception("A world record? Really?", "birthday_year")); } } if (!empty($_REQUEST['gender'])) { if (!in_array($_REQUEST['gender'], array('male', 'female'))) { Q_Response::addError(new Q_Exception("Please enter male or female", "gender")); } } }
/** * The default implementation. */ function Q_errors($params) { extract($params); /** * @var Exception $exception * @var boolean $startedResponse */ if (!empty($exception)) { Q_Response::addError($exception); } $errors = Q_Response::getErrors(); $errors_array = Q_Exception::toArray($errors); // Simply return the errors, if this was an AJAX request if ($is_ajax = Q_Request::isAjax()) { try { $errors_json = @Q::json_encode($errors_array); } catch (Exception $e) { $errors_array = array_slice($errors_array, 0, 1); unset($errors_array[0]['trace']); $errors_json = @Q::json_encode($errors_array); } $json = "{\"errors\": {$errors_json}}"; $callback = Q_Request::callback(); switch (strtolower($is_ajax)) { case 'iframe': if (!Q_Response::$batch) { header("Content-type: text/html"); } echo <<<EOT <!doctype html><html lang=en> <head><meta charset=utf-8><title>Q Result</title></head> <body> <script type="text/javascript"> window.result = function () { return {$json} }; </script> </body> </html> EOT; break; case 'json': default: header("Content-type: " . ($callback ? "application/javascript" : "application/json")); echo $callback ? "{$callback}({$json})" : $json; } return; } // Forward internally, if it was requested if ($onErrors = Q_Request::special('onErrors', null)) { $uri1 = Q_Dispatcher::uri(); $uri2 = Q_Uri::from($onErrors); $url2 = $uri2->toUrl(); if (!isset($uri2)) { throw new Q_Exception_WrongValue(array('field' => 'onErrors', 'range' => 'an internal URI reachable from a URL')); } if ($uri1->toUrl() !== $url2) { Q_Dispatcher::forward($uri2); return; // we don't really need this, but it's here anyway } } $params2 = compact('errors', 'exception', 'errors_array', 'exception_array'); if (Q::eventStack('Q/response')) { // Errors happened while rendering response. Just render errors view. return Q::view('Q/errors.php', $params2); } if (!$startedResponse) { try { // Try rendering the response, expecting it to // display the errors along with the rest. $ob = new Q_OutputBuffer(); Q::event('Q/response', $params2); $ob->endFlush(); return; } catch (Exception $e) { if (get_class($e) === 'Q_Exception_DispatcherForward') { throw $e; // if forwarding was requested, do it // for all other errors, continue trying other things } $output = $ob->getClean(); } } if ($errors) { // Try rendering the app's errors response, if any. $app = Q::app(); if (Q::canHandle("{$app}/errors/response/content")) { Q_Dispatcher::forward("{$app}/errors"); } else { echo Q::view("Q/errors.php", compact('errors')); } } if (!empty($e)) { return Q::event('Q/exception', array('exception' => $e)); } }
/** * Convenience method to apply certain criteria to an array. * and call Q_Response::addError for each one. * @see Q_Valid::requireFields * @method require * @static * @param {array} $fields Array of strings or arrays naming fields that are required * @return {array} The resulting list of exceptions */ static function requireFields($fields, $throwIfMissing = false) { $args = func_get_args(); array_splice($args, 1, 0, array(null)); $exceptions = call_user_func_array(array('Q_Valid', 'requireFields'), $args); foreach ($exceptions as $e) { Q_Response::addError($e); } }