PHP - HEX to EBCDIC Conversion - php

How do i convert an Hex value to EBCDIC in PHP?
What is the most practical way to do it?
What i did so far is hex to Ascii:
Hex : 313233343536373832313332333435
Ascii: 123456782132345
By using:
hex2bin($foo);

strtr should do the job, if you provide ebcdic to ascii mapping.
An example mapping for 3-letters alphabet:
$ebcdicSet = "\x81\x84\xa2";
$asciiSet = "ads";
$ebcdic = "\x81\xa2\x84\x81\xa2\x84"; // 6 bytes
$ascii = strtr($ebcdic, $ebcdicSet, $asciiSet);
echo $ascii; // outputs "asdasd"
If your input is not binary hex, but a sting with hex representation, you need to hex2bin it first:
$ebcdic = "81a28481a284"; // 12 bytes
$ascii = strtr(hex2bin($ebcdic), $ebcdicSet, $asciiSet);
echo $ascii; // outputs "asdasd"

If using built in function from EBCDIC to ASCII
function ebcdic_to_ascii($ebcdic_hexstring /*expecting something like F0F1....*/)
{
//need to delcare it to avoid warning
$ebcd_ascii= null;
// here come all the conversion
$ebcd_ascii["4A"] = "¢";
$ebcd_ascii["4B"] = ".";
$ebcd_ascii["4C"] = "<";
$ebcd_ascii["4D"] = "(";
$ebcd_ascii["4E"] = "+";
$ebcd_ascii["4F"] = "|";
$ebcd_ascii["5A"] = "!";
$ebcd_ascii["5B"] = "$";
$ebcd_ascii["5C"] = "*";
$ebcd_ascii["5D"] = ")";
$ebcd_ascii["5E"] = ";";
$ebcd_ascii["5F"] = "¬";
$ebcd_ascii["60"] = "-";
$ebcd_ascii["61"] = "/";
$ebcd_ascii["6A"] = "¦";
$ebcd_ascii["6B"] = ",";
$ebcd_ascii["6C"] = "%";
$ebcd_ascii["6D"] = "_";
$ebcd_ascii["6E"] = ">";
$ebcd_ascii["6F"] = "?";
$ebcd_ascii["79"] = "`";
$ebcd_ascii["7A"] = ":";
$ebcd_ascii["7B"] = "#";
$ebcd_ascii["7C"] = "#";
$ebcd_ascii["7D"] = "'";
$ebcd_ascii["7E"] = "=";
$ebcd_ascii["7F"] = " '' ";
$ebcd_ascii["81"] = "a";
$ebcd_ascii["82"] = "b";
$ebcd_ascii["83"] = "c";
$ebcd_ascii["84"] = "d";
$ebcd_ascii["85"] = "e";
$ebcd_ascii["86"] = "f";
$ebcd_ascii["87"] = "g";
$ebcd_ascii["88"] = "h";
$ebcd_ascii["89"] = "i";
$ebcd_ascii["91"] = "j";
$ebcd_ascii["92"] = "k";
$ebcd_ascii["93"] = "l";
$ebcd_ascii["94"] = "m";
$ebcd_ascii["95"] = "n";
$ebcd_ascii["96"] = "o";
$ebcd_ascii["97"] = "p";
$ebcd_ascii["98"] = "q";
$ebcd_ascii["99"] = "r";
$ebcd_ascii["A1"] = "~";
$ebcd_ascii["A2"] = "s";
$ebcd_ascii["A3"] = "t";
$ebcd_ascii["A4"] = "u";
$ebcd_ascii["A5"] = "v";
$ebcd_ascii["A6"] = "w";
$ebcd_ascii["A7"] = "x";
$ebcd_ascii["A8"] = "y";
$ebcd_ascii["A9"] = "z";
$ebcd_ascii["C0"] = "{";
$ebcd_ascii["C1"] = "A";
$ebcd_ascii["C2"] = "B";
$ebcd_ascii["C3"] = "C";
$ebcd_ascii["C4"] = "D";
$ebcd_ascii["C5"] = "E";
$ebcd_ascii["C6"] = "F";
$ebcd_ascii["C7"] = "G";
$ebcd_ascii["C7"] = "H";
$ebcd_ascii["C9"] = "I";
$ebcd_ascii["D0"] = "}";
$ebcd_ascii["D1"] = "J";
$ebcd_ascii["D2"] = "K";
$ebcd_ascii["D3"] = "L";
$ebcd_ascii["D4"] = "M";
$ebcd_ascii["D5"] = "N";
$ebcd_ascii["D6"] = "O";
$ebcd_ascii["D7"] = "P";
$ebcd_ascii["D8"] = "Q";
$ebcd_ascii["D9"] = "R";
$ebcd_ascii["E0"] = "\\";
$ebcd_ascii["E2"] = "S";
$ebcd_ascii["E3"] = "T";
$ebcd_ascii["E4"] = "U";
$ebcd_ascii["E5"] = "V";
$ebcd_ascii["E6"] = "W";
$ebcd_ascii["E7"] = "X";
$ebcd_ascii["E8"] = "Y";
$ebcd_ascii["E9"] = "Z";
$ebcd_ascii["F0"] = "0";
$ebcd_ascii["F1"] = "1";
$ebcd_ascii["F2"] = "2";
$ebcd_ascii["F3"] = "3";
$ebcd_ascii["F4"] = "4";
$ebcd_ascii["F5"] = "5";
$ebcd_ascii["F6"] = "6";
$ebcd_ascii["F7"] = "7";
$ebcd_ascii["F8"] = "8";
$ebcd_ascii["F9"] = "9";
$ebcd_ascii["FF"] = "E0";
//end of conversion
// loop until there is no more conversion.
$asciiOut = "";
while(strlen($ebcdic_hexstring)>1)//F0F1F2F3F -> F1F2F3F
{
$thisEbcdic = substr($ebcdic_hexstring, 0, 2);//F0->F1
//if(!is_null($ebcd_ascii[$thisEbcdic]))
$asciiOut = $asciiOut.$ebcd_ascii[$thisEbcdic];//0->01
$ebcdic_hexstring = substr($ebcdic_hexstring, 2);//F1F2F3F -> F2F3F
}
return $asciiOut;
}
?>

Related

PHP - I need to call 2 php functions via url parameter - but how?

I have to call an old PHP function with PC1 encryption and unfortunately I have no instructions or example, only the PHP file - and no PHP know how :( :(
Can someone help me how to call the encrypt and decrypt function via URL-parameter? I only need to show/print the encrypted or decrypted result.
This is the old found code:
<?
//class to encrypt and decrypt according to the "PC1" algorithm
// I. Verburgh 2007
class PC1 {
var $pkax;
var $pkbx;
var $pkcx;
var $pkdx;
var $pksi;
var $pktmp;
var $x1a2;
var $pkres;
var $pki;
var $inter;
var $cfc;
var $cfd;
var $compte;
var $x1a0;
var $cle;
var $pkc;
var $plainlen;
var $ascipherlen;
var $plainText;
var $ascCipherText;
function PC1() {
}
function pkfin() {
for ($j=0;$j<16;$j++) {
$this->cle[$j] = "";
}
for ($j=0;$j<8;$j++) {
$this->x1a0[$j] = 0;
}
$this->pkax = 0;
$this->pkbx = 0;
$this->pkcx = 0;
$this->pkdx = 0;
$this->pksi = 0;
$this->pktmp = 0;
$this->x1a2 = 0;
$this->pkres = 0;
$this->pki = 0;
$this->inter = 0;
$this->cfc = 0;
$this->cfd = 0;
$this->compte = 0;
$this->pkc = 0;
}
function pkcode() {
$this->pkdx = $this->x1a2 + $this->pki;
$this->pkax = $this->x1a0[$this->pki];
$this->pkcx = 0x015a;
$this->pkbx = 0x4e35;
$this->pktmp = $this->pkax;
$this->pkax = $this->pksi;
$this->pksi = $this->pktmp;
$this->pktmp = $this->pkax;
$this->pkax = $this->pkdx;
$this->pkdx = $this->pktmp;
if ($this->pkax != 0) {
$this->pkax = $this->wordmultiply($this->pkax, $this->pkbx);
}
$this->pktmp = $this->pkax;
$this->pkax = $this->pkcx;
$this->pkcx = $this->pktmp;
if ($this->pkax != 0) {
$this->pkax = $this->wordmultiply($this->pkax, $this->pksi);
$this->pkcx = $this->wordsum($this->pkax, $this->pkcx);
}
$this->pktmp = $this->pkax;
$this->pkax = $this->pksi;
$this->pksi = $this->pktmp;
$this->pkax = $this->wordmultiply($this->pkax, $this->pkbx);
$this->pkdx = $this->wordsum($this->pkcx, $this->pkdx);
$this->pkax = $this->wordsum($this->pkax, 1);
$this->x1a2 = $this->pkdx;
$this->x1a0[$this->pki] = $this->pkax;
$this->pkres = $this->wordxor($this->pkax, $this->pkdx);
$this->pki++;
}
function wordmultiply($value1, $value2) {
if (is_numeric($value1) && is_numeric($value2))
$product = (($value1 * $value2) % 65536);
else {
$product = 0;
echo "error with wordmulitply<br />";
}
return $product;
}
function wordsum($value1, $value2) {
$sum = (($value1 + $value2) % 65536);
return $sum;
}
function wordminus($value1, $value2) {
$minus = (($value1 - $value2) % 65536);
return $minus;
}
function wordxor($value1, $value2) {
$outcome = (($value1 ^ $value2) % 65536);
return $outcome;
}
function pkassemble() {
$this->x1a0[0] = $this->wordsum($this->wordmultiply(ord($this->cle[0]), 256), ord($this->cle[1]));
$this->pkcode();
$this->inter = $this->pkres;
$this->x1a0[1] = $this->wordxor($this->x1a0[0], $this->wordsum($this->wordmultiply(ord($this->cle[2]), 256), ord($this->cle[3])));
$this->pkcode();
$this->inter = $this->wordxor($this->inter, $this->pkres);
$this->x1a0[2] = $this->wordxor($this->x1a0[1], $this->wordsum($this->wordmultiply(ord($this->cle[4]), 256), ord($this->cle[5])));
$this->pkcode();
$this->inter = $this->wordxor($this->inter, $this->pkres);
$this->x1a0[3] = $this->wordxor($this->x1a0[2], $this->wordsum($this->wordmultiply(ord($this->cle[6]), 256), ord($this->cle[7])));
$this->pkcode();
$this->inter = $this->wordxor($this->inter, $this->pkres);
$this->x1a0[4] = $this->wordxor($this->x1a0[3], $this->wordsum($this->wordmultiply(ord($this->cle[8]), 256), ord($this->cle[9])));
$this->pkcode();
$this->inter = $this->wordxor($this->inter, $this->pkres);
$this->x1a0[5] = $this->wordxor($this->x1a0[4], $this->wordsum($this->wordmultiply(ord($this->cle[10]), 256), ord($this->cle[11])));
$this->pkcode();
$this->inter = $this->wordxor($this->inter, $this->pkres);
$this->x1a0[6] = $this->wordxor($this->x1a0[5], $this->wordsum($this->wordmultiply(ord($this->cle[12]), 256), ord($this->cle[13])));
$this->pkcode();
$this->inter = $this->wordxor($this->inter, $this->pkres);
$this->x1a0[7] = $this->wordxor($this->x1a0[6], $this->wordsum($this->wordmultiply(ord($this->cle[14]), 256), ord($this->cle[15])));
$this->pkcode();
$this->inter = $this->wordxor($this->inter, $this->pkres);
$this->pki=0;
}
function encrypt($in, $key) {
$this->pkfin();
$this->k = 0;
$this->plainlen = strlen($in);
for ($count=0;$count<16;$count++) {
if (isset($key[$count]))
$this->cle[$count] = $key[$count];
}
for ($count=0;$count<$this->plainlen;$count++) {
$this->pkc = ord($in[$count]);
$this->pkassemble();
$this->cfc = $this->inter >> 8;
$this->cfd = $this->inter & 255;
for ($this->compte=0;$this->compte<sizeof($this->cle);$this->compte++) {
$this->cle[$this->compte] = chr($this->wordxor(ord($this->cle[$this->compte]), $this->pkc));
}
$this->pkc = $this->wordxor($this->pkc, ($this->wordxor($this->cfc, $this->cfd)));
$this->pkd = ($this->pkc >> 4);
$this->pke = ($this->pkc & 15);
$this->ascCipherText[$this->k] = $this->wordsum(0x61, $this->pkd);
$this->k++;
$this->ascCipherText[$this->k] = $this->wordsum(0x61, $this->pke);
$this->k++;
}
$this->ascCipherText = array_map("chr", $this->ascCipherText);
return implode("", $this->ascCipherText);
}
function decrypt($in, $key) {
$this->pkfin();
$return = "";
for ($count=0;$count<16;$count++) {
if (isset($key[$count]))
$this->cle[$count] = $key[$count];
else
$this->cle[$count] = "";
}
$this->pksi = 0;
$this->x1a2 = 0;
$d = 0;
$e = 0;
$i = 0;
$j = 0;
$l = 0;
$len = strlen($in);
while ($j < $len) {
$rep = $in[$j];
switch($rep) {
case "a": {
$d = 0;
break;
}
case "b": {
$d = 1;
break;
}
case "c": {
$d = 2;
break;
}
case "d": {
$d = 3;
break;
}
case "e": {
$d = 4;
break;
}
case "f": {
$d = 5;
break;
}
case "g": {
$d = 6;
break;
}
case "h": {
$d = 7;
break;
}
case "i": {
$d = 8;
break;
}
case "j": {
$d = 9;
break;
}
case "k": {
$d = 10;
break;
}
case "l": {
$d = 11;
break;
}
case "m": {
$d = 12;
break;
}
case "n": {
$d = 13;
break;
}
case "o": {
$d = 14;
break;
}
case "p": {
$d = 15;
break;
}
}
$d = $d << 4;
$j++;
$rep = $in[$j];
switch($rep) {
case "a": {
$e = 0;
break;
}
case "b": {
$e = 1;
break;
}
case "c": {
$e = 2;
break;
}
case "d": {
$e = 3;
break;
}
case "e": {
$e = 4;
break;
}
case "f": {
$e = 5;
break;
}
case "g": {
$e = 6;
break;
}
case "h": {
$e = 7;
break;
}
case "i": {
$e = 8;
break;
}
case "j": {
$e = 9;
break;
}
case "k": {
$e = 10;
break;
}
case "l": {
$e = 11;
break;
}
case "m": {
$e = 12;
break;
}
case "n": {
$e = 13;
break;
}
case "o": {
$e = 14;
break;
}
case "p": {
$e = 15;
break;
}
}
$c = $d + $e;
$this->pkassemble();
$this->cfc = $this->inter >> 8;
$this->cfd = $this->inter & 255;
$c = $this->wordxor($c, ($this->wordxor($this->cfc, $this->cfd)));
for ($compte=0;$compte<16;$compte++)
$this->cle[$compte] = chr($this->wordxor(ord($this->cle[$compte]), $c));
$return = $return.chr($c);
$j++;
$l++;
}
return $return;
}
}
?>
What I tried was something like this
public function index()
{
var $test;
$test = encrypt("Hallo","nhifgbcnlfglffmh");
echo test;
}
$app = new PC1();
$app->index();
But I get Parse error: syntax error, unexpected T_VAR in....
I think the return implode does no return a string?
Since it is all in a class you first need to call the class
$app = new PC1();
Afther that you can call the functions inside that class:
$test = $app->encrypt("Hallo","nhifgbcnlfglffmh");
For the function to work as a URL param you could do the following:
URL: example.com?encrypt=Hallo
In the php page where it links to check for the $_GET so:
<?php
$app = new PC1();
if( !empty( $_GET['encrypt'] ) {
$encrypt = $_GET['encrypt'];
var_dump($app->encrypt($encrypt, "nhifgbcnlfglffmh"));
} elseif ( !empty( $_GET['decrypt'] ) ) {
$decrypt = $_GET['decrypt'];
var_dump($app->decrypt($decrypt, "nhifgbcnlfglffmh"));
}

How to find and make sum of different numbers from array?

I've read some texts and searched topics, but nothing help me. I'm beginner in PHP. I have array where are variables $qA01_1 up to $qA30_5 and their values can be different 0 or 1 or 5. From array I would like to find all variables with value 1 and make sum. Same for number 5.
$qA01_1 = $_SESSION['qA01_1'];
$qA01_2 = $_SESSION['qA01_2'];
$qA01_3 = $_SESSION['qA01_3'];
$qA01_4 = $_SESSION['qA01_4'];
$qA01_5 = $_SESSION['qA01_5'];
$qA02_1 = $_SESSION['qA02_1'];
$qA02_2 = $_SESSION['qA02_2'];
$qA02_3 = $_SESSION['qA02_3'];
$qA02_4 = $_SESSION['qA02_4'];
$qA02_5 = $_SESSION['qA02_5'];
$qA03_1 = $_SESSION['qA03_1'];
$qA03_2 = $_SESSION['qA03_2'];
$qA03_3 = $_SESSION['qA03_3'];
$qA03_4 = $_SESSION['qA03_4'];
$qA03_5 = $_SESSION['qA03_5'];
$qA04_1 = $_SESSION['qA04_1'];
$qA04_2 = $_SESSION['qA04_2'];
$qA04_3 = $_SESSION['qA04_3'];
$qA04_4 = $_SESSION['qA04_4'];
$qA04_5 = $_SESSION['qA04_5'];
$qA05_1 = $_SESSION['qA05_1'];
$qA05_2 = $_SESSION['qA05_2'];
$qA05_3 = $_SESSION['qA05_3'];
$qA05_4 = $_SESSION['qA05_4'];
$qA05_5 = $_SESSION['qA05_5'];
$qA06_1 = $_SESSION['qA06_1'];
$qA06_2 = $_SESSION['qA06_2'];
$qA06_3 = $_SESSION['qA06_3'];
$qA06_4 = $_SESSION['qA06_4'];
$qA06_5 = $_SESSION['qA06_5'];
$qA07_1 = $_SESSION['qA07_1'];
$qA07_2 = $_SESSION['qA07_2'];
$qA07_3 = $_SESSION['qA07_3'];
$qA07_4 = $_SESSION['qA07_4'];
$qA07_5 = $_SESSION['qA07_5'];
$qA08_1 = $_SESSION['qA08_1'];
$qA08_2 = $_SESSION['qA08_2'];
$qA08_3 = $_SESSION['qA08_3'];
$qA08_4 = $_SESSION['qA08_4'];
$qA08_5 = $_SESSION['qA08_5'];
$qA09_1 = $_SESSION['qA09_1'];
$qA09_2 = $_SESSION['qA09_2'];
$qA09_3 = $_SESSION['qA09_3'];
$qA09_4 = $_SESSION['qA09_4'];
$qA09_5 = $_SESSION['qA09_5'];
$qA10_1 = $_SESSION['qA10_1'];
$qA10_2 = $_SESSION['qA10_2'];
$qA10_3 = $_SESSION['qA10_3'];
$qA10_4 = $_SESSION['qA10_4'];
$qA10_5 = $_SESSION['qA10_5'];
$qA11_1 = $_SESSION['qA11_1'];
$qA11_2 = $_SESSION['qA11_2'];
$qA11_3 = $_SESSION['qA11_3'];
$qA11_4 = $_SESSION['qA11_4'];
$qA11_5 = $_SESSION['qA11_5'];
$qA12_1 = $_SESSION['qA12_1'];
$qA12_2 = $_SESSION['qA12_2'];
$qA12_3 = $_SESSION['qA12_3'];
$qA12_4 = $_SESSION['qA12_4'];
$qA12_5 = $_SESSION['qA12_5'];
$qA13_1 = $_SESSION['qA13_1'];
$qA13_2 = $_SESSION['qA13_2'];
$qA13_3 = $_SESSION['qA13_3'];
$qA13_4 = $_SESSION['qA13_4'];
$qA13_5 = $_SESSION['qA13_5'];
$qA14_1 = $_SESSION['qA14_1'];
$qA14_2 = $_SESSION['qA14_2'];
$qA14_3 = $_SESSION['qA14_3'];
$qA14_4 = $_SESSION['qA14_4'];
$qA14_5 = $_SESSION['qA14_5'];
$qA15_1 = $_SESSION['qA15_1'];
$qA15_2 = $_SESSION['qA15_2'];
$qA15_3 = $_SESSION['qA15_3'];
$qA15_4 = $_SESSION['qA15_4'];
$qA15_5 = $_SESSION['qA15_5'];
$qA16_1 = $_SESSION['qA16_1'];
$qA16_2 = $_SESSION['qA16_2'];
$qA16_3 = $_SESSION['qA16_3'];
$qA16_4 = $_SESSION['qA16_4'];
$qA16_5 = $_SESSION['qA16_5'];
$qA17_1 = $_SESSION['qA17_1'];
$qA17_2 = $_SESSION['qA17_2'];
$qA17_3 = $_SESSION['qA17_3'];
$qA17_4 = $_SESSION['qA17_4'];
$qA17_5 = $_SESSION['qA17_5'];
$qA18_1 = $_SESSION['qA18_1'];
$qA18_2 = $_SESSION['qA18_2'];
$qA18_3 = $_SESSION['qA18_3'];
$qA18_4 = $_SESSION['qA18_4'];
$qA18_5 = $_SESSION['qA18_5'];
$qA19_1 = $_SESSION['qA19_1'];
$qA19_2 = $_SESSION['qA19_2'];
$qA19_3 = $_SESSION['qA19_3'];
$qA19_4 = $_SESSION['qA19_4'];
$qA19_5 = $_SESSION['qA19_5'];
$qA20_1 = $_SESSION['qA20_1'];
$qA20_2 = $_SESSION['qA20_2'];
$qA20_3 = $_SESSION['qA20_3'];
$qA20_4 = $_SESSION['qA20_4'];
$qA20_5 = $_SESSION['qA20_5'];
$qA21_1 = $_SESSION['qA21_1'];
$qA21_2 = $_SESSION['qA21_2'];
$qA21_3 = $_SESSION['qA21_3'];
$qA21_4 = $_SESSION['qA21_4'];
$qA21_5 = $_SESSION['qA21_5'];
$qA22_1 = $_SESSION['qA22_1'];
$qA22_2 = $_SESSION['qA22_2'];
$qA22_3 = $_SESSION['qA22_3'];
$qA22_4 = $_SESSION['qA22_4'];
$qA22_5 = $_SESSION['qA22_5'];
$qA23_1 = $_SESSION['qA23_1'];
$qA23_2 = $_SESSION['qA23_2'];
$qA23_3 = $_SESSION['qA23_3'];
$qA23_4 = $_SESSION['qA23_4'];
$qA23_5 = $_SESSION['qA23_5'];
$qA24_1 = $_SESSION['qA24_1'];
$qA24_2 = $_SESSION['qA24_2'];
$qA24_3 = $_SESSION['qA24_3'];
$qA24_4 = $_SESSION['qA24_4'];
$qA24_5 = $_SESSION['qA24_5'];
$qA25_1 = $_SESSION['qA25_1'];
$qA25_2 = $_SESSION['qA25_2'];
$qA25_3 = $_SESSION['qA25_3'];
$qA25_4 = $_SESSION['qA25_4'];
$qA25_5 = $_SESSION['qA25_5'];
$qA26_1 = $_SESSION['qA26_1'];
$qA26_2 = $_SESSION['qA26_2'];
$qA26_3 = $_SESSION['qA26_3'];
$qA26_4 = $_SESSION['qA26_4'];
$qA26_5 = $_SESSION['qA26_5'];
$qA27_1 = $_SESSION['qA27_1'];
$qA27_2 = $_SESSION['qA27_2'];
$qA27_3 = $_SESSION['qA27_3'];
$qA27_4 = $_SESSION['qA27_4'];
$qA27_5 = $_SESSION['qA27_5'];
$qA28_1 = $_SESSION['qA28_1'];
$qA28_2 = $_SESSION['qA28_2'];
$qA28_3 = $_SESSION['qA28_3'];
$qA28_4 = $_SESSION['qA28_4'];
$qA28_5 = $_SESSION['qA28_5'];
$qA29_1 = $_SESSION['qA29_1'];
$qA29_2 = $_SESSION['qA29_2'];
$qA29_3 = $_SESSION['qA29_3'];
$qA29_4 = $_SESSION['qA29_4'];
$qA29_5 = $_SESSION['qA29_5'];
$qA30_1 = $_POST['qA30_1'];
$qA30_2 = $_POST['qA30_2'];
$qA30_1 = (isset($_POST['qA30_1'])) ? $_POST['qA30_1'] : 0;
$qA30_2 = (isset($_POST['qA30_2'])) ? $_POST['qA30_2'] : 0;
$qA30_3 = (isset($_POST['qA30_3'])) ? $_POST['qA30_3'] : 0;
$qA30_4 = (isset($_POST['qA30_4'])) ? $_POST['qA30_4'] : 0;
$qA30_5 = (isset($_POST['qA30_5'])) ? $_POST['qA30_5'] : 0;
Here is my proposal which do not work. I thing that I should to do something with "array" .
// Sum of all numbers 1
$sumOne = 0;
for ($i = 0; $i <= 3; $i++) {
for($j = 0; $j <= 9; $j++) {
for($k = 1; $k <= 5; $k++){
$u = 'qA_' . $i . $j. '_' . $k;
if ($u==1){
$sumOnet+=1;
}
}
}
}
echo "<br/>SumOne:" . " " . round($sumOne[0], 0);
You really should stick with an array. If you define an array within the $_SESSION array and use that it will be much simpler:
$_SESSION['data']['qA01_1'] = 0;
$_SESSION['data']['qA01_2'] = 1;
$_SESSION['data']['qA01_3'] = 5;
$counts = array_count_values($_SESSION['data']);
if(isset($counts[0])) { echo $counts[0]; } // sum of any 0s is 0 so here is the count
if(isset($counts[1])) { echo $counts[1]; } // sum of any 1s will be the count
if(isset($counts[5])) { echo $counts[5] * 5; }
If I understand the question correctly, you want to sum up all individual values in your array.
You can use array_count_values() for that, it returns you the number of occurances of each value where the value itself is the key:
foreach (array_count_values($_SESSION) as $value => $count) {
echo 'sum of ' . $value . ' values: ' . ($value * $count) . "<br>";
}
No idea where the POST variables fit in though...

imap "delete" message after downloading using php?

Downloading attachment was working... but the delete wasn't?
Can anyone tell me whats wrong and how to make it work?
<?php $hostname = '{xxxxxxpop3/notls}INBOX'; $username = 'xxx#gmail.com'; $password = 'password';
$savedirpath = "/root/Fax"; $type = 'ReadAttachment'; $obj = new $type; $obj->getdata($hostname, $username, $password, $savedirpath, $delete_emails = false);
class ReadAttachment {
function getdecodevalue ($message, $coding)
{
switch ($coding)
{
case 0:
case 1:
$message = imap_8bit($message);
break;
case 2:
$message = imap_binary($message);
break;
case 3:
case 5:
$message = imap_base64($message);
break;
case 4:
$message = imap_qprint($message);
break;
}
return $message;
}
function getdata ($hostname, $username, $password, $savedirpath, $delete_emails = false)
{
// make sure savepath has trailing slash(/)
$savedirpath = str_replace('\\', '/', $savedirpath);
if (substr($savedirpath, strlen($savedirpath) - 1) != '/')
{
$savedirpath .= '/';
}
$mbox = imap_open($hostname, $username, $password) or die("can't connect: " . imap_last_error());
$message = array();
$message["attachment"]["type"][0] = "text";
$message["attachment"]["type"][1] = "multipart";
$message["attachment"]["type"][2] = "message";
$message["attachment"]["type"][3] = "application";
$message["attachment"]["type"][4] = "audio";
$message["attachment"]["type"][5] = "image";
$message["attachment"]["type"][6] = "video";
$message["attachment"]["type"][7] = "other";
for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++)
{
$structure = imap_fetchstructure($mbox, $jk, FT_UID);
$parts = (isset($structure->parts) ? $structure->parts : false);
$fpos = 2;
for ($i = 1; $i < count($parts); $i++)
{
$message["pid"][$i] = ($i);
$part = $parts[$i];
if ($part->disposition == "ATTACHMENT")
{
$message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
$message["subtype"][$i] = strtolower($part->subtype);
$ext = $part->subtype;
$params = $part->dparameters;
$filename = $part->dparameters[0]->value;
$mege = "";
$data = "";
$mege = imap_fetchbody($mbox, $jk, $fpos);
$filename = "$filename";
$fp = fopen($savedirpath . $filename, 'w');
$data = $this->getdecodevalue($mege, $part->type);
fputs($fp, $data);
fclose($fp);
$fpos += 1;
}
}
if ($delete_emails)
{
// imap_delete tags a message for deletion
imap_delete($mbox, $jk);
}
} // imap_expunge deletes all tagged messages if ($delete_emails) { imap_expunge($mbox); }
imap_close($mbox);
} }
?>
add
imap_expunge($jk);
after
imap_delete($mbox, $jk);
if it doesn't work try imap_errors to see whats going on.

How to split and count sms messages from file in PHP

I have problem that my sms messages are imported with csv, then it is checked if number is ok and how long sms is. My problem is that if text messages is longer then 160 it still enters 1 in databse. But it should start counting, if it is less or equal than 160, it is 1 messages, if it is more than 160 but less or equal than 320 it is two messages and if it is more then it is 3 messages.
Page code is here:
<?php
$link = #mysql_connect("localhost", "admin", "") or die("Error: Database offline.");
mysql_select_db("database", $link);
mysql_query("SET NAMES 'utf8' ", $link);
function detect_type($smstext) {
$type = 0;
$dec_codes = array();
for ($i = 0; $i < strlen($smstext); $i++) {
$symbol = substr($smstext,$i,1);
if (!in_array(ord($symbol), $dec_codes)) { $type = 1; }
}
return $type;
}
$result_array = array();
$unic_numbers = array();
$fp = file_get_contents($_FILES['filename']['tmp_name']);
$fp = str_replace("\r\n", "\n", $fp);
$fp = str_replace("\r", "\n", $fp);
$fp = str_replace("\t", "", $fp);
$rows = explode("\n", $fp);
$imported_rows = 0;
$duplicate_rows = 0;
$error_rows = 0;
$long_rows = 0;
for ($i = 0; $i < sizeof($rows); $i++) {
$data = explode(";", $rows[$i]);
$data[1] = sms_formatNumbers($data[1]); // formating number
$userid = 78;
if(strlen($data[1]) > 9){
if($unic_numbers[$data[1]] != true ){ // unic number check
$unic_numbers[$data[1]] = true;
$imported_rows++;
$fullSMS = iconv("ISO-8859-1","UTF-8", trim($data[2])." ".trim($data[3])." ".trim($data[4]));
if(strlen($fullSMS) > 164){
$long_rows++;
}
if($_POST['action'] == 'send'){
// SMS TEXT
$smstext = str_replace("õ", "ò", $fullSMS);
$smstext = str_replace("Õ", "ò", $smstext);
$type = detect_type($smstext);
// servicegroup
$char2 = substr($data[1], 0, 2);
$char3 = substr($data[1], 0, 3);
$c1 = mysql_query("SELECT * FROM zone_info WHERE country_code = '".$char2."'", $link);
$c2 = mysql_query("SELECT * FROM zone_info WHERE country_code = '".$char3."'", $link);
if (mysql_num_rows($c1) == 1) {
$r = mysql_fetch_array($c1);
$price = $r['price'];
$z = mysql_query("SELECT * FROM zone WHERE id = ".$r['up']."", $link);
$zone = mysql_fetch_array($z);
$zone_id = $zone['id'];
$servicegroup = $zone['servicegroup'];
} else if (mysql_num_rows($c2) == 1) {
$r = mysql_fetch_array($c2);
$price = $r['price'];
$z = mysql_query("SELECT * FROM zone WHERE id = ".$r['up']."", $link);
$zone = mysql_fetch_array($z);
$zone_id = $zone['id'];
$servicegroup = $zone['servicegroup'];
}
require_once("../scripts/number.class.php");
$receiver = "00".$data[1];
$obj = new NumberClass($receiver);
$operator = $obj -> operator_code;
$country = $obj -> code;
$operator_name = $obj -> operator_name;
if(strlen($operator) > 0) {
$er = mysql_query("SELECT * FROM zone_exception WHERE country = ".$country." AND operator = ".$operator."", $link);
if (mysql_num_rows($er) == 1) {
$erand = mysql_fetch_array($er);
$price = $erand['price'];
$servicegroup = $erand['servicegroup'];
}
} else $operator_name = "-";
if ($operator_name == "-") { $servicegroup = $servicegroup; }
else {
if ($operator_name == " First Operator") $servicegroup = "90";
else if ($operator_name == "Second Operator") $servicegroup = "91";
else if ($operator_name == "Third Operator") $servicegroup = "92";
else $servicegroup = $servicegroup;
}
require_once("../core/init.mini.inc.php");
$servicegroup = UserBasedRerouting($receiver, $userid, $operator_name, $servicegroup);
$client_type ='corporative';
$sender = $data[0];
$zone_id = 11;
$client_sms_id = '0';
$client_want_report = '0';
$client_report_url = '';
$amount = 1;
$dt_delaysend = '1970-01-01 00:00:00';
$SMSsent = 0;
$SMStotal = 1;
$smstext_old = $smstext;
while($SMSsent < $SMStotal){
$sql = mysql_query("insert into sms_queue (user_id,client_type,dt_entered,sender,receiver,operator,smstext,sms_type,zone_id,client_sms_id,client_want_report,client_report_url,sms_price,amount,servicegroup,dt_delaysend) values ('$userid','$client_type','".date('Y-m-d H:i:s')."','$sender','$receiver','$operator_name','$smstext',0,'$zone_id','$client_sms_id','$client_want_report','$client_report_url','$price','$amount','$servicegroup','$dt_delaysend')", $link);
$SMSsent++;
}
}
}else{
$duplicate_rows ++;
}
}else{
$error_rows++;
}
}
$result_array['success'] = true;
$result_array['long_sms'] = $long_rows;
$result_array['send_sms'] = $imported_rows;
$result_array['error_sms'] = $error_rows;
$result_array['duplicate_sms'] = $duplicate_rows;
$result_array['action'] = $_POST['action'];
echo json_encode($result_array);
function sms_formatNumbers($number){
$number = (int)$number;
$start_code = (int)substr($number,0,4);
if($start_code < 3780 or $start_code == 3785 or $start_code > 3789){
return $number;
}else{
return '';
}
}
?>
Can someone help me out with that?
Thank you
Try
if(strlen($fullSMS) > 164){
$long_rows = ceil(strlen($fullSMS)/160);
}
instead of
if(strlen($fullSMS) > 164){
$long_rows++;
}

Parsing user input via AJAX and PHP

Now, this isn't a "debug my codez for me plz" question. I've spent... I don't even want to think how long on trying to fix this issue.
My problem is that I am executing this php page that is, ideally, examining a string and converting it to a more secure format since this page is to store passwords for accounts.
What I'm aiming at with this code below is to slice the string repeatedly at each character and then evaluate it to my key. It does this for the whole string length. What's been being returned to me is '0'. I don't know how the system is even getting this value.
Maybe I'm using the substr() function wrong? Also, I'm open to a completely different method of parsing the string, such as using a RegExp. Thank you for your help, guys!
Code:
<?php
error_reporting(0);
#region Initial
$accstr = "apple";
$accn = "scourge";
//Key
$a[0] = "#"; //These variables are for conversion; indexes of this array correspond to those
$a[1] = "!"; //of the other array ($ind)
$a[2] = "#";
$a[3] = "%$";
$a[4] = "%";
$a[5] = "!#";
$a[6] = "&*";
$a[7] = "^";
$a[8] = "##";
$a[9] = "&^";
$a[10] = "&";
$a[11] = "%~";
$a[12] = "!%";
$a[13] = "!$";
$a[14] = "*#";
$a[15] = "#*";
$a[16] = "~";
$a[17] = "~&";
$a[18] = "``";
$a[19] = "/^";
$a[20] = "%`";
$a[21] = "~~";
$a[22] = "`~";
$a[23] = "%%";
$a[24] = "~!";
$a[25] = "~#";
$a[26] = "``#";
$a[27] = "``!";
$a[28] = "``#";
$a[29] = "``%$";
$a[30] = "``%";
$a[31] = "``!#";
$a[32] = "``&*";
$a[33] = "``^";
$a[34] = "``##";
$a[35] = "``&^";
$a[36] = "&&^#";
$a[37] = "~#!";
$a[38] = "!#&#";
$a[39] = "%~~$";
$a[40] = "%`%";
$a[41] = "!^~#";
$a[42] = "&#$*";
$a[43] = "^**&";
$a[44] = "#%#`";
$a[45] = "&``!#^";
$a[46] = "&**~&";
$a[47] = "%|~";
$a[48] = "!-|~%";
$a[49] = "!$~";
$a[50] = "*/#";
$a[51] = "#%*";
$a[52] = "|~";
$ind[0] = "a";//These are used to tell what's being looked at in the string
$ind[1] = "b";
$ind[2] = "c";
$ind[3] = "d";
$ind[4] = "e";
$ind[5] = "f";
$ind[6] = "g";
$ind[7] = "h";
$ind[8] = "i";
$ind[9] = "j";
$ind[10] = "k";
$ind[11] = "l";
$ind[12] = "m";
$ind[13] = "n";
$ind[14] = "o";
$ind[15] = "p";
$ind[16] = "q";
$ind[17] = "r";
$ind[18] = "s";
$ind[19] = "t";
$ind[20] = "u";
$ind[21] = "v";
$ind[22] = "w";
$ind[23] = "x";
$ind[24] = "y";
$ind[25] = "z";
$ind[26] = "0";
$ind[27] = "1";
$ind[28] = "2";
$ind[29] = "3";
$ind[30] = "4";
$ind[31] = "5";
$ind[32] = "6";
$ind[33] = "7";
$ind[34] = "8";
$ind[35] = "9";
$ind[36] = "~";
$ind[37] = "!";
$ind[38] = "#";
$ind[39] = "#";
$ind[40] = "$";
$ind[41] = "%";
$ind[42] = "^";
$ind[43] = "&";
$ind[44] = "*";
$ind[45] = "(";
$ind[46] = ")";
$ind[47] = "_";
$ind[48] = "+";
$ind[49] = "`";
$ind[50] = "-";
$ind[51] = "=";
$ind[52] = "?";
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$finln = "";
#endregion
#region Create coded password
$pwlen = strlen($accstr);
for($cnter=1;$cnter<=$pwlen;$cnter++)
{
$a1 = substr($accstr,$cnter,1);
for($cnter2=1;$cnter2<=52;$cnter2++)
{
if($a1==$ind[$cnter2])
{
$finln += $a[$cnter2];
}
}
}
#endregion
#region Send finln
$newpw = $xml->createElement($accn);
$newpw->appendChild($xml->createElement('password', $finln));
$xml->getElementsByTagName('cache')->item(0)->appendChild($newpw);
file_put_contents("pwDB.xml",$xml->saveXML());
print $finln;
#endregion
?>
So typical password hashing is one way - if you need two way then youre talking about encryption which is different.
Normally for hashing youd do something like the following, though id urge not to take this verbatim but to research some on your own so oyu understand what youre doing and the concepts involved:
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$account = 'someuser';
$password = 'passw0rd';
// your salt can be a constant that you never change, or can be user specific
// if you make it user specific then you need to store it as well as the password
$salt = "1j0i90#$t%";
$hash = hash('sha256', $password . $salt);
$acct = $xml->createElement($account);
$pw = $xml->createElement('password', $salt);
$acct->appendChild($pw);
$xml->appendChild($acct);
file_put_contents("pwDB.xml",$xml->saveXML());
And then to compare credentials like for a login you would do:
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('pwDB.xml');
$account = 'someuser';
$password = 'passw0rd';
$salt = "1j0i90#$t%";
$hash = hash('sha256', $password . $salt);
$xpath = new DOMXPath($xml);
// look up by account name - assuming these are unique
$accountNodes = $xpath->query('//'.$account);
if($accountNodes->length) {
$accountNode = $accountNodes->item(0);
$pwNodes = $xpath->query('//password', $accountNode);
if($pwNodes->length) {
$pwNode = $pwNodes->item(0);
if($hash === (string) $pwNode) {
// authentication OK!
}
}
}

Categories