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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
<?php
// +-----------------------------------------------------------------------+
// | remote_site.php |
// +-----------------------------------------------------------------------+
// | application : PhpWebGallery <http://phpwebgallery.net> |
// | branch : BSF (Best So Far) |
// +-----------------------------------------------------------------------+
// | file : $RCSfile$
// | last update : $Date$
// | last modifier : $Author$
// | revision : $Revision$
// +-----------------------------------------------------------------------+
// | 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 |
// | |
// | 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, write to the Free Software |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA. |
// +-----------------------------------------------------------------------+
if (!defined('PHPWG_ROOT_PATH'))
{
die ("Hacking attempt!");
}
include_once(PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
define('CURRENT_DATE', date('Y-m-d'));
// +-----------------------------------------------------------------------+
// | functions |
// +-----------------------------------------------------------------------+
/**
* requests the given $url (a remote create_listing_file.php) and fills a
* list of lines corresponding to request output
*
* @param string $url
* @return void
*/
function remote_output($url)
{
global $template, $errors, $lang;
if($lines = @file($url))
{
$template->assign_block_vars('remote_output', array());
// cleaning lines from HTML tags
foreach ($lines as $line)
{
$line = trim(strip_tags($line));
if (preg_match('/^PWG-([A-Z]+)-/', $line, $matches))
{
$template->assign_block_vars(
'remote_output.remote_line',
array(
'CLASS' => 'remote'.ucfirst(strtolower($matches[1])),
'CONTENT' => $line
)
);
}
}
}
else
{
array_push($errors, $lang['remote_site_file_not_found']);
}
}
/**
* returns an array where are linked the sub-categories id and there
* directories corresponding to the given uppercat id
*
* @param int site_id
* @param mixed id_uppercat
* @return array
*/
function database_subdirs($site_id, $id_uppercat)
{
$database_dirs = array();
$query = '
SELECT id,dir
FROM '.CATEGORIES_TABLE.'
WHERE site_id = '.$site_id;
if (!is_numeric($id_uppercat))
{
$query.= '
AND id_uppercat IS NULL';
}
else
{
$query.= '
AND id_uppercat = '.$id_uppercat;
}
// virtual categories not taken
$query.= '
AND dir IS NOT NULL
;';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
$database_dirs[$row['id']] = $row['dir'];
}
return $database_dirs;
}
/**
* inserts multiple lines in a table
*
* @param string table_name
* @param array dbields
* @param array inserts
* @return void
*/
function mass_inserts($table_name, $dbfields, $inserts)
{
// inserts all found categories
$query = '
INSERT INTO '.$table_name.'
('.implode(',', $dbfields).')
VALUES';
foreach ($inserts as $insert_id => $insert)
{
$query.= '
';
if ($insert_id > 0)
{
$query.= ',';
}
$query.= '(';
foreach ($dbfields as $field_id => $dbfield)
{
if ($field_id > 0)
{
$query.= ',';
}
if (!isset($insert[$dbfield]) or $insert[$dbfield] == '')
{
$query.= 'NULL';
}
else
{
$query.= "'".$insert[$dbfield]."'";
}
}
$query.=')';
}
$query.= '
;';
mysql_query($query);
}
/**
* searchs the "dir" node of the xml_dir given and insert the contained
* categories if the are not in the database yet. The function also deletes
* the categories that are in the database and not in the xml_file.
*
* @param string xml_content
* @param int site_id
* @param mixed id_uppercat
* @param int level
* @return void
*/
function insert_remote_category($xml_content, $site_id, $id_uppercat, $level)
{
global $counts, $removes;
$uppercats = '';
// 0. retrieving informations on the category to display
if (is_numeric($id_uppercat))
{
$query = '
SELECT name,uppercats,dir
FROM '.CATEGORIES_TABLE.'
WHERE id = '.$id_uppercat.'
;';
$row = mysql_fetch_array(mysql_query($query));
$uppercats = $row['uppercats'];
$name = $row['name'];
insert_remote_element($xml_content, $id_uppercat);
}
// $xml_dirs contains dir names contained in the xml file for this
// id_uppercat
$xml_dirs = array();
$temp_dirs = getChildren($xml_content, 'dir'.$level);
foreach ($temp_dirs as $temp_dir)
{
array_push($xml_dirs, getAttribute($temp_dir, 'name'));
}
// $database_dirs contains dir names contained in the database for this
// id_uppercat and site_id
$database_dirs = database_subdirs($site_id, $id_uppercat);
// 3. we have to remove the categories of the database not present anymore
$to_delete = array();
foreach ($database_dirs as $id => $dir)
{
if (!in_array($dir, $xml_dirs))
{
array_push($to_delete, $id);
array_push($removes, get_complete_dir($id));
}
}
delete_categories($to_delete);
// array of new categories to insert
$inserts = array();
foreach ($xml_dirs as $xml_dir)
{
// 5. Is the category already existing ? we create a subcat if not
// existing
$category_id = array_search($xml_dir, $database_dirs);
if (!is_numeric($category_id))
{
$name = str_replace('_', ' ', $xml_dir);
$insert = array();
$insert{'dir'} = $xml_dir;
$insert{'name'} = $name;
$insert{'site_id'} = $site_id;
$insert{'uppercats'} = 'undef';
if (is_numeric($id_uppercat))
{
$insert{'id_uppercat'} = $id_uppercat;
}
array_push($inserts, $insert);
}
}
// we have to create the category
if (count($inserts) > 0)
{
// inserts all found categories
$dbfields = array('dir','name','site_id','uppercats','id_uppercat');
mass_inserts(CATEGORIES_TABLE, $dbfields, $inserts);
$counts{'new_categories'}+= count($inserts);
// updating uppercats field
$query = '
UPDATE '.CATEGORIES_TABLE.'
SET uppercats = ';
if ($uppercats != '')
{
$query.= "CONCAT('".$uppercats."',',',id)";
}
else
{
$query.= 'id';
}
$query.= '
WHERE id_uppercat ';
if (!is_numeric($id_uppercat))
{
$query.= 'IS NULL';
}
else
{
$query.= '= '.$id_uppercat;
}
$query.= '
;';
mysql_query($query);
}
// Recursive call on the sub-categories (not virtual ones)
$database_dirs = database_subdirs($site_id, $id_uppercat);
foreach ($temp_dirs as $temp_dir)
{
$dir = getAttribute($temp_dir, 'name');
$id_uppercat = array_search($dir, $database_dirs);
insert_remote_category($temp_dir, $site_id, $id_uppercat, $level+1);
}
}
/**
* searchs the "root" node of $xml_dir (xml string), inserts elements in the
* database if new
*
* @param string xml_dir
* @param int category_id
* @return void
*/
function insert_remote_element($xml_dir, $category_id)
{
global $counts, $lang, $removes;
$output = '';
$root = getChild($xml_dir, 'root');
$xml_files = array();
$xml_elements = getChildren($root, 'element');
foreach ($xml_elements as $xml_element)
{
array_push($xml_files, getAttribute($xml_element,'file'));
}
// we have to delete all the images from the database that are not in the
// directory anymore (not in the XML anymore)
$query = '
SELECT id,file
FROM '.IMAGES_TABLE.'
WHERE storage_category_id = '.$category_id.'
;';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
if (!in_array($row['file'], $xml_files))
{
array_push($removes, $row['file']);
delete_element($row['id']);
}
}
$database_elements = array();
$query = '
SELECT file
FROM '.IMAGES_TABLE.'
WHERE storage_category_id = '.$category_id.'
;';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
array_push($database_elements, $row['file']);
}
$inserts = array();
foreach ($xml_elements as $xml_element)
{
// minimal tag : <element file="albatros.jpg"/>
$file = getAttribute($xml_element, 'file');
// is the picture already existing in the database ?
if (!in_array($file, $database_elements))
{
$insert = array();
$insert{'file'} = $file;
$insert{'storage_category_id'} = $category_id;
$insert{'date_available'} = CURRENT_DATE;
$optional_atts = array('tn_ext',
'representative_ext',
'filesize',
'width',
'height');
foreach ($optional_atts as $att)
{
if (getAttribute($xml_element, $att) != '')
{
$insert{$att} = getAttribute($xml_element, $att);
}
}
array_push($inserts, $insert);
}
}
if (count($inserts) > 0)
{
$dbfields = array('file','storage_category_id','date_available','tn_ext',
'filesize','width','height');
mass_inserts(IMAGES_TABLE, $dbfields, $inserts);
$counts{'new_elements'}+= count($inserts);
// what are the ids of the pictures in the $category_id ?
$ids = array();
$query = '
SELECT id
FROM '.IMAGES_TABLE.'
WHERE storage_category_id = '.$category_id.'
;';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
array_push($ids, $row['id']);
}
// recreation of the links between this storage category pictures and
// its storage category
$query = '
DELETE FROM '.IMAGE_CATEGORY_TABLE.'
WHERE category_id = '.$category_id.'
AND image_id IN ('.implode(',', $ids).')
;';
mysql_query($query);
$query = '
INSERT INTO '.IMAGE_CATEGORY_TABLE.'
(category_id,image_id)
VALUES';
foreach ($ids as $num => $image_id)
{
$query.= '
';
if ($num > 0)
{
$query.= ',';
}
$query.= '('.$category_id.','.$image_id.')';
}
$query.= '
;';
mysql_query($query);
}
}
// +-----------------------------------------------------------------------+
// | template init |
// +-----------------------------------------------------------------------+
$template->set_filenames(array('remote_site'=>'admin/remote_site.tpl'));
$action = PHPWG_ROOT_PATH.'admin.php?page=remote_site';
$template->assign_vars(
array(
'L_SUBMIT'=>$lang['submit'],
'L_REMOTE_SITE_CREATE'=>$lang['remote_site_create'],
'L_REMOTE_SITE_GENERATE'=>$lang['remote_site_generate'],
'L_REMOTE_SITE_GENERATE_HINT'=>$lang['remote_site_generate_hint'],
'L_REMOTE_SITE_UPDATE'=>$lang['remote_site_update'],
'L_REMOTE_SITE_UPDATE_HINT'=>$lang['remote_site_update_hint'],
'L_REMOTE_SITE_CLEAN'=>$lang['remote_site_clean'],
'L_REMOTE_SITE_CLEAN_HINT'=>$lang['remote_site_clean_hint'],
'L_REMOTE_SITE_DELETE'=>$lang['remote_site_delete'],
'L_REMOTE_SITE_DELETE_HINT'=>$lang['remote_site_delete_hint'],
'L_NB_NEW_ELEMENTS'=>$lang['update_nb_new_elements'],
'L_NB_NEW_CATEGORIES'=>$lang['update_nb_new_categories'],
'L_NB_DEL_ELEMENTS'=>$lang['update_nb_del_elements'],
'L_NB_DEL_CATEGORIES'=>$lang['update_nb_del_categories'],
'L_REMOTE_SITE_REMOVED_TITLE'=>$lang['remote_site_removed_title'],
'L_REMOTE_SITE_REMOVED'=>$lang['remote_site_removed'],
'F_ACTION'=>add_session_id(PHPWG_ROOT_PATH.'admin.php?page=remote_site')
)
);
// +-----------------------------------------------------------------------+
// | new site creation form |
// +-----------------------------------------------------------------------+
$errors = array();
if (isset($_POST['submit']))
{
// site must start by http:// or https://
if (!preg_match('/^https?:\/\/[~\/\.\w-]+$/', $_POST['galleries_url']))
{
array_push($errors, $lang['remote_site_uncorrect_url']);
}
else
{
$page['galleries_url'] = preg_replace('/[\/]*$/',
'',
$_POST['galleries_url']);
$page['galleries_url'].= '/';
// site must not exists
$query = '
SELECT COUNT(id) AS count
FROM '.SITES_TABLE.'
WHERE galleries_url = \''.$page['galleries_url'].'\'
;';
$row = mysql_fetch_array(mysql_query($query));
if ($row['count'] > 0)
{
array_push($errors, $lang['remote_site_already_exists']);
}
}
if (count($errors) == 0)
{
$url = $page['galleries_url'].'create_listing_file.php';
$url.= '?action=test';
$url.= '&version='.PHPWG_VERSION;
if ($lines = @file($url))
{
$first_line = strip_tags($lines[0]);
if (!preg_match('/^PWG-INFO-2:/', $first_line))
{
array_push($errors, $lang['remote_site_error'].' : '.$first_line);
}
}
else
{
array_push($errors, $lang['remote_site_file_not_found']);
}
}
if (count($errors) == 0)
{
$query = '
INSERT INTO '.SITES_TABLE.'
(galleries_url)
VALUES
(\''.$page['galleries_url'].'\')
;';
mysql_query($query);
$template->assign_block_vars(
'confirmation',
array(
'CONTENT'=>$page['galleries_url'].' '.$lang['remote_site_created']
));
}
}
// +-----------------------------------------------------------------------+
// | actions on site |
// +-----------------------------------------------------------------------+
if (isset($_GET['site']) and is_numeric($_GET['site']))
{
$page['site'] = $_GET['site'];
}
if (isset($_GET['action']))
{
$query = '
SELECT galleries_url
FROM '.SITES_TABLE.'
WHERE id = '.$page['site'].'
;';
$row = mysql_fetch_array(mysql_query($query));
$clf = $row['galleries_url'].'create_listing_file.php';
switch($_GET['action'])
{
case 'delete' :
{
delete_site($page['site']);
$template->assign_block_vars(
'confirmation',
array(
'CONTENT'=>$row['galleries_url'].' '.$lang['remote_site_deleted']
));
break;
}
case 'generate' :
{
$title = $row['galleries_url'].' : '.$lang['remote_site_generate'];
$template->assign_vars(array('REMOTE_SITE_TITLE'=>$title));
remote_output($clf.'?action=generate');
break;
}
case 'update' :
{
$title = $row['galleries_url'].' : '.$lang['remote_site_update'];
$template->assign_vars(array('REMOTE_SITE_TITLE'=>$title));
if (@fopen($row['galleries_url'].'listing.xml', 'r'))
{
$counts = array(
'new_elements' => 0,
'new_categories' => 0,
'del_elements' => 0,
'del_categories' => 0
);
$removes = array();
$xml_content = getXmlCode($row['galleries_url'].'listing.xml');
insert_remote_category($xml_content, $page{'site'}, 'NULL', 0);
update_category();
$template->assign_block_vars(
'update',
array(
'NB_NEW_CATEGORIES'=>$counts['new_categories'],
'NB_DEL_CATEGORIES'=>$counts['del_categories'],
'NB_NEW_ELEMENTS'=>$counts['new_elements'],
'NB_DEL_ELEMENTS'=>$counts['del_elements']
));
if (count($removes) > 0)
{
$template->assign_block_vars('update.removes', array());
}
foreach ($removes as $remove)
{
$template->assign_block_vars('update.removes.remote_remove',
array('NAME'=>$remove));
}
}
else
{
array_push($errors, $lang['remote_site_listing_not_found']);
}
break;
}
case 'clean' :
{
$title = $row['galleries_url'].' : '.$lang['remote_site_clean'];
$template->assign_vars(array('REMOTE_SITE_TITLE'=>$title));
remote_output($clf.'?action=clean');
break;
}
}
}
// +-----------------------------------------------------------------------+
// | remote sites list |
// +-----------------------------------------------------------------------+
// site 1 is the local site, should not be taken into account
$query = '
SELECT id, galleries_url
FROM '.SITES_TABLE.'
WHERE id != 1
;';
$result = mysql_query($query);
while ($row = mysql_fetch_array($result))
{
$base_url = PHPWG_ROOT_PATH.'admin.php';
$base_url.= '?page=remote_site';
$base_url.= '&site='.$row['id'];
$base_url.= '&action=';
$template->assign_block_vars(
'site',
array(
'NAME' => $row['galleries_url'],
'U_GENERATE' => add_session_id($base_url.'generate'),
'U_UPDATE' => add_session_id($base_url.'update'),
'U_CLEAN' => add_session_id($base_url.'clean'),
'U_DELETE' => add_session_id($base_url.'delete')
)
);
}
// +-----------------------------------------------------------------------+
// | errors display |
// +-----------------------------------------------------------------------+
if (count($errors) != 0)
{
$template->assign_block_vars('errors',array());
foreach ($errors as $error)
{
$template->assign_block_vars('errors.error',array('ERROR'=>$error));
}
}
// +-----------------------------------------------------------------------+
// | sending html code |
// +-----------------------------------------------------------------------+
$template->assign_var_from_handle('ADMIN_CONTENT', 'remote_site');
?>
|