Crypto Encryption Path - php

i want to do crypto encryption in php but with the help of cryptoj.jar & one encyption class of java
i have encyption code that is work properly if i put my necessary jar file & java class in the following path
$_SERVER['DOCUMENT_ROOT'];
This is not an issue for local site, but this is not good idea to put libraries on this root on server.
If i created new directory & put this directory in project folder, the code is not work
i am doing the following code on server.
$path = 'java -cp ' . dirname(__FILE__).'/cryptoj.jar; EncryptURLParams "source=BAClubs&identifier=123456&timestamp=2014-02-21 09:59:37.498" ';
echo $path; exit;
$last_line = exec($path , $retval);
echo "XCODE=".$last_line; exit;
This code returns me nothing but if i put this on rootpath it gives me output. what is the problem guys..?

i got my answer by myself only
just have to put the libraries in th project's root folder & then simple use the following code
$path = 'java EncryptURLParams "source=BAClubs&identifier=123456&timestamp=2014-02-21 10:00:00';
$last_line = exec($path , $retval);
echo "return value--".$retval."<br/>";
echo "XCODE--".$last_line;
i got my output in $last_line variable.
Thanks Guys...

Related

PHP doesn't save to files

I have Apache 2 and PHP installed on a Raspberry Pi 1 B+ (RASPBIAN STRETCH LITE
). I have a website running on it with a textbox and a PHP script that is supposed to save the contents of the textbox to a text file on the server when the user submits.
I tryed basically everything but the php script just wont save.
PHP does get the textbox-content (I tested it - it works just fine).
This is my PHP:
<?php
include "code/multiPage/topBar.html";
$dir = "/data/searches.txt";
if ($_REQUEST) {
$input = $_REQUEST["search"];
file_put_contents($dir, $input, FILE_APPEND);
}
?>
PHP is working properly aside from this problem.
The folder has read-write permissions for everyone.
I have also tryed to let PHP create its own folder with code similar to this:
if (!file_exists('path/to/directory')) {
mkdir('path/to/directory', 0777, true);
}
PHP can't even do that.
Thanks in advance for any help!
I figured it out. The problem was that the webserver didn't have permission to write to the directory. problem solved by running sudo chown -R www-data var/www/html/data thanks for the help! Have a nice day :D
First just try this separately from other code
<?php
file_put_contents("/data/searches.txt", "bla", FILE_APPEND);
?>
if it's not working try to get a last error
echo print_r( error_get_last ( ), true)
In order to make sure that you can write to the file you can always check if it's writable
if(is_writable($dir))
Also for debugging it's nice to see how much bites was written by file_put_contents
so the final code easy to debug will be like this:
<?php
include "code/multiPage/topBar.html";
$dir = "/data/searches.txt";
if(!is_writable($dir)){
echo "cannot write to file";
}
if (!empty($_REQUEST["search"])) {
$input = $_REQUEST["search"];
$bitecount = file_put_contents($dir, $input, FILE_APPEND);
}
echo $bitecount . " bites were written to the file";
?>
There is a thing. If $_REQUEST["search"]="" or $_REQUEST["search"]=null the if($_REQUEST) will be TRUE anyway

Problems connecting Scilab with php?

I try to use/call Scilab with php. I followed instruction form https://www.ibm.com/developerworks/library/os-php-scilab/os-php-scilab-pdf.pdf but I cant make it work. I tried listing 1 and what I got is Array (). do I need to save sci/sce file in root folder before calling them in php? is Scilab can be called directly as the instruction?
I had exactly the same problem. I use scilab 6.0.1
This code works for me :
<?php
$path = 'C:\\wamp64\\apps\\scilab-6.0.1\\bin\\';
$path_script = 'C:\\wamp64\\www\\tests\\log.sce';
$command = $path.'WScilex-cli.exe -nb -f '.$path_script;
echo $command;
exec($command, $output);
echo '<pre>';
print_r ($output);
echo '</pre>';
?>
In the log.sce file, I saw matrix on firefox web page, save file and picture on my folder.
Best regards

"No such file or directory" on localhost copy

EDIT: I'm pretty sure the issue has to do with the firewall, which I can't access. Marking Canis' answer as correct and I will figure something else out, possibly wget or just manually scraping the files and hoping no major updates are needed.
EDIT: Here's the latest version of the builder and here's the output. The build directory has the proper structure and most of the files, but only their name and extension - no data inside them.
I am coding a php script that searches the local directory for files, then scrapes my localhost (xampp) for the same files to copy into a build folder (the goal is to build php on the localhost and then put it on a server as html).
Unfortunately I am getting the error: Warning: copy(https:\\localhost\intranet\builder.php): failed to open stream: No such file or directory in C:\xampp\htdocs\intranet\builder.php on line 73.
That's one example - every file in the local directory is spitting the same error back. The source addresses are correct (I can get to the file on localhost from the address in the error log) and the local directory is properly constructed - just moving the files into it doesn't work. The full code is here, the most relevant section is:
// output build files
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
copy($source, $dest);
echo "Copy $source to $dest. <br>";
}
}
You are trying to use URLs to travers local filesystem directories. URLs are only for webserver to understand web requests.
You will have more luck if you change this:
copy(https:\\localhost\intranet\builder.php)
to this:
copy(C:\xampp\htdocs\intranet\builder.php)
EDIT
Based on your additional info in the comments I understand that you need to generate static HTML-files for hosting on a static only webserver. This is not an issue of copying files really. It's accessing the HMTL that the script generates when run through a webserver.
You can do this in a few different ways actually. I'm not sure exactly how the generator script works, but it seems like that script is trying to copy the supposed output from loads of PHP-files.
To get the generated content from a PHP-file you can either use the command line php command to execute the script like so c:\some\path>php some_php_file.php > my_html_file.html, or use the power of the webserver to do it for you:
<?php
$hosted = "https://localhost/intranet/"; <--- UPDATED
foreach($paths as $path)
{
echo "<br>";
$path = str_replace($localroot, "", $path);
$path = str_replace("\\","/",$path); <--- ADDED
$source = $hosted . $path;
$dest = $localbuild . $path;
if (is_dir_path($dest))
{
mkdir($dest, 0755, true);
echo "Make folder $source at $dest. <br>";
}
else
{
$content = file_get_contents(urlencode($source));
file_put_contents(str_replace(".php", ".html", $dest), $content);
echo "Copy $source to $dest. <br>";
}
}
In the code above I use file_get_contents() to read the html from the URL you are using https://..., which in this case, unlike with copy(), will call up the webserver, triggering the PHP engine to produce the output.
Then I write the pure HTML to a file in the $dest folder, replacing the .php with .htmlin the filename.
EDIT
Added and revised the code a bit above.

PHP using ruby returning output (using metainspector)

I'm currently creating a php page which does a ssh call with a .rb (ruby) file.
rb file
require 'metainspector'
page = MetaInspector.new("www.hln.be")
puts page.image
When creating a php file with the following code (php):
$cmd = "ruby facescrape.rb";
$last_line = system($cmd, $retval);
echo $last_line . '
echo $retval;
this only returns value 1.
However 2 things :
When running the same command in ssh, it will print the page.image
correctly.
When i change the rb file and for instance set as last line
puts "test"
this value returns correctly with also print correctly with the aboven php code.
I don't get why printing the page.image works in ssh but won't work by using that php code.
Also tried using exec() instead of system().
Thank you in advance!
Kind regards,
Kurt Colemonts

Finding correct of PHP binary - exec()

I'm trying to execute a separate PHP script from within a PHP page. After some research, I found that it is possible using the exec() function.
I also referenced this SO solution to find the path of the php binary. So my full command looks like this:
$file_path = '192.168.1.13:8080/doSomething.php';
$cmd = PHP_BINDIR.'/php '.$file_path; // PHP_BINDIR prints /usr/local/bin
exec($cmd, $op, $er);
echo $er; // prints 127 which turns out to be invalid path/typo
doSomething.php
echo "Hi there!";
I know $file_path is a correct path because if I open its value; i.e. 192.168.1.13:8080/doSomething.php, I do get "Hi there!" printed out. This makes me assume that PHP_BINDIR.'/php' is wrong.
Should I be trying to get the path of the php binary in some other way?
The file you are requesting is accessible via a web server, not as a local PHP script. Thus you can get the result of the script simply by
$output = file_get_contents($file_path);
If you however for some reason really have to exec the file, then you must provide a full path to that file in your server directory structure instead of server URL:
$file_path = '/full/path/to/doSomething.php';
$cmd = PHP_BINDIR.'/php '.$file_path;
exec($cmd, $op, $er);

Categories