b5df8ba6fe001a1f17353b2d1df672b82c135a12
[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             'files',
21             array(
22                 'short_name'  => '-f',
23                 'long_name'   => '--files',
24                 'description' => 'Files to upload',
25                 'help_name'   => 'PATH',
26                 'action'      => 'StoreArray',
27                 'default'     => [],
28             )
29         );
30         $cmd->addOption(
31             'published',
32             array(
33                 'long_name'   => '--published',
34                 'description' => 'Publish date',
35                 'help_name'   => 'DATE',
36                 'action'      => 'StoreString',
37                 'default'     => null,
38             )
39         );
40         $cmd->addArgument(
41             'text',
42             [
43                 'optional'    => false,
44                 'multiple'    => false,
45                 'description' => 'Post text',
46             ]
47         );
48     }
49
50     public function run($command)
51     {
52         $req = new Request($this->cfg->host, $this->cfg);
53         $req->req->addPostParameter('h', 'entry');
54         $req->req->addPostParameter('content', $command->args['text']);
55         if ($command->options['published'] !== null) {
56             $req->req->addPostParameter(
57                 'published', $command->options['published']
58             );
59         }
60
61         $files = $command->options['files'];
62         $fileList = $urlList = [
63             'audio' => [],
64             'image' => [],
65             'video' => [],
66         ];
67
68         foreach ($files as $filePath) {
69             if (strpos($filePath, '://') !== false) {
70                 //url
71                 $mte      = new \MIME_Type_Extension();
72                 $mimetype = $mte->getMIMEType($filePath);
73                 $media    = \MIME_Type::getMedia($mimetype);
74                 if (!isset($urlList[$media])) {
75                     Log::err('File type not allowed: ' . $mimetype);
76                     exit(20);
77                 }
78                 $urlList[$media][] = $filePath;
79             } else if (file_exists($filePath)) {
80                 //file
81                 $mimetype = \MIME_Type::autoDetect($filePath);
82                 $media    = \MIME_Type::getMedia($mimetype);
83                 if (!isset($urlList[$media])) {
84                     Log::err('File type not allowed: ' . $mimetype);
85                     exit(20);
86                 }
87                 $fileList[$media][] = $filePath;
88             } else {
89                 Log::err('File does not exist: ' . $filePath);
90                 exit(20);
91             }
92         }
93         foreach ($urlList as $type => $urls) {
94             if ($type == 'image') {
95                 $type = 'photo';
96             }
97             if (count($urls) == 1) {
98                 $req->req->addPostParameter($type, reset($urls));
99             } else if (count($urls) > 1) {
100                 $n = 0;
101                 foreach ($urls as $url) {
102                     $req->req->addPostParameter(
103                         $type . '[' . $n++ . ']', reset($urls)
104                     );
105                 }
106             }
107         }
108         foreach ($fileList as $type => $filePaths) {
109             if ($type == 'image') {
110                 $type = 'photo';
111             }
112             if (count($filePaths) == 1) {
113                 $req->addUpload($type, reset($filePaths));
114             } else if (count($filePaths) > 0) {
115                 $req->addUpload($type, $filePaths);
116             }
117         }
118
119         $res = $req->send();
120         $postUrl = $res->getHeader('Location');
121         echo "Post created at server\n";
122         echo $postUrl . "\n";
123     }
124 }
125 ?>