Commit | Line | Data |
---|---|---|
a9396478 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
a9396478 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
a9396478 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
a9396478 TO |
27 | |
28 | /** | |
29 | * Base class for writing API_Wrappers which generically manipulate the content | |
30 | * of all fields (except for some black-listed skip-fields). | |
31 | * | |
32 | * @package CRM | |
e7112fa7 | 33 | * @copyright CiviCRM LLC (c) 2004-2015 |
a9396478 TO |
34 | */ |
35 | ||
36 | require_once 'api/Wrapper.php'; | |
5bc392e6 EM |
37 | |
38 | /** | |
39 | * Class CRM_Utils_API_AbstractFieldCoder | |
40 | */ | |
a9396478 TO |
41 | abstract class CRM_Utils_API_AbstractFieldCoder implements API_Wrapper { |
42 | ||
43 | /** | |
44 | * @return array<string> list of field names | |
45 | */ | |
46 | public function getSkipFields() { | |
47 | return NULL; | |
48 | } | |
49 | ||
50 | /** | |
51 | * @param string $fldName | |
a6c01b45 CW |
52 | * @return bool |
53 | * TRUE if encoding should be skipped for this field | |
a9396478 TO |
54 | */ |
55 | public function isSkippedField($fldName) { | |
56 | $skipFields = $this->getSkipFields(); | |
57 | if ($skipFields === NULL) { | |
58 | return FALSE; | |
59 | } | |
60 | ||
61 | // Field should be skipped | |
62 | if (in_array($fldName, $skipFields)) { | |
63 | return TRUE; | |
64 | } | |
65 | // Field is multilingual and after cutting off _xx_YY should be skipped (CRM-7230)… | |
66 | if ((preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $fldName) && in_array(substr($fldName, 0, -6), $skipFields))) { | |
67 | return TRUE; | |
68 | } | |
69 | // Field can take multiple entries, eg. fieldName[1], fieldName[2], etc. | |
70 | // We remove the index and check again if the fieldName in the list of skipped fields. | |
71 | $matches = array(); | |
72 | if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) { | |
73 | return TRUE; | |
74 | } | |
75 | ||
76 | return FALSE; | |
77 | } | |
78 | ||
79 | /** | |
dc195289 | 80 | * going to filter the |
a9396478 TO |
81 | * submitted values. |
82 | * | |
83 | * @param array|string $values the field value from the API | |
84 | */ | |
85 | public abstract function encodeInput(&$values); | |
86 | ||
5bc392e6 EM |
87 | /** |
88 | * @param $values | |
89 | * | |
90 | * @return mixed | |
91 | */ | |
a9396478 TO |
92 | public abstract function decodeOutput(&$values); |
93 | ||
94 | /** | |
da2e61bf | 95 | * @inheritDoc |
a9396478 TO |
96 | */ |
97 | public function fromApiInput($apiRequest) { | |
98 | $lowerAction = strtolower($apiRequest['action']); | |
99 | if ($apiRequest['version'] == 3 && in_array($lowerAction, array('get', 'create'))) { | |
100 | // note: 'getsingle', 'replace', 'update', and chaining all build on top of 'get'/'create' | |
101 | foreach ($apiRequest['params'] as $key => $value) { | |
102 | // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo') | |
103 | // and don't apply to other skippable fields | |
104 | if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) { | |
105 | $this->encodeInput($apiRequest['params'][$key]); | |
106 | } | |
107 | } | |
108 | } | |
109 | elseif ($apiRequest['version'] == 3 && $lowerAction == 'setvalue') { | |
110 | if (isset($apiRequest['params']['field']) && isset($apiRequest['params']['value'])) { | |
111 | if (!$this->isSkippedField($apiRequest['params']['field'])) { | |
112 | $this->encodeInput($apiRequest['params']['value']); | |
113 | } | |
114 | } | |
115 | } | |
116 | return $apiRequest; | |
117 | } | |
118 | ||
119 | /** | |
da2e61bf | 120 | * @inheritDoc |
a9396478 TO |
121 | */ |
122 | public function toApiOutput($apiRequest, $result) { | |
123 | $lowerAction = strtolower($apiRequest['action']); | |
124 | if ($apiRequest['version'] == 3 && in_array($lowerAction, array('get', 'create', 'setvalue'))) { | |
125 | foreach ($result as $key => $value) { | |
126 | // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo') | |
127 | // and don't apply to other skippable fields | |
128 | if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) { | |
129 | $this->decodeOutput($result[$key]); | |
130 | } | |
131 | } | |
132 | } | |
133 | // setvalue? | |
134 | return $result; | |
135 | } | |
136 | ||
137 | /** | |
77b97be7 EM |
138 | * @param $key |
139 | * | |
a9396478 TO |
140 | * @return bool |
141 | */ | |
142 | protected function isApiControlField($key) { | |
143 | return (FALSE !== strpos($key, '.')); | |
144 | } | |
96025800 | 145 | |
a9396478 | 146 | } |