/mem_simple_form/mem_simple_form.php
PHP | 399 lines | 304 code | 70 blank | 25 comment | 58 complexity | 0883207013a77cc4987505ecc5590a44 MD5 | raw file
- <?php
-
- // This is a PLUGIN TEMPLATE.
-
- // Copy this file to a new name like abc_myplugin.php. Edit the code, then
- // run this file at the command line to produce a plugin for distribution:
- // $ php abc_myplugin.php > abc_myplugin-0.1.txt
-
- // Plugin name is optional. If unset, it will be extracted from the current
- // file name. Uncomment and edit this line to override:
- $plugin['name'] = 'mem_simple_form';
-
- // 0 = Plugin help is in Textile format, no raw HTML allowed (default).
- // 1 = Plugin help is in raw HTML. Not recommended.
- # $plugin['allow_html_help'] = 1;
-
- $plugin['version'] = '0.3.2';
- $plugin['author'] = 'Michael Manfre';
- $plugin['author_uri'] = 'http://manfre.net/';
- $plugin['description'] = 'Store a form to a table.';
-
- // Plugin types:
- // 0 = regular plugin; loaded on the public web side only
- // 1 = admin plugin; loaded on both the public and admin side
- // 2 = library; loaded only when include_plugin() or require_plugin() is called
- $plugin['type'] = 0;
-
- if (!defined('txpinterface'))
- @include_once('../zem_tpl.php');
-
- if (0) {
- ?>
- # --- BEGIN PLUGIN HELP ---
-
- h1(title). mem_simple_form plugin
-
- h2(section summary). Summary
-
- p. This plugin allows for a site owner to have a form accept input and store the data in an existing table. Basic processing can be done to
- the data before writing to the table.
-
- p. This plugin requires mem_form.
-
- p. This plugin has MLP support.
-
- h2(section contact). Author Contact
-
- "Michael Manfre":mailto:mmanfre@gmail.com?subject=Textpattern%20mem_simple_form%20plugin
- "http://manfre.net":http://manfre.net
-
- h2(section license). License
-
- p. This plugin is licensed under the "GPLv2":http://www.fsf.org/licensing/licenses/info/GPLv2.html.
-
- h2(section installation). Installation
-
- p. No extra installation steps
-
- h2(section tags). Tags
-
- * "mem_simple_form":#mem_simple_form
- * "mem_simple_if_ps":#mem_simple_if_ps
- * "mem_simple_ps":#mem_simple_ps
-
- h3(tag#mem_simple_form). mem_simple_form
-
- p(tag-summary). This tag will output an HTML form. The HTML form field names must follow the pattern "@<type>_<name>@", where @<type>@ is a value from
- the below type list. The database table's field name must match @<name>@ exactly (case sensitive).
-
- p(tag-summary). Form Types:
- * now - MySQL "NOW()" function. Use with mem_form_secret or mem_form_hidden.
- * password - MySQL "PASSWORD()" function
- * oldpassword - MySQL "OLD_PASSWORD()" function
- * md5 - MySQL "MD5()" function
- * sha1 - MySQL "SHA1()" function
- * rand - MySQL "RAND()" function. Use with mem_form_secret or mem_form_hidden.
- * textile - Textile parse field
- * nl2br - Convert newlines to br tags
- * randuid - Generate a random unique id (md5 hash). Use with mem_form_secret or mem_form_hidden.
- * lower - Convert to lower case
- * upper - Convert to upper case
- * base64encode - Base64 encode
- * base64decode - Base64 decode
- * string - No processing
- * int - Field contains a numeric value
-
- *(atts) %(atts-name)table% %(atts-type)string% Name of the table to insert data on a successful form submission.
- *(atts) %(atts-name)id_field% %(atts-type)string% If specified, this is the name of the table field that is a PRIMARY KEY or UNIQUE INDEX. This allows updates if a record with a matching ID already exists.
- *(atts) %(atts-name)id_insert% %(atts-type)int% If "1", the ID field will be inserted/updated. Otherwise, the field is assumed to be auto generated by the database.
- *(atts) %(atts-name)ignore_fields% %(atts-type)string% A comma separated string of field names (including prefixes) that will not be written to the table. These are handy for ToS checkboxes and CAPTCHAs.
- *(atts) %(atts-name)form% %(atts-type)string% Name of form that contains the mem_simple_form form tags.
- *(atts) %(atts-name)success_form% %(atts-type)string% Name of the form that will be shown after a successful post.
- *(atts) %(atts-name)failure_form% %(atts-type)string% Name of the form that will be shown after a failed post.
-
- h3(tag#mem_simple_if_ps). mem_simple_if_ps
-
- p(tag-summary). Conditional tag that checks to see if a HTML field was posted, or if it has a specific value.
-
- *(atts) %(atts-name)name% %(atts-type)string% HTML field name posted with the form.
- *(atts) %(atts-name)equal% %(atts-type)string% Value to compare against the value of name. If not specified, tag checks to see if form posted variable HTML field name.
-
- h3(tag#mem_simple_ps). mem_simple_ps
-
- p(tag-summary). This tag will output the value of the posted HTML form field.
-
- *(atts) %(atts-name)name% %(atts-type)string% HTML field name posted with the form.
-
-
- # --- END PLUGIN HELP ---
- <?php
- }
-
- # --- BEGIN PLUGIN CODE ---
-
-
-
-
- // MLP
- global $mem_simple_lang;
- if (!is_array($mem_simple_lang))
- {
- $mem_simple_lang = array(
- 'table_name_required' => 'You must provide a table name as an attribute to mem_simple_form',
- 'form_submit_failed' => 'The form submission failed',
- 'no_fields_received' => 'No data submitted with form.',
- 'attribute_missing' => 'Required attribute {name} not provided',
- );
- }
- define( 'MEM_SIMPLE_PREFIX' , 'mem_self' );
-
- register_callback( 'mem_simple_enumerate_strings' , 'l10n.enumerate_strings' );
- function mem_simple_enumerate_strings($event , $step='' , $pre=0)
- {
- global $mem_simple_lang;
- $r = array (
- 'owner' => 'mem_simple_form', # Change to your plugin's name
- 'prefix' => MEM_SIMPLE_PREFIX, # Its unique string prefix
- 'lang' => 'en-gb', # The language of the initial strings.
- 'event' => 'public', # public/admin/common = which interface the strings will be loaded into
- 'strings' => $mem_simple_lang, # The strings themselves.
- );
- return $r;
- }
- function mem_simple_gTxt($what,$args = array())
- {
- global $mem_simple_lang, $textarray;
-
- $key = strtolower( MEM_SIMPLE_PREFIX . '-' . $what );
-
- if (isset($textarray[$key]))
- {
- $str = $textarray[$key];
- }
- else
- {
- $key = strtolower($what);
-
- if (isset($mem_simple_lang[$key]))
- $str = $mem_simple_lang[$key];
- elseif (isset($textarray[$key]))
- $str = $textarray[$key];
- else
- $str = $what;
- }
-
- if( !empty($args) )
- $str = strtr( $str , $args );
-
- return $str;
- }
-
-
- require_plugin('mem_form');
-
-
- function mem_simple_form($atts, $thing='')
- {
- $atts = lAtts(array(
- 'type' => 'mem_simple_form',
- 'table' => false,
- 'id_field' => false,
- 'id_insert' => false,
- 'ignore_fields' => false,
- 'form' => '',
- 'success_form' => false,
- 'failure_form' => false,
- ),$atts,0);
-
- if (empty($atts['table']))
- trigger_error(gTxt('attribute_required', array('{att}' => 'table')));
-
- if (!empty($atts['form'])) {
- $thing = fetch_form($atts['form']);
- unset($atts['form']);
- }
-
- foreach(array('table', 'id_field', 'id_insert', 'ignore_fields', 'success_form', 'failure_form') as $a) {
- $thing .= '<txp:mem_form_secret name="mem_simple_'.$a.'" value="'.$atts[$a].'" />';
- unset($atts[$a]);
- }
-
- return mem_form($atts, $thing);
- }
-
-
- register_callback('mem_simple_form_submitted', 'mem_form.submit');
- function mem_simple_form_submitted()
- {
- global $mem_form_type, $mem_form_values, $production_status;
-
- if ($mem_form_type !== 'mem_simple_form')
- return;
-
- $table = @$mem_form_values['mem_simple_table'];
-
- if (empty($table))
- {
- trigger_error(mem_simple_gTxt('table_name_required'));
- // table name required
- return;
- }
-
- $id_field = @$mem_form_values['mem_simple_id_field'];
- $id_insert = @$mem_form_values['mem_simple_id_insert'];
-
- $ignore_fields = @$mem_form_values['mem_simple_ignore_fields'];
- $ignore_fields = empty($ignore_fields) ? array() : explode(',', $ignore_fields);
- $ignore_fields_names = array_values($ignore_fields);
- $ignore_fields_names[] = 'mem_form_submit';
-
- $textile = false;
- $fields = array();
-
- $id = false;
-
- foreach($mem_form_values as $k=>$v)
- {
- // skip plugin config values and ignorable fields
- if (strncmp($k, 'mem_simple_', 11) == 0 || in_array($k, $ignore_fields_names) )
- {
- continue;
- }
-
- // split the type from name. int_number_value ==> int, number_value
- list($type, $name) = explode('_', $k, 2);
-
- if ($type == 'int')
- $format = "%s = %d";
- else
- {
- // standard string format
- $format = "%s = '%s'";
-
- // mysql functions
- $funcs = array(
- 'now' => "NOW()",
- 'password' => "PASSWORD('%s')",
- 'oldpassword' => "OLD_PASSWORD('%s')",
- 'md5' => "MD5('%s')",
- 'sha1' => "SHA1('%s')",
- 'rand' => 'RAND()',
- );
-
- if (array_key_exists($type, $funcs))
- {
- $format = "%s = " . $funcs[$type];
- }
- else if ($type == 'textile')
- {
- if (!$textile)
- {
- // load textile singleton
- include_once txpath.'/lib/classTextile.php';
- $textile = new Textile();
- }
-
- $v = $textile->TextileThis($v);
- }
- else if ($type == 'nl2br')
- $v = nl2br(trim($v));
- else if ($type == 'urltitle')
- $v = stripSpace($v,1);
- else if ($type == 'randuid')
- $v = md5(uniqid(rand(),true));
- else if ($type == 'lower')
- $v = strtolower($v);
- else if ($type == 'upper')
- $v = strtoupper($v);
- else if ($type == 'base64encode')
- $v = base64_encode($v);
- else if ($type == 'base64decode')
- $v = base64_decode($v);
- }
-
- // store the processed value so others can access
- $_POST[$k] = $v;
-
- $s = sprintf($format, doSlash($name), doSlash($v));
-
- if ($id_field == $name && !$id_insert)
- {
- $id = $s;
- // don't update id field
- continue;
- }
-
- $fields[] = $s;
- }
-
- if (!empty($fields))
- {
- $insert = empty($id);
-
- if ($insert)
- {
- $rs = safe_insert( doSlash($table), join(', ',$fields) );
- }
- else
- {
- $rs = safe_upsert( doSlash($table), join(', ',$fields), $id );
- }
-
- if ($rs)
- {
- // yay
- $form = @$mem_form_values['mem_simple_success_form'];
- if (!empty($form))
- {
- $form = @fetch_form($form);
-
- if (!empty($form))
- {
- return parse($form);
- }
- }
- }
- else
- {
- // boo
- if ($production_status != 'live')
- trigger_error('Failed to store data to table. ' . mysql_error($rs));
-
- $form = @$mem_form_values['mem_simple_failure_form'];
- if (!empty($form))
- {
- $form = @fetch_form($form);
-
- if (!empty($form))
- {
- return parse($form);
- }
- }
-
- return mem_simple_gTxt('form_submit_failed');
- }
- }
- else
- {
- return mem_simple_gTxt('no_fields_received');
- }
- }
-
- function mem_simple_ps($atts,$thing='')
- {
- extract(lAtts(array(
- 'name' => false,
- ),$atts));
-
- if (!empty($name))
- {
- $n = gps($name);
- if (!empty($n))
- return $n;
- }
- return '';
- }
-
- function mem_simple_if_ps($atts, $thing='')
- {
- extract(lAtts(array(
- 'name' => false,
- 'equal' => false,
- ),$atts));
-
- if ($name === false)
- trigger_error(gTxt('attribute_missing', array('{name}' => $name)));
-
- if ($equal === false)
- $condition = isset($_POST[$name]);
- else
- $condition = (gps('name') == $equal);
- $thing = EvalElse($thing, $condition);
-
- return parse($thing);
- }
-
- # --- END PLUGIN CODE ---
-
- ?>