PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Lampcms/Config/Ini.php

http://github.com/snytkine/LampCMS
PHP | 399 lines | 284 code | 20 blank | 95 comment | 13 complexity | ad9d06a921ca9b1174b86489b72f299d MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /**
  3. *
  4. * License, TERMS and CONDITIONS
  5. *
  6. * This software is licensed under the GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
  7. * Please read the license here : http://www.gnu.org/licenses/lgpl-3.0.txt
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * ATTRIBUTION REQUIRED
  20. * 4. All web pages generated by the use of this software, or at least
  21. * the page that lists the recent questions (usually home page) must include
  22. * a link to the http://www.lampcms.com and text of the link must indicate that
  23. * the website's Questions/Answers functionality is powered by lampcms.com
  24. * An example of acceptable link would be "Powered by <a href="http://www.lampcms.com">LampCMS</a>"
  25. * The location of the link is not important, it can be in the footer of the page
  26. * but it must not be hidden by style attributes
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  31. * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  32. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  33. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  35. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  36. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  37. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. * This product includes GeoLite data created by MaxMind,
  40. * available from http://www.maxmind.com/
  41. *
  42. *
  43. * @author Dmitri Snytkine <cms@lampcms.com>
  44. * @copyright 2005-2012 (or current year) Dmitri Snytkine
  45. * @license http://www.gnu.org/licenses/lgpl-3.0.txt GNU LESSER GENERAL PUBLIC LICENSE (LGPL) version 3
  46. * @link http://www.lampcms.com Lampcms.com project
  47. * @version Release: @package_version@
  48. *
  49. *
  50. */
  51. namespace Lampcms\Config;
  52. use Lampcms\IniException;
  53. /**
  54. * Object represents the parsed !config.ini file
  55. * has accessor for the whole section via getSection
  56. * or access values from the CONSTANTS section via
  57. * the magic __get method like this:
  58. * Ini->ADMIN_EMAIL
  59. *
  60. *
  61. * @author admin
  62. *
  63. */
  64. class Ini extends \Lampcms\LampcmsArray
  65. {
  66. protected $iniFile;
  67. /**
  68. * Array of Config\Section objects
  69. * key is section name
  70. * value of object
  71. *
  72. * @var array
  73. */
  74. protected $sections = array();
  75. /**
  76. * Constructor
  77. *
  78. * @param string $iniFile
  79. *
  80. * @throws IniException if unable to parse ini file
  81. *
  82. */
  83. public function __construct($iniFile = null)
  84. {
  85. if (null === $iniFile && defined('CONFIG_FILE_PATH')) {
  86. $iniFile = CONFIG_FILE_PATH;
  87. }
  88. $this->iniFile = (!empty($iniFile)) ? $iniFile : \rtrim(constant('LAMPCMS_CONFIG_DIR'), ' /\\') . DIRECTORY_SEPARATOR . '!config.ini';
  89. if (!\file_exists($this->iniFile)) {
  90. throw new IniException('Ini file not found at this location: ' . $this->iniFile);
  91. }
  92. $aIni = \parse_ini_file($this->iniFile, true);
  93. if (empty($aIni)) {
  94. throw new IniException('Unable to parse ini file: ' . $this->iniFile . ' probably a syntax error in file of file does not exist');
  95. }
  96. $this->exchangeArray($aIni);
  97. }
  98. public function getLogFilePath($prefix = '')
  99. {
  100. if (\substr(PHP_SAPI, 0, 3) === 'cli') {
  101. $ret = $this->__get('LOG_DIR') . DIRECTORY_SEPARATOR . 'cli-php';
  102. } elseif ((\substr(PHP_SAPI, 0, 3) === 'cgi')) {
  103. $ret = $this->__get('LOG_DIR') . DIRECTORY_SEPARATOR . 'cgi-php';
  104. } else {
  105. $ret = $this->__get('LOG_DIR') . DIRECTORY_SEPARATOR . 'php';
  106. }
  107. $ret .= $prefix . '_' . date('m-d-Y') . '.log';
  108. return $ret;
  109. }
  110. /**
  111. * Get value of config var from
  112. * object
  113. *
  114. * @param string $name
  115. *
  116. * @throws IniException if CONSTANTS key
  117. * does not exist OR if var
  118. * does not exist and is a required var
  119. *
  120. * @return string value of $name
  121. */
  122. public function getVar($name)
  123. {
  124. if (!$this->offsetExists('CONSTANTS')) {
  125. throw new IniException('"CONSTANTS" section is missing in !config.ini file: ' . $this->iniFile . ' config: ' . \print_r($this->getArrayCopy(), true));
  126. }
  127. $aConstants = $this->offsetGet('CONSTANTS');
  128. /**
  129. * TEMP_DIR returns path
  130. * to temp always ends with DIRECTORY_SEPARATOR
  131. * if TEMP_DIR not defined in !config.ini
  132. * then will use system's default temp dir
  133. *
  134. */
  135. if ('TEMP_DIR' === $name) {
  136. if (!empty($aConstants['TEMP_DIR'])) {
  137. $tmpDir = \rtrim($aConstants['TEMP_DIR'], '/');
  138. $tmpDir .= DIRECTORY_SEPARATOR;
  139. return $tmpDir;
  140. }
  141. return \sys_get_temp_dir();
  142. }
  143. if (!array_key_exists($name, $aConstants) && !$this->offsetExists($name) && ('LOG_FILE_PATH' !== $name)) {
  144. throw new IniException('Error: configuration param: ' . $name . ' does not exist in config file ' . $this->iniFile);
  145. }
  146. if ('MAGIC_MIME_FILE' === $name) {
  147. if (!empty($aConstants['MAGIC_MIME_FILE']) && !is_readable($aConstants['MAGIC_MIME_FILE'])) {
  148. throw new IniException('magic mime file does not exist in this location or not readable: ' . $aConstants['MAGIC_MIME_FILE']);
  149. }
  150. }
  151. switch ( $name ) {
  152. case 'SITE_URL':
  153. if (empty($aConstants['SITE_URL'])) {
  154. throw new IniException('Value of SITE_URL in ' . $this->iniFile . ' file SHOULD NOT be empty!');
  155. }
  156. $ret = \rtrim($aConstants['SITE_URL'], '/');
  157. break;
  158. /**
  159. * If these constants are not specifically set
  160. * then we should return the path to our
  161. * main website.
  162. * This is because we need to use absolute url, not
  163. * relative url for these.
  164. * The reason is if using virtual hosting, then
  165. * relative urls will point to just /images/
  166. * so they will actually resolve to individual's own domain + path
  167. * for example http://somedude.outsite.com/images/
  168. * and on another user's site http://johnny.oursite.com/images/
  169. * This will cause chaos in browser caching.
  170. * Browser will think (rightfully so) that these are different sites.
  171. *
  172. * That's why we must point to our main site
  173. * for all images, css, js, etc... so that no matter whose
  174. * site we are on the browser can use cached files and most
  175. * importantly will not keep storing the same images in cache for each
  176. * sub-domain
  177. */
  178. case 'THUMB_IMG_SITE':
  179. case 'ORIG_IMG_SITE':
  180. $ret = (empty($aConstants[$name])) ? $this->__get('SITE_URL') : \rtrim($aConstants[$name], '/');
  181. break;
  182. case 'EMAIL_ADMIN':
  183. if (empty($aConstants[$name])) {
  184. throw new IniException($name . ' param in ' . $this->iniFile . ' file has not been set! Please make sure it is set');
  185. }
  186. $ret = \trim($aConstants[$name], "\"'");
  187. /**
  188. * Always lower-case admin email
  189. * otherwise it may cause problems during
  190. * the registration of the first account
  191. */
  192. if ('EMAIL_ADMIN' === $name) {
  193. $ret = \mb_strtolower($ret);
  194. }
  195. break;
  196. case 'LOG_DIR':
  197. $ret = (empty($aConstants['LOG_DIR'])) ? \dirname(LAMPCMS_WWW_DIR) . DIRECTORY_SEPARATOR . 'logs' : \rtrim($aConstants['LOG_DIR'], DIRECTORY_SEPARATOR);
  198. break;
  199. case 'LOG_FILE_PATH':
  200. if (\substr(PHP_SAPI, 0, 3) === 'cli') {
  201. $ret = $this->__get('LOG_DIR') . DIRECTORY_SEPARATOR . 'cli-php';
  202. } elseif ((\substr(PHP_SAPI, 0, 3) === 'cgi')) {
  203. $ret = $this->__get('LOG_DIR') . DIRECTORY_SEPARATOR . 'cgi-php';
  204. } else {
  205. $ret = $this->__get('LOG_DIR') . DIRECTORY_SEPARATOR . 'php';
  206. }
  207. $ret .= LOG_PREFIX . '_' . date('m-d-Y') . '.log';
  208. break;
  209. case 'POINTS':
  210. if (!array_key_exists('POINTS', $this->sections)) {
  211. $this->sections['POINTS'] = new PointsSection($this->getSection('POINTS'));
  212. }
  213. $ret = $this->sections['POINTS'];
  214. break;
  215. case 'MYCOLLECTIONS':
  216. if (!array_key_exists('MYCOLLECTIONS', $this->sections)) {
  217. $this->sections['MYCOLLECTIONS'] = new MycollectionsSection($this->getSection('MYCOLLECTIONS'));
  218. }
  219. $ret = $this->sections['MYCOLLECTIONS'];
  220. break;
  221. default:
  222. $ret = $aConstants[$name];
  223. break;
  224. }
  225. return $ret;
  226. }
  227. /**
  228. * Magic method to get
  229. * a value of config param
  230. * from ini array's CONSTANTS section
  231. *
  232. * This is how other objects get values
  233. * from this object
  234. * most of the times
  235. *
  236. * @return string a value of $name
  237. *
  238. * @param string $name
  239. *
  240. * @throws LampcmsIniException if $name
  241. * does not exist as a key in this->aIni
  242. *
  243. */
  244. public function __get($name)
  245. {
  246. return $this->getVar($name);
  247. }
  248. public function __set($name, $val)
  249. {
  250. throw new IniException('Not allowed to set value this way');
  251. }
  252. /**
  253. *
  254. * @param string $name name of section in !config.ini file
  255. *
  256. * @throws \Lampcms\IniException
  257. * @return array associative array of
  258. * param => val of all params belonging to
  259. * one section in !config.ini file
  260. */
  261. public function getSection($name)
  262. {
  263. if (!$this->offsetExists($name)) {
  264. d('no section ' . $name . ' in config file');
  265. throw new IniException('Section ' . $name . ' does not exist in config file ' . $this->iniFile);
  266. }
  267. return $this->offsetGet($name);
  268. }
  269. /**
  270. * Setter to set particular section
  271. * This is useful during unit testing
  272. * We can set the "MONGO" section with arrays
  273. * of out test database so that read/write
  274. * operations are performed only on test database
  275. * Also can set values of other sections like "TWITTER", "FACEBOOK",
  276. * "GRAVATAR" or any other section that we want to "mock" during test
  277. *
  278. * @param string $name name of section in !config.ini file
  279. *
  280. * @param array $val array of values for this section
  281. *
  282. * @return object $this
  283. */
  284. public function setSection($name, array $val)
  285. {
  286. $this->offsetSet($name, $val);
  287. return $this;
  288. }
  289. /**
  290. * Get the value of $var from section named $section
  291. *
  292. * @param string $section name of section that supposed to contain $var
  293. * @param string $var name of variable to get from section
  294. *
  295. * @return string value of $var from section name $section
  296. * @throws \Lampcms\IniException if either section does not exist
  297. * or section does not contain variable name $var
  298. */
  299. public function getSectionVar($section, $var)
  300. {
  301. $a = $this->getSection($section);
  302. if (!array_key_exists($var, $a)) {
  303. throw new IniException($var . ' does not exist in section: ' . $section);
  304. }
  305. return $a[$var];
  306. }
  307. /**
  308. * Creates and returns array of
  309. * some config params;
  310. *
  311. * This array is usually added as json object
  312. * to some of the pages that then use javascript
  313. * to get values from it.
  314. *
  315. * @return array
  316. */
  317. public function getSiteConfigArray()
  318. {
  319. $a = array();
  320. $aUriParts = $this->getSection('URI_PARTS');
  321. if ('' !== $imgSite = $aUriParts['IMAGE_SITE']) {
  322. $a['IMG_SITE'] = $imgSite;
  323. }
  324. if ('' !== $avatarSite = $aUriParts['AVATAR_IMG_SITE']) {
  325. $a['LAMPCMS_AVATAR_IMG_SITE'] = $avatarSite;
  326. }
  327. if ('' !== $cssSite = $aUriParts['CSS_SITE']) {
  328. $a['CSS_SITE'] = $cssSite;
  329. }
  330. if ('' !== $jsSite = $aUriParts['JS_SITE']) {
  331. $a['JS_SITE'] = $jsSite;
  332. }
  333. return $a;
  334. }
  335. }