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' => '--xprop',
56 'description' => 'Additional property',
57 'help_name' => 'key=value',
58 'action' => 'StoreArray',
67 'description' => 'Post text',
72 public function run($command)
74 $req = new Request($this->cfg->host, $this->cfg);
75 $req->req->addPostParameter('h', 'entry');
76 $req->req->addPostParameter('content', $command->args['text']);
77 if ($command->options['published'] !== null) {
78 $req->req->addPostParameter(
79 'published', $command->options['published']
82 if (count($command->options['categories'])) {
83 $req->req->addPostParameter(
84 'category', $command->options['categories']
88 $files = $command->options['files'];
89 $fileList = $urlList = [
95 foreach ($files as $filePath) {
96 if (strpos($filePath, '://') !== false) {
98 $mte = new \MIME_Type_Extension();
99 $mimetype = $mte->getMIMEType($filePath);
100 $media = \MIME_Type::getMedia($mimetype);
101 if (!isset($urlList[$media])) {
102 Log::err('File type not allowed: ' . $mimetype);
105 $urlList[$media][] = $filePath;
106 } else if (file_exists($filePath)) {
108 $mimetype = \MIME_Type::autoDetect($filePath);
109 $media = \MIME_Type::getMedia($mimetype);
110 if (!isset($urlList[$media])) {
111 Log::err('File type not allowed: ' . $mimetype);
114 $fileList[$media][] = $filePath;
116 Log::err('File does not exist: ' . $filePath);
120 foreach ($urlList as $type => $urls) {
121 if ($type == 'image') {
124 if (count($urls) == 1) {
125 $req->req->addPostParameter($type, reset($urls));
126 } else if (count($urls) > 1) {
128 foreach ($urls as $url) {
129 $req->req->addPostParameter(
130 $type . '[' . $n++ . ']', reset($urls)
135 foreach ($fileList as $type => $filePaths) {
136 if ($type == 'image') {
139 if (count($filePaths) == 1) {
140 $req->addUpload($type, reset($filePaths));
141 } else if (count($filePaths) > 0) {
142 $req->addUpload($type, $filePaths);
146 if (count($command->options['x'])) {
147 foreach ($command->options['x'] as $xproperty) {
148 list($propkey, $propval) = explode('=', $xproperty, 2);
149 $req->req->addPostParameter($propkey, $propval);
154 $postUrl = $res->getHeader('Location');
155 echo "Post created at server\n";
156 echo $postUrl . "\n";