PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/code/classes/pinetd/ConfigManager.class.php

https://github.com/blekkzor/pinetd2
PHP | 233 lines | 201 code | 11 blank | 21 comment | 22 complexity | dc022a023c81db85a9038e9cb58d4627 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* Portable INET daemon v2 in PHP
  3. * Copyright (C) 2007 Mark Karpeles <mark@kinoko.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. namespace pinetd;
  20. use \SimpleXMLElement;
  21. use \DOMDocument;
  22. /**
  23. * \brief This class handles config-related work
  24. */
  25. class ConfigManager {
  26. static private $self = null;
  27. private $config = 'config.xml';
  28. private $xml = null;
  29. public function invoke() {
  30. if (is_null(self::$self)) {
  31. self::$self = new self;
  32. }
  33. return self::$self;
  34. }
  35. public function __get($var) {
  36. return $this->xml->$var;
  37. }
  38. protected function __construct() {
  39. if (!is_readable(PINETD_ROOT.'/'.$this->config)) {
  40. if (file_exists(PINETD_ROOT.'/config.php')) {
  41. $this->parseOldConfig(PINETD_ROOT.'/config.php');
  42. }
  43. if (!file_exists(PINETD_ROOT.'/config.php')) {
  44. $this->xml = new SimpleXMLElement(file_get_contents(PINETD_ROOT . '/etc/default_config.xml'));
  45. $this->saveConfig();
  46. }
  47. } else {
  48. $this->xml = new SimpleXMLElement(PINETD_ROOT.'/'.$this->config, LIBXML_NOBLANKS | LIBXML_NSCLEAN, true);
  49. }
  50. if (isset($this->xml->Global->RemoveMe)) {
  51. echo rtrim($this->xml->Global->RemoveMe)."\n";
  52. exit(8);
  53. }
  54. $this->saveConfig();
  55. }
  56. public function setConfigVar($var, $val) {
  57. if(is_null($this->xml)) $this->xml = new SimpleXMLElement(file_get_contents(PINETD_ROOT . '/etc/default_config.xml'));
  58. $cur = $this->xml;
  59. $var = str_replace(':', '/:', $var);
  60. while(($pos=strpos($var, '/')) !== false) {
  61. $sub = substr($var, 0, $pos);
  62. $var = substr($var, $pos + 1);
  63. if ($sub == '') continue;
  64. if (!isset($cur->$sub)) {
  65. $cur = $cur->addChild($sub);
  66. } else {
  67. $cur = $cur->$sub;
  68. }
  69. }
  70. if (substr($var, 0, 1) == ':') {
  71. $var = substr($var, 1);
  72. if (!isset($cur[$var])) {
  73. $cur->addAttribute($var, $val);
  74. } else {
  75. $cur[$var] = $val;
  76. }
  77. } else {
  78. if (!isset($cur->$var)) {
  79. $cur->addChild($var, $val);
  80. } else {
  81. $cur->$var = $val;
  82. }
  83. }
  84. // echo $var.' => '.$val."\n";
  85. }
  86. public function saveConfig() {
  87. $doc = new DOMDocument('1.0', 'utf-8');
  88. $doc->formatOutput = true;
  89. $domnode = dom_import_simplexml($this->xml);
  90. $domnode = $doc->importNode($domnode, true);
  91. $domnode = $doc->appendChild($domnode);
  92. $fp =@fopen(PINETD_ROOT.'/'.$this->config.'~', 'w');
  93. if (!$fp) {
  94. return false;
  95. }
  96. fwrite($fp, $doc->saveXML());
  97. fclose($fp);
  98. rename(PINETD_ROOT.'/'.$this->config.'~', PINETD_ROOT.'/'.$this->config);
  99. return true;
  100. }
  101. public function read($var) {
  102. $elem = $this->xml->xpath($var);
  103. if (count($elem) != 1) throw new Exception('No such - or too many - of this path : '.$var);
  104. $elem = $elem[0];
  105. return $elem[0];
  106. }
  107. private function parseOldConfig($file) {
  108. $old_vars = array(
  109. 'remove_me' => 'Global/RemoveMe',
  110. 'servername' => 'Global/Name',
  111. 'pidfile' => 'Global/PidFile',
  112. 'bind_ip' => 'Global/Network/Bind/Ip',
  113. 'pasv_ip' => 'Global/Network/Bind/Ip:External',
  114. 'sql_user' => 'Global/Storage/MySQL:Login',
  115. 'sql_pass' => 'Global/Storage/MySQL:Password',
  116. 'sql_host' => 'Global/Storage/MySQL:Host',
  117. 'max_users' => 'Daemons/FTPd/MaxUsers',
  118. 'max_anonymous' => 'Daemons/FTPd/MaxUsers:Anonymous',
  119. 'ftp_server' => 'Daemons/FTPd/Name',
  120. 'max_users_per_ip' => 'Daemons/FTPd/Network:MaxUsersPerIp',
  121. 'ftp_owner_u' => 'Daemons/FTPd/SUID:User',
  122. 'ftp_owner_g' => 'Daemons/FTPd/SUID:Group',
  123. 'PHPMAILD_STORAGE' => 'Daemons/PMaild/Mails:Path',
  124. 'PHPMAILD_DEFAULT_DOMAIN' => 'Daemons/PMaild/DefaultDomain',
  125. 'PHPMAILD_DB_NAME' => 'Daemons/PMaild/Storage/MySQL:Database',
  126. 'pmaild_mta_max_processes' => 'Daemons/PMaild/MTA:MaxProcesses',
  127. 'pmaild_mta_thread_start_threshold' => 'Daemons/PMaild/MTA:StartThreshold',
  128. 'pmaild_mta_max_attempt' => 'Daemons/PMaild/MTA:MaxAttempt',
  129. 'pmaild_mta_mail_max_lifetime' => 'Daemons/PMaild/MTA:MailMaxLifetime',
  130. );
  131. if(!extension_loaded('tokenizer')) {
  132. die("Can't parse old config without tokenizer!\n");
  133. }
  134. $dat = token_get_all(file_get_contents($file));
  135. $cmd = array();
  136. $config = array();
  137. foreach($dat as $tok) {
  138. if (is_string($tok)) {
  139. if ($tok == ';') {
  140. switch($cmd['op']) {
  141. case 'ignore':
  142. break;
  143. case 'eq';
  144. $config[$cmd['var']] = $cmd['value'];
  145. break;
  146. }
  147. $cmd = array();
  148. continue;
  149. }
  150. switch($tok) {
  151. case '=':
  152. $cmd['op'] = 'eq';
  153. break;
  154. case ',':
  155. if ($cmd['definemode'] == 1) {
  156. $cmd['definemode']=2;
  157. break;
  158. }
  159. case '(':
  160. case '.':
  161. case ')':
  162. break;
  163. default:
  164. var_dump($tok);
  165. exit;
  166. }
  167. continue;
  168. }
  169. if ($cmd['op'] == 'ignore') {
  170. continue;
  171. }
  172. switch($tok[0]) {
  173. case T_OPEN_TAG:
  174. break; // do not care about this
  175. case T_EXIT:
  176. $cmd['var'] = 'remove_me';
  177. $cmd['op'] = 'eq';
  178. break;
  179. case T_ARRAY:
  180. $cmd['arraymode'] = 1;
  181. $cmd['op'] = 'ignore';
  182. break;
  183. case T_VARIABLE:
  184. $cmd['var'] = substr($tok[1], 1);
  185. break;
  186. case T_CONSTANT_ENCAPSED_STRING:
  187. case T_LNUMBER;
  188. $val = eval('return '.$tok[1].';');
  189. if ($cmd['definemode'] == 1) {
  190. $cmd['var'].=$val;
  191. break;
  192. }
  193. $cmd['value'] .= $val;
  194. break;
  195. case T_COMMENT:
  196. case T_WHITESPACE:
  197. continue;
  198. case T_STRING:
  199. if ($tok[1] == 'define') {
  200. $cmd['definemode'] = 1;
  201. $cmd['op'] = 'eq';
  202. break;
  203. }
  204. default:
  205. $tok[0] = token_name((int)$tok[0]);
  206. var_dump($tok);
  207. exit;
  208. }
  209. }
  210. // need to map param from old config to new stuff
  211. foreach($config as $var => $val) {
  212. if (!isset($old_vars[$var])) {
  213. echo 'CONVERT_CONFIG: Dropping unknown variable '.$var."\n";
  214. continue;
  215. }
  216. $this->setConfigVar($old_vars[$var], $val);
  217. }
  218. return $this->saveConfig();
  219. }
  220. }