Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 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 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
0c3a6e64 | 35 | * Api Explorer |
6a488035 TO |
36 | */ |
37 | class CRM_Admin_Page_APIExplorer extends CRM_Core_Page { | |
38 | ||
7d16f66d SL |
39 | /** |
40 | * Return unique paths for checking for examples. | |
41 | * @return array | |
42 | */ | |
43 | private static function uniquePaths() { | |
44 | // Ensure that paths with trailing slashes are properly dealt with | |
45 | $paths = explode(PATH_SEPARATOR, get_include_path()); | |
46 | foreach ($paths as $id => $rawPath) { | |
47 | $pathParts = explode(DIRECTORY_SEPARATOR, $rawPath); | |
48 | foreach ($pathParts as $partId => $part) { | |
49 | if (empty($part)) { | |
50 | unset($pathParts[$partId]); | |
51 | } | |
52 | } | |
53 | $newRawPath = implode(DIRECTORY_SEPARATOR, $pathParts); | |
54 | if ($newRawPath != $rawPath) { | |
55 | $paths[$id] = DIRECTORY_SEPARATOR . $newRawPath; | |
56 | } | |
57 | } | |
58 | $paths = array_unique($paths); | |
59 | return $paths; | |
60 | } | |
61 | ||
e0ef6999 | 62 | /** |
ce064e4f | 63 | * Run page. |
64 | * | |
e0ef6999 EM |
65 | * @return string |
66 | */ | |
00be9182 | 67 | public function run() { |
2b6e1174 CW |
68 | CRM_Core_Resources::singleton() |
69 | ->addScriptFile('civicrm', 'templates/CRM/Admin/Page/APIExplorer.js') | |
37d79aed | 70 | ->addScriptFile('civicrm', 'bower_components/google-code-prettify/bin/prettify.min.js', 99) |
5b46e216 | 71 | ->addStyleFile('civicrm', 'bower_components/google-code-prettify/bin/prettify.min.css', 99) |
be2fb01f | 72 | ->addVars('explorer', ['max_joins' => \Civi\API\Api3SelectQuery::MAX_JOINS]); |
89ee60d5 | 73 | |
e4176358 | 74 | $this->assign('operators', CRM_Core_DAO::acceptedSQLOperators()); |
89ee60d5 CW |
75 | |
76 | // List example directories | |
7d16f66d | 77 | // use get_include_path to ensure that extensions are captured. |
be2fb01f | 78 | $examples = []; |
7d16f66d SL |
79 | $paths = self::uniquePaths(); |
80 | foreach ($paths as $path) { | |
81 | $dir = \CRM_Utils_File::addTrailingSlash($path) . 'api' . DIRECTORY_SEPARATOR . 'v3' . DIRECTORY_SEPARATOR . 'examples'; | |
82 | if (is_dir($dir)) { | |
83 | foreach (scandir($dir) as $item) { | |
84 | if ($item && strpos($item, '.') === FALSE && array_search($item, $examples) === FALSE) { | |
85 | $examples[] = $item; | |
86 | } | |
87 | } | |
89ee60d5 CW |
88 | } |
89 | } | |
7d16f66d | 90 | sort($examples); |
89ee60d5 CW |
91 | $this->assign('examples', $examples); |
92 | ||
6a488035 TO |
93 | return parent::run(); |
94 | } | |
95 | ||
6a488035 TO |
96 | /** |
97 | * Get user context. | |
98 | * | |
a6c01b45 CW |
99 | * @return string |
100 | * user context. | |
6a488035 | 101 | */ |
00be9182 | 102 | public function userContext() { |
d5a9020d | 103 | return 'civicrm/api'; |
6a488035 | 104 | } |
96025800 | 105 | |
89ee60d5 | 106 | /** |
ce064e4f | 107 | * AJAX callback to fetch examples. |
89ee60d5 CW |
108 | */ |
109 | public static function getExampleFile() { | |
89ee60d5 | 110 | if (!empty($_GET['entity']) && strpos($_GET['entity'], '.') === FALSE) { |
be2fb01f | 111 | $examples = []; |
7d16f66d SL |
112 | $paths = self::uniquePaths(); |
113 | foreach ($paths as $path) { | |
114 | $dir = \CRM_Utils_File::addTrailingSlash($path) . 'api' . DIRECTORY_SEPARATOR . 'v3' . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . $_GET['entity']; | |
115 | if (is_dir($dir)) { | |
116 | foreach (scandir($dir) as $item) { | |
117 | $item = str_replace('.php', '', $item); | |
118 | if ($item && strpos($item, '.') === FALSE) { | |
be2fb01f | 119 | $examples[] = ['key' => $item, 'value' => $item]; |
7d16f66d SL |
120 | } |
121 | } | |
89ee60d5 CW |
122 | } |
123 | } | |
124 | CRM_Utils_JSON::output($examples); | |
125 | } | |
126 | if (!empty($_GET['file']) && strpos($_GET['file'], '.') === FALSE) { | |
7d16f66d SL |
127 | $paths = self::uniquePaths(); |
128 | $fileFound = FALSE; | |
129 | foreach ($paths as $path) { | |
130 | $fileName = \CRM_Utils_File::addTrailingSlash($path) . 'api' . DIRECTORY_SEPARATOR . 'v3' . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . $_GET['file'] . '.php'; | |
131 | if (!$fileFound && file_exists($fileName)) { | |
132 | $fileFound = TRUE; | |
133 | echo file_get_contents($fileName); | |
134 | } | |
89ee60d5 | 135 | } |
7d16f66d | 136 | if (!$fileFound) { |
89ee60d5 CW |
137 | echo "Not found."; |
138 | } | |
139 | CRM_Utils_System::civiExit(); | |
140 | } | |
bc4aa590 CW |
141 | CRM_Utils_System::permissionDenied(); |
142 | } | |
143 | ||
144 | /** | |
ce064e4f | 145 | * Ajax callback to display code docs. |
bc4aa590 CW |
146 | */ |
147 | public static function getDoc() { | |
e80f05b2 CB |
148 | // Verify the API handler we're talking to is valid. |
149 | $entities = civicrm_api3('Entity', 'get'); | |
6469e038 CW |
150 | $entity = CRM_Utils_Array::value('entity', $_GET); |
151 | if (!empty($entity) && in_array($entity, $entities['values']) && strpos($entity, '.') === FALSE) { | |
bc4aa590 CW |
152 | $action = CRM_Utils_Array::value('action', $_GET); |
153 | $doc = self::getDocblock($entity, $action); | |
be2fb01f | 154 | $result = [ |
bc4aa590 CW |
155 | 'doc' => $doc ? self::formatDocBlock($doc[0]) : 'Not found.', |
156 | 'code' => $doc ? $doc[1] : NULL, | |
cf3a4f07 | 157 | 'file' => $doc ? $doc[2] : NULL, |
be2fb01f | 158 | ]; |
bc4aa590 CW |
159 | if (!$action) { |
160 | $actions = civicrm_api3($entity, 'getactions'); | |
161 | $result['actions'] = CRM_Utils_Array::makeNonAssociative(array_combine($actions['values'], $actions['values'])); | |
162 | } | |
163 | CRM_Utils_JSON::output($result); | |
164 | } | |
165 | CRM_Utils_System::permissionDenied(); | |
166 | } | |
167 | ||
168 | /** | |
ce064e4f | 169 | * Get documentation block. |
170 | * | |
bc4aa590 CW |
171 | * @param string $entity |
172 | * @param string|null $action | |
173 | * @return array|bool | |
174 | * [docblock, code] | |
175 | */ | |
176 | private static function getDocBlock($entity, $action) { | |
177 | if (!$entity) { | |
178 | return FALSE; | |
179 | } | |
cf3a4f07 CW |
180 | $file = "api/v3/$entity.php"; |
181 | $contents = file_get_contents($file, FILE_USE_INCLUDE_PATH); | |
bc4aa590 CW |
182 | if (!$contents) { |
183 | // Api does not exist | |
184 | return FALSE; | |
185 | } | |
be2fb01f | 186 | $docblock = $code = []; |
bc4aa590 CW |
187 | // Fetch docblock for the api file |
188 | if (!$action) { | |
189 | if (preg_match('#/\*\*\n.*?\n \*/\n#s', $contents, $docblock)) { | |
be2fb01f | 190 | return [$docblock[0], NULL, $file]; |
bc4aa590 CW |
191 | } |
192 | } | |
193 | // Fetch block for a specific action | |
194 | else { | |
195 | $action = strtolower($action); | |
196 | $fnName = 'civicrm_api3_' . _civicrm_api_get_entity_name_from_camel($entity) . '_' . $action; | |
197 | // Support the alternate "1 file per action" structure | |
cf3a4f07 CW |
198 | $actionFile = "api/v3/$entity/" . ucfirst($action) . '.php'; |
199 | $actionFileContents = file_get_contents("api/v3/$entity/" . ucfirst($action) . '.php', FILE_USE_INCLUDE_PATH); | |
200 | if ($actionFileContents) { | |
201 | $file = $actionFile; | |
202 | $contents = $actionFileContents; | |
bc4aa590 CW |
203 | } |
204 | // If action isn't in this file, try generic | |
41bd5fcb | 205 | if (strpos($contents, "function $fnName") === FALSE) { |
bc4aa590 | 206 | $fnName = "civicrm_api3_generic_$action"; |
cf3a4f07 CW |
207 | $file = "api/v3/Generic/" . ucfirst($action) . '.php'; |
208 | $contents = file_get_contents($file, FILE_USE_INCLUDE_PATH); | |
bc4aa590 | 209 | if (!$contents) { |
cf3a4f07 CW |
210 | $file = "api/v3/Generic.php"; |
211 | $contents = file_get_contents($file, FILE_USE_INCLUDE_PATH); | |
bc4aa590 CW |
212 | } |
213 | } | |
214 | if (preg_match('#(/\*\*(\n \*.*)*\n \*/\n)function[ ]+' . $fnName . '#i', $contents, $docblock)) { | |
215 | // Fetch the code in a separate regex to preserve sanity | |
216 | preg_match("#^function[ ]+$fnName.*?^}#ism", $contents, $code); | |
be2fb01f | 217 | return [$docblock[1], $code[0], $file]; |
bc4aa590 CW |
218 | } |
219 | } | |
220 | } | |
221 | ||
222 | /** | |
223 | * Format a docblock to be a bit more readable | |
224 | * Not a proper doc parser... patches welcome :) | |
225 | * | |
226 | * @param string $text | |
227 | * @return string | |
228 | */ | |
eecd39e1 TO |
229 | public static function formatDocBlock($text) { |
230 | // Normalize #leading spaces. | |
231 | $lines = explode("\n", $text); | |
232 | $lines = preg_replace('/^ +\*/', ' *', $lines); | |
233 | $text = implode("\n", $lines); | |
234 | ||
bc4aa590 | 235 | // Get rid of comment stars |
be2fb01f | 236 | $text = str_replace(["\n * ", "\n *\n", "\n */\n", "/**\n"], ["\n", "\n\n", '', ''], $text); |
bc4aa590 CW |
237 | |
238 | // Format for html | |
239 | $text = htmlspecialchars($text); | |
240 | ||
241 | // Extract code blocks - save for later to skip html conversion | |
be2fb01f | 242 | $code = []; |
bc4aa590 CW |
243 | preg_match_all('#@code(.*?)@endcode#is', $text, $code); |
244 | $text = preg_replace('#@code.*?@endcode#is', '<pre></pre>', $text); | |
245 | ||
246 | // Convert @annotations to titles | |
247 | $text = preg_replace_callback( | |
248 | '#^[ ]*@(\w+)([ ]*)#m', | |
249 | function($matches) { | |
250 | return "<strong>" . ucfirst($matches[1]) . "</strong>" . (empty($matches[2]) ? '' : ': '); | |
251 | }, | |
252 | $text); | |
253 | ||
254 | // Preserve indentation | |
255 | $text = str_replace("\n ", "\n ", $text); | |
256 | ||
257 | // Convert newlines | |
258 | $text = nl2br($text); | |
259 | ||
260 | // Add unformatted code blocks back in | |
261 | if ($code && !empty($code[1])) { | |
262 | foreach ($code[1] as $block) { | |
fea52a54 | 263 | $text = preg_replace('#<pre></pre>#', "<pre>$block</pre>", $text, 1); |
bc4aa590 CW |
264 | } |
265 | } | |
266 | return $text; | |
89ee60d5 CW |
267 | } |
268 | ||
6a488035 | 269 | } |