-
-
Save c33s/6f19715cec080ae913accc8a3a8e2471 to your computer and use it in GitHub Desktop.
iredmail-init.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "c33s/iredmail-init", | |
"description": "runs mixed curl calls", | |
"type": "project", | |
"require": { | |
"symfony/console": "^3.2", | |
"symfony/yaml": "^3.2", | |
"symfony/process": "^3.2" | |
}, | |
"license": "MIT", | |
"minimum-stability": "stable" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
api: | |
server: https://127.0.0.1/iredadmin/api | |
username: postmaster@example.com | |
password: mypassword | |
curl_flags: '--insecure' | |
domains: | |
example.com: | |
quota: 6144 | |
defaultQuota: 1024 | |
language: de_DE | |
numberOfUsers: 10 | |
numberOfAliases: 50 | |
name: Example Inc. | |
user: | |
example.com: | |
mainusername: | |
firstname: firstname | |
lastname: lastname | |
password: myotherpassword | |
quota: 1024 | |
office: | |
firstname: ~ | |
lastname: ~ | |
password: 54321 | |
quota: 1024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
require __DIR__.'/vendor/autoload.php'; | |
use Symfony\Component\Console\Application; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Yaml\Yaml; | |
use Symfony\Component\Process\Process; | |
(new Application('echo', '1.0.0')) | |
->register('echo') | |
->addArgument('foo', InputArgument::OPTIONAL, 'The directory') | |
->addOption('bar', null, InputOption::VALUE_REQUIRED) | |
->setCode(function(InputInterface $input, OutputInterface $output) { | |
//$worker = new Worker($input,$output); | |
//$worker->run(); | |
$config = Yaml::parse(file_get_contents('config.yaml')); | |
run("curl -X POST {$config['api']['curl_flags']} -c /tmp/cookie.txt -d \"username={$config['api']['username']}&password={$config['api']['password']}\" {$config['api']['server']}/login", $output); | |
//-i Include the HTTP-header in the output | |
$base="curl -X POST {$config['api']['curl_flags']} -b /tmp/cookie.txt -i -d"; | |
$base_d="curl -X POST {$config['api']['curl_flags']} -b /tmp/cookie.txt"; | |
$base_put="curl -X PUT {$config['api']['curl_flags']} -b /tmp/cookie.txt -i -d"; | |
$base_delete="curl -X DELETE {$config['api']['curl_flags']} -b /tmp/cookie.txt "; | |
$server="{$config['api']['server']}"; | |
foreach ($config['domains'] as $domain => $parameter) { | |
$output->write("<info>### creating domain $domain</info>"); | |
run("$base \"name={$parameter['name']}"a={$parameter['quota']}&defaultQuota={$parameter['defaultQuota']}\" {$server}/domain/{$domain}", $output); | |
} | |
foreach ($config['user'] as $domain => $users) { | |
foreach ($users as $username => $parameter) { | |
$output->writeln("<info>### creating $username</info>"); | |
$firstname=ucfirst($parameter['firstname']); | |
$lastname=ucfirst($parameter['lastname']); | |
$name="$firstname $lastname"; | |
$quota=$parameter['quota']; | |
$password=$parameter['password']; | |
$aliases=""; | |
if (array_key_exists('firstname',$parameter) && array_key_exists('lastname',$parameter) && $parameter['firstname'] && $parameter['lastname']) { | |
$alis=[ | |
"$firstname@$domain", | |
"$firstname.$lastname@$domain", | |
]; | |
foreach ($alis as $ali) { | |
$ali = strtolower($ali); | |
$cmd_ali = "$base_d {$server}/alias/{$ali}"; | |
$output->write("<comment>creating alias $ali</comment> "); | |
run($cmd_ali, $output); | |
} | |
$aliases="&aliases=".strtolower("$firstname@$domain,$firstname.$lastname@$domain"); | |
} | |
$output->write("<comment>cleanup</comment> "); | |
$cmd_delete="$base_delete {$server}/user/{$username}@{$domain}"; | |
run($cmd_delete, $output); | |
$output->write("<comment>create user</comment> "); | |
$cmd_create="$base \"name={$name}"a={$quota}&password={$password}\" {$server}/user/{$username}@{$domain}"; | |
run($cmd_create, $output); | |
// $output->write("<error>$aliases</error>"); | |
if (!empty($aliases)){ | |
$output->write("<comment>update user</comment> "); | |
$cmd_alias ="$base_put \"quota={$quota}{$aliases}\" {$server}/user/{$username}@{$domain}"; | |
$output->write("<error>$cmd_alias</error>"); | |
run($cmd_alias, $output); | |
} | |
} | |
} | |
}) | |
->getApplication() | |
->setDefaultCommand('echo', true) // Single command application | |
->run(); | |
function run($command, OutputInterface $output) | |
{ | |
$process = new Process($command); | |
$process->run(); | |
$output->writeln($process->getOutput()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment