1: <?php
2: // ------------------------------------------------------------------------ //
3: // XOOPS - PHP Content Management System //
4: // Copyright (c) 2000 XOOPS.org //
5: // <http://www.xoops.org/> //
6: // ------------------------------------------------------------------------ //
7: // This program is free software; you can redistribute it and/or modify //
8: // it under the terms of the GNU General Public License as published by //
9: // the Free Software Foundation; either version 2 of the License, or //
10: // (at your option) any later version. //
11: // //
12: // You may not change or alter any portion of this comment or credits //
13: // of supporting developers from this source code or any supporting //
14: // source code which is considered copyrighted (c) material of the //
15: // original comment or credit authors. //
16: // //
17: // This program is distributed in the hope that it will be useful, //
18: // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20: // GNU General Public License for more details. //
21: // //
22: // You should have received a copy of the GNU General Public License //
23: // along with this program; if not, write to the Free Software //
24: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
25: // ------------------------------------------------------------------------ //
26:
27: /**
28: * mainfile manager for XOOPS installer
29: *
30: * @author Haruki Setoyama <haruki@planewave.org>
31: * @version $Id: mainfilemanager.php,v 1.1 2007/05/15 02:35:13 minahito Exp $
32: * @access public
33: **/
34: class mainfile_manager {
35:
36: var $path = '../mainfile.php';
37: var $distfile = '../mainfile.dist.php';
38: var $rewrite = array();
39:
40: var $report = array();
41: var $error = false;
42:
43: function mainfile_manager(){
44: //
45: }
46:
47: function setRewrite($def, $val){
48: $this->rewrite[$def] = $val;
49: }
50:
51: function copyDistFile(){
52: if ( ! copy($this->distfile, $this->path) ) {
53: $this->report[] = _NGIMG.sprintf(_INSTALL_L126, '<b>'.$this->path.'</b>');
54: $this->error = true;
55: return false;
56: }
57: $this->report[] = _OKIMG.sprintf(_INSTALL_L125, '<b>'.$this->path.'</b>', '<b>'.$this->distfile.'</b>');
58: return true;
59: }
60:
61: function doRewrite(){
62: if ( ! $file = fopen($this->path,"r") ) {
63: $this->error = true;
64: return false;
65: }
66: clearstatcache();
67: $content = fread($file, filesize($this->path) );
68: fclose($file);
69:
70: foreach($this->rewrite as $key => $val){
71: if(is_int($val) &&
72: preg_match("/(define\()([\"'])(".$key.")\\2,\s*([0-9]+)\s*\)/",$content)){
73: $content = preg_replace("/(define\()([\"'])(".$key.")\\2,\s*([0-9]+)\s*\)/"
74: , "define('".$key."', ".$val.")"
75: , $content);
76: $this->report[] = _OKIMG.sprintf(_INSTALL_L121, "<b>$key</b>", $val);
77: }
78: elseif(preg_match("/(define\()([\"'])(".$key.")\\2,\s*([\"'])(.*?)\\4\s*\)/",$content)){
79: $content = preg_replace("/(define\()([\"'])(".$key.")\\2,\s*([\"'])(.*?)\\4\s*\)/"
80: , "define('".$key."', '".$this->sanitize($val)."')"
81: , $content);
82: $this->report[] = _OKIMG.sprintf(_INSTALL_L121, '<b>'.$key.'</b>', $val);
83:
84: }else{
85: $this->error = true;
86: $this->report[] = _NGIMG.sprintf(_INSTALL_L122, '<b>'.$val.'</b>');
87: }
88: }
89:
90: if ( !$file = fopen($this->path,"w") ) {
91: $this->error = true;
92: return false;
93: }
94:
95: if ( fwrite($file,$content) == -1 ) {
96: fclose($file);
97: $this->error = true;
98: return false;
99: }
100:
101: fclose($file);
102:
103: return true;
104: }
105:
106: function report(){
107: return $this->report;
108: }
109:
110: function error(){
111: return $this->error;
112: }
113:
114: function sanitize($val)
115: {
116: $val = addslashes($val);
117: return str_replace('$', '\$', $val);
118: }
119: }
120:
121: ?>