aboutsummaryrefslogtreecommitdiff
path: root/frontend/import.php
blob: 51da2fe25abb701c8b1664f042ed3c5fbd2a4c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php

/*
   Cheetah News import.php
   Copyright (C) 2005, 2006 Wojciech Polak.
   Copyright (C) 2006 The Cheetah News Team.

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require 'lib/include.php';

start_session (null, false);
$session->auth ('iflogged');

if ($session->status['afterlogged'] == 'yes')
{
  if ($session->email == 'guest') {
    printXmlError (null, _('You are using a guest account. You must register in order to do this.'));
    exit;
  }
  if (!isset ($_FILES['opmlfile']['tmp_name'])) {
    printXmlError (0, _('There was a problem with your upload'));
    exit;
  }
  if (is_uploaded_file ($_FILES['opmlfile']['tmp_name'])) {
    $opml =& new OPMLProcessor ();
    $res = $opml->process ($_FILES['opmlfile']['tmp_name'], $session->id);
    if ($res == 0) {
      if ($opml->isOPML)
	printXmlWithHeader ('<ok/>');
      else
	printXmlError (-3, _('The uploaded file is not an OPML.'));
    }
    else
      printXmlError ($res, _('XML Syntax Error'));
  }
  else {
    switch ($_FILES['opmlfile']['error']) {
      case 1:
      case 2:
	printXmlError ($_FILES['opmlfile']['error'],
		       _('The file you are trying to upload is too big'));
	break;
      case 3:
	printXmlError (3, _('The file was only partially uploaded'));
	break;
      case 4:
	printXmlError (4, _('No file was uploaded'));
	break;
      default:
	printXmlError (0, _('There was a problem with your upload'));
    }
  }
}
else {
  printXmlError (null, _('Operation not permitted. Access denied'));
}

class OPMLProcessor
{
  var $isOPML = false;
  var $feedId = -1;
  var $feedHash = '';
  var $folderId = -1;
  var $insideFeed = false;
  var $insideFolder = false;
  var $titleTag = false;
  var $errorWriteFolder = false;
  var $bloglinesExp = false;
  var $text = '';
  var $xmlUrl = '';
  var $userId;
  var $db;

  function OPMLProcessor () {}

  function process ($filename, $userId)
  {
    $this->userId = $userId;

    if (!($xml_parser = xml_parser_create ()))
      return -1;

    xml_parser_set_option ($xml_parser, XML_OPTION_CASE_FOLDING, false);
    xml_set_object ($xml_parser, $this);
    xml_set_element_handler ($xml_parser, 'startElement', 'endElement');
    xml_set_character_data_handler ($xml_parser, 'characterElement');

    $this->startDocument ($xml_parser);

    $res = 0;
    $fp = fopen ($filename, 'r');
    while (!feof ($fp)) {
      $line = fgets ($fp, 4096);
      if (!xml_parse ($xml_parser, $line)) {
	$res = -2;
	break;
      }
    }
    fclose ($fp);

    $this->endDocument ($xml_parser);

    xml_parser_free ($xml_parser);
    return $res;
  }

  function startDocument ($parser) {
    $this->db = new Database ();
  }

  function endDocument ($parser) {
  }

  function startElement ($parser, $name, $attrs) {
    $this->text = '';
    $this->xmlUrl = '';

    if ($name == 'opml')
      $this->isOPML = true;
    else if ($name == 'title')
      $this->titleTag = true;

    if (!$this->errorWriteFolder) {
      if ($name == 'outline') {
	if (isset ($attrs['text']))
	  $this->text = $this->db->escape ($attrs['text']);
	else
	  $this->text = '';
	if ($this->text == '') {
	  $this->text = $this->db->escape ($attrs['title']);
	}
	if ($this->text != '') {
	  $this->text = str_replace ("\n", ' ', $this->text);
	  if (isset ($attrs['xmlUrl'])) {
	    $this->xmlUrl = $this->db->escape ($attrs['xmlUrl']);
	    if ($this->xmlUrl[strlen ($this->xmlUrl) - 1] == '/') {
	      $this->xmlUrl = substr ($this->xmlUrl, 0, -1);
	    }
	    $url_parts  = parse_url ($this->xmlUrl);
	    $url = $url_parts['host'].(!empty ($url_parts['path'])
				       ? $url_parts['path'] : '');
	    $this->feedHash = md5 ($url);
	  }
	  else
	    $this->xmlUrl = '';

	  if ($this->insideFolder) {
	    $this->insideFeed = true;
	    if ($this->xmlUrl != '') { // feed w folderze

	      if (!preg_match ('/^http:\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}'
			       .'((:[0-9]{1,5})?\/.*)?$/i' , $this->xmlUrl))
		return;

	      $exists = false;

	      $this->db->query ("SELECT id FROM feed WHERE url='".
				$this->xmlUrl."'");
	      if ($this->db->next_record ()) {
		$this->feedId = $this->db->f ('id');
		$this->db->query ("SELECT id FROM subscription WHERE feedid='".
				  $this->feedId."' AND userid='".$this->userId."'");
		if ($this->db->next_record ()) {
		  $exists = true;
		}
	      }
	      else {
		$this->db->query ("INSERT INTO feed SET hash='".
				  $this->feedHash."', url='".
				  $this->xmlUrl."'");
		$this->db->query ("SELECT LAST_INSERT_ID() AS id FROM feed");
		if ($this->db->next_record ()) {
		  $this->feedId = $this->db->f ('id');
		}
		else {
		  return;
		}
	      }
	      $this->db->query ("UPDATE subscription SET pri=pri+1 WHERE userid='".$this->userId."'");
	      if ($exists) {
		$this->db->query ("UPDATE subscription SET folder='".
				  $this->folderId."', ".
				  "description='".$this->text.
				  "' WHERE userid='".$this->userId."' ".
				  "AND feedid='".$this->feedId."'");
	      }
	      else {
		$this->db->query ("INSERT INTO subscription SET userid='".
				  $this->userId."', "."feedid='".
				  $this->feedId."', folder='".
				  $this->folderId."', "."description='".
				  $this->text."'");
	      }
	    }
	    else {
	      return;
	    }
	  }
	  else {
	    if ($this->xmlUrl == '') { // folder
	      if ($this->bloglinesExp && $this->text == 'Subscriptions') {
		$this->bloglinesExp = false;
		return;
	      }
	      $this->insideFolder = true;
	      $this->db->query ("SELECT id FROM folder WHERE userid='".
				$this->userId."' AND fname='".
				$this->text."'");	
	      if ($this->db->next_record ()) {
		$this->folderId = $this->db->f ('id');
		return;
	      }
	      else {
		$this->db->query ("UPDATE folder SET pri=pri+1 WHERE userid='".$this->userId."'");
		$this->db->query ("INSERT INTO folder SET userid='".
				  $this->userId."', fname='".$this->text."'");
		$this->db->query ("SELECT LAST_INSERT_ID() AS id FROM folder");
		if ($this->db->next_record ()) {
		  $this->folderId = $this->db->f ('id');
		}
		else {
		  $this->errorWriteFolder = true;
		  return;
		}
	      }
	    }
	    else { // feed bez folderu
	      $this->insideFeed = true;

	      if (!preg_match ('/^http:\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}'
			       .'((:[0-9]{1,5})?\/.*)?$/i' , $this->xmlUrl))
		return;

	      $exists = false;

	      $this->db->query ("SELECT id FROM feed WHERE url='".
				$this->xmlUrl."'");
	      if ($this->db->next_record ()) {
		$this->feedId = $this->db->f ('id');
		$this->db->query ("SELECT id FROM subscription WHERE feedid='".
				  $this->feedId."' AND userid='".$this->userId."'");
		if ($this->db->next_record ()) {
		  $exists = true;
		  return;
		}
	      }
	      else {
		$this->db->query ("INSERT INTO feed SET hash='".
				  $this->feedHash."', url='".$this->xmlUrl."'");
		$this->db->query ("SELECT LAST_INSERT_ID() AS id FROM feed");
		if ($this->db->next_record ()) {
		  $this->feedId = $this->db->f ('id');
		}
		else {
		  return;
		}
	      }
	      $this->db->query ("UPDATE subscription SET pri=pri+1 WHERE folder='0' AND userid='".$this->userId."'");
	      $this->db->query ("INSERT INTO subscription (userid, feedid, description, pri) SELECT ".
				$this->userId.", ".$this->feedId.", '".
				$this->text."', COALESCE((MAX(pri) + 1), 1) ".
                                "FROM subscription WHERE folder!='0' AND userid='".$this->userId."'");
	      if ($exists) {
		$this->db->query ("DELETE FROM subscription WHERE folder!='0' AND userid='".
				  $this->userId."' AND feedid=".$this->feedId);
	      }
	    }
	  }
	}
      }
    }
  }

  function endElement ($parser, $name) {
    if ($name == 'outline') {
      if ($this->insideFeed) {
	$this->insideFeed = false;
      }
      else if ($this->insideFolder) {
	$this->insideFolder = false;
	$this->errorWriteFolder = false;
      }
    }	
  }

  function characterElement ($parser, $data) {
    if ($this->titleTag) {
      if ($data == 'Bloglines Subscriptions') {
	$this->bloglinesExp = true;
	$this->titleTag = false;
      }
    }
  }

}

?>

Return to:

Send suggestions and report system problems to the System administrator.