0184c10b5f840be15a90dbd9f8ff37ab312154b1
[shpub.git] / src / shpub / Command / AbstractProps.php
1 <?php
2 namespace shpub;
3
4 /**
5  * Abstract command class that handles generic properties
6  */
7 class Command_AbstractProps
8 {
9     /**
10      * @var Config
11      */
12     protected $cfg;
13
14     public function __construct($cfg)
15     {
16         $this->cfg = $cfg;
17     }
18
19     public static function optsGeneric(\Console_CommandLine_Command $cmd)
20     {
21         $cmd->addOption(
22             'categories',
23             array(
24                 'short_name'  => '-c',
25                 'long_name'   => '--category',
26                 'description' => 'Category names',
27                 'help_name'   => 'CAT',
28                 'action'      => 'StoreArray',
29                 'default'     => [],
30             )
31         );
32         $cmd->addOption(
33             'files',
34             array(
35                 'short_name'  => '-f',
36                 'long_name'   => '--files',
37                 'description' => 'Files or URLs to upload',
38                 'help_name'   => 'PATH',
39                 'action'      => 'StoreArray',
40                 'default'     => [],
41             )
42         );
43         $cmd->addOption(
44             'name',
45             array(
46                 'short_name'  => '-n',
47                 'long_name'   => '--name',
48                 'description' => 'Post title',
49                 'help_name'   => 'TITLE',
50                 'action'      => 'StoreString',
51                 'default'     => null,
52             )
53         );
54         $cmd->addOption(
55             'published',
56             array(
57                 'long_name'   => '--published',
58                 'description' => 'Publish date',
59                 'help_name'   => 'DATE',
60                 'action'      => 'StoreString',
61                 'default'     => null,
62             )
63         );
64         $cmd->addOption(
65             'updated',
66             array(
67                 'long_name'   => '--updated',
68                 'description' => 'Update date',
69                 'help_name'   => 'DATE',
70                 'action'      => 'StoreString',
71                 'default'     => null,
72             )
73         );
74         $cmd->addOption(
75             'slug',
76             array(
77                 'short_name'  => '-s',
78                 'long_name'   => '--slug',
79                 'description' => 'URL path',
80                 'help_name'   => 'PATH',
81                 'action'      => 'StoreString',
82                 'default'     => null,
83             )
84         );
85         $cmd->addOption(
86             'syndication',
87             array(
88                 'short_name'  => '-s',
89                 'long_name'   => '--syndication',
90                 'description' => 'Syndication URL(s)',
91                 'help_name'   => 'URL',
92                 'action'      => 'StoreArray',
93                 'default'     => [],
94             )
95         );
96         $cmd->addOption(
97             'x',
98             array(
99                 'short_name'  => '-x',
100                 'long_name'   => '--xprop',
101                 'description' => 'Additional property',
102                 'help_name'   => 'key=value',
103                 'action'      => 'StoreArray',
104                 'default'     => [],
105             )
106         );
107     }
108
109     protected function handleGenericOptions(
110         \Console_CommandLine_Result $cmdRes, Request $req
111     ) {
112         if ($cmdRes->options['published'] !== null) {
113             $req->req->addPostParameter(
114                 'published', $cmdRes->options['published']
115             );
116         }
117         if ($cmdRes->options['updated'] !== null) {
118             $req->req->addPostParameter(
119                 'updated', $cmdRes->options['updated']
120             );
121         }
122         if (count($cmdRes->options['categories'])) {
123             $req->addPostParameter(
124                 'category', $cmdRes->options['categories']
125             );
126         }
127         if ($cmdRes->options['name'] !== null) {
128             $req->req->addPostParameter(
129                 'name', $cmdRes->options['name']
130             );
131         }
132         if ($cmdRes->options['slug'] !== null) {
133             $req->req->addPostParameter(
134                 'slug', $cmdRes->options['slug']
135             );
136         }
137         if (count($cmdRes->options['syndication'])) {
138             $req->addPostParameter(
139                 'syndication', $cmdRes->options['syndication']
140             );
141         }
142
143         $this->handleFiles($cmdRes, $req);
144
145         if (count($cmdRes->options['x'])) {
146             $postParams = [];
147             foreach ($cmdRes->options['x'] as $xproperty) {
148                 list($propkey, $propval) = explode('=', $xproperty, 2);
149                 if (!isset($postParams[$propkey] )) {
150                     $postParams[$propkey] = [];
151                 }
152                 $postParams[$propkey][] = $propval;
153             }
154             foreach ($postParams as $propkey => $propvals) {
155                 $req->addPostParameter($propkey, $propvals);
156             }
157         }
158     }
159
160     protected function handleFiles(
161         \Console_CommandLine_Result $cmdRes, Request $req
162     ) {
163         $files = $cmdRes->options['files'];
164         $fileList = $urlList = [
165             'audio' => [],
166             'image' => [],
167             'video' => [],
168         ];
169
170         foreach ($files as $filePath) {
171             if (strpos($filePath, '://') !== false) {
172                 //url
173                 $mte      = new \MIME_Type_Extension();
174                 $mimetype = $mte->getMIMEType($filePath);
175                 $media    = \MIME_Type::getMedia($mimetype);
176                 if (!isset($urlList[$media])) {
177                     Log::err('File type not allowed: ' . $mimetype);
178                     exit(20);
179                 }
180                 $urlList[$media][] = $filePath;
181             } else if (file_exists($filePath)) {
182                 //file
183                 $mimetype = \MIME_Type::autoDetect($filePath);
184                 $media    = \MIME_Type::getMedia($mimetype);
185                 if (!isset($urlList[$media])) {
186                     Log::err('File type not allowed: ' . $mimetype);
187                     exit(20);
188                 }
189                 $fileList[$media][] = $filePath;
190             } else {
191                 Log::err('File does not exist: ' . $filePath);
192                 exit(20);
193             }
194         }
195         foreach ($urlList as $type => $urls) {
196             if ($type == 'image') {
197                 $type = 'photo';
198             }
199             if (count($urls) == 1) {
200                 $req->req->addPostParameter($type, reset($urls));
201             } else if (count($urls) > 1) {
202                 $n = 0;
203                 foreach ($urls as $url) {
204                     $req->req->addPostParameter(
205                         $type . '[' . $n++ . ']', $url
206                     );
207                 }
208             }
209         }
210         foreach ($fileList as $type => $filePaths) {
211             if ($type == 'image') {
212                 $type = 'photo';
213             }
214             if (count($filePaths) == 1) {
215                 $req->addUpload($type, reset($filePaths));
216             } else if (count($filePaths) > 0) {
217                 $req->addUpload($type, $filePaths);
218             }
219         }
220     }
221 }
222 ?>