2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
4 /*********************************************************************************
5 * SugarCRM Community Edition is a customer relationship management program developed by
6 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Affero General Public License version 3 as published by the
10 * Free Software Foundation with the addition of the following permission added
11 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
12 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
13 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
20 * You should have received a copy of the GNU Affero General Public License along with
21 * this program; if not, see http://www.gnu.org/licenses or write to the Free
22 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
26 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
28 * The interactive user interfaces in modified source and object code versions
29 * of this program must display Appropriate Legal Notices, as required under
30 * Section 5 of the GNU Affero General Public License version 3.
32 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
33 * these Appropriate Legal Notices must retain the display of the "Powered by
34 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
35 * technical reasons, the Appropriate Legal Notices must display the words
36 * "Powered by SugarCRM".
37 ********************************************************************************/
39 /*********************************************************************************
41 * Description: class for sanitizing field values
42 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
43 * All Rights Reserved.
44 ********************************************************************************/
45 require_once('modules/Import/sources/ImportFile.php');
47 class ImportFieldSanitize
50 * properties set to handle locale formatting
55 public $currency_symbol;
56 public $default_currency_significant_digits;
59 public $default_locale_name_format;
62 * array of modules/users_last_import ids pairs that are created in this class
63 * needs to be reset after the row is imported
65 public static $createdBeans = array();
68 * true if we will create related beans during the sanitize process
70 public $addRelatedBean = false;
73 * Checks the SugarField defintion for an available santization method.
75 * @param $value string
76 * @param $vardef array
77 * @param $focus object bean of the module we're importing into
78 * @return string sanitized and validated value on success, bool false on failure
80 public function __call(
88 require_once('include/SugarFields/SugarFieldHandler.php');
89 $sfh = new SugarFieldHandler();
93 if ( isset($params[2]) )
97 if ( $name == 'relate' && !empty($params[3]) )
98 $this->addRelatedBean = true;
100 $this->addRelatedBean = false;
102 $field = $sfh->getSugarField(ucfirst($name));
103 if ( $field instanceOf SugarFieldBase ) {
104 $value = $field->importSanitize($value,$vardef,$focus,$this);
111 * Validate date fields
113 * @param $value string
114 * @param $vardef array
115 * @param $focus object bean of the module we're importing into
116 * @return string sanitized and validated value on success, bool false on failure
118 public function date(
126 $format = $this->dateformat;
128 if ( !$timedate->check_matching_format($value, $format) )
131 if ( !$this->isValidTimeDate($value, $format) )
134 $value = $timedate->swap_formats(
135 $value, $format, $timedate->get_date_format());
141 * Validate email fields
143 * @param $value string
144 * @param $vardef array
145 * @param $focus object bean of the module we're importing into
146 * @return string sanitized and validated value on success, bool false on failure
148 public function email(
153 // cache $sea instance
156 if ( !($sea instanceof SugarEmailAddress) ) {
157 $sea = new SugarEmailAddress;
160 if ( !empty($value) && !preg_match($sea->regex,$value) ) {
168 * Validate sync_to_outlook field
170 * @param $value string
171 * @param $vardef array
172 * @param $bad_names array used to return list of bad users/teams in $value
173 * @return string sanitized and validated value on success, bool false on failure
175 public function synctooutlook(
183 // cache this object since we'll be reusing it a bunch
184 if ( !($focus_user instanceof User) ) {
186 $focus_user = new User();
190 if ( !empty($value) && strtolower($value) != "all" ) {
191 $theList = explode(",",$value);
193 $bad_names = array();
194 foreach ($theList as $eachItem) {
195 if ( $focus_user->retrieve_user_id($eachItem)
196 || $focus_user->retrieve($eachItem)
202 $bad_names[] = $eachItem;
215 * Validate time fields
217 * @param $value string
218 * @param $vardef array
219 * @param $focus object bean of the module we're importing into
220 * @return string sanitized and validated value on success, bool false on failure
222 public function time(
230 $format = $this->timeformat;
232 if ( !$timedate->check_matching_format($value, $format) )
235 if ( !$this->isValidTimeDate($value, $format) )
238 $value = $timedate->swap_formats(
239 $value, $format, $timedate->get_time_format());
240 $value = $timedate->handle_offset(
241 $value, $timedate->get_time_format(), false, $GLOBALS['current_user'], $this->timezone);
242 $value = $timedate->handle_offset(
243 $value, $timedate->get_time_format(), true);
249 * Added to handle Bug 24104, to make sure the date/time value is correct ( i.e. 20/20/2008 doesn't work )
251 * @param $value string
252 * @param $format string
253 * @return string sanitized and validated value on success, bool false on failure
255 public function isValidTimeDate(
262 $dateparts = array();
263 $reg = $timedate->get_regular_expression($format);
264 preg_match('@'.$reg['format'].'@', $value, $dateparts);
266 if ( empty($dateparts) )
268 if ( isset($reg['positions']['a'])
269 && !in_array($dateparts[$reg['positions']['a']], array('am','pm')) )
271 if ( isset($reg['positions']['A'])
272 && !in_array($dateparts[$reg['positions']['A']], array('AM','PM')) )
274 if ( isset($reg['positions']['h']) && (
275 !is_numeric($dateparts[$reg['positions']['h']])
276 || $dateparts[$reg['positions']['h']] < 1
277 || $dateparts[$reg['positions']['h']] > 12 ) )
279 if ( isset($reg['positions']['H']) && (
280 !is_numeric($dateparts[$reg['positions']['H']])
281 || $dateparts[$reg['positions']['H']] < 0
282 || $dateparts[$reg['positions']['H']] > 23 ) )
284 if ( isset($reg['positions']['i']) && (
285 !is_numeric($dateparts[$reg['positions']['i']])
286 || $dateparts[$reg['positions']['i']] < 0
287 || $dateparts[$reg['positions']['i']] > 59 ) )
289 if ( isset($reg['positions']['s']) && (
290 !is_numeric($dateparts[$reg['positions']['s']])
291 || $dateparts[$reg['positions']['s']] < 0
292 || $dateparts[$reg['positions']['s']] > 59 ) )
294 if ( isset($reg['positions']['d']) && (
295 !is_numeric($dateparts[$reg['positions']['d']])
296 || $dateparts[$reg['positions']['d']] < 1
297 || $dateparts[$reg['positions']['d']] > 31 ) )
299 if ( isset($reg['positions']['m']) && (
300 !is_numeric($dateparts[$reg['positions']['m']])
301 || $dateparts[$reg['positions']['m']] < 1
302 || $dateparts[$reg['positions']['m']] > 12 ) )
304 if ( isset($reg['positions']['Y']) &&
305 !is_numeric($dateparts[$reg['positions']['Y']]) )