folders.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. if (basename ($_SERVER['SCRIPT_NAME']) == basename (__FILE__)) {
  3. die ("no direct access allowed");
  4. }
  5. class folder {
  6. function folder ($user = false) {
  7. global $settings, $username, $folderid, $expand;
  8. $this->username = $username;
  9. $this->folderid = $folderid;
  10. $this->expand = $expand;
  11. $this->folders = array ();
  12. $this->tree = array ();
  13. $this->get_children = array ();
  14. $this->level = 0;
  15. $this->foreign_username = false;
  16. if ($user) {
  17. $this->get_shared_data ($user);
  18. }
  19. else {
  20. $this->get_user_data ();
  21. }
  22. if ($settings['simple_tree_mode']) {
  23. $this->expand = $this->get_path_to_root ($this->folderid);
  24. }
  25. # searching for invalid folderid in GET variable
  26. if ( ! array_key_exists ($this->folderid, $this->folders)) {
  27. $this->folderid = 0;
  28. }
  29. # searching for invalid expand entries
  30. foreach ($this->expand as $key => $value) {
  31. if (! array_key_exists ($value, $this->folders)) {
  32. unset ($this->expand[$key]);
  33. }
  34. }
  35. }
  36. function get_user_data () {
  37. global $mysql;
  38. $query = sprintf ("SELECT id, childof, name, public FROM folder WHERE user='%s' AND deleted!='1' ORDER BY name",
  39. $mysql->escape ($_SESSION['username']));
  40. if ($mysql->query ($query)) {
  41. while ($row = mysqli_fetch_assoc ($mysql->result)) {
  42. $this->folders[$row['id']] = $row;
  43. if (!isset ($this->children[$row['childof']])) {
  44. $this->children[$row['childof']] = array ();
  45. }
  46. array_push ($this->children[$row['childof']], $row['id']);
  47. }
  48. }
  49. else {
  50. message ($mysql->error);
  51. }
  52. }
  53. function get_shared_data ($user) {
  54. global $mysql, $username;
  55. # does the user exist in the database?
  56. if (check_username ($user)) {
  57. $this->foreign_username = $user;
  58. }
  59. else {
  60. $this->foreign_username = $username;
  61. }
  62. # get all shared folders for the given user
  63. $query = "SELECT id, childof, name, public FROM folder WHERE public='1' AND deleted!='1' AND user='$this->foreign_username' ORDER BY name";
  64. if ($mysql->query ($query)) {
  65. # make two arrays:
  66. # 1) $children containing arrays with children. the keys of these arrays are the id's of the parents
  67. # 2) $folders containing arrays with folder settings (id, childof, name, public)
  68. $shared_children = array ();
  69. while ($row = mysqli_fetch_assoc ($mysql->result)) {
  70. $this->folders[$row['id']] = $row;
  71. if (!isset ($this->children[$row['childof']])) {
  72. $this->children[$row['childof']] = array ();
  73. }
  74. array_push ($this->children[$row['childof']], $row['id']);
  75. array_push ($shared_children, $row['id']);
  76. }
  77. $this->children[0] = array ();
  78. # the childof fields of each folder with no parent is being set to 0, so it becomes a child of the root folder
  79. foreach ($this->folders as $value) {
  80. if (in_array ($value['childof'], $shared_children)) {
  81. continue;
  82. }
  83. else {
  84. array_push ($this->children[0], $value['id']);
  85. $this->folders[$value['id']]['childof'] = 0;
  86. }
  87. }
  88. }
  89. else {
  90. message ($mysql->error);
  91. }
  92. }
  93. # assembles the tree
  94. function make_tree ($id) {
  95. if (isset ($this->children)){
  96. $this->level++;
  97. if (isset ($this->children[$id])) {
  98. foreach ($this->children[$id] as $value) {
  99. array_push ($this->tree, array (
  100. 'level' => $this->level,
  101. 'id' => $value,
  102. 'name' => $this->folders[$value]['name'],
  103. 'public' => $this->folders[$value]['public'],
  104. ));
  105. # check for children
  106. $symbol = &$this->tree[count ($this->tree) - 1]['symbol'];
  107. if (isset ($this->children[$value])) {
  108. if (in_array ($value, $this->expand)) {
  109. $symbol = 'minus';
  110. $this->make_tree ($value);
  111. }
  112. else {
  113. $symbol = 'plus';
  114. }
  115. }
  116. else {
  117. $symbol = '';
  118. }
  119. }
  120. }
  121. $this->level--;
  122. }
  123. }
  124. # draws the tree
  125. function print_tree () {
  126. global $settings, $folder_opened, $folder_closed, $folder_opened_public, $folder_closed_public, $plus, $minus, $neutral;
  127. # depending on whom's bookmarks are being displayed, we set some variables differently
  128. if ($this->foreign_username) {
  129. $root_folder_name = $this->foreign_username . "'s Bookmarks";
  130. $user_var = "&amp;user=$this->foreign_username";
  131. }
  132. else {
  133. $root_folder_name = $settings['root_folder_name'];
  134. $user_var = "";
  135. }
  136. $root_folder = array (
  137. 'level' => 0,
  138. 'id' => 0,
  139. 'name' => $root_folder_name,
  140. 'symbol' => null,
  141. 'public' => 0,
  142. );
  143. array_unshift ($this->tree, $root_folder);
  144. # The top folder shows up too much on the top. Draw
  145. # a little space there.
  146. echo '<div class="foldertop"></div>' . "\n";
  147. foreach ($this->tree as $key => $value) {
  148. # this is the begining of the line that shows a folder
  149. # with the symbol (plus, minus or neutral)
  150. $spacer = '<div style="margin-left:' . $value['level'] * 20 . 'px;">';
  151. echo $spacer;
  152. if ($value['id'] == $this->folderid) {
  153. $folder_name = '<span class="active">' . $value['name'] . '</span>';
  154. if (!$this->foreign_username && $value['public']) {
  155. $folder_image = $folder_opened_public;
  156. }
  157. else {
  158. $folder_image = $folder_opened;
  159. }
  160. }
  161. else {
  162. $folder_name = $value['name'];
  163. if (!$this->foreign_username && $value['public']) {
  164. $folder_image = $folder_closed_public;
  165. }
  166. else {
  167. $folder_image = $folder_closed;
  168. }
  169. }
  170. if ($key > 5) {
  171. $ankor = "#" . $this->tree[$key - 5]['id'];
  172. }
  173. else {
  174. $ankor = "";
  175. }
  176. if ($value['symbol'] == "plus" || $value['symbol'] == "minus") {
  177. if ($value['symbol'] == "plus") {
  178. $symbol = $plus;
  179. $expand_s = $this->add_to_expand_list ($value['id']);
  180. if ($settings['fast_folder_plus']) {
  181. $expand_f = $expand_s;
  182. }
  183. else {
  184. $expand_f = $this->expand;
  185. }
  186. }
  187. else if ($value['symbol'] == "minus") {
  188. $symbol = $minus;
  189. $expand_s = $this->remove_from_expand_list ($value['id']);
  190. if ($settings['fast_folder_minus'] && $value['id'] == $this->folderid) {
  191. $expand_f = $expand_s;
  192. }
  193. else {
  194. $expand_f = $this->expand;
  195. }
  196. }
  197. if ($settings['fast_symbol']) {
  198. $folderid = $value['id'];
  199. }
  200. else {
  201. $folderid = $this->folderid;
  202. }
  203. # this prints the symbol (plus or minus) with its appropriate link
  204. echo '<a class="f" href="' . $_SERVER['SCRIPT_NAME'] . '?expand=' . implode(",", $expand_s);
  205. echo '&amp;folderid=' . $folderid . $user_var . $ankor . '">' . $symbol . '</a>';
  206. }
  207. else {
  208. $symbol = $neutral;
  209. $expand_f = $this->expand;
  210. echo $symbol;
  211. }
  212. # this prints the folder name with its appropriate link
  213. echo '<a class="f" href="' . $_SERVER['SCRIPT_NAME'] . '?expand=' . implode(",", $expand_f);
  214. echo '&amp;folderid=' . $value['id'] . $user_var . $ankor . '" name="' . $value['id'] . '">' . $folder_image . " " . $folder_name . '</a>';
  215. # and this is the end of the line
  216. echo "</div>\n";
  217. }
  218. }
  219. ###
  220. ### removes a value from the expand list
  221. ###
  222. function remove_from_expand_list ($id){
  223. $expand = $this->expand;
  224. foreach ($expand as $key => $value) {
  225. if ($value == $id) {
  226. unset ($expand[$key]);
  227. }
  228. }
  229. return $expand;
  230. }
  231. ###
  232. ### adds a value to the expand list
  233. ###
  234. function add_to_expand_list ($id){
  235. $expand = $this->expand;
  236. array_push ($expand, $id);
  237. return $expand;
  238. }
  239. ###
  240. ### returns an array containing all folder id's from
  241. ### a given folder up to the root folder
  242. ###
  243. function get_path_to_root ($id) {
  244. $path = array ();
  245. while ($id > 0) {
  246. array_push ($path, $id);
  247. if (!isset ($this->folders[$id])) {
  248. #echo "Folder Nr. $id does not have a parent";
  249. return array ();
  250. }
  251. else {
  252. $id = $this->folders[$id]['childof'];
  253. }
  254. }
  255. return $path;
  256. }
  257. ###
  258. ### prints a path
  259. ###
  260. function print_path ($id) {
  261. global $settings, $delimiter;
  262. $parents = $this->get_path_to_root ($id);
  263. $parents = array_reverse ($parents);
  264. # the following if condition has been disabled. could be enabled to
  265. # allow the "show_root_folder" function.
  266. $path = $delimiter . $settings['root_folder_name'];
  267. foreach ($parents as $value) {
  268. $path .= $delimiter . $this->folders[$value]['name'];
  269. }
  270. return $path;
  271. }
  272. ###
  273. ### returns an array containing all folder id's that
  274. ### are children from a given folder
  275. ###
  276. function get_children ($id) {
  277. if (isset ($this->children[$id])) {
  278. foreach ($this->children[$id] as $value) {
  279. array_push ($this->get_children, $value);
  280. $this->get_children ($value);
  281. }
  282. }
  283. }
  284. }
  285. ?>