Solution: Smartpix - how to read variables from config.php fast

Solution: Smartpix - how to read variables from config.php fast

by Tor Michal Kinn -
Number of replies: 3
The smartpix functionality that is available in the Experimental section of the Administrator block is a nice feature. The script, /pix/smartpix.php, is not loading the Moodle framework, as this would slow down the search for the image files.

Anyway, the scripts need some of the variables in the config.php file, but since the config.php file is the one that loads the framework (using the include_once function), smartpix is parsing the config.php file line by line and preg_match'ing it.

That didn't work for us, since we have an if-statement in our config.php file, giving different values for the variables the script is looking for. So, we needed a way to execute the config.php script to get the correct values, and at the same time avoid the framework to load.

Solution
Comment out the include_once in the config.php, and execute the config.php using eval().

The code

Find the line in the code right after it has opened the config.php file.
(line 96 in the previous version of the smartpix.php file)

Insert this code:
// Instead of parsing config.php, open it, deactive the require_once for the lib setup and eval.
// Then, assign the config variables to the local variables.
$config = str_replace('require_once', '//require_once', $config);
eval ( '?>' . $config . '<?php ' );
$dataroot = $CFG->dataroot;
$dirroot = $CFG->dirroot;

AND REMOVE OR COMMENT OUT THIS CODE (as I've already done here)

//$configlines=preg_split('/[\r\n]+/',$config);
//foreach($configlines as $configline) {
// if(preg_match('/^\s?\$CFG->dirroot\s*=\s*[\'"](.*?)[\'"]\s*;/',$configline,$matches)) {
// $dirroot=$matches[1];
// }
// if(preg_match('/^\s?\$CFG->dataroot\s*=\s*[\'"](.*?)[\'"]\s*;/',$configline,$matches)) {
// $dataroot=$matches[1];
// }
// if(isset($dirroot) && isset($dataroot)) {
// break;
// }
//}

//if(!(isset($dirroot) && isset($dataroot))) {
// error('No line in config.php like $CFG->dirroot=\'/somewhere/whatever\';');
//}

Future
From what I can see in the the Moodle Tracker, it seems to me like Moodle 2.0 will have a quite different way to handle image locations.
Average of ratings: -
In reply to Tor Michal Kinn

Re: Solution: Smartpix - how to read variables from config.php fast

by Navin Dutta -
Seems like exactly what I was looking for!!! A lifesaver!! Thank you so much for sharing this dear! smile
In reply to Tor Michal Kinn

Re: Solution: Smartpix - how to read variables from config.php fast

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Awesome hack! Even hackier than the existing code (and obviously more likely to work, too). Well done. smile

Smartpix is a really big management improvement for sites with lots of dependent themes (like us) which is why I wrote it - but the system for handling images in Moodle 2 will be much better. As well as, sadly, altogether less hacky. smile

--sam



In reply to sam marshall

Svar: Re: Solution: Smartpix - how to read variables from config.php fast

by Tor Michal Kinn -
Thanks Sam,

I guess the less hacky 2.0 will free some time to find new code to hack, then smile