6fb7ceca34d0def575b79756ee9a7e3e1f369d4d
[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             'x',
53             array(
54                 'short_name'  => '-x',
55                 'long_name'   => '--xprop',
56                 'description' => 'Additional property',
57                 'help_name'   => 'key=value',
58                 'action'      => 'StoreArray',
59                 'default'     => [],
60             )
61         );
62         $cmd->addArgument(
63             'text',
64             [
65                 'optional'    => false,
66                 'multiple'    => false,
67                 'description' => 'Post text',
68             ]
69         );
70     }
71
72     public function run($command)
73     {
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']
80             );
81         }
82         if (count($command->options['categories'])) {
83             $req->req->addPostParameter(
84                 'category', $command->options['categories']
85             );
86         }
87
88         $files = $command->options['files'];
89         $fileList = $urlList = [
90             'audio' => [],
91             'image' => [],
92             'video' => [],
93         ];
94
95         foreach ($files as $filePath) {
96             if (strpos($filePath, '://') !== false) {
97                 //url
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);
103                     exit(20);
104                 }
105                 $urlList[$media][] = $filePath;
106             } else if (file_exists($filePath)) {
107                 //file
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);
112                     exit(20);
113                 }
114                 $fileList[$media][] = $filePath;
115             } else {
116                 Log::err('File does not exist: ' . $filePath);
117                 exit(20);
118             }
119         }
120         foreach ($urlList as $type => $urls) {
121             if ($type == 'image') {
122                 $type = 'photo';
123             }
124             if (count($urls) == 1) {
125                 $req->req->addPostParameter($type, reset($urls));
126             } else if (count($urls) > 1) {
127                 $n = 0;
128                 foreach ($urls as $url) {
129                     $req->req->addPostParameter(
130                         $type . '[' . $n++ . ']', reset($urls)
131                     );
132                 }
133             }
134         }
135         foreach ($fileList as $type => $filePaths) {
136             if ($type == 'image') {
137                 $type = 'photo';
138             }
139             if (count($filePaths) == 1) {
140                 $req->addUpload($type, reset($filePaths));
141             } else if (count($filePaths) > 0) {
142                 $req->addUpload($type, $filePaths);
143             }
144         }
145
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);
150             }
151         }
152
153         $res = $req->send();
154         $postUrl = $res->getHeader('Location');
155         echo "Post created at server\n";
156         echo $postUrl . "\n";
157     }
158 }
159 ?>