PageRenderTime 91ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/php4/i18n_v1/class.I18N.inc.php

http://flaimo-php.googlecode.com/
PHP | 265 lines | 86 code | 29 blank | 150 comment | 20 complexity | 3803efbfbbc1209c95cf942f0566118b MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. //+----------------------------------------------------------------------+
  4. //| WAMP (XP-SP1/1.3.27/4.0.12/4.3.2) |
  5. //+----------------------------------------------------------------------+
  6. //| Copyright (c) 1992-2003 Michael Wimmer |
  7. //+----------------------------------------------------------------------+
  8. //| I don't have the time to read through all the licences to find out |
  9. //| what the exactly say. But it's simple. It's free for non commercial |
  10. //| projects, but as soon as you make money with it, i want my share :-) |
  11. //| (License : Free for non-commercial use) |
  12. //+----------------------------------------------------------------------+
  13. //| Authors: Michael Wimmer <flaimo@gmx.net> |
  14. //+----------------------------------------------------------------------+
  15. //
  16. // $Id$
  17. /**
  18. * @package i18n
  19. * @category FLP
  20. */
  21. /**
  22. * Abstract class for getting ini preferences
  23. *
  24. * Tested with WAMP (XP-SP1/1.3.24/4.0.12/4.3.0)
  25. * Last change: 2003-05-04
  26. *
  27. * @desc Abstract class for getting ini preferences
  28. * @access protected
  29. * @author Michael Wimmer <flaimo@gmx.net>
  30. * @copyright Michael Wimmer
  31. * @link http://www.flaimo.com/
  32. * @global array $GLOBALS['_I18N_ini_settings']
  33. * @global array $GLOBALS['_I18N_l10n_settings']
  34. * @abstract
  35. * @example i18n_example_script.php Sample script
  36. * @package i18n
  37. * @category FLP
  38. * @version 1.061
  39. */
  40. class I18N {
  41. /*-------------------*/
  42. /* V A R I A B L E S */
  43. /*-------------------*/
  44. /**#@+
  45. * @access private
  46. */
  47. /**
  48. * name (+path) of the i18n ini file
  49. *
  50. * @desc name (+path) of the i18n ini file
  51. * @var string
  52. */
  53. var $i18n_ini_file = 'i18n_settings.ini';
  54. /**
  55. * name of the l10n ini file (without path)
  56. *
  57. * @desc name of the l10n ini file (without path)
  58. * @var string
  59. */
  60. var $l10n_ini_file = 'l10n.ini';
  61. /**
  62. * name of the global variable which holds the i18n settings
  63. *
  64. * @desc name of the global variable which holds the i18n settings
  65. * @var array
  66. */
  67. var $i18n_globalname = '_I18N_ini_settings';
  68. /**
  69. * name of the global variable which holds the l10n settings
  70. *
  71. * @desc name of the global variable which holds the l10n settings
  72. * @var array
  73. */
  74. var $l10n_globalname = '_L10N_ini_settings';
  75. /**
  76. * name of the shared memory block for the i18n settings string
  77. *
  78. * @desc name of the shared memory block for the i18n settings string
  79. */
  80. var $i18n_shm_id = 0xDEAD; // 0xDEAD, 0xf42, 0xCAFE, 0xff3
  81. /**
  82. * size of the shared memory block for the i18n settings string
  83. *
  84. * @desc size of the shared memory block for the i18n settings string
  85. * @var int
  86. */
  87. var $i18n_shm_size = 1000;
  88. /**
  89. * size of the shared memory block for each l10n settings string.
  90. *
  91. * if you have a big badwords string you might want to change this to a
  92. * bigger value. if it's to small PHP should create an error
  93. *
  94. * @desc size of the shared memory block for each l10n settings string
  95. * @var int
  96. */
  97. var $l10n_shm_size = 1000;
  98. /**
  99. * turn on/off shared memory use
  100. *
  101. * @desc turn on/off shared memory use
  102. * @var boolean
  103. */
  104. var $use_shared_mem = FALSE;
  105. /**
  106. * set to TRUE, if you have changed one of the files which wer written to shared memory to flush the memry block
  107. *
  108. * @desc set to TRUE, if you have changed one of the files which wer written to shared memory to flush the memry block
  109. * @var boolean
  110. */
  111. var $flush_sm = FALSE;
  112. /**#@-*/
  113. /*-----------------------*/
  114. /* C O N S T R U C T O R */
  115. /*-----------------------*/
  116. /**
  117. * Constructor
  118. *
  119. * Only job is to set all the variablesnames
  120. *
  121. * @desc Constructor
  122. * @return void
  123. * @access private
  124. * @since 1.056 - 2003-05-01
  125. */
  126. function I18N() {
  127. } // end constructor
  128. /*-------------------*/
  129. /* F U N C T I O N S */
  130. /*-------------------*/
  131. /**#@+
  132. * @access public
  133. */
  134. /**
  135. * converts a four-letter string to a hex code (for creating a unique ID for the shared memory blocks)
  136. *
  137. * @desc converts a four-letter string to a hex code (for creating a unique ID for the shared memory blocks)
  138. * @param string $string fourletter inputstring
  139. * @return string
  140. * @since 1.057 - 2003-05-09
  141. * @author joeldegan@yahoo.com <joeldegan@yahoo.com>
  142. */
  143. function memSegSpot($string) {
  144. $string = str_pad(str_replace('_', '', $string), 4, 'Z'); // Z = 5a
  145. $out = '';
  146. for ($a = 0; $a < 4; $a++) {
  147. $out .= dechex(ord(substr($string, $a , 1))); // ord returns dec, we need hex for shared memory segments
  148. } // end for
  149. return substr(('0x' . substr($out, 2)), 0, 8); // prepend it with 0x
  150. }//end function
  151. /**
  152. * Reads the default settings from the settings file and loads it to shared memory if necessary
  153. *
  154. * @desc Reads the default settings from the settings file and loads it to shared memory if necessary
  155. * @return boolean if successful reading ini file
  156. * @since 1.053 - 2003-04-15
  157. */
  158. function readINIsettings() {
  159. if (isset($GLOBALS[$this->i18n_globalname])) {
  160. return (boolean) TRUE;
  161. } // end if
  162. if (!file_exists($this->i18n_ini_file)) {
  163. return (boolean) FALSE;
  164. } // end if
  165. if (!extension_loaded('shmop')) {
  166. @dl('shmop');
  167. } // end if
  168. /* if NOT able to use shared memory */
  169. if ($this->use_shared_mem === FALSE || !extension_loaded('shmop')) {
  170. $GLOBALS[$this->i18n_globalname] = (array) parse_ini_file($this->i18n_ini_file, TRUE);
  171. return (boolean) TRUE;
  172. } // end if
  173. /* if able to use shared memory */
  174. $shm = shmop_open($this->i18n_shm_id, 'c', 0644, $this->i18n_shm_size);
  175. $sm_content = trim(shmop_read($shm, 0, $this->i18n_shm_size));
  176. if(isset($sm_content) && strlen($sm_content) > 0) {
  177. $GLOBALS[$this->i18n_globalname] = (array) unserialize($sm_content);
  178. } else { // if nothing is there, write file to shared mem
  179. $GLOBALS[$this->i18n_globalname] = (array) parse_ini_file($this->i18n_ini_file, TRUE);
  180. $inifile = (string) serialize($GLOBALS[$this->i18n_globalname]);
  181. $shm_bytes_written = shmop_write($shm, $inifile, 0);
  182. unset($inifile);
  183. } // end if
  184. if ($this->flush_sm === TRUE) {
  185. shmop_delete($shm);
  186. } // end if
  187. shmop_close($shm);
  188. } // end function
  189. /**
  190. * Reads the default settings for numbers and dates from the settings file if necessary
  191. *
  192. * @desc Reads the default settings for numbers and dates from the settings file if necessary
  193. * @return boolean if successful reading ini file
  194. * @since 1.055 - 2003-04-22
  195. */
  196. function readL10NINIsettings($locale = 'en') {
  197. if ($this->readINIsettings() === FALSE) {
  198. return (boolean) FALSE;
  199. } // end if
  200. if (isset($GLOBALS[$this->l10n_globalname])) {
  201. return (boolean) TRUE;
  202. } // end if
  203. $languagefile_path =& $GLOBALS[$this->i18n_globalname]['Translator']['languagefile_path'];
  204. if (!file_exists($languagefile_path . '/' . $locale . '/' . $this->l10n_ini_file)) {
  205. return (boolean) FALSE;
  206. } // end if
  207. if (!extension_loaded('shmop')) {
  208. @dl('shmop');
  209. } // end if
  210. /* if NOT able to use shared memory */
  211. if ($this->use_shared_mem === FALSE || !extension_loaded('shmop')) {
  212. $GLOBALS[$this->l10n_globalname] = (array) parse_ini_file($languagefile_path . '/' . $locale . '/' . $this->l10n_ini_file, TRUE);
  213. return (boolean) TRUE;
  214. } // end if
  215. /* if able to use shared memory */
  216. $shm2_id = $this->memSegSpot(substr($locale, 0, 3) . 'L');
  217. $shm2 = shmop_open($shm2_id, 'c', 0644, $this->l10n_shm_size);
  218. $sm_content2 = trim(shmop_read($shm2, 0, $this->l10n_shm_size));
  219. if(isset($sm_content2) && strlen($sm_content2) > 0) {
  220. $GLOBALS[$this->l10n_globalname] = (array) unserialize($sm_content2);
  221. //echo '<hr>' . $sm_content2 . '<hr>';
  222. } else { // if nothing is there, write file to shared mem
  223. $GLOBALS[$this->l10n_globalname] = (array) parse_ini_file($languagefile_path . '/' . $locale . '/' . $this->l10n_ini_file, TRUE);
  224. $inifile = (string) serialize($GLOBALS[$this->l10n_globalname]); // hier vorher in global reinschreiben
  225. $shm2_bytes_written = shmop_write($shm2, $inifile, 0);
  226. unset($inifile);
  227. //echo 'l10 drinnen';
  228. } // end if
  229. if ($this->flush_sm === TRUE) {
  230. shmop_delete($shm2);
  231. } // end if
  232. shmop_close($shm2);
  233. } // end function
  234. /**#@-*/
  235. } // end class i18n
  236. ?>