/** * Given an array of values, generate the JSON in the Solr format * @param $values * @return string */ function &generateSolrJSON($values) { $result = "["; foreach ($values as $cid => $tokens) { if (empty($tokens)) { continue; } $result .= "\n {\n \"contact_id\" : \"{$cid}\","; foreach ($tokens as $n => $v) { if (is_array($v)) { $str = array(); foreach ($v as $el) { $el = escapeJsonString($el); $str[] = "\"{$el}\""; } $string = implode(",", $str); $result .= "\n \"{$n}\" : [{$string}],"; } else { $v = escapeJsonString($v); $result .= "\n \"{$n}\" : \"{$v}\","; } } // remove the last comma $result = rtrim($result, ","); $result .= "\n },"; } // remove the last comma $result = rtrim($result, ","); $result .= "\n]\n"; return $result; }
function aExit($code, $mess = 'error') { global $ajax_message; $iframe = Filter::input('json_iframe', 'post', 'bool'); $ajax_message['code'] = $code; $ajax_message['message'] = $mess == 'error' ? $mess . ' code: ' . $code : $mess; if (defined('JSON_HEX_QUOT')) { $result = json_encode($ajax_message, JSON_HEX_QUOT | JSON_HEX_APOS | JSON_HEX_TAG); } else { $result = json_encode($ajax_message); } if ($iframe) { $result = escapeJsonString($result); $result = '<html><head><title>jnone</title><script type="text/javascript"> var json_response = "' . $result . '"</script></head><body></body></html>'; } exit($result); }
function CreateJSON() { global $user, $serverhost, $db, $password; global $DesiredDiveSiteId; $connection = mysql_connect($serverhost, $user, $password) or die('ERROR!! Cannot connect to MySql'); $rs = mysql_select_db($db, $connection) or die('ERROR!! Cannot connect to divesites database'); $sql = "select * from DiveSitePOI where DiveSiteId =" . $DesiredDiveSiteId . " order by DiveSitePOIType"; #echo('sql: '.$sql); $result = mysql_query($sql, $connection) or die("ERROR!! DiveSitePOI GetNumRecs failure"); $NumDiveSitePOIRecords = mysql_num_rows($result); mysql_close($connection); if ($NumDiveSitePOIRecords != 0) { echo '['; for ($i = 1; $i <= $NumDiveSitePOIRecords; $i++) { $rowdata = mysql_fetch_row($result); echo "{"; echo '"DiveSitePOIId":"' . $rowdata[0] . '",'; #echo "<td align='left'>".$rowdata[1]." </td>"; #echo "<td align='left'>".$rowdata[2]." </td>"; #echo "<td align='left'>".$rowdata[3]." </td>"; #echo "<td align='left'>".$rowdata[4]." </td>"; #echo "<td align='left'>".$rowdata[5]." </td>"; #echo "<td align='left'>".$rowdata[6]." </td>"; echo '"DiveSiteName":"' . escapeJsonString($rowdata[7]) . '",'; echo '"DiveSiteMajorName":"' . escapeJsonString($rowdata[8]) . '",'; echo '"DiveSiteMinorName":"' . escapeJsonString($rowdata[9]) . '",'; echo '"DiveSiteExactLat":"' . escapeJsonString($rowdata[10]) . '",'; echo '"DiveSiteExactLong":"' . escapeJsonString($rowdata[11]) . '",'; echo '"DiveSitePOITitle":"' . escapeJsonString($rowdata[12]) . '",'; echo '"DiveSitePOIType":"' . escapeJsonString($rowdata[13]) . '",'; echo '"DiveSitePOILat":"' . escapeJsonString($rowdata[14]) . '",'; echo '"DiveSitePOILong":"' . escapeJsonString($rowdata[15]) . '",'; #echo "<td align='left'>".$rowdata[16]." </td>"; echo '"DiveSitePOINotes":"' . escapeJsonString($rowdata[17]) . '"'; #echo "<td align='left'>".$rowdata[18]." </td>"; if ($i != $NumDiveSitePOIRecords) { echo "},"; } else { echo "}"; } } echo "]"; } return; }
public function generateGeoJson($sql, $gp_name) { global $database; if (strlen(trim($parameters)) > 0) { if ($gp_name == "intersects" || $gp_name == "split" || $gp_name == "bbox") { $sql .= " AND " . $parameters; } else { $sql .= " WHERE " . $parameters; } } if (strlen(trim($orderby)) > 0) { $sql .= " ORDER BY " . pg_escape_string($orderby) . " " . $sort; } if (strlen(trim($limit)) > 0) { $sql .= " LIMIT " . pg_escape_string($limit); } if (strlen(trim($offset)) > 0) { $sql .= " OFFSET " . pg_escape_string($offset); } $rs = DBAccess::find_by_sql($sql); $output = ''; $rowOutput = ''; while ($row = $database->fetch_array($rs)) { $rowOutput = (strlen($rowOutput) > 0 ? ',' : '') . '{"type": "Feature", "geometry": ' . $row['gp_geom'] . ', "properties": {'; $props = ''; $id = ''; foreach ($row as $key => $val) { if ($key != "geojson") { $props .= (strlen($props) > 0 ? ',' : '') . '"' . $key . '":"' . escapeJsonString($val) . '"'; } if ($key == "id") { $id .= ',"id":"' . escapeJsonString($val) . '"'; } } $rowOutput .= $props . '}'; $rowOutput .= $id; $rowOutput .= '}'; $output .= $rowOutput; } $output = '{ "type": "FeatureCollection", "features": [ ' . $output . ' ]}'; return $output; }
$output = ''; $rowOutput = ''; $rec_count = pg_num_rows($rs); while ($row = pg_fetch_assoc($rs)) { $rowOutput = (strlen($rowOutput) > 0 ? ',' : '') . '{"type": "Feature", "geometry": ' . $row['geojson'] . ', "properties": {'; $props = ''; $id = ''; //print_r($row);exit; foreach ($row as $key => $val) { if ($key !== 'geojson') { if (strlen($val) > 0) { $props .= (strlen($props) > 0 ? ',' : '') . '"' . $key . '":"' . escapeJsonString($val) . '"'; } } if ($key == 'id') { $id .= ',"id":"' . escapeJsonString($val) . '"'; } } $rowOutput .= $props . '}'; $rowOutput .= $id; $rowOutput .= '}'; $output .= $rowOutput; } $output = '{ "type": "FeatureCollection", "features": [ ' . $output . ' ]}'; if ($rec_count) { if ($redis) { $compressed = gzcompress($output, 9); $redis->set($cachekey, $compressed); } } // echo $output;
function sql2json($data_sql) { $json_str = ""; if ($total = sqlRows($data_sql)) { if ($total > 1) { $json_str .= "{\"___\":["; } $row_count = 0; while ($data = sqlArray($data_sql)) { if (count($data) > 1) { $json_str .= "{"; } $count = 0; foreach ($data as $key => $value) { if (count($data) > 1) { $json_str .= "\"" . $key . "\":"; } if (is_string($value)) { $json_str .= "\""; $value = escapeJsonString($value); } $json_str .= $value; if (is_string($value)) { $json_str .= "\""; } $count++; if ($count < count($data)) { $json_str .= ","; } } $row_count++; if (count($data) > 1) { $json_str .= "}"; } if ($row_count < $total) { $json_str .= ","; } } if ($total > 1) { $json_str .= "]}"; } } else { $json_str = "{}"; } return $json_str; }
function get($company) { //return data for :company $m = new Mongo(getenv("MONGOLAB_URI")); $db = $m->msom0; $collection = $db->companies; $companiesDatas = $collection->find(json_decode('{ "name" : "' . $company . '" }')); if ($companiesDatas->count() > 0) { $x = array_values(iterator_to_array($companiesDatas)); echo json_encode($x[0]); } else { //twitter $twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET); $tweets = $twitter->get('statuses/user_timeline', array('screen_name' => $company, 'count' => 25)); $tweetsEncoded = ""; $count = 0; foreach ($tweets as $tweet) { //sentiment $senUrl = "http://wesoc.herokuapp.com/sentiment/"; $senData = array('texts' => $tweet->text); $options = array('http' => array('header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($senData))); $senContext = stream_context_create($options); $senResult = file_get_contents($senUrl, false, $senContext); //echo json_encode(json_decode($senResult)->texts[0]->text); if ($count > 0) { $tweetsEncoded .= ','; } //eeeeeeeh $tweetsEncoded .= '{' . '"text" : "' . escapeJsonString($tweet->text) . '", "dateTime" : "' . $tweet->created_at . '", "user_name" : "' . $tweet->user->name . '", "verified" : "' . $tweet->user->verified . '", "sentiment" : "' . json_decode($senResult)->texts[0]->sentiment . '" }'; $count += 1; } //facebook $fb = new Facebook(array('appId' => FBAPPID, 'secret' => FBSECRET)); $fbResponse = $fb->api("/" . $company . "/posts"); $fbSelfPostsEncoded = ""; $fcount = 0; foreach ($fbResponse['data'] as $response) { $coreMessage = ""; if ($response["message"] == "") { $coreMessage = $response["story"]; } else { $coreMessage = $response["message"]; } $senUrl = "http://wesoc.herokuapp.com/sentiment/"; $senData = array('texts' => $coreMessage); $options = array('http' => array('header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query($senData))); $senContext = stream_context_create($options); $senResult = file_get_contents($senUrl, false, $senContext); if ($fcount > 0) { $fbSelfPostsEncoded .= ','; } $fbSelfPostsEncoded .= '{' . '"message" : "' . escapeJsonString($coreMessage) . '", "dateTime" : "' . $response["created_time"] . '", "page_name" : "' . $response["from"]["name"] . '", "like_count" : "' . count($response["likes"]["data"]) . '", "sentiment" : "' . json_decode($senResult)->texts[0]->sentiment . '" }'; $fcount += 1; } //sentiment //mash $q = '{ "name" : "' . $company . '", "lastRefresh" :"' . time() . '", "tweets" : [' . $tweetsEncoded . '], "fbSelfPosts" : [' . $fbSelfPostsEncoded . '] }'; //echo echo $q; //insert $collection->insert(json_decode($q)); } }
* If Json string is to long browsers start asking to terminate javascript. * FIX: * Use julienlecomte()net/blog/2007/10/28/, the more reading I do about this subject it seems that off loading to a client is not recomended. */ if (!empty($newFilterRuleSigArray)) { $countSigList = count($newFilterRuleSigArray); echo "\n"; echo 'var snortObjlist = ['; $i = 0; foreach ($newFilterRuleSigArray as $val3) { $i++; // NOTE: escapeJsonString; foward slash has added spaces on each side, ie and chrome were giving issues with tablw widths if ($i !== $countSigList) { echo '{"sid":"' . $val3['sid'] . '","enable":"' . $val3['enable'] . '","proto":"' . $val3['proto'] . '","src":"' . $val3['src'] . '","srcport":"' . $val3['srcport'] . '","dst":"' . $val3['dst'] . '", "dstport":"' . $val3['dstport'] . '","msg":"' . escapeJsonString($val3['msg']) . '"},'; } else { echo '{"sid":"' . $val3['sid'] . '","enable":"' . $val3['enable'] . '","proto":"' . $val3['proto'] . '","src":"' . $val3['src'] . '","srcport":"' . $val3['srcport'] . '","dst":"' . $val3['dst'] . '", "dstport":"' . $val3['dstport'] . '","msg":"' . escapeJsonString($val3['msg']) . '"}'; } } echo '];' . "\n"; } if (!empty($countSig)) { echo 'var countRowAppend = ' . $countSig . ';' . "\n"; } else { echo 'var countRowAppend = 0;' . "\n"; } ?> if(typeof escapeHtmlEntities == 'undefined') { escapeHtmlEntities = function (text) { return text.replace(/[\u00A0-\u2666<>\&]/g, function(c) { return '&' + escapeHtmlEntities.entityTable[c.charCodeAt(0)] || '#'+c.charCodeAt(0) + ';'; });
function getSidBlockJsonArray($getEnableSid) { global $listGenRules, $listSigRules; if (!empty($getEnableSid)) { $i = 0; $countSigList = count($getEnableSid); foreach ($getEnableSid as $val3) { //$listGenRules $listSigRules $snortSigIpsExists = snortSearchArray($listSigRules, 'siguuid', trim($val3['0'])); // if sig is in db use its settings else use default settings if (!empty($snortSigIpsExists['siguuid'])) { $getSid = $snortSigIpsExists['siguuid']; $getEnable = $snortSigIpsExists['enable']; $getWho = $snortSigIpsExists['who']; $getTimeamount = $snortSigIpsExists['timeamount']; $getTimetype = $snortSigIpsExists['timetype']; } else { $getSid = escapeJsonString(trim($val3['0'])); $getEnable = $listGenRules[0]['enable']; $getWho = $listGenRules[0]['who']; $getTimeamount = $listGenRules[0]['timeamount']; $getTimetype = $listGenRules[0]['timetype']; } $i++; if ($i == 1) { $main .= '['; } if ($i == $countSigList) { $main .= '{"sid":"' . $getSid . '","enable":"' . $getEnable . '","who":"' . $getWho . '","timeamount":"' . $getTimeamount . '","timetype":"' . $getTimetype . '","msg":"' . escapeJsonString($val3['1']) . '"}'; } else { $main .= '{"sid":"' . $getSid . '","enable":"' . $getEnable . '","who":"' . $getWho . '","timeamount":"' . $getTimeamount . '","timetype":"' . $getTimetype . '","msg":"' . escapeJsonString($val3['1']) . '"},'; } if ($i == $countSigList) { $main .= ']'; } } // END foreach return $main; } // END of jSON build return false; }
function escapeJsonString(&$x) { # list from www.json.org: (\b backspace, \f formfeed)#accents !!!! if (is_array($x)) { foreach ($x as &$v) { escapeJsonString($v); } } $escapers = array("\\", "/", "\"", "\n", "\r", "\t", "", "\f"); $replacements = array("\\\\", "\\/", "\\\"", "\\n", "\\r", "\\t", "\\f", "\\b"); $x = str_replace($escapers, $replacements, $x); toUTF8($x); return $x; }