Commit | Line | Data |
---|---|---|
a9396478 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
a9396478 | 5 | +--------------------------------------------------------------------+ |
8c9251b3 | 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
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 | |
8c9251b3 | 33 | * @copyright CiviCRM LLC (c) 2004-2018 |
a9396478 TO |
34 | */ |
35 | ||
36 | require_once 'api/Wrapper.php'; | |
5bc392e6 EM |
37 | |
38 | /** | |
3d469574 | 39 | * Class CRM_Utils_API_AbstractFieldCoder. |
5bc392e6 | 40 | */ |
a9396478 TO |
41 | abstract class CRM_Utils_API_AbstractFieldCoder implements API_Wrapper { |
42 | ||
43 | /** | |
3d469574 | 44 | * Get skipped fields. |
45 | * | |
46 | * @return array<string> | |
47 | * List of field names | |
a9396478 TO |
48 | */ |
49 | public function getSkipFields() { | |
50 | return NULL; | |
51 | } | |
52 | ||
53 | /** | |
3d469574 | 54 | * Is field skipped. |
55 | * | |
a9396478 | 56 | * @param string $fldName |
e8e8f3ad | 57 | * |
a6c01b45 CW |
58 | * @return bool |
59 | * TRUE if encoding should be skipped for this field | |
a9396478 TO |
60 | */ |
61 | public function isSkippedField($fldName) { | |
62 | $skipFields = $this->getSkipFields(); | |
63 | if ($skipFields === NULL) { | |
64 | return FALSE; | |
65 | } | |
66 | ||
67 | // Field should be skipped | |
68 | if (in_array($fldName, $skipFields)) { | |
69 | return TRUE; | |
70 | } | |
71 | // Field is multilingual and after cutting off _xx_YY should be skipped (CRM-7230)… | |
72 | if ((preg_match('/_[a-z][a-z]_[A-Z][A-Z]$/', $fldName) && in_array(substr($fldName, 0, -6), $skipFields))) { | |
73 | return TRUE; | |
74 | } | |
75 | // Field can take multiple entries, eg. fieldName[1], fieldName[2], etc. | |
76 | // We remove the index and check again if the fieldName in the list of skipped fields. | |
77 | $matches = array(); | |
78 | if (preg_match('/^(.*)\[\d+\]/', $fldName, $matches) && in_array($matches[1], $skipFields)) { | |
79 | return TRUE; | |
80 | } | |
81 | ||
82 | return FALSE; | |
83 | } | |
84 | ||
85 | /** | |
3d469574 | 86 | * Going to filter the submitted values. |
a9396478 TO |
87 | * |
88 | * @param array|string $values the field value from the API | |
89 | */ | |
90 | public abstract function encodeInput(&$values); | |
91 | ||
5bc392e6 | 92 | /** |
3d469574 | 93 | * Decode output. |
94 | * | |
95 | * @param string $values | |
5bc392e6 EM |
96 | * |
97 | * @return mixed | |
98 | */ | |
a9396478 TO |
99 | public abstract function decodeOutput(&$values); |
100 | ||
101 | /** | |
da2e61bf | 102 | * @inheritDoc |
a9396478 TO |
103 | */ |
104 | public function fromApiInput($apiRequest) { | |
105 | $lowerAction = strtolower($apiRequest['action']); | |
106 | if ($apiRequest['version'] == 3 && in_array($lowerAction, array('get', 'create'))) { | |
107 | // note: 'getsingle', 'replace', 'update', and chaining all build on top of 'get'/'create' | |
108 | foreach ($apiRequest['params'] as $key => $value) { | |
109 | // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo') | |
110 | // and don't apply to other skippable fields | |
111 | if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) { | |
112 | $this->encodeInput($apiRequest['params'][$key]); | |
113 | } | |
114 | } | |
115 | } | |
116 | elseif ($apiRequest['version'] == 3 && $lowerAction == 'setvalue') { | |
117 | if (isset($apiRequest['params']['field']) && isset($apiRequest['params']['value'])) { | |
118 | if (!$this->isSkippedField($apiRequest['params']['field'])) { | |
119 | $this->encodeInput($apiRequest['params']['value']); | |
120 | } | |
121 | } | |
122 | } | |
123 | return $apiRequest; | |
124 | } | |
125 | ||
126 | /** | |
da2e61bf | 127 | * @inheritDoc |
a9396478 TO |
128 | */ |
129 | public function toApiOutput($apiRequest, $result) { | |
130 | $lowerAction = strtolower($apiRequest['action']); | |
91f70e70 | 131 | if ($apiRequest['version'] == 3 && in_array($lowerAction, array('get', 'create', 'setvalue', 'getquick'))) { |
a9396478 TO |
132 | foreach ($result as $key => $value) { |
133 | // Don't apply escaping to API control parameters (e.g. 'api.foo' or 'options.foo') | |
134 | // and don't apply to other skippable fields | |
135 | if (!$this->isApiControlField($key) && !$this->isSkippedField($key)) { | |
136 | $this->decodeOutput($result[$key]); | |
137 | } | |
138 | } | |
139 | } | |
140 | // setvalue? | |
141 | return $result; | |
142 | } | |
143 | ||
144 | /** | |
77b97be7 EM |
145 | * @param $key |
146 | * | |
a9396478 TO |
147 | * @return bool |
148 | */ | |
149 | protected function isApiControlField($key) { | |
150 | return (FALSE !== strpos($key, '.')); | |
151 | } | |
96025800 | 152 | |
a9396478 | 153 | } |