1<?php
2/**
3 * MyBB 1.8
4 * Copyright 2014 MyBB Group, All Rights Reserved
5 *
6 * Website: http://www.mybb.com
7 * License: http://www.mybb.com/about/license
8 *
9 */
10
11/**
12 * Base data handler class.
13 *
14 */
15class DataHandler
16{
17	/**
18	 * The data being managed by the data handler
19	 *
20	 * @var array Data being handled by the data handler.
21	 */
22	public $data = array();
23
24	/**
25	 * Whether or not the data has been validated. Note: "validated" != "valid".
26	 *
27	 * @var boolean True when validated, false when not validated.
28	 */
29	public $is_validated = false;
30
31	/**
32	 * The errors that occurred when handling data.
33	 *
34	 * @var array
35	 */
36	public $errors = array();
37
38	/**
39	 * The status of administrator override powers.
40	 *
41	 * @var boolean
42	 */
43	public $admin_override = false;
44
45	/**
46	 * Defines if we're performing an update or an insert.
47	 *
48	 * @var string
49	 */
50	public $method;
51
52	/**
53	* The prefix for the language variables used in the data handler.
54	*
55	* @var string
56	*/
57	public $language_prefix = '';
58
59
60	/**
61	 * Constructor for the data handler.
62	 *
63	 * @param string $method The method we're performing with this object.
64	 */
65	function __construct($method="insert")
66	{
67		if($method != "update" && $method != "insert" && $method != "get" && $method != "delete")
68		{
69			die("A valid method was not supplied to the data handler.");
70		}
71		$this->method = $method;
72	}
73
74	/**
75	 * Sets the data to be used for the data handler
76	 *
77	 * @param array $data The data.
78	 * @return bool
79	 */
80	function set_data($data)
81	{
82		if(!is_array($data))
83		{
84			return false;
85		}
86		$this->data = $data;
87		return true;
88	}
89
90	/**
91	 * Add an error to the error array.
92	 *
93	 * @param string $error The error name.
94	 * @param string $data
95	 */
96	function set_error($error, $data='')
97	{
98		$this->errors[$error] = array(
99			"error_code" => $error,
100			"data" => $data
101		);
102	}
103
104	/**
105	 * Returns the error(s) that occurred when handling data.
106	 *
107	 * @return array An array of errors.
108	 */
109	function get_errors()
110	{
111		return $this->errors;
112	}
113
114	/**
115	 * Returns the error(s) that occurred when handling data
116	 * in a format that MyBB can handle.
117	 *
118	 * @return array An array of errors in a MyBB format.
119	 */
120	function get_friendly_errors()
121	{
122		global $lang;
123
124		// Load the language pack we need
125		if($this->language_file)
126		{
127			$lang->load($this->language_file, true);
128		}
129		// Prefix all the error codes with the language prefix.
130		$errors = array();
131		foreach($this->errors as $error)
132		{
133			$lang_string = $this->language_prefix.'_'.$error['error_code'];
134			if(!$lang->$lang_string)
135			{
136				$errors[] = $error['error_code'];
137				continue;
138			}
139
140			if(!empty($error['data']) && !is_array($error['data']))
141			{
142				$error['data'] = array($error['data']);
143			}
144
145			if(is_array($error['data']))
146			{
147				array_unshift($error['data'], $lang->$lang_string);
148				$errors[] = call_user_func_array(array($lang, "sprintf"), $error['data']);
149			}
150			else
151			{
152				$errors[] = $lang->$lang_string;
153			}
154		}
155		return $errors;
156	}
157
158	/**
159	 * Sets whether or not we are done validating.
160	 *
161	 * @param boolean True when done, false when not done.
162	 */
163	function set_validated($validated = true)
164	{
165		$this->is_validated = $validated;
166	}
167
168	/**
169	 * Returns whether or not we are done validating.
170	 *
171	 * @return boolean True when done, false when not done.
172	 */
173	function get_validated()
174	{
175		if($this->is_validated == true)
176		{
177			return true;
178		}
179		else
180		{
181			return false;
182		}
183	}
184
185	/**
186	* Verifies if yes/no options haven't been modified.
187	*
188	* @param array $options The user options array.
189	* @param string $option The specific option to check.
190	* @param int|bool $default Optionally specify if the default should be used.
191	*/
192	function verify_yesno_option(&$options, $option, $default=1)
193	{
194		if($this->method == "insert" || array_key_exists($option, $options))
195		{
196			if(isset($options[$option]) && $options[$option] != $default && $options[$option] != "")
197			{
198				if($default == 1)
199				{
200					$options[$option] = 0;
201				}
202				else
203				{
204					$options[$option] = 1;
205				}
206			}
207			else if(@array_key_exists($option, $options) && $options[$option] == '')
208			{
209				$options[$option] = 0;
210			}
211			else
212			{
213				$options[$option] = $default;
214			}
215		}
216	}
217}
218