1
0

functions.common.settings.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /* $Id: functions.common.settings.php 2980 2007-05-11 21:21:32Z warion $ */
  3. /*******************************************************************************
  4. LICENSE
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License (GPL)
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. To read the license please visit http://www.gnu.org/copyleft/gpl.html
  14. *******************************************************************************/
  15. /**
  16. * load Settings
  17. *
  18. * @param $dbTable
  19. */
  20. function loadSettings($dbTable) {
  21. global $cfg, $db;
  22. // pull the config params out of the db
  23. $sql = "SELECT tf_key, tf_value FROM ".$dbTable;
  24. $recordset = $db->Execute($sql);
  25. if ($db->ErrorNo() != 0) dbError($sql);
  26. while(list($key, $value) = $recordset->FetchRow()) {
  27. $tmpValue = '';
  28. if (strpos($key,"Filter") > 0)
  29. $tmpValue = unserialize($value);
  30. elseif ($key == 'searchEngineLinks')
  31. $tmpValue = unserialize($value);
  32. if (is_array($tmpValue))
  33. $value = $tmpValue;
  34. $cfg[$key] = $value;
  35. }
  36. }
  37. /**
  38. * insert Setting
  39. *
  40. * @param $dbTable
  41. * @param $key
  42. * @param $value
  43. */
  44. function insertSetting($dbTable, $key, $value) {
  45. global $cfg, $db;
  46. // flush session-cache
  47. cacheFlush();
  48. $insert_value = (is_array($value)) ? serialize($value) : $value;
  49. $sql = "INSERT INTO ".$dbTable." VALUES (".$db->qstr($key).", ".$db->qstr($insert_value).")";
  50. $db->Execute($sql);
  51. if ($db->ErrorNo() != 0) dbError($sql);
  52. // update the Config.
  53. $cfg[$key] = $value;
  54. }
  55. /**
  56. * updateSetting
  57. *
  58. * @param $dbTable
  59. * @param $key
  60. * @param $value
  61. */
  62. function updateSetting($dbTable, $key, $value) {
  63. global $cfg, $db;
  64. // flush session-cache
  65. cacheFlush();
  66. $update_value = (is_array($value)) ? serialize($value) : $value;
  67. $sql = "UPDATE ".$dbTable." SET tf_value = ".$db->qstr($update_value)." WHERE tf_key = ".$db->qstr($key);
  68. $db->Execute($sql);
  69. if ($db->ErrorNo() != 0) dbError($sql);
  70. // update the Config.
  71. $cfg[$key] = $value;
  72. }
  73. /**
  74. * save Settings
  75. *
  76. * @param $dbTable
  77. * @param $settings
  78. */
  79. function saveSettings($dbTable, $settings) {
  80. global $cfg, $db;
  81. foreach ($settings as $key => $value) {
  82. if (array_key_exists($key, $cfg)) {
  83. if (is_array($cfg[$key]) || is_array($value)) {
  84. if (serialize($cfg[$key]) != serialize($value))
  85. updateSetting($dbTable, $key, $value);
  86. } elseif ($cfg[$key] != $value) {
  87. updateSetting($dbTable, $key, $value);
  88. }
  89. } else {
  90. insertSetting($dbTable, $key, $value);
  91. }
  92. }
  93. }
  94. /*
  95. * Function for saving user Settings
  96. *
  97. * @param $uid uid of the user
  98. * @param $settings settings-array
  99. */
  100. function saveUserSettings($uid, $settings) {
  101. global $cfg;
  102. // Messy - a not exists would prob work better. but would have to be done
  103. // on every key/value pair so lots of extra-statements.
  104. deleteUserSettings($uid);
  105. // load global settings + overwrite per-user settings
  106. loadSettings('tf_settings');
  107. // insert new settings
  108. foreach ($settings as $key => $value) {
  109. if (in_array($key, $cfg['validUserSettingsKeys']))
  110. insertUserSettingPair($uid, $key, $value);
  111. else
  112. AuditAction($cfg["constants"]["error"], "ILLEGAL SETTING: ".$cfg["user"]." tried to insert ".$value." for key ".$key);
  113. }
  114. // flush session-cache
  115. cacheFlush($cfg["user"]);
  116. // return
  117. return true;
  118. }
  119. /*
  120. * insert setting-key/val pair for user into db
  121. *
  122. * @param $uid uid of the user
  123. * @param $key
  124. * @param $value
  125. * @return boolean
  126. */
  127. function insertUserSettingPair($uid, $key, $value) {
  128. global $cfg, $db;
  129. $insert_value = $value;
  130. if (is_array($value)) {
  131. $insert_value = serialize($value);
  132. } else {
  133. // only insert if setting different from global settings or has changed
  134. if ($cfg[$key] == $value)
  135. return true;
  136. }
  137. $sql = "INSERT INTO tf_settings_user VALUES (".$db->qstr($uid).", ".$db->qstr($key).", ".$db->qstr($insert_value).")";
  138. $result = $db->Execute($sql);
  139. if ($db->ErrorNo() != 0) dbError($sql);
  140. // update the Config.
  141. $cfg[$key] = $value;
  142. // return
  143. return true;
  144. }
  145. /*
  146. * Function to delete saved user Settings
  147. *
  148. * @param $uid uid of the user
  149. */
  150. function deleteUserSettings($uid) {
  151. global $cfg, $db;
  152. // delete from db
  153. $sql = "DELETE FROM tf_settings_user WHERE uid = ".$db->qstr($uid);
  154. $db->Execute($sql);
  155. if ($db->ErrorNo() != 0) dbError($sql);
  156. // flush session-cache
  157. cacheFlush($cfg["user"]);
  158. // return
  159. return true;
  160. }
  161. /*
  162. * Function to delete all saved user Settings
  163. */
  164. function deleteAllUserSettings() {
  165. global $cfg, $db;
  166. // delete from db
  167. $sql = "DELETE FROM tf_settings_user";
  168. $db->Execute($sql);
  169. if ($db->ErrorNo() != 0) dbError($sql);
  170. // flush session-cache
  171. cacheFlush();
  172. // return
  173. return true;
  174. }
  175. /*
  176. * Function to load the settings for a user to global cfg-array
  177. *
  178. * @param $uid uid of the user
  179. * @return boolean
  180. */
  181. function loadUserSettingsToConfig($uid) {
  182. global $cfg, $db;
  183. // get user-settings from db and set in global cfg-array
  184. $sql = "SELECT tf_key, tf_value FROM tf_settings_user WHERE uid = ".$db->qstr($uid);
  185. $recordset = $db->Execute($sql);
  186. if ($db->ErrorNo() != 0) dbError($sql);
  187. if ((isset($recordset)) && ($recordset->NumRows() > 0)) {
  188. while(list($key, $value) = $recordset->FetchRow())
  189. $cfg[$key] = $value;
  190. }
  191. // return
  192. return true;
  193. }
  194. /**
  195. * process post-params on config-update and init settings-array
  196. *
  197. * @param $updateIndexSettings
  198. * @param $updateGoodlookinSettings
  199. * @return array with settings
  200. */
  201. function processSettingsParams($updateIndexSettings = true, $updateGoodlookinSettings = true) {
  202. // move
  203. if (isset($_POST['categorylist']))
  204. unset($_POST['categorylist']);
  205. if (isset($_POST['category']))
  206. unset($_POST['category']);
  207. // res-dir
  208. if (isset($_POST['resdirlist']))
  209. unset($_POST['resdirlist']);
  210. if (isset($_POST['resdirentry']))
  211. unset($_POST['resdirentry']);
  212. // init settings array from params
  213. // process and handle all specials and exceptions while doing this.
  214. $settings = array();
  215. // index-page
  216. if ($updateIndexSettings) {
  217. $indexPageSettingsPrefix = "index_page_settings_";
  218. $indexPageSettingsPrefixLen = strlen($indexPageSettingsPrefix);
  219. $settingsIndexPageAry = array();
  220. for ($j = 0; $j <= 11; $j++)
  221. $settingsIndexPageAry[$j] = 0;
  222. }
  223. // good-look-stats
  224. if ($updateGoodlookinSettings) {
  225. $hackStatsPrefix = "hack_goodlookstats_settings_";
  226. $hackStatsStringLen = strlen($hackStatsPrefix);
  227. $settingsHackAry = array();
  228. for ($i = 0; $i <= 5; $i++)
  229. $settingsHackAry[$i] = 0;
  230. }
  231. //
  232. foreach ($_POST as $key => $value) {
  233. if (($updateIndexSettings) && ((substr($key, 0, $hackStatsStringLen)) == $hackStatsPrefix)) {
  234. // good-look-stats
  235. $idx = intval(substr($key, -1, 1));
  236. $settingsHackAry[$idx] = ($value != "0") ? 1 : 0;
  237. } else if (($updateGoodlookinSettings) && ((substr($key, 0, $indexPageSettingsPrefixLen)) == $indexPageSettingsPrefix)) {
  238. // index-page
  239. $idx = intval(substr($key, ($indexPageSettingsPrefixLen - (strlen($key)))));
  240. $settingsIndexPageAry[$idx] = ($value != "0") ? 1 : 0;
  241. } else {
  242. switch ($key) {
  243. case "path": // tf-path
  244. $settings[$key] = trim(checkDirPathString($value));
  245. break;
  246. case "docroot": // tf-docroot
  247. $settings[$key] = trim(checkDirPathString($value));
  248. break;
  249. case "move_paths": // move-hack-paths
  250. if (strlen($value) > 0) {
  251. $val = "";
  252. $dirAry = explode(":",$value);
  253. for ($idx = 0; $idx < count($dirAry); $idx++) {
  254. if ($idx > 0)
  255. $val .= ':';
  256. $val .= trim(checkDirPathString($dirAry[$idx]));
  257. }
  258. $settings[$key] = trim($val);
  259. } else {
  260. $settings[$key] = "";
  261. }
  262. break;
  263. default: // "normal" key-val-pair
  264. $settings[$key] = $value;
  265. }
  266. }
  267. }
  268. // index-page
  269. if ($updateIndexSettings)
  270. $settings['index_page_settings'] = convertArrayToInteger($settingsIndexPageAry);
  271. // good-look-stats
  272. if ($updateGoodlookinSettings)
  273. $settings['hack_goodlookstats_settings'] = convertArrayToByte($settingsHackAry);
  274. // return
  275. return $settings;
  276. }
  277. ?>