1 | <?php |
||
23 | Class AutoUpdate { |
||
24 | |||
25 | /** |
||
26 | * @var Http |
||
27 | */ |
||
28 | protected $http; |
||
29 | protected $repository; |
||
30 | protected $logfile; |
||
31 | protected $lastused; |
||
32 | protected $commits; |
||
33 | |||
34 | function __construct( $http ) { |
||
35 | global $pgIP, $pgExperimentalupdates; |
||
36 | $this->http = $http; |
||
37 | $this->repository = ( $pgExperimentalupdates ? 'master' : 'stable' ); |
||
38 | $this->logfile = ( $pgExperimentalupdates ? 'Update.log' : 'StableUpdate.log' ); |
||
39 | $this->lastused = ( file_exists( $pgIP . 'Includes/updateversion' ) ? unserialize( file_get_contents( $pgIP . 'Includes/updateversion' ) ) : 'Unknown' ); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Scans the GitHub repository for any updates and returns false if there are. |
||
44 | * |
||
45 | * @access public |
||
46 | * @return bool |
||
47 | */ |
||
48 | public function Checkforupdate() { |
||
49 | global $pgIP, $pgExperimentalupdates; |
||
50 | pecho( "Checking for updates...\n\n", PECHO_NORMAL ); |
||
51 | if( $pgExperimentalupdates ) pecho( "Warning: You have experimental updates switched on.\nExperimental updates are not fully tested and can cause problems,\nsuch as, bot misbehaviors up to complete crashes.\nUse at your own risk.\nPeachy will not revert back to a stable release until switched off.\n\n", PECHO_NOTICE ); |
||
52 | $data = json_decode( $this->http->get( 'https://api.github.com/repos/MW-Peachy/Peachy/branches/' . $this->repository, null, array(), false ), true ); |
||
53 | $this->commits = $data; |
||
54 | /*if( strstr( $this->http->getLastHeader(), 'Status: 304 Not Modified') ) { |
||
55 | pecho( "Peachy is up to date.\n\n", PECHO_NORMAL ); |
||
56 | return true; |
||
57 | }*/ |
||
58 | if( is_array( $data ) && array_key_exists( 'message', $data ) && strpos( $data['message'], 'API rate limit exceeded' ) === 0 ) { |
||
59 | pecho( "Cant check for updates right now, next window in " . $this->getTimeToNextLimitWindow() . "\n\n", PECHO_NOTICE ); |
||
60 | return true; |
||
61 | } |
||
62 | $this->cacheLastGithubETag(); |
||
63 | if( $this->lastused !== $this->repository ) { |
||
64 | pecho( "Changing Peachy version to run using " . ( $pgExperimentalupdates ? "experimental" : "stable" ) . " updates.\n\n", PECHO_NOTICE ); |
||
65 | return false; |
||
66 | } |
||
67 | if( file_exists( $pgIP . 'Includes' . DIRECTORY_SEPARATOR . $this->logfile ) ) { |
||
68 | $log = unserialize( file_get_contents( $pgIP . 'Includes' . DIRECTORY_SEPARATOR . $this->logfile ) ); |
||
69 | if( isset( $data['commit']['sha'] ) && $log['commit']['sha'] != $data['commit']['sha'] ) { |
||
70 | pecho( "Update available!\n\n", PECHO_NOTICE ); |
||
71 | return false; |
||
72 | } else { |
||
73 | pecho( "Peachy is up to date.\n\n", PECHO_NORMAL ); |
||
74 | return true; |
||
75 | } |
||
76 | } else { |
||
77 | pecho( "No update log found.\n\n", PECHO_WARN ); |
||
78 | return false; |
||
79 | } |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * Caches the last Etag from github in a tmp file |
||
84 | */ |
||
85 | private function cacheLastGithubETag() { |
||
86 | global $pgIP; |
||
87 | if( preg_match( '/ETag\: \"([^\"]*)\"/', $this->http->getLastHeader(), $matches ) ) { |
||
88 | file_put_contents( $pgIP . 'tmp' . DIRECTORY_SEPARATOR . 'github-ETag.tmp', $matches[1] ); |
||
89 | } |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @return string representing time to next github api request window |
||
94 | */ |
||
95 | private function getTimeToNextLimitWindow() { |
||
101 | |||
102 | private function getLocalPath( $fullUpdatePath ) { |
||
103 | global $pgIP; |
||
104 | $xplodesAt = DIRECTORY_SEPARATOR . 'gitUpdate' . DIRECTORY_SEPARATOR . 'Peachy-' . $this->repository . DIRECTORY_SEPARATOR; |
||
108 | |||
109 | /** |
||
110 | * Updates the Peachy framework |
||
111 | * |
||
112 | * @access public |
||
113 | * @return boolean|null |
||
114 | */ |
||
115 | public function updatePeachy() { |
||
144 | |||
145 | /** |
||
146 | * @param string $gitFolder |
||
147 | */ |
||
148 | private function copyOverGitFiles( $gitFolder ) { |
||
165 | |||
166 | /** |
||
167 | * recursively remove a directory |
||
168 | * @param string $dir |
||
169 | */ |
||
170 | private function rrmdir( $dir ) { |
||
182 | } |
||
183 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.