PHP multidimentional associative array performance or similar code in Java - java

I am doing a lot of operations using a below code in PHP. The issue is PHP is getting slower and slower as my data gets bigger and bigger.
My solution to fix this is to move the code in Java so it can provide a better performance as it is complied language and I can also multithread or use async functions to do multiple such operations to make it faster.
What I want to know is how to speed up this kind of operation in PHP or what other data structure should I use to improve the performance of this code. And if not PHP how can I do this in Java.
foreach ( $dataArr as $direct ) {
//total dfpimpr for the date-li combi
if ( isset( $arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']]['COUNTER_TOTALIMPR'] ) ) {
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']]['COUNTER_TOTALIMPR'] += $direct[0]['DFPIMPR'];
}else {
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']]['COUNTER_TOTALIMPR'] = $direct[0]['DFPIMPR'];
}
$dfpAdUnit = $direct['AD1']['DFPAD1'].'/'.$direct['AD2']['DFPAD2'];
// can go on the first level of the array as not dependent on AD1/AD2-COUNTRY
if ( isset( self::$orderLineitemSetting[$direct['DOX']['ORDID']][$direct['DLI']['LIID']]['setting']['is_direct'] ) ) {
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['ISDIRECT'] = self::$orderLineitemSetting[$direct['DOX']['ORDID']][$direct['DLI']['LIID']]['setting']['is_direct'];
}else {
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['ISDIRECT'] = 1;
}
if ( isset( self::$orderLineitemSetting[$direct['DOX']['ORDID']][$direct['DLI']['LIID']]['setting']['is_ron'] ) ) {
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['ISRON'] = self::$orderLineitemSetting[$direct['DOX']['ORDID']][$direct['DLI']['LIID']]['setting']['is_ron'];
}else {
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['ISRON'] = 0;
}
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['DATE'] = $direct['DS']['DATE'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['ISADEX'] = 0;
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['ISMM'] = 0;
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['ADVERTISER'] = $direct['DA']['ADVERTISER'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['ORDID'] = $direct['DOX']['ORDID'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['ORDNAME'] = $direct['DOX']['ORDNAME'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['LINAME'] = $direct['DLI']['LINAME'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['LIID'] = $direct['DLI']['LIID'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['LISIZE'] = $direct['DSZ']['LISIZE'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['SITEID'] = $direct['PUBSITE']['SITEID'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['COUNTRYID'] = $direct['DC']['COUNTRYID'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['COUNTRY'] = $direct['DC']['COUNTRY'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['DFPADUNIT'] = $dfpAdUnit;
//if it is passback (ISDIRECT=2) make its revenue && impr = 0
//and add its impr to a new pbImpr column
if ( isset( self::$orderLineitemSetting[$direct['DOX']['ORDID']][$direct['DLI']['LIID']]['setting']['is_direct'] )
&& self::$orderLineitemSetting[$direct['DOX']['ORDID']][$direct['DLI']['LIID']]['setting']['is_direct'] == 2 ) {
//passback imprs
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['PBIMPR'] = $direct[0]['DFPIMPR'];
//make dfpimpr for passback all 0
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['DFPIMPR'] = $direct[0]['DFPIMPR'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['DFPCLCKS'] = $direct[0]['DFPCLCKS'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['DFPREV'] = 0;
//make tpimpr for passback all 0
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['TPIMPR'] = 0;
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['TPCLCKS'] = 0;
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['TPREV'] = 0;
}else {
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['DFPIMPR'] = $direct[0]['DFPIMPR'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['DFPCLCKS'] = $direct[0]['DFPCLCKS'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['DFPREV'] = $direct[0]['DFPREV'];
//include direct data into 3rd party
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['TPIMPR'] = $direct[0]['DFPIMPR'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['TPCLCKS'] = $direct[0]['DFPCLCKS'];
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['TPREV'] = $direct[0]['DFPREV'];
}
//include direct data into 3rd party
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['TP'] = '';
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']]['TPTAGID'] = 0;
}

Follow DRY principle. That way you'll reduce the length of the code, its complexity, and - what's most important in this case - you'll get rid of several dozens of multidimensional-arrays dereferences. Also the final code will be much easier to maintain:
foreach ($dataArr as $direct) {
// create a few references to reduce "ugly" code
$counter = &$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']]['COUNTER_TOTALIMPR'];
$directStatus = &self::$orderLineitemSetting[$direct['DOX']['ORDID']][$direct['DLI']['LIID']]['setting']['is_direct'];
$ronStatus = &self::$orderLineitemSetting[$direct['DOX']['ORDID']][$direct['DLI']['LIID']]['setting']['is_ron'];
$dfpAdUnit = $direct['AD1']['DFPAD1'].'/'.$direct['AD2']['DFPAD2'];
if (!isset($counter)) {
$counter = 0;
}
$counter += $direct[0]['DFPIMPR'];
$e = array(
'DATE' => $direct['DS']['DATE'],
'ISADEX' => 0,
'ISMM' => 0,
'ADVERTISER' => $direct['DA']['ADVERTISER'],
'ORDID' => $direct['DOX']['ORDID'],
'ORDNAME' => $direct['DOX']['ORDNAME'],
'LINAME' => $direct['DLI']['LINAME'],
'LIID' => $direct['DLI']['LIID'],
'LISIZE' => $direct['DSZ']['LISIZE'],
'SITEID' => $direct['PUBSITE']['SITEID'],
'COUNTRYID' => $direct['DC']['COUNTRYID'],
'COUNTRY' => $direct['DC']['COUNTRY'],
'DFPADUNIT' => $dfpAdUnit,
'TP' => '',
'TPTAGID' => 0
);
$e['ISDIRECT'] = !isset($directStatus) ? 1 : $directStatus;
$e['ISRON'] = !isset($ronStatus) ? 0 : $ronStatus;
if (isset($directStatus) && $directStatus === 2) { // Hint: don't use magic numbers (2),
// create some constant with appropriate name
$e += array(
'PBIMPR' => $direct[0]['DFPIMPR'],
'DFPIMPR' => $direct[0]['DFPIMPR'],
'DFPCLCKS' => $direct[0]['DFPCLCKS'],
'DFPREV' => 0,
'TPIMPR' => 0,
'TPCLCKS' => 0,
'TPREV' => 0
);
} else {
$e += array(
'DFPIMPR' => $direct[0]['DFPIMPR'],
'DFPCLCKS' => $direct[0]['DFPCLCKS'],
'DFPREV' => $direct[0]['DFPREV'],
'TPIMPR' => $direct[0]['DFPIMPR'],
'TPCLCKS' => $direct[0]['DFPCLCKS'],
'TPREV' => $direct[0]['DFPREV']
);
}
$arrayToBeFilled[$direct['DS']['DATE']][$direct['DLI']['LIID']][$dfpAdUnit][$direct['DC']['COUNTRYID']] = $e;
}
Optimize PHP itself. Use OP code accelerator like APC (for PHP < 5.5) or even better: a JIT compiler (HHVM) as suggested by #Alasdair

Related

How to pass dynamic value in .apned in reactjs

I m using file upload with react and axios and fileupload working fine. and currently i am using fixed id in data.append('customeId', '123456'); but i want to use id value dynamic bcz there are multiple user. i want to use this id like let customeId = localStorage.getItem("customeId");. please help me use id value dynamic.
i am currently using
uploadFile = ({ target: { files } }) =>{
console.log( files[0] )
let data = new FormData();
data.append('customeId', '123456');
data.append( 'file', files[0] )
// data.append = localStorage.getItem("brokerId");
const options = {
onUploadProgress: (progressEvent) => {
const {loaded, total} = progressEvent;
let percent = Math.floor( (loaded * 100) / total )
console.log( `${loaded}kb of ${total}kb | ${percent}%` );
if( percent < 100 ){
this.setState({ uploadPercentage: percent })
}
}
}
axios.post("https://apimarkp.com/user/", data, options).then(res => { }
What i want
uploadFile = ({ target: { files } }) =>{
let customeId = localStorage.getItem("customeId");
console.log( files[0] )
let data = new FormData();
data.append('customeId', 'customeId');
data.append( 'file', files[0] )
// data.append = localStorage.getItem("brokerId");
const options = {
onUploadProgress: (progressEvent) => {
const {loaded, total} = progressEvent;
let percent = Math.floor( (loaded * 100) / total )
console.log( `${loaded}kb of ${total}kb | ${percent}%` );
if( percent < 100 ){
this.setState({ uploadPercentage: percent })
}
}
}
axios.post("https://apimarkp.com/user/", data, options).then(res => { }
when you log in at the APP you should write on the localStorage
localStorage.setItem("customeId", customer.Id);
now you can use in your function:
uploadFile = ({ target: { files } }) =>{
let customeId = localStorage.getItem("customeId");

Akka Framing by Size

how can i frame Flow<ByteString, ByteString, NotUsed> by size? All examples I have found assumes that there is some delimiter, which is not my case, I just need to frame by length / size.
Framing via Framing.delimiter does require a designated delimiter, and there doesn't seem to be any built-in stream operator that does framing simply by a fixed chunk size. One of the challenges in coming up with a custom framing/chunking solution is to properly handle the last chunk of elements.
One solution would be to assemble a custom GraphStage like the "chunking" example illustrated in the Akka Stream-cookbook:
import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler}
import akka.stream.{Attributes, Inlet, Outlet, FlowShape}
import akka.util.ByteString
class Chunking(val chunkSize: Int) extends GraphStage[FlowShape[ByteString, ByteString]] {
val in = Inlet[ByteString]("Chunking.in")
val out = Outlet[ByteString]("Chunking.out")
override val shape = FlowShape.of(in, out)
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) {
private var buffer = ByteString.empty
setHandler(in, new InHandler {
override def onPush(): Unit = {
val elem = grab(in)
buffer ++= elem
emitChunk()
}
override def onUpstreamFinish(): Unit = {
if (buffer.isEmpty)
completeStage()
else {
if (isAvailable(out)) emitChunk()
}
}
})
setHandler(out, new OutHandler {
override def onPull(): Unit = {
if (isClosed(in)) emitChunk()
else pull(in)
}
})
private def emitChunk(): Unit = {
if (buffer.isEmpty) {
if (isClosed(in)) completeStage() else pull(in)
}
else {
val (chunk, nextBuffer) = buffer.splitAt(chunkSize)
buffer = nextBuffer
push(out, chunk)
}
}
}
}
Note that emitChunk() handles the fixed-size chunking and onUpstreamFinish() is necessary for processing the last chunk of elements in the internal buffer.
Test-running with a sample text file "/path/to/file" which has content as below:
Millions of people worldwide are in for a disastrous future of hunger, drought and disease, according to a draft report from the United Nations' Intergovernmental Panel on Climate Change, which was leaked to the media this week.
import akka.actor.ActorSystem
import akka.stream.scaladsl._
import java.nio.file.Paths
implicit val system = ActorSystem("system")
implicit val executionContext = system.dispatcher
val chunkSize = 32
FileIO.fromPath(Paths.get("/path/to/file")).
via(new Chunking(chunkSize)).
map(_.utf8String).
runWith(Sink.seq)
// res1: scala.concurrent.Future[Seq[String]] = Future(Success(Vector(
// "Millions of people worldwide are",
// " in for a disastrous future of h",
// "unger, drought and disease, acco",
// "rding to a draft report from the",
// " United Nations' Intergovernment",
// "al Panel on Climate Change, whic",
// "h was leaked to the media this w",
// "eek."
// )))
Something like (in Scala, disclaimer: only mentally compiled) this, using statefulMapConcat, which allows
emitting zero or more frames per input element
maintaining state from element to element of what's yet to be emitted
val frameSize: Int = ???
require(frameSize > 0, "frame size must be positive")
Flow[ByteString].statefulMapConcat { () =>
var carry: ByteString = ByteString.empty
{ in =>
val len = carry.length + in.length
if (len < frameSize) {
// append to carry and emit nothing
carry = carry ++ in
Nil
} else if (len == frameSize) {
if (carry.nonEmpty) {
carry = ByteString.empty
List(carry ++ in)
} else List(in)
} else {
if (carry.isEmpty) {
val frames = len / frameSize
val (emit, suffix) = in.splitAt(frames * frameSize)
carry = suffix
emit.grouped(frameSize)
} else {
val (appendToCarry, inn) = in.splitAt(frameSize - carry.length)
val first = carry ++ appendToCarry
val frames = inn.length / frameSize
if (frames > 0) {
val (emit, suffix) = inn.splitAt(frames * frameSize)
carry = suffix
Iterator.single(first) ++ emit.grouped(frameSize)
} else {
carry = inn
List(first)
}
}
}
}
If in Java, note that carry ++ in can be expressed as carry.concat(in). It may be useful, in order to get around the restriction in Java around closing over non-final variables, to use a 1-element ByteString[] (e.g. ByteString[] carry = { ByteString.empty }).

How to add Status = KO in Gatling script?

Is it possible to fail my request?
I would like to put Status = KO in asLongAs() section. My condition is like, if I get WorkflowFailed = True or Count > 8 then I want to fail that request using Status = KO.
I have seen somewhere about session.markAsFailed but how and where to use this?
Thanks.
Here is the code,
class LaunchResources extends Simulation {
val scenarioRepeatCount = Integer.getInteger("scenarioRepeatCount", 1).toInt
val userCount = Integer.getInteger("userCount", 1).toInt
val UUID = System.getProperty("UUID", "24d0e03")
val username = System.getProperty("username", "p1")
val password = System.getProperty("password", "P12")
val testServerUrl = System.getProperty("testServerUrl", "https://someurl.net")
val count = new java.util.concurrent.atomic.AtomicInteger(0)
val httpProtocol = http
.baseURL(testServerUrl)
.basicAuth(username, password)
.connection("""keep-alive""")
.contentTypeHeader("""application/vnd+json""")
val headers_0 = Map(
"""Cache-Control""" -> """no-cache""",
"""Origin""" -> """chrome-extension://fdmmgasdw1dojojpjoooidkmcomcm""")
val scn = scenario("LaunchAction")
.repeat (scenarioRepeatCount) {
exec(http("LaunchAResources")
.post( """/api/actions""")
.headers(headers_0)
.body(StringBody(s"""{"UUID": "$UUID", "stringVariables" : {"externalFilePath" : "/Test.mp4"}}"""))
.check(jsonPath("$.id").saveAs("WorkflowID")))
.exec(http("SaveWorkflowStatus")
.get("""/api/actions/{$WorkflowID}""")
.headers(headers_0)
.check(jsonPath("$.status").saveAs("WorkflowStatus")))
}
.asLongAs(session => session.attributes("WorkflowStatus") != "false" && count.getAndIncrement() < 8) {
doIf(session => session("WorkflowFailed").validate[String].map(WorkflowFailed => !WorkflowFailed.contains("true")).recover(true))
{
pause(pauseTime)
.exec(http("SaveWorkflowStatus")
.get("""/api/actions/${WorkflowID}""")
.headers(headers_0)
.check(jsonPath("$.running").saveAs("WorkflowStatus"))
.check(jsonPath("$.failed").saveAs("WorkflowFailed")))
.exec(session => {
val wflowStatus1 = session.get("WorkflowStatus").asOption[String]
val wflowFailed1 = session.get("WorkflowFailed").asOption[String]
println("Inner Loop Workflow Status: ========>>>>>>>> " + wflowStatus1.getOrElse("COULD NOT FIND STATUS"))
println("Inner Loop Workflow Failed?? ========>>>>>>>> " + wflowFailed1.getOrElse("COULD NOT FIND STATUS"))
println("Count =====>> " + count)
session})
}
}
setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol)
}
there's a method available on the session
exec(session => session.markAsFailed)

No Response from REST API, & Parse.com Cloud Error: Can't Form Encode an Object

I am trying to publish a Facebook post to a user's own wall using the Facebook Graph API, but I am experiencing multiple problems.
I have a Wordpress site and I am using the OneAll.com service to maintain and manage my users' social logins. I had added the required permission for posting for a user to my Facebook app, after I had already linked an account to a Facebook account. Problematically, OneAll does not seem to update the access token stored in that social identity until it expires. But it seems that access token was prematurely expired due to the newly added app permission, so I need to manually re-synchronize the user's social identity on OneAll.
They do have a function to call to synchronize an identity through their API (Here), but I cannot seem to successfully send a request to it.
I tried to set up an HTTP PUT request as specified in my Java servlet, but it fails every time when called with error 411 returned. I have done my research and that seems to be a need to specify the content-length in the request, yet it does not seem to work even when I do try adding that.
So instead I tried setting up a cloud function in my app's Parse.com cloud code to activate this resync, but this one fails each time too, with an error code I can't seem to figure out:
Input: {"oaIDToken":"924e6f**********"}
Result: Uncaught Error: Can't form encode an Object
Any idea what this error means and how to fix it? I can't tell what I am doing wrong. This is my cloud function:
Parse.Cloud.define("forceOAIDUpdate", function(request, response) {
var IDToken = request.params.oaIDToken;
var IDURL = "https://w*******d.api.oneall.com/identities/" + IDToken + "/synchronize.json";
Parse.Cloud.httpRequest({
method: 'PUT',
url: IDURL,
body: {
request: {
synchronize: {
update_user_data: true,
force_token_update: true
}
}
},
success: function(httpResponse) {
console.log("OA ID token successfully refreshed.");
console.log(httpResponse.text);
response.success("OA ID token refreshed");
},
error: function(httpResponse) {
console.error('Requested OA ID refresh failed with response code ' +
httpResponse.status);
response.error("Failed to refresh OA ID. Error: " +
httpResponse.data + httpResponse.text + httpResponse.error);
}
});
});
I know for a fact the OneAll identity token the function receives is valid/correct because it works in my other functions which perform other actions for the user. Also, I had the Basic Authentication login attached to the request when I tried doing it in my Java servlet, but it didn't seem to make a difference. Either way, is it possible that missing authentication is what is causing this error? And if so, how do I insert that authentication header in that Parse cloud HTTP request? I've checked and cannot find any resources online which clearly describe it.
There is another really odd thing I can't figure out. I have tried to manually make this call to OneAll's REST API using a REST Console/Client, but no matter the URL to any port of their REST API, the connection always fails - the console returns immediately that there was no response. This is true with or without the Basic Authentication header attached to the request. What in the world is going on??
I've reached a "writer's block", so-to-say, and have run out of ideas to debug this. I'll greatly appreciate any assistance. This problem has had me stumped for too many hours already.
I finally figured it out!!!
The Can't form encode an Object error from Parse simply meant that my body parameters needed to be encoded as a JSON string.
The authentication problem was resolved by including the Basic Authorization in the HTTP Request header. But it had to be encoded in Base64 and since Parse's Cloud code does not seem to support the btoa() function (as I've tried), I had to manually include/create a Base64 encoder.
So I changed the Parse Cloud function to the following and it finally worked:
Parse.Cloud.define("forceOAIDUpdate", function(request, response) {
var Base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
encode : function (input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
Base64._keyStr.charAt(enc1) + Base64._keyStr.charAt(enc2) +
Base64._keyStr.charAt(enc3) + Base64._keyStr.charAt(enc4);
}
return output;
},
// public method for decoding
decode : function (input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (i < input.length) {
enc1 = Base64._keyStr.indexOf(input.charAt(i++));
enc2 = Base64._keyStr.indexOf(input.charAt(i++));
enc3 = Base64._keyStr.indexOf(input.charAt(i++));
enc4 = Base64._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 encoding
_utf8_encode : function (string) {
string = string.replace(/\r\n/g,"\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// private method for UTF-8 decoding
_utf8_decode : function (utftext) {
var string = "";
var i = 0;
var c = c1 = c2 = 0;
while ( i < utftext.length ) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
}
else if((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i+1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
}
else {
c2 = utftext.charCodeAt(i+1);
c3 = utftext.charCodeAt(i+2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
var IDToken = request.params.oaIDToken;
var key = "f1a914*******************************";
var secret = "e804************************************";
var basicAuthOA = key + ":" + secret;
var basicAuthOAEncoded = Base64.encode(basicAuthOA);
var IDURL = "https://whentosend.api.oneall.com/identities/" + IDToken + "/synchronize.json";
var headerAuth = {
'Authorization': "Basic " + basicAuthOAEncoded
}
var params = {
request: {
synchronize: {
update_user_data: true,
force_token_update: true
}
}
}
Parse.Cloud.httpRequest({
method: 'PUT',
url: IDURL,
headers: headerAuth,
body: JSON.stringify(params),
success: function(httpResponse) {
console.log("OA ID token successfully refreshed.");
console.log(httpResponse.text);
response.success("OA ID token refreshed");
},
error: function(httpResponse) {
console.error('Requested OA ID refresh failed with response code ' +
httpResponse.status);
response.error("Failed to refresh OA ID. Error: " +
httpResponse.data + httpResponse.text + httpResponse.error);
}
});
});
I hope this helps someone else save a ton of debugging time. It frustratingly took me days to figure this out, and I wouldn't wish that on any programmer.

Possible SQL Injection in AJAX request?

I am developing search indexing using PHP and AJAX to make it powerful.
When I scan it using burpsuit or other security scanner, SQL injection appears in AJAX code and I can't find any solution for it. The code is below:
<?php
require_once 'Connections/connect.php';
if($_GET['type'] == 'mobile'){
$result = mysql_query("SELECT mobilep FROM dictionary where mobilep LIKE '".$_GET['name_startsWith']."%'");
$data = array();
while ($row = mysql_fetch_array($result)) {
array_push($data, $row['mobilep']);
}
echo json_encode($data);
}
?>
This is very bad... you're using the deprecated mysql adapter.
http://php.net/manual/en/book.pdo.php
Use pdo and binds, here's a full prototype:
class MySql
{
private $sDbName = '';
private $sUsername = '';
private $sPassword = '';
private $sHost = '';
private $oConnection = null;
public function __construct()
{
$this->oConnection = new PDO(
'mysql:host='
. $this->sHost
. ';dbname='
. $this->sDbName,
$this->sUsername,
$this->sPassword
);
}
public function getDb()
{
return $this->oConnection;
}
}
$aReturn[ 'data' ] = '';
if( !empty( $_GET[ 'type' ] )
&& ( !empty( $_GET[ 'name_startsWith' ] )
&& ( $_GET['type'] == 'mobile' )
)
{
$oMySql = new MySql;
$oDb = $oMySql->getDb();
$sSql = "SELECT mobilep FROM dictionary where mobilep LIKE :name";
$aBinds[ ':name' ] = $_GET[ 'name_startsWith' ] . '%';
$oStmp = $oDb->prepare( $sSql );
$oMySql->bindVariables( $oStmp, $aBinds );
$oStmp->execute();
$oResults = $oStmp->fetchall();
if( !empty( $oResults ) )
{
// var_dump( $aResults );
$oErrors = $oStmp->errorInfo();
// var_dump( $oErrors );
$aReturn[ 'data' ] = $aResults;
}
}
$sJson = json_encode( $aReturn, 1 );
header( 'Content-type', 'application/json' );
echo $sJson;
(Yes, this is question over a year old. But there is no selected answer. I ran across this question in a search...)
If you are stuck with mysql_ interface functions, and can't migrate to mysqli or PDO, the best you can do is to use the mysql_real_escape_string function.
existing code:
= mysql_query(" ... LIKE '". $_GET['name_startsWith'] ."%'");
to properly escape a potentially unsafe value, before it's incorporated into the SQL text, use the mysql_real_escape_string function...
= mysql_query(" ... LIKE '". mysql_real_escape_string( $_GET['name_startsWith'] )."%'");
^^^^^^^^^^^^^^^^^^^^^^^^^ ^

Categories