Example #1
0
 /**
  * Update and store all settings found in the $_POST array
  *
  * Note that you *MUST* call {@see init()} beforehand, or your settings
  * will be unknown and thus not be stored.
  * Sets up an error message on failure.
  * @return  boolean                 True on success, null on noop,
  *                                  or false on failure
  */
 static function storeFromPost()
 {
     global $_CORELANG;
     //echo("self::storeFromPost(): POST:<br />".nl2br(htmlentities(var_export($_POST, true)))."<hr />");
     //echo("self::storeFromPost(): FILES:<br />".nl2br(htmlentities(var_export($_FILES, true)))."<hr />");
     // There may be several tabs for different groups being edited, so
     // load the full set of settings for the module.
     // Note that this is why setting names should be unique.
     // TODO: You *MUST* call this yourself *before* in order to
     // properly initialize the section!
     // self::init();
     $engine = self::getSectionEngine();
     if ($engine == null) {
         return false;
     }
     $arrSettings = $engine->getArraySetting();
     unset($_POST['bsubmit']);
     $result = true;
     // Compare POST with current settings and only store what was changed.
     foreach (array_keys($arrSettings) as $name) {
         if (isset($_POST[$name])) {
             $value = contrexx_input2raw($_POST[$name]);
             //if (preg_match('/^'.preg_quote(CSRF::key(), '/').'$/', $name))
             //continue;
             switch ($arrSettings[$name]['type']) {
                 case self::TYPE_FILEUPLOAD:
                     // An empty folder path has been posted, indicating that the
                     // current file should be removed
                     if (empty($value)) {
                         //echo("Empty value, deleting file...<br />");
                         if ($arrSettings[$name]['value']) {
                             if (\File::delete_file($arrSettings[$name]['value'])) {
                                 //echo("File deleted<br />");
                                 $value = '';
                             } else {
                                 //echo("Failed to delete file<br />");
                                 \Message::error(\File::getErrorString());
                                 $result = false;
                             }
                         }
                     } else {
                         // No file uploaded.  Skip.
                         if (empty($_FILES[$name]['name'])) {
                             continue;
                         }
                         // $value is the target folder path
                         $target_path = $value . '/' . $_FILES[$name]['name'];
                         // TODO: Test if this works in all browsers:
                         // The path input field name is the same as the
                         // file upload input field name!
                         $result_upload = \File::upload_file_http($name, $target_path, \Filetype::MAXIMUM_UPLOAD_FILE_SIZE, $arrSettings[$name]['values']);
                         // If no file has been uploaded at all, ignore the no-change
                         // TODO: Noop is not implemented in File::upload_file_http()
                         // if ($result_upload === '') continue;
                         if ($result_upload === true) {
                             $value = $target_path;
                         } else {
                             //echo("self::storeFromPost(): Error uploading file for setting $name to $target_path<br />");
                             // TODO: Add error message
                             \Message::error(\File::getErrorString());
                             $result = false;
                         }
                     }
                     break;
                 case self::TYPE_CHECKBOX:
                     break;
                 case self::TYPE_CHECKBOXGROUP:
                     $value = is_array($value) ? join(',', array_keys($value)) : $value;
                     // 20120508
                 // 20120508
                 case self::TYPE_RADIO:
                     break;
                 default:
                     // Regular value of any other type
                     break;
             }
             //\DBG::log('setting value ' . $name . ' = ' . $value);
             self::set($name, $value);
         }
     }
     //echo("self::storeFromPost(): So far, the result is ".($result ? 'okay' : 'no good')."<br />");
     $result_update = self::updateAll();
     if ($result_update === false) {
         \Message::error($_CORELANG['TXT_CORE_SETTING_ERROR_STORING']);
     } elseif ($result_update === true) {
         \Message::ok($_CORELANG['TXT_CORE_SETTING_STORED_SUCCESSFULLY']);
     }
     // If nothing bad happened above, return the result of updateAll(),
     // which may be true, false, or the empty string
     if ($result === true) {
         return $result_update;
     }
     // There has been an error anyway
     return false;
 }