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

/lib/class-options.php

https://github.com/hsrai/GNDEC-SMS-Service
PHP | 265 lines | 175 code | 37 blank | 53 comment | 33 complexity | c308327f51eac78722e2a5c7cb39c65e MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*************************************************************
  3. * THE ADDRESS BOOK : version 1.04
  4. *
  5. * lib/class-options.php
  6. * Object: retrieve and set global or user options
  7. *
  8. *************************************************************/
  9. class Options {
  10. // DECLARE OPTION VARIABLES
  11. var $bdayInterval;
  12. var $bdayDisplay;
  13. var $displayAsPopup;
  14. var $useMailScript;
  15. var $picAlwaysDisplay;
  16. var $picWidth;
  17. var $picHeight;
  18. var $picDupeMode;
  19. var $picAllowUpload;
  20. var $modifyTime; // not currently in use; reserved for future use
  21. var $msgLogin;
  22. var $msgWelcome;
  23. var $countryDefault;
  24. var $allowUserReg;
  25. var $eMailAdmin;
  26. var $requireLogin;
  27. var $language;
  28. var $defaultLetter; // test
  29. var $limitEntries; // test
  30. // DECLARE OTHER VARIABLES
  31. var $global_options;
  32. var $user_options;
  33. var $message;
  34. // CONSTRUCTOR FUNCTION
  35. function Options() {
  36. $this->get();
  37. }
  38. function get() {
  39. // This function retrieves global options first. Then, it retrieves user options
  40. // if a user name is available, which will overwrite certain global options.
  41. $this->set_global();
  42. if ((isset($_SESSION['username'])) && ($_SESSION['username'] != '@auth_off')) {
  43. $this->set_user();
  44. }
  45. }
  46. function set_global() {
  47. // This function restores all options to the administrator-specified global settings.
  48. // Call this function when you need to ignore the user-specified settings.
  49. // Note: If you do not call this function, you can still obtain global settings
  50. // directly using the $this->global_options variable.
  51. global $db_link;
  52. $this->global_options = mysql_fetch_array(mysql_query("SELECT * FROM " . TABLE_OPTIONS . " LIMIT 1", $db_link))
  53. or die(reportScriptError("Unable to retrieve global options."));
  54. $this->bdayInterval = $this->global_options['bdayInterval'];
  55. $this->bdayDisplay = $this->global_options['bdayDisplay'];
  56. $this->displayAsPopup = $this->global_options['displayAsPopup'];
  57. $this->useMailScript = $this->global_options['useMailScript'];
  58. $this->picAlwaysDisplay = $this->global_options['picAlwaysDisplay'];
  59. $this->picWidth = $this->global_options['picWidth'];
  60. $this->picHeight = $this->global_options['picHeight'];
  61. $this->picDupeMode = $this->global_options['picDupeMode'];
  62. $this->picAllowUpload = $this->global_options['picAllowUpload'];
  63. $this->modifyTime = $this->global_options['modifyTime'];
  64. $this->msgLogin = stripslashes( $this->global_options['msgLogin'] );
  65. $this->msgWelcome = stripslashes( $this->global_options['msgWelcome'] );
  66. $this->countryDefault = $this->global_options['countryDefault'];
  67. $this->allowUserReg = $this->global_options['allowUserReg'];
  68. $this->eMailAdmin = $this->global_options['eMailAdmin'];
  69. $this->requireLogin = $this->global_options['requireLogin'];
  70. $this->language = $this->load_lang($this->global_options['language']);
  71. $this->defaultLetter = $this->global_options['defaultLetter'];
  72. $this->limitEntries = $this->global_options['limitEntries'];
  73. }
  74. function set_user() {
  75. // This function overrides admin-specified options with user options.
  76. // Call this function if you need to restore the user settings after resetting
  77. // to global settings.
  78. // Note: If you do not call this function, you can still obtain the user settings
  79. // directly using the $this->user_options variable.
  80. global $db_link;
  81. $this->user_options = mysql_fetch_array(mysql_query("SELECT * FROM " . TABLE_USERS . " WHERE username='" . $_SESSION['username'] . "' LIMIT 1", $db_link))
  82. or die(reportScriptError("Unable to retrieve user options."));
  83. if (!is_null($this->user_options['bdayInterval'])) $this->bdayInterval = $this->user_options['bdayInterval'];
  84. if (!is_null($this->user_options['bdayDisplay'])) $this->bdayDisplay = $this->user_options['bdayDisplay'];
  85. if (!is_null($this->user_options['displayAsPopup'])) $this->displayAsPopup = $this->user_options['displayAsPopup'];
  86. if (!is_null($this->user_options['useMailScript'])) $this->useMailScript = $this->user_options['useMailScript'];
  87. if (!is_null($this->user_options['language'])) $this->language = $this->load_lang($this->user_options['language']);
  88. if (!is_null($this->user_options['defaultLetter'])) $this->defaultLetter = $this->user_options['defaultLetter'];
  89. if (!is_null($this->user_options['limitEntries'])) $this->limitEntries = $this->user_options['limitEntries'];
  90. }
  91. function save_global() {
  92. // This function saves global settings to the database, in the options table.
  93. // It assumes that the options have already been placed in the $_POST superglobal.
  94. global $db_link;
  95. global $lang;
  96. // CHECK NUMERICAL INPUT
  97. // This is DIFFERENT from the previous implemenation (TAB 1.03 and earlier)
  98. // where empty or faulty information resulted in resetting the value to a
  99. // hard-coded default value. Here, it will check if the $_POST value is valid,
  100. // and if so, it will overwrite the existing setting. Otherwise the original
  101. // value (whatever it is) is retained.
  102. if (($_POST['bdayInterval'] > 0) && is_numeric($_POST['bdayInterval'])) $this->bdayInterval = $_POST['bdayInterval'];
  103. if (($_POST['picWidth'] > 0) && is_numeric($_POST['picWidth'])) $this->picWidth = $_POST['picWidth'];
  104. if (($_POST['picHeight'] > 0) && is_numeric($_POST['picHeight'])) $this->picHeight = $_POST['picHeight'];
  105. if (($_POST['picDupeMode'] == 1) || ($_POST['picDupeMode'] == 2) || ($_POST['picDupeMode'] == 3)) $this->picDupeMode = $_POST['picDupeMode'];
  106. if (($_POST['countryDefault'])) $this->countryDefault = $_POST['countryDefault'];
  107. if (($_POST['limitEntries'] >= 0) && is_numeric($_POST['limitEntries'])) $this->limitEntries = $_POST['limitEntries'];
  108. if ($_POST['language']) $this->language = $_POST['language']; // not numerical, but the same principle applies
  109. $this->defaultLetter = (empty($_POST['defaultLetter'])) ? "" : $_POST['defaultLetter']; // if no value is sent, then turn defaultLetter off (note: off must be empty string, NOT 0 value)
  110. // CLEAN UP STRING INPUT
  111. // These are allowed to be blank. We will take these "as is" -- no checking is done.
  112. $this->msgLogin = addslashes(strip_tags(trim($_POST['msgLogin']),'<a><b><i><u><p><br>'));
  113. $this->msgWelcome = addslashes(strip_tags(trim($_POST['msgWelcome']),'<a><b><i><u><p><br>'));
  114. // CHECKBOXES
  115. // If the variable does not exist in $_POST, that means the checkbox is turned off!
  116. // Give it a value of 0 so we know what to enter into the database.
  117. // Everything else results in 1 (which should be the contents of the $_POST variable anyway, but let's be sure
  118. $this->bdayDisplay = (empty($_POST['bdayDisplay'])) ? 0 : 1;
  119. $this->displayAsPopup = (empty($_POST['displayAsPopup'])) ? 0 : 1;
  120. $this->useMailScript = (empty($_POST['useMailScript'])) ? 0 : 1;
  121. $this->picAlwaysDisplay = (empty($_POST['picAlwaysDisplay'])) ? 0 : 1;
  122. $this->picAllowUpload = (empty($_POST['picAllowUpload'])) ? 0 : 1;
  123. $this->allowUserReg = (empty($_POST['allowUserReg'])) ? 0 : 1;
  124. $this->eMailAdmin = (empty($_POST['eMailAdmin'])) ? 0 : 1;
  125. $this->requireLogin = (empty($_POST['requireLogin'])) ? 0 : 1;
  126. // CREATES THE QUERY AND UPDATES THE OPTIONS TABLE
  127. $sql = "UPDATE " . TABLE_OPTIONS . " SET
  128. bdayInterval = $this->bdayInterval,
  129. bdayDisplay = $this->bdayDisplay,
  130. displayAsPopup = $this->displayAsPopup,
  131. useMailScript = $this->useMailScript,
  132. picAlwaysDisplay = $this->picAlwaysDisplay,
  133. picWidth = $this->picWidth,
  134. picHeight = $this->picHeight,
  135. picDupeMode = $this->picDupeMode,
  136. picAllowUpload = $this->picAllowUpload,
  137. modifyTime = $this->modifyTime,
  138. msgLogin = '$this->msgLogin',
  139. msgWelcome = '$this->msgWelcome',
  140. countryDefault = '$this->countryDefault',
  141. allowUserReg = $this->allowUserReg,
  142. requireLogin = $this->requireLogin,
  143. eMailAdmin = $this->eMailAdmin,
  144. language = '$this->language',
  145. defaultLetter = '$this->defaultLetter',
  146. limitEntries = $this->limitEntries";
  147. mysql_query($sql, $db_link)
  148. or die(reportSQLError($lang['ERR_OPTIONS_NO_SAVE']));
  149. $this->get();
  150. $this->message = $lang['OPT_SAVED'];
  151. return true;
  152. }
  153. function save_user() {
  154. // This function saves user settings to the database, in the users table.
  155. // This is largely similar in function to save_global() except that there are much fewer
  156. // options to deal with. It may be better to condense the two functions into
  157. // one function so as to avoid repetition of code but we can worry about that later.
  158. global $db_link;
  159. global $lang;
  160. // CHECK INPUT
  161. // Condensed version of events from save_global().
  162. if (($_POST['bdayInterval'] > 0) && is_numeric($_POST['bdayInterval'])) $this->bdayInterval = $_POST['bdayInterval'];
  163. if (($_POST['limitEntries'] >= 0) && is_numeric($_POST['limitEntries'])) $this->limitEntries = $_POST['limitEntries'];
  164. if ($_POST['language']) $this->language = $_POST['language'];
  165. $this->defaultLetter = (empty($_POST['defaultLetter'])) ? "" : $_POST['defaultLetter'];
  166. $this->bdayDisplay = (empty($_POST['bdayDisplay'])) ? 0 : 1;
  167. $this->displayAsPopup = (empty($_POST['displayAsPopup'])) ? 0 : 1;
  168. $this->useMailScript = (empty($_POST['useMailScript'])) ? 0 : 1;
  169. // CREATES THE QUERY AND UPDATES THE OPTIONS TABLE
  170. $sql = "UPDATE " . TABLE_USERS . " SET
  171. bdayInterval = $this->bdayInterval,
  172. bdayDisplay = $this->bdayDisplay,
  173. displayAsPopup = $this->displayAsPopup,
  174. useMailScript = $this->useMailScript,
  175. language = '$this->language',
  176. defaultLetter = '$this->defaultLetter',
  177. limitEntries = $this->limitEntries
  178. WHERE username='" . $_SESSION['username'] . "'";
  179. mysql_query($sql, $db_link)
  180. or die(reportSQLError($lang['ERR_OPTIONS_NO_SAVE']));
  181. $this->get();
  182. $this->message = $lang['OPT_SAVED_USER'];
  183. return true;
  184. }
  185. function reset_user() {
  186. // This function is designed to clear the user's settings and have all option variables
  187. // set to NULL in the database. NULL means neither yes or no, and will force the
  188. // script to look to the global options table for information.
  189. global $db_link;
  190. global $lang;
  191. // QUERY
  192. $sql = "UPDATE " . TABLE_USERS . " SET
  193. bdayInterval = NULL,
  194. bdayDisplay = NULL,
  195. displayAsPopup = NULL,
  196. useMailScript = NULL,
  197. language = NULL,
  198. defaultLetter = NULL,
  199. limitEntries = NULL
  200. WHERE username='" . $_SESSION['username'] . "'";
  201. mysql_query($sql, $db_link)
  202. or die(reportSQLError($lang['ERR_OPTIONS_NO_SAVE']));
  203. // RESET MEMBER VARIABLES
  204. $this->set_global();
  205. $this->message = $lang['OPT_RESET_USER'];
  206. return true;
  207. }
  208. function load_lang($file) {
  209. global $php_ext;
  210. // The following variables are loaded from country files. Make these global scope
  211. global $lang;
  212. global $country;
  213. $fullpath = dirname($_SERVER['SCRIPT_FILENAME']) . '/' . PATH_LANGUAGES . $file . '.' . $php_ext;
  214. // This function takes the value returned by the 'language' column in global or user options table,
  215. // and checks to make sure that the file exists in the /language directory. If it exists, it loads
  216. // the language into memory. If it does not exist, it attempts to loads 'english' (the default language).
  217. if (file_exists($fullpath)) {
  218. require_once($fullpath);
  219. return $file;
  220. } else {
  221. require_once(dirname($_SERVER['SCRIPT_FILENAME']) . '/' . PATH_LANGUAGES . 'english.' . $php_ext);
  222. $this->message = $lang['OPT_LANGUAGE_MISSING'];
  223. return 'english';
  224. }
  225. }
  226. // END Options
  227. }
  228. ?>