mirror of
https://github.com/typemill/typemill.git
synced 2025-08-16 11:04:46 +02:00
Version 1.1.0 Introducing Plugins, Event-Dispatcher and CookieConsent
This commit is contained in:
272
system/Models/Field.php
Normal file
272
system/Models/Field.php
Normal file
@@ -0,0 +1,272 @@
|
||||
<?php
|
||||
|
||||
namespace Typemill\Models;
|
||||
|
||||
class Field
|
||||
{
|
||||
private $type;
|
||||
|
||||
private $label;
|
||||
|
||||
private $name;
|
||||
|
||||
private $content;
|
||||
|
||||
/* holds all simple attributes for this field like "required" */
|
||||
private $attributes = array();
|
||||
|
||||
/* holds all attribute value pairs for this field like "id=''" */
|
||||
private $attributeValues = array();
|
||||
|
||||
/* holds all options for this field (e.g. select options) */
|
||||
private $options = array();
|
||||
|
||||
/* defines all field types, that are allowed */
|
||||
private $types = array(
|
||||
'checkbox',
|
||||
'checkboxlist',
|
||||
'color',
|
||||
'date',
|
||||
'datetime',
|
||||
'datetime-local',
|
||||
'email',
|
||||
'file',
|
||||
'hidden',
|
||||
'image',
|
||||
'month',
|
||||
'number',
|
||||
'password',
|
||||
'radio',
|
||||
'range',
|
||||
'tel',
|
||||
'text',
|
||||
'time',
|
||||
'url',
|
||||
'week',
|
||||
'textarea',
|
||||
'select'
|
||||
);
|
||||
|
||||
/* defines all boolean attributes, that are allowed for fields */
|
||||
private $attr = array(
|
||||
'autofocus',
|
||||
'checked',
|
||||
'disabled',
|
||||
'formnovalidate',
|
||||
'multiple',
|
||||
'readonly',
|
||||
'required'
|
||||
);
|
||||
|
||||
/* defines all attribute value paires, that are allowed for fields */
|
||||
private $attrValues = array(
|
||||
'id',
|
||||
'autocomplete',
|
||||
'placeholder',
|
||||
'value',
|
||||
'size',
|
||||
'rows',
|
||||
'cols',
|
||||
'class',
|
||||
'pattern'
|
||||
);
|
||||
|
||||
/* defines additional data, that are allowed for fields */
|
||||
private $helpers = array(
|
||||
'help',
|
||||
'description'
|
||||
);
|
||||
|
||||
public function __construct($fieldName, array $fieldConfigs)
|
||||
{
|
||||
$this->setName($fieldName);
|
||||
|
||||
if(isset($fieldConfigs['type']))
|
||||
{
|
||||
$this->setType($fieldConfigs['type']);
|
||||
}
|
||||
|
||||
if(isset($fieldConfigs['label']))
|
||||
{
|
||||
$this->setLabel($fieldConfigs['label']);
|
||||
}
|
||||
|
||||
if(isset($fieldConfigs['options']))
|
||||
{
|
||||
$this->setOptions($fieldConfigs['options']);
|
||||
}
|
||||
|
||||
$this->setAttributes($fieldConfigs);
|
||||
|
||||
$this->setAttributeValues($fieldConfigs);
|
||||
|
||||
$this->setHelpers($fieldConfigs);
|
||||
}
|
||||
|
||||
private function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
private function setType($type)
|
||||
{
|
||||
if(in_array($type, $this->types))
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
private function setLabel($label)
|
||||
{
|
||||
$this->label = $label;
|
||||
}
|
||||
|
||||
public function getLabel()
|
||||
{
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
public function setContent($content)
|
||||
{
|
||||
$this->content = $content;
|
||||
}
|
||||
|
||||
public function getContent()
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
private function setOptions(array $options)
|
||||
{
|
||||
foreach($options as $key => $value)
|
||||
{
|
||||
$this->options[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function getOptions()
|
||||
{
|
||||
if(isset($this->options))
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private function setAttributes($fieldConfigs)
|
||||
{
|
||||
foreach($fieldConfigs as $key => $value)
|
||||
{
|
||||
if(is_string($key) && in_array($key, $this->attr))
|
||||
{
|
||||
$this->attributes[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* get all attributes of the field and return them as a string. For usage in templates */
|
||||
public function getAttributes()
|
||||
{
|
||||
$string = false;
|
||||
|
||||
foreach($this->attributes as $key => $attribute)
|
||||
{
|
||||
$string .= ' ' . $key;
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/* set a single attribute. Used e.g. in controller to change the value */
|
||||
public function setAttribute($key, $value)
|
||||
{
|
||||
$this->attributes[$key] = $value;
|
||||
}
|
||||
|
||||
/* get a single attribute, if it is defined. For usage in templates like getAttribute('required') */
|
||||
public function getAttribute($key)
|
||||
{
|
||||
if(isset($this->attributes[$key]))
|
||||
{
|
||||
return $this->attributes[$key];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function setAttributeValues($fieldConfigs)
|
||||
{
|
||||
foreach($fieldConfigs as $key => $value)
|
||||
{
|
||||
if(is_string($key) && array_key_exists($key, $this->attrValues))
|
||||
{
|
||||
$this->attributeValues[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* get all attributes as string. For usage in template */
|
||||
public function getAttributeValues()
|
||||
{
|
||||
$string = false;
|
||||
|
||||
foreach($this->attributeValues as $key => $attribute)
|
||||
{
|
||||
$string .= ' ' . $key . '="' . $attribute . '"';
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
public function setAttributeValue($key, $value)
|
||||
{
|
||||
/* pretty dirty, but you should not add a value for a simple checkbox */
|
||||
if($key == 'value' && $this->type == 'checkbox')
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
$this->attributeValues[$key] = $value;
|
||||
}
|
||||
|
||||
private function getAttributeValue($key)
|
||||
{
|
||||
if(isset($this->attributeValues[$key]))
|
||||
{
|
||||
return $this->attributeValues[$key];
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
private function setHelpers($fieldConfigs)
|
||||
{
|
||||
foreach($fieldConfigs as $key => $config)
|
||||
{
|
||||
if(is_string($key) && in_array($key, $this->helpers))
|
||||
{
|
||||
$this->$key = $config;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getHelper($helperName)
|
||||
{
|
||||
if(isset($this->$helperName))
|
||||
{
|
||||
return $this->helperName;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
290
system/Models/Validation.php
Normal file
290
system/Models/Validation.php
Normal file
@@ -0,0 +1,290 @@
|
||||
<?php
|
||||
|
||||
namespace Typemill\Models;
|
||||
|
||||
use Valitron\Validator;
|
||||
|
||||
class Validation
|
||||
{
|
||||
/**
|
||||
* Constructor with custom validation rules
|
||||
*
|
||||
* @param obj $db the database connection.
|
||||
*/
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
Validator::langDir(__DIR__.'/../vendor/vlucas/valitron/lang'); // always set langDir before lang.
|
||||
Validator::lang('en');
|
||||
|
||||
Validator::addRule('emailAvailable', function($field, $value, array $params, array $fields)
|
||||
{
|
||||
$email = 'testmail@gmail.com';
|
||||
if($email){ return false; }
|
||||
return true;
|
||||
}, 'taken');
|
||||
|
||||
Validator::addRule('emailKnown', function($field, $value, array $params, array $fields)
|
||||
{
|
||||
$email = 'testmail@gmail.com';
|
||||
if(!$email){ return false; }
|
||||
return true;
|
||||
}, 'unknown');
|
||||
|
||||
Validator::addRule('userAvailable', function($field, $value, array $params, array $fields)
|
||||
{
|
||||
$username = 'trendschau';
|
||||
if($username){ return false; }
|
||||
return true;
|
||||
}, 'taken');
|
||||
|
||||
Validator::addRule('checkPassword', function($field, $value, array $params, array $fields)
|
||||
{
|
||||
if(password_verify($value, $fields['user_password'])){ return true; }
|
||||
return false;
|
||||
}, 'not valid');
|
||||
|
||||
Validator::addRule('noHTML', function($field, $value, array $params, array $fields)
|
||||
{
|
||||
if ( $value == strip_tags($value) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}, 'contains html');
|
||||
}
|
||||
|
||||
/**
|
||||
* validation for signup form
|
||||
*
|
||||
* @param array $params with form data.
|
||||
* @return obj $v the validation object passed to a result method.
|
||||
*/
|
||||
|
||||
public function validateSignin(array $params)
|
||||
{
|
||||
$v = new Validator($params);
|
||||
$v->rule('required', ['username', 'password'])->message("notwendig");
|
||||
$v->rule('alphaNum', 'username')->message("ungültig");
|
||||
$v->rule('lengthBetween', 'password', 5, 20)->message("Länge 5 - 20");
|
||||
$v->rule('lengthBetween', 'username', 5, 20)->message("Länge 5 - 20");
|
||||
|
||||
if($v->validate())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* validation for signup form
|
||||
*
|
||||
* @param array $params with form data.
|
||||
* @return obj $v the validation object passed to a result method.
|
||||
*/
|
||||
|
||||
public function validateSignup(array $params)
|
||||
{
|
||||
$v = new Validator($params);
|
||||
$v->rule('required', ['signup_username', 'signup_email', 'signup_password'])->message("notwendig");
|
||||
$v->rule('alphaNum', 'signup_username')->message("ungültig");
|
||||
$v->rule('lengthBetween', 'signup_password', 5, 20)->message("Länge 5 - 20");
|
||||
$v->rule('lengthBetween', 'signup_username', 5, 20)->message("Länge 5 - 20");
|
||||
$v->rule('userAvailable', 'signup_username')->message("vergeben");
|
||||
$v->rule('email', 'signup_email')->message("ungültig");
|
||||
$v->rule('emailAvailable', 'signup_email')->message("vergeben");
|
||||
|
||||
return $this->validationResult($v);
|
||||
}
|
||||
|
||||
public function validateReset(array $params)
|
||||
{
|
||||
$v = new Validator($params);
|
||||
$v->rule('required', 'email')->message("notwendig");
|
||||
$v->rule('email', 'email')->message("ungültig");
|
||||
$v->rule('emailKnown', 'email')->message("unbekannt");
|
||||
|
||||
return $this->validationResult($v);
|
||||
}
|
||||
|
||||
/**
|
||||
* validation for changing the password
|
||||
*
|
||||
* @param array $params with form data.
|
||||
* @return obj $v the validation object passed to a result method.
|
||||
*/
|
||||
|
||||
public function validatePassword(array $params)
|
||||
{
|
||||
$v = new Validator($params);
|
||||
$v->rule('required', ['password', 'password_old']);
|
||||
$v->rule('lengthBetween', 'password', 5, 50);
|
||||
$v->rule('checkPassword', 'password_old');
|
||||
|
||||
return $this->validationResult($v);
|
||||
}
|
||||
|
||||
/**
|
||||
* validation for password reset
|
||||
*
|
||||
* @param array $params with form data.
|
||||
* @return obj $v the validation object passed to a result method.
|
||||
*/
|
||||
|
||||
public function validateResetPassword(array $params)
|
||||
{
|
||||
$v = new Validator($params);
|
||||
$v->rule('required', ['password', 'username']);
|
||||
$v->rule(['lengthBetween' => [['password', 5, 50], ['username', 3,20]]]);
|
||||
|
||||
return $this->validationResult($v);
|
||||
}
|
||||
|
||||
/**
|
||||
* validation for basic settings input
|
||||
*
|
||||
* @param array $params with form data.
|
||||
* @return obj $v the validation object passed to a result method.
|
||||
*/
|
||||
|
||||
public function settings(array $params, array $themes, array $copyright, $name = false)
|
||||
{
|
||||
$v = new Validator($params);
|
||||
|
||||
$v->rule('required', ['title', 'author', 'copyright', 'year', 'theme']);
|
||||
$v->rule('lengthBetween', 'title', 2, 20);
|
||||
$v->rule('lengthBetween', 'author', 2, 40);
|
||||
$v->rule('regex', 'title', '/^[\pL0-9_ \-]*$/u');
|
||||
$v->rule('regex', 'author', '/^[\pL_ \-]*$/u');
|
||||
$v->rule('integer', 'year');
|
||||
$v->rule('length', 'year', 4);
|
||||
$v->rule('in', 'copyright', $copyright);
|
||||
$v->rule('in', 'theme', $themes);
|
||||
|
||||
return $this->validationResult($v, $name);
|
||||
}
|
||||
|
||||
public function pluginField($fieldName, $fieldValue, $pluginName, $fieldDefinitions)
|
||||
{
|
||||
$v = new Validator(array($fieldName => $fieldValue));
|
||||
|
||||
|
||||
if(isset($fieldDefinitions['required']))
|
||||
{
|
||||
$v->rule('required', $fieldName);
|
||||
}
|
||||
|
||||
switch($fieldDefinitions['type'])
|
||||
{
|
||||
case "select":
|
||||
case "radio":
|
||||
case "checkboxlist":
|
||||
$v->rule('in', $fieldName, $fieldDefinitions['options']);
|
||||
break;
|
||||
case "color":
|
||||
$v->rule('regex', $fieldName, '/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/');
|
||||
break;
|
||||
case "email":
|
||||
$v->rule('email', $fieldName);
|
||||
break;
|
||||
case "date":
|
||||
$v->rule('date', $fieldName);
|
||||
break;
|
||||
case "checkbox":
|
||||
$v->rule('accepted', $fieldName);
|
||||
break;
|
||||
case "url":
|
||||
$v->rule('lengthMax', $fieldName, 200);
|
||||
$v->rule('url', $fieldName);
|
||||
break;
|
||||
case "text":
|
||||
$v->rule('lengthMax', $fieldName, 200);
|
||||
$v->rule('regex', $fieldName, '/^[\pL0-9_ \-]*$/u');
|
||||
break;
|
||||
case "textarea":
|
||||
$v->rule('lengthMax', $fieldName, 1000);
|
||||
$v->rule('noHTML', $fieldName);
|
||||
// $v->rule('regex', $fieldName, '/<[^<]+>/');
|
||||
break;
|
||||
default:
|
||||
$v->rule('lengthMax', $fieldName, 1000);
|
||||
$v->rule('regex', $fieldName, '/^[\pL0-9_ \-]*$/u');
|
||||
}
|
||||
|
||||
return $this->validationResult($v, $pluginName);
|
||||
}
|
||||
|
||||
/**
|
||||
* validation for election input
|
||||
*
|
||||
* @param array $params with form data.
|
||||
* @return obj $v the validation object passed to a result method.
|
||||
*/
|
||||
|
||||
public function validateShowroom(array $params)
|
||||
{
|
||||
$v = new Validator($params);
|
||||
$v->rule('required', ['title']);
|
||||
$v->rule('lengthBetween', 'title', 2, 50);
|
||||
$v->rule('regex', 'title', '/^[^-_\-\s][0-9A-Za-zÄäÜüÖöß_ \-]+$/');
|
||||
$v->rule('integer', 'election' );
|
||||
$v->rule('email', 'email');
|
||||
$v->rule('length', 'invite', 40);
|
||||
$v->rule('alphaNum', 'invite');
|
||||
$v->rule('required', ['group_id', 'politician_id', 'ressort_id']);
|
||||
$v->rule('integer', ['group_id', 'politician_id', 'ressort_id']);
|
||||
|
||||
return $this->validationResult($v);
|
||||
}
|
||||
|
||||
|
||||
public function validateGroup(array $params)
|
||||
{
|
||||
// $v->rule('date', 'deadline');
|
||||
// $v->rule('dateAfter', 'deadline', new \DateTime('now'));
|
||||
// $v->rule('dateBefore', 'deadline', new \DateTime($params['election_date']));
|
||||
|
||||
return $this->validationResult($v);
|
||||
}
|
||||
|
||||
/**
|
||||
* result for validation
|
||||
*
|
||||
* @param obj $v the validation object.
|
||||
* @return bool
|
||||
*/
|
||||
|
||||
public function validationResult($v, $name = false)
|
||||
{
|
||||
if($v->validate())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if($name)
|
||||
{
|
||||
if(isset($_SESSION['errors'][$name]))
|
||||
{
|
||||
foreach ($v->errors() as $key => $val)
|
||||
{
|
||||
$_SESSION['errors'][$name][$key] = $val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$_SESSION['errors'][$name] = $v->errors();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$_SESSION['errors'] = $v->errors();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
@@ -15,16 +15,11 @@ class VersionCheck
|
||||
|
||||
$context = stream_context_create($opts);
|
||||
|
||||
try {
|
||||
$version = file_get_contents('http://typemill.net/tma1/checkversion', false, $context);
|
||||
|
||||
if ($version)
|
||||
{
|
||||
$version = json_decode($version);
|
||||
return $version->version;
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
if(false === ($version = @file_get_contents('http://typemill.net/api/v1/checkversion', false, $context)))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
$version = json_decode($version);
|
||||
return $version->version;
|
||||
}
|
||||
}
|
@@ -31,7 +31,7 @@ class Write
|
||||
}
|
||||
|
||||
protected function checkFile($folder, $file)
|
||||
{
|
||||
{
|
||||
if(!file_exists($this->basePath . $folder . DIRECTORY_SEPARATOR . $file))
|
||||
{
|
||||
return false;
|
||||
|
@@ -12,6 +12,7 @@ class WriteYaml extends Write
|
||||
public function getYaml($folderName, $yamlFileName)
|
||||
{
|
||||
$yaml = $this->getFile($folderName, $yamlFileName);
|
||||
|
||||
if($yaml)
|
||||
{
|
||||
return \Symfony\Component\Yaml\Yaml::parse($yaml);
|
||||
@@ -27,7 +28,7 @@ class WriteYaml extends Write
|
||||
*/
|
||||
public function updateYaml($folderName, $yamlFileName, $contentArray)
|
||||
{
|
||||
$yaml = \Symfony\Component\Yaml\Yaml::dump($contentArray);
|
||||
$yaml = \Symfony\Component\Yaml\Yaml::dump($contentArray,6);
|
||||
$this->writeFile($folderName, $yamlFileName, $yaml);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user