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

Benjamin Mako Hill || Want to submit a patch?