f25b439b7355f3a084ef1be927483e8d38729694
[scuttle] / import.php
1 <?
2 /***************************************************************************
3 Copyright (C) 2004 - 2006 Scuttle project
4 http://sourceforge.net/projects/scuttle/
5 http://scuttle.org/
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 ***************************************************************************/
21
22 require_once('header.inc.php');
23 $userservice =& ServiceFactory::getServiceInstance('UserService');
24 $templateservice =& ServiceFactory::getServiceInstance('TemplateService');
25 $tplVars = array();
26
27 if ($userservice->isLoggedOn() && sizeof($_FILES) > 0 && $_FILES['userfile']['size'] > 0) {
28     $userinfo = $userservice->getCurrentUser();
29
30     if (isset($_POST['status']) && is_numeric($_POST['status'])) {
31         $status = intval($_POST['status']);
32     } else {
33         $status = 2;
34     }
35
36     $depth = array();
37     $xml_parser = xml_parser_create();
38     xml_set_element_handler($xml_parser, "startElement", "endElement");
39
40     if (!($fp = fopen($_FILES['userfile']['tmp_name'], "r")))
41         die(T_("Could not open XML input"));
42
43     while ($data = fread($fp, 4096)) {
44         if (!xml_parse($xml_parser, $data, feof($fp))) {
45             die(sprintf(T_("XML error: %s at line %d"),
46                 xml_error_string(xml_get_error_code($xml_parser)),
47                 xml_get_current_line_number($xml_parser)));
48         }
49     }
50     xml_parser_free($xml_parser);
51     header('Location: '. createURL('bookmarks', $userinfo[$userservice->getFieldName('username')]));
52 } else {
53     $templatename = 'importDelicious.tpl';
54     $tplVars['subtitle'] = T_('Import Bookmarks from del.icio.us');
55     $tplVars['formaction']  = createURL('import');
56     $templateservice->loadTemplate($templatename, $tplVars);
57 }
58
59 function startElement($parser, $name, $attrs) {
60     global $depth, $status, $tplVars, $userservice;
61
62     $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
63     $userservice =& ServiceFactory::getServiceInstance('UserService');
64
65     if ($name == 'POST') {
66         while(list($attrTitle, $attrVal) = each($attrs)) {
67             switch ($attrTitle) {
68                 case 'HREF':
69                     $bAddress = $attrVal;
70                     break;
71                 case 'DESCRIPTION':
72                     $bTitle = $attrVal;
73                     break;
74                 case 'EXTENDED':
75                     $bDescription = $attrVal;
76                     break;
77                 case 'TIME':
78                     $bDatetime = $attrVal;
79                     break;
80                 case 'TAG':
81                     $tags = strtolower($attrVal);
82                     break;
83             }
84         }
85         if ($bookmarkservice->bookmarkExists($bAddress, $userservice->getCurrentUserId())) {
86             $tplVars['error'] = T_('You have already submitted this bookmark.');
87         } else {
88             // Strangely, PHP can't work out full ISO 8601 dates, so we have to chop off the Z.
89             $bDatetime = substr($bDatetime, 0, -1);
90
91             // If bookmark claims to be from the future, set it to be now instead
92             if (strtotime($bDatetime) > time()) {
93                 $bDatetime = gmdate('Y-m-d H:i:s');
94             }
95
96             if ($bookmarkservice->addBookmark($bAddress, $bTitle, $bDescription, $status, $tags, $bDatetime, true, true))
97                 $tplVars['msg'] = T_('Bookmark imported.');
98             else
99                 $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
100         }
101     }
102     $depth[$parser]++;
103 }
104
105 function endElement($parser, $name) {
106     global $depth;
107     $depth[$parser]--;
108 }
109 ?>

Benjamin Mako Hill || Want to submit a patch?