#!/usr/bin/php
<?php
/*
  vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
  +----------------------------------------------------------------------+
  | 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: fwconfig.php,v 1.1 2007/01/09 23:49:36 alex Exp $
*/
if (count($argv) < 2) {
    fprintf(STDERR, "$argv[0] newadminpassword\n");
    exit(1);
}
$sNuevoPassword = $argv[1];

if (trim($sNuevoPassword) == '' || !preg_match('/^[a-zA-Z0-9-@=_!<> .|&]+$/', $sNuevoPassword)) {
	fprintf(STDERR, "$argv[0]: invalid password\n");
    exit(1);
}

// Lista de archivos a modificar
$tablaModificar = array(
    array("name"=>"amportal.conf",  "pass_name"=>"AMPDBPASS", "path"=>"/etc/"),
    array("name"=>"res_mysql.conf", "pass_name"=>"dbpass",    "path"=>"/etc/asterisk/"),
    array("name"=>"cbmysql.conf",   "pass_name"=>"password",  "path"=>"/etc/asterisk/"),
    array("name"=>"cdr_mysql.conf", "pass_name"=>"password",  "path"=>"/etc/asterisk/"),
);
$bExito = TRUE;
foreach ($tablaModificar as $itemModificar) {
	$lineas = array();
    $sRutaArchivo = $itemModificar['path'].$itemModificar['name'];
    if (file_exists($sRutaArchivo)) {
        foreach (file($sRutaArchivo) as $sLinea) {
    		$regs = NULL;
            if (preg_match("/^({$itemModificar['pass_name']}\s*=\s*)/", $sLinea, $regs)) {
                $sLinea = "{$regs[1]}$sNuevoPassword\n";
            }
            $lineas[] = $sLinea;
    	}
        if (file_put_contents($sRutaArchivo, $lineas) === FALSE) {
            print "FAILED {$itemModificar['name']}\n";
        	$bExito = FALSE;
        } else {
            print "CHANGED {$itemModificar['name']}\n";
        }
    }
}

/* El archivo /etc/freepbx.conf se introduce en FreePBX 2.11 y es un caso 
 * especial. Este archivo no es un archivo de clave/valor como los archivos
 * anteriores, sino un segmento de código PHP. Por lo tanto las rutinas de
 * arriba no sirven para modificar la copia de la clave dentro de este archivo.
 */
$sRutaArchivo = '/etc/freepbx.conf';
if ($bExito && file_exists($sRutaArchivo)) {
    $sContenidoViejo = file_get_contents($sRutaArchivo);
    $sContenidoNuevo = preg_replace(
        "/(\\\$amp_conf\\[\\s*'AMPDBPASS'\\s*\\]\\s*=\\s*)(['\"]).*?\\2/",
        "\$1'{$sNuevoPassword}'",
        $sContenidoViejo);
    if (!is_null($sContenidoNuevo)) {
        if (file_put_contents($sRutaArchivo, $sContenidoNuevo) === FALSE) {
            print "FAILED $sRutaArchivo\n";
        	$bExito = FALSE;
        } else {
            print "CHANGED $sRutaArchivo\n";
        }
    } else {
        print "FAILED $sRutaArchivo\n";
        $bExito = FALSE;
    }
}
exit($bExito ? 0 : 1);
?>
