aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/send2kindle/io.php
blob: 284c08cc99f4577937fe6ad91c0d6fec2f53c797 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
/**
 * Simple output message and args prepared
 *
 * @author Jakub Westfalewski <jwest@jwest.pl>
 */
class IO {

    /**
     * Max line height
     */
    const COMMAND_LENGTH = 50;

    /**
     * args for run screen
     * @var array
     */
    protected static $_args = array
    (
        'help' => array('value' => FALSE, 'description' => 'show help for news2kindle'),
        'grab' => array('value' => FALSE, 'description' => 'grab articles from google reader'),
        'mobi' => array('value' => FALSE, 'description' => 'prepare mobi format'),
        'send' => array('value' => FALSE, 'description' => 'send articles to kindle'),
        'login' => array('value' => NULL, 'description' => 'your login to google account *requred'),
        'password' => array('value' => NULL, 'description' => 'your password to google account *required'),
        'kindle' => array('value' => NULL, 'description' => 'your kindle email *required'),
        'items' => array('value' => 100, 'description' => 'max items to grab in run'),        
        'render' => array('value' => 'std', 'description' => 'name of html template for newspapper'),
        //'timeout' => array('value' => FALSE, 'description' => 'timeout is most important than items count'),
    );

    /**
     * Write on screen text line
     * @param string $message
     * @param bool   $broken_line
     */
    public static function msg($message, $broken_line = TRUE)
    {
        echo ( $broken_line ? "\n" : "" ) . $message;
    }

    /**
     * Write command for status
     * @param string $message
     */
    public static function command($message)
    {
        $output_whitespaces = '';

        for ($i = strlen($message); $i <= self::COMMAND_LENGTH; ++$i )
        {
            $output_whitespaces .= '-';
        }

        self::msg( $message . ' ' . $output_whitespaces.' ' );
    }

    /**
     * Status - OK
     */
    public static function ok()
    {
        $colored_string = "\033[1;37m" . "\033[42m" . ' OK ' . "\033[0m";
        self::msg( $colored_string, FALSE );
    }

    /**
     * Status - Error
     */
    public static function error($message = NULL)
    {
        $colored_string = "\033[1;37m" . "\033[41m" . ' ERROR ' . "\033[0m";
        self::msg( $colored_string, FALSE );

        if ( $message !== NULL )
        {
            self::msg( '  - ' . $message );            
        }
    }

    /**
     * Get run args
     * @param string $name key config
     * @return mixed config value
     */
    public static function arg($name)
    {
        return self::$_args[$name]['value'];
    }

    /**
     * Get config from ini
     * @param string $path
     * @return void
     */
    protected static function _get_config($path)
    {
        $config = parse_ini_file( $path . 'config.ini' );

        foreach($config as $key => $value)
        {
            self::$_args[$key]['value'] = $value;
        }
    }

    /**
     * Prepare args for script
     * (from http://php.net/manual/en/features.commandline.php)
     * @param array $argv array
     * @param string $path
     * @return bool success or error
     */
    public static function prepare_args($argv, $path)
    {
        self::command('Parse args');

        self::_get_config($path);

        array_shift($argv);
        $out = array();

        foreach ( $argv as $arg )
        {
            if ( substr($arg,0,2) == '--' )
            {
                $eqPos = strpos($arg,'=');

                if ( $eqPos === false )
                {
                    $key = substr($arg,2);
                    $out[$key] = isset($out[$key]) ? $out[$key] : true;
                } 
                else 
                {
                    $key = substr($arg,2,$eqPos-2);
                    $out[$key] = substr($arg,$eqPos+1);
                }

            } 
            else if ( substr($arg,0,1) == '-' )
            {
                if ( substr($arg,2,1) == '=' )
                {
                    $key = substr($arg,1,1);
                    $out[$key] = substr($arg,3);
                } 
                else 
                {
                    $chars = str_split(substr($arg,1));

                    foreach ( $chars as $char )
                    {
                        $key = $char;
                        $out[$key] = isset($out[$key]) ? $out[$key] : true;
                    }
                }
            } 
            else 
            {
                $out[] = $arg;
            }
        }

        try
        {
            $args = self::_validate_args($out);

            foreach ( $args as $key => $value )
            {
                self::$_args[$key]['value'] = $value;
            }
        }
        catch(Exception $e)
        {
            self::error($e->getMessage());
            return false;
        }

        self::ok();

        return true;
    }

    /**
     * Validate args for script
     * (from http://php.net/manual/en/features.commandline.php)
     * @param array $argv array
     * @return array args
     */
    private static function _validate_args($args)
    {
        if( array_key_exists('help', $args) OR array_key_exists('h', $args) )
        {
            $args['grab'] = false;
            $args['mobi'] = false;
            $args['send'] = false;
            $args['login'] = false;
            $args['password'] = false;
            $args['kindle'] = false;
        }

        foreach ( $args as $key => $arg ) 
        {
            if ( strlen($key) === 1 )
            {
                foreach ( self::$_args as $keyA => $argA )
                {
                    if($keyA[0] === $key )
                    {
                        unset( $args[$key] );
                        $args[$keyA] = $arg;
                        $key = $keyA;
                    }
                }
            }

            if ( ! array_key_exists($key, self::$_args) )
            {
                throw new Exception('Param "'.$key.'" is invalid!');
            }
        }

        foreach ( self::$_args as $key => $arg )
        {
            if( self::$_args[$key]['value'] === NULL AND !array_key_exists($key, $args) )
            {
                throw new Exception('Param "'.$key.'" must be declared!');
            }
        }

        return $args;
    }

    /**
     * Prepare help
     * @return array
     */
    public static function get_help()
    {
        $output = array();

        foreach ( self::$_args as $arg => $item )
        {
            $output[$arg] = $item['description'];
        }

        return $output;
    }

}