This repository has been archived by the owner on Feb 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gelcms.php
707 lines (574 loc) · 18.9 KB
/
gelcms.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
<?php
/**
* Gel CMS
* Created by Corey Maass at Gelform Inc
* http://gelform.com/gelcms
*
*
*
* Description
* ============================
* Why does the world need another CMS? When working with most MVC
* frameworks, it's a huge pain to wedge another cms into my existing
* views. With this class, you define 3 constants, add one folder and
* you're done. Create includes, and put them in your views wherever
* you like.
*
*
*
* How to set it up
* ============================
* 1. Create a route in your application that can accept GET and POST
* requests.
*
* For example, in the Slim Framework, I added this route:
* $app->map('/cms/', array(new Controller_Cms(), 'index'))
* ->via('GET', 'POST');
*
* Or in Zend, add a controller called CmsController and add an
* action called indexAction();
*
* 2. Add an assets folder accessible from the web and make it writeable. So
* if your webroot is /public, you might add a folder called /cms (so your
* path would look like /public/cms). Then "chmod 777 cms" so it's writeable.
*
* 3. Back in your controller action, define 3 constants:
* // set this to be any string, or pull it from a config
* define('GELCMS_PASSWORD', 'password');
*
* // set this to the path of the assets folder you created in step 2.
* define('GELCMS_PATH', APPLICATION_DIR . 'public_html/cms');
*
* // Redundant, I know, but set this to the absolute path to the
* // same assets folder.
* define('GELCMS_URI', '/cms');
*
* 4. Include the GelCMS class:
* require APPLICATION_DIR . 'model/gelcms.php';
*
* 5. That's it! Visit the route you created, and you shuld be asked
* to sign in.
*
*
*
* How to use it
* ============================
* 1. Sign in using the password you set in the constant
* GELCMS_PASSWORD
* 2. A "section" is just an html blob. Click the button to "create
* a new section"
* 3. Give it a name, and add HTML to your hearts content.
* 4. Use the "images" button to upload images, or select images
* you've uploaded, previously. They will be uploaded to a "img"
* folder in the assets folder your created.
* 5. Save it.
* 6. Now at the bottom, below the HTML form, you'll see a link for
* the "PHP include statement". Copy this, and put it in your view
* scripts wherever you want the HTML to render.
*
*
*
* technical stuff
* ============================
* When you run it the first time, it will add 3 new folders in
* the folder you created. If you get an error, make sure the folder
* is writeable.
*
* The CMS uses jquery, TinyMCE and Twitter Bootstrap, loaded from
* CDNs. So you'll need an internet connection for presentation,
* image upload and some behavior. The core of the app should work
* without it, however. And it looks pretty good on mobile!
*
*
*
* To do
* ============================
* - option to import CSS into TinyMCE
* - limit number of revisions (delete after a certain amount)
* - multiple users, user management
* - put sections in buckets, collections
* - limit uploads to images
* - image upload resize
*
*/
// in case the session is not started, we need it for storing "signed in" - requires PHP >= 5.4.0
// for versions < 5.4 use session_id() == ''
if (session_status() == PHP_SESSION_NONE)
{
session_start();
}
class GelCMS
{
// where we store the html templates
protected $html = array();
// where we pass data to rendered html
protected $viewData = array();
// the folders where we put our data
protected $folders = array (
'data' => 'data',
'html' => 'html',
'img' => 'img'
);
// the orgainzation for each data item
protected $schemas = array (
'section' => array (
'id' => '',
'name' => '',
'updated' => '',
'isactive' => '',
'revisions' => array ()
),
'revision' => array (
'date' => '',
'html' => ''
)
);
// called first from construct to create our html templates
private function _htmlSetup()
{
$this->html['wrapper'] = function()
{
extract($this->viewData);
?>
<!DOCTYPE html>
<html>
<head>
<title>Gel CMS - A single file, single class CMS for MVC frameworks</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<!-- <link href="//netdna.bootstrapcdn.com/bootswatch/3.0.0/flatly/bootstrap.min.css" rel="stylesheet"> -->
<style>
body {margin: 6em 0;}
.navbar-btn {margin-left: .618em;}
body .modal { width: 90%; margin: 0 auto; }
body .modal-dialog {width: auto;}
iframe {height: 1px; width: 1px;}
.thumbnail {max-height: 10em; overflow: hidden;}
</style>
</head>
<body>
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<a href="<?= $_SERVER['REQUEST_URI'] ?>" class="navbar-brand">Gel CMS</a>
<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div><!-- navbar-header -->
<?php if ( $signedIn ) : ?>
<div class="navbar-collapse collapse" id="navbar-main">
<ul class="nav navbar-nav navbar-right">
<li>
<form action="" method="post">
<p>
<button type="submit" class="btn navbar-btn">Sign out</button>
</p>
<input type="hidden" name="action" value="signout" />
</form>
</li>
<li>
<form action="" method="post">
<p>
<button type="submit" class="btn navbar-btn">New section</button>
</p>
<input type="hidden" name="action" value="new" />
</form>
</li>
<li>
<form action="" method="get">
<p>
<button type="submit" class="btn navbar-btn">Edit another</button>
</p>
</form>
</li>
</ul>
</div><!-- navbar -->
<?php endif // signedIn ?>
</div><!-- container -->
</div><!-- navbar -->
<div class="container" id="container-body">
<?php if ( $alert ) : ?>
<p class="alert alert-success">
<?= $alert ?>
</p><!-- alert -->
<?php endif ?>
<?= $body ?>
</div><!-- container -->
<script src="http://code.jquery.com/jquery.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script>
<script>
// http://www.tinymce.com/wiki.php/Plugins
tinymce.init({
selector:'textarea',
plugins: "link, preview, image, code, autolink, autoresize"
});
function populateGallery()
{
if ( $("#iframe-images").length === 0 )
{
return false;
};
var imagesArr = $("#iframe-images")[0].contentWindow.imagesArr;
var $modalimagesrow = $('#modal-gallery-row').empty();
if ( undefined !== imagesArr )
{
for (var i = imagesArr.length - 1; i >= 0; i--)
{
var src = imagesArr[i];
$('<div class="col-md-3">'
+ '<a href="#" class="thumbnail">'
+ '<img src="' + src + '"></a>'
+ '</div>'
)
.appendTo($modalimagesrow);
};
};
}
$(document).ready(function(){
$('#modal-gallery')
.on('show.bs.modal', function ()
{
populateGallery();
});
$("#iframe-images").on('load', function(){
populateGallery();
});
$('#modal-gallery-row').on(
'click',
'img',
function()
{
var src = $(this).prop('src');
// http://stackoverflow.com/a/5193042/38241
var ed = tinyMCE.get('html');
var range = ed.selection.getRng();
var newNode = ed.getDoc().createElement ( "img" );
newNode.src= src;
range.insertNode(newNode);
$('#modal-gallery').modal('hide');
return false;
}
);
$("#include").on("click", function () {
$(this).select();
});
});
</script>
</body>
</html>
<?php }; // wrapper
$this->html['signin'] = function()
{
extract($this->viewData);
?>
<div class="col-md-4 col-md-offset-4">
<h1>Sign in</h1>
<div class="well">
<form action="" method="post" role="form">
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Password">
</div>
<p>
<button type="submit" class="btn btn-lg btn-block btn-primary">Sign in</button>
<input type="hidden" name="action" value="signin" />
</p>
</form>
</div><!-- well -->
</div><!-- col -->
<?php }; // signin
$this->html['chooser'] = function()
{
extract($this->viewData);
?>
<?php if ( $sections ) : ?>
<div class="col-md-4 col-md-offset-4">
<h1>What do you want to edit?</h1>
<div class="well">
<form action="" method="post" role="form">
<div class="form-group">
<label for="id">Sections</label>
<select name="id" id="id" class="form-control">
<option value="">-- Choose one --</option>
<?php foreach ($sections as $id => $name) : ?>
<option value="<?= $id ?>"><?= $name ?></option>
<?php endforeach ?>
</select>
</div>
<p>
<button type="submit" class="btn btn-block btn-lg btn-primary">Edit it!</button>
<input type="hidden" name="action" value="edit" />
</p>
</form>
<p class="lead text-center">Or </p>
<?php endif // sections ?>
<form action="" method="post" role="form">
<button type="submit" class="btn btn-block btn-primary">Create a new section</button>
<input type="hidden" name="action" value="new" />
</form>
</div><!-- well -->
</div><!-- col -->
<?php }; // chooser
$this->html['edit'] = function()
{
extract($this->viewData);
?>
<h1>Make your changes</h1>
<div class="well">
<form action="" method="post" role="form">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" id="name" class="form-control" placeholder="i.e. about us page" value="<?= $section->name ?>" required>
</div>
<p class="pull-right">
<a data-toggle="modal" href="#modal-gallery" class="btn btn-primary">Images</a>
</p>
<div class="form-group" style="padding-top: 1.618em;">
<label for="html">Content:</label>
<?php $newest = count($section->revisions) > 0 ? max(array_keys($section->revisions)) : 0; ?>
<textarea name="html" id="html" rows="10" class="form-control"><?= $section->revisions[$newest]->html ?></textarea>
</div>
<p>
<button type="submit" class="btn btn-block btn-lg btn-primary">Save it!</button>
<input type="hidden" name="action" value="save" />
<input type="hidden" name="id" value="<?= $section->id ?>" />
</p>
</form>
</div><!-- well -->
<?php if ( !$section->new ) : ?>
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion" href="#accordion-include">PHP include statement:</a>
</h4>
</div>
<div id="accordion-include" class="panel-collapse collapse">
<div class="panel-body">
<form class="form-horizontal" role="form">
<div class="form-group">
<div class="col-lg-12">
<input type="text" class="form-control" id="include" value="<?php include "<?= GELCMS_PATH . '/' . $this->folders['html'] . '/' ?><?= $section->id ?>.html"; ?>">
</div>
</div>
</form>
</div>
</div>
</div><!-- panel -->
</div><!-- panel-group -->
<?php endif // $section->id ?>
<div class="modal fade" id="modal-gallery">
<div class="modal-dialog">
<div class="modal-content">
<?php if ( defined('GELCMS_URI') ) : ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<form action="?action=upload" method="post" target="iframe-images" enctype="multipart/form-data" class="form-inline well text-center" role="form">
<div class="form-group">
<label class="sr-only" for="image">Image:</label>
<input type="file" name="image" id="image" />
</div>
<button type="submit" class="btn btn-sm">upload</button>
</form>
</div>
<div class="modal-body">
<div class="row" id="modal-gallery-row">
</div><!-- row -->
</div><!-- modal-body -->
<iframe name="iframe-images" id="iframe-images" src="<?= substr( $_SERVER['REQUEST_URI'], 0, strrpos( $_SERVER['REQUEST_URI'], "?")) ?>?action=upload"></iframe>
<?php else : // GELCMS_URI ?>
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<p class="lead">
To use the image gallery, please define the constant GELCMS_URI.
</p>
</div><!-- modal-header -->
<?php endif // GELCMS_URI ?>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<?php }; // chooser
$this->html['gallery'] = function()
{
extract($this->viewData);
?>
<script>
var imagesArr = <?= json_encode($images) ?>;
</script>
<?php }; // chooser
}
protected function render($template, $useWrapper = TRUE)
{
if ( !$useWrapper )
{
call_user_func($this->html[$template]);
exit;
}
ob_start();
call_user_func($this->html[$template]);
$body = ob_get_clean();
$this->viewData['body'] = $body;
if ( isset($_SESSION['alert']) )
{
$this->viewData['alert'] = $_SESSION['alert'];
unset($_SESSION['alert']);
}
call_user_func($this->html['wrapper']);
exit;
}
public function __construct()
{
if ( !defined('GELCMS_PASSWORD') ) exit ('Please set a password in the constant GELCMS_PASSWORD');
// setup html templates
$this->_htmlSetup();
// make sure our folders exist
foreach ($this->folders as $folder)
{
if ( !is_dir(GELCMS_PATH . '/' . $folder) )
{
if ( !mkdir(GELCMS_PATH . '/' . $folder) ) exit('cms asset folder is not writable');
}
}
// if not post and not signed in
if ( $_SERVER['REQUEST_METHOD'] !== 'POST' && ! (bool) $_SESSION['GELCMS_admin_signedIn'] )
{
$this->render('signin');
}
if ( isset($_SESSION['GELCMS_admin_signedIn']) )
{
$this->viewData['signedIn'] = TRUE;
}
// handle file uploads
if ( $_GET['action'] == 'upload' )
{
$allowed_types = array('image/jpeg', 'image/jpg', 'image/png', 'image/gif');
if( !empty($_FILES) && $_FILES["image"]["error"] == 0 && in_array($_FILES["image"]['type'], $allowed_types) )
{
// save it
move_uploaded_file(
$_FILES["image"]["tmp_name"],
GELCMS_PATH . '/' . $this->folders['img'] . '/' . $_FILES["image"]["name"]
);
}
$dir = new DirectoryIterator(GELCMS_PATH . '/' . $this->folders['img']);
$imgArr = array();
foreach ($dir as $fileinfo)
{
if (!$fileinfo->isDot())
{
$name = $fileinfo->getFilename();
$imgArr[] = GELCMS_URI . '/' . $this->folders['img'] . '/' . $name;
}
}
$this->viewData['images'] = $imgArr;
$this->render('gallery', FALSE);
exit;
} // upload
// if not post, show chooser
if ( $_SERVER['REQUEST_METHOD'] !== 'POST' )
{
$dir = new DirectoryIterator(GELCMS_PATH . '/' . $this->folders['data']);
$sectionsArr = array();
foreach ($dir as $fileinfo)
{
if (!$fileinfo->isDot())
{
$id = $fileinfo->getFilename();
$section = unserialize(file_get_contents(GELCMS_PATH . '/' . $this->folders['data'] . '/' . $id));
$sectionsArr[$id] = $section->name;
}
}
$this->viewData['sections'] = $sectionsArr;
$this->render('chooser');
} // chooser
// if is post
switch ($_POST['action'])
{
// process signing in
case 'signin':
if ( $_POST['password'] != GELCMS_PASSWORD )
{
$this->viewData['alert'] = 'Whoops! That password was incorrect. Give it another try?';
$this->render('signin');
}
// if password is correct, set the session variable
$_SESSION['GELCMS_admin_signedIn'] = TRUE;
// and reload the page
$_SESSION['alert'] = 'Welcome back!';
header('Location: '.$_SERVER['REQUEST_URI']);
exit;
break;
case 'signout':
unset($_SESSION['GELCMS_admin_signedIn']);
// and reload the page
$_SESSION['alert'] = 'Okay, you\'ve been signed out.';
header('Location: '.$_SERVER['REQUEST_URI']);
exit;
break;
case 'new':
// create a uniq object id
$id = uniqid();
// create our section object
$section = (object) $this->schemas['section'];
$section->id = $id;
$section->new = TRUE;
// continue from 'new'
case 'edit':
// load our section object if we're editing it
if ( !isset($id) )
{
$id = $_POST['id'];
// try loading the saved section object
if ( !is_file(GELCMS_PATH . '/' . $this->folders['data'] . '/' . $id) )
{
// if it's not found, alert the user
$_SESSION['alert'] = 'Whoops, there was a problem loading that section.';
header('Location: '.$_SERVER['REQUEST_URI']);
exit;
}
// otherwise, load it
$section = unserialize(file_get_contents(GELCMS_PATH . '/' . $this->folders['data'] . '/' . $id));
}
// populate the edit template
$this->viewData['section'] = $section;
$this->render('edit');
break;
case 'save':
// get id
$id = $_POST['id'];
// try to load existing
if ( is_file(GELCMS_PATH . '/' . $this->folders['data'] . '/' . $id) )
{
$section = unserialize(file_get_contents(GELCMS_PATH . '/' . $this->folders['data'] . '/' . $id));
}
else
{
// otherwise, create a new section object
$section = (object) $this->schemas['section'];
$section->id = $_POST['id'];
}
$now = date('U');
// overwrite the name, in case they changed it
$section->name = $_POST['name'];
// add a new revision
$revision = (object) $this->schemas['revision'];
$revision->html = $_POST['html'];
$revision->date = $now;
$section->updated = $now;
$section->revisions[$now] = $revision;
// save the data object
file_put_contents(GELCMS_PATH . '/' . $this->folders['data'] . '/' . $_POST['id'], serialize($section));
// save the html for quick rendering
file_put_contents(GELCMS_PATH . '/' . $this->folders['html'] . '/' . $_POST['id'] . '.html', $revision->html);
$this->viewData['alert'] = 'Saved!';
$this->viewData['section'] = $section;
$this->render('edit');
break;
} // switch
} // init
} // class
new GelCMS();
exit;