22
33use \ProcessWire \GraphQL \Type \InterfaceType \PageInterfaceType ;
44use \ProcessWire \GraphQL \Type \InterfaceType \PageFileInterfaceType ;
5+ use \ProcessWire \GraphQL \Settings ;
56
67require_once $ this ->config ->paths ->site . 'modules/ProcessGraphQL/vendor/autoload.php ' ;
78
@@ -31,6 +32,15 @@ public function getDefaults()
3132 );
3233 }
3334
35+ public static function isLegalTemplateName ($ name )
36+ {
37+ if (!$ name ) return false ;
38+ if (preg_match ('/^[_A-Za-z][-_0-9A-Za-z]*$/ ' , $ name ) !== 1 ) return false ; // the GraphQL naming requirement
39+ if (strpos ($ name , '__ ' ) === 0 ) return false ; // the names with `__` prefix are reserved by GraphQL
40+ if (in_array ($ name , Settings::getReservedWords ())) return false ; // some words that used now and might be for future
41+ return true ;
42+ }
43+
3444 public function getInputFields ()
3545 {
3646 $ inputfields = parent ::getInputFields ();
@@ -58,10 +68,22 @@ public function getInputFields()
5868 $ f ->attr ('name ' , 'legalTemplates ' );
5969 $ f ->label = 'Legal Templates ' ;
6070 $ f ->description = 'The pages with the templates that you select below will be available via your GraphQL api. ' ;
61- $ f ->notes = 'Please be careful with what you are exposing to the public. Choosing templates marked as `system` can lead to security vulnerabilities. ' ;
62- foreach (\ProcessWire \wire ('templates ' ) as $ template ) {
63- $ f ->addOption ($ template ->name , $ template ->flags & Template::flagSystem ? "{$ template ->name } `(system)` " : $ template ->name );
71+ $ gotDisabledFields = false ;
72+ foreach (\ProcessWire \wire ('templates ' ) as $ template ) {
73+ $ attributes = [];
74+ if (!self ::isLegalTemplateName ($ template ->name )) {
75+ $ attributes ['disabled ' ] = true ;
76+ $ gotDisabledFields = true ;
77+ }
78+ $ label = $ template ->flags & Template::flagSystem ? "{$ template ->name } `(system)` " : $ template ->name ;
79+ $ f ->addOption ($ template ->name , $ label , $ attributes );
80+ }
81+ $ notes = "Please be careful with what you are exposing to the public. Choosing templates marked as `system` can lead to security vulnerabilities. " ;
82+ if ($ gotDisabledFields ) {
83+ $ notes .= PHP_EOL ;
84+ $ notes .= "The template is disabled if it's name is incompatible or reserved for ProcessGraphQL module. " ;
6485 }
86+ $ f ->notes = $ notes ;
6587 $ inputfields ->add ($ f );
6688
6789 // legalFields
@@ -71,7 +93,7 @@ public function getInputFields()
7193 $ f ->label = 'Legal Fields ' ;
7294 $ f ->description = 'The fields that you select below will be available via your GraphQL api. ' ;
7395 $ f ->notes = 'Please be careful with what you are exposing to the public. Choosing fields marked as `system` can to lead security vulnerabilities. ' ;
74- foreach (\ProcessWire \wire ('fields ' )->find ("name!=pass " ) as $ field ) {
96+ foreach (\ProcessWire \wire ('fields ' )->find ("name!=pass " ) as $ field ) {
7597 if ($ field ->type instanceof FieldtypeFieldsetOpen) continue ;
7698 if ($ field ->type instanceof FieldtypeFieldsetClose) continue ;
7799 if ($ field ->type instanceof FieldtypeFieldsetTabOpen) continue ;
0 commit comments