#!/usr/bin/php
<?php
/*
  vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
  Codificación: UTF-8
  +----------------------------------------------------------------------+
  | Elastix version 2.0                                                  |
  | http://www.elastix.org                                               |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006 Palosanto Solutions S. A.                         |
  +----------------------------------------------------------------------+
  | Cdla. Nueva Kennedy Calle E 222 y 9na. Este                          |
  | Telfs. 2283-268, 2294-440, 2284-356                                  |
  | Guayaquil - Ecuador                                                  |
  | http://www.palosanto.com                                             |
  +----------------------------------------------------------------------+
  | The contents of this file are subject to the General Public License  |
  | (GPL) Version 2 (the "License"); you may not use this file except in |
  | compliance with the License. You may obtain a copy of the License at |
  | http://www.opensource.org/licenses/gpl-license.php                   |
  |                                                                      |
  | Software distributed under the License is distributed on an "AS IS"  |
  | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See  |
  | the License for the specific language governing rights and           |
  | limitations under the License.                                       |
  +----------------------------------------------------------------------+
  | The Original Code is: Elastix Open Source.                           |
  | The Initial Developer of the Original Code is PaloSanto Solutions    |
  +----------------------------------------------------------------------+
  $Id: rchkconfig.php,v 1.1 2007/01/09 23:49:36 alex Exp $
*/

/* Restricted chkconfig - can only chkconfig services: asterisk, openfire, hylafax, postfix, mysql, apache, and dialer 
 * will no accept other parameters  */
$services = array(
            'Asterisk'  =>  'asterisk',
            'OpenFire'  =>  'openfire',
            'Hylafax'   =>  'hylafax',
            'Postfix'   =>  'postfix',
            'MySQL'     =>  'mysqld',
            'Apache'    =>  'httpd',
            'Dialer'    =>  'elastixdialer',
);

if ((count($argv) < 5)||(count($argv) > 5)) error_exit("ERR: must specify [level - service -status]\n");
$args = array();
if($argv[1]=="--level"){
    $args[] = $argv[1]; 
    if(($argv[2]>0)&&($argv[2]<6)){
        $args[] = $argv[2];
        if(in_array($argv[3],$services)){
	    $args[] = $argv[3];
	    if(($argv[4]=="on")||($argv[4]=="off"))
	    {
	      $args[] = $argv[4];
	      pcntl_exec('/sbin/chkconfig', $args);
	      error_exit("FATAL: unable to execve chkconfig!\n");
	    }else{
	      error_exit("ERR: invalid operation\n");
	    }
	}else
	  error_exit("ERR: invalid service \n");
	
    }else
      error_exit("ERR: invalid runlevel.\n");
}else
    error_exit("ERR: invalid operation\n");


function error_exit($sMsg, $errorcode = 1)
{
    fwrite(STDERR, $sMsg);
    exit($errorcode);
}
?>
