[mod] An ON/OFF Button for Cron in the Admin Panel

3rd party code for phpList

[mod] An ON/OFF Button for Cron in the Admin Panel

Postby sing1ejack » 5:22pm, Thu 01 Mar, 2007

I made this modification to PHPList 2.10.3 so that anyone in the company could handle every aspect of sending a newsletter. We use a cronjob to process the queue, but, for various reasons, want to turn the cronjob off after each mailing. The simple PHPList interface means someone else can do the mailings, except that I have to turn the cronjob on and off. With this mod, mailings are totally off my sholders.

So here are the steps to add a link or button on PHPList admin sidebar that lets a user turn the cronjob on or off (ie. it toggles the setting).

This approach assumes:
    - you have some desire to be able to toggle the cronjob from the web pannel
    - your cronjob is a one-liner (if you have to comment out more than one line in the crontab, you'll have to modify this code some).


1) Copy core functions. copy cron_toggle.txt to your [your web root]/lists/admin/cron_toggle.php. Remember to change the extension.

2) Allow access to cron_toggle.php. first go to [your web root]/lists/admin/.htaccess and add cron_toggle.php to the list of allowed pages (along with index.php, upload.php, etc). Now you can browse to cron_toggle.php directly.

3) Modify the regex pattern. There is only one change you will need to make in the code. cron_toggle.php contains a global variable $cronjob_pattern. This is a regular expression that is used to identify the PHPList cronjob.

You may use any segment of text that is found on the cronjob. If your cronjob reads:
Code: Select all
0,30 * * * * wget --post-data "page=processqueue&login=admin&password=secret" http://www.mydom.com/lists/admin/ > /dev/null

then any of the following would work:
Code: Select all
$cronjob_pattern='/login=admin&password=secret/';
$cronjob_pattern='/cret" http:\/\/www.mydom.com/';

Since it is a regex pattern (not just an ordinary string) remember to escape any forward slashes. Be sure you keep the forward slashes at the beginning and the end of the string, too.

Also, it is important to use a pattern that matches *only* your PHPList cronjob. Changes will be made to the first cronjob that matches your pattern, so if you choose a pattern that matches anther job, that job will be toggled instead. This can be a difficult mistake to catch since everything on the admin web interface will appear to work correctly.

4) Test cron_toggle.php. Uncomment the 2 test lines in cron_toggle.php and save the file. Open a browser and browse to cron_toggle.php (something like www.example.com/lists/admin/cron_toggle.php). You will the test result and a brief explanation.

There is only one large problem you are likely to run into and this is where it would come up. If your web server runs under the same user that is running the PHPList cronjob, then you should be fine.

However, by default Apache uses the user 'nobody' (I don't know how other web servers work) and that user probably doesn't have the right crontab, if it has a crontab at all. I can't help you much here since I didn't have to figure this out: the server I wrote this script for runs under the user I need already. However, you might look into setting the sticky bit so that cron_toggle is executed with the privilages of it's owner. It would be great if someone could reply with a solution to this problem.

5) Edit PHPList sidebar. When you get cron_toggle.php testing ok, make changes to the PHPList sidebar code. Open the php file [your web root]/lists/admin/connect.php. Go down to the function newMenu(). Near the end of the function there is a for loop that you need to insert some code into. This should be somewhere around line 440 of connect.php.

Code: Select all
433  ...
434  foreach ($GLOBALS["main_menu"] as $page => $desc) {
435    $link = PageLink2($page,$GLOBALS["I18N"]->get($desc));
436    if ($link) {
437      if ($page == "preparesend" || $page == "sendprepared") {
438        if (USE_PREPARE) {
439          $html .= $spb.$link.$spe;
440        }
     
      /* <<begin>> */
      // this code places a button on the sidebar that toggles the cronjob
      // from the web interface
      } elseif ($page == "processqueue") {
        // get state of crontab
        require_once dirname(__FILE__).'/cron_toggle.php';
        $job_state = get_autoqueue_state();
        // if active
        if($job_state==1){
          $link = PageLink2("autoqueue_warn",($GLOBALS["I18N"]->get($desc)." (n/a)"));
          $autoq = $spb.PageLink2("autoqueue_toggle","autoqueue (on)").$spe;
        }
        // if inactive
        if($job_state==0){
          $link = PageLink2($page,($GLOBALS["I18N"]->get($desc)));
          $autoq = $spb.PageLink2("autoqueue_toggle","autoqueue (off)").$spe;
        }
        // if error
        if($job_state<0>get($desc)));
          $autoq = $spb.PageLink2("autoqueue_toggle","autoqueue (n/a)").$spe;
        }
        // print all
        $html .= $spb.$link.$spe;       // print processqueue link
        $html .= $autoq;                // print autoqueue link
      /* <<end>> */
     
441      } else {
442        $html .= $spb.$link.$spe;
443      }
444    }
445  }
446  ...


6) Implement functionality. Copy autoqueue_toggle.txt to [your web root]/lists/admin/autoqueue_toggle.php. This page toggles the PHPList cronjob on and off and prints a little message that corresponds to the cronjob state. No changes need to be made to this file. Remember to change the extension.

7) Create error page. Copy autoqueue_warn.txt to [your web root]/lists/admin/autoqueue_warn.php. If a user tries to use the normal process queue button while the autoqueue is active (ie, a cronjob is running) a user will get this error page with an explaniation and instructions. No changes need to be made to this file; remember to change the extension.

8) Verify cronjob toggle. It's important to be sure that the crontab is being affected correctly. In a shell, type 'crontab -l' and note the state of the PHPlist cronjob. In the PHPList admin panel, click autoqueue and verify that everything appears functional. Go back to your shell, type 'crontab -l' again and verify that the PHPList cronjob was indeed affected. Also, make sure you have undone any changes you made to the .htaccess file. That should do it.
sing1ejack
PL Nut
 
Posts: 15
Joined: 2:04pm, Wed 22 Nov, 2006

Postby jetmuzer » 3:06pm, Fri 23 Nov, 2007

damn it isn't work after many trys :evil:
maybe i did something wrong? :?:
jetmuzer
phpList newbie
 
Posts: 1
Joined: 4:22pm, Thu 22 Nov, 2007


Return to Contributions: Plug-ins, Add-ons, Mods

Who is online

Users browsing this forum: No registered users and 0 guests