/** * Decodes JSON sting. * * @static * * @param string $data * @param bool $asArray get result as array instead of object * * @return mixed */ public static function decodeJson($data, $asArray = true) { if (self::$json === null) { self::$json = new CJson(); } return self::$json->decode($data, $asArray); }
/** * convert string with data in JSON format to php array. * * @param string $string * * @return array */ public function read($string) { $json = new CJson(); return $json->decode($string, true); }
$colors['Yellow'] = imagecolorallocate($im, 255, 255, 0); $colors['Dark Yellow'] = imagecolorallocate($im, 150, 150, 0); $colors['Cyan'] = imagecolorallocate($im, 0, 255, 255); $colors['Black'] = imagecolorallocate($im, 0, 0, 0); $colors['Gray'] = imagecolorallocate($im, 150, 150, 150); $colors['White'] = imagecolorallocate($im, 255, 255, 255); $colors['Orange'] = imagecolorallocate($im, 238, 96, 0); $x = imagesx($im); $y = imagesy($im); /* * Actions */ $json = new CJson(); if (isset($_REQUEST['selements']) || isset($_REQUEST['noselements'])) { $map['selements'] = getRequest('selements', '[]'); $map['selements'] = $json->decode($map['selements'], true); } else { add_elementNames($map['selements']); } if (isset($_REQUEST['links']) || isset($_REQUEST['nolinks'])) { $map['links'] = getRequest('links', '[]'); $map['links'] = $json->decode($map['links'], true); } if (getRequest('nocalculations', false)) { foreach ($map['selements'] as $selement) { if ($selement['elementtype'] != SYSMAP_ELEMENT_TYPE_IMAGE) { add_elementNames($map['selements']); break; } } // get default iconmap id to use for elements that use icon map
* Ajax */ if (isset($_REQUEST['favobj'])) { $json = new CJson(); // if ($_REQUEST['favobj'] == 'sysmap' && getRequest('action') == 'update') { if (getRequest('favobj') == 'sysmap' && hasRequest('action') && getRequest('action') == 'update') { $sysmapid = getRequest('sysmapid', 0); @ob_start(); try { DBstart(); $sysmap = API::Map()->get(array('sysmapids' => $sysmapid, 'editable' => true, 'output' => array('sysmapid'))); $sysmap = reset($sysmap); if ($sysmap === false) { throw new Exception(_('Access denied!') . "\n\r"); } $sysmapUpdate = $json->decode($_REQUEST['sysmap'], true); $sysmapUpdate['sysmapid'] = $sysmapid; $result = API::Map()->update($sysmapUpdate); if ($result !== false) { echo 'if (confirm(' . CJs::encodeJson(_('Map is updated! Return?')) . ')) { location.href = "sysmaps.php"; }'; } else { throw new Exception(_('Map update failed.') . "\n\r"); } DBend(true); } catch (Exception $e) { DBend(false); $msg = array($e->getMessage()); foreach (clear_messages() as $errMsg) { $msg[] = $errMsg['type'] . ': ' . $errMsg['message']; } ob_clean();
public function testSubscribeContactsForReportType() { $type = 'report'; $report = SavedReportTestHelper::makeSimpleContactRowsAndColumnsReport(); $marketingList = MarketingListTestHelper::createMarketingListByName('MarketingList5', 'MarketingList Description5'); $marketingListId = $marketingList->id; $contactCount = Contact::getCount(); $this->assertNotNull($report); $this->setGetArray(array('marketingListId' => $marketingListId, 'id' => $report->id, 'type' => $type)); $content = $this->runControllerWithNoExceptionsAndGetContent('marketingLists/defaultPortlet/subscribeContacts'); $contentArray = CJson::decode($content); $this->assertNotEmpty($contentArray); $this->assertArrayHasKey('type', $contentArray); $this->assertArrayHasKey('message', $contentArray); $this->assertEquals($contactCount . ' subscribed.', $contentArray['message']); $this->assertEquals('message', $contentArray['type']); $content = $this->runControllerWithNoExceptionsAndGetContent('marketingLists/defaultPortlet/subscribeContacts'); $contentArray = CJson::decode($content); $this->assertNotEmpty($contentArray); $this->assertArrayHasKey('type', $contentArray); $this->assertArrayHasKey('message', $contentArray); $this->assertEquals('0 subscribed. ' . $contactCount . ' skipped, already in the list.', $contentArray['message']); $this->assertEquals('message', $contentArray['type']); Yii::app()->pagination->setForCurrentUserByType('reportResultsListPageSize', 1); $this->setGetArray(array('marketingListId' => $marketingListId, 'id' => $report->id, 'type' => $type, 'page' => 2)); $content = $this->runControllerWithNoExceptionsAndGetContent('marketingLists/defaultPortlet/subscribeContacts'); $contentArray = CJson::decode($content); $percent = round(2 / $contactCount * 100, 0); // Not Coding Standard $this->assertNotEmpty($contentArray); $this->assertArrayHasKey('type', $contentArray); $this->assertArrayHasKey('message', $contentArray); $this->assertEquals("Processing: {$percent} % complete", $contentArray['message']); $this->assertEquals('message', $contentArray['type']); $this->assertEquals(3, $contentArray['nextPage']); $this->assertEquals(0, $contentArray['subscribedCount']); $this->assertEquals(1, $contentArray['skippedCount']); $this->setGetArray(array('marketingListId' => $marketingListId, 'id' => $report->id, 'type' => $type, 'page' => $contactCount, 'skippedCount' => $contactCount - 1, 'subscribedCount' => 0)); $content = $this->runControllerWithNoExceptionsAndGetContent('marketingLists/defaultPortlet/subscribeContacts'); $contentArray = CJson::decode($content); $this->assertNotEmpty($contentArray); $this->assertArrayHasKey('type', $contentArray); $this->assertArrayHasKey('message', $contentArray); $this->assertEquals("0 subscribed. {$contactCount} skipped, already in the list.", $contentArray['message']); $this->assertEquals('message', $contentArray['type']); }
** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. **/ require_once dirname(__FILE__) . '/include/config.inc.php'; $requestType = getRequest('type', PAGE_TYPE_JSON); if ($requestType == PAGE_TYPE_JSON) { $http_request = new CHttpRequest(); $json = new CJson(); $data = $json->decode($http_request->body(), true); } else { $data = $_REQUEST; } $page['title'] = 'RPC'; $page['file'] = 'jsrpc.php'; $page['hist_arg'] = array(); $page['type'] = detect_page_type($requestType); require_once dirname(__FILE__) . '/include/page_header.php'; if (!is_array($data) || !isset($data['method']) || $requestType == PAGE_TYPE_JSON && (!isset($data['params']) || !is_array($data['params']))) { fatal_error('Wrong RPC call to JS RPC!'); } $result = array(); switch ($data['method']) { case 'host.get': $result = API::Host()->get(array('startSearch' => true, 'search' => $data['params']['search'], 'output' => array('hostid', 'host', 'name'), 'sortfield' => 'name', 'limit' => 15));
protected static function readAndAddConfig($configFp, $configName, $configs) { if (CMap::hasKey(self::$ms_configAliases, $configName)) { $configName = self::$ms_configAliases[$configName]; } $configName = CString::toLowerCase($configName); $configJson = CFile::read($configFp); $configJson = CRegex::remove($configJson, "/^\\h*\\/\\/.*/m"); // remove comments $configJson = "{\"{$configName}\": {$configJson}}"; $json = new CJson($configJson); $success; $config = $json->decode($success); assert('$success', vs(isset($this), get_defined_vars())); CArray::push($configs, $config); }
/** * execute when object is unserialized * @return mixed */ public function __wakeup() { return CJson::decode($this); }
public function testEncode() { $value = m(["prop0" => "value", "prop1" => a("value0", "value1"), "prop2" => m(["subProp0" => "value0", "subProp1" => "value1"]), "prop3" => a(true, 1234, 56.78)]); $json = new CJson($value); $encJson = $json->encode(); $json = new CJson($encJson); $success; $decMap = $json->decode($success); $this->assertTrue($success && $decMap->equals($value)); }
/** * Filters a string or a collection of strings according to the expected output type(s) and returns the output * value(s). * * @param mixed $inputStringOrDecodedCollection The string to be filtered or the array or map containing the * strings to be filtered. If the parameter's value is a JSON-encoded string, the output value is going to be * either an array or map. * @param reference $success **OUTPUT.** After the method is called, the value of this parameter tells whether * the filtering was successful. * * @return mixed The output value or a collection of values of the expected type(s) after having been put through * the filter. */ public function filter($inputStringOrDecodedCollection, &$success) { assert('is_cstring($inputStringOrDecodedCollection) || is_collection($inputStringOrDecodedCollection)', vs(isset($this), get_defined_vars())); $success = true; if ($this->m_expectedType != self::CARRAY && $this->m_expectedType != self::CMAP) { // The expected output type is not a collection; the input value must be of string type. if (!is_cstring($inputStringOrDecodedCollection)) { $success = false; return oop_x($this->m_defaultValue); } $inputString = $inputStringOrDecodedCollection; if ($this->m_expectedType == self::BOOL || $this->m_expectedType == self::INT || $this->m_expectedType == self::FLOAT || $this->m_expectedType == self::EMAIL || $this->m_expectedType == self::URL || $this->m_expectedType == self::IP) { // Trim the input string on both sides from whitespace, including Unicode whitespace and control // characters. $trimmingSubjectRe = CUString::TRIMMING_AND_SPACING_NORM_SUBJECT_RE; $inputString = CRegex::remove($inputString, "/^({$trimmingSubjectRe})+|({$trimmingSubjectRe})+\\z/u"); } // Pre-process the string for integer and floating-point types. $looksLikeHex; if ($this->m_expectedType == self::INT || $this->m_expectedType == self::FLOAT) { if (CString::startsWith($inputString, "+")) { // Remove the plus sign. $inputString = CString::substr($inputString, 1); } $looksLikeHex = CRegex::find($inputString, "/^-?0x/i"); if ($this->m_allowLeadingZeros && !($this->m_expectedType == self::INT && $this->m_allowHex && $looksLikeHex)) { // Remove any leading zeros (except for special cases). $inputString = CRegex::replace($inputString, "/^(\\D*)0*(?!\\b)/", "\$1"); } if ($this->m_allowComma) { $inputString = CRegex::remove($inputString, "/,(?=\\d{3}\\b)/"); } } // Validate and sanitize the value according to its expected type. if ($this->m_expectedType == self::BOOL) { if (!CRegex::find($inputString, "/^(1|true|yes|on|0|false|no|off)\\z/i")) { $success = false; return $this->m_defaultValue; } return CString::equals($inputString, "1") || CString::equalsCi($inputString, "true") || CString::equalsCi($inputString, "yes") || CString::equalsCi($inputString, "on"); } if ($this->m_expectedType == self::INT) { $value; if (!($this->m_allowHex && $looksLikeHex)) { // Regular. if (!CRegex::find($inputString, "/^-?(?!0(?!\\b))\\d+\\z/")) { $success = false; return $this->m_defaultValue; } $value = CString::toInt($inputString); } else { // Hex. if (!CRegex::find($inputString, "/^-?0x[0-9A-F]+\\z/i")) { $success = false; return $this->m_defaultValue; } $value = CString::toIntFromHex($inputString); } if (isset($this->m_intValidMin) && $value < $this->m_intValidMin || isset($this->m_intValidMax) && $value > $this->m_intValidMax) { $success = false; return $this->m_defaultValue; } if (isset($this->m_intClampingMin) && $value < $this->m_intClampingMin) { $value = $this->m_intClampingMin; } if (isset($this->m_intClampingMax) && $value > $this->m_intClampingMax) { $value = $this->m_intClampingMax; } return $value; } if ($this->m_expectedType == self::FLOAT) { if (!CRegex::find($inputString, "/^-?(?!0(?!\\b))\\d*\\.?\\d+(e[\\-+]?\\d+)?\\z/i")) { $success = false; return $this->m_defaultValue; } $value = CString::toFloat($inputString); if (isset($this->m_floatValidMin) && $value < $this->m_floatValidMin || isset($this->m_floatValidMax) && $value > $this->m_floatValidMax) { $success = false; return $this->m_defaultValue; } if (isset($this->m_floatClampingMin) && $value < $this->m_floatClampingMin) { $value = $this->m_floatClampingMin; } if (isset($this->m_floatClampingMax) && $value > $this->m_floatClampingMax) { $value = $this->m_floatClampingMax; } return $value; } if ($this->m_expectedType == self::CSTRING) { $value = $inputString; if (!CString::isValid($value)) { $success = false; return $this->m_defaultValue; } if (!$this->m_keepAbnormalNewlines) { $value = CString::normNewlines($value); } if (!$this->m_keepNonPrintable) { if (!$this->m_keepTabsAndNewlines) { $value = CRegex::remove($value, "/[\\x00-\\x1F\\x7F-\\xFF]/"); } else { $value = CRegex::remove($value, "/[\\x00-\\x1F\\x7F-\\xFF](?<![\\x09\\x0A\\x0D])/"); } } else { if (!$this->m_keepTabsAndNewlines) { $value = CRegex::remove($value, "/[\\x09\\x0A\\x0D]/"); } } if (!$this->m_keepSideSpacing) { $value = CString::trim($value); } if (!$this->m_keepExtraSpacing) { $value = CString::normSpacing($value); } return $value; } if ($this->m_expectedType == self::CUSTRING) { $value = $inputString; if (!CUString::isValid($value)) { $success = false; return $this->m_defaultValue; } if (!$this->m_keepAbnormalNewlines) { $value = CUString::normNewlines($value); } if (!$this->m_keepNonPrintable) { if (!$this->m_keepTabsAndNewlines) { $value = CRegex::remove($value, "/\\p{C}|\\p{Zl}|\\p{Zp}/u"); } else { $value = CRegex::remove($value, "/\\p{C}(?<!\\x{0009}|\\x{000A}|\\x{000D})/u"); } } else { if (!$this->m_keepTabsAndNewlines) { $value = CRegex::remove($value, "/\\x{0009}|\\x{000A}|\\x{000D}|\\p{Zl}|\\p{Zp}/u"); } } if (!$this->m_keepSideSpacing) { $value = CUString::trim($value); } if (!$this->m_keepExtraSpacing) { $value = CUString::normSpacing($value); } return $value; } if ($this->m_expectedType == self::EMAIL) { $value = filter_var($inputString, FILTER_VALIDATE_EMAIL); if (!is_cstring($value)) { $success = false; return $this->m_defaultValue; } return $value; } if ($this->m_expectedType == self::URL) { $value = $inputString; if (!CUrl::isValid($value, $this->m_ignoreProtocolAbsence)) { $success = false; return $this->m_defaultValue; } if ($this->m_ignoreProtocolAbsence) { $value = CUrl::ensureProtocol($value); } return $value; } if ($this->m_expectedType == self::IP) { $value = $inputString; $options = CBitField::ALL_UNSET; if (!$this->m_allowPrivateRange) { $options |= CIp::DISALLOW_PRIVATE_RANGE; } if (!$this->m_allowReservedRange) { $options |= CIp::DISALLOW_RESERVED_RANGE; } $isValid; if (!$this->m_ipV6 && !$this->m_ipV4OrV6) { $isValid = CIp::isValidV4($value, $options); } else { if (!$this->m_ipV4OrV6) { $isValid = CIp::isValidV6($value, $options); } else { $isValid = CIp::isValidV4($value, $options) || CIp::isValidV6($value, $options); } } if (!$isValid) { $success = false; return $this->m_defaultValue; } return $value; } } else { if ($this->m_expectedType == self::CARRAY) { if (!is_cstring($inputStringOrDecodedCollection) && !is_carray($inputStringOrDecodedCollection)) { $success = false; return oop_x($this->m_defaultValue); } $value; if (is_cstring($inputStringOrDecodedCollection)) { // Assume JSON format for the input string. $json = new CJson($inputStringOrDecodedCollection, $this->m_jsonStrictness); $value = $json->decode($success); if (!$success) { return oop_x($this->m_defaultValue); } if (!is_carray($value)) { $success = false; return oop_x($this->m_defaultValue); } } else { $value = $inputStringOrDecodedCollection; } $value = self::recurseCollectionFiltering($value, $this->m_collectionInputFilters, $success, 0); if (!$success) { return oop_x($this->m_defaultValue); } return $value; } else { if (!is_cstring($inputStringOrDecodedCollection) && !is_cmap($inputStringOrDecodedCollection)) { $success = false; return oop_x($this->m_defaultValue); } $value; if (is_cstring($inputStringOrDecodedCollection)) { // Assume JSON format for the input string. $json = new CJson($inputStringOrDecodedCollection, $this->m_jsonStrictness); $value = $json->decode($success); if (!$success) { return oop_x($this->m_defaultValue); } if (!is_cmap($value)) { $success = false; return oop_x($this->m_defaultValue); } } else { $value = $inputStringOrDecodedCollection; } $value = self::recurseCollectionFiltering($value, $this->m_collectionInputFilters, $success, 0); if (!$success) { return oop_x($this->m_defaultValue); } return $value; } } }