qa-filter-basic.php 6.26 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
		}
Scott committed
46 47 48
		if (in_array($handle, array('.', '..'))) {
			return qa_lang_sub('users/handle_has_bad', '. ..');
		}
Scott committed
49
		if (preg_match('/[\\@\\+\\/]/', $handle)) {
Scott committed
50
			return qa_lang_sub('users/handle_has_bad', '@ + /');
Scott committed
51 52
		}
		if (qa_strlen($handle) > QA_DB_MAX_HANDLE_LENGTH) {
Scott committed
53
			return qa_lang_sub('main/max_length_x', QA_DB_MAX_HANDLE_LENGTH);
Scott committed
54
		}
Scott committed
55
	}
Scott committed
56

Scott committed
57 58
	public function filter_question(&$question, &$errors, $oldquestion)
	{
59 60 61 62 63 64 65
		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
66

67 68 69
		$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
70

71 72
		$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
73

Scott committed
74
		if (isset($question['tags'])) {
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
			$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
91 92
		}

Scott committed
93 94
		$this->validate_post_email($errors, $question);
	}
Scott committed
95

Scott committed
96 97
	public function filter_answer(&$answer, &$errors, $question, $oldanswer)
	{
98 99
		$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
100 101
		$this->validate_post_email($errors, $answer);
	}
Scott committed
102

Scott committed
103 104
	public function filter_comment(&$comment, &$errors, $question, $parent, $oldcomment)
	{
105 106
		$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
107 108
		$this->validate_post_email($errors, $comment);
	}
Scott committed
109

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


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

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

Scott committed
131 132 133 134 135
		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
136

137 138 139 140 141 142 143 144 145
	/**
	 * 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
	 */
146
	private function validate_field_length(&$errors, &$post, $key, $minlength, $maxlength, $errorKey=null)
147
	{
148 149 150 151
		if (!$errorKey) {
			$errorKey = $key;
		}

152 153 154 155 156
		// 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) {
157
				$errors[$errorKey] = $minlength == 1 ? qa_lang('main/field_required') : qa_lang_sub('main/min_length_x', $minlength);
158 159
			}
			else if (isset($maxlength) && ($length > $maxlength)) {
160
				$errors[$errorKey] = qa_lang_sub('main/max_length_x', $maxlength);
161 162 163 164
			}
		}
	}

165 166 167 168 169 170 171
	/**
	 * 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
172 173
	{
		if (@$post['notify'] && strlen(@$post['email'])) {
Scott committed
174 175 176 177
			$error = $this->filter_email($post['email'], null);
			if (isset($error)) {
				$errors['email'] = $error;
			}
Scott committed
178 179
		}
	}
Scott committed
180
}