d5231a9d62160fa5723d47ec61391273a20cdf2b
[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 = [
63             'audio' => [],
64             'photo' => [],
65             'video' => [],
66         ];
67         $urls = [];
68         foreach ($files as $filePath) {
69             if (file_exists($filePath)) {
70                 $type = 'photo';
71                 $fileList[$type][] = $filePath;
72             } else if (strpos($filePath, '://') !== false) {
73                 //url
74                 $urls[] = $filePath;
75             } else {
76                 Log::err('File does not exist: ' . $filePath);
77                 exit(20);
78             }
79         }
80         if (count($urls)) {
81             if (count($urls) == 1) {
82                 $req->req->addPostParameter('photo', reset($urls));
83             } else {
84                 $n = 0;
85                 foreach ($urls as $url) {
86                     $req->req->addPostParameter(
87                         'photo[' . $n++ . ']', reset($urls)
88                     );
89                 }
90             }
91         }
92         foreach ($fileList as $type => $filePaths) {
93             if (count($filePaths) == 1) {
94                 $req->addUpload($type, reset($filePaths));
95             } else if (count($filePaths) > 0) {
96                 $req->addUpload($type, $filePaths);
97             }
98         }
99
100         $res = $req->send();
101         $postUrl = $res->getHeader('Location');
102         echo "Post created at server\n";
103         echo $postUrl . "\n";
104     }
105 }
106 ?>