11 public function __construct($cfg)
16 public static function opts(\Console_CommandLine $optParser)
18 $cmd = $optParser->addCommand('note');
23 'long_name' => '--category',
24 'description' => 'Categories',
26 'action' => 'StoreArray',
34 'long_name' => '--files',
35 'description' => 'Files to upload',
36 'help_name' => 'PATH',
37 'action' => 'StoreArray',
44 'long_name' => '--published',
45 'description' => 'Publish date',
46 'help_name' => 'DATE',
47 'action' => 'StoreString',
55 'long_name' => '--syndication',
56 'description' => 'Syndication URL(s)',
58 'action' => 'StoreArray',
66 'long_name' => '--xprop',
67 'description' => 'Additional property',
68 'help_name' => 'key=value',
69 'action' => 'StoreArray',
78 'description' => 'Post text',
83 public function run($command)
85 $req = new Request($this->cfg->host, $this->cfg);
86 $req->req->addPostParameter('h', 'entry');
87 $req->req->addPostParameter('content', $command->args['text']);
88 if ($command->options['published'] !== null) {
89 $req->req->addPostParameter(
90 'published', $command->options['published']
93 if (count($command->options['categories'])) {
94 $req->addPostParameter(
95 'category', $command->options['categories']
98 if (count($command->options['syndication'])) {
99 $req->addPostParameter(
100 'syndication', $command->options['syndication']
104 $files = $command->options['files'];
105 $fileList = $urlList = [
111 foreach ($files as $filePath) {
112 if (strpos($filePath, '://') !== false) {
114 $mte = new \MIME_Type_Extension();
115 $mimetype = $mte->getMIMEType($filePath);
116 $media = \MIME_Type::getMedia($mimetype);
117 if (!isset($urlList[$media])) {
118 Log::err('File type not allowed: ' . $mimetype);
121 $urlList[$media][] = $filePath;
122 } else if (file_exists($filePath)) {
124 $mimetype = \MIME_Type::autoDetect($filePath);
125 $media = \MIME_Type::getMedia($mimetype);
126 if (!isset($urlList[$media])) {
127 Log::err('File type not allowed: ' . $mimetype);
130 $fileList[$media][] = $filePath;
132 Log::err('File does not exist: ' . $filePath);
136 foreach ($urlList as $type => $urls) {
137 if ($type == 'image') {
140 if (count($urls) == 1) {
141 $req->req->addPostParameter($type, reset($urls));
142 } else if (count($urls) > 1) {
144 foreach ($urls as $url) {
145 $req->req->addPostParameter(
146 $type . '[' . $n++ . ']', $url
151 foreach ($fileList as $type => $filePaths) {
152 if ($type == 'image') {
155 if (count($filePaths) == 1) {
156 $req->addUpload($type, reset($filePaths));
157 } else if (count($filePaths) > 0) {
158 $req->addUpload($type, $filePaths);
162 if (count($command->options['x'])) {
164 foreach ($command->options['x'] as $xproperty) {
165 list($propkey, $propval) = explode('=', $xproperty, 2);
166 if (!isset($postParams[$propkey] )) {
167 $postParams[$propkey] = [];
169 $postParams[$propkey][] = $propval;
171 foreach ($postParams as $propkey => $propvals) {
172 $req->addPostParameter($propkey, $propvals);
177 $postUrl = $res->getHeader('Location');
178 echo "Post created at server\n";
179 echo $postUrl . "\n";