]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/ImportFieldSanitize.php
Release 6.2.0
[Github/sugarcrm.git] / modules / Import / ImportFieldSanitize.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
4 /*********************************************************************************
5  * SugarCRM Community Edition is a customer relationship management program developed by
6  * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
7  * 
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.
14  * 
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
18  * details.
19  * 
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
23  * 02110-1301 USA.
24  * 
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.
27  * 
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.
31  * 
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  ********************************************************************************/
38
39 /*********************************************************************************
40
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/ImportFile.php');
46
47 class ImportFieldSanitize
48 {
49     /**
50      * properties set to handle locale formatting
51      */
52     public $dateformat;
53     public $timeformat;
54     public $timezone;
55     public $currency_symbol;
56     public $default_currency_significant_digits;
57     public $num_grp_sep;
58     public $dec_sep;
59     public $default_locale_name_format;
60
61     /**
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
64      */
65     public static $createdBeans = array();
66
67     /**
68      * true if we will create related beans during the sanitize process
69      */
70     public $addRelatedBean = false;
71     
72     /**
73      * Checks the SugarField defintion for an available santization method.
74      *
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
79      */
80     public function __call(
81         $name,
82         $params
83         )
84     {
85         static $sfh;
86         
87         if(!isset($sfh)) {
88             require_once('include/SugarFields/SugarFieldHandler.php');
89             $sfh = new SugarFieldHandler();
90         }
91         $value = $params[0];
92         $vardef = $params[1];
93         if ( isset($params[2]) )
94             $focus = $params[2];
95         else
96             $focus = null;
97         if ( $name == 'relate' && !empty($params[3]) )
98             $this->addRelatedBean = true;
99         else
100             $this->addRelatedBean = false;
101         
102         $field = $sfh->getSugarField(ucfirst($name));
103         if ( $field instanceOf SugarFieldBase ) {
104             $value = $field->importSanitize($value,$vardef,$focus,$this);
105         }
106         
107         return $value;
108     }
109
110     /**
111      * Validate date fields
112      *
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
117      */
118     public function date(
119         $value,
120         $vardef,
121         &$focus
122         )
123     {
124         global $timedate;
125
126         $format = $this->dateformat;
127
128         if ( !$timedate->check_matching_format($value, $format) )
129             return false;
130
131         if ( !$this->isValidTimeDate($value, $format) )
132             return false;
133
134         $value = $timedate->swap_formats(
135             $value, $format, $timedate->get_date_format());
136
137         return $value;
138     }
139
140     /**
141      * Validate email fields
142      *
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
147      */
148     public function email(
149         $value,
150         $vardef
151         )
152     {
153         if ( !empty($value) && !preg_match('/^\w+(?:[\'.\-+]\w+)*@\w+(?:[.\-]\w+)*(?:[.]\w{2,})+$/',$value) ) {
154             return false;
155         }
156
157         return $value;
158     }
159
160     /**
161      * Validate sync_to_outlook field
162      *
163      * @param  $value     string
164      * @param  $vardef    array
165      * @param  $bad_names array used to return list of bad users/teams in $value
166      * @return string sanitized and validated value on success, bool false on failure
167      */
168     public function synctooutlook(
169         $value,
170         $vardef,
171         &$bad_names
172         )
173     {
174         static $focus_user;
175
176         // cache this object since we'll be reusing it a bunch
177         if ( !($focus_user instanceof User) ) {
178
179             $focus_user = new User();
180         }
181
182
183         if ( !empty($value) && strtolower($value) != "all" ) {
184             $theList   = explode(",",$value);
185             $isValid   = true;
186             $bad_names = array();
187             foreach ($theList as $eachItem) {
188                 if ( $focus_user->retrieve_user_id($eachItem)
189                         || $focus_user->retrieve($eachItem)
190                 ) {
191                     // all good
192                 }
193                 else {
194                     $isValid     = false;
195                     $bad_names[] = $eachItem;
196                     continue;
197                 }
198             }
199             if(!$isValid) {
200                 return false;
201             }
202         }
203
204         return $value;
205     }
206
207     /**
208      * Validate time fields
209      *
210      * @param  $value    string
211      * @param  $vardef   array
212      * @param  $focus  object bean of the module we're importing into
213      * @return string sanitized and validated value on success, bool false on failure
214      */
215     public function time(
216         $value,
217         $vardef,
218         $focus
219         )
220     {
221         global $timedate;
222
223         $format = $this->timeformat;
224
225         if ( !$timedate->check_matching_format($value, $format) )
226             return false;
227
228         if ( !$this->isValidTimeDate($value, $format) )
229             return false;
230
231         $value = $timedate->swap_formats(
232             $value, $format, $timedate->get_time_format());
233         $value = $timedate->handle_offset(
234             $value, $timedate->get_time_format(), false, $GLOBALS['current_user'], $this->timezone);
235         $value = $timedate->handle_offset(
236             $value, $timedate->get_time_format(), true);
237
238         return $value;
239     }
240
241     /**
242      * Added to handle Bug 24104, to make sure the date/time value is correct ( i.e. 20/20/2008 doesn't work )
243      *
244      * @param  $value  string
245      * @param  $format string
246      * @return string sanitized and validated value on success, bool false on failure
247      */
248     public function isValidTimeDate(
249         $value,
250         $format
251         )
252     {
253         global $timedate;
254
255         $dateparts = array();
256         $reg = $timedate->get_regular_expression($format);
257         preg_match('@'.$reg['format'].'@', $value, $dateparts);
258
259         if ( empty($dateparts) )
260             return false;
261         if ( isset($reg['positions']['a'])
262                 && !in_array($dateparts[$reg['positions']['a']], array('am','pm')) )
263             return false;
264         if ( isset($reg['positions']['A'])
265                 && !in_array($dateparts[$reg['positions']['A']], array('AM','PM')) )
266             return false;
267         if ( isset($reg['positions']['h']) && (
268                 !is_numeric($dateparts[$reg['positions']['h']])
269                 || $dateparts[$reg['positions']['h']] < 1
270                 || $dateparts[$reg['positions']['h']] > 12 ) )
271             return false;
272         if ( isset($reg['positions']['H']) && (
273                 !is_numeric($dateparts[$reg['positions']['H']])
274                 || $dateparts[$reg['positions']['H']] < 0
275                 || $dateparts[$reg['positions']['H']] > 23 ) )
276             return false;
277         if ( isset($reg['positions']['i']) && (
278                 !is_numeric($dateparts[$reg['positions']['i']])
279                 || $dateparts[$reg['positions']['i']] < 0
280                 || $dateparts[$reg['positions']['i']] > 59 ) )
281             return false;
282         if ( isset($reg['positions']['s']) && (
283                 !is_numeric($dateparts[$reg['positions']['s']])
284                 || $dateparts[$reg['positions']['s']] < 0
285                 || $dateparts[$reg['positions']['s']] > 59 ) )
286             return false;
287         if ( isset($reg['positions']['d']) && (
288                 !is_numeric($dateparts[$reg['positions']['d']])
289                 || $dateparts[$reg['positions']['d']] < 1
290                 || $dateparts[$reg['positions']['d']] > 31 ) )
291             return false;
292         if ( isset($reg['positions']['m']) && (
293                 !is_numeric($dateparts[$reg['positions']['m']])
294                 || $dateparts[$reg['positions']['m']] < 1
295                 || $dateparts[$reg['positions']['m']] > 12 ) )
296             return false;
297         if ( isset($reg['positions']['Y']) &&
298                 !is_numeric($dateparts[$reg['positions']['Y']]) )
299             return false;
300
301         return true;
302     }
303
304 }