Installing with composer via PHP without using exec() - php

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`;

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.

composer do not make good action

I try to install via a web admin a module. The action call a function allowing via composer to install a library.
My problem is composer create everytime an htaccess inside my shop directory with an access denied.
Conclusion, it's impossible the display the website
I do not need this htaccess.
How to resolve this problem that.
/shop/.htaccess ===> do not need this files (create automatically)
/shop/composer.lock
/shop/composer.json
public function __construct()
{
static::$root = CORE::getConfig('dir_root', 'Shop');
static::$composerJson = static::$root . 'composer.json';
//define composer environment
putenv('COMPOSER_HOME=' . self::$root);
putenv('COMPOSER_CACHE_DIR=' . CORE::BASE_DIRECTORY . '/Work/Cache/Composer/');
}
public static function install($library = null)
{
if (self::checkExecute() == true) {
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;
}
}
Thank you.

PHPMD Catch/Suppress fatal errors

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/

command line combine pdfs through php exec

I just setup wkhtmltopdf to take html docs and turn them into pdfs but now I need a way to combine the multiple pdfs.
Any suggestions?
We do have pdflib license if there is something it can do but seems its mainly an api for custom building pdfs.
If you got wkhtmltopdf I suppose you're using Linux and you got a root shell access.
You could use Pdftk Toolkit via system() or exec().
First install it. It's a package in Debian/Ubuntu, for example:
sudo apt-get install pdftk
Then make sure that's visible in your bin search PATH.
And a simple PHP snippet:
// In / Out filenames
$input_files = array( 'file_one.pdf', 'file_two.pdf' );
$file_out = 'merged.pdf';
// Escape names and create argument
$line_parts = '';
foreach( $input_files as $file ) {
$line_parts .= escapeshellarg( $file ) . ' ';
}
// Run pdftk
$cmd = 'pdftk ' . $line_parts . ' cat output ' . escapeshellarg( $file_out );
system( $cmd, $retval );
if( $retval ) { print 'There was an error!'; }

Is it possible to remove a Password from a PDF file using PHP?

I would like to ask if it's possible to use PHP in removing a password from a password-protected PDF file in which I already know the password? I've seen this page which provides many options but using bash script. :( I was required to use PHP as much as possible. Any suggestions appreciated!
Of course it's possible, all you need to do is reverse engineer the encryption and compression and implement the reverse operations in PHP - but why bother:
<?php
`gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=unencrypted.pdf -c .setpdfwrite -f encrypted.pdf`;
?>
C.
I use qpdf on linux to remove pdf password. To install run:
sudo apt install qpdf
Here is the function which invokes qpdf from php:
function removePdfPassword($inFilePath, $password, $outFilePath)
{
if (empty($inFilePath) || empty($password) || !file_exists($inFilePath)) {
return false;
}
$cmd = 'qpdf -password=' . escapeshellarg($password) . ' -decrypt ' . escapeshellarg($inFilePath) . ' ' . escapeshellarg($outFilePath);
exec($cmd, $output, $retcode);
$success = $retcode == 0;
if (!$success) {
#unlink($outFilePath);
}
return $success;
}

Categories