I have checked this before posting the question.
this is a small part of a code that uses create_function
$lambda_functions[$code_hash] = create_function('$action, &$self, $text', 'if ($action == "encrypt") { '.$encrypt.' } else { '.$decrypt.' }');
tried using this way
$lambda_functions[$code_hash] = function ( $action, &$self, $text ) use ( $encrypt, $decrypt ) {
if ($action == "encrypt") {
return $encrypt;
} else {
return $decrypt;
}
};
but doesn't work as expected $encrypt or $decrypt will contain code that looks something like this
$encrypt = $init_encryptBlock . '
$ciphertext = "";
$text = $self->_pad($text);
$plaintext_len = strlen($text);
$in = $self->encryptIV;
for ($i = 0; $i < $plaintext_len; $i+= '.$block_size.') {
$in = substr($text, $i, '.$block_size.') ^ $in;
'.$_encryptBlock.'
$ciphertext.= $in;
}
if ($self->continuousBuffer) {
$self->encryptIV = $in;
}
return $ciphertext;
';
It is working fine with create_function but not with anonymous function not sure where I am going wrong?
The difference is, that with create_function() your code was submitted as a string and interpreted as code, but with an anonymous function the string is interpreted as a string, not as the code it contains.
You can just extract the code you have from the string that you have in $encrypt and $decrypt. This would look like this:
/*
* Removed the "use ($encrypt, $decrypt)" part,
* because those were the strings that contained the code,
* but now the code itself is part of the anonymous function.
*
* Instead, i added "use ($block_size)", because this is a vairable,
* which is not defined inside of your function, but still used in it.
* The other code containing variables might include such variables as
* well, which you need to provide in the use block, too.
*/
$lambda_functions[$code_hash] = function ( $action, &$self, $text ) use ($block_size) {
if ($action == "encrypt") {
//Extract $init_encryptBlock here
$ciphertext = "";
$text = $self->_pad($text);
$plaintext_len = strlen($text);
$in = $self->encryptIV;
for ($i = 0; $i < $plaintext_len; $i+= $block_size) {
$in = substr($text, $i, $block_size) ^ $in;
// Extract $_encryptBlock here
$ciphertext.= $in;
}
if ($self->continuousBuffer) {
$self->encryptIV = $in;
}
return $ciphertext;
} else {
//Extract $decrypt here
}
};
Please keep in mind, that this is not a complete answer. You find numerous // Extract $variable here comments in the code, which stand for each code containing variable, that you have in your code and which needs to be extracted just in the way, in which i etracted the code from $encrypt.
Related
I have created for code to check if its palindrome. Now if the string is not a palindrome I want it to be reversed.Can it be done using one conditional loop?
My php code:
class user{
public function __construct() {
if ($this->String_Rev()) {
echo 'Yes, palindrome';
} else {
echo 'not palindrome';
}
}
public function String_Rev() {
$str = "abba";
$i = 0;
while ($str[$i] == $str[strlen($str) - ($i + 1)]) {//Incrementing and decrementing the values in the string at the same time/
$i++;
if ($i > strlen($str)/ 2) {//If the i goes ahead of half of its string then return true and stop its execution.
return 1;
}
}
return 0;
}
}
$obj = new user();
Strings in PHP are not arrays, but you can select the character index of a string just like an array key, which makes the following possible:
<?php
$string = 'foobar';
$string_array = array();
//make an actual array of the characters first
//if you want this to be an object, do this part in your constructor,
//and assign this variable to an object property to work with.
for ($i=0; $i < strlen($string); $i++)
{
$string_array[$i] = $string[$i];
}
echo implode( $string_array ); //prints 'foobar'
echo '<br>' . PHP_EOL;
echo implode( array_reverse( $string_array ) ); //prints 'raboof'
You could easily simplify your palindrome logic by doing this:
//in your case, this would probably be it's own method,
//using the aforementioned class property made in the constructor
$is_palindrome = implode( $string_array ) === implode( array_reverse( $string_array ) );
I have a class Tpl to mount template with this function (template.php)
function Set($var, $value){
$this->$var = $value;
}
A php file that call the function, example (form.php):
$t->Set("lbAddress","Address");
And a html file with the template with tags (template.html)
<tr><td>[lbAdress]</td></tr>
To print the html I have this function (template.php) - the notice points to this function
function Show_Temp($ident = ""){
// create array
$arr = file($this->file);
if( $ident == "" ){
$c = 0;
$len = count($arr);
while( $c < $len ){
$temp = str_replace("[", "$" . "this->", $arr[$c]);
$temp = str_replace("]", "", $temp);
$temp = addslashes($temp);
eval("\$x = \"$temp\";");
echo $x;
$c++;
}
} else {
$c = 0;
$len = count($arr);
$tag = "*=> " . $ident;
while( $c < $len ){
if( trim($arr[$c]) == $tag ){
$c++;
while( (substr(#$arr[$c], 0 ,3) != "*=>" ) && ($c < $len) ){
$temp = str_replace("[", "$" . "this->", $arr[$c]);
$temp = str_replace("]", "", $temp);
$temp = addslashes($temp);
eval("\$x= \"$temp\";"); //this is the line 200
echo $x;
$c++;
}
$c = $len;
}
$c++;
}
}
}
If the template .html have a line [lbName] and I don't have the line $t->Set("lbName","Name"); at the php code, I receive the error PHP Notice: Undefined property: Tpl::$lbName in ../template.php(200) : eval()'d code on line 1. The solution that I found is add lines like $t->Set("lbName","");, but if I have 50 tags in HTML that I don't use in PHP, I have to add all 50 $t->Set("tag_name","");. The error occurred after migrate to the PHP 5.
Can someone help me? Thanks
Perhaps a better way still would be not to rely on dynamic evaluation through eval (it's generally best to avoid eval where possible), but to replace [lbName] with the value stored in the object directly as and when needed. If you can replace [lbName] with $this->lbName, surely you can also replace it with the value of lBName that you've looked up on-the-fly?
To answer your original question, however:
If I understand correctly, you're setting the values like this:
$t->Set('foo', 'bar');
And – effectively – getting them like this:
$t->foo;
If so, you could implement a __get method to intercept the property references and provide your own logic for retrieving the value; e.g.:
public function __get($key)
{
// You can adapt this logic to suit your needs.
if (isset($this->$key))
{
return $this->$key;
}
else
{
return null;
}
}
In this case, you'd probably be better off using an associative array as the backing store, and then using __get and __set to access it; e.g.:
class Template
{
private $values = array();
public function __get($key)
{
if (array_key_exists[$key, $this->values])
{
return $this->values[$key];
}
else
{
return null;
}
}
public function __set($key, $value)
{
$this->values[$key] = $value;
}
}
I use the following class in my CakePHP app to create reversible encrypted ids:
class Tiny
{
public static $set = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
public static function toTiny($id)
{
$set = self::$set;
$HexN="";
$id = floor(abs(intval($id)));
$radix = strlen($set);
while (true)
{
$R=$id%$radix;
$HexN = $set{$R}.$HexN;
$id=($id-$R)/$radix;
if ($id==0) break;
}
return $HexN;
}
public static function reverseTiny($str)
{
$set = self::$set;
$radix = strlen($set);
$strlen = strlen($str);
$N = 0;
for($i=0;$i<$strlen;$i++)
{
$N += strpos($set,$str{$i})*pow($radix,($strlen-$i-1));
}
return "{$N}";
}
}
I'm looking for something just as simple for strings. Where it has a simple set variable and just two functions, one for encrypting and one for decrypting. All of the ones I have seen so far have been over-complicated and too framework-based. I want a class that is completely stand-alone and not reliant on other files and easily integrated into CakePHP.
Can anyone help? Does anyone know of one or could help me modify this script to work with strings instead so I can create another class. It needs to be simple to use: e.g. Class::encrypt(string);
Also security is not a major concern here as it's not being used for protecting anything rather just hashing a string in a way that can be reversed.
This is what I want:
class Encrypt
{
public static $set = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
public static function Encrypt($string)
{
$set = self::$set;
return;
}
public static function Decrypt($string)
{
$set = self::$set;
return;
}
}
Here is a modified version of the code that I use. It relies on openssl, so it may not be suitable if you need it to be portable.
<?php
class Cipher {
public static function strongRand($num_bytes, $raw = false) {
$rand = openssl_random_pseudo_bytes($num_bytes);
if (!$raw) {
$rand = base64_encode($rand);
}
return $rand;
}
public static function encrypt($data, $password = "default", $type = "aes128") {
$iv = Cipher::strongRand(12);
$encrypted = openssl_encrypt($data, $type, $password, false, $iv);
return $iv . $encrypted;
}
public static function decrypt($data, $password = "default", $type = "aes128") {
$iv = substr($data, 0, 16);
$data = substr($data, 16);
$decrypted = openssl_decrypt($data, $type, $password, false, $iv);
return $decrypted;
}
}
?>
An example of how to use the code:
<?php
include('cipher.php');
$enc = Cipher::encrypt('kachow');
echo $enc . "\n";
$dec = Cipher::decrypt($enc);
echo $dec . "\n";
?>
Outputs:
0fykYiBPJAL/C3fFuX+jApFRdRw7NY8uYmGaaQ==
kachow
If you need to crypt numbers, I use those simple functions (change the random letters in function _map_key to whatever you want, just be sure that they are unique). If you do not have numbers but string, you can STR2BIN them, then use this function:
function _map_kid($kid, $umap=false){
$map = array('M','Y','S','I','M','P','L','E','K');
$ret = '';
for($i = 0; $i<strlen($kid); $i++){
if($umap){
$ret .= array_search(substr($kid,$i,1),$map);
} else {
$ret .= $map[substr($kid,$i,1)];
}
}
return $ret;
}
function cript_customer_id($customer_id, $key=0){
if($key==0){
$key = trim(microtime(true));
}
$key = intval($key);
if(strlen($key)<=3) $key +=999;
if(substr($key,-1)==0) $key +=3;
$key = substr($key,-3);
$kid = ((($customer_id.substr($key,0,1))+substr($key,1,1))*substr($key,-1)).$key;
return _map_kid($kid);
}
function _decript_customer_id($kid){
if(trim($kid)=='') return false;
$kid = strtoupper($kid);
$kid = _qsc2_map_kid($kid, true);
$key = substr($kid, -3);
$customer_id = substr($kid, 0, -3);
if(substr($key,-1)>0){
$customer_id = substr((($customer_id/substr($key,-1))-substr($key,1,1)),0,-1);
} else {
$customer_id = 0;
}
return $customer_id;
}
There are various pure PHP implementations of standard crypto primitives like AES that you could use, like this or this one.
The RC4 algorithm, in particular, is something you could implement in about a dozen lines of PHP if you wanted.
However, those implementations are not likely to be very efficient; in general, when it comes to crypto in high-level languages like PHP, you can have any two out of "secure", "fast" and "self-contained".
OK, if all you want is obfuscation, here's something based on this example by "peter at NOSPAM jamit dot com" over at php.net:
class Obfuscate
{
public static function obfuscate($string)
{
return str_rot13( base64_encode( $string ) );
}
public static function deobfuscate($string)
{
return base64_decode( str_rot13( $string ) );
}
}
This should be fast and self-contained, but it provides very little security. At best it makes a nice little puzzle for amateur cryptographers, of a level perhaps comparable with a newspaper crossword puzzle.
Look into MCrypt functions. It'll be even shorter than the above code. And secure too!
I prefer to use mcrypt, but if don't wanna, the try to find something useful on phpclasses.org, -like this http://www.phpclasses.org/package/2995-PHP-Encode-and-decode-text-strings-with-random-keys.html
How would you write a short php function to reverse a string. The function must:
have only one argument
not use the built-in function 'strrev' or 'array_reverse'
not a use a looping construct like 'for', 'foreach' or 'while'.
Quickly scanning down, these all look so long!
function rev($str) {
return $str?rev(substr($str,1)).$str[0]:'';
}
Recursive so obviously doesn't work on strings longer than 100 chars.
Since this sounds like homework question I'll tell you how but you code it yourself.
Turn the string into an array with a cast. Then use one of the array sorting functions that takes a user defined sorting function.
function reverseString($string) {
return shell_exec(sprintf('ruby -e \'puts "%s".reverse\'', preg_replace("/\"/", "\\\"", $string)));
}
Recursive solution:
function sr( $txt ){
return $txt[ strlen( $txt ) - 1 ] . ( ( strlen( $txt ) > 1 )?sr( substr( $txt, 0, strlen($txt)-1 ) ):null) ;
}
echo sr( "abc def ghi jklm" );
Explanation:
return $txt[ strlen( $txt ) - 1 ] // return last byte of string
. // concatenate it with:
(( strlen( $txt ) > 1 ) ? // if there are more bytes in string
sr( substr( $txt, 0, strlen( $txt ) - 1 ) // then with reversed string without last letter
: null ); // otherwise with null
To make it work with zero-length string, another conditional expression was added:
return (strlen($txt))? ($txt[ strlen( $txt ) - 1 ] . ( ( strlen( $txt ) > 1 )?sr( substr( $txt, 0, strlen($txt)-1 ) ):null)):"" ;
<?php
// Reversed string and Number
// For Example :
$str = "hello world. This is john duvey";
$number = 123456789;
$newStr = strrev($str);
$newBum = strrev($number);
echo $newStr;
echo "<br />";
echo $newBum;
OUTPUT :
first : yevud nhoj si sihT .dlrow olleh
second: 987654321`enter code here`
Meets all your requirements but works only in PHP 5.3 or higher. Making it work on others is left as homework.
function reverse($str) {
$i=0;
$j=strlen($str)-1;
start:
if($i>=$j) {
goto done;
}
$tmp = $str[$j];
$str[$j--] = $str[$i];
$str[$i++] = $tmp;
goto start;
done:
return $str;
}
function reverse_string($string) {
if($string !== '')
return substr($string, -1).reverse_string(substr($string, 0, strlen($string)-2));
}
A recursive solution. Feels like something like that is what your teacher is looking for.
function my_strrev ($str) {
$length = strlen($str);
switch ($length) {
case 0: return '';
case 1: return $str; break;
case 2: return $str[1] . $str[0];
default :
return $str[$length-1] . my_strrev(substr($str,1,-1)) . $str[0];
break;
}
}
It swaps the first and the last letter and than makes the same with the rest of the string.
Update:
Inspired by mateusza I created another solution (its fun ;))
function my_strrev2 ($str) {
return $str
? my_strrev2(substr($str, 1)) . $str[0]
: '';
}
It works similar to mateuszas, but this one appends the first character, instead of prepend the last one.
My answer is OOP and uses recursion. Takes care by itself of the recursion limit.
class StringReverser
{
public $reversed_string;
public function __construct ($string) {
$original_recursion_limit = ini_get('pcre.recursion_limit');
$array = str_split($string);
krsort($array);
$i = strlen($string);
ini_set('pcre.recursion_limit', $i+1);
$this->add2string($string, $i, $array);
ini_set('pcre.recursion_limit', $original_recursion_limit);
}
public function reverse() {
return $this->reversed_string;
}
private function add2string ($s, $i, $a) {
if($i) {
$i--;
$this->reversed_string .= $a[$i];
$this->add2string($s, $i,$a, $this->reversed_string);
}
}
}
$string = "Elzo Valugi";
echo $string ."<br>";
$reverser = new StringReverser($string);
echo $reverser->reverse();
<?php
$string="jomon is name my";
for($i=strlen($string);$i>=0;$i--)
{
$char.=$string{$i};
}
echo $char."<br/>";
for($i=0; $i<strlen($char);$i++)
{
if($char[$i+1]==" " || $char[$i+1]=="")
{
for($temp=$i; $temp>=0 && $char[$temp]!=' '; $temp--)
echo $char[$temp];
}
echo " ";
}
?>
I've got a multidimensional associative array which includes an elements like
$data["status"]
$data["response"]["url"]
$data["entry"]["0"]["text"]
I've got a strings like:
$string = 'data["status"]';
$string = 'data["response"]["url"]';
$string = 'data["entry"]["0"]["text"]';
How can I convert the strings into a variable to access the proper array element? This method will need to work across any array at any of the dimensions.
PHP's variable variables will help you out here. You can use them by prefixing the variable with another dollar sign:
$foo = "Hello, world!";
$bar = "foo";
echo $$bar; // outputs "Hello, world!"
Quick and dirty:
echo eval('return $'. $string . ';');
Of course the input string would need to be be sanitized first.
If you don't like quick and dirty... then this will work too and it doesn't require eval which makes even me cringe.
It does, however, make assumptions about the string format:
<?php
$data['response'] = array(
'url' => 'http://www.testing.com'
);
function extract_data($string) {
global $data;
$found_matches = preg_match_all('/\[\"([a-z]+)\"\]/', $string, $matches);
if (!$found_matches) {
return null;
}
$current_data = $data;
foreach ($matches[1] as $name) {
if (key_exists($name, $current_data)) {
$current_data = $current_data[$name];
} else {
return null;
}
}
return $current_data;
}
echo extract_data('data["response"]["url"]');
?>
This can be done in a much simpler way. All you have to do is think about what function PHP provides that creates variables.
$string = 'myvariable';
extract(array($string => $string));
echo $myvariable;
done!
You can also use curly braces (complex variable notation) to do some tricks:
$h = 'Happy';
$n = 'New';
$y = 'Year';
$wish = ${$h.$n.$y};
echo $wish;
Found this on the Variable variables page:
function VariableArray($data, $string) {
preg_match_all('/\[([^\]]*)\]/', $string, $arr_matches, PREG_PATTERN_ORDER);
$return = $arr;
foreach($arr_matches[1] as $dimension) { $return = $return[$dimension]; }
return $return;
}
I was struggling with that as well,
I had this :
$user = array('a'=>'alber', 'b'=>'brad'...);
$array_name = 'user';
and I was wondering how to get into albert.
at first I tried
$value_for_a = $$array_name['a']; // this dosen't work
then
eval('return $'.$array_name['a'].';'); // this dosen't work, maybe the hoster block eval which is very common
then finally I tried the stupid thing:
$array_temp=$$array_name;
$value_for_a = $array_temp['a'];
and this just worked Perfect!
wisdom, do it simple do it stupid.
I hope this answers your question
You would access them like:
print $$string;
You can pass by reference with the operator &. So in your example you'll have something like this
$string = &$data["status"];
$string = &$data["response"]["url"];
$string = &$data["entry"]["0"]["text"];
Otherwise you need to do something like this:
$titular = array();
for ($r = 1; $r < $rooms + 1; $r ++)
{
$title = "titular_title_$r";
$firstName = "titular_firstName_$r";
$lastName = "titular_lastName_$r";
$phone = "titular_phone_$r";
$email = "titular_email_$r";
$bedType = "bedType_$r";
$smoker = "smoker_$r";
$titular[] = array(
"title" => $$title,
"first_name" => $$firstName,
"last_name" => $$lastName,
"phone" => $$phone,
"email" => $$email,
"bedType" => $$bedType,
"smoker" => $$smoker
);
}
There are native PHP function for this:
use http://php.net/manual/ru/function.parse-str.php (parse_str()).
don't forget to clean up the string from '"' before parsing.
Perhaps this option is also suitable:
$data["entry"]["0"]["text"];
$string = 'data["entry"]["0"]["text"]';
function getIn($arr, $params)
{
if(!is_array($arr)) {
return null;
}
if (array_key_exists($params[0], $arr) && count($params) > 1) {
$bf = $params[0];
array_shift($params);
return getIn($arr[$bf], $params);
} elseif (array_key_exists($params[0], $arr) && count($params) == 1) {
return $arr[$params[0]];
} else {
return null;
}
}
preg_match_all('/(?:(\w{1,}|\d))/', $string, $arr_matches, PREG_PATTERN_ORDER);
array_shift($arr_matches[0]);
print_r(getIn($data, $arr_matches[0]));
P.s. it's work for me.