/
field.php
1774 lines (1512 loc) · 74.4 KB
/
field.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
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
The contents of this file are subject to the Common Public Attribution License
Version 1.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.couchcms.com/cpal.html. The License is based on the Mozilla
Public License Version 1.1 but Sections 14 and 15 have been added to cover use
of software over a computer network and provide for limited attribution for the
Original Developer. In addition, Exhibit A has been modified to be consistent with
Exhibit B.
Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
WARRANTY OF ANY KIND, either express or implied. See the License for the
specific language governing rights and limitations under the License.
The Original Code is the CouchCMS project.
The Original Developer is the Initial Developer.
The Initial Developer of the Original Code is Kamran Kashif (kksidd@couchcms.com).
All portions of the code written by Initial Developer are Copyright (c) 2009, 2010
the Initial Developer. All Rights Reserved.
Contributor(s):
Alternatively, the contents of this file may be used under the terms of the
CouchCMS Commercial License (the CCCL), in which case the provisions of
the CCCL are applicable instead of those above.
If you wish to allow use of your version of this file only under the terms of the
CCCL and not to allow others to use your version of this file under the CPAL, indicate
your decision by deleting the provisions above and replace them with the notice
and other provisions required by the CCCL. If you do not delete the provisions
above, a recipient may use your version of this file under either the CPAL or the
CCCL.
*/
if ( !defined('K_COUCH_DIR') ) die(); // cannot be loaded directly
if( !defined('K_USE_KC_FINDER') ) define( 'K_USE_KC_FINDER', 0 );
class KField{
var $id;
var $template_id;
var $name;
var $label;
var $k_desc;
var $k_type;
var $hidden;
var $search_type;
var $k_order;
var $data;
var $default_data;
var $required;
var $validator;
var $validator_msg;
var $k_separator;
var $val_separator;
var $opt_values;
var $opt_selected;
var $toolbar;
var $custom_toolbar;
var $css;
var $custom_styles;
var $maxlength;
var $height;
var $width;
var $k_group;
var $collapsed;
var $assoc_field;
var $crop;
var $enforce_max;
var $quality;
var $show_preview;
var $preview_width;
var $preview_height;
var $no_xss_check;
var $rtl;
var $body_id;
var $body_class;
var $disable_uploader;
var $dynamic;
var $custom_params;
var $searchable;
var $class;
var $not_active;
var $page;
var $siblings;
var $processed;
var $system = 0;
var $err_msg = '';
var $modified = false;
var $udf = 0;
var $cached = null;
var $refresh_form = 0;
var $err_msg_refresh = '';
var $requires_multipart = 0;
var $trust_mode = 1;
var $simple_mode = 0;
var $no_js = 0;
var $orig_data = null;
var $module = null;
var $k_inactive = 0;
var $no_render = 0;
var $available_validators = array(
'min_len' => 'KFuncs::validate_min_len',
'max_len' => 'KFuncs::validate_max_len',
'exact_len' => 'KFuncs::validate_exact_len',
'alpha' => 'KFuncs::validate_alpha',
'alpha_num' => 'KFuncs::validate_alpha_num',
'integer' => 'KFuncs::validate_int',
'non_negative_integer' => 'KFuncs::validate_natural',
'non_zero_integer' => 'KFuncs::validate_non_zero_natural',
'decimal' => 'KFuncs::validate_numeric',
'non_negative_decimal' => 'KFuncs::validate_non_negative_numeric',
'non_zero_decimal' => 'KFuncs::validate_non_zero_numeric',
'email' => 'KFuncs::validate_email',
'url' => 'KFuncs::validate_url',
'matches_field' => 'KFuncs::validate_matches',
'regex' => 'KFuncs::validate_regex',
/* used only internally */
'title_ready' => 'KFuncs::validate_title',
'unique_page' => 'KFuncs::validate_unique_page'
);
var $available_buttons = array(
'bold'=>'Bold',
'italic'=>'Italic',
'underline'=>'Underline',
'strike'=>'Strike',
'subscript'=>'Subscript',
'superscript'=>'Superscript',
'justifyleft'=>'JustifyLeft',
'justifycenter'=>'JustifyCenter',
'justifyright'=>'JustifyRight',
'justifyblock'=>'JustifyBlock',
'numberedlist'=>'NumberedList',
'bulletedlist'=>'BulletedList',
'outdent'=>'Outdent',
'indent'=>'Indent',
'blockquote'=>'Blockquote',
'undo'=>'Undo',
'redo'=>'Redo',
'removeformat'=>'RemoveFormat',
'cut'=>'Cut',
'copy'=>'Copy',
'paste'=>'Paste',
'pastetext'=>'PasteText',
'pastefromword'=>'PasteFromWord',
'image'=>'Image',
'flash'=>'Flash',
'table'=>'Table',
'horizontalrule'=>'HorizontalRule',
'smiley'=>'Smiley',
'specialchar'=>'SpecialChar',
'pagebreak'=>'PageBreak',
'link'=>'Link',
'unlink'=>'Unlink',
'anchor'=>'Anchor',
'styles'=>'Styles',
'format'=>'Format',
'font'=>'Font',
'fontsize'=>'FontSize',
'textcolor'=>'TextColor',
'bgcolor'=>'BGColor',
'showblocks'=>'ShowBlocks',
'preview'=>'Preview',
'maximize'=>'Maximize',
'source'=>'Source',
'spellchecker'=>'SpellChecker',
'-'=>'-',
'_'=>'-'
);
function __construct( $row, &$page, &$siblings ){
global $FUNCS;
foreach( $row as $k=>$v ){
$this->$k = $v;
}
$this->page = &$page;
$this->siblings = &$siblings;
}
public function __toString(){
return get_class();
}
// Invoked only while editing a page where all parameters of a field (instead of just the usual data) are needed.
function resolve_dynamic_params(){
global $FUNCS;
if( !$this->system && $this->dynamic ){
// HOOK: resolve_dynamic_params
$skip = $FUNCS->dispatch_event( 'resolve_dynamic_params', array(&$this) );
if( $skip ) return;
$arr_dynamic = array_map( "trim", explode( '|', $this->dynamic ) );
foreach( $arr_dynamic as $dyn_param ){
if( in_array($dyn_param, array( 'desc', 'type', 'order', 'group', 'separator' )) ){
$dyn_param = 'k_'.$dyn_param;
}
if( property_exists($this, $dyn_param) && $this->$dyn_param ){
if( defined('K_SNIPPETS_DIR') ){ // always defined relative to the site
$base_snippets_dir = K_SITE_DIR . K_SNIPPETS_DIR . '/';
}
else{
$base_snippets_dir = K_COUCH_DIR . 'snippets/';
}
$filepath = $base_snippets_dir . ltrim( trim($this->$dyn_param), '/\\' );
if( file_exists($filepath) ){
$html = @file_get_contents($filepath);
if( strlen($html) ){
$parser = new KParser( $html );
$this->$dyn_param = $parser->get_HTML();
}
}
else{
// HOOK: resolve_dynamic_param (individual)
$FUNCS->dispatch_event( 'resolve_dynamic_param', array(&$this, $dyn_param) );
}
}
}
}
}
function store_posted_changes( $post_val ){
global $FUNCS, $Config;
if( $this->deleted || $this->k_inactive ) return; // no need to store
if( in_array($this->k_type, array('thumbnail', 'hidden', 'message', 'group')) ) return;
if( $this->k_type== 'checkbox' && is_array($post_val) ){
$separator = ( $this->k_separator ) ? $this->k_separator : '|';
$sep = '';
$str_val = '';
foreach( $post_val as $v ){
$str_val .= $sep . $v;
$sep = $separator;
}
$post_val = $str_val;
}
// v1.4 - added sanity check for checkbox, radio and dropdown types
// to allow only submitted values that are a subset of the available options
if( $this->k_type=='dropdown' || $this->k_type=='radio' || $this->k_type=='checkbox' ){
$post_val = trim( $post_val );
if( $post_val!='' ){
$separator = ( $this->k_separator ) ? $this->k_separator : '|';
$val_separator = ( $this->val_separator ) ? $this->val_separator : '=';
// get the selected options
$selected = html_entity_decode( $post_val, ENT_QUOTES, K_CHARSET );
if( $this->k_type=='checkbox' ){
$selected = ( $selected != '' ) ? array_map( "trim", preg_split( "/(?<!\\\)\\".$separator."/", $selected ) ) : array();
for( $x=0; $x<count($selected); $x++ ){
$selected[$x] = str_replace( '\\'.$separator, $separator, $selected[$x] );
$selected[$x] = str_replace( '\\'.$val_separator, $val_separator, $selected[$x] );
}
}
// get the valid options
$valid_values = array();
$arr_values = array_map( "trim", preg_split( "/(?<!\\\)\\".$separator."/", $this->opt_values ) );
foreach( $arr_values as $val ){
if( $val!='' ){
$val = str_replace( '\\'.$separator, $separator, $val );
$arr_values_args = array_map( "trim", preg_split( "/(?<!\\\)\\".$val_separator."/", $val ) );
if( isset($arr_values_args[1]) ){
$opt_val = str_replace( '\\'.$val_separator, $val_separator, $arr_values_args[1] );
}
else{
$opt_val = str_replace( '\\'.$val_separator, $val_separator, $arr_values_args[0] );
}
$valid_values[] = $opt_val;
}
}
// remove selected options that are not valid
if( !count($valid_values) ){
$post_val = '';
}
else{
if( $this->k_type=='dropdown' || $this->k_type=='radio' ){
if( !in_array($selected, $valid_values) ){
$post_val = '';
}
}
else{
$sep = '';
$str_val = '';
foreach( $selected as $v ){
if( in_array($v, $valid_values) ){
$str_val .= $sep . str_replace( $separator, '\\'.$separator, $v );
$sep = $separator;
}
}
$post_val = $str_val;
}
}
}
}
// strip off domain info from uploads
if( $this->k_type=='image' || $this->k_type=='file' ){
$domain_prefix = $Config['k_append_url'] . $Config['UserFilesPath'] . $this->k_type . '/';
$post_val = trim( $post_val );
if( strpos($post_val, $domain_prefix)===0 ){
$post_val = substr( $post_val, strlen($domain_prefix) );
$post_val = ( $post_val ) ? ':' . $post_val : ':'; // add marker
if( is_null($this->orig_data) ) $this->orig_data = $this->data;
}
else{
if( is_null($this->orig_data) ) $this->orig_data = $this->get_data();
}
}
else{
if( is_null($this->orig_data) ) $this->orig_data = $this->get_data();
}
if( $this->trust_mode==0 && in_array($this->k_type, array('text', 'textarea', 'richtext')) ){
// if value submitted from front-end form, input is untrusted and so only a limited subset of HTML tags will be allowed
if( $this->k_type=='richtext' ){
$allowed_tags = '<a><br><strong><b><em><i><u><blockquote><pre><code><ul><ol><li><del><strike><div><p><h1><h2><h3><h4><h5><h6>';
$this->data = trim( $FUNCS->cleanXSS(strip_tags($post_val, $allowed_tags), 2) );
}
else{
$this->data = trim( $FUNCS->cleanXSS(strip_tags($post_val)) );
}
}
else{
$this->data = ($this->k_type=='textarea' && $this->no_xss_check) ? $post_val : $FUNCS->cleanXSS( $post_val );
}
$this->modified = ( strcmp( $this->orig_data, $this->data )==0 ) ? false : true; // values unchanged
}
// Meant to be overridden by custom fields to store raw data picked from database into the field object.
function store_data_from_saved( $data ){
$this->data = $data;
}
// Meant to be overridden by custom fields to give data to be saved in database
// If a custum field overrides this function, it should also override store_data_from_saved() as both deal directly with $this->data
// From KUserDefinedField onwards, this function will return data straight from $f->data.
// For now using $f->get_data for backward functionality with our in-built fields.
function get_data_to_save(){
return $this->get_data();
}
// Used primarily for display on front-end (by feeding the content of this field into $CTX ) but is
// also used by _render() of built-in fields to render the field in admin panel.
function get_data(){
global $Config, $CTX;
if( $this->k_type=='message' ){ return $this->default_data; }
if( !$this->data ){
// make sure it is not numeric 0
$data = ( is_numeric($this->data) ) ? (string)$this->data : $this->default_data;
}
else{
$data = $this->data;
}
if( $this->search_type!='text' ){
$pos = strpos( $data, ".00");
if( $pos!==false ){
$data = substr( $data, 0, $pos );
}
}
else{
// add domain info to uploaded items
if( $this->k_type=='image' || $this->k_type=='thumbnail' || $this->k_type=='file' ){
if( $data[0]==':' ){ // if marker
$data = substr( $data, 1 );
$folder = ( $this->k_type=='thumbnail' ) ? 'image' : $this->k_type;
$domain_prefix = $Config['k_append_url'] . $Config['UserFilesPath'] . $folder . '/';
$data = $domain_prefix . $data;
}
}
elseif( $this->k_type=='checkbox' ){
$arr_data = array();
if( strlen($data) ){
$sep = ( $this->k_separator ) ? $this->k_separator : '|';
$arr_data = array_map( function($item)use($sep){
return trim( str_replace( '\\'.$sep, $sep, $item ) ); //unescape separator
}, preg_split( "/(?<!\\\)".preg_quote($sep, '/')."/", $data ) );
}
$CTX->set( '__'.$this->name, $arr_data );
}
}
return $data;
}
function validate(){
global $FUNCS;
if( $this->deleted || $this->k_inactive ) return true; // skip deleted fields
if( $this->page->tpl_nested_pages && !$this->system && $this->page->_fields['k_is_pointer']->get_data() ) return true; // skip custom fields if this nested page is a pointer_page
$this->err_msg = '';
$separator = ( $this->k_separator ) ? $this->k_separator : '|';
$val_separator = ( $this->val_separator ) ? $this->val_separator : '=';
// validation failure messages
$msgs = array();
if( $this->validator_msg ){
$arr_msgs = explode( $separator, $this->validator_msg );
$arr_msgs = array_map( "trim", $arr_msgs );
foreach( $arr_msgs as $msg ){
$arr_msgs_parts = explode( $val_separator, $msg );
$arr_msgs_parts = array_map( "trim", $arr_msgs_parts );
$msgs[$arr_msgs_parts[0]] = $arr_msgs_parts[1];
}
}
// check if a required field
if( $FUNCS->is_core_type($this->k_type) ){
$data = trim( $this->get_data() );
if( $this->required ){
switch( $this->k_type ){
case 'text':
case 'password':
case 'textarea':
case 'richtext':
case 'image':
case 'file':
case 'radio':
case 'checkbox':
if( $data=='' ){
$check_failed = true;
}
break;
case 'thumbnail':
case 'hidden':
case 'message':
case 'group':
break; // no validation required
case 'dropdown':
if( $data=='' || $data=='-' || $data=='_' ){
$check_failed = true;
}
}
}
else{
// if not a required field and no data submitted, no further checks needed.
if( !strlen($data) ) return true;
}
}
else{ // UDFs
if( $this->is_empty() ){
if( $this->required ){
$check_failed = true;
}
else{
return true;
}
}
}
if( $check_failed ){
if( array_key_exists('required', $msgs) ){
$this->err_msg = $msgs['required'];
}
else{
$this->err_msg = $FUNCS->t('required_msg');
}
return false;
}
// custom validators
if( $this->validator || ($this->search_type!='text') ){
$int_validators = array( 'integer', 'natural', 'non_zero_natural' );
$decimal_validators = array( 'numeric', 'non_negative_numeric', 'non_zero_numeric' );
if( $this->search_type!='text' ) $validation_required = 1;
$arr_validators = array();
if( $this->validator ){
$arr_validator_elems = array_map( "trim", explode( $separator, $this->validator ) );
foreach( $arr_validator_elems as $validator_elem ){
$args = array_map( "trim", explode( $val_separator, $validator_elem ) );
$arr_validators[$args[0]] = $args[1];
// lookout if user has already defined a proper validator for 'integer' and 'decimal' search types.
if( $this->search_type=='integer' ){
if( in_array($args[0], $int_validators) ) $validation_required = 0;
}
elseif( $this->search_type=='decimal' ){
if( in_array($args[0], $decimal_validators) ) $validation_required = 0;
}
}
}
if( $validation_required ){
// add appropriate validator if not added by user
if( $this->search_type=='integer' ){
$arr_validators['integer']='';
}
elseif( $this->search_type=='decimal' ){
$arr_validators['decimal']='';
}
}
foreach( $arr_validators as $validator=>$validator_args ){
if( array_key_exists($validator, $this->available_validators) ){
$validator_func = $this->available_validators[$validator];
}
elseif( array_key_exists($validator, $FUNCS->validators) ){
$validator_func = $FUNCS->validators[$validator];
}
else{
$validator_func = trim( $validator ); // allow user defined validator
}
$validator_func = $FUNCS->is_callable( $validator_func );
if( !$validator_func ){
$this->err_msg = "Validator '".$validator."' not found";
return false;
}
$err = call_user_func_array( $validator_func, array(&$this, $validator_args) );
if( $FUNCS->is_error($err) ){
if( array_key_exists($validator, $msgs) ){
$this->err_msg = $msgs[$validator];
}
else{
$this->err_msg = $err->err_msg;
}
return false;
}
}
}
return true;
}
// UDFs should override this method if using parent KField's validate method which calls this.
// If field is a required one, being empty will return 'required field' validation error.
// If field is non-required, being empty will not process any other validation rules attached to it.
function is_empty(){
// Default will always return false thus causing 'required' parameter not to take effect;
return false;
}
function _prep_cached(){
}
// tweak how the field gets rendered in the admin-panel
function pre_render( &$def ){
global $FUNCS;
if( $this->k_type=='group' ){ $def['content']=''; }
elseif( $this->k_type=='thumbnail' ){
$def['desc'] = ( $def['desc']!='') ? $def['desc'].' '.$FUNCS->t('thumb_created_auto') : $FUNCS->t('thumb_created_auto');
}
}
function render(){ // defunct from v2.0
}
function _render( $input_name, $input_id, $extra='', $dynamic_insertion=0 ){
global $FUNCS;
$html = $FUNCS->render( 'field_'.$this->k_type, $this, $input_name, $input_id, $extra, $dynamic_insertion );
return $html;
}
//// overridable render functions for core fields ////
// textarea
static function _render_textarea( $f, $input_name, $input_id, $extra, $dynamic_insertion ){
global $CTX, $FUNCS;
KField::_set_common_vars( $f->k_type, $input_name, $input_id, $extra, $dynamic_insertion, $f->simple_mode );
$arr_vars = array();
$arr_vars['k_field_value'] = $FUNCS->escape_HTML( $f->get_data(), ENT_QUOTES, K_CHARSET );
$arr_vars['k_field_rtl'] = ( $f->rtl ) ? '1' : '0';
$arr_vars['k_field_height'] = $f->height;
$arr_vars['k_field_width'] = $f->width;
$CTX->set_all( $arr_vars );
}
// richtext
static function _render_richtext( $f, $input_name, $input_id, $extra, $dynamic_insertion ){
global $FUNCS;
if( $f->deleted || $f->no_js ){
return $FUNCS->render( 'field_textarea', $f, $input_name, $input_id, $extra, $dynamic_insertion );
}
$html = '';
$value = $f->get_data();
$separator = ( $f->k_separator ) ? $f->k_separator : '|';
$val_separator = ( $f->val_separator ) ? $f->val_separator : '=';
$rtl = ( $f->rtl ) ? 'dir="RTL"' : '';
if( !$f->page->CKEditor ){
require_once( K_COUCH_DIR.'includes/ckeditor/ckeditor.php' );
$f->page->CKEditor = new CKEditor();
$f->page->CKEditor->returnOutput = true;
$f->page->CKEditor->basePath = K_ADMIN_URL . 'includes/ckeditor/';
if( $f->trust_mode ){
if( K_USE_KC_FINDER ){
$f->page->CKEditor->config['filebrowserBrowseUrl'] = K_ADMIN_URL . 'includes/kcfinder/browse.php?nonce='.$FUNCS->create_nonce( 'kc_finder' ).'&type=file';
$f->page->CKEditor->config['filebrowserImageBrowseUrl'] = K_ADMIN_URL . 'includes/kcfinder/browse.php?nonce='.$FUNCS->create_nonce( 'kc_finder' ).'&type=image';
$f->page->CKEditor->config['filebrowserFlashBrowseUrl'] = K_ADMIN_URL . 'includes/kcfinder/browse.php?nonce='.$FUNCS->create_nonce( 'kc_finder' ).'&type=flash';
$f->page->CKEditor->config['filebrowserWindowWidth'] = '670';
}
else{
$f->page->CKEditor->config['filebrowserBrowseUrl'] = K_ADMIN_URL . 'includes/fileuploader/browser/browser.html';
$f->page->CKEditor->config['filebrowserImageBrowseUrl'] = K_ADMIN_URL . 'includes/fileuploader/browser/browser.html?Type=Image';
$f->page->CKEditor->config['filebrowserFlashBrowseUrl'] = K_ADMIN_URL . 'includes/fileuploader/browser/browser.html?Type=Flash';
$f->page->CKEditor->config['filebrowserWindowWidth'] = '600';
}
}
$f->page->CKEditor->config['width'] = '100%';
$f->page->CKEditor->config['height'] = 240;
}
$f->page->CKEditor->textareaAttributes = array("style" => "visibility:hidden", "class" => "ckeditorx", "id" => $input_id, "cols" => 80, "rows" => 15);
//$config['baseHref'] = K_SITE_URL;
// RTL
if( $rtl ) $config['contentsLangDirection'] = 'rtl';
// body class and body id
if( $f->body_class ) $config['bodyClass'] = $f->body_class;
if( $f->body_id ) $config['bodyId'] = $f->body_id;
// height
if( $f->height && $f->height > 0 ){
$config['height'] = $f->height .'px';
}
// width
if( $f->width && $f->width > 0 ){
$config['width'] = $f->width .'px';
}
// content css
// Multiple css files can be loaded and non-local files are supported too.
$arr_css[] = $f->page->CKEditor->basePath . 'contents.css';
if( $f->css ){
$arr_custom_css = array_map( "trim", explode( $separator, $f->css ) );
foreach( $arr_custom_css as $css ){
if( strpos($css, '://')===false ){
$css = K_SITE_URL . (( $css[0]=='/' ) ? substr($css, 1) : $css);
}
$arr_css[] = $css;
}
}
$config['contentsCss'] = $arr_css;
// custom styles dropdown
// Only a single file can be added. Non local file supported.
if( $f->custom_styles ){
list( $custom_style_name, $custom_style_file ) = array_map( "trim", explode( $val_separator, $f->custom_styles ) );
if( strpos($custom_style_file, '://')===false ){
$custom_style_file = K_SITE_URL . (( $custom_style_file[0]=='/' ) ? substr($custom_style_file, 1) : $custom_style_file);
}
$config['stylesCombo_stylesSet'] = $custom_style_name . ':' . $custom_style_file;
}
// rendered within repeatable region?
$repeatable = 0;
if( $dynamic_insertion || $dynamic_insertion===false ){
// NOTE: With v2.0, for regions *within* repeatable-regions, when the need arose to differentiate between regions that already exist (from previous saves)
// and those being added on-the-fly by clicking 'new', to preserve backward compatibility the $dynamic_insertion is set to boolean 'false' for
// existing regions (and '1', as previously, for dynamically added).
$repeatable = 1;
}
// toolbars
$toolbar = $f->toolbar; // basic, medium, full.
if( $repeatable && $toolbar=='' ) $toolbar = 'basic';
if( $toolbar == 'full' ){
$config['toolbar'] = array(
array( 'Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'),
array( 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' ),
array( 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote' ),
array( 'Undo', 'Redo', 'RemoveFormat' ),
'/',
array( 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord' ),
array( 'Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak' ),
array( 'Link', 'Unlink', 'Anchor' ),
'/',
array( 'Styles', 'Format', 'Font', 'FontSize' ),
array( 'TextColor', 'BGColor' ),
array( 'ShowBlocks', 'Preview', 'Maximize', '-', 'Source' )
);
}
elseif( $toolbar == 'basic' ){
$config['toolbar'] = array(
array( 'Bold', 'Italic', 'Underline', 'Strike' ),
array( 'Format' ),
array( 'NumberedList', 'BulletedList', 'Blockquote', 'Link', 'Unlink' ),
array( 'Undo', 'Redo', 'RemoveFormat' ),
array( 'Preview', 'Maximize', '-', 'Source' )
);
}
elseif( $toolbar == 'custom' ){
$str_toolbar = $f->custom_toolbar;
if( $str_toolbar ){
$arr_toolbars = array_map( "trim", explode( $separator, $str_toolbar ) );
foreach( $arr_toolbars as $toolbar ){
$arr_buttons = array_map( "trim", explode( ',', $toolbar ) );
if( count($arr_buttons)==1 && $arr_buttons[0]=='' ){
$arr_tb_buttons[] = '/';
}
else{
$arr_tmp = array();
foreach( $arr_buttons as $btn ){
if( array_key_exists( strtolower($btn), $f->available_buttons ) ){
$arr_tmp[] = $f->available_buttons[strtolower($btn)];
}
// Check if a custom button specified (starts with a #)
elseif( substr($btn, 0, 1)=='#' ){
$arr_tmp[] = substr( $btn, 1 );
}
}
if( count($arr_tmp) ) $arr_tb_buttons[] = $arr_tmp;
}
}
if( count($arr_tb_buttons) ) $config['toolbar'] = $arr_tb_buttons;
}
}
else{ //medium (defult)
$config['toolbar'] = array(
array( 'Bold', 'Italic', 'Underline', 'Strike', '-', 'Subscript', 'Superscript'),
array( 'Format' ),
array( 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' ),
array( 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', 'Blockquote' ),
array( 'Undo', 'Redo', 'RemoveFormat' ),
'/',
array( 'Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord' ),
array( 'Image', 'Flash', 'Table', 'HorizontalRule', 'Smiley', 'SpecialChar', 'PageBreak' ),
array( 'Link', 'Unlink', 'Anchor' ),
array( 'ShowBlocks', 'Preview', 'Maximize', '-', 'Source' )
);
}
// HOOK: ckeditor_alter_config
$FUNCS->dispatch_event( 'ckeditor_alter_config', array(&$config, $f, $input_name, $input_id, $dynamic_insertion) );
if( $repeatable ){
ob_start();
?>
var parentRow = $("#"+eid).closest("tr");
if(parentRow.length){
parentRow.bind("row_delete", function(event){
var ed = CKEDITOR.instances[ eid ];
if( typeof ed != "undefined" ){
ed.destroy();
CKEDITOR.remove( ed );
}
});
parentRow.bind("_reorder_start", function(event){
var ed = CKEDITOR.instances[ eid ];
if( typeof ed != "undefined" ){
if( typeof CKEDITOR.k_config_cks == "undefined" ) CKEDITOR.k_config_cks = [];
CKEDITOR.k_config_cks[eid] = ed.config;
var txtarea = $("#"+eid);
var dummy = txtarea.next(".cke").clone().addClass("dummy");
ed.destroy();
CKEDITOR.remove( ed );
txtarea.hide().after(dummy);
}
});
parentRow.bind("_reorder_stop", function(event){
CKEDITOR.replace( eid, CKEDITOR.k_config_cks[eid] );
var txtarea = $("#"+eid);
txtarea.next(".dummy").remove();
});
}
<?php
$dyn_js = ob_get_contents();
ob_end_clean();
if( $dynamic_insertion ){ // being added on-the-fly.. output template markup that will be duplicated for each dynamically added new row
$tmp = $f->page->CKEditor->editor( $input_name, $value, $config );
if( preg_match('/<textarea.*?<\/textarea>/i', $tmp, $matches) ){
$html .= $matches[0];
}
if( strpos($tmp, 'window.CKEDITOR_BASEPATH')!==false ){
if( preg_match_all('/<script[^>]*>.*?<\/script>/s', $tmp, $matches, PREG_PATTERN_ORDER) ){
$matches = $matches[0]; // scripts
$tmp = array_pop( $matches );
$html0 = '';
foreach ($matches as $match) {
$html0 .= $match;
}
if( strlen($html0) ){ $FUNCS->add_html( $html0 ); };
}
}
if( preg_match('/CKEDITOR\\.replace\\(.*\\);/sim', $tmp, $matches) ){
$html1 = str_replace( '\'', '"', $matches[0] );
}
// Being dynamically inserted (e.g. through 'repeatable' tag).
// Simply outputting script will not work.
// Have to use a workaround (http://24ways.org/2005/have-your-dom-and-script-it-too).
// Additionally, we are adding an id - the logic is that the id gets duplicated into 'idx' for the 'template' row code.
// This 'idx' will not be present in the cloned rows. We use this property to avoid executing JavaScript in template row.
ob_start();
?>
<img src="<?php echo K_SYSTEM_THEME_URL; ?>assets/blank.gif" alt="" id="<?php echo $input_id ?>_dummyimg" onload='
el=$("#<?php echo $input_id ?>_dummyimg");
if(!el.attr("idx")){
<?php echo $html1 ?>
var eid = "<?php echo $input_id ?>";
<?php echo $dyn_js ?>
el.css("display", "none");
}
' />
<?php
$html .= ob_get_contents();
ob_end_clean();
return $html;
}
else{ /* not dynamically inserted but is being rendered within repeatable regions */
$config['removePlugins'] = ( trim($config['removePlugins'])!='' ) ? $config['removePlugins'].',resize' : 'resize';
static $done=0;
if( !$done ){
$done=1;
ob_start();
?>
$(function(){
if( CKEDITOR && CKEDITOR.k_repeatable_cks ){
$.each(CKEDITOR.k_repeatable_cks, function(index, value){
try{
var eid = value;
<?php echo $dyn_js ?>
}
catch(e){}
});
}
});
<?php
$js = ob_get_contents();
ob_end_clean();
$FUNCS->add_js( $js );
}
$html .= $f->page->CKEditor->editor( $input_name, $value, $config );
$html .= "<script type=\"text/javascript\">//<![CDATA[\n";
$html .= "if( !CKEDITOR.k_repeatable_cks ) CKEDITOR.k_repeatable_cks = []; CKEDITOR.k_repeatable_cks.push( '". $input_id ."' );";
$html .= "\n//]]></script>\n";
return $html;
}
}
$html .= $f->page->CKEditor->editor( $input_name, $value, $config );
return $html;
}
// image
static function _render_image( $f, $input_name, $input_id, $extra, $dynamic_insertion ){
global $CTX, $FUNCS;
KField::_set_common_vars( $f->k_type, $input_name, $input_id, $extra, $dynamic_insertion, $f->simple_mode );
$arr_vars = array();
$arr_vars['k_field_value'] = $f->get_data();
$arr_vars['k_field_show_preview'] = ( $f->show_preview ) ? '1' : '0';
$arr_vars['k_field_preview_width'] = $f->preview_width;
$arr_vars['k_field_preview_height'] = $f->preview_height;
$arr_vars['k_field_input_width'] = $f->input_width; // Set by repeatable tag
if( K_USE_KC_FINDER ){
$arr_vars['k_field_use_kcfinder'] = '1';
$dirname = '';
$data = $f->data;
if( $data[0]==':' ){ // if local
$pi = $FUNCS->pathinfo( substr($data, 1) );
$dirname = urlencode( 'image/'.$pi['dirname'] );
}
$arr_vars['k_field_kcfinder_dir'] = $dirname;
}
else{
$arr_vars['k_field_use_kcfinder'] = '0';
}
KField::add_browse_js();
$CTX->set_all( $arr_vars );
}
// thumbnail
static function _render_thumbnail( $f, $input_name, $input_id, $extra, $dynamic_insertion ){
global $CTX;
KField::_set_common_vars( $f->k_type, $input_name, $input_id, $extra, $dynamic_insertion, $f->simple_mode );
$arr_vars = array();
$arr_vars['k_field_value'] = $f->get_data();
$arr_vars['k_field_show_preview'] = ( $f->show_preview ) ? '1' : '0';
$arr_vars['k_field_preview_width'] = $f->preview_width;
$arr_vars['k_field_preview_height'] = $f->preview_height;
$arr_vars['k_field_tpl_id'] = $f->page->tpl_id;
$arr_vars['k_field_page_id'] = $f->page->id;
static $done=0;
if( !$done ){
$arr_vars['k_add_crop_js'] = '1';
$done=1;
}
else{
$arr_vars['k_add_crop_js'] = '0';
}
$CTX->set_all( $arr_vars );
}
// file
static function _render_file( $f, $input_name, $input_id, $extra, $dynamic_insertion ){
global $CTX, $FUNCS;
KField::_set_common_vars( $f->k_type, $input_name, $input_id, $extra, $dynamic_insertion, $f->simple_mode );
$arr_vars = array();
$arr_vars['k_field_value'] = $f->get_data();
$arr_vars['k_field_input_width'] = $f->input_width; // Set by repeatable tag
if( K_USE_KC_FINDER ){
$arr_vars['k_field_use_kcfinder'] = '1';
$dirname = '';
$data = $f->data;
if( $data[0]==':' ){ // if local
$pi = $FUNCS->pathinfo( substr($data, 1) );
$dirname = urlencode( 'file/'.$pi['dirname'] );
}
$arr_vars['k_field_kcfinder_dir'] = $dirname;
}
else{
$arr_vars['k_field_use_kcfinder'] = '0';
}
KField::add_browse_js();
$CTX->set_all( $arr_vars );
}
// js for type 'image' and 'file'
static function add_browse_js(){
global $CTX;
static $done=0;
if( !$done ){
$CTX->set( 'k_add_browse_js', '1' );
$done=1;
}
else{
$CTX->set( 'k_add_browse_js', '0' );
}
}
// text
static function _render_text( $f, $input_name, $input_id, $extra, $dynamic_insertion ){
global $CTX, $FUNCS;
KField::_set_common_vars( $f->k_type, $input_name, $input_id, $extra, $dynamic_insertion, $f->simple_mode );
$arr_vars = array();
$arr_vars['k_field_value'] = $FUNCS->escape_HTML( $f->get_data(), ENT_QUOTES, K_CHARSET );
$arr_vars['k_field_rtl'] = ( $f->rtl ) ? '1' : '0';
$arr_vars['k_field_width'] = $f->width;
$arr_vars['k_field_maxlength'] = $f->maxlength;
$CTX->set_all( $arr_vars );
}
// dropdown, radio, checkbox
static function _render_options( $f, $input_name, $input_id, $extra, $dynamic_insertion ){
global $FUNCS, $CTX;
$value = trim( $f->get_data() );
$selected = html_entity_decode( $value, ENT_QUOTES, K_CHARSET );
if( $selected=='' && $_SERVER['REQUEST_METHOD']!='POST' ){ // the posted value can also be a blank, hence this check.
$selected = trim( $f->opt_selected );