qa-filter-basic.php 6.16 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-filter-basic.php
	Description: Basic module for validating form inputs


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
23
require_once QA_INCLUDE_DIR.'db/maxima.php';
Scott committed
24
require_once QA_INCLUDE_DIR.'util/string.php';
Scott committed
25

Scott committed
26 27 28 29
class qa_filter_basic
{
	public function filter_email(&$email, $olduser)
	{
Scott committed
30
		if (!strlen($email)) {
Scott committed
31
			return qa_lang('users/email_required');
Scott committed
32 33
		}
		if (!qa_email_validate($email)) {
Scott committed
34
			return qa_lang('users/email_invalid');
Scott committed
35 36
		}
		if (qa_strlen($email) > QA_DB_MAX_EMAIL_LENGTH) {
Scott committed
37
			return qa_lang_sub('main/max_length_x', QA_DB_MAX_EMAIL_LENGTH);
Scott committed
38
		}
Scott committed
39
	}
Scott committed
40

Scott committed
41 42
	public function filter_handle(&$handle, $olduser)
	{
Scott committed
43
		if (!strlen($handle)) {
Scott committed
44
			return qa_lang('users/handle_empty');
Scott committed
45 46
		}
		if (preg_match('/[\\@\\+\\/]/', $handle)) {
Scott committed
47
			return qa_lang_sub('users/handle_has_bad', '@ + /');
Scott committed
48 49
		}
		if (qa_strlen($handle) > QA_DB_MAX_HANDLE_LENGTH) {
Scott committed
50
			return qa_lang_sub('main/max_length_x', QA_DB_MAX_HANDLE_LENGTH);
Scott committed
51
		}
Scott committed
52
	}
Scott committed
53

Scott committed
54 55
	public function filter_question(&$question, &$errors, $oldquestion)
	{
56 57 58 59 60 61 62
		if ($oldquestion === null) {
			// a new post requires these fields be set
			$question['title'] = isset($question['title']) ? $question['title'] : '';
			$question['content'] = isset($question['content']) ? $question['content'] : '';
			$question['text'] = isset($question['text']) ? $question['text'] : '';
			$question['tags'] = isset($question['tags']) ? $question['tags'] : array();
		}
Scott committed
63

64 65 66
		$qminlength = qa_opt('min_len_q_title');
		$qmaxlength = max($qminlength, min(qa_opt('max_len_q_title'), QA_DB_MAX_TITLE_LENGTH));
		$this->validate_field_length($errors, $question, 'title', $qminlength, $qmaxlength);
Scott committed
67

68 69
		$this->validate_field_length($errors, $question, 'content', 0, QA_DB_MAX_CONTENT_LENGTH); // for storage
		$this->validate_field_length($errors, $question, 'text', qa_opt('min_len_q_content'), null); // for display
Scott committed
70

Scott committed
71
		if (isset($question['tags'])) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
			$counttags = count($question['tags']);
			$maxtags = qa_opt('max_num_q_tags');
			$mintags = min(qa_opt('min_num_q_tags'), $maxtags);

			if ($counttags < $mintags) {
				$errors['tags'] = qa_lang_sub('question/min_tags_x', $mintags);
			}
			elseif ($counttags > $maxtags) {
				$errors['tags'] = qa_lang_sub('question/max_tags_x', $maxtags);
			}
			else {
				$tagstring = qa_tags_to_tagstring($question['tags']);
				if (qa_strlen($tagstring) > QA_DB_MAX_TAGS_LENGTH) { // for storage
					$errors['tags'] = qa_lang_sub('main/max_length_x', $maxlength);
				}
			}
Scott committed
88 89
		}

Scott committed
90 91
		$this->validate_post_email($errors, $question);
	}
Scott committed
92

Scott committed
93 94
	public function filter_answer(&$answer, &$errors, $question, $oldanswer)
	{
95 96
		$this->validate_field_length($errors, $answer, 'content', 0, QA_DB_MAX_CONTENT_LENGTH); // for storage
		$this->validate_field_length($errors, $answer, 'text', qa_opt('min_len_a_content'), null, 'content'); // for display
Scott committed
97 98
		$this->validate_post_email($errors, $answer);
	}
Scott committed
99

Scott committed
100 101
	public function filter_comment(&$comment, &$errors, $question, $parent, $oldcomment)
	{
102 103
		$this->validate_field_length($errors, $comment, 'content', 0, QA_DB_MAX_CONTENT_LENGTH); // for storage
		$this->validate_field_length($errors, $comment, 'text', qa_opt('min_len_a_content'), null, 'content'); // for display
Scott committed
104 105
		$this->validate_post_email($errors, $comment);
	}
Scott committed
106

Scott committed
107 108
	public function filter_profile(&$profile, &$errors, $user, $oldprofile)
	{
109 110 111
		foreach (array_keys($profile) as $field) {
			// ensure fields are not NULL
			$profile[$field] = (string) $profile[$field];
112 113
			$this->validate_field_length($errors, $profile, $field, 0, QA_DB_MAX_CONTENT_LENGTH);
		}
Scott committed
114
	}
Scott committed
115 116


Scott committed
117
	// The definitions below are not part of a standard filter module, but just used within this one
Scott committed
118

Scott committed
119
	/**
120 121
	 * Add textual element $field to $errors if length of $input is not between $minlength and $maxlength.
	 *
Scott committed
122
	 * @deprecated This function is no longer used and will removed in the future.
Scott committed
123
	 */
124
	public function validate_length(&$errors, $field, $input, $minlength, $maxlength)
Scott committed
125 126
	{
		$length = isset($input) ? qa_strlen($input) : 0;
Scott committed
127

Scott committed
128 129 130 131 132
		if ($length < $minlength)
			$errors[$field] = ($minlength == 1) ? qa_lang('main/field_required') : qa_lang_sub('main/min_length_x', $minlength);
		elseif (isset($maxlength) && ($length > $maxlength))
			$errors[$field] = qa_lang_sub('main/max_length_x', $maxlength);
	}
Scott committed
133

134 135 136 137 138 139 140 141 142
	/**
	 * Check that a field meets the length requirements. If we're editing the post we can ignore missing fields.
	 *
	 * @param array $errors    Array of errors, with keys matching $post
	 * @param array $post      The post containing the field we want to validate
	 * @param string $key      The element of $post to validate
	 * @param int $minlength
	 * @param int $maxlength
	 */
143
	private function validate_field_length(&$errors, &$post, $key, $minlength, $maxlength, $errorKey=null)
144
	{
145 146 147 148
		if (!$errorKey) {
			$errorKey = $key;
		}

149 150 151 152 153
		// skip the field is key not set (for example, 'title' when recategorizing questions)
		if (array_key_exists($key, $post)) {
			$length = qa_strlen($post[$key]);

			if ($length < $minlength) {
154
				$errors[$errorKey] = $minlength == 1 ? qa_lang('main/field_required') : qa_lang_sub('main/min_length_x', $minlength);
155 156
			}
			else if (isset($maxlength) && ($length > $maxlength)) {
157
				$errors[$errorKey] = qa_lang_sub('main/max_length_x', $maxlength);
158 159 160 161
			}
		}
	}

162 163 164 165 166 167 168
	/**
	 * Wrapper function for validating a post's email address.
	 *
	 * @deprecated This function will become private in Q2A 1.8. It is specific to this plugin and
	 * should not be used by outside code.
	 */
	public function validate_post_email(&$errors, $post)
Scott committed
169 170
	{
		if (@$post['notify'] && strlen(@$post['email'])) {
Scott committed
171 172 173 174
			$error = $this->filter_email($post['email'], null);
			if (isset($error)) {
				$errors['email'] = $error;
			}
Scott committed
175 176
		}
	}
Scott committed
177
}