aceffcfb1e6ffc34cc46c6a74879d8f96db3230d
[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->addArgument(
52             'text',
53             [
54                 'optional'    => false,
55                 'multiple'    => false,
56                 'description' => 'Post text',
57             ]
58         );
59     }
60
61     public function run($command)
62     {
63         $req = new Request($this->cfg->host, $this->cfg);
64         $req->req->addPostParameter('h', 'entry');
65         $req->req->addPostParameter('content', $command->args['text']);
66         if ($command->options['published'] !== null) {
67             $req->req->addPostParameter(
68                 'published', $command->options['published']
69             );
70         }
71         if (count($command->options['categories'])) {
72             $req->req->addPostParameter(
73                 'category', $command->options['categories']
74             );
75         }
76
77         $files = $command->options['files'];
78         $fileList = $urlList = [
79             'audio' => [],
80             'image' => [],
81             'video' => [],
82         ];
83
84         foreach ($files as $filePath) {
85             if (strpos($filePath, '://') !== false) {
86                 //url
87                 $mte      = new \MIME_Type_Extension();
88                 $mimetype = $mte->getMIMEType($filePath);
89                 $media    = \MIME_Type::getMedia($mimetype);
90                 if (!isset($urlList[$media])) {
91                     Log::err('File type not allowed: ' . $mimetype);
92                     exit(20);
93                 }
94                 $urlList[$media][] = $filePath;
95             } else if (file_exists($filePath)) {
96                 //file
97                 $mimetype = \MIME_Type::autoDetect($filePath);
98                 $media    = \MIME_Type::getMedia($mimetype);
99                 if (!isset($urlList[$media])) {
100                     Log::err('File type not allowed: ' . $mimetype);
101                     exit(20);
102                 }
103                 $fileList[$media][] = $filePath;
104             } else {
105                 Log::err('File does not exist: ' . $filePath);
106                 exit(20);
107             }
108         }
109         foreach ($urlList as $type => $urls) {
110             if ($type == 'image') {
111                 $type = 'photo';
112             }
113             if (count($urls) == 1) {
114                 $req->req->addPostParameter($type, reset($urls));
115             } else if (count($urls) > 1) {
116                 $n = 0;
117                 foreach ($urls as $url) {
118                     $req->req->addPostParameter(
119                         $type . '[' . $n++ . ']', reset($urls)
120                     );
121                 }
122             }
123         }
124         foreach ($fileList as $type => $filePaths) {
125             if ($type == 'image') {
126                 $type = 'photo';
127             }
128             if (count($filePaths) == 1) {
129                 $req->addUpload($type, reset($filePaths));
130             } else if (count($filePaths) > 0) {
131                 $req->addUpload($type, $filePaths);
132             }
133         }
134
135         $res = $req->send();
136         $postUrl = $res->getHeader('Location');
137         echo "Post created at server\n";
138         echo $postUrl . "\n";
139     }
140 }
141 ?>