Skip to content

Instantly share code, notes, and snippets.

@ChgoChad
Forked from escopecz/commands.php
Last active January 5, 2024 22:18
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save ChgoChad/fe9950c628ad8169cd27a58ee64106e8 to your computer and use it in GitHub Desktop.
Save ChgoChad/fe9950c628ad8169cd27a58ee64106e8 to your computer and use it in GitHub Desktop.
Script to run Mautic (https://mautic.org) commands from a URL.
<?php
if (!isset($_GET['ILoveMautic'])) {
echo 'The secret phrase is wrong.';
die;
}
$link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$allowedTasks = array(
'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'
);
if (!isset($_GET['task'])) {
echo '<html><head></head><body style="font-family: Open Sans, Helvetica, Arial, sans-serif;">';
echo '<p>Specify what task to run. You can run these:';
echo '<ul>';
foreach ($allowedTasks as $task) {
$href = $link . '&task=' . urlencode($task);
echo '<li><a href="' . $href . '">' . $task . '</a></li>';
}
echo '</ul><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></p>';
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 '<html><head></head><body style="font-family: Open Sans, Helvetica, Arial, sans-serif;">';
echo '<h3>Executing ' . implode(' ', $args) . '</h3>';
require_once __DIR__.'/app/autoload.php';
// require_once __DIR__.'/app/bootstrap.php.cache';
require_once __DIR__.'/app/AppKernel.php';
require __DIR__.'/vendor/autoload.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();
}
@khaoua
Copy link

khaoua commented Feb 20, 2017

Hi Guys

I'm pretty new to Mautic, and have no coding skills (yet lol) I'm an SAP expert, but starting with those digital tools like this marketing automation tool that looks pretty efficient, so let me get time to get use with it :-)
Can you guys let me know how I can use this latest update concretely? I follow the steps with url as I have put the php file in our servers in mautic root directory, but when I insert in chrome navigator xxx.com/commands.php?secretphrase, nothing happens at all, I must have missed something as nothing occurs and only website is poping up
I've downloaded the latest zip file to proceed, but I'm a bit lost, so if you guys can show me the exact steps of what I should exactly do that would help
I could download the plugin available in this procedure here below
https://cronfig.io/

But we have only a max of 3 Crons runing in batch job, so if I could make a run of more to get more efficient, this script seems to work from the latest comments so if you can let me know how to proceed as pure beginner that would be great, many thanks in advance for your help

@khaoua
Copy link

khaoua commented Feb 22, 2017

Hi Guys

I tested it on my shared host, I put it in the root file of my website, called the script in URL and nothing happens, can you guys let me know what's the issue or if I missed something?

I've put it this way in the url: http://xxx.com/cronjobfile.php

it displays the wordpress initial website page, nothing else

any thought?

@khaoua
Copy link

khaoua commented Feb 22, 2017

ok, sorry, forgot the secret phrase, and renamed correctly the file, so I did get the web page with all the links, when I click in one link, for example, leads update I get: "Executing console mautic:leadlists:update"
any info of where should we see the evolution or logs of those script to know that they went well?

@khaoua
Copy link

khaoua commented Feb 22, 2017

ok, I've just opened a free account to test them on easycron, segment, campaign are failing, those are the only ones I've tested, but there is an issue, so, if you guys have some tips, feel free to share

@khaoua
Copy link

khaoua commented Feb 23, 2017

Hello
I found the mistake, it works now on easycron, jobs succeed

when in a post above it said: "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"

Actually, there is a clarification to be done: The links for crons shown after they are generated from initial link with secret phrase, they can be copied, but we should remove the secret phrase that is still in the url link, then, it works, at least it worked this way for me

@ChgoChad
Copy link
Author

@khaoua - I added instructions above (copied from the original post and modified to include the changes I made) ... Let me know if you have any more issues.

@Reelicon
Copy link

Reelicon commented Mar 9, 2017

@ChgoChad are we supposed to click mautic:install:data to upgrade from version 2.6 to 2.7? Please note I don't have much data in 2.6 cause I set it up just a few days ago. Do I need to click on links to update campaigns, segments and all?

@Reelicon
Copy link

It's ok I found the answer...If upgrading from 2.6.0 or 2.6.1:
Since this does not include any schema changes, the update process built into Mautic's front-end will suffice in updating. Simply log out, back in, and you should see the upgrade notification

@khaoua
Copy link

khaoua commented Jun 9, 2017

@ChgoChad I will try your new version, hope it works with 2.8.2 version as well ;-)

so far, I was using this one below, but now on cron-jobs.org, it seems that it is not that stable, and generate strange behaviours of mautic, it only work when commands are made manually. Cache clear seems to to be wrong now on that script:

'; echo '

Specify what task to run. You can run these:'; echo '

    '; foreach ($allowedTasks as $task) { $href = $link . '&task=' . urlencode($task); echo '
  • ' . $task . '
  • '; } echo '

Read more'; echo '
Please, backup your database before executing the doctrine commands!

'; 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 ''; echo '

Executing ' . implode(' ', $args) . '

'; require_once __DIR__.'/app/autoload.php'; // require_once __DIR__.'/app/bootstrap.php.cache'; require_once __DIR__.'/app/AppKernel.php'; require __DIR__.'/vendor/autoload.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 "
\n".$output->fetch().'
'; } catch (\Exception $exception) { echo $exception->getMessage(); }

@mp-dasglobtech
Copy link

Thanks, It is helpful.

@raymondkent
Copy link

Hi,

I get some errors when running these

"Executing console mautic:campaigns:rebuild

Fatal error: Maximum execution time of 30 seconds exceeded in /home3/[accountname]/[mauticurl].com/m/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php on line 104

Also cahe:clear similar errror,

Executing console cache:clear

Fatal error: Maximum execution time of 30 seconds exceeded in /home3/[accountname]/[mauticurl].com/m/vendor/symfony/translation/MessageCatalogue.php on line 136

but "cache:clear -- no warmup" works

Any ideas?

The following also seem to run without generating any errors.
campaigns:trigger
segments:update

@lipnotic
Copy link

lipnotic commented Dec 1, 2018

Hi guys! I've just installed and tested here.... Working fine!
Installed in a shared Cpanel hosting.
Some tips:

  1. When you execute the script and it gives the commands list, after clicking on any command, you´ll get a urlencoded formatted link.
  2. Remember to change the line 3 ILoveMautic GET password!

@ChgoChad, do You know how to execute webhooks? (mautic:webhooks:process)
I've tryed some changes on your script to execute it but it returns on cronjob email log:

Webhook Bundle is in immediate process mode. To use the command function change to command mode.

@vsaifee
Copy link

vsaifee commented Feb 11, 2019

Hello guys,

I am new to Mautic. Can anyone help me please on how to use this code? I want to run Mautic on https. Please help.

@virgilwashere
Copy link

virgilwashere commented Sep 10, 2019

mautibot

mautic-cron-commands

I have an updated version of the script (https://github.com/virgilwashere/mautic-cron-commands) that supports Mautic 2.15.x

changes

  • 🎨 &pretty output mode
  • 💄 add screenshots of &pretty output
  • 📝 document parameters
  • ✨ add email and sms channels to mautic:campaigns:messages
  • 👌 add --no-interaction to commands that need it
  • ✨ add commands from some popular plugins
  • ✏️ fix mautic:emais:fetch
  • 💥 removed duplicate commands
  • 🚸 moved the dangerous commands to the end of the list
  • 🛠️ HTML5 standards compliance
  • 📝 update documentation references

@fredroo
Copy link

fredroo commented Jul 31, 2020

@ChgoChad not work in last version of mautic

page not found

@pagelab
Copy link

pagelab commented Jan 7, 2021

page not found

@fredroo This is a .htaccess issue. You should add your commands.php file to the whitelist in the .htaccess file, line 113, like that:

Before:

    # Except those whitelisted bellow.
    <FilesMatch "^(index|index_dev|filemanager|upgrade)\.php$">
        Require all granted
    </FilesMatch>

After:

    # Except those whitelisted bellow.
    <FilesMatch "^(index|index_dev|filemanager|upgrade|commands)\.php$">
        Require all granted
    </FilesMatch>

@saidhasyim
Copy link

Hello, is this still working in 4.4.x? I followed the steps and it shows me the command list. When I select clearing cache, it just stops there.

The browser just shows this.
"Executing console cache:clear"

@Willianarkeit
Copy link

Hello, is this still working in 4.4.x? I followed the steps and it shows me the command list. When I select clearing cache, it just stops there.

The browser just shows this. "Executing console cache:clear"

Yes working in 4.4.5

@cfloinc
Copy link

cfloinc commented May 7, 2023

Hello, is this still working in 4.4.x? I followed the steps and it shows me the command list. When I select clearing cache, it just stops there.

The browser just shows this. "Executing console cache:clear"

I have the exact same issue. I have updated ownership, privileges, and htaccess for commands.php. i cannot execute commands like saidhasyim, no action

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