hoolux /system/expressionengine/libraries/File_integrity.php

Language PHP Lines 247
MD5 Hash 914c3d3d4c808cffff2407a803a15274 Estimated Cost $3,454 (why?)
Repository https://bitbucket.org/tdevonshire/hoolux.git View Raw File View Project SPDX
  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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * ExpressionEngine - by EllisLab
 *
 * @package		ExpressionEngine
 * @author		EllisLab Dev Team
 * @copyright	Copyright (c) 2003 - 2012, EllisLab, Inc.
 * @license		http://ellislab.com/expressionengine/user-guide/license.html
 * @link		http://ellislab.com
 * @since		Version 2.0
 * @filesource
 */
 
// ------------------------------------------------------------------------

/**
 * ExpressionEngine Core File Integrity Class
 *
 * @package		ExpressionEngine
 * @subpackage	Core
 * @category	Core
 * @author		EllisLab Dev Team
 * @link		http://ellislab.com
 */
class File_integrity {

	var $emailed = array();
	var $checksums = array();

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		$this->EE =& get_instance();
	}
	
	// --------------------------------------------------------------------

	/**
	 * Check all bootstrap files
	 *
	 * @access	public
	 */
	function check_bootstrap_files($return_site_id = FALSE)
	{
		$this->EE->load->model('site_model');
		$sites = $this->EE->site_model->get_site();
		$sites = $sites->result_array();
		
		$bootstraps = array();
		
		// Retrieve all of the bootstrap files
		foreach($sites as $site)
		{
			if ( ! isset($site['site_bootstrap_checksums']))
			{
				continue;
			}
			
			$data = base64_decode($site['site_bootstrap_checksums']);
			
			if ( ! is_string($data) OR substr($data, 0, 2) != 'a:')
			{
				continue;
			}
			
			$data = unserialize($data);

			if ( ! isset($data['emailed']) OR ! is_array($data['emailed']))
			{
				$data['emailed'] = array();
			}

			$bootstraps[$site['site_id']] = $data;
		}
		
		
		$altered = array();
		$removed = array();
		$update_db = array();

		// Check them all
		foreach($bootstraps as $site_id => $checksums)
		{
			foreach($checksums as $path => $checksum)
			{
				if ($path == 'emailed')
				{
					continue;
				}
				
				if ( ! file_exists($path))
				{
					$removed[$site_id][] = $path;
				}
				else
				{
					$this->checksums[$site_id][$path] = $checksum;
					
					$current = md5_file($path);

					if ($current != $checksum)
					{
						if ($return_site_id)
						{
							$altered[$site_id][] = $path;
						}
						else
						{
							$altered[] = $path;
						}
					}
					elseif (($email_key = array_search($path, $checksums['emailed'])) !== FALSE)
					{
						// they were emailed about it and restored it without
						// hitting the 'accept changes' link on the homepage
						
						unset($bootstraps[$site_id]['emailed'][$email_key]);
						$update_db[] = $site_id;
					}
				}
			}
			
			$this->emailed = array_unique(array_merge($this->emailed, $bootstraps[$site_id]['emailed']));
		}
		
		// Remove obsolete files from the db
		foreach($removed as $site_id => $paths)
		{
			foreach($paths as $path)
			{
				if (isset($bootstraps[$site_id][$path]))
				{
					unset($bootstraps[$site_id][$path]);
					$update_db[] = $site_id;
				}
			}
		}
		
		// Update the db if we detected changes
		foreach(array_unique($update_db) as $site_id)
		{
			$this->_update_config($bootstraps[$site_id], $site_id);
		}

		// Any changes? report them
		if (count($altered))
		{
			return $altered;
		}

		return FALSE;
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Add a bootstrap file we didn't know about, or explicitly update
	 * a checksum (when accepting a change).
	 *
	 * @access	public
	 */
	function send_site_admin_warning($altered)
	{
		$affected_paths = array_diff($altered, $this->emailed);

		if (count($affected_paths))
		{
			$this->EE->load->library('notifications');
			$this->EE->notifications->send_checksum_notification($affected_paths);
			
			// add them to the existing emailed and update
			$affected_paths = array_unique(array_merge($this->emailed, $affected_paths));

			foreach($this->checksums as $site_id => $checksums)
			{
				$checksum_paths = array_keys($checksums);

				// did we send emails for this site id?
				$old_emailed = array_intersect($checksum_paths, $this->emailed);
				$new_emailed = array_intersect($checksum_paths, $affected_paths);

				if (count($new_emailed))
				{
					$checksums['emailed'] = array_unique(array_merge($old_emailed, $new_emailed));
					$this->_update_config($checksums, $site_id);
				}
			}
		}
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Add a bootstrap file we didn't know about, or explicitly update
	 * a checksum (when accepting a change).
	 *
	 * @access	public
	 */
	function create_bootstrap_checksum($path = '', $site_id = '')
	{
		$checksums	= $this->EE->config->item('site_bootstrap_checksums');
		$site_id	= ($site_id != '') ? $site_id : $this->EE->config->item('site_id');
		
		if (REQ == 'CP' && $path && file_exists($path))
		{
			$checksums				= $this->checksums[$site_id];		// should already have called check_bootstrap_files
			$checksums[$path]		= md5_file($path);
			$checksums['emailed']	= array();
			
			$this->_update_config($checksums, $site_id);
		}
		elseif (REQ != 'CP' && ! array_key_exists(FCPATH.SELF, $checksums))
		{
			$checksums[FCPATH.SELF] = md5_file(FCPATH.SELF);
			
			$this->_update_config($checksums, $site_id);
		}
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Update the bootstrap column in the db
	 *
	 * @access	private
	 */
	function _update_config($checksums, $site_id)
	{
		if ($site_id == $this->EE->config->item('site_id'))
		{
			$this->EE->config->config['site_bootstrap_checksums'] = $checksums;
		}

		$this->EE->db->query($this->EE->db->update_string('exp_sites', 
									  array('site_bootstrap_checksums' => base64_encode(serialize($checksums))),
									  "site_id = '".$this->EE->db->escape_str($site_id)."'"));
	}
}

// END File_integrity class

/* End of file File_integrity.php */
/* Location: ./system/expressionengine/libraries/File_integrity.php */
Back to Top