cc3a5c178aba0e3081d3331321d3f167238baaf4
[shpub.git] / src / shpub / Command / Note.php
1 <?php
2 namespace shpub;
3
4 class Command_Note
5 {
6     /**
7      * @var Config
8      */
9     protected $cfg;
10
11     public function __construct($cfg)
12     {
13         $this->cfg = $cfg;
14     }
15
16     public static function opts(\Console_CommandLine $optParser)
17     {
18         $cmd = $optParser->addCommand('note');
19         $cmd->addOption(
20             'categories',
21             array(
22                 'short_name'  => '-c',
23                 'long_name'   => '--category',
24                 'description' => 'Categories',
25                 'help_name'   => 'CAT',
26                 'action'      => 'StoreArray',
27                 'default'     => [],
28             )
29         );
30         $cmd->addOption(
31             'files',
32             array(
33                 'short_name'  => '-f',
34                 'long_name'   => '--files',
35                 'description' => 'Files to upload',
36                 'help_name'   => 'PATH',
37                 'action'      => 'StoreArray',
38                 'default'     => [],
39             )
40         );
41         $cmd->addOption(
42             'published',
43             array(
44                 'long_name'   => '--published',
45                 'description' => 'Publish date',
46                 'help_name'   => 'DATE',
47                 'action'      => 'StoreString',
48                 'default'     => null,
49             )
50         );
51         $cmd->addOption(
52             'syndication',
53             array(
54                 'short_name'  => '-s',
55                 'long_name'   => '--syndication',
56                 'description' => 'Syndication URL(s)',
57                 'help_name'   => 'URL',
58                 'action'      => 'StoreArray',
59                 'default'     => [],
60             )
61         );
62         $cmd->addOption(
63             'x',
64             array(
65                 'short_name'  => '-x',
66                 'long_name'   => '--xprop',
67                 'description' => 'Additional property',
68                 'help_name'   => 'key=value',
69                 'action'      => 'StoreArray',
70                 'default'     => [],
71             )
72         );
73         $cmd->addArgument(
74             'text',
75             [
76                 'optional'    => false,
77                 'multiple'    => false,
78                 'description' => 'Post text',
79             ]
80         );
81     }
82
83     public function run($command)
84     {
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']
91             );
92         }
93         if (count($command->options['categories'])) {
94             $req->addPostParameter(
95                 'category', $command->options['categories']
96             );
97         }
98         if (count($command->options['syndication'])) {
99             $req->addPostParameter(
100                 'syndication', $command->options['syndication']
101             );
102         }
103
104         $files = $command->options['files'];
105         $fileList = $urlList = [
106             'audio' => [],
107             'image' => [],
108             'video' => [],
109         ];
110
111         foreach ($files as $filePath) {
112             if (strpos($filePath, '://') !== false) {
113                 //url
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);
119                     exit(20);
120                 }
121                 $urlList[$media][] = $filePath;
122             } else if (file_exists($filePath)) {
123                 //file
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);
128                     exit(20);
129                 }
130                 $fileList[$media][] = $filePath;
131             } else {
132                 Log::err('File does not exist: ' . $filePath);
133                 exit(20);
134             }
135         }
136         foreach ($urlList as $type => $urls) {
137             if ($type == 'image') {
138                 $type = 'photo';
139             }
140             if (count($urls) == 1) {
141                 $req->req->addPostParameter($type, reset($urls));
142             } else if (count($urls) > 1) {
143                 $n = 0;
144                 foreach ($urls as $url) {
145                     $req->req->addPostParameter(
146                         $type . '[' . $n++ . ']', $url
147                     );
148                 }
149             }
150         }
151         foreach ($fileList as $type => $filePaths) {
152             if ($type == 'image') {
153                 $type = 'photo';
154             }
155             if (count($filePaths) == 1) {
156                 $req->addUpload($type, reset($filePaths));
157             } else if (count($filePaths) > 0) {
158                 $req->addUpload($type, $filePaths);
159             }
160         }
161
162         if (count($command->options['x'])) {
163             $postParams = [];
164             foreach ($command->options['x'] as $xproperty) {
165                 list($propkey, $propval) = explode('=', $xproperty, 2);
166                 if (!isset($postParams[$propkey] )) {
167                     $postParams[$propkey] = [];
168                 }
169                 $postParams[$propkey][] = $propval;
170             }
171             foreach ($postParams as $propkey => $propvals) {
172                 $req->addPostParameter($propkey, $propvals);
173             }
174         }
175
176         $res = $req->send();
177         $postUrl = $res->getHeader('Location');
178         echo "Post created at server\n";
179         echo $postUrl . "\n";
180     }
181 }
182 ?>