improve the perfromance of the javascript autocomplete lookup
[scuttle] / importNetscape.php
1 <?php
2 /***************************************************************************
3 Copyright (c) 2004 - 2010 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
23 $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
24 $userservice     =& ServiceFactory::getServiceInstance('UserService');
25 $templateservice =& ServiceFactory::getServiceInstance('TemplateService');
26
27 $tplVars = array();
28
29 if ($userservice->isLoggedOn() && sizeof($_FILES) > 0 && $_FILES['userfile']['size'] > 0) {
30     $userinfo = $userservice->getCurrentUser();
31
32     if (isset($_POST['status']) && is_numeric($_POST['status'])) {
33       $status = intval($_POST['status']);
34     }
35     else {
36       $status = 2;
37     }
38
39     // File handle
40     $html = file_get_contents($_FILES['userfile']['tmp_name']);
41     
42     // Create link array
43     preg_match_all('/<a\s+(.*?)\s*\/*>([^<]*)/si', $html, $matches);
44     $links  = $matches[1];
45     $titles = $matches[2];
46     
47     $size = count($links);
48     for ($i = 0; $i < $size; $i++) {
49       $bTags   = '';
50       $bStatus = $status;
51
52       $attributes = preg_split('/\s+/s', $links[$i]);
53       foreach ($attributes as $attribute) {
54           $att = preg_split('/\s*=\s*/s', $attribute, 2);
55           $attrTitle = $att[0];
56           $attrVal   = str_replace('"', '&quot;', preg_replace('/([\'"]?)(.*)\1/', '$2', $att[1]));
57           switch ($attrTitle) {
58             case 'HREF':
59               $bAddress = $attrVal;
60               break;
61             case 'ADD_DATE':
62               $bDatetime = gmdate('Y-m-d H:i:s', $attrVal);
63               break;
64             case 'PRIVATE':
65               $bStatus = (intval($attrVal) == 1) ? 2 : $status;
66               break;
67             case 'TAGS':
68               $bTags = strtolower($attrVal);
69               break;
70           }
71       }
72       $bTitle = str_replace('"', '&quot;', trim($titles[$i]));
73
74       if ($bookmarkservice->bookmarkExists($bAddress, $userservice->getCurrentUserId())) {
75         $tplVars['error'] = T_('You have already submitted this bookmark.');
76       } else {
77         // If bookmark claims to be from the future, set it to be now instead
78         if (strtotime($bDatetime) > time()) {
79           $bDatetime = gmdate('Y-m-d H:i:s');
80         }
81
82         if ($bookmarkservice->addBookmark($bAddress, $bTitle, NULL, $bStatus, $bTags, $bDatetime, false, true)) {
83           $tplVars['msg'] = T_('Bookmark imported.');
84         }
85         else {
86           $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
87         }
88       }
89     }
90     header('Location: '. createURL('bookmarks', $userinfo[$userservice->getFieldName('username')]));
91 }
92 else {
93   $templatename = 'importNetscape.tpl';
94   $tplVars['subtitle']   = T_('Import Bookmarks from Browser File');
95   $tplVars['formaction'] = createURL('importNetscape');
96   $templateservice->loadTemplate($templatename, $tplVars);
97 }

Benjamin Mako Hill || Want to submit a patch?