Alternc  latest
Alternc logiel libre pour l'hébergement
mail_add.php
Go to the documentation of this file.
1 #!/usr/bin/php -q
2 <?php
3 /**
4  * @file helper function to create emails from the commandline
5  *
6  * automatically generates a password based on the configured
7  * password generators
8  *
9  * Limitations
10  * - has snarky comments about how PHP or AlternC is badly designed
11  * - can create a mailbox or a forward, not both
12  * - configuration is inline here
13  * - quotas and "dryrun" options are hardcoded because php's getopt sucks
14  */
15 
16 // just for inspection
17 global $cuid;
18 
19 // those will be tried in order, the first one to return more than 7
20 // chars will win
21 $generators = array('pwqgen', 'pwgen');
22 $dryrun = false;
23 // 1GB default quota
24 $default_quotas = 1024; // in MB because using bytes would be too
25  // precise (try to guess AlternC, just you try)
26 
27 require_once("/usr/share/alternc/panel/class/config_nochk.php");
28 
29 function usage() {
30  global $argv;
31  // putting {$argv[0]} or $argv[0] in the heredoc fails
32  $wtfphp = $argv[0];
33  $u = '
34 Usage: $wtfphp <email> <alias> ...
35 
36 email: full email adress, including domain, which must exist
37 alias: one or many aliases the email should forward to, space separated
38 
39 ';
40  error_log($u);
41  exit(1);
42 }
43 
44 if (count($argv) < 2) {
45  usage();
46 }
47 
48 $user = preg_split('/@/', $argv[1])[0]; // first argument is user@domain
49 $domain = preg_split('/@/', $argv[1])[1];
50 $recipients = array_slice($argv, 2); // rest is recipients
51 
52 // there's no function to do that, oddly enough...
53 // there's one to extract the compte from the mail_id (!) but we
54 // haven't created it yet...
55 $db->query('SELECT id,compte FROM domaines WHERE domaine=?',array($domain));
56 if ($db->next_record()) {
57  $compte = $db->f('compte');
58  $domain_id = $db->f('id');
59 }
60 else {
61  error_log("domain $domain not found");
62  exit(2);
63 }
64 
65 foreach ($generators as $generator) {
66  $password = `$generator 2>/dev/null`;
67  if (!is_null($password) and strlen($password) > 7) {
68  $password = trim($password);
69  break;
70  }
71 }
72 if (is_null($password)) {
73  error_log('password generators failed to produce 8 characters: ' . join("\n", $generators));
74  exit(3);
75 }
76 
77 /* need not to be $quota because that would replace alternc's global
78  * $quota... even though we don't say global $quota anywhere here, yay
79  * php scoping.
80  */
82 $r = join(", ", $recipients);
83 
85 user: $user
86 domain: $domain
87 compte: $compte
88 password: $password
89 quota: $default_quotas
90 recipients: $r
91 
92 ';
93 
94 if ($dryrun) {
95  error_log('not creating email because of $dryrun is true');
96  exit(0);
97 }
98 print "cuid: $cuid\n";
99 $mem->su($compte);
100 print "cuid: $cuid\n";
101 
102 /* function signature is:
103  * function create($dom_id, $mail,$type="",$dontcheck=false)
104  * yet $type is never passed anywhere and is actually empty in the
105  * database (!) $dontcheck is undocumented, so we'll ignore it
106  *
107  * also, this function explicitely tells me to not use it, but doesn't
108  * provide an alternative. i choose to disobey instead of rewriting it
109  * from scratch
110  */
111 if (!($mail_id = $mail->create($domain_id, $user))) {
112  error_log('failed to create: ' . $msg->msg_str());
113  exit(4);
114 }
115 
116 /* function set_passwd($mail_id,$pass)
117  *
118  * just set the password
119  *
120  * no idea why this is a different function.
121  */
122 if (!$mail->set_passwd($mail_id,$password)) {
123  error_log("failed to set password on mail $mail_id: " . $msg->msg_str());
124  exit(5);
125 }
126 
127 /* function set_details($mail_id, $islocal, $quotamb,
128  * $recipients,$delivery="dovecot",$dontcheck=false)
129  *
130  * you read that right, recipients is a string (!)
131  *
132  * if we have no aliases, it's a mailbox. deal with it.
133  */
134 if (!$mail->set_details($mail_id, !count($recipients), $quota, join("\n", $recipients))) {
135  error_log('failed to set details: ' . $msg->msg_str());
136  exit(6);
137 }
138 
139 // maybe we need to call the hooks? i don't know!
140  /* $rh=$hooks->invoke("mail_edit_post",array($mail_id)); */
141  /* if (in_array(false,$res,true)) { */
142  /* include ("mail_edit.php"); */
143  /* exit(); */
144  /* } else { */
145  /* foreach($rh as $h) if ($h) $error.=$h."<br />"; */
146  /* } */
147 
exit
Definition: adm_doadd.php:70
global $db
Definition: bootstrap.php:26
$mem
Definition: bootstrap.php:71
$msg
Definition: bootstrap.php:75
$recipients
Definition: mail_add.php:50
print
Definition: mail_add.php:92
$r
Definition: mail_add.php:82
global $cuid
Definition: mail_add.php:17
if(count($argv)< 2) $user
Definition: mail_add.php:48
$default_quotas
Definition: mail_add.php:24
foreach($generators as $generator) if(is_null($password)) $quotas
Definition: mail_add.php:81
$generators
Definition: mail_add.php:21
$domain
Definition: mail_add.php:49
$dryrun
Definition: mail_add.php:22
usage()
Definition: mail_add.php:29
$mail_id
$password
Definition: bootstrap.php:85