import.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. require_once ("./header.php");
  3. logged_in_only ();
  4. ?>
  5. <h1 id="caption">Import Bookmarks</h1>
  6. <!-- Wrapper starts here. -->
  7. <div style="min-width: <?php echo 230 + $settings['column_width_folder']; ?>px;">
  8. <!-- Menu starts here. -->
  9. <div id="menu">
  10. <h2 class="nav">Bookmarks</h2>
  11. <ul class="nav">
  12. <li><a href="./index.php">My Bookmarks</a></li>
  13. <li><a href="./shared.php">Shared Bookmarks</a></li>
  14. </ul>
  15. <h2 class="nav">Tools</h2>
  16. <ul class="nav">
  17. <?php if (admin_only ()) { ?>
  18. <li><a href="./admin.php">Admin</a></li>
  19. <?php } ?>
  20. <li><a href="./import.php">Import</a></li>
  21. <li><a href="./export.php">Export</a></li>
  22. <li><a href="./sidebar.php">View as Sidebar</a></li>
  23. <li><a href="./settings.php">Settings</a></li>
  24. <li><a href="./index.php?logout=1">Logout</a></li>
  25. </ul>
  26. <!-- Menu ends here. -->
  27. </div>
  28. <!-- Main content starts here. -->
  29. <div id="main">
  30. <div id="content">
  31. <?php
  32. if (!isset ($_FILES['importfile']['tmp_name']) || $_FILES['importfile']['tmp_name'] == null){
  33. # get the browser type for default setting below if possible
  34. if( preg_match ("/opera/i", $_SERVER['HTTP_USER_AGENT'])){
  35. $default_browser = "opera";
  36. }
  37. else{
  38. $default_browser = "netscape";
  39. }
  40. ?>
  41. <form enctype="multipart/form-data" action="<?php echo $_SERVER['SCRIPT_NAME'];?>" method="post">
  42. <table border="0">
  43. <tr>
  44. <td>
  45. from Browser:
  46. </td>
  47. <td>
  48. <select name="browser">
  49. <option value="netscape"<?php if ($default_browser=="netscape"){echo " selected";} ?>>Netscape / Mozilla / IE</option>
  50. <option value="opera"<?php if ($default_browser=="opera"){echo " selected";} ?>>Opera .adr</option>
  51. </select>
  52. </td>
  53. </tr>
  54. <tr>
  55. <td>
  56. select File:
  57. </td>
  58. <td>
  59. <input type="file" name="importfile">
  60. </td>
  61. </tr>
  62. <tr>
  63. <td>Character encoding:</td>
  64. <td>
  65. <select name="charset">
  66. <?php
  67. $charsets = return_charsets ();
  68. foreach ($charsets as $value) {
  69. $selected = '';
  70. if ($value == 'UTF-8') {$selected = ' selected';}
  71. echo '<option value="'.$value.'"'.$selected.'>'.$value.'</option>' . "\n";
  72. }
  73. ?>
  74. </select>
  75. </td>
  76. </tr>
  77. <tr>
  78. <td>Make them:</td>
  79. <td>
  80. <select name="public">
  81. <option value="1">public</option>
  82. <option value="0" selected>private</option>
  83. </select>
  84. </td>
  85. </tr>
  86. <tr>
  87. <td valign="top">
  88. Destination Folder:
  89. </td>
  90. <td>
  91. <div style="width:<?php echo $column_width_folder; ?>; height:350px; overflow:auto;">
  92. <?php
  93. require_once (ABSOLUTE_PATH . "folders.php");
  94. $tree = new folder;
  95. $tree->make_tree (0);
  96. $tree->print_tree ();
  97. ?>
  98. </div>
  99. </td>
  100. </tr>
  101. <tr>
  102. <td>
  103. <p><input type="button" value=" New Folder " onClick="self.location.href='javascript:foldernew(<?php echo $folderid; ?>)'"></p>
  104. <input type="hidden" name="parentfolder" value="<?php echo $folderid; ?>">
  105. <input type="submit" value="Import">
  106. <input type="button" value=" Cancel " onClick="self.location.href='./index.php'">
  107. </td>
  108. <td>
  109. </td>
  110. </tr>
  111. </table>
  112. </form>
  113. <?php
  114. }
  115. else{
  116. if(!isset($_POST['browser']) || $_POST['browser'] == ""){
  117. message ("no browser selected");
  118. }
  119. $parentfolder = set_post_parentfolder ();
  120. $import = new import;
  121. if ($_POST['browser'] == "opera") {
  122. $import->import_opera ();
  123. }
  124. else if ($_POST['browser'] == "netscape") {
  125. $import->import_netscape ($mysql);
  126. }
  127. echo "$import->count_folders folders and $import->count_bookmarks bookmarks imported.<br>\n";
  128. echo '<a href="./index.php">My Bookmarks</a>';
  129. }
  130. ?>
  131. </div>
  132. <!-- Main content ends here. -->
  133. </div>
  134. <!-- Wrapper ends here. -->
  135. </div>
  136. <?php
  137. class import {
  138. function import () {
  139. global $username, $parentfolder, $mysql;
  140. # open the importfile
  141. $this->fp = fopen ($_FILES['importfile']['tmp_name'], "r");
  142. if ($this->fp == null){
  143. message ("Failed to open file");
  144. }
  145. $this->charset = set_post_charset ();
  146. $this->public = set_post_bool_var ("public", false);
  147. $this->count_folders = 0;
  148. $this->count_bookmarks = 0;
  149. $this->username = $username;
  150. $this->parent_folder = $parentfolder;
  151. $this->current_folder = $this->parent_folder;
  152. $this->folder_depth = array ();
  153. $this->mysql = $mysql;
  154. }
  155. function import_opera () {
  156. while (!feof ($this->fp)) {
  157. $line = trim (fgets ($this->fp, 4096));
  158. # a folder has been found
  159. if ($line == "#FOLDER") {
  160. $item = "Folder";
  161. }
  162. # a bookmark has been found
  163. else if ($line == "#URL") {
  164. $item = "Bookmark";
  165. }
  166. # if a line starts with NAME= ...
  167. else if (substr ($line, 0, strlen("NAME=")) == "NAME=") {
  168. $line = substr ($line, strlen ("NAME="));
  169. # ... depending on the value of "$item" we assign the name to
  170. # either folder or bookmark.
  171. if ($item == "Folder") {
  172. $this->name_folder = input_validation ($line, $this->charset);
  173. }
  174. else if ($item == "Bookmark") {
  175. $this->name_bookmark = input_validation ($line, $this->charset);
  176. }
  177. }
  178. # only bookmarks can have a description or/and an url.
  179. else if (substr ($line, 0, strlen ("DESCRIPTION=")) == "DESCRIPTION=") {
  180. $this->description = substr (input_validation ($line, $this->charset), strlen ("DESCRIPTION="));
  181. }
  182. else if (substr ($line, 0, strlen ("URL=")) == "URL="){
  183. $this->url = substr (input_validation ($line, $this->charset), strlen ("URL="));
  184. }
  185. # process the corresponding item, if there is an empty line found
  186. else if ($line == "") {
  187. if (isset ($item) && $item == "Folder") {
  188. $this->folder_new ();
  189. unset ($item);
  190. }
  191. else if (isset ($item) && $item == "Bookmark") {
  192. $this->bookmark_new ();
  193. unset ($item);
  194. }
  195. }
  196. # this indicates, that the folder is being closed
  197. else if ($line == "-") {
  198. $this->folder_close ();
  199. }
  200. }
  201. }
  202. function import_netscape () {
  203. while (!feof ($this->fp)){
  204. $line = trim (fgets ($this->fp));
  205. # netscape seems to store html encoded values
  206. $line = html_entity_decode ($line, ENT_QUOTES, $this->charset);
  207. # a folder has been found
  208. if (preg_match("/<DT><H3/", $line)) {
  209. $this->name_folder = input_validation (preg_replace ("/^( *<DT><[^>]*>)([^<]*)(.*)/", "\\2", $line), $this->charset);
  210. $this->folder_new ();
  211. }
  212. # a bookmark has been found
  213. else if (preg_match("/<DT><A/", $line)){
  214. $this->name_bookmark = input_validation (preg_replace ("/^( *<DT><[^>]*>)([^<]*)(.*)/", "\\2", $line), $this->charset);
  215. $this->url = input_validation (preg_replace ("/([^H]*HREF=\")([^\"]*)(\".*)/", "\\2", $line), $this->charset);
  216. $this->bookmark_new ();
  217. $insert_id = mysqli_insert_id ($this->mysql->link);
  218. }
  219. # this is a description. it is only being saved
  220. # if a bookmark has been saved previously
  221. else if (preg_match("/<DD>*/", $line)) {
  222. if (isset ($insert_id)) {
  223. $this->description = input_validation (preg_replace ("/^( *<DD>)(.*)/", "\\2", $line), $this->charset);
  224. $query = sprintf ("UPDATE bookmark SET description='%s' WHERE id='%d' and user='%s'",
  225. $this->mysql->escape ($this->description),
  226. $this->mysql->escape ($insert_id),
  227. $this->mysql->escape ($this->username));
  228. @$this->mysql->query ($query);
  229. unset ($this->description);
  230. unset ($insert_id);
  231. }
  232. }
  233. # this indicates, that the folder is being closed
  234. else if ($line == "</DL><p>") {
  235. $this->folder_close ();
  236. }
  237. }
  238. }
  239. function folder_new () {
  240. if (!isset ($this->name_folder)) {
  241. $this->name_folder == "";
  242. }
  243. $query = sprintf ("INSERT INTO folder (childof, name, user, public) values ('%d', '%s', '%s', '%d')",
  244. $this->mysql->escape ($this->current_folder),
  245. $this->mysql->escape ($this->name_folder),
  246. $this->mysql->escape ($this->username),
  247. $this->mysql->escape ($this->public));
  248. if ($this->mysql->query ($query)) {
  249. $this->current_folder = mysqli_insert_id ($this->mysql->link);
  250. array_push ($this->folder_depth, $this->current_folder);
  251. unset ($this->name_folder);
  252. $this->count_folders++;
  253. }
  254. else {
  255. message ($this->mysql->error);
  256. }
  257. }
  258. function bookmark_new () {
  259. if (!isset ($this->name_bookmark)) {
  260. $this->name_bookmark = "";
  261. }
  262. if (!isset ($this->url)) {
  263. $this->url = "";
  264. }
  265. if (!isset ($this->description)) {
  266. $this->description = "";
  267. }
  268. $query = sprintf ("INSERT INTO bookmark (user, title, url, description, childof, public)
  269. values ('%s', '%s', '%s', '%s', '%d', '%d')",
  270. $this->mysql->escape ($this->username),
  271. $this->mysql->escape ($this->name_bookmark),
  272. $this->mysql->escape ($this->url),
  273. $this->mysql->escape ($this->description),
  274. $this->mysql->escape ($this->current_folder),
  275. $this->mysql->escape ($this->public));
  276. if ($this->mysql->query ($query)) {
  277. unset ($this->name_bookmark, $this->url, $this->description);
  278. $this->count_bookmarks++;
  279. }
  280. else {
  281. message ($this->mysql->error);
  282. }
  283. }
  284. function folder_close () {
  285. if (count ($this->folder_depth) <= 1) {
  286. $this->folder_depth = array ();
  287. $this->current_folder = $this->parent_folder;
  288. }
  289. else{
  290. # remove the last folder from the folder history
  291. unset ($this->folder_depth[count ($this->folder_depth) - 1]);
  292. $this->folder_depth = array_values ($this->folder_depth);
  293. # set the last folder to the current folder
  294. $this->current_folder = $this->folder_depth[count ($this->folder_depth) - 1];
  295. }
  296. }
  297. }
  298. print_footer ();
  299. require_once (ABSOLUTE_PATH . "footer.php");
  300. ?>