Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.InstallUtils.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
8 {
9 
10  public static function replace_usage()
11  {
12  return <<<'EOF'
13 
14 Usage:
15 
16  [-f <file1>] ... [-f <fileN>] [+e[pcre_modifier]] <key1> <value1> [-e] [+s] <key2> <value2> [-s] ...
17 
18  -f
19  file to modify
20  +e[pcre_modifier]
21  activate regex matching for keys with optional PCRE modifiers
22  -e
23  disable regex matching (plain matching)(default)
24  +s
25  activate safe php string replacement
26  -s
27  disable safe php string replacement
28  +q
29  when using +s, add apostrophe around the string (default)
30  -q
31  when using +s, do not add apostrophe around the string
32  -R
33  reset flags to defaults
34 
35 
36 EOF;
37 
38 
39  }
40  public static function replace($argv)
41  {
42  $vars = array();
43  $files = array();
44  /* flags */
45  $regex = false;
46  $php_string = false;
47  $quote = true;
48  $end = false;
49  while (count($argv) > 0) {
50  $opt = array_shift($argv);
51  if (!$end && $opt == '-R') {
52  /* Reset flags to defaults */
53  $regex = false;
54  $php_string = false;
55  $quote = true;
56  $end = false;
57  } elseif (!$end && $opt == '+s') {
58  $php_string = true;
59  } elseif (!$end && $opt == '-s') {
60  $php_string = false;
61  } elseif (!$end && $opt == '+q') {
62  $quote = true;
63  } elseif (!$end && $opt == '-q') {
64  $quote = false;
65  } elseif (!$end && substr($opt, 0, 2) == '+e') {
66  $regex = substr($opt, 2);
67  if ($regex === false) {
68  $regex = '';
69  }
70  } elseif (!$end && $opt == '-e') {
71  $regex = false;
72  } else if (!$end && $opt == '--') {
73  $end = true;
74  } elseif (!$end && $opt == '-f') {
75  $file = array_shift($argv);
76  if ($file === null) {
77  throw new Exception(sprintf("Missing file after '-f' option.\n%s", self::replace_usage()));
78  }
79  $files[] = $file;
80  } else {
81  $value = array_shift($argv);
82  if ($value === null) {
83  throw new Exception(sprintf("Missing value for variable '%s'.\n%s", $opt, self::replace_usage()));
84  }
85  $vars[] = array(
86  'key' => $opt,
87  'value' => $value,
88  'regex' => $regex,
89  'php_string' => $php_string,
90  'quote' => $quote
91  );
92  }
93  }
94  foreach ($files as $file) {
95  $content = file_get_contents($file);
96  if ($content === false) {
97  throw new Exception(sprintf("Error reading from file '%s'.", $file));
98  }
99  foreach ($vars as $var) {
100  $modifier = '';
101  if ($var['regex'] === false) {
102  $var['key'] = preg_quote($var['key'], "/");
103  } else {
104  $modifier = $var['regex'];
105  }
106  if ($var['php_string']) {
107  $var['value'] = var_export($var['value'], true);
108  if (!$var['quote']) {
109  $var['value'] = substr($var['value'], 1, -1);
110  }
111  }
112 
113  $content = preg_replace('/' . $var['key'] . '/' . $modifier, $var['value'], $content);
114  if ($content === false) {
115  throw new Exception(sprintf("Error in regex '/%s/'.", $var['key']));
116  }
117  }
118  if (file_put_contents($file, $content) === false) {
119  throw new Exception(sprintf("Error writing to file '%s'.", $file));
120  }
121  }
122  return 0;
123  }
124  public static function doublequote_usage()
125  {
126  return <<<'EOF'
127 
128 Usage:
129 
130  [<string1>] [-b] [<string2>] [<string3>] +b [<stringN>]
131 
132 Returns the string escaped and enclosed between quotes (").
133 
134  +b
135  Enable backslash escaping (default)
136  -b
137  Disable backslash escaping
138  +q
139  Enable surrounding double-quotes (")(default)
140  -q
141  Disable surrounding double-quotes (")
142 
143 
144 EOF;
145 
146 
147  }
148  public static function doublequote($argv)
149  {
150  $strings = array();
151  $backslash = true;
152  $quote = true;
153  $end = false;
154  while (count($argv) > 0) {
155  $opt = array_shift($argv);
156  if (!$end && $opt == '+b') {
157  $backslash = true;
158  } elseif (!$end && $opt == '-b') {
159  $backslash = false;
160  } elseif (!$end && $opt == '+q') {
161  $quote = true;
162  } elseif (!$end && $opt == '-q') {
163  $quote = false;
164  } else if (!$end && $opt == '--') {
165  $end = true;
166  } else {
167  $strings[] = array(
168  'value' => $opt,
169  'backslash' => $backslash,
170  'quote' => $quote
171  );
172  }
173  }
174  foreach ($strings as $string) {
175  if ($string['backslash']) {
176  $string['value'] = str_replace(array(
177  "\""
178  ) , array(
179  "\\\""
180  ) , $string['value']);
181  }
182  $string['value'] = str_replace(array(
183  "\""
184  ) , array(
185  "\\\""
186  ) , $string['value']);
187  if ($string['quote']) {
188  $string['value'] = '"' . $string['value'] . '"';
189  }
190  print ($string['value'] . PHP_EOL);
191  }
192  }
193  public static function pg_escape_string_usage()
194  {
195  return <<<'EOF'
196 
197 Usage:
198 
199  [<string1>]
200 
201 Returns the string escaped for inclusion between apostrophes.
202 
203 EOF;
204 
205 
206  }
207  public static function pg_escape_string($argv)
208  {
209  $strings = array();
210  $end = false;
211  while (count($argv) > 0) {
212  $opt = array_shift($argv);
213  if (!$end && $opt == '--') {
214  $end = true;
215  } else {
216  $strings[] = array(
217  'value' => $opt,
218  );
219  }
220  }
221  foreach ($strings as $string) {
222  printf('%s' . PHP_EOL, pg_escape_string($string['value']));
223  }
224  }
225 
226  public static function _call($argv)
227  {
228  $function = array_shift($argv);
229  if ($function === null) {
230  throw new Exception(sprintf("Missing function name."));
231  }
232  if (!method_exists(__CLASS__, $function)) {
233  throw new Exception(sprintf("Function '%s' does not exists.", $function));
234  }
235  $function = __CLASS__ . '::' . $function;
236  return call_user_func($function, $argv);
237  }
238 }
239 
240 if (basename(__FILE__) == basename($argv[0])) {
241  array_shift($argv);
242  $ret = InstallUtils::_call($argv);
243  exit(($ret === false) ? 255 : $ret);
244 }
$ret
static pg_escape_string($argv)
$file
static pg_escape_string_usage()
static replace_usage()
static replace($argv)
static doublequote_usage()
static _call($argv)
print
Definition: checklist.php:49
switch($command) exit
Definition: checkVault.php:46
$value
← centre documentaire © anakeen