forked from gocom/MassPlugCompiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrah_plugcompile.php
593 lines (473 loc) · 14.2 KB
/
rah_plugcompile.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
<?php
/**
* MassPlugCompiler
*
* Compiles Textpattern's plugin installer packages from sources.
*
* @author Jukka Svahn
* @copyright (c) 2011 Jukka Svahn
* @license GNU GPLv2
* @link https://github.com/gocom/MassPlugCompiler
*/
/*
* Copyright (c) 2011 Jukka Svahn http://rahforum.biz
* Licensed under GNU Genral Public License version 2
* http://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* The compiler.
*
* @example
* rah_plugcompile::instance()
* ->set('cache', '/path/to');
* ->set('source', '/path/to')
* ->package()
* ->get();
*/
class rah_plugcompile
{
/**
* Get normal package.
*/
const GET_NORMAL = -1;
/**
* Get compressed package.
*/
const GET_COMPRESSED = -2;
/**
* Create new instance.
*/
const INSTANCE_NEW = true;
/**
* Path to source directory.
*
* @var string
*/
public $source;
/**
* Path to cache directory.
*
* @var string
*/
public $cache;
/**
* Plugin data.
*
* @var array
*/
protected $plugin;
/**
* Current file's location.
*
* @var string
*/
protected $path;
/**
* Current filepath's information.
*
* @var array
*/
protected $pathinfo;
/**
* Compiled packages.
*
* @var array
*/
protected $package = array();
/**
* File writing queue.
*
* @var array
*/
protected $write_queue = array();
/**
* Plugin header meta.
*
* @var string
*/
public $header = null;
/**
* Stores Package cache.
*
* @var array
*/
static public $package_cache = null;
/**
* Stores Textile instance.
*
* @var Textile
*/
static public $textileParser = null;
/** * Stores class instances.
*
* @var rah_plugcompile
*/
static public $instance;
/**
* Current running directory.
*
* @var string
*/
static public $rundir;
/**
* Constructor.
*/
public function __construct()
{
if (!self::$rundir) {
self::$rundir = dirname(__FILE__);
}
if (self::$textileParser === null) {
self::$textileParser = false;
$textileParser = 'Netcarver\Textile\Parser';
if (!class_exists($textileParser) &&
file_exists(self::$rundir.'/Netcarver/Textile/Parser.php') &&
file_exists(self::$rundir.'/Netcarver/Textile/DataBag.php') &&
file_exists(self::$rundir.'/Netcarver/Textile/Tag.php')
) {
@include_once self::$rundir.'/Netcarver/Textile/Parser.php';
@include_once self::$rundir.'/Netcarver/Textile/DataBag.php';
@include_once self::$rundir.'/Netcarver/Textile/Tag.php';
}
if (!class_exists($textileParser) && defined('txpath')) {
@include_once txpath.'/vendors/Netcarver/Textile/Parser.php';
@include_once txpath.'/vendors/Netcarver/Textile/DataBag.php';
@include_once txpath.'/vendors/Netcarver/Textile/Tag.php';
}
if (class_exists($textileParser)) {
self::$textileParser = new $textileParser();
}
}
if ($this->header === null) {
$this->header = implode("\n", array(
'# Name: {name} v{version}',
'# {description}',
'# Author: {author}',
'# URL: {author_uri}',
'# Recommended load order: {order}',
'# .....................................................................',
'# This is a plugin for Textpattern - http://textpattern.com/',
'# To install: textpattern > admin > plugins',
'# Paste the following text into the "Install plugin" box:',
'# .....................................................................',
'',
'',
));
}
}
/**
* Gets an instance.
*
* @param bool $new_instance
* @return rah_plugcompile
*/
public static function instance($new_instance = false)
{
if (!self::$instance || $new_instance == true) {
$class = __CLASS__;
self::$instance = new $class();
}
return self::$instance;
}
/**
* Sets a property.
*
* @param string $name
* @param mixed $value
* @return obj
*/
public function set($name, $value)
{
if (!property_exists($this, $name)) {
return $this;
}
$this->$name = $value;
return $this;
}
/**
* Builds plugin's Textpack.
*/
protected function formatTextpack()
{
if (!is_readable($this->path)) {
return;
}
if (is_file($this->path)) {
$this->plugin['textpack'][] = $this->read($this->path);
return;
}
if (is_dir($this->path)) {
foreach ($this->read((array) glob($this->globEscape($this->path) . '/*.textpack', GLOB_NOSORT)) as $file) {
if (strpos($file, '#@language') === false) {
array_unshift($this->plugin['textpack'], $file);
continue;
}
$this->plugin['textpack'][] = $file;
}
}
}
/**
* Builds plugin's meta data from a manifest file.
*/
protected function formatManifest()
{
$file = $this->read($this->path);
if (!$file) {
return;
}
if ($this->pathinfo['extension'] == 'json') {
$manifest = json_decode($file);
foreach ($manifest as $name => $value) {
$name = (string) $name;
if (!isset($this->plugin[$name])) {
continue;
}
$method = 'format' . ucfirst($name);
if (isset($value->file) && method_exists($this, $method)) {
foreach ((array) $value->file as $path) {
$this->path = $this->path(trim($path));
$this->pathinfo = pathinfo($this->path);
$this->$method();
}
} else {
$this->plugin[$name] = (string) $value;
}
}
return;
}
try {
@$manifest = new SimpleXMLElement($file, LIBXML_NOCDATA);
} catch (Exception $exception) {
return;
}
foreach ($manifest as $name => $value) {
$name = (string) $name;
if (!isset($this->plugin[$name])) {
continue;
}
$method = 'format' . ucfirst($name);
if (isset($value->attributes()->file) && method_exists($this, $method)) {
foreach (explode(',', (string) $value->attributes()->file) as $path) {
$this->path = $this->path(trim($path));
$this->pathinfo = pathinfo($this->path);
$this->$method();
}
} else {
$this->plugin[$name] = (string) $value;
}
}
}
/**
* Formats source code. Removes PHP tags and so on.
*/
protected function formatCode()
{
$code = $this->read($this->path);
if (substr(ltrim($code), 0, 5) == '<?php') {
$code = substr_replace(ltrim($code), '', 0, 5);
}
if (substr(rtrim($code), -2, 2) == '?>') {
$code = substr_replace(rtrim($code), '', -2, 2);
}
$this->plugin['code'][basename($this->path).':'.md5($code)] = $code;
}
/**
* Formats help file.
*/
protected function formatHelp()
{
$help = $this->read($this->path);
if ($this->pathinfo['extension'] == 'textile' ||
preg_match('/h1(\(.*\))?\./', $help)
) {
if (self::$textileParser) {
$this->plugin['help'][] = self::$textileParser->TextileThis($help);
} else {
$this->plugin['help_raw'][] = $help;
$this->plugin['allow_html_help'] = 0;
$this->plugin['help'] = '';
}
}
}
/**
* Forms absolute file path.
*
* @param string $path
* @return string
*/
public function path($path)
{
if (strpos($path, './') === 0 || strpos($path, '../') === 0) {
$path = $this->source . '/' . $path;
}
return $path;
}
/**
* Reads contents of file(s).
*
* @param string|array $file
* @return mixed
*/
public function read($file)
{
if (is_array($file)) {
return array_filter(array_map(array($this, 'read'), $file), 'is_string');
}
$file = $this->path($file);
if (!$file || !file_exists($file) || !is_file($file) || !is_readable($file)) {
return false;
}
return file_get_contents($file);
}
/**
* Packages the plugin.
*
* @return obj
*/
public function package()
{
$this->plugin = array(
'name' => '',
'version' => '0.1',
'author' => '',
'author_uri' => '',
'description' => '',
'help' => '',
'code' => array(),
'type' => 0,
'order' => 5,
'flags' => '',
'textpack' => array(),
'allow_html_help' => 1,
);
$this->collectSources();
if (!$this->plugin['code']) {
return $this;
}
if (!$this->plugin['version']) {
$this->plugin['version'] = basename(dirname($this->path));
}
$this->plugin['code'] = implode("\n", $this->plugin['code']);
$this->plugin['help'] = implode("\n", $this->plugin['help']);
$this->plugin['textpack'] = implode("\n", $this->plugin['textpack']);
$this->plugin['md5'] = md5($this->plugin['code']);
$header = $this->header;
foreach ($this->plugin as $tag => $value) {
if (strpos($header, '{'.$tag.'}') !== false) {
$header = str_replace('{'.$tag.'}', (string) $value, $header);
}
}
$filename = $this->plugin['name'] . '_v' . $this->plugin['version'];
$packed = serialize($this->plugin);
$this->package[$filename.'_zip.txt'] =
$header . chunk_split(base64_encode(gzencode($packed)), 72);
$this->package[$filename.'.txt'] =
$header . chunk_split(base64_encode($packed), 72);
if (!$this->cache($this->plugin['name'], $this->plugin['version'])) {
$this->write_queue[] = $filename.'.txt';
$this->write_queue[] = $filename.'_zip.txt';
}
return $this;
}
/**
* Writes current packages to the cache directory.
*
* @return rah_plugcompile
*/
public function write()
{
if (!file_exists($this->cache) || !is_dir($this->cache) || !is_writable($this->cache)) {
return $this;
}
foreach ($this->write_queue as $name) {
file_put_contents(
$this->cache . '/' . $name,
$this->package[$name]
);
}
return $this;
}
/**
* Gets last compiled installer.
*
* @param int $offset
* @return string Installer package
*/
public function get($offset = self::GET_NORMAL)
{
return implode('', array_slice($this->package, $offset, 1));
}
/**
* Collects files from a directory.
*/
protected function collectSources()
{
foreach ((array) glob($this->globEscape($this->source).'/*', GLOB_NOSORT) as $path) {
$this->path = $path;
$this->pathinfo = pathinfo($path);
if (!isset($this->plugin[$this->pathinfo['filename']])) {
if ($this->pathinfo['filename'] == 'manifest') {
$this->formatManifest();
}
if ($this->pathinfo['filename'] == 'textpacks') {
$this->formatTextpack();
}
if (!isset($this->pathinfo['extension'])) {
continue;
}
if ($this->pathinfo['extension'] == 'php') {
$this->formatCode();
}
if ($this->pathinfo['extension'] == 'textile') {
$this->formatHelp();
}
continue;
}
$method = 'format'. ucfirst($this->pathinfo['filename']);
if (method_exists($this, $method)) {
$this->$method();
} else {
$this->plugin[$this->pathinfo['filename']] = $this->read($path);
}
}
}
/**
* Checks whether compiled installer file is located in the cache dir.
*
* @param string $name
* @param string $version
* @return bool|rah_plugcompile
*/
public function cache($name = null, $version = null)
{
if (!file_exists($this->cache) || !is_dir($this->cache) || !is_readable($this->cache)) {
return false;
}
if (self::$package_cache === null) {
self::$package_cache = array();
foreach ((array) glob($this->globEscape($this->cache) . '/*.txt', GLOB_NOSORT) as $f) {
if ($f && is_file($f)) {
$n = explode('_', basename($f, '.txt'));
if (end($n) == 'zip') {
unset($n[count($n)-1]);
}
self::$package_cache[implode('_', array_slice($n, 0, -1))][ltrim(end($n), 'v')] = true;
}
}
}
if ($name !== null && $version !== null) {
return isset(self::$package_cache[$name][$version]);
}
return $this;
}
/**
* Escapes glob wildcard characters.
*
* @param string $filename
* @return string
*/
public function globEscape($filename)
{
return preg_replace('/(\*|\?|\[)/', '[$1]', $filename);
}
}