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

Benjamin Mako Hill || Want to submit a patch?