An PHP based Image Database
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
9.2 KiB

  1. <?
  2. //
  3. // Copyright (c) 2013 Mr. Gecko's Media (James Coleman). http://mrgeckosmedia.com/
  4. //
  5. // Permission to use, copy, modify, and/or distribute this software for any purpose
  6. // with or without fee is hereby granted, provided that the above copyright notice
  7. // and this permission notice appear in all copies.
  8. //
  9. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  10. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  11. // FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  12. // OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  14. // ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. //
  16. if (isset($_MGM['user']) && $_MGM['user']['level']==1 && $_MGM['path'][1]=="users") {
  17. if ($_MGM['path'][2]=="list") {
  18. $results = databaseQuery("SELECT * FROM users");
  19. while ($result = databaseFetchAssoc($results)) {
  20. $level = "Normal";
  21. if ($result['level']==0)
  22. $level = "Disabled";
  23. if ($result['level']==1)
  24. $level = "Administrator";
  25. if ($result['level']==2)
  26. $level = "Moderator";
  27. if ($result['level']==3)
  28. $level = "Tagger";
  29. ?><tr><td class="id"><?=htmlspecialchars($result['docid'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?></td><td class="email"><?=htmlspecialchars($result['email'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?></td><td class="level" value="<?=htmlspecialchars($result['level'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>"><?=$level?></td></tr><?
  30. }
  31. }
  32. if ($_MGM['path'][2]=="update") {
  33. $id = (isset($_REQUEST['id']) ? trim($_REQUEST['id']) : "");
  34. $email = (isset($_REQUEST['email']) ? trim($_REQUEST['email']) : "");
  35. $password = (isset($_REQUEST['password']) ? trim($_REQUEST['password']) : "");
  36. $level = (isset($_REQUEST['level']) ? trim($_REQUEST['level']) : "");
  37. $results = databaseQuery("SELECT * FROM users WHERE docid=%s", $id);
  38. $result = databaseFetchAssoc($results);
  39. if ($result!=NULL) {
  40. if (empty($email))
  41. $email = $result['email'];
  42. $epassword = $result['password'];
  43. if (!empty($password)) {
  44. $salt = substr(sha1(rand()),0,12);
  45. $epassword = $salt.hashPassword($password,hex2bin($salt));
  46. }
  47. if ($level=="")
  48. $level = $result['level'];
  49. databaseQuery("UPDATE users SET email=%s,password=%s,level=%s WHERE docid=%s", $email, $epassword, $level, $id);
  50. }
  51. }
  52. if ($_MGM['path'][2]=="create") {
  53. $email = (isset($_REQUEST['email']) ? trim($_REQUEST['email']) : "");
  54. $password = (isset($_REQUEST['password']) ? trim($_REQUEST['password']) : "");
  55. $level = (isset($_REQUEST['level']) ? trim($_REQUEST['level']) : "");
  56. if (!empty($email) && !empty($level)) {
  57. $salt = substr(sha1(rand()),0,12);
  58. $epassword = $salt.hashPassword($password,hex2bin($salt));
  59. databaseQuery("INSERT INTO users (email, password, time, level) VALUES (%s,%s,%d,%s)", $email, $epassword, $_MGM['time'], $level);
  60. }
  61. }
  62. exit();
  63. }
  64. if (isset($_MGM['user']) && $_MGM['path'][1]=="save_tags") {
  65. $hash = (isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : "");
  66. $tags = (isset($_REQUEST['tags']) ? trim($_REQUEST['tags']) : "");
  67. $results = databaseQuery("SELECT * FROM images WHERE hash=%s", $hash);
  68. $result = databaseFetchAssoc($results);
  69. if ($result!=NULL) {
  70. if ($_MGM['user']['level']<=3 || $_MGM['user']['docid']==$result['user_id'])
  71. databaseQuery("UPDATE images SET tags=%s WHERE hash=%s", $tags, $hash);
  72. }
  73. exit();
  74. }
  75. if (isset($_MGM['user']) && $_MGM['user']['level']<=2 && $_MGM['path'][1]=="delete") {
  76. $hash = (isset($_REQUEST['hash']) ? trim($_REQUEST['hash']) : "");
  77. $results = databaseQuery("SELECT * FROM images WHERE hash=%s", $hash);
  78. $result = databaseFetchAssoc($results);
  79. if ($result!=NULL) {
  80. unlink("./data/".$result['hash'].".".$result['extension']);
  81. unlink("./thumbs/".$result['hash'].".".$result['extension']);
  82. databaseQuery("DELETE FROM images WHERE hash=%s", $hash);
  83. }
  84. exit();
  85. }
  86. if (isset($_MGM['user']) && $_MGM['path'][1]=="tagless") {
  87. $results = NULL;
  88. if ($_MGM['user']['level']<=3)
  89. $results = databaseQuery("SELECT * FROM images WHERE tags='' ORDER BY time ASC LIMIT 1");
  90. else
  91. $results = databaseQuery("SELECT * FROM images WHERE tags='' AND user=%s ORDER BY time ASC LIMIT 1", $_MGM['user']['docid']);
  92. $result = databaseFetchAssoc($results);
  93. if ($result!=NULL) {
  94. ?>
  95. <span id="image" hash="<?=htmlspecialchars($result['hash'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" user="<?=htmlspecialchars($result['user_id'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" extension="<?=htmlspecialchars($result['extension'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" tags="<?=htmlspecialchars($result['tags'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" image_width="<?=htmlspecialchars($result['width'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" image_height="<?=htmlspecialchars($result['height'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" file_size="<?=htmlspecialchars($result['file_size'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" time="<?=htmlspecialchars($result['time'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" original="<?=generateURL("data/".htmlspecialchars($result['hash'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true).".".htmlspecialchars($result['extension'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true))?>"></span>
  96. <?
  97. }
  98. exit();
  99. }
  100. if ($_MGM['path'][1]=="hash") {
  101. $results = databaseQuery("SELECT * FROM images WHERE hash like %s ORDER BY time ASC LIMIT 1", $_MGM['path'][2]);
  102. $result = databaseFetchAssoc($results);
  103. if ($result!=NULL) {
  104. ?>
  105. <span id="image" hash="<?=htmlspecialchars($result['hash'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" user="<?=htmlspecialchars($result['user_id'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" extension="<?=htmlspecialchars($result['extension'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" tags="<?=htmlspecialchars($result['tags'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" image_width="<?=htmlspecialchars($result['width'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" image_height="<?=htmlspecialchars($result['height'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" file_size="<?=htmlspecialchars($result['file_size'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" time="<?=htmlspecialchars($result['time'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" original="<?=generateURL("data/".htmlspecialchars($result['hash'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true).".".htmlspecialchars($result['extension'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true))?>"></span>
  106. <?
  107. }
  108. exit();
  109. }
  110. $limit = 96;
  111. $page = (intval($_MGM['path'][1])==0 || empty($_MGM['path'][1]) ? 1 : intval($_MGM['path'][1]))-1;
  112. $offset = $limit*$page;
  113. $filter = (isset($_REQUEST['filter']) ? trim($_REQUEST['filter']) : "");
  114. $results = NULL;
  115. if (!empty($filter)) {
  116. $startTime = 0;
  117. $endTime = 0;
  118. if (preg_match("/(?:[0-9]{4}(?:\s|-)[0-9]{2}(?:\s|-)[0-9]{2}|[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}|yesterday|today)$/", $filter)) {
  119. $startTime = strtotime($filter);
  120. $endTime = strtotime($filter." 23:59:59");
  121. }
  122. if (preg_match("/From:(?:|\s)([a-z0-9\/\s-:]+)\sTo:(?:|\s)([a-z0-9\/\s-:]+)/i", $filter, $matches)) {
  123. $startTime = strtotime($matches[1]);
  124. $endTime = strtotime($matches[2]);
  125. }
  126. if ($startTime!=0 && $endTime!=0) {
  127. $results = databaseQuery("SELECT * FROM images WHERE time>=%s AND time<=%s ORDER BY time ASC LIMIT %d,%d", $startTime, $endTime, $offset, $limit);
  128. } else {
  129. $results = databaseQuery("SELECT * FROM images WHERE images MATCH %s LIMIT %d,%d", $filter, $offset, $limit);
  130. }
  131. } else {
  132. $results = databaseQuery("SELECT * FROM images ORDER BY time DESC LIMIT %d,%d", $offset, $limit);
  133. }
  134. ?><div id="content"><?
  135. $count = 0;
  136. while ($result = databaseFetchAssoc($results)) {
  137. ?>
  138. <span class="image" hash="<?=htmlspecialchars($result['hash'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" user="<?=htmlspecialchars($result['user_id'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" extension="<?=htmlspecialchars($result['extension'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" tags="<?=htmlspecialchars($result['tags'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" image_width="<?=htmlspecialchars($result['width'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" image_height="<?=htmlspecialchars($result['height'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" file_size="<?=htmlspecialchars($result['file_size'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" time="<?=htmlspecialchars($result['time'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" original="<?=generateURL("data/".htmlspecialchars($result['hash'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true).".".htmlspecialchars($result['extension'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true))?>"><img src="<?=generateURL("thumbs/".htmlspecialchars($result['hash'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true).".".htmlspecialchars($result['extension'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true))?>" width="<?=htmlspecialchars($result['thumb_width'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" height="<?=htmlspecialchars($result['thumb_height'], ENT_COMPAT | ENT_HTML401, 'UTF-8', true)?>" /></span>
  139. <?
  140. $count++;
  141. }
  142. ?>
  143. </div>
  144. <div id="count"><?=$count?></div>
  145. <div id="limit"><?=$limit?></div>
  146. <div id="page"><?=$page?></div>
  147. <div id="offset"><?=$offset?></div>
  148. <div id="next_page"><?=($count==$limit ? $page+2 : "")?></div>
  149. <?
  150. exit();
  151. ?>