| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @package Backup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @author Iurii Makukh <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace gplcart\modules\backup; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use gplcart\core\Config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     gplcart\core\Container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use gplcart\core\models\Language as LanguageModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Main class for Backup module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | class Backup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Database class instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var \gplcart\core\Database $db | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param Config $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct(Config $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->db = $config->getDb(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->db->addScheme($this->getDbScheme()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Implements hook "module.install.before" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param null|string $result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function hookModuleInstallBefore(&$result) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         if (!class_exists('ZipArchive')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $result = $this->getTranslationModel()->text('Class ZipArchive does not exist'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $result_db = $this->db->importScheme('backup', $this->getDbScheme()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             if ($result_db !== true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                 $result = $result_db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * Implements hook "module.uninstall.after" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function hookModuleUninstallAfter() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->db->deleteTable('backup'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Implements hook "user.role.permissions" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param array $permissions | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     public function hookUserRolePermissions(array &$permissions) | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $permissions['backup'] = /* @text */'Backup: access'; | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $permissions['backup_delete'] = /* @text */'Backup: delete'; | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $permissions['backup_download'] = /* @text */'Backup: download'; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * Implements hook "route.list" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param array $routes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function hookRouteList(array &$routes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $routes['admin/report/backup'] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'access' => 'backup', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'menu' => array('admin' => /* @text */'Backups'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'handlers' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 'controller' => array('gplcart\\modules\\backup\\controllers\\Backup', 'listBackup') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Performs backup operation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param string $handler_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @return boolean|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function backup($handler_id, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         return $this->getModel()->backup($handler_id, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * Performs restore operation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param string $handler_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return boolean|string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function restore($handler_id, array $data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         return $this->getModel()->restore($handler_id, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Returns an array of defined handlers | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     public function getHandlers() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return $this->getModel()->getHandlers(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * Whether a backup already exists | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param string $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param null|string $version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     public function exists($id, $version = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return $this->getModel()->exists($id, $version); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * Returns backup model | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @return \gplcart\modules\backup\models\Backup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     protected function getModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         return Container::get('gplcart\\modules\\backup\\models\\Backup'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * Translation UI model instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @return \gplcart\core\models\Translation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     protected function getTranslationModel() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         return Container::get('gplcart\\core\\models\\Translation'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * Returns an array of database scheme | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     protected function getDbScheme() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             'backup' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 'fields' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                     'backup_id' => array('type' => 'int', 'length' => 10, 'auto_increment' => true, 'primary' => true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                     'created' => array('type' => 'int', 'length' => 10, 'not_null' => true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                     'name' => array('type' => 'varchar', 'length' => 255, 'not_null' => true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                     'path' => array('type' => 'varchar', 'length' => 255, 'not_null' => true), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |                     'user_id' => array('type' => 'int', 'length' => 10, 'not_null' => true, 'default' => 0), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                     'type' => array('type' => 'varchar', 'length' => 50, 'not_null' => true, 'default' => ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                     'version' => array('type' => 'varchar', 'length' => 50, 'not_null' => true, 'default' => ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                     'id' => array('type' => 'varchar', 'length' => 50, 'not_null' => true, 'default' => '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 171 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |  |