Do you know those features some people have been requesting for a long time, and when you finally sit down to build them, it takes no more than 30 minutes to code the first version? This latest one definitely falls in that category.

Getting Groovy with the CLI

See, not everyone wants to use a nice GUI to manage their site. Some people are hardcore, and would rather use the commandline. Rawr. Truth be told, I'm not one of them, but here's a goodie for the people that do.

modCli is a command line php script enabling you to run any of the MODX processors from a (ssh) terminal, in just shy of 180 lines of code (including comments). After you dump the file in your MODX_BASE_URL, you can start running processors from the command line. It's kind of like Drush for Drupal, but then for MODX.

Examples

While you can find exact usage instructions and some other examples in the code, here's two quick examples as to what you can do with modCli.

php modcli.php resource/create pagetitle=Awesome! parent=6 content=Awesomer!
# Running modCLI 0.1.0-pl in modcli.php
# Processor: resource/create
# Properties:
# pagetitle => Awesome!
# parent => 6
# content => Awesomer!

> Object retrieved:
> id => 20
php modcli.php workspace/packages/getlist limit=1 start=2
# Running modCLI 0.1.0-pl in modcli.php
# Processor: workspace/packages/getlist
# Properties:
# limit => 1
# start => 2

> Amount of Results: 8
> Result 1:
> signature => login-1.8.1-pl
> created => Sep 07, 2012 12:59 AM
> updated => Sep 07, 2012 01:01 AM
> installed => Sep 07, 2012 01:01 AM
> state => 0
> workspace => 1
> provider => 0
> disabled =>
> source => login-1.8.1-pl.transport.zip
> manifest =>
> package_name => login
> version_major => 1
> version_minor => 8
> version_patch => 1
> release => pl
> release_index => 0
> provider_name =>
> name => login
> version => 1.8.1
> iconaction => icon-uninstall
> textaction => Uninstall
> readme =>
> --------------------<br />
> Snippet: Login<br />
> --------------------<br />
> Version: 1.7<br />
> Sin...
> updateable =>

Security Concerns

As to my knowledge you can't have sessions in a Terminal, this script basically bypasses the login process for MODX. Yes, you read that right, it just loads MODX in API mode, in the mgr context, which in my tests gives it full access to everything.

While it's a nifty tool which in some cases can be very convenient (I still prefer shiny buttons), do NOT EVER EVER EVER leave this available in the root of your site. Even better, don't put it there in the first place, but adjust line 44 of the script to point to your MODX install from, say, a non-web accessible SSH-only directory.

Disclaimer: use at your own risk and don't come whining if the script worked as intended for someone else, too.

Get the Code

I want to make sure you read the security concerns above before I even tell you where to get the code from, or how it exactly works. Only tick the box below if you did!

Happy New Year!

As the last post of 2012, it's my statutory duty to wish you a happy new year today. My hope for you is to make the next year at least twice as ambitious, successful and awesome as 2012!

Of course there's no way to say goodbye to 2012 as with an old fashioned list of what went on in 2012. So without further ado, here's the top 5 most visited blog posts in 2012:

Hi! Why not to disable executing of this script from web using such code (or php_sapi_name function, or PHP_SAPI constant), e.g.:
if (realpath($_SERVER['argv'][0]) != __FILE__)
{
die('This is CLI script! You can not run it from the web!');
}
I think it will be much safer for forgetful users :)
P.S.: Happy New Year!

Comments are closed :(

While I would prefer to keep comments open indefinitely, the amount of spam that old articles attract is becoming a strain to keep up with and I can't always answer questions about ancient blog postings. If you have valuable feedback or important questions, please feel free to get in touch.