Location: PHPKode > projects > Aukyla Platform > aukyla/doc/html/a00207.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
    <title>Aukyla PHP Framework</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link rel="StyleSheet" href="auton.css" type="text/css" media="screen">
  </head>

  <body>
    <div id="mainmenu">
      <a href="http://www.auton.nl/" class="home">Home</a>
    </div>
    <div id="submenu">
      <a class="logo" href="http://www.auton.nl/"></a>

      <div class="head">Ads</div>
      <a href="http://sourceforge.net/"><img src="http://sourceforge.net/sflogo.php?group_id=116358&amp;type=1" alt="SourceForge.net Logo" style="padding: 2px 4px; width: 88px; height: 31px; border: 0px"></a>
      <a href="http://getfirefox.com/" title="Get Firefox - The Browser, Reloaded."><img src="http://www.mozilla.org/products/firefox/buttons/getfirefox_small.png" alt="Get Firefox" style="width: 110px; height: 32px; border: 0px"></a>
    </div>
    <div id="main">
<!-- Generated by Doxygen 1.3.9.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a></div>
<div class="nav">
<a class="el" href="dir_000000.html">base</a></div>
<h1>Config.php</h1><div class="fragment"><pre class="fragment">00001 &lt;?php
00002 <span class="comment">/*</span>
00003 <span class="comment">     Config.php, gives access to configuration files</span>
00004 <span class="comment">     Copyright (C) 2003-2005 Arend van Beelen, Auton Rijnsburg</span>
00005 <span class="comment"></span>
00006 <span class="comment">     This program is free software; you can redistribute it and/or modify it</span>
00007 <span class="comment">     under the terms of the GNU General Public License as published by the Free</span>
00008 <span class="comment">     Software Foundation; either version 2 of the License, or (at your option)</span>
00009 <span class="comment">     any later version.</span>
00010 <span class="comment"></span>
00011 <span class="comment">     This program is distributed in the hope that it will be useful, but WITHOUT</span>
00012 <span class="comment">     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or</span>
00013 <span class="comment">     FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for</span>
00014 <span class="comment">     more details.</span>
00015 <span class="comment"></span>
00016 <span class="comment">     You should have received a copy of the GNU General Public License along</span>
00017 <span class="comment">     with this program; if not, write to the Free Software Foundation, Inc.,</span>
00018 <span class="comment">     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA</span>
00019 <span class="comment"></span>
00020 <span class="comment">     For any questions, comments or whatever, you may mail me at: hide@address.com</span>
00021 <span class="comment">*/</span>
00022 
00023 require_once('Constants.php');
00024 require_once('<a class="code" href="a00119.html">Locale</a>.php');
00025 require_once('<a class="code" href="a00153.html">String</a>.php');
00026 
00030 <span class="keyword">class </span>ConfigVariable
00031 {
00032         <span class="keyword">public</span> function __construct($name, $value, $changed = <span class="keyword">false</span>)
00033         {
00034                 $this-&gt;name = $name;
00035                 $this-&gt;value = $value;
00036                 $this-&gt;changed = $changed;
00037         }
00038 
00039         <span class="keyword">public</span> $name;
00040         <span class="keyword">public</span> $value;
00041         <span class="keyword">public</span> $changed;
00042 }
00043 
<a name="l00062"></a><a class="code" href="a00092.html">00062</a> <span class="keyword">class </span><a class="code" href="a00092.html">Config</a>
00063 {
<a name="l00083"></a><a class="code" href="a00092.html#a0">00083</a>         <span class="keyword">public</span> function __construct($modulename = '', $username = 'anonymous')
00084         {
00085                 <span class="keywordflow">if</span>(<a class="code" href="a00153.html#e2">String::startsWith</a>($username, 'anonymous'))
00086                 {
00087                         $username = 'anonymous';
00088                 }
00089 
00090                 $this-&gt;modulename = $modulename;
00091                 $this-&gt;username = $username;
00092 
00093                 $this-&gt;readConfiguration();
00094 
00095                 <span class="comment">// register all request variables for the global configuration</span>
00096                 <span class="keywordflow">if</span>($modulename == '' &amp;&amp; $username == 'anonymous')
00097                 {
00098                         $request = array_merge($_GET, $_POST);
00099                         foreach($request as $var =&gt; $val)
00100                         {
00101                                 <span class="keywordflow">if</span>($val != '')
00102                                 {
00103                                         <span class="keywordflow">if</span>(get_magic_quotes_gpc() == <span class="keyword">true</span>)
00104                                         {
00105                                                 $val = stripslashes($val);
00106                                         }
00107 
00108                                         $this-&gt;requests[$var] = $val;
00109                                 }
00110                         }
00111                 }
00112         }
00113 
<a name="l00122"></a><a class="code" href="a00092.html#a1">00122</a>         <span class="keyword">public</span> function readConfiguration()
00123         {
00124                 $this-&gt;variables = array();
00125 
00126                 $files[] = AUKYLA_DIR.'/config/global.conf';
00127                 <span class="keywordflow">if</span>($this-&gt;username != 'anonymous')
00128                 {
00129                         $files[] = AUKYLA_DIR.<span class="stringliteral">"/config/{$this-&gt;username}/global.conf"</span>;
00130                 }
00131                 <span class="keywordflow">if</span>($this-&gt;modulename != '')
00132                 {
00133                         $files[] = AUKYLA_DIR.<span class="stringliteral">"/config/{$this-&gt;modulename}.conf"</span>;
00134                         <span class="keywordflow">if</span>($this-&gt;username != 'anonymous')
00135                         {
00136                                 $files[] = AUKYLA_DIR.<span class="stringliteral">"/config/{$this-&gt;username}/{$this-&gt;modulename}.conf"</span>;
00137                         }
00138                 }
00139 
00140                 foreach($files as $file)
00141                 {
00142                         <span class="keywordflow">if</span>(file_exists($file) &amp;&amp; $lines = file($file))
00143                         {
00144                                 foreach($lines as $line)
00145                                 {
00146                                         $line = trim($line);
00147 
00148                                         <span class="keywordflow">if</span>(substr($line, 0, 1) == <span class="stringliteral">"#"</span> || strlen($line) == 0)
00149                                         {
00150                                                 <span class="keywordflow">continue</span>;
00151                                         }
00152 
00153                                         list($var, $val) = explode(<span class="charliteral">'='</span>, $line, 2);
00154                                         $var = rtrim($var);
00155                                         $val = ltrim($val);
00156                                         <span class="comment">// special tags...</span>
00157                                         <span class="keywordflow">switch</span>($var)
00158                                         {
00159                                                 <span class="keywordflow">case</span> 'baseURL':
00160                                                 <span class="keywordflow">case</span> 'secureBaseURL':
00161                                                 <span class="keywordflow">case</span> 'downloadURL':
00162                                                 <span class="keywordflow">case</span> 'secureDownloadURL':
00163                                                 <span class="keywordflow">case</span> 'editURL':
00164                                                 <span class="keywordflow">case</span> 'secureEditURL':
00165                                                         <span class="keywordflow">if</span>(isset($_SERVER['HTTP_HOST']))
00166                                                         {
00167                                                                 $val = str_replace('%HOST%', $_SERVER['HTTP_HOST'], $val);
00168                                                         }
00169                                                         <span class="keywordflow">break</span>;
00170 
00171                                                 <span class="keywordflow">case</span> 'language':
00172                                                         <span class="keywordflow">if</span>(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) == <span class="keyword">false</span>)
00173                                                         {
00174                                                                 $preferredLanguage = 'en';
00175                                                         }
00176                                                         <span class="keywordflow">else</span> <span class="keywordflow">if</span>(strstr($_SERVER['HTTP_ACCEPT_LANGUAGE'], <span class="charliteral">','</span>) == <span class="keyword">false</span>)
00177                                                         {
00178                                                                 $preferredLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
00179                                                         }
00180                                                         <span class="keywordflow">else</span>
00181                                                         {
00182                                                                 list($preferredLanguage, $otherLanguages) = explode(<span class="charliteral">','</span>, $_SERVER['HTTP_ACCEPT_LANGUAGE'], 2);
00183                                                         }
00184                                                         $val = str_replace('%ACCEPT_LANGUAGE%', $preferredLanguage, $val);
00185                                                         <span class="keywordflow">break</span>;
00186 
00187                                                 <span class="keywordflow">default</span>:
00188                                         }
00189                                         $this-&gt;variables[$var] = <span class="keyword">new</span> ConfigVariable($var, $val);
00190                                 }
00191                         }
00192                 }
00193         }
00194 
<a name="l00205"></a><a class="code" href="a00092.html#e0">00205</a>         <span class="keyword">public</span> <span class="keyword">static</span> function globals($variable, $<span class="keywordflow">default</span> = '')
00206         {
00207                 global $Config;
00208 
00209                 <span class="keywordflow">return</span> $Config-&gt;variable($variable, $<span class="keywordflow">default</span>);
00210         }
00211 
<a name="l00224"></a><a class="code" href="a00092.html#e1">00224</a>         <span class="keyword">public</span> <span class="keyword">static</span> function setGlobal($variable, $value)
00225         {
00226                 global $Config;
00227 
00228                 $Config-&gt;setVariable($variable, $value);
00229         }
00230 
<a name="l00243"></a><a class="code" href="a00092.html#e2">00243</a>         <span class="keyword">public</span> <span class="keyword">static</span> function request($variable, $<span class="keywordflow">default</span> = '')
00244         {
00245                 global $Config;
00246 
00247                 <span class="keywordflow">return</span> (isset($<a class="code" href="a00092.html">Config</a>-&gt;requests[$variable]) ? $Config-&gt;requests[$variable] : $default);
00248         }
00249 
<a name="l00259"></a><a class="code" href="a00092.html#e3">00259</a>         <span class="keyword">public</span> <span class="keyword">static</span> function requests()
00260         {
00261                 global $Config;
00262 
00263                 <span class="keywordflow">return</span> $Config-&gt;requests;
00264         }
00265 
<a name="l00275"></a><a class="code" href="a00092.html#e4">00275</a>         <span class="keyword">public</span> <span class="keyword">static</span> function setRequest($variable, $value = <span class="keyword">false</span>)
00276         {
00277                 global $Config;
00278 
00279                 <span class="keywordflow">if</span>($value === <span class="keyword">false</span>)
00280                 {
00281                         $Config-&gt;unsetRequest($variable);
00282                 }
00283                 <span class="keywordflow">else</span>
00284                 {
00285                         $Config-&gt;requests[$variable] = $value;
00286                 }
00287         }
00288 
<a name="l00298"></a><a class="code" href="a00092.html#e5">00298</a>         <span class="keyword">public</span> <span class="keyword">static</span> function unsetRequest($variable)
00299         {
00300                 global $Config;
00301 
00302                 unset($<a class="code" href="a00092.html">Config</a>-&gt;requests[$variable]);
00303         }
00304 
<a name="l00310"></a><a class="code" href="a00092.html#e6">00310</a>         <span class="keyword">public</span> <span class="keyword">static</span> function unsetRequests()
00311         {
00312                 global $Config;
00313 
00314                 $Config-&gt;requests = array();
00315         }
00316 
<a name="l00328"></a><a class="code" href="a00092.html#a2">00328</a>         <span class="keyword">public</span> function variable($variable, $<span class="keywordflow">default</span> = '')
00329         {
00330                 <span class="keywordflow">return</span> (isset($this-&gt;variables[$variable]) ? $this-&gt;variables[$variable]-&gt;value : $default);
00331         }
00332 
<a name="l00344"></a><a class="code" href="a00092.html#a3">00344</a>         <span class="keyword">public</span> function setVariable($variable, $value)
00345         {
00346                 <span class="keywordflow">if</span>(isset($this-&gt;variables[$variable]))
00347                 {
00348                         $this-&gt;variables[$variable]-&gt;value = $value;
00349                         $this-&gt;variables[$variable]-&gt;changed = <span class="keyword">true</span>;
00350                 }
00351                 <span class="keywordflow">else</span>
00352                 {
00353                         $this-&gt;variables[$variable] = <span class="keyword">new</span> ConfigVariable($variable, $value, <span class="keyword">true</span>);
00354                 }
00355         }
00356 
<a name="l00370"></a><a class="code" href="a00092.html#a4">00370</a>         <span class="keyword">public</span> function saveConfiguration()
00371         {
00372                 $dir = AUKYLA_DIR.'/config/'.($this-&gt;username != 'anonymous' ?
00373                                               <span class="stringliteral">"{$this-&gt;username}/"</span> : '');
00374                 $file = $dir.($this-&gt;modulename != '' ? $this-&gt;modulename : 'global').'.conf';
00375                 <span class="keywordflow">if</span>(!file_exists($dir))
00376                 {
00377                         mkdir($dir, 0700);
00378                 }
00379 
00380                 $lines = (file_exists($file) ? file($file) : array(0 =&gt; ''));
00381                 <span class="keywordflow">if</span>($fp = fopen($file, <span class="charliteral">'w'</span>))
00382                 {
00383                         foreach($this-&gt;variables as $var)
00384                         {
00385                                 <span class="keywordflow">if</span>($var-&gt;changed == <span class="keyword">false</span>)
00386                                 {
00387                                         <span class="keywordflow">continue</span>;
00388                                 }
00389 
00390                                 $setval = <span class="keyword">false</span>;
00391                                 foreach($lines as $linenum =&gt; $line)
00392                                 {
00393                                         $line = trim($line);
00394 
00395                                         <span class="keywordflow">if</span>(substr($line, 0, 1) != <span class="stringliteral">"#"</span> &amp;&amp; strlen($line) != 0)
00396                                         {
00397                                                 list($lineVar, $lineVal) = explode(<span class="charliteral">'='</span>, $line, 2);
00398                                                 $lineVar = rtrim($lineVar);
00399                                                 <span class="keywordflow">if</span>($lineVar == $var-&gt;name)
00400                                                 {
00401                                                         $lines[$linenum] = <span class="stringliteral">"{$var-&gt;name} = {$var-&gt;value}\n"</span>;
00402                                                         $setval = <span class="keyword">true</span>;
00403                                                 }
00404                                         }
00405                                 }
00406                                 <span class="keywordflow">if</span>($setval == <span class="keyword">false</span>)
00407                                 {
00408                                         $lines[] = <span class="stringliteral">"{$var-&gt;name} = {$var-&gt;value}\n"</span>;
00409                                 }
00410                         }
00411 
00412                         foreach($lines as $line)
00413                         {
00414                                 fwrite($fp, $line);
00415                         }
00416 
00417                         fclose($fp);
00418 
00419                         <span class="keywordflow">return</span> <span class="keyword">true</span>;
00420                 }
00421                 <span class="keywordflow">return</span> <span class="keyword">false</span>;
00422         }
00423 
00424         <span class="keyword">private</span> $modulename;
00425         <span class="keyword">private</span> $username;
00426         <span class="keyword">private</span> $variables;
00427         <span class="keyword">private</span> $requests;
00428 }
00429 
00430 <span class="comment">// create one global instance of the class and initialize localization according</span>
00431 <span class="comment">// to the global settings</span>
00432 global $Config;
00433 $Config = <span class="keyword">new</span> <a class="code" href="a00092.html">Config</a>();
00434 <a class="code" href="a00119.html#e0">Locale::init</a>('base', Config::globals('language', 'en'));
00435 
00436 ?&gt;
</pre></div>    </div>
  </body>
</html>
Return current item: Aukyla Platform