Echo file contents inside params array - php

I'm trying to finish creating a php keygen for my product.
Is it possible for me to echo a text value inside on array (specifically the HWID which is taken from an uploaded text file)?
<?php
$file = file_get_contents('text.txt');
$params = array(
hwid => "echo $file;", // Exactly as returned by VMProtectGetCurrentHWID
user_data => base64_decode("CGCvRvMWcPHGdMjQ"), // string of bytes
);
?>
Is there a better way of doing it?

You don't need to echo just do this:
<?php
$file = file_get_contents('text.txt');
$params = array(
hwid => $file, // Exactly as returned by VMProtectGetCurrentHWID
user_data => base64_decode("CGCvRvMWcPHGdMjQ"), // string of bytes
);

Related

How to integrate manifest.json into wordpress theme

I have searched the internet, but the results I have seen are not clear enough.
I was able to add a manifest.json file into my wordpress theme by adding this code to the functions.php file.
//manifest file
add_action( 'wp_head', 'inc_manifest_link' );
// Creates the link tag
function inc_manifest_link() {
echo '<link rel="manifest" href="'.get_template_directory_uri().'/manifest.json">';
}
The manifest.json is as it should be.
However I wish to use some theme_mod functions within the manifest file. Which I can't do without php.
So is there anyway to write php code within the manifest.json file.
Thanks in advance.
So you can do something like this:
<?php
$filename = "manifest.json";
// Grab contents and decode them into an array
$data = json_decode(file_get_contents($filename), true);
//this is where you're adding your content so below you can write to the file
//Creating new var
//(make sure no var has the name "['newnewname']" unless you want to edit it)
$data['newname'] = 'Adding NEW contents';
//Creating new 2 dimensional array
$data['newname'] = array("another1" => "val1", "another2" => "val2");
//adding content to an existing 2 dimensional array
$data['exists'] += array("another1" => "val1", "another2" => "val2");
//adding content to an existing 3 dimensional array
$data['exists']['name'] += array("another1" => "val1", "another2" => "val2");
//adding content to an existing 4 dimensional array
$data['exists']['name']['name'] += array("another1" => "val1", "another2" => "val2");
// encode back into json
$jencode = json_encode($data);
// write over file with new contents
$fopen = fopen($filename, "w");
if(fwrite($fopen, $jencode)){
echo('Success!<hr>');
echo(file_get_contents($filename));
}
fclose($fopen);
Heres a live example (with fopen/write/read stuff removed):
Advanced: http://sandbox.onlinephpfunctions.com/code/eacdd6b8254c2127521769461d5f6d9daeda224d
Simple: http://sandbox.onlinephpfunctions.com/code/3589151881aac2e442738b381c05a37240081901

how to read out a value from serialized array stored in a txt-document? php

I just started to do some simple things with php. Now i have a question… 
In this code i open a txt-file and write a key-value-array into it.
At the end I want to read out one value from this array which is stored serialized in the txt-document.
$open = fopen("write/doc.txt", 'w+');
$content = array ("red" => "FF0000", "green" => "#00FF00", "blue" => "#0000FF");
$contentserialized = serialize($content);
fwrite($open, $contentserialized);
fclose($open);
So until now all works properly. And with this code i can show the content of the file:
$file = file_get_contents("write/doc.txt");
echo $file;
But I want only one value out of it. How can I choose for example the value for key "green" on the page?
Would be nice if you can tell me!
echo unserialize($file)['green'];
or if that doesn't work(my php is a little rusty)
$array = unserialize($file);
echo $array['green'];
http://php.net/manual/en/function.unserialize.php
You could load the entire document and parse the content back into a PHP array. That would allow you to work with the data as any other arrays. Something like the following should be enough.
$file = 'write/doc.txt';
$fhandle = fopen($file, 'r');
$content = fread($fhandle, filesize($file));
$content = unserialize($content);
echo $content['green'];
Just as a good practice, remember to close the file handle.
fclose($fhandle);
Hope this helps.
http://php.net/manual/en/function.unserialize.php

how do I pass many php variables to python

I am using the following code to initiate a python script and pass a php variable to it.
$tmp = exec("python path/to/pythonfile.py $myVariable $mySecondVariable", $output);
This works very well, my issue is that I will need to pass 100+ variables to the python script. I don't want this exec line to become extremely long and unmanageable. I have also explored passing a php array instead of a variable with the following code:
$checked = array(
"key1" => "1"
"key2" => "1"
"key3" => "1"
);
$checkedJson = json_encode($checked);
$tmp = exec("python path/to/pythonfile.py $myVariable $checkedJson", $output);
With this I have been unable to decode the JSON on the python side. I have been able to do a basic print of the array variable(undecoded) in python, but it gives every individual character as a new array value. ie [0] = k, [1] = e, [2] = y, [3] = 1, etc...
Any help is greatly appreciated.
Just to be clear,I am looking for a simpler method than encoding and decoding an array. Is there a way I can format the exec line to allow for multiple variables.
Store your PHP variables within a temporary text file then use python to read that file.
Simple and effective.
Assuming Scripts are in the same directory
PHP Portion
long version (self contained script - skip to the short version below if you only want the code snippet)
<?php
#Establish an array with all parameters you'd like to pass.
#Either fill it manually or with a loop, ie:
#Loop below creates 100 dummy variables with this pattern.
#You'd need to come up with a way yourself to fill a single array to pass
#$variable1 = '1';
#$variable2 = '2';
#$variable3 = '3';
#....
#$variableN = 'N';
#...
for ($i=1; $i<=100; $i++) {
${'variable'.$i} = $i;
}
#Create/Open a file and prepare it for writing
$tempFile = "temp.dat";
$fh = fopen($tempFile, 'w') or die("can't open file");
#let's say N=100
for ($i=1; $i<=100; $i++) {
#for custom keys
$keyname = 'Key'.$i;
# using a variable variable here to grab $variable1 ... $variable2 ... $variableN ... $variable100
$phpVariablesToPass[$keyname] = ${'variable'.$i} + 1000;
}
#phpVariablesToPass looks like this:
# [Key1] => 1001 [Key2] => 1002 [Key3] => 1003 [KeyN] = > (1000+N)
#now write to the file for each value.
#You could modify the fwrite string to whatever you'd like
foreach ($phpVariablesToPass as $key=>$value) {
fwrite($fh, $value."\n");
}
#close the file
fclose($fh);
?>
or in short, assuming $phpVariablesToPass is an array filled with your values:
#Create/Open a file and prepare it for writing
$tempFile = "temp.dat";
$fh = fopen($tempFile, 'w') or die("can't open file");
foreach ($phpVariablesToPass as $key=>$value) {
fwrite($fh, $value."\n");
}
fclose($fh);
Python Snippet to Grab the Data
lines = [line.strip() for line in open('temp.dat')]
the variable lines now contains all of your php data as a python list.

How to get multiple images from a single Drupal node using PHP

I have a custom content type in Drupal that allows multiple image uploads through a single field. I want to programmatically access the image URI's, apply my theme, and then get the output one by one. I can do this with a single image like so,
<?php
$image_style_name = 'my_theme';
$image_uri = $entity->field_image['und'][0]['uri'];
$image = theme('image_style', array('style_name' => $image_style_name, 'path' => $image_uri));
$image = image_style_url($image_style_name, $image_uri); ?>
but I am unsure how to access the entire array of images.
For anyone who needs it... the full solution:
<?php
$image_style_name = 'my_theme';
foreach($entity->field_image['und'] as $key => $value){
$image_uri = $entity->field_image['und'][$key]['uri'];
$image = theme('image_style', array('style_name' => $image_style_name, 'path' => $image_uri));
$output = image_style_url($image_style_name, $image_uri);
echo $output;
}
?>
They should be in the $entity->field_image['und'] array so you should be able to loop over the array and theme each one with something like
foreach($entity->field_image['und'] as $image_field){
..etc
}

problem writing array results to a csv file

I have a csv file I need to cleanup. It contains 13 fields, but I only need 7 (Business, Address, City, St, Zip, Phone, Email)
I need to run through all of the records and create a new output of just the records with email addresses.
In nutshell... I load the original file, run the for loop, explode the results, then look for the records where the $tmp[10] index is not null. I then get the rest of the rest of required fields, and do a foreach loop and fwrite the results to a new csv file.
Depending on how I tweak the code, I get either...
A text file of just email addresses.
or
A text file of just the last record with an email address.
I have been working on this too long and I just need a fresh set of eyes to point out the problem. I am new to php, and want to make this work. Thanks on advance.
<?php
// See if file exists and is readable
$file = 'uploads/AK_Accountants.csv';
$newfile = basename($file,".csv");
$newfile = $newfile.Date("Ymd").".csw";
$fileNew = fopen('uploads/AK_'.Date("Ymd").'.csv','w+');
// Read the file into an array called $sourcefile
$sourcefile = file($file);
// Loop through the array and process each line
for ($i = 0; $i < count($sourcefile); $i++) {
// Separate each element and store in a temp array
$tmp = explode('","', $sourcefile[$i]);
// Assign each element of the temp array to a named array key
if ($tmp[10] != "") {
$sourcefile[$i] = array('Business_Name' => $tmp[1], 'Address' => $tmp[3], 'City' => $tmp[4], 'State' => $tmp[5], 'Zip' => $tmp[6], 'Phone' => $tmp[7], 'Email' => $tmp[10]);
foreach($sourcefile[$i] as $key => $value);
fwrite($fileNew, $value);
}
}
?>
From a quick glance:
foreach($sourcefile[$i] as $key => $value);
fwrite($fileNew, $value);
should be
foreach($sourcefile[$i] as $key => $value){
fwrite($fileNew, $value);
}
Also, you have
$newfile = $newfile.Date("Ymd").".csw";
rather than what I assume should be
$newfile = $newfile.Date("Ymd").".csv";
Your last foreach statement is terminated by a ';' and has no code block. Once the foreach statement has finished iterating you'll get the last value written to file i.e. just the email address.
You currently have
foreach (... ) ;
fwrite(...);.
but you probably mean
foreach( ... ) {
fwrite(...) ;
}
Been there, done that :)
HTH

Categories