forked from mapkyca/idno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
known.php
executable file
·166 lines (140 loc) · 7.63 KB
/
known.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
#!/usr/bin/php -q
<?php
define('KNOWN_CONSOLE', 'true');
// Load Symfony
require_once((dirname(__FILE__)) . '/external/Symfony/Component/ClassLoader/UniversalClassLoader.php');
global $known_loader;
$known_loader = new \Symfony\Component\ClassLoader\UniversalClassLoader();
$known_loader->registerNamespace('Symfony\Component', (dirname(__FILE__)) . '/external');
$known_loader->register();
/**
* Retrieve the loader
* @return \Symfony\Component\ClassLoader\UniversalClassLoader
*/
function &loader()
{
global $known_loader;
return $known_loader;
}
// Register console namespace
use Symfony\Component\Console\Application;
// Create new console application
global $console;
$console = new Application();
function &application() {
global $console;
return $console;
}
// Known core namespaces
$known_loader->registerNamespace('Idno', dirname(__FILE__) );
$known_loader->registerNamespace('ConsolePlugins', dirname(__FILE__) );
// Implement the PSR-3 logging interface
$known_loader->registerNamespace('Psr\Log', dirname(__FILE__) . '/external/log');
// Symfony is used for routing, observer design pattern support, and a bunch of other fun stuff
$known_loader->registerNamespace('Symfony\Component', dirname(__FILE__) . '/external');
$known_loader->registerNamespace('webignition\Url', dirname(__FILE__) . '/external/webignition/url/src');
$known_loader->registerNamespace('webignition\AbsoluteUrlDeriver', dirname(__FILE__) . '/external/webignition/absolute-url-deriver/src');
$known_loader->registerNamespace('webignition\NormalisedUrl', dirname(__FILE__) . '/external/webignition/url/src');
$known_loader->registerNamespace('Mf2', dirname(__FILE__) . '/external/mf2');
$known_loader->registerNamespace('IndieWeb', dirname(__FILE__) . '/external/mention-client-php/src');
// TODO: FIND A WAY TO NOT LOAD THESE FOR CONSOLE
// Using HTMLPurifier for HTML sanitization
include dirname(__FILE__) . '/external/htmlpurifier-lite/library/HTMLPurifier.auto.php';
///////////////////
// Load any plugin functions
$directory = dirname(__FILE__) . '/ConsolePlugins/';
if ($scanned_directory = array_diff(scandir($directory), array('..', '.'))) {
foreach ($scanned_directory as $file) {
if (is_dir($directory . $file)) {
$class = "ConsolePlugins\\$file\\Main";
if (class_exists($class)) {
$c = new $class();
if ($c instanceof \Idno\Common\ConsolePlugin) {
$console->register($c->getCommand())
->setDescription($c->getDescription())
->setDefinition($c->getParameters())
->setCode([$c, 'execute']);
}
}
}
}
}
// Allow regular plugins to contain a ConsoleMain.php
$directory = dirname(__FILE__) . '/IdnoPlugins/';
if ($scanned_directory = array_diff(scandir($directory), array('..', '.'))) {
foreach ($scanned_directory as $file) {
if (is_dir($directory . $file)) {
$class = "IdnoPlugins\\$file\\ConsoleMain";
if (class_exists($class)) {
$c = new $class();
if ($c instanceof \Idno\Common\ConsolePlugin) {
$console->register($c->getCommand())
->setDescription($c->getDescription())
->setDefinition($c->getParameters())
->setCode([$c, 'execute']);
}
}
// See if there's a console subdir, for multiple sub commands
$scanned = @scandir($directory . $file . '/Console/');
if (!empty($scanned) && $scanned_sub_directory = array_diff($scanned, array('..', '.'))) {
foreach ($scanned_sub_directory as $file2) {
$class2 = "IdnoPlugins\\$file\\Console\\$file2";
$class2 = str_replace('.php', '', $class2);
if (class_exists($class2)) {
$c = new $class2();
if ($c instanceof \Idno\Common\ConsolePlugin) {
$console->register($c->getCommand())
->setDescription($c->getDescription())
->setDefinition($c->getParameters())
->setCode([$c, 'execute']);
}
}
}
}
}
}
}
$console
->register('makeconfig')
->setDescription('Attempts to write configuration variables to a Known config.ini file')
->setDefinition([
new \Symfony\Component\Console\Input\InputArgument('dbuser', \Symfony\Component\Console\Input\InputArgument::REQUIRED, 'Database username'),
new \Symfony\Component\Console\Input\InputArgument('dbpass', \Symfony\Component\Console\Input\InputArgument::REQUIRED, 'Database password'),
new \Symfony\Component\Console\Input\InputArgument('dbname', \Symfony\Component\Console\Input\InputArgument::REQUIRED, 'Database name'),
new \Symfony\Component\Console\Input\InputArgument('database', \Symfony\Component\Console\Input\InputArgument::OPTIONAL, 'Database type', 'mysql'),
new \Symfony\Component\Console\Input\InputArgument('dbhost', \Symfony\Component\Console\Input\InputArgument::OPTIONAL, 'Database hostname', 'localhost'),
new \Symfony\Component\Console\Input\InputArgument('filename', \Symfony\Component\Console\Input\InputArgument::OPTIONAL, 'Configuration filename', 'config.ini'),
])
->setCode(function (\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output) {
if ($fp = fopen($input->getArgument('filename'), 'w')) {
fwrite($fp, "[Database configuration]\n");
fwrite($fp, "database=" . $input->getArgument('database') . "\n");
fwrite($fp, "dbhost=" . $input->getArgument('dbhost') . "\n");
fwrite($fp, "dbname=" . $input->getArgument('dbname') . "\n");
fwrite($fp, "dbuser=" . $input->getArgument('dbuser') . "\n");
fwrite($fp, "dbpass=" . $input->getArgument('dbpass') . "\n");
fclose($fp);
} else {
$output->writeln("Couldn't open " . $input->getArgument('filename'));
}
});
$console
->register('version')
->setDescription('Returns the current Known version as defined in version.known')
->setDefinition([])
->setCode(function (\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output) {
$output->writeln(file_get_contents(dirname(__FILE__) . '/version.known'));
});
// Boot known
try {
$idno = new Idno\Core\Idno();
$account = new Idno\Core\Account();
$admin = new Idno\Core\Admin();
$webfinger = new Idno\Core\Webfinger();
$webmention = new Idno\Core\Webmention();
$pubsubhubbub = new Idno\Core\PubSubHubbub();
} catch (\Exception $e) {
error_log($e->getMessage());
}
// Run the application
$console->run();