-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.php
583 lines (436 loc) · 18.6 KB
/
init.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
<?php
// kleeja plugin
// kj_ftp
// version: 1.0
// developer: kleeja team
// prevent illegal run
if (! defined('IN_PLUGINS_SYSTEM'))
{
exit();
}
// 1- create_folder, generate htaccess: done
// 2- kleeja_unlink: done
// plugin basic information
$kleeja_plugin['kj_ftp']['information'] = [
// the casucal name of this plugin, anything can a human being understands
'plugin_title' => [
'en' => 'Kleeja Multi-FTP Uploading',
'ar' => 'تحميل FTP متعدد'
],
// who is developing this plugin?
'plugin_developer' => 'kleeja.com',
// this plugin version
'plugin_version' => '1.1.1',
// explain what is this plugin, why should i use it?
'plugin_description' => [
'en' => 'Add Multi-FTP support to Kleeja',
'ar' => 'إضافة دعم التحميل لعدة FTP في كليجا'
],
//settings page, if there is one (what after ? like cp=j_plugins)
'settings_page' => 'cp=kj_ftp_options',
// min version of kleeja that's required to run this plugin
'plugin_kleeja_version_min' => '2.0',
// max version of kleeja that support this plugin, use 0 for unlimited
'plugin_kleeja_version_max' => '3.9',
// should this plugin run before others?, 0 is normal, and higher number has high priority
'plugin_priority' => 0
];
//after installation message, you can remove it, it's not required
$kleeja_plugin['kj_ftp']['first_run']['ar'] = '
شكراً لاستخدامك إضافة الـFTP المتعدد لكليجا.
';
$kleeja_plugin['kj_ftp']['first_run']['en'] = '
Thank you for using our plugin.
';
// plugin installation function
$kleeja_plugin['kj_ftp']['install'] = function ($plg_id) {
global $dbprefix, $SQL;
//create table
$sql = "CREATE TABLE IF NOT EXISTS `{$dbprefix}kj_ftp_info` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) COLLATE utf8_bin DEFAULT NULL,
`host` varchar(199) COLLATE utf8_bin DEFAULT NULL,
`username` varchar(199) COLLATE utf8_bin DEFAULT NULL,
`password` varchar(199) COLLATE utf8_bin DEFAULT NULL,
`port` int(4) DEFAULT '21',
`root` varchar(199) COLLATE utf8_bin DEFAULT '',
`passive` tinyint(1) DEFAULT '1',
`ssl` tinyint(1) DEFAULT '1',
`timeout` int(4) DEFAULT '60',
`link` varchar(199) COLLATE utf8_bin DEFAULT NULL,
`active` tinyint(1) NOT NULL DEFAULT '0',
`group` int(4) DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;";
$SQL->query($sql);
//new language variables
add_olang([
'R_KJ_FTP_OPTIONS' => 'تحميل الـFTP المتعدد',
'KJ_FTP_OPT_NAME' => 'الإسم الثابت',
'KJ_FTP_OPT_NAME_HELP' => 'كليجا تستخدم الإسم الثابت لحفظ وجلب الملفات، عند تغيير الإسم الثابت لن نستطيع جلب الملفات! عدل الإسم في حالة واحدة فقط، وهي لربط ملفات سابقة بحساب قديم تم حذفه.',
'KJ_FTP_OPT_HOST' => 'الخادم',
'KJ_FTP_OPT_HOST_HELP' => ' مثل: kleeja.com أو سب دومين sub.kleeja.com أو آي بي 188.54.12.11',
'KJ_FTP_OPT_USERNAME' => 'اسم المستخدم',
'KJ_FTP_OPT_PASSWORD' => 'كلمة المرور (مخفية، أكتب جديدة فقط لو أردت التغيير)',
'KJ_FTP_OPT_PORT' => 'منفذ',
'KJ_FTP_OPT_ROOT' => 'المسار الجذر',
'KJ_FTP_OPT_ROOT_HELP' => 'المسار الذي ستقوم كليجا بإنشاء مجلد uploads داخله، غالباً دعه فارغ',
'KJ_FTP_OPT_PASSIVE_HELP' => 'وضع FTP الآمن, قد يكون أبطئ',
'KJ_FTP_OPT_ACTIVE' => 'مُفعل',
'KJ_FTP_OPT_ACTIVE_HELP' => 'الحساب غير المفعل لن يستلم تحميلات جديدة ولكن سيتم جلب الملفات التي تم تحميلها مسبقاً منه.',
'KJ_FTP_OPT_LINK' => 'الرابط الفعلي',
'KJ_FTP_OPT_LINK_HELP' => 'مثل: http://kleeja.com أو https://www.example.com/ftp1، لو ترك فارغاً سيتم إستخدام إنتاج رابط من الخادم ومجلد الروت.',
'KJ_FTP_ADD_NEW_ACCOUNT' => 'أضف حساب FTP جديد',
'KJ_FTP_ADD_NEW_ACCOUNT_EXP' => ' هل أنت متأكد من إضافة حساب FTP جديد؟<br><small>يمكنك إضافة بيانات الحساب بعد الإضافة وتعديل الحساب.</small>',
'KJ_FTP_ACCOUNT_ADDED' => 'تم إضافة الحساب بنجاح',
'KJ_FTP_ACCOUNT_UPDATED' => 'تم تحديث الحساب بنجاح',
'KJ_FTP_ACCOUNT_NAME_CONFLICT' => 'الأسم الثابت موجود في حساب آخر! لايمكنك إستخدام اسم ثابت واحد في حسابين',
'KJ_FTP_NO_ACTIVE_ACCOUNTS_NOTE' => 'لآنه لايوجد أي حساب FTP تم تفعيله للتحميل، فإن كليجا ستستخدم نظام التحميل الإفتراضي المحلي. لجعل كليجا تقوم بالتحميل بإستخدام الإضافة، <a href="./?cp=kj_ftp_options">قم بتفعيل حساب FTP للتحميل</a> الآن!',
],
'ar',
$plg_id);
add_olang([
'R_KJ_FTP_OPTIONS' => 'KJ - Multi-FTP Uploading',
'KJ_FTP_OPT_NAME' => 'Unique Name',
'KJ_FTP_OPT_NAME_HELP' => 'Kleeja uses this name to identify that a file is related to this FTP account. Changing it will ruin the old files connection. Only edit this if you want to recover a connection of an old FTP account that has been deleted.',
'KJ_FTP_OPT_HOST' => 'Host',
'KJ_FTP_OPT_HOST_HELP' => ' Like: kleeja.com or a subdomain: sub.kleeja.com or an IP: 188.54.12.11',
'KJ_FTP_OPT_USERNAME' => 'Username',
'KJ_FTP_OPT_PASSWORD' => 'Password (hidden, type a new password only you want to change it)',
'KJ_FTP_OPT_PORT' => 'Port',
'KJ_FTP_OPT_ROOT' => 'Root Path',
'KJ_FTP_OPT_ROOT_HELP' => 'The path where Kleeja will create "uploads" folder in. Usually keeping it empty is fine.',
'KJ_FTP_OPT_PASSIVE_HELP' => 'Secure Passive FTP mode, slower.',
'KJ_FTP_OPT_ACTIVE' => 'Active',
'KJ_FTP_OPT_ACTIVE_HELP' => 'Inactive account will not receive new uploads to it, but will continue serving previous upload from it.',
'KJ_FTP_OPT_LINK' => 'Direct Link',
'KJ_FTP_OPT_LINK_HELP' => 'Link: http://kleeja.com or https://www.example.com/ftp1; If left empty, we will try to generate a link from the ftp host and given root folder.',
'KJ_FTP_ADD_NEW_ACCOUNT' => 'Add New FTP Account',
'KJ_FTP_ADD_NEW_ACCOUNT_EXP' => 'Are you sure of adding a new FTP account? <br><small>You can edit the account information after adding it.</small>',
'KJ_FTP_ACCOUNT_ADDED' => 'The FTP account has been added successfully!',
'KJ_FTP_ACCOUNT_UPDATED' => 'The FTP account has been updated successfully!',
'KJ_FTP_ACCOUNT_NAME_CONFLICT' => 'The unique name is existed before, you can not have two accounts with same unique name!',
'KJ_FTP_NO_ACTIVE_ACCOUNTS_NOTE' => 'Because you did not activate any FTP account, Kleeja will fallback to the default local uploading method. To make Kleeja Use FTP uploading method, <a href="./?cp=kj_ftp_options">activate an FTP account</a> now!',
],
'en',
$plg_id);
};
//plugin update function, called if plugin is already installed but version is different than current
$kleeja_plugin['kj_ftp']['update'] = function ($old_version, $new_version) {
// if(version_compare($old_version, '0.5', '<')){
// //... update to 0.5
// }
//
// if(version_compare($old_version, '0.6', '<')){
// //... update to 0.6
// }
//you could use update_config, update_olang
};
// plugin uninstalling, function to be called at uninstalling
$kleeja_plugin['kj_ftp']['uninstall'] = function ($plg_id) {
//delete options
// delete_config(array(
// 'kj_ftp_home_meta_description',
// 'kj_ftp_home_meta_keywords'
// ));
//delete language variables
foreach (['ar', 'en'] as $language)
{
delete_olang(null, $language, $plg_id);
}
};
// plugin functions
$kleeja_plugin['kj_ftp']['functions'] = [
//add to admin menu
'begin_admin_page' => function ($args) {
$adm_extensions = $args['adm_extensions'];
$ext_icons = $args['ext_icons'];
$adm_extensions[] = 'kj_ftp_options';
$ext_icons['kj_ftp_options'] = 'cloud-upload';
return compact('adm_extensions', 'ext_icons');
},
'not_exists_kj_ftp_options' => function() {
$include_alternative = dirname(__FILE__) . '/kj_ftp_options.php';
return compact('include_alternative');
},
'begin_index_page' => function() {
if (defined('DISABLE_KLJ_FTP'))
{
return;
}
$uploadingMethodClass = dirname(__FILE__) . '/ftpUploader.php';
return compact('uploadingMethodClass');
},
'kleeja_get_link_func2' => function($args) {
if (defined('DISABLE_KLJ_FTP'))
{
return;
}
global $config;
if (($args['pid'] == 'image' || $args['pid'] == 'thumb') && $config['id_form_img'] == 'direct')
{
$realFolder = str_replace(array_keys($args['extra']), array_values($args['extra']), $args['links'][$args['pid']]);
$uniqueName = '';
if (strpos($realFolder, 'ftp://') !== false)
{
$afterFTP = explode('ftp://', $realFolder, 2);
$folder = explode(':', $afterFTP[1], 2);
$uniqueName = $folder[0];
$realFolder = $folder[1];
if (empty($uniqueName))
{
$return_link = getKleejaFtpInstance()->getLink('') . $realFolder;
}
else
{
$return_link = getKleejaFtpLink($uniqueName, $realFolder);
}
return compact('return_link');
}
}
},
'down_go_page' => function($args) {
if (defined('DISABLE_KLJ_FTP'))
{
return;
}
if (strpos($args['f'], 'ftp://') !== false)
{
define('MAKE_DOPHP_301_HEADER', true);
$afterFTP = explode('ftp://', $args['f'], 2);
$filename = $args['n'];
$folder = explode(':', $afterFTP[1], 2);
$uniqueName = $folder[0];
$realFolder = ig('thmb') || ig('thmbf') ? $folder[1] . '/thumbs' : $folder[1];
$path_file = getKleejaFtpLink($uniqueName, $realFolder, $filename);
return compact('path_file');
}
},
'kleeja_unlink_func' => function($args) {
if (defined('DISABLE_KLJ_FTP'))
{
return;
}
if (strpos($args['filePath'], 'ftp://') !== false)
{
$afterFTP = explode('ftp://', $args['filePath'], 2);
$path = explode(':', $afterFTP[1], 2);
$uniqueName = $path[0];
$filePath = $path[1];
$ftpAccount = getKleejaFtpAccount($uniqueName);
getKleejaFtpAccountInstance($ftpAccount)->delete($filePath);
// getKleejaFtpAccountInstance($ftpAccount)->close();
$return = true;
return compact('return');
}
},
'end_common' => function() {
global $dbprefix, $SQL;
if (! $SQL->num_rows(
$SQL->query("SELECT active FROM {$dbprefix}kj_ftp_info WHERE active=1")
)
) {
define('DISABLE_KLJ_FTP', true);
}
},
'stats_start_admin' => function($args) {
$ADM_NOTIFICATIONS = $args['ADM_NOTIFICATIONS'];
if (defined('DISABLE_KLJ_FTP'))
{
global $lang, $olang;
$ADM_NOTIFICATIONS['kljFtpNoActive'] = [
'id' => 'kljFtpNoActive',
'msg_type'=> 'info',
'title' => $lang['NOTE'] . ' (' . $olang['R_KJ_FTP_OPTIONS'] . ')',
'msg' => $olang['KJ_FTP_NO_ACTIVE_ACCOUNTS_NOTE']
];
}
else
{
unset($ADM_NOTIFICATIONS['htaccess_u'], $ADM_NOTIFICATIONS['htaccess_t'], $ADM_NOTIFICATIONS['no_thumbs']);
}
return compact('ADM_NOTIFICATIONS');
},
//support video plugin
'plugin:video_player:do_display' => function($args) {
$folder = $args['file_info']['folder'];
$filename = $args['file_info']['name'];
if (strpos($folder, 'ftp://') !== false)
{
$afterFTP = explode('ftp://', $folder, 2);
$path = explode(':', $afterFTP[1], 2);
$uniqueName = $path[0];
$realFolder = $path[1];
$video_path = getKleejaFtpLink($uniqueName, $realFolder, $filename);
return compact('video_path');
}
},
//support pdf plugin
'plugin:pdf_viewer:do_display' => function($args) {
$folder = $args['file_info']['folder'];
$filename = $args['file_info']['name'];
if (strpos($folder, 'ftp://') !== false)
{
$afterFTP = explode('ftp://', $folder, 2);
$path = explode(':', $afterFTP[1], 2);
$uniqueName = $path[0];
$realFolder = $path[1];
$pdf_path = getKleejaFtpLink($uniqueName, $realFolder, $filename);
return compact('pdf_path');
}
}
];
if (! function_exists('getKleejaFtpInstance'))
{
/**
* @return kleeja_ftp|null
*/
function getKleejaFtpInstance()
{
if (! class_exists('kleeja_ftp'))
{
require_once dirname(__FILE__) . '/ftp.php';
}
/** @var kleeja_ftp $kljFtp */
static $kljFtp = null;
if (is_null($kljFtp))
{
global $dbprefix, $SQL, $cache;
//#### ------ > cached random, solve inadequate mySQL RAND.
if (! ($ftp_accounts = $cache->get('klj_ftp::ftp_names')))
{
$query = [
'SELECT' => 'k.name',
'FROM' => "`{$dbprefix}kj_ftp_info` k",
'WHERE' => 'k.active = 1'
];
$result = $SQL->build($query);
$ftp_accounts = [];
while ($row=$SQL->fetch_array($result))
{
array_push($ftp_accounts, $row['name']);
}
$SQL->freeresult($result);
$cache->save('klj_ftp::ftp_names', $ftp_accounts);
}
if (sizeof($ftp_accounts) == 0)
{
kleeja_show_error(102, 'NO FTP ACCOUNT FOUND!', __FILE__, __LINE__);
return null;
}
//###
//random
shuffle($ftp_accounts);
shuffle($ftp_accounts);
shuffle($ftp_accounts);
$ftp_account = getKleejaFtpAccount($ftp_accounts[0]);
$kljFtp = new kleeja_ftp();
$connect = $kljFtp->open(
$ftp_account['host'],
$ftp_account['username'],
$ftp_account['password'],
$ftp_account['port'],
$ftp_account['root'],
$ftp_account['passive'] == 1,
$ftp_account['ssl'] == 1,
$ftp_account['timeout']
);
if (! $connect)
{
kleeja_show_error(102, 'FTP ACCOUNT CAN NOT CONNECT (' . $ftp_account['name'] . ')!', __FILE__, __LINE__);
}
$kljFtp->setUniqueName($ftp_account['name']);
$kljFtp->setLink($ftp_account['link']);
register_shutdown_function(function() {
getKleejaFtpInstance()->close();
});
}
return $kljFtp;
}
}
if (! function_exists('getKleejaFtpAccountInstance'))
{
/**
* @return kleeja_ftp|null
* @param mixed $ftp_account
*/
function getKleejaFtpAccountInstance($ftp_account)
{
if (! class_exists('kleeja_ftp'))
{
require_once dirname(__FILE__) . '/ftp.php';
}
/** @var kleeja_ftp $kljFtp */
static $kljFtp = null;
if ($kljFtp === null || $kljFtp->getUniqueName() !== $ftp_account['name'])
{
$kljFtp = new kleeja_ftp();
$connect = $kljFtp->open(
$ftp_account['host'],
$ftp_account['username'],
$ftp_account['password'],
$ftp_account['port'],
$ftp_account['root'],
$ftp_account['passive'] == 1,
$ftp_account['ssl'] == 1,
$ftp_account['timeout']
);
if (! $connect)
{
kleeja_show_error(102, 'FTP ACCOUNT CAN NOT CONNECT (' . $ftp_account['name'] . ')!', __FILE__, __LINE__);
}
$kljFtp->setUniqueName($ftp_account['name']);
$kljFtp->setLink($ftp_account['link']);
register_shutdown_function(function() use ($kljFtp) {
$kljFtp->close();
});
}
return $kljFtp;
}
}
if (! function_exists('getKleejaFtpAccount'))
{
function getKleejaFtpAccount($uniqueName)
{
global $dbprefix, $SQL;
$query = [
'SELECT' => 'k.*',
'FROM' => "`{$dbprefix}kj_ftp_info` k",
'WHERE' => "k.name = '" . $SQL->escape($uniqueName) . "'",
'LIMIT' => '1'
];
$result = $SQL->build($query);
if ($SQL->num_rows($result))
{
return $SQL->fetch_array($result);
}
return false;
}
}
if (! function_exists('getKleejaFtpLink'))
{
function getKleejaFtpLink($uniqueName, $folder, $filename = '')
{
$ftp_account = getKleejaFtpAccount($uniqueName);
if ($ftp_account == false)
{
return '...' . $uniqueName . '...';
}
$link = ! empty($ftp_account['link']) ? trim($ftp_account['link']) : '';
$host = trim($ftp_account['host']);
$rootPath = trim($ftp_account['root']);
if ($link != '')
{
return ltrim($link, '/') . '/' . $folder . ($filename !== '' ? '/' . $filename : '');
}
if ($rootPath != '')
{
if (substr($rootPath, -1, 1) == '/')
{
$rootPath = substr($rootPath, 0, -1);
}
}
return $host . '/' . $rootPath . '/' . $folder . ($filename !== '' ? '/' . $filename : '');
}
}