source: trunk/bookmarks/import.php.txt @ 1933

Revision 1933, 4.0 KB checked in by Xiping.Wang, 9 months ago (diff)

[trunk] Fix #320 , rename import php source code

Line 
1<?
2/*
3       
4        This file is part of omCollab.
5       
6        Copyright (c) 2008 omCollab (Openmethodology Project)
7        http://www.openmethodology.org
8        http://mike2.openmethodology.org/wiki/OmCollab
9       
10        And parts copyright of Scuttle project
11        (http://sourceforge.net/projects/scuttle/, http://scuttle.org/)
12       
13        omCollab is free software: you can redistribute it and/or modify
14        it under the terms of the GNU General Public License as published by
15        the Free Software Foundation, either version 3 of the License, or
16        (at your option) any later version.
17       
18        omCollab is distributed in the hope that it will be useful,
19        but WITHOUT ANY WARRANTY; without even the implied warranty of
20        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21        GNU General Public License for more details.
22
23*/
24
25require_once('header.inc.php');
26$userservice =& ServiceFactory::getServiceInstance('UserService');
27$templateservice =& ServiceFactory::getServiceInstance('TemplateService');
28$tplVars = array();
29
30if ($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    } else {
36        $status = 2;
37    }
38
39    $depth = array();
40    $xml_parser = xml_parser_create();
41    xml_set_element_handler($xml_parser, "startElement", "endElement");
42
43    if (!($fp = fopen($_FILES['userfile']['tmp_name'], "r")))
44        die(T_("Could not open XML input"));
45
46    while ($data = fread($fp, 4096)) {
47        if (!xml_parse($xml_parser, $data, feof($fp))) {
48            die(sprintf(T_("XML error: %s at line %d"),
49                xml_error_string(xml_get_error_code($xml_parser)),
50                xml_get_current_line_number($xml_parser)));
51        }
52    }
53    xml_parser_free($xml_parser);
54    header('Location: '. createURL('bookmarks', $userinfo[$userservice->getFieldName('username')]));
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
62function 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 'TAG':
84                    $tags = strtolower($attrVal);
85                    break;
86            }
87        }
88        if ($bookmarkservice->bookmarkExists($bAddress, $userservice->getCurrentUserId())) {
89            $tplVars['error'] = T_('You have already submitted this bookmark.');
90        } else {
91            // Strangely, PHP can't work out full ISO 8601 dates, so we have to chop off the Z.
92            $bDatetime = substr($bDatetime, 0, -1);
93
94            // If bookmark claims to be from the future, set it to be now instead
95            if (strtotime($bDatetime) > time()) {
96                $bDatetime = gmdate('Y-m-d H:i:s');
97            }
98
99            if ($bookmarkservice->addBookmark($bAddress, $bTitle, $bDescription, $status, $tags, $bDatetime, true, true))
100                $tplVars['msg'] = T_('Bookmark imported.');
101            else
102                $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
103        }
104    }
105    $depth[$parser]++;
106}
107
108function endElement($parser, $name) {
109    global $depth;
110    $depth[$parser]--;
111}
112?>
Note: See TracBrowser for help on using the repository browser.