qa-filter-basic.php 4.71 KB
Newer Older
Gideon Greenspan committed
1 2 3 4 5 6 7
<?php

/*
	Question2Answer (c) Gideon Greenspan

	http://www.question2answer.org/

Scott Vivian committed
8

Gideon Greenspan committed
9 10 11 12 13 14 15 16 17
	File: qa-include/qa-filter-basic.php
	Version: See define()s at top of qa-include/qa-base.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.
Scott Vivian committed
18

Gideon Greenspan committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
	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
*/

	if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
		header('Location: ../');
		exit;
	}

	require_once QA_INCLUDE_DIR.'qa-db-maxima.php';
	require_once QA_INCLUDE_DIR.'qa-util-string.php';

Scott Vivian committed
35

Gideon Greenspan committed
36
	class qa_filter_basic {
Scott Vivian committed
37

38
		public function filter_email(&$email, $olduser)
Gideon Greenspan committed
39 40 41
		{
			if (!strlen($email))
				return qa_lang('users/email_required');
Scott Vivian committed
42

Gideon Greenspan committed
43 44
			if (!qa_email_validate($email))
				return qa_lang('users/email_invalid');
Scott Vivian committed
45

Gideon Greenspan committed
46 47 48 49
			if (qa_strlen($email)>QA_DB_MAX_EMAIL_LENGTH)
				return qa_lang_sub('main/max_length_x', QA_DB_MAX_EMAIL_LENGTH);
		}

Scott Vivian committed
50

51
		public function filter_handle(&$handle, $olduser)
Gideon Greenspan committed
52 53 54
		{
			if (!strlen($handle))
				return qa_lang('users/handle_empty');
Scott Vivian committed
55

Gideon Greenspan committed
56 57
			if (preg_match('/[\\@\\+\\/]/', $handle))
				return qa_lang_sub('users/handle_has_bad', '@ + /');
Scott Vivian committed
58

Gideon Greenspan committed
59 60 61
			if (qa_strlen($handle)>QA_DB_MAX_HANDLE_LENGTH)
				return qa_lang_sub('main/max_length_x', QA_DB_MAX_HANDLE_LENGTH);
		}
Scott Vivian committed
62

Gideon Greenspan committed
63

64
		public function filter_question(&$question, &$errors, $oldquestion)
Gideon Greenspan committed
65 66 67
		{
			$this->validate_length($errors, 'title', @$question['title'], qa_opt('min_len_q_title'),
				max(qa_opt('min_len_q_title'), min(qa_opt('max_len_q_title'), QA_DB_MAX_TITLE_LENGTH)));
Scott Vivian committed
68

Gideon Greenspan committed
69
			$this->validate_length($errors, 'content', @$question['content'], 0, QA_DB_MAX_CONTENT_LENGTH); // for storage
Scott Vivian committed
70

Gideon Greenspan committed
71
			$this->validate_length($errors, 'content', @$question['text'], qa_opt('min_len_q_content'), null); // for display
Scott Vivian committed
72

Gideon Greenspan committed
73 74 75 76 77 78 79 80 81 82 83
			if (isset($question['tags'])) {
				$counttags=count($question['tags']);
				$mintags=min(qa_opt('min_num_q_tags'), qa_opt('max_num_q_tags'));

				if ($counttags<$mintags)
					$errors['tags']=qa_lang_sub('question/min_tags_x', $mintags);
				elseif ($counttags>qa_opt('max_num_q_tags'))
					$errors['tags']=qa_lang_sub('question/max_tags_x', qa_opt('max_num_q_tags'));
				else
					$this->validate_length($errors, 'tags', qa_tags_to_tagstring($question['tags']), 0, QA_DB_MAX_TAGS_LENGTH); // for storage
			}
Scott Vivian committed
84

Gideon Greenspan committed
85 86 87
			$this->validate_post_email($errors, $question);
		}

Scott Vivian committed
88

89
		public function filter_answer(&$answer, &$errors, $question, $oldanswer)
Gideon Greenspan committed
90 91 92 93 94 95
		{
			$this->validate_length($errors, 'content', @$answer['content'], 0, QA_DB_MAX_CONTENT_LENGTH); // for storage
			$this->validate_length($errors, 'content', @$answer['text'], qa_opt('min_len_a_content'), null); // for display
			$this->validate_post_email($errors, $answer);
		}

Scott Vivian committed
96

97
		public function filter_comment(&$comment, &$errors, $question, $parent, $oldcomment)
Gideon Greenspan committed
98 99 100 101 102 103
		{
			$this->validate_length($errors, 'content', @$comment['content'], 0, QA_DB_MAX_CONTENT_LENGTH); // for storage
			$this->validate_length($errors, 'content', @$comment['text'], qa_opt('min_len_c_content'), null); // for display
			$this->validate_post_email($errors, $comment);
		}

Scott Vivian committed
104

105
		public function filter_profile(&$profile, &$errors, $user, $oldprofile)
Gideon Greenspan committed
106 107 108 109 110 111 112 113
		{
			foreach ($profile as $field => $value)
				$this->validate_length($errors, $field, $value, 0, QA_DB_MAX_PROFILE_CONTENT_LENGTH);
		}


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

114
		private function validate_length(&$errors, $field, $input, $minlength, $maxlength)
Gideon Greenspan committed
115 116 117 118 119 120
	/*
		Add textual element $field to $errors if length of $input is not between $minlength and $maxlength
	*/
		{
			if (isset($input)) {
				$length=qa_strlen($input);
Scott Vivian committed
121

Gideon Greenspan committed
122 123 124 125 126 127 128
				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 Vivian committed
129

130
		private function validate_post_email(&$errors, $post)
Gideon Greenspan committed
131 132 133 134 135 136 137
		{
			if (@$post['notify'] && strlen(@$post['email'])) {
				$error=$this->filter_email($post['email'], null);
				if (isset($error))
					$errors['email']=$error;
			}
		}
Scott Vivian committed
138

Gideon Greenspan committed
139
	}
Scott Vivian committed
140

Gideon Greenspan committed
141 142 143 144

/*
	Omit PHP closing tag to help avoid accidental output
*/