-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
810 lines (767 loc) · 24.7 KB
/
index.php
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
<?php
/*
Plugin Name: WP-MultiTarget-Uploads-Sync-Tool
Plugin URI: http://rainmoe.com/
Description: A WordPress plugin which able to sync attachments to multiple FTP targets.
Author: evlos
Version: 1.0.4
Author URI: http://rainmoe.com/
*/
MUST::ins();
class MUST {
static $name = 'MUST';
static $version = '1.0.4';
static $ins;
static $addons = array(
'ftp' => 'MUST_ftp',
);
private function __construct() {
$this->wpInit();
//$this->createOpt();
}
function ins() {
if (is_null(self::$ins))
self::$ins = new self();
return self::$ins;
}
function arrayRewrite($default, $changed) {
foreach ($default as $key => $val) {
if (isset($changed[$key])) $default[$key] = $changed[$key];
}
return $default;
}
function zip($data) {
return json_encode($data);
}
function unzip($data) {
return json_decode($data, true);
}
function singleOpt($changed = array()) {
$opt = array(
'name' => '',
'type' => '',
'conn' => '',
'enable' => 0
);
return self::arrayRewrite($opt, $changed);
}
function getOpt() {
$opt = get_option(self::$name.'_option', '');
return empty($opt) ? array() : json_decode($opt, true);
}
function refreshOpt() {
// Notice
}
function putOpt($changed) {
$res = update_option(self::$name.'_option', json_encode($changed));
self::refreshOpt();
return $res;
}
function getMT() {
$res = get_option(self::$name.'_mtarget', '');
if (self::isNoLocal()&&($res=='-1'||empty($res))) {
// IMP
return $res;
}
else return $res;
}
function putMT($data) {
return update_option(self::$name.'_mtarget', $data);
}
function getPM($pid, $key) {
return get_post_meta($pid, self::$name.'_'.$key, true);
}
function putPM($pid, $key, $data) {
return update_post_meta($pid, self::$name.'_'.$key, $data);
}
function createOpt() {
if (!get_option(self::$name.'_option')) {
update_option(self::$name.'_option', '');
}
// Useless
}
function readOpt($name) {
return get_option(self::$name.'_option_'.$name, '');
}
function setOpt($name, $data) {
return update_option(self::$name.'_option_'.$name, $data);
}
function isNoLocal() {
return self::readOpt('nolocal') == 'yes' ? true : false;
}
function setNoLocal() {
return self::setOpt('nolocal', 'yes');
}
function wpInit() {
add_action('admin_menu', array($this, 'adminMenu'));
//add_action('wp_enqueue_scripts', array($this, 'addScripts'));
//add_action('wp_head', array($this, 'addText2Header'));
if (!self::isNoLocal()) add_filter('the_content', array($this, 'addAfterTheContent'));
add_action('save_post', array($this, 'addWhenSavingPost'));
//add_filter('content_save_pre', array($this, 'addOnEditorSaving')); // uploadA() will replace this
if (self::isNoLocal()) self::putMT(0);
}
function addScripts() {
wp_enqueue_script('jquery');
}
function addStyle() {
wp_enqueue_style('/wp-admin/css/colors-classic.css');
}
function addText2Header() {
//FIX later
echo '
<script type="text/javascript">
jQuery(function($){
$.one(function(){
$(".entry-content img").bind("load", function(){
$(this).hide().attr("src", "");
self.loaded = true;
});
});
});
jQuery(document).ready(function($){
$(".entry-content img").each(function(){
//$(this).attr("src", $(this).attr("src").replace("'.self::urlDefault().'", "'.self::urlCurrent().'"));
//$(this).css("display", "block");
});
});
</script>';
}
function readAttachments() {
global $wpdb;
return array_reverse($wpdb->get_results("
SELECT * FROM {$wpdb->posts}
WHERE post_status = 'inherit' AND post_type = 'attachment'
"));
}
function readChildAttachments($pid) {
global $wpdb;
return array_reverse($wpdb->get_results("
SELECT * FROM {$wpdb->posts}
WHERE post_status = 'inherit' AND post_type = 'attachment' AND post_parent = {$pid}
"));
}
function readArticles($aid = -1) {
global $wpdb;
if ($aid == -1)
return array_reverse($wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type = 'post'"));
else
return array_reverse($wpdb->get_results("SELECT * FROM {$wpdb->posts} WHERE post_type = 'post' AND ID = {$aid}"));
}
/*
* $res refer to
*
* Array
(
[0] => wp-content/uploads/2012/06/goodwp.com-15509.jpg
[1] => 2012
[2] => 06
[3] => goodwp.com-15509.jpg
[4] => C:\path\to\wordpress\wp-content\uploads
)
*
*/
function splitUrl($guid) {
$regex = '/wp-content\/uploads\/([0-9]+)\/([0-9]+)\/(.+)$/i';
preg_match($regex, $guid, $res);
$dir = wp_upload_dir();
$res[] = $dir['basedir'];
return $res;
}
function linko($pid) {
global $wpdb;
return $wpdb->get_var("
SELECT guid FROM {$wpdb->posts}
WHERE post_status = 'inherit' AND post_type = 'attachment' AND ID = {$pid}
");
}
function link($pid) {
$MT = self::getMT();
$url = getPM($pid, 'link_'.$MT);
if (!empty($url)) return $url;
else {
return self::linko($pid);
}
}
function isRemoteFileExists($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($retcode==200);
}
function isReplaced() {
$opt = self::getOpt();
if (self::isNoLocal()&&!empty($opt)) return true;
if (empty($opt)) return false;
$MT = self::getMT();
if (empty($MT)||$MT == '-1') return false;
return true;
}
function urlDefault() {
return site_url().'/wp-content/uploads/';
}
function urlCurrent() {
$MT = self::getMT();
$opt = self::getOpt();
return $opt[$MT]['conn']['folder_url'];
}
function addWhenSavingPost($aid) {
if (self::isNoLocal()) self::uploadA(false, false, $aid);
else self::upload();
}
function addAfterTheContent($content) {
if (self::isReplaced())
return str_replace(self::urlDefault(), self::urlCurrent(), $content);
else
return $content;
}
function addOnEditorSaving($content) {
if (self::isReplaced())
return str_replace(self::urlDefault(), self::urlCurrent(), $content);
else
return $content;
}
function upload($echo = false, $check = false) {
set_time_limit(600);
$opt = self::getOpt();
foreach ($opt as $key => $val) {
if ($val['enable']) {
$conn = $val['conn'];
$attach = self::readAttachments();
foreach ($attach as $val) {
$isReady = true;
//Check remote ifUploaded
if ($check)
if (function_exists('curl_init')) {
$rurl = self::getPM($val->ID, 'link_'.$key);
if ($rurl!=''&&self::isRemoteFileExists($rurl)) {
$isReady = false;
}
}
else {
if ($echo) echo '* cUrl is not installed, file will be uploaded without exists check ...'."\r\n";
}
//Check ifUploaded
$tmp = self::getPM($val->ID, 'link_'.$key);
if (empty($tmp)) $isReady = true;
//Upload
if ($isReady) {
$nurl = MUST_ftp::upload(self::splitUrl($val->guid), $conn);
if ($nurl) self::putPM($val->ID, 'link_'.$key, $nurl);
if ($echo) echo '* '.$val->guid.' uploaded to '.$nurl.' ...'."\r\n";
}
else {
if ($echo) echo '* '.$val->guid.' remote existed ...'."\r\n";
}
}
}
}
}
function adminMenu() {
$page = add_menu_page('WP-MUST', 'WP-MUST', 'administrator', __FILE__, array($this, 'pageReadMe'));
add_action('admin_print_styles-'.$page, array($this, 'addStyle'));
$page = add_submenu_page(__FILE__, 'WP-MUST', 'WP-MUST', 'administrator', __FILE__, array($this, 'pageReadMe'));
if (self::isNoLocal())
$page = add_submenu_page(__FILE__, 'WP-MUST InArticles', 'WP-MUST InArticles', 'administrator', 'MUSTpageInArticles', array($this, 'pageInArticles'));
else
$page = add_submenu_page(__FILE__, 'WP-MUST InMedia', 'WP-MUST InMedia', 'administrator', 'MUSTpageInMedia', array($this, 'pageList'));
$page = add_submenu_page(__FILE__, 'WP-MUST Setting', 'WP-MUST Setting', 'administrator', 'MUSTpageSetting', array($this, 'pageSetting'));
$page = add_submenu_page(__FILE__, 'WP-MUST Changelog', 'WP-MUST Changelog', 'administrator', 'MUSTpageChangelog', array($this, 'pageChangelog'));
add_action('admin_print_styles-'.$page, array($this, 'addStyle'));
}
function pageList() {
$opt = self::getOpt();
?>
<div style="margin: 4px 15px 0 0;">
<!-- div -->
<h2>WP-MultiTarget-Uploads-Sync-Tool Attachments in Media Library</h2>
<div>
<form action="" method="POST" style="display:inline;">
<input type="hidden" name="do" value="upload" />
<input type="submit" value="Upload All" />
</form>
-
<form action="" method="POST" style="display:inline;">
<input type="hidden" name="do" value="check"/>
<input type="submit" value="Check All" />
</form>
</div>
<?php
if (isset($_POST['do'])) {
if ($_POST['do']=='upload') {
echo '<br /><textarea style="width: 100%; height: 120px;">';
self::upload(true);
echo '</textarea><br />';
}
else if ($_POST['do']=='check') {
self::upload(true, true);
}
}
?>
<br />
<table class="widefat">
<?php
$data = self::readAttachments();
echo '<thead><th>ID</th><th>User</th><th>Date</th><th>Title</th><th>Mime</th><th>Guid</th>';
foreach ($opt as $key_ => $val_) echo '<th>'.$val_['name'].'</th>';
echo '</thead>';
foreach ($data as $val) {
$res = self::splitUrl($val->guid);
echo '<tr><td>'.$val->ID.'</td><td>'.get_user_meta($val->post_author, 'nickname', true).'</td>
<td>'.$val->post_date_gmt.'</td><td>'.$val->post_title.'</td><td>'.$val->post_mime_type.'</td>
<td><a target="_blank" href="'.$val->guid.'">'.$res[3].'</a></td>';
foreach ($opt as $key_ => $val_) {
echo '<td>';
$url = self::getPM($val->ID, 'link_'.$key_);
if (!$url) echo '<span style="color:grey;">None</span>';
else echo '<a target="_blank" href="'.$url.'" style="color:green;">OPEN</a>';
// - <a target="_blank" href="#" style="color:green;">UPLOAD</a>
echo '</td>';
}
echo '</tr>';
}
?>
</table>
<!-- div -->
</div>
<?php
}
function pageSetting() {
$opt = self::getOpt();
$MT = self::getMT();
if (isset($_POST['do'])) {
$do = $_POST['do'];
//print_r($_POST);
if ($do == 'add') {
$opt[] = self::singleOpt(array('type' => $_POST['add_type']));
self::putOpt($opt);
}
elseif ($do == 'modify') {
$count = 0;
while (isset($_POST[$count.'_name'])) {
$tmp = array();
foreach ($_POST as $key => $val) {
$tmp[str_replace($count.'_', '', $key)] = $val;
}
$singleSet = MUST_ftp::set($tmp);
if (self::isNoLocal()) $enable = 0;
else $enable = isset($_POST[$count.'_enable']) ? 1 : 0;
$opt_[$count] = self::singleOpt(array(
'name' => $_POST[$count.'_name'],
'type' => $_POST[$count.'_type'],
'conn' => $singleSet,
'enable' => $enable,
));
$count++;
}
$opt = $opt_;
self::putOpt($opt);
}
elseif ($do == 'mtarget') {
self::putMT($_POST['mtarget']);
$MT = $_POST['mtarget'];
}
elseif ($do == 'clearmtarget') {
self::putMT('-1');
}
elseif ($do == 'nolocalforsure') {
self::setNoLocal();
}
}
?>
<style type="text/css">
.widefat tr td .widefat {
border-color: #EEE;
}
.widefat tr td .widefat thead tr th {
background-image: none;
background: #fafafa;
}
.widefat tr td .widefat td, .widefat tr td .widefat th {
border-bottom-color: #eee;
}
input[disabled="disabled"] {
background: #eee;
}
.widefat .child input {
width: 120px;
}
input[type="submit"] {
padding: 3px 12px;
text-transform: uppercase;
}
.inputlong {
width: 600px;
}
</style>
<div style="margin: 4px 15px 0 0;">
<!-- div -->
<h2>WP-MultiTarget-Uploads-Sync-Tool Setting</h2>
<div>
<?php if (!self::isNoLocal()||empty($opt)): ?>
<form action="" method="POST" style="display:inline;">
New Target:
<select name="add_type"><?php foreach (self::$addons as $key => $val) : ?>
<option value="<?php echo $key; ?>"><?php echo strtoupper($key); ?></option>
<?php endforeach; ?></select>
<input type="hidden" name="do" value="add" />
<input type="submit" value="Add" />
</form>
<?php endif; ?>
<?php if (!self::isNoLocal()&&!empty($opt)) : ?>
<form action="" method="POST" style="display:inline;">
- Current Target:
<select name="mtarget"><?php $choosed = false; foreach ($opt as $key_ => $val_): ?>
<?php if ($MT == $key_) $choosed = true; ?>
<option value="<?php echo $key_; ?>"<?php echo $MT == $key_ ? ' selected' : ''; ?>><?php echo $val_['name']; ?></option>
<?php endforeach; ?><?php if (!self::isNoLocal()) echo $choosed ? '' : '<option value="-1" selected>-</option>'; ?></select>
<input type="hidden" name="do" value="mtarget" />
<input type="submit" value="Set" />
</form>
<form action="" method="POST" style="display:inline;">
<input type="hidden" name="do" value="clearmtarget" />
- <input type="submit" value="Stop url replacement" />
</form>
<?php endif; ?>
<?php if (self::isNoLocal()) : ?>
No Local Mode:
<span style="color:green;">ENABLED</span>
<?php else: ?>
- No Local Mode:
<?php if (isset($_POST['do'])&&$_POST['do']=='nolocal') : ?>
<form action="" method="POST" style="display:inline;">
<input type="hidden" name="do" value="nolocalforsure" />
<input type="submit" value="I am Sure" /> (Are you sure? There is no turning back.)
</form>
<?php else : ?>
<form action="" method="POST" style="display:inline;">
<input type="hidden" name="do" value="nolocal" />
<input type="submit" value="Enable" />
</form>
<?php endif; ?>
<?php endif; ?>
<?php if (self::isNoLocal() && $MT == '-1') : ?>
<span style="color:red;">- Warning: No main target! One must be choosen!</span>
<?php endif; ?>
</div>
<br />
<div>
<?php if (!empty($opt)): ?>
<form action="" method="POST">
<table class="widefat">
<thead><th>Enable</th></th><th>ID</th><th>Name</th><th>Type</th><th>Connection</th></thead>
<?php $count = 0; foreach ($opt as $key_ => $val_): ?>
<?php if (self::isNoLocal()&&$count++==1) break; ?>
<tr id="target-<?php echo $key_; ?>">
<td>
<?php if (self::isNoLocal()) : ?>
<input type="checkbox" name="<?php echo $key_; ?>_none" disabled=disabled />
<?php else : ?>
<input type="checkbox" name="<?php echo $key_; ?>_enable"<?php echo $val_['enable'] ? ' checked' : '';?> />
<?php endif; ?>
</td>
<td><?php echo $key_; ?></td>
<td><input type="text" name="<?php echo $key_; ?>_name" value="<?php echo $val_['name']; ?>" /></td>
<td><?php echo strtoupper($val_['type']); ?></td>
<td><table class="widefat child">
<thead>
<?php foreach (MUST_ftp::$set as $key => $val): ?>
<th><?php echo strtoupper($key); ?></th>
<?php endforeach; ?>
</thead>
<tr>
<?php foreach (MUST_ftp::set($val_['conn']) as $key => $val): ?>
<td><input type="text" name="<?php echo $key_; ?>_<?php echo $key; ?>" value="<?php echo $val; ?>" /></td>
<?php endforeach; ?>
</tr>
</table>
<input type="hidden" name="<?php echo $key_; ?>_type" value="<?php echo $val_['type']; ?>" />
</td>
</tr>
<?php endforeach; ?>
</table>
<input type="hidden" name="do" value="modify" />
<br />
<input type="submit" value="Save" />
</form>
<?php endif; ?>
</div>
<br />
Default URL: <?php echo self::urlDefault(); ?>
<br />
Current URL: <?php echo self::isReplaced() ? self::urlCurrent() : 'disabled.'; ?>
<!-- div -->
</div>
<?php
}
function pageReadMe() {
?>
<div style="margin: 4px 15px 0 0;">
<!-- div -->
<h2>WP-MultiTarget-Uploads-Sync-Tool ReadMe</h2>
<div>
<h3>Current version v<?php echo self::$version; ?></h3>
</div>
<div>
<h3>For English Users:</h3>
<p>This is a WordPress Multiple Targets Sync Tool, which means you are able to add multiple targets with FTP supported (Currently), and sync attachments to these targets.
<br />Also, it is possible to use the url of attachments in one of these targets to show on fontend.</p>
<p>Steps:</p>
<ul>
<li>1. Create an new target.</li>
<li>2. Fill in connection information in the form displayed afterwards.</li>
<li>3. Enable this new target to make it syncable, and save it.</li>
<li>4. (Optional) Find this target at current target option, choose it and set. Which means all url of attachments on fontend will be replaced with new url of attachments in this choosed target.</li>
<li>5. Switch to WP-MUST InMedia/InFolder, and press upload all. After this, it not necessary to press it again when upload new attachments.</li>
</ul>
</div>
<div>
<h3>For Chinese Users:</h3>
<p>这是一个 WordPress 多目标(图床)附件同步工具。你可以添加多个图床,当然目前仅支持 FTP 图床。设置完成后附件就能够被同步到这些图床,并从某一图床调用显示。</p>
<p>步骤:</p>
<ul>
<li>1. 先新建一个 target,即新建同步目标。</li>
<li>2. 然后在出现的空白表单内填入连接信息,如果是 ftp 则是此 ftp 的连接信息。</li>
<li>3. Enable 这个 target,即启用这个目标。启用后的目标才会允许被同步,点 Save 保存。</li>
<li>4. 在 Current Target 处找到这个新建的 target 目标,点击 Set。设置为当前的目标,那么所有原附件地址将被替换为其在此目标的上的地址。</li>
<li>5. 到文件列表处点 Upload All 即可。以后传的附件会自动被同步。</li>
</ul>
</div>
<div>
<h3>FTP example:</h3>
<ul>
<li>Host: example.com</li>
<li>Username: example_username</li>
<li>Password: example_passweord</li>
<li>Folder: public_html/example/</li>
<li>Folder URL: http://example.com/example/</li>
<li>Port: 21</li>
</ul>
</div>
<!-- div -->
</div>
<?php
}
function pageChangelog() {
?>
<div style="margin: 4px 15px 0 0;">
<!-- div -->
<h2>WP-MultiTarget-Uploads-Sync-Tool Changlog</h2>
<div>
<h3>Current version v<?php echo self::$version; ?></h3>
</div>
<div>
<h3>Changlog:</h3>
<ul>
<li>= 1.0.4 = Fixed no FTP port support</li>
<li>= 1.0.3 = Made no local mode works better</li>
<li>= 1.0.2 = Added no local mode</li>
<li>= 1.0.1 = Enhancement</li>
<li>= 1.0.0 = Init</li>
</ul>
</div>
<!-- div -->
</div>
<?php
}
function pageInFolder() {
?>
<div style="margin: 4px 15px 0 0;">
<!-- div -->
<h2>WP-MultiTarget-Uploads-Sync-Tool Files in Uploads Folder</h2>
<div>
<h3>I am working on it.</h3>
<p>It should be completed on later version.</p>
</div>
<!-- div -->
</div>
<?php
}
/*function getKeywords() {
return self::readOpt('keywords');
}
function setKeywords() {
return self::setOpt('keywords', 'yes');
}*/
function resetAIID($theid) {
global $wpdb;
return $wpdb->get_var("
ALTER TABLE {$wpdb->posts} AUTO_INCREMENT = {$theid}
");
}
function uploadA($echo = true, $check = false, $aid = -1) {
set_time_limit(600);
$data = self::readArticles($aid);
$opt = self::getOpt();
$conn = $opt[0]['conn'];
foreach ($data as $val) {
preg_match_all("/".str_replace('/', '\/', self::urlDefault())."[^\"]+|".str_replace('/', '\/', self::urlCurrent())."[^\"]+/i", $val->post_content, $res);
foreach ($res[0] as $img) $final[md5(str_replace(self::urlDefault(), self::urlCurrent(), $img))] = str_replace(self::urlCurrent(), self::urlDefault(), $img);
foreach (array_unique($final) as $key => $img) {
$isReady = true;
$needClear = false;
$ourl = self::getPM($val->ID, 'link_'.$key, $nurl);
// Check remote ifUploaded
if ($check) {
if (function_exists('curl_init')) {
if ($ourl != ''&&self::isRemoteFileExists($ourl)) {
if ($echo) echo '* '.$img.' remote file existed ...'."\r\n";
$isReady = false;
$needClear = true;
}
}
else {
if ($echo) echo '* cUrl is not installed, file will be uploaded without exists check ...'."\r\n";
}
}
if ($ourl != '') {
if ($echo) echo '* '.$img.' remote record existed ...'."\r\n";
$isReady = false;
}
if ($isReady) {
$nurl = MUST_ftp::upload(self::splitUrl($img), $conn);
if ($nurl) self::putPM($val->ID, 'link_'.$key, $nurl);
if ($echo) echo '* '.$img.' uploaded to '.$nurl.' ...'."\r\n";
$needClear = true;
}
if ($needClear) {
$post_tmp = array();
$post_tmp['ID'] = $val->ID;
$post_tmp['post_content'] = str_replace($img, str_replace(self::urlDefault(), self::urlCurrent(), $img), $val->post_content);
wp_update_post($post_tmp);
$posts = self::readChildAttachments($val->ID);
foreach ($posts as $post) {
if ($post->guid == $img) wp_delete_attachment($post->ID, true);
}
self::resetAIID($val->ID + 1);
}
}
}
}
function pageInArticles() {
$opt = self::getOpt();
?>
<div style="margin: 4px 15px 0 0;">
<!-- div -->
<h2>WP-MultiTarget-Uploads-Sync-Tool Attachments in Articles</h2>
<div>
<form action="" method="POST" style="display:inline;">
<input type="hidden" name="do" value="upload"/>
<input type="submit" value="Upload All" />
</form>
-
<form action="" method="POST" style="display:inline;">
<input type="hidden" name="do" value="check"/>
<input type="submit" value="Check All" />
</form>
</div>
<?php
if (isset($_POST['do'])) {
echo '<br /><textarea style="width: 100%; height: 120px;">';
if ($_POST['do']=='upload') {
self::uploadA();
}
else if ($_POST['do']=='check') {
self::uploadA(true, true);
}
else if ($_POST['do']=='uploadone') {
$aid = $_POST['aid'];
self::uploadA(true, true, $aid);
}
echo '</textarea><br />';
}
?>
<br />
<style type="text/css">
.widefat tr td .widefat {
border-color: #EEE;
}
.widefat tr td .widefat thead tr th {
background-image: none;
background: #fafafa;
}
.widefat tr td .widefat td, .widefat tr td .widefat th {
border-bottom-color: #eee;
}
input[disabled="disabled"] {
background: #eee;
}
.widefat .child input {
width: 120px;
}
input[type="submit"] {
padding: 3px 12px;
text-transform: uppercase;
}
.inputlong {
width: 600px;
}
</style>
<table class="widefat">
<?php
$data = self::readArticles();
echo '<thead><th>ID</th><th>User</th><th>Date</th><th>Title</th><th>Images</th><th>Control</th></thead>';
//print_r($data);
foreach ($data as $val) {
$tmp = $val->post_content;
preg_match_all("/".str_replace('/', '\/', self::urlDefault())."[^\"]+|".str_replace('/', '\/', self::urlCurrent())."[^\"]+/i", $tmp, $res);
//print_r($res);
if (!empty($res[0])) {
$final = array();
foreach ($res[0] as $img) $final[md5(str_replace(self::urlDefault(), self::urlCurrent(), $img))] = $img;
//print_r($final);
echo '<tr><td>'.$val->ID.'</td><td>'.get_user_meta($val->post_author, 'nickname', true).'</td>
<td>'.$val->post_date_gmt.'</td><td>'.$val->post_title.'</td><td><table class="widefat child">
<thead><th>CurrentURL</th></thead>';
foreach (array_unique($final) as $key => $img) {
$nurl = self::getPM($val->ID, 'link_'.$key); //<td>'.$img.'</td>
echo '<tr><td>'.(empty($nurl) ? $img : '<a target="_blank" href="'.$nurl.'">'.$nurl.'</a>').'</td></tr>';
}
echo '</table></td><td>
<form action="" method="POST" style="display:inline;">
<input type="hidden" name="do" value="uploadone"/>
<input type="hidden" name="aid" value="'.$val->ID.'"/>
<input type="submit" value="Upload This" />
</form>
</td></tr>';
}
}
?>
</table>
<!-- div -->
</div>
<?php
}
}
class MUST_ftp {
private static $name = 'ftp';
public static $set = array(
'host' => '',
'username' => '',
'password' => '',
'folder' => '',
'folder_url' => '',
'port' => 21,
);
public static function set($data = array()) {
return MUST::arrayRewrite(self::$set, $data);
}
public static function upload($res, $conn) {
$send_file = $res[4].'/'.$res[1].'/'.$res[2].'/'.$res[3];
$remote_file = $res[3];
$ftp_server = $conn['host'];
$ftp_user_name = $conn['username'];
$ftp_user_pass = $conn['password'];
$ftp_dst_dir = $conn['folder'];
$ftp_port = $conn['port']; #Fixed
$dir_level = array($res[1], $res[2]);
require_once plugin_dir_path(__FILE__).'/lib/ftp_do.php';
$final = MUST_ftp_do::ins($ftp_server, $ftp_user_name, $ftp_user_pass, $ftp_dst_dir, $dir_level, $ftp_port)
->send($remote_file, $send_file, FTP_BINARY, $dir_level);
$nurl = $conn['folder_url'].$res[1].'/'.$res[2].'/'.$res[3];
if ($final) return $nurl; else return false;
}
}
// Made by Evlos >w< ||