lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. ###
  3. ### prints a message and exits the application properly
  4. ###
  5. function message ($message) {
  6. if (isset ($message)) {
  7. echo "<p>" . $message . "</p>";
  8. }
  9. require_once (ABSOLUTE_PATH . "footer.php");
  10. }
  11. /*
  12. * Checks whether the user is logged in.
  13. * Displays a link to login if not and exit application.
  14. */
  15. function logged_in_only () {
  16. if (! isset ($_SESSION['logged_in']) || ! $_SESSION['logged_in']) {
  17. global $auth;
  18. $auth->display_login_form ();
  19. require_once (ABSOLUTE_PATH . "footer.php");
  20. }
  21. }
  22. function input_validation ($data, $charset = 'UTF-8') {
  23. if (is_array ($data)) {
  24. foreach ($data as $key => $value) {
  25. $data[$key] = input_validation ($value);
  26. }
  27. }
  28. else {
  29. $data = htmlentities (trim ($data), ENT_QUOTES, $charset);
  30. }
  31. return $data;
  32. }
  33. /*
  34. * Verify some GET variables
  35. */
  36. ###
  37. ### Setting the expand variable. If empty in _GET we use the one from _SESSION if available.
  38. ### Call this function only once, otherwise some strange things will happen.
  39. ###
  40. function set_get_expand () {
  41. if (!isset ($_GET['expand'])) {
  42. if (isset ($_SESSION['expand']) && is_array ($_SESSION['expand'])) {
  43. $return = set_num_array ($_SESSION['expand']);
  44. }
  45. else {
  46. $return = array();
  47. }
  48. }
  49. else if ($_GET['expand'] == '') {
  50. $return = array();
  51. }
  52. else {
  53. $return = explode (",", $_GET['expand']);
  54. $return = set_num_array ($return);
  55. }
  56. $return = input_validation ($return);
  57. $_SESSION['expand'] = $return;
  58. return ($return);
  59. }
  60. function set_get_folderid () {
  61. if (!isset ($_GET['folderid']) || $_GET['folderid'] == '' || !is_numeric ($_GET['folderid'])) {
  62. if (isset ($_SESSION['folderid'])) {
  63. $return = $_SESSION['folderid'];
  64. }
  65. else {
  66. $return = 0;
  67. }
  68. }
  69. else {
  70. $return = $_GET['folderid'];
  71. }
  72. $return = input_validation ($return);
  73. $_SESSION['folderid'] = $return;
  74. return ($return);
  75. }
  76. ################## GET title and url are handled a bit special
  77. function set_get_title () {
  78. if (!isset ($_GET['title']) || $_GET['title'] == '') {
  79. $return = '';
  80. }
  81. else {
  82. $return = $_GET['title'];
  83. }
  84. return input_validation ($return);
  85. }
  86. function set_get_url () {
  87. if (!isset ($_GET['url']) || $_GET['url'] == '') {
  88. $return = '';
  89. }
  90. else {
  91. $return = $_GET['url'];
  92. }
  93. return input_validation ($return);
  94. }
  95. function set_session_title () {
  96. if (!isset ($_SESSION['title']) || $_SESSION['title'] == '') {
  97. $return = '';
  98. }
  99. else {
  100. $return = $_SESSION['title'];
  101. }
  102. return $return;
  103. }
  104. function set_session_url () {
  105. if (!isset ($_SESSION['url']) || $_SESSION['url'] == '') {
  106. $return = '';
  107. }
  108. else {
  109. $return = $_SESSION['url'];
  110. }
  111. return $return;
  112. }
  113. function set_title () {
  114. $get_title = set_get_title ();
  115. $session_title = set_session_title ();
  116. if ($get_title == '' && $session_title == '') {
  117. $return = '';
  118. }
  119. else if ($get_title != '') {
  120. $_SESSION['title'] = $get_title;
  121. $return = $get_title;
  122. }
  123. else if ($session_title != '') {
  124. $_SESSION['title'] = $session_title;
  125. $return = $session_title;
  126. }
  127. return $return;
  128. }
  129. function set_url () {
  130. $get_url = set_get_url ();
  131. $session_url = set_session_url ();
  132. if ($get_url == '' && $session_url == '') {
  133. $return = '';
  134. }
  135. else if ($get_url != '') {
  136. $_SESSION['url'] = $get_url;
  137. $return = $get_url;
  138. }
  139. else if ($session_url != '') {
  140. $_SESSION['url'] = $session_url;
  141. $return = $session_url;
  142. }
  143. return $return;
  144. }
  145. #############################################
  146. function set_get_noconfirm () {
  147. if (!isset ($_GET['noconfirm']) || $_GET['noconfirm'] == '') {
  148. $return = false;
  149. }
  150. else {
  151. $return = true;
  152. }
  153. return $return;
  154. }
  155. function set_get_order () {
  156. if (!isset ($_GET['order']) || $_GET['order'] == '') {
  157. $return = array ("titleasc", "title ASC");
  158. }
  159. else if ($_GET['order'] == 'datedesc') {
  160. $return = array ("datedesc", "date DESC");
  161. }
  162. else if ($_GET['order'] == 'dateasc') {
  163. $return = array ("dateasc", "date ASC");
  164. }
  165. else if ($_GET['order'] == 'titledesc') {
  166. $return = array ("titledesc", "title DESC");
  167. }
  168. else if ($_GET['order'] == 'titleasc') {
  169. $return = array ("titleasc", "title ASC");
  170. }
  171. else {
  172. $return = array ("titleasc", "title ASC");
  173. }
  174. return $return;
  175. }
  176. /*
  177. * Verify some POST variables
  178. */
  179. function set_post_childof () {
  180. if (!isset ($_POST['childof']) || $_POST['childof'] == '' || !is_numeric($_POST['childof'])) {
  181. $return = 0;
  182. }
  183. else {
  184. $return = $_POST['childof'];
  185. }
  186. return input_validation ($return);
  187. }
  188. function set_post_title () {
  189. if (!isset ($_POST['title']) || $_POST['title'] == '') {
  190. $return = '';
  191. }
  192. else {
  193. $return = $_POST['title'];
  194. }
  195. return input_validation ($return);
  196. }
  197. function set_post_url () {
  198. if (!isset ($_POST['url']) || $_POST['url'] == '') {
  199. $return = '';
  200. }
  201. else {
  202. $return = $_POST['url'];
  203. }
  204. return input_validation ($return);
  205. }
  206. function set_post_description () {
  207. if (!isset ($_POST['description']) || $_POST['description'] == '') {
  208. $return = '';
  209. }
  210. else {
  211. $return = $_POST['description'];
  212. }
  213. return input_validation ($return);
  214. }
  215. function set_post_foldername () {
  216. if (!isset ($_POST['foldername']) || $_POST['foldername'] == '') {
  217. $return = '';
  218. }
  219. else {
  220. $return = $_POST['foldername'];
  221. }
  222. return input_validation ($return);
  223. }
  224. function set_post_sourcefolder () {
  225. if (!isset ($_POST['sourcefolder']) || $_POST['sourcefolder'] == '' || !is_numeric ($_POST['sourcefolder'])) {
  226. $return = '';
  227. }
  228. else {
  229. $return = $_POST['sourcefolder'];
  230. }
  231. return input_validation ($return);
  232. }
  233. function set_post_parentfolder () {
  234. if (!isset ($_POST['parentfolder']) || $_POST['parentfolder'] == '' || !is_numeric ($_POST['parentfolder'])) {
  235. $return = 0;
  236. }
  237. else {
  238. $return = $_POST['parentfolder'];
  239. }
  240. return input_validation ($return);
  241. }
  242. function set_post_browser () {
  243. if (!isset ($_POST['browser'])) {
  244. $return = '';
  245. }
  246. else if ($_POST['browser'] == 'opera') {
  247. $return = 'opera';
  248. }
  249. else if ($_POST['browser'] == 'netscape') {
  250. $return = 'netscape';
  251. }
  252. else if ($_POST['browser'] == 'IE') {
  253. $return = 'IE';
  254. }
  255. else {
  256. $return = '';
  257. }
  258. return input_validation ($return);
  259. }
  260. #########################################################
  261. ###
  262. ###
  263. ###
  264. function return_charsets () {
  265. $charsets = array (
  266. 'ISO-8859-1',
  267. 'ISO-8859-15',
  268. 'UTF-8',
  269. 'cp866',
  270. 'cp1251',
  271. 'cp1252',
  272. 'KOI8-R',
  273. 'BIG5',
  274. 'GB2312',
  275. 'BIG5-HKSCS',
  276. 'Shift_JIS',
  277. 'EUC-JP',
  278. );
  279. return $charsets;
  280. }
  281. function set_post_charset () {
  282. $charsets = return_charsets ();
  283. if (!isset ($_POST['charset']) || $_POST['charset'] == '') {
  284. $return = 'UTF-8';
  285. }
  286. else if (in_array ($_POST['charset'], $charsets)) {
  287. $return = $_POST['charset'];
  288. }
  289. else {
  290. $return = 'UTF-8';
  291. }
  292. return $return;
  293. }
  294. function check_username ($username) {
  295. $return = false;
  296. if (isset ($username) || $username == '') {
  297. global $mysql;
  298. $query = sprintf ("SELECT COUNT(*) FROM user WHERE md5(username)=md5('%s')",
  299. $mysql->escape ($username));
  300. if ($mysql->query ($query)) {
  301. $res = mysqli_fetch_assoc ($mysql->result);
  302. if (reset($res) == 1) {
  303. $return = true;
  304. }
  305. }
  306. }
  307. return input_validation ($return);
  308. }
  309. function admin_only () {
  310. $return = false;
  311. global $mysql, $username;
  312. $query = sprintf ("SELECT COUNT(*) FROM user WHERE admin='1'
  313. AND username='%s'",
  314. $mysql->escape ($username));
  315. if ($mysql->query ($query)) {
  316. $res = mysqli_fetch_assoc ($mysql->result);
  317. if (reset($res) == "1") {
  318. $return = true;
  319. }
  320. }
  321. return input_validation ($return);
  322. }
  323. function set_get_string_var ($varname, $default = '') {
  324. if (! isset ($_GET[$varname]) || $_GET[$varname] == '') {
  325. $return = $default;
  326. }
  327. else {
  328. $return = $_GET[$varname];
  329. }
  330. return input_validation ($return);
  331. }
  332. function set_post_string_var ($varname, $default = '') {
  333. if (! isset ($_POST[$varname]) || $_POST[$varname] == '') {
  334. $return = $default;
  335. }
  336. else {
  337. $return = $_POST[$varname];
  338. }
  339. return input_validation ($return);
  340. }
  341. function set_post_num_var ($varname, $default = 0) {
  342. if (! isset ($_POST[$varname]) || $_POST[$varname] == '' || !is_numeric ($_POST[$varname])) {
  343. $return = $default;
  344. }
  345. else {
  346. $return = intval ($_POST[$varname]);
  347. }
  348. return input_validation ($return);
  349. }
  350. function set_post_bool_var ($varname, $default = true) {
  351. if (! isset ($_POST[$varname])) {
  352. $return = $default;
  353. }
  354. else if (! $_POST[$varname] ) {
  355. $return = false;
  356. }
  357. else if ($_POST[$varname] ) {
  358. $return = true;
  359. }
  360. else {
  361. $return = $default;
  362. }
  363. return $return;
  364. }
  365. function set_get_num_list ($varname) {
  366. if (!isset ($_GET[$varname]) || $_GET[$varname] == '') {
  367. $return = array ();
  368. }
  369. else {
  370. $return = set_num_array (explode ("_", $_GET[$varname]));
  371. }
  372. return input_validation ($return);
  373. }
  374. function set_post_num_list ($varname) {
  375. if (!isset ($_POST[$varname]) || $_POST[$varname] == '') {
  376. $return = array ();
  377. }
  378. else {
  379. $return = set_num_array (explode ("_", $_POST[$varname]));
  380. }
  381. return input_validation ($return);
  382. }
  383. /*
  384. * This function checks the values of each entry in an array.
  385. * It returns an array with unique and only numeric entries.
  386. */
  387. function set_num_array ($array){
  388. foreach ($array as $key => $value) {
  389. if ($value == '' || !is_numeric ($value)) {
  390. unset ($array[$key]);
  391. }
  392. }
  393. return array_unique ($array);
  394. }
  395. function print_footer () {
  396. echo '<div id="footer">';
  397. object_count();
  398. echo "<br>\n";
  399. echo '<a class="footer" href="http://www.frech.ch/online-bookmarks/" target="_blank">Online-Bookmarks</a>' . "\n";
  400. @readfile (ABSOLUTE_PATH . "VERSION");
  401. echo ' by Stefan Frech.';
  402. echo "</p>\n";
  403. }
  404. function object_count () {
  405. global $mysql, $username;
  406. $return = '';
  407. $query = sprintf ("SELECT (SELECT COUNT(*) FROM bookmark WHERE user='%s') AS bookmarks,
  408. (SELECT COUNT(*) FROM folder WHERE user='%s') AS folders",
  409. $mysql->escape ($username),
  410. $mysql->escape ($username));
  411. if ($mysql->query ($query)) {
  412. if (mysqli_num_rows ($mysql->result) == "1") {
  413. $row = mysqli_fetch_object ($mysql->result);
  414. $return = "You have $row->bookmarks Bookmarks and $row->folders Folders";
  415. }
  416. }
  417. else {
  418. $return = $mysql->error;
  419. }
  420. echo $return;
  421. }
  422. function assemble_query_string ($data) {
  423. $return = array ();
  424. foreach ($data as $key => $value) {
  425. array_push ($return, $key . "=" . $value);
  426. }
  427. return implode ($return, "&");
  428. }
  429. ?>