PHP 5.3.3 bug or preg_match bad validate? - preg-match

<?php
$query = 'q=Riha47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a4744444444444444444444444444444444444444444444444na&start-index=60&max-results=20';
$pattern = "/^(?:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*'()\[\];\/?:#&=+$,])*$/";
if (preg_match($pattern, $query))
echo 'true';
else
echo 'false';
The script has nothing as the output... It offers to download php file 0 bytes..
I need to fix it because it's probably still a bug in Zend_Gdata! Sorry for bad english.
EDIT:
You have a 5.3.3 version of php?
The problem disappears if you add a "U" at the end of the expression.
What the f*ck?

$query = 'q=Riha47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a47444444444444444444444444444444444444444444444444444444444444a4744444444444444444444444444444444444444444444444na&start-index=60&max-results=20';
if (preg_match('#^(?:%[[:xdigit:]]{2}|[A-Za-z0-9-_.!~*\'()[\];/?:#&=+$,])*$#i', $query)) {
echo "TRUE"; // THIS CASE
} else {
echo "FALSE"
}

Related

How to use output of Laravel-(artisan)-command in bash-script?

I am trying to write a small startup-script for one of my docker-containers. The problem is that the bash-script has to wait until a artisan-command echoes "1".
The artisan-commands handle-function looks like this:
$condition = something-to-check;
if($condition){ echo 1; } else { echo 0; }
What I had in mind was something like like this:
#!/bin/bash
while php artisan myapp:mycommand == "0"
do
sleep 1m
done
do-something-else
How can I achieve this?
EDIT:
For anyone comming here via Google - James Taylor's answer pointed me in the right direction, which is why I accepted it and edit my solution in the question.
The approach was to edit the handle-function like this:
/**
* Execute the console command.
*
* #return int
*/
public function handle()
{
$mycondition = true; //or whatever
try{
if($mycondition == false){
echo "Some fancy status message based on the condition is false \n";
exit(1);
} else {
echo "Some fancy status message based on the condition is true \n";
exit (0);
}
} catch (\Exception){
echo "Some fancy status message based on the condition with an exception\n";
exit(1);
}
}
And set up the bash-script like this:
#!/bin/bash
until php /path/to/artisan myapp:mycommand
do
echo "Condition is false!"
sleep 1m #or whatever you wanna do
done
echo "Condition is true!"
do-something
Update your my app:mycommand to return an exit code instead of echo.
exit(1);
Reference the PHP exit function: https://www.php.net/manual/en/function.exit.php

GET path not accepting 'dot' in it

I have router path with a '.' in it, my path is downloads/pass.pkpass
I have defined the path in web.php as the following
$router->get('downloads/pass.pkpass', 'PassServerController#downloadPass');
but somehow it's not working. if I remove the '.' it's working fine. what might be the problem here?
It's was working before recently I updated lumen after that it's not working.
The . is the string concatenation operator in PHP.
example
<?php
$string1 = "Test";
$string2 = "working!";
$string = $string1 . $string2;
echo $string;
?>
this will print "Test working!"
You can get your route by this may be
$router->get('downloads/pass.'.'pkpass', 'PassServerController#downloadPass');
But this is some kind of hack we can say.
You can use _ instead of . parameter that will work for sure.

Can't locate WWW/Mechanize.pm: Using cpanm doesn't work

I know this is a duplicate, but my question was not answered in any other threads. the output of sudo cpanm WWW::Mechanize is to long to put in tread. pastebin: 3BYUtSss
I tried executing a perl script, and I get this error:
Can't locate WWW/Mechanize.pm in #INC (#INC contains: /opt/local/lib/perl5/site_perl/5.16.3/darwin-thread-multi-2level /opt/local/lib/perl5/site_perl/5.16.3 /opt/local/lib/perl5/vendor_perl/5.16.3/darwin-thread-multi-2level /opt/local/lib/perl5/vendor_perl/5.16.3 /opt/local/lib/perl5/5.16.3/darwin-thread-multi-2level /opt/local/lib/perl5/5.16.3 /opt/local/lib/perl5/site_perl /opt/local/lib/perl5/vendor_perl .) at io.pl line 5.
In case you need it, here is my perl script's contents:
#!/usr/bin/env perl
use warnings;
use strict;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
my ($get,$host,$title);
while (<>) {
if (m|^GET (\S+) |) {
$get = $1;
} elsif ( m|^Host: (\S+)\.| ) {
$host = $1;
} else {
# Unrecognized line...reset
$get = $host = $title = '';
}
if ($get and $host) {
my ($title) = $get =~ m|^.*\/(.+?)$|; # default title
my $url = 'http://' . $host . $get;
$mech->get($url);
if ($mech->success) {
# HTML may have title, images will not
$title = $mech->title() || $title;
}
print "Title: $title\n";
print "URL: $url\n";
print "\n";
$get = $host = $title = '';
}
}
These look to be the key lines in the output from cpanm, down at the bottom.
! Installing the dependencies failed: Installed version (3.59) of CGI is not in range '4.08'
! Bailing out the installation for WWW-Mechanize-1.75.
Looks like you need to install a higher version of the CGI distribution.
The key lines in the cpanm output are:
Building and testing CGI-4.21 ... FAIL
! Installing CGI failed. See /Users/skylerspaeth/.cpanm/work/1440436409.90704/build.log for details. Retry with --force to force install it.
So look in /Users/skylerspaeth/.cpanm/work/1440436409.90704/build.log and see what the problem is. If that log is no longer there, you may need to run cpanm again, which will generate another build.log.
You find the key lines in cpanm output by searching for "fail". Usually, it'll point you at a build.log file for further details.

How can I check if stdin exists in PHP ( php-cgi )?

Setup and Background
I am working on script that needs to run as /usr/bin/php-cgi instead /usr/local/bin/php and I'm having trouble checking for stdin
If I use /usr/local/bin/php as the interpreter I can do something like
if defined('STDIN'){ ... }
This doesn't seem to work with php-cgi - Looks to always be undefined. I checked the man page for php-cgi but didn't find it very helpful. Also, if I understand it correctly, the STDIN constant is a file handle for php://stdin. I read somewhere that constant is not supposed to be available in php-cgi
Requirements
The shebang needs to be #!/usr/bin/php-cgi -q
The script will sometimes be passed arguments
The script will sometimes receive input via STDIN
Current Script
#!/usr/bin/php-cgi -q
<?php
$stdin = '';
$fh = fopen('php://stdin', 'r');
if($fh)
{
while ($line = fgets( $fh )) {
$stdin .= $line;
}
fclose($fh);
}
echo $stdin;
Problematic Behavior
This works OK:
$ echo hello | ./myscript.php
hello
This just hangs:
./myscript.php
These things don't work for me:
Checking defined('STDIN') // always returns false
Looking to see if CONTENT_LENGTH is defined
Checking variables and constants
I have added this to the script and run it both ways:
print_r(get_defined_constants());
print_r($GLOBALS);
print_r($_COOKIE);
print_r($_ENV);
print_r($_FILES);
print_r($_GET);
print_r($_POST);
print_r($_REQUEST);
print_r($_SERVER);
echo shell_exec('printenv');
I then diff'ed the output and it is the same.
I don't know any other way to check for / get stdin via php-cgi without locking up the script if it does not exist.
/usr/bin/php-cgi -v yields: PHP 5.4.17 (cgi-fcgi)
You can use the select function such as:
$stdin = '';
$fh = fopen('php://stdin', 'r');
$read = array($fh);
$write = NULL;
$except = NULL;
if ( stream_select( $read, $write, $except, 0 ) === 1 ) {
while ($line = fgets( $fh )) {
$stdin .= $line;
}
}
fclose($fh);
Regarding your specific problem of hanging when there is no input: php stream reads are blocking operations by default. You can change that behavior with stream_set_blocking(). Like so:
$fh = fopen('php://stdin', 'r');
stream_set_blocking($fh, false);
$stdin = fgets($fh);
echo "stdin: '$stdin'"; // immediately returns "stdin: ''"
Note that this solution does not work with that magic file handle STDIN.
stream_get_meta_data helped me :)
And as mentioned in the previous answer by Seth Battin stream_set_blocking($fh, false); works very well 👍
The next code reads data from the command line if provided and skips when it's not.
For example:
echo "x" | php render.php
and php render.php
In the first case, I provide some data from another stream (I really need to see the changed files from git, something like git status | php render.php.
Here is an example of my solution which works:
$input = [];
$fp = fopen('php://stdin', 'r+');
$info = stream_get_meta_data($fp);
if (!$info['seekable'] && $fp) {
while (false !== ($line = fgets($fp))) {
$input[] = trim($line);
}
fclose($fp);
}
The problem is that you create a endless loop with the while($line = fgets($fh)) part in your code.
$stdin = '';
$fh = fopen('php://stdin','r');
if($fh) {
// read *one* line from stdin upto "\r\n"
$stdin = fgets($fh);
fclose($fh);
}
echo $stdin;
The above would work if you're passing arguments like echo foo=bar | ./myscript.php and will read a single line when you call it like ./myscript.php
If you like to read more lines and keep your original code you can send a quit signal CTRL + D
To get parameters passed like ./myscript.php foo=bar you could check the contents of the $argv variable, in which the first argument always is the name of the executing script:
./myscript.php foo=bar
// File: myscript.php
$stdin = '';
for($i = 1; $i < count($argv); i++) {
$stdin .= $argv[$i];
}
I'm not sure that this solves anything but perhaps it give you some ideas.

write csv file to server with codeigniter csv_from_result

Trying to save a csv file on my server with codeigniter.
my syntax is:
$this->load->dbutil();
$query = $this->db->query("SELECT id,customeraccount,band FROM Customer_Bands");
$delimiter = ',';
$newline = "<br>";
echo $this->dbutil->csv_from_result($query, $delimiter, $newline);
if ( ! write_file(base_url().'/application/authorizations/test.csv', $this->dbutil->csv_from_result($query, $delimiter, $newline)))
{
echo 'Unable to write the file';
}
else
{
echo 'File written!';
}
This is rough right now but want to get functionality working first.
Two issues.
Firstly the echo $this->dbutil->csv_from_result($query, $delimiter, $newline); outputs to my view correctly but each field is inside "", how can I remove these?
Secondly, the file is not writing to my server.
base_url() is my codeigniter root folder then inside applications I have created a folder named authorizations so base_url()/application/authorizations
This currently fails and echos 'Unable to write the file'
Thanks in advance

Resources