-
Notifications
You must be signed in to change notification settings - Fork 66
/
m_admin.php
1575 lines (1412 loc) · 55.6 KB
/
m_admin.php
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
193
194
195
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
----------------------------------------------------------------------
LICENSE
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License (GPL)
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program 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 General Public License for more details.
To read the license please visit http://www.gnu.org/copyleft/gpl.html
----------------------------------------------------------------------
*/
/**
* Manage the AlternC's account administration (create/edit/delete)
*
* @copyright AlternC-Team 2000-2017 https://alternc.com/
*/
class m_admin {
/**
* $enabled tells if the logged user is super-admin or not
*/
var $enabled = 0;
/** List of the controls made for each TLD
*
* $tldmode is used by the administration panel, while choosing
* the authorized TLDs. It's an array of strings explaining the current state of the TLD.
*/
public $tldmode = array();
var $archive = '';
/**
* Constructor
*
* @global type $db
* @global type $cuid
*/
function __construct() {
global $db, $cuid;
$db->query("SELECT su FROM membres WHERE uid=?;", array($cuid));
$db->next_record();
$this->enabled = $db->f("su");
$this->tldmode = array(
0 => _("This TLD is forbidden"),
1 => _("primary DNS is checked in WHOIS db"),
2 => _("primary & secondary DNS are checked in WHOIS db"),
3 => _("Domain must exist, but don't do any DNS check"),
4 => _("Domain can be installed, no check at all"),
5 => _("Domain can be installed, force NO DNS hosting"),
);
$this->archive = variable_get('archive_del_data', '', 'If folder specified html folder of deleted user is archived, else it is deleted. ');
}
/**
* Hook function called by the menu class to add menu to the left panel.
* @global type $mem
* @global type $cuid
* @global type $debug_alternc
* @global type $L_INOTIFY_UPDATE_DOMAIN
* @return boolean|string
*/
function hook_menu() {
global $mem, $cuid, $debug_alternc, $L_INOTIFY_UPDATE_DOMAIN;
if (!$mem->checkRight()) {
return false;
}
$obj = array(
'title' => _("Administration"),
'link' => 'toggle',
'class' => 'adminmenu',
'pos' => 10,
'links' =>
array(
array(
'txt' => _("Manage AlternC accounts"),
'url' => 'adm_list.php',
'class' => 'adminmenu'
),
array(
'txt' => _("User Quotas"),
'url' => 'quotas_users.php?mode=4',
'class' => 'adminmenu'
),
)
);
if ($cuid == 2000) { // only ADMIN, not available to subadmins
$obj['links'][] = array(
'txt' => _("Admin Control Panel"),
'url' => 'adm_panel.php',
'class' => 'adminmenu'
);
$obj['links'][] = array(
'txt' => _("PhpMyAdmin"),
'url' => 'sql_pma_sso.php',
'class' => 'adminmenu',
'target' => '_blank',
);
$obj['links'][] = array(
'txt' => ($debug_alternc->status) ? _("Switch debug Off") : _("Switch debug On"),
'url' => "alternc_debugme.php?enable=" . ($debug_alternc->status ? "0" : "1"),
'class' => 'adminmenu'
);
if (empty($L_INOTIFY_UPDATE_DOMAIN) || file_exists("$L_INOTIFY_UPDATE_DOMAIN")) {
$obj['links'][] = array(
'txt' => _("Applying..."),
'url' => 'javascript:alert(\'' . _("Domain changes are already applying") . '\');',
'class' => 'adminmenu',
);
} else {
$obj['links'][] = array(
'txt' => _("Apply changes"),
'url' => 'adm_update_domains.php',
'class' => 'adminmenu',
'onclick' => 'return confirm("' . addslashes(_("Server configuration changes are applied every 5 minutes. Do you want to do it right now?")) . '");',
);
} // L_INOTIFY_UPDATE_DOMAIN
} // cuid == 2000
return $obj;
}
/**
* Password kind used in this class (hook for admin class)
*
* @return array
*/
function alternc_password_policy() {
return array("adm" => "Administration section");
}
/**
*
*/
function stop_if_jobs_locked() {
if (file_exists(ALTERNC_LOCK_JOBS)) {
echo "There is a file " . ALTERNC_LOCK_JOBS . "\n";
echo "So no jobs are allowed\n";
echo "Did you launch alternc.install ?\n";
die();
}
}
/**
* return the uid of an alternc account
*
* @global type $db
* @param type $login
* @return null
*/
function get_uid_by_login($login) {
global $db;
$db->query("SELECT uid FROM membres WHERE login= ?;", array($login));
if (!$db->next_record()) {
return null;
}
return $db->f('uid');
}
/**
* return the name of an alternc account
*
* @global type $db
* @param type $uid
* @return null if missing
*/
function get_login_by_uid($uid) {
global $db;
$db->query("SELECT login FROM membres WHERE uid= ?;", array($uid));
if (!$db->next_record()) {
return null;
}
return $db->f('login');
}
/**
* Returns the known information about a hosted account
*
* Returns all what we know about an account (contents of the tables
* <code>membres</code> et <code>local</code>)
* Ckecks if the account is super-admin
*
* @global type $msg
* @global type $db
* @global string $lst_users_properties
* @param int $uid a unique integer identifying the account
* @param boolean $recheck
* @return array|boolean an associative array containing all the fields of the
* table <code>membres</code> and <code>local</code> of the corresponding account.
* Returns FALSE if an error occurs.
*/
function get($uid, $recheck = false) {
global $msg, $db, $lst_users_properties;
$msg->debug("admin","get",$uid);
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
if (!isset($lst_users_properties) || empty($lst_users_properties) || !is_array($lst_users_properties) || $recheck) {
$lst_users_properties = array();
$db->query("
SELECT
m.uid as muid,
l.*,
m.*,
parent.login as parentlogin,
dbs.name as db_server_name,
m.renewed + INTERVAL m.duration MONTH as expiry,
CASE
WHEN m.duration IS NULL THEN 0
WHEN m.renewed + INTERVAL m.duration MONTH <= NOW() THEN 3
WHEN m.renewed <= NOW() THEN 2
ELSE 1 END 'status'
FROM membres as m
LEFT JOIN membres as parent ON (parent.uid = m.creator)
LEFT JOIN db_servers as dbs ON (m.db_server_id = dbs.id)
LEFT JOIN local as l ON (m.uid = l.uid) ;");
while ($db->next_record()) {
$lst_users_properties[$db->f('muid')] = $db->Record;
}
}
if (!isset($lst_users_properties[$uid])) {
if (!$recheck) {
// don't exist, but is not a forced check. Do a forced check
return $this->get($uid, true);
}
$msg->raise("ERROR", "admin", _("Account not found"));
return false;
}
return $lst_users_properties[$uid];
}
/**
* Returns the known information about a specific hosted account
*
* Similar to get_list() but for creators/resellers.
*
* @global type $msg
* @global type $db
* @param int $uid
* @return boolean
*/
function get_creator($uid) {
global $msg, $db;
$msg->debug("admin","get_creator",$uid);
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
$db->query("SELECT m.*, parent.login as parentlogin FROM membres as m LEFT JOIN membres as parent ON (parent.uid = m.creator) WHERE m.uid= ?;", array($uid));
if ($db->num_rows()) {
$db->next_record();
$c = $db->Record;
} else {
$msg->raise("ERROR", "admin", _("Account not found"));
return false;
}
$db->query("SELECT * FROM local WHERE uid= ?;", array($uid));
if ($db->num_rows()) {
$db->next_record();
reset($db->Record);
while (list($key, $val) = each($db->Record)) {
$c[$key] = $val;
}
}
$db->query("SELECT count(*) as nbcreated FROM membres WHERE creator= ?;", array($uid));
if ($db->num_rows()) {
$db->next_record();
reset($db->Record);
while (list($key, $val) = each($db->Record)) {
$c[$key] = $val;
}
}
return $c;
}
/**
*
* @global type $db
* @return boolean TRUE if there is only one admin account
* (allow the program to prevent the destruction of the last admin account)
*/
function onesu() {
global $db;
$db->query("SELECT COUNT(*) AS cnt FROM membres WHERE su=1");
$db->next_record();
return ($db->f("cnt") == 1);
}
/**
* @TODO :EM: those request should have been escaped
* Returns the list of the hosted accounts
*
* Returns all what we know about ALL the accounts (contents of the tables
* <code>membres</code> et <code>local</code>)
* Check for super-admin accounts
* @param
* @return
*
* @global type $msg
* @global type $mem
* @global type $cuid
* @param integer $all
* @param integer $creator
* @param string $pattern
* @param string $pattern_type
* @return boolean | array an associative array containing all the fields of the
* table <code>membres</code> and <code>local</code> of all the accounts.
* Returns FALSE if an error occurs.
*/
function get_list($all = 0, $creator = 0, $pattern = FALSE, $pattern_type = FALSE) {
global $msg, $mem, $cuid;
$msg->debug("admin", "get_list");
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
$db = new DB_System();
if ($pattern) {
if ($pattern_type === 'domaine') {
$request = 'SELECT compte AS uid FROM domaines WHERE 1';
if ($pattern && preg_match('/[.a-zA-Z0-9]+/', $pattern)) {
$request .= sprintf(' AND domaine LIKE "%%%s%%"', $pattern);
}
if ($creator) {
$request .= sprintf(' AND compte in (select uid from membres where creator = "%s" ) ', $creator);
}
if ($mem->user['uid'] != 2000 && !$all) {
$request .= sprintf(' AND compte in (select uid from membres where creator = "%s") ', $cuid);
}
$request .= ' GROUP BY uid';
} elseif ($pattern_type === 'login') {
$request = 'SELECT uid FROM membres WHERE 1';
if ($pattern && preg_match('/[a-zA-Z0-9]+/', $pattern)) {
$request .= sprintf(' AND login LIKE "%%%s%%"', $pattern);
}
if ($creator) {
$request .= sprintf(' AND creator = "%s"', $creator);
}
if ($mem->user['uid'] != 2000 && !$all) {
$request .= sprintf(' AND creator = "%s"', $cuid);
}
$request .= ' ORDER BY login;';
} else {
$msg->raise("ERROR", "admin", _("Invalid pattern type provided. Are you even performing a legitimate action?"));
return FALSE;
}
} else {
if ($creator) {
// Limit listing to a specific reseller
$request = "SELECT uid FROM membres WHERE creator='" . $creator . "' ORDER BY login;";
} elseif ($mem->user['uid'] == 2000 || $all) {
$request = "SELECT uid FROM membres ORDER BY login;";
} else {
$request = "SELECT uid FROM membres WHERE creator='" . $cuid . "' ORDER BY login;";
}
}
$db->query($request);
if ($db->num_rows()) {
$c = array();
while ($db->next_record()) {
$c[$db->f("uid")] = $this->get($db->f("uid"));
}
return $c;
} else {
return false;
}
}
/**
* Send an email to all AlternC's accounts
*
* @global type $msg
* @global type $mem
* @global type $cuid
* @global type $db
* @param string $subject Subject of the email to send
* @param string $message Message to send
* @param string $from Expeditor of that email
* @return boolean
*/
function mailallmembers($subject, $message, $from) {
global $msg, $db;
$msg->log("admin", "mailallmembers");
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
$subject = trim($subject);
$message = trim($message);
$from = trim($from);
if (empty($subject) || empty($message) || empty($from)) {
$msg->raise("ERROR", "admin", _("Subject, message and sender are mandatory"));
return false;
}
//@todo remove cf functions.php
if (checkmail($from) != 0) {
$msg->raise("ERROR", "admin", _("Sender is syntaxically incorrect"));
return false;
}
@set_time_limit(1200);
$db->query("SELECT DISTINCT mail FROM membres WHERE mail!='';");
while ($db->next_record()) {
// Can't do BCC due to postfix limitation
// FIXME: use phpmailer, far better for mass-mailing than sendmail (reply-to issue among others)
mail($db->f('mail'), $subject, $message, null, "-f$from");
}
return true;
}
/**
* Returns an array with the known information about resellers (uid, login, number of accounts)
* Does not include account 2000 in the list.
* May only be called by the admin account (2000)
* If there are no reseller accounts, returns an empty array.
*
* @global type $msg
* @global type $mem
* @global type $cuid
* @return boolean
*/
function get_creator_list() {
global $msg, $cuid;
$creators = array();
$msg->debug("admin", "get_creator_list");
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
$db = new DB_System();
$db->query("SELECT DISTINCT creator FROM membres WHERE creator <> 0 ORDER BY creator ASC;");
if ($db->num_rows()) {
while ($db->next_record()) {
$creators[] = $this->get_creator($db->f("creator"));
}
}
$creators2 = array();
foreach ($creators as $cc) {
$creators2[$cc['uid']] = $cc;
}
return $creators2;
}
/**
* Check if I am the creator of the member $uid
*
* @global type $msg
* @global type $mem
* @global type $db
* @global type $cuid
* @param int $uid a unique integer identifying the account
* @return boolean TRUE if I am the creator of that account. FALSE else.
*/
function checkcreator($uid) {
global $msg, $db, $cuid;
if ($cuid == 2000) {
return true;
}
$db->query("SELECT creator FROM membres WHERE uid= ?;", array($uid));
$db->next_record();
if ($db->Record["creator"] != $cuid) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
return true;
}
/**
* When the admin want to delegate a subdomain to an account
*
* @global m_mysql $db
* @global m_messages $msg
* @global m_dom $dom
* @global m_mem $mem
* @global int $cuid
* @param string $u
* @param string $domain_name
* @return boolean
*/
function add_shared_domain($u, $domain_name) {
global $msg, $dom, $mem;
$msg->log("admin", "add_shared_domain", $u . "/" . $domain_name);
if (!$mem->checkright()) {
$msg->raise("ERROR", "admin", _("-- Only administrators can do that! --"));
return false;
}
// Check if this domain exist on this admin account
if ((!in_array($domain_name, $dom->enum_domains()))&&($domain_name!=variable_get("hosting_tld"))) {
$msg->raise("ERROR", "admin", _("You don't seem to be allowed to delegate this domain"));
$msg->log("admin", "add_shared_domain", "domain not allowed");
return false;
}
// Clean the domain_name
$domain_name = preg_replace("/^\.\.*/", "", $domain_name);
$mem->su($u);
$dom->lock();
// option : 1=hébergement dns + courriels, 1=noerase, empeche de modifier, 1=force
// we do not allow DNS modification for hosting_tld
if (variable_get("free_domain_enable_dns")) {
$dns = 1;
}
else {
$dns=($domain_name==variable_get("hosting_tld")) ? 0 : 1;
}
$dom->add_domain($mem->user['login'] . "." . $domain_name, $dns, 1, 1);
$dom->unlock();
$mem->unsu();
return true;
}
/** Creates a new hosted account
*
* Creates a new hosted account (in the tables <code>membres</code>
* and <code>local</code>). Prevents any manipulation of the account if
* the account $mid is not super-admin.
*
*
* @global m_messages $msg
* @global m_quota $quota
* @global array $classes
* @global int $cuid
* @global m_mem $mem
* @global string $L_MYSQL_DATABASE
* @global string $L_MYSQL_LOGIN
* @global m_hooks $hooks
* @global m_action $action
* @param string $login Login name like [a-z][a-z0-9]*
* @param string $pass Password (max. 64 characters)
* @param string $nom Name of the account owner
* @param string $prenom First name of the account owner
* @param string $mail Email address of the account owner, useful to get
* one's lost password
* @param integer $canpass
* @param string $type Account type for quotas
* @param int $duration
* @param string $notes
* @param integer $force
* @param string $create_dom
* @param int $db_server_id
* @return boolean Returns FALSE if an error occurs, TRUE if not.
*/
function add_mem($login, $pass, $nom, $prenom, $mail, $canpass = 1, $type = 'default', $duration = 0, $notes = "", $force = 0, $create_dom = '', $db_server_id) {
global $msg, $cuid, $mem, $L_MYSQL_DATABASE, $L_MYSQL_LOGIN, $hooks, $action;
$msg->log("admin", "add_mem", $login . "/" . $mail);
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
if (empty($db_server_id)) {
$msg->raise("ERROR", "admin", _("Missing db_server field"));
return false;
}
if (($login == "") || ($pass == "")) {
$msg->raise("ERROR", "admin", _("Please fill all mandatory fields"));
return false;
}
if (!$force) {
if ($mail == "") {
$msg->raise("ERROR", "admin", _("Please fill all mandatory fields"));
return false;
}
//@todo remove cf functions.php
if (checkmail($mail) != 0) {
$msg->raise("ERROR", "admin", _("Please enter a valid email address"));
return false;
}
}
$login = strtolower($login);
if (!preg_match("#^[a-z0-9]+$#", $login)) { //$
$msg->raise("ERROR", "admin", _("Login can only contains characters a-z and 0-9"));
return false;
}
if (strlen($login) > 14) {
// Not an arbitrary value : MySQL user names can be up to 16 characters long
// If we want to allow people to create a few mysql_user (and we want to!)
// we have to limit the login lenght
$msg->raise("ERROR", "admin", _("The login is too long (14 chars max)"));
return false;
}
// Some login are not allowed...
if ($login == $L_MYSQL_DATABASE || $login == $L_MYSQL_LOGIN || $login == "mysql" || $login == "root") {
$msg->raise("ERROR", "admin", _("Login can only contains characters a-z, 0-9 and -"));
return false;
}
// Additional checks before a user is created
// Do not create the account if any hook has a return value
// The returned value should provide additional information
$before_add_hook_data = $hooks->invoke('hook_before_alternc_add_member', [$login]);
foreach($before_add_hook_data as $create) {
if($create !== null) {
$msg->raise("ERROR", "admin", _("The account '%s' cannot be created. %s"), [$login, $create]);
return false;
}
}
$pass = password_hash($pass, PASSWORD_BCRYPT);
$db = new DB_System();
// Already exist?
$db->query("SELECT count(*) AS cnt FROM membres WHERE login= ?;", array($login));
$db->next_record();
if (!$db->f("cnt")) {
$db->query("SELECT max(m.uid)+1 as nextid FROM membres m");
if (!$db->next_record()) {
$uid = 2000;
} else {
$uid = $db->Record["nextid"];
if ($uid <= 2000) {
$uid = 2000;
}
}
$db->query("INSERT INTO membres (uid,login,pass,mail,creator,canpass,type,created,notes,db_server_id) VALUES (?, ?, ?, ?, ?, ?, ?, NOW(), ?, ?);", array($uid, $login, $pass, $mail, $cuid, $canpass, $type, $notes, $db_server_id));
$db->query("INSERT INTO local(uid,nom,prenom) VALUES(?, ?, ?);", array($uid, $nom, $prenom));
$this->renew_update($uid, $duration);
$action->create_dir(getuserpath("$login"));
$action->fix_user($uid);
// Triggering hooks
$mem->su($uid);
$hooks->invoke("alternc_add_member");
// New hook way
$hooks->invoke("hook_admin_add_member", array(), array('quota')); // First !!! The quota !!! Eitherway, we can't be sure to be able to create all
$hooks->invoke("hook_admin_add_member");
$mem->unsu();
if (!empty($create_dom)) {
$this->add_shared_domain($uid, $create_dom);
}
return $uid;
} else {
$msg->raise("ERROR", "admin", _("This login already exists"));
return false;
}
}
/**
* AlternC's standard function called when a user is created
* This sends an email if configured through the interface.
*
* @global m_messages $msg
* @global int $cuid
* @global string $L_FQDN
* @global string $L_HOSTING
* @return boolean
*/
function hook_admin_add_member() {
global $msg, $cuid, $L_FQDN, $L_HOSTING;
$dest = variable_get('new_email', '0', 'An email will be sent to this address when new accounts are created if set.', array('desc' => 'Enabled', 'type' => 'boolean'));
if (!$dest) {
return false;
}
$db = new DB_System();
if (!$db->query("SELECT m.*, parent.login as parentlogin FROM membres m LEFT JOIN membres parent ON parent.uid=m.creator WHERE m.uid= ?", array($cuid))) {
$msg->raise("ERROR", "admin", sprintf(_("query failed: %s "), $db->Error));
return false;
}
if ($db->next_record()) {
// TODO: put that string into gettext !
$mail = '
A new AlternC account was created on %fqdn by %creator.
Account details
---------------
login: %login (%uid)
email: %mail
createor: %creator (%cuid)
can change password: %canpass
type: %type
notes: %notes
';
$mail = strtr($mail, array('%fqdn' => $L_FQDN,
'%creator' => $db->Record['parentlogin'],
'%uid' => $db->Record['uid'],
'%login' => $db->Record['login'],
'%mail' => $db->Record['mail'],
'%cuid' => $db->Record['creator'],
'%canpass' => $db->Record['canpass'],
'%type' => $db->Record['type'],
'%notes' => $db->Record['notes']));
$subject = sprintf(_("New account %s from %s on %s"), $db->Record['login'], $db->Record['parentlogin'], $L_HOSTING);
if (mail($dest, $subject, $mail, "From: postmaster@$L_FQDN")) {
//sprintf(_("Email successfully sent to %s"), $dest);
return true;
} else {
$msg->raise("ERROR", "admin", sprintf(_("Cannot send email to %s"), $dest));
return false;
}
} else {
$msg->raise("ERROR", "admin", sprintf(_("Query failed: %s"), $db->Error));
return false;
}
}
/**
* Edit an account
*
* Change an account (in the tables <code>membres</code>
* and <code>local</code>). Prevents any manipulation of the account if
* the account $mid is not super-admin.
*
* @global m_messages $msg
* @global m_mysql $db
* @global int $cuid
* @global m_quota $quota
* @param int $uid The uid number of the account we want to modify
* @param string $mail New email address of the account owner
* @param string $nom New name of the account owner
* @param string $prenom New first name of the account owner
* @param string $pass New password (max. 64 characters)
* @param string $enabled (value: 0 or 1) activates or desactivates the
* @param boolean $canpass
* @param int $type New type of account
* @param int $duration
* @param string $notes
* @param boolean $reset_quotas
* @return boolean Returns FALSE if an error occurs, TRUE if not
*/
function update_mem($uid, $mail, $nom, $prenom, $pass, $enabled, $canpass, $type = 'default', $duration = 0, $notes = "", $reset_quotas = false) {
global $msg, $db, $quota;
$msg->log("admin", "update_mem", $uid);
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
$db = new DB_System();
if ($pass) {
$pass = password_hash($pass, PASSWORD_BCRYPT);
$second_query = "UPDATE membres SET mail= ?, canpass= ?, enabled= ?, `type`= ?, notes= ? , pass = ? WHERE uid= ?;";
$second_query_args = array($mail, $canpass, $enabled, $type, $notes, $pass, $uid);
} else {
$second_query = "UPDATE membres SET mail= ?, canpass= ?, enabled= ?, `type`= ?, notes= ? WHERE uid= ?;";
$second_query_args = array($mail, $canpass, $enabled, $type, $notes, $uid);
}
$old_mem = $this->get($uid);
if(
($db->query("UPDATE local SET nom= ?, prenom= ? WHERE uid=?;", array($nom, $prenom, $uid))) &&
($db->query($second_query, $second_query_args))
){
if ($reset_quotas == "on" || $type != $old_mem['type']) {
$quota->addquotas();
$quota->synchronise_user_profile();
}
$this->renew_update($uid, $duration);
return true;
} else {
$msg->raise("ERROR", "admin", _("Account not found"));
return false;
}
}
/**
* Lock an account
*
* Lock an account and prevent the user to access its account.
*
* @global m_messages $msg
* @global m_mysql $db
* @param int $uid The uid number of the account
* @return boolean Returns FALSE if an error occurs, TRUE if not.
*/
function lock_mem($uid) {
global $msg, $db;
$msg->log("admin", "lock_mem", $uid);
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
$db = new DB_System();
if ($db->query("UPDATE membres SET enabled='0' WHERE uid= ?;", array($uid))) {
return true;
} else {
$msg->raise("ERROR", "admin", _("Account not found"));
return false;
}
}
/**
* UnLock an account
*
* UnLock an account and prevent the user to access its account.
*
*
* @global m_messages $msg
* @global m_mysql $db
* @param int $uid The uid number of the account
* @return boolean Returns FALSE if an error occurs, TRUE if not.
*/
function unlock_mem($uid) {
global $msg, $db;
$msg->log("admin", "unlock_mem", $uid);
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
$db = new DB_System();
if ($db->query("UPDATE membres SET enabled='1' WHERE uid= ?;", array($uid))) {
return true;
} else {
$msg->raise("ERROR", "admin", _("Account not found"));
return false;
}
}
/** Deletes an account
* Deletes the specified account. Prevents any manipulation of the account if
* the account $mid is not super-admin.
*
* @global m_messages $msg
* @global m_quota $quota
* @global array $classes
* @global int $cuid
* @global m_mem $mem
* @global m_dom $dom
* @global m_hooks $hooks
* @global m_action $action
* @param int $uid The uid number of the account
* @return boolean Returns FALSE if an error occurs, TRUE if not.
*/
function del_mem($uid) {
global $msg, $mem, $dom, $hooks, $action;
$msg->log("admin", "del_mem", $uid);
if (!$this->enabled) {
$msg->raise("ERROR", "admin", _("-- Only administrators can access this page! --"));
return false;
}
$db = new DB_System();
$tt = $this->get($uid);
$mem->su($uid);
// This script may take a long time on big accounts, let's give us some time ... Fixes 1132
@set_time_limit(0);
// WE MUST call m_dom before all others because of conflicts ...
$dom->admin_del_member();
# New way of deleting or backup delted user html folders using action class
$path = getuserpath($tt['login']);
$action->archive($path);
$hooks->invoke("alternc_del_member");
$hooks->invoke("hook_admin_del_member");
if (($db->query("DELETE FROM membres WHERE uid= ?;", array($uid))) &&
($db->query("DELETE FROM local WHERE uid= ?;", array($uid)))) {
$mem->unsu();
// If this user was (one day) an administrator one, he may have a list of his own accounts. Let's associate those accounts to nobody as a creator.
$db->query("UPDATE membres SET creator=2000 WHERE creator= ?;", array($uid));
return true;
} else {
$msg->raise("ERROR", "admin", _("Account not found"));
$mem->unsu();
return false;
}
}
/**
* Renew an account
*
* Renew an account for its duration
*
* @global m_messages $msg
* @global m_mysql $db
* @param int $uid The uid number of the account
* @param int $periods The new duration, in months, of the account
* @return boolean Returns FALSE if an error occurs, TRUE if not.
*/
function renew_mem($uid, $periods = 1) {
global $msg, $db;
$periods = intval($periods);
if ($periods == 0) {
return false;
}
if ($db->query("UPDATE membres SET renewed = renewed + INTERVAL (duration * ?) MONTH WHERE uid= ?;", array($periods, $uid))) {
return true;
} else {
$msg->raise("ERROR", "admin", _("Account not found"));
return false;
}
}
/**
* Update the duration information for an account
*
* @global m_messages $msg
* @global m_mysql $db
* @param int $uid The uid number of the account
* @param int $duration The new duration, in months, of the account
* @return boolean Returns FALSE if an error occurs, TRUE if not.
*/
function renew_update($uid, $duration) {
global $msg, $db;
if ($duration == 0) {
if ($db->query("UPDATE membres SET duration = NULL, renewed = NULL WHERE uid= ?;", array($uid))) {
return true;
}
} else {
if ($db->query("UPDATE membres SET duration = ? WHERE uid= ?", array($duration, $uid)) &&
$db->query("UPDATE membres SET renewed = NOW() WHERE uid= ? and renewed is null;", array($uid))) {
return true;
}
}
$msg->raise("ERROR", "admin", _("Account not found"));
return false;
}
/**
* Get the expiry date for an account
*
* @param int $uid The uid number of the account
* @return string The expiry date, a string as printed by MySQL
*/
function renew_get_expiry($uid) {
$jj = $this->get($uid);
if (isset($jj) && isset($jj['expiry']) && !empty($jj['expiry'])) {
return $jj['expiry'];
}
return '';
}
/**
* Get the expiry status for an account
*
* @param int $uid The uid number of the account
* @return integer The expiry status:
* 0: account does not expire