| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Database Optimize & Repair Tool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright (c) 2013, 2019 Matt Friedman | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license GNU General Public License, version 2 (GPL-2.0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace vse\dbtool\tool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use phpbb\cache\driver\driver_interface as cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use phpbb\config\config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use phpbb\db\driver\driver_interface as db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use phpbb\log\log_interface as log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use phpbb\user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use vse\dbtool\ext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class tool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	/** @var \phpbb\cache\driver\driver_interface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	protected $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	/** @var \phpbb\config\config */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	protected $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	/** @var \phpbb\db\driver\driver_interface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	protected $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** @var log */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	protected $log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	/** @var \phpbb\user */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	protected $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * tool constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * @param \phpbb\cache\driver\driver_interface $cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @param \phpbb\config\config                 $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 * @param \phpbb\db\driver\driver_interface    $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	 * @param \phpbb\log\log_interface             $log | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	 * @param \phpbb\user                          $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	public function __construct(cache $cache, config $config, db $db, log $log, user $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 		$this->cache   = $cache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 		$this->config  = $config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 		$this->db      = $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		$this->log     = $log; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		$this->user    = $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * Perform table SQL query and return any messages | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * @param string $operation     OPTIMIZE, REPAIR, or CHECK | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * @param array  $tables        Array of all tables to be processed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * @param int    $disable_board The users option to disable the board during run time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 * @return array Any errors or status information | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	public function run($operation, $tables, $disable_board = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 		$this->extend_execution_limits(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$tablestr = implode(', ', $tables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$disabled = $this->disable_board($disable_board, $this->config->offsetGet('board_disable')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		$output = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 		$result = $this->db->sql_query($operation . ' TABLE ' . $this->db->sql_escape($tablestr)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		while ($row = $this->db->sql_fetchrow($result)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 			// Build a message only for optimize/repair errors, or if check table is run | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 			if ($operation === ext::CHECK || in_array(strtolower($row['Msg_type']), ['error', 'info', 'note', 'warning'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 			{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 				$output[] = substr($row['Table'], strpos($row['Table'], '.') + 1) . ' ... ' . $row['Msg_type'] . ': ' . $row['Msg_text']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 		$this->db->sql_freeresult($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $operation . '_LOG', time(), [$tablestr]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$this->disable_board($disable_board, $disabled); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		// Clear cache to ensure board is re-enabled for all users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		$this->cache->purge(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 		return $output ?: $tables; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * Set disable board config state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * @param int $disable  The users option to disable the board during run time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * @param int $disabled The current disabled state | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @return int The original disabled state of the board | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 * @access public | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  | 	public function disable_board($disable, $disabled) | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  | 	{ | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  | 		if ($disable && !$disabled) | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  | 		{ | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  | 			$this->config->set('board_disable', !$this->config->offsetGet('board_disable')); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  | 		return $disabled; | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * Is the database using MySQL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * @return bool True if MySQL, false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	public function is_mysql() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return $this->db->get_sql_layer() === 'mysql4' || $this->db->get_sql_layer() === 'mysqli'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * Is requested operation to optimize, repair or check tables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * @param string $operation The name of the operation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * @return bool True if valid operation, false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	public function is_valid_operation($operation) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		return in_array($operation, [ext::OPTIMIZE, ext::REPAIR, ext::CHECK]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * Only allow tables using MyISAM, InnoDB or Archive storage engines | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 * @param string $engine The name of the engine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 * @return bool True if valid engine, false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 	public function is_valid_engine($engine) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		return in_array(strtolower($engine), ['myisam', 'innodb', 'archive']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	 * Is the storage engine InnoDB | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 * @param string $engine The name of the engine | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * @return bool True if InnoDB engine, false otherwise | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 	 * @access public | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	public function is_innodb($engine) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		return strtolower($engine) === 'innodb'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * Extend execution limits to mitigate timeouts | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	 * @access protected | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	protected function extend_execution_limits() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 		@set_time_limit(1200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 		@set_time_limit(0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |  |