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

Revision 1933, 3.3 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$bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
27$userservice =& ServiceFactory::getServiceInstance('UserService');
28$templateservice =& ServiceFactory::getServiceInstance('TemplateService');
29$tplVars = array();
30
31if ($userservice->isLoggedOn() && sizeof($_FILES) > 0 && $_FILES['userfile']['size'] > 0) {
32    $userinfo = $userservice->getCurrentUser();
33
34    if (isset($_POST['status']) && is_numeric($_POST['status'])) {
35        $status = intval($_POST['status']);
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        $attributes = preg_split('/\s+/s', $links[$i]);
51        foreach ($attributes as $attribute) {
52            $att = preg_split('/\s*=\s*/s', $attribute, 2);
53            $attrTitle = $att[0];
54            $attrVal = eregi_replace('"', '&quot;', preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]));
55            switch ($attrTitle) {
56                case "HREF":
57                    $bAddress = $attrVal;
58                    break;
59                case "ADD_DATE":
60                    $bDatetime = gmdate('Y-m-d H:i:s', $attrVal);
61                    break;
62            }
63        }
64        $bTitle = eregi_replace('"', '&quot;', trim($titles[$i]));
65
66        if ($bookmarkservice->bookmarkExists($bAddress, $userservice->getCurrentUserId())) {
67            $tplVars['error'] = T_('You have already submitted this bookmark.');
68        } else {
69            // If bookmark claims to be from the future, set it to be now instead
70            if (strtotime($bDatetime) > time()) {
71                $bDatetime = gmdate('Y-m-d H:i:s');
72            }
73
74            if ($bookmarkservice->addBookmark($bAddress, $bTitle, NULL, $status, NULL, $bDatetime, false, true)) {
75                $tplVars['msg'] = T_('Bookmark imported.');
76            } else {
77                $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
78            }
79        }
80    }
81    header('Location: '. createURL('bookmarks', $userinfo[$userservice->getFieldName('username')]));
82} else {
83    $templatename = 'importNetscape.tpl';
84    $tplVars['subtitle'] = T_('Import Bookmarks from Browser File');
85    $tplVars['formaction'] = createURL('importNetscape');
86    $templateservice->loadTemplate($templatename, $tplVars);
87}
88?>
Note: See TracBrowser for help on using the repository browser.