| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 5 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2018 | |
| 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 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @package CRM |
| 31 | * @copyright CiviCRM LLC (c) 2004-2018 |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Class handles functions for JSON format |
| 36 | */ |
| 37 | class CRM_Utils_JSON { |
| 38 | |
| 39 | /** |
| 40 | * Output json to the client. |
| 41 | * @param mixed $input |
| 42 | */ |
| 43 | public static function output($input) { |
| 44 | CRM_Utils_System::setHttpHeader('Content-Type', 'application/json'); |
| 45 | echo json_encode($input); |
| 46 | CRM_Utils_System::civiExit(); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Do not use this function. See CRM-16353. |
| 51 | * @deprecated |
| 52 | * |
| 53 | * @param array $params |
| 54 | * Associated array of row elements. |
| 55 | * @param int $sEcho |
| 56 | * Datatable needs this to make it more secure. |
| 57 | * @param int $iTotal |
| 58 | * Total records. |
| 59 | * @param int $iFilteredTotal |
| 60 | * Total records on a page. |
| 61 | * @param array $selectorElements |
| 62 | * Selector elements. |
| 63 | * @return string |
| 64 | */ |
| 65 | public static function encodeDataTableSelector($params, $sEcho, $iTotal, $iFilteredTotal, $selectorElements) { |
| 66 | $sOutput = '{'; |
| 67 | $sOutput .= '"sEcho": ' . intval($sEcho) . ', '; |
| 68 | $sOutput .= '"iTotalRecords": ' . $iTotal . ', '; |
| 69 | $sOutput .= '"iTotalDisplayRecords": ' . $iFilteredTotal . ', '; |
| 70 | $sOutput .= '"aaData": [ '; |
| 71 | foreach ((array) $params as $key => $value) { |
| 72 | $addcomma = FALSE; |
| 73 | $sOutput .= "["; |
| 74 | foreach ($selectorElements as $element) { |
| 75 | if ($addcomma) { |
| 76 | $sOutput .= ","; |
| 77 | } |
| 78 | // CRM-7130 --lets addslashes to only double quotes, |
| 79 | // since we are using it to quote the field value. |
| 80 | // str_replace helps to provide a break for new-line |
| 81 | $sOutput .= '"' . addcslashes(str_replace(array("\r\n", "\n", "\r"), '<br />', $value[$element]), '"\\') . '"'; |
| 82 | |
| 83 | // remove extra spaces and tab character that breaks dataTable CRM-12551 |
| 84 | $sOutput = preg_replace("/\s+/", " ", $sOutput); |
| 85 | $addcomma = TRUE; |
| 86 | } |
| 87 | $sOutput .= "],"; |
| 88 | } |
| 89 | $sOutput = substr_replace($sOutput, "", -1); |
| 90 | $sOutput .= '] }'; |
| 91 | |
| 92 | return $sOutput; |
| 93 | } |
| 94 | |
| 95 | } |