Import Delicious private settings
[scuttle] / import.php
1 <?php
2 /***************************************************************************
3 Copyright (c) 2004 - 2006 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 $userservice     =& ServiceFactory::getServiceInstance('UserService');
24 $templateservice =& ServiceFactory::getServiceInstance('TemplateService');
25
26 $tplVars = array();
27
28 if ($userservice->isLoggedOn() && sizeof($_FILES) > 0 && $_FILES['userfile']['size'] > 0) {
29     $userinfo = $userservice->getCurrentUser();
30
31     if (isset($_POST['status']) && is_numeric($_POST['status'])) {
32       $status = intval($_POST['status']);
33     }
34     else {
35       $status = 2;
36     }
37
38     $depth = array();
39     $xml_parser = xml_parser_create();
40     xml_set_element_handler($xml_parser, "startElement", "endElement");
41
42     if (!($fp = fopen($_FILES['userfile']['tmp_name'], "r")))
43         die(T_("Could not open XML input"));
44
45     while ($data = fread($fp, 4096)) {
46         if (!xml_parse($xml_parser, $data, feof($fp))) {
47             die(sprintf(T_("XML error: %s at line %d"),
48                 xml_error_string(xml_get_error_code($xml_parser)),
49                 xml_get_current_line_number($xml_parser)));
50         }
51     }
52     xml_parser_free($xml_parser);
53     header('Location: '. createURL('bookmarks', $userinfo[$userservice->getFieldName('username')]));
54 }
55 else {
56   $templatename = 'importDelicious.tpl';
57   $tplVars['subtitle'] = T_('Import Bookmarks from del.icio.us');
58   $tplVars['formaction']  = createURL('import');
59   $templateservice->loadTemplate($templatename, $tplVars);
60 }
61
62 function startElement($parser, $name, $attrs) {
63     global $depth, $status, $tplVars, $userservice;
64
65     $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
66     $userservice =& ServiceFactory::getServiceInstance('UserService');
67
68     if ($name == 'POST') {
69         while(list($attrTitle, $attrVal) = each($attrs)) {
70             switch ($attrTitle) {
71                 case 'HREF':
72                     $bAddress = $attrVal;
73                     break;
74                 case 'DESCRIPTION':
75                     $bTitle = $attrVal;
76                     break;
77                 case 'EXTENDED':
78                     $bDescription = $attrVal;
79                     break;
80                 case 'TIME':
81                     $bDatetime = $attrVal;
82                     break;
83                 case 'PRIVATE':
84                     $bStatus = (strcasecmp('yes', $attrVal) == 0) ? 2 : $status ;
85                     break;
86                 case 'TAG':
87                     $tags = strtolower($attrVal);
88                     break;
89             }
90         }
91         if ($bookmarkservice->bookmarkExists($bAddress, $userservice->getCurrentUserId())) {
92             $tplVars['error'] = T_('You have already submitted this bookmark.');
93         } else {
94             // Strangely, PHP can't work out full ISO 8601 dates, so we have to chop off the Z.
95             $bDatetime = substr($bDatetime, 0, -1);
96
97             // If bookmark claims to be from the future, set it to be now instead
98             if (strtotime($bDatetime) > time()) {
99                 $bDatetime = gmdate('Y-m-d H:i:s');
100             }
101
102             if ($bookmarkservice->addBookmark($bAddress, $bTitle, $bDescription, $bStatus, $tags, $bDatetime, true, true))
103                 $tplVars['msg'] = T_('Bookmark imported.');
104             else
105                 $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
106         }
107     }
108     $depth[$parser]++;
109 }
110
111 function endElement($parser, $name) {
112     global $depth;
113     $depth[$parser]--;
114 }
115 ?>

Benjamin Mako Hill || Want to submit a patch?