All Data Structures Functions Variables Pages
DataUtil.php
1 <?php
2  /*********************************************************************************
3  * Zurmo is a customer relationship management program developed by
4  * Zurmo, Inc. Copyright (C) 2017 Zurmo Inc.
5  *
6  * Zurmo is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  *
13  * Zurmo is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  *
23  * You can contact Zurmo, Inc. with a mailing address at 27 North Wacker Drive
24  * Suite 370 Chicago, IL 60606. or at email address contact@zurmo.com.
25  *
26  * The interactive user interfaces in original and modified versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  *
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the Zurmo
32  * logo and Zurmo copyright notice. If the display of the logo is not reasonably
33  * feasible for technical reasons, the Appropriate Legal Notices must display the words
34  * "Copyright Zurmo Inc. 2017. All rights reserved".
35  ********************************************************************************/
36 
40  class DataUtil
41  {
42  protected static $skippedAttributes = array();
43 
49  public static function sanitizeDataByDesignerTypeForSavingModel($model, $data)
50  {
51  assert('$model instanceof RedBeanModel || $model instanceof ModelForm');
52  assert('is_array($data)');
53  foreach ($data as $attributeName => $value)
54  {
55  if ($value !== null && static::isNotMarkedSkipped($attributeName))
56  {
57  if (!is_array($value))
58  {
59  if ($model->isAttribute($attributeName) && $model->isAttributeSafe($attributeName))
60  {
62  $model, $attributeName);
63  if ($designerType == 'Date' && !empty($value))
64  {
65  $data[$attributeName] = DateTimeUtil::resolveValueForDateDBFormatted($value);
66  }
67  if ($designerType == 'DateTime' && !empty($value))
68  {
70  }
71  $data[$attributeName] = static::purifyHtml($data[$attributeName]);
72  }
73  }
74  else
75  {
76  try
77  {
78  $designerType = ModelAttributeToDesignerTypeUtil::getDesignerType($model, $attributeName);
79  }
80  catch (NotImplementedException $e)
81  {
82  //In the event that a designer type does not exist.
83  $designerType = null;
84  }
85  if ($model->isAttributeSafe($attributeName) && $designerType != 'TagCloud')
86  {
87  if ($designerType == 'MixedDateTypesForSearch' && isset($value['firstDate']) &&
88  $value['firstDate'] != null)
89  {
90  $data[$attributeName]['firstDate'] = DateTimeUtil::
91  resolveValueForDateDBFormatted(
92  $value['firstDate']);
93  }
94  if ($designerType == 'MixedDateTypesForSearch' && isset($value['secondDate']) &&
95  $value['secondDate'] != null)
96  {
97  $data[$attributeName]['secondDate'] = DateTimeUtil::
98  resolveValueForDateDBFormatted(
99  $value['secondDate']);
100  }
101  }
102  elseif (isset($value['values']) && is_string($value['values']) && $designerType == 'TagCloud')
103  {
104  if ($data[$attributeName]['values'] == '')
105  {
106  $data[$attributeName]['values'] = array();
107  }
108  else
109  {
110  $data[$attributeName]['values'] = explode(',', $data[$attributeName]['values']); // Not Coding Standard
111  }
112  }
113  if ($designerType == 'CheckBox')
114  {
115  $data[$attributeName] = $value['value'];
116  }
117  else
118  {
119  array_walk_recursive($data[$attributeName], array(get_called_class(), 'purifyHtmlAndModifyInput'));
120  }
121  }
122  }
123  }
124  return $data;
125  }
126 
133  public static function sanitizeDataToJustHavingElementForSavingModel($sanitizedData, $elementName)
134  {
135  assert('is_array($sanitizedData)');
136  assert('is_string($elementName) || is_int($elementName)');
137  if (!isset($sanitizedData[$elementName]))
138  {
139  return null;
140  }
141  return array($elementName => $sanitizedData[$elementName]);
142  }
143 
149  public static function removeElementFromDataForSavingModel($sanitizedData, $elementName)
150  {
151  assert('is_array($sanitizedData)');
152  assert('is_string($elementName) || is_int($elementName)');
153  if (isset($sanitizedData[$elementName]))
154  {
155  unset($sanitizedData[$elementName]);
156  }
157  return $sanitizedData;
158  }
159 
165  public static function purifyHtml($text)
166  {
167  if (is_string($text))
168  {
169  $safeCharacters = array('&' => '&amp;',
170  '[' => '%5B',
171  '^' => '%5E',
172  ']' => '%5D',
173  '%' => '%25',
174  '<' => '&lt;',
175  '>' => '&gt;');
176  $purifier = new CHtmlPurifier();
177  $purifier->options = array('Cache.SerializerPermissions' => 0777,
178  'Attr.AllowedFrameTargets' => '_blank');
179  $purifiedText = $purifier->purify($text);
180  foreach ($safeCharacters as $specialCharacter => $purifiedCode)
181  {
182  if (strpos($text, $specialCharacter) !== false)
183  {
184  $purifiedText = str_replace($purifiedCode, $specialCharacter, $purifiedText);
185  }
186  }
187  $text = $purifiedText;
188  }
189  return $text;
190  }
191 
199  public static function purifyHtmlAndModifyInput(&$item)
200  {
201  assert('is_scalar($item) || empty($item)');
202  if (!empty($item))
203  {
204  $item = static::purifyHtml($item);
205  }
206  }
207 
208  protected static function isNotMarkedSkipped($attributeName)
209  {
210  return !(in_array($attributeName, static::$skippedAttributes));
211  }
212  }
213 ?>
static getDesignerType($model, $attributeName)
static sanitizeDataByDesignerTypeForSavingModel($model, $data)
Definition: DataUtil.php:49
static convertDateTimeLocaleFormattedDisplayToDbFormattedDateTimeWithSecondsAsZero($localeFormattedDateTime)
static purifyHtmlAndModifyInput(&$item)
Definition: DataUtil.php:199
static removeElementFromDataForSavingModel($sanitizedData, $elementName)
Definition: DataUtil.php:149
static purifyHtml($text)
Definition: DataUtil.php:165
static sanitizeDataToJustHavingElementForSavingModel($sanitizedData, $elementName)
Definition: DataUtil.php:133
Generated on Fri Jun 5 2020 07:10:28