C:\xampp2_not used\php\tests\parseFile\PackageUpdate.php


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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
<?php
/**
 * A package to make adding self updating functionality to other
 * packages easy.
 *
 * PHP versions 4 and 5
 *
 * LICENSE: This source file is subject to version 3.01 of the PHP license
 * that is available through the world-wide-web at the following URI:
 * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
 * the PHP License and are unable to obtain it through the web, please
 * send a note to license@php.net so we can mail you a copy immediately.
 *
 * @category   PEAR
 * @package    PEAR_PackageUpdate
 * @author     Scott Mattocks
 * @copyright  2006 Scott Mattocks
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
 * @version    CVS: $Id: PackageUpdate.php,v 1.1 2008/02/02 15:09:40 farell Exp $
 * @link       http://pear.php.net/package/PEAR_PackageUpdate
 * @since      File available since Release 0.4.0a1
 */

require_once 'PEAR/ErrorStack.php';
require_once 
'PEAR/Config.php';

// Constants for preferences.
define('PEAR_PACKAGEUPDATE_PREF_NOUPDATES',   0);
define('PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE'1);
define('PEAR_PACKAGEUPDATE_PREF_TYPE',        2);
define('PEAR_PACKAGEUPDATE_PREF_STATE',       3);

// Constants for states.
define('PEAR_PACKAGEUPDATE_STATE_DEVEL',  'devel');
define('PEAR_PACKAGEUPDATE_STATE_ALPHA',  'alpha');
define('PEAR_PACKAGEUPDATE_STATE_BETA',   'beta');
define('PEAR_PACKAGEUPDATE_STATE_STABLE''stable');

// Constants for release types.
define('PEAR_PACKAGEUPDATE_TYPE_BUG',   'bug');
define('PEAR_PACKAGEUPDATE_TYPE_MINOR''minor');
define('PEAR_PACKAGEUPDATE_TYPE_MAJOR''major');

// Constants for errors.
define('PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE',            -1);
define('PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL',            -2);
define('PEAR_PACKAGEUPDATE_ERROR_NOINFO',               -3);
define('PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED',         -4);
define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS',  -5);
define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS', -6);
define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR',  -7);
define('PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED',   -8);
define('PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPE',          -9);
define('PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE',         -10);
define('PEAR_PACKAGEUPDATE_ERROR_INVALIDPREF',          -11);
define('PEAR_PACKAGEUPDATE_ERROR_NONEXISTENTDRIVER',    -12);

// Error messages.
$GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'] =
    array(
        
PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE =>
            
'No package name provided',
        
PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL =>
            
'No channel name provided',
        
PEAR_PACKAGEUPDATE_ERROR_NOINFO =>
            
'No update information is available for %packagename%.',
        
PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED =>
            
'%packagename% is not installed. It cannot be updated.',
        
PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS =>
            
'Preferences cannot be read from %file%.',
        
PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS =>
            
'Preferences cannot be written to %file% because of access permission errors.',
        
PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR =>
            
'An error occurred while trying to write the preferences to %file%.',
        
PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED =>
            
'Preferences file is corrupted.',
        
PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPE =>
            
'Invalid release type: %type%',
        
PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE =>
            
'Invalid release state: %state%',
        
PEAR_PACKAGEUPDATE_ERROR_INVALIDPREF =>
            
'Invalid preference: %preference%',
        
PEAR_PACKAGEUPDATE_ERROR_NONEXISTENTDRIVER =>
            
'Driver %drivername% could not be found.'
    
);

/**
 * The package allows a developer to add a few lines of code to
 * their existing packages and have the ability to update their
 * package automatically. This auto-update ability allows users
 * to stay up to date much more easily than requiring them to
 * update manually.
 *
 * This package keeps track of user preferences such as "don't
 * remind me again".
 *
 * This package is designed to be a backend to different front
 * ends written for this package. For example, this package can
 * be used to drive a PHP-GTK 2, CLI or web front end. The API
 * for this package should be flexible enough to allow any type
 * of front end to be used and also to allow the package to be
 * used directly in another package without a front end driver.
 *
 * The interface for this package must allow for the following
 * functionality:
 * - check to see if a new version is available for a given
 *   package on a given channel
 *   - check minimum state
 * - present information regarding the upgrade (version, size)
 *   - inform user about dependencies
 * - allow user to confirm or cancel upgrade
 * - download and install the package
 * - track preferences on a per package basis
 *   - don't ask again
 *   - don't ask until next release
 *   - only ask for state XXXX or higher
 *   - bug/minor/major updates only
 * - update channel automatically
 * - force application to exit when upgrade complete
 *   - PHP-GTK/CLI apps must exit to allow classes to reload
 *   - web front end could send headers to reload certain page
 *
 * This class is simply a wrapper for PEAR classes that actually
 * do the work.
 *
 * EXAMPLE:
 * <code>
 * <?php
 *  class Goo {
 *      function __construct()
 *      {
 *          // Check for updates...
 *          require_once 'PEAR/PackageUpdate.php';
 *          $ppu =& PEAR_PackageUpdate::factory('Gtk2', 'Goo', 'pear');
 *          if ($ppu !== false) {
 *              if ($ppu->checkUpdate()) {
 *                  // Use a dialog window to ask permission to update.
 *                  if ($ppu->presentUpdate()) {
 *                      if ($ppu->update()) {
 *                          // If the update succeeded, the application should
 *                          // be restarted.
 *                          $ppu->forceRestart();
 *                      }
 *                  }
 *              }
 *          }
 *          // ...
 *      }
 *      // ...
 *  }
 * ?>
 * </code>
 *
 * @category   PEAR
 * @package    PEAR_PackageUpdate
 * @author     Scott Mattocks
 * @copyright  2006 Scott Mattocks
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
 * @version    Release: 0.5.0
 * @link       http://pear.php.net/package/PEAR_PackageUpdate
 * @since      Class available since Release 0.4.0a1
 */

class PEAR_PackageUpdate
{
    
/**
     * The user's update preferences.
     *
     * @access public
     * @var    array
     * @since  0.4.0a1
     */
    
var $preferences = array();

    
/**
     * The name of the package.
     *
     * @access public
     * @var    string
     * @since  0.4.0a1
     */
    
var $packageName;

    
/**
     * The channel the package is hosted on.
     *
     * @access public
     * @var    string
     * @since  0.4.0a1
     */
    
var $channel;

    
/**
     * The latest version available for the given package.
     *
     * @access public
     * @var    string
     * @since  0.4.0a1
     */
    
var $latestVersion;

    
/**
     * Information about the latest version of the package.
     *
     * @access public
     * @var    array
     * @since  0.4.0a1
     */
    
var $info = array();

    
/**
     * The current installed version of the package.
     *
     * @access public
     * @var    string
     * @since  0.4.0a1
     */
    
var $instVersion;

    
/**
     * A collection of errors that have occurred.
     *
     * @access public
     * @var    object
     * @since  0.4.0a1
     */
    
var $errors;

    
/**
     * PHP 4 style constructor. Calls the PHP 5 style constructor.
     *
     * @access public
     * @param  string $packageName The package to update.
     * @param  string $channel     The channel the package resides on.
     * @return void
     * @since  0.4.0a1
     */
    
function PEAR_PackageUpdate($packageName$channel)
    {
        
PEAR_PackageUpdate::__construct($packageName$channel);
    }

    
/**
     * PHP 5 style constructor. Loads the user preferences.
     *
     * @access public
     * @param  string $packageName The package to update.
     * @param  string $channel     The channel the package resides on.
     * @return void
     * @since  0.4.0a1
     */
    
function __construct($packageName$channel)
    {
        
// Create a pear error stack.
        
$this->errors =& PEAR_ErrorStack::singleton(get_class($this));
        
$this->errors->setContextCallback(array(&$this'_getBacktrace'));

        
// Set the package name and channel.
        
$this->packageName $packageName;
        
$this->channel     $channel;

        
// Load the user's preferences.
        
$this->loadPreferences();
    }

    
/**
     * Callback that generates context information (location of error)
     * for the package error stack.
     *
     * @access private
     * @return mixed  backtrace context array or false is unavailable
     * @since  0.4.3
     */
    
function _getBacktrace()
    {
        if (
function_exists('debug_backtrace')) {
            
$backtrace debug_backtrace();
            
$backtrace $backtrace[count($backtrace)-1];
        } else {
            
$backtrace false;
        }
        return 
$backtrace;
    }

    
/**
     * Creates an instance of the given update class.
     *
     * @access public
     * @param  string $driver The type of PPU to create.
     * @param  string $packageName The package to update.
     * @param  string $channel     The channel the package resides on.
     * @return object An instance of type PEAR_PackageUpdate_$driver
     * @since  0.4.0a1
     * @throws PEAR_PACKAGEUPDATE_ERROR_NONEXISTENTDRIVER
     */
    
function &factory($driver$packageName$channel)
    {
        
$class 'PEAR_PackageUpdate_' $driver;

        
// Attempt to include a custom version of the named class, but don't treat
        // a failure as fatal.  The caller may have already included their own
        // version of the named class.
        
if (!class_exists($class)) {

            
// Try to include the driver.
            
$file 'PEAR/PackageUpdate/' $driver '.php';

            if (!
PEAR_PackageUpdate::isIncludable($file)) {
                
PEAR_ErrorStack::staticPush('PEAR_PackageUpdate',
                                            
PEAR_PACKAGEUPDATE_ERROR_NONEXISTENTDRIVER,
                                            
null,
                                            array(
'drivername' => $driver),
                                            
$GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'][PEAR_PACKAGEUPDATE_ERROR_NONEXISTENTDRIVER]
                                            );
                
// Must assign a variable to avoid notice about references.
                
$false false;
                return 
$false;
            }
            include_once 
$file;
        }

        
// See if the class exists now.
        
if (!class_exists($class)) {
            
// Must assign a variable to avoid notice about references.
            
$false false;
            return 
$false;
        }

        
// Try to instantiate the class.
        
$instance =& new $class($packageName$channel);
        return 
$instance;
    }

    
/**
     * Returns whether or not a path is in the include path.
     *
     * @static
     * @access public
     * @param  string  $path
     * @return boolean true if the path is in the include path.
     * @since  0.4.2
     */
    
function isIncludable($path)
    {
        
// Break up the include path and check to see if the path is readable.
        
foreach (explode(PATH_SEPARATORget_include_path()) as $ip) {
            if (
file_exists($ip DIRECTORY_SEPARATOR $path) &&
                
is_readable($ip DIRECTORY_SEPARATOR $path)
                ) {
                return 
true;
            }
        }

        
// If we got down here, the path is not readable from the include path.
        
return false;
    }

    
/**
     * Loads the user's preferences from a file.
     *
     * The preferences are stored in the user's home directory
     * as the file .ppurc. The file contains a serialized array
     * of preferences for each package that has been checked for
     * updates so far.
     *
     * @access protected
     * @return boolean   true on success, false on error
     * @since  0.4.0a1
     * @throws PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS,
     *         PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED
     */
    
function loadPreferences()
    {
        
// Get the preferences file.
        
$prefFile $this->determinePrefFile();

        
// Make sure the prefFile exists.
        
if (!@file_exists($prefFile)) {
            
// Try to create it by saving the current preferences (probably
            // just an empty array).
            
$this->savePreferences();
        }

        
// Make sure the prefFile is readable.
        
if (!@is_readable($prefFile)) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_READACCESS,
                             
NULL, array('file' => $prefFile)
                             );
            return 
false;
        }

        
// Get the contents of the prefFile.
        
$contents file_get_contents($prefFile);

        
// Unserialize the data.
        
$preferences unserialize($contents);

        
// Make sure the contents were unserialized properly.
        // Borrowed from PEAR_Config.
        
if (!$preferences && strlen($contents) > 7) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_CORRUPTED);
            return 
false;
        }

        
$this->preferences $preferences;

        return 
true;
    }

    
/**
     * Returns the path to the preferences file.
     *
     * @access protected
     * @return string
     * @since  0.4.0a1
     */
    
function determinePrefFile()
    {
        
// Determine the preferences file.
        // Borrowed from PEAR_Config

        
$ds DIRECTORY_SEPARATOR;
        if (
OS_WINDOWS) {
            
$prefFile PEAR_CONFIG_SYSCONFDIR $ds 'ppurc.ini';
        } else {
            
$prefFile getenv('HOME') . $ds '.ppurc';
        }

        return 
$prefFile;
    }

    
/**
     * Checks to see if an update is available.
     *
     * Respects the user preferences when determining if an
     * update is avaiable. Returns true if an update is available
     * and the user may want to update the package.
     *
     * @access public
     * @return boolean true if an update is available.
     * @since  0.4.0a1
     */
    
function checkUpdate()
    {
        
// Check to see if an update is available.
        
if (empty($this->latestVersion) || empty ($this->info)) {
            if (!
$this->getPackageInfo()) {
                return 
false;
            }
        }

        
// See if the installed version is older than the current version.
        
if (version_compare($this->latestVersion$this->instVersion'>')) {
            
// Check to see if the user's preferences allow an update.
            
if (!$this->preferencesAllowUpdate()) {
                
// User doesn't want to update to the latest version.
                
return false;
            }
            return 
true;
        } else {
            return 
false;
        }
    }

    
/**
     * Returns the latest information about the given package.
     *
     * @access protected
     * @return boolean   true on success, false on error
     * @since  0.4.0a1
     * @throws PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE,
     *         PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL,
     *         PEAR_PACKAGEUPDATE_ERROR_NOINFO
     */
    
function getPackageInfo()
    {
        
// Only check once.
        
if (isset($this->latestVersion) && isset($this->info)) {
            return 
true;
        }

        
// Make sure the channel and package are set.
        
if (empty($this->packageName)) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOPACKAGE);
            return 
false;
        }

        if (empty(
$this->channel)) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOCHANNEL);
            return 
false;
        }

        
// Create a config object.
        
$config = new PEAR_Config();

        
// Get the config's registry object.
        
$reg $config->getRegistry();

        
// Parse the package name.
        
$parsed $reg->parsePackageName($this->channel '/' $this->packageName);

        
// Check for errors.
        
if (PEAR::isError($parsed)) {
            
$this->pushError($parsed);
            return 
false;
        }

        
// Get a PEAR_Remote instance.
        
$r $config->getRemote();

        
// Get the package info.
        
$info $r->call('package.info'$parsed['package']);

        
// Check to make sure the package was found.
        
if (PEAR::isError($info)) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOINFONULL,
                             array(
'packagename' => $this->packageName)
                             );
            return 
false;
        }

        
// Get the installed version of the package.
        
$this->instVersion $reg->packageInfo($parsed['package'],
                                               
'version',
                                               
$parsed['channel']
                                               );

        
// If the package is not installed, create a dummy version.
        
if (empty($this->instVersion)) {
            
$this->instVersion '0.0.0';
        }

        
// Pull out the latest information.
        
$this->latestVersion reset(array_keys($info['releases']));
        
$this->info          reset($info['releases']);

        return 
true;
    }

    
/**
     * Returns the preferences associated with the given package.
     *
     * The preferences returned are an array with the folling values:
     * - don't ask again
     * - don't ask until next version
     * - only ask for state x
     * - bug/minor/major updates only
     *
     * @access public
     * @return array
     * @since  0.4.0a1
     */
    
function getPackagePreferences()
    {
        if (isset(
$this->preferences[$this->channel][$this->packageName])) {
            return 
$this->preferences[$this->channel][$this->packageName];
        } else {
            return array();
        }
    }

    
/**
     * Saves the current prefernces to the RC file.
     *
     * @access public
     * @return boolean true on success, false on error
     * @since  0.4.0a1
     * @throws PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS,
     *         PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR
     */
    
function savePreferences()
    {
        
// Get the file to save the preferences to.
        
$prefFile $this->determinePrefFile();

        
// Open the file for writing.
        
$fp fopen($prefFile'w');
        if (
$fp === false) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEACCESS,
                             
NULL, array('file' => $prefFile)
                             );
            return 
false;
        }

        
// Serialize the contents.
        
$serialCont serialize($this->preferences);

        
// Write the contents to the file.
        
if (fwrite($fp$serialCont) === false) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR,
                             
NULL, array('file' => $prefFile)
                             );
            return 
false;
        }

        
// Close the file.
        
if (!fclose($fp)) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_PREFFILE_WRITEERROR,
                             
NULL, array('file' => $prefFile)
                             );
            return 
false;
        } else {
            return 
true;
        }
    }

    
/**
     * Returns whether or not the user's preferences will allow an update to
     * take place.
     *
     * The user's preferences may define restrictions such as:
     *   - don't update
     *   - don't ask until next version (remembers last version asked)
     *   - only ask for state XXXX or higher
     *   - minor or higher (no bug fix)
     *   - major only
     *
     * @access public
     * @return boolean true if the preferences will allow an update for the
     *                 latest version.
     * @since  0.4.0a1
     */
    
function preferencesAllowUpdate()
    {
        
// Get the preferences for the package.
        
$prefs $this->getPackagePreferences();

        
// Check to see if the user wants to be notified about any updates for
        // this package.
        
if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_NOUPDATES]) &&
            
$prefs[PEAR_PACKAGEUPDATE_PREF_NOUPDATES]
            ) {
            return 
false;
        }

        
// Check to see if the user has requested not to be asked until a new
        // version is released.
        
if (isset($prefs[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE]) &&
            !
version_compare($this->latestVersion,
                             
$prefs[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE],
                             
'>')
            ) {
            return 
false;
        }

        
// Check to see if the user has requested not to be asked about the
        // state of the latest release.
        // Create an array of states.
        
$states = array(PEAR_PACKAGEUPDATE_STATE_DEVEL  => 0,
                        
PEAR_PACKAGEUPDATE_STATE_ALPHA  => 1,
                        
PEAR_PACKAGEUPDATE_STATE_BETA   => 2,
                        
PEAR_PACKAGEUPDATE_STATE_STABLE => 3
                        
);
        if (isset(
$prefs[PEAR_PACKAGEUPDATE_PREF_STATE]) &&
            
$states[$prefs[PEAR_PACKAGEUPDATE_PREF_STATE]] > $states[$this->info['state']]
            ) {
            return 
false;
        }

        
// Check to see if the user only wants to be asked about a certain
        // type of release (minor|major).
        // Create an array for the types of releases.
        
$releases = array(PEAR_PACKAGEUPDATE_TYPE_BUG   => 0,
                          
PEAR_PACKAGEUPDATE_TYPE_MINOR => 1,
                          
PEAR_PACKAGEUPDATE_TYPE_MAJOR => 2
                          
);
        if (isset(
$prefs[PEAR_PACKAGEUPDATE_PREF_TYPE]) &&
            
$releases[$prefs[PEAR_PACKAGEUPDATE_PREF_TYPE]] > $releases[$this->releaseType()]
            ) {
            return 
false;
        }

        
// If we got down here, either there are no preferences for this
        // package or everything checked out.
        
return true;
    }

    
/**
     * Returns the type of release. (bug|minor|major);
     *
     * @access protected
     * @return string
     * @since  0.4.0a1
     */
    
function releaseType()
    {
        
// Break the two versions into pieces.
        
$latest  explode('.'$this->latestVersion);
        
$current explode('.'$this->instVersion);

        if (
$latest[0] > $current[0]) {
            
$type PEAR_PACKAGEUPDATE_TYPE_MAJOR;
        } elseif (
$latest[1] > $current[1]) {
            
$type PEAR_PACKAGEUPDATE_TYPE_MINOR;
        } else {
            
$type PEAR_PACKAGEUPDATE_TYPE_BUG;
        }

        return 
$type;
    }

    
/**
     * Sets the user's preference for asking about all updates for this
     * package.
     *
     * @access public
     * @param  boolean $dontAsk
     * @return boolean true on success, false on failure
     * @since  0.4.0a1
     */
    
function setDontAskAgain($dontAsk)
    {
        
// Make sure the value is a boolean.
        
settype($dontAsk'boolean');

        
// Set the preference.
        
return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_NOUPDATES$dontAsk);
    }

    
/**
     * Sets the user's preference for asking about updates again until the next
     * release.
     *
     * @access public
     * @param  boolean $nextrelease
     * @return boolean true on success, false on failure
     * @since  0.4.0a1
     * @throws PEAR_PACKAGEUPDATE_ERROR_NOINFO
     */
    
function setDontAskUntilNextRelease($nextrelease)
    {
        
// If nextrelease is true, we have to swap its value out with the
        // latest version.
        
if ($nextrelease) {
            
// Make sure that the package info was found.
            
if (empty($this->latestVersion)) {
                
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOINFO);
                return 
false;
            }
            
$nextrelease $this->latestVersion;
        }

        
// Set the preference.
        
return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE,
                                    
$nextrelease);
    }

    
/**
     * Sets the user's preference for asking about release types.
     *
     * @access public
     * @param  string  $minType The minimum release type to allow.
     * @return boolean true on success, false on failure
     * @since  0.4.0a1
     * @throws PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPE
     */
    
function setMinimumReleaseType($minType)
    {
        
// Make sure the type is acceptable.
        
if ($minType != PEAR_PACKAGEUPDATE_TYPE_BUG   &&
            
$minType != PEAR_PACKAGEUPDATE_TYPE_MINOR &&
            
$minType != PEAR_PACKAGEUPDATE_TYPE_MAJOR
            
) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_INVALIDTYPENULL,
                             array(
'type' => $minType)
                             );
            return 
false;
        }

        
// Set the preference.
        
return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_TYPE$minType);
    }

    
/**
     * Sets the user's preference for asking about release states.
     *
     * @access public
     * @param  string  $minState The minimum release state to allow.
     * @return boolean true on success, false on failure
     * @since  0.4.0a1
     * @throws PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATE
     */
    
function setMinimumState($minState)
    {
        
// Make sure the type is acceptable.
        
if ($minState != PEAR_PACKAGEUPDATE_STATE_DEVEL  &&
            
$minState != PEAR_PACKAGEUPDATE_STATE_ALPHA  &&
            
$minState != PEAR_PACKAGEUPDATE_STATE_BETA   &&
            
$minState != PEAR_PACKAGEUPDATE_STATE_STABLE
            
) {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_INVALIDSTATENULL,
                             array(
'state' => $minState)
                             );
            return 
false;
        }

        
// Set the preference.
        
return $this->setPreference(PEAR_PACKAGEUPDATE_PREF_STATE$minState);
    }

    
/**
     * Sets the given preference to the given value.
     *
     * Don't take any chances. Anytime a preference is set, the preferences are
     * saved. We can't rely on the developer to call savePreferences.
     *
     * @access protected
     * @param  integer   $pref  One of the preference constants.
     * @param  mixed     $value The value of the preference.
     * @return boolean   true if the preference was set and saved properly.
     * @since  0.4.0a1
     * @throws PEAR_PACKAGEUPDATE_ERROR_INVALIDPREF
     */
    
function setPreference($pref$value)
    {
        
// Make sure the preference is valid.
        
if ($pref != PEAR_PACKAGEUPDATE_PREF_NOUPDATES   &&
            
$pref != PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE &&
            
$pref != PEAR_PACKAGEUPDATE_PREF_TYPE        &&
            
$pref != PEAR_PACKAGEUPDATE_PREF_STATE
            
) {
            
// Invalid preference!
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_INVALIDPREFNULL,
                             array(
'preference' => $pref)
                             );
            return 
false;
        }

        
// Make sure the preferences for the channel exist.
        
if (!isset($this->preferences[$this->channel])) {
            
$this->preferences[$this->channel] = array();
        }

        
// Make sure the preferences for the package exist.
        
if (!isset($this->preferences[$this->channel][$this->packageName])) {
            
$this->preferences[$this->channel][$this->packageName] = array();
        }

        
// Set the preference value.
        
$this->preferences[$this->channel][$this->packageName][$pref] = $value;

        
// Save the preferences.
        
return $this->savePreferences();
    }

    
/**
     * Sets all preferences at once.
     *
     * @access public
     * @param  array   $preferences
     * @return boolean true if the preferences were set and saved.
     * @since  0.4.0a1
     */
    
function setPreferences($preferences)
    {
        
// Make sure preferences is an array.
        
settype($preferences'array');

        
// Make sure there is only valid preference information.
        
foreach ($preferences as $pref => $value) {
            if (
$pref != PEAR_PACKAGEUPDATE_PREF_NOUPDATES   &&
                
$pref != PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE &&
                
$pref != PEAR_PACKAGEUPDATE_PREF_TYPE        &&
                
$pref != PEAR_PACKAGEUPDATE_PREF_STATE
                
) {
                unset(
$preferences[$pref]);
            }
        }

        
// Make sure that next release has the latest release.
        
if ($preferences[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE]) {
            
$preferences[PEAR_PACKAGEUPDATE_PREF_NEXTRELEASE] = $this->latestVersion;
        }

        
// Set the preferences.
        
$this->preferences[$this->channel][$this->packageName] = $preferences;

        
// Save the preferences.
        
return $this->savePreferences();
    }

    
/**
     * Updates the source for the package.
     *
     * We have to update required dependencies automatically to make sure that
     * everything still works properly.
     *
     * It is the developers responsibility to make sure the user is given the
     * option to update any optional dependencies if needed. This can be done
     * by creating a new instance of PEAR_PackageUpdate for the optional
     * dependency.
     *
     * @access public
     * @return boolean true if the update was successful.
     * @since  0.4.0a1
     * @throws PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLED
     */
    
function update()
    {
        
// Create a config object.
        
$config  = new PEAR_Config();

        
// Change the verbosity but keep track of the value to reset it just in
        // case this does something permanent.
        
$verbose $config->get('verbose');
        
$config->set('verbose'0);

        
// Create a command object to do the upgrade.
        // If the current version is 0.0.0 don't upgrade. That would be a
        // sneaky way for devs to install packages without the use knowing.
        
if ($this->instVersion == '0.0.0') {
            
$this->pushError(PEAR_PACKAGEUPDATE_ERROR_NOTINSTALLEDNULL,
                             array(
'packagename' => $this->packageName)
                             );
            return 
false;
        }

        require_once 
'PEAR/Command.php';
        
$upgrade PEAR_Command::factory('upgrade'$config);

        
// Try to upgrade the application.
        
$channelPackage $this->channel '/' $this->packageName;
        
$result $upgrade->doInstall('upgrade',
                                      array(
'onlyreqdeps' => true),
                                      array(
$channelPackage));

        
// Reset the verbose level just to be safe.
        
$config->set('verbose'$verbose);

        
// Check for errors.
        
if (PEAR::isError($result)) {
            
$this->pushError($result);
            return 
false;
        } else {
            return 
true;
        }
    }

    
/**
     * Redirects or exits to force the user to restart the application.
     *
     * @abstract
     * @access public
     * @return void
     * @since  0.4.0a1
     */
    
function forceRestart()
    {
        
$this->pushError('forceRestart is an abstract method that must be' .
                         
' overridden in the child class.');
    }

    
/**
     * Presents the user with the option to update.
     *
     * @abstract
     * @access public
     * @return boolean true if the user wants to update
     * @since  0.4.0a1
     */
    
function presentUpdate()
    {
        
$this->pushError('presentUpdate is an abstract method that must be' .
                         
' overridden in the child class.');

        
// Return false just in case something odd has happened.
        
return false;
    }

    
/**
     * Pushes an error onto an error stack.
     *
     * This method is just for collecting errors that occur while checking for
     * updates and updating a package. The child class is responsible for
     * displaying all errors and handling them properly. This is because the
     * way errors are handled varies greatly depending on the driver used.
     *
     * @access public
     * @param int    $code      Package-specific error code
     * @param string $level     Error level.  This is NOT spell-checked
     * @param array  $params    associative array of error parameters
     * @param string $msg       Error message, or a portion of it if the
     *                          message is to be generated
     * @param array  $repackage If this error re-packages an error pushed by
     *                          another package, place the array returned from
     *                          {@link pop()} in this parameter
     * @param array  $backtrace Protected parameter: use this to pass in the
     *                          {@link debug_backtrace()} that should be used
     *                          to find error context
     * @return PEAR_Error|array|Exception
     *                          if compatibility mode is on, a PEAR_Error is
     *                          also thrown.  If the class Exception exists,
     *                          then one is returned.
     * @since  0.4.0a1
     */
    
function pushError($code$level 'error'$params = array(),
                        
$msg false$repackage false$backtrace false)
    {
        
// Check to see if a PEAR_Error was pushed.
        
if (PEAR::isError($code)) {
            return 
$this->repackagePEAR_Error($code);
        }

        
// Check the arguments to see if just a code was submitted.
        
if (is_int($code) &&
            !(bool) 
$msg &&
            isset(
$GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'][$code])
            ) {
            
$msg $GLOBALS['_PEAR_PACKAGEUPDATE_ERRORS'][$code];
        }

        
// Append the error onto the stack.
        
return $this->errors->push($codenull$params$msg,
                                   
$repackage$backtrace
                                   
);
    }

    
/**
     * Repackages PEAR_Errors for use with ErrorStack.
     *
     * @author Ian Eure
     *
     * @access public
     * @param  object $error A PEAR_Error
     * @return mixed  The return from PEAR::ErrorStack::push()
     * @since  0.4.0a1
     */
    
function repackagePEAR_Error(&$error)
    {
        static 
$map;
        if (!isset(
$map)) {
            
$map = array(
                         
E_ERROR            => 'error',
                         
E_WARNING          => 'warning',
                         
E_PARSE            => 'exception',
                         
E_NOTICE           => 'notice',
                         
E_CORE_ERROR       => 'error',
                         
E_CORE_WARNING     => 'warning',
                         
E_COMPILE_ERROR    => 'exception',
                         
E_COMPILE_WARNING  => 'warning',
                         
E_USER_ERROR       => 'error',
                         
E_USER_WARNING     => 'warning',
                         
E_USER_NOTICE      => 'notice'
                         
);
        }

        
// Strip this function from the trace
        
if (is_array($error->backtrace)) {
            
array_shift($error->backtrace);
            
$error->userinfo['backtrace'] =& $error->backtrace;
        }

        return 
$this->errors->push($error->code$map[$error->level],
                                   
$error->userinfo$error->messagefalse,
                                   
$error->backtrace
                                   
);
    }

    
/**
     * Pops an error off the error stack.
     *
     * This method is just for collecting errors that occur while checking for
     * updates and updating a package. The child class is responsible for
     * displaying all errors and handling them properly. This is because the
     * way errors are handled varies greatly depending on the driver used.
     *
     * @access public
     * @return object A PEAR_Error instance or false if no errors exist.
     * @since  0.4.0a1
     */
    
function popError()
    {
        return 
$this->errors->pop();
    }

    
/**
     * Returns whether or not errors have occurred (and been captured).
     *
     * @access public
     * @return boolean
     * @since  0.4.0a1
     */
    
function hasErrors()
    {
        return 
$this->errors->hasErrors();
    }
}
/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */
?>
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586