2011-05-15 01:32:47 +02:00
< ? php
2014-09-04 17:52:18 +02:00
/**
* This software is governed by the CeCILL - B license . If a copy of this license
* is not distributed with this file , you can obtain one at
* http :// www . cecill . info / licences / Licence_CeCILL - B_V1 - en . txt
*
* Authors of STUdS ( initial project ) : Guilhem BORGHESI ( borghesi @ unistra . fr ) and Raphaël DROZ
2016-08-04 22:26:37 +02:00
* Authors of Framadate / OpenSondage : Framasoft ( https :// github . com / framasoft )
2014-09-04 17:52:18 +02:00
*
2014-07-04 11:21:31 +02:00
* =============================
2014-09-04 17:52:18 +02:00
*
* Ce logiciel est régi par la licence CeCILL - B . Si une copie de cette licence
* ne se trouve pas avec ce fichier vous pouvez l ' obtenir sur
* http :// www . cecill . info / licences / Licence_CeCILL - B_V1 - fr . txt
*
2014-07-04 11:21:31 +02:00
* Auteurs de STUdS ( projet initial ) : Guilhem BORGHESI ( borghesi @ unistra . fr ) et Raphaël DROZ
2014-09-04 17:52:18 +02:00
* Auteurs de Framadate / OpenSondage : Framasoft ( https :// github . com / framasoft )
2014-07-04 11:21:31 +02:00
*/
2018-02-19 00:18:43 +01:00
use Framadate\Choice ;
2018-05-25 18:16:22 +02:00
use Framadate\Form ;
2018-04-20 22:05:13 +02:00
use Framadate\Services\InputService ;
2014-12-26 23:32:53 +01:00
use Framadate\Services\LogService ;
use Framadate\Services\MailService ;
2018-02-19 00:18:43 +01:00
use Framadate\Services\PollService ;
2014-12-29 21:54:07 +01:00
use Framadate\Services\PurgeService ;
2018-04-08 11:21:11 +02:00
use Framadate\Services\SessionService ;
2014-12-26 23:32:53 +01:00
use Framadate\Utils ;
2014-09-04 17:52:18 +02:00
2014-12-06 19:42:01 +01:00
include_once __DIR__ . '/app/inc/init.php' ;
2014-12-26 23:32:53 +01:00
/* Service */
/*---------*/
2015-01-06 23:52:52 +01:00
$logService = new LogService ();
2014-12-26 23:32:53 +01:00
$pollService = new PollService ( $connect , $logService );
2018-05-25 15:50:35 +02:00
$mailService = new MailService ( $config [ 'use_smtp' ], $config [ 'smtp_options' ], $config [ 'use_sendmail' ]);
2014-12-29 21:54:07 +01:00
$purgeService = new PurgeService ( $connect , $logService );
2018-04-08 11:21:11 +02:00
$sessionService = new SessionService ();
2014-12-26 23:32:53 +01:00
2015-09-03 08:03:37 +02:00
if ( is_file ( 'bandeaux_local.php' )) {
2014-09-04 17:52:18 +02:00
include_once ( 'bandeaux_local.php' );
2011-05-15 03:56:54 +02:00
} else {
2014-09-04 17:52:18 +02:00
include_once ( 'bandeaux.php' );
2011-05-15 01:32:47 +02:00
}
2018-04-20 22:05:13 +02:00
// Min/Max archive date
$min_expiry_time = $pollService -> minExpiryDate ();
$max_expiry_time = $pollService -> maxExpiryDate ();
2018-05-25 18:16:22 +02:00
$form = isset ( $_SESSION [ 'form' ]) ? unserialize ( $_SESSION [ 'form' ]) : null ;
if ( $form === null || ! ( $form instanceof Form )) {
$smarty -> assign ( 'title' , __ ( 'Error' , 'Error!' ));
$smarty -> assign ( 'error' , __ ( 'Error' , 'You haven\'t filled the first section of the poll creation, or your session has expired.' ));
$smarty -> display ( 'error.tpl' );
exit ;
}
2018-05-24 17:45:22 +02:00
2018-05-25 10:57:46 +02:00
// The poll format is AUTRE (other) if we are in this file
if ( ! isset ( $form -> format )) {
$form -> format = 'A' ;
}
// The poll format is AUTRE (other)
if ( $form -> format !== 'A' ) {
$form -> format = 'A' ;
$form -> clearChoices ();
}
2018-04-20 22:05:13 +02:00
if ( ! isset ( $form -> title ) || ! isset ( $form -> admin_name ) || ( $config [ 'use_smtp' ] && ! isset ( $form -> admin_mail ))) {
$step = 1 ;
} elseif ( isset ( $_POST [ 'confirmation' ])) {
$step = 4 ;
} elseif ( empty ( $_POST [ 'fin_sondage_autre' ]) ) {
$step = 2 ;
} else {
$step = 3 ;
2018-02-21 11:07:11 +01:00
}
2015-01-04 01:36:55 +01:00
2018-04-20 22:05:13 +02:00
switch ( $step ) {
case 2 : // Step 2/4 : Select choices of the poll
$choices = $form -> getChoices ();
$nb_choices = max ( 5 - count ( $choices ), 0 );
while ( $nb_choices -- > 0 ) {
$c = new Choice ( '' );
$form -> addChoice ( $c );
2015-01-04 01:36:55 +01:00
}
2018-04-20 22:05:13 +02:00
$_SESSION [ 'form' ] = serialize ( $form );
2014-12-26 23:32:53 +01:00
2018-04-20 22:05:13 +02:00
// Display step 2
2018-07-06 18:06:35 +02:00
$smarty -> assign ( 'title' , __ ( 'Step 2 classic' , 'Poll options (2 of 3)' ));
2018-04-20 22:05:13 +02:00
$smarty -> assign ( 'choices' , $form -> getChoices ());
$smarty -> assign ( 'allowMarkdown' , $config [ 'user_can_add_img_or_link' ]);
$smarty -> assign ( 'error' , null );
2014-12-06 19:42:01 +01:00
2018-04-20 22:05:13 +02:00
$smarty -> display ( 'create_classic_poll_step_2.tpl' );
exit ;
2014-12-26 23:32:53 +01:00
2018-04-20 22:05:13 +02:00
case 3 : // Step 3/4 : Confirm poll creation and choose a removal date
// Handle Step2 submission
if ( ! empty ( $_POST [ 'choices' ])) {
// remove empty choices
$_POST [ 'choices' ] = array_filter ( $_POST [ 'choices' ], function ( $c ) {
return ! empty ( $c );
});
2018-04-23 10:27:15 +02:00
2018-05-24 17:45:22 +02:00
$form -> clearChoices ();
2018-04-20 22:05:13 +02:00
// store choices in $_SESSION
2014-12-26 23:32:53 +01:00
foreach ( $_POST [ 'choices' ] as $c ) {
2018-04-20 22:05:13 +02:00
$c = strip_tags ( $c );
$choice = new Choice ( $c );
$form -> addChoice ( $choice );
2014-09-04 17:52:18 +02:00
}
2011-05-22 04:03:44 +02:00
}
2014-09-04 17:52:18 +02:00
2018-07-06 18:06:35 +02:00
// Expiry date is initialised with config parameter. Value will be modified in step 4 if user has defined an other date
2018-05-24 17:45:22 +02:00
$form -> end_date = $max_expiry_time ;
2014-10-21 01:31:26 +02:00
2014-11-12 16:35:13 +01:00
// Summary
2014-10-21 01:31:26 +02:00
$summary = '<ol>' ;
2018-04-20 22:05:13 +02:00
foreach ( $form -> getChoices () as $i => $choice ) {
/** @var Choice $choice */
2014-12-26 23:32:53 +01:00
preg_match_all ( '/\[!\[(.*?)\]\((.*?)\)\]\((.*?)\)/' , $choice -> getName (), $md_a_img ); // Markdown [![alt](src)](href)
preg_match_all ( '/!\[(.*?)\]\((.*?)\)/' , $choice -> getName (), $md_img ); // Markdown ![alt](src)
preg_match_all ( '/\[(.*?)\]\((.*?)\)/' , $choice -> getName (), $md_a ); // Markdown [text](href)
2018-02-19 00:18:43 +01:00
if ( isset ( $md_a_img [ 2 ][ 0 ]) && $md_a_img [ 2 ][ 0 ] !== '' && isset ( $md_a_img [ 3 ][ 0 ]) && $md_a_img [ 3 ][ 0 ] !== '' ) { // [![alt](src)](href)
$li_subject_text = ( isset ( $md_a_img [ 1 ][ 0 ]) && $md_a_img [ 1 ][ 0 ] !== '' ) ? stripslashes ( $md_a_img [ 1 ][ 0 ]) : __ ( 'Generic' , 'Choice' ) . ' ' . ( $i + 1 );
2014-12-26 23:32:53 +01:00
$li_subject_html = '<a href="' . $md_a_img [ 3 ][ 0 ] . '"><img src="' . $md_a_img [ 2 ][ 0 ] . '" class="img-responsive" alt="' . $li_subject_text . '" /></a>' ;
2018-02-19 00:18:43 +01:00
} elseif ( isset ( $md_img [ 2 ][ 0 ]) && $md_img [ 2 ][ 0 ] !== '' ) { // ![alt](src)
$li_subject_text = ( isset ( $md_img [ 1 ][ 0 ]) && $md_img [ 1 ][ 0 ] !== '' ) ? stripslashes ( $md_img [ 1 ][ 0 ]) : __ ( 'Generic' , 'Choice' ) . ' ' . ( $i + 1 );
2014-12-26 23:32:53 +01:00
$li_subject_html = '<img src="' . $md_img [ 2 ][ 0 ] . '" class="img-responsive" alt="' . $li_subject_text . '" />' ;
2018-02-19 00:18:43 +01:00
} elseif ( isset ( $md_a [ 2 ][ 0 ]) && $md_a [ 2 ][ 0 ] !== '' ) { // [text](href)
$li_subject_text = ( isset ( $md_a [ 1 ][ 0 ]) && $md_a [ 1 ][ 0 ] !== '' ) ? stripslashes ( $md_a [ 1 ][ 0 ]) : __ ( 'Generic' , 'Choice' ) . ' ' . ( $i + 1 );
2014-12-26 23:32:53 +01:00
$li_subject_html = '<a href="' . $md_a [ 2 ][ 0 ] . '">' . $li_subject_text . '</a>' ;
2014-10-21 01:31:26 +02:00
} else { // text only
2014-12-06 19:42:01 +01:00
$li_subject_text = stripslashes ( $choice -> getName ());
2014-10-21 01:31:26 +02:00
$li_subject_html = $li_subject_text ;
}
2014-12-26 23:32:53 +01:00
$summary .= '<li>' . $li_subject_html . '</li>' . " \n " ;
2014-10-21 01:31:26 +02:00
}
$summary .= '</ol>' ;
2015-10-30 22:08:38 +01:00
$end_date_str = utf8_encode ( strftime ( $date_format [ 'txt_date' ], $max_expiry_time )); //textual date
2014-12-06 19:42:01 +01:00
2018-05-24 17:45:22 +02:00
$_SESSION [ 'form' ] = serialize ( $form );
2018-07-06 18:06:35 +02:00
$smarty -> assign ( 'title' , __ ( 'Step 3' , 'Removal date and confirmation (3 of 3)' ));
2016-05-04 00:08:29 +02:00
$smarty -> assign ( 'summary' , $summary );
$smarty -> assign ( 'end_date_str' , $end_date_str );
$smarty -> assign ( 'default_poll_duration' , $config [ 'default_poll_duration' ]);
$smarty -> assign ( 'use_smtp' , $config [ 'use_smtp' ]);
2018-04-20 22:05:13 +02:00
$smarty -> display ( 'create_poll_step_3.tpl' );
exit ;
case 4 : // Step 4 : Data prepare before insert in DB
$enddate = filter_input ( INPUT_POST , 'enddate' , FILTER_VALIDATE_REGEXP , [ 'options' => [ 'regexp' => '#^[0-9]{2}/[0-9]{2}/[0-9]{4}$#' ]]);
if ( ! empty ( $enddate )) {
$registredate = explode ( '/' , $enddate );
if ( is_array ( $registredate ) && count ( $registredate ) === 3 ) {
$time = mktime ( 0 , 0 , 0 , $registredate [ 1 ], $registredate [ 0 ], $registredate [ 2 ]);
if ( $time < $min_expiry_time ) {
$form -> end_date = $min_expiry_time ;
} elseif ( $max_expiry_time < $time ) {
$form -> end_date = $max_expiry_time ;
} else {
$form -> end_date = $time ;
}
}
2014-11-14 17:35:22 +01:00
}
2014-09-04 17:52:18 +02:00
2018-04-20 22:05:13 +02:00
if ( empty ( $form -> end_date )) {
// By default, expiration date is 6 months after last day
$form -> end_date = $max_expiry_time ;
}
// Insert poll in database
$ids = $pollService -> createPoll ( $form );
$poll_id = $ids [ 0 ];
$admin_poll_id = $ids [ 1 ];
// Send confirmation by mail if enabled
if ( $config [ 'use_smtp' ] === true ) {
2018-07-06 18:06:35 +02:00
$message = __ ( 'Mail' , " This is the message to forward to the poll participants. " );
2018-04-20 22:05:13 +02:00
$message .= '<br/><br/>' ;
2018-07-06 18:06:35 +02:00
$message .= Utils :: htmlMailEscape ( $form -> admin_name ) . ' ' . __ ( 'Mail' , 'has just created a poll called' ) . ' : "' . Utils :: htmlMailEscape ( $form -> title ) . '".<br/>' ;
$message .= sprintf ( __ ( 'Mail' , 'Thank you for participating in the poll at the following link' ) . ' :<br/><br/><a href="%1$s">%1$s</a>' , Utils :: getUrlSondage ( $poll_id ));
2018-04-20 22:05:13 +02:00
2018-07-06 18:06:35 +02:00
$message_admin = __ ( 'Mail' , " This message should NOT be sent to the poll participants. You should keep it private. <br/><br/>You can modify your poll at the following link " );
2018-04-20 22:05:13 +02:00
$message_admin .= sprintf ( ' :<br/><br/><a href="%1$s">%1$s</a>' , Utils :: getUrlSondage ( $admin_poll_id , true ));
if ( $mailService -> isValidEmail ( $form -> admin_mail )) {
2018-07-06 18:06:35 +02:00
$mailService -> send ( $form -> admin_mail , '[' . NOMAPPLICATION . '][' . __ ( 'Mail' , 'Message for the author' ) . '] ' . __ ( 'Generic' , 'Poll' ) . ': ' . $form -> title , $message_admin );
$mailService -> send ( $form -> admin_mail , '[' . NOMAPPLICATION . '][' . __ ( 'Mail' , 'Participant link' ) . '] ' . __ ( 'Generic' , 'Poll' ) . ': ' . $form -> title , $message );
2014-12-26 23:32:53 +01:00
}
2014-09-04 17:52:18 +02:00
}
2018-04-20 22:05:13 +02:00
// Clean Form data in $_SESSION
unset ( $_SESSION [ 'form' ]);
2018-02-19 00:18:43 +01:00
2018-04-20 22:05:13 +02:00
// Delete old polls
$purgeService -> purgeOldPolls ();
// creation message
$sessionService -> set ( " Framadate " , " messagePollCreated " , TRUE );
// Redirect to poll administration
header ( 'Location:' . Utils :: getUrlSondage ( $admin_poll_id , true ));
exit ;
case 1 : // Step 1/4 : error if $_SESSION from info_sondage are not valid
default :
$smarty -> assign ( 'title' , __ ( 'Error' , 'Error!' ));
2018-05-25 18:16:22 +02:00
$smarty -> assign ( 'error' , __ ( 'Error' , 'You haven\'t filled the first section of the poll creation, or your session has expired.' ));
2018-04-20 22:05:13 +02:00
$smarty -> display ( 'error.tpl' );
exit ;
}