PHPMD Catch/Suppress fatal errors - php

I'm trying to automate my testing. As a smoke test I would like to check my PHP code with PHPMD before continuing with the actual Unit tests. Sounds sensible enough right?
Thing is that PHPMD seems to crash when fatal errors arise in my PHP files. For a test I added an extra accolade at a function definition like so:
function foo() {{
// Stuff
}
Were I expected a 1 exit code, PHPMD seems to crash completely and instead returns a 0 exit code. Rendering my automated script useless. Is there a way to suppress these errors and return the expected exit code? For PHPUnit the --process-isolation option solved this problem but I can't seem to find such option for PHPMD.
Relevant automated testing code
#!/usr/bin/php
<?php
exec('meta/phpmd', $output, $returnCode);
if ($returnCode == 1) {
echo '[Fail] PHP code is breaking', PHP_EOL;
exit(1);
} elseif ($returnCode == 2) {
echo '[Warn] PHP code is unclean', PHP_EOL;
} else {
echo '[OK] Code is clean! ', PHP_EOL;
}

As a workaround (and possible solution) one could check the syntax before passing it to PHPMD. I changed my testing code to this:
#!/usr/bin/php
<?php
$dir_root = dirname(dirname(__DIR__));
$dir_php = $dir_root . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'App';
exec('find ' . $dir_php . ' -iname *.php | xargs -n1 php -l 2>/dev/null', $output, $returnCode);
if ($returnCode != 0) {
echo '[Fail] PHP contains syntax errors', PHP_EOL,
implode(PHP_EOL, $output), PHP_EOL;
exit($returnCode);
}
exec('meta/phpmd', $output, $returnCode);
if ($returnCode == 1) {
echo '[Fail] PHP code is breaking', PHP_EOL;
exit(1);
} elseif ($returnCode == 2) {
echo '[Warn] PHP code is unclean', PHP_EOL;
}
Credits to Winglian at Reddit for the mass php -l code
https://www.reddit.com/r/PHP/comments/2t7mvc/lint_an_entire_directory_of_php_files_in_parallel/

Related

User not allowed to use crontab

I want to edit cron tab daily to delete / add some jobs .. so I added cron job ( helper job ) to run php script to handle these edits.
When I run this script by browser works fine .. but when it runs using the helper job not running and I receive notification mail from cpanel with this error :
Please note I am on shared hosting plan with C-Panel so I have no root access .. but the code working fine when run from browser.
Error:
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
You (dcfn35c8st1b) are not allowed to use this program (crontab)
See crontab(1) for more information
PHP Script:
exec('crontab -l', $crontab);
$record_found = false;
foreach($crontab as $key => $value){
if(strpos($value, 'record_extra.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
$record_found = true;
}
if(strpos($value, 'record.php') !== false){
//echo $key . ' ' . $value . '<br>';
unset($crontab[$key]);
}
}
if($record_found){
file_put_contents('/tmp/crontab.txt', arrayToString($crontab) . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
} else {
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $firstJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output . $secondJob.PHP_EOL);
if(exec('crontab /tmp/crontab.txt')){
//echo 'success <br>';
} else {
//echo 'error <br>';
}
}
Need your help.
Thanks in advance.
Seems like the user isn't allowed to run crontab. Look into the files "/etc/cron.allow" and "/etc/cron.deny". If cron.allow exists, then only users in that list can use crontab. Users in cron.deny, can't use. If neither exist then only root user can use crontab.
I've looked at numerous "can't run crontab" posts and none of them addressed my problem. I want to report my unique solution.
My service provider copied my system to another place, and apparently they literally used the "cp" command to do it, which meant that no setuid bits were preserved. /usr/bin/crontab was one of several files for which I had to restore the setuid bit.
You can't run crontab if it doesn't have the setuid bit set.

Installing with composer via PHP without using exec()

I developed a function using exec() but my hosting company do not allow for exec() functions to be run. How would I get the same result without using exec()?
I need something with the same approach or a library similar to composer which can allow me to install, update, and remove.
Here is what I have at the moment:
public static function install($library = null)
{
if (self::checkExecute() === true) { // check if exec is authorize
if (is_null($library)) {
$result = false;
} else {
$cmd = 'cd ' . self::$root . ' && composer require ' . $library . ' 2>&1';
exec($cmd, $output, $return); // update dependencies
$result = $output[2];
}
return $result;
}
}
https://www.php.net/manual/en/language.operators.execution.php
You could try to see if all shell execution functions are blocked. I would assume so but I've been in your shoes once upon a time.
print `composer install`;

Why is PHP class not found in a script when it works fine in another?

I have two scripts. Both scripts are located in the same folder on the same server.
Script 1:
<?php
$text = "Is this not working? Why, yes it is.";
$locale = 'en_US';
$i = IntlBreakIterator::createSentenceInstance($locale);
$i->setText($text);
foreach($i->getPartsIterator() as $sentence) {
echo $sentence . PHP_EOL . '----- next -----' . PHP_EOL;
}
?>
Script 2:
... bunch of other code ...
$doc = file_get_contents("file.txt");
$locale = 'en_US';
$i = IntlBreakIterator::createSentenceInstance($locale);
$i->setText($doc);
foreach($i->getPartsIterator() as $sentence) {
echo $sentence . PHP_EOL . '----- next -----' . PHP_EOL;
}
... bunch of other code ...
Script 1 executes as expected, with the desired output and no errors. Script 2 does not execute and PHP keeps throwing the error PHP Fatal error: Uncaught Error: Class 'IntlBreakIterator' not found
What is going on here?

Connection between php and scilab (FuzzyToolbox)

I want to do connection between php and scilab, this my code
try {
$path = 'C:\\wamp64\\apps\\scilab-5.5.2\\bin\\Scilex.exe';
$path_script = "ea=loadfls('C:\\wamp64\\www\\scilab\\estilosaprendizaje.fls');res=evalfls([-11,11],ea); disp(res);exit;";
$command = $path . ' -nb -e "' . $path_script.'"';
echo $command;
exec($command, $output);
foreach ($output as $line) {
print_r($line);
echo "<br />";
}
} catch (Exception $e) {
echo 'Excepción capturada: ', $e->getMessage();
}
but when I run the php, it does not work, it keeps loading, I do not have error messages or anything.
In scilab my code it works.
My output in scilab
Try adding -nw to $command to lauch scilab in console mode. It may be because scilab try to launch itself in graphical mode. I dont have php nor fuzzy toolbox, so I could not test it.

Working Python script throw out error when PHP calls it with exec

I have a simple php code:
<?php echo exec('/opt/anaconda2/bin/python test2.py 2>&1'); ?><br>
And test2.py simply import a library called theano
import theano
It works under ssh but throw out:
KeyError: 'PATH'
when looking at the php in browser.
What's happeneing here? Is there any way that I can see full trace of error msg?
You can try this for the PHP side of things, but I think KeyError is a Python thing:
<?php
$output = array();
exec('/opt/anaconda2/bin/python test2.py 2>&1', $output, $returnCode);
echo 'Output is: ' . PHP_EOL;
var_dump($output);
echo 'Return code is: ' . PHP_EOL;
var_dump($returnCode);
?>

Categories