Add location header to --dry-run fake response
[shpub.git] / src / shpub / Command / AbstractProps.php
1 <?php
2 namespace shpub;
3
4 /**
5  * Abstract command class that handles generic properties
6  *
7  * @author  Christian Weiske <cweiske@cweiske.de>
8  * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
9  * @link    http://cweiske.de/shpub.htm
10  */
11 class Command_AbstractProps
12 {
13     /**
14      * @var Config
15      */
16     protected $cfg;
17
18     public function __construct($cfg)
19     {
20         $this->cfg = $cfg;
21     }
22
23     public static function optsGeneric(\Console_CommandLine_Command $cmd)
24     {
25         $cmd->addOption(
26             'categories',
27             array(
28                 'short_name'  => '-c',
29                 'long_name'   => '--category',
30                 'description' => 'Category names',
31                 'help_name'   => 'CAT',
32                 'action'      => 'StoreArray',
33                 'default'     => [],
34             )
35         );
36         $cmd->addOption(
37             'files',
38             array(
39                 'short_name'  => '-f',
40                 'long_name'   => '--file',
41                 'description' => 'Files or URLs to upload',
42                 'help_name'   => 'PATH',
43                 'action'      => 'StoreArray',
44                 'default'     => [],
45             )
46         );
47         $cmd->addOption(
48             'direct_upload',
49             array(
50                 'long_name'   => '--direct-upload',
51                 'description' => 'Ignore media endpoint at file upload',
52                 'action'      => 'StoreTrue',
53                 'default'     => false,
54             )
55         );
56         $cmd->addOption(
57             'name',
58             array(
59                 'short_name'  => '-n',
60                 'long_name'   => '--name',
61                 'description' => 'Post title',
62                 'help_name'   => 'TITLE',
63                 'action'      => 'StoreString',
64                 'default'     => null,
65             )
66         );
67         $cmd->addOption(
68             'published',
69             array(
70                 'long_name'   => '--published',
71                 'description' => 'Publish date',
72                 'help_name'   => 'DATE',
73                 'action'      => 'StoreString',
74                 'default'     => null,
75             )
76         );
77         $cmd->addOption(
78             'updated',
79             array(
80                 'long_name'   => '--updated',
81                 'description' => 'Update date',
82                 'help_name'   => 'DATE',
83                 'action'      => 'StoreString',
84                 'default'     => null,
85             )
86         );
87         $cmd->addOption(
88             'slug',
89             array(
90                 'short_name'  => '-s',
91                 'long_name'   => '--slug',
92                 'description' => 'URL path',
93                 'help_name'   => 'PATH',
94                 'action'      => 'StoreString',
95                 'default'     => null,
96             )
97         );
98         $cmd->addOption(
99             'syndication',
100             array(
101                 'short_name'  => '-u',
102                 'long_name'   => '--syndication',
103                 'description' => 'Syndication URL(s)',
104                 'help_name'   => 'URL',
105                 'action'      => 'StoreArray',
106                 'default'     => [],
107             )
108         );
109         $cmd->addOption(
110             'x',
111             array(
112                 'short_name'  => '-x',
113                 'long_name'   => '--xprop',
114                 'description' => 'Additional property',
115                 'help_name'   => 'key=value',
116                 'action'      => 'StoreArray',
117                 'default'     => [],
118             )
119         );
120         static::addOptJson($cmd);
121     }
122
123     protected static function addOptHtml(\Console_CommandLine_Command $cmd)
124     {
125         $cmd->addOption(
126             'html',
127             array(
128                 'long_name'   => '--html',
129                 'description' => 'Text content is HTML',
130                 'action'      => 'StoreTrue',
131                 'default'     => false,
132             )
133         );
134     }
135
136     protected static function addOptJson(\Console_CommandLine_Command $cmd)
137     {
138         $cmd->addOption(
139             'json',
140             array(
141                 'long_name'   => '--json',
142                 'description' => 'Send request data as JSON',
143                 'action'      => 'StoreTrue',
144                 'default'     => false,
145             )
146         );
147     }
148
149     protected function handleGenericOptions(
150         \Console_CommandLine_Result $cmdRes, Request $req
151     ) {
152         $this->handleOptJson($cmdRes, $req);
153
154         if ($cmdRes->options['published'] !== null) {
155             $req->addProperty(
156                 'published', $cmdRes->options['published']
157             );
158         }
159         if ($cmdRes->options['updated'] !== null) {
160             $req->addProperty(
161                 'updated', $cmdRes->options['updated']
162             );
163         }
164         if (count($cmdRes->options['categories'])) {
165             $req->addProperty(
166                 'category', $cmdRes->options['categories']
167             );
168         }
169         if ($cmdRes->options['name'] !== null) {
170             $req->addProperty(
171                 'name', $cmdRes->options['name']
172             );
173         }
174         if ($cmdRes->options['slug'] !== null) {
175             $req->addProperty(
176                 'slug', $cmdRes->options['slug']
177             );
178         }
179         if (count($cmdRes->options['syndication'])) {
180             $req->addProperty(
181                 'syndication', $cmdRes->options['syndication']
182             );
183         }
184
185         $req->setDirectUpload($cmdRes->options['direct_upload']);
186         $this->handleFiles($cmdRes, $req);
187
188         if (count($cmdRes->options['x'])) {
189             $postParams = [];
190             foreach ($cmdRes->options['x'] as $xproperty) {
191                 list($propkey, $propval) = explode('=', $xproperty, 2);
192                 if (!isset($postParams[$propkey])) {
193                     $postParams[$propkey] = [];
194                 }
195                 $postParams[$propkey][] = $propval;
196             }
197             foreach ($postParams as $propkey => $propvals) {
198                 $req->addProperty($propkey, $propvals);
199             }
200         }
201     }
202
203     protected function handleOptJson(
204         \Console_CommandLine_Result $cmdRes, Request $req
205     ) {
206         $req->setSendAsJson($cmdRes->options['json']);
207     }
208
209     protected function handleFiles(
210         \Console_CommandLine_Result $cmdRes, Request $req
211     ) {
212         $files = $cmdRes->options['files'];
213         $fileList = $urlList = [
214             'audio' => [],
215             'image' => [],
216             'video' => [],
217         ];
218
219         foreach ($files as $filePath) {
220             if (strpos($filePath, '://') !== false) {
221                 //url
222                 $urlPath  = parse_url($filePath, PHP_URL_PATH);
223                 $mte      = new \MIME_Type_Extension();
224                 $mimetype = $mte->getMIMEType($urlPath);
225                 $media    = \MIME_Type::getMedia($mimetype);
226                 if (!isset($urlList[$media])) {
227                     Log::err('File type not allowed: ' . $mimetype);
228                     exit(20);
229                 }
230                 $urlList[$media][] = $filePath;
231             } else if (file_exists($filePath)) {
232                 //file
233                 $mimetype = \MIME_Type::autoDetect($filePath);
234                 $media    = \MIME_Type::getMedia($mimetype);
235                 if (!isset($urlList[$media])) {
236                     Log::err('File type not allowed: ' . $mimetype);
237                     exit(20);
238                 }
239                 $fileList[$media][] = $filePath;
240             } else {
241                 Log::err('File does not exist: ' . $filePath);
242                 exit(20);
243             }
244         }
245         foreach ($urlList as $type => $urls) {
246             if ($type == 'image') {
247                 $type = 'photo';
248             }
249             if (count($urls) > 0) {
250                 $req->addProperty($type, $urls);
251             }
252         }
253         foreach ($fileList as $type => $filePaths) {
254             if ($type == 'image') {
255                 $type = 'photo';
256             }
257             if (count($filePaths) > 0) {
258                 $req->addUpload($type, $filePaths);
259             }
260         }
261     }
262 }
263 ?>