This file is indexed.

/usr/share/php/MDB2/Schema/Tool.php is in php-mdb2-schema 0.8.5-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
 * PHP versions 4 and 5
 *
 * Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox,
 * Stig. S. Bakken, Lukas Smith, Igor Feghali
 * All rights reserved.
 *
 * MDB2_Schema enables users to maintain RDBMS independant schema files
 * in XML that can be used to manipulate both data and database schemas
 * This LICENSE is in the BSD license style.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,
 * Lukas Smith, Igor Feghali nor the names of his contributors may be
 * used to endorse or promote products derived from this software
 * without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Author: Christian Weiske <cweiske@php.net>
 * $Id: Tool.php,v 1.6 2008/12/13 00:26:07 clockwerx Exp $
 *
 * @category Database
 * @package  MDB2_Schema
 * @author   Christian Weiske <cweiske@php.net>
 * @license  BSD http://www.opensource.org/licenses/bsd-license.php
 * @version  CVS: $Id: Tool.php,v 1.6 2008/12/13 00:26:07 clockwerx Exp $
 * @link     http://pear.php.net/packages/MDB2_Schema
 */

require_once 'MDB2/Schema.php';
require_once 'MDB2/Schema/Tool/ParameterException.php';

/**
* Command line tool to work with database schemas
*
* Functionality:
* - dump a database schema to stdout
* - import schema into database
* - create a diff between two schemas
* - apply diff to database
*
 * @category Database
 * @package  MDB2_Schema
 * @author   Christian Weiske <cweiske@php.net>
 * @license  BSD http://www.opensource.org/licenses/bsd-license.php
 * @link     http://pear.php.net/packages/MDB2_Schema
 */
class MDB2_Schema_Tool
{
    /**
    * Run the schema tool
    *
    * @param array $args Array of command line arguments
    */
    public function __construct($args)
    {
        $strAction = $this->getAction($args);
        try {
            $this->{'do' . ucfirst($strAction)}($args);
        } catch (MDB2_Schema_Tool_ParameterException $e) {
            $this->{'doHelp' . ucfirst($strAction)}($e->getMessage());
        }
    }//public function __construct($args)



    /**
    * Runs the tool with command line arguments
    *
    * @return void
    */
    public static function run()
    {
        $args = $GLOBALS['argv'];
        array_shift($args);

        try {
            $tool = new MDB2_Schema_Tool($args);
        } catch (Exception $e) {
            self::toStdErr($e->getMessage() . "\n");
        }
    }//public static function run()



    /**
    * Reads the first parameter from the argument array and
    * returns the action.
    *
    * @param array &$args Command line parameters
    *
    * @return string Action to execute
    */
    protected function getAction(&$args)
    {
        if (count($args) == 0) {
            return 'help';
        }
        $arg = array_shift($args);
        switch ($arg) {
        case 'h':
        case 'help':
        case '-h':
        case '--help':
            return 'help';
        case 'd':
        case 'dump':
        case '-d':
        case '--dump':
            return 'dump';
        case 'l':
        case 'load':
        case '-l':
        case '--load':
            return 'load';
        case 'i':
        case 'diff':
        case '-i':
        case '--diff':
            return 'diff';
        case 'a':
        case 'apply':
        case '-a':
        case '--apply':
            return 'apply';
        case 'n':
        case 'init':
        case '-i':
        case '--init':
            return 'init';
        default:
            throw new MDB2_Schema_Tool_ParameterException("Unknown mode \"$arg\"");
        }
    }//protected function getAction(&$args)



    /**
    * Writes the message to stderr
    *
    * @param string $msg Message to print
    *
    * @return void
    */
    protected static function toStdErr($msg)
    {
        file_put_contents('php://stderr', $msg);
    }//protected static function toStdErr($msg)



    /**
    * Displays generic help to stdout
    *
    * @return void
    */
    protected function doHelp()
    {
        self::toStdErr(<<<EOH
Usage: mdb2_schematool mode parameters

Works with database schemas

mode: (- and -- are optional)
 h,  help     Show this help screen
 d,  dump     Dump a schema to stdout
 l,  load     Load a schema into database
 i,  diff     Create a diff between two schemas and dump it to stdout
 a,  apply    Apply a diff to a database
 n,  init     Initialize a database with data

EOH
        );
    }//protected function doHelp()



    /**
    * Displays the help screen for "dump" command
    *
    * @return void
    */
    protected function doHelpDump()
    {
        self::toStdErr( <<<EOH
Usage: mdb2_schematool dump [all|data|schema] [-p] DSN

Dumps a database schema to stdout

If dump type is not specified, defaults to "schema".

DSN: Data source name in the form of
 driver://user:password@host/database

User and password may be omitted.
Using -p reads password from stdin which is more secure than passing it in the parameter.

EOH
        );
    }//protected function doHelpDump()



    /**
    * Displays the help screen for "init" command
    *
    * @return void
    */
    protected function doHelpInit()
    {
        self::toStdErr( <<<EOH
Usage: mdb2_schematool init source [-p] destination

Initializes a database with data
 (Inserts data on a previous created database at destination)

source should be a schema file containing data,
destination should be a DSN

DSN: Data source name in the form of
 driver://user:password@host/database

User and password may be omitted.
Using -p reads password from stdin which is more secure than passing it in the parameter.

EOH
        );
    }//protected function doHelpInit()



    /**
    * Displays the help screen for "load" command
    *
    * @return void
    */
    protected function doHelpLoad()
    {
        self::toStdErr( <<<EOH
Usage: mdb2_schematool load [-p] source [-p] destination

Loads a database schema from source to destination
 (Creates the database schema at destination)

source can be a DSN or a schema file,
destination should be a DSN

DSN: Data source name in the form of
 driver://user:password@host/database

User and password may be omitted.
Using -p reads password from stdin which is more secure than passing it in the parameter.

EOH
        );
    }//protected function doHelpLoad()



    /**
    * Returns an array of options for MDB2_Schema constructor
    *
    * @return array Options for MDB2_Schema constructor
    */
    protected function getSchemaOptions()
    {
        $options = array(
            'log_line_break' => '<br>',
            'idxname_format' => '%s',
            'debug' => true,
            'quote_identifier' => true,
            'force_defaults' => false,
            'portability' => true,
            'use_transactions' => false,
        );
        return $options;
    }//protected function getSchemaOptions()



    /**
    * Checks if the passed parameter is a PEAR_Error object
    * and throws an exception in that case.
    *
    * @param mixed  $object   Some variable to check
    * @param string $location Where the error occured
    *
    * @return void
    */
    protected function throwExceptionOnError($object, $location = '')
    {
        if (PEAR::isError($object)) {
            //FIXME: exception class
            //debug_print_backtrace();
            throw new Exception('Error ' . $location
                . "\n" . $object->getMessage()
                . "\n" . $object->getUserInfo()
            );
        }
    }//protected function throwExceptionOnError($object, $location = '')



    /**
    * Loads a file or a dsn from the arguments
    *
    * @param array &$args Array of arguments to the program
    *
    * @return array Array of ('file'|'dsn', $value)
    */
    protected function getFileOrDsn(&$args)
    {
        if (count($args) == 0) {
            throw new MDB2_Schema_Tool_ParameterException('File or DSN expected');
        }

        $arg = array_shift($args);
        if ($arg == '-p') {
            $bAskPassword = true;
            $arg          = array_shift($args);
        } else {
            $bAskPassword = false;
        }

        if (strpos($arg, '://') === false) {
            if (file_exists($arg)) {
                //File
                return array('file', $arg);
            } else {
                throw new Exception('Schema file does not exist');
            }
        }

        //read password if necessary
        if ($bAskPassword) {
            $password = $this->readPasswordFromStdin($arg);
            $arg      = self::setPasswordIntoDsn($arg, $password);
            self::toStdErr($arg);
        }
        return array('dsn', $arg);
    }//protected function getFileOrDsn(&$args)



    /**
    * Takes a DSN data source name and integrates the given
    * password into it.
    *
    * @param string $dsn      Data source name
    * @param string $password Password
    *
    * @return string DSN with password
    */
    protected function setPasswordIntoDsn($dsn, $password)
    {
        //simple try to integrate password
        if (strpos($dsn, '@') === false) {
            //no @ -> no user and no password
            return str_replace('://', '://:' . $password . '@', $dsn);
        } else if (preg_match('|://[^:]+@|', $dsn)) {
            //user only, no password
            return str_replace('@', ':' . $password . '@', $dsn);
        } else if (strpos($dsn, ':@') !== false) {
            //abstract version
            return str_replace(':@', ':' . $password . '@', $dsn);
        }

        return $dsn;
    }//protected function setPasswordIntoDsn($dsn, $password)



    /**
    * Reads a password from stdin
    *
    * @param string $dsn DSN name to put into the message
    *
    * @return string Password
    */
    protected function readPasswordFromStdin($dsn)
    {
        $stdin = fopen('php://stdin', 'r');
        self::toStdErr('Please insert password for ' . $dsn . "\n");
        $password = '';
        $breakme  = false;
        while (false !== ($char = fgetc($stdin))) {
            if (ord($char) == 10 || $char == "\n" || $char == "\r") {
                break;
            }
            $password .= $char;
        }
        fclose($stdin);

        return trim($password);
    }//protected function readPasswordFromStdin()



    /**
    * Creates a database schema dump and sends it to stdout
    *
    * @param array $args Command line arguments
    *
    * @return void
    */
    protected function doDump($args)
    {
        $dump_what = MDB2_SCHEMA_DUMP_STRUCTURE;
        $arg = '';
        if (count($args)) {
            $arg = $args[0];
        }

        switch (strtolower($arg)) {
        case 'all':
            $dump_what = MDB2_SCHEMA_DUMP_ALL;
            array_shift($args);
            break;
        case 'data':
            $dump_what = MDB2_SCHEMA_DUMP_CONTENT;
            array_shift($args);
            break;
        case 'schema':
            array_shift($args);
        }

        list($type, $dsn) = $this->getFileOrDsn($args);
        if ($type == 'file') {
            throw new MDB2_Schema_Tool_ParameterException(
                'Dumping a schema file as a schema file does not make much sense'
            );
        }

        $schema = MDB2_Schema::factory($dsn, $this->getSchemaOptions());
        $this->throwExceptionOnError($schema);

        $definition = $schema->getDefinitionFromDatabase();
        $this->throwExceptionOnError($definition);


        $dump_options = array(
            'output_mode' => 'file',
            'output' => 'php://stdout',
            'end_of_line' => "\r\n"
        );
        $op = $schema->dumpDatabase(
            $definition, $dump_options, $dump_what
        );
        $this->throwExceptionOnError($op);

        $schema->disconnect();
    }//protected function doDump($args)



    /**
    * Loads a database schema
    *
    * @param array $args Command line arguments
    *
    * @return void
    */
    protected function doLoad($args)
    {
        list($typeSource, $dsnSource) = $this->getFileOrDsn($args);
        list($typeDest,   $dsnDest)   = $this->getFileOrDsn($args);

        if ($typeDest == 'file') {
            throw new MDB2_Schema_Tool_ParameterException(
                'A schema can only be loaded into a database, not a file'
            );
        }


        $schemaDest = MDB2_Schema::factory($dsnDest, $this->getSchemaOptions());
        $this->throwExceptionOnError($schemaDest);

        //load definition
        if ($typeSource == 'file') {
            $definition = $schemaDest->parseDatabaseDefinitionFile($dsnSource);
            $where      = 'loading schema file';
        } else {
            $schemaSource = MDB2_Schema::factory($dsnSource, $this->getSchemaOptions());
            $this->throwExceptionOnError($schemaSource, 'connecting to source database');

            $definition = $schemaSource->getDefinitionFromDatabase();
            $where      = 'loading definition from database';
        }
        $this->throwExceptionOnError($definition, $where);


        //create destination database from definition
        $simulate = false;
        $op       = $schemaDest->createDatabase($definition, array(), $simulate);
        $this->throwExceptionOnError($op, 'creating the database');
    }//protected function doLoad($args)



    /**
    * Initializes a database with data
    *
    * @param array $args Command line arguments
    *
    * @return void
    */
    protected function doInit($args)
    {
        list($typeSource, $dsnSource) = $this->getFileOrDsn($args);
        list($typeDest,   $dsnDest)   = $this->getFileOrDsn($args);

        if ($typeSource != 'file') {
            throw new MDB2_Schema_Tool_ParameterException(
                'Data must come from a source file'
            );
        }

        if ($typeDest != 'dsn') {
            throw new MDB2_Schema_Tool_ParameterException(
                'A schema can only be loaded into a database, not a file'
            );
        }

        $schemaDest = MDB2_Schema::factory($dsnDest, $this->getSchemaOptions());
        $this->throwExceptionOnError($schemaDest, 'connecting to destination database');

        $definition = $schemaDest->getDefinitionFromDatabase();
        $this->throwExceptionOnError($definition, 'loading definition from database');

        $op = $schemaDest->writeInitialization($dsnSource, $definition);
        $this->throwExceptionOnError($op, 'initializing database');
    }//protected function doInit($args)


}//class MDB2_Schema_Tool

?>