Skip to content

Instantly share code, notes, and snippets.

@escopecz
Forked from alanhartless/cron.php
Last active April 18, 2023 22:43
Show Gist options
  • Save escopecz/9a1a0b10861941a457f4 to your computer and use it in GitHub Desktop.
Save escopecz/9a1a0b10861941a457f4 to your computer and use it in GitHub Desktop.
Script to run Mautic (https://mautic.org) commands from a URL.
<?php
if (!isset($_GET['ILoveMauticReallyIDo'])) {
echo 'The secret phrase is wrong.';
die;
}
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$allowedTasks = array(
'cache:clear',
'mautic:leadlists:update',
'mautic:campaigns:update',
'mautic:campaigns:trigger',
'mautic:email:process',
'mautic:fetch:email',
'doctrine:migrations:migrate',
'doctrine:schema:update --dump-sql',
'doctrine:schema:update --force'
);
if (!isset($_GET['task'])) {
echo 'Specify what task to run. You can run these:';
foreach ($allowedTasks as $task) {
$href = $link . '&task=' . urlencode($task);
echo '<br><a href="' . $href . '">' . $href . '</a>';
}
echo '<br><a href="https://www.mautic.org/docs/setup/index.html">Read more</a>';
echo '<br><b style="color:red">Please, backup your database before executing the doctrine commands!</b>';
die;
}
$task = urldecode($_GET['task']);
if (!in_array($task, $allowedTasks)) {
echo 'Task ' . $task . ' is not allowed.';
die;
}
$fullCommand = explode(' ', $task);
$command = $fullCommand[0];
$argsCount = count($fullCommand) - 1;
$args = array('console', $command);
if ($argsCount) {
for ($i = 1; $i <= $argsCount; $i++) {
$args[] = $fullCommand[$i];
}
}
echo '<h3>Executing ' . implode(' ', $args) . '</h3>';
require_once __DIR__.'/app/bootstrap.php.cache';
require_once __DIR__.'/app/AppKernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\BufferedOutput;
defined('IN_MAUTIC_CONSOLE') or define('IN_MAUTIC_CONSOLE', 1);
try {
$input = new ArgvInput($args);
$output = new BufferedOutput();
$kernel = new AppKernel('prod', false);
$app = new Application($kernel);
$app->setAutoExit(false);
$result = $app->run($input, $output);
echo "<pre>\n".$output->fetch().'</pre>';
} catch (\Exception $exception) {
echo $exception->getMessage();
}
@nerddigitalgit
Copy link

This script can be used in shared web hostings which doesn't allow to execute CLI commands to run Mautic cron and maintenance tasks.

How to make it work

  1. Create a PHP file in the root folder of Mautic.
  2. Copy and paste the code of the script above.
  3. Change ILoveMauticReallyIDo in that script to some secret phrase so only you'll be able to run the commands.
  4. Run a command by accessing this URL with your browser http://[yourmautic.com]/commands.php?ILoveMauticReallyIDo. Change[yourmautic.com] to your domain or wherever your Mautic runs and ILoveMauticReallyIDo to your secret phrase form 3.

How to use it

When you execute the URL form 4., you should get the list of available commands with URL addresses. You can try to click on those to run the command manually or insert those URL addresses to a cron UI provided by your web hosting company.

Or use Cronfig.io

I prepared a cron service at https://cronfig.io if you don't want to deal with a PHP script. Plus it will give you logs in your Mautic administration and monitoring.

I created the file in the root of my mautic installation, updated the secret, and visited the page but I'm seeing a 403 error.
I gave the file full permissions (777) and that did not work.

Do you think this is a limitation of my hosting?

My issue is that I'm not able to update Mautic using the built in upgrade notification. I think my mautic is in between versions at the moment 2.6 and 3.2.

@nerddigitalgit
Copy link

I am receiving
Forbidden

You don't have permission to access this resource.
and it is working in 2.16.5

hey did you find a solution? are you still seeing the "permission denied" message?

I'm still stuck

@dapsdiary
Copy link

dapsdiary commented Jul 23, 2021

Yes .htaccess file needs to include command.php as allowed Sent from my iPhone. Please excuse any typos Best regards,

El 30 jun 2021, a las 13:48, nerddigital @.> escribió: @. commented on this gist. I am receiving Forbidden You don't have permission to access this resource. and it is working in 2.16.5 hey did you find a solution? are you still seeing the "permission denied" message? I'm still stuck — You are receiving this because you commented. Reply to this email directly, view it on GitHub, or unsubscribe.

Please do you mean one needs to copy the content in commands.php into .htaccess file?

@josefcoribeiro
Copy link

No.. edit the file and you have one row with the php files allowed to be executed... you have to add commands. That is all
Please I did not realize that my phone was in the signature so I will appreciate if you edit your message and delete it
Thank you

@dapsdiary
Copy link

No.. edit the file and you have one row with the php files allowed to be executed... you have to add commands. That is all
Please I did not realize that my phone was in the signature so I will appreciate if you edit your message and delete it
Thank you

Thank you for your prompt response. I have also edited my last comment.
I am a rookie please, just one last clarification, please, I'll be grateful.

Do you commands like this as found in the script:
'cache:clear',
'cache:clear --no-warmup',
'mautic:install:data',
'mautic:campaigns:trigger',
'mautic:campaigns:rebuild',
'mautic:social:monitoring',
'mautic:iplookup:download',
'mautic:emails:send',
'mautic:email:fetch',
'mautic:messages:send',
'mautic:integration:fetchleads -i Salesforce',
'mautic:integration:fetchleads --integration=Hubspot',
'mautic:segments:update',
'doctrine:migration:status',
'doctrine:migrations:migrate',
'doctrine:schema:update --dump-sql',
'doctrine:schema:update --force',
'mautic:maintenance:cleanup --days-old=365 --dry-run'
);
or
https://myurl.com/commands.php?secretphase

Kindly assist, I'll be grateful.

@josefcoribeiro
Copy link

josefcoribeiro commented Jul 23, 2021

Just "commands" added in .htaccess after upgrade_V3

Except those whitelisted bellow.

<FilesMatch "^(index|index_dev|filemanager|upgrade|upgrade_v3|commands)\.php$">
    Require all granted
</FilesMatch>

@dapsdiary
Copy link

Just "commands" added in .htaccess after upgrade_V3

Except those whitelisted bellow.

<FilesMatch "^(index|index_dev|filemanager|upgrade|upgrade_v3|commands)\.php$">
    Require all granted
</FilesMatch>

I got it. Thank you.

@Masho123
Copy link

Hello Brother and sisters

i am creating commands.php file but i access it thorogh my browser i receive this error
Untitled

Please help me to resolve this issue i have installed latest version of mautic and facing this error

Uh oh! I think I broke it. If I do it again, please report me to the system administrator!
500 Internal Server Error

mautic not give access to login to my administrator account and i receive this error

Let me know what to do to resolve this prompt

Best Regards
Muhammad Ashfaq

@dsp76
Copy link

dsp76 commented Apr 18, 2023

Hey Muhammad, please read the post above yours - but maybe you solved it meanwhile ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment