3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2007-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2007-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2007-10-14
38 namespace Galette\Core
;
41 use Galette\Entity\Adherent
;
42 use Galette\Entity\Status
;
44 use Galette\IO\PdfMembersCards
;
45 use Galette\Repository\Members
;
48 * Preferences for galette
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2007-2014 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.7dev - 2007-10-14
65 const TABLE
= 'preferences';
66 const PK
= 'nom_pref';
68 /** Postal address will be the one given in the preferences */
69 const POSTAL_ADDRESS_FROM_PREFS
= 0;
70 /** Postal address will be the one of the selected staff member */
71 const POSTAL_ADDRESS_FROM_STAFF
= 1;
73 /** Public pages stuff */
74 /** Public pages are publically visibles */
75 const PUBLIC_PAGES_VISIBILITY_PUBLIC
= 0;
76 /** Public pages are visibles for up to date members only */
77 const PUBLIC_PAGES_VISIBILITY_RESTRICTED
= 1;
78 /** Public pages are visibles for admin and staff members only */
79 const PUBLIC_PAGES_VISIBILITY_PRIVATE
= 2;
81 const LOG_DISABLED
= 0;
82 const LOG_ENABLED
= 1;
84 private static $fields = array(
89 private static $defaults = array(
90 'pref_admin_login' => 'admin',
91 'pref_admin_pass' => 'admin',
92 'pref_nom' => 'Galette',
94 'pref_adresse' => '-',
95 'pref_adresse2' => '',
99 'pref_postal_adress' => self
::POSTAL_ADDRESS_FROM_PREFS
,
100 'pref_postal_staff_member' => '',
101 'pref_lang' => I18n
::DEFAULT_LANG
,
102 'pref_numrows' => 30,
103 'pref_log' => self
::LOG_ENABLED
,
104 'pref_statut' => Status
::DEFAULT_STATUS
,
105 /* Preferences for mails */
106 'pref_email_nom' => 'Galette',
107 'pref_email' => 'mail@domain.com',
108 'pref_email_newadh' => 'mail@domain.com',
109 'pref_bool_mailadh' => false,
110 'pref_editor_enabled' => false,
111 'pref_mail_method' => GaletteMail
::METHOD_DISABLED
,
112 'pref_mail_smtp' => '',
113 'pref_mail_smtp_host' => '',
114 'pref_mail_smtp_auth' => false,
115 'pref_mail_smtp_secure' => false,
116 'pref_mail_smtp_port' => '',
117 'pref_mail_smtp_user' => '',
118 'pref_mail_smtp_password' => '',
119 'pref_membership_ext' => 12,
120 'pref_beg_membership' => '',
121 'pref_email_reply_to' => '',
122 'pref_website' => '',
123 /* Preferences for labels */
124 'pref_etiq_marges_v' => 10,
125 'pref_etiq_marges_h' => 10,
126 'pref_etiq_hspace' => 10,
127 'pref_etiq_vspace' => 5,
128 'pref_etiq_hsize' => 90,
129 'pref_etiq_vsize' => 35,
130 'pref_etiq_cols' => 2,
131 'pref_etiq_rows' => 7,
132 'pref_etiq_corps' => 12,
133 /* Preferences for members cards */
134 'pref_card_abrev' => 'GALETTE',
135 'pref_card_strip' => 'Gestion d\'Adherents en Ligne Extrêmement Tarabiscotée',
136 'pref_card_tcol' => 'FFFFFF',
137 'pref_card_scol' => '8C2453',
138 'pref_card_bcol' => '53248C',
139 'pref_card_hcol' => '248C53',
140 'pref_bool_display_title' => false,
141 'pref_card_address' => 1,
142 'pref_card_year' => '',
143 'pref_card_marges_v' => 15,
144 'pref_card_marges_h' => 20,
145 'pref_card_vspace' => 5,
146 'pref_card_hspace' => 10,
147 'pref_card_self' => 1,
148 'pref_theme' => 'default',
149 'pref_bool_publicpages' => true,
150 'pref_publicpages_visibility' => self
::PUBLIC_PAGES_VISIBILITY_RESTRICTED
,
151 'pref_bool_selfsubscribe' => true,
152 'pref_googleplus' => '',
153 'pref_facebook' => '',
154 'pref_twitter' => '',
156 'pref_linkedin' => '',
157 'pref_mail_sign' => "{NAME}\r\n\r\n{WEBSITE}\r\n{GOOGLEPLUS}\r\n{FACEBOOK}\r\n{TWITTER}\r\n{LINKEDIN}\r\n{VIADEO}",
158 /* New contribution script */
159 'pref_new_contrib_script' => '',
160 'pref_bool_wrap_mails' => true,
161 'pref_rss_url' => 'http://galette.eu/dc/index.php/feed/atom',
162 'pref_show_id' => false,
163 'pref_adhesion_form' => '\Galette\IO\PdfAdhesionForm',
164 'pref_mail_allow_unsecure' => false,
165 'pref_instance_uuid' => '',
166 'pref_registration_uuid' => '',
167 'pref_telemetry_date' => '',
168 'pref_registration_date' => '',
170 'pref_filter_account' => Members
::ALL_ACCOUNTS
,
171 'pref_galette_url' => '',
172 'pref_redirect_on_create' => Adherent
::AFTER_ADD_DEFAULT
175 // flagging required fields
176 private $required = array(
181 'pref_etiq_marges_v',
182 'pref_etiq_marges_h',
192 'pref_card_marges_v',
193 'pref_card_marges_h',
199 * Default constructor
201 * @param Db $zdb Db instance
202 * @param boolean $load Automatically load preferences on load
206 public function __construct(Db
$zdb, $load = true)
211 $this->checkUpdate();
216 * Check if all fields referenced in the default array does exists,
221 private function checkUpdate()
225 foreach (self
::$defaults as $k => $v) {
226 if (!isset($this->prefs
[$k])) {
227 $this->prefs
[$k] = $v;
229 'The field `' . $k . '` does not exists, Galette will attempt to create it.',
239 if ($proceed !== false) {
241 $insert = $this->zdb
->insert(self
::TABLE
);
244 'nom_pref' => ':nom_pref',
245 'val_pref' => ':val_pref'
248 $stmt = $this->zdb
->sql
->prepareStatementForSqlObject($insert);
250 foreach ($params as $p) {
253 'nom_pref' => $p['nom_pref'],
254 'val_pref' => $p['val_pref']
258 } catch (\Exception
$e) {
260 'Unable to add missing preferences.' . $e->getMessage(),
267 'Missing preferences were successfully stored into database.',
274 * Load current preferences from database.
278 public function load()
280 $this->prefs
= array();
283 $result = $this->zdb
->selectAll(self
::TABLE
);
284 foreach ($result as $pref) {
285 $this->prefs
[$pref->nom_pref
] = $pref->val_pref
;
288 } catch (\Exception
$e) {
290 'Preferences cannot be loaded. Galette should not work without ' .
291 'preferences. Exiting.',
299 * Set default preferences at install time
301 * @param staing $lang language selected at install screen
302 * @param string $adm_login admin login entered at install time
303 * @param string $adm_pass admin password entered at install time
305 * @return boolean|Exception
307 public function installInit($lang, $adm_login, $adm_pass)
310 //first, we drop all values
311 $delete = $this->zdb
->delete(self
::TABLE
);
312 $this->zdb
->execute($delete);
314 //we then replace default values with the ones user has selected
315 $values = self
::$defaults;
316 $values['pref_lang'] = $lang;
317 $values['pref_admin_login'] = $adm_login;
318 $values['pref_admin_pass'] = $adm_pass;
319 $values['pref_card_year'] = date('Y');
321 $insert = $this->zdb
->insert(self
::TABLE
);
324 'nom_pref' => ':nom_pref',
325 'val_pref' => ':val_pref'
328 $stmt = $this->zdb
->sql
->prepareStatementForSqlObject($insert);
330 foreach ($values as $k => $v) {
340 'Default preferences were successfully stored into database.',
344 } catch (\Exception
$e) {
346 'Unable to initialize default preferences.' . $e->getMessage(),
354 * Returns all preferences keys
358 public function getFieldsNames()
360 return array_keys($this->prefs
);
366 * @param array $values Values
367 * @param Login $login Logged in user
371 public function check(array $values, Login
$login)
373 $insert_values = array();
374 if ($login->isSuperAdmin() && GALETTE_MODE
!== 'DEMO') {
375 $this->required
[] = 'pref_admin_login';
379 foreach ($this->getFieldsNames() as $fieldname) {
380 if (isset($values[$fieldname])) {
381 $value = trim($values[$fieldname]);
386 // now, check validity
388 switch ($fieldname) {
389 case 'pref_admin_login':
390 if (GALETTE_MODE
=== 'DEMO') {
392 'Trying to set superadmin login while in DEMO.',
396 if (strlen($value) < 4) {
397 $this->errors
[] = _T("- The username must be composed of at least 4 characters!");
399 //check if login is already taken
400 if ($login->loginExists($value)) {
401 $this->errors
[] = _T("- This username is already used by another member !");
407 if (!is_numeric($value) ||
$value < 0) {
408 $this->errors
[] = _T("- The numbers and measures have to be integers!");
411 case 'pref_etiq_marges_h':
412 case 'pref_etiq_marges_v':
413 case 'pref_etiq_hspace':
414 case 'pref_etiq_vspace':
415 case 'pref_etiq_hsize':
416 case 'pref_etiq_vsize':
417 case 'pref_etiq_cols':
418 case 'pref_etiq_rows':
419 case 'pref_etiq_corps':
420 case 'pref_card_marges_v':
421 case 'pref_card_marges_h':
422 case 'pref_card_hspace':
423 case 'pref_card_vspace':
424 // prevent division by zero
425 if ($fieldname=='pref_numrows' && $value=='0') {
428 if (!is_numeric($value) ||
$value < 0) {
429 $this->errors
[] = _T("- The numbers and measures have to be integers!");
432 case 'pref_card_tcol':
433 // Set strip text color to white
434 if (! preg_match("/#([0-9A-F]{6})/i", $value)) {
438 case 'pref_card_scol':
439 case 'pref_card_bcol':
440 case 'pref_card_hcol':
441 // Set strip background colors to black
442 if (!preg_match("/#([0-9A-F]{6})/i", $value)) {
446 case 'pref_admin_pass':
447 if (GALETTE_MODE
== 'DEMO') {
449 'Trying to set superadmin pass while in DEMO.',
453 if (strlen($value) < 4) {
454 $this->errors
[] = _T("- The password must be of at least 4 characters!");
458 case 'pref_membership_ext':
459 if (!is_numeric($value) ||
$value < 0) {
460 $this->errors
[] = _T("- Invalid number of months of membership extension.");
463 case 'pref_beg_membership':
464 $beg_membership = explode("/", $value);
465 if (count($beg_membership) != 2) {
466 $this->errors
[] = _T("- Invalid format of beginning of membership.");
469 if (!checkdate($beg_membership[1], $beg_membership[0], $now['year'])) {
470 $this->errors
[] = _T("- Invalid date for beginning of membership.");
474 case 'pref_card_year':
475 if (!preg_match('/^(?:\d{4}|\d{2})(\D?)(?:\d{4}|\d{2})$/', $value)) {
476 $this->errors
[] = _T("- Invalid year for cards.");
482 $insert_values[$fieldname] = $value;
486 if (GALETTE_MODE
!== 'DEMO'
487 && isset($insert_values['pref_mail_method'])
489 if ($insert_values['pref_mail_method'] > GaletteMail
::METHOD_DISABLED
) {
490 if (!isset($insert_values['pref_email_nom'])
491 ||
$insert_values['pref_email_nom'] == ''
493 $this->errors
[] = _T("- You must indicate a sender name for emails!");
495 if (!isset($insert_values['pref_email'])
496 ||
$insert_values['pref_email'] == ''
498 $this->errors
[] = _T("- You must indicate an email address Galette should use to send emails!");
500 if ($insert_values['pref_mail_method'] == GaletteMail
::METHOD_SMTP
) {
501 if (!isset($insert_values['pref_mail_smtp_host'])
502 ||
$insert_values['pref_mail_smtp_host'] == ''
504 $this->errors
[] = _T("- You must indicate the SMTP server you want to use!");
507 if ($insert_values['pref_mail_method'] == GaletteMail
::METHOD_GMAIL
508 ||
($insert_values['pref_mail_method'] == GaletteMail
::METHOD_SMTP
509 && $insert_values['pref_mail_smtp_auth'])
511 if (!isset($insert_values['pref_mail_smtp_user'])
512 ||
trim($insert_values['pref_mail_smtp_user']) == ''
514 $this->errors
[] = _T("- You must provide a login for SMTP authentication.");
516 if (!isset($insert_values['pref_mail_smtp_password'])
517 ||
($insert_values['pref_mail_smtp_password']) == ''
519 $this->errors
[] = _T("- You must provide a password for SMTP authentication.");
525 if (isset($insert_values['pref_beg_membership'])
526 && $insert_values['pref_beg_membership'] != ''
527 && isset($insert_values['pref_membership_ext'])
528 && $insert_values['pref_membership_ext'] != ''
530 $this->errors
[] =_T("- Default membership extention and beginning of membership are mutually exclusive.");
533 // missing required fields?
534 foreach ($this->required
as $val) {
535 if (!isset($values[$val]) ||
isset($values[$val]) && trim($values[$val]) == '') {
536 $this->errors
[] = str_replace(
539 _T("- Mandatory field %field empty.")
544 if (GALETTE_MODE
!== 'DEMO') {
545 // Check passwords. Hash will be done into the Preferences class
546 if (strcmp($insert_values['pref_admin_pass'], $values['pref_admin_pass_check']) != 0) {
547 $this->errors
[] = _T("Passwords mismatch");
552 if (isset($insert_values['pref_postal_adress'])) {
553 $value = $insert_values['pref_postal_adress'];
554 if ($value == Preferences
::POSTAL_ADDRESS_FROM_PREFS
) {
555 if (isset($insert_values['pref_postal_staff_member'])) {
556 unset($insert_values['pref_postal_staff_member']);
558 } elseif ($value == Preferences
::POSTAL_ADDRESS_FROM_STAFF
) {
559 if (!isset($value) ||
$value < 1) {
560 $this->errors
[] = _T("You have to select a staff member");
565 // update preferences
566 foreach ($insert_values as $champ => $valeur) {
567 if ($login->isSuperAdmin()
568 ||
(!$login->isSuperAdmin()
569 && ($champ != 'pref_admin_pass' && $champ != 'pref_admin_login'))
571 if (($champ == "pref_admin_pass" && $_POST['pref_admin_pass'] != '')
572 ||
($champ != "pref_admin_pass")
574 $this->$champ = $valeur;
579 return 0 === count($this->errors
);
583 * Will store all preferences in the database
587 public function store()
590 $this->zdb
->connection
->beginTransaction();
591 $update = $this->zdb
->update(self
::TABLE
);
594 'val_pref' => ':val_pref'
596 )->where
->equalTo('nom_pref', ':nom_pref');
598 $stmt = $this->zdb
->sql
->prepareStatementForSqlObject($update);
600 foreach (self
::$defaults as $k => $v) {
601 Analog
::log('Storing ' . $k, Analog
::DEBUG
);
603 $value = $this->prefs
[$k];
604 //do not store pdf_adhesion_form, it's designed to be overriden by plugin
605 if ($k === 'pref_adhesion_form') {
606 if (trim($v) == '') {
607 //Reset to default, should not be empty
608 $v = self
::$defaults['pref_adhesion_form'];
615 'val_pref' => $value,
620 $this->zdb
->connection
->commit();
622 'Preferences were successfully stored into database.',
626 } catch (\Exception
$e) {
627 $this->zdb
->connection
->rollBack();
631 $messages[] = $e->getMessage();
632 } while ($e = $e->getPrevious());
635 'Unable to store preferences | ' . print_r($messages, true),
643 * Returns postal address
645 * @return string postal address
647 public function getPostalAddress()
658 $replacements = null;
660 if ($this->prefs
['pref_postal_adress'] == self
::POSTAL_ADDRESS_FROM_PREFS
) {
661 $_address = $this->prefs
['pref_adresse'];
662 if ($this->prefs
['pref_adresse2'] && $this->prefs
['pref_adresse2'] != '') {
663 $_address .= "\n" . $this->prefs
['pref_adresse2'];
665 $replacements = array(
669 $this->prefs
['pref_cp'],
670 $this->prefs
['pref_ville'],
671 $this->prefs
['pref_pays']
674 //get selected staff member address
675 $adh = new Adherent($this->zdb
, (int)$this->prefs
['pref_postal_staff_member']);
676 $_complement = preg_replace(
677 array('/%name/', '/%status/'),
678 array($this->prefs
['pref_nom'], $adh->sstatus
),
679 _T("%name association's %status")
681 $_address = $adh->address
;
682 if ($adh->address_continuation
&& $adh->address_continuation
!= '') {
683 $_address .= "\n" . $adh->address_continuation
;
685 $replacements = array(
686 $adh->sfullname
. "\n",
695 /*FIXME: i18n fails :/ */
699 _T("%name\n%complement\n%address\n%zip %town - %country")
704 "%name%complement%address\n%zip %town - %country"
710 * Are public pages visibles?
712 * @param Authentication $login Authenticaqtion instance
716 public function showPublicPages(Authentication
$login)
718 if ($this->prefs
['pref_bool_publicpages']) {
719 //if public pages are actives, let's check if we
720 //display them for curent call
721 switch ($this->prefs
['pref_publicpages_visibility']) {
722 case self
::PUBLIC_PAGES_VISIBILITY_PUBLIC
:
723 //pages are publically visibles
726 case self
::PUBLIC_PAGES_VISIBILITY_RESTRICTED
:
727 //pages should be displayed only for up to date members
728 if ($login->isUp2Date()
737 case self
::PUBLIC_PAGES_VISIBILITY_PRIVATE
:
738 //pages should be displayed only for staff and admins
739 if ($login->isAdmin() ||
$login->isStaff()) {
746 //should never be there
756 * Global getter method
758 * @param string $name name of the property we want to retrive
760 * @return false|object the called property
762 public function __get($name)
764 $forbidden = array('logged', 'admin', 'active', 'defaults');
765 $virtuals = array('vpref_email_newadh');
767 if (!in_array($name, $forbidden) && isset($this->prefs
[$name])) {
768 if (GALETTE_MODE
=== 'DEMO'
769 && $name == 'pref_mail_method'
771 return GaletteMail
::METHOD_DISABLED
;
773 if ($name == 'pref_adhesion_form' && $this->prefs
[$name] == '') {
774 $this->prefs
[$name] = self
::$defaults['pref_adhesion_form'];
776 $value = $this->prefs
[$name];
777 if (TYPE_DB
=== \Galette\Core\Db
::PGSQL
) {
778 if ($value === 'f') {
783 if (in_array($name, ['pref_email_newadh'])) {
784 $values = explode(',', $value);
785 $value = $values[0]; //take first as default
790 } elseif (in_array($name, $virtuals)) {
791 $virtual = str_replace('vpref_', 'pref_', $name);
792 return explode(',', $this->prefs
[$virtual]);
795 'Preference `' . $name . '` is not set or is forbidden',
803 * Get default preferences
807 public function getDefaults()
809 return self
::$defaults;
813 * Global setter method
815 * @param string $name name of the property we want to assign a value to
816 * @param object $value a relevant value for the property
820 public function __set($name, $value)
822 //does this pref exists ?
823 if (!array_key_exists($name, self
::$defaults)) {
825 'Trying to set a preference value which does not seem to exist ('
832 if ($name == 'pref_email'
833 ||
$name == 'pref_email_newadh'
834 ||
$name == 'pref_email_reply_to'
836 if (GALETTE_MODE
=== 'DEMO') {
838 'Trying to set pref_email while in DEMO.',
844 //check emails validity
845 //may be a comma separated list of valid mails identifiers:
846 //"The Name <mail@domain.com>,The Other <other@mail.com>" expect for reply_to.
848 if (trim($value) != '') {
849 if ($name == 'pref_email_newadh') {
850 $addresses = explode(',', $value);
852 $addresses = [$value];
855 foreach ($addresses as $address) {
856 if (!GaletteMail
::isValidEmail($address)) {
857 $msg = str_replace('%s', $address, _T("Invalid E-Mail address: %s"));
858 Analog
::log($msg, Analog
::WARNING
);
859 $this->errors
[] = $msg;
864 //some values need to be changed (eg. passwords)
865 if ($name == 'pref_admin_pass') {
866 $value = password_hash($value, PASSWORD_BCRYPT
);
869 //okay, let's update value
870 $this->prefs
[$name] = $value;
874 * Get instance URL from configuration (if set) or guessed if not
878 public function getURL()
881 if (isset($this->prefs
['pref_galette_url']) && !empty($this->prefs
['pref_galette_url'])) {
882 $url = $this->prefs
['pref_galette_url'];
884 $url = $this->getDefaultURL();
890 * Get default URL (when not setted by user in preferences)
894 public function getDefaultURL()
896 $scheme = (isset($_SERVER['HTTPS']) ?
'https' : 'http');
897 $uri = $scheme . '://' . $_SERVER['HTTP_HOST'];
902 * Check member cards sizes
903 * Always a A4/portrait
907 public function checkCardsSizes()
909 $warning_detected = [];
913 $size = $this->pref_card_marges_h
* 2;
915 $size +
= PdfMembersCards
::getWidth() * PdfMembersCards
::getCols();
917 $size +
= $this->pref_card_hspace
* (PdfMembersCards
::getCols() - 1);
919 $warning_detected[] = _T('Current cards configuration may exceed page width!');
924 $size = $this->pref_card_marges_v
* 2;
926 $size +
= PdfMembersCards
::getHeight() * PdfMembersCards
::getRows();
928 $size +
= $this->pref_card_vspace
* (PdfMembersCards
::getRows() - 1);
930 $warning_detected[] = _T('Current cards configuration may exceed page height!');
933 return $warning_detected;
941 public function getErrors()
943 return $this->errors
;