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',
56 'description' => 'Post text',
61 public function run($command)
63 $req = new Request($this->cfg->host, $this->cfg);
64 $req->req->addPostParameter('h', 'entry');
65 $req->req->addPostParameter('content', $command->args['text']);
66 if ($command->options['published'] !== null) {
67 $req->req->addPostParameter(
68 'published', $command->options['published']
71 if (count($command->options['categories'])) {
72 $req->req->addPostParameter(
73 'category', $command->options['categories']
77 $files = $command->options['files'];
78 $fileList = $urlList = [
84 foreach ($files as $filePath) {
85 if (strpos($filePath, '://') !== false) {
87 $mte = new \MIME_Type_Extension();
88 $mimetype = $mte->getMIMEType($filePath);
89 $media = \MIME_Type::getMedia($mimetype);
90 if (!isset($urlList[$media])) {
91 Log::err('File type not allowed: ' . $mimetype);
94 $urlList[$media][] = $filePath;
95 } else if (file_exists($filePath)) {
97 $mimetype = \MIME_Type::autoDetect($filePath);
98 $media = \MIME_Type::getMedia($mimetype);
99 if (!isset($urlList[$media])) {
100 Log::err('File type not allowed: ' . $mimetype);
103 $fileList[$media][] = $filePath;
105 Log::err('File does not exist: ' . $filePath);
109 foreach ($urlList as $type => $urls) {
110 if ($type == 'image') {
113 if (count($urls) == 1) {
114 $req->req->addPostParameter($type, reset($urls));
115 } else if (count($urls) > 1) {
117 foreach ($urls as $url) {
118 $req->req->addPostParameter(
119 $type . '[' . $n++ . ']', reset($urls)
124 foreach ($fileList as $type => $filePaths) {
125 if ($type == 'image') {
128 if (count($filePaths) == 1) {
129 $req->addUpload($type, reset($filePaths));
130 } else if (count($filePaths) > 0) {
131 $req->addUpload($type, $filePaths);
136 $postUrl = $res->getHeader('Location');
137 echo "Post created at server\n";
138 echo $postUrl . "\n";