FirstLogin.fnc.php 5.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
<?php
/**
 * First Login functions.
 *
 * Called on index.php & misc/Portal.php on submit.
 */


if ( ! function_exists( 'DoFirstLoginForm' ) )
{
	/**
	 * Do First Login Form
	 *
	 * @since 4.0
	 *
	 * @param array $values Form values.
	 *
	 * @return bool False if no action performed or error, else true.
	 */
	function DoFirstLoginForm( $values )
	{
		global $note;

		$return = false;

		if ( ! empty( $values['ADMIN_PASSWORD'] )
			&& User( 'STAFF_ID' ) === '1' )
		{
			// Admin password set.
			$new_password = encrypt_password( $values['ADMIN_PASSWORD'] );

			DBQuery( "UPDATE STAFF
				SET PASSWORD='" . $new_password . "'
				WHERE STAFF_ID='" . User( 'STAFF_ID' ) . "'
				AND SYEAR='" . UserSyear() . "'" );

			unset( $values['ADMIN_PASSWORD'], $new_password );

			$note[] = _( 'Your new password was saved.' );

			$return = true;
		}

		// Set Config( 'LOGIN' ) to Yes.
45
		Config( 'LOGIN', 'Yes' );
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

		return $return;
	}
}

/**
 * First Login Form
 *
 * @since 4.0
 *
 * Seen by admin on first login after installation.
 *
 * @return string Form HTML.
 */
function FirstLoginForm()
{
	ob_start();

64
	PopTable( 'header', _( 'Confirm Successful Installation' ) ); ?>
65

66 67 68 69 70 71 72 73 74 75 76
	<form action="index.php" method="POST" id="first-login-form">
		<h4 class="center">
			<?php
				echo sprintf(
					_( 'You have successfully installed %s.' ),
					ParseMLField( Config( 'TITLE' ) )
				);
			?>
		</h4>
		<p><?php echo implode( '</p><p>', FirstLoginFormFields() ); ?></p>
		<p class="center"><?php echo Buttons( _( 'OK' ) ); ?></p>
77
	</form>
78 79 80
	<?php echo FirstLoginPoll(); ?>

	<?php PopTable( 'footer' );
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

	return ob_get_clean();
}


if ( ! function_exists( 'FirstLoginFormFields' ) )
{
	/**
	 * Get First Login Form Fields
	 *
	 * @since 4.0
	 *
	 * @return array Fields HTML array.
	 */
	function FirstLoginFormFields()
	{
		$fields = array();

99 100 101 102 103
		$fields[] = sprintf(
			_( 'Check the %s page to spot remaining configuration problems.' ),
			'<a href="diagnostic.php" target="_blank">diagnostic.php</a>'
		);

104 105 106
		if ( User( 'STAFF_ID' ) === '1' )
		{
			// Set admin password on first login.
107
			$fields[] = '<br /><input type="text" name="first_login[ADMIN_PASSWORD]" id="first_login_ADMIN_PASSWORD"
108
				size="25" maxlength="42" minlength="5" tabindex="1" required />' .
109 110 111 112 113
				FormatInputTitle(
					'<span class="legend-red">' . _( 'New Password' ) . '</span>',
					'first_login_ADMIN_PASSWORD',
					true
				);
114 115
		}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
		return $fields;
	}
}


if ( ! function_exists( 'FirstLoginPoll' ) )
{
	/**
	 * Get First Login Poll
	 *
	 * @since 4.6
	 *
	 * @return array Poll HTML array.
	 */
	function FirstLoginPoll()
	{
		global $locale,
			$_ROSARIO;

		if ( User( 'STAFF_ID' ) !== '1' )
		{
			return false;
		}

		// Check if client has Internet connection.
		$has_connection = @file_get_contents( 'https://www.rosariosis.org/installation-poll/poll-submit.php' );

		if ( ! $has_connection )
		{
			// Server may be down?
			return false;
		}

		$fields = array();

		$fields[] = '<input type="hidden" name="locale" value="' . $locale . '" />';

		$fields[] = '<input type="hidden" name="version" value="' . ROSARIO_VERSION . '" />';

		$_ROSARIO['allow_edit'] = true;

		$usage_options = array(
			'testing' => _( 'Testing' ),
			'production' => _( 'Production' ),
		);

		$fields[] = RadioInput( '', 'usage', _( 'Usage' ), $usage_options, false );

		$school_options = array(
			'primary' => _( 'Primary' ),
			'secondary' => _( 'Secondary' ),
			'superior' => _( 'Superior' ),
			'other' => _( 'Other' ),
		);

		$fields[] = RadioInput( '', 'school', _( 'School' ), $school_options, false );

		$fields[] = TextInput(
			'0',
			'students',
			_( 'Students' ),
			'type="number" min="0" max="100000" length="4"',
			false
		);

		$_ROSARIO['allow_edit'] = false;

		$fields[] = '<div class="center">' . Buttons( _( 'Submit' ), _( 'Cancel' ) ) . '</div>';

		$url_lang = '';

		if ( $locale === 'es_ES.utf8'
			|| $locale === 'fr_FR.utf8' )
		{
			$url_lang = substr( $locale, 0, 2 ) . '/';
		}

193
		$fields[] = sprintf(
194 195
			_( 'Poll answers are anonymous. Consult installation statistics <a href="%s" target="_blank">online</a>.' ),
			'https://www.rosariosis.org/' . $url_lang . 'installation-poll/'
196 197
		);

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		ob_start(); ?>
		<script src="assets/js/jquery.js"></script>
		<script>$('#first-login-form').hide();

		$('#first-login-poll-form input[type="reset"]').click(function(){
			console.log('ici');
			$('#first-login-poll-form').hide();
			$('#first-login-form').show();
		});

		$('#first-login-poll-form').submit(function(e){

			var form = $(this),
				url = form.attr('action');

			$('#first-login-poll-form input[type="submit"],#first-login-poll-form input[type="reset"]').attr('disabled', 'disabled');
			$.ajax({
				type: 'POST',
				url: url,
				data: form.serialize(),
				complete: function(jqxhr,status) {
					$('#first-login-poll-form').hide();
					$('#first-login-form').show();
				}
			});

			e.preventDefault();
		});</script>
		<?php
		$js = ob_get_clean();

		$form = '<form action="https://www.rosariosis.org/installation-poll/poll-submit.php" method="POST" id="first-login-poll-form">';

		$title = '<legend>' . _( 'Installation Poll' ) . '</legend>';

		return $form . '<fieldset>' . $title . implode( '</p><p>', $fields ) . '</fieldset></form>' . $js;
234 235
	}
}