Trunk, 0.7: Add patch [ 1597978 ] Missing tag rename for 0.7.2
[scuttle] / importNetscape.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 $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
24 $userservice =& ServiceFactory::getServiceInstance('UserService');
25 $templateservice =& ServiceFactory::getServiceInstance('TemplateService');
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     } else {
34         $status = 2;
35     }
36
37     // File handle
38     $html = file_get_contents($_FILES['userfile']['tmp_name']);
39     
40     // Create link array
41     preg_match_all('/<a\s+(.*?)\s*\/*>([^<]*)/si', $html, $matches);
42     $links = $matches[1];
43     $titles = $matches[2];
44     
45     $size = count($links);
46     for ($i = 0; $i < $size; $i++) {
47         $attributes = preg_split('/\s+/s', $links[$i]);
48         foreach ($attributes as $attribute) {
49             $att = preg_split('/\s*=\s*/s', $attribute, 2);
50             $attrTitle = $att[0];
51             $attrVal = eregi_replace('"', '&quot;', preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]));
52             switch ($attrTitle) {
53                 case "HREF":
54                     $bAddress = $attrVal;
55                     break;
56                 case "ADD_DATE":
57                     $bDatetime = gmdate('Y-m-d H:i:s', $attrVal);
58                     break;
59             }
60         }
61         $bTitle = eregi_replace('"', '&quot;', trim($titles[$i]));
62
63         if ($bookmarkservice->bookmarkExists($bAddress, $userservice->getCurrentUserId())) {
64             $tplVars['error'] = T_('You have already submitted this bookmark.');
65         } else {
66             // If bookmark claims to be from the future, set it to be now instead
67             if (strtotime($bDatetime) > time()) {
68                 $bDatetime = gmdate('Y-m-d H:i:s');
69             }
70
71             if ($bookmarkservice->addBookmark($bAddress, $bTitle, NULL, $status, NULL, $bDatetime, false, true)) {
72                 $tplVars['msg'] = T_('Bookmark imported.');
73             } else {
74                 $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
75             }
76         }
77     }
78     header('Location: '. createURL('bookmarks', $userinfo[$userservice->getFieldName('username')]));
79 } else {
80     $templatename = 'importNetscape.tpl';
81     $tplVars['subtitle'] = T_('Import Bookmarks from Browser File');
82     $tplVars['formaction'] = createURL('importNetscape');
83     $templateservice->loadTemplate($templatename, $tplVars);
84 }
85 ?>

Benjamin Mako Hill || Want to submit a patch?