]> projects.mako.cc - scuttle/blob - importNetscape.php
updated readme with information on a series of bugs I know exist
[scuttle] / importNetscape.php
1 <?php
2 /***************************************************************************
3 Copyright (c) 2004 - 2010 Marcus Campbell
4 http://scuttle.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ***************************************************************************/
20
21 require_once 'header.inc.php';
22
23 $sf = new ServiceFactory();
24 $bookmarkservice =& $sf->getServiceInstance('BookmarkService');
25 $userservice     =& $sf->getServiceInstance('UserService');
26 $templateservice =& $sf->getServiceInstance('TemplateService');
27
28 $tplVars = array();
29
30 if ($userservice->isLoggedOn() && sizeof($_FILES) > 0 && $_FILES['userfile']['size'] > 0) {
31     $userinfo = $userservice->getCurrentUser();
32
33     if (isset($_POST['status']) && is_numeric($_POST['status'])) {
34       $status = intval($_POST['status']);
35     }
36     else {
37       $status = 2;
38     }
39
40     // File handle
41     $html = file_get_contents($_FILES['userfile']['tmp_name']);
42     
43     // Create link array
44     preg_match_all('/<a\s+(.*?)\s*\/*>([^<]*)/si', $html, $matches);
45     $links  = $matches[1];
46     $titles = $matches[2];
47     
48     $size = count($links);
49     for ($i = 0; $i < $size; $i++) {
50       $bTags   = '';
51       $bStatus = $status;
52
53       $attributes = preg_split('/\s+/s', $links[$i]);
54       foreach ($attributes as $attribute) {
55           $att = preg_split('/\s*=\s*/s', $attribute, 2);
56           $attrTitle = $att[0];
57           $attrVal   = str_replace('"', '&quot;', preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]));
58           switch ($attrTitle) {
59             case 'HREF':
60               $bAddress = $attrVal;
61               break;
62             case 'ADD_DATE':
63               $bDatetime = gmdate('Y-m-d H:i:s', $attrVal);
64               break;
65             case 'PRIVATE':
66               $bStatus = (intval($attrVal) == 1) ? 2 : $status;
67               break;
68             case 'TAGS':
69               $bTags = strtolower($attrVal);
70               break;
71           }
72       }
73       $bTitle = str_replace('"', '&quot;', trim($titles[$i]));
74
75       if ($bookmarkservice->bookmarkExists($bAddress, $userservice->getCurrentUserId())) {
76         $tplVars['error'] = T_('You have already submitted this bookmark.');
77       } else {
78         // If bookmark claims to be from the future, set it to be now instead
79         if (strtotime($bDatetime) > time()) {
80           $bDatetime = gmdate('Y-m-d H:i:s');
81         }
82
83         if ($bookmarkservice->addBookmark($bAddress, $bTitle, NULL, $bStatus, $bTags, $bDatetime, false, true)) {
84           $tplVars['msg'] = T_('Bookmark imported.');
85         }
86         else {
87           $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
88         }
89       }
90     }
91     header('Location: '. createURL('bookmarks', $userinfo[$userservice->getFieldName('username')]));
92 }
93 else {
94   $templatename = 'importNetscape.tpl';
95   $tplVars['subtitle']   = T_('Import Bookmarks from Browser File');
96   $tplVars['formaction'] = createURL('importNetscape');
97   $templateservice->loadTemplate($templatename, $tplVars);
98 }

Benjamin Mako Hill || Want to submit a patch?