Initial commit
This commit is contained in:
36
pluf/src/Pluf/Form/Field/Boolean.php
Normal file
36
pluf/src/Pluf/Form/Field/Boolean.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_Boolean extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_CheckboxInput';
|
||||
|
||||
public function clean($value)
|
||||
{
|
||||
//parent::clean($value);
|
||||
if (in_array($value, array('on', 'y', '1', 1, true))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
55
pluf/src/Pluf/Form/Field/Date.php
Normal file
55
pluf/src/Pluf/Form/Field/Date.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_Date extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_TextInput';
|
||||
public $input_formats = array(
|
||||
'%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', // 2006-10-25, 10/25/2006, 10/25/06
|
||||
'%b %d %Y', '%b %d, %Y', // 'Oct 25 2006', 'Oct 25, 2006'
|
||||
'%d %b %Y', '%d %b, %Y', // '25 Oct 2006', '25 Oct, 2006'
|
||||
'%B %d %Y', '%B %d, %Y', // 'October 25 2006', 'October 25, 2006'
|
||||
'%d %B %Y', '%d %B, %Y', // '25 October 2006', '25 October, 2006'
|
||||
);
|
||||
|
||||
public function clean($value)
|
||||
{
|
||||
parent::clean($value);
|
||||
if (in_array($value, $this->empty_values)) {
|
||||
return '';
|
||||
}
|
||||
foreach ($this->input_formats as $format) {
|
||||
if (false !== ($date = strptime($value, $format))) {
|
||||
$day = $date['tm_mday'];
|
||||
$month = $date['tm_mon'] + 1;
|
||||
$year = $date['tm_year'] + 1900;
|
||||
if (checkdate($month, $day, $year)) {
|
||||
return str_pad($year, 4, '0', STR_PAD_LEFT).'-'.
|
||||
str_pad($month, 2, '0', STR_PAD_LEFT).'-'.
|
||||
str_pad($day, 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Pluf_Form_Invalid(__('Enter a valid date.'));
|
||||
}
|
||||
}
|
68
pluf/src/Pluf/Form/Field/Datetime.php
Normal file
68
pluf/src/Pluf/Form/Field/Datetime.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_Datetime extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_DatetimeInput';
|
||||
public $input_formats = array(
|
||||
'%Y-%m-%d %H:%M:%S', // '2006-10-25 14:30:59'
|
||||
'%Y-%m-%d %H:%M', // '2006-10-25 14:30'
|
||||
'%Y-%m-%d', // '2006-10-25'
|
||||
'%m/%d/%Y %H:%M:%S', // '10/25/2006 14:30:59'
|
||||
'%m/%d/%Y %H:%M', // '10/25/2006 14:30'
|
||||
'%m/%d/%Y', // '10/25/2006'
|
||||
'%m/%d/%y %H:%M:%S', // '10/25/06 14:30:59'
|
||||
'%m/%d/%y %H:%M', // '10/25/06 14:30'
|
||||
'%m/%d/%y', // '10/25/06'
|
||||
);
|
||||
|
||||
public function clean($value)
|
||||
{
|
||||
parent::clean($value);
|
||||
if (in_array($value, $this->empty_values)) {
|
||||
return '';
|
||||
}
|
||||
foreach ($this->input_formats as $format) {
|
||||
if (false !== ($date = strptime($value, $format))) {
|
||||
$day = $date['tm_mday'];
|
||||
$month = $date['tm_mon'] + 1;
|
||||
$year = $date['tm_year'] + 1900;
|
||||
// PHP's strptime has various quirks, e.g. it doesn't check
|
||||
// gregorian dates for validity and it also allows '60' in
|
||||
// the seconds part
|
||||
if (checkdate($month, $day, $year) && $date['tm_sec'] < 60) {
|
||||
$date = str_pad($year, 4, '0', STR_PAD_LEFT).'-'.
|
||||
str_pad($month, 2, '0', STR_PAD_LEFT).'-'.
|
||||
str_pad($day, 2, '0', STR_PAD_LEFT).' '.
|
||||
str_pad($date['tm_hour'], 2, '0', STR_PAD_LEFT).':'.
|
||||
str_pad($date['tm_min'], 2, '0', STR_PAD_LEFT).':';
|
||||
str_pad($date['tm_sec'], 2, '0', STD_PAD_LEFT);
|
||||
|
||||
// we internally use GMT, so we convert it to a GMT date.
|
||||
return gmdate('Y-m-d H:i:s', strtotime($date));
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new Pluf_Form_Invalid(__('Enter a valid date/time.'));
|
||||
}
|
||||
}
|
42
pluf/src/Pluf/Form/Field/Email.php
Normal file
42
pluf/src/Pluf/Form/Field/Email.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_Email extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_TextInput';
|
||||
|
||||
public function clean($value)
|
||||
{
|
||||
parent::clean($value);
|
||||
if (in_array($value, $this->empty_values)) {
|
||||
$value = '';
|
||||
}
|
||||
if ($value == '') {
|
||||
return $value;
|
||||
}
|
||||
if (!Pluf_Utils::isValidEmail($value)) {
|
||||
throw new Pluf_Form_Invalid(__('Enter a valid email address.'));
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
151
pluf/src/Pluf/Form/Field/File.php
Normal file
151
pluf/src/Pluf/Form/Field/File.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_File extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_FileInput';
|
||||
public $move_function = 'Pluf_Form_Field_File_moveToUploadFolder';
|
||||
public $max_size = 2097152; // 2MB
|
||||
public $move_function_params = array();
|
||||
|
||||
/**
|
||||
* Validate some possible input for the field.
|
||||
*
|
||||
* @param mixed Input
|
||||
* @return string Path to the file relative to 'upload_path'
|
||||
*/
|
||||
function clean($value)
|
||||
{
|
||||
parent::clean($value);
|
||||
if (is_null($value) and !$this->required) {
|
||||
return ''; // no file
|
||||
} elseif (is_null($value) and $this->required) {
|
||||
throw new Pluf_Form_Invalid(__('No files were uploaded. Please try to send the file again.'));
|
||||
}
|
||||
$errors = array();
|
||||
$no_files = false;
|
||||
switch ($value['error']) {
|
||||
case UPLOAD_ERR_OK:
|
||||
break;
|
||||
case UPLOAD_ERR_INI_SIZE:
|
||||
throw new Pluf_Form_Invalid(sprintf(__('The uploaded file is too large. Reduce the size of the file to %s and send it again.'),
|
||||
Pluf_Utils::prettySize(ini_get('upload_max_filesize'))));
|
||||
break;
|
||||
case UPLOAD_ERR_FORM_SIZE:
|
||||
throw new Pluf_Form_Invalid(sprintf(__('The uploaded file is too large. Reduce the size of the file to %s and send it again.'),
|
||||
Pluf_Utils::prettySize($_REQUEST['MAX_FILE_SIZE'])));
|
||||
break;
|
||||
case UPLOAD_ERR_PARTIAL:
|
||||
throw new Pluf_Form_Invalid(__('The upload did not complete. Please try to send the file again.'));
|
||||
break;
|
||||
case UPLOAD_ERR_NO_FILE:
|
||||
if ($this->required) {
|
||||
throw new Pluf_Form_Invalid(__('No files were uploaded. Please try to send the file again.'));
|
||||
} else {
|
||||
return ''; // no file
|
||||
}
|
||||
break;
|
||||
case UPLOAD_ERR_NO_TMP_DIR:
|
||||
case UPLOAD_ERR_CANT_WRITE:
|
||||
throw new Pluf_Form_Invalid(__('The server has no temporary folder correctly configured to store the uploaded file.'));
|
||||
break;
|
||||
case UPLOAD_ERR_EXTENSION:
|
||||
throw new Pluf_Form_Invalid(__('The uploaded file has been stopped by an extension.'));
|
||||
break;
|
||||
default:
|
||||
throw new Pluf_Form_Invalid(__('An error occured when upload the file. Please try to send the file again.'));
|
||||
}
|
||||
if ($value['size'] > $this->max_size) {
|
||||
throw new Pluf_Form_Invalid(sprintf(__('The uploaded file is to big (%1$s). Reduce the size to less than %2$s and try again.'),
|
||||
Pluf_Utils::prettySize($value['size']),
|
||||
Pluf_Utils::prettySize($this->max_size)));
|
||||
}
|
||||
// copy the file to the final destination and updated $value
|
||||
// with the final path name. 'final_name' is relative to
|
||||
// Pluf::f('upload_path')
|
||||
Pluf::loadFunction($this->move_function);
|
||||
// Should throw a Pluf_Form_Invalid exception if error or the
|
||||
// value to be stored in the database.
|
||||
return call_user_func($this->move_function, $value,
|
||||
$this->move_function_params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Default move function. The file name is sanitized.
|
||||
*
|
||||
* In the extra parameters, options can be used so that this function is
|
||||
* matching most of the needs:
|
||||
*
|
||||
* * 'upload_path': The path in which the uploaded file will be
|
||||
* stored.
|
||||
* * 'upload_path_create': If set to true, try to create the
|
||||
* upload path if not existing.
|
||||
*
|
||||
* * 'upload_overwrite': Set it to true if you want to allow overwritting.
|
||||
*
|
||||
* * 'file_name': Force the file name to this name and do not use the
|
||||
* original file name. If this name contains '%s' for
|
||||
* example 'myid-%s', '%s' will be replaced by the
|
||||
* original filename. This can be used when for
|
||||
* example, you want to prefix with the id of an
|
||||
* article all the files attached to this article.
|
||||
*
|
||||
* If you combine those options, you can dynamically generate the path
|
||||
* name in your form (for example date base) and let this upload
|
||||
* function create it on demand.
|
||||
*
|
||||
* @param array Upload value of the form.
|
||||
* @param array Extra parameters. If upload_path key is set, use it. (array())
|
||||
* @return string Name relative to the upload path.
|
||||
*/
|
||||
function Pluf_Form_Field_File_moveToUploadFolder($value, $params=array())
|
||||
{
|
||||
$name = Pluf_Utils::cleanFileName($value['name']);
|
||||
$upload_path = Pluf::f('upload_path', '/tmp');
|
||||
if (isset($params['file_name'])) {
|
||||
if (false !== strpos($params['file_name'], '%s')) {
|
||||
$name = sprintf($params['file_name'], $name);
|
||||
} else {
|
||||
$name = $params['file_name'];
|
||||
}
|
||||
}
|
||||
if (isset($params['upload_path'])) {
|
||||
$upload_path = $params['upload_path'];
|
||||
}
|
||||
$dest = $upload_path.'/'.$name;
|
||||
if (isset($params['upload_path_create'])
|
||||
and !is_dir(dirname($dest))) {
|
||||
if (false == @mkdir(dirname($dest), 0777, true)) {
|
||||
throw new Pluf_Form_Invalid(__('An error occured when creating the upload path. Please try to send the file again.'));
|
||||
}
|
||||
}
|
||||
if ((!isset($params['upload_overwrite']) or $params['upload_overwrite'] == false) and file_exists($dest)) {
|
||||
throw new Pluf_Form_Invalid(sprintf(__('A file with the name "%s" has already been uploaded.'), $name));
|
||||
}
|
||||
if (@!move_uploaded_file($value['tmp_name'], $dest)) {
|
||||
throw new Pluf_Form_Invalid(__('An error occured when uploading the file. Please try to send the file again.'));
|
||||
}
|
||||
@chmod($dest, 0666);
|
||||
return $name;
|
||||
}
|
48
pluf/src/Pluf/Form/Field/Float.php
Normal file
48
pluf/src/Pluf/Form/Field/Float.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_Float extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_TextInput';
|
||||
public $max_value = null;
|
||||
public $min_value = null;
|
||||
|
||||
public function clean($value)
|
||||
{
|
||||
parent::clean($value);
|
||||
if (in_array($value, $this->empty_values)) {
|
||||
$value = '';
|
||||
}
|
||||
if (!is_numeric($value)) {
|
||||
throw new Pluf_Form_Invalid(__('Enter a number.'));
|
||||
}
|
||||
$value = (float) $value;
|
||||
if ($this->max_value !== null and $this->max_value < $value) {
|
||||
throw new Pluf_Form_Invalid(sprintf(__('Ensure this value is less than or equal to %s.'), $this->max_value));
|
||||
}
|
||||
if ($this->min_value !== null and $this->min_value > $value) {
|
||||
throw new Pluf_Form_Invalid(sprintf(__('Ensure this value is greater than or equal to %s.'), $this->min_value));
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
56
pluf/src/Pluf/Form/Field/Integer.php
Normal file
56
pluf/src/Pluf/Form/Field/Integer.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_Integer extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_TextInput';
|
||||
public $max = null;
|
||||
public $min = null;
|
||||
|
||||
public function clean($value)
|
||||
{
|
||||
parent::clean($value);
|
||||
$value = $this->setDefaultEmpty($value);
|
||||
if ($this->multiple) {
|
||||
return $this->multiClean($value);
|
||||
} else {
|
||||
if ($value == '') return $value;
|
||||
if (!preg_match('/^[\+\-]?[0-9]+$/', $value)) {
|
||||
throw new Pluf_Form_Invalid(__('The value must be an integer.'));
|
||||
}
|
||||
$this->checkMinMax($value);
|
||||
}
|
||||
return (int) $value;
|
||||
}
|
||||
|
||||
protected function checkMinMax($value)
|
||||
{
|
||||
if ($this->max !== null and $value > $this->max) {
|
||||
throw new Pluf_Form_Invalid(sprintf(__('Ensure that this value is not greater than %1$d.'), $this->max));
|
||||
}
|
||||
if ($this->min !== null and $value < $this->min) {
|
||||
throw new Pluf_Form_Invalid(sprintf(__('Ensure that this value is not lower than %1$d.'), $this->min));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
170
pluf/src/Pluf/Form/Field/ReCaptcha.php
Normal file
170
pluf/src/Pluf/Form/Field/ReCaptcha.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
/**
|
||||
* Add ReCaptcha control to your forms.
|
||||
*
|
||||
* You need first to get a ReCaptcha account, create a domain and get
|
||||
* the API keys for your domain. Check http://recaptcha.net/ for more
|
||||
* information.
|
||||
*
|
||||
* The recaptcha field needs to know the IP address of the user
|
||||
* submitting the form and if the request is made over SSL or
|
||||
* not. This means that you need to provide the $request object in the
|
||||
* extra parameters of your form.
|
||||
*
|
||||
* To add the ReCaptcha field to your form, simply add the following
|
||||
* to your form object (note the use of $extra['request']):
|
||||
*
|
||||
* <pre>
|
||||
* $ssl = (!empty($extra['request']->SERVER['HTTPS'])
|
||||
* and $extra['request']->SERVER['HTTPS'] != 'off');
|
||||
*
|
||||
* $this->fields['recaptcha'] = new Pluf_Form_Field_ReCaptcha(
|
||||
* array('required' => true,
|
||||
* 'label' => __('Please solve this challenge'),
|
||||
* 'privkey' => 'PRIVATE_RECAPTCHA_KEY_HERE',
|
||||
* 'remoteip' => $extra['request']->remote_addr,
|
||||
* 'widget_attrs' => array(
|
||||
* 'pubkey' => 'PUBLIC_RECAPTCHA_KEY_HERE',
|
||||
* ),
|
||||
* ));
|
||||
* </pre>
|
||||
*
|
||||
* Then in your template, you simply need to add the ReCaptcha field:
|
||||
*
|
||||
* <pre>
|
||||
* {if $form.f.recaptcha.errors}{$form.f.recaptcha.fieldErrors}{/if}
|
||||
* {$form.f.recaptcha|safe}
|
||||
* </pre>
|
||||
*
|
||||
* Based on http://recaptcha.googlecode.com/files/recaptcha-php-1.10.zip
|
||||
*
|
||||
* Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
|
||||
* AUTHORS:
|
||||
* Mike Crawford
|
||||
* Ben Maurer
|
||||
*/
|
||||
class Pluf_Form_Field_ReCaptcha extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_ReCaptcha';
|
||||
public $privkey = '';
|
||||
public $remoteip = '';
|
||||
public $extra_params = array();
|
||||
|
||||
public function clean($value)
|
||||
{
|
||||
// will throw the Pluf_Form_Invalid exception in case of
|
||||
// error.
|
||||
self::checkAnswer($this->privkey, $this->remoteip,
|
||||
$value[0], $value[1], $this->extra_params);
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Submits an HTTP POST to a reCAPTCHA server
|
||||
*
|
||||
* @param string Host
|
||||
* @param string Path
|
||||
* @param array Data
|
||||
* @param int port (80
|
||||
* @return array response
|
||||
*/
|
||||
public static function httpPost($host, $path, $data, $port=80)
|
||||
{
|
||||
|
||||
$req = self::qsencode($data);
|
||||
$http_request = "POST $path HTTP/1.0\r\n";
|
||||
$http_request .= "Host: $host\r\n";
|
||||
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
|
||||
$http_request .= "Content-Length: " . strlen($req) . "\r\n";
|
||||
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n";
|
||||
$http_request .= "\r\n";
|
||||
$http_request .= $req;
|
||||
|
||||
if (false === ($fs=@fsockopen($host, $port, $errno, $errstr, 10))) {
|
||||
throw new Pluf_Form_Invalid(__('Cannot connect to the reCaptcha server for validation.'));
|
||||
}
|
||||
fwrite($fs, $http_request);
|
||||
$response = '';
|
||||
while (!feof($fs)) {
|
||||
$response .= fgets($fs, 1160); // One TCP-IP packet
|
||||
}
|
||||
fclose($fs);
|
||||
return explode("\r\n\r\n", $response, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the given data into a query string format
|
||||
*
|
||||
* @param array Array of string elements to be encoded
|
||||
* @return string Encoded request
|
||||
*/
|
||||
public static function qsencode($data)
|
||||
{
|
||||
$d = array();
|
||||
foreach ($data as $key => $value) {
|
||||
$d[] = $key.'='.urlencode(stripslashes($value));
|
||||
}
|
||||
return implode('&', $d);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls an HTTP POST function to verify if the user's guess was correct
|
||||
* @param string $privkey
|
||||
* @param string $remoteip
|
||||
* @param string $challenge
|
||||
* @param string $response
|
||||
* @param array $extra_params an array of extra variables to post to the server
|
||||
* @return ReCaptchaResponse
|
||||
*/
|
||||
public static function checkAnswer($privkey, $remoteip, $challenge, $response, $extra_params=array())
|
||||
{
|
||||
if ($privkey == '') {
|
||||
throw new Pluf_Form_Invalid(__('To use reCAPTCHA you must set your API key.'));
|
||||
}
|
||||
if ($remoteip == '') {
|
||||
throw new Pluf_Form_Invalid(__('For security reasons, you must pass the remote ip to reCAPTCHA.'));
|
||||
}
|
||||
//discard spam submissions
|
||||
if (strlen($challenge) == 0 || strlen($response) == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$response = self::httpPost('api-verify.recaptcha.net', '/verify',
|
||||
array(
|
||||
'privatekey' => $privkey,
|
||||
'remoteip' => $remoteip,
|
||||
'challenge' => $challenge,
|
||||
'response' => $response
|
||||
) + $extra_params
|
||||
);
|
||||
|
||||
$answers = explode("\n", $response[1]);
|
||||
if (trim($answers[0]) == 'true') {
|
||||
return true;
|
||||
} else {
|
||||
throw new Pluf_Form_Invalid($answers[1]);
|
||||
}
|
||||
}
|
||||
}
|
111
pluf/src/Pluf/Form/Field/Slug.php
Normal file
111
pluf/src/Pluf/Form/Field/Slug.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2010 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_Slug extends Pluf_Form_Field
|
||||
{
|
||||
/**
|
||||
* Name of the widget to use for build the forms.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $widget = 'Pluf_Form_Widget_TextInput';
|
||||
|
||||
/**
|
||||
* Minimum size of field.
|
||||
*
|
||||
* Default to 1.
|
||||
*
|
||||
* @var int
|
||||
**/
|
||||
public $min_size = 1;
|
||||
|
||||
/**
|
||||
* Maximum size of field.
|
||||
*
|
||||
* Default to 50.
|
||||
*
|
||||
* @var int
|
||||
**/
|
||||
public $max_size = 50;
|
||||
|
||||
protected $_error_messages = array();
|
||||
|
||||
public function __construct($params=array())
|
||||
{
|
||||
if (in_array($this->help_text, $this->empty_values)) {
|
||||
$this->help_text = __('The “slug” is the URL-friendly'.
|
||||
' version of the name, consisting of '.
|
||||
'letters, numbers, underscores or hyphens.');
|
||||
}
|
||||
$this->_error_messages = array(
|
||||
'min_size' => __('Ensure this value has at most %1$d characters (it has %2$d).'),
|
||||
'max_size' => __('Ensure this value has at least %1$d characters (it has %2$d).')
|
||||
);
|
||||
|
||||
parent::__construct($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes any character not allowed and valid the size of the field.
|
||||
*
|
||||
* @see Pluf_Form_Field::clean()
|
||||
* @throws Pluf_Form_Invalid If the lenght of the field has not a valid size.
|
||||
*/
|
||||
public function clean($value)
|
||||
{
|
||||
parent::clean($value);
|
||||
if ($value) {
|
||||
$value = Pluf_DB_Field_Slug::slugify($value);
|
||||
$len = mb_strlen($value, Pluf::f('encoding', 'UTF-8'));
|
||||
if ($this->max_size < $len) {
|
||||
throw new Pluf_Form_Invalid(sprintf($this->_error_messages['max_size'],
|
||||
$this->max_size,
|
||||
$len));
|
||||
}
|
||||
if ($this->min_size > $len) {
|
||||
throw new Pluf_Form_Invalid(sprintf($this->_error_messages['min_size'],
|
||||
$this->min_size,
|
||||
$len));
|
||||
}
|
||||
}
|
||||
else
|
||||
$value = '';
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see Pluf_Form_Field::widgetAttrs()
|
||||
*/
|
||||
public function widgetAttrs($widget)
|
||||
{
|
||||
$attrs = array();
|
||||
if (!isset($widget->attrs['maxlength'])) {
|
||||
$attrs['maxlength'] = $this->max_size;
|
||||
} else {
|
||||
$this->max_size = $widget->attrs['maxlength'];
|
||||
}
|
||||
|
||||
return $attrs;
|
||||
}
|
||||
}
|
39
pluf/src/Pluf/Form/Field/Url.php
Normal file
39
pluf/src/Pluf/Form/Field/Url.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_Url extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_TextInput';
|
||||
|
||||
public function clean($value)
|
||||
{
|
||||
parent::clean($value);
|
||||
if (in_array($value, $this->empty_values)) {
|
||||
return '';
|
||||
}
|
||||
if (!Pluf_Utils::isValidUrl($value)) {
|
||||
throw new Pluf_Form_Invalid(__('Enter a valid address.'));
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
}
|
57
pluf/src/Pluf/Form/Field/Varchar.php
Normal file
57
pluf/src/Pluf/Form/Field/Varchar.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
||||
/*
|
||||
# ***** BEGIN LICENSE BLOCK *****
|
||||
# This file is part of Plume Framework, a simple PHP Application Framework.
|
||||
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
|
||||
#
|
||||
# Plume Framework is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by
|
||||
# the Free Software Foundation; either version 2.1 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# Plume Framework is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Lesser General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
#
|
||||
# ***** END LICENSE BLOCK ***** */
|
||||
|
||||
class Pluf_Form_Field_Varchar extends Pluf_Form_Field
|
||||
{
|
||||
public $widget = 'Pluf_Form_Widget_TextInput';
|
||||
public $max_length = null;
|
||||
public $min_length = null;
|
||||
|
||||
public function clean($value)
|
||||
{
|
||||
parent::clean($value);
|
||||
if (in_array($value, $this->empty_values)) {
|
||||
$value = '';
|
||||
}
|
||||
$value_length = mb_strlen($value);
|
||||
if ($this->max_length !== null and $value_length > $this->max_length) {
|
||||
throw new Pluf_Form_Invalid(sprintf(__('Ensure this value has at most %1$d characters (it has %2$d).'), $this->max_length, $value_length));
|
||||
}
|
||||
if ($this->min_length !== null and $value_length < $this->min_length) {
|
||||
throw new Pluf_Form_Invalid(sprintf(__('Ensure this value has at least %1$d characters (it has %2$d).'), $this->min_length, $value_length));
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function widgetAttrs($widget)
|
||||
{
|
||||
if ($this->max_length !== null and
|
||||
in_array(get_class($widget),
|
||||
array('Pluf_Form_Widget_TextInput',
|
||||
'Pluf_Form_Widget_PasswordInput'))) {
|
||||
return array('maxlength'=>$this->max_length);
|
||||
}
|
||||
return array();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user