]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/send2kindle/io.php
3 * Simple output message and args prepared
5 * @author Jakub Westfalewski <jwest@jwest.pl>
12 const COMMAND_LENGTH
= 50;
18 protected static $_args = array
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'),
33 * Write on screen text line
34 * @param string $message
35 * @param bool $broken_line
37 public static function msg($message, $broken_line = TRUE)
39 echo ( $broken_line ? "\n" : "" ) . $message;
43 * Write command for status
44 * @param string $message
46 public static function command($message)
48 $output_whitespaces = '';
50 for ($i = strlen($message); $i <= self
::COMMAND_LENGTH
; ++
$i )
52 $output_whitespaces .= '-';
55 self
::msg( $message . ' ' . $output_whitespaces.' ' );
61 public static function ok()
63 $colored_string = "\033[1;37m" . "\033[42m" . ' OK ' . "\033[0m";
64 self
::msg( $colored_string, FALSE );
70 public static function error($message = NULL)
72 $colored_string = "\033[1;37m" . "\033[41m" . ' ERROR ' . "\033[0m";
73 self
::msg( $colored_string, FALSE );
75 if ( $message !== NULL )
77 self
::msg( ' - ' . $message );
83 * @param string $name key config
84 * @return mixed config value
86 public static function arg($name)
88 return self
::$_args[$name]['value'];
96 protected static function _get_config($path)
98 $config = parse_ini_file( $path . 'config.ini' );
100 foreach($config as $key => $value)
102 self
::$_args[$key]['value'] = $value;
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
113 public static function prepare_args($argv, $path)
115 self
::command('Parse args');
117 self
::_get_config($path);
122 foreach ( $argv as $arg )
124 if ( substr($arg,0,2) == '--' )
126 $eqPos = strpos($arg,'=');
128 if ( $eqPos === false )
130 $key = substr($arg,2);
131 $out[$key] = isset($out[$key]) ? $out[$key] : true;
135 $key = substr($arg,2,$eqPos-2);
136 $out[$key] = substr($arg,$eqPos+
1);
140 else if ( substr($arg,0,1) == '-' )
142 if ( substr($arg,2,1) == '=' )
144 $key = substr($arg,1,1);
145 $out[$key] = substr($arg,3);
149 $chars = str_split(substr($arg,1));
151 foreach ( $chars as $char )
154 $out[$key] = isset($out[$key]) ? $out[$key] : true;
166 $args = self
::_validate_args($out);
168 foreach ( $args as $key => $value )
170 self
::$_args[$key]['value'] = $value;
175 self
::error($e->getMessage());
185 * Validate args for script
186 * (from http://php.net/manual/en/features.commandline.php)
187 * @param array $argv array
190 private static function _validate_args($args)
192 if( array_key_exists('help', $args) OR array_key_exists('h', $args) )
194 $args['grab'] = false;
195 $args['mobi'] = false;
196 $args['send'] = false;
197 $args['login'] = false;
198 $args['password'] = false;
199 $args['kindle'] = false;
202 foreach ( $args as $key => $arg )
204 if ( strlen($key) === 1 )
206 foreach ( self
::$_args as $keyA => $argA )
208 if($keyA[0] === $key )
210 unset( $args[$key] );
217 if ( ! array_key_exists($key, self
::$_args) )
219 throw new Exception('Param "'.$key.'" is invalid!');
223 foreach ( self
::$_args as $key => $arg )
225 if( self
::$_args[$key]['value'] === NULL AND !array_key_exists($key, $args) )
227 throw new Exception('Param "'.$key.'" must be declared!');
238 public static function get_help()
242 foreach ( self
::$_args as $arg => $item )
244 $output[$arg] = $item['description'];