Ubiquity  2.0.3
php rapid development framework
ConfigTrait.php
Go to the documentation of this file.
1 <?php
2 
4 
13 
21 
22  abstract public function _getAdminData();
23 
24  abstract public function _getAdminViewer();
25 
26  abstract public function _getAdminFiles();
27 
28  abstract public function loadView($viewName, $pData = NULL, $asString = false);
29 
30  abstract public function config($hasHeader = true);
31 
32  abstract protected function showConfMessage($content, $type, $title, $url, $responseElement, $data, $attributes = NULL): HtmlMessage;
33 
34  abstract protected function showSimpleMessage($content, $type, $title=null,$icon = "info", $timeout = NULL, $staticName = null): HtmlMessage;
35 
36  public function formConfig($hasHeader = true) {
37  global $config;
38  if ($hasHeader === true){
39  $this->getHeader ( "config" );
40  }
41  $this->_getAdminViewer ()->getConfigDataForm ( $config );
42  $this->jquery->compile ( $this->view );
43  $this->loadView ( $this->_getAdminFiles ()->getViewConfigForm () );
44  }
45 
46  public function _config(){
47  $config=Startup::getConfig();
48  echo $this->_getAdminViewer ()->getConfigDataElement ( $config );
49  echo $this->jquery->compile($this->view);
50  }
51 
52  public function submitConfig($partial=true){
53  $result=Startup::getConfig();
54  $postValues=$_POST;
55  if($partial!==true){
56  if(isset($postValues["lbl-ck-div-de-database-input-cache"])){
57  unset($postValues["lbl-ck-div-de-database-input-cache"]);
58  if(!(isset($postValues["database-cache"]) && UString::isNotNull($postValues["database-cache"]))){
59  $postValues["database-cache"]=false;
60  }
61  }else{
62  $postValues["database-cache"]=false;
63  }
64  $postValues["debug"]=isset($postValues["debug"]);
65  $postValues["test"]=isset($postValues["test"]);
66  $postValues["templateEngineOptions-cache"]=isset($postValues["templateEngineOptions-cache"]);
67  }
68  foreach ($postValues as $key=>$value){
69  if(strpos($key, "-")===false){
70  $result[$key]=$value;
71  }else{
72  list($k1,$k2)=explode("-", $key);
73  if(!isset($result[$k1])){
74  $result[$k1]=[];
75  }
76  $result[$k1][$k2]=$value;
77  }
78  }
79  $content="<?php\nreturn ".UArray::asPhpArray($result,"array",1,true).";";
80  if(CodeUtils::isValidCode($content)){
81  if(Startup::saveConfig($content)){
82  $this->showSimpleMessage("The configuration file has been successfully modified!", "positive","check square",null,"msgConfig");
83  }else{
84  $this->showSimpleMessage("Impossible to write the configuration file.", "negative","warning circle",null,"msgConfig");
85  }
86  }else{
87  $this->showSimpleMessage("Your configuration contains errors.<br>The configuration file has not been saved.", "negative","warning circle",null,"msgConfig");
88  }
90  $this->config(false);
91  }
92 
93  protected function _checkCondition($callback){
94  if (URequest::isPost()) {
95  $result=[ ];
97  $value=$_POST["_value"];
98  $result["result"]=$callback($value);
99  echo json_encode($result);
100  }
101  }
102 
103  public function _checkArray() {
104  $this->_checkCondition(function($value){
105  try{
106  $array=eval("return ".$value.";");
107  return is_array($array);
108  }catch(\ParseError $e){
109  return false;
110  }
111  });
112  }
113 
114  public function _checkDirectory(){
115  $folder=URequest::post("_ruleValue");
116  $this->_checkCondition(function($value) use($folder){
118  return file_exists($base.DS.$folder.DS.$value);
119  });
120  }
121 
122  public function _checkClass(){
123  $parent=URequest::post("_ruleValue");
124  $this->_checkCondition(function($value) use($parent){
125  try{
126  $class=new \ReflectionClass($value);
127  return $class->isSubclassOf($parent);
128  }catch(\ReflectionException $e){
129  return false;
130  }
131  });
132  }
133  private function convert_smart_quotes($string){
134  $search = array(chr(145),
135  chr(146),
136  chr(147),
137  chr(148),
138  chr(151));
139 
140  $replace = array("'",
141  "'",
142  '"',
143  '"',
144  '-');
145 
146  return str_replace($search, $replace, $string);
147  }
148  public function _checkDbStatus(){
149  $postValues=$_POST;
150  $connected=false;
151  $db=new Database($postValues["database-type"], $postValues["database-dbName"],$postValues["database-serverName"],$postValues["database-port"],$postValues["database-user"],$postValues["database-password"]);
152  try{
153  $db->_connect();
154  $connected=$db->isConnected();
155  }catch(\Exception $e){
156  $errorMsg=$e->getMessage();
157  $msg=((mb_detect_encoding($errorMsg, "UTF-8, ISO-8859-1, ISO-8859-15","CP1252")) !== "UTF-8") ? utf8_encode($this->convert_smart_quotes($errorMsg)) : ($errorMsg);
158  $connected=false;
159  }
160  $icon="exclamation triangle red";
161  if($connected){
162  $icon="check square green";
163  }
164  $icon=$this->jquery->semantic()->htmlIcon("db-status", $icon);
165  if(isset($msg)){
166  $icon->addPopup("Error",$msg);
167  }else{
168  $icon->addPopup("Success","Connexion is ok!");
169  }
170  $this->jquery->execAtLast('$("#db-status").popup("show");');
171  echo $icon;
172  echo $this->jquery->compile($this->view);
173  }
174 }
static isPost()
Returns true if the request is sent by the POST method.
Definition: URequest.php:109
showConfMessage($content, $type, $title, $url, $responseElement, $data, $attributes=NULL)
loadView($viewName, $pData=NULL, $asString=false)
static asJSON()
Sets the response content-type to application/json.
Definition: UResponse.php:72
static post($key, $default=NULL)
Returns the value of the $key variable passed by the post method or $default if the $key variable doe...
Definition: URequest.php:146
$replace
Definition: traits.php:14
static saveConfig($content)
Definition: Startup.php:250
showSimpleMessage($content, $type, $title=null, $icon="info", $timeout=NULL, $staticName=null)