]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/Import/ImportFieldSanitize.php
Release 6.2.3
[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         // cache $sea instance
154         static $sea;
155         
156         if ( !($sea instanceof SugarEmailAddress) ) {
157             $sea = new SugarEmailAddress;
158         }
159         
160         if ( !empty($value) && !preg_match($sea->regex,$value) ) {
161             return false;
162         }
163
164         return $value;
165     }
166
167     /**
168      * Validate sync_to_outlook field
169      *
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
174      */
175     public function synctooutlook(
176         $value,
177         $vardef,
178         &$bad_names
179         )
180     {
181         static $focus_user;
182
183         // cache this object since we'll be reusing it a bunch
184         if ( !($focus_user instanceof User) ) {
185
186             $focus_user = new User();
187         }
188
189
190         if ( !empty($value) && strtolower($value) != "all" ) {
191             $theList   = explode(",",$value);
192             $isValid   = true;
193             $bad_names = array();
194             foreach ($theList as $eachItem) {
195                 if ( $focus_user->retrieve_user_id($eachItem)
196                         || $focus_user->retrieve($eachItem)
197                 ) {
198                     // all good
199                 }
200                 else {
201                     $isValid     = false;
202                     $bad_names[] = $eachItem;
203                     continue;
204                 }
205             }
206             if(!$isValid) {
207                 return false;
208             }
209         }
210
211         return $value;
212     }
213
214     /**
215      * Validate time fields
216      *
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
221      */
222     public function time(
223         $value,
224         $vardef,
225         $focus
226         )
227     {
228         global $timedate;
229
230         $format = $this->timeformat;
231
232         if ( !$timedate->check_matching_format($value, $format) )
233             return false;
234
235         if ( !$this->isValidTimeDate($value, $format) )
236             return false;
237
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);
244
245         return $value;
246     }
247
248     /**
249      * Added to handle Bug 24104, to make sure the date/time value is correct ( i.e. 20/20/2008 doesn't work )
250      *
251      * @param  $value  string
252      * @param  $format string
253      * @return string sanitized and validated value on success, bool false on failure
254      */
255     public function isValidTimeDate(
256         $value,
257         $format
258         )
259     {
260         global $timedate;
261
262         $dateparts = array();
263         $reg = $timedate->get_regular_expression($format);
264         preg_match('@'.$reg['format'].'@', $value, $dateparts);
265
266         if ( empty($dateparts) )
267             return false;
268         if ( isset($reg['positions']['a'])
269                 && !in_array($dateparts[$reg['positions']['a']], array('am','pm')) )
270             return false;
271         if ( isset($reg['positions']['A'])
272                 && !in_array($dateparts[$reg['positions']['A']], array('AM','PM')) )
273             return false;
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 ) )
278             return false;
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 ) )
283             return false;
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 ) )
288             return false;
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 ) )
293             return false;
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 ) )
298             return false;
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 ) )
303             return false;
304         if ( isset($reg['positions']['Y']) &&
305                 !is_numeric($dateparts[$reg['positions']['Y']]) )
306             return false;
307
308         return true;
309     }
310
311 }