php recursion - notice: undefined offset: 20 array - php

I'm working to parse the following string but at the same time trying to get rid of the current undefined offset on the index array. I would appreciate some help w/ the undefined offset issue.
error_reporting(E_ALL);
ini_set('display_errors', '1');
function my_recursion($String, &$Inc) {
$l = strlen($String);
$has_quotes = 0; $array = array();
$x= 0;
for ($Inc; $Inc < $l; $Inc++) {
$my_char = $String[$Inc];
if ($my_char == '(' && !$has_quotes) {
$Inc++;
$array[$x] = my_recursion($String, $Inc);
$x++;
} else if ($my_char == '"') {
$has_quotes = !$has_quotes;
if (!$has_quotes)
$x++;
} else if ($has_quotes) {
$array[$x] .= $my_char;
}
}
print_r($array);
}
$String = '(("HELLO"("BAR")("FOO")()""))';
$Inc = 0;
(my_recursion($String, $Inc));

To get rid of the errors, add this line to the start of your function:
$array = array();
and replace this line:
$array[$x] .= $my_char;
with this:
$array[$x] = isset($array[$x])? $array[$x].$my_char : $my_char;
For help with your recursion, you'll need to describe its desired behavior.

Related

slowAES decrypt to another key

There was a problem with the old slowAES library.
When trying to decrypt, in js it produces one, and in php it produces another.
There are a lot of errors in the console that I can’t figure out.
Tell me what's wrong? How to get the same keys?
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$a = "cd36b76f96b103402924bd5f76d3c204";
$b = "680eb6a492f48ea1b342aea7b79e18eb";
$c = "f746749b113236227058bd471f5c91dc";
function toHex($args){
if(func_num_args() != 1 || !is_array($args)){
$args = func_get_args();
}
$ret = '';
for($i = 0; $i < count($args) ;$i++)
$ret .= sprintf('%02x', $args[$i]);
return $ret;
}
function toNumbers($s){
$ret = array();
for($i=0; $i<strlen($s); $i+=2){
$ret[] = hexdec(substr($s, $i, 2));
}
return $ret;
}
function getRandom($min,$max){
if($min === null)
$min = 0;
if($max === null)
$max = 1;
return mt_rand($min, $max);
}
function generateSharedKey($len){
if($len === null)
$len = 16;
$key = array();
for($i = 0; $i < $len; $i++)
$key[] = getRandom(0,255);
return $key;
}
function generatePrivateKey($s,$size){
if(function_exists('mhash') && defined('MHASH_SHA256')){
return convertStringToByteArray(substr(mhash(MHASH_SHA256, $s), 0, $size));
}else{
throw new Exception('cryptoHelpers::generatePrivateKey currently requires mhash');
}
}
function convertStringToByteArray($s){
$byteArray = array();
for($i = 0; $i < strlen($s); $i++){
$byteArray[] = ord($s[$i]);
}
return $byteArray;
}
function convertByteArrayToString($byteArray){
$s = '';
for($i = 0; $i < count($byteArray); $i++){
$s .= chr($byteArray[$i]);
}
return $s;
}
include 'cryptovh/aes.php';
$aes = new AES();
$token = $aes->decrypt(toNumbers($c), 16, 2, toNumbers($a), 16, toNumbers($b));
echo toHex($token); // WHAT I HAVE
echo "<br>";
echo "016e9be78dd5130beb5febcd328ff588"; // WHAT I NEED
?>
cryptovh/aes.php
that is copy of: https://github.com/aleaxit/slowaes/blob/master/php/aes_fast.php
use this library:
https://github.com/aleaxit/slowaes
At the output, I get this token:
dd2f6d60b939b390dc19688babc3873d
And console errors:
Notice: Undefined offset: 16 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined offset: 20 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined offset: 24 in
/var/www/myuser/data/www/example.com/cryptovh/aes.php on line 386
Notice: Undefined index: in
/var/www/myuser/data/www/example.com/panel/cryptovh/aes.php on line
386
In slowaes/php/aes_fast.php, the inversion of the MixColumns operation in the mixColumns method is implemented incorrectly, the else-block must be:
...
} else {
for ($c = 0; $c < 4; $c++) {
$t[ $c] = self::$GEX[$state[$c]] ^ self::$GBX[$state[4+$c]] ^ self::$GDX[$state[8+$c]] ^ self::$G9X[$state[12+$c]];
$t[ 4+$c] = self::$G9X[$state[$c]] ^ self::$GEX[$state[4+$c]] ^ self::$GBX[$state[8+$c]] ^ self::$GDX[$state[12+$c]];
$t[ 8+$c] = self::$GDX[$state[$c]] ^ self::$G9X[$state[4+$c]] ^ self::$GEX[$state[8+$c]] ^ self::$GBX[$state[12+$c]];
$t[12+$c] = self::$GBX[$state[$c]] ^ self::$GDX[$state[4+$c]] ^ self::$G9X[$state[8+$c]] ^ self::$GEX[$state[12+$c]];
}
}
...
The inverse of the MixColumns operation is required for decryption.
There is also a typo in the method invMain, line 3, where i must be replaced by $i.
With these changes, the expected result is obtained, which can be verified e.g. here. The warnings are also no longer displayed.
I've filed an issue here. Note the ReadMe: The code is intended more for didactic purposes. In practice, openssl_encrypt / openssl_decrypt or similar should be used.

Undefined Offset Error in a controlled for loop using array_intersect

I am getting an undefined offset error for the 2 lines I have marked near the bottom of this code. From what I can see it is in a controlled for loop, but obviously something is amiss.
I think that array_intersect creates an array of the shared values between $temparray1 and $temparray2.
$temparray1 = (1,2,3,4,5,6);
$temparray2 = (5,5,6,7,8,9);
$array3 = array_intersect($temparray1, $temparray2);
if (!empty($array3)) {
$match = true;
} else {
$match = false;
}
if ($match === true){
$tempcount = count($array3);
$i = 0;
while ($i < $tempcount){
//UNDEFINED OFFSET ERROR FOR NEXT 2 LINES
if(!in_array($array3[$i],$scheduled_groups)){
$insertgroup = $array3[$i];
$i++;
} // End of ifarray
} // End of while
}

PHP Notice: Undefined offset: 0

I receive the following PHP notice
PHP Notice: Undefined offset: 0
Here is a sample of what causes the error.
$file = $_SERVER['DOCUMENT_ROOT']."/savedposts/edited".$post_id.".txt";
$saved_content = file_get_contents($_SERVER['DOCUMENT_ROOT']."/savedposts/".$post_id.".txt");
$cct = 0;
$contexttext = array();
for ($i = 0; $i < strlen($saved_content); $i++) {
if (substr($saved_content, $i, 9) == "[Context]") {
$i = $i + 9;
while (substr($saved_content, $i, 10) !== "[/Context]") {
$contexttext[$cct] .= substr($saved_content, $i, 1);
$i++;
}
$cct++;
}
}
The error is on this line
$contexttext[$cct] .= substr($saved_content, $i, 1);
How to fix the notice.
To replicate this:
$cct = 0;
$contexttext = array();
$contexttext[$cct] .= 'test';
Here 'test' is being appended to $contexttext[$cct], which evaluates to: $contexttext[0]. However there is nothing at [0] yet, because it's an empty array, we can't append to something that doesn't exist
If however you'd done this:
$cct = 0;
$contexttext = array();
$contexttext[$cct] = '';
$contexttext[$cct] .= 'test';
Then the notice would dissapear, because now when we append a string, we have something to append it to

Undefined variable for loop

I have a code in use and it generates a PHP error in the second "for" loop.
PHP Notice: Undefined variable: newmatches
if (empty($result['ERR'])) {
preg_match_all('(<h3><a[^<>]*href="([^<>]*)"[^<>]*>(.*)</a>\s*</h3>)siU', $result['EXE'], $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
$matches[1][$i] = urldecode($matches[1][$i]);
preg_match_all('/\*\*(http:\/\/.*$)/siU', $matches[1][$i], $urls);
$newmatches[1][$i] = $urls[1][0];
}
for ($i = 0; $i < count($newmatches[1]); $i++) { //PHP Notice: Undefined variable: newmatches
if(strstr($newmatches[1][$i], $domain))
return $i+1;
}
} else {
return '0';
}
Thank you in advance!
I don't see anywhere where $newmatches would be set, aside from the first for loop, which won't run if count($matches[1]) is 0.
Not sure what all this hardcoding of your index to 1 is about, but a simple fix is to set $newmatches[1] = array() before the first loop.

Undefined offset: 9, Undefined variable

Error at this line: $an = explode(";", $f[$i]);
This one too: if ($wasone) (Undefined variable)
Any help? Thank you.
<?
if ($_POST["submit"])
{
$a = answer();
$out = "Q: $ask<br>A: ".$a;
$tile = ($cfg["scrolling"]) ? $tile : "";
echo "$out<br>$tile";
echo "<input name='tile' type='hidden' id='tile' value='$out<br>$tile'>";
}
// answers
function answer()
{
global $cfg, $ask;
$ask = (empty($ask)) ? "<empty>" : $ask;
$kick = array("?","\n");
$ask = str_replace($kick,"",$ask);
$f = file($cfg["answersfile"]);
for ($i=0; $i<=count($f); $i++)
{
$an = explode(";", $f[$i]);
$a = $an[0];
if (strstr($a,trim($ask)))
{
if ($wasone)
{
return("Please be more concrete");
}
array_shift($an);
array_pop($an);
$ai = rand(0, count($an)-1);
// answering
$wasone = true;
$retval = $an[$ai];
}
}
$retval = (empty($retval)) ? "I dont understand you. Please try again." : $retval;
return $retval;
}
?>
the condition in the for loop should be
$count = count($f);
for ($i=0; $i<$count; $i++)
without the '=' to ensure that only indexes accessed range from 0 to count-1
The line
for ($i=0; $i<=count($f); $i++)
should probably be
for ($i=0; $i<count($f); $i++)
count() returns the number of elements of $f, which is one more than the index of the last element of $f (in this case nine). You want to stop before $i gets past the index of the last element
By default array indexes begin from 0. If count($f) === 9, then it means your array there has indexes 0, 1, 2 ... 8. If you loop while $i <= 9, then you will try to access element with index 9 ... which is not there.

Categories