aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/libraries/send2kindle/io.php
diff options
context:
space:
mode:
Diffstat (limited to 'inc/3rdparty/libraries/send2kindle/io.php')
-rw-r--r--inc/3rdparty/libraries/send2kindle/io.php250
1 files changed, 250 insertions, 0 deletions
diff --git a/inc/3rdparty/libraries/send2kindle/io.php b/inc/3rdparty/libraries/send2kindle/io.php
new file mode 100644
index 00000000..284c08cc
--- /dev/null
+++ b/inc/3rdparty/libraries/send2kindle/io.php
@@ -0,0 +1,250 @@
1<?php
2/**
3 * Simple output message and args prepared
4 *
5 * @author Jakub Westfalewski <jwest@jwest.pl>
6 */
7class IO {
8
9 /**
10 * Max line height
11 */
12 const COMMAND_LENGTH = 50;
13
14 /**
15 * args for run screen
16 * @var array
17 */
18 protected static $_args = array
19 (
20 'help' => array('value' => FALSE, 'description' => 'show help for news2kindle'),
21 'grab' => array('value' => FALSE, 'description' => 'grab articles from google reader'),
22 'mobi' => array('value' => FALSE, 'description' => 'prepare mobi format'),
23 'send' => array('value' => FALSE, 'description' => 'send articles to kindle'),
24 'login' => array('value' => NULL, 'description' => 'your login to google account *requred'),
25 'password' => array('value' => NULL, 'description' => 'your password to google account *required'),
26 'kindle' => array('value' => NULL, 'description' => 'your kindle email *required'),
27 'items' => array('value' => 100, 'description' => 'max items to grab in run'),
28 'render' => array('value' => 'std', 'description' => 'name of html template for newspapper'),
29 //'timeout' => array('value' => FALSE, 'description' => 'timeout is most important than items count'),
30 );
31
32 /**
33 * Write on screen text line
34 * @param string $message
35 * @param bool $broken_line
36 */
37 public static function msg($message, $broken_line = TRUE)
38 {
39 echo ( $broken_line ? "\n" : "" ) . $message;
40 }
41
42 /**
43 * Write command for status
44 * @param string $message
45 */
46 public static function command($message)
47 {
48 $output_whitespaces = '';
49
50 for ($i = strlen($message); $i <= self::COMMAND_LENGTH; ++$i )
51 {
52 $output_whitespaces .= '-';
53 }
54
55 self::msg( $message . ' ' . $output_whitespaces.' ' );
56 }
57
58 /**
59 * Status - OK
60 */
61 public static function ok()
62 {
63 $colored_string = "\033[1;37m" . "\033[42m" . ' OK ' . "\033[0m";
64 self::msg( $colored_string, FALSE );
65 }
66
67 /**
68 * Status - Error
69 */
70 public static function error($message = NULL)
71 {
72 $colored_string = "\033[1;37m" . "\033[41m" . ' ERROR ' . "\033[0m";
73 self::msg( $colored_string, FALSE );
74
75 if ( $message !== NULL )
76 {
77 self::msg( ' - ' . $message );
78 }
79 }
80
81 /**
82 * Get run args
83 * @param string $name key config
84 * @return mixed config value
85 */
86 public static function arg($name)
87 {
88 return self::$_args[$name]['value'];
89 }
90
91 /**
92 * Get config from ini
93 * @param string $path
94 * @return void
95 */
96 protected static function _get_config($path)
97 {
98 $config = parse_ini_file( $path . 'config.ini' );
99
100 foreach($config as $key => $value)
101 {
102 self::$_args[$key]['value'] = $value;
103 }
104 }
105
106 /**
107 * Prepare args for script
108 * (from http://php.net/manual/en/features.commandline.php)
109 * @param array $argv array
110 * @param string $path
111 * @return bool success or error
112 */
113 public static function prepare_args($argv, $path)
114 {
115 self::command('Parse args');
116
117 self::_get_config($path);
118
119 array_shift($argv);
120 $out = array();
121
122 foreach ( $argv as $arg )
123 {
124 if ( substr($arg,0,2) == '--' )
125 {
126 $eqPos = strpos($arg,'=');
127
128 if ( $eqPos === false )
129 {
130 $key = substr($arg,2);
131 $out[$key] = isset($out[$key]) ? $out[$key] : true;
132 }
133 else
134 {
135 $key = substr($arg,2,$eqPos-2);
136 $out[$key] = substr($arg,$eqPos+1);
137 }
138
139 }
140 else if ( substr($arg,0,1) == '-' )
141 {
142 if ( substr($arg,2,1) == '=' )
143 {
144 $key = substr($arg,1,1);
145 $out[$key] = substr($arg,3);
146 }
147 else
148 {
149 $chars = str_split(substr($arg,1));
150
151 foreach ( $chars as $char )
152 {
153 $key = $char;
154 $out[$key] = isset($out[$key]) ? $out[$key] : true;
155 }
156 }
157 }
158 else
159 {
160 $out[] = $arg;
161 }
162 }
163
164 try
165 {
166 $args = self::_validate_args($out);
167
168 foreach ( $args as $key => $value )
169 {
170 self::$_args[$key]['value'] = $value;
171 }
172 }
173 catch(Exception $e)
174 {
175 self::error($e->getMessage());
176 return false;
177 }
178
179 self::ok();
180
181 return true;
182 }
183
184 /**
185 * Validate args for script
186 * (from http://php.net/manual/en/features.commandline.php)
187 * @param array $argv array
188 * @return array args
189 */
190 private static function _validate_args($args)
191 {
192 if( array_key_exists('help', $args) OR array_key_exists('h', $args) )
193 {
194 $args['grab'] = false;
195 $args['mobi'] = false;
196 $args['send'] = false;
197 $args['login'] = false;
198 $args['password'] = false;
199 $args['kindle'] = false;
200 }
201
202 foreach ( $args as $key => $arg )
203 {
204 if ( strlen($key) === 1 )
205 {
206 foreach ( self::$_args as $keyA => $argA )
207 {
208 if($keyA[0] === $key )
209 {
210 unset( $args[$key] );
211 $args[$keyA] = $arg;
212 $key = $keyA;
213 }
214 }
215 }
216
217 if ( ! array_key_exists($key, self::$_args) )
218 {
219 throw new Exception('Param "'.$key.'" is invalid!');
220 }
221 }
222
223 foreach ( self::$_args as $key => $arg )
224 {
225 if( self::$_args[$key]['value'] === NULL AND !array_key_exists($key, $args) )
226 {
227 throw new Exception('Param "'.$key.'" must be declared!');
228 }
229 }
230
231 return $args;
232 }
233
234 /**
235 * Prepare help
236 * @return array
237 */
238 public static function get_help()
239 {
240 $output = array();
241
242 foreach ( self::$_args as $arg => $item )
243 {
244 $output[$arg] = $item['description'];
245 }
246
247 return $output;
248 }
249
250} \ No newline at end of file